Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 54724ac

Browse files
committed
adjust to new RasterCache::Draw() API and code cleanup
1 parent 0125a64 commit 54724ac

File tree

5 files changed

+73
-116
lines changed

5 files changed

+73
-116
lines changed

flow/layers/opacity_layer_unittests.cc

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ TEST_F(OpacityLayerTest, ChildIsCached) {
6666
#ifndef SUPPORT_FRACTIONAL_TRANSLATION
6767
cache_ctm = RasterCache::GetIntegralTransCTM(cache_ctm);
6868
#endif
69+
SkCanvas cache_canvas;
70+
cache_canvas.setMatrix(cache_ctm);
71+
SkCanvas other_canvas;
72+
other_canvas.setMatrix(other_transform);
6973

7074
EXPECT_EQ(raster_cache()->LayerCacheCount(), 0);
71-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer.get(), other_transform));
72-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer.get(), cache_ctm));
75+
EXPECT_FALSE(raster_cache()->Draw(mock_layer.get(), other_canvas));
76+
EXPECT_FALSE(raster_cache()->Draw(mock_layer.get(), cache_canvas));
7377

7478
layer->Preroll(preroll_context(), initial_transform);
7579

7680
EXPECT_EQ(raster_cache()->LayerCacheCount(), 1);
77-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer.get(), other_transform));
78-
EXPECT_TRUE(raster_cache()->WasPrepared(mock_layer.get(), cache_ctm));
81+
EXPECT_FALSE(raster_cache()->Draw(mock_layer.get(), other_canvas));
82+
EXPECT_TRUE(raster_cache()->Draw(mock_layer.get(), cache_canvas));
7983
}
8084

8185
TEST_F(OpacityLayerTest, ChildrenNotCached) {
@@ -95,20 +99,24 @@ TEST_F(OpacityLayerTest, ChildrenNotCached) {
9599
#ifndef SUPPORT_FRACTIONAL_TRANSLATION
96100
cache_ctm = RasterCache::GetIntegralTransCTM(cache_ctm);
97101
#endif
102+
SkCanvas cache_canvas;
103+
cache_canvas.setMatrix(cache_ctm);
104+
SkCanvas other_canvas;
105+
other_canvas.setMatrix(other_transform);
98106

99107
EXPECT_EQ(raster_cache()->LayerCacheCount(), 0);
100-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer1.get(), other_transform));
101-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer1.get(), cache_ctm));
102-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer2.get(), other_transform));
103-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer2.get(), cache_ctm));
108+
EXPECT_FALSE(raster_cache()->Draw(mock_layer1.get(), other_canvas));
109+
EXPECT_FALSE(raster_cache()->Draw(mock_layer1.get(), cache_canvas));
110+
EXPECT_FALSE(raster_cache()->Draw(mock_layer2.get(), other_canvas));
111+
EXPECT_FALSE(raster_cache()->Draw(mock_layer2.get(), cache_canvas));
104112

105113
layer->Preroll(preroll_context(), initial_transform);
106114

107115
EXPECT_EQ(raster_cache()->LayerCacheCount(), 1);
108-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer1.get(), other_transform));
109-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer1.get(), cache_ctm));
110-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer2.get(), other_transform));
111-
EXPECT_FALSE(raster_cache()->WasPrepared(mock_layer2.get(), cache_ctm));
116+
EXPECT_FALSE(raster_cache()->Draw(mock_layer1.get(), other_canvas));
117+
EXPECT_FALSE(raster_cache()->Draw(mock_layer1.get(), cache_canvas));
118+
EXPECT_FALSE(raster_cache()->Draw(mock_layer2.get(), other_canvas));
119+
EXPECT_FALSE(raster_cache()->Draw(mock_layer2.get(), cache_canvas));
112120
}
113121

