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