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 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 85 | TestResource(GrGpu* gpu, const GrScratchKey& scratchKey) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 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 | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 176 | static void make_scratch_key(GrScratchKey* key) { |
| 177 | static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 178 | GrScratchKey::Builder builder(key, t, 0); |
| 179 | } |
| 180 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 181 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 182 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 183 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 184 | if (NULL == context) { |
| 185 | return; |
| 186 | } |
| 187 | context->setResourceCacheLimits(10, 300); |
| 188 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 189 | cache2->purgeAllUnlocked(); |
| 190 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 191 | SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes()); |
| 192 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 193 | GrScratchKey scratchKey; |
| 194 | make_scratch_key(&scratchKey); |
| 195 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 196 | GrCacheID::Key keyData; |
| 197 | memset(&keyData, 0, sizeof(keyData)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 198 | GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 199 | |
| 200 | // Create a scratch, a content, and a wrapped resource |
| 201 | TestResource* scratch = new TestResource(context->getGpu(), scratchKey); |
| 202 | scratch->setSize(10); |
| 203 | TestResource* content = new TestResource(context->getGpu()); |
| 204 | scratch->setSize(11); |
| 205 | REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
| 206 | TestResource* wrapped = new TestResource(context->getGpu(), true); |
| 207 | scratch->setSize(12); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 208 | TestResource* unbudgeted = new TestResource(context->getGpu()); |
| 209 | unbudgeted->setSize(13); |
| 210 | unbudgeted->cacheAccess().setBudgeted(false); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 211 | |
| 212 | // Make sure we can't add a content key to the wrapped resource |
| 213 | keyData.fData8[0] = 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 214 | GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 215 | REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)); |
| 216 | REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentKey2)); |
| 217 | |
| 218 | // Make sure sizes are as we expect |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 219 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 220 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 221 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 222 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 224 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 225 | cache2->getBudgetedResourceBytes()); |
| 226 | |
| 227 | // Our refs mean that the resources are non purgable. |
| 228 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 229 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 231 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 232 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 234 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 235 | cache2->getBudgetedResourceBytes()); |
| 236 | |
| 237 | // Unreffing the wrapped resource should free it right away. |
| 238 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 239 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 240 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
| 241 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 242 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 243 | // Now try freeing the budgeted resources first |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 244 | wrapped = new TestResource(context->getGpu(), true); |
| 245 | scratch->setSize(12); |
| 246 | content->unref(); |
| 247 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 249 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
| 250 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 251 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 252 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedResourceBytes()); |
| 253 | |
| 254 | scratch->unref(); |
| 255 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 256 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 257 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
| 258 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 259 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 260 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 261 | |
| 262 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 264 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
| 265 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 266 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 267 | |
| 268 | unbudgeted->unref(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 269 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 270 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 272 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 273 | } |
| 274 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 275 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 276 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 277 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 278 | if (NULL == context) { |
| 279 | return; |
| 280 | } |
| 281 | context->setResourceCacheLimits(5, 30000); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 282 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 283 | cache2->purgeAllUnlocked(); |
| 284 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 285 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 286 | GrScratchKey scratchKey; |
| 287 | make_scratch_key(&scratchKey); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 288 | |
| 289 | // Create two resources that have the same scratch key. |
| 290 | TestResource* a = new TestResource(context->getGpu(), scratchKey); |
| 291 | TestResource* b = new TestResource(context->getGpu(), scratchKey); |
| 292 | a->setSize(11); |
| 293 | b->setSize(12); |
| 294 | // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 296 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 298 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 299 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 300 | |
| 301 | // Our refs mean that the resources are non purgable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 302 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 303 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 305 | |
| 306 | // Unref but don't purge |
| 307 | a->unref(); |
| 308 | b->unref(); |
| 309 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 310 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
| 311 | |
| 312 | // Purge again. This time resources should be purgable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 313 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 314 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 316 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 317 | } |
| 318 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 319 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
| 320 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 321 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 322 | if (NULL == context) { |
| 323 | return; |
| 324 | } |
| 325 | context->setResourceCacheLimits(5, 30000); |
| 326 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 327 | cache2->purgeAllUnlocked(); |
| 328 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 329 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 330 | GrScratchKey scratchKey; |
| 331 | make_scratch_key(&scratchKey); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 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)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 392 | GrResourceKey key(GrCacheID(domain, keyData), 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 393 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 394 | // 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] | 395 | TestResource* a = new TestResource(context->getGpu()); |
| 396 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 397 | a->setSize(11); |
| 398 | b->setSize(12); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 399 | |
| 400 | // Can't set the same content key on two resources. |
| 401 | REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key)); |
| 402 | REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key)); |
| 403 | |
| 404 | // Still have two resources because b is still reffed. |
| 405 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 406 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 407 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 408 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 409 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 410 | b->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 411 | // Now b should be gone. |
| 412 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 413 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 414 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 415 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 416 | cache2->purgeAllUnlocked(); |
| 417 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 418 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 419 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 420 | |
| 421 | // 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] | 422 | a->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 423 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 424 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 425 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 426 | |
| 427 | cache2->purgeAllUnlocked(); |
| 428 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 429 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 430 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 431 | } |
| 432 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 433 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
| 434 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 435 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 436 | if (NULL == context) { |
| 437 | return; |
| 438 | } |
| 439 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 440 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 441 | GrCacheID::Key keyData; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 442 | memset(&keyData, 0, sizeof(keyData)); |
| 443 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 444 | keyData.fData64[0] = 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 445 | GrResourceKey key1(GrCacheID(domain, keyData), 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 446 | keyData.fData64[0] = 2; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 447 | GrResourceKey key2(GrCacheID(domain, keyData), 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 448 | keyData.fData64[0] = 3; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 449 | GrResourceKey key3(GrCacheID(domain, keyData), 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 450 | |
| 451 | context->setResourceCacheLimits(5, 30000); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 452 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 453 | cache2->purgeAllUnlocked(); |
| 454 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 455 | |
| 456 | // Add three resources to the cache. |
| 457 | TestResource* a = new TestResource(context->getGpu()); |
| 458 | TestResource* b = new TestResource(context->getGpu()); |
| 459 | TestResource* c = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 460 | a->cacheAccess().setContentKey(key1); |
| 461 | b->cacheAccess().setContentKey(key2); |
| 462 | c->cacheAccess().setContentKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 463 | a->unref(); |
| 464 | b->unref(); |
| 465 | c->unref(); |
| 466 | |
| 467 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 468 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
| 469 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 470 | |
| 471 | // Invalidate two of the three, they should be purged and destroyed. |
| 472 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
| 473 | const GrResourceInvalidatedMessage msg1 = { key1 }; |
| 474 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1); |
| 475 | const GrResourceInvalidatedMessage msg2 = { key2 }; |
| 476 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 477 | #if 0 // Disabled until reimplemented in GrResourceCache2. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 478 | cache2->purgeAsNeeded(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 479 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 480 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 481 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
| 482 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 483 | #endif |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 484 | |
| 485 | // Invalidate the third. |
| 486 | const GrResourceInvalidatedMessage msg3 = { key3 }; |
| 487 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 488 | #if 0 // Disabled until reimplemented in GrResourceCache2. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 489 | cache2->purgeAsNeeded(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 490 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 491 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 492 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 493 | |
| 494 | cache2->purgeAllUnlocked(); |
| 495 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 496 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 497 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 498 | } |
| 499 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 500 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 501 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 502 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 503 | if (NULL == context) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 508 | GrCacheID::Key keyData; |
| 509 | memset(&keyData, 0, sizeof(keyData)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 510 | |
| 511 | keyData.fData64[0] = 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 512 | GrResourceKey key1(GrCacheID(domain, keyData), 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 513 | |
| 514 | keyData.fData64[0] = 2; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 515 | GrResourceKey key2(GrCacheID(domain, keyData), 0); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 516 | |
| 517 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 518 | context->setResourceCacheLimits(3, 30000); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 519 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 520 | cache2->purgeAllUnlocked(); |
| 521 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 522 | |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 523 | TestResource* a = new TestResource(context->getGpu()); |
| 524 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 525 | a->cacheAccess().setContentKey(key1); |
| 526 | b->cacheAccess().setContentKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 527 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 528 | // Make a cycle |
| 529 | a->setUnrefWhenDestroyed(b); |
| 530 | b->setUnrefWhenDestroyed(a); |
| 531 | |
| 532 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 533 | |
| 534 | a->unref(); |
| 535 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 536 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 537 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 538 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 539 | cache2->purgeAllUnlocked(); |
| 540 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 541 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 542 | // Break the cycle |
| 543 | a->setUnrefWhenDestroyed(NULL); |
| 544 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 545 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 546 | cache2->purgeAllUnlocked(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 547 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 551 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
| 552 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 553 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 554 | if (NULL == context) { |
| 555 | return; |
| 556 | } |
| 557 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 558 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 559 | |
| 560 | GrCacheID::Key key1Data; |
| 561 | key1Data.fData64[0] = 0; |
| 562 | key1Data.fData64[1] = 0; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 563 | GrResourceKey key1(GrCacheID(domain, key1Data), 0); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 564 | |
| 565 | GrCacheID::Key key2Data; |
| 566 | key2Data.fData64[0] = 1; |
| 567 | key2Data.fData64[1] = 0; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 568 | GrResourceKey key2(GrCacheID(domain, key2Data), 0); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 569 | |
| 570 | // Test changing resources sizes (both increase & decrease). |
| 571 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 572 | context->setResourceCacheLimits(3, 30000); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 573 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 574 | cache2->purgeAllUnlocked(); |
| 575 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 576 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 577 | TestResource* a = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 578 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 579 | a->unref(); |
| 580 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 581 | TestResource* b = new TestResource(context->getGpu()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 582 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 583 | b->unref(); |
| 584 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 585 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 586 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 587 | { |
| 588 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 589 | find2->setSize(200); |
| 590 | SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->findAndRefContentResource(key1))); |
| 591 | find1->setSize(50); |
| 592 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 593 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 594 | REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); |
| 595 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // Test increasing a resources size beyond the cache budget. |
| 599 | { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 600 | context->setResourceCacheLimits(2, 300); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 601 | GrResourceCache2* cache2 = context->getResourceCache2(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 602 | cache2->purgeAllUnlocked(); |
| 603 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 604 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 605 | TestResource* a = new TestResource(context->getGpu()); |
| 606 | a->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 607 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 608 | a->unref(); |
| 609 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 610 | TestResource* b = new TestResource(context->getGpu()); |
| 611 | b->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 612 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 613 | b->unref(); |
| 614 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 615 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 616 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 617 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 618 | { |
| 619 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 620 | find2->setSize(201); |
| 621 | } |
| 622 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 623 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 624 | REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); |
| 625 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 626 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 627 | } |
| 628 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 629 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
| 630 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 631 | REPORTER_ASSERT(reporter, SkToBool(context)); |
| 632 | if (NULL == context) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | static const int kResourceCnt = 2000; |
| 637 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 638 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 639 | // down to 1 byte after creating the resource. |
| 640 | context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 641 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 642 | cache2->purgeAllUnlocked(); |
| 643 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
| 644 | |
| 645 | GrCacheID::Domain domain0 = GrCacheID::GenerateDomain(); |
| 646 | GrCacheID::Domain domain1 = GrCacheID::GenerateDomain(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 647 | |
| 648 | GrCacheID::Key keyData; |
| 649 | memset(&keyData, 0, sizeof(keyData)); |
| 650 | |
| 651 | for (int i = 0; i < kResourceCnt; ++i) { |
| 652 | TestResource* resource; |
| 653 | keyData.fData32[0] = i; |
| 654 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 655 | GrResourceKey key0(GrCacheID(domain0, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 656 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 657 | resource->cacheAccess().setContentKey(key0); |
| 658 | resource->setSize(1); |
| 659 | resource->unref(); |
| 660 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 661 | GrResourceKey key1(GrCacheID(domain1, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 662 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 663 | resource->cacheAccess().setContentKey(key1); |
| 664 | resource->setSize(1); |
| 665 | resource->unref(); |
| 666 | } |
| 667 | |
| 668 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
| 669 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 670 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 671 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt); |
| 672 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt); |
| 673 | for (int i = 0; i < kResourceCnt; ++i) { |
| 674 | keyData.fData32[0] = i; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 675 | GrResourceKey key0(GrCacheID(domain0, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 676 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key0)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 677 | GrResourceKey key1(GrCacheID(domain0, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 679 | } |
| 680 | |
| 681 | cache2->purgeAllUnlocked(); |
| 682 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
| 683 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0); |
| 684 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0); |
| 685 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0); |
| 686 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0); |
| 687 | |
| 688 | for (int i = 0; i < kResourceCnt; ++i) { |
| 689 | keyData.fData32[0] = i; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 690 | GrResourceKey key0(GrCacheID(domain0, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 691 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key0)); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 692 | GrResourceKey key1(GrCacheID(domain0, keyData), 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 693 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 698 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 699 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 700 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 701 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 702 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 703 | continue; |
| 704 | } |
| 705 | GrContext* context = factory->get(glType); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 706 | if (NULL == context) { |
| 707 | continue; |
| 708 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 709 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 710 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 711 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 712 | desc.fWidth = gWidth; |
| 713 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 714 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 715 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 716 | test_cache(reporter, context, surface->getCanvas()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 717 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 718 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 719 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 720 | test_no_key(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 721 | test_budgeting(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 722 | test_duplicate_content_key(reporter); |
| 723 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 724 | test_remove_scratch_key(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 725 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 726 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 727 | test_resource_size_changed(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 728 | test_large_resource_count(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 729 | } |
| 730 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 731 | #endif |