114122
TEST_F(OpacityLayerTest, FullyOpaque) {

flow/raster_cache.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ static RasterCacheResult Rasterize(
122122
}
123123

124124
static RasterCacheResult RasterizePicture(SkPicture* picture,
125-
GrContext* context,
126-
const SkMatrix& ctm,
127-
SkColorSpace* dst_color_space,
128-
bool checkerboard) {
125+
GrContext* context,
126+
const SkMatrix& ctm,
127+
SkColorSpace* dst_color_space,
128+
bool checkerboard) {
129129
return Rasterize(context, ctm, dst_color_space, checkerboard,
130130
picture->cullRect(),
131131
[=](SkCanvas* canvas) { canvas->drawPicture(picture); });

flow/raster_cache.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,30 @@ class RasterCache {
7878
// 4. There are too many pictures to be cached in the current frame.
7979
// (See also kDefaultPictureCacheLimitPerFrame.)
8080
virtual bool Prepare(GrContext* context,
81-
SkPicture* picture,
82-
const SkMatrix& transformation_matrix,
83-
SkColorSpace* dst_color_space,
84-
bool is_complex,
85-
bool will_change);
81+
SkPicture* picture,
82+
const SkMatrix& transformation_matrix,
83+
SkColorSpace* dst_color_space,
84+
bool is_complex,
85+
bool will_change);
8686

87-
virtual void Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm);
87+
virtual void Prepare(PrerollContext* context,
88+
Layer* layer,
89+
const SkMatrix& ctm);
8890

8991
// Find the raster cache for the picture and draw it to the canvas.
9092
//
9193
// Return true if it's found and drawn.
92-
bool Draw(const SkPicture& picture, SkCanvas& canvas) const;
94+
virtual bool Draw(const SkPicture& picture, SkCanvas& canvas) const;
9395

9496
// Find the raster cache for the layer and draw it to the canvas.
9597
//
9698
// Addional paint can be given to change how the raster cache is drawn (e.g.,
9799
// draw the raster cache with some opacity).
98100
//
99101
// Return true if the layer raster cache is found and drawn.
100-
bool Draw(const Layer* layer,
101-
SkCanvas& canvas,
102-
SkPaint* paint = nullptr) const;
102+
virtual bool Draw(const Layer* layer,
103+
SkCanvas& canvas,
104+
SkPaint* paint = nullptr) const;
103105

104106
void SweepAfterFrame();
105107

testing/mock_raster_cache.cc

Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "flutter/flow/layers/layer.h"
65
#include "flutter/testing/mock_raster_cache.h"
6+
#include "flutter/flow/layers/layer.h"
77

88
namespace flutter {
99
namespace testing {
1010

11-
// RasterCacheResult::RasterCacheResult(sk_sp<SkImage> image,
12-
// const SkRect& logical_rect)
13-
// : image_(std::move(image)), logical_rect_(logical_rect) {}
14-
15-
// void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
16-
// TRACE_EVENT0("flutter", "RasterCacheResult::draw");
17-
// SkAutoCanvasRestore auto_restore(&canvas, true);
18-
// SkIRect bounds =
19-
// RasterCache::GetDeviceBounds(logical_rect_, canvas.getTotalMatrix());
20-
// FML_DCHECK(
21-
// std::abs(bounds.size().width() - image_->dimensions().width()) <= 1 &&
22-
// std::abs(bounds.size().height() - image_->dimensions().height()) <= 1);
23-
// canvas.resetMatrix();
24-
// canvas.drawImage(image_, bounds.fLeft, bounds.fTop, paint);
25-
// }
26-
27-
MockRasterCache::MockRasterCache()
28-
: RasterCache::RasterCache(1, 1000000) { }
11+
MockRasterCache::MockRasterCache() : RasterCache::RasterCache(1, 1000000) {}
2912

3013
static bool CanRasterizePicture(SkPicture* picture) {
3114
if (picture == nullptr) {
@@ -73,55 +56,22 @@ static bool IsPictureWorthRasterizing(SkPicture* picture,
7356
return picture->approximateOpCount() > 5;
7457
}
7558

76-
/// @note Procedure doesn't copy all closures.
77-
static RasterCacheResult Rasterize(
78-
const SkMatrix& ctm,
79-
SkColorSpace* dst_color_space,
80-
const SkRect& logical_rect) {
81-
TRACE_EVENT0("flutter", "RasterCachePopulate");
82-
SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm);
83-
84-
const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
85-
cache_rect.width(), cache_rect.height(), sk_ref_sp(dst_color_space));
86-
87-
sk_sp<SkData> data = SkData::MakeUninitialized(image_info.computeMinByteSize());
88-
sk_sp<SkImage> image = SkImage::MakeRasterData(image_info, data, image_info.minRowBytes());
89-
90-
return {image, logical_rect};
91-
}
92-
93-
static RasterCacheResult RasterizePicture(SkPicture* picture,
94-
const SkMatrix& ctm,
95-
SkColorSpace* dst_color_space) {
96-
return Rasterize(ctm, dst_color_space, picture->cullRect());
97-
}
98-
9959
void MockRasterCache::Prepare(PrerollContext* context,
10060
Layer* layer,
10161
const SkMatrix& ctm) {
10262
LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
103-
Entry& entry = layer_cache_[cache_key];
63+
MockEntry& entry = layer_cache_[cache_key];
10464
entry.access_count++;
10565
entry.used_this_frame = true;
106-
if (!entry.image.is_valid()) {
107-
entry.image = Rasterize(
108-
ctm, context->dst_color_space,
109-
layer->paint_bounds());
110-
}
111-
}
112-
113-
bool MockRasterCache::WasPrepared(Layer* layer, const SkMatrix& ctm) {
114-
LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
115-
// return layer_cache_.contains(cache_key); - Requires STD_VER > 17, C++20
116-
return layer_cache_.find(cache_key) != layer_cache_.end();
66+
entry.rasterized = true;
11767
}
11868

11969
bool MockRasterCache::Prepare(GrContext* context,
120-
SkPicture* picture,
121-
const SkMatrix& transformation_matrix,
122-
SkColorSpace* dst_color_space,
123-
bool is_complex,
124-
bool will_change) {
70+
SkPicture* picture,
71+
const SkMatrix& transformation_matrix,
72+
SkColorSpace* dst_color_space,
73+
bool is_complex,
74+
bool will_change) {
12575
if (!IsPictureWorthRasterizing(picture, will_change, is_complex)) {
12676
// We only deal with pictures that are worthy of rasterization.
12777
return false;
@@ -139,42 +89,40 @@ bool MockRasterCache::Prepare(GrContext* context,
13989
PictureRasterCacheKey cache_key(picture->uniqueID(), transformation_matrix);
14090

14191
// Creates an entry, if not present prior.
142-
Entry& entry = picture_cache_[cache_key];
92+
MockEntry& entry = picture_cache_[cache_key];
93+
entry.rasterized = true;
14394

144-
if (!entry.image.is_valid()) {
145-
entry.image = RasterizePicture(picture, transformation_matrix,
146-
dst_color_space);
147-
}
14895
return true;
14996
}
15097

151-
RasterCacheResult MockRasterCache::Get(const SkPicture& picture,
152-
const SkMatrix& ctm) const {
153-
PictureRasterCacheKey cache_key(picture.uniqueID(), ctm);
98+
bool MockRasterCache::Draw(const SkPicture& picture, SkCanvas& canvas) const {
99+
PictureRasterCacheKey cache_key(picture.uniqueID(), canvas.getTotalMatrix());
154100
auto it = picture_cache_.find(cache_key);
155101
if (it == picture_cache_.end()) {
156-
return RasterCacheResult();
102+
return false;
157103
}
158104

159-
Entry& entry = it->second;
105+
MockEntry& entry = it->second;
160106
entry.access_count++;
161107
entry.used_this_frame = true;
162108

163-
return entry.image;
109+
return entry.rasterized;
164110
}
165111

166-
RasterCacheResult MockRasterCache::Get(Layer* layer, const SkMatrix& ctm) const {
167-
LayerRasterCacheKey cache_key(layer->unique_id(), ctm);
112+
bool MockRasterCache::Draw(const Layer* layer,
113+
SkCanvas& canvas,
114+
SkPaint* paint) const {
115+
LayerRasterCacheKey cache_key(layer->unique_id(), canvas.getTotalMatrix());
168116
auto it = layer_cache_.find(cache_key);
169117
if (it == layer_cache_.end()) {
170-
return RasterCacheResult();
118+
return false;
171119
}
172120

173-
Entry& entry = it->second;
121+
MockEntry& entry = it->second;
174122
entry.access_count++;
175123
entry.used_this_frame = true;
176124

177-
return entry.image;
125+
return entry.rasterized;
178126
}
179127

180128
void MockRasterCache::SweepAfterFrame() {
@@ -191,8 +139,7 @@ size_t MockRasterCache::GetCachedEntriesCount() const {
191139
return layer_cache_.size() + picture_cache_.size();
192140
}
193141

194-
void MockRasterCache::SetCheckboardCacheImages(bool checkerboard) {
195-
}
142+
void MockRasterCache::SetCheckboardCacheImages(bool checkerboard) {}
196143

197144
} // namespace testing
198145
} // namespace flutter

testing/mock_raster_cache.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#define TESTING_MOCK_RASTER_CACHE_H_
77

88
#include "flutter/flow/raster_cache.h"
9-
#include "third_party/skia/include/core/SkPicture.h"
109
#include "third_party/skia/include/core/SkImage.h"
10+
#include "third_party/skia/include/core/SkPicture.h"
1111

1212
namespace flutter {
1313
namespace testing {
@@ -35,13 +35,15 @@ class MockRasterCache : public RasterCache {
3535
bool is_complex,
3636
bool will_change) override;
3737

38-
void Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm) override;
38+
void Prepare(PrerollContext* context,
39+
Layer* layer,
40+
const SkMatrix& ctm) override;
3941

40-
RasterCacheResult Get(const SkPicture& picture, const SkMatrix& ctm) const override;
42+
bool Draw(const SkPicture& picture, SkCanvas& canvas) const override;
4143

42-
RasterCacheResult Get(Layer* layer, const SkMatrix& ctm) const override;
43-
44-
bool WasPrepared(Layer* layer, const SkMatrix& ctm);
44+
bool Draw(const Layer* layer,
45+
SkCanvas& canvas,
46+
SkPaint* paint = nullptr) const override;
4547

4648
void SweepAfterFrame();
4749

@@ -56,21 +58,19 @@ class MockRasterCache : public RasterCache {
5658

5759
MockRasterCache();
5860

59-
protected:
60-
6161
private:
62-
struct Entry {
62+
struct MockEntry {
6363
bool used_this_frame = false;
6464
size_t access_count = 0;
65-
RasterCacheResult image;
65+
bool rasterized;
6666
};
6767

6868
template <class Cache>
6969
static void SweepOneCacheAfterFrame(Cache& cache) {
7070
std::vector<typename Cache::iterator> dead;
7171

7272
for (auto it = cache.begin(); it != cache.end(); ++it) {
73-
Entry& entry = it->second;
73+
MockEntry& entry = it->second;
7474
if (!entry.used_this_frame) {
7575
dead.push_back(it);
7676
}
@@ -82,8 +82,8 @@ class MockRasterCache : public RasterCache {
8282
}
8383
}
8484

85-
mutable PictureRasterCacheKey::Map<Entry> picture_cache_;
86-
mutable LayerRasterCacheKey::Map<Entry> layer_cache_;
85+
mutable PictureRasterCacheKey::Map<MockEntry> picture_cache_;
86+
mutable LayerRasterCacheKey::Map<MockEntry> layer_cache_;
8787
};
8888

8989
} // namespace testing

0 commit comments

Comments
 (0)