Skip to content

Commit 0570581

Browse files
chinmaygardednfield
authored andcommitted
Add support for typed commands.
1 parent 9ce9a81 commit 0570581

15 files changed

+167
-10
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ TEST_F(AiksTest, CanRenderClips) {
104104
canvas.ClipPath(
105105
PathBuilder{}.AddRect(Rect::MakeXYWH(0, 0, 100, 100)).CreatePath());
106106
canvas.DrawPath(PathBuilder{}.AddCircle({100, 100}, 50).CreatePath(), paint);
107-
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
107+
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
108108
}
109109

110110
} // namespace testing

impeller/aiks/canvas.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
7777
}
7878

7979
void Canvas::ClipPath(Path path) {
80-
passes_.push_back({});
80+
Entity entity;
81+
entity.SetTransformation(GetCurrentTransformation());
82+
entity.SetPath(std::move(path));
83+
entity.SetContents(std::make_shared<ClipContents>());
84+
GetCurrentPass().PushEntity(std::move(entity));
8185
}
8286

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

impeller/aiks/canvas_pass.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ const std::vector<Entity>& CanvasPass::GetPassEntities() const {
1818
return ops_;
1919
}
2020

21+
void CanvasPass::SetPostProcessingEntity(Entity entity) {
22+
post_processing_entity_ = std::move(entity);
23+
}
24+
25+
const Entity& CanvasPass::GetPostProcessingEntity() const {
26+
return post_processing_entity_;
27+
}
28+
2129
} // namespace impeller

impeller/aiks/canvas_pass.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#pragma once
66

7-
#include <vector>
7+
#include <memory>
88

99
#include "flutter/fml/macros.h"
10+
#include "impeller/entity/contents.h"
1011
#include "impeller/entity/entity.h"
1112

1213
namespace impeller {
@@ -21,8 +22,13 @@ class CanvasPass {
2122

2223
const std::vector<Entity>& GetPassEntities() const;
2324

25+
void SetPostProcessingEntity(Entity entity);
26+
27+
const Entity& GetPostProcessingEntity() const;
28+
2429
private:
2530
std::vector<Entity> ops_;
31+
Entity post_processing_entity_;
2632
};
2733

2834
} // namespace impeller

impeller/aiks/picture_renderer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ bool PictureRenderer::IsValid() const {
2323
}
2424

