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