Implement gradient contents renderer.
This commit is contained in:
@@ -71,10 +71,13 @@ void Canvas::RestoreToCount(size_t count) {
|
||||
void Canvas::DrawPath(Path path, Paint paint) {
|
||||
Color color = paint.color;
|
||||
color.alpha *= paint_stack_.top().color.alpha;
|
||||
|
||||
Entity entity;
|
||||
entity.SetTransformation(GetCurrentTransformation());
|
||||
entity.SetPath(std::move(path));
|
||||
entity.SetBackgroundColor(color);
|
||||
entity.SetContents(std::move(paint.contents));
|
||||
|
||||
ops_.emplace_back(std::move(entity));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
#include "impeller/entity/contents.h"
|
||||
#include "impeller/geometry/color.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -13,6 +14,7 @@ namespace impeller {
|
||||
struct Paint {
|
||||
Color color;
|
||||
Scalar stroke_width = 0.0;
|
||||
std::shared_ptr<Contents> contents;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -47,10 +47,10 @@ class VertexBufferBuilder {
|
||||
return *this;
|
||||
}
|
||||
|
||||
VertexBuffer CreateVertexBuffer(HostBuffer& host) const {
|
||||
VertexBuffer CreateVertexBuffer(HostBuffer& host_buffer) const {
|
||||
VertexBuffer buffer;
|
||||
buffer.vertex_buffer = CreateVertexBufferView(host);
|
||||
buffer.index_buffer = CreateIndexBufferView(host);
|
||||
buffer.vertex_buffer = CreateVertexBufferView(host_buffer);
|
||||
buffer.index_buffer = CreateIndexBufferView(host_buffer);
|
||||
buffer.index_count = GetIndexCount();
|
||||
return buffer;
|
||||
};
|
||||
|
||||
@@ -8,13 +8,19 @@ impeller_shaders("entity_shaders") {
|
||||
name = "entity"
|
||||
|
||||
shaders = [
|
||||
"path.vert",
|
||||
"gradient_fill.frag",
|
||||
"gradient_fill.vert",
|
||||
"solid_fill.frag",
|
||||
"solid_fill.vert",
|
||||
]
|
||||
}
|
||||
|
||||
impeller_component("entity") {
|
||||
sources = [
|
||||
"content_renderer.h",
|
||||
"content_renderer.mm",
|
||||
"contents.h",
|
||||
"contents.mm",
|
||||
"entity.cc",
|
||||
"entity.h",
|
||||
"entity_renderer.h",
|
||||
|
||||
39
engine/src/flutter/impeller/entity/content_renderer.h
Normal file
39
engine/src/flutter/impeller/entity/content_renderer.h
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/pipeline.h"
|
||||
#include "impeller/entity/gradient_fill.frag.h"
|
||||
#include "impeller/entity/gradient_fill.vert.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
using GradientFillPipeline =
|
||||
PipelineT<GradientFillVertexShader, GradientFillFragmentShader>;
|
||||
|
||||
class ContentRenderer {
|
||||
public:
|
||||
ContentRenderer(std::shared_ptr<Context> context);
|
||||
|
||||
~ContentRenderer();
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
std::shared_ptr<Pipeline> GetGradientFillPipeline() const;
|
||||
|
||||
std::shared_ptr<Context> GetContext() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Context> context_;
|
||||
std::unique_ptr<GradientFillPipeline> gradient_fill_pipeline_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
37
engine/src/flutter/impeller/entity/content_renderer.mm
Normal file
37
engine/src/flutter/impeller/entity/content_renderer.mm
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#include "impeller/entity/content_renderer.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
ContentRenderer::ContentRenderer(std::shared_ptr<Context> context)
|
||||
: context_(std::move(context)) {
|
||||
if (!context_ || !context_->IsValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
gradient_fill_pipeline_ = std::make_unique<GradientFillPipeline>(*context_);
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
ContentRenderer::~ContentRenderer() = default;
|
||||
|
||||
bool ContentRenderer::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
std::shared_ptr<Context> ContentRenderer::GetContext() const {
|
||||
return context_;
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> ContentRenderer::GetGradientFillPipeline() const {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
return gradient_fill_pipeline_->WaitAndGet();
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
61
engine/src/flutter/impeller/entity/contents.h
Normal file
61
engine/src/flutter/impeller/entity/contents.h
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/geometry/color.h"
|
||||
#include "impeller/geometry/point.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ContentRenderer;
|
||||
class Entity;
|
||||
class Surface;
|
||||
class RenderPass;
|
||||
|
||||
class Contents {
|
||||
public:
|
||||
Contents();
|
||||
|
||||
~Contents();
|
||||
|
||||
virtual bool Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const = 0;
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Contents);
|
||||
};
|
||||
|
||||
class LinearGradientContents final : public Contents {
|
||||
public:
|
||||
LinearGradientContents();
|
||||
|
||||
~LinearGradientContents();
|
||||
|
||||
// |Contents|
|
||||
bool Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const override;
|
||||
|
||||
void SetEndPoints(Point start_point, Point end_point);
|
||||
|
||||
void SetColors(std::vector<Color> colors);
|
||||
|
||||
const std::vector<Color>& GetColors() const;
|
||||
|
||||
private:
|
||||
Point start_point_;
|
||||
Point end_point_;
|
||||
std::vector<Color> colors_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
88
engine/src/flutter/impeller/entity/contents.mm
Normal file
88
engine/src/flutter/impeller/entity/contents.mm
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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.
|
||||
|
||||
#include "impeller/entity/contents.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "impeller/compositor/render_pass.h"
|
||||
#include "impeller/compositor/surface.h"
|
||||
#include "impeller/compositor/tessellator.h"
|
||||
#include "impeller/compositor/vertex_buffer_builder.h"
|
||||
#include "impeller/entity/content_renderer.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Contents::Contents() = default;
|
||||
|
||||
Contents::~Contents() = default;
|
||||
|
||||
LinearGradientContents::LinearGradientContents() = default;
|
||||
|
||||
LinearGradientContents::~LinearGradientContents() = default;
|
||||
|
||||
void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
|
||||
start_point_ = start_point;
|
||||
end_point_ = end_point;
|
||||
}
|
||||
|
||||
void LinearGradientContents::SetColors(std::vector<Color> colors) {
|
||||
colors_ = std::move(colors);
|
||||
if (colors_.empty()) {
|
||||
colors_.push_back(Color::Black());
|
||||
colors_.push_back(Color::Black());
|
||||
} else if (colors_.size() < 2u) {
|
||||
colors_.push_back(colors_.back());
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<Color>& LinearGradientContents::GetColors() const {
|
||||
return colors_;
|
||||
}
|
||||
|
||||
// |Contents|
|
||||
bool LinearGradientContents::Render(const ContentRenderer& renderer,
|
||||
const Entity& entity,
|
||||
const Surface& surface,
|
||||
RenderPass& pass) const {
|
||||
using VS = GradientFillPipeline::VertexShader;
|
||||
using FS = GradientFillPipeline::FragmentShader;
|
||||
|
||||
auto vertices_builder = VertexBufferBuilder<VS::PerVertexData>();
|
||||
{
|
||||
auto result =
|
||||
Tessellator{}.Tessellate(entity.GetPath().SubdivideAdaptively(),
|
||||
[&vertices_builder](Point point) {
|
||||
VS::PerVertexData vtx;
|
||||
vtx.vertices = point;
|
||||
vertices_builder.AppendVertex(vtx);
|
||||
});
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp =
|
||||
Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation();
|
||||
|
||||
FS::GradientInfo gradient_info;
|
||||
gradient_info.start_point = start_point_;
|
||||
gradient_info.end_point = end_point_;
|
||||
gradient_info.start_color = colors_[0];
|
||||
gradient_info.end_color = colors_[1];
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "LinearGradientFill";
|
||||
cmd.pipeline = renderer.GetGradientFillPipeline();
|
||||
cmd.BindVertices(vertices_builder.CreateVertexBuffer(
|
||||
*renderer.GetContext()->GetPermanentsAllocator()));
|
||||
cmd.primitive_type = PrimitiveType::kTriangle;
|
||||
FS::BindGradientInfo(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));
|
||||
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
|
||||
return pass.RecordCommand(std::move(cmd));
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -58,11 +58,22 @@ bool Entity::IsClip() const {
|
||||
return is_clip_;
|
||||
}
|
||||
|
||||
void Entity::SetContents(std::shared_ptr<Contents> contents) {
|
||||
contents_ = std::move(contents);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "impeller/entity/contents.h"
|
||||
#include "impeller/geometry/color.h"
|
||||
#include "impeller/geometry/matrix.h"
|
||||
#include "impeller/geometry/path.h"
|
||||
@@ -48,9 +49,14 @@ class Entity {
|
||||
|
||||
bool HasRenderableContents() const;
|
||||
|
||||
void SetContents(std::shared_ptr<Contents> contents);
|
||||
|
||||
const std::shared_ptr<Contents>& GetContents() const;
|
||||
|
||||
private:
|
||||
Matrix transformation_;
|
||||
Color background_color_;
|
||||
std::shared_ptr<Contents> contents_;
|
||||
|
||||
Path path_;
|
||||
Color stroke_color_;
|
||||
|
||||
@@ -30,29 +30,14 @@ bool EntityRenderer::RenderEntities(const Surface& surface,
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t success = 0u;
|
||||
size_t failure = 0u;
|
||||
size_t skipped = 0u;
|
||||
|
||||
for (const auto& entity : entities) {
|
||||
auto result = renderer_->RenderEntity(surface, onscreen_pass, entity);
|
||||
switch (result) {
|
||||
case EntityRendererImpl::RenderResult::kSuccess:
|
||||
success++;
|
||||
break;
|
||||
case EntityRendererImpl::RenderResult::kFailure:
|
||||
failure++;
|
||||
break;
|
||||
case EntityRendererImpl::RenderResult::kSkipped:
|
||||
skipped++;
|
||||
break;
|
||||
if (renderer_->RenderEntity(surface, onscreen_pass, entity) ==
|
||||
EntityRendererImpl::RenderResult::kFailure) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
FML_LOG(ERROR) << "Success " << success << " failure " << failure
|
||||
<< " skipped " << skipped << " total " << entities.size();
|
||||
|
||||
return failure == 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -12,9 +12,10 @@
|
||||
#include "impeller/compositor/pipeline_builder.h"
|
||||
#include "impeller/compositor/render_pass.h"
|
||||
#include "impeller/compositor/surface.h"
|
||||
#include "impeller/entity/content_renderer.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/entity/path.vert.h"
|
||||
#include "impeller/entity/solid_fill.frag.h"
|
||||
#include "impeller/entity/solid_fill.vert.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -41,10 +42,11 @@ class EntityRendererImpl {
|
||||
|
||||
private:
|
||||
using SolidFillPipeline =
|
||||
PipelineT<PathVertexShader, SolidFillFragmentShader>;
|
||||
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;
|
||||
|
||||
|
||||
@@ -16,6 +16,11 @@ EntityRendererImpl::EntityRendererImpl(std::shared_ptr<Context> context)
|
||||
return;
|
||||
}
|
||||
|
||||
content_renderer_ = std::make_unique<ContentRenderer>(context_);
|
||||
if (!content_renderer_->IsValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
|
||||
|
||||
is_valid_ = true;
|
||||
@@ -32,13 +37,10 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity(
|
||||
RenderPass& pass,
|
||||
const Entity& entity) {
|
||||
if (!entity.HasRenderableContents()) {
|
||||
if (entity.GetPath().GetBoundingBox().IsZero()) {
|
||||
FML_LOG(ERROR) << "Skipped because bounds box zero.";
|
||||
}
|
||||
return RenderResult::kSkipped;
|
||||
}
|
||||
|
||||
if (entity.HasContents() && !entity.IsClip()) {
|
||||
if (entity.HasContents() && !entity.IsClip() && !entity.GetContents()) {
|
||||
using VS = SolidFillPipeline::VertexShader;
|
||||
|
||||
Command cmd;
|
||||
@@ -50,7 +52,6 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity(
|
||||
|
||||
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
|
||||
{
|
||||
TRACE_EVENT0("flutter", "Tesselate");
|
||||
auto tesselation_result = Tessellator{}.Tessellate(
|
||||
entity.GetPath().SubdivideAdaptively(), [&vtx_builder](auto point) {
|
||||
VS::PerVertexData vtx;
|
||||
@@ -77,6 +78,12 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity(
|
||||
if (!pass.RecordCommand(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;
|
||||
|
||||
24
engine/src/flutter/impeller/entity/gradient_fill.frag
Normal file
24
engine/src/flutter/impeller/entity/gradient_fill.frag
Normal file
@@ -0,0 +1,24 @@
|
||||
// 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 GradientInfo {
|
||||
vec2 start_point;
|
||||
vec2 end_point;
|
||||
vec4 start_color;
|
||||
vec4 end_color;
|
||||
} gradient_info;
|
||||
|
||||
in vec2 interpolated_vertices;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
float len = length(gradient_info.end_point - gradient_info.start_point);
|
||||
float dot = dot(
|
||||
interpolated_vertices - gradient_info.start_point,
|
||||
gradient_info.end_point - gradient_info.start_point
|
||||
);
|
||||
float interp = dot / (len * len);
|
||||
frag_color = mix(gradient_info.start_color, gradient_info.end_color, interp);
|
||||
}
|
||||
16
engine/src/flutter/impeller/entity/gradient_fill.vert
Normal file
16
engine/src/flutter/impeller/entity/gradient_fill.vert
Normal file
@@ -0,0 +1,16 @@
|
||||
// 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 vertices;
|
||||
|
||||
out vec2 interpolated_vertices;
|
||||
|
||||
void main() {
|
||||
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
|
||||
interpolated_vertices = vertices;
|
||||
}
|
||||
Reference in New Issue
Block a user