| 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 #include "third_party/skia/include/core/SkPoint.h"
15
16 namespace impeller {
17
18 DlAtlasGeometry::DlAtlasGeometry(const std::shared_ptr<Texture>& atlas,
19 const SkRSXform* xform,
20 const flutter::DlRect* tex,
21 const flutter::DlColor* colors,
22 size_t count,
23 BlendMode mode,
24 const SamplerDescriptor& sampling,
25 std::optional<Rect> cull_rect)
26 : atlas_(atlas),
27 xform_(xform),
28 tex_(tex),
29 colors_(colors),
30 count_(count),
31 mode_(mode),
32 sampling_(sampling),
33 cull_rect_(cull_rect) {}
34
35 DlAtlasGeometry::~DlAtlasGeometry() = default;
36
37 bool DlAtlasGeometry::ShouldUseBlend() const {
38 return colors_ != nullptr && mode_ != BlendMode::kSource;
39 }
40
41 bool DlAtlasGeometry::ShouldSkip() const {
42 return atlas_ == nullptr || (ShouldUseBlend() && mode_ == BlendMode::kClear);
43 }
44
45 Rect DlAtlasGeometry::ComputeBoundingBox() const {
46 if (cull_rect_.has_value()) {
47 return cull_rect_.value();
48 }
49 Rect bounding_box = {};
50 for (size_t i = 0; i < count_; i++) {
51 auto matrix = skia_conversions::ToRSXForm(xform_[i]);
52 auto sample_rect = tex_[i];
53 auto bounds = Rect::MakeSize(sample_rect.GetSize()).TransformBounds(matrix);
54 bounding_box = bounds.Union(bounding_box);
55 }
56 cull_rect_ = bounding_box;
57 return bounding_box;
58 }
59
60 std::shared_ptr<Texture> DlAtlasGeometry::GetAtlas() const {
61 return atlas_;
62 }
63
64 const SamplerDescriptor& DlAtlasGeometry::GetSamplerDescriptor() const {
65 return sampling_;
66 }
67
68 BlendMode DlAtlasGeometry::GetBlendMode() const {
69 return mode_;
70 }
71
72 VertexBuffer DlAtlasGeometry::CreateSimpleVertexBuffer(
73 HostBuffer& host_buffer) const {
74 using VS = TextureFillVertexShader;
75
76 constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
77
78 auto buffer_view = host_buffer.Emplace(
79 sizeof(VS::PerVertexData) * count_ * 6, alignof(VS::PerVertexData),
80 [&](uint8_t* raw_data) {
81 VS::PerVertexData* data =
82 reinterpret_cast<VS::PerVertexData*>(raw_data);
83 int offset = 0;
84 auto texture_size = atlas_->GetSize();
85 for (auto i = 0u; i < count_; i++) {
86 flutter::DlRect sample_rect = tex_[i];
87 Matrix matrix = skia_conversions::ToRSXForm(xform_[i]);
88 auto points = sample_rect.GetPoints();
89 auto transformed_points = Rect::MakeSize(sample_rect.GetSize())
90 .GetTransformedPoints(matrix);
91 for (size_t j = 0; j < 6; j++) {
92 data[offset].position = transformed_points[indices[j]];
93 data[offset].texture_coords = points[indices[j]] / texture_size;
94 offset += 1;
95 }
96 }
97 });
98
99 return VertexBuffer{
100 .vertex_buffer = buffer_view,
101 .index_buffer = {},
102 .vertex_count = count_ * 6,
103 .index_type = IndexType::kNone,
104 };
105 }
106
107 VertexBuffer DlAtlasGeometry::CreateBlendVertexBuffer(
108 HostBuffer& host_buffer) const {
109 using VS = PorterDuffBlendVertexShader;
110
111 constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
112
113 auto buffer_view = host_buffer.Emplace(
114 sizeof(VS::PerVertexData) * count_ * 6, alignof(VS::PerVertexData),
115 [&](uint8_t* raw_data) {
116 VS::PerVertexData* data =
117 reinterpret_cast<VS::PerVertexData*>(raw_data);
118 int offset = 0;
119 auto texture_size = atlas_->GetSize();
120 for (auto i = 0u; i < count_; i++) {
121 flutter::DlRect sample_rect = tex_[i];
122 Matrix matrix = skia_conversions::ToRSXForm(xform_[i]);
123 auto points = sample_rect.GetPoints();
124 auto transformed_points = Rect::MakeSize(sample_rect.GetSize())
125 .GetTransformedPoints(matrix);
126 for (size_t j = 0; j < 6; j++) {
127 data[offset].vertices = transformed_points[indices[j]];
128 data[offset].texture_coords = points[indices[j]] / texture_size;
129 data[offset].color =
130 skia_conversions::ToColor(colors_[i]).Premultiply();
131 offset += 1;
132 }
133 }
134 });
135
136 return VertexBuffer{
137 .vertex_buffer = buffer_view,
138 .index_buffer = {},
139 .vertex_count = count_ * 6,
140 .index_type = IndexType::kNone,
141 };
142 }
143
144 } // namespace impeller
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:95
impeller::DlAtlasGeometry::CreateSimpleVertexBuffer
VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const override
Definition: dl_atlas_geometry.cc:72
point.h
impeller::DlAtlasGeometry::GetBlendMode
BlendMode GetBlendMode() const override
Definition: dl_atlas_geometry.cc:68
vertex_buffer.h
impeller::DlAtlasGeometry::CreateBlendVertexBuffer
VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const override
Definition: dl_atlas_geometry.cc:107
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::HostBuffer
Definition: host_buffer.h:28
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::BlendMode::kSource
@ kSource
impeller::VertexBuffer
Definition: vertex_buffer.h:13
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:466
formats.h
impeller::skia_conversions::ToColor
Color ToColor(const flutter::DlColor &color)
Definition: skia_conversions.cc:106
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:128
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:14
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:304
impeller::skia_conversions::ToRSXForm
Matrix ToRSXForm(const SkRSXform &form)
Definition: skia_conversions.cc:117
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
skia_conversions.h
impeller::DlAtlasGeometry::DlAtlasGeometry
DlAtlasGeometry(const std::shared_ptr< Texture > &atlas, const SkRSXform *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:18
impeller::TRect::GetTransformedPoints
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
Definition: rect.h:420
impeller::BlendMode::kClear
@ kClear
impeller::DlAtlasGeometry::GetAtlas
std::shared_ptr< Texture > GetAtlas() const override
Definition: dl_atlas_geometry.cc:60
impeller::DlRect
flutter::DlRect DlRect
Definition: dl_dispatcher.h:23
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
dl_atlas_geometry.h
impeller::DlAtlasGeometry::ComputeBoundingBox
Rect ComputeBoundingBox() const override
Definition: dl_atlas_geometry.cc:45
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::DlAtlasGeometry::ShouldSkip
bool ShouldSkip() const override
Definition: dl_atlas_geometry.cc:41
impeller::DlAtlasGeometry::ShouldUseBlend
bool ShouldUseBlend() const override
Whether the blend shader should be used.
Definition: dl_atlas_geometry.cc:37
impeller::TRect::Union
constexpr TRect Union(const TRect &o) const
Definition: rect.h:507
color.h
impeller::DlAtlasGeometry::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const override
Definition: dl_atlas_geometry.cc:64
impeller::DlAtlasGeometry::~DlAtlasGeometry
~DlAtlasGeometry()
impeller
Definition: allocation.cc:12
impeller::Color::Premultiply
constexpr Color Premultiply() const
Definition: color.h:211
impeller::TRect
Definition: rect.h:122
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37