[Impeller] Implement tiled texture contents for image color source (flutter/engine#35264)

This commit is contained in:
ColdPaleLight
2022-08-11 21:18:16 +08:00
committed by GitHub
parent 9900922ec2
commit 104accebb1
11 changed files with 395 additions and 51 deletions

View File

@@ -589,6 +589,8 @@ FILE: ../../../flutter/impeller/entity/contents/text_contents.cc
FILE: ../../../flutter/impeller/entity/contents/text_contents.h
FILE: ../../../flutter/impeller/entity/contents/texture_contents.cc
FILE: ../../../flutter/impeller/entity/contents/texture_contents.h
FILE: ../../../flutter/impeller/entity/contents/tiled_texture_contents.cc
FILE: ../../../flutter/impeller/entity/contents/tiled_texture_contents.h
FILE: ../../../flutter/impeller/entity/contents/vertices_contents.cc
FILE: ../../../flutter/impeller/entity/contents/vertices_contents.h
FILE: ../../../flutter/impeller/entity/entity.cc
@@ -645,6 +647,8 @@ FILE: ../../../flutter/impeller/entity/shaders/sweep_gradient_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/sweep_gradient_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/texture_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/tiled_texture_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/tiled_texture_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/vertices.frag
FILE: ../../../flutter/impeller/entity/shaders/vertices.vert
FILE: ../../../flutter/impeller/geometry/color.cc

View File

@@ -10,10 +10,12 @@
#include "impeller/aiks/aiks_playground.h"
#include "impeller/aiks/canvas.h"
#include "impeller/aiks/image.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/geometry_unittests.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/playground/widgets.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/snapshot.h"
#include "impeller/typographer/backends/skia/text_frame_skia.h"
#include "impeller/typographer/backends/skia/text_render_context_skia.h"
@@ -70,6 +72,82 @@ TEST_P(AiksTest, CanRenderImage) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
bool GenerateMipmap(std::shared_ptr<Context> context,
std::shared_ptr<Texture> texture,
std::string label) {
auto buffer = context->CreateCommandBuffer();
if (!buffer) {
return false;
}
auto pass = buffer->CreateBlitPass();
if (!pass) {
return false;
}
pass->GenerateMipmap(texture, label);
pass->EncodeCommands(context->GetResourceAllocator());
return true;
}
TEST_P(AiksTest, CanRenderTiledTexture) {
auto context = GetContext();
ASSERT_TRUE(context);
bool first_frame = true;
auto texture = CreateTextureForFixture("table_mountain_nx.png");
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
if (first_frame) {
first_frame = false;
GenerateMipmap(context, texture, "table_mountain_nx");
ImGui::SetNextWindowSize({480, 100});
ImGui::SetNextWindowPos({100, 550});
}
const char* tile_mode_names[] = {"Clamp", "Repeat", "Mirror", "Decal"};
const Entity::TileMode tile_modes[] = {
Entity::TileMode::kClamp, Entity::TileMode::kRepeat,
Entity::TileMode::kMirror, Entity::TileMode::kDecal};
const char* mip_filter_names[] = {"None", "Nearest", "Linear"};
const MipFilter mip_filters[] = {MipFilter::kNone, MipFilter::kNearest,
MipFilter::kLinear};
const char* min_mag_filter_names[] = {"Nearest", "Linear"};
const MinMagFilter min_mag_filters[] = {MinMagFilter::kNearest,
MinMagFilter::kLinear};
static int selected_x_tile_mode = 0;
static int selected_y_tile_mode = 0;
static int selected_mip_filter = 0;
static int selected_min_mag_filter = 0;
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Combo("X tile mode", &selected_x_tile_mode, tile_mode_names,
sizeof(tile_mode_names) / sizeof(char*));
ImGui::Combo("Y tile mode", &selected_y_tile_mode, tile_mode_names,
sizeof(tile_mode_names) / sizeof(char*));
ImGui::Combo("Mip filter", &selected_mip_filter, mip_filter_names,
sizeof(mip_filter_names) / sizeof(char*));
ImGui::Combo("Min Mag filter", &selected_min_mag_filter,
min_mag_filter_names,
sizeof(min_mag_filter_names) / sizeof(char*));
ImGui::End();
Canvas canvas;
Paint paint;
canvas.Translate({100.0, 100.0, 0});
auto x_tile_mode = tile_modes[selected_x_tile_mode];
auto y_tile_mode = tile_modes[selected_y_tile_mode];
SamplerDescriptor descriptor;
descriptor.mip_filter = mip_filters[selected_mip_filter];
descriptor.min_filter = min_mag_filters[selected_min_mag_filter];
descriptor.mag_filter = min_mag_filters[selected_min_mag_filter];
paint.color_source = [texture, x_tile_mode, y_tile_mode, descriptor]() {
auto contents = std::make_shared<TiledTextureContents>();
contents->SetTexture(texture);
contents->SetTileModes(x_tile_mode, y_tile_mode);
contents->SetSamplerDescriptor(descriptor);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(AiksTest, CanRenderImageRect) {
Canvas canvas;
Paint paint;

View File

@@ -48,9 +48,26 @@ float IPFloatTile(float t, float tile_mode) {
/// Remap a vec2 using a tiling mode.
///
/// Runs each component of the vec2 through `IPFloatTile`.
vec2 IPVec2Tile(vec2 coords, float tile_mode) {
return vec2(IPFloatTile(coords.x, tile_mode),
IPFloatTile(coords.y, tile_mode));
vec2 IPVec2Tile(vec2 coords, float x_tile_mode, float y_tile_mode) {
return vec2(IPFloatTile(coords.x, x_tile_mode),
IPFloatTile(coords.y, y_tile_mode));
}
/// Sample a texture, emulating a specific tile mode.
///
/// This is useful for Impeller graphics backend that don't have native support
/// for Decal.
vec4 IPSampleWithTileMode(sampler2D tex,
vec2 coords,
float y_coord_scale,
float x_tile_mode,
float y_tile_mode) {
if (x_tile_mode == kTileModeDecal && (coords.x < 0 || coords.x >= 1) ||
y_tile_mode == kTileModeDecal && (coords.y < 0 || coords.y >= 1)) {
return vec4(0);
}
return IPSample(tex, IPVec2Tile(coords, x_tile_mode, y_tile_mode), y_coord_scale);
}
/// Sample a texture, emulating a specific tile mode.
@@ -61,12 +78,7 @@ vec4 IPSampleWithTileMode(sampler2D tex,
vec2 coords,
float y_coord_scale,
float tile_mode) {
if (tile_mode == kTileModeDecal &&
(coords.x < 0 || coords.y < 0 || coords.x >= 1 || coords.y >= 1)) {
return vec4(0);
}
return IPSample(tex, IPVec2Tile(coords, tile_mode), y_coord_scale);
return IPSampleWithTileMode(tex, coords, y_coord_scale, tile_mode, tile_mode);
}
#endif

View File

@@ -21,6 +21,7 @@
#include "impeller/entity/contents/radial_gradient_contents.h"
#include "impeller/entity/contents/solid_stroke_contents.h"
#include "impeller/entity/contents/sweep_gradient_contents.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/path_builder.h"
@@ -118,6 +119,47 @@ static Entity::TileMode ToTileMode(flutter::DlTileMode tile_mode) {
}
}
static impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlImageSampling options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlImageSampling::kNearestNeighbor:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlImageSampling::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
case flutter::DlImageSampling::kMipmapLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.mip_filter = impeller::MipFilter::kLinear;
desc.label = "Mipmap Linear Sampler";
break;
default:
break;
}
return desc;
}
static impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlFilterMode options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlFilterMode::kNearest:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlFilterMode::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
default:
break;
}
return desc;
}
// |flutter::Dispatcher|
void DisplayListDispatcher::setAntiAlias(bool aa) {
// Nothing to do because AA is implicit.
@@ -316,7 +358,24 @@ void DisplayListDispatcher::setColorSource(
};
return;
}
case flutter::DlColorSourceType::kImage:
case flutter::DlColorSourceType::kImage: {
const flutter::DlImageColorSource* image_color_source = source->asImage();
FML_DCHECK(image_color_source &&
image_color_source->image()->impeller_texture());
auto texture = image_color_source->image()->impeller_texture();
auto x_tile_mode = ToTileMode(image_color_source->horizontal_tile_mode());
auto y_tile_mode = ToTileMode(image_color_source->vertical_tile_mode());
auto desc = ToSamplerDescriptor(image_color_source->sampling());
paint_.color_source = [texture, x_tile_mode, y_tile_mode, desc]() {
auto contents = std::make_shared<TiledTextureContents>();
contents->SetTexture(texture);
contents->SetTileModes(x_tile_mode, y_tile_mode);
contents->SetSamplerDescriptor(desc);
// TODO(109384) Support 'matrix' parameter for all color sources.
return contents;
};
return;
}
case flutter::DlColorSourceType::kConicalGradient:
case flutter::DlColorSourceType::kUnknown:
UNIMPLEMENTED;
@@ -908,47 +967,6 @@ void DisplayListDispatcher::drawImage(const sk_sp<flutter::DlImage> image,
);
}
static impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlImageSampling options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlImageSampling::kNearestNeighbor:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlImageSampling::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
case flutter::DlImageSampling::kMipmapLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.mip_filter = impeller::MipFilter::kLinear;
desc.label = "Mipmap Linear Sampler";
break;
default:
break;
}
return desc;
}
static impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlFilterMode options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlFilterMode::kNearest:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlFilterMode::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
default:
break;
}
return desc;
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawImageRect(
const sk_sp<flutter::DlImage> image,

View File

@@ -50,6 +50,8 @@ impeller_shaders("entity_shaders") {
"shaders/sweep_gradient_fill.vert",
"shaders/texture_fill.frag",
"shaders/texture_fill.vert",
"shaders/tiled_texture_fill.frag",
"shaders/tiled_texture_fill.vert",
"shaders/vertices.frag",
"shaders/vertices.vert",
]
@@ -101,6 +103,8 @@ impeller_component("entity") {
"contents/text_contents.h",
"contents/texture_contents.cc",
"contents/texture_contents.h",
"contents/tiled_texture_contents.cc",
"contents/tiled_texture_contents.h",
"contents/vertices_contents.cc",
"contents/vertices_contents.h",
"entity.cc",

View File

@@ -191,6 +191,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
blend_softlight_pipelines_[{}] =
CreateDefaultPipeline<BlendSoftLightPipeline>(*context_);
texture_pipelines_[{}] = CreateDefaultPipeline<TexturePipeline>(*context_);
tiled_texture_pipelines_[{}] =
CreateDefaultPipeline<TiledTexturePipeline>(*context_);
gaussian_blur_pipelines_[{}] =
CreateDefaultPipeline<GaussianBlurPipeline>(*context_);
border_mask_blur_pipelines_[{}] =

View File

@@ -54,6 +54,8 @@
#include "impeller/entity/sweep_gradient_fill.vert.h"
#include "impeller/entity/texture_fill.frag.h"
#include "impeller/entity/texture_fill.vert.h"
#include "impeller/entity/tiled_texture_fill.frag.h"
#include "impeller/entity/tiled_texture_fill.vert.h"
#include "impeller/entity/vertices.frag.h"
#include "impeller/entity/vertices.vert.h"
#include "impeller/renderer/formats.h"
@@ -104,6 +106,8 @@ using BlendSoftLightPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendSoftlightFragmentShader>;
using TexturePipeline =
PipelineT<TextureFillVertexShader, TextureFillFragmentShader>;
using TiledTexturePipeline =
PipelineT<TiledTextureFillVertexShader, TiledTextureFillFragmentShader>;
using GaussianBlurPipeline =
PipelineT<GaussianBlurVertexShader, GaussianBlurFragmentShader>;
using BorderMaskBlurPipeline =
@@ -189,6 +193,11 @@ class ContentContext {
return GetPipeline(texture_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetTiledTexturePipeline(
ContentContextOptions opts) const {
return GetPipeline(tiled_texture_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetGaussianBlurPipeline(
ContentContextOptions opts) const {
return GetPipeline(gaussian_blur_pipelines_, opts);
@@ -333,6 +342,7 @@ class ContentContext {
mutable Variants<RRectBlurPipeline> rrect_blur_pipelines_;
mutable Variants<BlendPipeline> texture_blend_pipelines_;
mutable Variants<TexturePipeline> texture_pipelines_;
mutable Variants<TiledTexturePipeline> tiled_texture_pipelines_;
mutable Variants<GaussianBlurPipeline> gaussian_blur_pipelines_;
mutable Variants<BorderMaskBlurPipeline> border_mask_blur_pipelines_;
mutable Variants<ColorMatrixColorFilterPipeline>

View File

@@ -0,0 +1,118 @@
// 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/tiled_texture_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/tiled_texture_fill.frag.h"
#include "impeller/entity/tiled_texture_fill.vert.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
TiledTextureContents::TiledTextureContents() = default;
TiledTextureContents::~TiledTextureContents() = default;
void TiledTextureContents::SetPath(Path path) {
path_ = std::move(path);
}
void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
texture_ = std::move(texture);
}
void TiledTextureContents::SetTileModes(Entity::TileMode x_tile_mode,
Entity::TileMode y_tile_mode) {
x_tile_mode_ = x_tile_mode;
y_tile_mode_ = y_tile_mode;
}
void TiledTextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
sampler_descriptor_ = std::move(desc);
}
std::optional<Rect> TiledTextureContents::GetCoverage(
const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};
bool TiledTextureContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
if (texture_ == nullptr) {
return true;
}
using VS = TiledTextureFillVertexShader;
using FS = TiledTextureFillFragmentShader;
const auto coverage_rect = path_.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(path_.GetFillType(), path_.CreatePolyline(),
[&vertex_builder, &texture_size](Point vtx) {
VS::PerVertexData data;
data.position = vtx;
data.texture_coords = vtx / texture_size;
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;
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
Command cmd;
cmd.label = "TiledTextureFill";
cmd.pipeline =
renderer.GetTiledTexturePipeline(OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_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_));
pass.AddCommand(std::move(cmd));
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,51 @@
// 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 <functional>
#include <memory>
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/path_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path.h"
#include "impeller/renderer/sampler_descriptor.h"
namespace impeller {
class TiledTextureContents final : public PathContents {
public:
TiledTextureContents();
~TiledTextureContents() override;
void SetPath(Path path) override;
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
void SetTexture(std::shared_ptr<Texture> texture);
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode);
void SetSamplerDescriptor(SamplerDescriptor desc);
private:
Path path_;
std::shared_ptr<Texture> texture_;
SamplerDescriptor sampler_descriptor_ = {};
Entity::TileMode x_tile_mode_;
Entity::TileMode y_tile_mode_;
FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents);
};
} // namespace impeller

View File

@@ -0,0 +1,29 @@
// 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/texture.glsl>
uniform sampler2D texture_sampler;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float x_tile_mode;
float y_tile_mode;
}
frag_info;
in vec2 v_texture_coords;
out vec4 frag_color;
void main() {
frag_color =
IPSampleWithTileMode(
texture_sampler, // sampler
v_texture_coords, // texture coordinates
frag_info.texture_sampler_y_coord_scale, // y coordinate scale
frag_info.x_tile_mode, // x tile mode
frag_info.y_tile_mode // y tile mode
);
}

View File

@@ -0,0 +1,18 @@
// 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.
uniform VertInfo {
mat4 mvp;
}
vert_info;
in vec2 position;
in vec2 texture_coords;
out vec2 v_texture_coords;
void main() {
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0);
v_texture_coords = texture_coords;
}