From 37461305f14fbb2f245b5f20f202c3d22a97ec66 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Wed, 14 Sep 2022 16:48:17 +0800 Subject: [PATCH] [Impeller] Make 'DrawPaint' support color sources (flutter/engine#36115) --- .../ci/licenses_golden/licenses_flutter | 2 + engine/src/flutter/impeller/aiks/paint.cc | 1 + .../display_list/display_list_unittests.cc | 52 +++++++++++++++++++ engine/src/flutter/impeller/entity/BUILD.gn | 2 + .../impeller/entity/contents/clip_contents.cc | 4 +- .../entity/contents/color_source_contents.cc | 17 ++++++ .../entity/contents/color_source_contents.h | 9 ++++ .../contents/linear_gradient_contents.cc | 28 +++------- .../contents/radial_gradient_contents.cc | 27 +++------- .../entity/contents/solid_color_contents.cc | 20 +------ .../entity/contents/solid_color_contents.h | 3 -- .../entity/contents/solid_fill_utils.cc | 11 ++++ .../entity/contents/solid_fill_utils.h | 26 ++++++++++ .../contents/sweep_gradient_contents.cc | 28 +++------- .../entity/contents/tiled_texture_contents.cc | 41 +++------------ 15 files changed, 152 insertions(+), 119 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/contents/solid_fill_utils.cc create mode 100644 engine/src/flutter/impeller/entity/contents/solid_fill_utils.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 679a41aeb9..c217b75c43 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -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 diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 8b5487e3c2..238ec21025 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -15,6 +15,7 @@ std::shared_ptr Paint::CreateContentsForEntity(Path path, auto contents = source(); contents->SetPath(std::move(path)); contents->SetAlpha(color.alpha); + contents->SetCover(cover); return contents; } diff --git a/engine/src/flutter/impeller/display_list/display_list_unittests.cc b/engine/src/flutter/impeller/display_list/display_list_unittests.cc index 93f4213be0..d42c2c3fd0 100644 --- a/engine/src/flutter/impeller/display_list/display_list_unittests.cc +++ b/engine/src/flutter/impeller/display_list/display_list_unittests.cc @@ -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( + 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 diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index c0ab517f49..10bc6ce9a3 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -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", diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.cc b/engine/src/flutter/impeller/entity/contents/clip_contents.cc index b677864a30..b9928fe84d 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.cc @@ -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( path_, pass.GetTransientsBuffer())); info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.cc b/engine/src/flutter/impeller/entity/contents/color_source_contents.cc index 69add3ea15..359a0e1dc5 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.cc @@ -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 ColorSourceContents::GetCoverage( const Entity& entity) const { return path_.GetTransformedBoundingBox(entity.GetTransformation()); }; +bool ColorSourceContents::ShouldRender( + const Entity& entity, + const std::optional& stencil_coverage) const { + if (!stencil_coverage.has_value()) { + return false; + } + return cover_ || Contents::ShouldRender(entity, stencil_coverage); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.h b/engine/src/flutter/impeller/entity/contents/color_source_contents.h index 3f2f4268df..4e01f46843 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.h +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.h @@ -23,9 +23,15 @@ class ColorSourceContents : public Contents { void SetAlpha(Scalar alpha); + void SetCover(bool cover); + // |Contents| std::optional GetCoverage(const Entity& entity) const override; + // |Contents| + bool ShouldRender(const Entity& entity, + const std::optional& 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); }; diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index bf639ac839..55fd5a37c8 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -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(); - { - 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( + GetCover() + ? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath() + : GetPath(), + pass.GetTransientsBuffer())); cmd.primitive_type = PrimitiveType::kTriangle; FS::BindGradientInfo( cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info)); diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc index 15fcb20271..7d0141a763 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -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(); - { - 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( + GetCover() + ? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath() + : GetPath(), + pass.GetTransientsBuffer())); cmd.primitive_type = PrimitiveType::kTriangle; FS::BindGradientInfo( cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info)); diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc index 78732be59d..e24c6c2eda 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc @@ -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 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( cover_ ? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath() : path_, diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h index 223ae1388c..3ad3d0aa2a 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h @@ -27,9 +27,6 @@ class SolidColorContents final : public Contents { static std::unique_ptr Make(Path path, Color color); - static VertexBuffer CreateSolidFillVertices(const Path& path, - HostBuffer& buffer); - void SetPath(Path path); void SetCover(bool cover); diff --git a/engine/src/flutter/impeller/entity/contents/solid_fill_utils.cc b/engine/src/flutter/impeller/entity/contents/solid_fill_utils.cc new file mode 100644 index 0000000000..13c0684127 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/solid_fill_utils.cc @@ -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 diff --git a/engine/src/flutter/impeller/entity/contents/solid_fill_utils.h b/engine/src/flutter/impeller/entity/contents/solid_fill_utils.h new file mode 100644 index 0000000000..2a8382fe53 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/solid_fill_utils.h @@ -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 +VertexBuffer CreateSolidFillVertices(const Path& path, HostBuffer& buffer) { + VertexBufferBuilder 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 diff --git a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc index 7be8162ed0..80dd6f08f6 100644 --- a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc @@ -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(); - { - 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( + GetCover() + ? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath() + : GetPath(), + pass.GetTransientsBuffer())); cmd.primitive_type = PrimitiveType::kTriangle; FS::BindGradientInfo( cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info)); diff --git a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc index abf910d624..52c60a5cd5 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc @@ -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 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( + 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_,