| Flutter Impeller |

atlas_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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_ATLAS_CONTENTS_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_ATLAS_CONTENTS_H_
7
8 #include <memory>
9
10 #include "impeller/core/sampler_descriptor.h"
11 #include "impeller/entity/contents/contents.h"
12 #include "impeller/entity/entity.h"
13 #include "impeller/geometry/color.h"
14
15 namespace impeller {
16
17 struct VertexBuffer;
18
19 // Interface wrapper to allow usage of DL pointer data without copying (or
20 // circular imports).
21 class AtlasGeometry {
22 public:
23 virtual bool ShouldUseBlend() const = 0;
24
25 virtual bool ShouldSkip() const = 0;
26
27 virtual VertexBuffer CreateSimpleVertexBuffer(
28 HostBuffer& host_buffer) const = 0;
29
30 virtual VertexBuffer CreateBlendVertexBuffer(
31 HostBuffer& host_buffer) const = 0;
32
33 virtual Rect ComputeBoundingBox() const = 0;
34
35 virtual const std::shared_ptr<Texture>& GetAtlas() const = 0;
36
37 virtual const SamplerDescriptor& GetSamplerDescriptor() const = 0;
38
39 virtual BlendMode GetBlendMode() const = 0;
40
41 virtual bool ShouldInvertBlendMode() const { return true; }
42
43 /// @brief The source rect of the draw in texture coordinates if a strict
44 /// source rect should be applied, or nullopt.
45 ///
46 /// See also `Canvas::AttemptColorFilterOptimization`
47 virtual std::optional<Rect> GetStrictSrcRect() const { return std::nullopt; }
48 };
49
50 /// @brief An atlas geometry that adapts for drawImageRect.
51 class DrawImageRectAtlasGeometry : public AtlasGeometry {
52 public:
53 DrawImageRectAtlasGeometry(std::shared_ptr<Texture> texture,
54 const Rect& source,
55 const Rect& destination,
56 const Color& color,
57 BlendMode blend_mode,
58 const SamplerDescriptor& desc,
59 bool use_strict_src_rect = false);
60
61 ~DrawImageRectAtlasGeometry();
62
63 bool ShouldUseBlend() const override;
64
65 bool ShouldSkip() const override;
66
67 VertexBuffer CreateSimpleVertexBuffer(HostBuffer& host_buffer) const override;
68
69 VertexBuffer CreateBlendVertexBuffer(HostBuffer& host_buffer) const override;
70
71 Rect ComputeBoundingBox() const override;
72
73 const std::shared_ptr<Texture>& GetAtlas() const override;
74
75 const SamplerDescriptor& GetSamplerDescriptor() const override;
76
77 BlendMode GetBlendMode() const override;
78
79 bool ShouldInvertBlendMode() const override;
80
81 std::optional<Rect> GetStrictSrcRect() const override;
82
83 private:
84 const std::shared_ptr<Texture> texture_;
85 const Rect source_;
86 const Rect destination_;
87 const Color color_;
88 const BlendMode blend_mode_;
89 const SamplerDescriptor desc_;
90 const bool use_strict_src_rect_;
91 };
92
93 class AtlasContents final : public Contents {
94 public:
95 explicit AtlasContents();
96
97 ~AtlasContents() override;
98
99 void SetGeometry(AtlasGeometry* geometry);
100
101 void SetAlpha(Scalar alpha);
102
103 // |Contents|
104 std::optional<Rect> GetCoverage(const Entity& entity) const override;
105
106 // |Contents|
107 bool Render(const ContentContext& renderer,
108 const Entity& entity,
109 RenderPass& pass) const override;
110
111 private:
112 AtlasGeometry* geometry_ = nullptr;
113 Scalar alpha_ = 1.0;
114
115 AtlasContents(const AtlasContents&) = delete;
116
117 AtlasContents& operator=(const AtlasContents&) = delete;
118 };
119
120 /// A specialized atlas class for applying a color matrix filter to a
121 /// drawImageRect call.
122 class ColorFilterAtlasContents final : public Contents {
123 public:
124 explicit ColorFilterAtlasContents();
125
126 ~ColorFilterAtlasContents() override;
127
128 void SetGeometry(AtlasGeometry* geometry);
129
130 void SetAlpha(Scalar alpha);
131
132 void SetMatrix(ColorMatrix matrix);
133
134 // |Contents|
135 std::optional<Rect> GetCoverage(const Entity& entity) const override;
136
137 // |Contents|
138 bool Render(const ContentContext& renderer,
139 const Entity& entity,
140 RenderPass& pass) const override;
141
142 private:
143 // These contents are created temporarily on the stack and never stored.
144 // The referenced geometry is also stack allocated and will be de-allocated
145 // after the contents are.
146 AtlasGeometry* geometry_ = nullptr;
147 ColorMatrix matrix_;
148 Scalar alpha_ = 1.0;
149
150 ColorFilterAtlasContents(const ColorFilterAtlasContents&) = delete;
151
152 ColorFilterAtlasContents& operator=(const ColorFilterAtlasContents&) = delete;
153 };
154
155 } // namespace impeller
156
157 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_ATLAS_CONTENTS_H_
impeller::AtlasContents
Definition: atlas_contents.h:93
impeller::AtlasContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: atlas_contents.cc:159
impeller::AtlasContents::SetGeometry
void SetGeometry(AtlasGeometry *geometry)
Definition: atlas_contents.cc:151
impeller::AtlasContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: atlas_contents.cc:144
impeller::AtlasContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: atlas_contents.cc:155
impeller::AtlasContents::~AtlasContents
~AtlasContents() override
impeller::AtlasContents::AtlasContents
AtlasContents()
impeller::AtlasGeometry
Definition: atlas_contents.h:21
impeller::AtlasGeometry::GetStrictSrcRect
virtual std::optional< Rect > GetStrictSrcRect() const
The source rect of the draw in texture coordinates if a strict source rect should be applied,...
Definition: atlas_contents.h:47
impeller::AtlasGeometry::CreateSimpleVertexBuffer
virtual VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const =0
impeller::AtlasGeometry::ComputeBoundingBox
virtual Rect ComputeBoundingBox() const =0
impeller::AtlasGeometry::ShouldInvertBlendMode
virtual bool ShouldInvertBlendMode() const
Definition: atlas_contents.h:41
impeller::AtlasGeometry::ShouldUseBlend
virtual bool ShouldUseBlend() const =0
impeller::AtlasGeometry::GetSamplerDescriptor
virtual const SamplerDescriptor & GetSamplerDescriptor() const =0
impeller::AtlasGeometry::ShouldSkip
virtual bool ShouldSkip() const =0
impeller::AtlasGeometry::CreateBlendVertexBuffer
virtual VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const =0
impeller::AtlasGeometry::GetBlendMode
virtual BlendMode GetBlendMode() const =0
impeller::AtlasGeometry::GetAtlas
virtual const std::shared_ptr< Texture > & GetAtlas() const =0
impeller::ColorFilterAtlasContents
Definition: atlas_contents.h:122
impeller::ColorFilterAtlasContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: atlas_contents.cc:291
impeller::ColorFilterAtlasContents::SetGeometry
void SetGeometry(AtlasGeometry *geometry)
Definition: atlas_contents.cc:299
impeller::ColorFilterAtlasContents::ColorFilterAtlasContents
ColorFilterAtlasContents()
impeller::ColorFilterAtlasContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: atlas_contents.cc:303
impeller::ColorFilterAtlasContents::~ColorFilterAtlasContents
~ColorFilterAtlasContents() override
impeller::ColorFilterAtlasContents::SetMatrix
void SetMatrix(ColorMatrix matrix)
Definition: atlas_contents.cc:307
impeller::ColorFilterAtlasContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: atlas_contents.cc:311
impeller::ContentContext
Definition: content_context.h:138
impeller::Contents
Definition: contents.h:31
impeller::DrawImageRectAtlasGeometry
An atlas geometry that adapts for drawImageRect.
Definition: atlas_contents.h:51
impeller::DrawImageRectAtlasGeometry::DrawImageRectAtlasGeometry
DrawImageRectAtlasGeometry(std::shared_ptr< Texture > texture, const Rect &source, const Rect &destination, const Color &color, BlendMode blend_mode, const SamplerDescriptor &desc, bool use_strict_src_rect=false)
Definition: atlas_contents.cc:20
impeller::DrawImageRectAtlasGeometry::ComputeBoundingBox
Rect ComputeBoundingBox() const override
Definition: atlas_contents.cc:107
impeller::DrawImageRectAtlasGeometry::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const override
Definition: atlas_contents.cc:115
impeller::DrawImageRectAtlasGeometry::ShouldUseBlend
bool ShouldUseBlend() const override
Definition: atlas_contents.cc:38
impeller::DrawImageRectAtlasGeometry::GetStrictSrcRect
std::optional< Rect > GetStrictSrcRect() const override
The source rect of the draw in texture coordinates if a strict source rect should be applied,...
Definition: atlas_contents.cc:128
impeller::DrawImageRectAtlasGeometry::GetBlendMode
BlendMode GetBlendMode() const override
Definition: atlas_contents.cc:120
impeller::DrawImageRectAtlasGeometry::ShouldInvertBlendMode
bool ShouldInvertBlendMode() const override
Definition: atlas_contents.cc:124
impeller::DrawImageRectAtlasGeometry::CreateBlendVertexBuffer
VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const override
Definition: atlas_contents.cc:76
impeller::DrawImageRectAtlasGeometry::CreateSimpleVertexBuffer
VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const override
Definition: atlas_contents.cc:46
impeller::DrawImageRectAtlasGeometry::ShouldSkip
bool ShouldSkip() const override
Definition: atlas_contents.cc:42
impeller::DrawImageRectAtlasGeometry::~DrawImageRectAtlasGeometry
~DrawImageRectAtlasGeometry()
impeller::DrawImageRectAtlasGeometry::GetAtlas
const std::shared_ptr< Texture > & GetAtlas() const override
Definition: atlas_contents.cc:111
impeller::Entity
Definition: entity.h:26
impeller::HostBuffer
Definition: host_buffer.h:26
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
color.h
contents.h
entity.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::BlendMode
BlendMode
Definition: color.h:58
sampler_descriptor.h
impeller::Color
Definition: color.h:124
impeller::ColorMatrix
Definition: color.h:117
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::TRect< Scalar >
impeller::VertexBuffer
Definition: vertex_buffer.h:15