blob: 0ac64edd0758ce104e3354eb8017a8446d4309a0 [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) {
bsalomonf2f1c172016-04-05 12:59:06 -070034 GrContext* context = ctxInfo.fGrContext;
kkinnunen15302832015-12-01 04:35:26 -080035 GrSurfaceDesc desc;
36 desc.fConfig = kSkia8888_GrPixelConfig;
37 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
bsalomon68d91342016-04-12 09:59:58 -070083DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheStencilBuffers, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070084 GrContext* context = ctxInfo.fGrContext;
bsalomon02a44a42015-02-19 09:09:00 -080085 GrSurfaceDesc smallDesc;
86 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
87 smallDesc.fConfig = kSkia8888_GrPixelConfig;
88 smallDesc.fWidth = 4;
89 smallDesc.fHeight = 4;
90 smallDesc.fSampleCnt = 0;
91
bsalomond309e7a2015-04-30 14:18:54 -070092 GrTextureProvider* cache = context->textureProvider();
egdanielec00d942015-09-14 12:56:10 -070093 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -080094 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomon5ec26ae2016-02-25 08:33:02 -080095 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -080096 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -070097 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -080098 }
99
bsalomon5ec26ae2016-02-25 08:33:02 -0800100 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800101 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700102 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800103 }
104
egdaniel8dc7c3a2015-04-16 11:22:42 -0700105 REPORTER_ASSERT(reporter,
106 smallRT0 && smallRT1 &&
107 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700108 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
109 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800110
111 // An unbudgeted RT with the same desc should also share.
bsalomon5ec26ae2016-02-25 08:33:02 -0800112 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700114 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800115 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700116 REPORTER_ASSERT(reporter,
117 smallRT0 && smallRT2 &&
118 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700119 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
120 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800121
122 // An RT with a much larger size should not share.
123 GrSurfaceDesc bigDesc;
124 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
125 bigDesc.fConfig = kSkia8888_GrPixelConfig;
126 bigDesc.fWidth = 400;
127 bigDesc.fHeight = 200;
128 bigDesc.fSampleCnt = 0;
bsalomon5ec26ae2016-02-25 08:33:02 -0800129 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800130 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700131 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800132 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700133 REPORTER_ASSERT(reporter,
134 smallRT0 && bigRT &&
135 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700136 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
137 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800138
bsalomon76228632015-05-29 08:02:10 -0700139 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700140 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800141 GrSurfaceDesc smallMSAADesc = smallDesc;
142 smallMSAADesc.fSampleCnt = 4;
bsalomon5ec26ae2016-02-25 08:33:02 -0800143 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800144 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700145 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800146 }
bsalomonb602d4d2015-02-19 12:05:58 -0800147#ifdef SK_BUILD_FOR_ANDROID
148 if (!smallMSAART0) {
149 // The nexus player seems to fail to create MSAA textures.
150 return;
151 }
152#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800153 REPORTER_ASSERT(reporter,
154 smallRT0 && smallMSAART0 &&
155 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700156 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
157 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800158 // A second MSAA RT should share with the first MSAA RT.
bsalomon5ec26ae2016-02-25 08:33:02 -0800159 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800160 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700161 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800162 }
163 REPORTER_ASSERT(reporter,
164 smallMSAART0 && smallMSAART1 &&
165 smallMSAART0->asRenderTarget() &&
166 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700167 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
168 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800169 // But not one with a larger sample count should not. (Also check that the request for 4
170 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700171 if (context->caps()->maxSampleCount() >= 8 &&
172 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700173 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800174 smallMSAADesc.fSampleCnt = 8;
bsalomon5ec26ae2016-02-25 08:33:02 -0800175 smallMSAART1.reset(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
176 SkAutoTUnref<GrTexture> smallMSAART1(
177 cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800178 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700179 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800180 }
181 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700182 smallMSAART0 && smallMSAART1 &&
183 smallMSAART0->asRenderTarget() &&
184 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700185 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
186 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800187 }
188 }
189}
190
bsalomon68d91342016-04-12 09:59:58 -0700191DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700192 GrContext* context = ctxInfo.fGrContext;
bsalomone63ffef2016-02-05 07:17:34 -0800193 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700194 // this test is only valid for GL
195 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700196 return;
197 }
198
bsalomon091f60c2015-11-10 11:54:56 -0800199 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700200 static const int kW = 100;
201 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700202
bsalomon091f60c2015-11-10 11:54:56 -0800203 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
204 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700205
bsalomon6dc6f5f2015-06-18 09:12:16 -0700206 context->resetContext();
207
208 GrBackendTextureDesc desc;
209 desc.fConfig = kBGRA_8888_GrPixelConfig;
210 desc.fWidth = kW;
211 desc.fHeight = kH;
212
bsalomon091f60c2015-11-10 11:54:56 -0800213 desc.fTextureHandle = texHandles[0];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700214 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700215 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216
bsalomon091f60c2015-11-10 11:54:56 -0800217 desc.fTextureHandle = texHandles[1];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700219 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220
mtklein5f939ab2016-03-16 10:28:35 -0700221 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
222 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223 return;
224 }
225
halcanary96fcdcc2015-08-27 07:41:13 -0700226 borrowed.reset(nullptr);
227 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
229 context->flush();
230
bsalomon091f60c2015-11-10 11:54:56 -0800231 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
232 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233
234 REPORTER_ASSERT(reporter, borrowedIsAlive);
235 REPORTER_ASSERT(reporter, !adoptedIsAlive);
236
bsalomon67d76202015-11-11 12:40:42 -0800237 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
238 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700239
240 context->resetContext();
241}
242
bsalomon6d3fe022014-07-25 08:35:45 -0700243class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800244 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000245public:
robertphillips6e83ac72015-08-13 05:19:14 -0700246 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700247
bsalomon1c60dfe2015-01-21 09:32:40 -0800248 /** Property that distinctly categorizes the resource.
249 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800250 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800251
bsalomon5236cf42015-01-14 10:42:08 -0800252 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
253 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700254 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800255 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800256 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800257 ++fNumAlive;
258 this->registerWithCache();
259 }
260
261 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
262 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700263 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800264 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800265 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800266 ++fNumAlive;
267 this->registerWithCache();
268 }
269
bsalomon8b79d232014-11-10 10:19:06 -0800270 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800271 : INHERITED(gpu, kCached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700272 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800273 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800274 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800275 ++fNumAlive;
276 this->registerWithCache();
277 }
278
bsalomon23e619c2015-02-06 11:54:28 -0800279 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
halcanary385fe4d2015-08-26 13:07:48 -0700280 return new TestResource(gpu, property, cached, kScratchConstructor);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
283 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800284 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800285 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000286 }
287
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000288 void setSize(size_t size) {
289 fSize = size;
290 this->didChangeGpuMemorySize();
291 }
292
bsalomon33435572014-11-05 14:47:41 -0800293 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294
bsalomon71cb0c22014-11-14 12:10:14 -0800295 void setUnrefWhenDestroyed(TestResource* resource) {
296 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000297 }
298
bsalomon1c60dfe2015-01-21 09:32:40 -0800299 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
300 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
301 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800302 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
303 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800304 }
305 }
306
307 static size_t ExpectedScratchKeySize() {
308 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
309 }
310
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000311private:
bsalomon24db3b12015-01-23 04:24:04 -0800312 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800313
bsalomonc2f35b72015-01-23 07:19:22 -0800314 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
315 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700316 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800317 , fSize(kDefaultSize)
318 , fProperty(property) {
319 GrScratchKey scratchKey;
320 ComputeScratchKey(fProperty, &scratchKey);
321 this->setScratchKey(scratchKey);
322 ++fNumAlive;
323 this->registerWithCache();
324 }
325
mtklein36352bf2015-03-25 18:17:31 -0700326 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800327
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000328 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000329 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800330 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800331 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700332 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000333};
bsalomon33435572014-11-05 14:47:41 -0800334int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000335
bsalomonc2f35b72015-01-23 07:19:22 -0800336class Mock {
337public:
338 Mock(int maxCnt, size_t maxBytes) {
339 fContext.reset(GrContext::CreateMockContext());
340 SkASSERT(fContext);
341 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800342 GrResourceCache* cache = fContext->getResourceCache();
343 cache->purgeAllUnlocked();
344 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800345 }
bsalomonc2f35b72015-01-23 07:19:22 -0800346
bsalomon0ea80f42015-02-11 10:49:59 -0800347 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800348
349 GrContext* context() { return fContext; }
350
351private:
352 SkAutoTUnref<GrContext> fContext;
353};
354
355static void test_no_key(skiatest::Reporter* reporter) {
356 Mock mock(10, 30000);
357 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800358 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800359
360 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700361 TestResource* a = new TestResource(context->getGpu());
362 TestResource* b = new TestResource(context->getGpu());
363 TestResource* c = new TestResource(context->getGpu());
364 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800365 a->setSize(11);
366 b->setSize(12);
367 c->setSize(13);
368 d->setSize(14);
369
370 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800371 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800372 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800373 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800374
375 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800376 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800377 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
378
bsalomon8718aaf2015-02-19 07:24:21 -0800379 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800380
381 a->unref();
382 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800383 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800384 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800385 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386
387 c->unref();
388 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800389 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800391 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800392
393 d->unref();
394 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800395 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
396 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800397
398 b->unref();
399 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800400 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
401 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800402}
403
bsalomon24db3b12015-01-23 04:24:04 -0800404// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800405template <int> static void make_unique_key(GrUniqueKey* key, int data) {
406 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
407 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800408 builder[0] = data;
409}
410
bsalomon84c8e622014-11-17 09:33:27 -0800411static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800412 Mock mock(10, 300);
413 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800414 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800415
bsalomon8718aaf2015-02-19 07:24:21 -0800416 GrUniqueKey uniqueKey;
417 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800418
bsalomon8718aaf2015-02-19 07:24:21 -0800419 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800420 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800421 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800422 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700423 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800424 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800425 unique->resourcePriv().setUniqueKey(uniqueKey);
halcanary385fe4d2015-08-26 13:07:48 -0700426 TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomon5236cf42015-01-14 10:42:08 -0800427 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700428 TestResource* unbudgeted =
429 new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCycle);
bsalomon84c8e622014-11-17 09:33:27 -0800430 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800431
bsalomon8718aaf2015-02-19 07:24:21 -0800432 // Make sure we can't add a unique key to the wrapped resource
433 GrUniqueKey uniqueKey2;
434 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800435 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700436 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800437
438 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800439 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800440 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800441 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800442 cache->getResourceBytes());
443 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800444 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800445 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800446
bsalomon63c992f2015-01-23 12:47:59 -0800447 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800448 cache->purgeAllUnlocked();
449 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800450 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800451 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800452 cache->getResourceBytes());
453 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800454 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800455 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800456
457 // Unreffing the wrapped resource should free it right away.
458 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800459 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800460 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800461 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800462
bsalomon84c8e622014-11-17 09:33:27 -0800463 // Now try freeing the budgeted resources first
halcanary385fe4d2015-08-26 13:07:48 -0700464 wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomondace19e2014-11-17 07:34:06 -0800465 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800466 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800467 cache->purgeAllUnlocked();
468 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800470 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
472 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800473
474 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800475 cache->purgeAllUnlocked();
476 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800477 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800478 cache->getResourceBytes());
479 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
480 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800481
482 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800483 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
484 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
485 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
486 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800487
488 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800489 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
490 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
491 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
492 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800493}
494
bsalomon5236cf42015-01-14 10:42:08 -0800495static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800496 Mock mock(10, 30000);
497 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800498 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800499
bsalomon8718aaf2015-02-19 07:24:21 -0800500 GrUniqueKey uniqueKey;
501 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800502
503 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800504 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800505 TestResource* wrapped;
506 TestResource* unbudgeted;
507
508 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800509 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800510 scratch->setSize(10);
511 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800512 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
513 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
514 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
515 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800516
halcanary385fe4d2015-08-26 13:07:48 -0700517 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800518 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800519 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800520 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800521 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
522 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
523 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
524 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800525
bsalomon0ea80f42015-02-11 10:49:59 -0800526 size_t large = 2 * cache->getResourceBytes();
halcanary385fe4d2015-08-26 13:07:48 -0700527 unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUncached_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800528 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
529 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
530 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
531 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800532
533 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800534 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
535 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
536 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
537 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800538
halcanary385fe4d2015-08-26 13:07:48 -0700539 wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800540 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
541 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
542 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
543 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800544
545 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800546 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
547 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
548 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
549 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800550
bsalomon0ea80f42015-02-11 10:49:59 -0800551 cache->purgeAllUnlocked();
552 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
553 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
554 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
555 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800556}
557
bsalomon3582d3e2015-02-13 14:20:05 -0800558// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
559void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
560/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800561 Mock mock(10, 300);
562 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800563 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800564
565 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800566 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800567 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800568 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800569
570 size_t size = resource->gpuMemorySize();
571 for (int i = 0; i < 2; ++i) {
572 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800573 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800574 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800575 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700576 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800577 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
578 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
579 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
580 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800581
582 // Once it is unrefed, it should become available as scratch.
583 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800584 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
585 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
586 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
587 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700588 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800589 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800590 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800591 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800592 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800593
594 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700595 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800596 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800597 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800598 } else {
599 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800600 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
602 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
603 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
604 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800605 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800606 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800607 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800608
609 // now when it is unrefed it should die since it has no key.
610 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800611 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
612 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
614 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800615 }
bsalomon8b79d232014-11-10 10:19:06 -0800616 }
bsalomonc2f35b72015-01-23 07:19:22 -0800617}
618
619static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
620 Mock mock(5, 30000);
621 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800622 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800623
bsalomon8b79d232014-11-10 10:19:06 -0800624 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800625 TestResource* a = TestResource::CreateScratch(context->getGpu(),
626 TestResource::kB_SimulatedProperty);
627 TestResource* b = TestResource::CreateScratch(context->getGpu(),
628 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800629 a->setSize(11);
630 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800631 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800632 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800633 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700634 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800635
636 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800637 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800638
bsalomon0ea80f42015-02-11 10:49:59 -0800639 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800640 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800641 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
642 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800643 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800644 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800645
bsalomon63c992f2015-01-23 12:47:59 -0800646 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800647 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800648 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800649 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800650
651 // Unref but don't purge
652 a->unref();
653 b->unref();
654 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800655 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800656
bsalomon63c992f2015-01-23 12:47:59 -0800657 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800658 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800659 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800660 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
661 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800662}
663
bsalomon10e23ca2014-11-25 05:52:06 -0800664static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800665 Mock mock(5, 30000);
666 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800667 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800668
bsalomon10e23ca2014-11-25 05:52:06 -0800669 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800670 TestResource* a = TestResource::CreateScratch(context->getGpu(),
671 TestResource::kB_SimulatedProperty);
672 TestResource* b = TestResource::CreateScratch(context->getGpu(),
673 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800674 a->unref();
675 b->unref();
676
bsalomon1c60dfe2015-01-21 09:32:40 -0800677 GrScratchKey scratchKey;
678 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800679 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800680 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700681 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800682
bsalomon0ea80f42015-02-11 10:49:59 -0800683 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800684 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800685 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800686 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
687 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800688
689 // Find the first resource and remove its scratch key
690 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700691 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800692 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800693 // It's still alive, but not cached by scratch key anymore
694 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800695 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
696 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800697
698 // The cache should immediately delete it when it's unrefed since it isn't accessible.
699 find->unref();
700 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800701 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
702 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800703
704 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700705 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800706 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800707 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800708 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
709 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800710
711 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800712 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800713 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800714 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
715 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800716
717 find->unref();
718 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800719 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
720 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800721}
722
bsalomon1c60dfe2015-01-21 09:32:40 -0800723static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800724 Mock mock(5, 30000);
725 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800726 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800727
728 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800729 TestResource* a = TestResource::CreateScratch(context->getGpu(),
730 TestResource::kB_SimulatedProperty);
731 TestResource* b = TestResource::CreateScratch(context->getGpu(),
732 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800733 a->unref();
734 b->unref();
735
736 GrScratchKey scratchKey;
737 // Ensure that scratch key comparison and assignment is consistent.
738 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800739 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800740 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800741 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800742 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
743 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
744 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
745 scratchKey = scratchKey1;
746 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
747 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
748 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
749 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
750 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
751 scratchKey = scratchKey2;
752 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
753 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
754 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
755 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
756 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
757
758 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800759 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800760 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700761 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800762
763 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800764 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700765 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700766 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800767 find->unref();
768
769 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700770 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700771 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800772 REPORTER_ASSERT(reporter, find == a || find == b);
773
robertphillips6e83ac72015-08-13 05:19:14 -0700774 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700775 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800776 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
777 REPORTER_ASSERT(reporter, find2 != find);
778 find2->unref();
779 find->unref();
780}
781
bsalomon8718aaf2015-02-19 07:24:21 -0800782static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800783 Mock mock(5, 30000);
784 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800785 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000786
bsalomon8718aaf2015-02-19 07:24:21 -0800787 GrUniqueKey key;
788 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700789
bsalomon8718aaf2015-02-19 07:24:21 -0800790 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700791 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800792 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700793
bsalomonf99e9612015-02-19 08:24:16 -0800794 // Set key on resource a.
795 a->resourcePriv().setUniqueKey(key);
796 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
797 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800798
bsalomonf99e9612015-02-19 08:24:16 -0800799 // Make sure that redundantly setting a's key works.
800 a->resourcePriv().setUniqueKey(key);
801 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800802 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800803 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
804 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800805 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
806
bsalomonf99e9612015-02-19 08:24:16 -0800807 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700808 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800809 b->setSize(12);
810 b->resourcePriv().setUniqueKey(key);
811 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
812 b->unref();
813
814 // Still have two resources because a is still reffed.
815 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
816 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
817 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
818
819 a->unref();
820 // Now a should be gone.
821 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
822 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
823 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
824
825 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
826 // Also make b be unreffed when replacement occurs.
827 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700828 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800829 GrUniqueKey differentKey;
830 make_unique_key<0>(&differentKey, 1);
831 c->setSize(13);
832 c->resourcePriv().setUniqueKey(differentKey);
833 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
834 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
835 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
836 // c replaces b and b should be immediately purged.
837 c->resourcePriv().setUniqueKey(key);
838 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
839 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
840 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
841
842 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800843 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800844 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
845 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
846 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
847
848 // Drop the ref on c, it should be kept alive because it has a unique key.
849 c->unref();
850 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
851 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
852 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
853
854 // Verify that we can find c, then remove its unique key. It should get purged immediately.
855 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
856 c->resourcePriv().removeUniqueKey();
857 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800858 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
859 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800860 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700861
862 {
863 GrUniqueKey key2;
864 make_unique_key<0>(&key2, 0);
halcanary385fe4d2015-08-26 13:07:48 -0700865 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700866 int foo = 4132;
867 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
868 key2.setCustomData(data.get());
869 d->resourcePriv().setUniqueKey(key2);
870 }
871
872 GrUniqueKey key3;
873 make_unique_key<0>(&key3, 0);
874 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
875 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000876}
877
bsalomon8b79d232014-11-10 10:19:06 -0800878static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800879 Mock mock(5, 30000);
880 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800881 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800882
bsalomon8718aaf2015-02-19 07:24:21 -0800883 GrUniqueKey key1, key2, key3;
884 make_unique_key<0>(&key1, 1);
885 make_unique_key<0>(&key2, 2);
886 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700887
bsalomon23e619c2015-02-06 11:54:28 -0800888 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700889 TestResource* a = new TestResource(context->getGpu());
890 TestResource* b = new TestResource(context->getGpu());
bsalomon23e619c2015-02-06 11:54:28 -0800891 TestResource* c = TestResource::CreateScratch(context->getGpu(),
892 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800893 a->resourcePriv().setUniqueKey(key1);
894 b->resourcePriv().setUniqueKey(key2);
895 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800896 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800897 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800898 c->unref();
899
bsalomon8718aaf2015-02-19 07:24:21 -0800900 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
901 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
902 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800903 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800904
bsalomon8718aaf2015-02-19 07:24:21 -0800905 typedef GrUniqueKeyInvalidatedMessage Msg;
906 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800907
908 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
909 Bus::Post(Msg(key1));
910 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800911 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800912 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800913 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
914 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800915 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800916 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800917
918 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800919 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800920 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800921 // we still have a ref on b, c should be recycled as scratch.
922 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800923 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800924
bsalomon23e619c2015-02-06 11:54:28 -0800925 // make b purgeable. It should be immediately deleted since it has no key.
926 b->unref();
927 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
928
929 // Make sure we actually get to c via it's scratch key, before we say goodbye.
930 GrScratchKey scratchKey;
931 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700932 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800933 REPORTER_ASSERT(reporter, scratch == c);
934 SkSafeUnref(scratch);
935
936 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800937 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700938 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800939 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800940 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
941 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800942 REPORTER_ASSERT(reporter, !scratch);
943 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800944}
945
bsalomon71cb0c22014-11-14 12:10:14 -0800946static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800947 Mock mock(3, 30000);
948 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800949 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800950
bsalomon8718aaf2015-02-19 07:24:21 -0800951 GrUniqueKey key1, key2;
952 make_unique_key<0>(&key1, 1);
953 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000954
halcanary385fe4d2015-08-26 13:07:48 -0700955 TestResource* a = new TestResource(context->getGpu());
956 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800957 a->resourcePriv().setUniqueKey(key1);
958 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800959
bsalomonc2f35b72015-01-23 07:19:22 -0800960 // Make a cycle
961 a->setUnrefWhenDestroyed(b);
962 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800963
bsalomonc2f35b72015-01-23 07:19:22 -0800964 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800965
bsalomonc2f35b72015-01-23 07:19:22 -0800966 a->unref();
967 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800968
bsalomonc2f35b72015-01-23 07:19:22 -0800969 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800970
bsalomon0ea80f42015-02-11 10:49:59 -0800971 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800972 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800973
bsalomonc2f35b72015-01-23 07:19:22 -0800974 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700975 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800976 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800977
bsalomon0ea80f42015-02-11 10:49:59 -0800978 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800979 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000980}
981
bsalomon8b79d232014-11-10 10:19:06 -0800982static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800983 GrUniqueKey key1, key2;
984 make_unique_key<0>(&key1, 1);
985 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000986
987 // Test changing resources sizes (both increase & decrease).
988 {
bsalomonc2f35b72015-01-23 07:19:22 -0800989 Mock mock(3, 30000);
990 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800991 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000992
halcanary385fe4d2015-08-26 13:07:48 -0700993 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800994 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000995 a->unref();
996
halcanary385fe4d2015-08-26 13:07:48 -0700997 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800998 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000999 b->unref();
1000
bsalomon0ea80f42015-02-11 10:49:59 -08001001 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1002 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001003 {
bsalomon8718aaf2015-02-19 07:24:21 -08001004 SkAutoTUnref<TestResource> find2(
1005 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001006 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -08001007 SkAutoTUnref<TestResource> find1(
1008 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001009 find1->setSize(50);
1010 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001011
bsalomon0ea80f42015-02-11 10:49:59 -08001012 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1013 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001014 }
1015
1016 // Test increasing a resources size beyond the cache budget.
1017 {
bsalomonc2f35b72015-01-23 07:19:22 -08001018 Mock mock(2, 300);
1019 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001020 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001021
halcanary385fe4d2015-08-26 13:07:48 -07001022 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001023 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001024 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001025 a->unref();
1026
halcanary385fe4d2015-08-26 13:07:48 -07001027 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001028 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001029 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001030 b->unref();
1031
bsalomon0ea80f42015-02-11 10:49:59 -08001032 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1033 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001034
bsalomon8b79d232014-11-10 10:19:06 -08001035 {
bsalomon8718aaf2015-02-19 07:24:21 -08001036 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1037 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001038 find2->setSize(201);
1039 }
bsalomon8718aaf2015-02-19 07:24:21 -08001040 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001041
bsalomon0ea80f42015-02-11 10:49:59 -08001042 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1043 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001044 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001045}
1046
bsalomonddf30e62015-02-19 11:38:44 -08001047static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1048 static const int kCount = 50;
1049 static const int kBudgetCnt = kCount / 2;
1050 static const int kLockedFreq = 8;
1051 static const int kBudgetSize = 0x80000000;
1052
1053 SkRandom random;
1054
1055 // Run the test 2*kCount times;
1056 for (int i = 0; i < 2 * kCount; ++i ) {
1057 Mock mock(kBudgetCnt, kBudgetSize);
1058 GrContext* context = mock.context();
1059 GrResourceCache* cache = mock.cache();
1060
1061 // Pick a random number of resources to add before the timestamp will wrap.
1062 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1063
1064 static const int kNumToPurge = kCount - kBudgetCnt;
1065
1066 SkTDArray<int> shouldPurgeIdxs;
1067 int purgeableCnt = 0;
1068 SkTDArray<GrGpuResource*> resourcesToUnref;
1069
1070 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1071 // unpurgeable resources.
1072 for (int j = 0; j < kCount; ++j) {
1073 GrUniqueKey key;
1074 make_unique_key<0>(&key, j);
1075
halcanary385fe4d2015-08-26 13:07:48 -07001076 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001077 r->resourcePriv().setUniqueKey(key);
1078 if (random.nextU() % kLockedFreq) {
1079 // Make this is purgeable.
1080 r->unref();
1081 ++purgeableCnt;
1082 if (purgeableCnt <= kNumToPurge) {
1083 *shouldPurgeIdxs.append() = j;
1084 }
1085 } else {
1086 *resourcesToUnref.append() = r;
1087 }
1088 }
1089
1090 // Verify that the correct resources were purged.
1091 int currShouldPurgeIdx = 0;
1092 for (int j = 0; j < kCount; ++j) {
1093 GrUniqueKey key;
1094 make_unique_key<0>(&key, j);
1095 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1096 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1097 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1098 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001099 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001100 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001101 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001102 }
1103 SkSafeUnref(res);
1104 }
1105
1106 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1107 resourcesToUnref[j]->unref();
1108 }
1109 }
1110}
1111
bsalomon3f324322015-04-08 11:01:54 -07001112static void test_flush(skiatest::Reporter* reporter) {
1113 Mock mock(1000000, 1000000);
1114 GrContext* context = mock.context();
1115 GrResourceCache* cache = mock.cache();
1116
1117 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1118 // power of two here to keep things simpler.
1119 static const int kFlushCount = 16;
1120 cache->setLimits(1000000, 1000000, kFlushCount);
1121
1122 {
1123 // Insert a resource and send a flush notification kFlushCount times.
1124 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001125 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001126 GrUniqueKey k;
1127 make_unique_key<1>(&k, i);
1128 r->resourcePriv().setUniqueKey(k);
1129 r->unref();
1130 cache->notifyFlushOccurred();
1131 }
1132
1133 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1134 for (int i = 0; i < kFlushCount - 1; ++i) {
1135 // The first resource was purged after the last flush in the initial loop, hence the -1.
1136 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1137 for (int j = 0; j < i; ++j) {
1138 GrUniqueKey k;
1139 make_unique_key<1>(&k, j);
1140 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1141 REPORTER_ASSERT(reporter, !SkToBool(r));
1142 SkSafeUnref(r);
1143 }
1144 cache->notifyFlushOccurred();
1145 }
1146
1147 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1148 cache->purgeAllUnlocked();
1149 }
1150
1151 // Do a similar test but where we leave refs on some resources to prevent them from being
1152 // purged.
1153 {
1154 GrGpuResource* refedResources[kFlushCount >> 1];
1155 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001156 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001157 GrUniqueKey k;
1158 make_unique_key<1>(&k, i);
1159 r->resourcePriv().setUniqueKey(k);
1160 // Leave a ref on every other resource, beginning with the first.
1161 if (SkToBool(i & 0x1)) {
1162 refedResources[i/2] = r;
1163 } else {
1164 r->unref();
1165 }
1166 cache->notifyFlushOccurred();
1167 }
1168
1169 for (int i = 0; i < kFlushCount; ++i) {
1170 // Should get a resource purged every other flush.
1171 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1172 cache->notifyFlushOccurred();
1173 }
1174
1175 // Unref all the resources that we kept refs on in the first loop.
1176 for (int i = 0; i < kFlushCount >> 1; ++i) {
1177 refedResources[i]->unref();
1178 }
1179
1180 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1181 // get kFlushCount additional flushes. Then everything should be purged.
1182 for (int i = 0; i < kFlushCount; ++i) {
1183 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1184 cache->notifyFlushOccurred();
1185 }
1186 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1187
1188 cache->purgeAllUnlocked();
1189 }
1190
1191 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1192}
1193
bsalomon10e23ca2014-11-25 05:52:06 -08001194static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001195 // Set the cache size to double the resource count because we're going to create 2x that number
1196 // resources, using two different key domains. Add a little slop to the bytes because we resize
1197 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001198 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001199
bsalomonc2f35b72015-01-23 07:19:22 -08001200 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1201 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001202 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001203
1204 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001205 GrUniqueKey key1, key2;
1206 make_unique_key<1>(&key1, i);
1207 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001208
bsalomon24db3b12015-01-23 04:24:04 -08001209 TestResource* resource;
1210
halcanary385fe4d2015-08-26 13:07:48 -07001211 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001212 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001213 resource->setSize(1);
1214 resource->unref();
1215
halcanary385fe4d2015-08-26 13:07:48 -07001216 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001217 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001218 resource->setSize(1);
1219 resource->unref();
1220 }
1221
1222 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001223 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1224 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1225 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1226 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001227 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001228 GrUniqueKey key1, key2;
1229 make_unique_key<1>(&key1, i);
1230 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001231
bsalomon8718aaf2015-02-19 07:24:21 -08001232 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1233 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001234 }
1235
bsalomon0ea80f42015-02-11 10:49:59 -08001236 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001237 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001238 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1239 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1240 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1241 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001242
1243 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001244 GrUniqueKey key1, key2;
1245 make_unique_key<1>(&key1, i);
1246 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001247
bsalomon8718aaf2015-02-19 07:24:21 -08001248 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1249 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001250 }
1251}
1252
senorblanco84cd6212015-08-04 10:01:58 -07001253static void test_custom_data(skiatest::Reporter* reporter) {
1254 GrUniqueKey key1, key2;
1255 make_unique_key<0>(&key1, 1);
1256 make_unique_key<0>(&key2, 2);
1257 int foo = 4132;
1258 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1259 key1.setCustomData(data.get());
1260 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1261 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1262
1263 // Test that copying a key also takes a ref on its custom data.
1264 GrUniqueKey key3 = key1;
1265 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1266}
1267
bsalomonc6363ef2015-09-24 07:07:40 -07001268static void test_abandoned(skiatest::Reporter* reporter) {
1269 Mock mock(10, 300);
1270 GrContext* context = mock.context();
Brian Salomon89438a12015-09-24 13:22:45 -04001271 SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001272 context->abandonContext();
1273
1274 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1275
1276 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1277
1278 int foo = 4132;
1279 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1280 resource->setCustomData(data.get());
1281 resource->getCustomData();
1282 resource->getUniqueID();
1283 resource->getUniqueKey();
1284 resource->wasDestroyed();
1285 resource->gpuMemorySize();
1286 resource->getContext();
1287
1288 resource->abandon();
1289 resource->resourcePriv().getScratchKey();
1290 resource->resourcePriv().isBudgeted();
1291 resource->resourcePriv().makeBudgeted();
1292 resource->resourcePriv().makeUnbudgeted();
1293 resource->resourcePriv().removeScratchKey();
1294 GrUniqueKey key;
1295 make_unique_key<0>(&key, 1);
1296 resource->resourcePriv().setUniqueKey(key);
1297 resource->resourcePriv().removeUniqueKey();
1298}
1299
kkinnunen15302832015-12-01 04:35:26 -08001300DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001301 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001302 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001303 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001304 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001305 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001306 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001307 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001308 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001309 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001310 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001311 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001312 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001313 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001314 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001315 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001316 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001317 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001318}
1319
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001320#endif