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