2525
bool PictureRenderer::Render(const Surface& surface,
26-
RenderPass& onscreen_pass,
26+
RenderPass& parent_pass,
2727
const Picture& picture) {
2828
if (!IsValid()) {
2929
return false;
3030
}
3131

3232
for (const auto& pass : picture.passes) {
33-
if (!entity_renderer_.RenderEntities(surface, onscreen_pass,
33+
if (!entity_renderer_.RenderEntities(surface, parent_pass,
3434
pass.GetPassEntities())) {
3535
return false;
3636
}

impeller/aiks/picture_renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PictureRenderer {
2323
bool IsValid() const;
2424

2525
[[nodiscard]] bool Render(const Surface& surface,
26-
RenderPass& onscreen_pass,
26+
RenderPass& parent_pass,
2727
const Picture& picture);
2828

2929
private:

impeller/entity/contents.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,19 @@ Scalar SolidStrokeContents::GetStrokeSize() const {
354354
return stroke_size_;
355355
}
356356

357+
/*******************************************************************************
358+
******* ClipContents
359+
******************************************************************************/
360+
361+
ClipContents::ClipContents() = default;
362+
363+
ClipContents::~ClipContents() = default;
364+
365+
bool ClipContents::Render(const ContentRenderer& renderer,
366+
const Entity& entity,
367+
const Surface& surface,
368+
RenderPass& pass) const {
369+
return true;
370+
}
371+
357372
} // namespace impeller

impeller/entity/contents.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,20 @@ class SolidStrokeContents final : public Contents {
139139
FML_DISALLOW_COPY_AND_ASSIGN(SolidStrokeContents);
140140
};
141141

142+
class ClipContents final : public Contents {
143+
public:
144+
ClipContents();
145+
146+
~ClipContents();
147+
148+
// |Contents|
149+
bool Render(const ContentRenderer& renderer,
150+
const Entity& entity,
151+
const Surface& surface,
152+
RenderPass& pass) const override;
153+
154+
private:
155+
FML_DISALLOW_COPY_AND_ASSIGN(ClipContents);
156+
};
157+
142158
} // namespace impeller

impeller/entity/entity_renderer.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ bool EntityRenderer::IsValid() const {
3030
}
3131

3232
bool EntityRenderer::RenderEntities(const Surface& surface,
33-
RenderPass& onscreen_pass,
33+
RenderPass& parent_pass,
3434
const std::vector<Entity>& entities) {
3535
if (!IsValid()) {
3636
return false;
3737
}
3838

3939
for (const auto& entity : entities) {
4040
if (auto contents = entity.GetContents()) {
41-
if (!contents->Render(*content_renderer_, entity, surface,
42-
onscreen_pass)) {
41+
if (!contents->Render(*content_renderer_, entity, surface, parent_pass)) {
4342
return false;
4443
}
4544
}

impeller/entity/entity_renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EntityRenderer {
2424
bool IsValid() const;
2525

2626
[[nodiscard]] bool RenderEntities(const Surface& surface,
27-
RenderPass& onscreen_pass,
27+
RenderPass& parent_pass,
2828
const std::vector<Entity>& entities);
2929

3030
private:

impeller/fixtures/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ impeller_shaders("shader_fixtures") {
1212
shaders = [
1313
"box_fade.vert",
1414
"box_fade.frag",
15+
"test_texture.vert",
16+
"test_texture.frag",
1517
]
1618
}
1719

impeller/fixtures/test_texture.frag

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
out vec4 frag_color;
6+
7+
void main() {
8+
frag_color = vec4(1.0);
9+
}

impeller/fixtures/test_texture.vert

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
uniform FrameInfo {
6+
mat4 mvp;
7+
} frame_info;
8+
9+
in vec2 vtx;
10+
11+
void main() {
12+
gl_Position = frame_info.mvp * vec4(vtx, 0.0, 1.0);
13+
14+
}

impeller/renderer/command.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "impeller/renderer/shader_types.h"
1818
#include "impeller/renderer/texture.h"
1919
#include "impeller/renderer/vertex_buffer.h"
20+
#include "impeller/renderer/vertex_buffer_builder.h"
2021

2122
namespace impeller {
2223

@@ -94,4 +95,41 @@ struct Command {
9495
constexpr operator bool() const { return pipeline && pipeline->IsValid(); }
9596
};
9697

98+
template <class VertexShader_, class FragmentShader_>
99+
struct CommandT {
100+
using VertexShader = VertexShader_;
101+
using FragmentShader = FragmentShader_;
102+
using VertexBufferBuilder =
103+
VertexBufferBuilder<typename VertexShader_::PerVertexData>;
104+
using Pipeline = PipelineT<VertexShader_, FragmentShader_>;
105+
106+
CommandT(PipelineT<VertexShader, FragmentShader>& pipeline) {
107+
command_.label = VertexShader::kLabel;
108+
109+
// This could be moved to the accessor to delay the wait.
110+
command_.pipeline = pipeline.WaitAndGet();
111+
}
112+
113+
static VertexBufferBuilder CreateVertexBuilder() {
114+
VertexBufferBuilder builder;
115+
builder.SetLabel(std::string{VertexShader::kLabel});
116+
return builder;
117+
}
118+
119+
Command& Get() { return command_; }
120+
121+
operator Command&() { return Get(); }
122+
123+
bool BindVertices(VertexBufferBuilder builder, HostBuffer& buffer) {
124+
return command_.BindVertices(builder.CreateVertexBuffer(buffer));
125+
}
126+
127+
bool BindVerticesDynamic(const VertexBuffer& buffer) {
128+
return command_.BindVertices(buffer);
129+
}
130+
131+
private:
132+
Command command_;
133+
};
134+
97135
} // namespace impeller

impeller/renderer/renderer_unittests.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "flutter/fml/time/time_point.h"
66
#include "flutter/impeller/fixtures/box_fade.frag.h"
77
#include "flutter/impeller/fixtures/box_fade.vert.h"
8+
#include "flutter/impeller/fixtures/test_texture.frag.h"
9+
#include "flutter/impeller/fixtures/test_texture.vert.h"
810
#include "flutter/testing/testing.h"
911
#include "impeller/geometry/path_builder.h"
1012
#include "impeller/image/compressed_image.h"
@@ -323,5 +325,49 @@ TEST_F(RendererTest, CanRenderPath) {
323325
// OpenPlaygroundHere(callback);
324326
}
325327

328+
TEST_F(RendererTest, CanPerformStencilOperations) {
329+
using VS = TestTextureVertexShader;
330+
using FS = TestTextureFragmentShader;
331+
using TestTextureCommand = CommandT<VS, FS>;
332+
333+
auto pipeline = std::make_unique<TestTextureCommand::Pipeline>(*GetContext());
334+
335+
auto buffer = HostBuffer::Create();
336+
337+
auto circle_vtx_builder = TestTextureCommand::CreateVertexBuilder();
338+
Tessellator{}.Tessellate(
339+
PathBuilder{}.AddCircle({500, 500}, 250).CreatePath().CreatePolyline(),
340+
[&circle_vtx_builder](Point vtx) {
341+
VS::PerVertexData data;
342+
data.vtx = vtx;
343+
circle_vtx_builder.AppendVertex(data);
344+
});
345+
auto cicle_vertices = circle_vtx_builder.CreateVertexBuffer(*buffer);
346+
347+
auto square_vtx_builder = TestTextureCommand::CreateVertexBuilder();
348+
Tessellator{}.Tessellate(
349+
PathBuilder{}.AddRect({0, 0, 250, 250}).CreatePath().CreatePolyline(),
350+
[&square_vtx_builder](Point vtx) {
351+
VS::PerVertexData data;
352+
data.vtx = vtx;
353+
square_vtx_builder.AppendVertex(data);
354+
});
355+
auto square_vertices = square_vtx_builder.CreateVertexBuffer(*buffer);
356+
357+
OpenPlaygroundHere([&](const Surface& surface, RenderPass& pass) -> bool {
358+
TestTextureCommand command(*pipeline.get());
359+
360+
command.BindVerticesDynamic(cicle_vertices);
361+
362+
VS::FrameInfo info;
363+
info.mvp = Matrix::MakeOrthographic(surface.GetSize());
364+
365+
VS::BindFrameInfo(command, pass.GetTransientsBuffer().EmplaceUniform(info));
366+
367+
pass.AddCommand(command);
368+
return true;
369+
});
370+
}
371+
326372
} // namespace testing
327373
} // namespace impeller

0 commit comments

Comments
 (0)