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