blob: dad958f835f01c72044038b9dbecf2ec4a1d28dc [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"
Robert Phillipsd6214d42016-11-07 08:23:48 -050026#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070027#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000028#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000029
30static const int gWidth = 640;
31static const int gHeight = 480;
32
33////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070034DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070035 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080036 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040037 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080038 desc.fFlags = kRenderTarget_GrSurfaceFlag;
39 desc.fWidth = gWidth;
40 desc.fHeight = gHeight;
41 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070042 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080043 SkCanvas* canvas = surface->getCanvas();
44
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
46
47 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000048 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000049 src.eraseColor(SK_ColorBLACK);
50 size_t srcSize = src.getSize();
51
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000052 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070053 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
55 int oldMaxNum;
56 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000057 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000058
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000059 // Set the cache limits so we can fit 10 "src" images and the
60 // max number of textures doesn't matter
61 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000062 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000063
64 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000065 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 for (int i = 0; i < 100; ++i) {
68 canvas->drawBitmap(src, 0, 0);
69 canvas->readPixels(size, &readback);
70
71 // "modify" the src texture
72 src.notifyPixelsChanged();
73
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000074 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070075 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000076
77 // we should never go over the size limit
78 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
79 }
80
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000081 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000082}
83
bsalomon11abd8d2016-10-14 08:13:48 -070084static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
85 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
86 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
87 return false;
88 }
89 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
90}
91
92// This currently fails on ES3 ANGLE contexts
93DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
94 ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070095 GrContext* context = ctxInfo.grContext();
bsalomon02a44a42015-02-19 09:09:00 -080096 GrSurfaceDesc smallDesc;
97 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -040098 smallDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -080099 smallDesc.fWidth = 4;
100 smallDesc.fHeight = 4;
101 smallDesc.fSampleCnt = 0;
102
bsalomond309e7a2015-04-30 14:18:54 -0700103 GrTextureProvider* cache = context->textureProvider();
egdanielec00d942015-09-14 12:56:10 -0700104 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800105 // Test that two budgeted RTs with the same desc share a stencil buffer.
Hal Canary342b7ac2016-11-04 11:49:42 -0400106 sk_sp<GrTexture> smallRT0(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800107 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700108 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800109 }
110
Hal Canary342b7ac2016-11-04 11:49:42 -0400111 sk_sp<GrTexture> smallRT1(cache->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800112 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700113 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800114 }
115
egdaniel8dc7c3a2015-04-16 11:22:42 -0700116 REPORTER_ASSERT(reporter,
117 smallRT0 && smallRT1 &&
118 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700119 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
120 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800121
122 // An unbudgeted RT with the same desc should also share.
Hal Canary342b7ac2016-11-04 11:49:42 -0400123 sk_sp<GrTexture> smallRT2(cache->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800124 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700125 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800126 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700127 REPORTER_ASSERT(reporter,
128 smallRT0 && smallRT2 &&
129 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700130 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
131 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800132
133 // An RT with a much larger size should not share.
134 GrSurfaceDesc bigDesc;
135 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400136 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800137 bigDesc.fWidth = 400;
138 bigDesc.fHeight = 200;
139 bigDesc.fSampleCnt = 0;
Hal Canary342b7ac2016-11-04 11:49:42 -0400140 sk_sp<GrTexture> bigRT(cache->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800141 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700142 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800143 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700144 REPORTER_ASSERT(reporter,
145 smallRT0 && bigRT &&
146 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700147 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
148 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800149
bsalomon76228632015-05-29 08:02:10 -0700150 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700151 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800152 GrSurfaceDesc smallMSAADesc = smallDesc;
153 smallMSAADesc.fSampleCnt = 4;
Hal Canary342b7ac2016-11-04 11:49:42 -0400154 sk_sp<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800155 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700156 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800157 }
bsalomonb602d4d2015-02-19 12:05:58 -0800158#ifdef SK_BUILD_FOR_ANDROID
159 if (!smallMSAART0) {
160 // The nexus player seems to fail to create MSAA textures.
161 return;
162 }
163#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800164 REPORTER_ASSERT(reporter,
165 smallRT0 && smallMSAART0 &&
166 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700167 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
168 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800169 // A second MSAA RT should share with the first MSAA RT.
Hal Canary342b7ac2016-11-04 11:49:42 -0400170 sk_sp<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800171 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700172 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800173 }
174 REPORTER_ASSERT(reporter,
175 smallMSAART0 && smallMSAART1 &&
176 smallMSAART0->asRenderTarget() &&
177 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700178 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
179 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800180 // But not one with a larger sample count should not. (Also check that the request for 4
181 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700182 if (context->caps()->maxSampleCount() >= 8 &&
183 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700184 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800185 smallMSAADesc.fSampleCnt = 8;
bsalomon5ec26ae2016-02-25 08:33:02 -0800186 smallMSAART1.reset(cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
Hal Canary342b7ac2016-11-04 11:49:42 -0400187 sk_sp<GrTexture> smallMSAART1(
bsalomon5ec26ae2016-02-25 08:33:02 -0800188 cache->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800189 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700190 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800191 }
192 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700193 smallMSAART0 && smallMSAART1 &&
194 smallMSAART0->asRenderTarget() &&
195 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700196 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
197 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800198 }
199 }
200}
201
bsalomon68d91342016-04-12 09:59:58 -0700202DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700203 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800204 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700205 // this test is only valid for GL
206 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700207 return;
208 }
209
bsalomon091f60c2015-11-10 11:54:56 -0800210 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700211 static const int kW = 100;
212 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700213
bsalomon091f60c2015-11-10 11:54:56 -0800214 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
215 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700216
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217 context->resetContext();
218
219 GrBackendTextureDesc desc;
220 desc.fConfig = kBGRA_8888_GrPixelConfig;
221 desc.fWidth = kW;
222 desc.fHeight = kH;
223
bsalomon091f60c2015-11-10 11:54:56 -0800224 desc.fTextureHandle = texHandles[0];
bungeman6bd52842016-10-27 09:30:08 -0700225 sk_sp<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
226 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227
bsalomon091f60c2015-11-10 11:54:56 -0800228 desc.fTextureHandle = texHandles[1];
bungeman6bd52842016-10-27 09:30:08 -0700229 sk_sp<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
230 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
bungeman6bd52842016-10-27 09:30:08 -0700232 printf("\nborrowed: %p, adopted: %p\n", borrowed.get(), adopted.get());
mtklein5f939ab2016-03-16 10:28:35 -0700233 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
234 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700235 return;
236 }
237
halcanary96fcdcc2015-08-27 07:41:13 -0700238 borrowed.reset(nullptr);
239 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
241 context->flush();
242
bsalomon091f60c2015-11-10 11:54:56 -0800243 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
244 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700245
246 REPORTER_ASSERT(reporter, borrowedIsAlive);
247 REPORTER_ASSERT(reporter, !adoptedIsAlive);
248
bsalomon67d76202015-11-11 12:40:42 -0800249 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
250 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700251
252 context->resetContext();
253}
254
bsalomon6d3fe022014-07-25 08:35:45 -0700255class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000257public:
robertphillips6e83ac72015-08-13 05:19:14 -0700258 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700259
bsalomon1c60dfe2015-01-21 09:32:40 -0800260 /** Property that distinctly categorizes the resource.
261 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800262 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800263
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
265 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700266 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800267 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700268 , fProperty(kA_SimulatedProperty)
269 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800270 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700271 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800272 }
273
kkinnunen2e6055b2016-04-22 01:48:29 -0700274 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
275 SimulatedProperty property) {
276 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800277 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700278 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
279 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000280 }
281
282 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800283 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800284 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000285 }
286
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000287 void setSize(size_t size) {
288 fSize = size;
289 this->didChangeGpuMemorySize();
290 }
291
bsalomon33435572014-11-05 14:47:41 -0800292 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293
bsalomon71cb0c22014-11-14 12:10:14 -0800294 void setUnrefWhenDestroyed(TestResource* resource) {
295 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000296 }
297
bsalomon1c60dfe2015-01-21 09:32:40 -0800298 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
299 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
300 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800301 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
302 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800303 }
304 }
305
306 static size_t ExpectedScratchKeySize() {
307 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
308 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000309private:
bsalomon24db3b12015-01-23 04:24:04 -0800310 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800311
kkinnunen2e6055b2016-04-22 01:48:29 -0700312 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
313 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700314 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 , fProperty(property)
317 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800318 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700319 this->registerWithCache(budgeted);
320 }
321
322 // Constructor for simulating resources that wrap backend objects.
323 TestResource(GrGpu* gpu, size_t size)
324 : INHERITED(gpu)
325 , fToDelete(nullptr)
326 , fSize(size)
327 , fProperty(kA_SimulatedProperty)
328 , fIsScratch(false) {
329 ++fNumAlive;
330 this->registerWithCacheWrapped();
331 }
332
333 void computeScratchKey(GrScratchKey* key) const override {
334 if (fIsScratch) {
335 ComputeScratchKey(fProperty, key);
336 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800337 }
338
mtklein36352bf2015-03-25 18:17:31 -0700339 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800340
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000341 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000342 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800343 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800344 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700345 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700346 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000347};
bsalomon33435572014-11-05 14:47:41 -0800348int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000349
bsalomonc2f35b72015-01-23 07:19:22 -0800350class Mock {
351public:
352 Mock(int maxCnt, size_t maxBytes) {
353 fContext.reset(GrContext::CreateMockContext());
354 SkASSERT(fContext);
355 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800356 GrResourceCache* cache = fContext->getResourceCache();
357 cache->purgeAllUnlocked();
358 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800359 }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
bsalomon0ea80f42015-02-11 10:49:59 -0800361 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800362
Hal Canary342b7ac2016-11-04 11:49:42 -0400363 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800364
365private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400366 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800367};
368
369static void test_no_key(skiatest::Reporter* reporter) {
370 Mock mock(10, 30000);
371 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800372 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800373
374 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700375 TestResource* a = new TestResource(context->getGpu());
376 TestResource* b = new TestResource(context->getGpu());
377 TestResource* c = new TestResource(context->getGpu());
378 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800379 a->setSize(11);
380 b->setSize(12);
381 c->setSize(13);
382 d->setSize(14);
383
384 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800387 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800390 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800391 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
392
bsalomon8718aaf2015-02-19 07:24:21 -0800393 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 a->unref();
396 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800398 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 c->unref();
402 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800405 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 d->unref();
408 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411
412 b->unref();
413 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800414 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
415 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800416}
417
bsalomon24db3b12015-01-23 04:24:04 -0800418// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800419template <int> static void make_unique_key(GrUniqueKey* key, int data) {
420 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
421 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800422 builder[0] = data;
423}
424
bsalomon84c8e622014-11-17 09:33:27 -0800425static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800426 Mock mock(10, 300);
427 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800428 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800429
bsalomon8718aaf2015-02-19 07:24:21 -0800430 GrUniqueKey uniqueKey;
431 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800432
bsalomon8718aaf2015-02-19 07:24:21 -0800433 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800434 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700435 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800436 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700437 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800438 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800439 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700440 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800441 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700442 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700443 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800444 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800445
bsalomon8718aaf2015-02-19 07:24:21 -0800446 // Make sure we can't add a unique key to the wrapped resource
447 GrUniqueKey uniqueKey2;
448 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800449 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700450 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800451
452 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800453 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800454 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800455 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800456 cache->getResourceBytes());
457 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800458 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800459 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800460
bsalomon63c992f2015-01-23 12:47:59 -0800461 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800462 cache->purgeAllUnlocked();
463 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800464 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800465 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800466 cache->getResourceBytes());
467 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800468 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800469 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800470
471 // Unreffing the wrapped resource should free it right away.
472 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800473 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800474 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800475 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800476
bsalomon84c8e622014-11-17 09:33:27 -0800477 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700478 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800479 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800480 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800481 cache->purgeAllUnlocked();
482 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800483 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800484 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
485 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
486 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800487
488 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800489 cache->purgeAllUnlocked();
490 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800491 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800492 cache->getResourceBytes());
493 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
494 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800495
496 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800497 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
498 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
499 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
500 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800501
502 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800503 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
504 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
505 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
506 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800507}
508
bsalomon5236cf42015-01-14 10:42:08 -0800509static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800510 Mock mock(10, 30000);
511 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800512 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800513
bsalomon8718aaf2015-02-19 07:24:21 -0800514 GrUniqueKey uniqueKey;
515 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800516
517 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800518 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800519 TestResource* wrapped;
520 TestResource* unbudgeted;
521
522 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700523 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
524 TestResource::kB_SimulatedProperty);
525
bsalomon5236cf42015-01-14 10:42:08 -0800526 scratch->setSize(10);
527 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800528 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
529 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
530 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
531 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800532
halcanary385fe4d2015-08-26 13:07:48 -0700533 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800534 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800535 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800536 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800537 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
538 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
539 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
540 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800541
bsalomon0ea80f42015-02-11 10:49:59 -0800542 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700543 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, 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 unbudgeted->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
kkinnunen2e6055b2016-04-22 01:48:29 -0700555 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800556 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
557 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
558 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
559 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800560
561 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800562 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
563 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
564 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
565 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800566
bsalomon0ea80f42015-02-11 10:49:59 -0800567 cache->purgeAllUnlocked();
568 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
569 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
570 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
571 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800572}
573
bsalomon3582d3e2015-02-13 14:20:05 -0800574// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
575void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
576/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800577 Mock mock(10, 300);
578 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800579 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800580
581 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700582 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
583 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800584 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800585 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800586
587 size_t size = resource->gpuMemorySize();
588 for (int i = 0; i < 2; ++i) {
589 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800590 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800591 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800592 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700593 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800594 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
595 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
596 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
597 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800598
599 // Once it is unrefed, it should become available as scratch.
600 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
602 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
603 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
604 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700605 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800606 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800607 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800608 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800609 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800610
611 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700612 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800613 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800614 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800615 } else {
616 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800617 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800618 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
619 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
620 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
621 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800622 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800623 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800624 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800625
626 // now when it is unrefed it should die since it has no key.
627 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800628 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
629 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
630 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
631 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800632 }
bsalomon8b79d232014-11-10 10:19:06 -0800633 }
bsalomonc2f35b72015-01-23 07:19:22 -0800634}
635
636static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
637 Mock mock(5, 30000);
638 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800639 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800640
bsalomon8b79d232014-11-10 10:19:06 -0800641 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800642 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700643 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800644 TestResource::kB_SimulatedProperty);
645 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700646 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800647 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800648 a->setSize(11);
649 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800650 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800651 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800652 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700653 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800654
655 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800656 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800657
bsalomon0ea80f42015-02-11 10:49:59 -0800658 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800659 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800660 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
661 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800662 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800663 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800664
bsalomon63c992f2015-01-23 12:47:59 -0800665 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800666 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800667 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800668 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800669
670 // Unref but don't purge
671 a->unref();
672 b->unref();
673 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800674 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800675
bsalomon63c992f2015-01-23 12:47:59 -0800676 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800677 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800678 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800679 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
680 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800681}
682
bsalomon10e23ca2014-11-25 05:52:06 -0800683static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800684 Mock mock(5, 30000);
685 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800686 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800687
bsalomon10e23ca2014-11-25 05:52:06 -0800688 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700689 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800690 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700691 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800692 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800693 a->unref();
694 b->unref();
695
bsalomon1c60dfe2015-01-21 09:32:40 -0800696 GrScratchKey scratchKey;
697 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800698 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800699 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700700 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800701
bsalomon0ea80f42015-02-11 10:49:59 -0800702 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800703 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800704 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800705 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
706 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800707
708 // Find the first resource and remove its scratch key
709 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700710 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800711 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800712 // It's still alive, but not cached by scratch key anymore
713 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800714 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
715 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800716
717 // The cache should immediately delete it when it's unrefed since it isn't accessible.
718 find->unref();
719 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800720 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
721 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800722
723 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700724 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800725 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800726 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800727 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
728 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800729
730 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800731 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800732 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800733 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
734 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800735
736 find->unref();
737 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800738 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
739 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800740}
741
bsalomon1c60dfe2015-01-21 09:32:40 -0800742static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800743 Mock mock(5, 30000);
744 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800745 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800746
747 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700748 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800749 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700750 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800751 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800752 a->unref();
753 b->unref();
754
755 GrScratchKey scratchKey;
756 // Ensure that scratch key comparison and assignment is consistent.
757 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800758 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800759 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800760 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800761 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
762 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
763 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
764 scratchKey = scratchKey1;
765 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
766 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
767 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
768 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
769 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
770 scratchKey = scratchKey2;
771 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
772 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
773 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
774 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
775 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
776
777 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800778 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800779 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700780 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800781
782 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800783 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700784 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700785 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800786 find->unref();
787
788 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700789 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700790 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800791 REPORTER_ASSERT(reporter, find == a || find == b);
792
robertphillips6e83ac72015-08-13 05:19:14 -0700793 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700794 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800795 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
796 REPORTER_ASSERT(reporter, find2 != find);
797 find2->unref();
798 find->unref();
799}
800
bsalomon8718aaf2015-02-19 07:24:21 -0800801static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800802 Mock mock(5, 30000);
803 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800804 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000805
bsalomon8718aaf2015-02-19 07:24:21 -0800806 GrUniqueKey key;
807 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700808
bsalomon8718aaf2015-02-19 07:24:21 -0800809 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700810 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800811 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700812
bsalomonf99e9612015-02-19 08:24:16 -0800813 // Set key on resource a.
814 a->resourcePriv().setUniqueKey(key);
815 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
816 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800817
bsalomonf99e9612015-02-19 08:24:16 -0800818 // Make sure that redundantly setting a's key works.
819 a->resourcePriv().setUniqueKey(key);
820 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800821 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800822 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
823 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800824 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
825
bsalomonf99e9612015-02-19 08:24:16 -0800826 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700827 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800828 b->setSize(12);
829 b->resourcePriv().setUniqueKey(key);
830 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
831 b->unref();
832
833 // Still have two resources because a is still reffed.
834 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
835 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
836 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
837
838 a->unref();
839 // Now a should be gone.
840 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
841 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
842 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
843
844 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
845 // Also make b be unreffed when replacement occurs.
846 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700847 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800848 GrUniqueKey differentKey;
849 make_unique_key<0>(&differentKey, 1);
850 c->setSize(13);
851 c->resourcePriv().setUniqueKey(differentKey);
852 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
853 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
854 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
855 // c replaces b and b should be immediately purged.
856 c->resourcePriv().setUniqueKey(key);
857 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
859 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
860
861 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800862 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800863 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
864 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
865 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
866
867 // Drop the ref on c, it should be kept alive because it has a unique key.
868 c->unref();
869 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
870 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
871 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
872
873 // Verify that we can find c, then remove its unique key. It should get purged immediately.
874 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
875 c->resourcePriv().removeUniqueKey();
876 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800877 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
878 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800879 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700880
881 {
882 GrUniqueKey key2;
883 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400884 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700885 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700886 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700887 d->resourcePriv().setUniqueKey(key2);
888 }
889
890 GrUniqueKey key3;
891 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400892 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700893 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000894}
895
bsalomon8b79d232014-11-10 10:19:06 -0800896static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800897 Mock mock(5, 30000);
898 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800899 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800900
bsalomon8718aaf2015-02-19 07:24:21 -0800901 GrUniqueKey key1, key2, key3;
902 make_unique_key<0>(&key1, 1);
903 make_unique_key<0>(&key2, 2);
904 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700905
bsalomon23e619c2015-02-06 11:54:28 -0800906 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700907 TestResource* a = new TestResource(context->getGpu());
908 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700909 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800910 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800911 a->resourcePriv().setUniqueKey(key1);
912 b->resourcePriv().setUniqueKey(key2);
913 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800914 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800915 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800916 c->unref();
917
bsalomon8718aaf2015-02-19 07:24:21 -0800918 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
919 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
920 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800921 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800922
bsalomon8718aaf2015-02-19 07:24:21 -0800923 typedef GrUniqueKeyInvalidatedMessage Msg;
924 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800925
926 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
927 Bus::Post(Msg(key1));
928 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800929 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800930 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800931 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
932 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800933 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800934 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800935
936 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800937 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800938 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800939 // we still have a ref on b, c should be recycled as scratch.
940 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800941 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800942
bsalomon23e619c2015-02-06 11:54:28 -0800943 // make b purgeable. It should be immediately deleted since it has no key.
944 b->unref();
945 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
946
947 // Make sure we actually get to c via it's scratch key, before we say goodbye.
948 GrScratchKey scratchKey;
949 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700950 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800951 REPORTER_ASSERT(reporter, scratch == c);
952 SkSafeUnref(scratch);
953
954 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800955 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700956 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800957 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800958 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
959 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800960 REPORTER_ASSERT(reporter, !scratch);
961 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800962}
963
bsalomon71cb0c22014-11-14 12:10:14 -0800964static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800965 Mock mock(3, 30000);
966 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800967 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800968
bsalomon8718aaf2015-02-19 07:24:21 -0800969 GrUniqueKey key1, key2;
970 make_unique_key<0>(&key1, 1);
971 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000972
halcanary385fe4d2015-08-26 13:07:48 -0700973 TestResource* a = new TestResource(context->getGpu());
974 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800975 a->resourcePriv().setUniqueKey(key1);
976 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800977
bsalomonc2f35b72015-01-23 07:19:22 -0800978 // Make a cycle
979 a->setUnrefWhenDestroyed(b);
980 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800981
bsalomonc2f35b72015-01-23 07:19:22 -0800982 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800983
bsalomonc2f35b72015-01-23 07:19:22 -0800984 a->unref();
985 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800986
bsalomonc2f35b72015-01-23 07:19:22 -0800987 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800988
bsalomon0ea80f42015-02-11 10:49:59 -0800989 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800990 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800991
bsalomonc2f35b72015-01-23 07:19:22 -0800992 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700993 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800994 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800995
bsalomon0ea80f42015-02-11 10:49:59 -0800996 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800997 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000998}
999
bsalomon8b79d232014-11-10 10:19:06 -08001000static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001001 GrUniqueKey key1, key2;
1002 make_unique_key<0>(&key1, 1);
1003 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001004
1005 // Test changing resources sizes (both increase & decrease).
1006 {
bsalomonc2f35b72015-01-23 07:19:22 -08001007 Mock mock(3, 30000);
1008 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001009 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001010
halcanary385fe4d2015-08-26 13:07:48 -07001011 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001012 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001013 a->unref();
1014
halcanary385fe4d2015-08-26 13:07:48 -07001015 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001016 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001017 b->unref();
1018
bsalomon0ea80f42015-02-11 10:49:59 -08001019 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1020 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001021 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001022 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001023 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001024 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001025 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001026 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001027 find1->setSize(50);
1028 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001029
bsalomon0ea80f42015-02-11 10:49:59 -08001030 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1031 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001032 }
1033
1034 // Test increasing a resources size beyond the cache budget.
1035 {
bsalomonc2f35b72015-01-23 07:19:22 -08001036 Mock mock(2, 300);
1037 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001038 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001039
halcanary385fe4d2015-08-26 13:07:48 -07001040 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001041 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001042 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001043 a->unref();
1044
halcanary385fe4d2015-08-26 13:07:48 -07001045 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001046 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001047 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001048 b->unref();
1049
bsalomon0ea80f42015-02-11 10:49:59 -08001050 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1051 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001052
bsalomon8b79d232014-11-10 10:19:06 -08001053 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001054 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001055 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001056 find2->setSize(201);
1057 }
bsalomon8718aaf2015-02-19 07:24:21 -08001058 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001059
bsalomon0ea80f42015-02-11 10:49:59 -08001060 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1061 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001063}
1064
bsalomonddf30e62015-02-19 11:38:44 -08001065static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1066 static const int kCount = 50;
1067 static const int kBudgetCnt = kCount / 2;
1068 static const int kLockedFreq = 8;
1069 static const int kBudgetSize = 0x80000000;
1070
1071 SkRandom random;
1072
1073 // Run the test 2*kCount times;
1074 for (int i = 0; i < 2 * kCount; ++i ) {
1075 Mock mock(kBudgetCnt, kBudgetSize);
1076 GrContext* context = mock.context();
1077 GrResourceCache* cache = mock.cache();
1078
1079 // Pick a random number of resources to add before the timestamp will wrap.
1080 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1081
1082 static const int kNumToPurge = kCount - kBudgetCnt;
1083
1084 SkTDArray<int> shouldPurgeIdxs;
1085 int purgeableCnt = 0;
1086 SkTDArray<GrGpuResource*> resourcesToUnref;
1087
1088 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1089 // unpurgeable resources.
1090 for (int j = 0; j < kCount; ++j) {
1091 GrUniqueKey key;
1092 make_unique_key<0>(&key, j);
1093
halcanary385fe4d2015-08-26 13:07:48 -07001094 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001095 r->resourcePriv().setUniqueKey(key);
1096 if (random.nextU() % kLockedFreq) {
1097 // Make this is purgeable.
1098 r->unref();
1099 ++purgeableCnt;
1100 if (purgeableCnt <= kNumToPurge) {
1101 *shouldPurgeIdxs.append() = j;
1102 }
1103 } else {
1104 *resourcesToUnref.append() = r;
1105 }
1106 }
1107
1108 // Verify that the correct resources were purged.
1109 int currShouldPurgeIdx = 0;
1110 for (int j = 0; j < kCount; ++j) {
1111 GrUniqueKey key;
1112 make_unique_key<0>(&key, j);
1113 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1114 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1115 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1116 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001117 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001118 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001119 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001120 }
1121 SkSafeUnref(res);
1122 }
1123
1124 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1125 resourcesToUnref[j]->unref();
1126 }
1127 }
1128}
1129
bsalomon3f324322015-04-08 11:01:54 -07001130static void test_flush(skiatest::Reporter* reporter) {
1131 Mock mock(1000000, 1000000);
1132 GrContext* context = mock.context();
1133 GrResourceCache* cache = mock.cache();
1134
1135 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1136 // power of two here to keep things simpler.
1137 static const int kFlushCount = 16;
1138 cache->setLimits(1000000, 1000000, kFlushCount);
1139
1140 {
1141 // Insert a resource and send a flush notification kFlushCount times.
1142 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001143 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001144 GrUniqueKey k;
1145 make_unique_key<1>(&k, i);
1146 r->resourcePriv().setUniqueKey(k);
1147 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001148 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001149 }
1150
1151 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001152 for (int i = 0; i < kFlushCount; ++i) {
1153 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001154 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1155 for (int j = 0; j < i; ++j) {
1156 GrUniqueKey k;
1157 make_unique_key<1>(&k, j);
1158 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1159 REPORTER_ASSERT(reporter, !SkToBool(r));
1160 SkSafeUnref(r);
1161 }
bsalomon3f324322015-04-08 11:01:54 -07001162 }
1163
1164 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1165 cache->purgeAllUnlocked();
1166 }
1167
1168 // Do a similar test but where we leave refs on some resources to prevent them from being
1169 // purged.
1170 {
1171 GrGpuResource* refedResources[kFlushCount >> 1];
1172 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001173 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001174 GrUniqueKey k;
1175 make_unique_key<1>(&k, i);
1176 r->resourcePriv().setUniqueKey(k);
1177 // Leave a ref on every other resource, beginning with the first.
1178 if (SkToBool(i & 0x1)) {
1179 refedResources[i/2] = r;
1180 } else {
1181 r->unref();
1182 }
bsalomonb77a9072016-09-07 10:02:04 -07001183 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001184 }
1185
1186 for (int i = 0; i < kFlushCount; ++i) {
1187 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001188 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001189 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001190 }
1191
1192 // Unref all the resources that we kept refs on in the first loop.
1193 for (int i = 0; i < kFlushCount >> 1; ++i) {
1194 refedResources[i]->unref();
1195 }
1196
bsalomone2e87f32016-09-22 12:42:11 -07001197 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1198 // kFlushCount full flushes.
1199 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001200 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001201 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001202 }
1203 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1204
1205 cache->purgeAllUnlocked();
1206 }
1207
1208 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001209
1210 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1211 // eviction.
1212 context->flush();
1213 for (int i = 0; i < 10; ++i) {
1214 TestResource* r = new TestResource(context->getGpu());
1215 GrUniqueKey k;
1216 make_unique_key<1>(&k, i);
1217 r->resourcePriv().setUniqueKey(k);
1218 r->unref();
1219 }
1220 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1221 for (int i = 0; i < 10 * kFlushCount; ++i) {
1222 context->flush();
1223 }
1224 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001225}
1226
bsalomon10e23ca2014-11-25 05:52:06 -08001227static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001228 // Set the cache size to double the resource count because we're going to create 2x that number
1229 // resources, using two different key domains. Add a little slop to the bytes because we resize
1230 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001231 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001232
bsalomonc2f35b72015-01-23 07:19:22 -08001233 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1234 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001235 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001236
1237 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001238 GrUniqueKey key1, key2;
1239 make_unique_key<1>(&key1, i);
1240 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001241
bsalomon24db3b12015-01-23 04:24:04 -08001242 TestResource* resource;
1243
halcanary385fe4d2015-08-26 13:07:48 -07001244 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001245 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001246 resource->setSize(1);
1247 resource->unref();
1248
halcanary385fe4d2015-08-26 13:07:48 -07001249 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001250 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001251 resource->setSize(1);
1252 resource->unref();
1253 }
1254
1255 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001256 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1257 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1258 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1259 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001260 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001261 GrUniqueKey key1, key2;
1262 make_unique_key<1>(&key1, i);
1263 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001264
bsalomon8718aaf2015-02-19 07:24:21 -08001265 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1266 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001267 }
1268
bsalomon0ea80f42015-02-11 10:49:59 -08001269 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001270 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001271 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1272 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1273 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1274 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001275
1276 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001277 GrUniqueKey key1, key2;
1278 make_unique_key<1>(&key1, i);
1279 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001280
bsalomon8718aaf2015-02-19 07:24:21 -08001281 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1282 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001283 }
1284}
1285
senorblanco84cd6212015-08-04 10:01:58 -07001286static void test_custom_data(skiatest::Reporter* reporter) {
1287 GrUniqueKey key1, key2;
1288 make_unique_key<0>(&key1, 1);
1289 make_unique_key<0>(&key2, 2);
1290 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001291 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001292 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1293 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1294
1295 // Test that copying a key also takes a ref on its custom data.
1296 GrUniqueKey key3 = key1;
1297 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1298}
1299
bsalomonc6363ef2015-09-24 07:07:40 -07001300static void test_abandoned(skiatest::Reporter* reporter) {
1301 Mock mock(10, 300);
1302 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001303 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001304 context->abandonContext();
1305
1306 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1307
1308 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1309
robertphillips8abb3702016-08-31 14:04:06 -07001310 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001311 resource->getUniqueKey();
1312 resource->wasDestroyed();
1313 resource->gpuMemorySize();
1314 resource->getContext();
1315
1316 resource->abandon();
1317 resource->resourcePriv().getScratchKey();
1318 resource->resourcePriv().isBudgeted();
1319 resource->resourcePriv().makeBudgeted();
1320 resource->resourcePriv().makeUnbudgeted();
1321 resource->resourcePriv().removeScratchKey();
1322 GrUniqueKey key;
1323 make_unique_key<0>(&key, 1);
1324 resource->resourcePriv().setUniqueKey(key);
1325 resource->resourcePriv().removeUniqueKey();
1326}
1327
kkinnunen15302832015-12-01 04:35:26 -08001328DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001329 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001330 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001331 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001332 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001333 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001334 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001335 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001336 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001337 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001338 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001339 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001340 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001341 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001342 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001343 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001344 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001345 test_abandoned(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001346}
1347
Robert Phillipsd6214d42016-11-07 08:23:48 -05001348////////////////////////////////////////////////////////////////////////////////
1349static sk_sp<GrTexture> make_normal_texture(GrTextureProvider* provider,
1350 GrSurfaceFlags flags,
1351 int width, int height,
1352 int sampleCnt) {
1353 GrSurfaceDesc desc;
1354 desc.fFlags = flags;
1355 desc.fWidth = width;
1356 desc.fHeight = height;
1357 desc.fConfig = kRGBA_8888_GrPixelConfig;
1358 desc.fSampleCnt = sampleCnt;
1359
1360 return sk_sp<GrTexture>(provider->createTexture(desc, SkBudgeted::kYes));
1361}
1362
1363static sk_sp<GrTexture> make_mipmap_texture(GrTextureProvider* provider,
1364 GrSurfaceFlags flags,
1365 int width, int height,
1366 int sampleCnt) {
1367 SkBitmap bm;
1368
1369 bm.allocN32Pixels(width, height, true);
1370 bm.eraseColor(SK_ColorBLUE);
1371
Brian Osman7b8400d2016-11-08 17:08:54 -05001372 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001373 SkASSERT(mipmaps);
1374 SkASSERT(mipmaps->countLevels() > 1);
1375
1376 int mipLevelCount = mipmaps->countLevels() + 1;
1377
1378 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1379
1380 texels[0].fPixels = bm.getPixels();
1381 texels[0].fRowBytes = bm.rowBytes();
1382
1383 for (int i = 1; i < mipLevelCount; ++i) {
1384 SkMipMap::Level generatedMipLevel;
1385 mipmaps->getLevel(i - 1, &generatedMipLevel);
1386 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1387 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1388 }
1389
1390 GrSurfaceDesc desc;
1391 desc.fFlags = flags;
1392 desc.fWidth = width;
1393 desc.fHeight = height;
1394 desc.fConfig = kRGBA_8888_GrPixelConfig;
1395 desc.fSampleCnt = sampleCnt;
1396 desc.fIsMipMapped = true;
1397
1398 return sk_sp<GrTexture>(provider->createMipMappedTexture(desc, SkBudgeted::kYes,
1399 texels.get(), mipLevelCount));
1400}
1401
1402// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1403// Texture-only, both-RT-and-Texture and MIPmapped
1404DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1405 GrContext* context = ctxInfo.grContext();
1406 GrTextureProvider* provider = context->textureProvider();
1407
Robert Phillipsd6214d42016-11-07 08:23:48 -05001408 static const int kSize = 64;
1409
1410 sk_sp<GrTexture> tex;
1411
1412 // Normal versions
1413 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1414 size_t size = tex->gpuMemorySize();
1415 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1416
1417 if (context->caps()->maxSampleCount() >= 4) {
1418 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1419 size = tex->gpuMemorySize();
1420 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1421 kSize*kSize*4*4 == size || // auto-resolving
1422 kSize*kSize*4*5 == size); // explicit resolve buffer
1423 }
1424
1425 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1426 size = tex->gpuMemorySize();
1427 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1428
1429 // Mipmapped versions
1430 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1431 size = tex->gpuMemorySize();
1432 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1433
1434 if (context->caps()->maxSampleCount() >= 4) {
1435 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1436 size = tex->gpuMemorySize();
1437 REPORTER_ASSERT(reporter,
1438 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1439 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1440 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
1441 }
1442
1443 tex = make_mipmap_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1444 size = tex->gpuMemorySize();
1445 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1446}
1447
1448
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001449#endif