Skip to content

Commit c5474f6

Browse files
bderodnfield
authored andcommitted
Untie paths from entities (flutter#145)
1 parent df261f9 commit c5474f6

27 files changed

+171
-175
lines changed

impeller/aiks/canvas.cc

+4-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "flutter/fml/logging.h"
1010
#include "impeller/aiks/paint_pass_delegate.h"
11-
#include "impeller/entity/contents/clear_contents.h"
1211
#include "impeller/entity/contents/clip_contents.h"
1312
#include "impeller/entity/contents/text_contents.h"
1413
#include "impeller/entity/contents/texture_contents.h"
@@ -111,10 +110,9 @@ void Canvas::RestoreToCount(size_t count) {
111110
void Canvas::DrawPath(Path path, Paint paint) {
112111
Entity entity;
113112
entity.SetTransformation(GetCurrentTransformation());
114-
entity.SetPath(std::move(path));
115113
entity.SetStencilDepth(GetStencilDepth());
116114
entity.SetBlendMode(paint.blend_mode);
117-
entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity()));
115+
entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity(std::move(path))));
118116

119117
GetCurrentPass().AddEntity(std::move(entity));
120118
}
@@ -124,8 +122,7 @@ void Canvas::DrawPaint(Paint paint) {
124122
entity.SetTransformation(GetCurrentTransformation());
125123
entity.SetStencilDepth(GetStencilDepth());
126124
entity.SetBlendMode(paint.blend_mode);
127-
entity.SetContents(
128-
std::make_shared<ClearContents>(paint.CreateContentsForEntity()));
125+
entity.SetContents(paint.CreateContentsForEntity({}, true));
129126

130127
GetCurrentPass().AddEntity(std::move(entity));
131128
}
@@ -158,11 +155,11 @@ void Canvas::SaveLayer(Paint paint, std::optional<Rect> bounds) {
158155

159156
void Canvas::ClipPath(Path path, Entity::ClipOperation clip_op) {
160157
auto contents = std::make_shared<ClipContents>();
158+
contents->SetPath(std::move(path));
161159
contents->SetClipOperation(clip_op);
162160

163161
Entity entity;
164162
entity.SetTransformation(GetCurrentTransformation());
165-
entity.SetPath(std::move(path));
166163
entity.SetContents(std::move(contents));
167164
entity.SetStencilDepth(GetStencilDepth());
168165
entity.SetAddsToCoverage(false);
@@ -178,7 +175,6 @@ void Canvas::RestoreClip() {
178175
entity.SetTransformation(GetCurrentTransformation());
179176
// This path is empty because ClipRestoreContents just generates a quad that
180177
// takes up the full render target.
181-
entity.SetPath({});
182178
entity.SetContents(std::make_shared<ClipRestoreContents>());
183179
entity.SetStencilDepth(GetStencilDepth());
184180
entity.SetAddsToCoverage(false);
@@ -234,12 +230,12 @@ void Canvas::DrawImageRect(std::shared_ptr<Image> image,
234230
}
235231

236232
auto contents = std::make_shared<TextureContents>();
233+
contents->SetPath(PathBuilder{}.AddRect(dest).TakePath());
237234
contents->SetTexture(image->GetTexture());
238235
contents->SetSourceRect(source);
239236
contents->SetSamplerDescriptor(std::move(sampler));
240237

241238
Entity entity;
242-
entity.SetPath(PathBuilder{}.AddRect(dest).TakePath());
243239
entity.SetBlendMode(paint.blend_mode);
244240
entity.SetStencilDepth(GetStencilDepth());
245241
entity.SetContents(paint.WithFilters(contents, false));
@@ -293,7 +289,6 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) {
293289
Entity entity;
294290
entity.SetTransformation(GetCurrentTransformation() *
295291
Matrix::MakeTranslation(position));
296-
entity.SetPath({});
297292
entity.SetStencilDepth(GetStencilDepth());
298293
entity.SetBlendMode(paint.blend_mode);
299294
entity.SetContents(paint.WithFilters(std::move(text_contents), true));

impeller/aiks/paint.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88

99
namespace impeller {
1010

11-
std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
11+
std::shared_ptr<Contents> Paint::CreateContentsForEntity(Path path,
12+
bool cover) const {
1213
if (contents) {
1314
return contents;
1415
}
1516

1617
switch (style) {
1718
case Style::kFill: {
1819
auto solid_color = std::make_shared<SolidColorContents>();
20+
solid_color->SetPath(std::move(path));
1921
solid_color->SetColor(color);
22+
solid_color->SetCover(cover);
2023
return solid_color;
2124
}
2225
case Style::kStroke: {
2326
auto solid_stroke = std::make_shared<SolidStrokeContents>();
27+
solid_stroke->SetPath(std::move(path));
2428
solid_stroke->SetColor(color);
2529
solid_stroke->SetStrokeSize(stroke_width);
2630
solid_stroke->SetStrokeMiter(stroke_miter);

impeller/aiks/paint.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ struct Paint {
3636
std::optional<MaskBlur> mask_blur;
3737
std::shared_ptr<Contents> contents;
3838

39-
std::shared_ptr<Contents> CreateContentsForEntity() const;
40-
4139
/// @brief Wrap this paint's configured filters to the given contents.
4240
/// @param[in] input The contents to wrap with paint's filters.
4341
/// @param[in] is_solid_color Affects mask blurring behavior. If false, use
@@ -52,6 +50,9 @@ struct Paint {
5250
std::shared_ptr<Contents> WithFilters(
5351
std::shared_ptr<Contents> input,
5452
std::optional<bool> is_solid_color = std::nullopt) const;
53+
54+
std::shared_ptr<Contents> CreateContentsForEntity(Path path = {},
55+
bool cover = false) const;
5556
};
5657

5758
} // namespace impeller

impeller/aiks/paint_pass_delegate.cc

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "impeller/entity/contents/contents.h"
88
#include "impeller/entity/contents/texture_contents.h"
9+
#include "impeller/geometry/path_builder.h"
910

1011
namespace impeller {
1112

@@ -34,6 +35,9 @@ bool PaintPassDelegate::CanCollapseIntoParentPass() {
3435
std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
3536
std::shared_ptr<Texture> target) {
3637
auto contents = std::make_shared<TextureContents>();
38+
contents->SetPath(PathBuilder{}
39+
.AddRect(Rect::MakeSize(Size(target->GetSize())))
40+
.TakePath());
3741
contents->SetTexture(target);
3842
contents->SetSourceRect(Rect::MakeSize(Size(target->GetSize())));
3943
contents->SetOpacity(paint_.color.alpha);

impeller/entity/BUILD.gn

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ impeller_shaders("entity_shaders") {
3131

3232
impeller_component("entity") {
3333
sources = [
34-
"contents/clear_contents.cc",
35-
"contents/clear_contents.h",
3634
"contents/clip_contents.cc",
3735
"contents/clip_contents.h",
3836
"contents/content_context.cc",

impeller/entity/contents/clear_contents.cc

-34
This file was deleted.

impeller/entity/contents/clear_contents.h

-33
This file was deleted.

impeller/entity/contents/clip_contents.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <optional>
56
#include "impeller/geometry/path_builder.h"
67
#include "impeller/renderer/formats.h"
78
#include "impeller/renderer/vertex_buffer_builder.h"
@@ -23,10 +24,18 @@ ClipContents::ClipContents() = default;
2324

2425
ClipContents::~ClipContents() = default;
2526

27+
void ClipContents::SetPath(Path path) {
28+
path_ = std::move(path);
29+
}
30+
2631
void ClipContents::SetClipOperation(Entity::ClipOperation clip_op) {
2732
clip_op_ = clip_op;
2833
}
2934

35+
std::optional<Rect> ClipContents::GetCoverage(const Entity& entity) const {
36+
return path_.GetTransformedBoundingBox(entity.GetTransformation());
37+
};
38+
3039
bool ClipContents::Render(const ContentContext& renderer,
3140
const Entity& entity,
3241
RenderPass& pass) const {
@@ -77,7 +86,7 @@ bool ClipContents::Render(const ContentContext& renderer,
7786

7887
cmd.pipeline = renderer.GetClipPipeline(options);
7988
cmd.BindVertices(SolidColorContents::CreateSolidFillVertices(
80-
entity.GetPath(), pass.GetTransientsBuffer()));
89+
path_, pass.GetTransientsBuffer()));
8190

8291
info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
8392
entity.GetTransformation();
@@ -95,6 +104,11 @@ ClipRestoreContents::ClipRestoreContents() = default;
95104

96105
ClipRestoreContents::~ClipRestoreContents() = default;
97106

107+
std::optional<Rect> ClipRestoreContents::GetCoverage(
108+
const Entity& entity) const {
109+
return std::nullopt;
110+
};
111+
98112
bool ClipRestoreContents::Render(const ContentContext& renderer,
99113
const Entity& entity,
100114
RenderPass& pass) const {

impeller/entity/contents/clip_contents.h

+9
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ class ClipContents final : public Contents {
2020

2121
~ClipContents();
2222

23+
void SetPath(Path path);
24+
2325
void SetClipOperation(Entity::ClipOperation clip_op);
2426

27+
// |Contents|
28+
std::optional<Rect> GetCoverage(const Entity& entity) const override;
29+
2530
// |Contents|
2631
bool Render(const ContentContext& renderer,
2732
const Entity& entity,
2833
RenderPass& pass) const override;
2934

3035
private:
36+
Path path_;
3137
Entity::ClipOperation clip_op_ = Entity::ClipOperation::kIntersect;
3238

3339
FML_DISALLOW_COPY_AND_ASSIGN(ClipContents);
@@ -39,6 +45,9 @@ class ClipRestoreContents final : public Contents {
3945

4046
~ClipRestoreContents();
4147

48+
// |Contents|
49+
std::optional<Rect> GetCoverage(const Entity& entity) const override;
50+
4251
// |Contents|
4352
bool Render(const ContentContext& renderer,
4453
const Entity& entity,

impeller/entity/contents/contents.cc

-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ Contents::Contents() = default;
2929

3030
Contents::~Contents() = default;
3131

32-
std::optional<Rect> Contents::GetCoverage(const Entity& entity) const {
33-
return entity.GetPathCoverage();
34-
}
35-
3632
std::optional<Snapshot> Contents::RenderToSnapshot(
3733
const ContentContext& renderer,
3834
const Entity& entity) const {
@@ -46,7 +42,6 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
4642
[&contents = *this, &entity, &bounds](const ContentContext& renderer,
4743
RenderPass& pass) -> bool {
4844
Entity sub_entity;
49-
sub_entity.SetPath(entity.GetPath());
5045
sub_entity.SetBlendMode(Entity::BlendMode::kSourceOver);
5146
sub_entity.SetTransformation(
5247
Matrix::MakeTranslation(Vector3(-bounds->origin)) *

impeller/entity/contents/contents.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Contents {
3737
RenderPass& pass) const = 0;
3838

3939
/// @brief Get the screen space bounding rectangle that this contents affects.
40-
virtual std::optional<Rect> GetCoverage(const Entity& entity) const;
40+
virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;
4141

4242
/// @brief Render this contents to a snapshot, respecting the entity's
4343
/// transform, path, stencil depth, and blend mode.

impeller/entity/contents/filters/filter_contents.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ bool FilterContents::Render(const ContentContext& renderer,
129129
// Draw the result texture, respecting the transform and clip stack.
130130

131131
auto contents = std::make_shared<TextureContents>();
132+
contents->SetPath(
133+
PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath());
132134
contents->SetTexture(snapshot.texture);
133135
contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize())));
134136

135137
Entity e;
136-
e.SetPath(PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath());
137138
e.SetBlendMode(entity.GetBlendMode());
138139
e.SetStencilDepth(entity.GetStencilDepth());
139140
return contents->Render(renderer, e, pass);

impeller/entity/contents/filters/filter_input.cc

+2-12
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,13 @@ std::optional<Snapshot> TextureFilterInput::GetSnapshot(
146146

147147
std::optional<Rect> TextureFilterInput::GetCoverage(
148148
const Entity& entity) const {
149-
auto path_bounds = entity.GetPath().GetBoundingBox();
150-
if (!path_bounds.has_value()) {
151-
return std::nullopt;
152-
}
153149
return Rect::MakeSize(Size(texture_->GetSize()))
154150
.TransformBounds(GetTransform(entity));
155151
}
156152

157153
Matrix TextureFilterInput::GetLocalTransform(const Entity& entity) const {
158-
// Compute the local transform such that the texture will cover the entity
159-
// path bounding box.
160-
auto path_bounds = entity.GetPath().GetBoundingBox();
161-
if (!path_bounds.has_value()) {
162-
return Matrix();
163-
}
164-
return Matrix::MakeTranslation(path_bounds->origin) *
165-
Matrix::MakeScale(Vector2(path_bounds->size) / texture_->GetSize());
154+
// Compute the local transform such that the texture is centered.
155+
return Matrix::MakeTranslation(-Point(texture_->GetSize()) / 2);
166156
}
167157

168158
} // namespace impeller

0 commit comments

Comments
 (0)