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