Skip to content

Commit

Permalink
Add support for typed commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 9ce9a81 commit 0570581
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 10 deletions.
2 changes: 1 addition & 1 deletion impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_F(AiksTest, CanRenderClips) {
canvas.ClipPath(
PathBuilder{}.AddRect(Rect::MakeXYWH(0, 0, 100, 100)).CreatePath());
canvas.DrawPath(PathBuilder{}.AddCircle({100, 100}, 50).CreatePath(), paint);
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

} // namespace testing
Expand Down
6 changes: 5 additions & 1 deletion impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
}

void Canvas::ClipPath(Path path) {
passes_.push_back({});
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetContents(std::make_shared<ClipContents>());
GetCurrentPass().PushEntity(std::move(entity));
}

void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {}
Expand Down
8 changes: 8 additions & 0 deletions impeller/aiks/canvas_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ const std::vector<Entity>& CanvasPass::GetPassEntities() const {
return ops_;
}

void CanvasPass::SetPostProcessingEntity(Entity entity) {
post_processing_entity_ = std::move(entity);
}

const Entity& CanvasPass::GetPostProcessingEntity() const {
return post_processing_entity_;
}

} // namespace impeller
8 changes: 7 additions & 1 deletion impeller/aiks/canvas_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#pragma once

#include <vector>
#include <memory>

#include "flutter/fml/macros.h"
#include "impeller/entity/contents.h"
#include "impeller/entity/entity.h"

namespace impeller {
Expand All @@ -21,8 +22,13 @@ class CanvasPass {

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

void SetPostProcessingEntity(Entity entity);

const Entity& GetPostProcessingEntity() const;

private:
std::vector<Entity> ops_;
Entity post_processing_entity_;
};

} // namespace impeller
4 changes: 2 additions & 2 deletions impeller/aiks/picture_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ bool PictureRenderer::IsValid() const {
}

