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