blob: 45946cf7d84600f6eddee84cba618fe640f17c9b [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
Brian Salomon5e150852017-03-22 14:53:13 -040012#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000014#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000015#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080017#include "GrGpuResourceCacheAccess.h"
18#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTarget.h"
20#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070023#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070024#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080025#include "SkGr.h"
26#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050027#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070028#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000029#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000030
31static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070036 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080037 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040038 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080039 desc.fFlags = kRenderTarget_GrSurfaceFlag;
40 desc.fWidth = gWidth;
41 desc.fHeight = gHeight;
42 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070043 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080044 SkCanvas* canvas = surface->getCanvas();
45
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000046 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
47
48 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000049 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050 src.eraseColor(SK_ColorBLACK);
51 size_t srcSize = src.getSize();
52
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000053 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070054 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000055
56 int oldMaxNum;
57 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000058 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000059
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060 // Set the cache limits so we can fit 10 "src" images and the
61 // max number of textures doesn't matter
62 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000063 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000066 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000067
68 for (int i = 0; i < 100; ++i) {
69 canvas->drawBitmap(src, 0, 0);
Mike Reed12e946b2017-04-17 10:53:29 -040070 canvas->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000071
72 // "modify" the src texture
73 src.notifyPixelsChanged();
74
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000075 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070076 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000077
78 // we should never go over the size limit
79 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
80 }
81
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000082 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000083}
84
bsalomon11abd8d2016-10-14 08:13:48 -070085static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
86 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
87 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
88 return false;
89 }
90 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
91}
92
93// This currently fails on ES3 ANGLE contexts
94DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
95 ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070096 GrContext* context = ctxInfo.grContext();
bsalomon02a44a42015-02-19 09:09:00 -080097 GrSurfaceDesc smallDesc;
98 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -040099 smallDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800100 smallDesc.fWidth = 4;
101 smallDesc.fHeight = 4;
102 smallDesc.fSampleCnt = 0;
103
Eric Karl5c779752017-05-08 12:02:07 -0700104 if (context->caps()->avoidStencilBuffers()) {
105 return;
106 }
egdanielec00d942015-09-14 12:56:10 -0700107 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800108 // Test that two budgeted RTs with the same desc share a stencil buffer.
Brian Osman32342f02017-03-04 08:12:46 -0500109 sk_sp<GrTexture> smallRT0(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800110 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700111 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800112 }
113
Brian Osman32342f02017-03-04 08:12:46 -0500114 sk_sp<GrTexture> smallRT1(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800115 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700116 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800117 }
118
egdaniel8dc7c3a2015-04-16 11:22:42 -0700119 REPORTER_ASSERT(reporter,
120 smallRT0 && smallRT1 &&
121 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700122 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
123 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800124
125 // An unbudgeted RT with the same desc should also share.
Brian Osman32342f02017-03-04 08:12:46 -0500126 sk_sp<GrTexture> smallRT2(resourceProvider->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800127 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700128 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800129 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700130 REPORTER_ASSERT(reporter,
131 smallRT0 && smallRT2 &&
132 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700133 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
134 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800135
136 // An RT with a much larger size should not share.
137 GrSurfaceDesc bigDesc;
138 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400139 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800140 bigDesc.fWidth = 400;
141 bigDesc.fHeight = 200;
142 bigDesc.fSampleCnt = 0;
Brian Osman32342f02017-03-04 08:12:46 -0500143 sk_sp<GrTexture> bigRT(resourceProvider->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800144 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700145 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800146 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700147 REPORTER_ASSERT(reporter,
148 smallRT0 && bigRT &&
149 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700150 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
151 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800152
bsalomon76228632015-05-29 08:02:10 -0700153 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700154 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800155 GrSurfaceDesc smallMSAADesc = smallDesc;
156 smallMSAADesc.fSampleCnt = 4;
Brian Osman32342f02017-03-04 08:12:46 -0500157 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
158 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800159 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700160 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800161 }
bsalomonb602d4d2015-02-19 12:05:58 -0800162#ifdef SK_BUILD_FOR_ANDROID
163 if (!smallMSAART0) {
164 // The nexus player seems to fail to create MSAA textures.
165 return;
166 }
167#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800168 REPORTER_ASSERT(reporter,
169 smallRT0 && smallMSAART0 &&
170 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700171 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
172 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800173 // A second MSAA RT should share with the first MSAA RT.
Brian Osman32342f02017-03-04 08:12:46 -0500174 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
175 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800176 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700177 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800178 }
179 REPORTER_ASSERT(reporter,
180 smallMSAART0 && smallMSAART1 &&
181 smallMSAART0->asRenderTarget() &&
182 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700183 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
184 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800185 // But not one with a larger sample count should not. (Also check that the request for 4
186 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700187 if (context->caps()->maxSampleCount() >= 8 &&
188 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700189 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800190 smallMSAADesc.fSampleCnt = 8;
Robert Phillipse78b7252017-04-06 07:59:41 -0400191 smallMSAART1 = resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo);
Hal Canary342b7ac2016-11-04 11:49:42 -0400192 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500193 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800194 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700195 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800196 }
197 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700198 smallMSAART0 && smallMSAART1 &&
199 smallMSAART0->asRenderTarget() &&
200 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700201 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
202 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800203 }
204 }
205}
206
bsalomon68d91342016-04-12 09:59:58 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700208 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800209 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700210 // this test is only valid for GL
211 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 return;
213 }
214
Brian Osman85d34b22017-05-10 12:06:26 -0400215 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216 static const int kW = 100;
217 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700218
bsalomon091f60c2015-11-10 11:54:56 -0800219 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
220 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700221
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222 context->resetContext();
223
Greg Daniel7ef28f32017-04-20 16:41:55 +0000224 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
225 kW,
226 kH,
227 kRGBA_8888_GrPixelConfig,
228 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500229 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000230 backendTex1, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
231 kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
Greg Daniel7ef28f32017-04-20 16:41:55 +0000233 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
234 kW,
235 kH,
236 kRGBA_8888_GrPixelConfig,
237 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500238 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000239 backendTex2, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
240 kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241
Brian Osman85d34b22017-05-10 12:06:26 -0400242 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
243 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244 return;
245 }
246
halcanary96fcdcc2015-08-27 07:41:13 -0700247 borrowed.reset(nullptr);
248 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700249
250 context->flush();
251
bsalomon091f60c2015-11-10 11:54:56 -0800252 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
253 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700254
255 REPORTER_ASSERT(reporter, borrowedIsAlive);
256 REPORTER_ASSERT(reporter, !adoptedIsAlive);
257
bsalomon67d76202015-11-11 12:40:42 -0800258 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
259 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700260
261 context->resetContext();
262}
263
bsalomon6d3fe022014-07-25 08:35:45 -0700264class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800265 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000266public:
robertphillips6e83ac72015-08-13 05:19:14 -0700267 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700268
bsalomon1c60dfe2015-01-21 09:32:40 -0800269 /** Property that distinctly categorizes the resource.
270 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800271 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800272
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
274 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700275 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800276 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700277 , fProperty(kA_SimulatedProperty)
278 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800279 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700280 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800281 }
282
kkinnunen2e6055b2016-04-22 01:48:29 -0700283 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
284 SimulatedProperty property) {
285 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800286 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700287 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
288 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289 }
290
Brian Salomond3b65972017-03-22 12:05:03 -0400291 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800292 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800293 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294 }
295
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000296 void setSize(size_t size) {
297 fSize = size;
298 this->didChangeGpuMemorySize();
299 }
300
bsalomon33435572014-11-05 14:47:41 -0800301 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000302
bsalomon71cb0c22014-11-14 12:10:14 -0800303 void setUnrefWhenDestroyed(TestResource* resource) {
304 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000305 }
306
bsalomon1c60dfe2015-01-21 09:32:40 -0800307 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
308 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
309 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800310 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
311 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800312 }
313 }
314
315 static size_t ExpectedScratchKeySize() {
316 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
317 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000318private:
bsalomon24db3b12015-01-23 04:24:04 -0800319 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800320
kkinnunen2e6055b2016-04-22 01:48:29 -0700321 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
322 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700323 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800324 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700325 , fProperty(property)
326 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800327 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700328 this->registerWithCache(budgeted);
329 }
330
331 // Constructor for simulating resources that wrap backend objects.
332 TestResource(GrGpu* gpu, size_t size)
333 : INHERITED(gpu)
334 , fToDelete(nullptr)
335 , fSize(size)
336 , fProperty(kA_SimulatedProperty)
337 , fIsScratch(false) {
338 ++fNumAlive;
339 this->registerWithCacheWrapped();
340 }
341
342 void computeScratchKey(GrScratchKey* key) const override {
343 if (fIsScratch) {
344 ComputeScratchKey(fProperty, key);
345 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800346 }
347
mtklein36352bf2015-03-25 18:17:31 -0700348 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800349
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000350 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000351 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800352 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800353 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700354 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700355 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000356};
bsalomon33435572014-11-05 14:47:41 -0800357int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000358
bsalomonc2f35b72015-01-23 07:19:22 -0800359class Mock {
360public:
361 Mock(int maxCnt, size_t maxBytes) {
362 fContext.reset(GrContext::CreateMockContext());
363 SkASSERT(fContext);
364 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800365 GrResourceCache* cache = fContext->getResourceCache();
366 cache->purgeAllUnlocked();
367 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800368 }
bsalomonc2f35b72015-01-23 07:19:22 -0800369
bsalomon0ea80f42015-02-11 10:49:59 -0800370 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800371
Hal Canary342b7ac2016-11-04 11:49:42 -0400372 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800373
374private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400375 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800376};
377
378static void test_no_key(skiatest::Reporter* reporter) {
379 Mock mock(10, 30000);
380 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800381 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700384 TestResource* a = new TestResource(context->getGpu());
385 TestResource* b = new TestResource(context->getGpu());
386 TestResource* c = new TestResource(context->getGpu());
387 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800388 a->setSize(11);
389 b->setSize(12);
390 c->setSize(13);
391 d->setSize(14);
392
393 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800394 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800395 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800396 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800397
398 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800400 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
401
bsalomon8718aaf2015-02-19 07:24:21 -0800402 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800403
404 a->unref();
405 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800406 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800407 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800408 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800409
410 c->unref();
411 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800413 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800414 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415
416 d->unref();
417 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800418 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
419 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800420
421 b->unref();
422 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800423 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
424 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800425}
426
bsalomon24db3b12015-01-23 04:24:04 -0800427// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500428template <int>
429static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800430 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500431 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800432 builder[0] = data;
433}
434
bsalomon84c8e622014-11-17 09:33:27 -0800435static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800436 Mock mock(10, 300);
437 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800438 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800439
bsalomon8718aaf2015-02-19 07:24:21 -0800440 GrUniqueKey uniqueKey;
441 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800442
bsalomon8718aaf2015-02-19 07:24:21 -0800443 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800444 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700445 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800446 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700447 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800448 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800449 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700450 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800451 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700452 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700453 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800454 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800455
Brian Osman0562eb92017-05-08 11:16:39 -0400456 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800457 GrUniqueKey uniqueKey2;
458 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800459 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400460 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
461 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
462
463 // Remove the extra ref we just added.
464 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800465
466 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800467 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800468 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800469 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800472 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800474
bsalomon63c992f2015-01-23 12:47:59 -0800475 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800476 cache->purgeAllUnlocked();
477 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800478 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800479 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800480 cache->getResourceBytes());
481 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800482 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800483 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800484
485 // Unreffing the wrapped resource should free it right away.
486 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800487 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800488 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800489 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800490
bsalomon84c8e622014-11-17 09:33:27 -0800491 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700492 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800493 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800494 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800495 cache->purgeAllUnlocked();
496 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800497 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800498 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
499 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
500 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800501
502 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800503 cache->purgeAllUnlocked();
504 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800505 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800506 cache->getResourceBytes());
507 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
508 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800509
510 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800511 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
512 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800515
516 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800517 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
518 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
519 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
520 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800521}
522
bsalomon5236cf42015-01-14 10:42:08 -0800523static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800524 Mock mock(10, 30000);
525 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800526 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800527
bsalomon8718aaf2015-02-19 07:24:21 -0800528 GrUniqueKey uniqueKey;
529 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800530
531 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800532 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800533 TestResource* wrapped;
534 TestResource* unbudgeted;
535
536 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700537 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
538 TestResource::kB_SimulatedProperty);
539
bsalomon5236cf42015-01-14 10:42:08 -0800540 scratch->setSize(10);
541 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800542 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
543 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
544 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
545 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800546
halcanary385fe4d2015-08-26 13:07:48 -0700547 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800548 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800549 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800550 unique->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
bsalomon0ea80f42015-02-11 10:49:59 -0800556 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700557 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800558 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
559 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
560 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
561 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800562
563 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800564 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
565 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
566 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
567 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800568
kkinnunen2e6055b2016-04-22 01:48:29 -0700569 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800570 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
571 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
572 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
573 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800574
575 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800580
bsalomon0ea80f42015-02-11 10:49:59 -0800581 cache->purgeAllUnlocked();
582 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800586}
587
bsalomon3582d3e2015-02-13 14:20:05 -0800588// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
589void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
590/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800591 Mock mock(10, 300);
592 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800593 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800594
595 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700596 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
597 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800598 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800599 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800600
601 size_t size = resource->gpuMemorySize();
602 for (int i = 0; i < 2; ++i) {
603 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800604 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800605 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800606 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700607 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800608 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
609 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
610 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
611 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800612
613 // Once it is unrefed, it should become available as scratch.
614 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800615 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
616 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
617 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
618 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700619 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800620 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800621 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800622 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800623 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800624
625 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700626 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800627 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800628 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800629 } else {
630 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800631 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800632 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
633 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
634 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
635 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800636 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800637 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800638 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800639
640 // now when it is unrefed it should die since it has no key.
641 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800642 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
643 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
644 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
645 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800646 }
bsalomon8b79d232014-11-10 10:19:06 -0800647 }
bsalomonc2f35b72015-01-23 07:19:22 -0800648}
649
650static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
651 Mock mock(5, 30000);
652 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800653 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800654
bsalomon8b79d232014-11-10 10:19:06 -0800655 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800656 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700657 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800658 TestResource::kB_SimulatedProperty);
659 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700660 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800661 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800662 a->setSize(11);
663 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800664 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800665 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800666 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700667 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800668
669 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800670 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800671
bsalomon0ea80f42015-02-11 10:49:59 -0800672 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800673 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800674 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
675 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800676 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800677 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800678
bsalomon63c992f2015-01-23 12:47:59 -0800679 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800680 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800681 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800682 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800683
684 // Unref but don't purge
685 a->unref();
686 b->unref();
687 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800688 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800689
bsalomon63c992f2015-01-23 12:47:59 -0800690 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800691 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800692 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800693 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
694 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800695}
696
bsalomon10e23ca2014-11-25 05:52:06 -0800697static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800698 Mock mock(5, 30000);
699 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800700 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800701
bsalomon10e23ca2014-11-25 05:52:06 -0800702 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700703 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800704 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700705 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800706 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800707 a->unref();
708 b->unref();
709
bsalomon1c60dfe2015-01-21 09:32:40 -0800710 GrScratchKey scratchKey;
711 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800712 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800713 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700714 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800715
bsalomon0ea80f42015-02-11 10:49:59 -0800716 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800717 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800718 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800719 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
720 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800721
722 // Find the first resource and remove its scratch key
723 GrGpuResource* find;
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 // It's still alive, but not cached by scratch key anymore
727 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800728 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
729 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800730
731 // The cache should immediately delete it when it's unrefed since it isn't accessible.
732 find->unref();
733 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800734 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
735 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800736
737 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700738 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800739 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800740 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800741 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
742 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800743
744 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800745 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800746 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800747 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
748 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800749
750 find->unref();
751 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800752 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
753 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800754}
755
bsalomon1c60dfe2015-01-21 09:32:40 -0800756static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800757 Mock mock(5, 30000);
758 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800759 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800760
761 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700762 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800763 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700764 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800765 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800766 a->unref();
767 b->unref();
768
769 GrScratchKey scratchKey;
770 // Ensure that scratch key comparison and assignment is consistent.
771 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800772 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800773 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800774 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800775 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
776 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
777 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
778 scratchKey = scratchKey1;
779 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
780 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
781 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
782 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
783 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
784 scratchKey = scratchKey2;
785 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
786 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
787 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
788 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
789 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
790
791 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800793 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700794 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800795
796 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800797 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700798 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700799 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800800 find->unref();
801
802 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700803 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700804 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800805 REPORTER_ASSERT(reporter, find == a || find == b);
806
robertphillips6e83ac72015-08-13 05:19:14 -0700807 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700808 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800809 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
810 REPORTER_ASSERT(reporter, find2 != find);
811 find2->unref();
812 find->unref();
813}
814
bsalomon8718aaf2015-02-19 07:24:21 -0800815static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800816 Mock mock(5, 30000);
817 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800818 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000819
bsalomon8718aaf2015-02-19 07:24:21 -0800820 GrUniqueKey key;
821 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700822
bsalomon8718aaf2015-02-19 07:24:21 -0800823 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700824 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800825 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700826
bsalomonf99e9612015-02-19 08:24:16 -0800827 // Set key on resource a.
828 a->resourcePriv().setUniqueKey(key);
829 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
830 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800831
bsalomonf99e9612015-02-19 08:24:16 -0800832 // Make sure that redundantly setting a's key works.
833 a->resourcePriv().setUniqueKey(key);
834 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800835 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800836 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
837 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800838 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
839
bsalomonf99e9612015-02-19 08:24:16 -0800840 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700841 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800842 b->setSize(12);
843 b->resourcePriv().setUniqueKey(key);
844 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
845 b->unref();
846
847 // Still have two resources because a is still reffed.
848 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
849 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
850 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
851
852 a->unref();
853 // Now a should be gone.
854 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
855 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
856 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
857
858 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
859 // Also make b be unreffed when replacement occurs.
860 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700861 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800862 GrUniqueKey differentKey;
863 make_unique_key<0>(&differentKey, 1);
864 c->setSize(13);
865 c->resourcePriv().setUniqueKey(differentKey);
866 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
867 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
868 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
869 // c replaces b and b should be immediately purged.
870 c->resourcePriv().setUniqueKey(key);
871 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
872 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
873 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
874
875 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800876 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800877 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
878 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
879 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
880
881 // Drop the ref on c, it should be kept alive because it has a unique key.
882 c->unref();
883 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
884 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
885 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
886
887 // Verify that we can find c, then remove its unique key. It should get purged immediately.
888 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
889 c->resourcePriv().removeUniqueKey();
890 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800891 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
892 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800893 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700894
895 {
896 GrUniqueKey key2;
897 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400898 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700899 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700900 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700901 d->resourcePriv().setUniqueKey(key2);
902 }
903
904 GrUniqueKey key3;
905 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400906 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700907 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000908}
909
bsalomon8b79d232014-11-10 10:19:06 -0800910static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800911 Mock mock(5, 30000);
912 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800913 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800914
bsalomon8718aaf2015-02-19 07:24:21 -0800915 GrUniqueKey key1, key2, key3;
916 make_unique_key<0>(&key1, 1);
917 make_unique_key<0>(&key2, 2);
918 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700919
bsalomon23e619c2015-02-06 11:54:28 -0800920 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700921 TestResource* a = new TestResource(context->getGpu());
922 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700923 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800924 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800925 a->resourcePriv().setUniqueKey(key1);
926 b->resourcePriv().setUniqueKey(key2);
927 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800928 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800929 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800930 c->unref();
931
bsalomon8718aaf2015-02-19 07:24:21 -0800932 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
933 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
934 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800935 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800936
bsalomon8718aaf2015-02-19 07:24:21 -0800937 typedef GrUniqueKeyInvalidatedMessage Msg;
938 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800939
940 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
941 Bus::Post(Msg(key1));
942 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800943 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800944 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800945 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
946 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800947 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800948 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800949
950 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800951 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800952 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800953 // we still have a ref on b, c should be recycled as scratch.
954 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800955 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800956
bsalomon23e619c2015-02-06 11:54:28 -0800957 // make b purgeable. It should be immediately deleted since it has no key.
958 b->unref();
959 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
960
961 // Make sure we actually get to c via it's scratch key, before we say goodbye.
962 GrScratchKey scratchKey;
963 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700964 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800965 REPORTER_ASSERT(reporter, scratch == c);
966 SkSafeUnref(scratch);
967
968 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800969 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700970 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800971 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800972 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
973 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800974 REPORTER_ASSERT(reporter, !scratch);
975 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800976}
977
bsalomon71cb0c22014-11-14 12:10:14 -0800978static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800979 Mock mock(3, 30000);
980 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800981 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800982
bsalomon8718aaf2015-02-19 07:24:21 -0800983 GrUniqueKey key1, key2;
984 make_unique_key<0>(&key1, 1);
985 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000986
halcanary385fe4d2015-08-26 13:07:48 -0700987 TestResource* a = new TestResource(context->getGpu());
988 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800989 a->resourcePriv().setUniqueKey(key1);
990 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800991
bsalomonc2f35b72015-01-23 07:19:22 -0800992 // Make a cycle
993 a->setUnrefWhenDestroyed(b);
994 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800995
bsalomonc2f35b72015-01-23 07:19:22 -0800996 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800997
bsalomonc2f35b72015-01-23 07:19:22 -0800998 a->unref();
999 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001000
bsalomonc2f35b72015-01-23 07:19:22 -08001001 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001002
bsalomon0ea80f42015-02-11 10:49:59 -08001003 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001004 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001005
bsalomonc2f35b72015-01-23 07:19:22 -08001006 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001007 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001008 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001009
bsalomon0ea80f42015-02-11 10:49:59 -08001010 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001011 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001012}
1013
bsalomon8b79d232014-11-10 10:19:06 -08001014static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001015 GrUniqueKey key1, key2;
1016 make_unique_key<0>(&key1, 1);
1017 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001018
1019 // Test changing resources sizes (both increase & decrease).
1020 {
bsalomonc2f35b72015-01-23 07:19:22 -08001021 Mock mock(3, 30000);
1022 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001023 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001024
halcanary385fe4d2015-08-26 13:07:48 -07001025 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001026 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001027 a->unref();
1028
halcanary385fe4d2015-08-26 13:07:48 -07001029 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001030 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001031 b->unref();
1032
bsalomon0ea80f42015-02-11 10:49:59 -08001033 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1034 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001035 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001036 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001037 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001038 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001039 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001040 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001041 find1->setSize(50);
1042 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001043
bsalomon0ea80f42015-02-11 10:49:59 -08001044 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1045 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001046 }
1047
1048 // Test increasing a resources size beyond the cache budget.
1049 {
bsalomonc2f35b72015-01-23 07:19:22 -08001050 Mock mock(2, 300);
1051 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001052 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001053
halcanary385fe4d2015-08-26 13:07:48 -07001054 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001055 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001056 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001057 a->unref();
1058
halcanary385fe4d2015-08-26 13:07:48 -07001059 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001060 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001061 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062 b->unref();
1063
bsalomon0ea80f42015-02-11 10:49:59 -08001064 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1065 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001066
bsalomon8b79d232014-11-10 10:19:06 -08001067 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001068 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001069 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001070 find2->setSize(201);
1071 }
bsalomon8718aaf2015-02-19 07:24:21 -08001072 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001073
bsalomon0ea80f42015-02-11 10:49:59 -08001074 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1075 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001076 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001077}
1078
bsalomonddf30e62015-02-19 11:38:44 -08001079static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1080 static const int kCount = 50;
1081 static const int kBudgetCnt = kCount / 2;
1082 static const int kLockedFreq = 8;
1083 static const int kBudgetSize = 0x80000000;
1084
1085 SkRandom random;
1086
1087 // Run the test 2*kCount times;
1088 for (int i = 0; i < 2 * kCount; ++i ) {
1089 Mock mock(kBudgetCnt, kBudgetSize);
1090 GrContext* context = mock.context();
1091 GrResourceCache* cache = mock.cache();
1092
1093 // Pick a random number of resources to add before the timestamp will wrap.
1094 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1095
1096 static const int kNumToPurge = kCount - kBudgetCnt;
1097
1098 SkTDArray<int> shouldPurgeIdxs;
1099 int purgeableCnt = 0;
1100 SkTDArray<GrGpuResource*> resourcesToUnref;
1101
1102 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1103 // unpurgeable resources.
1104 for (int j = 0; j < kCount; ++j) {
1105 GrUniqueKey key;
1106 make_unique_key<0>(&key, j);
1107
halcanary385fe4d2015-08-26 13:07:48 -07001108 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001109 r->resourcePriv().setUniqueKey(key);
1110 if (random.nextU() % kLockedFreq) {
1111 // Make this is purgeable.
1112 r->unref();
1113 ++purgeableCnt;
1114 if (purgeableCnt <= kNumToPurge) {
1115 *shouldPurgeIdxs.append() = j;
1116 }
1117 } else {
1118 *resourcesToUnref.append() = r;
1119 }
1120 }
1121
1122 // Verify that the correct resources were purged.
1123 int currShouldPurgeIdx = 0;
1124 for (int j = 0; j < kCount; ++j) {
1125 GrUniqueKey key;
1126 make_unique_key<0>(&key, j);
1127 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1128 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1129 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1130 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001131 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001132 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001133 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001134 }
1135 SkSafeUnref(res);
1136 }
1137
1138 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1139 resourcesToUnref[j]->unref();
1140 }
1141 }
1142}
1143
bsalomon3f324322015-04-08 11:01:54 -07001144static void test_flush(skiatest::Reporter* reporter) {
1145 Mock mock(1000000, 1000000);
1146 GrContext* context = mock.context();
1147 GrResourceCache* cache = mock.cache();
1148
1149 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1150 // power of two here to keep things simpler.
1151 static const int kFlushCount = 16;
1152 cache->setLimits(1000000, 1000000, kFlushCount);
1153
1154 {
1155 // Insert a resource and send a flush notification kFlushCount times.
1156 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001157 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001158 GrUniqueKey k;
1159 make_unique_key<1>(&k, i);
1160 r->resourcePriv().setUniqueKey(k);
1161 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001162 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001163 }
1164
1165 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001166 for (int i = 0; i < kFlushCount; ++i) {
1167 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001168 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1169 for (int j = 0; j < i; ++j) {
1170 GrUniqueKey k;
1171 make_unique_key<1>(&k, j);
1172 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1173 REPORTER_ASSERT(reporter, !SkToBool(r));
1174 SkSafeUnref(r);
1175 }
bsalomon3f324322015-04-08 11:01:54 -07001176 }
1177
1178 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1179 cache->purgeAllUnlocked();
1180 }
1181
1182 // Do a similar test but where we leave refs on some resources to prevent them from being
1183 // purged.
1184 {
1185 GrGpuResource* refedResources[kFlushCount >> 1];
1186 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001187 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001188 GrUniqueKey k;
1189 make_unique_key<1>(&k, i);
1190 r->resourcePriv().setUniqueKey(k);
1191 // Leave a ref on every other resource, beginning with the first.
1192 if (SkToBool(i & 0x1)) {
1193 refedResources[i/2] = r;
1194 } else {
1195 r->unref();
1196 }
bsalomonb77a9072016-09-07 10:02:04 -07001197 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001198 }
1199
1200 for (int i = 0; i < kFlushCount; ++i) {
1201 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001202 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001203 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001204 }
1205
1206 // Unref all the resources that we kept refs on in the first loop.
1207 for (int i = 0; i < kFlushCount >> 1; ++i) {
1208 refedResources[i]->unref();
1209 }
1210
bsalomone2e87f32016-09-22 12:42:11 -07001211 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1212 // kFlushCount full flushes.
1213 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001214 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001215 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001216 }
1217 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1218
1219 cache->purgeAllUnlocked();
1220 }
1221
1222 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001223
1224 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1225 // eviction.
1226 context->flush();
1227 for (int i = 0; i < 10; ++i) {
1228 TestResource* r = new TestResource(context->getGpu());
1229 GrUniqueKey k;
1230 make_unique_key<1>(&k, i);
1231 r->resourcePriv().setUniqueKey(k);
1232 r->unref();
1233 }
1234 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1235 for (int i = 0; i < 10 * kFlushCount; ++i) {
1236 context->flush();
1237 }
1238 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001239}
1240
Brian Salomon5e150852017-03-22 14:53:13 -04001241static void test_time_purge(skiatest::Reporter* reporter) {
1242 Mock mock(1000000, 1000000);
1243 GrContext* context = mock.context();
1244 GrResourceCache* cache = mock.cache();
1245
1246 static constexpr int kCnts[] = {1, 10, 1024};
1247 auto nowish = []() {
1248 // We sleep so that we ensure we get a value that is greater than the last call to
1249 // GrStdSteadyClock::now().
1250 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1251 auto result = GrStdSteadyClock::now();
1252 // Also sleep afterwards so we don't get this value again.
1253 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1254 return result;
1255 };
1256
1257 for (int cnt : kCnts) {
1258 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1259 new GrStdSteadyClock::time_point[cnt]);
1260 {
1261 // Insert resources and get time points between each addition.
1262 for (int i = 0; i < cnt; ++i) {
1263 TestResource* r = new TestResource(context->getGpu());
1264 GrUniqueKey k;
1265 make_unique_key<1>(&k, i);
1266 r->resourcePriv().setUniqueKey(k);
1267 r->unref();
1268 timeStamps.get()[i] = nowish();
1269 }
1270
1271 // Purge based on the time points between resource additions. Each purge should remove
1272 // the oldest resource.
1273 for (int i = 0; i < cnt; ++i) {
1274 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1275 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1276 for (int j = 0; j < i; ++j) {
1277 GrUniqueKey k;
1278 make_unique_key<1>(&k, j);
1279 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1280 REPORTER_ASSERT(reporter, !SkToBool(r));
1281 SkSafeUnref(r);
1282 }
1283 }
1284
1285 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1286 cache->purgeAllUnlocked();
1287 }
1288
1289 // Do a similar test but where we leave refs on some resources to prevent them from being
1290 // purged.
1291 {
1292 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1293 for (int i = 0; i < cnt; ++i) {
1294 TestResource* r = new TestResource(context->getGpu());
1295 GrUniqueKey k;
1296 make_unique_key<1>(&k, i);
1297 r->resourcePriv().setUniqueKey(k);
1298 // Leave a ref on every other resource, beginning with the first.
1299 if (SkToBool(i & 0x1)) {
1300 refedResources.get()[i / 2] = r;
1301 } else {
1302 r->unref();
1303 }
1304 timeStamps.get()[i] = nowish();
1305 }
1306
1307 for (int i = 0; i < cnt; ++i) {
1308 // Should get a resource purged every other frame.
1309 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1310 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1311 }
1312
1313 // Unref all the resources that we kept refs on in the first loop.
1314 for (int i = 0; i < (cnt / 2); ++i) {
1315 refedResources.get()[i]->unref();
1316 cache->purgeResourcesNotUsedSince(nowish());
1317 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1318 }
1319
1320 cache->purgeAllUnlocked();
1321 }
1322
1323 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1324
1325 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1326 // eviction
1327 context->flush();
1328 for (int i = 0; i < 10; ++i) {
1329 TestResource* r = new TestResource(context->getGpu());
1330 GrUniqueKey k;
1331 make_unique_key<1>(&k, i);
1332 r->resourcePriv().setUniqueKey(k);
1333 r->unref();
1334 }
1335 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1336 context->flush();
1337 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1338 cache->purgeResourcesNotUsedSince(nowish());
1339 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1340 }
1341}
1342
bsalomon10e23ca2014-11-25 05:52:06 -08001343static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001344 // Set the cache size to double the resource count because we're going to create 2x that number
1345 // resources, using two different key domains. Add a little slop to the bytes because we resize
1346 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001347 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001348
bsalomonc2f35b72015-01-23 07:19:22 -08001349 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1350 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001351 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001352
1353 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001354 GrUniqueKey key1, key2;
1355 make_unique_key<1>(&key1, i);
1356 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001357
bsalomon24db3b12015-01-23 04:24:04 -08001358 TestResource* resource;
1359
halcanary385fe4d2015-08-26 13:07:48 -07001360 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001361 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001362 resource->setSize(1);
1363 resource->unref();
1364
halcanary385fe4d2015-08-26 13:07:48 -07001365 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001366 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001367 resource->setSize(1);
1368 resource->unref();
1369 }
1370
1371 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001372 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1373 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1374 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1375 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001376 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001377 GrUniqueKey key1, key2;
1378 make_unique_key<1>(&key1, i);
1379 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001380
bsalomon8718aaf2015-02-19 07:24:21 -08001381 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1382 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001383 }
1384
bsalomon0ea80f42015-02-11 10:49:59 -08001385 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001386 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001387 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1388 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1389 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1390 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001391
1392 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001393 GrUniqueKey key1, key2;
1394 make_unique_key<1>(&key1, i);
1395 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001396
bsalomon8718aaf2015-02-19 07:24:21 -08001397 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1398 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001399 }
1400}
1401
senorblanco84cd6212015-08-04 10:01:58 -07001402static void test_custom_data(skiatest::Reporter* reporter) {
1403 GrUniqueKey key1, key2;
1404 make_unique_key<0>(&key1, 1);
1405 make_unique_key<0>(&key2, 2);
1406 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001407 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001408 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1409 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1410
1411 // Test that copying a key also takes a ref on its custom data.
1412 GrUniqueKey key3 = key1;
1413 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1414}
1415
bsalomonc6363ef2015-09-24 07:07:40 -07001416static void test_abandoned(skiatest::Reporter* reporter) {
1417 Mock mock(10, 300);
1418 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001419 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001420 context->abandonContext();
1421
1422 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1423
1424 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1425
robertphillips8abb3702016-08-31 14:04:06 -07001426 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001427 resource->getUniqueKey();
1428 resource->wasDestroyed();
1429 resource->gpuMemorySize();
1430 resource->getContext();
1431
1432 resource->abandon();
1433 resource->resourcePriv().getScratchKey();
1434 resource->resourcePriv().isBudgeted();
1435 resource->resourcePriv().makeBudgeted();
1436 resource->resourcePriv().makeUnbudgeted();
1437 resource->resourcePriv().removeScratchKey();
1438 GrUniqueKey key;
1439 make_unique_key<0>(&key, 1);
1440 resource->resourcePriv().setUniqueKey(key);
1441 resource->resourcePriv().removeUniqueKey();
1442}
1443
Brian Salomon1090da62017-01-06 12:04:19 -05001444static void test_tags(skiatest::Reporter* reporter) {
1445#ifdef SK_DEBUG
1446 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1447 static constexpr int kLastTagIdx = 10;
1448 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1449
1450 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1451 GrContext* context = mock.context();
1452 GrResourceCache* cache = mock.cache();
1453
1454 SkString tagStr;
1455 int tagIdx = 0;
1456 int currTagCnt = 0;
1457
1458 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1459 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1460 GrUniqueKey key;
1461 if (currTagCnt == tagIdx) {
1462 tagIdx += 1;
1463 currTagCnt = 0;
1464 tagStr.printf("tag%d", tagIdx);
1465 }
1466 make_unique_key<1>(&key, i, tagStr.c_str());
1467 resource->resourcePriv().setUniqueKey(key);
1468 }
1469 SkASSERT(kLastTagIdx == tagIdx);
1470 SkASSERT(currTagCnt == kLastTagIdx);
1471
1472 // Test i = 0 to exercise unused tag string.
1473 for (int i = 0; i <= kLastTagIdx; ++i) {
1474 tagStr.printf("tag%d", i);
1475 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1476 }
1477#endif
1478}
1479
kkinnunen15302832015-12-01 04:35:26 -08001480DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001481 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001482 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001483 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001484 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001485 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001486 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001487 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001488 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001489 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001490 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001491 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001492 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001493 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001494 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001495 test_time_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001496 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001497 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001498 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001499 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001500}
1501
Robert Phillipsd6214d42016-11-07 08:23:48 -05001502////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001503static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001504 GrSurfaceFlags flags,
1505 int width, int height,
1506 int sampleCnt) {
1507 GrSurfaceDesc desc;
1508 desc.fFlags = flags;
1509 desc.fWidth = width;
1510 desc.fHeight = height;
1511 desc.fConfig = kRGBA_8888_GrPixelConfig;
1512 desc.fSampleCnt = sampleCnt;
1513
Robert Phillipse78b7252017-04-06 07:59:41 -04001514 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001515}
1516
Robert Phillipse78b7252017-04-06 07:59:41 -04001517static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1518 GrSurfaceFlags flags,
1519 int width, int height,
1520 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001521 SkBitmap bm;
1522
1523 bm.allocN32Pixels(width, height, true);
1524 bm.eraseColor(SK_ColorBLUE);
1525
Brian Osman7b8400d2016-11-08 17:08:54 -05001526 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001527 SkASSERT(mipmaps);
1528 SkASSERT(mipmaps->countLevels() > 1);
1529
1530 int mipLevelCount = mipmaps->countLevels() + 1;
1531
1532 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1533
1534 texels[0].fPixels = bm.getPixels();
1535 texels[0].fRowBytes = bm.rowBytes();
1536
1537 for (int i = 1; i < mipLevelCount; ++i) {
1538 SkMipMap::Level generatedMipLevel;
1539 mipmaps->getLevel(i - 1, &generatedMipLevel);
1540 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1541 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1542 }
1543
1544 GrSurfaceDesc desc;
1545 desc.fFlags = flags;
1546 desc.fWidth = width;
1547 desc.fHeight = height;
1548 desc.fConfig = kRGBA_8888_GrPixelConfig;
1549 desc.fSampleCnt = sampleCnt;
1550 desc.fIsMipMapped = true;
1551
Robert Phillipse78b7252017-04-06 07:59:41 -04001552 return provider->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001553}
1554
1555// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1556// Texture-only, both-RT-and-Texture and MIPmapped
1557DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1558 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001559 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001560
Robert Phillipsd6214d42016-11-07 08:23:48 -05001561 static const int kSize = 64;
1562
Robert Phillipsd6214d42016-11-07 08:23:48 -05001563 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001564 {
1565 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001566
Robert Phillipse78b7252017-04-06 07:59:41 -04001567 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1568 size_t size = tex->gpuMemorySize();
1569 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1570
1571 if (context->caps()->maxSampleCount() >= 4) {
1572 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1573 size = tex->gpuMemorySize();
1574 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1575 kSize*kSize*4*4 == size || // auto-resolving
1576 kSize*kSize*4*5 == size); // explicit resolve buffer
1577 }
1578
1579 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001580 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001581 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001582 }
1583
Robert Phillipsd6214d42016-11-07 08:23:48 -05001584
1585 // Mipmapped versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001586 {
1587 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001588
Robert Phillipse78b7252017-04-06 07:59:41 -04001589 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1590 size_t size = proxy->gpuMemorySize();
1591 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1592
1593 if (context->caps()->maxSampleCount() >= 4) {
1594 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1595 size = proxy->gpuMemorySize();
1596 REPORTER_ASSERT(reporter,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001597 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1598 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1599 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001600 }
Robert Phillips1b352562017-04-05 18:56:21 +00001601
Robert Phillipse78b7252017-04-06 07:59:41 -04001602 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1603 size = proxy->gpuMemorySize();
1604 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1605 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001606}
1607
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001608#endif