[impeller] drawAtlas (flutter/engine#35009)

This commit is contained in:
Jonah Williams
2022-08-02 16:45:04 -07:00
committed by GitHub
parent d9df5e8846
commit 15d42dad5d
12 changed files with 436 additions and 2 deletions

View File

@@ -541,6 +541,8 @@ FILE: ../../../flutter/impeller/docs/assets/xcode_frame_capture/image6.png
FILE: ../../../flutter/impeller/docs/assets/xcode_frame_capture/image7.png
FILE: ../../../flutter/impeller/docs/assets/xcode_frame_capture/image8.png
FILE: ../../../flutter/impeller/docs/assets/xcode_frame_capture/image9.png
FILE: ../../../flutter/impeller/entity/contents/atlas_contents.cc
FILE: ../../../flutter/impeller/entity/contents/atlas_contents.h
FILE: ../../../flutter/impeller/entity/contents/clip_contents.cc
FILE: ../../../flutter/impeller/entity/contents/clip_contents.h
FILE: ../../../flutter/impeller/entity/contents/content_context.cc
@@ -593,6 +595,8 @@ FILE: ../../../flutter/impeller/entity/entity_playground.h
FILE: ../../../flutter/impeller/entity/entity_unittests.cc
FILE: ../../../flutter/impeller/entity/inline_pass_context.cc
FILE: ../../../flutter/impeller/entity/inline_pass_context.h
FILE: ../../../flutter/impeller/entity/shaders/atlas_fill.frag
FILE: ../../../flutter/impeller/entity/shaders/atlas_fill.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag

View File

@@ -8,6 +8,7 @@
#include "flutter/fml/logging.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/entity/contents/atlas_contents.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/text_contents.h"
#include "impeller/entity/contents/texture_contents.h"
@@ -321,4 +322,39 @@ void Canvas::DrawVertices(Vertices vertices,
GetCurrentPass().AddEntity(std::move(entity));
}
void Canvas::DrawAtlas(std::shared_ptr<Image> atlas,
std::vector<Matrix> transforms,
std::vector<Rect> texture_coordinates,
std::vector<Color> colors,
Entity::BlendMode blend_mode,
SamplerDescriptor sampler,
std::optional<Rect> cull_rect,
Paint paint) {
if (!atlas) {
return;
}
auto size = atlas->GetSize();
if (size.IsEmpty()) {
return;
}
std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
contents->SetColors(std::move(colors));
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas->GetTexture());
contents->SetSamplerDescriptor(std::move(sampler));
contents->SetBlendMode(blend_mode);
// TODO(jonahwilliams): set cull rect.
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetStencilDepth(GetStencilDepth());
entity.SetBlendMode(paint.blend_mode);
entity.SetContents(paint.WithFilters(contents, false));
GetCurrentPass().AddEntity(std::move(entity));
}
} // namespace impeller

View File

@@ -97,6 +97,15 @@ class Canvas {
Entity::BlendMode blend_mode,
Paint paint);
void DrawAtlas(std::shared_ptr<Image> atlas,
std::vector<Matrix> transforms,
std::vector<Rect> texture_coordinates,
std::vector<Color> colors,
Entity::BlendMode blend_mode,
SamplerDescriptor sampler,
std::optional<Rect> cull_rect,
Paint paint);
Picture EndRecordingAsPicture();
private:

View File

