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