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