blob: 24ecb949ca91a7034a0823968891ae3b355f0679 [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"
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000013#include "GrResourceCache.h"
bsalomon33435572014-11-05 14:47:41 -080014#include "GrResourceCache2.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "SkCanvas.h"
reed69f6f002014-09-18 06:09:44 -070016#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000017#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000018
19static const int gWidth = 640;
20static const int gHeight = 480;
21
22////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080023static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000024 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
25
26 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000027 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028 src.eraseColor(SK_ColorBLACK);
29 size_t srcSize = src.getSize();
30
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000031 size_t initialCacheSize;
32 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000033
34 int oldMaxNum;
35 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000036 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000037
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000038 // Set the cache limits so we can fit 10 "src" images and the
39 // max number of textures doesn't matter
40 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000041 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000042
43 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000044 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045
46 for (int i = 0; i < 100; ++i) {
47 canvas->drawBitmap(src, 0, 0);
48 canvas->readPixels(size, &readback);
49
50 // "modify" the src texture
51 src.notifyPixelsChanged();
52
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000053 size_t curCacheSize;
54 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000055
56 // we should never go over the size limit
57 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
58 }
59
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000060 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000061}
62
bsalomon6d3fe022014-07-25 08:35:45 -070063class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000064 static const size_t kDefaultSize = 100;
65
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000066public:
67 SK_DECLARE_INST_COUNT(TestResource);
bsalomon8b79d232014-11-10 10:19:06 -080068 TestResource(GrGpu* gpu)
bsalomonc44be0e2014-07-25 07:32:33 -070069 : INHERITED(gpu, false)
70 , fCache(NULL)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000071 , fToDelete(NULL)
bsalomon8b79d232014-11-10 10:19:06 -080072 , fSize(kDefaultSize) {
73 ++fNumAlive;
74 this->registerWithCache();
75 }
76
77 TestResource(GrGpu* gpu, const GrResourceKey& scratchKey)
78 : INHERITED(gpu, false)
79 , fCache(NULL)
80 , fToDelete(NULL)
81 , fSize(kDefaultSize) {
82 this->setScratchKey(scratchKey);
bsalomon33435572014-11-05 14:47:41 -080083 ++fNumAlive;
bsalomon16961262014-08-26 14:01:07 -070084 this->registerWithCache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000085 }
86
87 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -080088 --fNumAlive;
bsalomon49f085d2014-09-05 13:34:00 -070089 if (fToDelete) {
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000090 // Breaks our little 2-element cycle below.
91 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
92 fCache->deleteResource(fToDelete->getCacheEntry());
93 }
bsalomonc44be0e2014-07-25 07:32:33 -070094 this->release();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000095 }
96
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000097 void setSize(size_t size) {
98 fSize = size;
99 this->didChangeGpuMemorySize();
100 }
101
102 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000103
bsalomon33435572014-11-05 14:47:41 -0800104 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000105
106 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
107 fCache = cache;
108 fToDelete = resource;
109 }
110
111private:
112 GrResourceCache* fCache;
113 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000114 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800115 static int fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000116
bsalomon6d3fe022014-07-25 08:35:45 -0700117 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000118};
bsalomon33435572014-11-05 14:47:41 -0800119int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000120
bsalomon8b79d232014-11-10 10:19:06 -0800121static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
122 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
123 REPORTER_ASSERT(reporter, SkToBool(context));
124 if (NULL == context) {
125 return;
126 }
127 context->setResourceCacheLimits(5, 30000);
128 GrResourceCache* cache = context->getResourceCache();
129 SkDEBUGCODE(GrResourceCache2* cache2 = context->getResourceCache2();)
130 cache->purgeAllUnlocked();
131 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
132
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000133 GrCacheID::Key keyData;
bsalomon8b79d232014-11-10 10:19:06 -0800134 GrCacheID::Domain domain = GrResourceKey::ScratchDomain();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000135 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
bsalomon8b79d232014-11-10 10:19:06 -0800136 GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0);
137
138 // Create two resources that have the same scratch key.
139 TestResource* a = new TestResource(context->getGpu(), scratchKey);
140 TestResource* b = new TestResource(context->getGpu(), scratchKey);
141 a->setSize(11);
142 b->setSize(12);
143 // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
144 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
145
146 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, a));
147
148 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
149
150 // Can't add the same resource twice.
151 REPORTER_ASSERT(reporter, !cache->addResource(scratchKey, a));
152 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount());
153 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceBytes());
154 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
155
156 // Add a second with the same key.
157 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, b));
158 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount());
159 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
160 cache->getCachedResourceBytes());
161 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
162 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
163
164 // Our refs mean that the resources are non purgable.
165 cache->purgeAllUnlocked();
166 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
167 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount());
168
169 // Unref but don't purge
170 a->unref();
171 b->unref();
172 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
173 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
174
175 // Purge again. This time resources should be purgable.
176 cache->purgeAllUnlocked();
177 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
178 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount());
179 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));)
180}
181
182static void test_duplicate_content_key(skiatest::Reporter* reporter) {
183 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
184 REPORTER_ASSERT(reporter, SkToBool(context));
185 if (NULL == context) {
186 return;
187 }
bsalomon33435572014-11-05 14:47:41 -0800188 context->setResourceCacheLimits(5, 30000);
189 GrResourceCache* cache = context->getResourceCache();
190 cache->purgeAllUnlocked();
191 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000192
bsalomon8b79d232014-11-10 10:19:06 -0800193 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
194 GrCacheID::Key keyData;
195 memset(&keyData, 0, sizeof(keyData));
196 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
197 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
198
199
200 // Create two resources that we will attempt to register with the same content key.
bsalomonc44be0e2014-07-25 07:32:33 -0700201 TestResource* a = new TestResource(context->getGpu());
202 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800203 a->setSize(11);
204 b->setSize(12);
205 REPORTER_ASSERT(reporter, cache->addResource(key, a));
206 // Can't add the same or another resource with the same key.
207 REPORTER_ASSERT(reporter, !cache->addResource(key, a));
208 REPORTER_ASSERT(reporter, !cache->addResource(key, b));
209 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount());
210 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceBytes());
211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
212
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000213 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800214 cache->purgeAllUnlocked();
215 a->setSize(10);
216 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount());
217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000218
bsalomon8b79d232014-11-10 10:19:06 -0800219 a->unref();
220 cache->purgeAllUnlocked();
221 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount());
222 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800223 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000224}
225
bsalomon8b79d232014-11-10 10:19:06 -0800226static void test_purge_invalidated(skiatest::Reporter* reporter) {
227 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
228 REPORTER_ASSERT(reporter, SkToBool(context));
229 if (NULL == context) {
230 return;
231 }
232
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000233 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
234 GrCacheID::Key keyData;
bsalomon8b79d232014-11-10 10:19:06 -0800235 memset(&keyData, 0, sizeof(keyData));
236
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000237 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
238
bsalomon8b79d232014-11-10 10:19:06 -0800239 keyData.fData64[0] = 1;
240 GrResourceKey key1(GrCacheID(domain, keyData), t, 0);
241 keyData.fData64[0] = 2;
242 GrResourceKey key2(GrCacheID(domain, keyData), t, 0);
243 keyData.fData64[0] = 3;
244 GrResourceKey key3(GrCacheID(domain, keyData), t, 0);
245
246 context->setResourceCacheLimits(5, 30000);
247 GrResourceCache* cache = context->getResourceCache();
248 GrResourceCache2* cache2 = context->getResourceCache2();
249 cache->purgeAllUnlocked();
250 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
251
252 // Add three resources to the cache.
253 TestResource* a = new TestResource(context->getGpu());
254 TestResource* b = new TestResource(context->getGpu());
255 TestResource* c = new TestResource(context->getGpu());
256 cache->addResource(key1, a);
257 cache->addResource(key2, b);
258 cache->addResource(key3, c);
259 a->unref();
260 b->unref();
261 c->unref();
262
263 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1));
264 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2));
265 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3));
266
267 // Invalidate two of the three, they should be purged and destroyed.
268 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
269 const GrResourceInvalidatedMessage msg1 = { key1 };
270 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1);
271 const GrResourceInvalidatedMessage msg2 = { key2 };
272 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2);
273 cache->purgeAsNeeded();
274 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
275 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
276 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2));
277 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3));
278
279 // Invalidate the third.
280 const GrResourceInvalidatedMessage msg3 = { key3 };
281 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3);
282 cache->purgeAsNeeded();
283 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
284 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3));
285}
286
287static void test_cache_delete_on_destruction(skiatest::Reporter* reporter) {
288 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
289 REPORTER_ASSERT(reporter, SkToBool(context));
290 if (NULL == context) {
291 return;
292 }
293
294 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
295 GrCacheID::Key keyData;
296 memset(&keyData, 0, sizeof(keyData));
297 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
298
299 keyData.fData64[0] = 1;
300 GrResourceKey key1(GrCacheID(domain, keyData), t, 0);
301
302 keyData.fData64[0] = 2;
303 GrResourceKey key2(GrCacheID(domain, keyData), t, 0);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000304
305 {
bsalomon33435572014-11-05 14:47:41 -0800306 context->setResourceCacheLimits(3, 30000);
307 GrResourceCache* cache = context->getResourceCache();
308 cache->purgeAllUnlocked();
309 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000310
bsalomon820dd6c2014-11-05 12:09:45 -0800311 TestResource* a = new TestResource(context->getGpu());
312 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800313 cache->addResource(key1, a);
314 cache->addResource(key2, b);
bsalomon820dd6c2014-11-05 12:09:45 -0800315
bsalomon33435572014-11-05 14:47:41 -0800316 a->setDeleteWhenDestroyed(cache, b);
317 b->setDeleteWhenDestroyed(cache, a);
318
319 a->unref();
320 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800321
bsalomon33435572014-11-05 14:47:41 -0800322 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800323
bsalomon33435572014-11-05 14:47:41 -0800324 cache->purgeAllUnlocked();
325 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
326 }
327 {
328 context->setResourceCacheLimits(3, 30000);
329 GrResourceCache* cache = context->getResourceCache();
330 cache->purgeAllUnlocked();
331 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800332
bsalomon33435572014-11-05 14:47:41 -0800333 TestResource* a = new TestResource(context->getGpu());
334 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800335 cache->addResource(key1, a);
336 cache->addResource(key2, b);
bsalomon33435572014-11-05 14:47:41 -0800337
338 a->setDeleteWhenDestroyed(cache, b);
339 b->setDeleteWhenDestroyed(cache, a);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000340
341 a->unref();
342 b->unref();
343
bsalomon33435572014-11-05 14:47:41 -0800344 cache->deleteResource(a->getCacheEntry());
bsalomon33435572014-11-05 14:47:41 -0800345 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000346 }
347}
348
bsalomon8b79d232014-11-10 10:19:06 -0800349static void test_resource_size_changed(skiatest::Reporter* reporter) {
350 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
351 REPORTER_ASSERT(reporter, SkToBool(context));
352 if (NULL == context) {
353 return;
354 }
355
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000356 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
357 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
358
359 GrCacheID::Key key1Data;
360 key1Data.fData64[0] = 0;
361 key1Data.fData64[1] = 0;
362 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
363
364 GrCacheID::Key key2Data;
365 key2Data.fData64[0] = 1;
366 key2Data.fData64[1] = 0;
367 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
368
369 // Test changing resources sizes (both increase & decrease).
370 {
bsalomon33435572014-11-05 14:47:41 -0800371 context->setResourceCacheLimits(3, 30000);
372 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800373 GrResourceCache2* cache2 = context->getResourceCache2();
bsalomon33435572014-11-05 14:47:41 -0800374 cache->purgeAllUnlocked();
375 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000376
bsalomonc44be0e2014-07-25 07:32:33 -0700377 TestResource* a = new TestResource(context->getGpu());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000378 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
bsalomon33435572014-11-05 14:47:41 -0800379 cache->addResource(key1, a);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000380 a->unref();
381
bsalomonc44be0e2014-07-25 07:32:33 -0700382 TestResource* b = new TestResource(context->getGpu());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000383 b->setSize(100);
bsalomon33435572014-11-05 14:47:41 -0800384 cache->addResource(key2, b);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000385 b->unref();
386
bsalomon33435572014-11-05 14:47:41 -0800387 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes());
388 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800389 {
390 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2)));
391 find2->setSize(200);
392 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->findAndRefContentResource(key1)));
393 find1->setSize(50);
394 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000395
bsalomon33435572014-11-05 14:47:41 -0800396 REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes());
397 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000398 }
399
400 // Test increasing a resources size beyond the cache budget.
401 {
bsalomon33435572014-11-05 14:47:41 -0800402 context->setResourceCacheLimits(2, 300);
403 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800404 GrResourceCache2* cache2 = context->getResourceCache2();
bsalomon33435572014-11-05 14:47:41 -0800405 cache->purgeAllUnlocked();
406 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000407
bsalomon8b79d232014-11-10 10:19:06 -0800408 TestResource* a = new TestResource(context->getGpu());
409 a->setSize(100);
bsalomon33435572014-11-05 14:47:41 -0800410 cache->addResource(key1, a);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000411 a->unref();
412
bsalomon8b79d232014-11-10 10:19:06 -0800413 TestResource* b = new TestResource(context->getGpu());
414 b->setSize(100);
bsalomon33435572014-11-05 14:47:41 -0800415 cache->addResource(key2, b);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000416 b->unref();
417
bsalomon33435572014-11-05 14:47:41 -0800418 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes());
419 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000420
bsalomon8b79d232014-11-10 10:19:06 -0800421 {
422 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2)));
423 find2->setSize(201);
424 }
425 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000426
bsalomon33435572014-11-05 14:47:41 -0800427 REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes());
428 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000429 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000430}
431
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000432////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000433DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000434 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
435 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
436 if (!GrContextFactory::IsRenderingGLContext(glType)) {
437 continue;
438 }
439 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -0800440 if (NULL == context) {
441 continue;
442 }
bsalomonf2703d82014-10-28 14:33:06 -0700443 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000444 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -0700445 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000446 desc.fWidth = gWidth;
447 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -0700448 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reed4a8126e2014-09-22 07:29:03 -0700449 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info));
reed69f6f002014-09-18 06:09:44 -0700450 test_cache(reporter, context, surface->getCanvas());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000451 }
bsalomon33435572014-11-05 14:47:41 -0800452
bsalomon8b79d232014-11-10 10:19:06 -0800453 // The below tests create their own mock contexts.
454 test_duplicate_content_key(reporter);
455 test_duplicate_scratch_key(reporter);
456 test_purge_invalidated(reporter);
457 test_cache_delete_on_destruction(reporter);
458 test_resource_size_changed(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000459}
460
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000461#endif