Skip to content

Commit 2747332

Browse files
chinmaygardednfield
authored andcommitted
Separate save layer calls into a canvas pass.
1 parent 612b54f commit 2747332

File tree

14 files changed

+106
-49
lines changed

14 files changed

+106
-49
lines changed

impeller/aiks/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ impeller_component("aiks") {
88
sources = [
99
"canvas.cc",
1010
"canvas.h",
11+
"canvas_pass.cc",
12+
"canvas_pass.h",
1113
"image.cc",
1214
"image.h",
1315
"paint.cc",

impeller/aiks/canvas.cc

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ namespace impeller {
1212

1313
Canvas::Canvas() {
1414
xformation_stack_.push({});
15-
16-
Paint default_paint;
17-
default_paint.color = Color::White();
18-
paint_stack_.push(default_paint);
15+
passes_.emplace_back(CanvasPass{});
1916
}
2017

2118
Canvas::~Canvas() = default;
2219

2320
void Canvas::Save() {
2421
FML_DCHECK(xformation_stack_.size() > 0);
2522
xformation_stack_.push(xformation_stack_.top());
26-
paint_stack_.push(paint_stack_.top());
2723
}
2824

2925
bool Canvas::Restore() {
@@ -32,7 +28,6 @@ bool Canvas::Restore() {
3228
return false;
3329
}
3430
xformation_stack_.pop();
35-
paint_stack_.pop();
3631
return true;
3732
}
3833

@@ -69,46 +64,49 @@ void Canvas::RestoreToCount(size_t count) {
6964
}
7065

7166
void Canvas::DrawPath(Path path, Paint paint) {
72-
Color color = paint.color;
73-
color.alpha *= paint_stack_.top().color.alpha;
74-
7567
Entity entity;
7668
entity.SetTransformation(GetCurrentTransformation());
7769
entity.SetPath(std::move(path));
78-
entity.SetBackgroundColor(color);
79-
entity.SetContents(std::move(paint.contents));
80-
81-
ops_.emplace_back(std::move(entity));
70+
entity.SetContents(paint.CreateContentsForEntity());
71+
GetCurrentPass().PushEntity(std::move(entity));
8272
}
8373

8474
void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
8575
Save();
86-
paint_stack_.top() = paint;
8776
}
8877

8978
void Canvas::ClipPath(Path path) {
9079
Entity entity;
9180
entity.SetTransformation(GetCurrentTransformation());
9281
entity.SetPath(std::move(path));
9382
entity.SetIsClip(true);
94-
ops_.emplace_back(std::move(entity));
83+
GetCurrentPass().PushEntity(std::move(entity));
9584
}
9685

9786
void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {}
9887

9988
void Canvas::DrawPicture(const Picture& picture) {
100-
for (const auto& entity : picture.entities) {
101-
auto new_entity = entity;
102-
new_entity.SetTransformation(GetCurrentTransformation() *
103-
new_entity.GetTransformation());
104-
ops_.emplace_back(std::move(new_entity));
89+
for (const auto& pass : picture.passes) {
90+
CanvasPass new_pass;
91+
for (const auto& entity : pass.GetPassEntities()) {
92+
auto new_entity = entity;
93+
new_entity.SetTransformation(GetCurrentTransformation() *
94+
entity.GetTransformation());
95+
new_pass.PushEntity(std::move(new_entity));
96+
}
97+
passes_.emplace_back(std::move(new_pass));
10598
}
10699
}
107100

108101
Picture Canvas::EndRecordingAsPicture() {
109102
Picture picture;
110-
picture.entities = std::move(ops_);
103+
picture.passes = std::move(passes_);
111104
return picture;
112105
}
113106

107+
CanvasPass& Canvas::GetCurrentPass() {
108+
FML_DCHECK(!passes_.empty());
109+
return passes_.back();
110+
}
111+
114112
} // namespace impeller

impeller/aiks/canvas.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <vector>
1111

1212
#include "flutter/fml/macros.h"
13+
#include "impeller/aiks/canvas_pass.h"
1314
#include "impeller/aiks/paint.h"
1415
#include "impeller/aiks/picture.h"
1516
#include "impeller/geometry/matrix.h"
@@ -58,8 +59,9 @@ class Canvas {
5859

5960
private:
6061
std::stack<Matrix> xformation_stack_;
61-
std::stack<Paint> paint_stack_;
62-
std::vector<Entity> ops_;
62+
std::vector<CanvasPass> passes_;
63+
64+
CanvasPass& GetCurrentPass();
6365

6466
FML_DISALLOW_COPY_AND_ASSIGN(Canvas);
6567
};

impeller/aiks/canvas_pass.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/canvas_pass.h"
6+
7+
namespace impeller {
8+
9+
CanvasPass::CanvasPass() = default;
10+
11+
CanvasPass::~CanvasPass() = default;
12+
13+
void CanvasPass::PushEntity(Entity entity) {
14+
ops_.emplace_back(std::move(entity));
15+
}
16+
17+
const std::vector<Entity>& CanvasPass::GetPassEntities() const {
18+
return ops_;
19+
}
20+
21+
} // namespace impeller

impeller/aiks/canvas_pass.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 <vector>
8+
9+
#include "flutter/fml/macros.h"
10+
#include "impeller/entity/entity.h"
11+
12+
namespace impeller {
13+
14+
class CanvasPass {
15+
public:
16+
CanvasPass();
17+
18+
~CanvasPass();
19+
20+
void PushEntity(Entity entity);
21+
22+
const std::vector<Entity>& GetPassEntities() const;
23+
24+
private:
25+
std::vector<Entity> ops_;
26+
};
27+
28+
} // namespace impeller

impeller/aiks/paint.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
namespace impeller {
88

9-
//
9+
std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
10+
if (!color.IsTransparent()) {
11+
auto solid_color = std::make_shared<SolidColorContents>();
12+
solid_color->SetColor(color);
13+
return solid_color;
14+
}
15+
16+
return nullptr;
17+
}
1018

1119
} // namespace impeller

impeller/aiks/paint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace impeller {
1414
struct Paint {
1515
Color color;
1616
Scalar stroke_width = 0.0;
17-
std::shared_ptr<Contents> contents;
17+
18+
std::shared_ptr<Contents> CreateContentsForEntity() const;
1819
};
1920

2021
} // namespace impeller

impeller/aiks/picture.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <vector>
99

1010
#include "flutter/fml/macros.h"
11+
#include "impeller/aiks/canvas_pass.h"
1112
#include "impeller/entity/entity.h"
1213

1314
namespace impeller {
1415

1516
struct Picture {
16-
std::vector<Entity> entities;
17+
std::vector<CanvasPass> passes;
1718
};
1819

1920
} // namespace impeller

impeller/aiks/picture_renderer.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ bool PictureRenderer::Render(const Surface& surface,
2929
return false;
3030
}
3131

32-
return entity_renderer_.RenderEntities(surface, //
33-
onscreen_pass, //
34-
picture.entities //
35-
);
32+
for (const auto& pass : picture.passes) {
33+
if (!entity_renderer_.RenderEntities(surface, onscreen_pass,
34+
pass.GetPassEntities())) {
35+
return false;
36+
}
37+
}
38+
39+
return true;
3640
}
3741

3842
} // namespace impeller

impeller/entity/contents.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer,
9595
******* SolidColorContents
9696
******************************************************************************/
9797

98+
SolidColorContents::SolidColorContents() = default;
99+
100+
SolidColorContents::~SolidColorContents() = default;
101+
98102
void SolidColorContents::SetColor(Color color) {
99103
color_ = color;
100104
}
@@ -135,7 +139,7 @@ bool SolidColorContents::Render(const ContentRenderer& renderer,
135139
VS::FrameInfo frame_info;
136140
frame_info.mvp =
137141
Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation();
138-
frame_info.color = entity.GetBackgroundColor();
142+
frame_info.color = color_;
139143
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
140144

141145
cmd.primitive_type = PrimitiveType::kTriangle;

0 commit comments

Comments
 (0)