Start consolidating content rendering.
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
# Defines the Chromium style for automatic reformatting.
|
||||
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
||||
BasedOnStyle: Chromium
|
||||
# This defaults to 'Auto'. Explicitly set it for a while, so that
|
||||
# 'vector<vector<int> >' in existing files gets formatted to
|
||||
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
|
||||
# 'int>>' if the file already contains at least one such instance.)
|
||||
Standard: Cpp11
|
||||
Standard: c++17
|
||||
EmptyLineBeforeAccessModifier: Always
|
||||
|
||||
@@ -13,6 +13,7 @@ ContentRenderer::ContentRenderer(std::shared_ptr<Context> context)
|
||||
}
|
||||
|
||||
gradient_fill_pipeline_ = std::make_unique<GradientFillPipeline>(*context_);
|
||||
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
@@ -34,4 +35,12 @@ std::shared_ptr<Pipeline> ContentRenderer::GetGradientFillPipeline() const {
|
||||
return gradient_fill_pipeline_->WaitAndGet();
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> ContentRenderer::GetSolidFillPipeline() const {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return solid_fill_pipeline_->WaitAndGet();
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -9,12 +9,16 @@
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/impeller/entity/gradient_fill.frag.h"
|
||||
#include "flutter/impeller/entity/gradient_fill.vert.h"
|
||||
#include "flutter/impeller/entity/solid_fill.frag.h"
|
||||
#include "flutter/impeller/entity/solid_fill.vert.h"
|
||||
#include "impeller/renderer/pipeline.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
using GradientFillPipeline =
|
||||
PipelineT<GradientFillVertexShader, GradientFillFragmentShader>;
|
||||
using SolidFillPipeline =
|
||||
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
|
||||
|
||||
class ContentRenderer {
|
||||
public:
|
||||
@@ -26,11 +30,14 @@ class ContentRenderer {
|
||||
|
||||
std::shared_ptr<Pipeline> GetGradientFillPipeline() const;
|
||||
|
||||
std::shared_ptr<Pipeline> GetSolidFillPipeline() const;
|
||||
|
||||
std::shared_ptr<Context> GetContext() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Context> context_;
|
||||
std::unique_ptr<GradientFillPipeline> gradient_fill_pipeline_;
|
||||
std::unique_ptr<SolidFillPipeline> solid_fill_pipeline_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer);
|
||||
|
||||
@@ -14,10 +14,18 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
/*******************************************************************************
|
||||
******* Contents
|
||||
******************************************************************************/
|
||||
|
||||
Contents::Contents() = default;
|
||||
|
||||
Contents::~Contents() = default;
|
||||
|
||||
/*******************************************************************************
|
||||
******* Linear Gradient Contents
|
||||
******************************************************************************/
|
||||
|
||||
LinearGradientContents::LinearGradientContents() = default;
|
||||
|
||||
LinearGradientContents::~LinearGradientContents() = default;
|
||||
@@ -41,7 +49,6 @@ const std::vector<Color>& LinearGradientContents::GetColors() const {
|
||||
return colors_;
|
||||
}
|
||||
|
||||
// |Contents|
|
||||
bool LinearGradientContents::Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
@@ -84,4 +91,72 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer,
|
||||
return pass.AddCommand(std::move(cmd));
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
******* SolidColorContents
|
||||
******************************************************************************/
|
||||
|
||||
void SolidColorContents::SetColor(Color color) {
|
||||
color_ = color;
|
||||
}
|
||||
|
||||
const Color& SolidColorContents::GetColor() const {
|
||||
return color_;
|
||||
}
|
||||
|
||||
bool SolidColorContents::Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const {
|
||||
using VS = SolidFillPipeline::VertexShader;
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "SolidFill";
|
||||
cmd.pipeline = renderer.GetSolidFillPipeline();
|
||||
if (cmd.pipeline == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
|
||||
{
|
||||
auto tesselation_result = Tessellator{}.Tessellate(
|
||||
entity.GetPath().CreatePolyline(), [&vtx_builder](auto point) {
|
||||
VS::PerVertexData vtx;
|
||||
vtx.vertices = point;
|
||||
vtx_builder.AppendVertex(vtx);
|
||||
});
|
||||
if (!tesselation_result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
cmd.BindVertices(vtx_builder.CreateVertexBuffer(
|
||||
*renderer.GetContext()->GetPermanentsAllocator()));
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp =
|
||||
Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation();
|
||||
frame_info.color = entity.GetBackgroundColor();
|
||||
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
|
||||
|
||||
cmd.primitive_type = PrimitiveType::kTriangle;
|
||||
|
||||
if (!pass.AddCommand(std::move(cmd))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
******* SolidStrokeContents
|
||||
******************************************************************************/
|
||||
|
||||
void SolidStrokeContents::SetColor(Color color) {
|
||||
color_ = color;
|
||||
}
|
||||
|
||||
const Color& SolidStrokeContents::GetColor() const {
|
||||
return color_;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -21,7 +21,7 @@ class Contents {
|
||||
public:
|
||||
Contents();
|
||||
|
||||
~Contents();
|
||||
virtual ~Contents();
|
||||
|
||||
virtual bool Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
@@ -36,7 +36,7 @@ class LinearGradientContents final : public Contents {
|
||||
public:
|
||||
LinearGradientContents();
|
||||
|
||||
~LinearGradientContents();
|
||||
~LinearGradientContents() override;
|
||||
|
||||
// |Contents|
|
||||
bool Render(const ContentRenderer& renderer,
|
||||
@@ -58,4 +58,53 @@ class LinearGradientContents final : public Contents {
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents);
|
||||
};
|
||||
|
||||
class SolidColorContents final : public Contents {
|
||||
public:
|
||||
SolidColorContents();
|
||||
|
||||
~SolidColorContents() override;
|
||||
|
||||
void SetColor(Color color);
|
||||
|
||||
const Color& GetColor() const;
|
||||
|
||||
// |Contents|
|
||||
bool Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const override;
|
||||
|
||||
private:
|
||||
Color color_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SolidColorContents);
|
||||
};
|
||||
|
||||
class SolidStrokeContents final : public Contents {
|
||||
public:
|
||||
SolidStrokeContents();
|
||||
|
||||
~SolidStrokeContents();
|
||||
|
||||
void SetColor(Color color);
|
||||
|
||||
const Color& GetColor() const;
|
||||
|
||||
void SetStrokeSize(Scalar size) { stroke_size_ = size; }
|
||||
|
||||
Scalar GetStrokeSize() const { return stroke_size_; }
|
||||
|
||||
// |Contents|
|
||||
bool Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const override;
|
||||
|
||||
private:
|
||||
Color color_;
|
||||
Scalar stroke_size_ = 0.0;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SolidStrokeContents);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -66,37 +66,4 @@ const std::shared_ptr<Contents>& Entity::GetContents() const {
|
||||
return contents_;
|
||||
}
|
||||
|
||||
bool Entity::HasStroke() const {
|
||||
return stroke_size_ > 0.0 && !stroke_color_.IsTransparent();
|
||||
}
|
||||
|
||||
bool Entity::HasContents() const {
|
||||
if (contents_) {
|
||||
return true;
|
||||
}
|
||||
return !background_color_.IsTransparent();
|
||||
}
|
||||
|
||||
bool Entity::HasRenderableContents() const {
|
||||
const bool has_empty_path = path_.GetBoundingBox().IsZero();
|
||||
|
||||
if (has_empty_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsClip()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (HasStroke()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (HasContents()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -43,12 +43,6 @@ class Entity {
|
||||
|
||||
bool IsClip() const;
|
||||
|
||||
bool HasStroke() const;
|
||||
|
||||
bool HasContents() const;
|
||||
|
||||
bool HasRenderableContents() const;
|
||||
|
||||
void SetContents(std::shared_ptr<Contents> contents);
|
||||
|
||||
const std::shared_ptr<Contents>& GetContents() const;
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
#include "flutter/impeller/entity/entity_renderer.h"
|
||||
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/renderer/tessellator.h"
|
||||
#include "impeller/renderer/vertex_buffer_builder.h"
|
||||
#include "impeller/entity/content_renderer.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -21,8 +20,6 @@ EntityRenderer::EntityRenderer(std::shared_ptr<Context> context)
|
||||
return;
|
||||
}
|
||||
|
||||
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -40,8 +37,8 @@ bool EntityRenderer::RenderEntities(const Surface& surface,
|
||||
}
|
||||
|
||||
for (const auto& entity : entities) {
|
||||
if (RenderEntity(surface, onscreen_pass, entity) ==
|
||||
EntityRenderer::RenderResult::kFailure) {
|
||||
if (auto contents = entity.GetContents();
|
||||
!contents->Render(*content_renderer_, entity, surface, onscreen_pass)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -49,61 +46,4 @@ bool EntityRenderer::RenderEntities(const Surface& surface,
|
||||
return true;
|
||||
}
|
||||
|
||||
EntityRenderer::RenderResult EntityRenderer::RenderEntity(
|
||||
const Surface& surface,
|
||||
RenderPass& pass,
|
||||
const Entity& entity) {
|
||||
if (!entity.HasRenderableContents()) {
|
||||
return RenderResult::kSkipped;
|
||||
}
|
||||
|
||||
if (entity.HasContents() && !entity.IsClip() && !entity.GetContents()) {
|
||||
using VS = SolidFillPipeline::VertexShader;
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "SolidFill";
|
||||
cmd.pipeline = solid_fill_pipeline_->WaitAndGet();
|
||||
if (cmd.pipeline == nullptr) {
|
||||
return RenderResult::kFailure;
|
||||
}
|
||||
|
||||
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
|
||||
{
|
||||
auto tesselation_result = Tessellator{}.Tessellate(
|
||||
entity.GetPath().CreatePolyline(), [&vtx_builder](auto point) {
|
||||
VS::PerVertexData vtx;
|
||||
vtx.vertices = point;
|
||||
vtx_builder.AppendVertex(vtx);
|
||||
});
|
||||
if (!tesselation_result) {
|
||||
return RenderResult::kFailure;
|
||||
}
|
||||
}
|
||||
|
||||
cmd.BindVertices(
|
||||
vtx_builder.CreateVertexBuffer(*context_->GetPermanentsAllocator()));
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(surface.GetSize()) *
|
||||
entity.GetTransformation();
|
||||
frame_info.color = entity.GetBackgroundColor();
|
||||
VS::BindFrameInfo(cmd,
|
||||
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
|
||||
|
||||
cmd.primitive_type = PrimitiveType::kTriangle;
|
||||
|
||||
if (!pass.AddCommand(std::move(cmd))) {
|
||||
return RenderResult::kFailure;
|
||||
}
|
||||
} else if (entity.GetContents()) {
|
||||
auto result =
|
||||
entity.GetContents()->Render(*content_renderer_, entity, surface, pass);
|
||||
if (!result) {
|
||||
return RenderResult::kFailure;
|
||||
}
|
||||
}
|
||||
|
||||
return RenderResult::kSuccess;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -7,18 +7,14 @@
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/impeller/entity/solid_fill.frag.h"
|
||||
#include "flutter/impeller/entity/solid_fill.vert.h"
|
||||
#include "impeller/entity/content_renderer.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/renderer/context.h"
|
||||
#include "impeller/renderer/pipeline.h"
|
||||
#include "impeller/renderer/pipeline_builder.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/surface.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ContentRenderer;
|
||||
|
||||
class EntityRenderer {
|
||||
public:
|
||||
EntityRenderer(std::shared_ptr<Context> context);
|
||||
@@ -31,22 +27,8 @@ class EntityRenderer {
|
||||
RenderPass& onscreen_pass,
|
||||
const std::vector<Entity>& entities);
|
||||
|
||||
enum class RenderResult {
|
||||
kSkipped,
|
||||
kSuccess,
|
||||
kFailure,
|
||||
};
|
||||
|
||||
[[nodiscard]] RenderResult RenderEntity(const Surface& surface,
|
||||
RenderPass& onscreen_pass,
|
||||
const Entity& entities);
|
||||
|
||||
private:
|
||||
using SolidFillPipeline =
|
||||
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
|
||||
|
||||
std::shared_ptr<Context> context_;
|
||||
std::unique_ptr<SolidFillPipeline> solid_fill_pipeline_;
|
||||
std::unique_ptr<ContentRenderer> content_renderer_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
|
||||
@@ -3,11 +3,24 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/geometry/path_builder.h"
|
||||
#include "impeller/playground/playground.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
|
||||
//
|
||||
using EntityTest = Playground;
|
||||
|
||||
TEST_F(EntityTest, CanCreateEntity) {
|
||||
Entity entity;
|
||||
ASSERT_TRUE(entity.GetTransformation().IsIdentity());
|
||||
}
|
||||
|
||||
TEST_F(EntityTest, CanDrawRect) {
|
||||
Entity entity;
|
||||
entity.SetPath(PathBuilder{}.AddRect({100, 100, 100, 100}).CreatePath());
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
Reference in New Issue
Block a user