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