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