[Impeller] Make 'DrawPaint' support color sources (flutter/engine#36115)

This commit is contained in:
ColdPaleLight
2022-09-14 16:48:17 +08:00
committed by GitHub
parent 2f2a164055
commit 37461305f1
15 changed files with 152 additions and 119 deletions

View File

@@ -602,6 +602,8 @@ FILE: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.cc
FILE: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.h
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.h
FILE: ../../../flutter/impeller/entity/contents/solid_fill_utils.cc
FILE: ../../../flutter/impeller/entity/contents/solid_fill_utils.h
FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.cc
FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.h
FILE: ../../../flutter/impeller/entity/contents/sweep_gradient_contents.cc

View File

@@ -15,6 +15,7 @@ std::shared_ptr<Contents> Paint::CreateContentsForEntity(Path path,
auto contents = source();
contents->SetPath(std::move(path));
contents->SetAlpha(color.alpha);
contents->SetCover(cover);
return contents;
}

View File

@@ -704,5 +704,57 @@ TEST_P(DisplayListTest, CanDrawWithMatrixFilter) {
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(DisplayListTest, CanDrawPaintWithColorSource) {
const flutter::DlColor colors[2] = {
flutter::DlColor(0xFFF44336),
flutter::DlColor(0xFF2196F3),
};
const float stops[2] = {0.0, 1.0};
flutter::DlPaint paint;
flutter::DisplayListBuilder builder;
auto clip_bounds = SkRect::MakeWH(300.0, 300.0);
builder.save();
builder.translate(100, 100);
builder.clipRect(clip_bounds, SkClipOp::kIntersect, false);
auto linear =
flutter::DlColorSource::MakeLinear({0.0, 0.0}, {100.0, 100.0}, 2, colors,
stops, flutter::DlTileMode::kRepeat);
paint.setColorSource(linear);
builder.drawPaint(paint);
builder.restore();
builder.save();
builder.translate(500, 100);
builder.clipRect(clip_bounds, SkClipOp::kIntersect, false);
auto radial = flutter::DlColorSource::MakeRadial(
{100.0, 100.0}, 100.0, 2, colors, stops, flutter::DlTileMode::kRepeat);
paint.setColorSource(radial);
builder.drawPaint(paint);
builder.restore();
builder.save();
builder.translate(100, 500);
builder.clipRect(clip_bounds, SkClipOp::kIntersect, false);
auto sweep =
flutter::DlColorSource::MakeSweep({100.0, 100.0}, 180.0, 270.0, 2, colors,
stops, flutter::DlTileMode::kRepeat);
paint.setColorSource(sweep);
builder.drawPaint(paint);
builder.restore();
builder.save();
builder.translate(500, 500);
builder.clipRect(clip_bounds, SkClipOp::kIntersect, false);
auto texture = CreateTextureForFixture("table_mountain_nx.png");
auto image = std::make_shared<flutter::DlImageColorSource>(
DlImageImpeller::Make(texture), flutter::DlTileMode::kRepeat,
flutter::DlTileMode::kRepeat);
paint.setColorSource(image);
builder.drawPaint(paint);
builder.restore();
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
} // namespace testing
} // namespace impeller

View File

@@ -109,6 +109,8 @@ impeller_component("entity") {
"contents/rrect_shadow_contents.h",
"contents/solid_color_contents.cc",
"contents/solid_color_contents.h",
"contents/solid_fill_utils.cc",
"contents/solid_fill_utils.h",
"contents/solid_stroke_contents.cc",
"contents/solid_stroke_contents.h",
"contents/sweep_gradient_contents.cc",

View File

@@ -7,7 +7,7 @@
#include "fml/logging.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/formats.h"
@@ -119,7 +119,7 @@ bool ClipContents::Render(const ContentContext& renderer,
}
cmd.pipeline = renderer.GetClipPipeline(options);
cmd.BindVertices(SolidColorContents::CreateSolidFillVertices(
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
path_, pass.GetTransientsBuffer()));
info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *

View File

@@ -37,9 +37,26 @@ const Matrix& ColorSourceContents::GetInverseMatrix() const {
return inverse_matrix_;
}
void ColorSourceContents::SetCover(bool cover) {
cover_ = cover;
}
bool ColorSourceContents::GetCover() const {
return cover_;
}
std::optional<Rect> ColorSourceContents::GetCoverage(
const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};
bool ColorSourceContents::ShouldRender(
const Entity& entity,
const std::optional<Rect>& stencil_coverage) const {
if (!stencil_coverage.has_value()) {
return false;
}
return cover_ || Contents::ShouldRender(entity, stencil_coverage);
}
} // namespace impeller

View File

@@ -23,9 +23,15 @@ class ColorSourceContents : public Contents {
void SetAlpha(Scalar alpha);
void SetCover(bool cover);
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool ShouldRender(const Entity& entity,
const std::optional<Rect>& stencil_coverage) const override;
protected:
const Path& GetPath() const;
@@ -33,10 +39,13 @@ class ColorSourceContents : public Contents {
Scalar GetAlpha() const;
bool GetCover() const;
private:
Path path_;
Matrix inverse_matrix_;
Scalar alpha_ = 1.0;
bool cover_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(ColorSourceContents);
};

View File

@@ -7,11 +7,12 @@
#include "flutter/fml/logging.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/gradient_generator.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
@@ -50,24 +51,6 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
using VS = LinearGradientFillPipeline::VertexShader;
using FS = LinearGradientFillPipeline::FragmentShader;
auto vertices_builder = VertexBufferBuilder<VS::PerVertexData>();
{
auto result = Tessellator{}.Tessellate(GetPath().GetFillType(),
GetPath().CreatePolyline(),
[&vertices_builder](Point point) {
VS::PerVertexData vtx;
vtx.position = point;
vertices_builder.AppendVertex(vtx);
});
if (result == Tessellator::Result::kInputError) {
return true;
}
if (result == Tessellator::Result::kTessellationError) {
return false;
}
}
auto gradient_texture =
CreateGradientTexture(colors_, stops_, renderer.GetContext());
if (gradient_texture == nullptr) {
@@ -92,8 +75,11 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
cmd.pipeline = renderer.GetLinearGradientFillPipeline(
OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(
vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
GetCover()
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: GetPath(),
pass.GetTransientsBuffer()));
cmd.primitive_type = PrimitiveType::kTriangle;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));

View File

@@ -7,10 +7,11 @@
#include "flutter/fml/logging.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/gradient_generator.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
@@ -48,23 +49,6 @@ bool RadialGradientContents::Render(const ContentContext& renderer,
RenderPass& pass) const {
using VS = RadialGradientFillPipeline::VertexShader;
using FS = RadialGradientFillPipeline::FragmentShader;
auto vertices_builder = VertexBufferBuilder<VS::PerVertexData>();
{
auto result = Tessellator{}.Tessellate(GetPath().GetFillType(),
GetPath().CreatePolyline(),
[&vertices_builder](Point point) {
VS::PerVertexData vtx;
vtx.position = point;
vertices_builder.AppendVertex(vtx);
});
if (result == Tessellator::Result::kInputError) {
return true;
}
if (result == Tessellator::Result::kTessellationError) {
return false;
}
}
auto gradient_texture =
CreateGradientTexture(colors_, stops_, renderer.GetContext());
@@ -90,8 +74,11 @@ bool RadialGradientContents::Render(const ContentContext& renderer,
cmd.pipeline = renderer.GetRadialGradientFillPipeline(
OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(
vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
GetCover()
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: GetPath(),
pass.GetTransientsBuffer()));
cmd.primitive_type = PrimitiveType::kTriangle;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));

View File

@@ -5,11 +5,11 @@
#include "solid_color_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
@@ -50,22 +50,6 @@ bool SolidColorContents::ShouldRender(
return cover_ || Contents::ShouldRender(entity, stencil_coverage);
}
VertexBuffer SolidColorContents::CreateSolidFillVertices(const Path& path,
HostBuffer& buffer) {
using VS = SolidFillPipeline::VertexShader;
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
auto tesselation_result = Tessellator{}.Tessellate(
path.GetFillType(), path.CreatePolyline(),
[&vtx_builder](auto point) { vtx_builder.AppendVertex({point}); });
if (tesselation_result != Tessellator::Result::kSuccess) {
return {};
}
return vtx_builder.CreateVertexBuffer(buffer);
}
bool SolidColorContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -78,7 +62,7 @@ bool SolidColorContents::Render(const ContentContext& renderer,
renderer.GetSolidFillPipeline(OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(CreateSolidFillVertices(
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
cover_
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: path_,

View File

@@ -27,9 +27,6 @@ class SolidColorContents final : public Contents {
static std::unique_ptr<SolidColorContents> Make(Path path, Color color);
static VertexBuffer CreateSolidFillVertices(const Path& path,
HostBuffer& buffer);
void SetPath(Path path);
void SetCover(bool cover);

View File

@@ -0,0 +1,11 @@
// 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/solid_fill_utils.h"
namespace impeller {
//
} // namespace impeller

View File

@@ -0,0 +1,26 @@
// 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 "impeller/renderer/vertex_buffer_builder.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
template <typename PerVertexData>
VertexBuffer CreateSolidFillVertices(const Path& path, HostBuffer& buffer) {
VertexBufferBuilder<PerVertexData> vtx_builder;
auto tesselation_result = Tessellator{}.Tessellate(
path.GetFillType(), path.CreatePolyline(),
[&vtx_builder](auto point) { vtx_builder.AppendVertex({point}); });
if (tesselation_result != Tessellator::Result::kSuccess) {
return {};
}
return vtx_builder.CreateVertexBuffer(buffer);
}
} // namespace impeller

View File

@@ -7,10 +7,11 @@
#include "flutter/fml/logging.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/gradient_generator.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
@@ -55,24 +56,6 @@ bool SweepGradientContents::Render(const ContentContext& renderer,
using VS = SweepGradientFillPipeline::VertexShader;
using FS = SweepGradientFillPipeline::FragmentShader;
auto vertices_builder = VertexBufferBuilder<VS::PerVertexData>();
{
auto result = Tessellator{}.Tessellate(GetPath().GetFillType(),
GetPath().CreatePolyline(),
[&vertices_builder](Point point) {
VS::PerVertexData vtx;
vtx.position = point;
vertices_builder.AppendVertex(vtx);
});
if (result == Tessellator::Result::kInputError) {
return true;
}
if (result == Tessellator::Result::kTessellationError) {
return false;
}
}
auto gradient_texture =
CreateGradientTexture(colors_, stops_, renderer.GetContext());
if (gradient_texture == nullptr) {
@@ -98,8 +81,11 @@ bool SweepGradientContents::Render(const ContentContext& renderer,
cmd.pipeline = renderer.GetSweepGradientFillPipeline(
OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(
vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
GetCover()
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: GetPath(),
pass.GetTransientsBuffer()));
cmd.primitive_type = PrimitiveType::kTriangle;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));

View File

@@ -5,11 +5,12 @@
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/solid_fill_utils.h"
#include "impeller/entity/tiled_texture_fill.frag.h"
#include "impeller/entity/tiled_texture_fill.vert.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
@@ -41,43 +42,11 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
using VS = TiledTextureFillVertexShader;
using FS = TiledTextureFillFragmentShader;
const auto coverage_rect = GetPath().GetBoundingBox();
if (!coverage_rect.has_value()) {
return true;
}
if (coverage_rect->size.IsEmpty()) {
return true;
}
const auto texture_size = texture_->GetSize();
if (texture_size.IsEmpty()) {
return true;
}
VertexBufferBuilder<VS::PerVertexData> vertex_builder;
{
const auto tess_result = Tessellator{}.Tessellate(
GetPath().GetFillType(), GetPath().CreatePolyline(),
[&vertex_builder](Point vtx) {
VS::PerVertexData data;
data.position = vtx;
vertex_builder.AppendVertex(data);
});
if (tess_result == Tessellator::Result::kInputError) {
return true;
}
if (tess_result == Tessellator::Result::kTessellationError) {
return false;
}
}
if (!vertex_builder.HasVertices()) {
return true;
}
auto& host_buffer = pass.GetTransientsBuffer();
VS::VertInfo vert_info;
@@ -98,7 +67,11 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
cmd.pipeline =
renderer.GetTiledTexturePipeline(OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
cmd.BindVertices(CreateSolidFillVertices<VS::PerVertexData>(
GetCover()
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: GetPath(),
pass.GetTransientsBuffer()));
VS::BindVertInfo(cmd, host_buffer.EmplaceUniform(vert_info));
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
FS::BindTextureSampler(cmd, texture_,