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