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