Skip to content

Commit ead8058

Browse files
chinmaygardednfield
authored andcommitted
Setup a paint pass delgate.
1 parent d00efdd commit ead8058

16 files changed

+108
-15
lines changed

impeller/aiks/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ impeller_component("aiks") {
1414
"image.h",
1515
"paint.cc",
1616
"paint.h",
17+
"paint_pass_delegate.cc",
18+
"paint_pass_delegate.h",
1719
"picture.cc",
1820
"picture.h",
1921
"picture_recorder.cc",

impeller/aiks/aiks_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {
112112
Paint red;
113113
red.color = Color::Red();
114114
Paint green;
115-
green.color = Color::Green();
115+
green.color = Color::Green().WithAlpha(0.5);
116116
Paint blue;
117117
blue.color = Color::Blue();
118118

impeller/aiks/canvas.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <algorithm>
88

99
#include "flutter/fml/logging.h"
10+
#include "impeller/aiks/paint_pass_delegate.h"
1011
#include "impeller/geometry/path_builder.h"
1112

1213
namespace impeller {
@@ -90,7 +91,9 @@ void Canvas::DrawPath(Path path, Paint paint) {
9091
GetCurrentPass().AddEntity(std::move(entity));
9192
}
9293

93-
void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
94+
void Canvas::SaveLayer(Paint paint, std::optional<Rect> bounds) {
95+
GetCurrentPass().SetDelegate(
96+
std::make_unique<PaintPassDelegate>(std::move(paint)));
9497
Save(true);
9598
}
9699

impeller/aiks/canvas.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Canvas {
3131

3232
void Save();
3333

34-
void SaveLayer(const Paint& paint, std::optional<Rect> bounds = std::nullopt);
34+
void SaveLayer(Paint paint, std::optional<Rect> bounds = std::nullopt);
3535

3636
bool Restore();
3737

impeller/aiks/paint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Paint {
1818
kStroke,
1919
};
2020

21-
Color color;
21+
Color color = Color::Black();
2222
Scalar stroke_width = 0.0;
2323
Style style = Style::kFill;
2424

impeller/aiks/paint_pass_delegate.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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/aiks/paint_pass_delegate.h"
6+
7+
#include "impeller/entity/contents.h"
8+
9+
namespace impeller {
10+
11+
PaintPassDelegate::PaintPassDelegate(Paint paint) : paint_(std::move(paint)) {}
12+
13+
// |EntityPassDelgate|
14+
PaintPassDelegate::~PaintPassDelegate() = default;
15+
16+
// |EntityPassDelgate|
17+
bool PaintPassDelegate::CanCollapseIntoParentPass() {
18+
if (paint_.color.IsOpaque()) {
19+
return true;
20+
}
21+
22+
return false;
23+
}
24+
25+
// |EntityPassDelgate|
26+
std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
27+
std::shared_ptr<Texture> target) {
28+
auto contents = std::make_shared<TextureContents>();
29+
contents->SetTexture(target);
30+
contents->SetSourceRect(IRect::MakeSize(target->GetSize()));
31+
contents->SetOpacity(paint_.color.alpha);
32+
return contents;
33+
}
34+
35+
} // namespace impeller

impeller/aiks/paint_pass_delegate.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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"
8+
#include "impeller/aiks/paint.h"
9+
#include "impeller/entity/entity_pass_delegate.h"
10+
11+
namespace impeller {
12+
13+
class PaintPassDelegate final : public EntityPassDelegate {
14+
public:
15+
PaintPassDelegate(Paint paint);
16+
17+
// |EntityPassDelgate|
18+
~PaintPassDelegate() override;
19+
20+
// |EntityPassDelgate|
21+
bool CanCollapseIntoParentPass() override;
22+
23+
// |EntityPassDelgate|
24+
std::shared_ptr<Contents> CreateContentsForSubpassTarget(
25+
std::shared_ptr<Texture> target) override;
26+
27+
private:
28+
const Paint paint_;
29+
30+
FML_DISALLOW_COPY_AND_ASSIGN(PaintPassDelegate);
31+
};
32+
33+
} // namespace impeller

impeller/entity/contents.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ std::shared_ptr<Texture> TextureContents::GetTexture() const {
184184
return texture_;
185185
}
186186

187+
void TextureContents::SetOpacity(Scalar opacity) {
188+
opacity_ = opacity;
189+
}
190+
187191
bool TextureContents::Render(const ContentRenderer& renderer,
188192
const Entity& entity,
189193
RenderPass& pass) const {
@@ -233,6 +237,7 @@ bool TextureContents::Render(const ContentRenderer& renderer,
233237
VS::FrameInfo frame_info;
234238
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
235239
entity.GetTransformation();
240+
frame_info.alpha = opacity_;
236241

237242
Command cmd;
238243
cmd.label = "TextureFill";

impeller/entity/contents.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class TextureContents final : public Contents {
9494

9595
void SetSourceRect(const IRect& source_rect);
9696

97+
void SetOpacity(Scalar opacity);
98+
9799
const IRect& GetSourceRect() const;
98100

99101
// |Contents|
@@ -104,6 +106,7 @@ class TextureContents final : public Contents {
104106
public:
105107
std::shared_ptr<Texture> texture_;
106108
IRect source_rect_;
109+
Scalar opacity_ = 1.0f;
107110

108111
FML_DISALLOW_COPY_AND_ASSIGN(TextureContents);
109112
};

impeller/entity/entity_pass.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111

1212
namespace impeller {
1313

14-
EntityPass::EntityPass(std::unique_ptr<EntityPassDelegate> delegate)
15-
: delegate_(std::move(delegate)) {
16-
if (!delegate_) {
17-
delegate_ = EntityPassDelegate::MakeDefault();
18-
}
19-
}
14+
EntityPass::EntityPass() = default;
2015

2116
EntityPass::~EntityPass() = default;
2217

18+
void EntityPass::SetDelegate(std::unique_ptr<EntityPassDelegate> delegate) {
19+
if (!delegate) {
20+
return;
21+
}
22+
delegate_ = std::move(delegate);
23+
}
24+
2325
void EntityPass::AddEntity(Entity entity) {
2426
entities_.emplace_back(std::move(entity));
2527
}
@@ -117,7 +119,7 @@ bool EntityPass::Render(ContentRenderer& renderer,
117119
}
118120

119121
auto offscreen_texture_contents =
120-
delegate_->CreateContentsForSubpassTarget(*subpass_texture);
122+
delegate_->CreateContentsForSubpassTarget(subpass_texture);
121123

122124
if (!offscreen_texture_contents) {
123125
// This is an error because the subpass delegate said the pass couldn't be

impeller/entity/entity_pass.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ class EntityPass {
2323
using Entities = std::vector<Entity>;
2424
using Subpasses = std::vector<std::unique_ptr<EntityPass>>;
2525

26-
EntityPass(std::unique_ptr<EntityPassDelegate> delegate = nullptr);
26+
EntityPass();
2727

2828
~EntityPass();
2929

30+
void SetDelegate(std::unique_ptr<EntityPassDelegate> delgate);
31+
3032
size_t GetSubpassesDepth() const;
3133

3234
std::unique_ptr<EntityPass> Clone() const;
@@ -61,7 +63,8 @@ class EntityPass {
6163
EntityPass* superpass_ = nullptr;
6264
Matrix xformation_;
6365
size_t stencil_depth_ = 0u;
64-
std::unique_ptr<EntityPassDelegate> delegate_;
66+
std::unique_ptr<EntityPassDelegate> delegate_ =
67+
EntityPassDelegate::MakeDefault();
6568

6669
FML_DISALLOW_COPY_AND_ASSIGN(EntityPass);
6770
};

impeller/entity/entity_pass_delegate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DefaultEntityPassDelegate final : public EntityPassDelegate {
1919
bool CanCollapseIntoParentPass() override { return true; }
2020

2121
std::shared_ptr<Contents> CreateContentsForSubpassTarget(
22-
const Texture& target) override {
22+
std::shared_ptr<Texture> target) override {
2323
// Not possible since this pass always collapses into its parent.
2424
FML_UNREACHABLE();
2525
}

impeller/entity/entity_pass_delegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class EntityPassDelegate {
2323
virtual bool CanCollapseIntoParentPass() = 0;
2424

2525
virtual std::shared_ptr<Contents> CreateContentsForSubpassTarget(
26-
const Texture& target) = 0;
26+
std::shared_ptr<Texture> target) = 0;
2727

2828
private:
2929
FML_DISALLOW_COPY_AND_ASSIGN(EntityPassDelegate);

impeller/entity/shaders/texture_fill.frag

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
uniform sampler2D texture_sampler;
66

77
in vec2 v_texture_coords;
8+
in float v_alpha;
89

910
out vec4 frag_color;
1011

1112
void main() {
1213
vec4 sampled = texture(texture_sampler, v_texture_coords);
14+
sampled.w *= v_alpha;
1315
frag_color = sampled;
1416
}

impeller/entity/shaders/texture_fill.vert

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
uniform FrameInfo {
66
mat4 mvp;
7+
float alpha;
78
} frame_info;
89

910
in vec2 vertices;
1011
in vec2 texture_coords;
1112

1213
out vec2 v_texture_coords;
14+
out float v_alpha;
1315

1416
void main() {
1517
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
1618
v_texture_coords = texture_coords;
19+
v_alpha = frame_info.alpha;
1720
}

impeller/geometry/color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ struct Color {
644644
}
645645

646646
constexpr bool IsTransparent() const { return alpha == 0.0; }
647+
648+
constexpr bool IsOpaque() const { return alpha == 1.0; }
647649
};
648650

649651
/**

0 commit comments

Comments
 (0)