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; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 66 | enum ScratchConstructor { kScratchConstructor }; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 67 | public: |
| 68 | SK_DECLARE_INST_COUNT(TestResource); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 69 | /** Property that distinctly categorizes the resource. |
| 70 | * For example, textures have width, height, ... */ |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 71 | enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 72 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 73 | TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
| 74 | : INHERITED(gpu, lifeCycle) |
| 75 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 76 | , fSize(size) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 77 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 78 | ++fNumAlive; |
| 79 | this->registerWithCache(); |
| 80 | } |
| 81 | |
| 82 | TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
| 83 | : INHERITED(gpu, lifeCycle) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 84 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 85 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 86 | , fProperty(kA_SimulatedProperty) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 87 | ++fNumAlive; |
| 88 | this->registerWithCache(); |
| 89 | } |
| 90 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 91 | TestResource(GrGpu* gpu) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 92 | : INHERITED(gpu, kCached_LifeCycle) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 93 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 94 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 95 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 96 | ++fNumAlive; |
| 97 | this->registerWithCache(); |
| 98 | } |
| 99 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 100 | static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 101 | return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor)); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | ~TestResource() { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 105 | --fNumAlive; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 106 | SkSafeUnref(fToDelete); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 107 | } |
| 108 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 109 | void setSize(size_t size) { |
| 110 | fSize = size; |
| 111 | this->didChangeGpuMemorySize(); |
| 112 | } |
| 113 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 114 | static int NumAlive() { return fNumAlive; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 115 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 116 | void setUnrefWhenDestroyed(TestResource* resource) { |
| 117 | SkRefCnt_SafeAssign(fToDelete, resource); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 118 | } |
| 119 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 120 | static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) { |
| 121 | static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 122 | GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 123 | for (int i = 0; i < kScratchKeyFieldCnt; ++i) { |
| 124 | builder[i] = static_cast<uint32_t>(i + property); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | static size_t ExpectedScratchKeySize() { |
| 129 | return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt); |
| 130 | } |
| 131 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 132 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 133 | static const int kScratchKeyFieldCnt = 6; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 134 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 135 | TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor) |
| 136 | : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 137 | , fToDelete(NULL) |
| 138 | , fSize(kDefaultSize) |
| 139 | , fProperty(property) { |
| 140 | GrScratchKey scratchKey; |
| 141 | ComputeScratchKey(fProperty, &scratchKey); |
| 142 | this->setScratchKey(scratchKey); |
| 143 | ++fNumAlive; |
| 144 | this->registerWithCache(); |
| 145 | } |
| 146 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 147 | size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } |
| 148 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 149 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 150 | size_t fSize; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 151 | static int fNumAlive; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 152 | SimulatedProperty fProperty; |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 153 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 154 | }; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 155 | int TestResource::fNumAlive = 0; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 156 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 157 | class Mock { |
| 158 | public: |
| 159 | Mock(int maxCnt, size_t maxBytes) { |
| 160 | fContext.reset(GrContext::CreateMockContext()); |
| 161 | SkASSERT(fContext); |
| 162 | fContext->setResourceCacheLimits(maxCnt, maxBytes); |
| 163 | GrResourceCache2* cache2 = fContext->getResourceCache2(); |
| 164 | cache2->purgeAllUnlocked(); |
| 165 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 166 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 167 | |
| 168 | GrResourceCache2* cache() { return fContext->getResourceCache2(); } |
| 169 | |
| 170 | GrContext* context() { return fContext; } |
| 171 | |
| 172 | private: |
| 173 | SkAutoTUnref<GrContext> fContext; |
| 174 | }; |
| 175 | |
| 176 | static void test_no_key(skiatest::Reporter* reporter) { |
| 177 | Mock mock(10, 30000); |
| 178 | GrContext* context = mock.context(); |
| 179 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 180 | |
| 181 | // Create a bunch of resources with no keys |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 182 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 183 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 184 | TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 185 | TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 186 | a->setSize(11); |
| 187 | b->setSize(12); |
| 188 | c->setSize(13); |
| 189 | d->setSize(14); |
| 190 | |
| 191 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 192 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
| 193 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
| 194 | d->gpuMemorySize() == cache2->getResourceBytes()); |
| 195 | |
| 196 | // Should be safe to purge without deleting the resources since we still have refs. |
| 197 | cache2->purgeAllUnlocked(); |
| 198 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 199 | |
| 200 | // Since the resources have neither content nor scratch keys, delete immediately upon unref. |
| 201 | |
| 202 | a->unref(); |
| 203 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
| 204 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 205 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() == |
| 206 | cache2->getResourceBytes()); |
| 207 | |
| 208 | c->unref(); |
| 209 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 210 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 211 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
| 212 | cache2->getResourceBytes()); |
| 213 | |
| 214 | d->unref(); |
| 215 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 216 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 217 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); |
| 218 | |
| 219 | b->unref(); |
| 220 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 221 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 222 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 223 | } |
| 224 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 225 | // Each integer passed as a template param creates a new domain. |
| 226 | template <int> static void make_content_key(GrContentKey* key, int data) { |
| 227 | static GrContentKey::Domain d = GrContentKey::GenerateDomain(); |
| 228 | GrContentKey::Builder builder(key, d, 1); |
| 229 | builder[0] = data; |
| 230 | } |
| 231 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 232 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 233 | Mock mock(10, 300); |
| 234 | GrContext* context = mock.context(); |
| 235 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 236 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 237 | GrContentKey contentKey; |
| 238 | make_content_key<0>(&contentKey, 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 239 | |
| 240 | // Create a scratch, a content, and a wrapped resource |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 241 | TestResource* scratch = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 242 | TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 243 | scratch->setSize(10); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 244 | TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 245 | content->setSize(11); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 246 | REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 247 | TestResource* wrapped = SkNEW_ARGS(TestResource, |
| 248 | (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
| 249 | wrapped->setSize(12); |
| 250 | TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
| 251 | (context->getGpu(), GrGpuResource::kUncached_LifeCycle)); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 252 | unbudgeted->setSize(13); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 253 | |
| 254 | // Make sure we can't add a content key to the wrapped resource |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 255 | GrContentKey contentKey2; |
| 256 | make_content_key<0>(&contentKey2, 1); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)); |
| 258 | REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentKey2)); |
| 259 | |
| 260 | // Make sure sizes are as we expect |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 261 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 263 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 264 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 265 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 266 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 267 | cache2->getBudgetedResourceBytes()); |
| 268 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 269 | // Our refs mean that the resources are non purgeable. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 270 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 272 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 273 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
| 274 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 275 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 276 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
| 277 | cache2->getBudgetedResourceBytes()); |
| 278 | |
| 279 | // Unreffing the wrapped resource should free it right away. |
| 280 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 281 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 282 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
| 283 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 284 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 285 | // Now try freeing the budgeted resources first |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 286 | wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 287 | scratch->setSize(12); |
| 288 | content->unref(); |
| 289 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 290 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 291 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
| 292 | unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 293 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 294 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedResourceBytes()); |
| 295 | |
| 296 | scratch->unref(); |
| 297 | cache2->purgeAllUnlocked(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 298 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 299 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
| 300 | cache2->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 302 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 303 | |
| 304 | wrapped->unref(); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 306 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResourceBytes()); |
| 307 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 308 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 309 | |
| 310 | unbudgeted->unref(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 312 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 314 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 315 | } |
| 316 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 317 | static void test_unbudgeted(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 318 | Mock mock(10, 30000); |
| 319 | GrContext* context = mock.context(); |
| 320 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 321 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 322 | GrContentKey contentKey; |
| 323 | make_content_key<0>(&contentKey, 0); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 324 | |
| 325 | TestResource* scratch; |
| 326 | TestResource* content; |
| 327 | TestResource* wrapped; |
| 328 | TestResource* unbudgeted; |
| 329 | |
| 330 | // A large uncached or wrapped resource shouldn't evict anything. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 331 | scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 332 | scratch->setSize(10); |
| 333 | scratch->unref(); |
| 334 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 335 | REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); |
| 336 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 337 | REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); |
| 338 | |
| 339 | content = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 340 | content->setSize(11); |
| 341 | REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
| 342 | content->unref(); |
| 343 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 344 | REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); |
| 345 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 346 | REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
| 347 | |
| 348 | size_t large = 2 * cache2->getResourceBytes(); |
| 349 | unbudgeted = SkNEW_ARGS(TestResource, |
| 350 | (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle)); |
| 351 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 352 | REPORTER_ASSERT(reporter, 21 + large == cache2->getResourceBytes()); |
| 353 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 354 | REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
| 355 | |
| 356 | unbudgeted->unref(); |
| 357 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 358 | REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); |
| 359 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 360 | REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
| 361 | |
| 362 | wrapped = SkNEW_ARGS(TestResource, |
| 363 | (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle)); |
| 364 | REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); |
| 365 | REPORTER_ASSERT(reporter, 21 + large == cache2->getResourceBytes()); |
| 366 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 367 | REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
| 368 | |
| 369 | wrapped->unref(); |
| 370 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 371 | REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); |
| 372 | REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); |
| 373 | REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); |
| 374 | |
| 375 | cache2->purgeAllUnlocked(); |
| 376 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 377 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 378 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 379 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 380 | } |
| 381 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 382 | static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
| 383 | Mock mock(10, 300); |
| 384 | GrContext* context = mock.context(); |
| 385 | GrResourceCache2* cache2 = mock.cache(); |
| 386 | |
| 387 | TestResource* resource = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 388 | TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 389 | GrScratchKey key; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 390 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 391 | |
| 392 | size_t size = resource->gpuMemorySize(); |
| 393 | for (int i = 0; i < 2; ++i) { |
| 394 | // Since this resource is unbudgeted, it should not be reachable as scratch. |
| 395 | REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key); |
| 396 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
| 397 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); |
| 398 | REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(key)); |
| 399 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 400 | REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 401 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 402 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 403 | |
| 404 | // Once it is unrefed, it should become available as scratch. |
| 405 | resource->unref(); |
| 406 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 407 | REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 408 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 409 | REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()); |
| 410 | resource = static_cast<TestResource*>(cache2->findAndRefScratchResource(key)); |
| 411 | REPORTER_ASSERT(reporter, resource); |
| 412 | REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key); |
| 413 | REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
| 414 | REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
| 415 | |
| 416 | if (0 == i) { |
| 417 | // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try |
| 418 | // the above tests again. |
| 419 | resource->cacheAccess().makeUnbudgeted(); |
| 420 | } else { |
| 421 | // After the second time around, try removing the scratch key |
| 422 | resource->cacheAccess().removeScratchKey(); |
| 423 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 424 | REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); |
| 425 | REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); |
| 426 | REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()); |
| 427 | REPORTER_ASSERT(reporter, !resource->cacheAccess().getScratchKey().isValid()); |
| 428 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
| 429 | REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
| 430 | |
| 431 | // now when it is unrefed it should die since it has no key. |
| 432 | resource->unref(); |
| 433 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 434 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
| 435 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); |
| 436 | REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); |
| 437 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 438 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 442 | Mock mock(5, 30000); |
| 443 | GrContext* context = mock.context(); |
| 444 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 445 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 446 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 447 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 448 | TestResource::kB_SimulatedProperty); |
| 449 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 450 | TestResource::kB_SimulatedProperty); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 451 | a->setSize(11); |
| 452 | b->setSize(12); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 453 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 454 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 455 | // Check for negative case consistency. (leaks upon test failure.) |
| 456 | REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchKey1)); |
| 457 | |
| 458 | GrScratchKey scratchKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 459 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 460 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 461 | // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 462 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 463 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 464 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 465 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 466 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 467 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 468 | // Our refs mean that the resources are non purgeable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 469 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 470 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 471 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 472 | |
| 473 | // Unref but don't purge |
| 474 | a->unref(); |
| 475 | b->unref(); |
| 476 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 477 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
| 478 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 479 | // Purge again. This time resources should be purgeable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 480 | cache2->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 481 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 482 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 483 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 484 | } |
| 485 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 486 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 487 | Mock mock(5, 30000); |
| 488 | GrContext* context = mock.context(); |
| 489 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 490 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 491 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 492 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 493 | TestResource::kB_SimulatedProperty); |
| 494 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 495 | TestResource::kB_SimulatedProperty); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 496 | a->unref(); |
| 497 | b->unref(); |
| 498 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 499 | |
| 500 | GrScratchKey scratchKey; |
| 501 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 502 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 503 | // (following leaks upon test failure). |
| 504 | REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == NULL); |
| 505 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 506 | // Scratch resources are registered with GrResourceCache2 just by existing. There are 2. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 507 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 508 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 509 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) |
| 510 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 511 | |
| 512 | // Find the first resource and remove its scratch key |
| 513 | GrGpuResource* find; |
| 514 | find = cache2->findAndRefScratchResource(scratchKey); |
| 515 | find->cacheAccess().removeScratchKey(); |
| 516 | // It's still alive, but not cached by scratch key anymore |
| 517 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 518 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey(scratchKey));) |
| 519 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 520 | |
| 521 | // The cache should immediately delete it when it's unrefed since it isn't accessible. |
| 522 | find->unref(); |
| 523 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 524 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey(scratchKey));) |
| 525 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 526 | |
| 527 | // Repeat for the second resource. |
| 528 | find = cache2->findAndRefScratchResource(scratchKey); |
| 529 | find->cacheAccess().removeScratchKey(); |
| 530 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 531 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 532 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 533 | |
| 534 | // Should be able to call this multiple times with no problem. |
| 535 | find->cacheAccess().removeScratchKey(); |
| 536 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 537 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 538 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 539 | |
| 540 | find->unref(); |
| 541 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 542 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));) |
| 543 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 544 | } |
| 545 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 546 | static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 547 | Mock mock(5, 30000); |
| 548 | GrContext* context = mock.context(); |
| 549 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 550 | |
| 551 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 552 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 553 | TestResource::kB_SimulatedProperty); |
| 554 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 555 | TestResource::kB_SimulatedProperty); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 556 | a->unref(); |
| 557 | b->unref(); |
| 558 | |
| 559 | GrScratchKey scratchKey; |
| 560 | // Ensure that scratch key comparison and assignment is consistent. |
| 561 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 562 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 563 | GrScratchKey scratchKey2; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 564 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 565 | REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize()); |
| 566 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); |
| 567 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); |
| 568 | scratchKey = scratchKey1; |
| 569 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 570 | REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); |
| 571 | REPORTER_ASSERT(reporter, scratchKey == scratchKey1); |
| 572 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); |
| 573 | REPORTER_ASSERT(reporter, scratchKey != scratchKey2); |
| 574 | scratchKey = scratchKey2; |
| 575 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 576 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
| 577 | REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
| 578 | REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
| 579 | REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
| 580 | |
| 581 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 582 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 583 | // (following leaks upon test failure). |
| 584 | REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == NULL); |
| 585 | |
| 586 | // Find the first resource with a scratch key and a copy of a scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 587 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 588 | GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey); |
| 589 | REPORTER_ASSERT(reporter, find != NULL); |
| 590 | find->unref(); |
| 591 | |
| 592 | scratchKey2 = scratchKey; |
| 593 | find = cache2->findAndRefScratchResource(scratchKey2); |
| 594 | REPORTER_ASSERT(reporter, find != NULL); |
| 595 | REPORTER_ASSERT(reporter, find == a || find == b); |
| 596 | |
| 597 | GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); |
| 598 | REPORTER_ASSERT(reporter, find2 != NULL); |
| 599 | REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
| 600 | REPORTER_ASSERT(reporter, find2 != find); |
| 601 | find2->unref(); |
| 602 | find->unref(); |
| 603 | } |
| 604 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 605 | static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 606 | Mock mock(5, 30000); |
| 607 | GrContext* context = mock.context(); |
| 608 | GrResourceCache2* cache2 = mock.cache(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 609 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 610 | GrContentKey key; |
| 611 | make_content_key<0>(&key, 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 612 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 613 | // Create two resources that we will attempt to register with the same content key. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 614 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 615 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 616 | a->setSize(11); |
| 617 | b->setSize(12); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 618 | |
| 619 | // Can't set the same content key on two resources. |
| 620 | REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key)); |
| 621 | REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key)); |
| 622 | |
| 623 | // Still have two resources because b is still reffed. |
| 624 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
| 625 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 626 | cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 627 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 628 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 629 | b->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 630 | // Now b should be gone. |
| 631 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 632 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 633 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 634 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 635 | cache2->purgeAllUnlocked(); |
| 636 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 637 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 638 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 639 | |
| 640 | // 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] | 641 | a->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 642 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
| 643 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); |
| 644 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 645 | |
| 646 | cache2->purgeAllUnlocked(); |
| 647 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 648 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 649 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 650 | } |
| 651 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 652 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 653 | Mock mock(5, 30000); |
| 654 | GrContext* context = mock.context(); |
| 655 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 656 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 657 | GrContentKey key1, key2, key3; |
| 658 | make_content_key<0>(&key1, 1); |
| 659 | make_content_key<0>(&key2, 2); |
| 660 | make_content_key<0>(&key3, 3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 661 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 662 | // Add three resources to the cache. Only c is usable as scratch. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 663 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 664 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 665 | TestResource* c = TestResource::CreateScratch(context->getGpu(), |
| 666 | TestResource::kA_SimulatedProperty); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 667 | a->cacheAccess().setContentKey(key1); |
| 668 | b->cacheAccess().setContentKey(key2); |
| 669 | c->cacheAccess().setContentKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 670 | a->unref(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 671 | // hold b until *after* the message is sent. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 672 | c->unref(); |
| 673 | |
| 674 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 675 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
| 676 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 677 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 678 | |
| 679 | typedef GrContentKeyInvalidatedMessage Msg; |
| 680 | typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; |
| 681 | |
| 682 | // Invalidate two of the three, they should be purged and no longer accessible via their keys. |
| 683 | Bus::Post(Msg(key1)); |
| 684 | Bus::Post(Msg(key2)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 685 | cache2->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 686 | // a should be deleted now, but we still have a ref on b. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 687 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 688 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 689 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 690 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 691 | |
| 692 | // Invalidate the third. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 693 | Bus::Post(Msg(key3)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 694 | cache2->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 695 | // we still have a ref on b, c should be recycled as scratch. |
| 696 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 698 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 699 | // make b purgeable. It should be immediately deleted since it has no key. |
| 700 | b->unref(); |
| 701 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 702 | |
| 703 | // Make sure we actually get to c via it's scratch key, before we say goodbye. |
| 704 | GrScratchKey scratchKey; |
| 705 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
| 706 | GrGpuResource* scratch = cache2->findAndRefScratchResource(scratchKey); |
| 707 | REPORTER_ASSERT(reporter, scratch == c); |
| 708 | SkSafeUnref(scratch); |
| 709 | |
| 710 | // Get rid of c. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 711 | cache2->purgeAllUnlocked(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 712 | scratch = cache2->findAndRefScratchResource(scratchKey); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 713 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 714 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); |
| 715 | REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 716 | REPORTER_ASSERT(reporter, !scratch); |
| 717 | SkSafeUnref(scratch); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 718 | } |
| 719 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 720 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 721 | Mock mock(3, 30000); |
| 722 | GrContext* context = mock.context(); |
| 723 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 724 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 725 | GrContentKey key1, key2; |
| 726 | make_content_key<0>(&key1, 1); |
| 727 | make_content_key<0>(&key2, 2); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 728 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 729 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 730 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 731 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 732 | a->cacheAccess().setContentKey(key1); |
| 733 | b->cacheAccess().setContentKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 734 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 735 | // Make a cycle |
| 736 | a->setUnrefWhenDestroyed(b); |
| 737 | b->setUnrefWhenDestroyed(a); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 738 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 739 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 740 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 741 | a->unref(); |
| 742 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 743 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 744 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 745 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 746 | cache2->purgeAllUnlocked(); |
| 747 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 748 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 749 | // Break the cycle |
| 750 | a->setUnrefWhenDestroyed(NULL); |
| 751 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 752 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 753 | cache2->purgeAllUnlocked(); |
| 754 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 755 | } |
| 756 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 757 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 758 | GrContentKey key1, key2; |
| 759 | make_content_key<0>(&key1, 1); |
| 760 | make_content_key<0>(&key2, 2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 761 | |
| 762 | // Test changing resources sizes (both increase & decrease). |
| 763 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 764 | Mock mock(3, 30000); |
| 765 | GrContext* context = mock.context(); |
| 766 | GrResourceCache2* cache2 = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 767 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 768 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 769 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 770 | a->unref(); |
| 771 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 772 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 773 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 774 | b->unref(); |
| 775 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 776 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 777 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 778 | { |
| 779 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 780 | find2->setSize(200); |
| 781 | SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->findAndRefContentResource(key1))); |
| 782 | find1->setSize(50); |
| 783 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 784 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 785 | REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); |
| 786 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Test increasing a resources size beyond the cache budget. |
| 790 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 791 | Mock mock(2, 300); |
| 792 | GrContext* context = mock.context(); |
| 793 | GrResourceCache2* cache2 = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 794 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 795 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 796 | a->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 797 | a->cacheAccess().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 798 | a->unref(); |
| 799 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 800 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 801 | b->setSize(100); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 802 | b->cacheAccess().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 803 | b->unref(); |
| 804 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 805 | REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); |
| 806 | REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 807 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 808 | { |
| 809 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2))); |
| 810 | find2->setSize(201); |
| 811 | } |
| 812 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 813 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 814 | REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); |
| 815 | REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 816 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 817 | } |
| 818 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 819 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 820 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 821 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 822 | // down to 1 byte after creating the resource. |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 823 | static const int kResourceCnt = 2000; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 824 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 825 | Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 826 | GrContext* context = mock.context(); |
| 827 | GrResourceCache2* cache2 = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 828 | |
| 829 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 830 | GrContentKey key1, key2; |
| 831 | make_content_key<1>(&key1, i); |
| 832 | make_content_key<2>(&key2, i); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 833 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 834 | TestResource* resource; |
| 835 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 836 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 837 | resource->cacheAccess().setContentKey(key1); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 838 | resource->setSize(1); |
| 839 | resource->unref(); |
| 840 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 841 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 842 | resource->cacheAccess().setContentKey(key2); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 843 | resource->setSize(1); |
| 844 | resource->unref(); |
| 845 | } |
| 846 | |
| 847 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
| 848 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 849 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 850 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt); |
| 851 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt); |
| 852 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 853 | GrContentKey key1, key2; |
| 854 | make_content_key<1>(&key1, i); |
| 855 | make_content_key<2>(&key2, i); |
| 856 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 857 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 858 | REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | cache2->purgeAllUnlocked(); |
| 862 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
| 863 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0); |
| 864 | REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0); |
| 865 | REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0); |
| 866 | REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0); |
| 867 | |
| 868 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 869 | GrContentKey key1, key2; |
| 870 | make_content_key<1>(&key1, i); |
| 871 | make_content_key<2>(&key2, i); |
| 872 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 873 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 874 | REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 879 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 880 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 881 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 882 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 883 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 884 | continue; |
| 885 | } |
| 886 | GrContext* context = factory->get(glType); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 887 | if (NULL == context) { |
| 888 | continue; |
| 889 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 890 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 891 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 892 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 893 | desc.fWidth = gWidth; |
| 894 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 895 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 896 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, |
| 897 | SkSurface::kNo_Budgeted, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 898 | test_cache(reporter, context, surface->getCanvas()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 899 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 900 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 901 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 902 | test_no_key(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 903 | test_budgeting(reporter); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 904 | test_unbudgeted(reporter); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 905 | test_unbudgeted_to_scratch(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 906 | test_duplicate_content_key(reporter); |
| 907 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 908 | test_remove_scratch_key(reporter); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 909 | test_scratch_key_consistency(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 910 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 911 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 912 | test_resource_size_changed(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 913 | test_large_resource_count(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 914 | } |
| 915 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 916 | #endif |