blob: 3e4efb108fefceb03359aec14230cda56e6ea09e [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
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
bsalomon3f324322015-04-08 11:01:54 -07008// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourceCacheAccess.h"
17#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTarget.h"
19#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070023#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080024#include "SkGr.h"
25#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070026#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000027#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028
29static const int gWidth = 640;
30static const int gHeight = 480;
31
32////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070033DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070034 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080035 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040036 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080037 desc.fFlags = kRenderTarget_GrSurfaceFlag;
38 desc.fWidth = gWidth;
39 desc.fHeight = gHeight;
40 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070041 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080042 SkCanvas* canvas = surface->getCanvas();
43
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000044 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
45
46 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000047 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048 src.eraseColor(SK_ColorBLACK);
49 size_t srcSize = src.getSize();
50
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000051 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070052 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053
54 int oldMaxNum;
55 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000056 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000057
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000058 // 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.org95c20032014-05-09 14:29:32 +000061 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062
63 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000064 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000065
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.org95c20032014-05-09 14:29:32 +000073 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070074 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000075
76 // we should never go over the size limit
77 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
78 }
79
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000080 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000081}
82
bsalomon11abd8d2016-10-14 08:13:48 -070083static 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
92DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
93 ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070094 GrContext* context = ctxInfo.grContext();
bsalomon02a44a42015-02-19 09:09:00 -080095 GrSurfaceDesc smallDesc;
96 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -040097 smallDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -080098 smallDesc.fWidth = 4;
99 smallDesc.fHeight = 4;
100 smallDesc.fSampleCnt = 0;
101
bsalomond309e7a2015-04-30 14:18:54 -0700102 GrTextureProvider* cache = context->textureProvider();
egdanielec00d942015-09-14 12:56:10 -0700103 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800104 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomon5ec26ae2016-02-25 08:33:02 -0800105 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800106 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700107 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800108 }
109
bsalomon5ec26ae2016-02-25 08:33:02 -0800110 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800111 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700112 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 }
114
egdaniel8dc7c3a2015-04-16 11:22:42 -0700115 REPORTER_ASSERT(reporter,
116 smallRT0 && smallRT1 &&
117 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700118 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
119 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800120
121 // An unbudgeted RT with the same desc should also share.
bsalomon5ec26ae2016-02-25 08:33:02 -0800122 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800123 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700124 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800125 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700126 REPORTER_ASSERT(reporter,
127 smallRT0 && smallRT2 &&
128 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700129 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
130 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800131
132 // An RT with a much larger size should not share.
133 GrSurfaceDesc bigDesc;
134 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400135 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800136 bigDesc.fWidth = 400;
137 bigDesc.fHeight = 200;
138 bigDesc.fSampleCnt = 0;
bsalomon5ec26ae2016-02-25 08:33:02 -0800139 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700141 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700143 REPORTER_ASSERT(reporter,
144 smallRT0 && bigRT &&
145 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700146 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
147 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800148
bsalomon76228632015-05-29 08:02:10 -0700149 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700150 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800151 GrSurfaceDesc smallMSAADesc = smallDesc;
152 smallMSAADesc.fSampleCnt = 4;
bsalomon5ec26ae2016-02-25 08:33:02 -0800153 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800154 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700155 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 }
bsalomonb602d4d2015-02-19 12:05:58 -0800157#ifdef SK_BUILD_FOR_ANDROID
158 if (!smallMSAART0) {
159 // The nexus player seems to fail to create MSAA textures.
160 return;
161 }
162#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800163 REPORTER_ASSERT(reporter,
164 smallRT0 && smallMSAART0 &&
165 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700166 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
167 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800168 // A second MSAA RT should share with the first MSAA RT.
bsalomon5ec26ae2016-02-25 08:33:02 -0800169 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800170 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700171 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800172 }
173 REPORTER_ASSERT(reporter,
174 smallMSAART0 && smallMSAART1 &&
175 smallMSAART0->asRenderTarget() &&
176 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700177 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
178 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800179 // 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.).
bsalomon76228632015-05-29 08:02:10 -0700181 if (context->caps()->maxSampleCount() >= 8 &&
182 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700183 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800184 smallMSAADesc.fSampleCnt = 8;
bsalomon5ec26ae2016-02-25 08:33:02 -0800185 smallMSAART1.reset(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
186 SkAutoTUnref<GrTexture> smallMSAART1(
187 cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800188 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700189 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800190 }
191 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700192 smallMSAART0 && smallMSAART1 &&
193 smallMSAART0->asRenderTarget() &&
194 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700195 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
196 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800197 }
198 }
199}
200
bsalomon68d91342016-04-12 09:59:58 -0700201DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700202 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800203 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700204 // this test is only valid for GL
205 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700206 return;
207 }
208
bsalomon091f60c2015-11-10 11:54:56 -0800209 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700210 static const int kW = 100;
211 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700212
bsalomon091f60c2015-11-10 11:54:56 -0800213 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
214 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700215
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216 context->resetContext();
217
218 GrBackendTextureDesc desc;
219 desc.fConfig = kBGRA_8888_GrPixelConfig;
220 desc.fWidth = kW;
221 desc.fHeight = kH;
222
bsalomon091f60c2015-11-10 11:54:56 -0800223 desc.fTextureHandle = texHandles[0];
bungeman6bd52842016-10-27 09:30:08 -0700224 sk_sp<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
225 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700226
bsalomon091f60c2015-11-10 11:54:56 -0800227 desc.fTextureHandle = texHandles[1];
bungeman6bd52842016-10-27 09:30:08 -0700228 sk_sp<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
229 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
bungeman6bd52842016-10-27 09:30:08 -0700231 printf("\nborrowed: %p, adopted: %p\n", borrowed.get(), adopted.get());
mtklein5f939ab2016-03-16 10:28:35 -0700232 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
233 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700234 return;
235 }
236
halcanary96fcdcc2015-08-27 07:41:13 -0700237 borrowed.reset(nullptr);
238 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700239
240 context->flush();
241
bsalomon091f60c2015-11-10 11:54:56 -0800242 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
243 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244
245 REPORTER_ASSERT(reporter, borrowedIsAlive);
246 REPORTER_ASSERT(reporter, !adoptedIsAlive);
247
bsalomon67d76202015-11-11 12:40:42 -0800248 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
249 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700250
251 context->resetContext();
252}
253
bsalomon6d3fe022014-07-25 08:35:45 -0700254class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800255 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000256public:
robertphillips6e83ac72015-08-13 05:19:14 -0700257 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700258
bsalomon1c60dfe2015-01-21 09:32:40 -0800259 /** Property that distinctly categorizes the resource.
260 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800261 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800262
kkinnunen2e6055b2016-04-22 01:48:29 -0700263 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
264 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700265 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800266 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 , fProperty(kA_SimulatedProperty)
268 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800269 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700270 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800271 }
272
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
274 SimulatedProperty property) {
275 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800276 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700277 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
278 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000279 }
280
281 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800282 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800283 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284 }
285
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000286 void setSize(size_t size) {
287 fSize = size;
288 this->didChangeGpuMemorySize();
289 }
290
bsalomon33435572014-11-05 14:47:41 -0800291 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000292
bsalomon71cb0c22014-11-14 12:10:14 -0800293 void setUnrefWhenDestroyed(TestResource* resource) {
294 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000295 }
296
bsalomon1c60dfe2015-01-21 09:32:40 -0800297 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
298 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
299 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800300 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
301 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800302 }
303 }
304
305 static size_t ExpectedScratchKeySize() {
306 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
307 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000308private:
bsalomon24db3b12015-01-23 04:24:04 -0800309 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800310
kkinnunen2e6055b2016-04-22 01:48:29 -0700311 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
312 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700313 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 , fProperty(property)
316 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800317 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700318 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 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800336 }
337
mtklein36352bf2015-03-25 18:17:31 -0700338 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800339
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000340 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000341 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800342 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800343 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700344 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700345 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000346};
bsalomon33435572014-11-05 14:47:41 -0800347int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000348
bsalomonc2f35b72015-01-23 07:19:22 -0800349class Mock {
350public:
351 Mock(int maxCnt, size_t maxBytes) {
352 fContext.reset(GrContext::CreateMockContext());
353 SkASSERT(fContext);
354 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800355 GrResourceCache* cache = fContext->getResourceCache();
356 cache->purgeAllUnlocked();
357 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800358 }
bsalomonc2f35b72015-01-23 07:19:22 -0800359
bsalomon0ea80f42015-02-11 10:49:59 -0800360 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800361
362 GrContext* context() { return fContext; }
363
364private:
365 SkAutoTUnref<GrContext> fContext;
366};
367
368static void test_no_key(skiatest::Reporter* reporter) {
369 Mock mock(10, 30000);
370 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800371 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800372
373 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700374 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());
bsalomon71cb0c22014-11-14 12:10:14 -0800378 a->setSize(11);
379 b->setSize(12);
380 c->setSize(13);
381 d->setSize(14);
382
383 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800384 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800385 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800386 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800387
388 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
391
bsalomon8718aaf2015-02-19 07:24:21 -0800392 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800393
394 a->unref();
395 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800396 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800397 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800398 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399
400 c->unref();
401 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800403 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800404 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800405
406 d->unref();
407 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800408 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
409 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 b->unref();
412 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800413 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
414 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415}
416
bsalomon24db3b12015-01-23 04:24:04 -0800417// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800418template <int> static void make_unique_key(GrUniqueKey* key, int data) {
419 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
420 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800421 builder[0] = data;
422}
423
bsalomon84c8e622014-11-17 09:33:27 -0800424static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800425 Mock mock(10, 300);
426 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800427 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 GrUniqueKey uniqueKey;
430 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800431
bsalomon8718aaf2015-02-19 07:24:21 -0800432 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800433 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700434 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800435 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700436 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800437 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800438 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700439 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800440 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700441 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700442 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800443 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800444
bsalomon8718aaf2015-02-19 07:24:21 -0800445 // Make sure we can't add a unique key to the wrapped resource
446 GrUniqueKey uniqueKey2;
447 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800448 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700449 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800450
451 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800452 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800453 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800454 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800455 cache->getResourceBytes());
456 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800458 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800459
bsalomon63c992f2015-01-23 12:47:59 -0800460 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800461 cache->purgeAllUnlocked();
462 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800463 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800464 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800465 cache->getResourceBytes());
466 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800467 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800468 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800469
470 // Unreffing the wrapped resource should free it right away.
471 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800472 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800473 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800474 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800475
bsalomon84c8e622014-11-17 09:33:27 -0800476 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700477 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800478 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800479 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 cache->purgeAllUnlocked();
481 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800482 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800483 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
484 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
485 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800486
487 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800488 cache->purgeAllUnlocked();
489 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800490 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800491 cache->getResourceBytes());
492 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
493 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800494
495 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800496 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());
bsalomon84c8e622014-11-17 09:33:27 -0800500
501 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800502 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());
bsalomondace19e2014-11-17 07:34:06 -0800506}
507
bsalomon5236cf42015-01-14 10:42:08 -0800508static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800509 Mock mock(10, 30000);
510 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800511 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800512
bsalomon8718aaf2015-02-19 07:24:21 -0800513 GrUniqueKey uniqueKey;
514 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800515
516 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800517 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800518 TestResource* wrapped;
519 TestResource* unbudgeted;
520
521 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700522 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
523 TestResource::kB_SimulatedProperty);
524
bsalomon5236cf42015-01-14 10:42:08 -0800525 scratch->setSize(10);
526 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800527 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());
bsalomon5236cf42015-01-14 10:42:08 -0800531
halcanary385fe4d2015-08-26 13:07:48 -0700532 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800533 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800534 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800535 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800536 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());
bsalomon5236cf42015-01-14 10:42:08 -0800540
bsalomon0ea80f42015-02-11 10:49:59 -0800541 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700542 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800543 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());
bsalomon5236cf42015-01-14 10:42:08 -0800547
548 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800549 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());
bsalomon5236cf42015-01-14 10:42:08 -0800553
kkinnunen2e6055b2016-04-22 01:48:29 -0700554 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800555 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());
bsalomon5236cf42015-01-14 10:42:08 -0800559
560 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800561 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());
bsalomon5236cf42015-01-14 10:42:08 -0800565
bsalomon0ea80f42015-02-11 10:49:59 -0800566 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());
bsalomon5236cf42015-01-14 10:42:08 -0800571}
572
bsalomon3582d3e2015-02-13 14:20:05 -0800573// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
574void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
575/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800576 Mock mock(10, 300);
577 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800578 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800579
580 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700581 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
582 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800583 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800584 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800585
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.
bsalomon3582d3e2015-02-13 14:20:05 -0800589 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800590 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800591 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700592 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800593 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());
bsalomonc2f35b72015-01-23 07:19:22 -0800597
598 // Once it is unrefed, it should become available as scratch.
599 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800600 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());
robertphillips6e83ac72015-08-13 05:19:14 -0700604 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800605 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800606 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800607 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800608 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800609
610 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700611 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800612 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800613 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800614 } else {
615 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800616 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800617 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());
bsalomon3582d3e2015-02-13 14:20:05 -0800621 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800622 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800623 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800624
625 // now when it is unrefed it should die since it has no key.
626 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800627 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());
bsalomonc2f35b72015-01-23 07:19:22 -0800631 }
bsalomon8b79d232014-11-10 10:19:06 -0800632 }
bsalomonc2f35b72015-01-23 07:19:22 -0800633}
634
635static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
636 Mock mock(5, 30000);
637 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800638 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800639
bsalomon8b79d232014-11-10 10:19:06 -0800640 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800641 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700642 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800643 TestResource::kB_SimulatedProperty);
644 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700645 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800646 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800647 a->setSize(11);
648 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800649 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800650 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800651 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700652 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800653
654 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800655 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800656
bsalomon0ea80f42015-02-11 10:49:59 -0800657 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800658 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800659 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
660 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800661 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800662 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800663
bsalomon63c992f2015-01-23 12:47:59 -0800664 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800665 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800666 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800667 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800668
669 // Unref but don't purge
670 a->unref();
671 b->unref();
672 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800673 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800674
bsalomon63c992f2015-01-23 12:47:59 -0800675 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800676 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800677 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800678 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
679 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800680}
681
bsalomon10e23ca2014-11-25 05:52:06 -0800682static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800683 Mock mock(5, 30000);
684 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800685 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800686
bsalomon10e23ca2014-11-25 05:52:06 -0800687 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700688 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800689 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700690 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800691 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800692 a->unref();
693 b->unref();
694
bsalomon1c60dfe2015-01-21 09:32:40 -0800695 GrScratchKey scratchKey;
696 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800697 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800698 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700699 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800700
bsalomon0ea80f42015-02-11 10:49:59 -0800701 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800702 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800703 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800704 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
705 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800706
707 // Find the first resource and remove its scratch key
708 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700709 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800710 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800711 // It's still alive, but not cached by scratch key anymore
712 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800713 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
714 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800715
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());
bsalomon0ea80f42015-02-11 10:49:59 -0800719 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
720 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800721
722 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700723 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800724 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800725 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800726 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
727 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800728
729 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800730 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800731 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800732 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
733 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800734
735 find->unref();
736 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800737 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
738 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800739}
740
bsalomon1c60dfe2015-01-21 09:32:40 -0800741static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800742 Mock mock(5, 30000);
743 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800744 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800745
746 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700747 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800748 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700749 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800750 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800751 a->unref();
752 b->unref();
753
754 GrScratchKey scratchKey;
755 // Ensure that scratch key comparison and assignment is consistent.
756 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800757 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800759 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800760 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.
bsalomon23e619c2015-02-06 11:54:28 -0800777 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800778 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700779 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800780
781 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800782 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700783 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700784 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800785 find->unref();
786
787 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700788 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700789 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800790 REPORTER_ASSERT(reporter, find == a || find == b);
791
robertphillips6e83ac72015-08-13 05:19:14 -0700792 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700793 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800794 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
795 REPORTER_ASSERT(reporter, find2 != find);
796 find2->unref();
797 find->unref();
798}
799
bsalomon8718aaf2015-02-19 07:24:21 -0800800static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800801 Mock mock(5, 30000);
802 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800803 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000804
bsalomon8718aaf2015-02-19 07:24:21 -0800805 GrUniqueKey key;
806 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700807
bsalomon8718aaf2015-02-19 07:24:21 -0800808 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700809 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800810 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700811
bsalomonf99e9612015-02-19 08:24:16 -0800812 // Set key on resource a.
813 a->resourcePriv().setUniqueKey(key);
814 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
815 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800816
bsalomonf99e9612015-02-19 08:24:16 -0800817 // Make sure that redundantly setting a's key works.
818 a->resourcePriv().setUniqueKey(key);
819 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800820 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800821 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
822 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800823 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
824
bsalomonf99e9612015-02-19 08:24:16 -0800825 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700826 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800827 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();
halcanary385fe4d2015-08-26 13:07:48 -0700846 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800847 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.
bsalomon0ea80f42015-02-11 10:49:59 -0800861 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800862 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();
bsalomon0ea80f42015-02-11 10:49:59 -0800876 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
877 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800878 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700879
880 {
881 GrUniqueKey key2;
882 make_unique_key<0>(&key2, 0);
halcanary385fe4d2015-08-26 13:07:48 -0700883 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700884 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700885 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700886 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.orgc6658042014-01-15 23:09:01 +0000893}
894
bsalomon8b79d232014-11-10 10:19:06 -0800895static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800896 Mock mock(5, 30000);
897 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800898 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800899
bsalomon8718aaf2015-02-19 07:24:21 -0800900 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);
mtklein5f939ab2016-03-16 10:28:35 -0700904
bsalomon23e619c2015-02-06 11:54:28 -0800905 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700906 TestResource* a = new TestResource(context->getGpu());
907 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700908 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800909 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800910 a->resourcePriv().setUniqueKey(key1);
911 b->resourcePriv().setUniqueKey(key2);
912 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800913 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800914 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800915 c->unref();
916
bsalomon8718aaf2015-02-19 07:24:21 -0800917 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
918 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
919 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800920 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800921
bsalomon8718aaf2015-02-19 07:24:21 -0800922 typedef GrUniqueKeyInvalidatedMessage Msg;
923 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800924
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));
bsalomon0ea80f42015-02-11 10:49:59 -0800928 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800929 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800930 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
931 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800932 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800933 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800934
935 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800936 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800937 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800938 // we still have a ref on b, c should be recycled as scratch.
939 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800940 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800941
bsalomon23e619c2015-02-06 11:54:28 -0800942 // 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);
robertphillips6e83ac72015-08-13 05:19:14 -0700949 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800950 REPORTER_ASSERT(reporter, scratch == c);
951 SkSafeUnref(scratch);
952
953 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800954 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700955 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800956 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800957 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
958 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800959 REPORTER_ASSERT(reporter, !scratch);
960 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800961}
962
bsalomon71cb0c22014-11-14 12:10:14 -0800963static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800964 Mock mock(3, 30000);
965 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800966 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800967
bsalomon8718aaf2015-02-19 07:24:21 -0800968 GrUniqueKey key1, key2;
969 make_unique_key<0>(&key1, 1);
970 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000971
halcanary385fe4d2015-08-26 13:07:48 -0700972 TestResource* a = new TestResource(context->getGpu());
973 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800974 a->resourcePriv().setUniqueKey(key1);
975 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800976
bsalomonc2f35b72015-01-23 07:19:22 -0800977 // Make a cycle
978 a->setUnrefWhenDestroyed(b);
979 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800980
bsalomonc2f35b72015-01-23 07:19:22 -0800981 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800982
bsalomonc2f35b72015-01-23 07:19:22 -0800983 a->unref();
984 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800985
bsalomonc2f35b72015-01-23 07:19:22 -0800986 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800987
bsalomon0ea80f42015-02-11 10:49:59 -0800988 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800989 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800990
bsalomonc2f35b72015-01-23 07:19:22 -0800991 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700992 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800993 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800994
bsalomon0ea80f42015-02-11 10:49:59 -0800995 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800996 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000997}
998
bsalomon8b79d232014-11-10 10:19:06 -0800999static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001000 GrUniqueKey key1, key2;
1001 make_unique_key<0>(&key1, 1);
1002 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001003
1004 // Test changing resources sizes (both increase & decrease).
1005 {
bsalomonc2f35b72015-01-23 07:19:22 -08001006 Mock mock(3, 30000);
1007 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001008 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001009
halcanary385fe4d2015-08-26 13:07:48 -07001010 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001011 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001012 a->unref();
1013
halcanary385fe4d2015-08-26 13:07:48 -07001014 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001015 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001016 b->unref();
1017
bsalomon0ea80f42015-02-11 10:49:59 -08001018 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1019 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001020 {
bsalomon8718aaf2015-02-19 07:24:21 -08001021 SkAutoTUnref<TestResource> find2(
1022 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001023 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -08001024 SkAutoTUnref<TestResource> find1(
1025 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001026 find1->setSize(50);
1027 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001028
bsalomon0ea80f42015-02-11 10:49:59 -08001029 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1030 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001031 }
1032
1033 // Test increasing a resources size beyond the cache budget.
1034 {
bsalomonc2f35b72015-01-23 07:19:22 -08001035 Mock mock(2, 300);
1036 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001037 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001038
halcanary385fe4d2015-08-26 13:07:48 -07001039 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001040 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001041 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001042 a->unref();
1043
halcanary385fe4d2015-08-26 13:07:48 -07001044 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001045 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001046 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001047 b->unref();
1048
bsalomon0ea80f42015-02-11 10:49:59 -08001049 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1050 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001051
bsalomon8b79d232014-11-10 10:19:06 -08001052 {
bsalomon8718aaf2015-02-19 07:24:21 -08001053 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1054 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001055 find2->setSize(201);
1056 }
bsalomon8718aaf2015-02-19 07:24:21 -08001057 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001058
bsalomon0ea80f42015-02-11 10:49:59 -08001059 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1060 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001061 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062}
1063
bsalomonddf30e62015-02-19 11:38:44 -08001064static 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
halcanary385fe4d2015-08-26 13:07:48 -07001093 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001094 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;
halcanary96fcdcc2015-08-27 07:41:13 -07001116 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001117 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001118 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001119 }
1120 SkSafeUnref(res);
1121 }
1122
1123 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1124 resourcesToUnref[j]->unref();
1125 }
1126 }
1127}
1128
bsalomon3f324322015-04-08 11:01:54 -07001129static 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) {
halcanary385fe4d2015-08-26 13:07:48 -07001142 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001143 GrUniqueKey k;
1144 make_unique_key<1>(&k, i);
1145 r->resourcePriv().setUniqueKey(k);
1146 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001147 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001148 }
1149
1150 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001151 for (int i = 0; i < kFlushCount; ++i) {
1152 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001153 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 }
bsalomon3f324322015-04-08 11:01:54 -07001161 }
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) {
halcanary385fe4d2015-08-26 13:07:48 -07001172 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001173 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 }
bsalomonb77a9072016-09-07 10:02:04 -07001182 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001183 }
1184
1185 for (int i = 0; i < kFlushCount; ++i) {
1186 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001187 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001188 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001189 }
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
bsalomone2e87f32016-09-22 12:42:11 -07001196 // 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) {
bsalomon3f324322015-04-08 11:01:54 -07001199 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001200 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001201 }
1202 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1203
1204 cache->purgeAllUnlocked();
1205 }
1206
1207 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001208
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());
bsalomon3f324322015-04-08 11:01:54 -07001224}
1225
bsalomon10e23ca2014-11-25 05:52:06 -08001226static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001227 // 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.
bsalomonc2f35b72015-01-23 07:19:22 -08001230 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001231
bsalomonc2f35b72015-01-23 07:19:22 -08001232 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1233 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001234 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001235
1236 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001237 GrUniqueKey key1, key2;
1238 make_unique_key<1>(&key1, i);
1239 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001240
bsalomon24db3b12015-01-23 04:24:04 -08001241 TestResource* resource;
1242
halcanary385fe4d2015-08-26 13:07:48 -07001243 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001244 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001245 resource->setSize(1);
1246 resource->unref();
1247
halcanary385fe4d2015-08-26 13:07:48 -07001248 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001249 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001250 resource->setSize(1);
1251 resource->unref();
1252 }
1253
1254 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001255 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);
bsalomon10e23ca2014-11-25 05:52:06 -08001259 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001260 GrUniqueKey key1, key2;
1261 make_unique_key<1>(&key1, i);
1262 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001263
bsalomon8718aaf2015-02-19 07:24:21 -08001264 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1265 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001266 }
1267
bsalomon0ea80f42015-02-11 10:49:59 -08001268 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001269 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001270 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);
bsalomon10e23ca2014-11-25 05:52:06 -08001274
1275 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001276 GrUniqueKey key1, key2;
1277 make_unique_key<1>(&key1, i);
1278 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001279
bsalomon8718aaf2015-02-19 07:24:21 -08001280 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1281 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001282 }
1283}
1284
senorblanco84cd6212015-08-04 10:01:58 -07001285static 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;
bungeman38d909e2016-08-02 14:40:46 -07001290 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001291 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
bsalomonc6363ef2015-09-24 07:07:40 -07001299static void test_abandoned(skiatest::Reporter* reporter) {
1300 Mock mock(10, 300);
1301 GrContext* context = mock.context();
Brian Salomon89438a12015-09-24 13:22:45 -04001302 SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001303 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
robertphillips8abb3702016-08-31 14:04:06 -07001309 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001310 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
kkinnunen15302832015-12-01 04:35:26 -08001327DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001328 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001329 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001330 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001331 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001332 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001333 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001334 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001335 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001336 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001337 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001338 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001339 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001340 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001341 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001342 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001343 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001344 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001345}
1346
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001347#endif