Use display list images. (flutter/engine#102)

This commit is contained in:
Chinmay Garde
2022-03-29 13:22:31 -07:00
committed by Dan Field
parent ff91b522dd
commit bc8eb90c27
20 changed files with 227 additions and 45 deletions

View File

@@ -2,8 +2,16 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("tools/impeller.gni")
config("impeller_public_config") {
include_dirs = [ ".." ]
defines = []
if (impeller_supports_platform) {
defines += [ "IMPELLER_SUPPORTS_PLATFORM=1" ]
}
}
group("impeller") {

View File

@@ -67,7 +67,7 @@ TEST_F(AiksTest, CanRenderImageRect) {
Canvas canvas;
Paint paint;
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
auto source_rect = IRect::MakeSize(image->GetSize());
auto source_rect = Rect::MakeSize(Size(image->GetSize()));
// Render the bottom right quarter of the source image in a stretched rect.
source_rect.size.width /= 2;

View File

@@ -200,22 +200,24 @@ void Canvas::DrawPicture(Picture picture) {
void Canvas::DrawImage(std::shared_ptr<Image> image,
Point offset,
Paint paint) {
Paint paint,
SamplerDescriptor sampler) {
if (!image) {
return;
}
const auto source = IRect::MakeSize(image->GetSize());
const auto source = Rect::MakeSize(Size(image->GetSize()));
const auto dest =
Rect::MakeXYWH(offset.x, offset.y, source.size.width, source.size.height);
DrawImageRect(image, source, dest, std::move(paint));
DrawImageRect(image, source, dest, std::move(paint), std::move(sampler));
}
void Canvas::DrawImageRect(std::shared_ptr<Image> image,
IRect source,
Rect source,
Rect dest,
Paint paint) {
Paint paint,
SamplerDescriptor sampler) {
if (!image || source.size.IsEmpty() || dest.size.IsEmpty()) {
return;
}
@@ -229,10 +231,12 @@ void Canvas::DrawImageRect(std::shared_ptr<Image> image,
auto contents = std::make_shared<TextureContents>();
contents->SetTexture(image->GetTexture());
contents->SetSourceRect(source);
contents->SetSamplerDescriptor(std::move(sampler));
Entity entity;
entity.SetPath(PathBuilder{}.AddRect(dest).TakePath());
entity.SetBlendMode(paint.blend_mode);
entity.SetStencilDepth(GetStencilDepth());
entity.SetContents(contents);
entity.SetTransformation(GetCurrentTransformation());

View File

@@ -18,6 +18,7 @@
#include "impeller/geometry/path.h"
#include "impeller/geometry/point.h"
#include "impeller/geometry/vector.h"
#include "impeller/renderer/sampler_descriptor.h"
#include "impeller/typographer/glyph_atlas.h"
#include "impeller/typographer/text_frame.h"
@@ -65,12 +66,16 @@ class Canvas {
void DrawCircle(Point center, Scalar radius, Paint paint);
void DrawImage(std::shared_ptr<Image> image, Point offset, Paint paint);
void DrawImage(std::shared_ptr<Image> image,
Point offset,
Paint paint,
SamplerDescriptor sampler = {});
void DrawImageRect(std::shared_ptr<Image> image,
IRect source,
Rect source,
Rect dest,
Paint paint);
Paint paint,
SamplerDescriptor sampler = {});
void ClipPath(
Path path,

View File

@@ -35,7 +35,7 @@ std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
std::shared_ptr<Texture> target) {
auto contents = std::make_shared<TextureContents>();
contents->SetTexture(target);
contents->SetSourceRect(IRect::MakeSize(target->GetSize()));
contents->SetSourceRect(Rect::MakeSize(Size(target->GetSize())));
contents->SetOpacity(paint_.color.alpha);
return contents;
}

View File

@@ -8,6 +8,8 @@ impeller_component("display_list") {
sources = [
"display_list_dispatcher.cc",
"display_list_dispatcher.h",
"display_list_image_impeller.cc",
"display_list_image_impeller.h",
]
public_deps = [

View File

@@ -564,28 +564,71 @@ void DisplayListDispatcher::drawVertices(const sk_sp<SkVertices> vertices,
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawImage(const sk_sp<SkImage> image,
void DisplayListDispatcher::drawImage(const sk_sp<flutter::DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,
bool render_with_attributes) {
// Needs https://github.com/flutter/flutter/issues/95434
UNIMPLEMENTED;
if (!image) {
return;
}
auto texture = image->impeller_texture();
if (!texture) {
return;
}
const auto size = texture->GetSize();
const auto src = SkRect::MakeWH(size.width, size.height);
const auto dest =
SkRect::MakeXYWH(point.fX, point.fY, size.width, size.height);
drawImageRect(
image, // image
src, // source rect
dest, // destination rect
sampling, // sampling options
render_with_attributes, // render with attributes
SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint // constraint
);
}
static impeller::SamplerDescriptor ToSamplerDescriptor(
const SkSamplingOptions& options) {
impeller::SamplerDescriptor desc;
switch (options.filter) {
case SkFilterMode::kNearest:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case SkFilterMode::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<SkImage> image,
const sk_sp<flutter::DlImage> image,
const SkRect& src,
const SkRect& dst,
const SkSamplingOptions& sampling,
bool render_with_attributes,
SkCanvas::SrcRectConstraint constraint) {
// Needs https://github.com/flutter/flutter/issues/95434
UNIMPLEMENTED;
canvas_.DrawImageRect(
std::make_shared<Image>(image->impeller_texture()), // image
ToRect(src), // source rect
ToRect(dst), // destination rect
paint_, // paint
ToSamplerDescriptor(sampling) // sampling
);
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawImageNine(const sk_sp<SkImage> image,
void DisplayListDispatcher::drawImageNine(const sk_sp<flutter::DlImage> image,
const SkIRect& center,
const SkRect& dst,
SkFilterMode filter,
@@ -595,17 +638,18 @@ void DisplayListDispatcher::drawImageNine(const sk_sp<SkImage> image,
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawImageLattice(const sk_sp<SkImage> image,
const SkCanvas::Lattice& lattice,
const SkRect& dst,
SkFilterMode filter,
bool render_with_attributes) {
void DisplayListDispatcher::drawImageLattice(
const sk_sp<flutter::DlImage> image,
const SkCanvas::Lattice& lattice,
const SkRect& dst,
SkFilterMode filter,
bool render_with_attributes) {
// Needs https://github.com/flutter/flutter/issues/95434
UNIMPLEMENTED;
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawAtlas(const sk_sp<SkImage> atlas,
void DisplayListDispatcher::drawAtlas(const sk_sp<flutter::DlImage> atlas,
const SkRSXform xform[],
const SkRect tex[],
const SkColor colors[],

View File

@@ -172,13 +172,13 @@ class DisplayListDispatcher final : public flutter::Dispatcher {
flutter::DlBlendMode mode) override;
// |flutter::Dispatcher|
void drawImage(const sk_sp<SkImage> image,
void drawImage(const sk_sp<flutter::DlImage> image,
const SkPoint point,
const SkSamplingOptions& sampling,
bool render_with_attributes) override;
// |flutter::Dispatcher|
void drawImageRect(const sk_sp<SkImage> image,
void drawImageRect(const sk_sp<flutter::DlImage> image,
const SkRect& src,
const SkRect& dst,
const SkSamplingOptions& sampling,
@@ -186,21 +186,21 @@ class DisplayListDispatcher final : public flutter::Dispatcher {
SkCanvas::SrcRectConstraint constraint) override;
// |flutter::Dispatcher|
void drawImageNine(const sk_sp<SkImage> image,
void drawImageNine(const sk_sp<flutter::DlImage> image,
const SkIRect& center,
const SkRect& dst,
SkFilterMode filter,
bool render_with_attributes) override;
// |flutter::Dispatcher|
void drawImageLattice(const sk_sp<SkImage> image,
void drawImageLattice(const sk_sp<flutter::DlImage> image,
const SkCanvas::Lattice& lattice,
const SkRect& dst,
SkFilterMode filter,
bool render_with_attributes) override;
// |flutter::Dispatcher|
void drawAtlas(const sk_sp<SkImage> atlas,
void drawAtlas(const sk_sp<flutter::DlImage> atlas,
const SkRSXform xform[],
const SkRect tex[],
const SkColor colors[],

View File

@@ -0,0 +1,53 @@
// 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/display_list/display_list_image_impeller.h"
namespace impeller {
sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture) {
if (!texture) {
return nullptr;
}
return sk_sp<DlImageImpeller>(new DlImageImpeller(std::move(texture)));
}
DlImageImpeller::DlImageImpeller(std::shared_ptr<Texture> texture)
: texture_(std::move(texture)) {}
// |DlImage|
DlImageImpeller::~DlImageImpeller() = default;
// |DlImage|
sk_sp<SkImage> DlImageImpeller::skia_image() const {
return nullptr;
};
// |DlImage|
std::shared_ptr<impeller::Texture> DlImageImpeller::impeller_texture() const {
return texture_;
}
// |DlImage|
bool DlImageImpeller::isTextureBacked() const {
// Impeller textures are always ... textures :/
return true;
}
// |DlImage|
SkISize DlImageImpeller::dimensions() const {
const auto size = texture_ ? texture_->GetSize() : ISize{};
return SkISize::Make(size.width, size.height);
}
// |DlImage|
size_t DlImageImpeller::GetApproximateByteSize() const {
auto size = sizeof(this);
if (texture_) {
size += texture_->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
}
return size;
}
} // namespace impeller

View File

@@ -0,0 +1,43 @@
// 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 "flutter/display_list/display_list_image.h"
#include "flutter/fml/macros.h"
#include "impeller/renderer/texture.h"
namespace impeller {
class DlImageImpeller final : public flutter::DlImage {
public:
static sk_sp<DlImageImpeller> Make(std::shared_ptr<Texture> texture);
// |DlImage|
~DlImageImpeller() override;
// |DlImage|
sk_sp<SkImage> skia_image() const override;
// |DlImage|
std::shared_ptr<impeller::Texture> impeller_texture() const override;
// |DlImage|
bool isTextureBacked() const override;
// |DlImage|
SkISize dimensions() const override;
// |DlImage|
size_t GetApproximateByteSize() const override;
private:
std::shared_ptr<Texture> texture_;
explicit DlImageImpeller(std::shared_ptr<Texture> texture);
FML_DISALLOW_COPY_AND_ASSIGN(DlImageImpeller);
};
} // namespace impeller

View File

@@ -4,6 +4,7 @@
#include "flutter/display_list/display_list_builder.h"
#include "flutter/testing/testing.h"
#include "impeller/display_list/display_list_image_impeller.h"
#include "impeller/display_list/display_list_playground.h"
#include "third_party/skia/include/core/SkPathBuilder.h"
@@ -27,6 +28,14 @@ TEST_F(DisplayListTest, CanDrawTextBlob) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_F(DisplayListTest, CanDrawImage) {
auto texture = CreateTextureForFixture("embarcadero.jpg");
flutter::DisplayListBuilder builder;
builder.drawImage(DlImageImpeller::Make(texture), SkPoint::Make(100, 100),
SkSamplingOptions{}, true);
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_F(DisplayListTest, CapsAndJoins) {
flutter::DisplayListBuilder builder;

View File

@@ -99,7 +99,7 @@ bool FilterContents::Render(const ContentContext& renderer,
auto contents = std::make_shared<TextureContents>();
contents->SetTexture(snapshot.texture);
contents->SetSourceRect(IRect::MakeSize(snapshot.texture->GetSize()));
contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize())));
Entity e;
e.SetPath(PathBuilder{}.AddRect(GetBounds(entity)).GetCurrentPath());
@@ -162,7 +162,7 @@ static std::optional<Contents::Snapshot> ResolveSnapshotForInput(
const ContentContext& renderer, RenderPass& pass) -> bool {
TextureContents contents;
contents.SetTexture(texture);
contents.SetSourceRect(IRect::MakeSize(texture->GetSize()));
contents.SetSourceRect(Rect::MakeSize(Size(texture->GetSize())));
Entity sub_entity;
sub_entity.SetPath(entity.GetPath());
sub_entity.SetBlendMode(Entity::BlendMode::kSource);

View File

@@ -96,20 +96,28 @@ bool TextureContents::Render(const ContentContext& renderer,
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
FS::BindTextureSampler(
cmd, texture_,
renderer.GetContext()->GetSamplerLibrary()->GetSampler({}));
FS::BindTextureSampler(cmd, texture_,
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
sampler_descriptor_));
pass.AddCommand(std::move(cmd));
return true;
}
void TextureContents::SetSourceRect(const IRect& source_rect) {
void TextureContents::SetSourceRect(const Rect& source_rect) {
source_rect_ = source_rect;
}
const IRect& TextureContents::GetSourceRect() const {
const Rect& TextureContents::GetSourceRect() const {
return source_rect_;
}
void TextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
sampler_descriptor_ = std::move(desc);
}
const SamplerDescriptor& TextureContents::GetSamplerDescriptor() const {
return sampler_descriptor_;
}
} // namespace impeller

View File

@@ -10,6 +10,7 @@
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/renderer/sampler_descriptor.h"
namespace impeller {
@@ -25,12 +26,16 @@ class TextureContents final : public Contents {
std::shared_ptr<Texture> GetTexture() const;
void SetSourceRect(const IRect& source_rect);
void SetSamplerDescriptor(SamplerDescriptor desc);
const SamplerDescriptor& GetSamplerDescriptor() const;
void SetSourceRect(const Rect& source_rect);
const Rect& GetSourceRect() const;
void SetOpacity(Scalar opacity);
const IRect& GetSourceRect() const;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
@@ -38,7 +43,8 @@ class TextureContents final : public Contents {
public:
std::shared_ptr<Texture> texture_;
IRect source_rect_;
SamplerDescriptor sampler_descriptor_ = {};
Rect source_rect_;
Scalar opacity_ = 1.0f;
FML_DISALLOW_COPY_AND_ASSIGN(TextureContents);

View File

@@ -58,7 +58,7 @@ struct TRect {
auto right = points[0].x;
auto bottom = points[0].y;
if (points.size() > 1) {
for (uint i = 1; i < points.size(); i++) {
for (size_t i = 1; i < points.size(); i++) {
left = std::min(left, points[i].x);
top = std::min(top, points[i].y);
right = std::max(right, points[i].x);

View File

@@ -78,7 +78,7 @@ bool ImGui_ImplImpeller_Init(std::shared_ptr<impeller::Context> context) {
"Could not allocate ImGui font texture.");
[[maybe_unused]] bool uploaded = bd->font_texture->SetContents(
pixels, texture_descriptor.GetSizeOfBaseMipLevel());
pixels, texture_descriptor.GetByteSizeOfBaseMipLevel());
IM_ASSERT(uploaded &&
"Could not upload ImGui font texture to device memory.");
}

View File

@@ -29,7 +29,7 @@ std::shared_ptr<Texture> DeviceBufferMTL::MakeTexture(TextureDescriptor desc,
}
// Avoid overruns.
if (offset + desc.GetSizeOfBaseMipLevel() > size_) {
if (offset + desc.GetByteSizeOfBaseMipLevel() > size_) {
VALIDATION_LOG << "Avoiding buffer overrun when creating texture.";
return nullptr;
}

View File

@@ -38,7 +38,7 @@ bool TextureMTL::SetContents(const uint8_t* contents, size_t length) {
const auto& desc = GetTextureDescriptor();
// Out of bounds access.
if (length != desc.GetSizeOfBaseMipLevel()) {
if (length != desc.GetByteSizeOfBaseMipLevel()) {
return false;
}

View File

@@ -25,7 +25,7 @@ struct TextureDescriptor {
static_cast<TextureUsageMask>(TextureUsage::kShaderRead);
SampleCount sample_count = SampleCount::kCount1;
constexpr size_t GetSizeOfBaseMipLevel() const {
constexpr size_t GetByteSizeOfBaseMipLevel() const {
if (!IsValid()) {
return 0u;
}

View File

@@ -166,7 +166,7 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
texture_descriptor.size = ISize::MakeWH(atlas_size, atlas_size);
if (pixmap.rowBytes() * pixmap.height() !=
texture_descriptor.GetSizeOfBaseMipLevel()) {
texture_descriptor.GetByteSizeOfBaseMipLevel()) {
return nullptr;
}