[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
This commit is contained in:
Jonah Williams
2023-02-14 11:25:50 -08:00
committed by GitHub
parent cf1add3a71
commit e0a3218055
4 changed files with 60 additions and 5 deletions

View File

@@ -99,6 +99,11 @@ std::shared_ptr<Contents> Paint::WithImageFilter(
std::shared_ptr<Contents> Paint::WithColorFilter(
std::shared_ptr<Contents> 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));

View File

@@ -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<TiledTextureContents>();
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;

View File

@@ -33,6 +33,23 @@ void TiledTextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
sampler_descriptor_ = std::move(desc);
}
void TiledTextureContents::SetColorFilter(
std::optional<ColorFilterProc> color_filter) {
color_filter_ = std::move(color_filter);
}
std::optional<std::shared_ptr<Texture>>
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;

View File

@@ -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<std::shared_ptr<ColorFilterContents>(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<ColorFilterProc> color_filter);
private:
std::optional<std::shared_ptr<Texture>> CreateFilterTexture(
const ContentContext& renderer) const;
std::shared_ptr<Texture> texture_;
SamplerDescriptor sampler_descriptor_ = {};
Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp;
Entity::TileMode y_tile_mode_ = Entity::TileMode::kClamp;
std::optional<ColorFilterProc> color_filter_;
FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents);
};