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