blob: a2064ae557545fcc13c40de41c9c61eb58a3e902 [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
egdanielec00d942015-09-14 12:56:10 -0700103 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800104 // Test that two budgeted RTs with the same desc share a stencil buffer.
Brian Osman32342f02017-03-04 08:12:46 -0500105 sk_sp<GrTexture> smallRT0(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800106 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700107 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800108 }
109
Brian Osman32342f02017-03-04 08:12:46 -0500110 sk_sp<GrTexture> smallRT1(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800111 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700112 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 }
114
egdaniel8dc7c3a2015-04-16 11:22:42 -0700115 REPORTER_ASSERT(reporter,
116 smallRT0 && smallRT1 &&
117 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700118 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
119 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800120
121 // An unbudgeted RT with the same desc should also share.
Brian Osman32342f02017-03-04 08:12:46 -0500122 sk_sp<GrTexture> smallRT2(resourceProvider->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800123 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700124 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800125 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700126 REPORTER_ASSERT(reporter,
127 smallRT0 && smallRT2 &&
128 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700129 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
130 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800131
132 // An RT with a much larger size should not share.
133 GrSurfaceDesc bigDesc;
134 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400135 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800136 bigDesc.fWidth = 400;
137 bigDesc.fHeight = 200;
138 bigDesc.fSampleCnt = 0;
Brian Osman32342f02017-03-04 08:12:46 -0500139 sk_sp<GrTexture> bigRT(resourceProvider->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700141 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700143 REPORTER_ASSERT(reporter,
144 smallRT0 && bigRT &&
145 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700146 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
147 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800148
bsalomon76228632015-05-29 08:02:10 -0700149 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700150 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800151 GrSurfaceDesc smallMSAADesc = smallDesc;
152 smallMSAADesc.fSampleCnt = 4;
Brian Osman32342f02017-03-04 08:12:46 -0500153 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
154 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.
Brian Osman32342f02017-03-04 08:12:46 -0500170 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
171 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800172 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700173 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800174 }
175 REPORTER_ASSERT(reporter,
176 smallMSAART0 && smallMSAART1 &&
177 smallMSAART0->asRenderTarget() &&
178 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700179 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
180 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800181 // But not one with a larger sample count should not. (Also check that the request for 4
182 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700183 if (context->caps()->maxSampleCount() >= 8 &&
184 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700185 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800186 smallMSAADesc.fSampleCnt = 8;
Brian Osman32342f02017-03-04 08:12:46 -0500187 smallMSAART1.reset(resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
Hal Canary342b7ac2016-11-04 11:49:42 -0400188 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500189 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800190 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700191 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800192 }
193 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700194 smallMSAART0 && smallMSAART1 &&
195 smallMSAART0->asRenderTarget() &&
196 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700197 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
198 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800199 }
200 }
201}
202
bsalomon68d91342016-04-12 09:59:58 -0700203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700204 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800205 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700206 // this test is only valid for GL
207 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700208 return;
209 }
210
bsalomon091f60c2015-11-10 11:54:56 -0800211 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 static const int kW = 100;
213 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700214
bsalomon091f60c2015-11-10 11:54:56 -0800215 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
216 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700217
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218 context->resetContext();
219
220 GrBackendTextureDesc desc;
221 desc.fConfig = kBGRA_8888_GrPixelConfig;
222 desc.fWidth = kW;
223 desc.fHeight = kH;
224
bsalomon091f60c2015-11-10 11:54:56 -0800225 desc.fTextureHandle = texHandles[0];
Brian Osman32342f02017-03-04 08:12:46 -0500226 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
bungeman6bd52842016-10-27 09:30:08 -0700227 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
bsalomon091f60c2015-11-10 11:54:56 -0800229 desc.fTextureHandle = texHandles[1];
Brian Osman32342f02017-03-04 08:12:46 -0500230 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
bungeman6bd52842016-10-27 09:30:08 -0700231 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
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.
Brian Salomon1090da62017-01-06 12:04:19 -0500419template <int>
420static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800421 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500422 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800423 builder[0] = data;
424}
425
bsalomon84c8e622014-11-17 09:33:27 -0800426static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800427 Mock mock(10, 300);
428 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800429 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800430
bsalomon8718aaf2015-02-19 07:24:21 -0800431 GrUniqueKey uniqueKey;
432 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800433
bsalomon8718aaf2015-02-19 07:24:21 -0800434 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800435 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700436 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800437 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700438 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800439 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800440 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700441 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800442 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700443 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700444 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800445 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800446
bsalomon8718aaf2015-02-19 07:24:21 -0800447 // Make sure we can't add a unique key to the wrapped resource
448 GrUniqueKey uniqueKey2;
449 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800450 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700451 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800452
453 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800454 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800455 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800456 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800457 cache->getResourceBytes());
458 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800459 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800460 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800461
bsalomon63c992f2015-01-23 12:47:59 -0800462 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800463 cache->purgeAllUnlocked();
464 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800465 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800466 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800467 cache->getResourceBytes());
468 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800471
472 // Unreffing the wrapped resource should free it right away.
473 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800474 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800475 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800476 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800477
bsalomon84c8e622014-11-17 09:33:27 -0800478 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700479 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800480 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800481 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800482 cache->purgeAllUnlocked();
483 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800484 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800485 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
486 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
487 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800488
489 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800490 cache->purgeAllUnlocked();
491 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800492 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800493 cache->getResourceBytes());
494 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
495 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800496
497 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800498 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
499 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
500 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
501 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800502
503 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800504 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
505 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
506 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
507 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800508}
509
bsalomon5236cf42015-01-14 10:42:08 -0800510static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800511 Mock mock(10, 30000);
512 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800513 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800514
bsalomon8718aaf2015-02-19 07:24:21 -0800515 GrUniqueKey uniqueKey;
516 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800517
518 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800519 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800520 TestResource* wrapped;
521 TestResource* unbudgeted;
522
523 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700524 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
525 TestResource::kB_SimulatedProperty);
526
bsalomon5236cf42015-01-14 10:42:08 -0800527 scratch->setSize(10);
528 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800529 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
530 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
531 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
532 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800533
halcanary385fe4d2015-08-26 13:07:48 -0700534 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800535 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800536 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800537 unique->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
bsalomon0ea80f42015-02-11 10:49:59 -0800543 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700544 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800545 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
546 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
547 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
548 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800549
550 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800551 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
552 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
553 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
554 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800555
kkinnunen2e6055b2016-04-22 01:48:29 -0700556 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800557 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
558 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
559 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
560 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800561
562 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800563 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
564 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
565 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
566 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800567
bsalomon0ea80f42015-02-11 10:49:59 -0800568 cache->purgeAllUnlocked();
569 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
570 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
571 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
572 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800573}
574
bsalomon3582d3e2015-02-13 14:20:05 -0800575// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
576void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
577/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800578 Mock mock(10, 300);
579 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800580 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800581
582 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700583 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
584 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800585 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800586 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800587
588 size_t size = resource->gpuMemorySize();
589 for (int i = 0; i < 2; ++i) {
590 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800591 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800592 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800593 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700594 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800595 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
596 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
597 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
598 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800599
600 // Once it is unrefed, it should become available as scratch.
601 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800602 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
603 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
604 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
605 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700606 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800607 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800608 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800609 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800610 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800611
612 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700613 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800614 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800615 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800616 } else {
617 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800618 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800619 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
620 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
621 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
622 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800623 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800624 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800625 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800626
627 // now when it is unrefed it should die since it has no key.
628 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800629 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
630 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
631 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
632 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800633 }
bsalomon8b79d232014-11-10 10:19:06 -0800634 }
bsalomonc2f35b72015-01-23 07:19:22 -0800635}
636
637static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
638 Mock mock(5, 30000);
639 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800640 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800641
bsalomon8b79d232014-11-10 10:19:06 -0800642 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800643 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700644 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800645 TestResource::kB_SimulatedProperty);
646 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700647 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800648 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800649 a->setSize(11);
650 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800651 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800652 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800653 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700654 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800655
656 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800657 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800658
bsalomon0ea80f42015-02-11 10:49:59 -0800659 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800660 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800661 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
662 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800663 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800664 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800665
bsalomon63c992f2015-01-23 12:47:59 -0800666 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800667 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800668 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800669 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800670
671 // Unref but don't purge
672 a->unref();
673 b->unref();
674 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800675 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800676
bsalomon63c992f2015-01-23 12:47:59 -0800677 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800678 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800679 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800680 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
681 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800682}
683
bsalomon10e23ca2014-11-25 05:52:06 -0800684static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800685 Mock mock(5, 30000);
686 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800687 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800688
bsalomon10e23ca2014-11-25 05:52:06 -0800689 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700690 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800691 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700692 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800693 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800694 a->unref();
695 b->unref();
696
bsalomon1c60dfe2015-01-21 09:32:40 -0800697 GrScratchKey scratchKey;
698 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800699 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800700 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700701 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800702
bsalomon0ea80f42015-02-11 10:49:59 -0800703 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800704 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800705 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800706 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
707 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800708
709 // Find the first resource and remove its scratch key
710 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700711 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800712 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800713 // It's still alive, but not cached by scratch key anymore
714 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800715 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
716 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800717
718 // The cache should immediately delete it when it's unrefed since it isn't accessible.
719 find->unref();
720 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800721 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
722 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800723
724 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700725 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800726 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800727 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800728 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
729 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800730
731 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800732 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800733 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800734 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
735 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800736
737 find->unref();
738 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800739 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
740 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800741}
742
bsalomon1c60dfe2015-01-21 09:32:40 -0800743static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800744 Mock mock(5, 30000);
745 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800746 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800747
748 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700749 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800750 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700751 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800752 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800753 a->unref();
754 b->unref();
755
756 GrScratchKey scratchKey;
757 // Ensure that scratch key comparison and assignment is consistent.
758 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800759 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800760 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800761 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800762 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
763 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
764 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
765 scratchKey = scratchKey1;
766 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
767 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
768 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
769 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
770 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
771 scratchKey = scratchKey2;
772 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
773 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
774 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
775 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
776 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
777
778 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800779 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800780 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700781 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800782
783 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800784 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700785 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700786 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800787 find->unref();
788
789 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700790 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700791 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800792 REPORTER_ASSERT(reporter, find == a || find == b);
793
robertphillips6e83ac72015-08-13 05:19:14 -0700794 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700795 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800796 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
797 REPORTER_ASSERT(reporter, find2 != find);
798 find2->unref();
799 find->unref();
800}
801
bsalomon8718aaf2015-02-19 07:24:21 -0800802static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800803 Mock mock(5, 30000);
804 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800805 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000806
bsalomon8718aaf2015-02-19 07:24:21 -0800807 GrUniqueKey key;
808 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700809
bsalomon8718aaf2015-02-19 07:24:21 -0800810 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700811 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800812 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700813
bsalomonf99e9612015-02-19 08:24:16 -0800814 // Set key on resource a.
815 a->resourcePriv().setUniqueKey(key);
816 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
817 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800818
bsalomonf99e9612015-02-19 08:24:16 -0800819 // Make sure that redundantly setting a's key works.
820 a->resourcePriv().setUniqueKey(key);
821 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800822 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800823 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
824 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800825 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
826
bsalomonf99e9612015-02-19 08:24:16 -0800827 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700828 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800829 b->setSize(12);
830 b->resourcePriv().setUniqueKey(key);
831 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
832 b->unref();
833
834 // Still have two resources because a is still reffed.
835 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
836 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
837 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
838
839 a->unref();
840 // Now a should be gone.
841 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
842 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
843 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
844
845 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
846 // Also make b be unreffed when replacement occurs.
847 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700848 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800849 GrUniqueKey differentKey;
850 make_unique_key<0>(&differentKey, 1);
851 c->setSize(13);
852 c->resourcePriv().setUniqueKey(differentKey);
853 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
854 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
855 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
856 // c replaces b and b should be immediately purged.
857 c->resourcePriv().setUniqueKey(key);
858 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
859 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
860 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
861
862 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800863 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800864 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
865 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
866 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
867
868 // Drop the ref on c, it should be kept alive because it has a unique key.
869 c->unref();
870 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
871 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
872 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
873
874 // Verify that we can find c, then remove its unique key. It should get purged immediately.
875 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
876 c->resourcePriv().removeUniqueKey();
877 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800878 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
879 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800880 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700881
882 {
883 GrUniqueKey key2;
884 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400885 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700886 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700887 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700888 d->resourcePriv().setUniqueKey(key2);
889 }
890
891 GrUniqueKey key3;
892 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400893 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700894 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000895}
896
bsalomon8b79d232014-11-10 10:19:06 -0800897static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800898 Mock mock(5, 30000);
899 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800900 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800901
bsalomon8718aaf2015-02-19 07:24:21 -0800902 GrUniqueKey key1, key2, key3;
903 make_unique_key<0>(&key1, 1);
904 make_unique_key<0>(&key2, 2);
905 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700906
bsalomon23e619c2015-02-06 11:54:28 -0800907 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700908 TestResource* a = new TestResource(context->getGpu());
909 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700910 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800911 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800912 a->resourcePriv().setUniqueKey(key1);
913 b->resourcePriv().setUniqueKey(key2);
914 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800915 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800916 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800917 c->unref();
918
bsalomon8718aaf2015-02-19 07:24:21 -0800919 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
920 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
921 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800922 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800923
bsalomon8718aaf2015-02-19 07:24:21 -0800924 typedef GrUniqueKeyInvalidatedMessage Msg;
925 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800926
927 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
928 Bus::Post(Msg(key1));
929 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800930 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800931 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800932 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
933 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800934 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800935 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800936
937 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800938 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800939 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800940 // we still have a ref on b, c should be recycled as scratch.
941 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800942 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800943
bsalomon23e619c2015-02-06 11:54:28 -0800944 // make b purgeable. It should be immediately deleted since it has no key.
945 b->unref();
946 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
947
948 // Make sure we actually get to c via it's scratch key, before we say goodbye.
949 GrScratchKey scratchKey;
950 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700951 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800952 REPORTER_ASSERT(reporter, scratch == c);
953 SkSafeUnref(scratch);
954
955 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800956 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700957 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800958 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800959 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
960 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800961 REPORTER_ASSERT(reporter, !scratch);
962 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800963}
964
bsalomon71cb0c22014-11-14 12:10:14 -0800965static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800966 Mock mock(3, 30000);
967 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800968 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800969
bsalomon8718aaf2015-02-19 07:24:21 -0800970 GrUniqueKey key1, key2;
971 make_unique_key<0>(&key1, 1);
972 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000973
halcanary385fe4d2015-08-26 13:07:48 -0700974 TestResource* a = new TestResource(context->getGpu());
975 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800976 a->resourcePriv().setUniqueKey(key1);
977 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800978
bsalomonc2f35b72015-01-23 07:19:22 -0800979 // Make a cycle
980 a->setUnrefWhenDestroyed(b);
981 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800982
bsalomonc2f35b72015-01-23 07:19:22 -0800983 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800984
bsalomonc2f35b72015-01-23 07:19:22 -0800985 a->unref();
986 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800987
bsalomonc2f35b72015-01-23 07:19:22 -0800988 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800989
bsalomon0ea80f42015-02-11 10:49:59 -0800990 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800991 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800992
bsalomonc2f35b72015-01-23 07:19:22 -0800993 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -0700994 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800995 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800996
bsalomon0ea80f42015-02-11 10:49:59 -0800997 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800998 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000999}
1000
bsalomon8b79d232014-11-10 10:19:06 -08001001static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001002 GrUniqueKey key1, key2;
1003 make_unique_key<0>(&key1, 1);
1004 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001005
1006 // Test changing resources sizes (both increase & decrease).
1007 {
bsalomonc2f35b72015-01-23 07:19:22 -08001008 Mock mock(3, 30000);
1009 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001010 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001011
halcanary385fe4d2015-08-26 13:07:48 -07001012 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001013 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001014 a->unref();
1015
halcanary385fe4d2015-08-26 13:07:48 -07001016 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001017 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001018 b->unref();
1019
bsalomon0ea80f42015-02-11 10:49:59 -08001020 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1021 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001022 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001023 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001024 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001025 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001026 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001027 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001028 find1->setSize(50);
1029 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001030
bsalomon0ea80f42015-02-11 10:49:59 -08001031 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1032 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001033 }
1034
1035 // Test increasing a resources size beyond the cache budget.
1036 {
bsalomonc2f35b72015-01-23 07:19:22 -08001037 Mock mock(2, 300);
1038 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001039 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001040
halcanary385fe4d2015-08-26 13:07:48 -07001041 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001042 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001043 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001044 a->unref();
1045
halcanary385fe4d2015-08-26 13:07:48 -07001046 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001047 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001048 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001049 b->unref();
1050
bsalomon0ea80f42015-02-11 10:49:59 -08001051 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1052 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001053
bsalomon8b79d232014-11-10 10:19:06 -08001054 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001055 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001056 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001057 find2->setSize(201);
1058 }
bsalomon8718aaf2015-02-19 07:24:21 -08001059 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001060
bsalomon0ea80f42015-02-11 10:49:59 -08001061 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1062 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001063 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001064}
1065
bsalomonddf30e62015-02-19 11:38:44 -08001066static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1067 static const int kCount = 50;
1068 static const int kBudgetCnt = kCount / 2;
1069 static const int kLockedFreq = 8;
1070 static const int kBudgetSize = 0x80000000;
1071
1072 SkRandom random;
1073
1074 // Run the test 2*kCount times;
1075 for (int i = 0; i < 2 * kCount; ++i ) {
1076 Mock mock(kBudgetCnt, kBudgetSize);
1077 GrContext* context = mock.context();
1078 GrResourceCache* cache = mock.cache();
1079
1080 // Pick a random number of resources to add before the timestamp will wrap.
1081 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1082
1083 static const int kNumToPurge = kCount - kBudgetCnt;
1084
1085 SkTDArray<int> shouldPurgeIdxs;
1086 int purgeableCnt = 0;
1087 SkTDArray<GrGpuResource*> resourcesToUnref;
1088
1089 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1090 // unpurgeable resources.
1091 for (int j = 0; j < kCount; ++j) {
1092 GrUniqueKey key;
1093 make_unique_key<0>(&key, j);
1094
halcanary385fe4d2015-08-26 13:07:48 -07001095 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001096 r->resourcePriv().setUniqueKey(key);
1097 if (random.nextU() % kLockedFreq) {
1098 // Make this is purgeable.
1099 r->unref();
1100 ++purgeableCnt;
1101 if (purgeableCnt <= kNumToPurge) {
1102 *shouldPurgeIdxs.append() = j;
1103 }
1104 } else {
1105 *resourcesToUnref.append() = r;
1106 }
1107 }
1108
1109 // Verify that the correct resources were purged.
1110 int currShouldPurgeIdx = 0;
1111 for (int j = 0; j < kCount; ++j) {
1112 GrUniqueKey key;
1113 make_unique_key<0>(&key, j);
1114 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1115 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1116 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1117 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001118 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001119 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001120 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001121 }
1122 SkSafeUnref(res);
1123 }
1124
1125 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1126 resourcesToUnref[j]->unref();
1127 }
1128 }
1129}
1130
bsalomon3f324322015-04-08 11:01:54 -07001131static void test_flush(skiatest::Reporter* reporter) {
1132 Mock mock(1000000, 1000000);
1133 GrContext* context = mock.context();
1134 GrResourceCache* cache = mock.cache();
1135
1136 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1137 // power of two here to keep things simpler.
1138 static const int kFlushCount = 16;
1139 cache->setLimits(1000000, 1000000, kFlushCount);
1140
1141 {
1142 // Insert a resource and send a flush notification kFlushCount times.
1143 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001144 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001145 GrUniqueKey k;
1146 make_unique_key<1>(&k, i);
1147 r->resourcePriv().setUniqueKey(k);
1148 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001149 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001150 }
1151
1152 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001153 for (int i = 0; i < kFlushCount; ++i) {
1154 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001155 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1156 for (int j = 0; j < i; ++j) {
1157 GrUniqueKey k;
1158 make_unique_key<1>(&k, j);
1159 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1160 REPORTER_ASSERT(reporter, !SkToBool(r));
1161 SkSafeUnref(r);
1162 }
bsalomon3f324322015-04-08 11:01:54 -07001163 }
1164
1165 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1166 cache->purgeAllUnlocked();
1167 }
1168
1169 // Do a similar test but where we leave refs on some resources to prevent them from being
1170 // purged.
1171 {
1172 GrGpuResource* refedResources[kFlushCount >> 1];
1173 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001174 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001175 GrUniqueKey k;
1176 make_unique_key<1>(&k, i);
1177 r->resourcePriv().setUniqueKey(k);
1178 // Leave a ref on every other resource, beginning with the first.
1179 if (SkToBool(i & 0x1)) {
1180 refedResources[i/2] = r;
1181 } else {
1182 r->unref();
1183 }
bsalomonb77a9072016-09-07 10:02:04 -07001184 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001185 }
1186
1187 for (int i = 0; i < kFlushCount; ++i) {
1188 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001189 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001190 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001191 }
1192
1193 // Unref all the resources that we kept refs on in the first loop.
1194 for (int i = 0; i < kFlushCount >> 1; ++i) {
1195 refedResources[i]->unref();
1196 }
1197
bsalomone2e87f32016-09-22 12:42:11 -07001198 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1199 // kFlushCount full flushes.
1200 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001201 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001202 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001203 }
1204 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1205
1206 cache->purgeAllUnlocked();
1207 }
1208
1209 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001210
1211 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1212 // eviction.
1213 context->flush();
1214 for (int i = 0; i < 10; ++i) {
1215 TestResource* r = new TestResource(context->getGpu());
1216 GrUniqueKey k;
1217 make_unique_key<1>(&k, i);
1218 r->resourcePriv().setUniqueKey(k);
1219 r->unref();
1220 }
1221 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1222 for (int i = 0; i < 10 * kFlushCount; ++i) {
1223 context->flush();
1224 }
1225 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001226}
1227
bsalomon10e23ca2014-11-25 05:52:06 -08001228static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001229 // Set the cache size to double the resource count because we're going to create 2x that number
1230 // resources, using two different key domains. Add a little slop to the bytes because we resize
1231 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001232 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001233
bsalomonc2f35b72015-01-23 07:19:22 -08001234 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1235 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001236 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001237
1238 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001239 GrUniqueKey key1, key2;
1240 make_unique_key<1>(&key1, i);
1241 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001242
bsalomon24db3b12015-01-23 04:24:04 -08001243 TestResource* resource;
1244
halcanary385fe4d2015-08-26 13:07:48 -07001245 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001246 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001247 resource->setSize(1);
1248 resource->unref();
1249
halcanary385fe4d2015-08-26 13:07:48 -07001250 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001251 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001252 resource->setSize(1);
1253 resource->unref();
1254 }
1255
1256 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001257 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1258 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1259 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1260 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001261 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001262 GrUniqueKey key1, key2;
1263 make_unique_key<1>(&key1, i);
1264 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001265
bsalomon8718aaf2015-02-19 07:24:21 -08001266 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1267 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001268 }
1269
bsalomon0ea80f42015-02-11 10:49:59 -08001270 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001271 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001272 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1273 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1274 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1275 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001276
1277 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001278 GrUniqueKey key1, key2;
1279 make_unique_key<1>(&key1, i);
1280 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001281
bsalomon8718aaf2015-02-19 07:24:21 -08001282 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1283 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001284 }
1285}
1286
senorblanco84cd6212015-08-04 10:01:58 -07001287static void test_custom_data(skiatest::Reporter* reporter) {
1288 GrUniqueKey key1, key2;
1289 make_unique_key<0>(&key1, 1);
1290 make_unique_key<0>(&key2, 2);
1291 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001292 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001293 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1294 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1295
1296 // Test that copying a key also takes a ref on its custom data.
1297 GrUniqueKey key3 = key1;
1298 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1299}
1300
bsalomonc6363ef2015-09-24 07:07:40 -07001301static void test_abandoned(skiatest::Reporter* reporter) {
1302 Mock mock(10, 300);
1303 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001304 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001305 context->abandonContext();
1306
1307 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1308
1309 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1310
robertphillips8abb3702016-08-31 14:04:06 -07001311 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001312 resource->getUniqueKey();
1313 resource->wasDestroyed();
1314 resource->gpuMemorySize();
1315 resource->getContext();
1316
1317 resource->abandon();
1318 resource->resourcePriv().getScratchKey();
1319 resource->resourcePriv().isBudgeted();
1320 resource->resourcePriv().makeBudgeted();
1321 resource->resourcePriv().makeUnbudgeted();
1322 resource->resourcePriv().removeScratchKey();
1323 GrUniqueKey key;
1324 make_unique_key<0>(&key, 1);
1325 resource->resourcePriv().setUniqueKey(key);
1326 resource->resourcePriv().removeUniqueKey();
1327}
1328
Brian Salomon1090da62017-01-06 12:04:19 -05001329static void test_tags(skiatest::Reporter* reporter) {
1330#ifdef SK_DEBUG
1331 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1332 static constexpr int kLastTagIdx = 10;
1333 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1334
1335 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1336 GrContext* context = mock.context();
1337 GrResourceCache* cache = mock.cache();
1338
1339 SkString tagStr;
1340 int tagIdx = 0;
1341 int currTagCnt = 0;
1342
1343 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1344 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1345 GrUniqueKey key;
1346 if (currTagCnt == tagIdx) {
1347 tagIdx += 1;
1348 currTagCnt = 0;
1349 tagStr.printf("tag%d", tagIdx);
1350 }
1351 make_unique_key<1>(&key, i, tagStr.c_str());
1352 resource->resourcePriv().setUniqueKey(key);
1353 }
1354 SkASSERT(kLastTagIdx == tagIdx);
1355 SkASSERT(currTagCnt == kLastTagIdx);
1356
1357 // Test i = 0 to exercise unused tag string.
1358 for (int i = 0; i <= kLastTagIdx; ++i) {
1359 tagStr.printf("tag%d", i);
1360 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1361 }
1362#endif
1363}
1364
kkinnunen15302832015-12-01 04:35:26 -08001365DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001366 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001367 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001368 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001369 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001370 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001371 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001372 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001373 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001374 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001375 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001376 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001377 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001378 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001379 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001380 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001381 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001382 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001383 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001384}
1385
Robert Phillipsd6214d42016-11-07 08:23:48 -05001386////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001387static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001388 GrSurfaceFlags flags,
1389 int width, int height,
1390 int sampleCnt) {
1391 GrSurfaceDesc desc;
1392 desc.fFlags = flags;
1393 desc.fWidth = width;
1394 desc.fHeight = height;
1395 desc.fConfig = kRGBA_8888_GrPixelConfig;
1396 desc.fSampleCnt = sampleCnt;
1397
1398 return sk_sp<GrTexture>(provider->createTexture(desc, SkBudgeted::kYes));
1399}
1400
Brian Osman32342f02017-03-04 08:12:46 -05001401static sk_sp<GrTexture> make_mipmap_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001402 GrSurfaceFlags flags,
1403 int width, int height,
1404 int sampleCnt) {
1405 SkBitmap bm;
1406
1407 bm.allocN32Pixels(width, height, true);
1408 bm.eraseColor(SK_ColorBLUE);
1409
Brian Osman7b8400d2016-11-08 17:08:54 -05001410 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001411 SkASSERT(mipmaps);
1412 SkASSERT(mipmaps->countLevels() > 1);
1413
1414 int mipLevelCount = mipmaps->countLevels() + 1;
1415
1416 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1417
1418 texels[0].fPixels = bm.getPixels();
1419 texels[0].fRowBytes = bm.rowBytes();
1420
1421 for (int i = 1; i < mipLevelCount; ++i) {
1422 SkMipMap::Level generatedMipLevel;
1423 mipmaps->getLevel(i - 1, &generatedMipLevel);
1424 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1425 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1426 }
1427
1428 GrSurfaceDesc desc;
1429 desc.fFlags = flags;
1430 desc.fWidth = width;
1431 desc.fHeight = height;
1432 desc.fConfig = kRGBA_8888_GrPixelConfig;
1433 desc.fSampleCnt = sampleCnt;
1434 desc.fIsMipMapped = true;
1435
1436 return sk_sp<GrTexture>(provider->createMipMappedTexture(desc, SkBudgeted::kYes,
1437 texels.get(), mipLevelCount));
1438}
1439
1440// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1441// Texture-only, both-RT-and-Texture and MIPmapped
1442DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1443 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001444 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001445
Robert Phillipsd6214d42016-11-07 08:23:48 -05001446 static const int kSize = 64;
1447
1448 sk_sp<GrTexture> tex;
1449
1450 // Normal versions
1451 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1452 size_t size = tex->gpuMemorySize();
1453 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1454
1455 if (context->caps()->maxSampleCount() >= 4) {
1456 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1457 size = tex->gpuMemorySize();
1458 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1459 kSize*kSize*4*4 == size || // auto-resolving
1460 kSize*kSize*4*5 == size); // explicit resolve buffer
1461 }
1462
1463 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1464 size = tex->gpuMemorySize();
1465 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1466
1467 // Mipmapped versions
1468 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1469 size = tex->gpuMemorySize();
1470 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1471
1472 if (context->caps()->maxSampleCount() >= 4) {
1473 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1474 size = tex->gpuMemorySize();
1475 REPORTER_ASSERT(reporter,
1476 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1477 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1478 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
1479 }
1480
1481 tex = make_mipmap_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1482 size = tex->gpuMemorySize();
1483 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1484}
1485
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001486#endif