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