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" |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 13 | #include "GrResourceCache2.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 15 | #include "SkGr.h" |
| 16 | #include "SkMessageBus.h" |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 18 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 19 | |
| 20 | static const int gWidth = 640; |
| 21 | static const int gHeight = 480; |
| 22 | |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 24 | 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] | 25 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 26 | |
| 27 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 28 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 29 | src.eraseColor(SK_ColorBLACK); |
| 30 | size_t srcSize = src.getSize(); |
| 31 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 32 | size_t initialCacheSize; |
| 33 | context->getResourceCacheUsage(NULL, &initialCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 34 | |
| 35 | int oldMaxNum; |
| 36 | size_t oldMaxBytes; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 37 | context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 39 | // Set the cache limits so we can fit 10 "src" images and the |
| 40 | // max number of textures doesn't matter |
| 41 | size_t maxCacheSize = initialCacheSize + 10*srcSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 42 | context->setResourceCacheLimits(1000, maxCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 43 | |
| 44 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 45 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 46 | |
| 47 | for (int i = 0; i < 100; ++i) { |
| 48 | canvas->drawBitmap(src, 0, 0); |
| 49 | canvas->readPixels(size, &readback); |
| 50 | |
| 51 | // "modify" the src texture |
| 52 | src.notifyPixelsChanged(); |
| 53 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 54 | size_t curCacheSize; |
| 55 | context->getResourceCacheUsage(NULL, &curCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 56 | |
| 57 | // we should never go over the size limit |
| 58 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 59 | } |
| 60 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 61 | context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 62 | } |
| 63 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 64 | class TestResource : public GrGpuResource { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 65 | static const size_t kDefaultSize = 100; |
| 66 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 67 | public: |
| 68 | SK_DECLARE_INST_COUNT(TestResource); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 69 | TestResource(GrGpu* gpu, bool isWrapped) |
| 70 | : INHERITED(gpu, isWrapped) |
| 71 | , fToDelete(NULL) |
| 72 | , fSize(kDefaultSize) { |
| 73 | ++fNumAlive; |
| 74 | this->registerWithCache(); |
| 75 | } |
| 76 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 77 | TestResource(GrGpu* gpu) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 78 | : INHERITED(gpu, false) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 79 | , fToDelete(NULL) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 80 | , fSize(kDefaultSize) { |
| 81 | ++fNumAlive; |
| 82 | this->registerWithCache(); |
| 83 | } |
| 84 | |
| 85 | TestResource(GrGpu* gpu, const GrResourceKey& scratchKey) |
| 86 | : INHERITED(gpu, false) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 87 | , fToDelete(NULL) |
| 88 | , fSize(kDefaultSize) { |
| 89 | this->setScratchKey(scratchKey); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 90 | ++fNumAlive; |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 91 | this->registerWithCache(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ~TestResource() { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 95 | --fNumAlive; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 96 | SkSafeUnref(fToDelete); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 97 | } |
| 98 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 99 | void setSize(size_t size) { |
| 100 | fSize = size; |
| 101 | this->didChangeGpuMemorySize(); |
| 102 | } |
| 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 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 106 | void setUnrefWhenDestroyed(TestResource* resource) { |
| 107 | SkRefCnt_SafeAssign(fToDelete, resource); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private: |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 111 | size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } |
| 112 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 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 | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 121 | static void test_no_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(10, 30000); |
| 128 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 129 | cache2->purgeAllUnlocked(); |
| 130 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 131 | |
| 132 | // Create a bunch of resources with no keys |
| 133 | TestResource* a = new TestResource(context->getGpu()); |
| 134 | TestResource* b = new TestResource(context->getGpu()); |
| 135 | TestResource* c = new TestResource(context->getGpu()); |
| 136 | TestResource* d = new TestResource(context->getGpu()); |
| 137 | a->setSize(11); |
| 138 | b->setSize(12); |
| 139 | c->setSize(13); |
| 140 | d->setSize(14); |
| 141 | |
| 142 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 143 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
| 144 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
| 145 | d->gpuMemorySize() == cache2->getResourceBytes()); |
| 146 | |
| 147 | // Should be safe to purge without deleting the resources since we still have refs. |
| 148 | cache2->purgeAllUnlocked(); |
| 149 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 150 | |
| 151 | // Since the resources have neither content nor scratch keys, delete immediately upon unref. |
| 152 | |
| 153 | a->unref(); |
| 154 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
| 155 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 156 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() == |
| 157 | cache2->getResourceBytes()); |
| 158 | |
| 159 | c->unref(); |
| 160 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 161 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 162 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
| 163 | cache2->getResourceBytes()); |
| 164 | |
| 165 | d->unref(); |
| 166 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 167 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 168 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); |
| 169 | |
| 170 | b->unref(); |
| 171 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 172 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 173 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 174 | } |
| 175 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 176 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 177 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 178 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 179 | if (NULL == context) { |
| 180 | return; |
| 181 | } |
| 182 | context->setResourceCacheLimits(10, 300); |
| 183 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 184 | cache2->purgeAllUnlocked(); |
| 185 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 186 | SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes()); |
| 187 | |
| 188 | GrCacheID::Key keyData; |
| 189 | memset(&keyData, 0, sizeof(keyData)); |
| 190 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 191 | GrResourceKey scratchKey(GrCacheID(GrResourceKey::ScratchDomain(), keyData), t, 0); |
| 192 | GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), t, 0); |
| 193 | |
| 194 | // Create a scratch, a content, and a wrapped resource |
| 195 | TestResource* scratch = new TestResource(context->getGpu(), scratchKey); |
| 196 | scratch->setSize(10); |
| 197 | TestResource* content = new TestResource(context->getGpu()); |
| 198 | scratch->setSize(11); |
| 199 | REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
| 200 | TestResource* wrapped = new TestResource(context->getGpu(), true); |
| 201 | scratch->setSize(12); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 202 | TestResource* unbudgeted = new TestResource(context->getGpu()); |
| 203 | unbudgeted->setSize(13); |
| 204 | unbudgeted->cacheAccess().setBudgeted(false); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 205 | |
| 206 | // Make sure we can't add a content key to the wrapped resource |
| 207 | keyData.fData8[0] = 1; |
| 208 | GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), t, 0); |
| 209 | REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)); |
| 210 | REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentKey2)); |
| 211 | |
| 212 | // Make sure sizes are as we expect |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 213 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 215 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 216 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 218 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 219 | cache2->getBudgetedResourceBytes()); |
| 220 | |
| 221 | // Our refs mean that the resources are non purgable. |
| 222 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 224 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 225 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 226 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 227 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 228 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 229 | cache2->getBudgetedResourceBytes()); |
| 230 | |
| 231 | // Unreffing the wrapped resource should free it right away. |
| 232 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 234 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
| 235 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 236 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 237 | // Now try freeing the budgeted resources first |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 238 | wrapped = new TestResource(context->getGpu(), true); |
| 239 | scratch->setSize(12); |
| 240 | content->unref(); |
| 241 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 242 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 243 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
| 244 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 245 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 246 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedResourceBytes()); |
| 247 | |
| 248 | scratch->unref(); |
| 249 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 250 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 251 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
| 252 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 253 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 254 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 255 | |
| 256 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 258 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
| 259 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 260 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 261 | |
| 262 | unbudgeted->unref(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 264 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 265 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 266 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 267 | } |
| 268 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 269 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 270 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 271 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 272 | if (NULL == context) { |
| 273 | return; |
| 274 | } |
| 275 | context->setResourceCacheLimits(5, 30000); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 276 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 277 | cache2->purgeAllUnlocked(); |
| 278 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 279 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 280 | GrCacheID::Key keyData; |
bsalomon | 48ea202 | 2014-11-12 10:28:17 -0800 | [diff] [blame] | 281 | memset(&keyData, 0, sizeof(keyData)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 282 | GrCacheID::Domain domain = GrResourceKey::ScratchDomain(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 283 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 284 | GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0); |
| 285 | |
| 286 | // Create two resources that have the same scratch key. |
| 287 | TestResource* a = new TestResource(context->getGpu(), scratchKey); |
| 288 | TestResource* b = new TestResource(context->getGpu(), scratchKey); |
| 289 | a->setSize(11); |
| 290 | b->setSize(12); |
| 291 | // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 292 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 293 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 294 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 295 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 296 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 297 | |
| 298 | // Our refs mean that the resources are non purgable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 299 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 300 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 302 | |
| 303 | // Unref but don't purge |
| 304 | a->unref(); |
| 305 | b->unref(); |
| 306 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 307 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
| 308 | |
| 309 | // Purge again. This time resources should be purgable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 310 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 313 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 314 | } |
| 315 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 316 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
| 317 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 318 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 319 | if (NULL == context) { |
| 320 | return; |
| 321 | } |
| 322 | context->setResourceCacheLimits(5, 30000); |
| 323 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 324 | cache2->purgeAllUnlocked(); |
| 325 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 326 | |
| 327 | GrCacheID::Key keyData; |
| 328 | memset(&keyData, 0, sizeof(keyData)); |
| 329 | GrCacheID::Domain domain = GrResourceKey::ScratchDomain(); |
| 330 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 331 | GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0); |
| 332 | |
| 333 | // Create two resources that have the same scratch key. |
| 334 | TestResource* a = new TestResource(context->getGpu(), scratchKey); |
| 335 | TestResource* b = new TestResource(context->getGpu(), scratchKey); |
| 336 | a->unref(); |
| 337 | b->unref(); |
| 338 | |
| 339 | // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. |
| 340 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 341 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
| 342 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 343 | |
| 344 | // Find the first resource and remove its scratch key |
| 345 | GrGpuResource* find; |
| 346 | find = cache2->findAndRefScratchResource(scratchKey); |
| 347 | find->cacheAccess().removeScratchKey(); |
| 348 | // It's still alive, but not cached by scratch key anymore |
| 349 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 350 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey(scratchKey));) |
| 351 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 352 | |
| 353 | // The cache should immediately delete it when it's unrefed since it isn't accessible. |
| 354 | find->unref(); |
| 355 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 356 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey(scratchKey));) |
| 357 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 358 | |
| 359 | // Repeat for the second resource. |
| 360 | find = cache2->findAndRefScratchResource(scratchKey); |
| 361 | find->cacheAccess().removeScratchKey(); |
| 362 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 363 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 364 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 365 | |
| 366 | // Should be able to call this multiple times with no problem. |
| 367 | find->cacheAccess().removeScratchKey(); |
| 368 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 369 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 370 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 371 | |
| 372 | find->unref(); |
| 373 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 374 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 375 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 376 | } |
| 377 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 378 | static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
| 379 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 380 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 381 | if (NULL == context) { |
| 382 | return; |
| 383 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 384 | context->setResourceCacheLimits(5, 30000); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 385 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 386 | cache2->purgeAllUnlocked(); |
| 387 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 388 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 389 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 390 | GrCacheID::Key keyData; |
| 391 | memset(&keyData, 0, sizeof(keyData)); |
| 392 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 393 | GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 394 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 395 | // 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] | 396 | TestResource* a = new TestResource(context->getGpu()); |
| 397 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 398 | a->setSize(11); |
| 399 | b->setSize(12); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 400 | |
| 401 | // Can't set the same content key on two resources. |
| 402 | REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key)); |
| 403 | REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key)); |
| 404 | |
| 405 | // Still have two resources because b is still reffed. |
| 406 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 407 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 408 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 409 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 410 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 411 | b->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 412 | // Now b should be gone. |
| 413 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 414 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 415 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 416 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 417 | cache2->purgeAllUnlocked(); |
| 418 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 419 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 420 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 421 | |
| 422 | // Drop the ref on a but it isn't immediately purged as it still has a valid scratch key. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 423 | a->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 424 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 425 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 426 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 427 | |
| 428 | cache2->purgeAllUnlocked(); |
| 429 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 430 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 431 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 432 | } |
| 433 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 434 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
| 435 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 436 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 437 | if (NULL == context) { |
| 438 | return; |
| 439 | } |
| 440 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 441 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 442 | GrCacheID::Key keyData; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 443 | memset(&keyData, 0, sizeof(keyData)); |
| 444 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 445 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 446 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 447 | keyData.fData64[0] = 1; |
| 448 | GrResourceKey key1(GrCacheID(domain, keyData), t, 0); |
| 449 | keyData.fData64[0] = 2; |
| 450 | GrResourceKey key2(GrCacheID(domain, keyData), t, 0); |
| 451 | keyData.fData64[0] = 3; |
| 452 | GrResourceKey key3(GrCacheID(domain, keyData), t, 0); |
| 453 | |
| 454 | context->setResourceCacheLimits(5, 30000); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 455 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 456 | cache2->purgeAllUnlocked(); |
| 457 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 458 | |
| 459 | // Add three resources to the cache. |
| 460 | TestResource* a = new TestResource(context->getGpu()); |
| 461 | TestResource* b = new TestResource(context->getGpu()); |
| 462 | TestResource* c = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 463 | a->cacheAccess().setContentKey(key1); |
| 464 | b->cacheAccess().setContentKey(key2); |
| 465 | c->cacheAccess().setContentKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 466 | a->unref(); |
| 467 | b->unref(); |
| 468 | c->unref(); |
| 469 | |
| 470 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 471 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
| 472 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 473 | |
| 474 | // Invalidate two of the three, they should be purged and destroyed. |
| 475 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
| 476 | const GrResourceInvalidatedMessage msg1 = { key1 }; |
| 477 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1); |
| 478 | const GrResourceInvalidatedMessage msg2 = { key2 }; |
| 479 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 480 | #if 0 // Disabled until reimplemented in GrResourceCache2. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 481 | cache2->purgeAsNeeded(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 482 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 483 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 484 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
| 485 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 486 | #endif |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 487 | |
| 488 | // Invalidate the third. |
| 489 | const GrResourceInvalidatedMessage msg3 = { key3 }; |
| 490 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 491 | #if 0 // Disabled until reimplemented in GrResourceCache2. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 492 | cache2->purgeAsNeeded(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 493 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 494 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 495 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 496 | |
| 497 | cache2->purgeAllUnlocked(); |
| 498 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 499 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 500 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 501 | } |
| 502 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 503 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 504 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 505 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 506 | if (NULL == context) { |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 511 | GrCacheID::Key keyData; |
| 512 | memset(&keyData, 0, sizeof(keyData)); |
| 513 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 514 | |
| 515 | keyData.fData64[0] = 1; |
| 516 | GrResourceKey key1(GrCacheID(domain, keyData), t, 0); |
| 517 | |
| 518 | keyData.fData64[0] = 2; |
| 519 | GrResourceKey key2(GrCacheID(domain, keyData), t, 0); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 520 | |
| 521 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 522 | context->setResourceCacheLimits(3, 30000); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 523 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 524 | cache2->purgeAllUnlocked(); |
| 525 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 526 | |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 527 | TestResource* a = new TestResource(context->getGpu()); |
| 528 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 529 | a->cacheAccess().setContentKey(key1); |
| 530 | b->cacheAccess().setContentKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 531 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 532 | // Make a cycle |
| 533 | a->setUnrefWhenDestroyed(b); |
| 534 | b->setUnrefWhenDestroyed(a); |
| 535 | |
| 536 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 537 | |
| 538 | a->unref(); |
| 539 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 540 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 541 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 542 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 543 | cache2->purgeAllUnlocked(); |
| 544 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 545 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 546 | // Break the cycle |
| 547 | a->setUnrefWhenDestroyed(NULL); |
| 548 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 549 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 550 | cache2->purgeAllUnlocked(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 551 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 555 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
| 556 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 557 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 558 | if (NULL == context) { |
| 559 | return; |
| 560 | } |
| 561 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 562 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 563 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 564 | |
| 565 | GrCacheID::Key key1Data; |
| 566 | key1Data.fData64[0] = 0; |
| 567 | key1Data.fData64[1] = 0; |
| 568 | GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); |
| 569 | |
| 570 | GrCacheID::Key key2Data; |
| 571 | key2Data.fData64[0] = 1; |
| 572 | key2Data.fData64[1] = 0; |
| 573 | GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); |
| 574 | |
| 575 | // Test changing resources sizes (both increase & decrease). |
| 576 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 577 | context->setResourceCacheLimits(3, 30000); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 578 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 579 | cache2->purgeAllUnlocked(); |
| 580 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 581 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 582 | TestResource* a = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 583 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 584 | a->unref(); |
| 585 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 586 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 587 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 588 | b->unref(); |
| 589 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 590 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 591 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 592 | { |
| 593 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 594 | find2->setSize(200); |
| 595 | SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->findAndRefContentResource(key1))); |
| 596 | find1->setSize(50); |
| 597 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 598 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 599 | REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); |
| 600 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Test increasing a resources size beyond the cache budget. |
| 604 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 605 | context->setResourceCacheLimits(2, 300); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 606 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 607 | cache2->purgeAllUnlocked(); |
| 608 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 609 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 610 | TestResource* a = new TestResource(context->getGpu()); |
| 611 | a->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 612 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 613 | a->unref(); |
| 614 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 615 | TestResource* b = new TestResource(context->getGpu()); |
| 616 | b->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 617 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 618 | b->unref(); |
| 619 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 620 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 621 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 622 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 623 | { |
| 624 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 625 | find2->setSize(201); |
| 626 | } |
| 627 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 628 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 629 | REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); |
| 630 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 631 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 632 | } |
| 633 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 634 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
| 635 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 636 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 637 | if (NULL == context) { |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | static const int kResourceCnt = 2000; |
| 642 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 643 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 644 | // down to 1 byte after creating the resource. |
| 645 | context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 646 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 647 | cache2->purgeAllUnlocked(); |
| 648 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 649 | |
| 650 | GrCacheID::Domain domain0 = GrCacheID::GenerateDomain(); |
| 651 | GrCacheID::Domain domain1 = GrCacheID::GenerateDomain(); |
| 652 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 653 | |
| 654 | GrCacheID::Key keyData; |
| 655 | memset(&keyData, 0, sizeof(keyData)); |
| 656 | |
| 657 | for (int i = 0; i < kResourceCnt; ++i) { |
| 658 | TestResource* resource; |
| 659 | keyData.fData32[0] = i; |
| 660 | |
| 661 | GrResourceKey key0(GrCacheID(domain0, keyData), t, 0); |
| 662 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 663 | resource->cacheAccess().setContentKey(key0); |
| 664 | resource->setSize(1); |
| 665 | resource->unref(); |
| 666 | |
| 667 | GrResourceKey key1(GrCacheID(domain1, keyData), t, 0); |
| 668 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 669 | resource->cacheAccess().setContentKey(key1); |
| 670 | resource->setSize(1); |
| 671 | resource->unref(); |
| 672 | } |
| 673 | |
| 674 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
| 675 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 676 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 677 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt); |
| 678 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt); |
| 679 | for (int i = 0; i < kResourceCnt; ++i) { |
| 680 | keyData.fData32[0] = i; |
| 681 | GrResourceKey key0(GrCacheID(domain0, keyData), t, 0); |
| 682 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key0)); |
| 683 | GrResourceKey key1(GrCacheID(domain0, keyData), t, 0); |
| 684 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 685 | } |
| 686 | |
| 687 | cache2->purgeAllUnlocked(); |
| 688 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
| 689 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0); |
| 690 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0); |
| 691 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0); |
| 692 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0); |
| 693 | |
| 694 | for (int i = 0; i < kResourceCnt; ++i) { |
| 695 | keyData.fData32[0] = i; |
| 696 | GrResourceKey key0(GrCacheID(domain0, keyData), t, 0); |
| 697 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key0)); |
| 698 | GrResourceKey key1(GrCacheID(domain0, keyData), t, 0); |
| 699 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 704 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 705 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 706 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 707 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 708 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 709 | continue; |
| 710 | } |
| 711 | GrContext* context = factory->get(glType); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 712 | if (NULL == context) { |
| 713 | continue; |
| 714 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 715 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 716 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 717 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 718 | desc.fWidth = gWidth; |
| 719 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 720 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 721 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 722 | test_cache(reporter, context, surface->getCanvas()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 723 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 724 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 725 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 726 | test_no_key(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 727 | test_budgeting(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 728 | test_duplicate_content_key(reporter); |
| 729 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 730 | test_remove_scratch_key(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 731 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 732 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 733 | test_resource_size_changed(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 734 | test_large_resource_count(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 735 | } |
| 736 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 737 | #endif |