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