Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Enumerations | Functions | Variables
SkResourceCacheTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkSize.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/private/chromium/SkDiscardableMemory.h"
#include "src/core/SkBitmapCache.h"
#include "src/core/SkCachedData.h"
#include "src/core/SkMipmap.h"
#include "src/core/SkResourceCache.h"
#include "src/image/SkImage_Base.h"
#include "src/lazy/SkDiscardableMemoryPool.h"
#include "tests/Test.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <memory>

Go to the source code of this file.

Classes

struct  TestKey
 
struct  TestRec
 

Enumerations

enum  LockedState { kNotLocked , kLocked }
 
enum  CachedState { kNotInCache , kInCache }
 

Functions

static void check_data (skiatest::Reporter *reporter, const SkCachedData *data, int refcnt, CachedState cacheState, LockedState lockedState)
 
static void test_mipmapcache (skiatest::Reporter *reporter, SkResourceCache *cache)
 
static void test_mipmap_notify (skiatest::Reporter *reporter, SkResourceCache *cache)
 
static SkDiscardableMemorypool_factory (size_t bytes)
 
static void testBitmapCache_discarded_bitmap (skiatest::Reporter *reporter, SkResourceCache *cache, SkResourceCache::DiscardableFactory factory)
 
 DEF_TEST (BitmapCache_discarded_bitmap, reporter)
 
static void test_discarded_image (skiatest::Reporter *reporter, const SkMatrix &transform, sk_sp< SkImage >(*buildImage)())
 
 DEF_TEST (BitmapCache_discarded_image, reporter)
 
static void test_duplicate_add (SkResourceCache *cache, skiatest::Reporter *reporter, bool purgable)
 
 DEF_TEST (ResourceCache_purge, reporter)
 

Variables

static SkDiscardableMemoryPoolgPool = nullptr
 
static int gFactoryCalls = 0
 
static void * gTestNamespace
 

Enumeration Type Documentation

◆ CachedState

Enumerator
kNotInCache 
kInCache 

Definition at line 44 of file SkResourceCacheTest.cpp.

44 {
47};

◆ LockedState

Enumerator
kNotLocked 
kLocked 

Definition at line 39 of file SkResourceCacheTest.cpp.

39 {
41 kLocked,
42};

Function Documentation

◆ check_data()

static void check_data ( skiatest::Reporter reporter,
const SkCachedData data,
int  refcnt,
CachedState  cacheState,
LockedState  lockedState 
)
static

Definition at line 49 of file SkResourceCacheTest.cpp.

50 {
51 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
52 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
53 bool isLocked = (data->data() != nullptr);
54 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
55}
reporter
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41

◆ DEF_TEST() [1/3]

DEF_TEST ( BitmapCache_discarded_bitmap  ,
reporter   
)

Definition at line 142 of file SkResourceCacheTest.cpp.

142 {
143 const size_t byteLimit = 100 * 1024;
144 {
145 SkResourceCache cache(byteLimit);
147 }
148 {
150 gPool = pool.get();
152 SkResourceCache cache(factory);
154 }
156}
AutoreleasePool pool
static void testBitmapCache_discarded_bitmap(skiatest::Reporter *reporter, SkResourceCache *cache, SkResourceCache::DiscardableFactory factory)
static SkDiscardableMemory * pool_factory(size_t bytes)
static SkDiscardableMemoryPool * gPool
static int gFactoryCalls
static sk_sp< SkDiscardableMemoryPool > Make(size_t size)
SkDiscardableMemory *(* DiscardableFactory)(size_t bytes)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition switches.h:191

◆ DEF_TEST() [2/3]

DEF_TEST ( BitmapCache_discarded_image  ,
reporter   
)

Definition at line 190 of file SkResourceCacheTest.cpp.

