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