Skip to content

Commit 9886da8

Browse files
chinmaygardednfield
authored andcommitted
Use display list images. (flutter#102)
1 parent cbd14ba commit 9886da8

20 files changed

+227
-45
lines changed

impeller/BUILD.gn

+8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5+
import("tools/impeller.gni")
6+
57
config("impeller_public_config") {
68
include_dirs = [ ".." ]
9+
10+
defines = []
11+
12+
if (impeller_supports_platform) {
13+
defines += [ "IMPELLER_SUPPORTS_PLATFORM=1" ]
14+
}
715
}
816

917
group("impeller") {

impeller/aiks/aiks_unittests.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ TEST_F(AiksTest, CanRenderImageRect) {
6767
Canvas canvas;
6868
Paint paint;
6969
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
70-
auto source_rect = IRect::MakeSize(image->GetSize());
70+
auto source_rect = Rect::MakeSize(Size(image->GetSize()));
7171

7272
// Render the bottom right quarter of the source image in a stretched rect.
7373
source_rect.size.width /= 2;

impeller/aiks/canvas.cc

+9-5
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,24 @@ void Canvas::DrawPicture(Picture picture) {
200200

201201
void Canvas::DrawImage(std::shared_ptr<Image> image,
202202
Point offset,
203-
Paint paint) {
203+
Paint paint,
204+
SamplerDescriptor sampler) {
204205
if (!image) {
205206
return;
206207
}
207208

208-
const auto source = IRect::MakeSize(image->GetSize());
209+
const auto source = Rect::MakeSize(Size(image->GetSize()));
209210
const auto dest =
210211
Rect::MakeXYWH(offset.x, offset.y, source.size.width, source.size.height);
211212

212-
DrawImageRect(image, source, dest, std::move(paint));
213+
DrawImageRect(image, source, dest, std::move(paint), std::move(sampler));
213214
}
214215

215216
void Canvas::DrawImageRect(std::shared_ptr<Image> image,
216-
IRect source,
217+
Rect source,
217218
Rect dest,
218-
Paint paint) {
219+
Paint paint,
220+
SamplerDescriptor sampler) {
219221
if (!image || source.size.IsEmpty() || dest.size.IsEmpty()) {
220222
return;
221223
}
@@ -229,10 +231,12 @@ void Canvas::DrawImageRect(std::shared_ptr<Image> image,
229231
auto contents = std::make_shared<TextureContents>();
230232
contents->SetTexture(image->GetTexture());
231233
contents->SetSourceRect(source);
234+
contents->SetSamplerDescriptor(std::move(sampler));
232235

233236
Entity entity;
234237
entity.SetPath(PathBuilder{}.AddRect(dest).TakePath());
235238
entity.SetBlendMode(paint.blend_mode);
239+
entity.SetStencilDepth(GetStencilDepth());
236240
entity.SetContents(contents);
237241
entity.SetTransformation(GetCurrentTransformation());
238242

impeller/aiks/canvas.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "impeller/geometry/path.h"
1919
#include "impeller/geometry/point.h"
2020
#include "impeller/geometry/vector.h"
21+
#include "impeller/renderer/sampler_descriptor.h"
2122
#include "impeller/typographer/glyph_atlas.h"
2223
#include "impeller/typographer/text_frame.h"
2324

@@ -65,12 +66,16 @@ class Canvas {
6566

6667
void DrawCircle(Point center, Scalar radius, Paint paint);
6768

68-
void DrawImage(std::shared_ptr<Image> image, Point offset, Paint paint);
69+
void DrawImage(std::shared_ptr<Image> image,
70+
Point offset,
71+
Paint paint,
72+
SamplerDescriptor sampler = {});
6973

7074
void DrawImageRect(std::shared_ptr<Image> image,
71-
IRect source,
75+
Rect source,
7276
Rect dest,
73-
Paint paint);
77+
Paint paint,
78+
SamplerDescriptor sampler = {});
7479

7580
void ClipPath(
7681
Path path,

impeller/aiks/paint_pass_delegate.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
3535
std::shared_ptr<Texture> target) {
3636
auto contents = std::make_shared<TextureContents>();
3737
contents->SetTexture(target);
38-
contents->SetSourceRect(IRect::MakeSize(target->GetSize()));
38+
contents->SetSourceRect(Rect::MakeSize(Size(target->GetSize())));
3939
contents->SetOpacity(paint_.color.alpha);
4040
return contents;
4141
}

impeller/display_list/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ impeller_component("display_list") {
88
sources = [
99
"display_list_dispatcher.cc",
1010
"display_list_dispatcher.h",
11+
"display_list_image_impeller.cc",
12+
"display_list_image_impeller.h",
1113
]
1214

1315
public_deps = [

impeller/display_list/display_list_dispatcher.cc

+57-13
Original file line numberDiff line numberDiff line change
@@ -564,28 +564,71 @@ void DisplayListDispatcher::drawVertices(const sk_sp<SkVertices> vertices,
564564
}
565565

566566
// |flutter::Dispatcher|
567-
void DisplayListDispatcher::drawImage(const sk_sp<SkImage> image,
567+
void DisplayListDispatcher::drawImage(const sk_sp<flutter::DlImage> image,
568568
const SkPoint point,
569569
const SkSamplingOptions& sampling,
570570
bool render_with_attributes) {
571-
// Needs https://github.com/flutter/flutter/issues/95434
572-
UNIMPLEMENTED;
571+
if (!image) {
572+
return;
573+
}
574+
575+
auto texture = image->impeller_texture();
576+
if (!texture) {
577+
return;
578+
}
579+
580+
const auto size = texture->GetSize();
581+
const auto src = SkRect::MakeWH(size.width, size.height);
582+
const auto dest =
583+
SkRect::MakeXYWH(point.fX, point.fY, size.width, size.height);
584+
585+
drawImageRect(
586+
image, // image
587+
src, // source rect
588+
dest, // destination rect
589+
sampling, // sampling options
590+
render_with_attributes, // render with attributes
591+
SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint // constraint
592+
);
593+
}
594+
595+
static impeller::SamplerDescriptor ToSamplerDescriptor(
596+
const SkSamplingOptions& options) {
597+
impeller::SamplerDescriptor desc;
598+
switch (options.filter) {
599+
case SkFilterMode::kNearest:
600+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
601+
desc.label = "Nearest Sampler";
602+
break;
603+
case SkFilterMode::kLinear:
604+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
605+
desc.label = "Linear Sampler";
606+
break;
607+
default:
608+
break;
609+
}
610+
return desc;
573611
}
574612

575613
// |flutter::Dispatcher|
576614
void DisplayListDispatcher::drawImageRect(
577-
const sk_sp<SkImage> image,
615+
const sk_sp<flutter::DlImage> image,
578616
const SkRect& src,
579617
const SkRect& dst,
580618
const SkSamplingOptions& sampling,
581619
bool render_with_attributes,
582620
SkCanvas::SrcRectConstraint constraint) {
583-
// Needs https://github.com/flutter/flutter/issues/95434
584-
UNIMPLEMENTED;
621+
canvas_.DrawImageRect(
622+
std::make_shared<Image>(image->impeller_texture()), // image
623+
ToRect(src), // source rect
624+
ToRect(dst), // destination rect
625+
paint_, // paint
626+
ToSamplerDescriptor(sampling) // sampling
627+
);
585628
}
586629

587630
// |flutter::Dispatcher|
588-
void DisplayListDispatcher::drawImageNine(const sk_sp<SkImage> image,
631+
void DisplayListDispatcher::drawImageNine(const sk_sp<flutter::DlImage> image,
589632
const SkIRect& center,
590633
const SkRect& dst,
591634
SkFilterMode filter,
@@ -595,17 +638,18 @@ void DisplayListDispatcher::drawImageNine(const sk_sp<SkImage> image,
595638
}
596639

597640
// |flutter::Dispatcher|
598-
void DisplayListDispatcher::drawImageLattice(const sk_sp<SkImage> image,
599-
const SkCanvas::Lattice& lattice,
600-
const SkRect& dst,
601-
SkFilterMode filter,
602-
bool render_with_attributes) {
641+
void DisplayListDispatcher::drawImageLattice(
642+
const sk_sp<flutter::DlImage> image,
643+
const SkCanvas::Lattice& lattice,
644+
const SkRect& dst,
645+
SkFilterMode filter,
646+
bool render_with_attributes) {
603647
// Needs https://github.com/flutter/flutter/issues/95434
604648
UNIMPLEMENTED;
605649
}
606650

607651
// |flutter::Dispatcher|
608-
void DisplayListDispatcher::drawAtlas(const sk_sp<SkImage> atlas,
652+
void DisplayListDispatcher::drawAtlas(const sk_sp<flutter::DlImage> atlas,
609653
const SkRSXform xform[],
610654
const SkRect tex[],
611655
const SkColor colors[],

impeller/display_list/display_list_dispatcher.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -172,35 +172,35 @@ class DisplayListDispatcher final : public flutter::Dispatcher {
172172
flutter::DlBlendMode mode) override;
173173

174174
// |flutter::Dispatcher|
175-
void drawImage(const sk_sp<SkImage> image,
175+
void drawImage(const sk_sp<flutter::DlImage> image,
176176
const SkPoint point,
177177
const SkSamplingOptions& sampling,
178178
bool render_with_attributes) override;
179179

180180
// |flutter::Dispatcher|
181-
void drawImageRect(const sk_sp<SkImage> image,
181+
void drawImageRect(const sk_sp<flutter::DlImage> image,
182182
const SkRect& src,
183183
const SkRect& dst,
184184
const SkSamplingOptions& sampling,
185185
bool render_with_attributes,
186186
SkCanvas::SrcRectConstraint constraint) override;
187187

188188
// |flutter::Dispatcher|
189-
void drawImageNine(const sk_sp<SkImage> image,
189+
void drawImageNine(const sk_sp<flutter::DlImage> image,
190190
const SkIRect& center,
191191
const SkRect& dst,
192192
SkFilterMode filter,
193193
bool render_with_attributes) override;
194194

195195
// |flutter::Dispatcher|
196-
void drawImageLattice(const sk_sp<SkImage> image,
196+
void drawImageLattice(const sk_sp<flutter::DlImage> image,
197197
const SkCanvas::Lattice& lattice,
198198
const SkRect& dst,
199199
SkFilterMode filter,
200200
bool render_with_attributes) override;
201201

202202
// |flutter::Dispatcher|
203-
void drawAtlas(const sk_sp<SkImage> atlas,
203+
void drawAtlas(const sk_sp<flutter::DlImage> atlas,
204204
const SkRSXform xform[],
205205
const SkRect tex[],
206206
const SkColor colors[],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/display_list/display_list_image_impeller.h"
6+
7+
namespace impeller {
8+
9+
sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture) {
10+
if (!texture) {
11+
return nullptr;
12+
}
13+
return sk_sp<DlImageImpeller>(new DlImageImpeller(std::move(texture)));
14+
}
15+
16+
DlImageImpeller::DlImageImpeller(std::shared_ptr<Texture> texture)
17+
: texture_(std::move(texture)) {}
18+
19+
// |DlImage|
20+
DlImageImpeller::~DlImageImpeller() = default;
21+
22+
// |DlImage|
23+
sk_sp<SkImage> DlImageImpeller::skia_image() const {
24+
return nullptr;
25+
};
26+
27+
// |DlImage|
28+
std::shared_ptr<impeller::Texture> DlImageImpeller::impeller_texture() const {
29+
return texture_;
30+
}
31+
32+
// |DlImage|
33+
bool DlImageImpeller::isTextureBacked() const {
34+
// Impeller textures are always ... textures :/
35+
return true;
36+
}
37+
38+
// |DlImage|
39+
SkISize DlImageImpeller::dimensions() const {
40+
const auto size = texture_ ? texture_->GetSize() : ISize{};
41+
return SkISize::Make(size.width, size.height);
42+
}
43+
44+
// |DlImage|
45+
size_t DlImageImpeller::GetApproximateByteSize() const {
46+
auto size = sizeof(this);
47+
if (texture_) {
48+
size += texture_->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
49+
}
50+
return size;
51+
}
52+
53+
} // namespace impeller
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "flutter/display_list/display_list_image.h"
8+
#include "flutter/fml/macros.h"
9+
#include "impeller/renderer/texture.h"
10+
11+
namespace impeller {
12+
13+
class DlImageImpeller final : public flutter::DlImage {
14+
public:
15+
static sk_sp<DlImageImpeller> Make(std::shared_ptr<Texture> texture);
16+
17+
// |DlImage|
18+
~DlImageImpeller() override;
19+
20+
// |DlImage|
21+
sk_sp<SkImage> skia_image() const override;
22+
23+
// |DlImage|
24+
std::shared_ptr<impeller::Texture> impeller_texture() const override;
25+
26+
// |DlImage|
27+
bool isTextureBacked() const override;
28+
29+
// |DlImage|
30+
SkISize dimensions() const override;
31+
32+
// |DlImage|
33+
size_t GetApproximateByteSize() const override;
34+
35+
private:
36+
std::shared_ptr<Texture> texture_;
37+
38+
explicit DlImageImpeller(std::shared_ptr<Texture> texture);
39+
40+
FML_DISALLOW_COPY_AND_ASSIGN(DlImageImpeller);
41+
};
42+
43+
} // namespace impeller

impeller/display_list/display_list_unittests.cc

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/display_list/display_list_builder.h"
66
#include "flutter/testing/testing.h"
7+
#include "impeller/display_list/display_list_image_impeller.h"
78
#include "impeller/display_list/display_list_playground.h"
89
#include "third_party/skia/include/core/SkPathBuilder.h"
910

@@ -27,6 +28,14 @@ TEST_F(DisplayListTest, CanDrawTextBlob) {
2728
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
2829
}
2930

31+
TEST_F(DisplayListTest, CanDrawImage) {
32+
auto texture = CreateTextureForFixture("embarcadero.jpg");
33+
flutter::DisplayListBuilder builder;
34+
builder.drawImage(DlImageImpeller::Make(texture), SkPoint::Make(100, 100),
35+
SkSamplingOptions{}, true);
36+
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
37+
}
38+
3039
TEST_F(DisplayListTest, CapsAndJoins) {
3140
flutter::DisplayListBuilder builder;
3241

impeller/entity/contents/filters/filter_contents.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool FilterContents::Render(const ContentContext& renderer,
9999

100100
auto contents = std::make_shared<TextureContents>();
101101
contents->SetTexture(snapshot.texture);
102-
contents->SetSourceRect(IRect::MakeSize(snapshot.texture->GetSize()));
102+
contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize())));
103103

104104
Entity e;
105105
e.SetPath(PathBuilder{}.AddRect(GetBounds(entity)).GetCurrentPath());
@@ -162,7 +162,7 @@ static std::optional<Contents::Snapshot> ResolveSnapshotForInput(
162162
const ContentContext& renderer, RenderPass& pass) -> bool {
163163
TextureContents contents;
164164
contents.SetTexture(texture);
165-
contents.SetSourceRect(IRect::MakeSize(texture->GetSize()));
165+
contents.SetSourceRect(Rect::MakeSize(Size(texture->GetSize())));
166166
Entity sub_entity;
167167
sub_entity.SetPath(entity.GetPath());
168168
sub_entity.SetBlendMode(Entity::BlendMode::kSource);

0 commit comments

Comments
 (0)