190 {
191 // Cache entries associated with SkImages fall into two categories:
192 //
193 // 1) generated image bitmaps (managed by the image cacherator)
194 // 2) scaled/resampled bitmaps (cached when HQ filters are used)
195 //
196 // To exercise the first cache type, we use generated/picture-backed SkImages.
197 // To exercise the latter, we draw scaled bitmap images using HQ filters.
198
199 const SkMatrix xforms[] = {
200 SkMatrix::Scale(1, 1),
201 SkMatrix::Scale(1.7f, 0.5f),
202 };
203
204 for (size_t i = 0; i < std::size(xforms); ++i) {
205 test_discarded_image(reporter, xforms[i], []() {
207 surface->getCanvas()->clear(SK_ColorCYAN);
208 return surface->makeImageSnapshot();
209 });
210
211 test_discarded_image(reporter, xforms[i], []() {
212 SkPictureRecorder recorder;
213 SkCanvas* canvas = recorder.beginRecording(10, 10);
214 canvas->clear(SK_ColorCYAN);
216 SkISize::Make(10, 10),
217 nullptr,
218 nullptr,
221 });
222 }
223}
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
static void test_discarded_image(skiatest::Reporter *reporter, const SkMatrix &transform, sk_sp< SkImage >(*buildImage)())
void clear(SkColor color)
Definition SkCanvas.h:1199
static sk_sp< SkColorSpace > MakeSRGB()
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
@ kU8
uses 8-bit unsigned int per color component
PODArray< SkRSXform > xforms
Definition SkRecords.h:332
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)

◆ DEF_TEST() [3/3]

DEF_TEST ( ResourceCache_purge  ,
reporter   
)

Definition at line 297 of file SkResourceCacheTest.cpp.

297 {
298 for (bool purgable : { false, true }) {
299 {
300 SkResourceCache cache(1024 * 1024);
301 test_duplicate_add(&cache, reporter, purgable);
302 }
303 {
305 test_duplicate_add(&cache, reporter, purgable);
306 }
307 }
308}
static void test_duplicate_add(SkResourceCache *cache, skiatest::Reporter *reporter, bool purgable)
static SkDiscardableMemory * Create(size_t bytes)

◆ pool_factory()

static SkDiscardableMemory * pool_factory ( size_t  bytes)
static

Definition at line 130 of file SkResourceCacheTest.cpp.

130 {
133 return gPool->create(bytes);
134}
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual SkDiscardableMemory * create(size_t bytes)=0

◆ test_discarded_image()

static void test_discarded_image ( skiatest::Reporter reporter,
const SkMatrix transform,
sk_sp< SkImage >(*)()  buildImage 
)
static

Definition at line 158 of file SkResourceCacheTest.cpp.

159 {
161 SkCanvas* canvas = surface->getCanvas();
162
163 // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times
164 // to mitigate this risk.
165 const unsigned kRepeatCount = 42;
166 for (unsigned i = 0; i < kRepeatCount; ++i) {
167 SkAutoCanvasRestore acr(canvas, true);
168
169 sk_sp<SkImage> image(buildImage());
170
171 // draw the image (with a transform, to tickle different code paths) to ensure
172 // any associated resources get cached
173 canvas->concat(transform);
174 // always use high quality to ensure caching when scaled
175 canvas->drawImage(image, 0, 0, SkSamplingOptions({1.0f/3, 1.0f/3}));
176
177 const auto desc = SkBitmapCacheDesc::Make(image.get());
178
179 // delete the image
180 image.reset(nullptr);
181
182 // all resources should have been purged
185 }
186}
static bool Find(const SkBitmapCacheDesc &, SkBitmap *result)
void concat(const SkMatrix &matrix)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
T * get() const
Definition SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
sk_sp< SkImage > image
Definition examples.cpp:29
GAsyncResult * result
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
static SkBitmapCacheDesc Make(const SkImage *)

◆ test_duplicate_add()

static void test_duplicate_add ( SkResourceCache cache,
skiatest::Reporter reporter,
bool  purgable 
)
static

Definition at line 259 of file SkResourceCacheTest.cpp.

260 {
261 int sharedID = 1;
262 int data = 0;
263
264 int flags0 = 0, flags1 = 0;
265
266 auto rec0 = std::make_unique<TestRec>(sharedID, data, &flags0);
267 auto rec1 = std::make_unique<TestRec>(sharedID, data, &flags1);
268 SkASSERT(rec0->getKey() == rec1->getKey());
269
270 TestRec* r0 = rec0.get(); // save the bare-pointer since we will release rec0
271 r0->fCanBePurged = purgable;
272
273 REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall));
274 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
275
276 cache->add(rec0.release(), nullptr);
277 REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall);
278 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
279 flags0 = 0; // reset the flag
280
281 cache->add(rec1.release(), nullptr);
282 if (purgable) {
283 // we purged rec0, and did install rec1
284 REPORTER_ASSERT(reporter, !(flags0 & TestRec::kDidInstall));
285 REPORTER_ASSERT(reporter, flags1 & TestRec::kDidInstall);
286 } else {
287 // we re-used rec0 and did not install rec1
288 REPORTER_ASSERT(reporter, flags0 & TestRec::kDidInstall);
289 REPORTER_ASSERT(reporter, !(flags1 & TestRec::kDidInstall));
290 r0->fCanBePurged = true; // so we can cleanup the cache
291 }
292}

