blob: c56bd0156419555310eb765d373d21f6f8489f49 [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"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040020#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"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
25
bsalomonbcf0a522014-10-08 08:40:09 -070026#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027#include "SkGr.h"
28#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050029#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070030#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000031#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032
33static const int gWidth = 640;
34static const int gHeight = 480;
35
36////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070038 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080039 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040040 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080041 desc.fFlags = kRenderTarget_GrSurfaceFlag;
42 desc.fWidth = gWidth;
43 desc.fHeight = gHeight;
44 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070045 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080046 SkCanvas* canvas = surface->getCanvas();
47
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
49
50 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000051 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000052 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040053 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000055 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070056 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 int oldMaxNum;
59 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000060 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000061
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062 // Set the cache limits so we can fit 10 "src" images and the
63 // max number of textures doesn't matter
64 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000065 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000068 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000069
70 for (int i = 0; i < 100; ++i) {
71 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040072 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000073
74 // "modify" the src texture
75 src.notifyPixelsChanged();
76
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000077 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070078 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000079
80 // we should never go over the size limit
81 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
82 }
83
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000084 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000085}
86
bsalomon11abd8d2016-10-14 08:13:48 -070087static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
88 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
89 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
90 return false;
91 }
92 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
93}
94
Robert Phillipsc0192e32017-09-21 12:00:26 -040095static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
96 return rt->renderTargetPriv().getStencilAttachment();
97}
98
99static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
100 int size, int sampleCount, SkBudgeted budgeted) {
101 GrSurfaceDesc desc;
102 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc0192e32017-09-21 12:00:26 -0400103 desc.fWidth = size;
104 desc.fHeight = size;
105 desc.fConfig = kRGBA_8888_GrPixelConfig;
106 desc.fSampleCnt = sampleCount;
107
108 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
109 if (!tex || !tex->asRenderTarget()) {
110 return nullptr;
111 }
112
113 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
114 return nullptr;
115 }
116 SkASSERT(get_SB(tex->asRenderTarget()));
117
118 return sk_ref_sp(tex->asRenderTarget());
119}
120
bsalomon11abd8d2016-10-14 08:13:48 -0700121// This currently fails on ES3 ANGLE contexts
122DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000123 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700124 GrContext* context = ctxInfo.grContext();
Eric Karl5c779752017-05-08 12:02:07 -0700125 if (context->caps()->avoidStencilBuffers()) {
126 return;
127 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128
Robert Phillips6be756b2018-01-16 15:07:54 -0500129 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400130
Brian Salomonbdecacf2018-02-02 20:32:49 -0500131 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400132 REPORTER_ASSERT(reporter, smallRT0);
133
134 {
135 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
137 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400138
Brian Salomonbdecacf2018-02-02 20:32:49 -0500139 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 }
141
Robert Phillipsc0192e32017-09-21 12:00:26 -0400142 {
143 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400145 REPORTER_ASSERT(reporter, smallRT2);
146
147 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800148 }
149
Robert Phillipsc0192e32017-09-21 12:00:26 -0400150 {
151 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500152 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400153 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800154
Robert Phillipsc0192e32017-09-21 12:00:26 -0400155 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 }
bsalomon02a44a42015-02-19 09:09:00 -0800157
Brian Salomonbdecacf2018-02-02 20:32:49 -0500158 int smallSampleCount = context->caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
159 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700160 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500161 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
162 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800163#ifdef SK_BUILD_FOR_ANDROID
164 if (!smallMSAART0) {
165 // The nexus player seems to fail to create MSAA textures.
166 return;
167 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168#else
169 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800170#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400171
172 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
173
174 {
175 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500176 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
177 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400178 SkBudgeted::kNo);
179 REPORTER_ASSERT(reporter, smallMSAART1);
180
181 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800182 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400183
Brian Salomonbdecacf2018-02-02 20:32:49 -0500184 // But one with a larger sample count should not. (Also check that the two requests didn't
185 // rounded up to the same actual sample count or else they could share.).
186 int bigSampleCount =
187 context->caps()->getRenderTargetSampleCount(5, kRGBA_8888_GrPixelConfig);
188 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
190 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400191 SkBudgeted::kNo);
192 REPORTER_ASSERT(reporter, smallMSAART2);
193
194 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800195 }
196 }
197}
198
bsalomon68d91342016-04-12 09:59:58 -0700199DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700200 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500201 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500202 GrGpu* gpu = context->contextPriv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700203 // this test is only valid for GL
204 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205 return;
206 }
207
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500208 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700209 static const int kW = 100;
210 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700211
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500212 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
213 kRGBA_8888_GrPixelConfig,
214 false, GrMipMapped::kNo);
215 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
216 kRGBA_8888_GrPixelConfig,
217 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500218 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
219 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
220 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
221 return;
222 }
jvanverth672bb7f2015-07-13 07:19:57 -0700223
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224 context->resetContext();
225
Robert Phillips6be756b2018-01-16 15:07:54 -0500226 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500227 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
Robert Phillips6be756b2018-01-16 15:07:54 -0500229 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500230 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
Brian Osman85d34b22017-05-10 12:06:26 -0400232 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
233 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700234 return;
235 }
236
halcanary96fcdcc2015-08-27 07:41:13 -0700237 borrowed.reset(nullptr);
238 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700239
240 context->flush();
241
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500242 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
243 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244
245 REPORTER_ASSERT(reporter, borrowedIsAlive);
246 REPORTER_ASSERT(reporter, !adoptedIsAlive);
247
Brian Salomone64b0642018-03-07 11:47:54 -0500248 if (borrowedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500249 gpu->deleteTestingOnlyBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500250 }
251 if (adoptedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500252 gpu->deleteTestingOnlyBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500253 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700254
255 context->resetContext();
256}
257
bsalomon6d3fe022014-07-25 08:35:45 -0700258class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800259 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000260public:
robertphillips6e83ac72015-08-13 05:19:14 -0700261 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700262
bsalomon1c60dfe2015-01-21 09:32:40 -0800263 /** Property that distinctly categorizes the resource.
264 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800265 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800266
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
268 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700269 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800270 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700271 , fProperty(kA_SimulatedProperty)
272 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800273 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700274 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800275 }
276
kkinnunen2e6055b2016-04-22 01:48:29 -0700277 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
278 SimulatedProperty property) {
279 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800280 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700281 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
282 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283 }
284
Brian Salomond3b65972017-03-22 12:05:03 -0400285 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800286 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800287 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000288 }
289
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000290 void setSize(size_t size) {
291 fSize = size;
292 this->didChangeGpuMemorySize();
293 }
294
bsalomon33435572014-11-05 14:47:41 -0800295 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000296
bsalomon71cb0c22014-11-14 12:10:14 -0800297 void setUnrefWhenDestroyed(TestResource* resource) {
298 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000299 }
300
bsalomon1c60dfe2015-01-21 09:32:40 -0800301 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
302 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
303 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800304 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
305 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800306 }
307 }
308
309 static size_t ExpectedScratchKeySize() {
310 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
311 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000312private:
bsalomon24db3b12015-01-23 04:24:04 -0800313 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800314
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
316 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700317 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800318 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700319 , fProperty(property)
320 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800321 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700322 this->registerWithCache(budgeted);
323 }
324
325 // Constructor for simulating resources that wrap backend objects.
326 TestResource(GrGpu* gpu, size_t size)
327 : INHERITED(gpu)
328 , fToDelete(nullptr)
329 , fSize(size)
330 , fProperty(kA_SimulatedProperty)
331 , fIsScratch(false) {
332 ++fNumAlive;
333 this->registerWithCacheWrapped();
334 }
335
336 void computeScratchKey(GrScratchKey* key) const override {
337 if (fIsScratch) {
338 ComputeScratchKey(fProperty, key);
339 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800340 }
341
mtklein36352bf2015-03-25 18:17:31 -0700342 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800343
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000344 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000345 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800346 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800347 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700348 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700349 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000350};
bsalomon33435572014-11-05 14:47:41 -0800351int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000352
bsalomonc2f35b72015-01-23 07:19:22 -0800353class Mock {
354public:
355 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400356 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800357 SkASSERT(fContext);
358 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500359 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800360 cache->purgeAllUnlocked();
361 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800362 }
bsalomonc2f35b72015-01-23 07:19:22 -0800363
Robert Phillips6be756b2018-01-16 15:07:54 -0500364 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800365
Hal Canary342b7ac2016-11-04 11:49:42 -0400366 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800367
368private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400369 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800370};
371
372static void test_no_key(skiatest::Reporter* reporter) {
373 Mock mock(10, 30000);
374 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800375 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500376 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800377
378 // Create a bunch of resources with no keys
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500379 TestResource* a = new TestResource(gpu);
380 TestResource* b = new TestResource(gpu);
381 TestResource* c = new TestResource(gpu);
382 TestResource* d = new TestResource(gpu);
bsalomon71cb0c22014-11-14 12:10:14 -0800383 a->setSize(11);
384 b->setSize(12);
385 c->setSize(13);
386 d->setSize(14);
387
388 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800389 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800391 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800392
393 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800394 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800395 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
396
bsalomon8718aaf2015-02-19 07:24:21 -0800397 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800398
399 a->unref();
400 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800401 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800402 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800403 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404
405 c->unref();
406 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800407 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800408 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800409 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 d->unref();
412 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800413 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
414 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415
416 b->unref();
417 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800418 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
419 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800420}
421
bsalomon24db3b12015-01-23 04:24:04 -0800422// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500423template <int>
424static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800425 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500426 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800427 builder[0] = data;
428}
429
Robert Phillips6eba0632018-03-28 12:25:42 -0400430static void test_purge_unlocked(skiatest::Reporter* reporter) {
431 Mock mock(10, 30000);
432 GrContext* context = mock.context();
433 GrResourceCache* cache = mock.cache();
434 GrGpu* gpu = context->contextPriv().getGpu();
435
436 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
437 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
438 TestResource::kA_SimulatedProperty);
439 a->setSize(11);
440
441 GrUniqueKey uniqueKey;
442 make_unique_key<0>(&uniqueKey, 0);
443
444 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
445 TestResource::kA_SimulatedProperty);
446 b->setSize(12);
447 b->resourcePriv().setUniqueKey(uniqueKey);
448
449 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
450 TestResource::kA_SimulatedProperty);
451 c->setSize(13);
452
453 GrUniqueKey uniqueKey2;
454 make_unique_key<0>(&uniqueKey2, 1);
455
456 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
457 TestResource::kA_SimulatedProperty);
458 d->setSize(14);
459 d->resourcePriv().setUniqueKey(uniqueKey2);
460
461
462 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
463 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
464 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
465 d->gpuMemorySize() == cache->getResourceBytes());
466
467 // Should be safe to purge without deleting the resources since we still have refs.
468 cache->purgeUnlockedResources(false);
469 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
470
471 // Unref them all. Since they all have keys they should remain in the cache.
472
473 a->unref();
474 b->unref();
475 c->unref();
476 d->unref();
477 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
478 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
479 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
480 d->gpuMemorySize() == cache->getResourceBytes());
481
482 // Purge only the two scratch resources
483 cache->purgeUnlockedResources(true);
484
485 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
486 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
487 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
488 cache->getResourceBytes());
489
490 // Purge the uniquely keyed resources
491 cache->purgeUnlockedResources(false);
492
493 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
494 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
495 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
496}
497
bsalomon84c8e622014-11-17 09:33:27 -0800498static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800499 Mock mock(10, 300);
500 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800501 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500502 GrGpu* gpu = context->contextPriv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800503
bsalomon8718aaf2015-02-19 07:24:21 -0800504 GrUniqueKey uniqueKey;
505 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800506
bsalomon8718aaf2015-02-19 07:24:21 -0800507 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800508 TestResource* scratch =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500509 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800510 scratch->setSize(10);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500511 TestResource* unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800512 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800513 unique->resourcePriv().setUniqueKey(uniqueKey);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500514 TestResource* wrapped = TestResource::CreateWrapped(gpu);
bsalomon5236cf42015-01-14 10:42:08 -0800515 wrapped->setSize(12);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500516 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800517 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800518
Brian Osman0562eb92017-05-08 11:16:39 -0400519 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800520 GrUniqueKey uniqueKey2;
521 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800522 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400523 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
524 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
525
526 // Remove the extra ref we just added.
527 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800528
529 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800530 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800531 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800532 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800533 cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800535 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800536 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400537 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800538
bsalomon63c992f2015-01-23 12:47:59 -0800539 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800540 cache->purgeAllUnlocked();
541 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800542 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800543 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800544 cache->getResourceBytes());
545 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800546 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800547 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400548 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800549
550 // Unreffing the wrapped resource should free it right away.
551 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800552 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800553 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800554 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400555 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800556
bsalomon84c8e622014-11-17 09:33:27 -0800557 // Now try freeing the budgeted resources first
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500558 wrapped = TestResource::CreateWrapped(gpu);
bsalomondace19e2014-11-17 07:34:06 -0800559 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800560 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400561 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800562 cache->purgeAllUnlocked();
563 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800564 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800565 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
566 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
567 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400568 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800569
570 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400571 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800572 cache->purgeAllUnlocked();
573 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800574 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800575 cache->getResourceBytes());
576 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
577 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400578 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800579
580 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800581 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
582 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
583 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
584 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400585 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800586
587 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
589 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
590 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400592 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800593}
594
bsalomon5236cf42015-01-14 10:42:08 -0800595static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800596 Mock mock(10, 30000);
597 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500599 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800600
bsalomon8718aaf2015-02-19 07:24:21 -0800601 GrUniqueKey uniqueKey;
602 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800603
604 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800605 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800606 TestResource* wrapped;
607 TestResource* unbudgeted;
608
609 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500610 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
kkinnunen2e6055b2016-04-22 01:48:29 -0700611 TestResource::kB_SimulatedProperty);
612
bsalomon5236cf42015-01-14 10:42:08 -0800613 scratch->setSize(10);
614 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800615 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
616 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
617 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
618 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400619 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800620
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500621 unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800622 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800623 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800624 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800625 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
626 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
627 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
628 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400629 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800630
bsalomon0ea80f42015-02-11 10:49:59 -0800631 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500632 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800633 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
634 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
635 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
636 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400637 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800638
639 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800640 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
641 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
642 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
643 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400644 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800645
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500646 wrapped = TestResource::CreateWrapped(gpu, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800647 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
648 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
649 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
650 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400651 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800652
653 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800654 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
655 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
656 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
657 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400658 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800659
bsalomon0ea80f42015-02-11 10:49:59 -0800660 cache->purgeAllUnlocked();
661 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
662 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
663 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
664 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400665 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800666}
667
bsalomon3582d3e2015-02-13 14:20:05 -0800668// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
669void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
670/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800671 Mock mock(10, 300);
672 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800673 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500674 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800675
676 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500677 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800678 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800679 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800680
681 size_t size = resource->gpuMemorySize();
682 for (int i = 0; i < 2; ++i) {
683 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800684 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800685 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800686 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700687 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800688 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
689 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
690 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
691 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400692 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800693
694 // Once it is unrefed, it should become available as scratch.
695 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800696 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
697 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
698 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
699 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400700 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700701 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800702 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800703 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800704 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800705 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800706
707 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700708 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800709 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800710 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800711 } else {
712 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800713 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800714 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
715 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
716 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
717 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400718 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800719 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800720 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800721 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800722
723 // now when it is unrefed it should die since it has no key.
724 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800725 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
726 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
727 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
728 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400729 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800730 }
bsalomon8b79d232014-11-10 10:19:06 -0800731 }
bsalomonc2f35b72015-01-23 07:19:22 -0800732}
733
734static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
735 Mock mock(5, 30000);
736 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800737 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500738 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800739
bsalomon8b79d232014-11-10 10:19:06 -0800740 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500741 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700742 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800743 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500744 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700745 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800746 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800747 a->setSize(11);
748 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800749 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800750 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800751 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700752 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800753
754 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800755 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800756
bsalomon0ea80f42015-02-11 10:49:59 -0800757 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800758 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800759 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
760 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800761 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800762 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800763
bsalomon63c992f2015-01-23 12:47:59 -0800764 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800765 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800766 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800767 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800768
769 // Unref but don't purge
770 a->unref();
771 b->unref();
772 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800773 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800774
bsalomon63c992f2015-01-23 12:47:59 -0800775 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800776 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800777 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800778 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
779 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800780}
781
bsalomon10e23ca2014-11-25 05:52:06 -0800782static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800783 Mock mock(5, 30000);
784 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800785 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500786 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800787
bsalomon10e23ca2014-11-25 05:52:06 -0800788 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500789 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800790 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500791 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800793 a->unref();
794 b->unref();
795
bsalomon1c60dfe2015-01-21 09:32:40 -0800796 GrScratchKey scratchKey;
797 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800798 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800799 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700800 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800801
bsalomon0ea80f42015-02-11 10:49:59 -0800802 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800803 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800804 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800805 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
806 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800807
808 // Find the first resource and remove its scratch key
809 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700810 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800811 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800812 // It's still alive, but not cached by scratch key anymore
813 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800814 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
815 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800816
817 // The cache should immediately delete it when it's unrefed since it isn't accessible.
818 find->unref();
819 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800820 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
821 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800822
823 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700824 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800825 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800826 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800827 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
828 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800829
830 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800831 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800832 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800833 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
834 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800835
836 find->unref();
837 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800838 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
839 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800840}
841
bsalomon1c60dfe2015-01-21 09:32:40 -0800842static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800843 Mock mock(5, 30000);
844 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800845 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500846 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800847
848 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500849 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800850 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500851 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800852 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800853 a->unref();
854 b->unref();
855
856 GrScratchKey scratchKey;
857 // Ensure that scratch key comparison and assignment is consistent.
858 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800859 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800860 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800861 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800862 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
863 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
864 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
865 scratchKey = scratchKey1;
866 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
867 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
868 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
869 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
870 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
871 scratchKey = scratchKey2;
872 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
873 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
874 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
875 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
876 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
877
878 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800879 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800880 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700881 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800882
883 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800884 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700885 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700886 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800887 find->unref();
888
889 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700890 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700891 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800892 REPORTER_ASSERT(reporter, find == a || find == b);
893
robertphillips6e83ac72015-08-13 05:19:14 -0700894 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700895 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800896 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
897 REPORTER_ASSERT(reporter, find2 != find);
898 find2->unref();
899 find->unref();
900}
901
bsalomon8718aaf2015-02-19 07:24:21 -0800902static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800903 Mock mock(5, 30000);
904 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800905 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500906 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000907
bsalomon8718aaf2015-02-19 07:24:21 -0800908 GrUniqueKey key;
909 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700910
bsalomon8718aaf2015-02-19 07:24:21 -0800911 // Create two resources that we will attempt to register with the same unique key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500912 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -0800913 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700914
bsalomonf99e9612015-02-19 08:24:16 -0800915 // Set key on resource a.
916 a->resourcePriv().setUniqueKey(key);
917 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
918 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800919
bsalomonf99e9612015-02-19 08:24:16 -0800920 // Make sure that redundantly setting a's key works.
921 a->resourcePriv().setUniqueKey(key);
922 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800923 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800924 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
925 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800926 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
927
bsalomonf99e9612015-02-19 08:24:16 -0800928 // Create resource b and set the same key. It should replace a's unique key cache entry.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500929 TestResource* b = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800930 b->setSize(12);
931 b->resourcePriv().setUniqueKey(key);
932 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
933 b->unref();
934
935 // Still have two resources because a is still reffed.
936 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
937 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
938 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
939
940 a->unref();
941 // Now a should be gone.
942 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
943 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
944 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
945
946 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
947 // Also make b be unreffed when replacement occurs.
948 b->unref();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500949 TestResource* c = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800950 GrUniqueKey differentKey;
951 make_unique_key<0>(&differentKey, 1);
952 c->setSize(13);
953 c->resourcePriv().setUniqueKey(differentKey);
954 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
955 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
956 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
957 // c replaces b and b should be immediately purged.
958 c->resourcePriv().setUniqueKey(key);
959 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
960 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
961 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
962
963 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800964 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800965 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
966 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
967 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
968
969 // Drop the ref on c, it should be kept alive because it has a unique key.
970 c->unref();
971 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
972 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
973 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
974
975 // Verify that we can find c, then remove its unique key. It should get purged immediately.
976 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
977 c->resourcePriv().removeUniqueKey();
978 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800979 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
980 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800981 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700982
983 {
984 GrUniqueKey key2;
985 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500986 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700987 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700988 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700989 d->resourcePriv().setUniqueKey(key2);
990 }
991
992 GrUniqueKey key3;
993 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400994 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700995 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000996}
997
bsalomon8b79d232014-11-10 10:19:06 -0800998static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800999 Mock mock(5, 30000);
1000 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001001 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001002 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001003
bsalomon8718aaf2015-02-19 07:24:21 -08001004 GrUniqueKey key1, key2, key3;
1005 make_unique_key<0>(&key1, 1);
1006 make_unique_key<0>(&key2, 2);
1007 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001008
bsalomon23e619c2015-02-06 11:54:28 -08001009 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001010 TestResource* a = new TestResource(gpu);
1011 TestResource* b = new TestResource(gpu);
1012 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001013 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001014 a->resourcePriv().setUniqueKey(key1);
1015 b->resourcePriv().setUniqueKey(key2);
1016 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001017 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001018 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001019 c->unref();
1020
bsalomon8718aaf2015-02-19 07:24:21 -08001021 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1022 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1023 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001024 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001025
bsalomon8718aaf2015-02-19 07:24:21 -08001026 typedef GrUniqueKeyInvalidatedMessage Msg;
1027 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001028
1029 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
1030 Bus::Post(Msg(key1));
1031 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -08001032 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001033 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001034 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1035 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001036 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001037 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001038
1039 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -08001040 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -08001041 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001042 // we still have a ref on b, c should be recycled as scratch.
1043 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001044 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001045
bsalomon23e619c2015-02-06 11:54:28 -08001046 // make b purgeable. It should be immediately deleted since it has no key.
1047 b->unref();
1048 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1049
1050 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1051 GrScratchKey scratchKey;
1052 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -07001053 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -08001054 REPORTER_ASSERT(reporter, scratch == c);
1055 SkSafeUnref(scratch);
1056
1057 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001058 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -07001059 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -08001060 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001061 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1062 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001063 REPORTER_ASSERT(reporter, !scratch);
1064 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001065}
1066
bsalomon71cb0c22014-11-14 12:10:14 -08001067static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001068 Mock mock(3, 30000);
1069 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001070 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001071 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001072
bsalomon8718aaf2015-02-19 07:24:21 -08001073 GrUniqueKey key1, key2;
1074 make_unique_key<0>(&key1, 1);
1075 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001076
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001077 TestResource* a = new TestResource(gpu);
1078 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001079 a->resourcePriv().setUniqueKey(key1);
1080 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001081
bsalomonc2f35b72015-01-23 07:19:22 -08001082 // Make a cycle
1083 a->setUnrefWhenDestroyed(b);
1084 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001085
bsalomonc2f35b72015-01-23 07:19:22 -08001086 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001087
bsalomonc2f35b72015-01-23 07:19:22 -08001088 a->unref();
1089 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001090
bsalomonc2f35b72015-01-23 07:19:22 -08001091 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001092
bsalomon0ea80f42015-02-11 10:49:59 -08001093 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001094 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001095
bsalomonc2f35b72015-01-23 07:19:22 -08001096 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001097 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001098 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001099
bsalomon0ea80f42015-02-11 10:49:59 -08001100 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001101 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001102}
1103
bsalomon8b79d232014-11-10 10:19:06 -08001104static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001105 GrUniqueKey key1, key2;
1106 make_unique_key<0>(&key1, 1);
1107 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001108
1109 // Test changing resources sizes (both increase & decrease).
1110 {
bsalomonc2f35b72015-01-23 07:19:22 -08001111 Mock mock(3, 30000);
1112 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001113 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001114 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001115
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001116 TestResource* a = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001117 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001118 a->unref();
1119
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001120 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001121 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001122 b->unref();
1123
bsalomon0ea80f42015-02-11 10:49:59 -08001124 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1125 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001126 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001127 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001128 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001129 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001130 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001131 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001132 find1->setSize(50);
1133 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001134
bsalomon0ea80f42015-02-11 10:49:59 -08001135 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1136 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001137 }
1138
1139 // Test increasing a resources size beyond the cache budget.
1140 {
bsalomonc2f35b72015-01-23 07:19:22 -08001141 Mock mock(2, 300);
1142 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001143 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001144 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001145
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001146 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001147 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001148 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001149 a->unref();
1150
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001151 TestResource* b = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001152 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001153 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001154 b->unref();
1155
bsalomon0ea80f42015-02-11 10:49:59 -08001156 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1157 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001158
bsalomon8b79d232014-11-10 10:19:06 -08001159 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001160 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001161 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001162 find2->setSize(201);
1163 }
bsalomon8718aaf2015-02-19 07:24:21 -08001164 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001165
bsalomon0ea80f42015-02-11 10:49:59 -08001166 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1167 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001168 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001169}
1170
bsalomonddf30e62015-02-19 11:38:44 -08001171static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1172 static const int kCount = 50;
1173 static const int kBudgetCnt = kCount / 2;
1174 static const int kLockedFreq = 8;
1175 static const int kBudgetSize = 0x80000000;
1176
1177 SkRandom random;
1178
1179 // Run the test 2*kCount times;
1180 for (int i = 0; i < 2 * kCount; ++i ) {
1181 Mock mock(kBudgetCnt, kBudgetSize);
1182 GrContext* context = mock.context();
1183 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001184 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001185
1186 // Pick a random number of resources to add before the timestamp will wrap.
1187 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1188
1189 static const int kNumToPurge = kCount - kBudgetCnt;
1190
1191 SkTDArray<int> shouldPurgeIdxs;
1192 int purgeableCnt = 0;
1193 SkTDArray<GrGpuResource*> resourcesToUnref;
1194
1195 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1196 // unpurgeable resources.
1197 for (int j = 0; j < kCount; ++j) {
1198 GrUniqueKey key;
1199 make_unique_key<0>(&key, j);
1200
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001201 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001202 r->resourcePriv().setUniqueKey(key);
1203 if (random.nextU() % kLockedFreq) {
1204 // Make this is purgeable.
1205 r->unref();
1206 ++purgeableCnt;
1207 if (purgeableCnt <= kNumToPurge) {
1208 *shouldPurgeIdxs.append() = j;
1209 }
1210 } else {
1211 *resourcesToUnref.append() = r;
1212 }
1213 }
1214
1215 // Verify that the correct resources were purged.
1216 int currShouldPurgeIdx = 0;
1217 for (int j = 0; j < kCount; ++j) {
1218 GrUniqueKey key;
1219 make_unique_key<0>(&key, j);
1220 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1221 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1222 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1223 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001224 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001225 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001226 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001227 }
1228 SkSafeUnref(res);
1229 }
1230
1231 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1232 resourcesToUnref[j]->unref();
1233 }
1234 }
1235}
1236
bsalomon3f324322015-04-08 11:01:54 -07001237static void test_flush(skiatest::Reporter* reporter) {
1238 Mock mock(1000000, 1000000);
1239 GrContext* context = mock.context();
1240 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001241 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon3f324322015-04-08 11:01:54 -07001242
1243 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1244 // power of two here to keep things simpler.
1245 static const int kFlushCount = 16;
1246 cache->setLimits(1000000, 1000000, kFlushCount);
1247
1248 {
1249 // Insert a resource and send a flush notification kFlushCount times.
1250 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001251 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001252 GrUniqueKey k;
1253 make_unique_key<1>(&k, i);
1254 r->resourcePriv().setUniqueKey(k);
1255 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001256 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001257 }
1258
1259 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001260 for (int i = 0; i < kFlushCount; ++i) {
1261 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001262 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1263 for (int j = 0; j < i; ++j) {
1264 GrUniqueKey k;
1265 make_unique_key<1>(&k, j);
1266 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1267 REPORTER_ASSERT(reporter, !SkToBool(r));
1268 SkSafeUnref(r);
1269 }
bsalomon3f324322015-04-08 11:01:54 -07001270 }
1271
1272 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1273 cache->purgeAllUnlocked();
1274 }
1275
1276 // Do a similar test but where we leave refs on some resources to prevent them from being
1277 // purged.
1278 {
1279 GrGpuResource* refedResources[kFlushCount >> 1];
1280 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001281 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001282 GrUniqueKey k;
1283 make_unique_key<1>(&k, i);
1284 r->resourcePriv().setUniqueKey(k);
1285 // Leave a ref on every other resource, beginning with the first.
1286 if (SkToBool(i & 0x1)) {
1287 refedResources[i/2] = r;
1288 } else {
1289 r->unref();
1290 }
bsalomonb77a9072016-09-07 10:02:04 -07001291 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001292 }
1293
1294 for (int i = 0; i < kFlushCount; ++i) {
1295 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001296 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001297 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001298 }
1299
1300 // Unref all the resources that we kept refs on in the first loop.
1301 for (int i = 0; i < kFlushCount >> 1; ++i) {
1302 refedResources[i]->unref();
1303 }
1304
bsalomone2e87f32016-09-22 12:42:11 -07001305 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1306 // kFlushCount full flushes.
1307 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001308 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001309 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001310 }
1311 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1312
1313 cache->purgeAllUnlocked();
1314 }
1315
1316 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001317
1318 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1319 // eviction.
1320 context->flush();
1321 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001322 TestResource* r = new TestResource(gpu);
bsalomondc438982016-08-31 11:53:49 -07001323 GrUniqueKey k;
1324 make_unique_key<1>(&k, i);
1325 r->resourcePriv().setUniqueKey(k);
1326 r->unref();
1327 }
1328 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1329 for (int i = 0; i < 10 * kFlushCount; ++i) {
1330 context->flush();
1331 }
1332 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001333}
1334
Brian Salomon5e150852017-03-22 14:53:13 -04001335static void test_time_purge(skiatest::Reporter* reporter) {
1336 Mock mock(1000000, 1000000);
1337 GrContext* context = mock.context();
1338 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001339 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001340
1341 static constexpr int kCnts[] = {1, 10, 1024};
1342 auto nowish = []() {
1343 // We sleep so that we ensure we get a value that is greater than the last call to
1344 // GrStdSteadyClock::now().
1345 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1346 auto result = GrStdSteadyClock::now();
1347 // Also sleep afterwards so we don't get this value again.
1348 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1349 return result;
1350 };
1351
1352 for (int cnt : kCnts) {
1353 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1354 new GrStdSteadyClock::time_point[cnt]);
1355 {
1356 // Insert resources and get time points between each addition.
1357 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001358 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001359 GrUniqueKey k;
1360 make_unique_key<1>(&k, i);
1361 r->resourcePriv().setUniqueKey(k);
1362 r->unref();
1363 timeStamps.get()[i] = nowish();
1364 }
1365
1366 // Purge based on the time points between resource additions. Each purge should remove
1367 // the oldest resource.
1368 for (int i = 0; i < cnt; ++i) {
1369 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1370 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1371 for (int j = 0; j < i; ++j) {
1372 GrUniqueKey k;
1373 make_unique_key<1>(&k, j);
1374 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1375 REPORTER_ASSERT(reporter, !SkToBool(r));
1376 SkSafeUnref(r);
1377 }
1378 }
1379
1380 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1381 cache->purgeAllUnlocked();
1382 }
1383
1384 // Do a similar test but where we leave refs on some resources to prevent them from being
1385 // purged.
1386 {
1387 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1388 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001389 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001390 GrUniqueKey k;
1391 make_unique_key<1>(&k, i);
1392 r->resourcePriv().setUniqueKey(k);
1393 // Leave a ref on every other resource, beginning with the first.
1394 if (SkToBool(i & 0x1)) {
1395 refedResources.get()[i / 2] = r;
1396 } else {
1397 r->unref();
1398 }
1399 timeStamps.get()[i] = nowish();
1400 }
1401
1402 for (int i = 0; i < cnt; ++i) {
1403 // Should get a resource purged every other frame.
1404 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1405 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1406 }
1407
1408 // Unref all the resources that we kept refs on in the first loop.
1409 for (int i = 0; i < (cnt / 2); ++i) {
1410 refedResources.get()[i]->unref();
1411 cache->purgeResourcesNotUsedSince(nowish());
1412 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1413 }
1414
1415 cache->purgeAllUnlocked();
1416 }
1417
1418 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1419
1420 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1421 // eviction
1422 context->flush();
1423 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001424 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001425 GrUniqueKey k;
1426 make_unique_key<1>(&k, i);
1427 r->resourcePriv().setUniqueKey(k);
1428 r->unref();
1429 }
1430 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1431 context->flush();
1432 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1433 cache->purgeResourcesNotUsedSince(nowish());
1434 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1435 }
1436}
1437
Derek Sollenberger5480a182017-05-25 16:43:59 -04001438static void test_partial_purge(skiatest::Reporter* reporter) {
1439 Mock mock(6, 100);
1440 GrContext* context = mock.context();
1441 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001442 GrGpu* gpu = context->contextPriv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001443
1444 enum TestsCase {
1445 kOnlyScratch_TestCase = 0,
1446 kPartialScratch_TestCase = 1,
1447 kAllScratch_TestCase = 2,
1448 kPartial_TestCase = 3,
1449 kAll_TestCase = 4,
1450 kNone_TestCase = 5,
1451 kEndTests_TestCase = kNone_TestCase + 1
1452 };
1453
1454 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1455
1456 GrUniqueKey key1, key2, key3;
1457 make_unique_key<0>(&key1, 1);
1458 make_unique_key<0>(&key2, 2);
1459 make_unique_key<0>(&key3, 3);
1460
1461 // Add three unique resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001462 TestResource *unique1 = new TestResource(gpu);
1463 TestResource *unique2 = new TestResource(gpu);
1464 TestResource *unique3 = new TestResource(gpu);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001465
1466 unique1->resourcePriv().setUniqueKey(key1);
1467 unique2->resourcePriv().setUniqueKey(key2);
1468 unique3->resourcePriv().setUniqueKey(key3);
1469
1470 unique1->setSize(10);
1471 unique2->setSize(11);
1472 unique3->setSize(12);
1473
1474 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001475 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001476 TestResource::kA_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001477 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001478 TestResource::kB_SimulatedProperty);
1479 scratch1->setSize(13);
1480 scratch2->setSize(14);
1481
1482
1483 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1484 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1485 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1486
1487 // Add resources to the purgeable queue
1488 unique1->unref();
1489 scratch1->unref();
1490 unique2->unref();
1491 scratch2->unref();
1492 unique3->unref();
1493
1494 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1495 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1496 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1497
1498 switch(testCase) {
1499 case kOnlyScratch_TestCase: {
1500 context->purgeUnlockedResources(14, true);
1501 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1502 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1503 break;
1504 }
1505 case kPartialScratch_TestCase: {
1506 context->purgeUnlockedResources(3, true);
1507 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1508 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1509 break;
1510 }
1511 case kAllScratch_TestCase: {
1512 context->purgeUnlockedResources(50, true);
1513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1515 break;
1516 }
1517 case kPartial_TestCase: {
1518 context->purgeUnlockedResources(13, false);
1519 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1520 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1521 break;
1522 }
1523 case kAll_TestCase: {
1524 context->purgeUnlockedResources(50, false);
1525 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1526 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1527 break;
1528 }
1529 case kNone_TestCase: {
1530 context->purgeUnlockedResources(0, true);
1531 context->purgeUnlockedResources(0, false);
1532 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1533 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1534 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1535 break;
1536 }
1537 };
1538
1539 // ensure all are purged before the next
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001540 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001541 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1542 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1543
1544 }
1545}
1546
bsalomon10e23ca2014-11-25 05:52:06 -08001547static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001548 // Set the cache size to double the resource count because we're going to create 2x that number
1549 // resources, using two different key domains. Add a little slop to the bytes because we resize
1550 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001551 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001552
bsalomonc2f35b72015-01-23 07:19:22 -08001553 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1554 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001555 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001556 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001557
1558 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001559 GrUniqueKey key1, key2;
1560 make_unique_key<1>(&key1, i);
1561 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001562
bsalomon24db3b12015-01-23 04:24:04 -08001563 TestResource* resource;
1564
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001565 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001566 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001567 resource->setSize(1);
1568 resource->unref();
1569
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001570 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001571 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001572 resource->setSize(1);
1573 resource->unref();
1574 }
1575
1576 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001577 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001578 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1579 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1580 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1581 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001582 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001583 GrUniqueKey key1, key2;
1584 make_unique_key<1>(&key1, i);
1585 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001586
bsalomon8718aaf2015-02-19 07:24:21 -08001587 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1588 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001589 }
1590
bsalomon0ea80f42015-02-11 10:49:59 -08001591 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001592 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001593 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001594 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1595 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1596 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1597 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001598
1599 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001600 GrUniqueKey key1, key2;
1601 make_unique_key<1>(&key1, i);
1602 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001603
bsalomon8718aaf2015-02-19 07:24:21 -08001604 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1605 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001606 }
1607}
1608
senorblanco84cd6212015-08-04 10:01:58 -07001609static void test_custom_data(skiatest::Reporter* reporter) {
1610 GrUniqueKey key1, key2;
1611 make_unique_key<0>(&key1, 1);
1612 make_unique_key<0>(&key2, 2);
1613 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001614 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001615 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1616 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1617
1618 // Test that copying a key also takes a ref on its custom data.
1619 GrUniqueKey key3 = key1;
1620 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1621}
1622
bsalomonc6363ef2015-09-24 07:07:40 -07001623static void test_abandoned(skiatest::Reporter* reporter) {
1624 Mock mock(10, 300);
1625 GrContext* context = mock.context();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001626 GrGpu* gpu = context->contextPriv().getGpu();
1627
1628 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001629 context->abandonContext();
1630
1631 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1632
1633 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1634
robertphillips8abb3702016-08-31 14:04:06 -07001635 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001636 resource->getUniqueKey();
1637 resource->wasDestroyed();
1638 resource->gpuMemorySize();
1639 resource->getContext();
1640
1641 resource->abandon();
1642 resource->resourcePriv().getScratchKey();
1643 resource->resourcePriv().isBudgeted();
1644 resource->resourcePriv().makeBudgeted();
1645 resource->resourcePriv().makeUnbudgeted();
1646 resource->resourcePriv().removeScratchKey();
1647 GrUniqueKey key;
1648 make_unique_key<0>(&key, 1);
1649 resource->resourcePriv().setUniqueKey(key);
1650 resource->resourcePriv().removeUniqueKey();
1651}
1652
Brian Salomon1090da62017-01-06 12:04:19 -05001653static void test_tags(skiatest::Reporter* reporter) {
1654#ifdef SK_DEBUG
1655 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1656 static constexpr int kLastTagIdx = 10;
1657 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1658
1659 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1660 GrContext* context = mock.context();
1661 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001662 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001663
1664 SkString tagStr;
1665 int tagIdx = 0;
1666 int currTagCnt = 0;
1667
1668 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001669 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001670 GrUniqueKey key;
1671 if (currTagCnt == tagIdx) {
1672 tagIdx += 1;
1673 currTagCnt = 0;
1674 tagStr.printf("tag%d", tagIdx);
1675 }
1676 make_unique_key<1>(&key, i, tagStr.c_str());
1677 resource->resourcePriv().setUniqueKey(key);
1678 }
1679 SkASSERT(kLastTagIdx == tagIdx);
1680 SkASSERT(currTagCnt == kLastTagIdx);
1681
1682 // Test i = 0 to exercise unused tag string.
1683 for (int i = 0; i <= kLastTagIdx; ++i) {
1684 tagStr.printf("tag%d", i);
1685 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1686 }
1687#endif
1688}
1689
Brian Salomondcfca432017-11-15 15:48:03 -05001690DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001691 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001692 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001693 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001694 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001695 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001696 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001697 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001698 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001699 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001700 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001701 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001702 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001703 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001704 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001705 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001706 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001707 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001708 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001709 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001710 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001711 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001712}
1713
Robert Phillipsd6214d42016-11-07 08:23:48 -05001714////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001715static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001716 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001717 int width, int height,
1718 int sampleCnt) {
1719 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001720 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001721 desc.fWidth = width;
1722 desc.fHeight = height;
1723 desc.fConfig = kRGBA_8888_GrPixelConfig;
1724 desc.fSampleCnt = sampleCnt;
1725
Robert Phillipse78b7252017-04-06 07:59:41 -04001726 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001727}
1728
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001729static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001730 GrSurfaceDescFlags descFlags,
Robert Phillipse78b7252017-04-06 07:59:41 -04001731 int width, int height,
1732 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001733 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001734 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001735 desc.fWidth = width;
1736 desc.fHeight = height;
1737 desc.fConfig = kRGBA_8888_GrPixelConfig;
1738 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001739
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001740 auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1741 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001742
1743 return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001744}
1745
1746// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1747// Texture-only, both-RT-and-Texture and MIPmapped
1748DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1749 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001750 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001751 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001752
Robert Phillipsd6214d42016-11-07 08:23:48 -05001753 static const int kSize = 64;
1754
Robert Phillipsd6214d42016-11-07 08:23:48 -05001755 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001756 {
1757 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001758
Brian Salomonbdecacf2018-02-02 20:32:49 -05001759 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001760 size_t size = tex->gpuMemorySize();
1761 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1762
Brian Salomonbdecacf2018-02-02 20:32:49 -05001763 size_t sampleCount =
1764 (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001765 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001766 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001767 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001768 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001769 REPORTER_ASSERT(reporter,
1770 kSize*kSize*4 == size || // msaa4 failed
1771 kSize*kSize*4*sampleCount == size || // auto-resolving
1772 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001773 }
1774
Brian Salomonbdecacf2018-02-02 20:32:49 -05001775 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001776 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001777 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001778 }
1779
Robert Phillipsd6214d42016-11-07 08:23:48 -05001780
1781 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001782 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001783 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001784
Brian Salomonbdecacf2018-02-02 20:32:49 -05001785 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001786 size_t size = proxy->gpuMemorySize();
1787 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1788
Brian Salomonbdecacf2018-02-02 20:32:49 -05001789 size_t sampleCount =
1790 (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001791 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001792 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001793 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001794 size = proxy->gpuMemorySize();
1795 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001796 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1797 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1798 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001799 }
Robert Phillips1b352562017-04-05 18:56:21 +00001800
Brian Salomonbdecacf2018-02-02 20:32:49 -05001801 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001802 size = proxy->gpuMemorySize();
1803 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1804 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001805}
1806
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001807#endif