| Flutter Impeller |

dl_atlas_geometry.cc
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 #include "impeller/display_list/dl_atlas_geometry.h"
6
7 #include "impeller/core/formats.h"
8 #include "impeller/core/vertex_buffer.h"
9 #include "impeller/display_list/skia_conversions.h"
10 #include "impeller/entity/porter_duff_blend.vert.h"
11 #include "impeller/entity/texture_fill.vert.h"
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/point.h"
14
15 namespace impeller {
16
17 DlAtlasGeometry::DlAtlasGeometry(const std::shared_ptr<Texture>& atlas,
18 const RSTransform* xform,
19 const flutter::DlRect* tex,
20 const flutter::DlColor* colors,
21 size_t count,
22 BlendMode mode,
23 const SamplerDescriptor& sampling,
24 std::optional<Rect> cull_rect)
25 : atlas_(atlas),
26 xform_(xform),
27 tex_(tex),
28 colors_(colors),
29 count_(count),
30 mode_(mode),
31 sampling_(sampling),
32 cull_rect_(cull_rect) {}
33
34 DlAtlasGeometry::~DlAtlasGeometry() = default;
35
36 bool DlAtlasGeometry::ShouldUseBlend() const {
37 return colors_ != nullptr && mode_ != BlendMode::kSrc;
38 }
39
40 bool DlAtlasGeometry::ShouldSkip() const {
41 return atlas_ == nullptr || (ShouldUseBlend() && mode_ == BlendMode::kClear);
42 }
43
44 Rect DlAtlasGeometry::ComputeBoundingBox() const {
45 if (cull_rect_.has_value()) {
46 return cull_rect_.value();
47 }
48 Rect bounding_box;
49 for (size_t i = 0; i < count_; i++) {
50 auto bounds = xform_[i].GetBounds(tex_[i].GetSize());
51 bounding_box = Rect::Union(bounding_box, bounds);
52 }
53 cull_rect_ = bounding_box;
54 return bounding_box;
55 }
56
57 const std::shared_ptr<Texture>& DlAtlasGeometry::GetAtlas() const {
58 return atlas_;
59 }
60
61 const SamplerDescriptor& DlAtlasGeometry::GetSamplerDescriptor() const {
62 return sampling_;
63 }
64
65 BlendMode DlAtlasGeometry::GetBlendMode() const {
66 return mode_;
67 }
68
69 VertexBuffer DlAtlasGeometry::CreateSimpleVertexBuffer(
70 HostBuffer& host_buffer) const {
71 using VS = TextureFillVertexShader;
72 constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
73
74 BufferView buffer_view = host_buffer.Emplace(
75 sizeof(VS::PerVertexData) * count_ * 6, alignof(VS::PerVertexData),
76 [&](uint8_t* raw_data) {
77 VS::PerVertexData* data =
78 reinterpret_cast<VS::PerVertexData*>(raw_data);
79 int offset = 0;
80 ISize texture_size = atlas_->GetSize();
81 for (auto i = 0u; i < count_; i++) {
82 flutter::DlRect sample_rect = tex_[i];
83 auto points = sample_rect.GetPoints();
84 auto transformed_points = xform_[i].GetQuad(sample_rect.GetSize());
85 for (size_t j = 0; j < 6; j++) {
86 data[offset].position = transformed_points[indices[j]];
87 data[offset].texture_coords = points[indices[j]] / texture_size;
88 offset += 1;
89 }
90 }
91 });
92
93 return VertexBuffer{
94 .vertex_buffer = buffer_view,
95 .index_buffer = {},
96 .vertex_count = count_ * 6,
97 .index_type = IndexType::kNone,
98 };
99 }
100
101 VertexBuffer DlAtlasGeometry::CreateBlendVertexBuffer(
102 HostBuffer& host_buffer) const {
103 using VS = PorterDuffBlendVertexShader;
104 constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
105
106 BufferView buffer_view = host_buffer.Emplace(
107 sizeof(VS::PerVertexData) * count_ * 6, alignof(VS::PerVertexData),
108 [&](uint8_t* raw_data) {
109 VS::PerVertexData* data =
110 reinterpret_cast<VS::PerVertexData*>(raw_data);
111 int offset = 0;
112 ISize texture_size = atlas_->GetSize();
113 for (auto i = 0u; i < count_; i++) {
114 flutter::DlRect sample_rect = tex_[i];
115 auto points = sample_rect.GetPoints();
116 auto transformed_points = xform_[i].GetQuad(sample_rect.GetSize());
117 for (size_t j = 0; j < 6; j++) {
118 data[offset].vertices = transformed_points[indices[j]];
119 data[offset].texture_coords = points[indices[j]] / texture_size;
120 data[offset].color =
121 skia_conversions::ToColor(colors_[i]).Premultiply();
122 offset += 1;
123 }
124 }
125 });
126
127 return VertexBuffer{
128 .vertex_buffer = buffer_view,
129 .index_buffer = {},
130 .vertex_count = count_ * 6,
131 .index_type = IndexType::kNone,
132 };
133 }
134
135 } // namespace impeller
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:154
impeller::DlAtlasGeometry::GetAtlas
const std::shared_ptr< Texture > & GetAtlas() const override
Definition: dl_atlas_geometry.cc:57
impeller::DlAtlasGeometry::~DlAtlasGeometry
~DlAtlasGeometry()
impeller::DlAtlasGeometry::GetBlendMode
BlendMode GetBlendMode() const override
Definition: dl_atlas_geometry.cc:65
impeller::DlAtlasGeometry::CreateBlendVertexBuffer
VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const override
Definition: dl_atlas_geometry.cc:101
impeller::DlAtlasGeometry::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const override
Definition: dl_atlas_geometry.cc:61
impeller::DlAtlasGeometry::DlAtlasGeometry
DlAtlasGeometry(const std::shared_ptr< Texture > &atlas, const RSTransform *xform, const flutter::DlRect *tex, const flutter::DlColor *colors, size_t count, BlendMode mode, const SamplerDescriptor &sampling, std::optional< Rect > cull_rect)
Definition: dl_atlas_geometry.cc:17
impeller::DlAtlasGeometry::ShouldUseBlend
bool ShouldUseBlend() const override
Whether the blend shader should be used.
Definition: dl_atlas_geometry.cc:36
impeller::DlAtlasGeometry::ShouldSkip
bool ShouldSkip() const override
Definition: dl_atlas_geometry.cc:40
impeller::DlAtlasGeometry::ComputeBoundingBox
Rect ComputeBoundingBox() const override
Definition: dl_atlas_geometry.cc:44
impeller::DlAtlasGeometry::CreateSimpleVertexBuffer
VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const override
Definition: dl_atlas_geometry.cc:69
impeller::HostBuffer
Definition: host_buffer.h:26
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
color.h
formats.h
dl_atlas_geometry.h
impeller::skia_conversions::ToColor
Color ToColor(const flutter::DlColor &color)
Definition: skia_conversions.cc:13
impeller
Definition: allocation.cc:12
impeller::DlRect
flutter::DlRect DlRect
Definition: dl_dispatcher.h:25
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::BlendMode::kSrc
@ kSrc
impeller::BlendMode::kClear
@ kClear
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
point.h
skia_conversions.h
impeller::BufferView
Definition: buffer_view.h:19
impeller::Color::Premultiply
constexpr Color Premultiply() const
Definition: color.h:212
impeller::RSTransform
Definition: rstransform.h:20
impeller::RSTransform::GetBounds
std::optional< Rect > GetBounds(Scalar width, Scalar height) const
Definition: rstransform.cc:49
impeller::RSTransform::GetQuad
void GetQuad(Scalar width, Scalar height, Quad &quad) const
Definition: rstransform.cc:24
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::TRect< Scalar >
impeller::TRect< Scalar >::Union
constexpr TRect Union(const TRect &o) const
Definition: rect.h:517
impeller::TSize
Definition: size.h:25
impeller::VertexBuffer
Definition: vertex_buffer.h:15
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:16
points
std::vector< Point > points
Definition: tessellator_libtess.cc:76
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
vertex_buffer.h