From a832a6056074e82f828448aedd5fd0f3ec0d376c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 27 Feb 2023 14:01:16 -0800 Subject: [PATCH] [Impeller] texture coordinates implementation (flutter/engine#39781) [Impeller] texture coordinates implementation --- engine/src/flutter/impeller/aiks/canvas.cc | 43 ++++++- .../display_list/display_list_dispatcher.cc | 17 +++ .../display_list/display_list_unittests.cc | 54 +++++++++ .../display_list_vertices_geometry.cc | 108 +++++++++++++++++- .../display_list_vertices_geometry.h | 10 +- .../entity/contents/content_context.cc | 2 + .../entity/contents/content_context.h | 9 +- .../impeller/entity/contents/contents.h | 10 ++ .../entity/contents/tiled_texture_contents.cc | 76 +++++++++++- .../entity/contents/tiled_texture_contents.h | 4 + .../entity/contents/vertices_contents.cc | 84 +++++++++++++- .../entity/contents/vertices_contents.h | 25 ++++ .../src/flutter/impeller/entity/geometry.cc | 8 ++ engine/src/flutter/impeller/entity/geometry.h | 14 ++- 14 files changed, 448 insertions(+), 16 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 6ba4d872f6..59d4c0d2d0 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -21,6 +21,23 @@ namespace impeller { +static bool UseColorSourceContents( + const std::shared_ptr& vertices, + const Paint::ColorSourceType& type) { + // If there are no vertex color or texture coordinates. Or if there + // are vertex coordinates then only if the contents are an image or + // a solid color. + if (vertices->HasVertexColors()) { + return false; + } + if (vertices->HasTextureCoordinates() && + (type == Paint::ColorSourceType::kImage || + type == Paint::ColorSourceType::kColor)) { + return true; + } + return !vertices->HasTextureCoordinates(); +} + Canvas::Canvas() { Initialize(); } @@ -386,7 +403,9 @@ void Canvas::DrawVertices(const std::shared_ptr& vertices, entity.SetStencilDepth(GetStencilDepth()); entity.SetBlendMode(paint.blend_mode); - if (!vertices->HasVertexColors()) { + // If there are no vertex color or texture coordinates. Or if there + // are vertex coordinates then only if the contents are an image. + if (UseColorSourceContents(vertices, paint.color_source_type)) { auto contents = paint.CreateContentsForGeometry(vertices); entity.SetContents(paint.WithFilters(std::move(contents))); GetCurrentPass().AddEntity(entity); @@ -395,7 +414,27 @@ void Canvas::DrawVertices(const std::shared_ptr& vertices, auto src_paint = paint; src_paint.color = paint.color.WithAlpha(1.0); - auto src_contents = src_paint.CreateContentsForGeometry(vertices); + + std::shared_ptr src_contents = + src_paint.CreateContentsForGeometry(vertices); + if (vertices->HasTextureCoordinates() && + paint.color_source_type != Paint::ColorSourceType::kImage) { + // If the color source has an intrinsic size, then we use that to + // create the src contents as a simplification. Otherwise we use + // the extent of the texture coordinates to determine how large + // the src contents should be. If neither has a value we fall back + // to using the geometry coverage data. + Rect src_coverage; + auto size = src_contents->ColorSourceSize(); + if (size.has_value()) { + src_coverage = Rect::MakeXYWH(0, 0, size->width, size->height); + } else { + src_coverage = vertices->GetTextureCoordinateCoverge().value_or( + vertices->GetCoverage(Matrix{}).value()); + } + src_contents = + src_paint.CreateContentsForGeometry(Geometry::MakeRect(src_coverage)); + } auto contents = std::make_shared(); contents->SetAlpha(paint.color.alpha); diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 02e8516570..1d5dba8e76 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -396,6 +396,13 @@ void DisplayListDispatcher::setColorSource( contents->SetEndPoints(start_point, end_point); contents->SetTileMode(tile_mode); contents->SetEffectTransform(matrix); + + std::vector bounds{start_point, end_point}; + auto intrinsic_size = + Rect::MakePointBounds(bounds.begin(), bounds.end()); + if (intrinsic_size.has_value()) { + contents->SetColorSourceSize(intrinsic_size->size); + } return contents; }; return; @@ -420,6 +427,14 @@ void DisplayListDispatcher::setColorSource( contents->SetCenterAndRadius(center, radius); contents->SetTileMode(tile_mode); contents->SetEffectTransform(matrix); + + auto radius_pt = Point(radius, radius); + std::vector bounds{center + radius_pt, center - radius_pt}; + auto intrinsic_size = + Rect::MakePointBounds(bounds.begin(), bounds.end()); + if (intrinsic_size.has_value()) { + contents->SetColorSourceSize(intrinsic_size->size); + } return contents; }; return; @@ -447,6 +462,7 @@ void DisplayListDispatcher::setColorSource( contents->SetStops(stops); contents->SetTileMode(tile_mode); contents->SetEffectTransform(matrix); + return contents; }; return; @@ -468,6 +484,7 @@ void DisplayListDispatcher::setColorSource( contents->SetSamplerDescriptor(desc); contents->SetEffectTransform(matrix); contents->SetColorFilter(paint.color_filter); + contents->SetColorSourceSize(Size::Ceil(texture->GetSize())); return contents; }; return; 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 20e5e0c4c1..a9e23ef05a 100644 --- a/engine/src/flutter/impeller/display_list/display_list_unittests.cc +++ b/engine/src/flutter/impeller/display_list/display_list_unittests.cc @@ -1151,6 +1151,60 @@ TEST_P(DisplayListTest, DrawVerticesLinearGradientWithoutIndices) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(DisplayListTest, DrawVerticesLinearGradientWithTextureCoordinates) { + std::vector positions = {SkPoint::Make(100, 300), + SkPoint::Make(200, 100), + SkPoint::Make(300, 300)}; + std::vector texture_coordinates = {SkPoint::Make(300, 100), + SkPoint::Make(100, 200), + SkPoint::Make(300, 300)}; + + auto vertices = flutter::DlVertices::Make( + flutter::DlVertexMode::kTriangles, 3, positions.data(), + texture_coordinates.data(), /*colors=*/nullptr); + + std::vector colors = {flutter::DlColor::kBlue(), + flutter::DlColor::kRed()}; + const float stops[2] = {0.0, 1.0}; + + auto linear = flutter::DlColorSource::MakeLinear( + {100.0, 100.0}, {300.0, 300.0}, 2, colors.data(), stops, + flutter::DlTileMode::kRepeat); + + flutter::DisplayListBuilder builder; + flutter::DlPaint paint; + + paint.setColorSource(linear); + builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint); + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + +TEST_P(DisplayListTest, DrawVerticesImageSourceWithTextureCoordinates) { + auto texture = CreateTextureForFixture("embarcadero.jpg"); + auto dl_image = DlImageImpeller::Make(texture); + std::vector positions = {SkPoint::Make(100, 300), + SkPoint::Make(200, 100), + SkPoint::Make(300, 300)}; + std::vector texture_coordinates = { + SkPoint::Make(0, 0), SkPoint::Make(100, 200), SkPoint::Make(200, 100)}; + + auto vertices = flutter::DlVertices::Make( + flutter::DlVertexMode::kTriangles, 3, positions.data(), + texture_coordinates.data(), /*colors=*/nullptr); + + flutter::DisplayListBuilder builder; + flutter::DlPaint paint; + + auto image_source = flutter::DlImageColorSource( + dl_image, flutter::DlTileMode::kRepeat, flutter::DlTileMode::kRepeat); + + paint.setColorSource(&image_source); + builder.DrawVertices(vertices, flutter::DlBlendMode::kSrcOver, paint); + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + TEST_P(DisplayListTest, DrawVerticesSolidColorTrianglesWithIndices) { std::vector positions = { SkPoint::Make(100, 300), SkPoint::Make(200, 100), SkPoint::Make(300, 300), diff --git a/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.cc b/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.cc index 52caeea8be..19662297cc 100644 --- a/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.cc +++ b/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.cc @@ -7,6 +7,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity.h" #include "impeller/entity/position_color.vert.h" +#include "impeller/entity/texture_fill.vert.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/path_builder.h" #include "impeller/geometry/point.h" @@ -100,10 +101,38 @@ static PrimitiveType GetPrimitiveType(const flutter::DlVertices* vertices) { } } +std::optional DLVerticesGeometry::GetTextureCoordinateCoverge() const { + if (!HasTextureCoordinates()) { + return std::nullopt; + } + auto vertex_count = vertices_->vertex_count(); + auto* dl_texture_coordinates = vertices_->texture_coordinates(); + if (vertex_count == 0) { + return std::nullopt; + } + + auto left = dl_texture_coordinates[0].x(); + auto top = dl_texture_coordinates[0].y(); + auto right = dl_texture_coordinates[0].x(); + auto bottom = dl_texture_coordinates[0].y(); + + for (auto i = 0; i < vertex_count; i++) { + left = std::min(left, dl_texture_coordinates[i].x()); + top = std::min(top, dl_texture_coordinates[i].y()); + right = std::max(right, dl_texture_coordinates[i].x()); + bottom = std::max(bottom, dl_texture_coordinates[i].y()); + } + return Rect::MakeLTRB(left, top, right, bottom); +} + bool DLVerticesGeometry::HasVertexColors() const { return vertices_->colors() != nullptr; } +bool DLVerticesGeometry::HasTextureCoordinates() const { + return vertices_->texture_coordinates() != nullptr; +} + GeometryResult DLVerticesGeometry::GetPositionBuffer( const ContentContext& renderer, const Entity& entity, @@ -225,12 +254,80 @@ GeometryResult DLVerticesGeometry::GetPositionColorBuffer( } GeometryResult DLVerticesGeometry::GetPositionUVBuffer( + Rect texture_coverage, + Matrix effect_transform, const ContentContext& renderer, const Entity& entity, RenderPass& pass) { - // TODO(jonahwilliams): support texture coordinates in vertices - // https://github.com/flutter/flutter/issues/109956 - return {}; + using VS = TexturePipeline::VertexShader; + + auto index_count = normalized_indices_.size() == 0 + ? vertices_->index_count() + : normalized_indices_.size(); + auto vertex_count = vertices_->vertex_count(); + auto* dl_indices = normalized_indices_.size() == 0 + ? vertices_->indices() + : normalized_indices_.data(); + auto* dl_vertices = vertices_->vertices(); + auto* dl_texture_coordinates = vertices_->texture_coordinates(); + + auto size = texture_coverage.size; + auto origin = texture_coverage.origin; + std::vector vertex_data(vertex_count); + { + for (auto i = 0; i < vertex_count; i++) { + auto sk_point = dl_vertices[i]; + auto texture_coord = dl_texture_coordinates[i]; + auto uv = effect_transform * + Point((texture_coord.x() - origin.x) / size.width, + (texture_coord.y() - origin.y) / size.height); + // From experimentation we need to clamp these values to < 1.0 or else + // there can be flickering. + vertex_data[i] = { + .position = Point(sk_point.x(), sk_point.y()), + .texture_coords = + Point(std::clamp(uv.x, 0.0f, 1.0f - kEhCloseEnough), + std::clamp(uv.y, 0.0f, 1.0f - kEhCloseEnough)), + }; + } + } + + size_t total_vtx_bytes = vertex_data.size() * sizeof(VS::PerVertexData); + size_t total_idx_bytes = index_count * sizeof(uint16_t); + + DeviceBufferDescriptor buffer_desc; + buffer_desc.size = total_vtx_bytes + total_idx_bytes; + buffer_desc.storage_mode = StorageMode::kHostVisible; + + auto buffer = + renderer.GetContext()->GetResourceAllocator()->CreateBuffer(buffer_desc); + + if (!buffer->CopyHostBuffer(reinterpret_cast(vertex_data.data()), + Range{0, total_vtx_bytes}, 0)) { + return {}; + } + if (!buffer->CopyHostBuffer( + reinterpret_cast(const_cast(dl_indices)), + Range{0, total_idx_bytes}, total_vtx_bytes)) { + return {}; + } + + return GeometryResult{ + .type = GetPrimitiveType(vertices_), + .vertex_buffer = + { + .vertex_buffer = {.buffer = buffer, + .range = Range{0, total_vtx_bytes}}, + .index_buffer = {.buffer = buffer, + .range = + Range{total_vtx_bytes, total_idx_bytes}}, + .index_count = index_count, + .index_type = IndexType::k16bit, + }, + .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(), + .prevent_overdraw = false, + }; } GeometryVertexType DLVerticesGeometry::GetVertexType() const { @@ -238,6 +335,11 @@ GeometryVertexType DLVerticesGeometry::GetVertexType() const { if (dl_colors != nullptr) { return GeometryVertexType::kColor; } + auto* dl_texture_coordinates = vertices_->texture_coordinates(); + if (dl_texture_coordinates != nullptr) { + return GeometryVertexType::kUV; + } + return GeometryVertexType::kPosition; } diff --git a/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.h b/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.h index 3ebfa89780..9a668f92cc 100644 --- a/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.h +++ b/engine/src/flutter/impeller/display_list/display_list_vertices_geometry.h @@ -32,7 +32,9 @@ class DLVerticesGeometry : public VerticesGeometry { RenderPass& pass) override; // |VerticesGeometry| - GeometryResult GetPositionUVBuffer(const ContentContext& renderer, + GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, const Entity& entity, RenderPass& pass) override; @@ -50,6 +52,12 @@ class DLVerticesGeometry : public VerticesGeometry { // |VerticesGeometry| bool HasVertexColors() const override; + // |VerticesGeometry| + bool HasTextureCoordinates() const override; + + // |VerticesGeometry| + std::optional GetTextureCoordinateCoverge() const override; + private: void NormalizeIndices(); diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 2799a9164d..fda3198c86 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -250,6 +250,8 @@ ContentContext::ContentContext(std::shared_ptr context) texture_blend_pipelines_[{}] = CreateDefaultPipeline(*context_); texture_pipelines_[{}] = CreateDefaultPipeline(*context_); + position_uv_pipelines_[{}] = + CreateDefaultPipeline(*context_); tiled_texture_pipelines_[{}] = CreateDefaultPipeline(*context_); gaussian_blur_pipelines_[{}] = diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index 65bd2a020b..c5a57882ed 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -54,7 +54,6 @@ #include "impeller/entity/position.vert.h" #include "impeller/entity/position_color.vert.h" -#include "impeller/entity/position_uv.vert.h" #include "impeller/scene/scene_context.h" #include "impeller/typographer/glyph_atlas.h" @@ -123,6 +122,8 @@ using RRectBlurPipeline = using BlendPipeline = RenderPipelineT; using TexturePipeline = RenderPipelineT; +using PositionUVPipeline = + RenderPipelineT; using TiledTexturePipeline = RenderPipelineT; using GaussianBlurPipeline = @@ -362,6 +363,11 @@ class ContentContext { return GetPipeline(texture_pipelines_, opts); } + std::shared_ptr> GetPositionUVPipeline( + ContentContextOptions opts) const { + return GetPipeline(position_uv_pipelines_, opts); + } + std::shared_ptr> GetTiledTexturePipeline( ContentContextOptions opts) const { return GetPipeline(tiled_texture_pipelines_, opts); @@ -626,6 +632,7 @@ class ContentContext { mutable Variants rrect_blur_pipelines_; mutable Variants texture_blend_pipelines_; mutable Variants texture_pipelines_; + mutable Variants position_uv_pipelines_; mutable Variants tiled_texture_pipelines_; mutable Variants gaussian_blur_pipelines_; mutable Variants gaussian_blur_decal_pipelines_; diff --git a/engine/src/flutter/impeller/entity/contents/contents.h b/engine/src/flutter/impeller/entity/contents/contents.h index 2c5f3abf0c..a4ff06b93c 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.h +++ b/engine/src/flutter/impeller/entity/contents/contents.h @@ -75,9 +75,19 @@ class Contents { virtual bool ShouldRender(const Entity& entity, const std::optional& stencil_coverage) const; + /// @brief Return the color source's intrinsic size, if available. + /// + /// For example, a gradient has a size based on its end and beginning points, + /// ignoring any tiling. Solid colors and runtime effects have no size. + std::optional ColorSourceSize() const { return color_source_size_; } + + void SetColorSourceSize(Size size) { color_source_size_ = size; } + protected: private: + std::optional color_source_size_; + FML_DISALLOW_COPY_AND_ASSIGN(Contents); }; 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 d50af6509e..d9e8a8001d 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc @@ -56,6 +56,13 @@ bool TiledTextureContents::Render(const ContentContext& renderer, if (texture_ == nullptr) { return true; } + // TODO(jonahwilliams): this is a special case for VerticesGeometry which + // implements GetPositionUVBuffer. The general geometry case does not use + // this method (see note below). + auto geometry = GetGeometry(); + if (geometry->GetVertexType() == GeometryVertexType::kUV) { + return RenderVertices(renderer, entity, pass); + } using VS = TiledTextureFillVertexShader; using FS = TiledTextureFillFragmentShader; @@ -67,7 +74,6 @@ bool TiledTextureContents::Render(const ContentContext& renderer, auto& host_buffer = pass.GetTransientsBuffer(); - auto geometry = GetGeometry(); auto geometry_result = GetGeometry()->GetPositionBuffer(renderer, entity, pass); @@ -132,4 +138,72 @@ bool TiledTextureContents::Render(const ContentContext& renderer, return true; } +bool TiledTextureContents::RenderVertices(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const { + using VS = PositionUVPipeline::VertexShader; + using FS = PositionUVPipeline::FragmentShader; + + const auto texture_size = texture_->GetSize(); + if (texture_size.IsEmpty()) { + return true; + } + + auto& host_buffer = pass.GetTransientsBuffer(); + + auto geometry_result = GetGeometry()->GetPositionUVBuffer( + Rect::MakeSize(texture_size), GetInverseMatrix(), renderer, entity, pass); + + VS::FrameInfo frame_info; + frame_info.mvp = geometry_result.transform; + + FS::FragInfo frag_info; + frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale(); + frag_info.x_tile_mode = static_cast(x_tile_mode_); + frag_info.y_tile_mode = static_cast(y_tile_mode_); + frag_info.alpha = GetAlpha(); + + Command cmd; + cmd.label = "PositionUV"; + cmd.stencil_reference = entity.GetStencilDepth(); + + auto options = OptionsFromPassAndEntity(pass, entity); + if (geometry_result.prevent_overdraw) { + options.stencil_compare = CompareFunction::kEqual; + options.stencil_operation = StencilOperation::kIncrementClamp; + } + options.primitive_type = geometry_result.type; + cmd.pipeline = renderer.GetPositionUVPipeline(options); + + cmd.BindVertices(geometry_result.vertex_buffer); + VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); + if (color_filter_.has_value()) { + auto filtered_texture = CreateFilterTexture(renderer); + if (!filtered_texture.has_value()) { + return false; + } + FS::BindTextureSampler( + cmd, filtered_texture.value(), + renderer.GetContext()->GetSamplerLibrary()->GetSampler( + sampler_descriptor_)); + } else { + FS::BindTextureSampler( + cmd, texture_, + renderer.GetContext()->GetSamplerLibrary()->GetSampler( + sampler_descriptor_)); + } + + if (!pass.AddCommand(std::move(cmd))) { + return false; + } + + if (geometry_result.prevent_overdraw) { + auto restore = ClipRestoreContents(); + restore.SetRestoreCoverage(GetCoverage(entity)); + return restore.Render(renderer, entity, pass); + } + return true; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h index f1db94f51e..0f0c6b3210 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h @@ -53,6 +53,10 @@ class TiledTextureContents final : public ColorSourceContents { std::optional> CreateFilterTexture( const ContentContext& renderer) const; + bool RenderVertices(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const; + std::shared_ptr texture_; SamplerDescriptor sampler_descriptor_ = {}; Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp; diff --git a/engine/src/flutter/impeller/entity/contents/vertices_contents.cc b/engine/src/flutter/impeller/entity/contents/vertices_contents.cc index a7b6c0d95a..de2954b00e 100644 --- a/engine/src/flutter/impeller/entity/contents/vertices_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/vertices_contents.cc @@ -10,7 +10,6 @@ #include "impeller/entity/contents/texture_contents.h" #include "impeller/entity/position.vert.h" #include "impeller/entity/position_color.vert.h" -#include "impeller/entity/position_uv.vert.h" #include "impeller/entity/vertices.frag.h" #include "impeller/geometry/color.h" #include "impeller/renderer/formats.h" @@ -48,22 +47,100 @@ void VerticesContents::SetBlendMode(BlendMode blend_mode) { blend_mode_ = blend_mode; } +const std::shared_ptr& VerticesContents::GetSourceContents() const { + return src_contents_; +} + bool VerticesContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { if (blend_mode_ == BlendMode::kClear) { return true; } + std::shared_ptr src_contents = src_contents_; + if (geometry_->HasTextureCoordinates()) { + auto contents = std::make_shared(*this); + if (!geometry_->HasVertexColors()) { + contents->SetAlpha(alpha_); + return contents->Render(renderer, entity, pass); + } + src_contents = contents; + } + auto dst_contents = std::make_shared(*this); auto contents = ColorFilterContents::MakeBlend( blend_mode_, {FilterInput::Make(dst_contents, false), - FilterInput::Make(src_contents_, false)}); + FilterInput::Make(src_contents, false)}); contents->SetAlpha(alpha_); return contents->Render(renderer, entity, pass); } +//------------------------------------------------------ +// VerticesUVContents + +VerticesUVContents::VerticesUVContents(const VerticesContents& parent) + : parent_(parent) {} + +VerticesUVContents::~VerticesUVContents() {} + +std::optional VerticesUVContents::GetCoverage( + const Entity& entity) const { + return parent_.GetCoverage(entity); +} + +void VerticesUVContents::SetAlpha(Scalar alpha) { + alpha_ = alpha; +} + +bool VerticesUVContents::Render(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const { + using VS = TexturePipeline::VertexShader; + using FS = TexturePipeline::FragmentShader; + + auto src_contents = parent_.GetSourceContents(); + + auto snapshot = src_contents->RenderToSnapshot(renderer, entity); + if (!snapshot.has_value()) { + return false; + } + + Command cmd; + cmd.label = "VerticesUV"; + auto& host_buffer = pass.GetTransientsBuffer(); + auto geometry = parent_.GetGeometry(); + + auto coverage = src_contents->GetCoverage(Entity{}); + if (!coverage.has_value()) { + return false; + } + auto geometry_result = geometry->GetPositionUVBuffer( + coverage.value(), Matrix(), renderer, entity, pass); + auto opts = OptionsFromPassAndEntity(pass, entity); + opts.primitive_type = geometry_result.type; + cmd.pipeline = renderer.GetTexturePipeline(opts); + cmd.stencil_reference = entity.GetStencilDepth(); + cmd.BindVertices(geometry_result.vertex_buffer); + + VS::FrameInfo frame_info; + frame_info.mvp = geometry_result.transform; + frame_info.texture_sampler_y_coord_scale = + snapshot->texture->GetYCoordScale(); + VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); + + FS::FragInfo frag_info; + frag_info.alpha = alpha_ * snapshot->opacity; + FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); + + FS::BindTextureSampler(cmd, snapshot->texture, + renderer.GetContext()->GetSamplerLibrary()->GetSampler( + snapshot->sampler_descriptor)); + + return pass.AddCommand(std::move(cmd)); +} + //------------------------------------------------------ // VerticesColorContents @@ -72,7 +149,6 @@ VerticesColorContents::VerticesColorContents(const VerticesContents& parent) VerticesColorContents::~VerticesColorContents() {} -// |Contents| std::optional VerticesColorContents::GetCoverage( const Entity& entity) const { return parent_.GetCoverage(entity); @@ -82,7 +158,6 @@ void VerticesColorContents::SetAlpha(Scalar alpha) { alpha_ = alpha; } -// |Contents| bool VerticesColorContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -99,6 +174,7 @@ bool VerticesColorContents::Render(const ContentContext& renderer, auto opts = OptionsFromPassAndEntity(pass, entity); opts.primitive_type = geometry_result.type; cmd.pipeline = renderer.GetGeometryColorPipeline(opts); + cmd.stencil_reference = entity.GetStencilDepth(); cmd.BindVertices(geometry_result.vertex_buffer); VS::FrameInfo frame_info; diff --git a/engine/src/flutter/impeller/entity/contents/vertices_contents.h b/engine/src/flutter/impeller/entity/contents/vertices_contents.h index b2459d549f..88facd3b4b 100644 --- a/engine/src/flutter/impeller/entity/contents/vertices_contents.h +++ b/engine/src/flutter/impeller/entity/contents/vertices_contents.h @@ -35,6 +35,8 @@ class VerticesContents final : public Contents { std::shared_ptr GetGeometry() const; + const std::shared_ptr& GetSourceContents() const; + // |Contents| std::optional GetCoverage(const Entity& entity) const override; @@ -75,4 +77,27 @@ class VerticesColorContents final : public Contents { FML_DISALLOW_COPY_AND_ASSIGN(VerticesColorContents); }; +class VerticesUVContents final : public Contents { + public: + explicit VerticesUVContents(const VerticesContents& parent); + + ~VerticesUVContents() override; + + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + + // |Contents| + bool Render(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const override; + + void SetAlpha(Scalar alpha); + + private: + const VerticesContents& parent_; + Scalar alpha_ = 1.0; + + FML_DISALLOW_COPY_AND_ASSIGN(VerticesUVContents); +}; + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry.cc b/engine/src/flutter/impeller/entity/geometry.cc index c86eaa6849..b610e03dd8 100644 --- a/engine/src/flutter/impeller/entity/geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry.cc @@ -17,6 +17,14 @@ Geometry::Geometry() = default; Geometry::~Geometry() = default; +GeometryResult Geometry::GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) { + return {}; +} + // static std::unique_ptr Geometry::MakeFillPath(const Path& path) { return std::make_unique(path); diff --git a/engine/src/flutter/impeller/entity/geometry.h b/engine/src/flutter/impeller/entity/geometry.h index f595eeea77..edc59cd5c6 100644 --- a/engine/src/flutter/impeller/entity/geometry.h +++ b/engine/src/flutter/impeller/entity/geometry.h @@ -65,6 +65,12 @@ class Geometry { const Entity& entity, RenderPass& pass) = 0; + virtual GeometryResult GetPositionUVBuffer(Rect texture_coverage, + Matrix effect_transform, + const ContentContext& renderer, + const Entity& entity, + RenderPass& pass); + virtual GeometryVertexType GetVertexType() const = 0; virtual std::optional GetCoverage(const Matrix& transform) const = 0; @@ -77,11 +83,11 @@ class VerticesGeometry : public Geometry { const Entity& entity, RenderPass& pass) = 0; - virtual GeometryResult GetPositionUVBuffer(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) = 0; - virtual bool HasVertexColors() const = 0; + + virtual bool HasTextureCoordinates() const = 0; + + virtual std::optional GetTextureCoordinateCoverge() const = 0; }; /// @brief A geometry that is created from a filled path object.