blob: b568485e4bcfe8c9cd99cf1edb79239fa4be2688 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon3f324322015-04-08 11:01:54 -07008// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourceCacheAccess.h"
17#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTarget.h"
19#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070023#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080024#include "SkGr.h"
25#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070026#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000027#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028
29static const int gWidth = 640;
30static const int gHeight = 480;
31
32////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070033DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070034 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080035 GrSurfaceDesc desc;
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) {
bsalomon8b7451a2016-05-11 06:33:06 -070084 GrContext* context = ctxInfo.grContext();
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) {
bsalomon8b7451a2016-05-11 06:33:06 -0700192 GrContext* context = ctxInfo.grContext();
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
kkinnunen2e6055b2016-04-22 01:48:29 -0700252 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
253 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700254 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800255 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700256 , fProperty(kA_SimulatedProperty)
257 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800258 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700259 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800260 }
261
kkinnunen2e6055b2016-04-22 01:48:29 -0700262 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
263 SimulatedProperty property) {
264 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800265 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700266 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
267 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000268 }
269
270 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800271 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800272 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000273 }
274
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000275 void setSize(size_t size) {
276 fSize = size;
277 this->didChangeGpuMemorySize();
278 }
279
bsalomon33435572014-11-05 14:47:41 -0800280 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281
bsalomon71cb0c22014-11-14 12:10:14 -0800282 void setUnrefWhenDestroyed(TestResource* resource) {
283 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284 }
285
bsalomon1c60dfe2015-01-21 09:32:40 -0800286 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
287 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
288 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800289 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
290 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800291 }
292 }
293
294 static size_t ExpectedScratchKeySize() {
295 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
296 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000297private:
bsalomon24db3b12015-01-23 04:24:04 -0800298 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800299
kkinnunen2e6055b2016-04-22 01:48:29 -0700300 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
301 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700302 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800303 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700304 , fProperty(property)
305 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800306 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700307 this->registerWithCache(budgeted);
308 }
309
310 // Constructor for simulating resources that wrap backend objects.
311 TestResource(GrGpu* gpu, size_t size)
312 : INHERITED(gpu)
313 , fToDelete(nullptr)
314 , fSize(size)
315 , fProperty(kA_SimulatedProperty)
316 , fIsScratch(false) {
317 ++fNumAlive;
318 this->registerWithCacheWrapped();
319 }
320
321 void computeScratchKey(GrScratchKey* key) const override {
322 if (fIsScratch) {
323 ComputeScratchKey(fProperty, key);
324 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800325 }
326
mtklein36352bf2015-03-25 18:17:31 -0700327 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800328
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000329 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000330 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800331 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800332 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700333 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700334 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000335};
bsalomon33435572014-11-05 14:47:41 -0800336int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000337
bsalomonc2f35b72015-01-23 07:19:22 -0800338class Mock {
339public:
340 Mock(int maxCnt, size_t maxBytes) {
341 fContext.reset(GrContext::CreateMockContext());
342 SkASSERT(fContext);
343 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800344 GrResourceCache* cache = fContext->getResourceCache();
345 cache->purgeAllUnlocked();
346 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800347 }
bsalomonc2f35b72015-01-23 07:19:22 -0800348
bsalomon0ea80f42015-02-11 10:49:59 -0800349 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800350
351 GrContext* context() { return fContext; }
352
353private:
354 SkAutoTUnref<GrContext> fContext;
355};
356
357static void test_no_key(skiatest::Reporter* reporter) {
358 Mock mock(10, 30000);
359 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800360 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800361
362 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700363 TestResource* a = new TestResource(context->getGpu());
364 TestResource* b = new TestResource(context->getGpu());
365 TestResource* c = new TestResource(context->getGpu());
366 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800367 a->setSize(11);
368 b->setSize(12);
369 c->setSize(13);
370 d->setSize(14);
371
372 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800373 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800374 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800375 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800378 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800379 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
380
bsalomon8718aaf2015-02-19 07:24:21 -0800381 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 a->unref();
384 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800387 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 c->unref();
390 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800392 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800393 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 d->unref();
396 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
398 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399
400 b->unref();
401 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
403 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404}
405
bsalomon24db3b12015-01-23 04:24:04 -0800406// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800407template <int> static void make_unique_key(GrUniqueKey* key, int data) {
408 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
409 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800410 builder[0] = data;
411}
412
bsalomon84c8e622014-11-17 09:33:27 -0800413static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800414 Mock mock(10, 300);
415 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800416 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800417
bsalomon8718aaf2015-02-19 07:24:21 -0800418 GrUniqueKey uniqueKey;
419 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800420
bsalomon8718aaf2015-02-19 07:24:21 -0800421 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800422 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700423 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800424 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700425 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800426 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800427 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700428 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800429 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700430 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700431 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800432 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800433
bsalomon8718aaf2015-02-19 07:24:21 -0800434 // Make sure we can't add a unique key to the wrapped resource
435 GrUniqueKey uniqueKey2;
436 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800437 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700438 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800439
440 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800441 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800442 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800443 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800444 cache->getResourceBytes());
445 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800446 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800447 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800448
bsalomon63c992f2015-01-23 12:47:59 -0800449 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800450 cache->purgeAllUnlocked();
451 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800452 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800453 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800454 cache->getResourceBytes());
455 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800456 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800457 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800458
459 // Unreffing the wrapped resource should free it right away.
460 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800461 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800462 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800463 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800464
bsalomon84c8e622014-11-17 09:33:27 -0800465 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700466 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800467 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800468 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800469 cache->purgeAllUnlocked();
470 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800471 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800472 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
473 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
474 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800475
476 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800477 cache->purgeAllUnlocked();
478 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800479 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800480 cache->getResourceBytes());
481 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
482 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800483
484 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800485 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
486 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
487 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
488 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800489
490 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800491 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
492 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
493 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
494 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800495}
496
bsalomon5236cf42015-01-14 10:42:08 -0800497static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800498 Mock mock(10, 30000);
499 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800500 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800501
bsalomon8718aaf2015-02-19 07:24:21 -0800502 GrUniqueKey uniqueKey;
503 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800504
505 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800506 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800507 TestResource* wrapped;
508 TestResource* unbudgeted;
509
510 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700511 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
512 TestResource::kB_SimulatedProperty);
513
bsalomon5236cf42015-01-14 10:42:08 -0800514 scratch->setSize(10);
515 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800516 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
517 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
518 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
519 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800520
halcanary385fe4d2015-08-26 13:07:48 -0700521 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800522 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800523 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800524 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800525 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
526 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
527 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
528 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800529
bsalomon0ea80f42015-02-11 10:49:59 -0800530 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700531 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800536
537 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800538 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
539 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
540 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
541 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800542
kkinnunen2e6055b2016-04-22 01:48:29 -0700543 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800544 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
545 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
546 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
547 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800548
549 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800550 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800554
bsalomon0ea80f42015-02-11 10:49:59 -0800555 cache->purgeAllUnlocked();
556 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
557 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
558 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
559 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800560}
561
bsalomon3582d3e2015-02-13 14:20:05 -0800562// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
563void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
564/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800565 Mock mock(10, 300);
566 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800567 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800568
569 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700570 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
571 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800572 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800573 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800574
575 size_t size = resource->gpuMemorySize();
576 for (int i = 0; i < 2; ++i) {
577 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800578 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800579 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800580 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700581 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800582 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800586
587 // Once it is unrefed, it should become available as scratch.
588 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800589 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
590 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
591 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
592 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700593 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800594 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800595 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800596 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800597 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800598
599 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700600 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800601 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800602 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800603 } else {
604 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800605 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800606 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
607 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
608 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
609 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800610 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800611 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800612 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800613
614 // now when it is unrefed it should die since it has no key.
615 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800616 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
617 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
618 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
619 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800620 }
bsalomon8b79d232014-11-10 10:19:06 -0800621 }
bsalomonc2f35b72015-01-23 07:19:22 -0800622}
623
624static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
625 Mock mock(5, 30000);
626 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800627 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800628
bsalomon8b79d232014-11-10 10:19:06 -0800629 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800630 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700631 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800632 TestResource::kB_SimulatedProperty);
633 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700634 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800635 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800636 a->setSize(11);
637 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800638 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800639 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800640 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700641 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800642
643 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800644 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800645
bsalomon0ea80f42015-02-11 10:49:59 -0800646 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800647 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800648 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
649 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800650 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800651 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800652
bsalomon63c992f2015-01-23 12:47:59 -0800653 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800654 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800655 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800656 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800657
658 // Unref but don't purge
659 a->unref();
660 b->unref();
661 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800662 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800663
bsalomon63c992f2015-01-23 12:47:59 -0800664 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800665 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800666 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800667 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
668 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800669}
670
bsalomon10e23ca2014-11-25 05:52:06 -0800671static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800672 Mock mock(5, 30000);
673 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800674 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800675
bsalomon10e23ca2014-11-25 05:52:06 -0800676 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700677 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800678 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700679 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800680 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800681 a->unref();
682 b->unref();
683
bsalomon1c60dfe2015-01-21 09:32:40 -0800684 GrScratchKey scratchKey;
685 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800686 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800687 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700688 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800689
bsalomon0ea80f42015-02-11 10:49:59 -0800690 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800691 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800692 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800693 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
694 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800695
696 // Find the first resource and remove its scratch key
697 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700698 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800699 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800700 // It's still alive, but not cached by scratch key anymore
701 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800702 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
703 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800704
705 // The cache should immediately delete it when it's unrefed since it isn't accessible.
706 find->unref();
707 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800708 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
709 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800710
711 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700712 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800713 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800714 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800715 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
716 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800717
718 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800719 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800720 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800721 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
722 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800723
724 find->unref();
725 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800726 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
727 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800728}
729
bsalomon1c60dfe2015-01-21 09:32:40 -0800730static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800731 Mock mock(5, 30000);
732 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800733 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800734
735 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700736 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800737 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700738 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800739 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800740 a->unref();
741 b->unref();
742
743 GrScratchKey scratchKey;
744 // Ensure that scratch key comparison and assignment is consistent.
745 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800746 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800747 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800748 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800749 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
750 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
751 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
752 scratchKey = scratchKey1;
753 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
754 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
755 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
756 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
757 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
758 scratchKey = scratchKey2;
759 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
760 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
761 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
762 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
763 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
764
765 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800766 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800767 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700768 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800769
770 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800771 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700772 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700773 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800774 find->unref();
775
776 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700777 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700778 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800779 REPORTER_ASSERT(reporter, find == a || find == b);
780
robertphillips6e83ac72015-08-13 05:19:14 -0700781 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700782 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800783 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
784 REPORTER_ASSERT(reporter, find2 != find);
785 find2->unref();
786 find->unref();
787}
788
bsalomon8718aaf2015-02-19 07:24:21 -0800789static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800790 Mock mock(5, 30000);
791 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800792 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000793
bsalomon8718aaf2015-02-19 07:24:21 -0800794 GrUniqueKey key;
795 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700796
bsalomon8718aaf2015-02-19 07:24:21 -0800797 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700798 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800799 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700800
bsalomonf99e9612015-02-19 08:24:16 -0800801 // Set key on resource a.
802 a->resourcePriv().setUniqueKey(key);
803 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
804 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800805
bsalomonf99e9612015-02-19 08:24:16 -0800806 // Make sure that redundantly setting a's key works.
807 a->resourcePriv().setUniqueKey(key);
808 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800809 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800810 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
811 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800812 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
813
bsalomonf99e9612015-02-19 08:24:16 -0800814 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700815 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800816 b->setSize(12);
817 b->resourcePriv().setUniqueKey(key);
818 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
819 b->unref();
820
821 // Still have two resources because a is still reffed.
822 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
823 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
824 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
825
826 a->unref();
827 // Now a should be gone.
828 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
829 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
830 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
831
832 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
833 // Also make b be unreffed when replacement occurs.
834 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700835 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800836 GrUniqueKey differentKey;
837 make_unique_key<0>(&differentKey, 1);
838 c->setSize(13);
839 c->resourcePriv().setUniqueKey(differentKey);
840 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
841 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
842 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
843 // c replaces b and b should be immediately purged.
844 c->resourcePriv().setUniqueKey(key);
845 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
846 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
847 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
848
849 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800850 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800851 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
852 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
853 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
854
855 // Drop the ref on c, it should be kept alive because it has a unique key.
856 c->unref();
857 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
859 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
860
861 // Verify that we can find c, then remove its unique key. It should get purged immediately.
862 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
863 c->resourcePriv().removeUniqueKey();
864 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800865 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
866 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800867 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700868
869 {
870 GrUniqueKey key2;
871 make_unique_key<0>(&key2, 0);
halcanary385fe4d2015-08-26 13:07:48 -0700872 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700873 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700874 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700875 d->resourcePriv().setUniqueKey(key2);
876 }
877
878 GrUniqueKey key3;
879 make_unique_key<0>(&key3, 0);
880 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
881 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000882}
883
bsalomon8b79d232014-11-10 10:19:06 -0800884static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800885 Mock mock(5, 30000);
886 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800887 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800888
bsalomon8718aaf2015-02-19 07:24:21 -0800889 GrUniqueKey key1, key2, key3;
890 make_unique_key<0>(&key1, 1);
891 make_unique_key<0>(&key2, 2);
892 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700893
bsalomon23e619c2015-02-06 11:54:28 -0800894 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700895 TestResource* a = new TestResource(context->getGpu());
896 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700897 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800898 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800899 a->resourcePriv().setUniqueKey(key1);
900 b->resourcePriv().setUniqueKey(key2);
901 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800902 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800903 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800904 c->unref();
905
bsalomon8718aaf2015-02-19 07:24:21 -0800906 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
907 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
908 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800909 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800910
bsalomon8718aaf2015-02-19 07:24:21 -0800911 typedef GrUniqueKeyInvalidatedMessage Msg;
912 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800913
914 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
915 Bus::Post(Msg(key1));
916 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800917 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800918 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800919 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
920 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800921 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800922 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800923
924 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800925 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800926 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800927 // we still have a ref on b, c should be recycled as scratch.
928 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800929 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800930
bsalomon23e619c2015-02-06 11:54:28 -0800931 // make b purgeable. It should be immediately deleted since it has no key.
932 b->unref();
933 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
934
935 // Make sure we actually get to c via it's scratch key, before we say goodbye.
936 GrScratchKey scratchKey;
937 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700938 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800939 REPORTER_ASSERT(reporter, scratch == c);
940 SkSafeUnref(scratch);
941
942 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800943 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700944 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800945 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800946 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
947 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800948 REPORTER_ASSERT(reporter, !scratch);
949 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800950}
951
bsalomon71cb0c22014-11-14 12:10:14 -0800952static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800953 Mock mock(3, 30000);
954 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800955 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800956
bsalomon8718aaf2015-02-19 07:24:21 -0800957 GrUniqueKey key1, key2;
958 make_unique_key<0>(&key1, 1);
959 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000960
halcanary385fe4d2015-08-26 13:07:48 -0700961 TestResource* a = new TestResource(context->getGpu());
962 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800963 a->resourcePriv().setUniqueKey(key1);
964 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800965
bsalomonc2f35b72015-01-23 07:19:22 -0800966 // Make a cycle
967 a->setUnrefWhenDestroyed(b);
968 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800969
bsalomonc2f35b72015-01-23 07:19:22 -0800970 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800971
bsalomonc2f35b72015-01-23 07:19:22 -0800972 a->unref();
973 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800974
bsalomonc2f35b72015-01-23 07:19:22 -0800975 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800976
bsalomon0ea80f42015-02-11 10:49:59 -0800977 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800978 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800979
bsalomonc2f35b72015-01-23 07:19:22 -0800980 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700981 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800982 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800983
bsalomon0ea80f42015-02-11 10:49:59 -0800984 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800985 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000986}
987
bsalomon8b79d232014-11-10 10:19:06 -0800988static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800989 GrUniqueKey key1, key2;
990 make_unique_key<0>(&key1, 1);
991 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000992
993 // Test changing resources sizes (both increase & decrease).
994 {
bsalomonc2f35b72015-01-23 07:19:22 -0800995 Mock mock(3, 30000);
996 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800997 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000998
halcanary385fe4d2015-08-26 13:07:48 -0700999 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001000 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001001 a->unref();
1002
halcanary385fe4d2015-08-26 13:07:48 -07001003 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001004 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001005 b->unref();
1006
bsalomon0ea80f42015-02-11 10:49:59 -08001007 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1008 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001009 {
bsalomon8718aaf2015-02-19 07:24:21 -08001010 SkAutoTUnref<TestResource> find2(
1011 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001012 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -08001013 SkAutoTUnref<TestResource> find1(
1014 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001015 find1->setSize(50);
1016 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001017
bsalomon0ea80f42015-02-11 10:49:59 -08001018 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1019 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001020 }
1021
1022 // Test increasing a resources size beyond the cache budget.
1023 {
bsalomonc2f35b72015-01-23 07:19:22 -08001024 Mock mock(2, 300);
1025 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001026 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001027
halcanary385fe4d2015-08-26 13:07:48 -07001028 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001029 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001030 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001031 a->unref();
1032
halcanary385fe4d2015-08-26 13:07:48 -07001033 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001034 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001035 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001036 b->unref();
1037
bsalomon0ea80f42015-02-11 10:49:59 -08001038 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1039 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001040
bsalomon8b79d232014-11-10 10:19:06 -08001041 {
bsalomon8718aaf2015-02-19 07:24:21 -08001042 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1043 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001044 find2->setSize(201);
1045 }
bsalomon8718aaf2015-02-19 07:24:21 -08001046 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001047
bsalomon0ea80f42015-02-11 10:49:59 -08001048 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1049 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001050 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001051}
1052
bsalomonddf30e62015-02-19 11:38:44 -08001053static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1054 static const int kCount = 50;
1055 static const int kBudgetCnt = kCount / 2;
1056 static const int kLockedFreq = 8;
1057 static const int kBudgetSize = 0x80000000;
1058
1059 SkRandom random;
1060
1061 // Run the test 2*kCount times;
1062 for (int i = 0; i < 2 * kCount; ++i ) {
1063 Mock mock(kBudgetCnt, kBudgetSize);
1064 GrContext* context = mock.context();
1065 GrResourceCache* cache = mock.cache();
1066
1067 // Pick a random number of resources to add before the timestamp will wrap.
1068 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1069
1070 static const int kNumToPurge = kCount - kBudgetCnt;
1071
1072 SkTDArray<int> shouldPurgeIdxs;
1073 int purgeableCnt = 0;
1074 SkTDArray<GrGpuResource*> resourcesToUnref;
1075
1076 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1077 // unpurgeable resources.
1078 for (int j = 0; j < kCount; ++j) {
1079 GrUniqueKey key;
1080 make_unique_key<0>(&key, j);
1081
halcanary385fe4d2015-08-26 13:07:48 -07001082 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001083 r->resourcePriv().setUniqueKey(key);
1084 if (random.nextU() % kLockedFreq) {
1085 // Make this is purgeable.
1086 r->unref();
1087 ++purgeableCnt;
1088 if (purgeableCnt <= kNumToPurge) {
1089 *shouldPurgeIdxs.append() = j;
1090 }
1091 } else {
1092 *resourcesToUnref.append() = r;
1093 }
1094 }
1095
1096 // Verify that the correct resources were purged.
1097 int currShouldPurgeIdx = 0;
1098 for (int j = 0; j < kCount; ++j) {
1099 GrUniqueKey key;
1100 make_unique_key<0>(&key, j);
1101 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1102 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1103 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1104 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001105 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001106 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001107 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001108 }
1109 SkSafeUnref(res);
1110 }
1111
1112 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1113 resourcesToUnref[j]->unref();
1114 }
1115 }
1116}
1117
bsalomon3f324322015-04-08 11:01:54 -07001118static void test_flush(skiatest::Reporter* reporter) {
1119 Mock mock(1000000, 1000000);
1120 GrContext* context = mock.context();
1121 GrResourceCache* cache = mock.cache();
1122
1123 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1124 // power of two here to keep things simpler.
1125 static const int kFlushCount = 16;
1126 cache->setLimits(1000000, 1000000, kFlushCount);
1127
1128 {
1129 // Insert a resource and send a flush notification kFlushCount times.
1130 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001131 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001132 GrUniqueKey k;
1133 make_unique_key<1>(&k, i);
1134 r->resourcePriv().setUniqueKey(k);
1135 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001136 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001137 }
1138
1139 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1140 for (int i = 0; i < kFlushCount - 1; ++i) {
1141 // The first resource was purged after the last flush in the initial loop, hence the -1.
1142 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1143 for (int j = 0; j < i; ++j) {
1144 GrUniqueKey k;
1145 make_unique_key<1>(&k, j);
1146 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1147 REPORTER_ASSERT(reporter, !SkToBool(r));
1148 SkSafeUnref(r);
1149 }
bsalomonb77a9072016-09-07 10:02:04 -07001150 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001151 }
1152
1153 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1154 cache->purgeAllUnlocked();
1155 }
1156
1157 // Do a similar test but where we leave refs on some resources to prevent them from being
1158 // purged.
1159 {
1160 GrGpuResource* refedResources[kFlushCount >> 1];
1161 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001162 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001163 GrUniqueKey k;
1164 make_unique_key<1>(&k, i);
1165 r->resourcePriv().setUniqueKey(k);
1166 // Leave a ref on every other resource, beginning with the first.
1167 if (SkToBool(i & 0x1)) {
1168 refedResources[i/2] = r;
1169 } else {
1170 r->unref();
1171 }
bsalomonb77a9072016-09-07 10:02:04 -07001172 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001173 }
1174
1175 for (int i = 0; i < kFlushCount; ++i) {
1176 // Should get a resource purged every other flush.
1177 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001178 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001179 }
1180
1181 // Unref all the resources that we kept refs on in the first loop.
1182 for (int i = 0; i < kFlushCount >> 1; ++i) {
1183 refedResources[i]->unref();
1184 }
1185
1186 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1187 // get kFlushCount additional flushes. Then everything should be purged.
1188 for (int i = 0; i < kFlushCount; ++i) {
1189 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001190 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001191 }
1192 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1193
1194 cache->purgeAllUnlocked();
1195 }
1196
1197 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001198
1199 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1200 // eviction.
1201 context->flush();
1202 for (int i = 0; i < 10; ++i) {
1203 TestResource* r = new TestResource(context->getGpu());
1204 GrUniqueKey k;
1205 make_unique_key<1>(&k, i);
1206 r->resourcePriv().setUniqueKey(k);
1207 r->unref();
1208 }
1209 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1210 for (int i = 0; i < 10 * kFlushCount; ++i) {
1211 context->flush();
1212 }
1213 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001214}
1215
bsalomon10e23ca2014-11-25 05:52:06 -08001216static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001217 // Set the cache size to double the resource count because we're going to create 2x that number
1218 // resources, using two different key domains. Add a little slop to the bytes because we resize
1219 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001220 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001221
bsalomonc2f35b72015-01-23 07:19:22 -08001222 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1223 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001224 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001225
1226 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001227 GrUniqueKey key1, key2;
1228 make_unique_key<1>(&key1, i);
1229 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001230
bsalomon24db3b12015-01-23 04:24:04 -08001231 TestResource* resource;
1232
halcanary385fe4d2015-08-26 13:07:48 -07001233 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001234 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001235 resource->setSize(1);
1236 resource->unref();
1237
halcanary385fe4d2015-08-26 13:07:48 -07001238 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001239 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001240 resource->setSize(1);
1241 resource->unref();
1242 }
1243
1244 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001245 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1246 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1247 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1248 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001249 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001250 GrUniqueKey key1, key2;
1251 make_unique_key<1>(&key1, i);
1252 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001253
bsalomon8718aaf2015-02-19 07:24:21 -08001254 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1255 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001256 }
1257
bsalomon0ea80f42015-02-11 10:49:59 -08001258 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001259 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001260 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1261 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1262 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1263 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001264
1265 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001266 GrUniqueKey key1, key2;
1267 make_unique_key<1>(&key1, i);
1268 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001269
bsalomon8718aaf2015-02-19 07:24:21 -08001270 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1271 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001272 }
1273}
1274
senorblanco84cd6212015-08-04 10:01:58 -07001275static void test_custom_data(skiatest::Reporter* reporter) {
1276 GrUniqueKey key1, key2;
1277 make_unique_key<0>(&key1, 1);
1278 make_unique_key<0>(&key2, 2);
1279 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001280 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001281 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1282 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1283
1284 // Test that copying a key also takes a ref on its custom data.
1285 GrUniqueKey key3 = key1;
1286 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1287}
1288
bsalomonc6363ef2015-09-24 07:07:40 -07001289static void test_abandoned(skiatest::Reporter* reporter) {
1290 Mock mock(10, 300);
1291 GrContext* context = mock.context();
Brian Salomon89438a12015-09-24 13:22:45 -04001292 SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001293 context->abandonContext();
1294
1295 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1296
1297 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1298
robertphillips8abb3702016-08-31 14:04:06 -07001299 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001300 resource->getUniqueKey();
1301 resource->wasDestroyed();
1302 resource->gpuMemorySize();
1303 resource->getContext();
1304
1305 resource->abandon();
1306 resource->resourcePriv().getScratchKey();
1307 resource->resourcePriv().isBudgeted();
1308 resource->resourcePriv().makeBudgeted();
1309 resource->resourcePriv().makeUnbudgeted();
1310 resource->resourcePriv().removeScratchKey();
1311 GrUniqueKey key;
1312 make_unique_key<0>(&key, 1);
1313 resource->resourcePriv().setUniqueKey(key);
1314 resource->resourcePriv().removeUniqueKey();
1315}
1316
kkinnunen15302832015-12-01 04:35:26 -08001317DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001318 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001319 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001320 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001321 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001322 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001323 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001324 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001325 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001326 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001327 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001328 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001329 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001330 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001331 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001332 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001333 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001334 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001335}
1336
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001337#endif