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