blob: ee8d7c7eea7bbe39cdba373b7e9fa1d42b75c4fd [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,
41 SkSurface::kNo_Budgeted, info));
42 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.
bsalomond309e7a2015-04-30 14:18:54 -070094 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true));
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
bsalomond309e7a2015-04-30 14:18:54 -070099 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true));
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.
bsalomond309e7a2015-04-30 14:18:54 -0700111 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false));
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;
bsalomond309e7a2015-04-30 14:18:54 -0700128 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false));
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) {
bsalomon02a44a42015-02-19 09:09:00 -0800139 // An RT with a different sample count should not share.
140 GrSurfaceDesc smallMSAADesc = smallDesc;
141 smallMSAADesc.fSampleCnt = 4;
bsalomond309e7a2015-04-30 14:18:54 -0700142 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false));
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.
bsalomond309e7a2015-04-30 14:18:54 -0700158 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
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;
bsalomond309e7a2015-04-30 14:18:54 -0700174 smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
175 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800176 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700177 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800178 }
179 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700180 smallMSAART0 && smallMSAART1 &&
181 smallMSAART0->asRenderTarget() &&
182 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700183 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
184 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800185 }
186 }
187}
188
kkinnunen15302832015-12-01 04:35:26 -0800189DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, context) {
jvanverth672bb7f2015-07-13 07:19:57 -0700190 const GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700191 // this test is only valid for GL
192 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700193 return;
194 }
195
bsalomon091f60c2015-11-10 11:54:56 -0800196 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700197 static const int kW = 100;
198 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700199
bsalomon091f60c2015-11-10 11:54:56 -0800200 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
201 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700202
bsalomon6dc6f5f2015-06-18 09:12:16 -0700203 context->resetContext();
204
205 GrBackendTextureDesc desc;
206 desc.fConfig = kBGRA_8888_GrPixelConfig;
207 desc.fWidth = kW;
208 desc.fHeight = kH;
209
bsalomon091f60c2015-11-10 11:54:56 -0800210 desc.fTextureHandle = texHandles[0];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700211 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700212 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700213
bsalomon091f60c2015-11-10 11:54:56 -0800214 desc.fTextureHandle = texHandles[1];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700215 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700216 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217
218 REPORTER_ASSERT(reporter, SkToBool(borrowed) && SkToBool(adopted));
219 if (!SkToBool(borrowed) || !SkToBool(adopted)) {
220 return;
221 }
222
halcanary96fcdcc2015-08-27 07:41:13 -0700223 borrowed.reset(nullptr);
224 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225
226 context->flush();
227
bsalomon091f60c2015-11-10 11:54:56 -0800228 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
229 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
231 REPORTER_ASSERT(reporter, borrowedIsAlive);
232 REPORTER_ASSERT(reporter, !adoptedIsAlive);
233
bsalomon67d76202015-11-11 12:40:42 -0800234 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
235 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700236
237 context->resetContext();
238}
239
bsalomon6d3fe022014-07-25 08:35:45 -0700240class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800241 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000242public:
robertphillips6e83ac72015-08-13 05:19:14 -0700243 static const size_t kDefaultSize = 100;
mtklein2766c002015-06-26 11:45:03 -0700244
bsalomon1c60dfe2015-01-21 09:32:40 -0800245 /** Property that distinctly categorizes the resource.
246 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800247 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800248
bsalomon5236cf42015-01-14 10:42:08 -0800249 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
250 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700251 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800252 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800253 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800254 ++fNumAlive;
255 this->registerWithCache();
256 }
257
258 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
259 : INHERITED(gpu, lifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700260 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800261 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800262 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800263 ++fNumAlive;
264 this->registerWithCache();
265 }
266
bsalomon8b79d232014-11-10 10:19:06 -0800267 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800268 : INHERITED(gpu, kCached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700269 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800270 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800271 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800272 ++fNumAlive;
273 this->registerWithCache();
274 }
275
bsalomon23e619c2015-02-06 11:54:28 -0800276 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
halcanary385fe4d2015-08-26 13:07:48 -0700277 return new TestResource(gpu, property, cached, kScratchConstructor);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278 }
279
280 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800281 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800282 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283 }
284
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000285 void setSize(size_t size) {
286 fSize = size;
287 this->didChangeGpuMemorySize();
288 }
289
bsalomon33435572014-11-05 14:47:41 -0800290 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000291
bsalomon71cb0c22014-11-14 12:10:14 -0800292 void setUnrefWhenDestroyed(TestResource* resource) {
293 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294 }
295
bsalomon1c60dfe2015-01-21 09:32:40 -0800296 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
297 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
298 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800299 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
300 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800301 }
302 }
303
304 static size_t ExpectedScratchKeySize() {
305 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
306 }
307
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000308private:
bsalomon24db3b12015-01-23 04:24:04 -0800309 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800310
bsalomonc2f35b72015-01-23 07:19:22 -0800311 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
312 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700313 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 , fSize(kDefaultSize)
315 , fProperty(property) {
316 GrScratchKey scratchKey;
317 ComputeScratchKey(fProperty, &scratchKey);
318 this->setScratchKey(scratchKey);
319 ++fNumAlive;
320 this->registerWithCache();
321 }
322
mtklein36352bf2015-03-25 18:17:31 -0700323 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800324
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000325 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000326 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800327 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800328 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700329 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000330};
bsalomon33435572014-11-05 14:47:41 -0800331int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000332
bsalomonc2f35b72015-01-23 07:19:22 -0800333class Mock {
334public:
335 Mock(int maxCnt, size_t maxBytes) {
336 fContext.reset(GrContext::CreateMockContext());
337 SkASSERT(fContext);
338 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800339 GrResourceCache* cache = fContext->getResourceCache();
340 cache->purgeAllUnlocked();
341 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800342 }
bsalomonc2f35b72015-01-23 07:19:22 -0800343
bsalomon0ea80f42015-02-11 10:49:59 -0800344 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800345
346 GrContext* context() { return fContext; }
347
348private:
349 SkAutoTUnref<GrContext> fContext;
350};
351
352static void test_no_key(skiatest::Reporter* reporter) {
353 Mock mock(10, 30000);
354 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800355 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800356
357 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700358 TestResource* a = new TestResource(context->getGpu());
359 TestResource* b = new TestResource(context->getGpu());
360 TestResource* c = new TestResource(context->getGpu());
361 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800362 a->setSize(11);
363 b->setSize(12);
364 c->setSize(13);
365 d->setSize(14);
366
367 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800368 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800369 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800370 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800371
372 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800373 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800374 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
375
bsalomon8718aaf2015-02-19 07:24:21 -0800376 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800377
378 a->unref();
379 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800380 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800381 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800382 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 c->unref();
385 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800386 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800387 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800388 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800389
390 d->unref();
391 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800392 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
393 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 b->unref();
396 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
398 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399}
400
bsalomon24db3b12015-01-23 04:24:04 -0800401// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800402template <int> static void make_unique_key(GrUniqueKey* key, int data) {
403 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
404 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800405 builder[0] = data;
406}
407
bsalomon84c8e622014-11-17 09:33:27 -0800408static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800409 Mock mock(10, 300);
410 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800411 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800412
bsalomon8718aaf2015-02-19 07:24:21 -0800413 GrUniqueKey uniqueKey;
414 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800415
bsalomon8718aaf2015-02-19 07:24:21 -0800416 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800417 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800418 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800419 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700420 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800421 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800422 unique->resourcePriv().setUniqueKey(uniqueKey);
halcanary385fe4d2015-08-26 13:07:48 -0700423 TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomon5236cf42015-01-14 10:42:08 -0800424 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700425 TestResource* unbudgeted =
426 new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCycle);
bsalomon84c8e622014-11-17 09:33:27 -0800427 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 // Make sure we can't add a unique key to the wrapped resource
430 GrUniqueKey uniqueKey2;
431 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800432 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700433 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800434
435 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800436 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800437 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800438 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800439 cache->getResourceBytes());
440 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800441 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800442 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800443
bsalomon63c992f2015-01-23 12:47:59 -0800444 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800445 cache->purgeAllUnlocked();
446 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800447 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800448 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800449 cache->getResourceBytes());
450 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800451 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800452 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800453
454 // Unreffing the wrapped resource should free it right away.
455 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800456 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800458 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800459
bsalomon84c8e622014-11-17 09:33:27 -0800460 // Now try freeing the budgeted resources first
halcanary385fe4d2015-08-26 13:07:48 -0700461 wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
bsalomondace19e2014-11-17 07:34:06 -0800462 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800463 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800464 cache->purgeAllUnlocked();
465 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800466 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800467 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
468 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800470
471 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800472 cache->purgeAllUnlocked();
473 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800474 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800475 cache->getResourceBytes());
476 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
477 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800478
479 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
482 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
483 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800484
485 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800486 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
487 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
488 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
489 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800490}
491
bsalomon5236cf42015-01-14 10:42:08 -0800492static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800493 Mock mock(10, 30000);
494 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800495 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800496
bsalomon8718aaf2015-02-19 07:24:21 -0800497 GrUniqueKey uniqueKey;
498 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800499
500 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800501 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800502 TestResource* wrapped;
503 TestResource* unbudgeted;
504
505 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800506 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800507 scratch->setSize(10);
508 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800509 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
510 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
511 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
512 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800513
halcanary385fe4d2015-08-26 13:07:48 -0700514 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800515 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800516 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800517 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800518 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
519 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
520 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
521 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800522
bsalomon0ea80f42015-02-11 10:49:59 -0800523 size_t large = 2 * cache->getResourceBytes();
halcanary385fe4d2015-08-26 13:07:48 -0700524 unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUncached_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800525 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
526 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
527 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
528 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800529
530 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800531 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
532 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
533 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
534 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800535
halcanary385fe4d2015-08-26 13:07:48 -0700536 wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle);
bsalomon0ea80f42015-02-11 10:49:59 -0800537 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
538 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
539 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
540 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800541
542 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800543 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
544 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
545 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
546 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800547
bsalomon0ea80f42015-02-11 10:49:59 -0800548 cache->purgeAllUnlocked();
549 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
550 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
551 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
552 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800553}
554
bsalomon3582d3e2015-02-13 14:20:05 -0800555// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
556void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
557/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800558 Mock mock(10, 300);
559 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800560 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800561
562 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800563 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800564 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800565 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800566
567 size_t size = resource->gpuMemorySize();
568 for (int i = 0; i < 2; ++i) {
569 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800570 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800571 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800572 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700573 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800574 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
575 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
576 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
577 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800578
579 // Once it is unrefed, it should become available as scratch.
580 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800581 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
582 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
583 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
584 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700585 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800586 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800587 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800588 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800589 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800590
591 if (0 == i) {
592 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
593 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800594 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800595 } else {
596 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800597 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
599 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
600 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
601 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800602 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800603 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800604 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800605
606 // now when it is unrefed it should die since it has no key.
607 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800608 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
609 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
610 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
611 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800612 }
bsalomon8b79d232014-11-10 10:19:06 -0800613 }
bsalomonc2f35b72015-01-23 07:19:22 -0800614}
615
616static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
617 Mock mock(5, 30000);
618 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800619 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800620
bsalomon8b79d232014-11-10 10:19:06 -0800621 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800622 TestResource* a = TestResource::CreateScratch(context->getGpu(),
623 TestResource::kB_SimulatedProperty);
624 TestResource* b = TestResource::CreateScratch(context->getGpu(),
625 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800626 a->setSize(11);
627 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800628 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800629 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800630 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700631 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800632
633 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800634 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800635
bsalomon0ea80f42015-02-11 10:49:59 -0800636 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800637 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800638 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
639 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800640 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800641 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800642
bsalomon63c992f2015-01-23 12:47:59 -0800643 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800644 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800645 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800646 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800647
648 // Unref but don't purge
649 a->unref();
650 b->unref();
651 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800652 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800653
bsalomon63c992f2015-01-23 12:47:59 -0800654 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800655 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800656 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800657 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
658 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800659}
660
bsalomon10e23ca2014-11-25 05:52:06 -0800661static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800662 Mock mock(5, 30000);
663 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800664 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800665
bsalomon10e23ca2014-11-25 05:52:06 -0800666 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800667 TestResource* a = TestResource::CreateScratch(context->getGpu(),
668 TestResource::kB_SimulatedProperty);
669 TestResource* b = TestResource::CreateScratch(context->getGpu(),
670 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800671 a->unref();
672 b->unref();
673
bsalomon1c60dfe2015-01-21 09:32:40 -0800674 GrScratchKey scratchKey;
675 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800677 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700678 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800679
bsalomon0ea80f42015-02-11 10:49:59 -0800680 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800681 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800682 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800683 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
684 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800685
686 // Find the first resource and remove its scratch key
687 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700688 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800689 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800690 // It's still alive, but not cached by scratch key anymore
691 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800692 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
693 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800694
695 // The cache should immediately delete it when it's unrefed since it isn't accessible.
696 find->unref();
697 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800698 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
699 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800700
701 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700702 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800703 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800704 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800705 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
706 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800707
708 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800709 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800710 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800711 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
712 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800713
714 find->unref();
715 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800716 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
717 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800718}
719
bsalomon1c60dfe2015-01-21 09:32:40 -0800720static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800721 Mock mock(5, 30000);
722 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800723 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800724
725 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800726 TestResource* a = TestResource::CreateScratch(context->getGpu(),
727 TestResource::kB_SimulatedProperty);
728 TestResource* b = TestResource::CreateScratch(context->getGpu(),
729 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800730 a->unref();
731 b->unref();
732
733 GrScratchKey scratchKey;
734 // Ensure that scratch key comparison and assignment is consistent.
735 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800736 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800737 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800738 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800739 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
740 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
741 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
742 scratchKey = scratchKey1;
743 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
744 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
745 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
746 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
747 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
748 scratchKey = scratchKey2;
749 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
750 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
751 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
752 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
753 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
754
755 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800756 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800757 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700758 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800759
760 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800761 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700762 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700763 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800764 find->unref();
765
766 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700767 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700768 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800769 REPORTER_ASSERT(reporter, find == a || find == b);
770
robertphillips6e83ac72015-08-13 05:19:14 -0700771 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700772 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800773 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
774 REPORTER_ASSERT(reporter, find2 != find);
775 find2->unref();
776 find->unref();
777}
778
bsalomon8718aaf2015-02-19 07:24:21 -0800779static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800780 Mock mock(5, 30000);
781 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800782 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000783
bsalomon8718aaf2015-02-19 07:24:21 -0800784 GrUniqueKey key;
785 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800786
bsalomon8718aaf2015-02-19 07:24:21 -0800787 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700788 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800789 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800790
bsalomonf99e9612015-02-19 08:24:16 -0800791 // Set key on resource a.
792 a->resourcePriv().setUniqueKey(key);
793 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
794 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800795
bsalomonf99e9612015-02-19 08:24:16 -0800796 // Make sure that redundantly setting a's key works.
797 a->resourcePriv().setUniqueKey(key);
798 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800799 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800800 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
801 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800802 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
803
bsalomonf99e9612015-02-19 08:24:16 -0800804 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700805 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800806 b->setSize(12);
807 b->resourcePriv().setUniqueKey(key);
808 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
809 b->unref();
810
811 // Still have two resources because a is still reffed.
812 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
813 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
814 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
815
816 a->unref();
817 // Now a should be gone.
818 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
819 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
820 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
821
822 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
823 // Also make b be unreffed when replacement occurs.
824 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700825 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800826 GrUniqueKey differentKey;
827 make_unique_key<0>(&differentKey, 1);
828 c->setSize(13);
829 c->resourcePriv().setUniqueKey(differentKey);
830 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
831 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
832 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
833 // c replaces b and b should be immediately purged.
834 c->resourcePriv().setUniqueKey(key);
835 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
836 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
837 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
838
839 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800840 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800841 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
842 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
843 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
844
845 // Drop the ref on c, it should be kept alive because it has a unique key.
846 c->unref();
847 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
848 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
849 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
850
851 // Verify that we can find c, then remove its unique key. It should get purged immediately.
852 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
853 c->resourcePriv().removeUniqueKey();
854 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800855 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
856 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800857 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700858
859 {
860 GrUniqueKey key2;
861 make_unique_key<0>(&key2, 0);
halcanary385fe4d2015-08-26 13:07:48 -0700862 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700863 int foo = 4132;
864 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
865 key2.setCustomData(data.get());
866 d->resourcePriv().setUniqueKey(key2);
867 }
868
869 GrUniqueKey key3;
870 make_unique_key<0>(&key3, 0);
871 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
872 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000873}
874
bsalomon8b79d232014-11-10 10:19:06 -0800875static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800876 Mock mock(5, 30000);
877 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800878 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800879
bsalomon8718aaf2015-02-19 07:24:21 -0800880 GrUniqueKey key1, key2, key3;
881 make_unique_key<0>(&key1, 1);
882 make_unique_key<0>(&key2, 2);
883 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800884
bsalomon23e619c2015-02-06 11:54:28 -0800885 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700886 TestResource* a = new TestResource(context->getGpu());
887 TestResource* b = new TestResource(context->getGpu());
bsalomon23e619c2015-02-06 11:54:28 -0800888 TestResource* c = TestResource::CreateScratch(context->getGpu(),
889 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800890 a->resourcePriv().setUniqueKey(key1);
891 b->resourcePriv().setUniqueKey(key2);
892 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800893 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800894 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800895 c->unref();
896
bsalomon8718aaf2015-02-19 07:24:21 -0800897 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
898 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
899 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800900 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800901
bsalomon8718aaf2015-02-19 07:24:21 -0800902 typedef GrUniqueKeyInvalidatedMessage Msg;
903 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800904
905 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
906 Bus::Post(Msg(key1));
907 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800908 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800909 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800910 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
911 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800912 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800913 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800914
915 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800916 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800917 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800918 // we still have a ref on b, c should be recycled as scratch.
919 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800920 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800921
bsalomon23e619c2015-02-06 11:54:28 -0800922 // make b purgeable. It should be immediately deleted since it has no key.
923 b->unref();
924 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
925
926 // Make sure we actually get to c via it's scratch key, before we say goodbye.
927 GrScratchKey scratchKey;
928 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700929 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800930 REPORTER_ASSERT(reporter, scratch == c);
931 SkSafeUnref(scratch);
932
933 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800934 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700935 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800936 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800937 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
938 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800939 REPORTER_ASSERT(reporter, !scratch);
940 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800941}
942
bsalomon71cb0c22014-11-14 12:10:14 -0800943static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800944 Mock mock(3, 30000);
945 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800946 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800947
bsalomon8718aaf2015-02-19 07:24:21 -0800948 GrUniqueKey key1, key2;
949 make_unique_key<0>(&key1, 1);
950 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000951
halcanary385fe4d2015-08-26 13:07:48 -0700952 TestResource* a = new TestResource(context->getGpu());
953 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800954 a->resourcePriv().setUniqueKey(key1);
955 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800956
bsalomonc2f35b72015-01-23 07:19:22 -0800957 // Make a cycle
958 a->setUnrefWhenDestroyed(b);
959 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800960
bsalomonc2f35b72015-01-23 07:19:22 -0800961 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800962
bsalomonc2f35b72015-01-23 07:19:22 -0800963 a->unref();
964 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800965
bsalomonc2f35b72015-01-23 07:19:22 -0800966 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800967
bsalomon0ea80f42015-02-11 10:49:59 -0800968 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800969 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800970
bsalomonc2f35b72015-01-23 07:19:22 -0800971 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700972 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800973 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800974
bsalomon0ea80f42015-02-11 10:49:59 -0800975 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800976 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000977}
978
bsalomon8b79d232014-11-10 10:19:06 -0800979static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800980 GrUniqueKey key1, key2;
981 make_unique_key<0>(&key1, 1);
982 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000983
984 // Test changing resources sizes (both increase & decrease).
985 {
bsalomonc2f35b72015-01-23 07:19:22 -0800986 Mock mock(3, 30000);
987 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800988 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000989
halcanary385fe4d2015-08-26 13:07:48 -0700990 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800991 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000992 a->unref();
993
halcanary385fe4d2015-08-26 13:07:48 -0700994 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800995 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000996 b->unref();
997
bsalomon0ea80f42015-02-11 10:49:59 -0800998 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
999 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001000 {
bsalomon8718aaf2015-02-19 07:24:21 -08001001 SkAutoTUnref<TestResource> find2(
1002 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001003 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -08001004 SkAutoTUnref<TestResource> find1(
1005 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001006 find1->setSize(50);
1007 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001008
bsalomon0ea80f42015-02-11 10:49:59 -08001009 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1010 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001011 }
1012
1013 // Test increasing a resources size beyond the cache budget.
1014 {
bsalomonc2f35b72015-01-23 07:19:22 -08001015 Mock mock(2, 300);
1016 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001017 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001018
halcanary385fe4d2015-08-26 13:07:48 -07001019 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001020 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001021 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001022 a->unref();
1023
halcanary385fe4d2015-08-26 13:07:48 -07001024 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001025 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001026 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001027 b->unref();
1028
bsalomon0ea80f42015-02-11 10:49:59 -08001029 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1030 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001031
bsalomon8b79d232014-11-10 10:19:06 -08001032 {
bsalomon8718aaf2015-02-19 07:24:21 -08001033 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1034 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001035 find2->setSize(201);
1036 }
bsalomon8718aaf2015-02-19 07:24:21 -08001037 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001038
bsalomon0ea80f42015-02-11 10:49:59 -08001039 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1040 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001041 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001042}
1043
bsalomonddf30e62015-02-19 11:38:44 -08001044static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1045 static const int kCount = 50;
1046 static const int kBudgetCnt = kCount / 2;
1047 static const int kLockedFreq = 8;
1048 static const int kBudgetSize = 0x80000000;
1049
1050 SkRandom random;
1051
1052 // Run the test 2*kCount times;
1053 for (int i = 0; i < 2 * kCount; ++i ) {
1054 Mock mock(kBudgetCnt, kBudgetSize);
1055 GrContext* context = mock.context();
1056 GrResourceCache* cache = mock.cache();
1057
1058 // Pick a random number of resources to add before the timestamp will wrap.
1059 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1060
1061 static const int kNumToPurge = kCount - kBudgetCnt;
1062
1063 SkTDArray<int> shouldPurgeIdxs;
1064 int purgeableCnt = 0;
1065 SkTDArray<GrGpuResource*> resourcesToUnref;
1066
1067 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1068 // unpurgeable resources.
1069 for (int j = 0; j < kCount; ++j) {
1070 GrUniqueKey key;
1071 make_unique_key<0>(&key, j);
1072
halcanary385fe4d2015-08-26 13:07:48 -07001073 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001074 r->resourcePriv().setUniqueKey(key);
1075 if (random.nextU() % kLockedFreq) {
1076 // Make this is purgeable.
1077 r->unref();
1078 ++purgeableCnt;
1079 if (purgeableCnt <= kNumToPurge) {
1080 *shouldPurgeIdxs.append() = j;
1081 }
1082 } else {
1083 *resourcesToUnref.append() = r;
1084 }
1085 }
1086
1087 // Verify that the correct resources were purged.
1088 int currShouldPurgeIdx = 0;
1089 for (int j = 0; j < kCount; ++j) {
1090 GrUniqueKey key;
1091 make_unique_key<0>(&key, j);
1092 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1093 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1094 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1095 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001096 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001097 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001098 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001099 }
1100 SkSafeUnref(res);
1101 }
1102
1103 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1104 resourcesToUnref[j]->unref();
1105 }
1106 }
1107}
1108
bsalomon3f324322015-04-08 11:01:54 -07001109static void test_flush(skiatest::Reporter* reporter) {
1110 Mock mock(1000000, 1000000);
1111 GrContext* context = mock.context();
1112 GrResourceCache* cache = mock.cache();
1113
1114 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1115 // power of two here to keep things simpler.
1116 static const int kFlushCount = 16;
1117 cache->setLimits(1000000, 1000000, kFlushCount);
1118
1119 {
1120 // Insert a resource and send a flush notification kFlushCount times.
1121 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001122 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001123 GrUniqueKey k;
1124 make_unique_key<1>(&k, i);
1125 r->resourcePriv().setUniqueKey(k);
1126 r->unref();
1127 cache->notifyFlushOccurred();
1128 }
1129
1130 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1131 for (int i = 0; i < kFlushCount - 1; ++i) {
1132 // The first resource was purged after the last flush in the initial loop, hence the -1.
1133 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1134 for (int j = 0; j < i; ++j) {
1135 GrUniqueKey k;
1136 make_unique_key<1>(&k, j);
1137 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1138 REPORTER_ASSERT(reporter, !SkToBool(r));
1139 SkSafeUnref(r);
1140 }
1141 cache->notifyFlushOccurred();
1142 }
1143
1144 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1145 cache->purgeAllUnlocked();
1146 }
1147
1148 // Do a similar test but where we leave refs on some resources to prevent them from being
1149 // purged.
1150 {
1151 GrGpuResource* refedResources[kFlushCount >> 1];
1152 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001153 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001154 GrUniqueKey k;
1155 make_unique_key<1>(&k, i);
1156 r->resourcePriv().setUniqueKey(k);
1157 // Leave a ref on every other resource, beginning with the first.
1158 if (SkToBool(i & 0x1)) {
1159 refedResources[i/2] = r;
1160 } else {
1161 r->unref();
1162 }
1163 cache->notifyFlushOccurred();
1164 }
1165
1166 for (int i = 0; i < kFlushCount; ++i) {
1167 // Should get a resource purged every other flush.
1168 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1169 cache->notifyFlushOccurred();
1170 }
1171
1172 // Unref all the resources that we kept refs on in the first loop.
1173 for (int i = 0; i < kFlushCount >> 1; ++i) {
1174 refedResources[i]->unref();
1175 }
1176
1177 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1178 // get kFlushCount additional flushes. Then everything should be purged.
1179 for (int i = 0; i < kFlushCount; ++i) {
1180 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1181 cache->notifyFlushOccurred();
1182 }
1183 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1184
1185 cache->purgeAllUnlocked();
1186 }
1187
1188 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1189}
1190
bsalomon10e23ca2014-11-25 05:52:06 -08001191static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001192 // Set the cache size to double the resource count because we're going to create 2x that number
1193 // resources, using two different key domains. Add a little slop to the bytes because we resize
1194 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001195 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001196
bsalomonc2f35b72015-01-23 07:19:22 -08001197 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1198 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001199 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001200
1201 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001202 GrUniqueKey key1, key2;
1203 make_unique_key<1>(&key1, i);
1204 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001205
bsalomon24db3b12015-01-23 04:24:04 -08001206 TestResource* resource;
1207
halcanary385fe4d2015-08-26 13:07:48 -07001208 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001209 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001210 resource->setSize(1);
1211 resource->unref();
1212
halcanary385fe4d2015-08-26 13:07:48 -07001213 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001214 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001215 resource->setSize(1);
1216 resource->unref();
1217 }
1218
1219 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001220 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1221 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1222 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1223 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001224 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001225 GrUniqueKey key1, key2;
1226 make_unique_key<1>(&key1, i);
1227 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001228
bsalomon8718aaf2015-02-19 07:24:21 -08001229 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1230 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001231 }
1232
bsalomon0ea80f42015-02-11 10:49:59 -08001233 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001234 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001235 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1236 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1237 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1238 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001239
1240 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001241 GrUniqueKey key1, key2;
1242 make_unique_key<1>(&key1, i);
1243 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001244
bsalomon8718aaf2015-02-19 07:24:21 -08001245 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1246 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001247 }
1248}
1249
senorblanco84cd6212015-08-04 10:01:58 -07001250static void test_custom_data(skiatest::Reporter* reporter) {
1251 GrUniqueKey key1, key2;
1252 make_unique_key<0>(&key1, 1);
1253 make_unique_key<0>(&key2, 2);
1254 int foo = 4132;
1255 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1256 key1.setCustomData(data.get());
1257 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1258 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1259
1260 // Test that copying a key also takes a ref on its custom data.
1261 GrUniqueKey key3 = key1;
1262 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1263}
1264
bsalomonc6363ef2015-09-24 07:07:40 -07001265static void test_abandoned(skiatest::Reporter* reporter) {
1266 Mock mock(10, 300);
1267 GrContext* context = mock.context();
Brian Salomon89438a12015-09-24 13:22:45 -04001268 SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001269 context->abandonContext();
1270
1271 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1272
1273 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1274
1275 int foo = 4132;
1276 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1277 resource->setCustomData(data.get());
1278 resource->getCustomData();
1279 resource->getUniqueID();
1280 resource->getUniqueKey();
1281 resource->wasDestroyed();
1282 resource->gpuMemorySize();
1283 resource->getContext();
1284
1285 resource->abandon();
1286 resource->resourcePriv().getScratchKey();
1287 resource->resourcePriv().isBudgeted();
1288 resource->resourcePriv().makeBudgeted();
1289 resource->resourcePriv().makeUnbudgeted();
1290 resource->resourcePriv().removeScratchKey();
1291 GrUniqueKey key;
1292 make_unique_key<0>(&key, 1);
1293 resource->resourcePriv().setUniqueKey(key);
1294 resource->resourcePriv().removeUniqueKey();
1295}
1296
kkinnunen15302832015-12-01 04:35:26 -08001297DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001298 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001299 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001300 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001301 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001302 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001303 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001304 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001305 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001306 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001307 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001308 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001309 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001310 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001311 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001312 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001313 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001314 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001315}
1316
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001317#endif