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 |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 12 | #include <thread> |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 15 | #include "GrContextFactory.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 16 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 17 | #include "GrGpuResourceCacheAccess.h" |
| 18 | #include "GrGpuResourcePriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 19 | #include "GrProxyProvider.h" |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 20 | #include "GrRenderTargetPriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 21 | #include "GrResourceCache.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 22 | #include "GrResourceProvider.h" |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 23 | #include "GrTest.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 24 | #include "GrTexture.h" |
| 25 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 26 | #include "SkCanvas.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 27 | #include "SkGr.h" |
| 28 | #include "SkMessageBus.h" |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 29 | #include "SkMipMap.h" |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 30 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 31 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 32 | |
| 33 | static const int gWidth = 640; |
| 34 | static const int gHeight = 480; |
| 35 | |
| 36 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 37 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 38 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 39 | GrSurfaceDesc desc; |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 40 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 41 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 42 | desc.fWidth = gWidth; |
| 43 | desc.fHeight = gHeight; |
| 44 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 45 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 46 | SkCanvas* canvas = surface->getCanvas(); |
| 47 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 48 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 49 | |
| 50 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 51 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 52 | src.eraseColor(SK_ColorBLACK); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 53 | size_t srcSize = src.computeByteSize(); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 54 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 55 | size_t initialCacheSize; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | context->getResourceCacheUsage(nullptr, &initialCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 57 | |
| 58 | int oldMaxNum; |
| 59 | size_t oldMaxBytes; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 60 | context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 61 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 62 | // Set the cache limits so we can fit 10 "src" images and the |
| 63 | // max number of textures doesn't matter |
| 64 | size_t maxCacheSize = initialCacheSize + 10*srcSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 65 | context->setResourceCacheLimits(1000, maxCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 66 | |
| 67 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 68 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 69 | |
| 70 | for (int i = 0; i < 100; ++i) { |
| 71 | canvas->drawBitmap(src, 0, 0); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 72 | surface->readPixels(readback, 0, 0); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 73 | |
| 74 | // "modify" the src texture |
| 75 | src.notifyPixelsChanged(); |
| 76 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 77 | size_t curCacheSize; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | context->getResourceCacheUsage(nullptr, &curCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 79 | |
| 80 | // we should never go over the size limit |
| 81 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 82 | } |
| 83 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 84 | context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame] | 87 | static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) { |
| 88 | if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType || |
| 89 | type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) { |
| 90 | return false; |
| 91 | } |
| 92 | return sk_gpu_test::GrContextFactory::IsRenderingContext(type); |
| 93 | } |
| 94 | |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 95 | static GrStencilAttachment* get_SB(GrRenderTarget* rt) { |
| 96 | return rt->renderTargetPriv().getStencilAttachment(); |
| 97 | } |
| 98 | |
| 99 | static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider, |
| 100 | int size, int sampleCount, SkBudgeted budgeted) { |
| 101 | GrSurfaceDesc desc; |
| 102 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 103 | desc.fWidth = size; |
| 104 | desc.fHeight = size; |
| 105 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 106 | desc.fSampleCnt = sampleCount; |
| 107 | |
| 108 | sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted)); |
| 109 | if (!tex || !tex->asRenderTarget()) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | if (!provider->attachStencilAttachment(tex->asRenderTarget())) { |
| 114 | return nullptr; |
| 115 | } |
| 116 | SkASSERT(get_SB(tex->asRenderTarget())); |
| 117 | |
| 118 | return sk_ref_sp(tex->asRenderTarget()); |
| 119 | } |
| 120 | |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame] | 121 | // This currently fails on ES3 ANGLE contexts |
| 122 | DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter, |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 123 | ctxInfo, nullptr) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 124 | GrContext* context = ctxInfo.grContext(); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 125 | if (context->caps()->avoidStencilBuffers()) { |
| 126 | return; |
| 127 | } |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 128 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 129 | GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider(); |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 130 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 131 | sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes); |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, smallRT0); |
| 133 | |
| 134 | { |
| 135 | // Two budgeted RTs with the same desc should share a stencil buffer. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 136 | sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes); |
| 137 | REPORTER_ASSERT(reporter, smallRT1); |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 138 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get())); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 142 | { |
| 143 | // An unbudgeted RT with the same desc should also share. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 144 | sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo); |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 145 | REPORTER_ASSERT(reporter, smallRT2); |
| 146 | |
| 147 | REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get())); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 150 | { |
| 151 | // An RT with a much larger size should not share. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 152 | sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo); |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, bigRT); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 154 | |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 155 | REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get())); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 156 | } |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 157 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 158 | int smallSampleCount = context->caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig); |
| 159 | if (smallSampleCount > 1) { |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 160 | // An RT with a different sample count should not share. |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 161 | sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4, |
| 162 | smallSampleCount, SkBudgeted::kNo); |
bsalomon | b602d4d | 2015-02-19 12:05:58 -0800 | [diff] [blame] | 163 | #ifdef SK_BUILD_FOR_ANDROID |
| 164 | if (!smallMSAART0) { |
| 165 | // The nexus player seems to fail to create MSAA textures. |
| 166 | return; |
| 167 | } |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 168 | #else |
| 169 | REPORTER_ASSERT(reporter, smallMSAART0); |
bsalomon | b602d4d | 2015-02-19 12:05:58 -0800 | [diff] [blame] | 170 | #endif |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 171 | |
| 172 | REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get())); |
| 173 | |
| 174 | { |
| 175 | // A second MSAA RT should share with the first MSAA RT. |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 176 | sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4, |
| 177 | smallSampleCount, |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 178 | SkBudgeted::kNo); |
| 179 | REPORTER_ASSERT(reporter, smallMSAART1); |
| 180 | |
| 181 | REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get())); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 182 | } |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 183 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 184 | // But one with a larger sample count should not. (Also check that the two requests didn't |
| 185 | // rounded up to the same actual sample count or else they could share.). |
| 186 | int bigSampleCount = |
| 187 | context->caps()->getRenderTargetSampleCount(5, kRGBA_8888_GrPixelConfig); |
| 188 | if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 189 | sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4, |
| 190 | bigSampleCount, |
Robert Phillips | c0192e3 | 2017-09-21 12:00:26 -0400 | [diff] [blame] | 191 | SkBudgeted::kNo); |
| 192 | REPORTER_ASSERT(reporter, smallMSAART2); |
| 193 | |
| 194 | REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get())); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 199 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 200 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 201 | GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 202 | GrGpu* gpu = context->contextPriv().getGpu(); |
jvanverth | eeb8d99 | 2015-07-15 10:16:56 -0700 | [diff] [blame] | 203 | // this test is only valid for GL |
| 204 | if (!gpu || !gpu->glContextForTesting()) { |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 208 | GrBackendTexture backendTextures[2]; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 209 | static const int kW = 100; |
| 210 | static const int kH = 100; |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 211 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 212 | backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, |
| 213 | kRGBA_8888_GrPixelConfig, |
| 214 | false, GrMipMapped::kNo); |
| 215 | backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, |
| 216 | kRGBA_8888_GrPixelConfig, |
| 217 | false, GrMipMapped::kNo); |
Greg Daniel | 5366e59 | 2018-01-10 09:57:53 -0500 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, backendTextures[0].isValid()); |
| 219 | REPORTER_ASSERT(reporter, backendTextures[1].isValid()); |
| 220 | if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) { |
| 221 | return; |
| 222 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 223 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 224 | context->resetContext(); |
| 225 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 226 | sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture( |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 227 | backendTextures[0], kBorrow_GrWrapOwnership)); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 228 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 229 | sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture( |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 230 | backendTextures[1], kAdopt_GrWrapOwnership)); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 231 | |
Brian Osman | 85d34b2 | 2017-05-10 12:06:26 -0400 | [diff] [blame] | 232 | REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr); |
| 233 | if (!borrowed || !adopted) { |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 237 | borrowed.reset(nullptr); |
| 238 | adopted.reset(nullptr); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 239 | |
| 240 | context->flush(); |
| 241 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 242 | bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]); |
| 243 | bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 244 | |
| 245 | REPORTER_ASSERT(reporter, borrowedIsAlive); |
| 246 | REPORTER_ASSERT(reporter, !adoptedIsAlive); |
| 247 | |
Brian Salomon | e64b064 | 2018-03-07 11:47:54 -0500 | [diff] [blame] | 248 | if (borrowedIsAlive) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 249 | gpu->deleteTestingOnlyBackendTexture(backendTextures[0]); |
Brian Salomon | e64b064 | 2018-03-07 11:47:54 -0500 | [diff] [blame] | 250 | } |
| 251 | if (adoptedIsAlive) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 252 | gpu->deleteTestingOnlyBackendTexture(backendTextures[1]); |
Brian Salomon | e64b064 | 2018-03-07 11:47:54 -0500 | [diff] [blame] | 253 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 254 | |
| 255 | context->resetContext(); |
| 256 | } |
| 257 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 258 | class TestResource : public GrGpuResource { |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 259 | enum ScratchConstructor { kScratchConstructor }; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 260 | public: |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 261 | static const size_t kDefaultSize = 100; |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 262 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 263 | /** Property that distinctly categorizes the resource. |
| 264 | * For example, textures have width, height, ... */ |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 265 | enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 266 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 267 | TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize) |
| 268 | : INHERITED(gpu) |
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(size) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 271 | , fProperty(kA_SimulatedProperty) |
| 272 | , fIsScratch(false) { |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 273 | ++fNumAlive; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 274 | this->registerWithCache(budgeted); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 275 | } |
| 276 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 277 | static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted, |
| 278 | SimulatedProperty property) { |
| 279 | return new TestResource(gpu, budgeted, property, kScratchConstructor); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 280 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 281 | static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) { |
| 282 | return new TestResource(gpu, size); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 285 | ~TestResource() override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 286 | --fNumAlive; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 287 | SkSafeUnref(fToDelete); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 288 | } |
| 289 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 290 | void setSize(size_t size) { |
| 291 | fSize = size; |
| 292 | this->didChangeGpuMemorySize(); |
| 293 | } |
| 294 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 295 | static int NumAlive() { return fNumAlive; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 296 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 297 | void setUnrefWhenDestroyed(TestResource* resource) { |
| 298 | SkRefCnt_SafeAssign(fToDelete, resource); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 299 | } |
| 300 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 301 | static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) { |
| 302 | static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 303 | GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 304 | for (int i = 0; i < kScratchKeyFieldCnt; ++i) { |
| 305 | builder[i] = static_cast<uint32_t>(i + property); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | static size_t ExpectedScratchKeySize() { |
| 310 | return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt); |
| 311 | } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 312 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 313 | static const int kScratchKeyFieldCnt = 6; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 314 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 315 | TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor) |
| 316 | : INHERITED(gpu) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 317 | , fToDelete(nullptr) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 318 | , fSize(kDefaultSize) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 319 | , fProperty(property) |
| 320 | , fIsScratch(true) { |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 321 | ++fNumAlive; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 322 | this->registerWithCache(budgeted); |
| 323 | } |
| 324 | |
| 325 | // Constructor for simulating resources that wrap backend objects. |
| 326 | TestResource(GrGpu* gpu, size_t size) |
| 327 | : INHERITED(gpu) |
| 328 | , fToDelete(nullptr) |
| 329 | , fSize(size) |
| 330 | , fProperty(kA_SimulatedProperty) |
| 331 | , fIsScratch(false) { |
| 332 | ++fNumAlive; |
| 333 | this->registerWithCacheWrapped(); |
| 334 | } |
| 335 | |
| 336 | void computeScratchKey(GrScratchKey* key) const override { |
| 337 | if (fIsScratch) { |
| 338 | ComputeScratchKey(fProperty, key); |
| 339 | } |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 340 | } |
| 341 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 342 | size_t onGpuMemorySize() const override { return fSize; } |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame^] | 343 | const char* getResourceType() const override { return "Test"; } |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 344 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 345 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 346 | size_t fSize; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 347 | static int fNumAlive; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 348 | SimulatedProperty fProperty; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 349 | bool fIsScratch; |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 350 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 351 | }; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 352 | int TestResource::fNumAlive = 0; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 353 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 354 | class Mock { |
| 355 | public: |
| 356 | Mock(int maxCnt, size_t maxBytes) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 357 | fContext = GrContext::MakeMock(nullptr); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 358 | SkASSERT(fContext); |
| 359 | fContext->setResourceCacheLimits(maxCnt, maxBytes); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 360 | GrResourceCache* cache = fContext->contextPriv().getResourceCache(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 361 | cache->purgeAllUnlocked(); |
| 362 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 363 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 364 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 365 | GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 366 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 367 | GrContext* context() { return fContext.get(); } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 368 | |
| 369 | private: |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 370 | sk_sp<GrContext> fContext; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | static void test_no_key(skiatest::Reporter* reporter) { |
| 374 | Mock mock(10, 30000); |
| 375 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 376 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 377 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 378 | |
| 379 | // Create a bunch of resources with no keys |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 380 | TestResource* a = new TestResource(gpu); |
| 381 | TestResource* b = new TestResource(gpu); |
| 382 | TestResource* c = new TestResource(gpu); |
| 383 | TestResource* d = new TestResource(gpu); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 384 | a->setSize(11); |
| 385 | b->setSize(12); |
| 386 | c->setSize(13); |
| 387 | d->setSize(14); |
| 388 | |
| 389 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 390 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 391 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 392 | d->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 393 | |
| 394 | // 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] | 395 | cache->purgeAllUnlocked(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 396 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 397 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 398 | // Since the resources have neither unique nor scratch keys, delete immediately upon unref. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 399 | |
| 400 | a->unref(); |
| 401 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 403 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 404 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 405 | |
| 406 | c->unref(); |
| 407 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 408 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 409 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 410 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 411 | |
| 412 | d->unref(); |
| 413 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 414 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 415 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 416 | |
| 417 | b->unref(); |
| 418 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 419 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 420 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 421 | } |
| 422 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 423 | // Each integer passed as a template param creates a new domain. |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 424 | template <int> |
| 425 | static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 426 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 427 | GrUniqueKey::Builder builder(key, d, 1, tag); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 428 | builder[0] = data; |
| 429 | } |
| 430 | |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 431 | static void test_purge_unlocked(skiatest::Reporter* reporter) { |
| 432 | Mock mock(10, 30000); |
| 433 | GrContext* context = mock.context(); |
| 434 | GrResourceCache* cache = mock.cache(); |
| 435 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 436 | |
| 437 | // Create two resource w/ a unique key and two w/o but all of which have scratch keys. |
| 438 | TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
| 439 | TestResource::kA_SimulatedProperty); |
| 440 | a->setSize(11); |
| 441 | |
| 442 | GrUniqueKey uniqueKey; |
| 443 | make_unique_key<0>(&uniqueKey, 0); |
| 444 | |
| 445 | TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
| 446 | TestResource::kA_SimulatedProperty); |
| 447 | b->setSize(12); |
| 448 | b->resourcePriv().setUniqueKey(uniqueKey); |
| 449 | |
| 450 | TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
| 451 | TestResource::kA_SimulatedProperty); |
| 452 | c->setSize(13); |
| 453 | |
| 454 | GrUniqueKey uniqueKey2; |
| 455 | make_unique_key<0>(&uniqueKey2, 1); |
| 456 | |
| 457 | TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
| 458 | TestResource::kA_SimulatedProperty); |
| 459 | d->setSize(14); |
| 460 | d->resourcePriv().setUniqueKey(uniqueKey2); |
| 461 | |
| 462 | |
| 463 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 464 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
| 465 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
| 466 | d->gpuMemorySize() == cache->getResourceBytes()); |
| 467 | |
| 468 | // Should be safe to purge without deleting the resources since we still have refs. |
| 469 | cache->purgeUnlockedResources(false); |
| 470 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 471 | |
| 472 | // Unref them all. Since they all have keys they should remain in the cache. |
| 473 | |
| 474 | a->unref(); |
| 475 | b->unref(); |
| 476 | c->unref(); |
| 477 | d->unref(); |
| 478 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 479 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
| 480 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
| 481 | d->gpuMemorySize() == cache->getResourceBytes()); |
| 482 | |
| 483 | // Purge only the two scratch resources |
| 484 | cache->purgeUnlockedResources(true); |
| 485 | |
| 486 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 487 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 488 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
| 489 | cache->getResourceBytes()); |
| 490 | |
| 491 | // Purge the uniquely keyed resources |
| 492 | cache->purgeUnlockedResources(false); |
| 493 | |
| 494 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 495 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 496 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 497 | } |
| 498 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 499 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 500 | Mock mock(10, 300); |
| 501 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 502 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 503 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 504 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 505 | GrUniqueKey uniqueKey; |
| 506 | make_unique_key<0>(&uniqueKey, 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 507 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 508 | // Create a scratch, a unique, and a wrapped resource |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 509 | TestResource* scratch = |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 510 | TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 511 | scratch->setSize(10); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 512 | TestResource* unique = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 513 | unique->setSize(11); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 514 | unique->resourcePriv().setUniqueKey(uniqueKey); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 515 | TestResource* wrapped = TestResource::CreateWrapped(gpu); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 516 | wrapped->setSize(12); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 517 | TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 518 | unbudgeted->setSize(13); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 519 | |
Brian Osman | 0562eb9 | 2017-05-08 11:16:39 -0400 | [diff] [blame] | 520 | // Make sure we can add a unique key to the wrapped resource |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 521 | GrUniqueKey uniqueKey2; |
| 522 | make_unique_key<0>(&uniqueKey2, 1); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 523 | wrapped->resourcePriv().setUniqueKey(uniqueKey2); |
Brian Osman | 0562eb9 | 2017-05-08 11:16:39 -0400 | [diff] [blame] | 524 | GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2); |
| 525 | REPORTER_ASSERT(reporter, wrappedViaKey != nullptr); |
| 526 | |
| 527 | // Remove the extra ref we just added. |
| 528 | wrappedViaKey->unref(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 529 | |
| 530 | // Make sure sizes are as we expect |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 531 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 532 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 533 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 534 | cache->getResourceBytes()); |
| 535 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 536 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 537 | cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 538 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 539 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 540 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 541 | cache->purgeAllUnlocked(); |
| 542 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 543 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 544 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 545 | cache->getResourceBytes()); |
| 546 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 547 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 548 | cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 549 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 550 | |
| 551 | // Unreffing the wrapped resource should free it right away. |
| 552 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 553 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 554 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 555 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 556 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 557 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 558 | // Now try freeing the budgeted resources first |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 559 | wrapped = TestResource::CreateWrapped(gpu); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 560 | scratch->setSize(12); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 561 | unique->unref(); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 563 | cache->purgeAllUnlocked(); |
| 564 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 565 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 566 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 567 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 568 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 569 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 570 | |
| 571 | scratch->unref(); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 572 | REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 573 | cache->purgeAllUnlocked(); |
| 574 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 575 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 576 | cache->getResourceBytes()); |
| 577 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 578 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 579 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 580 | |
| 581 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 582 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 583 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 584 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 585 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 586 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 587 | |
| 588 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 589 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 590 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 591 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 592 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 593 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 594 | } |
| 595 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 596 | static void test_unbudgeted(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 597 | Mock mock(10, 30000); |
| 598 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 599 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 600 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 601 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 602 | GrUniqueKey uniqueKey; |
| 603 | make_unique_key<0>(&uniqueKey, 0); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 604 | |
| 605 | TestResource* scratch; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 606 | TestResource* unique; |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 607 | TestResource* wrapped; |
| 608 | TestResource* unbudgeted; |
| 609 | |
| 610 | // A large uncached or wrapped resource shouldn't evict anything. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 611 | scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 612 | TestResource::kB_SimulatedProperty); |
| 613 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 614 | scratch->setSize(10); |
| 615 | scratch->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 616 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 617 | REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
| 618 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 619 | REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 620 | REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 621 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 622 | unique = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 623 | unique->setSize(11); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 624 | unique->resourcePriv().setUniqueKey(uniqueKey); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 625 | unique->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 626 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 627 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 628 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 629 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 630 | REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 631 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 632 | size_t large = 2 * cache->getResourceBytes(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 633 | unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 634 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 635 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 636 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 637 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 638 | REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 639 | |
| 640 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 641 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 642 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 643 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 644 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 645 | REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 646 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 647 | wrapped = TestResource::CreateWrapped(gpu, large); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 648 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 649 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 650 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 651 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 652 | REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 653 | |
| 654 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 655 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 656 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 657 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 658 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 659 | REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 660 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 661 | cache->purgeAllUnlocked(); |
| 662 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 663 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 664 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 665 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 666 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 667 | } |
| 668 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 669 | // This method can't be static because it needs to friended in GrGpuResource::CacheAccess. |
| 670 | void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); |
| 671 | /*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 672 | Mock mock(10, 300); |
| 673 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 674 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 675 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 676 | |
| 677 | TestResource* resource = |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 678 | TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 679 | GrScratchKey key; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 680 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 681 | |
| 682 | size_t size = resource->gpuMemorySize(); |
| 683 | for (int i = 0; i < 2; ++i) { |
| 684 | // Since this resource is unbudgeted, it should not be reachable as scratch. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 685 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 686 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 687 | REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 688 | REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 689 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 690 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 691 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 692 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 693 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 694 | |
| 695 | // Once it is unrefed, it should become available as scratch. |
| 696 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 698 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 699 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 700 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 701 | REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes()); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 702 | resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0)); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 703 | REPORTER_ASSERT(reporter, resource); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 704 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 705 | REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 706 | REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 707 | |
| 708 | if (0 == i) { |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 709 | // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 710 | // the above tests again. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 711 | resource->resourcePriv().makeUnbudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 712 | } else { |
| 713 | // After the second time around, try removing the scratch key |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 714 | resource->resourcePriv().removeScratchKey(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 715 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 716 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 717 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 718 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 719 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 720 | REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 721 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 722 | REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 723 | |
| 724 | // now when it is unrefed it should die since it has no key. |
| 725 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 726 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 727 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 728 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 729 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 730 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 731 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 732 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 736 | Mock mock(5, 30000); |
| 737 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 738 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 739 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 740 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 741 | // Create two resources that have the same scratch key. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 742 | TestResource* a = TestResource::CreateScratch(gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 743 | SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 744 | TestResource::kB_SimulatedProperty); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 745 | TestResource* b = TestResource::CreateScratch(gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 746 | SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 747 | TestResource::kB_SimulatedProperty); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 748 | a->setSize(11); |
| 749 | b->setSize(12); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 750 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 751 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 752 | // Check for negative case consistency. (leaks upon test failure.) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 753 | REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0)); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 754 | |
| 755 | GrScratchKey scratchKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 756 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 757 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 758 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 759 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 760 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 761 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 762 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 763 | cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 764 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 765 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 766 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 767 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 768 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 769 | |
| 770 | // Unref but don't purge |
| 771 | a->unref(); |
| 772 | b->unref(); |
| 773 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 774 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 775 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 776 | // Purge again. This time resources should be purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 777 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 778 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 779 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 780 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 781 | } |
| 782 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 783 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 784 | Mock mock(5, 30000); |
| 785 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 786 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 787 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 788 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 789 | // Create two resources that have the same scratch key. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 790 | TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 791 | TestResource::kB_SimulatedProperty); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 792 | TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 793 | TestResource::kB_SimulatedProperty); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 794 | a->unref(); |
| 795 | b->unref(); |
| 796 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 797 | GrScratchKey scratchKey; |
| 798 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 799 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 800 | // (following leaks upon test failure). |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 801 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 802 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 803 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 804 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 805 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 806 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 807 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 808 | |
| 809 | // Find the first resource and remove its scratch key |
| 810 | GrGpuResource* find; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 811 | find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 812 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 813 | // It's still alive, but not cached by scratch key anymore |
| 814 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 815 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 816 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 817 | |
| 818 | // The cache should immediately delete it when it's unrefed since it isn't accessible. |
| 819 | find->unref(); |
| 820 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 821 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 822 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 823 | |
| 824 | // Repeat for the second resource. |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 825 | find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 826 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 827 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 828 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 829 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 830 | |
| 831 | // Should be able to call this multiple times with no problem. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 832 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 833 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 834 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 835 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 836 | |
| 837 | find->unref(); |
| 838 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 839 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 840 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 841 | } |
| 842 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 843 | static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 844 | Mock mock(5, 30000); |
| 845 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 846 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 847 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 848 | |
| 849 | // Create two resources that have the same scratch key. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 850 | TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 851 | TestResource::kB_SimulatedProperty); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 852 | TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 853 | TestResource::kB_SimulatedProperty); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 854 | a->unref(); |
| 855 | b->unref(); |
| 856 | |
| 857 | GrScratchKey scratchKey; |
| 858 | // Ensure that scratch key comparison and assignment is consistent. |
| 859 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 860 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 861 | GrScratchKey scratchKey2; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 862 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 863 | REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize()); |
| 864 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); |
| 865 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); |
| 866 | scratchKey = scratchKey1; |
| 867 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 868 | REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); |
| 869 | REPORTER_ASSERT(reporter, scratchKey == scratchKey1); |
| 870 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); |
| 871 | REPORTER_ASSERT(reporter, scratchKey != scratchKey2); |
| 872 | scratchKey = scratchKey2; |
| 873 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 874 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
| 875 | REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
| 876 | REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
| 877 | REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
| 878 | |
| 879 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 880 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 881 | // (following leaks upon test failure). |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 882 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 883 | |
| 884 | // 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] | 885 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 886 | GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 887 | REPORTER_ASSERT(reporter, find != nullptr); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 888 | find->unref(); |
| 889 | |
| 890 | scratchKey2 = scratchKey; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 891 | find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 892 | REPORTER_ASSERT(reporter, find != nullptr); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 893 | REPORTER_ASSERT(reporter, find == a || find == b); |
| 894 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 895 | GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 896 | REPORTER_ASSERT(reporter, find2 != nullptr); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 897 | REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
| 898 | REPORTER_ASSERT(reporter, find2 != find); |
| 899 | find2->unref(); |
| 900 | find->unref(); |
| 901 | } |
| 902 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 903 | static void test_duplicate_unique_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 904 | Mock mock(5, 30000); |
| 905 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 906 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 907 | GrGpu* gpu = context->contextPriv().getGpu(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 908 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 909 | GrUniqueKey key; |
| 910 | make_unique_key<0>(&key, 0); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 911 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 912 | // Create two resources that we will attempt to register with the same unique key. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 913 | TestResource* a = new TestResource(gpu); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 914 | a->setSize(11); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 915 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 916 | // Set key on resource a. |
| 917 | a->resourcePriv().setUniqueKey(key); |
| 918 | REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
| 919 | a->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 920 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 921 | // Make sure that redundantly setting a's key works. |
| 922 | a->resourcePriv().setUniqueKey(key); |
| 923 | REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 924 | a->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 925 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 926 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 927 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 928 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 929 | // Create resource b and set the same key. It should replace a's unique key cache entry. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 930 | TestResource* b = new TestResource(gpu); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 931 | b->setSize(12); |
| 932 | b->resourcePriv().setUniqueKey(key); |
| 933 | REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key)); |
| 934 | b->unref(); |
| 935 | |
| 936 | // Still have two resources because a is still reffed. |
| 937 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 938 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes()); |
| 939 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 940 | |
| 941 | a->unref(); |
| 942 | // Now a should be gone. |
| 943 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 944 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
| 945 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 946 | |
| 947 | // Now replace b with c, but make sure c can start with one unique key and change it to b's key. |
| 948 | // Also make b be unreffed when replacement occurs. |
| 949 | b->unref(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 950 | TestResource* c = new TestResource(gpu); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 951 | GrUniqueKey differentKey; |
| 952 | make_unique_key<0>(&differentKey, 1); |
| 953 | c->setSize(13); |
| 954 | c->resourcePriv().setUniqueKey(differentKey); |
| 955 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 956 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes()); |
| 957 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 958 | // c replaces b and b should be immediately purged. |
| 959 | c->resourcePriv().setUniqueKey(key); |
| 960 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 961 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 962 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 963 | |
| 964 | // c shouldn't be purged because it is ref'ed. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 965 | cache->purgeAllUnlocked(); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 966 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 967 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 968 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 969 | |
| 970 | // Drop the ref on c, it should be kept alive because it has a unique key. |
| 971 | c->unref(); |
| 972 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 973 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 974 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 975 | |
| 976 | // Verify that we can find c, then remove its unique key. It should get purged immediately. |
| 977 | REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key)); |
| 978 | c->resourcePriv().removeUniqueKey(); |
| 979 | c->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 980 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 981 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 982 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 983 | |
| 984 | { |
| 985 | GrUniqueKey key2; |
| 986 | make_unique_key<0>(&key2, 0); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 987 | sk_sp<TestResource> d(new TestResource(gpu)); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 988 | int foo = 4132; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 989 | key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo))); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 990 | d->resourcePriv().setUniqueKey(key2); |
| 991 | } |
| 992 | |
| 993 | GrUniqueKey key3; |
| 994 | make_unique_key<0>(&key3, 0); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 995 | sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3)); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 996 | REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 997 | } |
| 998 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 999 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1000 | Mock mock(5, 30000); |
| 1001 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1002 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1003 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1004 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1005 | GrUniqueKey key1, key2, key3; |
| 1006 | make_unique_key<0>(&key1, 1); |
| 1007 | make_unique_key<0>(&key2, 2); |
| 1008 | make_unique_key<0>(&key3, 3); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1009 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1010 | // Add three resources to the cache. Only c is usable as scratch. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1011 | TestResource* a = new TestResource(gpu); |
| 1012 | TestResource* b = new TestResource(gpu); |
| 1013 | TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1014 | TestResource::kA_SimulatedProperty); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1015 | a->resourcePriv().setUniqueKey(key1); |
| 1016 | b->resourcePriv().setUniqueKey(key2); |
| 1017 | c->resourcePriv().setUniqueKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1018 | a->unref(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1019 | // hold b until *after* the message is sent. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1020 | c->unref(); |
| 1021 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1022 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
| 1023 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
| 1024 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1025 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1026 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1027 | typedef GrUniqueKeyInvalidatedMessage Msg; |
| 1028 | typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1029 | |
| 1030 | // Invalidate two of the three, they should be purged and no longer accessible via their keys. |
| 1031 | Bus::Post(Msg(key1)); |
| 1032 | Bus::Post(Msg(key2)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1033 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1034 | // a should be deleted now, but we still have a ref on b. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1035 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
| 1036 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1037 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1038 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1039 | |
| 1040 | // Invalidate the third. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1041 | Bus::Post(Msg(key3)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1042 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1043 | // we still have a ref on b, c should be recycled as scratch. |
| 1044 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1045 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1046 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1047 | // make b purgeable. It should be immediately deleted since it has no key. |
| 1048 | b->unref(); |
| 1049 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 1050 | |
| 1051 | // Make sure we actually get to c via it's scratch key, before we say goodbye. |
| 1052 | GrScratchKey scratchKey; |
| 1053 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 1054 | GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1055 | REPORTER_ASSERT(reporter, scratch == c); |
| 1056 | SkSafeUnref(scratch); |
| 1057 | |
| 1058 | // Get rid of c. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1059 | cache->purgeAllUnlocked(); |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 1060 | scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1061 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1062 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1063 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 1064 | REPORTER_ASSERT(reporter, !scratch); |
| 1065 | SkSafeUnref(scratch); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1068 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1069 | Mock mock(3, 30000); |
| 1070 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1071 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1072 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1073 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1074 | GrUniqueKey key1, key2; |
| 1075 | make_unique_key<0>(&key1, 1); |
| 1076 | make_unique_key<0>(&key2, 2); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 1077 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1078 | TestResource* a = new TestResource(gpu); |
| 1079 | TestResource* b = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1080 | a->resourcePriv().setUniqueKey(key1); |
| 1081 | b->resourcePriv().setUniqueKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 1082 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1083 | // Make a cycle |
| 1084 | a->setUnrefWhenDestroyed(b); |
| 1085 | b->setUnrefWhenDestroyed(a); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1086 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1087 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 1088 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1089 | a->unref(); |
| 1090 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1091 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1092 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1093 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1094 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1095 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1096 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1097 | // Break the cycle |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1098 | a->setUnrefWhenDestroyed(nullptr); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1099 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 1100 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1101 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1102 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1105 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1106 | GrUniqueKey key1, key2; |
| 1107 | make_unique_key<0>(&key1, 1); |
| 1108 | make_unique_key<0>(&key2, 2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Test changing resources sizes (both increase & decrease). |
| 1111 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1112 | Mock mock(3, 30000); |
| 1113 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1114 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1115 | GrGpu* gpu = context->contextPriv().getGpu(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1116 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1117 | TestResource* a = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1118 | a->resourcePriv().setUniqueKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1119 | a->unref(); |
| 1120 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1121 | TestResource* b = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1122 | b->resourcePriv().setUniqueKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1123 | b->unref(); |
| 1124 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1125 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 1126 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1127 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 1128 | sk_sp<TestResource> find2( |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1129 | static_cast<TestResource*>(cache->findAndRefUniqueResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1130 | find2->setSize(200); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 1131 | sk_sp<TestResource> find1( |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1132 | static_cast<TestResource*>(cache->findAndRefUniqueResource(key1))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1133 | find1->setSize(50); |
| 1134 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1135 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1136 | REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
| 1137 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | // Test increasing a resources size beyond the cache budget. |
| 1141 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1142 | Mock mock(2, 300); |
| 1143 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1144 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1145 | GrGpu* gpu = context->contextPriv().getGpu(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1146 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1147 | TestResource* a = new TestResource(gpu); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1148 | a->setSize(100); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1149 | a->resourcePriv().setUniqueKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1150 | a->unref(); |
| 1151 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1152 | TestResource* b = new TestResource(gpu); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1153 | b->setSize(100); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1154 | b->resourcePriv().setUniqueKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1155 | b->unref(); |
| 1156 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1157 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 1158 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1159 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1160 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 1161 | sk_sp<TestResource> find2(static_cast<TestResource*>( |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1162 | cache->findAndRefUniqueResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1163 | find2->setSize(201); |
| 1164 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1165 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1166 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1167 | REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); |
| 1168 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1169 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1172 | static void test_timestamp_wrap(skiatest::Reporter* reporter) { |
| 1173 | static const int kCount = 50; |
| 1174 | static const int kBudgetCnt = kCount / 2; |
| 1175 | static const int kLockedFreq = 8; |
| 1176 | static const int kBudgetSize = 0x80000000; |
| 1177 | |
| 1178 | SkRandom random; |
| 1179 | |
| 1180 | // Run the test 2*kCount times; |
| 1181 | for (int i = 0; i < 2 * kCount; ++i ) { |
| 1182 | Mock mock(kBudgetCnt, kBudgetSize); |
| 1183 | GrContext* context = mock.context(); |
| 1184 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1185 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1186 | |
| 1187 | // Pick a random number of resources to add before the timestamp will wrap. |
| 1188 | cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1)); |
| 1189 | |
| 1190 | static const int kNumToPurge = kCount - kBudgetCnt; |
| 1191 | |
| 1192 | SkTDArray<int> shouldPurgeIdxs; |
| 1193 | int purgeableCnt = 0; |
| 1194 | SkTDArray<GrGpuResource*> resourcesToUnref; |
| 1195 | |
| 1196 | // Add kCount resources, holding onto resources at random so we have a mix of purgeable and |
| 1197 | // unpurgeable resources. |
| 1198 | for (int j = 0; j < kCount; ++j) { |
| 1199 | GrUniqueKey key; |
| 1200 | make_unique_key<0>(&key, j); |
| 1201 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1202 | TestResource* r = new TestResource(gpu); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1203 | r->resourcePriv().setUniqueKey(key); |
| 1204 | if (random.nextU() % kLockedFreq) { |
| 1205 | // Make this is purgeable. |
| 1206 | r->unref(); |
| 1207 | ++purgeableCnt; |
| 1208 | if (purgeableCnt <= kNumToPurge) { |
| 1209 | *shouldPurgeIdxs.append() = j; |
| 1210 | } |
| 1211 | } else { |
| 1212 | *resourcesToUnref.append() = r; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // Verify that the correct resources were purged. |
| 1217 | int currShouldPurgeIdx = 0; |
| 1218 | for (int j = 0; j < kCount; ++j) { |
| 1219 | GrUniqueKey key; |
| 1220 | make_unique_key<0>(&key, j); |
| 1221 | GrGpuResource* res = cache->findAndRefUniqueResource(key); |
| 1222 | if (currShouldPurgeIdx < shouldPurgeIdxs.count() && |
| 1223 | shouldPurgeIdxs[currShouldPurgeIdx] == j) { |
| 1224 | ++currShouldPurgeIdx; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1225 | REPORTER_ASSERT(reporter, nullptr == res); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1226 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1227 | REPORTER_ASSERT(reporter, nullptr != res); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1228 | } |
| 1229 | SkSafeUnref(res); |
| 1230 | } |
| 1231 | |
| 1232 | for (int j = 0; j < resourcesToUnref.count(); ++j) { |
| 1233 | resourcesToUnref[j]->unref(); |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1238 | static void test_flush(skiatest::Reporter* reporter) { |
| 1239 | Mock mock(1000000, 1000000); |
| 1240 | GrContext* context = mock.context(); |
| 1241 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1242 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1243 | |
| 1244 | // The current cache impl will round the max flush count to the next power of 2. So we choose a |
| 1245 | // power of two here to keep things simpler. |
| 1246 | static const int kFlushCount = 16; |
| 1247 | cache->setLimits(1000000, 1000000, kFlushCount); |
| 1248 | |
| 1249 | { |
| 1250 | // Insert a resource and send a flush notification kFlushCount times. |
| 1251 | for (int i = 0; i < kFlushCount; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1252 | TestResource* r = new TestResource(gpu); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1253 | GrUniqueKey k; |
| 1254 | make_unique_key<1>(&k, i); |
| 1255 | r->resourcePriv().setUniqueKey(k); |
| 1256 | r->unref(); |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 1257 | cache->notifyFlushOccurred(GrResourceCache::kExternal); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // Send flush notifications to the cache. Each flush should purge the oldest resource. |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 1261 | for (int i = 0; i < kFlushCount; ++i) { |
| 1262 | cache->notifyFlushOccurred(GrResourceCache::kExternal); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1263 | REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount()); |
| 1264 | for (int j = 0; j < i; ++j) { |
| 1265 | GrUniqueKey k; |
| 1266 | make_unique_key<1>(&k, j); |
| 1267 | GrGpuResource* r = cache->findAndRefUniqueResource(k); |
| 1268 | REPORTER_ASSERT(reporter, !SkToBool(r)); |
| 1269 | SkSafeUnref(r); |
| 1270 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1274 | cache->purgeAllUnlocked(); |
| 1275 | } |
| 1276 | |
| 1277 | // Do a similar test but where we leave refs on some resources to prevent them from being |
| 1278 | // purged. |
| 1279 | { |
| 1280 | GrGpuResource* refedResources[kFlushCount >> 1]; |
| 1281 | for (int i = 0; i < kFlushCount; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1282 | TestResource* r = new TestResource(gpu); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1283 | GrUniqueKey k; |
| 1284 | make_unique_key<1>(&k, i); |
| 1285 | r->resourcePriv().setUniqueKey(k); |
| 1286 | // Leave a ref on every other resource, beginning with the first. |
| 1287 | if (SkToBool(i & 0x1)) { |
| 1288 | refedResources[i/2] = r; |
| 1289 | } else { |
| 1290 | r->unref(); |
| 1291 | } |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 1292 | cache->notifyFlushOccurred(GrResourceCache::kExternal); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | for (int i = 0; i < kFlushCount; ++i) { |
| 1296 | // Should get a resource purged every other flush. |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 1297 | cache->notifyFlushOccurred(GrResourceCache::kExternal); |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 1298 | REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount()); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | // Unref all the resources that we kept refs on in the first loop. |
| 1302 | for (int i = 0; i < kFlushCount >> 1; ++i) { |
| 1303 | refedResources[i]->unref(); |
| 1304 | } |
| 1305 | |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 1306 | // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for |
| 1307 | // kFlushCount full flushes. |
| 1308 | for (int i = 0; i < kFlushCount + 1; ++i) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1309 | REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount()); |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 1310 | cache->notifyFlushOccurred(GrResourceCache::kExternal); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1311 | } |
| 1312 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1313 | |
| 1314 | cache->purgeAllUnlocked(); |
| 1315 | } |
| 1316 | |
| 1317 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame] | 1318 | |
| 1319 | // Verify that calling flush() on a GrContext with nothing to do will not trigger resource |
| 1320 | // eviction. |
| 1321 | context->flush(); |
| 1322 | for (int i = 0; i < 10; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1323 | TestResource* r = new TestResource(gpu); |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame] | 1324 | GrUniqueKey k; |
| 1325 | make_unique_key<1>(&k, i); |
| 1326 | r->resourcePriv().setUniqueKey(k); |
| 1327 | r->unref(); |
| 1328 | } |
| 1329 | REPORTER_ASSERT(reporter, 10 == cache->getResourceCount()); |
| 1330 | for (int i = 0; i < 10 * kFlushCount; ++i) { |
| 1331 | context->flush(); |
| 1332 | } |
| 1333 | REPORTER_ASSERT(reporter, 10 == cache->getResourceCount()); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1336 | static void test_time_purge(skiatest::Reporter* reporter) { |
| 1337 | Mock mock(1000000, 1000000); |
| 1338 | GrContext* context = mock.context(); |
| 1339 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1340 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1341 | |
| 1342 | static constexpr int kCnts[] = {1, 10, 1024}; |
| 1343 | auto nowish = []() { |
| 1344 | // We sleep so that we ensure we get a value that is greater than the last call to |
| 1345 | // GrStdSteadyClock::now(). |
| 1346 | std::this_thread::sleep_for(GrStdSteadyClock::duration(5)); |
| 1347 | auto result = GrStdSteadyClock::now(); |
| 1348 | // Also sleep afterwards so we don't get this value again. |
| 1349 | std::this_thread::sleep_for(GrStdSteadyClock::duration(5)); |
| 1350 | return result; |
| 1351 | }; |
| 1352 | |
| 1353 | for (int cnt : kCnts) { |
| 1354 | std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps( |
| 1355 | new GrStdSteadyClock::time_point[cnt]); |
| 1356 | { |
| 1357 | // Insert resources and get time points between each addition. |
| 1358 | for (int i = 0; i < cnt; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1359 | TestResource* r = new TestResource(gpu); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1360 | GrUniqueKey k; |
| 1361 | make_unique_key<1>(&k, i); |
| 1362 | r->resourcePriv().setUniqueKey(k); |
| 1363 | r->unref(); |
| 1364 | timeStamps.get()[i] = nowish(); |
| 1365 | } |
| 1366 | |
| 1367 | // Purge based on the time points between resource additions. Each purge should remove |
| 1368 | // the oldest resource. |
| 1369 | for (int i = 0; i < cnt; ++i) { |
| 1370 | cache->purgeResourcesNotUsedSince(timeStamps[i]); |
| 1371 | REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount()); |
| 1372 | for (int j = 0; j < i; ++j) { |
| 1373 | GrUniqueKey k; |
| 1374 | make_unique_key<1>(&k, j); |
| 1375 | GrGpuResource* r = cache->findAndRefUniqueResource(k); |
| 1376 | REPORTER_ASSERT(reporter, !SkToBool(r)); |
| 1377 | SkSafeUnref(r); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1382 | cache->purgeAllUnlocked(); |
| 1383 | } |
| 1384 | |
| 1385 | // Do a similar test but where we leave refs on some resources to prevent them from being |
| 1386 | // purged. |
| 1387 | { |
| 1388 | std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]); |
| 1389 | for (int i = 0; i < cnt; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1390 | TestResource* r = new TestResource(gpu); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1391 | GrUniqueKey k; |
| 1392 | make_unique_key<1>(&k, i); |
| 1393 | r->resourcePriv().setUniqueKey(k); |
| 1394 | // Leave a ref on every other resource, beginning with the first. |
| 1395 | if (SkToBool(i & 0x1)) { |
| 1396 | refedResources.get()[i / 2] = r; |
| 1397 | } else { |
| 1398 | r->unref(); |
| 1399 | } |
| 1400 | timeStamps.get()[i] = nowish(); |
| 1401 | } |
| 1402 | |
| 1403 | for (int i = 0; i < cnt; ++i) { |
| 1404 | // Should get a resource purged every other frame. |
| 1405 | cache->purgeResourcesNotUsedSince(timeStamps[i]); |
| 1406 | REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount()); |
| 1407 | } |
| 1408 | |
| 1409 | // Unref all the resources that we kept refs on in the first loop. |
| 1410 | for (int i = 0; i < (cnt / 2); ++i) { |
| 1411 | refedResources.get()[i]->unref(); |
| 1412 | cache->purgeResourcesNotUsedSince(nowish()); |
| 1413 | REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount()); |
| 1414 | } |
| 1415 | |
| 1416 | cache->purgeAllUnlocked(); |
| 1417 | } |
| 1418 | |
| 1419 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1420 | |
| 1421 | // Verify that calling flush() on a GrContext with nothing to do will not trigger resource |
| 1422 | // eviction |
| 1423 | context->flush(); |
| 1424 | for (int i = 0; i < 10; ++i) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1425 | TestResource* r = new TestResource(gpu); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1426 | GrUniqueKey k; |
| 1427 | make_unique_key<1>(&k, i); |
| 1428 | r->resourcePriv().setUniqueKey(k); |
| 1429 | r->unref(); |
| 1430 | } |
| 1431 | REPORTER_ASSERT(reporter, 10 == cache->getResourceCount()); |
| 1432 | context->flush(); |
| 1433 | REPORTER_ASSERT(reporter, 10 == cache->getResourceCount()); |
| 1434 | cache->purgeResourcesNotUsedSince(nowish()); |
| 1435 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1436 | } |
| 1437 | } |
| 1438 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1439 | static void test_partial_purge(skiatest::Reporter* reporter) { |
| 1440 | Mock mock(6, 100); |
| 1441 | GrContext* context = mock.context(); |
| 1442 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1443 | GrGpu* gpu = context->contextPriv().getGpu(); |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1444 | |
| 1445 | enum TestsCase { |
| 1446 | kOnlyScratch_TestCase = 0, |
| 1447 | kPartialScratch_TestCase = 1, |
| 1448 | kAllScratch_TestCase = 2, |
| 1449 | kPartial_TestCase = 3, |
| 1450 | kAll_TestCase = 4, |
| 1451 | kNone_TestCase = 5, |
| 1452 | kEndTests_TestCase = kNone_TestCase + 1 |
| 1453 | }; |
| 1454 | |
| 1455 | for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) { |
| 1456 | |
| 1457 | GrUniqueKey key1, key2, key3; |
| 1458 | make_unique_key<0>(&key1, 1); |
| 1459 | make_unique_key<0>(&key2, 2); |
| 1460 | make_unique_key<0>(&key3, 3); |
| 1461 | |
| 1462 | // Add three unique resources to the cache. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1463 | TestResource *unique1 = new TestResource(gpu); |
| 1464 | TestResource *unique2 = new TestResource(gpu); |
| 1465 | TestResource *unique3 = new TestResource(gpu); |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1466 | |
| 1467 | unique1->resourcePriv().setUniqueKey(key1); |
| 1468 | unique2->resourcePriv().setUniqueKey(key2); |
| 1469 | unique3->resourcePriv().setUniqueKey(key3); |
| 1470 | |
| 1471 | unique1->setSize(10); |
| 1472 | unique2->setSize(11); |
| 1473 | unique3->setSize(12); |
| 1474 | |
| 1475 | // Add two scratch resources to the cache. |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1476 | TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1477 | TestResource::kA_SimulatedProperty); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1478 | TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes, |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1479 | TestResource::kB_SimulatedProperty); |
| 1480 | scratch1->setSize(13); |
| 1481 | scratch2->setSize(14); |
| 1482 | |
| 1483 | |
| 1484 | REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount()); |
| 1485 | REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes()); |
| 1486 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
| 1487 | |
| 1488 | // Add resources to the purgeable queue |
| 1489 | unique1->unref(); |
| 1490 | scratch1->unref(); |
| 1491 | unique2->unref(); |
| 1492 | scratch2->unref(); |
| 1493 | unique3->unref(); |
| 1494 | |
| 1495 | REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount()); |
| 1496 | REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes()); |
| 1497 | REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes()); |
| 1498 | |
| 1499 | switch(testCase) { |
| 1500 | case kOnlyScratch_TestCase: { |
| 1501 | context->purgeUnlockedResources(14, true); |
| 1502 | REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount()); |
| 1503 | REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes()); |
| 1504 | break; |
| 1505 | } |
| 1506 | case kPartialScratch_TestCase: { |
| 1507 | context->purgeUnlockedResources(3, true); |
| 1508 | REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount()); |
| 1509 | REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes()); |
| 1510 | break; |
| 1511 | } |
| 1512 | case kAllScratch_TestCase: { |
| 1513 | context->purgeUnlockedResources(50, true); |
| 1514 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 1515 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
| 1516 | break; |
| 1517 | } |
| 1518 | case kPartial_TestCase: { |
| 1519 | context->purgeUnlockedResources(13, false); |
| 1520 | REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount()); |
| 1521 | REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes()); |
| 1522 | break; |
| 1523 | } |
| 1524 | case kAll_TestCase: { |
| 1525 | context->purgeUnlockedResources(50, false); |
| 1526 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 1527 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
| 1528 | break; |
| 1529 | } |
| 1530 | case kNone_TestCase: { |
| 1531 | context->purgeUnlockedResources(0, true); |
| 1532 | context->purgeUnlockedResources(0, false); |
| 1533 | REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount()); |
| 1534 | REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes()); |
| 1535 | REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes()); |
| 1536 | break; |
| 1537 | } |
| 1538 | }; |
| 1539 | |
| 1540 | // ensure all are purged before the next |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1541 | context->contextPriv().purgeAllUnlockedResources_ForTesting(); |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1542 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 1543 | REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes()); |
| 1544 | |
| 1545 | } |
| 1546 | } |
| 1547 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1548 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1549 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 1550 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 1551 | // down to 1 byte after creating the resource. |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1552 | static const int kResourceCnt = 2000; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1553 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1554 | Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 1555 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1556 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1557 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1558 | |
| 1559 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1560 | GrUniqueKey key1, key2; |
| 1561 | make_unique_key<1>(&key1, i); |
| 1562 | make_unique_key<2>(&key2, i); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1563 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1564 | TestResource* resource; |
| 1565 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1566 | resource = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1567 | resource->resourcePriv().setUniqueKey(key1); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1568 | resource->setSize(1); |
| 1569 | resource->unref(); |
| 1570 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1571 | resource = new TestResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1572 | resource->resourcePriv().setUniqueKey(key2); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1573 | resource->setSize(1); |
| 1574 | resource->unref(); |
| 1575 | } |
| 1576 | |
| 1577 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 1578 | REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1579 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 1580 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 1581 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
| 1582 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1583 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1584 | GrUniqueKey key1, key2; |
| 1585 | make_unique_key<1>(&key1, i); |
| 1586 | make_unique_key<2>(&key2, i); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1587 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1588 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
| 1589 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1592 | cache->purgeAllUnlocked(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1593 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 1594 | REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1595 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); |
| 1596 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); |
| 1597 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); |
| 1598 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1599 | |
| 1600 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1601 | GrUniqueKey key1, key2; |
| 1602 | make_unique_key<1>(&key1, i); |
| 1603 | make_unique_key<2>(&key2, i); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1604 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1605 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
| 1606 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 1610 | static void test_custom_data(skiatest::Reporter* reporter) { |
| 1611 | GrUniqueKey key1, key2; |
| 1612 | make_unique_key<0>(&key1, 1); |
| 1613 | make_unique_key<0>(&key2, 2); |
| 1614 | int foo = 4132; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 1615 | key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo))); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 1616 | REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132); |
| 1617 | REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr); |
| 1618 | |
| 1619 | // Test that copying a key also takes a ref on its custom data. |
| 1620 | GrUniqueKey key3 = key1; |
| 1621 | REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132); |
| 1622 | } |
| 1623 | |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 1624 | static void test_abandoned(skiatest::Reporter* reporter) { |
| 1625 | Mock mock(10, 300); |
| 1626 | GrContext* context = mock.context(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1627 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 1628 | |
| 1629 | sk_sp<GrGpuResource> resource(new TestResource(gpu)); |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 1630 | context->abandonContext(); |
| 1631 | |
| 1632 | REPORTER_ASSERT(reporter, resource->wasDestroyed()); |
| 1633 | |
| 1634 | // Call all the public methods on resource in the abandoned state. They shouldn't crash. |
| 1635 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 1636 | resource->uniqueID(); |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 1637 | resource->getUniqueKey(); |
| 1638 | resource->wasDestroyed(); |
| 1639 | resource->gpuMemorySize(); |
| 1640 | resource->getContext(); |
| 1641 | |
| 1642 | resource->abandon(); |
| 1643 | resource->resourcePriv().getScratchKey(); |
| 1644 | resource->resourcePriv().isBudgeted(); |
| 1645 | resource->resourcePriv().makeBudgeted(); |
| 1646 | resource->resourcePriv().makeUnbudgeted(); |
| 1647 | resource->resourcePriv().removeScratchKey(); |
| 1648 | GrUniqueKey key; |
| 1649 | make_unique_key<0>(&key, 1); |
| 1650 | resource->resourcePriv().setUniqueKey(key); |
| 1651 | resource->resourcePriv().removeUniqueKey(); |
| 1652 | } |
| 1653 | |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1654 | static void test_tags(skiatest::Reporter* reporter) { |
| 1655 | #ifdef SK_DEBUG |
| 1656 | // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx. |
| 1657 | static constexpr int kLastTagIdx = 10; |
| 1658 | static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2; |
| 1659 | |
| 1660 | Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize); |
| 1661 | GrContext* context = mock.context(); |
| 1662 | GrResourceCache* cache = mock.cache(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1663 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1664 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame^] | 1665 | // tag strings are expected to be long lived |
| 1666 | std::vector<SkString> tagStrings; |
| 1667 | |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1668 | SkString tagStr; |
| 1669 | int tagIdx = 0; |
| 1670 | int currTagCnt = 0; |
| 1671 | |
| 1672 | for (int i = 0; i < kNumResources; ++i, ++currTagCnt) { |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame^] | 1673 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 1674 | sk_sp<GrGpuResource> resource(new TestResource(gpu)); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1675 | GrUniqueKey key; |
| 1676 | if (currTagCnt == tagIdx) { |
| 1677 | tagIdx += 1; |
| 1678 | currTagCnt = 0; |
| 1679 | tagStr.printf("tag%d", tagIdx); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame^] | 1680 | tagStrings.emplace_back(tagStr); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1681 | } |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame^] | 1682 | make_unique_key<1>(&key, i, tagStrings.back().c_str()); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1683 | resource->resourcePriv().setUniqueKey(key); |
| 1684 | } |
| 1685 | SkASSERT(kLastTagIdx == tagIdx); |
| 1686 | SkASSERT(currTagCnt == kLastTagIdx); |
| 1687 | |
| 1688 | // Test i = 0 to exercise unused tag string. |
| 1689 | for (int i = 0; i <= kLastTagIdx; ++i) { |
| 1690 | tagStr.printf("tag%d", i); |
| 1691 | REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i); |
| 1692 | } |
| 1693 | #endif |
| 1694 | } |
| 1695 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 1696 | DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1697 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1698 | test_no_key(reporter); |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 1699 | test_purge_unlocked(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 1700 | test_budgeting(reporter); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1701 | test_unbudgeted(reporter); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1702 | test_unbudgeted_to_scratch(reporter); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1703 | test_duplicate_unique_key(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1704 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1705 | test_remove_scratch_key(reporter); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 1706 | test_scratch_key_consistency(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1707 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1708 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1709 | test_resource_size_changed(reporter); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1710 | test_timestamp_wrap(reporter); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1711 | test_flush(reporter); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 1712 | test_time_purge(reporter); |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 1713 | test_partial_purge(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1714 | test_large_resource_count(reporter); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 1715 | test_custom_data(reporter); |
bsalomon | c6363ef | 2015-09-24 07:07:40 -0700 | [diff] [blame] | 1716 | test_abandoned(reporter); |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 1717 | test_tags(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1720 | //////////////////////////////////////////////////////////////////////////////// |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 1721 | static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 1722 | GrSurfaceDescFlags descFlags, |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1723 | int width, int height, |
| 1724 | int sampleCnt) { |
| 1725 | GrSurfaceDesc desc; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 1726 | desc.fFlags = descFlags; |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1727 | desc.fWidth = width; |
| 1728 | desc.fHeight = height; |
| 1729 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 1730 | desc.fSampleCnt = sampleCnt; |
| 1731 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1732 | return provider->createTexture(desc, SkBudgeted::kYes); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1733 | } |
| 1734 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1735 | static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 1736 | GrSurfaceDescFlags descFlags, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1737 | int width, int height, |
| 1738 | int sampleCnt) { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1739 | GrSurfaceDesc desc; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 1740 | desc.fFlags = descFlags; |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1741 | desc.fWidth = width; |
| 1742 | desc.fHeight = height; |
| 1743 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 1744 | desc.fSampleCnt = sampleCnt; |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1745 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 1746 | auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin |
| 1747 | : kTopLeft_GrSurfaceOrigin; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1748 | |
| 1749 | return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | // Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only, |
| 1753 | // Texture-only, both-RT-and-Texture and MIPmapped |
| 1754 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) { |
| 1755 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1756 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 1757 | GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider(); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1758 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1759 | static const int kSize = 64; |
| 1760 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1761 | // Normal versions |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1762 | { |
| 1763 | sk_sp<GrTexture> tex; |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1764 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1765 | tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1766 | size_t size = tex->gpuMemorySize(); |
| 1767 | REPORTER_ASSERT(reporter, kSize*kSize*4 == size); |
| 1768 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1769 | size_t sampleCount = |
| 1770 | (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig); |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1771 | if (sampleCount >= 4) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1772 | tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1773 | sampleCount); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1774 | size = tex->gpuMemorySize(); |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1775 | REPORTER_ASSERT(reporter, |
| 1776 | kSize*kSize*4 == size || // msaa4 failed |
| 1777 | kSize*kSize*4*sampleCount == size || // auto-resolving |
| 1778 | kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1779 | } |
| 1780 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1781 | tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1782 | size = tex->gpuMemorySize(); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1783 | REPORTER_ASSERT(reporter, kSize*kSize*4 == size); |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1784 | } |
| 1785 | |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1786 | |
| 1787 | // Mipmapped versions |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 1788 | if (context->caps()->mipMapSupport()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1789 | sk_sp<GrTextureProxy> proxy; |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1790 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1791 | proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1792 | size_t size = proxy->gpuMemorySize(); |
| 1793 | REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size); |
| 1794 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1795 | size_t sampleCount = |
| 1796 | (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig); |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1797 | if (sampleCount >= 4) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1798 | proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1799 | sampleCount); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1800 | size = proxy->gpuMemorySize(); |
| 1801 | REPORTER_ASSERT(reporter, |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 1802 | kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed |
| 1803 | kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving |
| 1804 | kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1805 | } |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 1806 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1807 | proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 1808 | size = proxy->gpuMemorySize(); |
| 1809 | REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size); |
| 1810 | } |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1813 | #endif |