bool PictureRenderer::Render(const Surface& surface,
RenderPass& onscreen_pass,
RenderPass& parent_pass,
const Picture& picture) {
if (!IsValid()) {
return false;
}

for (const auto& pass : picture.passes) {
if (!entity_renderer_.RenderEntities(surface, onscreen_pass,
if (!entity_renderer_.RenderEntities(surface, parent_pass,
pass.GetPassEntities())) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion impeller/aiks/picture_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PictureRenderer {
bool IsValid() const;

[[nodiscard]] bool Render(const Surface& surface,
RenderPass& onscreen_pass,
RenderPass& parent_pass,
const Picture& picture);

private:
Expand Down
15 changes: 15 additions & 0 deletions impeller/entity/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,19 @@ Scalar SolidStrokeContents::GetStrokeSize() const {
return stroke_size_;
}

/*******************************************************************************
******* ClipContents
******************************************************************************/

ClipContents::ClipContents() = default;

ClipContents::~ClipContents() = default;

bool ClipContents::Render(const ContentRenderer& renderer,
const Entity& entity,
const Surface& surface,
RenderPass& pass) const {
return true;
}

} // namespace impeller
16 changes: 16 additions & 0 deletions impeller/entity/contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,20 @@ class SolidStrokeContents final : public Contents {
FML_DISALLOW_COPY_AND_ASSIGN(SolidStrokeContents);
};

class ClipContents final : public Contents {
public:
ClipContents();

~ClipContents();

// |Contents|
bool Render(const ContentRenderer& renderer,
const Entity& entity,
const Surface& surface,
RenderPass& pass) const override;

private:
FML_DISALLOW_COPY_AND_ASSIGN(ClipContents);
};

} // namespace impeller
5 changes: 2 additions & 3 deletions impeller/entity/entity_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ bool EntityRenderer::IsValid() const {
}

bool EntityRenderer::RenderEntities(const Surface& surface,
RenderPass& onscreen_pass,
RenderPass& parent_pass,
const std::vector<Entity>& entities) {
if (!IsValid()) {
return false;
}

for (const auto& entity : entities) {
if (auto contents = entity.GetContents()) {
if (!contents->Render(*content_renderer_, entity, surface,
onscreen_pass)) {
if (!contents->Render(*content_renderer_, entity, surface, parent_pass)) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/entity_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EntityRenderer {
bool IsValid() const;

[[nodiscard]] bool RenderEntities(const Surface& surface,
RenderPass& onscreen_pass,
RenderPass& parent_pass,
const std::vector<Entity>& entities);

private:
Expand Down
2 changes: 2 additions & 0 deletions impeller/fixtures/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ impeller_shaders("shader_fixtures") {
shaders = [
"box_fade.vert",
"box_fade.frag",
"test_texture.vert",
"test_texture.frag",
]
}

Expand Down
9 changes: 9 additions & 0 deletions impeller/fixtures/test_texture.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

out vec4 frag_color;

void main() {
frag_color = vec4(1.0);
}
14 changes: 14 additions & 0 deletions impeller/fixtures/test_texture.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

uniform FrameInfo {
mat4 mvp;
} frame_info;

in vec2 vtx;

void main() {
gl_Position = frame_info.mvp * vec4(vtx, 0.0, 1.0);

}
38 changes: 38 additions & 0 deletions impeller/renderer/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "impeller/renderer/shader_types.h"
#include "impeller/renderer/texture.h"
#include "impeller/renderer/vertex_buffer.h"
#include "impeller/renderer/vertex_buffer_builder.h"

namespace impeller {

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

template <class VertexShader_, class FragmentShader_>
struct CommandT {
using VertexShader = VertexShader_;
using FragmentShader = FragmentShader_;
using VertexBufferBuilder =
VertexBufferBuilder<typename VertexShader_::PerVertexData>;
using Pipeline = PipelineT<VertexShader_, FragmentShader_>;

CommandT(PipelineT<VertexShader, FragmentShader>& pipeline) {
command_.label = VertexShader::kLabel;

// This could be moved to the accessor to delay the wait.
command_.pipeline = pipeline.WaitAndGet();
}

static VertexBufferBuilder CreateVertexBuilder() {
VertexBufferBuilder builder;
builder.SetLabel(std::string{VertexShader::kLabel});
return builder;
}

Command& Get() { return command_; }

operator Command&() { return Get(); }

bool BindVertices(VertexBufferBuilder builder, HostBuffer& buffer) {
return command_.BindVertices(builder.CreateVertexBuffer(buffer));
}

bool BindVerticesDynamic(const VertexBuffer& buffer) {
return command_.BindVertices(buffer);
}

private:
Command command_;
};

} // namespace impeller
46 changes: 46 additions & 0 deletions impeller/renderer/renderer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "flutter/fml/time/time_point.h"
#include "flutter/impeller/fixtures/box_fade.frag.h"
#include "flutter/impeller/fixtures/box_fade.vert.h"
#include "flutter/impeller/fixtures/test_texture.frag.h"
#include "flutter/impeller/fixtures/test_texture.vert.h"
#include "flutter/testing/testing.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/image/compressed_image.h"
Expand Down Expand Up @@ -323,5 +325,49 @@ TEST_F(RendererTest, CanRenderPath) {
// OpenPlaygroundHere(callback);
}

TEST_F(RendererTest, CanPerformStencilOperations) {
using VS = TestTextureVertexShader;
using FS = TestTextureFragmentShader;
using TestTextureCommand = CommandT<VS, FS>;

auto pipeline = std::make_unique<TestTextureCommand::Pipeline>(*GetContext());

auto buffer = HostBuffer::Create();

auto circle_vtx_builder = TestTextureCommand::CreateVertexBuilder();
Tessellator{}.Tessellate(
PathBuilder{}.AddCircle({500, 500}, 250).CreatePath().CreatePolyline(),
[&circle_vtx_builder](Point vtx) {
VS::PerVertexData data;
data.vtx = vtx;
circle_vtx_builder.AppendVertex(data);
});
auto cicle_vertices = circle_vtx_builder.CreateVertexBuffer(*buffer);

auto square_vtx_builder = TestTextureCommand::CreateVertexBuilder();
Tessellator{}.Tessellate(
PathBuilder{}.AddRect({0, 0, 250, 250}).CreatePath().CreatePolyline(),
[&square_vtx_builder](Point vtx) {
VS::PerVertexData data;
data.vtx = vtx;
square_vtx_builder.AppendVertex(data);
});
auto square_vertices = square_vtx_builder.CreateVertexBuffer(*buffer);

OpenPlaygroundHere([&](const Surface& surface, RenderPass& pass) -> bool {
TestTextureCommand command(*pipeline.get());

command.BindVerticesDynamic(cicle_vertices);

VS::FrameInfo info;
info.mvp = Matrix::MakeOrthographic(surface.GetSize());

VS::BindFrameInfo(command, pass.GetTransientsBuffer().EmplaceUniform(info));

pass.AddCommand(command);
return true;
});
}

} // namespace testing
} // namespace impeller

0 comments on commit 0570581

Please sign in to comment.