From e0a3218055f16ee7c59801101206206f838a75bc Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 14 Feb 2023 11:25:50 -0800 Subject: [PATCH] [Impeller] Special case color filter in tiled texture. (flutter/engine#39445) * [impeller] special case color filter in tiled texture * stdmove * add texture size testing * comments and exact size * enable/disable * remove test in release mode * remove stencil attachment * back out allocator change * ++ * more comment --- engine/src/flutter/impeller/aiks/paint.cc | 5 +++ .../display_list/display_list_dispatcher.cc | 5 +-- .../entity/contents/tiled_texture_contents.cc | 35 +++++++++++++++++-- .../entity/contents/tiled_texture_contents.h | 20 +++++++++++ 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index d0cbc7b0a1..f0a29fa740 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -99,6 +99,11 @@ std::shared_ptr Paint::WithImageFilter( std::shared_ptr Paint::WithColorFilter( std::shared_ptr input, bool absorb_opacity) const { + // Image input types will directly set their color filter, + // if any. See `TiledTextureContents.SetColorFilter`. + if (color_source_type == ColorSourceType::kImage) { + return input; + } if (color_filter.has_value()) { const ColorFilterProc& filter = color_filter.value(); auto color_filter_contents = filter(FilterInput::Make(input)); 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 58e826ab16..4e7d7ffe69 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -460,13 +460,14 @@ void DisplayListDispatcher::setColorSource( auto y_tile_mode = ToTileMode(image_color_source->vertical_tile_mode()); auto desc = ToSamplerDescriptor(image_color_source->sampling()); auto matrix = ToMatrix(image_color_source->matrix()); - paint_.color_source = [texture, x_tile_mode, y_tile_mode, desc, - matrix]() { + paint_.color_source = [texture, x_tile_mode, y_tile_mode, desc, matrix, + &paint = paint_]() { auto contents = std::make_shared(); contents->SetTexture(texture); contents->SetTileModes(x_tile_mode, y_tile_mode); contents->SetSamplerDescriptor(desc); contents->SetEffectTransform(matrix); + contents->SetColorFilter(paint.color_filter); return contents; }; return; 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 3324863613..95ab63de5d 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.cc @@ -33,6 +33,23 @@ void TiledTextureContents::SetSamplerDescriptor(SamplerDescriptor desc) { sampler_descriptor_ = std::move(desc); } +void TiledTextureContents::SetColorFilter( + std::optional color_filter) { + color_filter_ = std::move(color_filter); +} + +std::optional> +TiledTextureContents::CreateFilterTexture( + const ContentContext& renderer) const { + const ColorFilterProc& filter = color_filter_.value(); + auto color_filter_contents = filter(FilterInput::Make(texture_)); + auto snapshot = color_filter_contents->RenderToSnapshot(renderer, Entity()); + if (snapshot.has_value()) { + return snapshot.value().texture; + } + return std::nullopt; +} + bool TiledTextureContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -87,9 +104,21 @@ bool TiledTextureContents::Render(const ContentContext& renderer, cmd.BindVertices(geometry_result.vertex_buffer); VS::BindVertInfo(cmd, host_buffer.EmplaceUniform(vert_info)); FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); - FS::BindTextureSampler(cmd, texture_, - renderer.GetContext()->GetSamplerLibrary()->GetSampler( - sampler_descriptor_)); + 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; 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 b83ff3e512..f1db94f51e 100644 --- a/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h +++ b/engine/src/flutter/impeller/entity/contents/tiled_texture_contents.h @@ -10,6 +10,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/color_source_contents.h" +#include "impeller/entity/contents/filters/color_filter_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/path.h" #include "impeller/renderer/sampler_descriptor.h" @@ -22,6 +23,9 @@ class TiledTextureContents final : public ColorSourceContents { ~TiledTextureContents() override; + using ColorFilterProc = + std::function(FilterInput::Ref)>; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, @@ -33,11 +37,27 @@ class TiledTextureContents final : public ColorSourceContents { void SetSamplerDescriptor(SamplerDescriptor desc); + /// @brief Set a color filter to apply directly to this tiled texture + /// @param color_filter + /// + /// When applying a color filter to a tiled texture, we can reduce the + /// size and number of the subpasses required and the shader workloadby + /// applying the filter to the untiled image and absorbing the opacity before + /// tiling it into the final location. + /// + /// This may not be a performance improvement if the image is tiled into a + /// much smaller size that its original texture size. + void SetColorFilter(std::optional color_filter); + private: + std::optional> CreateFilterTexture( + const ContentContext& renderer) const; + std::shared_ptr texture_; SamplerDescriptor sampler_descriptor_ = {}; Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp; Entity::TileMode y_tile_mode_ = Entity::TileMode::kClamp; + std::optional color_filter_; FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents); };