@@ -190,6 +190,34 @@ static Color ToColor(const SkColor& color) {
};
}
static std::vector<Color> ToColors(const flutter::DlColor colors[], int count) {
auto result = std::vector<Color>();
if (colors == nullptr) {
return result;
}
for (int i = 0; i < count; i++) {
result.push_back(ToColor(colors[i]));
}
return result;
}
static std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count) {
auto result = std::vector<Matrix>();
for (int i = 0; i < count; i++) {
auto form = xform[i];
// clang-format off
auto matrix = Matrix{
form.fSCos, form.fSSin, 0, 0,
-form.fSSin, form.fSCos, 0, 0,
0, 0, 1, 0,
form.fTx, form.fTy, 0, 1
};
// clang-format on
result.push_back(matrix);
}
return result;
}
// |flutter::Dispatcher|
void DisplayListDispatcher::setColorSource(
const flutter::DlColorSource* source) {
@@ -393,6 +421,14 @@ static std::optional<Rect> ToRect(const SkRect* rect) {
return Rect::MakeLTRB(rect->fLeft, rect->fTop, rect->fRight, rect->fBottom);
}
static std::vector<Rect> ToRects(const SkRect tex[], int count) {
auto result = std::vector<Rect>();
for (int i = 0; i < count; i++) {
result.push_back(ToRect(&tex[i]).value());
}
return result;
}
// |flutter::Dispatcher|
void DisplayListDispatcher::saveLayer(const SkRect* bounds,
const flutter::SaveLayerOptions options,
@@ -839,8 +875,10 @@ void DisplayListDispatcher::drawAtlas(const sk_sp<flutter::DlImage> atlas,
flutter::DlImageSampling sampling,
const SkRect* cull_rect,
bool render_with_attributes) {
// Needs https://github.com/flutter/flutter/issues/95434
UNIMPLEMENTED;
canvas_.DrawAtlas(std::make_shared<Image>(atlas->impeller_texture()),
ToRSXForms(xform, count), ToRects(tex, count),
ToColors(colors, count), ToBlendMode(mode),
ToSamplerDescriptor(sampling), ToRect(cull_rect), paint_);
}
// |flutter::Dispatcher|

View File

@@ -8,6 +8,8 @@ impeller_shaders("entity_shaders") {
name = "entity"
shaders = [
"shaders/atlas_fill.frag",
"shaders/atlas_fill.vert",
"shaders/blending/advanced_blend.vert",
"shaders/blending/advanced_blend_color.frag",
"shaders/blending/advanced_blend_colorburn.frag",
@@ -51,6 +53,8 @@ impeller_shaders("entity_shaders") {
impeller_component("entity") {
sources = [
"contents/atlas_contents.cc",
"contents/atlas_contents.h",
"contents/clip_contents.cc",
"contents/clip_contents.h",
"contents/content_context.cc",

View File

@@ -0,0 +1,137 @@
// 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 <optional>
#include "impeller/renderer/formats.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/renderer/vertex_buffer_builder.h"
#include "impeller/entity/atlas_fill.frag.h"
#include "impeller/entity/atlas_fill.vert.h"
#include "impeller/entity/contents/atlas_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/entity.h"
#include "impeller/renderer/render_pass.h"
namespace impeller {
AtlasContents::AtlasContents() = default;
AtlasContents::~AtlasContents() = default;
void AtlasContents::SetTexture(std::shared_ptr<Texture> texture) {
texture_ = std::move(texture);
}
std::shared_ptr<Texture> AtlasContents::GetTexture() const {
return texture_;
}
void AtlasContents::SetTransforms(std::vector<Matrix> transforms) {
transforms_ = transforms;
}
void AtlasContents::SetTextureCoordinates(std::vector<Rect> texture_coords) {
texture_coords_ = texture_coords;
}
void AtlasContents::SetColors(std::vector<Color> colors) {
colors_ = colors;
}
void AtlasContents::SetBlendMode(Entity::BlendMode blend_mode) {
// TODO(jonahwilliams): blending of colors with texture.
blend_mode_ = blend_mode;
}
std::optional<Rect> AtlasContents::GetCoverage(const Entity& entity) const {
Rect bounding_box = {};
for (size_t i = 0; i < texture_coords_.size(); i++) {
auto matrix = transforms_[i];
auto sample_rect = texture_coords_[i];
auto bounds = Rect::MakeSize(sample_rect.size).TransformBounds(matrix);
bounding_box = bounds.Union(bounding_box);
}
return bounding_box.TransformBounds(entity.GetTransformation());
}
void AtlasContents::SetSamplerDescriptor(SamplerDescriptor desc) {
sampler_descriptor_ = std::move(desc);
}
const SamplerDescriptor& AtlasContents::GetSamplerDescriptor() const {
return sampler_descriptor_;
}
bool AtlasContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
if (texture_ == nullptr) {
return true;
}
using VS = AtlasFillVertexShader;
using FS = AtlasFillFragmentShader;
const auto texture_size = texture_->GetSize();
if (texture_size.IsEmpty()) {
return true;
}
VertexBufferBuilder<VS::PerVertexData> vertex_builder;
vertex_builder.Reserve(texture_coords_.size() * 6);
constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
constexpr Scalar width[6] = {0, 1, 0, 1, 0, 1};
constexpr Scalar height[6] = {0, 0, 1, 0, 1, 1};
for (size_t i = 0; i < texture_coords_.size(); i++) {
auto sample_rect = texture_coords_[i];
auto matrix = transforms_[i];
auto color = (colors_.size() > 0 ? colors_[i] : Color::Black());
auto transformed_points =
Rect::MakeSize(sample_rect.size).GetTransformedPoints(matrix);
for (size_t j = 0; j < 6; j++) {
VS::PerVertexData data;
data.position = transformed_points[indices[j]];
data.texture_coords =
(sample_rect.origin + Point(sample_rect.size.width * width[j],
sample_rect.size.height * height[j])) /
texture_size;
data.color = color.Premultiply();
vertex_builder.AppendVertex(data);
}
}
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.has_color = colors_.size() > 0 ? 1.0 : 0.0;
Command cmd;
cmd.label = "DrawAtlas";
cmd.pipeline =
renderer.GetAtlasPipeline(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,59 @@
// 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/contents.h"
#include "impeller/entity/entity.h"
#include "impeller/renderer/sampler_descriptor.h"
namespace impeller {
class AtlasContents final : public Contents {
public:
explicit AtlasContents();
~AtlasContents() override;
void SetTexture(std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> GetTexture() const;
void SetTransforms(std::vector<Matrix> transforms);
void SetBlendMode(Entity::BlendMode blend_mode);
void SetTextureCoordinates(std::vector<Rect> texture_coords);
void SetColors(std::vector<Color> colors);
void SetSamplerDescriptor(SamplerDescriptor desc);
const SamplerDescriptor& GetSamplerDescriptor() const;
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
private:
std::shared_ptr<Texture> texture_;
std::vector<Rect> texture_coords_;
std::vector<Color> colors_;
std::vector<Matrix> transforms_;
Entity::BlendMode blend_mode_;
SamplerDescriptor sampler_descriptor_ = {};
FML_DISALLOW_COPY_AND_ASSIGN(AtlasContents);
};
} // namespace impeller

View File

@@ -198,6 +198,7 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
glyph_atlas_pipelines_[{}] =
CreateDefaultPipeline<GlyphAtlasPipeline>(*context_);
vertices_pipelines_[{}] = CreateDefaultPipeline<VerticesPipeline>(*context_);
atlas_pipelines_[{}] = CreateDefaultPipeline<AtlasPipeline>(*context_);
// Pipelines that are variants of the base pipelines with custom descriptors.
// TODO(98684): Rework this API to allow fetching the descriptor without

View File

@@ -27,6 +27,8 @@
#include "impeller/entity/advanced_blend_saturation.frag.h"
#include "impeller/entity/advanced_blend_screen.frag.h"
#include "impeller/entity/advanced_blend_softlight.frag.h"
#include "impeller/entity/atlas_fill.frag.h"
#include "impeller/entity/atlas_fill.vert.h"
#include "impeller/entity/blend.frag.h"
#include "impeller/entity/blend.vert.h"
#include "impeller/entity/border_mask_blur.frag.h"
@@ -106,6 +108,7 @@ using GlyphAtlasPipeline =
PipelineT<GlyphAtlasVertexShader, GlyphAtlasFragmentShader>;
using VerticesPipeline =
PipelineT<VerticesVertexShader, VerticesFragmentShader>;
using AtlasPipeline = PipelineT<AtlasFillVertexShader, AtlasFillFragmentShader>;
// Instead of requiring new shaders for clips, the solid fill stages are used
// to redirect writing to the stencil instead of color attachments.
using ClipPipeline = PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
@@ -200,6 +203,10 @@ class ContentContext {
return GetPipeline(vertices_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetAtlasPipeline(ContentContextOptions opts) const {
return GetPipeline(atlas_pipelines_, opts);
}
// Advanced blends.
std::shared_ptr<Pipeline> GetBlendColorPipeline(
@@ -311,6 +318,7 @@ class ContentContext {
mutable Variants<ClipPipeline> clip_pipelines_;
mutable Variants<GlyphAtlasPipeline> glyph_atlas_pipelines_;
mutable Variants<VerticesPipeline> vertices_pipelines_;
mutable Variants<AtlasPipeline> atlas_pipelines_;
// Advanced blends.
mutable Variants<BlendColorPipeline> blend_color_pipelines_;
mutable Variants<BlendColorBurnPipeline> blend_colorburn_pipelines_;

View File

@@ -6,6 +6,7 @@
#include <optional>
#include "flutter/testing/testing.h"
#include "impeller/entity/contents/atlas_contents.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/filters/blend_filter_contents.h"
#include "impeller/entity/contents/filters/filter_contents.h"
@@ -1090,6 +1091,94 @@ TEST_P(EntityTest, DrawVerticesSolidColorTrianglesWithIndices) {
ASSERT_TRUE(OpenPlaygroundHere(e));
}
TEST_P(EntityTest, DrawAtlasNoColor) {
// Draws the image as four squares stiched together.
auto atlas = CreateTextureForFixture("bay_bridge.jpg");
auto size = atlas->GetSize();
// Divide image into four quadrants.
Scalar half_width = size.width / 2;
Scalar half_height = size.height / 2;
std::vector<Rect> texture_coordinates = {
Rect::MakeLTRB(0, 0, half_width, half_height),
Rect::MakeLTRB(half_width, 0, size.width, half_height),
Rect::MakeLTRB(0, half_height, half_width, size.height),
Rect::MakeLTRB(half_width, half_height, size.width, size.height)};
// Position quadrants adjacent to eachother.
std::vector<Matrix> transforms = {
Matrix::MakeTranslation({0, 0, 0}),
Matrix::MakeTranslation({half_width, 0, 0}),
Matrix::MakeTranslation({0, half_height, 0}),
Matrix::MakeTranslation({half_width, half_height, 0})};
std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas);
contents->SetBlendMode(Entity::BlendMode::kSource);
Entity e;
e.SetTransformation(Matrix::MakeScale(GetContentScale()));
e.SetContents(contents);
ASSERT_TRUE(OpenPlaygroundHere(e));
}
TEST_P(EntityTest, DrawAtlasWithColor) {
// Draws the image as four squares stiched together. Because blend modes
// aren't implented this ends up as four solid color blocks.
auto atlas = CreateTextureForFixture("bay_bridge.jpg");
auto size = atlas->GetSize();
// Divide image into four quadrants.
Scalar half_width = size.width / 2;
Scalar half_height = size.height / 2;
std::vector<Rect> texture_coordinates = {
Rect::MakeLTRB(0, 0, half_width, half_height),
Rect::MakeLTRB(half_width, 0, size.width, half_height),
Rect::MakeLTRB(0, half_height, half_width, size.height),
Rect::MakeLTRB(half_width, half_height, size.width, size.height)};
// Position quadrants adjacent to eachother.
std::vector<Matrix> transforms = {
Matrix::MakeTranslation({0, 0, 0}),
Matrix::MakeTranslation({half_width, 0, 0}),
Matrix::MakeTranslation({0, half_height, 0}),
Matrix::MakeTranslation({half_width, half_height, 0})};
std::vector<Color> colors = {Color::Red(), Color::Green(), Color::Blue(),
Color::Yellow()};
std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas);
contents->SetColors(colors);
contents->SetBlendMode(Entity::BlendMode::kSource);
Entity e;
e.SetTransformation(Matrix::MakeScale(GetContentScale()));
e.SetContents(contents);
ASSERT_TRUE(OpenPlaygroundHere(e));
}
TEST_P(EntityTest, DrawAtlasNoColorFullSize) {
auto atlas = CreateTextureForFixture("bay_bridge.jpg");
auto size = atlas->GetSize();
std::vector<Rect> texture_coordinates = {
Rect::MakeLTRB(0, 0, size.width, size.height)};
std::vector<Matrix> transforms = {Matrix::MakeTranslation({0, 0, 0})};
std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas);
contents->SetBlendMode(Entity::BlendMode::kSource);
Entity e;
e.SetTransformation(Matrix::MakeScale(GetContentScale()));
e.SetContents(contents);
ASSERT_TRUE(OpenPlaygroundHere(e));
}
TEST_P(EntityTest, SolidFillCoverageIsCorrect) {
// No transform
{

View File

@@ -0,0 +1,28 @@
// 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 has_color;
}
frag_info;
in vec2 v_texture_coords;
in vec4 v_color;
out vec4 frag_color;
void main() {
vec4 sampled = IPSample(texture_sampler, v_texture_coords,
frag_info.texture_sampler_y_coord_scale);
if (frag_info.has_color == 1.0) {
frag_color = sampled.aaaa * v_color;
} else {
frag_color = sampled;
}
}

View File

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