blob: 620a2fead539f475c9c1705426f329c8a42e605d [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00008#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009
bsalomonbcf0a522014-10-08 08:40:09 -070010#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070012#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourceCacheAccess.h"
14#include "GrGpuResourcePriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080015#include "GrResourceCache.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080017#include "SkGr.h"
18#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070019#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000020#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000021
22static const int gWidth = 640;
23static const int gHeight = 480;
24
25////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080026static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000027 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
28
29 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000030 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031 src.eraseColor(SK_ColorBLACK);
32 size_t srcSize = src.getSize();
33
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000034 size_t initialCacheSize;
35 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000036
37 int oldMaxNum;
38 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000039 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000040
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041 // Set the cache limits so we can fit 10 "src" images and the
42 // max number of textures doesn't matter
43 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000044 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045
46 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000047 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048
49 for (int i = 0; i < 100; ++i) {
50 canvas->drawBitmap(src, 0, 0);
51 canvas->readPixels(size, &readback);
52
53 // "modify" the src texture
54 src.notifyPixelsChanged();
55
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000056 size_t curCacheSize;
57 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000058
59 // we should never go over the size limit
60 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
61 }
62
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000063 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064}
65
bsalomon6d3fe022014-07-25 08:35:45 -070066class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000067 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -080068 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000069public:
70 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -080071 /** Property that distinctly categorizes the resource.
72 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -080073 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -080074
bsalomon5236cf42015-01-14 10:42:08 -080075 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
76 : INHERITED(gpu, lifeCycle)
77 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -080078 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -080079 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -080080 ++fNumAlive;
81 this->registerWithCache();
82 }
83
84 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
85 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -080086 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -080087 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -080088 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -080089 ++fNumAlive;
90 this->registerWithCache();
91 }
92
bsalomon8b79d232014-11-10 10:19:06 -080093 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -080094 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000095 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -080096 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -080097 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -080098 ++fNumAlive;
99 this->registerWithCache();
100 }
101
bsalomon23e619c2015-02-06 11:54:28 -0800102 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800103 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000104 }
105
106 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800107 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800108 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000109 }
110
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000111 void setSize(size_t size) {
112 fSize = size;
113 this->didChangeGpuMemorySize();
114 }
115
bsalomon33435572014-11-05 14:47:41 -0800116 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000117
bsalomon71cb0c22014-11-14 12:10:14 -0800118 void setUnrefWhenDestroyed(TestResource* resource) {
119 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000120 }
121
bsalomon1c60dfe2015-01-21 09:32:40 -0800122 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
123 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
124 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800125 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
126 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800127 }
128 }
129
130 static size_t ExpectedScratchKeySize() {
131 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
132 }
133
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000134private:
bsalomon24db3b12015-01-23 04:24:04 -0800135 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800136
bsalomonc2f35b72015-01-23 07:19:22 -0800137 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
138 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800139 , fToDelete(NULL)
140 , fSize(kDefaultSize)
141 , fProperty(property) {
142 GrScratchKey scratchKey;
143 ComputeScratchKey(fProperty, &scratchKey);
144 this->setScratchKey(scratchKey);
145 ++fNumAlive;
146 this->registerWithCache();
147 }
148
bsalomon69ed47f2014-11-12 11:13:39 -0800149 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
150
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000151 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000152 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800153 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800154 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700155 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000156};
bsalomon33435572014-11-05 14:47:41 -0800157int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000158
bsalomonc2f35b72015-01-23 07:19:22 -0800159class Mock {
160public:
161 Mock(int maxCnt, size_t maxBytes) {
162 fContext.reset(GrContext::CreateMockContext());
163 SkASSERT(fContext);
164 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800165 GrResourceCache* cache = fContext->getResourceCache();
166 cache->purgeAllUnlocked();
167 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800168 }
bsalomonc2f35b72015-01-23 07:19:22 -0800169
bsalomon0ea80f42015-02-11 10:49:59 -0800170 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800171
172 GrContext* context() { return fContext; }
173
174private:
175 SkAutoTUnref<GrContext> fContext;
176};
177
178static void test_no_key(skiatest::Reporter* reporter) {
179 Mock mock(10, 30000);
180 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800181 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800182
183 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800184 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
185 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
186 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
187 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800188 a->setSize(11);
189 b->setSize(12);
190 c->setSize(13);
191 d->setSize(14);
192
193 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800194 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800195 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800196 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800197
198 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800199 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800200 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
201
202 // Since the resources have neither content nor scratch keys, delete immediately upon unref.
203
204 a->unref();
205 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800206 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800207 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800208 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800209
210 c->unref();
211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800212 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800213 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800214 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800215
216 d->unref();
217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800218 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
219 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800220
221 b->unref();
222 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
224 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800225}
226
bsalomon24db3b12015-01-23 04:24:04 -0800227// Each integer passed as a template param creates a new domain.
228template <int> static void make_content_key(GrContentKey* key, int data) {
229 static GrContentKey::Domain d = GrContentKey::GenerateDomain();
230 GrContentKey::Builder builder(key, d, 1);
231 builder[0] = data;
232}
233
bsalomon84c8e622014-11-17 09:33:27 -0800234static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800235 Mock mock(10, 300);
236 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800237 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800238
bsalomon24db3b12015-01-23 04:24:04 -0800239 GrContentKey contentKey;
240 make_content_key<0>(&contentKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800241
242 // Create a scratch, a content, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800243 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800244 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800245 scratch->setSize(10);
bsalomon5236cf42015-01-14 10:42:08 -0800246 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu()));
247 content->setSize(11);
bsalomon3582d3e2015-02-13 14:20:05 -0800248 REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey));
bsalomon5236cf42015-01-14 10:42:08 -0800249 TestResource* wrapped = SkNEW_ARGS(TestResource,
250 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
251 wrapped->setSize(12);
252 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
253 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800254 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800255
256 // Make sure we can't add a content key to the wrapped resource
bsalomon24db3b12015-01-23 04:24:04 -0800257 GrContentKey contentKey2;
258 make_content_key<0>(&contentKey2, 1);
bsalomon3582d3e2015-02-13 14:20:05 -0800259 REPORTER_ASSERT(reporter, !wrapped->resourcePriv().setContentKey(contentKey2));
bsalomon0ea80f42015-02-11 10:49:59 -0800260 REPORTER_ASSERT(reporter, NULL == cache->findAndRefContentResource(contentKey2));
bsalomondace19e2014-11-17 07:34:06 -0800261
262 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800263 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800264 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800265 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800266 cache->getResourceBytes());
267 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800268 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800269 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800270
bsalomon63c992f2015-01-23 12:47:59 -0800271 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800272 cache->purgeAllUnlocked();
273 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800274 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800275 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800276 cache->getResourceBytes());
277 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800278 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800279 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800280
281 // Unreffing the wrapped resource should free it right away.
282 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800283 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800284 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800285 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800286
bsalomon84c8e622014-11-17 09:33:27 -0800287 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800288 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800289 scratch->setSize(12);
290 content->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800291 cache->purgeAllUnlocked();
292 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800293 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800294 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
295 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
296 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800297
298 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800299 cache->purgeAllUnlocked();
300 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800301 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800302 cache->getResourceBytes());
303 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
304 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800305
306 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800307 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
308 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
309 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
310 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800311
312 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800313 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
314 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
315 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
316 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800317}
318
bsalomon5236cf42015-01-14 10:42:08 -0800319static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800320 Mock mock(10, 30000);
321 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800322 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800323
bsalomon24db3b12015-01-23 04:24:04 -0800324 GrContentKey contentKey;
325 make_content_key<0>(&contentKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800326
327 TestResource* scratch;
328 TestResource* content;
329 TestResource* wrapped;
330 TestResource* unbudgeted;
331
332 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800333 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800334 scratch->setSize(10);
335 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800336 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
337 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
338 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
339 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800340
341 content = SkNEW_ARGS(TestResource, (context->getGpu()));
342 content->setSize(11);
bsalomon3582d3e2015-02-13 14:20:05 -0800343 REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey));
bsalomon5236cf42015-01-14 10:42:08 -0800344 content->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800345 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
346 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
347 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
348 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800349
bsalomon0ea80f42015-02-11 10:49:59 -0800350 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800351 unbudgeted = SkNEW_ARGS(TestResource,
352 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800353 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
354 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
355 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
356 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800357
358 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800359 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
360 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
361 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
362 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800363
364 wrapped = SkNEW_ARGS(TestResource,
365 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800366 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
367 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
368 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
369 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800370
371 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800372 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
373 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
374 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
375 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800376
bsalomon0ea80f42015-02-11 10:49:59 -0800377 cache->purgeAllUnlocked();
378 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
379 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
380 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
381 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800382}
383
bsalomon3582d3e2015-02-13 14:20:05 -0800384// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
385void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
386/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800387 Mock mock(10, 300);
388 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800389 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800390
391 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800392 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800393 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800394 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800395
396 size_t size = resource->gpuMemorySize();
397 for (int i = 0; i < 2; ++i) {
398 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800399 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800400 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800401 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
403 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
404 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
405 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
406 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800407
408 // Once it is unrefed, it should become available as scratch.
409 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800410 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
411 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
412 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
413 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
414 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800415 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800416 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800417 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800418 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800419
420 if (0 == i) {
421 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
422 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800423 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800424 } else {
425 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800426 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800427 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
428 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
429 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
430 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800431 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800432 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800433 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800434
435 // now when it is unrefed it should die since it has no key.
436 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800437 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
438 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
439 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
440 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800441 }
bsalomon8b79d232014-11-10 10:19:06 -0800442 }
bsalomonc2f35b72015-01-23 07:19:22 -0800443}
444
445static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
446 Mock mock(5, 30000);
447 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800448 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800449
bsalomon8b79d232014-11-10 10:19:06 -0800450 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800451 TestResource* a = TestResource::CreateScratch(context->getGpu(),
452 TestResource::kB_SimulatedProperty);
453 TestResource* b = TestResource::CreateScratch(context->getGpu(),
454 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800455 a->setSize(11);
456 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800457 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800458 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800459 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800460 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800461
462 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800463 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800464
bsalomon0ea80f42015-02-11 10:49:59 -0800465 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800466 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800467 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
468 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800469 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800471
bsalomon63c992f2015-01-23 12:47:59 -0800472 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800474 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800475 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800476
477 // Unref but don't purge
478 a->unref();
479 b->unref();
480 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800481 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800482
bsalomon63c992f2015-01-23 12:47:59 -0800483 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800484 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800485 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800486 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
487 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800488}
489
bsalomon10e23ca2014-11-25 05:52:06 -0800490static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800491 Mock mock(5, 30000);
492 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800493 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800494
bsalomon10e23ca2014-11-25 05:52:06 -0800495 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800496 TestResource* a = TestResource::CreateScratch(context->getGpu(),
497 TestResource::kB_SimulatedProperty);
498 TestResource* b = TestResource::CreateScratch(context->getGpu(),
499 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800500 a->unref();
501 b->unref();
502
bsalomon1c60dfe2015-01-21 09:32:40 -0800503
504 GrScratchKey scratchKey;
505 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800506 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800507 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800508 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800509
bsalomon0ea80f42015-02-11 10:49:59 -0800510 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800511 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800512 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800513 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
514 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800515
516 // Find the first resource and remove its scratch key
517 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800518 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800519 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800520 // It's still alive, but not cached by scratch key anymore
521 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800522 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
523 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800524
525 // The cache should immediately delete it when it's unrefed since it isn't accessible.
526 find->unref();
527 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800528 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
529 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800530
531 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800532 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800533 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800534 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800535 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
536 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800537
538 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800539 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800540 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800541 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
542 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800543
544 find->unref();
545 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800546 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
547 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800548}
549
bsalomon1c60dfe2015-01-21 09:32:40 -0800550static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800551 Mock mock(5, 30000);
552 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800553 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800554
555 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800556 TestResource* a = TestResource::CreateScratch(context->getGpu(),
557 TestResource::kB_SimulatedProperty);
558 TestResource* b = TestResource::CreateScratch(context->getGpu(),
559 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800560 a->unref();
561 b->unref();
562
563 GrScratchKey scratchKey;
564 // Ensure that scratch key comparison and assignment is consistent.
565 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800566 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800567 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800568 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800569 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
570 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
571 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
572 scratchKey = scratchKey1;
573 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
574 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
575 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
576 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
577 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
578 scratchKey = scratchKey2;
579 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
580 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
581 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
582 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
583 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
584
585 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800586 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800587 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800589
590 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800591 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800592 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800593 REPORTER_ASSERT(reporter, find != NULL);
594 find->unref();
595
596 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800597 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800598 REPORTER_ASSERT(reporter, find != NULL);
599 REPORTER_ASSERT(reporter, find == a || find == b);
600
bsalomon0ea80f42015-02-11 10:49:59 -0800601 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800602 REPORTER_ASSERT(reporter, find2 != NULL);
603 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
604 REPORTER_ASSERT(reporter, find2 != find);
605 find2->unref();
606 find->unref();
607}
608
bsalomon8b79d232014-11-10 10:19:06 -0800609static void test_duplicate_content_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800610 Mock mock(5, 30000);
611 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800612 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000613
bsalomon24db3b12015-01-23 04:24:04 -0800614 GrContentKey key;
615 make_content_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800616
bsalomon8b79d232014-11-10 10:19:06 -0800617 // Create two resources that we will attempt to register with the same content key.
bsalomon5236cf42015-01-14 10:42:08 -0800618 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
619 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800620 a->setSize(11);
621 b->setSize(12);
bsalomon71cb0c22014-11-14 12:10:14 -0800622
623 // Can't set the same content key on two resources.
bsalomon3582d3e2015-02-13 14:20:05 -0800624 REPORTER_ASSERT(reporter, a->resourcePriv().setContentKey(key));
625 REPORTER_ASSERT(reporter, !b->resourcePriv().setContentKey(key));
bsalomon71cb0c22014-11-14 12:10:14 -0800626
627 // Still have two resources because b is still reffed.
bsalomon0ea80f42015-02-11 10:49:59 -0800628 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800629 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800630 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800631 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
632
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000633 b->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800634 // Now b should be gone.
bsalomon0ea80f42015-02-11 10:49:59 -0800635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
636 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800637 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000638
bsalomon0ea80f42015-02-11 10:49:59 -0800639 cache->purgeAllUnlocked();
640 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
641 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800642 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
643
644 // Drop the ref on a but it isn't immediately purged as it still has a valid scratch key.
bsalomon8b79d232014-11-10 10:19:06 -0800645 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800646 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
647 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800648 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
649
bsalomon0ea80f42015-02-11 10:49:59 -0800650 cache->purgeAllUnlocked();
651 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
652 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800653 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000654}
655
bsalomon8b79d232014-11-10 10:19:06 -0800656static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800657 Mock mock(5, 30000);
658 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800659 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800660
bsalomon24db3b12015-01-23 04:24:04 -0800661 GrContentKey key1, key2, key3;
662 make_content_key<0>(&key1, 1);
663 make_content_key<0>(&key2, 2);
664 make_content_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800665
bsalomon23e619c2015-02-06 11:54:28 -0800666 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800667 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
668 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800669 TestResource* c = TestResource::CreateScratch(context->getGpu(),
670 TestResource::kA_SimulatedProperty);
bsalomon3582d3e2015-02-13 14:20:05 -0800671 a->resourcePriv().setContentKey(key1);
672 b->resourcePriv().setContentKey(key2);
673 c->resourcePriv().setContentKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800674 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800675 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800676 c->unref();
677
bsalomon0ea80f42015-02-11 10:49:59 -0800678 REPORTER_ASSERT(reporter, cache->hasContentKey(key1));
679 REPORTER_ASSERT(reporter, cache->hasContentKey(key2));
680 REPORTER_ASSERT(reporter, cache->hasContentKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800681 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800682
683 typedef GrContentKeyInvalidatedMessage Msg;
684 typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus;
685
686 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
687 Bus::Post(Msg(key1));
688 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800689 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800690 // a should be deleted now, but we still have a ref on b.
bsalomon0ea80f42015-02-11 10:49:59 -0800691 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
692 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800694 REPORTER_ASSERT(reporter, cache->hasContentKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800695
696 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800697 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800698 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800699 // we still have a ref on b, c should be recycled as scratch.
700 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800701 REPORTER_ASSERT(reporter, !cache->hasContentKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800702
bsalomon23e619c2015-02-06 11:54:28 -0800703 // make b purgeable. It should be immediately deleted since it has no key.
704 b->unref();
705 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
706
707 // Make sure we actually get to c via it's scratch key, before we say goodbye.
708 GrScratchKey scratchKey;
709 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800710 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800711 REPORTER_ASSERT(reporter, scratch == c);
712 SkSafeUnref(scratch);
713
714 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800715 cache->purgeAllUnlocked();
716 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800717 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800718 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
719 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800720 REPORTER_ASSERT(reporter, !scratch);
721 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800722}
723
bsalomon71cb0c22014-11-14 12:10:14 -0800724static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800725 Mock mock(3, 30000);
726 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800727 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800728
bsalomon24db3b12015-01-23 04:24:04 -0800729 GrContentKey key1, key2;
730 make_content_key<0>(&key1, 1);
731 make_content_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000732
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000733
bsalomonc2f35b72015-01-23 07:19:22 -0800734 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
735 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon3582d3e2015-02-13 14:20:05 -0800736 a->resourcePriv().setContentKey(key1);
737 b->resourcePriv().setContentKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800738
bsalomonc2f35b72015-01-23 07:19:22 -0800739 // Make a cycle
740 a->setUnrefWhenDestroyed(b);
741 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800742
bsalomonc2f35b72015-01-23 07:19:22 -0800743 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800744
bsalomonc2f35b72015-01-23 07:19:22 -0800745 a->unref();
746 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800747
bsalomonc2f35b72015-01-23 07:19:22 -0800748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800749
bsalomon0ea80f42015-02-11 10:49:59 -0800750 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800752
bsalomonc2f35b72015-01-23 07:19:22 -0800753 // Break the cycle
754 a->setUnrefWhenDestroyed(NULL);
755 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800756
bsalomon0ea80f42015-02-11 10:49:59 -0800757 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800758 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000759}
760
bsalomon8b79d232014-11-10 10:19:06 -0800761static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon24db3b12015-01-23 04:24:04 -0800762 GrContentKey key1, key2;
763 make_content_key<0>(&key1, 1);
764 make_content_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000765
766 // Test changing resources sizes (both increase & decrease).
767 {
bsalomonc2f35b72015-01-23 07:19:22 -0800768 Mock mock(3, 30000);
769 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800770 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000771
bsalomon5236cf42015-01-14 10:42:08 -0800772 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon3582d3e2015-02-13 14:20:05 -0800773 a->resourcePriv().setContentKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000774 a->unref();
775
bsalomon5236cf42015-01-14 10:42:08 -0800776 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon3582d3e2015-02-13 14:20:05 -0800777 b->resourcePriv().setContentKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000778 b->unref();
779
bsalomon0ea80f42015-02-11 10:49:59 -0800780 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
781 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800782 {
bsalomon0ea80f42015-02-11 10:49:59 -0800783 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800784 find2->setSize(200);
bsalomon0ea80f42015-02-11 10:49:59 -0800785 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache->findAndRefContentResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800786 find1->setSize(50);
787 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000788
bsalomon0ea80f42015-02-11 10:49:59 -0800789 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
790 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000791 }
792
793 // Test increasing a resources size beyond the cache budget.
794 {
bsalomonc2f35b72015-01-23 07:19:22 -0800795 Mock mock(2, 300);
796 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800797 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000798
bsalomon5236cf42015-01-14 10:42:08 -0800799 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800800 a->setSize(100);
bsalomon3582d3e2015-02-13 14:20:05 -0800801 a->resourcePriv().setContentKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000802 a->unref();
803
bsalomon5236cf42015-01-14 10:42:08 -0800804 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800805 b->setSize(100);
bsalomon3582d3e2015-02-13 14:20:05 -0800806 b->resourcePriv().setContentKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000807 b->unref();
808
bsalomon0ea80f42015-02-11 10:49:59 -0800809 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
810 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000811
bsalomon8b79d232014-11-10 10:19:06 -0800812 {
bsalomon0ea80f42015-02-11 10:49:59 -0800813 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800814 find2->setSize(201);
815 }
bsalomon0ea80f42015-02-11 10:49:59 -0800816 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000817
bsalomon0ea80f42015-02-11 10:49:59 -0800818 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
819 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000820 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000821}
822
bsalomon10e23ca2014-11-25 05:52:06 -0800823static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -0800824 // Set the cache size to double the resource count because we're going to create 2x that number
825 // resources, using two different key domains. Add a little slop to the bytes because we resize
826 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -0800827 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -0800828
bsalomonc2f35b72015-01-23 07:19:22 -0800829 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
830 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800831 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800832
833 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon24db3b12015-01-23 04:24:04 -0800834 GrContentKey key1, key2;
835 make_content_key<1>(&key1, i);
836 make_content_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -0800837
bsalomon24db3b12015-01-23 04:24:04 -0800838 TestResource* resource;
839
bsalomon10e23ca2014-11-25 05:52:06 -0800840 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon3582d3e2015-02-13 14:20:05 -0800841 resource->resourcePriv().setContentKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -0800842 resource->setSize(1);
843 resource->unref();
844
bsalomon10e23ca2014-11-25 05:52:06 -0800845 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon3582d3e2015-02-13 14:20:05 -0800846 resource->resourcePriv().setContentKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -0800847 resource->setSize(1);
848 resource->unref();
849 }
850
851 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -0800852 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
853 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
854 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
855 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -0800856 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon24db3b12015-01-23 04:24:04 -0800857 GrContentKey key1, key2;
858 make_content_key<1>(&key1, i);
859 make_content_key<2>(&key2, i);
860
bsalomon0ea80f42015-02-11 10:49:59 -0800861 REPORTER_ASSERT(reporter, cache->hasContentKey(key1));
862 REPORTER_ASSERT(reporter, cache->hasContentKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -0800863 }
864
bsalomon0ea80f42015-02-11 10:49:59 -0800865 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -0800866 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -0800867 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
868 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
869 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
870 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -0800871
872 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon24db3b12015-01-23 04:24:04 -0800873 GrContentKey key1, key2;
874 make_content_key<1>(&key1, i);
875 make_content_key<2>(&key2, i);
876
bsalomon0ea80f42015-02-11 10:49:59 -0800877 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
878 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -0800879 }
880}
881
882
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000883////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000884DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000885 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
886 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
887 if (!GrContextFactory::IsRenderingGLContext(glType)) {
888 continue;
889 }
890 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -0800891 if (NULL == context) {
892 continue;
893 }
bsalomonf2703d82014-10-28 14:33:06 -0700894 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000895 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -0700896 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000897 desc.fWidth = gWidth;
898 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -0700899 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -0800900 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
901 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -0700902 test_cache(reporter, context, surface->getCanvas());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000903 }
bsalomon33435572014-11-05 14:47:41 -0800904
bsalomon8b79d232014-11-10 10:19:06 -0800905 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -0800906 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -0800907 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -0800908 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -0800909 test_unbudgeted_to_scratch(reporter);
bsalomon8b79d232014-11-10 10:19:06 -0800910 test_duplicate_content_key(reporter);
911 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -0800912 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -0800913 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -0800914 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -0800915 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -0800916 test_resource_size_changed(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -0800917 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000918}
919
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000920#endif