Flutter Impeller
color_source_contents.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include "flutter/fml/macros.h"
11 #include "impeller/geometry/path.h"
12 
13 namespace impeller {
14 
15 //------------------------------------------------------------------------------
16 /// Color sources are geometry-ignostic `Contents` capable of shading any area
17 /// defined by an `impeller::Geometry`. Conceptually,
18 /// `impeller::ColorSourceContents` implement a particular color shading
19 /// behavior.
20 ///
21 /// This separation of concerns between geometry and color source output allows
22 /// Impeller to handle most possible draw combinations in a consistent way.
23 /// For example: There are color sources for handling solid colors, gradients,
24 /// textures, custom runtime effects, and even 3D scenes.
25 ///
26 /// There are some special rendering exceptions that deviate from this pattern
27 /// and cross geometry and color source concerns, such as text atlas and image
28 /// atlas rendering. Special `Contents` exist for rendering these behaviors
29 /// which don't implement `ColorSourceContents`.
30 ///
31 /// @see `impeller::Geometry`
32 ///
33 class ColorSourceContents : public Contents {
34  public:
36 
37  ~ColorSourceContents() override;
38 
39  //----------------------------------------------------------------------------
40  /// @brief Set the geometry that this contents will use to render.
41  ///
42  void SetGeometry(std::shared_ptr<Geometry> geometry);
43 
44  //----------------------------------------------------------------------------
45  /// @brief Get the geometry that this contents will use to render.
46  ///
47  const std::shared_ptr<Geometry>& GetGeometry() const;
48 
49  //----------------------------------------------------------------------------
50  /// @brief Set the effect transform for this color source.
51  ///
52  /// The effect transform is a transformation matrix that is applied to
53  /// the shaded color output and does not impact geometry in any way.
54  ///
55  /// For example: With repeat tiling, any gradient or
56  /// `TiledTextureContents` could be used with an effect transform to
57  /// inexpensively draw an infinite scrolling background pattern.
58  ///
59  void SetEffectTransform(Matrix matrix);
60 
61  //----------------------------------------------------------------------------
62  /// @brief Set the inverted effect transform for this color source.
63  ///
64  /// When the effect transform is set via `SetEffectTransform`, the
65  /// value is inverted upon storage. The reason for this is that most
66  /// color sources internally use the inverted transform.
67  ///
68  /// @return The inverse of the transform set by `SetEffectTransform`.
69  ///
70  /// @see `SetEffectTransform`
71  ///
72  const Matrix& GetInverseEffectTransform() const;
73 
74  //----------------------------------------------------------------------------
75  /// @brief Set the opacity factor for this color source.
76  ///
77  void SetOpacityFactor(Scalar opacity);
78 
79  //----------------------------------------------------------------------------
80  /// @brief Get the opacity factor for this color source.
81  ///
82  /// This value is is factored into the output of the color source in
83  /// addition to opacity information that may be supplied any other
84  /// inputs.
85  ///
86  /// @note If set, the output of this method factors factors in the inherited
87  /// opacity of this `Contents`.
88  ///
89  /// @see `Contents::CanInheritOpacity`
90  ///
91  Scalar GetOpacityFactor() const;
92 
93  virtual bool IsSolidColor() const;
94 
95  // |Contents|
96  std::optional<Rect> GetCoverage(const Entity& entity) const override;
97 
98  // |Contents|
99  bool CanInheritOpacity(const Entity& entity) const override;
100 
101  // |Contents|
102  void SetInheritedOpacity(Scalar opacity) override;
103 
104  private:
105  std::shared_ptr<Geometry> geometry_;
106  Matrix inverse_matrix_;
107  Scalar opacity_ = 1.0;
108  Scalar inherited_opacity_ = 1.0;
109 
110  FML_DISALLOW_COPY_AND_ASSIGN(ColorSourceContents);
111 };
112 
113 } // namespace impeller
path.h
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
contents.h
impeller::ColorSourceContents::IsSolidColor
virtual bool IsSolidColor() const
Definition: color_source_contents.cc:41
impeller::ColorSourceContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: color_source_contents.cc:54
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::ColorSourceContents::SetGeometry
void SetGeometry(std::shared_ptr< Geometry > geometry)
Set the geometry that this contents will use to render.
Definition: color_source_contents.cc:17
impeller::ColorSourceContents::SetEffectTransform
void SetEffectTransform(Matrix matrix)
Set the effect transform for this color source.
Definition: color_source_contents.cc:33
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::ColorSourceContents::CanInheritOpacity
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
Definition: color_source_contents.cc:50
impeller::ColorSourceContents::ColorSourceContents
ColorSourceContents()
matrix.h
impeller::Entity
Definition: entity.h:21
impeller::ColorSourceContents::~ColorSourceContents
~ColorSourceContents() override
geometry.h
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::ColorSourceContents::SetOpacityFactor
void SetOpacityFactor(Scalar opacity)
Set the opacity factor for this color source.
Definition: color_source_contents.cc:25
impeller::Contents
Definition: contents.h:33
impeller::ColorSourceContents
Definition: color_source_contents.h:33
impeller
Definition: aiks_context.cc:10
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37