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