commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1 | /* |
| 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.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 8 | #if SK_SUPPORT_GPU |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 9 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 11 | #include "GrContextFactory.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 12 | #include "GrGpu.h" |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 13 | #include "GrResourceCache.h" |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 14 | #include "GrResourceCache2.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 15 | #include "SkCanvas.h" |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 17 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 18 | |
| 19 | static const int gWidth = 640; |
| 20 | static const int gHeight = 480; |
| 21 | |
| 22 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 23 | static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 24 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 25 | |
| 26 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 27 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 28 | src.eraseColor(SK_ColorBLACK); |
| 29 | size_t srcSize = src.getSize(); |
| 30 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 31 | size_t initialCacheSize; |
| 32 | context->getResourceCacheUsage(NULL, &initialCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 33 | |
| 34 | int oldMaxNum; |
| 35 | size_t oldMaxBytes; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 36 | context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 38 | // 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 41 | context->setResourceCacheLimits(1000, maxCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 42 | |
| 43 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 44 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 45 | |
| 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 53 | size_t curCacheSize; |
| 54 | context->getResourceCacheUsage(NULL, &curCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 55 | |
| 56 | // we should never go over the size limit |
| 57 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 58 | } |
| 59 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 60 | context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 63 | class TestResource : public GrGpuResource { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 64 | static const size_t kDefaultSize = 100; |
| 65 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 66 | public: |
| 67 | SK_DECLARE_INST_COUNT(TestResource); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 68 | TestResource(GrGpu* gpu) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 69 | : INHERITED(gpu, false) |
| 70 | , fCache(NULL) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 71 | , fToDelete(NULL) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 72 | , 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); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 83 | ++fNumAlive; |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 84 | this->registerWithCache(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ~TestResource() { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 88 | --fNumAlive; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 89 | if (fToDelete) { |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 90 | // Breaks our little 2-element cycle below. |
| 91 | fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
| 92 | fCache->deleteResource(fToDelete->getCacheEntry()); |
| 93 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 94 | this->release(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 95 | } |
| 96 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 97 | 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.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 103 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 104 | static int NumAlive() { return fNumAlive; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 105 | |
| 106 | void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) { |
| 107 | fCache = cache; |
| 108 | fToDelete = resource; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | GrResourceCache* fCache; |
| 113 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 114 | size_t fSize; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 115 | static int fNumAlive; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 116 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 117 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 118 | }; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 119 | int TestResource::fNumAlive = 0; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 120 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 121 | static 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.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 133 | GrCacheID::Key keyData; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 134 | GrCacheID::Domain domain = GrResourceKey::ScratchDomain(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 135 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 136 | 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 | |
| 182 | static 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 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 188 | context->setResourceCacheLimits(5, 30000); |
| 189 | GrResourceCache* cache = context->getResourceCache(); |
| 190 | cache->purgeAllUnlocked(); |
| 191 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 192 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 193 | 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. |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 201 | TestResource* a = new TestResource(context->getGpu()); |
| 202 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 203 | 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.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 213 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 214 | cache->purgeAllUnlocked(); |
| 215 | a->setSize(10); |
| 216 | REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
| 217 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 218 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 219 | a->unref(); |
| 220 | cache->purgeAllUnlocked(); |
| 221 | REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount()); |
| 222 | REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 224 | } |
| 225 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 226 | static 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.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 233 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 234 | GrCacheID::Key keyData; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 235 | memset(&keyData, 0, sizeof(keyData)); |
| 236 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 237 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 238 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 239 | 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 | |
| 287 | static 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.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 304 | |
| 305 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 306 | context->setResourceCacheLimits(3, 30000); |
| 307 | GrResourceCache* cache = context->getResourceCache(); |
| 308 | cache->purgeAllUnlocked(); |
| 309 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 310 | |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 311 | TestResource* a = new TestResource(context->getGpu()); |
| 312 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 313 | cache->addResource(key1, a); |
| 314 | cache->addResource(key2, b); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 315 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 316 | a->setDeleteWhenDestroyed(cache, b); |
| 317 | b->setDeleteWhenDestroyed(cache, a); |
| 318 | |
| 319 | a->unref(); |
| 320 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 321 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 323 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 324 | 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()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 332 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 333 | TestResource* a = new TestResource(context->getGpu()); |
| 334 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 335 | cache->addResource(key1, a); |
| 336 | cache->addResource(key2, b); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 337 | |
| 338 | a->setDeleteWhenDestroyed(cache, b); |
| 339 | b->setDeleteWhenDestroyed(cache, a); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 340 | |
| 341 | a->unref(); |
| 342 | b->unref(); |
| 343 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 344 | cache->deleteResource(a->getCacheEntry()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 345 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 349 | static 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.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 356 | 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 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 371 | context->setResourceCacheLimits(3, 30000); |
| 372 | GrResourceCache* cache = context->getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 373 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 374 | cache->purgeAllUnlocked(); |
| 375 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 376 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 377 | TestResource* a = new TestResource(context->getGpu()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 378 | a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 379 | cache->addResource(key1, a); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 380 | a->unref(); |
| 381 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 382 | TestResource* b = new TestResource(context->getGpu()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 383 | b->setSize(100); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 384 | cache->addResource(key2, b); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 385 | b->unref(); |
| 386 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 387 | REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
| 388 | REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 389 | { |
| 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.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 395 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 396 | REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes()); |
| 397 | REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Test increasing a resources size beyond the cache budget. |
| 401 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 402 | context->setResourceCacheLimits(2, 300); |
| 403 | GrResourceCache* cache = context->getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 404 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 405 | cache->purgeAllUnlocked(); |
| 406 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 407 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 408 | TestResource* a = new TestResource(context->getGpu()); |
| 409 | a->setSize(100); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 410 | cache->addResource(key1, a); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 411 | a->unref(); |
| 412 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 413 | TestResource* b = new TestResource(context->getGpu()); |
| 414 | b->setSize(100); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 415 | cache->addResource(key2, b); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 416 | b->unref(); |
| 417 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 418 | REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
| 419 | REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 420 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 421 | { |
| 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.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 426 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 427 | REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes()); |
| 428 | REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 429 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 430 | } |
| 431 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 432 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 433 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 434 | 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); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 440 | if (NULL == context) { |
| 441 | continue; |
| 442 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 443 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 444 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 445 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 446 | desc.fWidth = gWidth; |
| 447 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 448 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 449 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 450 | test_cache(reporter, context, surface->getCanvas()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 451 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 452 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 453 | // 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.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 459 | } |
| 460 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 461 | #endif |