Add support for typed commands.

This commit is contained in:
Chinmay Garde
2021-11-19 15:03:27 -08:00
committed by Dan Field
parent b0fbe2252b
commit 9f2632abb2
15 changed files with 167 additions and 10 deletions

View File

@@ -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

View File

@@ -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) {}

View File

@@ -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

View File

@@ -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 {
@@ -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

View File

@@ -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;
}

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -30,7 +30,7 @@ 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;
@@ -38,8 +38,7 @@ bool EntityRenderer::RenderEntities(const Surface& surface,
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;
}
}

View File

@@ -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:

View File

@@ -12,6 +12,8 @@ impeller_shaders("shader_fixtures") {
shaders = [
"box_fade.vert",
"box_fade.frag",
"test_texture.vert",
"test_texture.frag",
]
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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 {
@@ -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

View File

@@ -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"
@@ -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