blob: 784ae7e94db5efc40bdd0f03bcb2bdfb3f211412 [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////////////////////////////////////////////////////////////////////////////////
kkinnunen15302832015-12-01 04:35:26 -080033DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, context) {
34 GrSurfaceDesc desc;
35 desc.fConfig = kSkia8888_GrPixelConfig;
36 desc.fFlags = kRenderTarget_GrSurfaceFlag;
37 desc.fWidth = gWidth;
38 desc.fHeight = gHeight;
39 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
40 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
bsalomon5ec26ae2016-02-25 08:33:02 -080041 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
kkinnunen15302832015-12-01 04:35:26 -080083DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheStencilBuffers, reporter, context) {
bsalomon02a44a42015-02-19 09:09:00 -080084 GrSurfaceDesc smallDesc;
85 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
86 smallDesc.fConfig = kSkia8888_GrPixelConfig;
87 smallDesc.fWidth = 4;
88 smallDesc.fHeight = 4;
89 smallDesc.fSampleCnt = 0;
90
bsalomond309e7a2015-04-30 14:18:54 -070091 GrTextureProvider* cache = context->textureProvider();
egdanielec00d942015-09-14 12:56:10 -070092 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -080093 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomon5ec26ae2016-02-25 08:33:02 -080094 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -080095 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -070096 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -080097 }
98
bsalomon5ec26ae2016-02-25 08:33:02 -080099 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800100 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700101 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800102 }
103
egdaniel8dc7c3a2015-04-16 11:22:42 -0700104 REPORTER_ASSERT(reporter,
105 smallRT0 && smallRT1 &&
106 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700107 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
108 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800109
110 // An unbudgeted RT with the same desc should also share.
bsalomon5ec26ae2016-02-25 08:33:02 -0800111 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800112 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700113 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800114 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700115 REPORTER_ASSERT(reporter,
116 smallRT0 && smallRT2 &&
117 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700118 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
119 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800120
121 // An RT with a much larger size should not share.
122 GrSurfaceDesc bigDesc;
123 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
124 bigDesc.fConfig = kSkia8888_GrPixelConfig;
125 bigDesc.fWidth = 400;
126 bigDesc.fHeight = 200;
127 bigDesc.fSampleCnt = 0;
bsalomon5ec26ae2016-02-25 08:33:02 -0800128 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800129 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700130 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800131 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700132 REPORTER_ASSERT(reporter,
133 smallRT0 && bigRT &&
134 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700135 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
136 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800137
bsalomon76228632015-05-29 08:02:10 -0700138 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700139 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800140 GrSurfaceDesc smallMSAADesc = smallDesc;
141 smallMSAADesc.fSampleCnt = 4;
bsalomon5ec26ae2016-02-25 08:33:02 -0800142 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800143 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700144 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800145 }
bsalomonb602d4d2015-02-19 12:05:58 -0800146#ifdef SK_BUILD_FOR_ANDROID
147 if (!smallMSAART0) {
148 // The nexus player seems to fail to create MSAA textures.
149 return;
150 }
151#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800152 REPORTER_ASSERT(reporter,
153 smallRT0 && smallMSAART0 &&
154 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700155 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
156 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800157 // A second MSAA RT should share with the first MSAA RT.
bsalomon5ec26ae2016-02-25 08:33:02 -0800158 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800159 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700160 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800161 }
162 REPORTER_ASSERT(reporter,
163 smallMSAART0 && smallMSAART1 &&
164 smallMSAART0->asRenderTarget() &&
165 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700166 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
167 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800168 // But not one with a larger sample count should not. (Also check that the request for 4
169 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700170 if (context->caps()->maxSampleCount() >= 8 &&
171 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700172 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800173 smallMSAADesc.fSampleCnt = 8;
bsalomon5ec26ae2016-02-25 08:33:02 -0800174 smallMSAART1.reset(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
175 SkAutoTUnref<GrTexture> smallMSAART1(
176 cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800177 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700178 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800179 }
180 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700181 smallMSAART0 && smallMSAART1 &&
182 smallMSAART0->asRenderTarget() &&
183 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700184 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
185 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800186 }
187 }
188}
189
kkinnunen15302832015-12-01 04:35:26 -0800190DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, context) {
bsalomone63ffef2016-02-05 07:17:34 -0800191 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700192 // this test is only valid for GL
193 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700194 return;
195 }
196
bsalomon091f60c2015-11-10 11:54:56 -0800197 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700198 static const int kW = 100;
199 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700200
bsalomon091f60c2015-11-10 11:54:56 -0800201 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
202 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700203
bsalomon6dc6f5f2015-06-18 09:12:16 -0700204 context->resetContext();
205
206 GrBackendTextureDesc desc;
207 desc.fConfig = kBGRA_8888_GrPixelConfig;
208 desc.fWidth = kW;
209 desc.fHeight = kH;
210
bsalomon091f60c2015-11-10 11:54:56 -0800211 desc.fTextureHandle = texHandles[0];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700213 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700214
bsalomon091f60c2015-11-10 11:54:56 -0800215 desc.fTextureHandle = texHandles[1];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700217 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218
mtklein5f939ab2016-03-16 10:28:35 -0700219 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
220 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221 return;
222 }
223
halcanary96fcdcc2015-08-27 07:41:13 -0700224 borrowed.reset(nullptr);
225 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700226
227 context->flush();
228
bsalomon091f60c2015-11-10 11:54:56 -0800229 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
230 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
232 REPORTER_ASSERT(reporter, borrowedIsAlive);
233 REPORTER_ASSERT(reporter, !adoptedIsAlive);
234
bsalomon67d76202015-11-11 12:40:42 -0800235 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
236 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700237
238 context->resetContext();
239}
240
bsalomon6d3fe022014-07-25 08:35:45 -0700241class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800242 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000243public:
robertphillips6e83ac72015-08-13 05:19:14 -0700244 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700245
bsalomon1c60dfe2015-01-21 09:32:40 -0800246 /** Property that distinctly categorizes the resource.
247 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800248 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800249
bsalomon5236cf42015-01-14 10:42:08 -0800250 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
251 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700252 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800253 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800254 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800255 ++fNumAlive;
256 this->registerWithCache();
257 }
258
259 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
260 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700261 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800262 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800263 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800264 ++fNumAlive;
265 this->registerWithCache();
266 }
267
bsalomon8b79d232014-11-10 10:19:06 -0800268 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800269 : INHERITED(gpu, kCached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700270 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800271 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800272 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800273 ++fNumAlive;
274 this->registerWithCache();
275 }
276
bsalomon23e619c2015-02-06 11:54:28 -0800277 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
halcanary385fe4d2015-08-26 13:07:48 -0700278 return new TestResource(gpu, property, cached, kScratchConstructor);
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 }
308
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000309private:
bsalomon24db3b12015-01-23 04:24:04 -0800310 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800311
bsalomonc2f35b72015-01-23 07:19:22 -0800312 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
313 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700314 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 , fSize(kDefaultSize)
316 , fProperty(property) {
317 GrScratchKey scratchKey;
318 ComputeScratchKey(fProperty, &scratchKey);
319 this->setScratchKey(scratchKey);
320 ++fNumAlive;
321 this->registerWithCache();
322 }
323
mtklein36352bf2015-03-25 18:17:31 -0700324 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800325
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000326 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000327 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800328 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800329 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700330 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000331};
bsalomon33435572014-11-05 14:47:41 -0800332int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000333
bsalomonc2f35b72015-01-23 07:19:22 -0800334class Mock {
335public:
336 Mock(int maxCnt, size_t maxBytes) {
337 fContext.reset(GrContext::CreateMockContext());
338 SkASSERT(fContext);
339 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800340 GrResourceCache* cache = fContext->getResourceCache();
341 cache->purgeAllUnlocked();
342 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800343 }
bsalomonc2f35b72015-01-23 07:19:22 -0800344
bsalomon0ea80f42015-02-11 10:49:59 -0800345 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800346
347 GrContext* context() { return fContext; }
348
349private:
350 SkAutoTUnref<GrContext> fContext;
351};
352
353static void test_no_key(skiatest::Reporter* reporter) {
354 Mock mock(10, 30000);
355 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800356 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800357
358 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700359 TestResource* a = new TestResource(context->getGpu());
360 TestResource* b = new TestResource(context->getGpu());
361 TestResource* c = new TestResource(context->getGpu());
362 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800363 a->setSize(11);
364 b->setSize(12);
365 c->setSize(13);
366 d->setSize(14);
367
368 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800369 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800370 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800371 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800372
373 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800374 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800375 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
376
bsalomon8718aaf2015-02-19 07:24:21 -0800377 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800378
379 a->unref();
380 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800381 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800382 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800383 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800384
385 c->unref();
386 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800387 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800388 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800390
391 d->unref();
392 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800393 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
394 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800395
396 b->unref();
397 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800398 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
399 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400}
401
bsalomon24db3b12015-01-23 04:24:04 -0800402// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800403template <int> static void make_unique_key(GrUniqueKey* key, int data) {
404 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
405 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800406 builder[0] = data;
407}
408
bsalomon84c8e622014-11-17 09:33:27 -0800409static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800410 Mock mock(10, 300);
411 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800412 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800413
bsalomon8718aaf2015-02-19 07:24:21 -0800414 GrUniqueKey uniqueKey;
415 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800416
bsalomon8718aaf2015-02-19 07:24:21 -0800417 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800418 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800419 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800420 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700421 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800422 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800423 unique->resourcePriv().setUniqueKey(uniqueKey);
halcanary385fe4d2015-08-26 13:07:48 -0700424 TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomon5236cf42015-01-14 10:42:08 -0800425 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700426 TestResource* unbudgeted =
427 new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCycle);
bsalomon84c8e622014-11-17 09:33:27 -0800428 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800429
bsalomon8718aaf2015-02-19 07:24:21 -0800430 // Make sure we can't add a unique key to the wrapped resource
431 GrUniqueKey uniqueKey2;
432 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800433 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700434 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800435
436 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800437 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800438 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800439 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800440 cache->getResourceBytes());
441 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800442 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800443 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800444
bsalomon63c992f2015-01-23 12:47:59 -0800445 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800446 cache->purgeAllUnlocked();
447 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800448 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800449 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800450 cache->getResourceBytes());
451 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800452 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800453 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800454
455 // Unreffing the wrapped resource should free it right away.
456 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800457 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800458 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800459 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800460
bsalomon84c8e622014-11-17 09:33:27 -0800461 // Now try freeing the budgeted resources first
halcanary385fe4d2015-08-26 13:07:48 -0700462 wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomondace19e2014-11-17 07:34:06 -0800463 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800464 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800465 cache->purgeAllUnlocked();
466 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800467 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800468 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
469 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
470 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800471
472 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->purgeAllUnlocked();
474 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800475 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800476 cache->getResourceBytes());
477 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
478 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800479
480 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800481 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
482 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
483 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
484 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800485
486 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800487 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
488 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
489 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
490 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800491}
492
bsalomon5236cf42015-01-14 10:42:08 -0800493static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800494 Mock mock(10, 30000);
495 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800496 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800497
bsalomon8718aaf2015-02-19 07:24:21 -0800498 GrUniqueKey uniqueKey;
499 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800500
501 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800502 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800503 TestResource* wrapped;
504 TestResource* unbudgeted;
505
506 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800507 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800508 scratch->setSize(10);
509 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800510 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
511 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
512 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
513 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800514
halcanary385fe4d2015-08-26 13:07:48 -0700515 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800516 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800517 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800518 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800519 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
520 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
521 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
522 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800523
bsalomon0ea80f42015-02-11 10:49:59 -0800524 size_t large = 2 * cache->getResourceBytes();
halcanary385fe4d2015-08-26 13:07:48 -0700525 unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUncached_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800526 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
527 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
528 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
529 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800530
531 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800536
halcanary385fe4d2015-08-26 13:07:48 -0700537 wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800538 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
539 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
540 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
541 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800542
543 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800544 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
545 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
546 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
547 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800548
bsalomon0ea80f42015-02-11 10:49:59 -0800549 cache->purgeAllUnlocked();
550 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800554}
555
bsalomon3582d3e2015-02-13 14:20:05 -0800556// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
557void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
558/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800559 Mock mock(10, 300);
560 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800561 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800562
563 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800564 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800565 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800566 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800567
568 size_t size = resource->gpuMemorySize();
569 for (int i = 0; i < 2; ++i) {
570 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800571 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800572 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800573 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700574 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
576 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
577 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
578 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800579
580 // Once it is unrefed, it should become available as scratch.
581 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800582 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700586 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800587 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800588 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800589 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800590 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800591
592 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700593 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800594 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800595 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800596 } else {
597 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800598 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800599 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
600 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
601 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
602 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800603 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800604 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800605 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800606
607 // now when it is unrefed it should die since it has no key.
608 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800609 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
610 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
611 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
612 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800613 }
bsalomon8b79d232014-11-10 10:19:06 -0800614 }
bsalomonc2f35b72015-01-23 07:19:22 -0800615}
616
617static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
618 Mock mock(5, 30000);
619 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800620 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800621
bsalomon8b79d232014-11-10 10:19:06 -0800622 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800623 TestResource* a = TestResource::CreateScratch(context->getGpu(),
624 TestResource::kB_SimulatedProperty);
625 TestResource* b = TestResource::CreateScratch(context->getGpu(),
626 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800627 a->setSize(11);
628 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800629 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800630 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800631 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700632 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800633
634 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800635 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800636
bsalomon0ea80f42015-02-11 10:49:59 -0800637 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800638 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800639 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
640 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800641 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800642 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800643
bsalomon63c992f2015-01-23 12:47:59 -0800644 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800645 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800646 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800647 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800648
649 // Unref but don't purge
650 a->unref();
651 b->unref();
652 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800653 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800654
bsalomon63c992f2015-01-23 12:47:59 -0800655 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800656 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800657 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800658 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
659 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800660}
661
bsalomon10e23ca2014-11-25 05:52:06 -0800662static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800663 Mock mock(5, 30000);
664 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800665 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800666
bsalomon10e23ca2014-11-25 05:52:06 -0800667 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800668 TestResource* a = TestResource::CreateScratch(context->getGpu(),
669 TestResource::kB_SimulatedProperty);
670 TestResource* b = TestResource::CreateScratch(context->getGpu(),
671 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800672 a->unref();
673 b->unref();
674
bsalomon1c60dfe2015-01-21 09:32:40 -0800675 GrScratchKey scratchKey;
676 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800677 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800678 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700679 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800680
bsalomon0ea80f42015-02-11 10:49:59 -0800681 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800682 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800683 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800684 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
685 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800686
687 // Find the first resource and remove its scratch key
688 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700689 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800690 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800691 // It's still alive, but not cached by scratch key anymore
692 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800693 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
694 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800695
696 // The cache should immediately delete it when it's unrefed since it isn't accessible.
697 find->unref();
698 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800699 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
700 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800701
702 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700703 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800704 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800705 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800706 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
707 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800708
709 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800710 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800711 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800712 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
713 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800714
715 find->unref();
716 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800717 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
718 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800719}
720
bsalomon1c60dfe2015-01-21 09:32:40 -0800721static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800722 Mock mock(5, 30000);
723 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800724 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800725
726 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800727 TestResource* a = TestResource::CreateScratch(context->getGpu(),
728 TestResource::kB_SimulatedProperty);
729 TestResource* b = TestResource::CreateScratch(context->getGpu(),
730 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800731 a->unref();
732 b->unref();
733
734 GrScratchKey scratchKey;
735 // Ensure that scratch key comparison and assignment is consistent.
736 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800737 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800738 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800739 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800740 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
741 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
742 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
743 scratchKey = scratchKey1;
744 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
745 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
746 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
747 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
748 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
749 scratchKey = scratchKey2;
750 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
751 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
752 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
753 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
754 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
755
756 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800757 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700759 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800760
761 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800762 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700763 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700764 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800765 find->unref();
766
767 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700768 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700769 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800770 REPORTER_ASSERT(reporter, find == a || find == b);
771
robertphillips6e83ac72015-08-13 05:19:14 -0700772 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700773 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800774 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
775 REPORTER_ASSERT(reporter, find2 != find);
776 find2->unref();
777 find->unref();
778}
779
bsalomon8718aaf2015-02-19 07:24:21 -0800780static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800781 Mock mock(5, 30000);
782 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800783 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000784
bsalomon8718aaf2015-02-19 07:24:21 -0800785 GrUniqueKey key;
786 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700787
bsalomon8718aaf2015-02-19 07:24:21 -0800788 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700789 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800790 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700791
bsalomonf99e9612015-02-19 08:24:16 -0800792 // Set key on resource a.
793 a->resourcePriv().setUniqueKey(key);
794 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
795 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800796
bsalomonf99e9612015-02-19 08:24:16 -0800797 // Make sure that redundantly setting a's key works.
798 a->resourcePriv().setUniqueKey(key);
799 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800800 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800801 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
802 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800803 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
804
bsalomonf99e9612015-02-19 08:24:16 -0800805 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700806 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800807 b->setSize(12);
808 b->resourcePriv().setUniqueKey(key);
809 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
810 b->unref();
811
812 // Still have two resources because a is still reffed.
813 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
814 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
815 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
816
817 a->unref();
818 // Now a should be gone.
819 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
820 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
821 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
822
823 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
824 // Also make b be unreffed when replacement occurs.
825 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700826 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800827 GrUniqueKey differentKey;
828 make_unique_key<0>(&differentKey, 1);
829 c->setSize(13);
830 c->resourcePriv().setUniqueKey(differentKey);
831 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
832 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
833 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
834 // c replaces b and b should be immediately purged.
835 c->resourcePriv().setUniqueKey(key);
836 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
837 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
838 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
839
840 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800841 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800842 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
843 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
844 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
845
846 // Drop the ref on c, it should be kept alive because it has a unique key.
847 c->unref();
848 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
849 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
850 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
851
852 // Verify that we can find c, then remove its unique key. It should get purged immediately.
853 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
854 c->resourcePriv().removeUniqueKey();
855 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800856 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
857 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800858 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700859
860 {
861 GrUniqueKey key2;
862 make_unique_key<0>(&key2, 0);
halcanary385fe4d2015-08-26 13:07:48 -0700863 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700864 int foo = 4132;
865 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
866 key2.setCustomData(data.get());
867 d->resourcePriv().setUniqueKey(key2);
868 }
869
870 GrUniqueKey key3;
871 make_unique_key<0>(&key3, 0);
872 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
873 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000874}
875
bsalomon8b79d232014-11-10 10:19:06 -0800876static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800877 Mock mock(5, 30000);
878 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800879 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800880
bsalomon8718aaf2015-02-19 07:24:21 -0800881 GrUniqueKey key1, key2, key3;
882 make_unique_key<0>(&key1, 1);
883 make_unique_key<0>(&key2, 2);
884 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700885
bsalomon23e619c2015-02-06 11:54:28 -0800886 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700887 TestResource* a = new TestResource(context->getGpu());
888 TestResource* b = new TestResource(context->getGpu());
bsalomon23e619c2015-02-06 11:54:28 -0800889 TestResource* c = TestResource::CreateScratch(context->getGpu(),
890 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800891 a->resourcePriv().setUniqueKey(key1);
892 b->resourcePriv().setUniqueKey(key2);
893 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800894 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800895 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800896 c->unref();
897
bsalomon8718aaf2015-02-19 07:24:21 -0800898 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
899 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
900 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800901 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800902
bsalomon8718aaf2015-02-19 07:24:21 -0800903 typedef GrUniqueKeyInvalidatedMessage Msg;
904 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800905
906 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
907 Bus::Post(Msg(key1));
908 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800909 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800910 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800911 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
912 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800913 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800914 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800915
916 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800917 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800918 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800919 // we still have a ref on b, c should be recycled as scratch.
920 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800921 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800922
bsalomon23e619c2015-02-06 11:54:28 -0800923 // make b purgeable. It should be immediately deleted since it has no key.
924 b->unref();
925 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
926
927 // Make sure we actually get to c via it's scratch key, before we say goodbye.
928 GrScratchKey scratchKey;
929 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700930 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800931 REPORTER_ASSERT(reporter, scratch == c);
932 SkSafeUnref(scratch);
933
934 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800935 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700936 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800937 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800938 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
939 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800940 REPORTER_ASSERT(reporter, !scratch);
941 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800942}
943
bsalomon71cb0c22014-11-14 12:10:14 -0800944static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800945 Mock mock(3, 30000);
946 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800947 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800948
bsalomon8718aaf2015-02-19 07:24:21 -0800949 GrUniqueKey key1, key2;
950 make_unique_key<0>(&key1, 1);
951 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000952
halcanary385fe4d2015-08-26 13:07:48 -0700953 TestResource* a = new TestResource(context->getGpu());
954 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800955 a->resourcePriv().setUniqueKey(key1);
956 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800957
bsalomonc2f35b72015-01-23 07:19:22 -0800958 // Make a cycle
959 a->setUnrefWhenDestroyed(b);
960 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800961
bsalomonc2f35b72015-01-23 07:19:22 -0800962 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800963
bsalomonc2f35b72015-01-23 07:19:22 -0800964 a->unref();
965 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800966
bsalomonc2f35b72015-01-23 07:19:22 -0800967 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800968
bsalomon0ea80f42015-02-11 10:49:59 -0800969 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800970 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800971
bsalomonc2f35b72015-01-23 07:19:22 -0800972 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700973 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800974 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800975
bsalomon0ea80f42015-02-11 10:49:59 -0800976 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800977 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000978}
979
bsalomon8b79d232014-11-10 10:19:06 -0800980static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800981 GrUniqueKey key1, key2;
982 make_unique_key<0>(&key1, 1);
983 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000984
985 // Test changing resources sizes (both increase & decrease).
986 {
bsalomonc2f35b72015-01-23 07:19:22 -0800987 Mock mock(3, 30000);
988 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800989 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000990
halcanary385fe4d2015-08-26 13:07:48 -0700991 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800992 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000993 a->unref();
994
halcanary385fe4d2015-08-26 13:07:48 -0700995 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800996 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000997 b->unref();
998
bsalomon0ea80f42015-02-11 10:49:59 -0800999 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1000 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001001 {
bsalomon8718aaf2015-02-19 07:24:21 -08001002 SkAutoTUnref<TestResource> find2(
1003 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001004 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -08001005 SkAutoTUnref<TestResource> find1(
1006 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001007 find1->setSize(50);
1008 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001009
bsalomon0ea80f42015-02-11 10:49:59 -08001010 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1011 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001012 }
1013
1014 // Test increasing a resources size beyond the cache budget.
1015 {
bsalomonc2f35b72015-01-23 07:19:22 -08001016 Mock mock(2, 300);
1017 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001018 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001019
halcanary385fe4d2015-08-26 13:07:48 -07001020 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001021 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001022 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001023 a->unref();
1024
halcanary385fe4d2015-08-26 13:07:48 -07001025 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001026 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001027 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001028 b->unref();
1029
bsalomon0ea80f42015-02-11 10:49:59 -08001030 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1031 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001032
bsalomon8b79d232014-11-10 10:19:06 -08001033 {
bsalomon8718aaf2015-02-19 07:24:21 -08001034 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1035 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001036 find2->setSize(201);
1037 }
bsalomon8718aaf2015-02-19 07:24:21 -08001038 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001039
bsalomon0ea80f42015-02-11 10:49:59 -08001040 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1041 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001042 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001043}
1044
bsalomonddf30e62015-02-19 11:38:44 -08001045static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1046 static const int kCount = 50;
1047 static const int kBudgetCnt = kCount / 2;
1048 static const int kLockedFreq = 8;
1049 static const int kBudgetSize = 0x80000000;
1050
1051 SkRandom random;
1052
1053 // Run the test 2*kCount times;
1054 for (int i = 0; i < 2 * kCount; ++i ) {
1055 Mock mock(kBudgetCnt, kBudgetSize);
1056 GrContext* context = mock.context();
1057 GrResourceCache* cache = mock.cache();
1058
1059 // Pick a random number of resources to add before the timestamp will wrap.
1060 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1061
1062 static const int kNumToPurge = kCount - kBudgetCnt;
1063
1064 SkTDArray<int> shouldPurgeIdxs;
1065 int purgeableCnt = 0;
1066 SkTDArray<GrGpuResource*> resourcesToUnref;
1067
1068 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1069 // unpurgeable resources.
1070 for (int j = 0; j < kCount; ++j) {
1071 GrUniqueKey key;
1072 make_unique_key<0>(&key, j);
1073
halcanary385fe4d2015-08-26 13:07:48 -07001074 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001075 r->resourcePriv().setUniqueKey(key);
1076 if (random.nextU() % kLockedFreq) {
1077 // Make this is purgeable.
1078 r->unref();
1079 ++purgeableCnt;
1080 if (purgeableCnt <= kNumToPurge) {
1081 *shouldPurgeIdxs.append() = j;
1082 }
1083 } else {
1084 *resourcesToUnref.append() = r;
1085 }
1086 }
1087
1088 // Verify that the correct resources were purged.
1089 int currShouldPurgeIdx = 0;
1090 for (int j = 0; j < kCount; ++j) {
1091 GrUniqueKey key;
1092 make_unique_key<0>(&key, j);
1093 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1094 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1095 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1096 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001097 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001098 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001099 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001100 }
1101 SkSafeUnref(res);
1102 }
1103
1104 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1105 resourcesToUnref[j]->unref();
1106 }
1107 }
1108}
1109
bsalomon3f324322015-04-08 11:01:54 -07001110static void test_flush(skiatest::Reporter* reporter) {
1111 Mock mock(1000000, 1000000);
1112 GrContext* context = mock.context();
1113 GrResourceCache* cache = mock.cache();
1114
1115 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1116 // power of two here to keep things simpler.
1117 static const int kFlushCount = 16;
1118 cache->setLimits(1000000, 1000000, kFlushCount);
1119
1120 {
1121 // Insert a resource and send a flush notification kFlushCount times.
1122 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001123 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001124 GrUniqueKey k;
1125 make_unique_key<1>(&k, i);
1126 r->resourcePriv().setUniqueKey(k);
1127 r->unref();
1128 cache->notifyFlushOccurred();
1129 }
1130
1131 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1132 for (int i = 0; i < kFlushCount - 1; ++i) {
1133 // The first resource was purged after the last flush in the initial loop, hence the -1.
1134 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1135 for (int j = 0; j < i; ++j) {
1136 GrUniqueKey k;
1137 make_unique_key<1>(&k, j);
1138 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1139 REPORTER_ASSERT(reporter, !SkToBool(r));
1140 SkSafeUnref(r);
1141 }
1142 cache->notifyFlushOccurred();
1143 }
1144
1145 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1146 cache->purgeAllUnlocked();
1147 }
1148
1149 // Do a similar test but where we leave refs on some resources to prevent them from being
1150 // purged.
1151 {
1152 GrGpuResource* refedResources[kFlushCount >> 1];
1153 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001154 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001155 GrUniqueKey k;
1156 make_unique_key<1>(&k, i);
1157 r->resourcePriv().setUniqueKey(k);
1158 // Leave a ref on every other resource, beginning with the first.
1159 if (SkToBool(i & 0x1)) {
1160 refedResources[i/2] = r;
1161 } else {
1162 r->unref();
1163 }
1164 cache->notifyFlushOccurred();
1165 }
1166
1167 for (int i = 0; i < kFlushCount; ++i) {
1168 // Should get a resource purged every other flush.
1169 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1170 cache->notifyFlushOccurred();
1171 }
1172
1173 // Unref all the resources that we kept refs on in the first loop.
1174 for (int i = 0; i < kFlushCount >> 1; ++i) {
1175 refedResources[i]->unref();
1176 }
1177
1178 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1179 // get kFlushCount additional flushes. Then everything should be purged.
1180 for (int i = 0; i < kFlushCount; ++i) {
1181 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1182 cache->notifyFlushOccurred();
1183 }
1184 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1185
1186 cache->purgeAllUnlocked();
1187 }
1188
1189 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1190}
1191
bsalomon10e23ca2014-11-25 05:52:06 -08001192static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001193 // Set the cache size to double the resource count because we're going to create 2x that number
1194 // resources, using two different key domains. Add a little slop to the bytes because we resize
1195 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001196 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001197
bsalomonc2f35b72015-01-23 07:19:22 -08001198 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1199 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001200 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001201
1202 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001203 GrUniqueKey key1, key2;
1204 make_unique_key<1>(&key1, i);
1205 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001206
bsalomon24db3b12015-01-23 04:24:04 -08001207 TestResource* resource;
1208
halcanary385fe4d2015-08-26 13:07:48 -07001209 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001210 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001211 resource->setSize(1);
1212 resource->unref();
1213
halcanary385fe4d2015-08-26 13:07:48 -07001214 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001215 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001216 resource->setSize(1);
1217 resource->unref();
1218 }
1219
1220 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001221 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1222 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1223 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1224 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001225 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001226 GrUniqueKey key1, key2;
1227 make_unique_key<1>(&key1, i);
1228 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001229
bsalomon8718aaf2015-02-19 07:24:21 -08001230 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1231 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001232 }
1233
bsalomon0ea80f42015-02-11 10:49:59 -08001234 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001235 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001236 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1237 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1238 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1239 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001240
1241 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001242 GrUniqueKey key1, key2;
1243 make_unique_key<1>(&key1, i);
1244 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001245
bsalomon8718aaf2015-02-19 07:24:21 -08001246 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1247 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001248 }
1249}
1250
senorblanco84cd6212015-08-04 10:01:58 -07001251static void test_custom_data(skiatest::Reporter* reporter) {
1252 GrUniqueKey key1, key2;
1253 make_unique_key<0>(&key1, 1);
1254 make_unique_key<0>(&key2, 2);
1255 int foo = 4132;
1256 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1257 key1.setCustomData(data.get());
1258 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1259 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1260
1261 // Test that copying a key also takes a ref on its custom data.
1262 GrUniqueKey key3 = key1;
1263 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1264}
1265
bsalomonc6363ef2015-09-24 07:07:40 -07001266static void test_abandoned(skiatest::Reporter* reporter) {
1267 Mock mock(10, 300);
1268 GrContext* context = mock.context();
Brian Salomon89438a12015-09-24 13:22:45 -04001269 SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001270 context->abandonContext();
1271
1272 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1273
1274 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1275
1276 int foo = 4132;
1277 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1278 resource->setCustomData(data.get());
1279 resource->getCustomData();
1280 resource->getUniqueID();
1281 resource->getUniqueKey();
1282 resource->wasDestroyed();
1283 resource->gpuMemorySize();
1284 resource->getContext();
1285
1286 resource->abandon();
1287 resource->resourcePriv().getScratchKey();
1288 resource->resourcePriv().isBudgeted();
1289 resource->resourcePriv().makeBudgeted();
1290 resource->resourcePriv().makeUnbudgeted();
1291 resource->resourcePriv().removeScratchKey();
1292 GrUniqueKey key;
1293 make_unique_key<0>(&key, 1);
1294 resource->resourcePriv().setUniqueKey(key);
1295 resource->resourcePriv().removeUniqueKey();
1296}
1297
kkinnunen15302832015-12-01 04:35:26 -08001298DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001299 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001300 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001301 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001302 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001303 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001304 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001305 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001306 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001307 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001308 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001309 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001310 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001311 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001312 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001313 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001314 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001315 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001316}
1317
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001318#endif