◆ test_mipmap_notify()

static void test_mipmap_notify ( skiatest::Reporter reporter,
SkResourceCache cache 
)
static

Definition at line 96 of file SkResourceCacheTest.cpp.

96 {
97 const int N = 3;
98
99 SkBitmap src[N];
100 sk_sp<SkImage> img[N];
102 for (int i = 0; i < N; ++i) {
103 src[i].allocN32Pixels(5, 5);
104 src[i].setImmutable();
105 img[i] = src[i].asImage();
106 SkMipmapCache::AddAndRef(as_IB(img[i].get()), cache)->unref();
107 desc[i] = SkBitmapCacheDesc::Make(img[i].get());
108 }
109
110 for (int i = 0; i < N; ++i) {
111 const SkMipmap* mipmap = SkMipmapCache::FindAndRef(desc[i], cache);
112 // We're always using a local cache, so we know we won't be purged by other threads
113 REPORTER_ASSERT(reporter, mipmap);
114 SkSafeUnref(mipmap);
115
116 img[i].reset(); // delete the image, which *should not* remove us from the cache
117 mipmap = SkMipmapCache::FindAndRef(desc[i], cache);
118 REPORTER_ASSERT(reporter, mipmap);
119 SkSafeUnref(mipmap);
120
121 src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
122 mipmap = SkMipmapCache::FindAndRef(desc[i], cache);
123 REPORTER_ASSERT(reporter, !mipmap);
124 }
125}
static SkImage_Base * as_IB(SkImage *image)
static void SkSafeUnref(T *obj)
Definition SkRefCnt.h:149
#define N
Definition beziers.cpp:19
void unref() const
static const SkMipmap * AddAndRef(const SkImage_Base *, SkResourceCache *localCache=nullptr)
static const SkMipmap * FindAndRef(const SkBitmapCacheDesc &, SkResourceCache *localCache=nullptr)
const myers::Point & get(const myers::Segment &)

◆ test_mipmapcache()

static void test_mipmapcache ( skiatest::Reporter reporter,
SkResourceCache cache 
)
static

Definition at line 57 of file SkResourceCacheTest.cpp.

57 {
58 cache->purgeAll();
59
61 src.allocN32Pixels(5, 5);
62 src.setImmutable();
63 sk_sp<SkImage> img = src.asImage();
64 const auto desc = SkBitmapCacheDesc::Make(img.get());
65
66 const SkMipmap* mipmap = SkMipmapCache::FindAndRef(desc, cache);
67 REPORTER_ASSERT(reporter, nullptr == mipmap);
68
69 mipmap = SkMipmapCache::AddAndRef(as_IB(img.get()), cache);
71
72 {
73 const SkMipmap* mm = SkMipmapCache::FindAndRef(desc, cache);
75 REPORTER_ASSERT(reporter, mm == mipmap);
76 mm->unref();
77 }
78
79 check_data(reporter, mipmap, 2, kInCache, kLocked);
80
81 mipmap->unref();
82 // tricky, since technically after this I'm no longer an owner, but since the cache is
83 // local, I know it won't get purged behind my back
85
86 // find us again
87 mipmap = SkMipmapCache::FindAndRef(desc, cache);
88 check_data(reporter, mipmap, 2, kInCache, kLocked);
89
90 cache->purgeAll();
92
93 mipmap->unref();
94}
static void check_data(skiatest::Reporter *reporter, const SkCachedData *data, int refcnt, CachedState cacheState, LockedState lockedState)

◆ testBitmapCache_discarded_bitmap()

static void testBitmapCache_discarded_bitmap ( skiatest::Reporter reporter,
SkResourceCache cache,
SkResourceCache::DiscardableFactory  factory 
)
static

Definition at line 136 of file SkResourceCacheTest.cpp.

137 {
140}
static void test_mipmapcache(skiatest::Reporter *reporter, SkResourceCache *cache)
static void test_mipmap_notify(skiatest::Reporter *reporter, SkResourceCache *cache)

Variable Documentation

◆ gFactoryCalls

int gFactoryCalls = 0
static

Definition at line 128 of file SkResourceCacheTest.cpp.

◆ gPool

SkDiscardableMemoryPool* gPool = nullptr
static

Definition at line 127 of file SkResourceCacheTest.cpp.

◆ gTestNamespace

void* gTestNamespace
static

Definition at line 227 of file SkResourceCacheTest.cpp.