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