blob: 3ca5acce460f173847f3c820bfc3f6ce0775696b [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
Mike Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Brian Salomon72050802020-10-12 20:45:06 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040011#include "include/gpu/GrDirectContext.h"
Brian Salomon72050802020-10-12 20:45:06 -040012#include "src/core/SkMessageBus.h"
13#include "src/core/SkMipmap.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrGpuResourceCacheAccess.h"
17#include "src/gpu/GrGpuResourcePriv.h"
18#include "src/gpu/GrProxyProvider.h"
Robert Phillipse94b4e12020-07-23 13:54:35 -040019#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040020#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrResourceCache.h"
22#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000023#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/SkGr.h"
25#include "tests/Test.h"
Brian Salomon72050802020-10-12 20:45:06 -040026#include "tools/gpu/GrContextFactory.h"
27#include "tools/gpu/ManagedBackendTexture.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028
Hal Canary8a001442018-09-19 11:31:27 -040029#include <thread>
30
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040036 auto context = ctxInfo.directContext();
kkinnunen15302832015-12-01 04:35:26 -080037 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070038 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080039 SkCanvas* canvas = surface->getCanvas();
40
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
42
43 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000044 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040046 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000048 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070049 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050
Robert Phillipscf39f372019-09-03 10:29:20 -040051 size_t oldMaxBytes = context->getResourceCacheLimit();
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000052
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053 // Set the cache limits so we can fit 10 "src" images and the
54 // max number of textures doesn't matter
55 size_t maxCacheSize = initialCacheSize + 10*srcSize;
Robert Phillipscf39f372019-09-03 10:29:20 -040056 context->setResourceCacheLimit(maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000059 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060
61 for (int i = 0; i < 100; ++i) {
Mike Reed34a0c972021-01-25 17:49:32 -050062 canvas->drawImage(src.asImage(), 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040063 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 // "modify" the src texture
66 src.notifyPixelsChanged();
67
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000068 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070069 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000070
71 // we should never go over the size limit
72 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
73 }
74
Robert Phillipscf39f372019-09-03 10:29:20 -040075 context->setResourceCacheLimit(oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000076}
77
bsalomon11abd8d2016-10-14 08:13:48 -070078static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
79 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
80 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
81 return false;
82 }
83 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
84}
85
Greg Danielc0d69152020-10-08 14:59:00 -040086static GrAttachment* get_SB(GrRenderTarget* rt) { return rt->getStencilAttachment(); }
Robert Phillipsc0192e32017-09-21 12:00:26 -040087
88static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
89 int size, int sampleCount, SkBudgeted budgeted) {
Brian Salomon4eb38b72019-08-05 12:58:39 -040090 auto format =
91 provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
Brian Salomona56a7462020-02-07 14:17:25 -050092 sk_sp<GrTexture> tex(provider->createTexture({size, size}, format, GrRenderable::kYes,
Brian Salomon7e67dca2020-07-21 09:27:25 -040093 sampleCount, GrMipmapped::kNo, budgeted,
Brian Salomona56a7462020-02-07 14:17:25 -050094 GrProtected::kNo));
Robert Phillipsc0192e32017-09-21 12:00:26 -040095 if (!tex || !tex->asRenderTarget()) {
96 return nullptr;
97 }
98
Chris Daltone0fe23a2021-04-23 13:11:44 -060099 if (!provider->attachStencilAttachment(tex->asRenderTarget(), sampleCount > 1)) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400100 return nullptr;
101 }
102 SkASSERT(get_SB(tex->asRenderTarget()));
103
104 return sk_ref_sp(tex->asRenderTarget());
105}
106
bsalomon11abd8d2016-10-14 08:13:48 -0700107// This currently fails on ES3 ANGLE contexts
108DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000109 ctxInfo, nullptr) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400110 auto context = ctxInfo.directContext();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400111 const GrCaps* caps = context->priv().caps();
112
113 if (caps->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700114 return;
115 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400116
Robert Phillips9da87e02019-02-04 13:26:26 -0500117 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400118
Greg Daniel5c96db82019-07-09 14:06:58 -0400119 GrColorType grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400120 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400121
Brian Salomonbdecacf2018-02-02 20:32:49 -0500122 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400123 REPORTER_ASSERT(reporter, smallRT0);
124
125 {
126 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500127 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
128 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400129
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800131 }
132
Robert Phillipsc0192e32017-09-21 12:00:26 -0400133 {
134 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400136 REPORTER_ASSERT(reporter, smallRT2);
137
138 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800139 }
140
Robert Phillipsc0192e32017-09-21 12:00:26 -0400141 {
142 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400144 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800145
Robert Phillipsc0192e32017-09-21 12:00:26 -0400146 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147 }
bsalomon02a44a42015-02-19 09:09:00 -0800148
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400149 int smallSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400150 context->priv().caps()->getRenderTargetSampleCount(2, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500151 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700152 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500153 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
154 smallSampleCount, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400155 REPORTER_ASSERT(reporter, smallMSAART0);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400156
157 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
158
159 {
160 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500161 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
162 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400163 SkBudgeted::kNo);
164 REPORTER_ASSERT(reporter, smallMSAART1);
165
166 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800167 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168
Brian Salomonbdecacf2018-02-02 20:32:49 -0500169 // But one with a larger sample count should not. (Also check that the two requests didn't
170 // rounded up to the same actual sample count or else they could share.).
Greg Daniel6fa62e22019-08-07 15:52:37 -0400171 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(5, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500172 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500173 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
174 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400175 SkBudgeted::kNo);
176 REPORTER_ASSERT(reporter, smallMSAART2);
177
178 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800179 }
180 }
181}
182
bsalomon68d91342016-04-12 09:59:58 -0700183DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400184 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500185 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
186 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700187 // this test is only valid for GL
188 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700189 return;
190 }
191
bsalomon6dc6f5f2015-06-18 09:12:16 -0700192 static const int kW = 100;
193 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700194
Brian Salomon72050802020-10-12 20:45:06 -0400195 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
196 context, kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
197 GrBackendTexture unmbet = context->createBackendTexture(
198 kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
199 if (!mbet || !unmbet.isValid()) {
200 ERRORF(reporter, "Could not create backend texture.");
Greg Daniel5366e592018-01-10 09:57:53 -0500201 return;
202 }
jvanverth672bb7f2015-07-13 07:19:57 -0700203
bsalomon6dc6f5f2015-06-18 09:12:16 -0700204 context->resetContext();
205
Robert Phillips6be756b2018-01-16 15:07:54 -0500206 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -0400207 mbet->texture(), kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700208
Robert Phillips6be756b2018-01-16 15:07:54 -0500209 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -0400210 unmbet, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700211
Brian Osman85d34b22017-05-10 12:06:26 -0400212 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
213 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700214 return;
215 }
216
Brian Salomon72050802020-10-12 20:45:06 -0400217 borrowed.reset();
218 adopted.reset();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700219
Brian Salomon72050802020-10-12 20:45:06 -0400220 context->flushAndSubmit(/*sync*/ true);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221
Brian Salomon72050802020-10-12 20:45:06 -0400222 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(mbet->texture());
223 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(unmbet);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224
225 REPORTER_ASSERT(reporter, borrowedIsAlive);
226 REPORTER_ASSERT(reporter, !adoptedIsAlive);
227
Brian Salomone64b0642018-03-07 11:47:54 -0500228 if (adoptedIsAlive) {
Brian Salomon72050802020-10-12 20:45:06 -0400229 context->deleteBackendTexture(unmbet);
Brian Salomone64b0642018-03-07 11:47:54 -0500230 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
232 context->resetContext();
233}
234
bsalomon6d3fe022014-07-25 08:35:45 -0700235class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800236 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000237public:
robertphillips6e83ac72015-08-13 05:19:14 -0700238 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700239
bsalomon1c60dfe2015-01-21 09:32:40 -0800240 /** Property that distinctly categorizes the resource.
241 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800242 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800243
kkinnunen2e6055b2016-04-22 01:48:29 -0700244 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
245 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700246 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800247 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700248 , fProperty(kA_SimulatedProperty)
249 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800250 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700251 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800252 }
253
kkinnunen2e6055b2016-04-22 01:48:29 -0700254 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400255 SimulatedProperty property, size_t size = kDefaultSize) {
256 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800257 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500258 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
259 size_t size = kDefaultSize) {
260 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000261 }
262
Brian Salomond3b65972017-03-22 12:05:03 -0400263 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800264 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000265 }
266
bsalomon33435572014-11-05 14:47:41 -0800267 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000268
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400269 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
270 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000271 }
272
bsalomon1c60dfe2015-01-21 09:32:40 -0800273 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
274 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
275 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800276 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
277 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800278 }
279 }
280
281 static size_t ExpectedScratchKeySize() {
282 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
283 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284private:
bsalomon24db3b12015-01-23 04:24:04 -0800285 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800286
Greg Danielda86e282018-06-13 09:41:19 -0400287 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
288 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700289 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700290 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400291 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700292 , fProperty(property)
293 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700295 this->registerWithCache(budgeted);
296 }
297
298 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500299 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
300 : INHERITED(gpu)
301 , fToDelete(nullptr)
302 , fSize(size)
303 , fProperty(kA_SimulatedProperty)
304 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500306 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700307 }
308
309 void computeScratchKey(GrScratchKey* key) const override {
310 if (fIsScratch) {
311 ComputeScratchKey(fProperty, key);
312 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800313 }
314
mtklein36352bf2015-03-25 18:17:31 -0700315 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400316 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800317
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400318 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000319 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800320 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800321 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700322 bool fIsScratch;
John Stiles7571f9e2020-09-02 22:42:33 -0400323 using INHERITED = GrGpuResource;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000324};
bsalomon33435572014-11-05 14:47:41 -0800325int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000326
bsalomonc2f35b72015-01-23 07:19:22 -0800327class Mock {
328public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400329 Mock(size_t maxBytes) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400330 fDContext = GrDirectContext::MakeMock(nullptr);
331 SkASSERT(fDContext);
332 fDContext->setResourceCacheLimit(maxBytes);
333 GrResourceCache* cache = fDContext->priv().getResourceCache();
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400334 cache->purgeUnlockedResources();
bsalomon0ea80f42015-02-11 10:49:59 -0800335 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800336 }
bsalomonc2f35b72015-01-23 07:19:22 -0800337
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400338 GrResourceCache* cache() { return fDContext->priv().getResourceCache(); }
339 GrGpu* gpu() { return fDContext->priv().getGpu(); }
340 GrDirectContext* dContext() { return fDContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800341
Greg Danielc27eb722018-08-10 09:48:08 -0400342 void reset() {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400343 fDContext.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400344 }
345
bsalomonc2f35b72015-01-23 07:19:22 -0800346private:
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400347 sk_sp<GrDirectContext> fDContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800348};
349
350static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400351 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800352 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400353 GrGpu* gpu = mock.gpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800354
355 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400356 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
357 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
358 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
359 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800360
361 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800362 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800363 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800364 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800365
366 // Should be safe to purge without deleting the resources since we still have refs.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400367 cache->purgeUnlockedResources();
bsalomon71cb0c22014-11-14 12:10:14 -0800368 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
369
bsalomon8718aaf2015-02-19 07:24:21 -0800370 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800371
372 a->unref();
373 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800374 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800375 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800376 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800377
378 c->unref();
379 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800380 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800381 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800382 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 d->unref();
385 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800386 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
387 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 b->unref();
390 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
392 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800393}
394
bsalomon24db3b12015-01-23 04:24:04 -0800395// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500396template <int>
397static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800398 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500399 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800400 builder[0] = data;
401}
402
Robert Phillips6eba0632018-03-28 12:25:42 -0400403static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400404 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400405 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400406 GrGpu* gpu = mock.gpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400407
408 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
409 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400410 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400411
412 GrUniqueKey uniqueKey;
413 make_unique_key<0>(&uniqueKey, 0);
414
415 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400416 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400417 b->resourcePriv().setUniqueKey(uniqueKey);
418
419 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400420 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400421
422 GrUniqueKey uniqueKey2;
423 make_unique_key<0>(&uniqueKey2, 1);
424
425 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400426 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400427 d->resourcePriv().setUniqueKey(uniqueKey2);
428
429
430 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
431 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
432 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
433 d->gpuMemorySize() == cache->getResourceBytes());
434
435 // Should be safe to purge without deleting the resources since we still have refs.
436 cache->purgeUnlockedResources(false);
437 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
438
439 // Unref them all. Since they all have keys they should remain in the cache.
440
441 a->unref();
442 b->unref();
443 c->unref();
444 d->unref();
445 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
446 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
447 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
448 d->gpuMemorySize() == cache->getResourceBytes());
449
450 // Purge only the two scratch resources
451 cache->purgeUnlockedResources(true);
452
453 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
454 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
455 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
456 cache->getResourceBytes());
457
458 // Purge the uniquely keyed resources
459 cache->purgeUnlockedResources(false);
460
461 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
462 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
463 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
464}
465
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500466static void test_purge_command_buffer_usage(skiatest::Reporter* reporter) {
467 Mock mock(30000);
468 GrResourceCache* cache = mock.cache();
469 GrGpu* gpu = mock.gpu();
470
471 // Create two resource w/ scratch keys.
472 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
473 TestResource::kA_SimulatedProperty, 11);
474
475 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
476 TestResource::kA_SimulatedProperty, 12);
477
478 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
479 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
480 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
481
482 // Should be safe to purge without deleting the resources since we still have refs.
483 cache->purgeUnlockedResources(true);
484 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
485
486 // Add command buffer usages to all resources
487 a->addCommandBufferUsage();
488 b->addCommandBufferUsage();
489
490 // Should be safe to purge without deleting the resources since we still have refs and command
491 // buffer usages.
492 cache->purgeUnlockedResources(true);
493 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
494
495 // Unref the first resource
496 a->unref();
497 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
498 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
499 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
500
501 // Should be safe to purge without deleting the resources since we still have command buffer
502 // usages and the second still has a ref.
503 cache->purgeUnlockedResources(true);
504 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
505
506 // Remove command buffer usages
507 a->removeCommandBufferUsage();
508 b->removeCommandBufferUsage();
509 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
510 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
511 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
512
513 // Purge this time should remove the first resources since it no longer has any refs or command
514 // buffer usages.
515 cache->purgeUnlockedResources(true);
516 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
517 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
518 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
519
520 // Unref the second resource
521 b->unref();
522 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
523 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
524 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
525
526 // Purge the last resource
527 cache->purgeUnlockedResources(false);
528
529 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
530 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
531 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
532}
533
bsalomon84c8e622014-11-17 09:33:27 -0800534static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400535 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800536 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400537 GrGpu* gpu = mock.gpu();
bsalomondace19e2014-11-17 07:34:06 -0800538
bsalomon8718aaf2015-02-19 07:24:21 -0800539 GrUniqueKey uniqueKey;
540 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800541
bsalomon8718aaf2015-02-19 07:24:21 -0800542 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800543 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400544 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
545 10);
546 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800547 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500548 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
549 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
550 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800551
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500552 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800553 GrUniqueKey uniqueKey2;
554 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500555 GrUniqueKey uniqueKey3;
556 make_unique_key<0>(&uniqueKey3, 2);
557 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
558 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
559 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
560 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
561 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
562 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400563
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500564 // Remove the extra refs we just added.
565 SkSafeUnref(wrappedCacheableViaKey);
566 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800567
568 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500569 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800570 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500571 wrappedCacheable->gpuMemorySize() +
572 wrappedUncacheable->gpuMemorySize() +
573 unbudgeted->gpuMemorySize() ==
574 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800576 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800577 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400578 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800579
bsalomon63c992f2015-01-23 12:47:59 -0800580 // Our refs mean that the resources are non purgeable.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400581 cache->purgeUnlockedResources();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500582 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800583 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500584 wrappedCacheable->gpuMemorySize() +
585 wrappedUncacheable->gpuMemorySize() +
586 unbudgeted->gpuMemorySize() ==
587 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800589 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800590 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400591 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800592
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500593 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
594 // However, unreffing the uncacheable wrapped resource should free it.
595 wrappedCacheable->unref();
596 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400597 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800598 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500599 wrappedCacheable->gpuMemorySize() +
600 unbudgeted->gpuMemorySize() ==
601 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500602 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800603
bsalomon84c8e622014-11-17 09:33:27 -0800604 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500605 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800606 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500607 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
608 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
609 // removed to be freed.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400610 cache->purgeUnlockedResources();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500611 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500612
613 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
614 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
615 if (wrappedCacheableViaKey) {
616 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
617 wrappedCacheable->unref();
618 }
619 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
620 // resource should immediately delete it.
621 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
622
623 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500624 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
625 wrappedUncacheable->gpuMemorySize() +
626 unbudgeted->gpuMemorySize() ==
627 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800628 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
629 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400630 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800631
632 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400633 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400634 cache->purgeUnlockedResources();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500635 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
636 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
637 wrappedUncacheable->gpuMemorySize() ==
638 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800639 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
640 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400641 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800642
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500643 // Unreffing the wrapped resources (with no unique key) should free them right away.
644 wrappedUncacheable->unref();
645 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800646 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
647 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
648 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
649 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400650 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800651
652 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800653 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
654 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
655 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
656 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400657 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800658}
659
bsalomon5236cf42015-01-14 10:42:08 -0800660static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400661 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800662 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400663 GrGpu* gpu = mock.gpu();
bsalomon5236cf42015-01-14 10:42:08 -0800664
bsalomon8718aaf2015-02-19 07:24:21 -0800665 GrUniqueKey uniqueKey;
666 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800667
668 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800669 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800670 TestResource* wrapped;
671 TestResource* unbudgeted;
672
673 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500674 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400675 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700676
bsalomon5236cf42015-01-14 10:42:08 -0800677 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800678 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
679 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
680 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
681 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400682 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800683
Greg Danielda86e282018-06-13 09:41:19 -0400684 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800685 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800686 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800687 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
688 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
689 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
690 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400691 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800692
bsalomon0ea80f42015-02-11 10:49:59 -0800693 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500694 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800695 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
696 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
697 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
698 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400699 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800700
701 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800702 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
703 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
704 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
705 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400706 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800707
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500708 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800709 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
710 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
711 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
712 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400713 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800714
715 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800716 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
717 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
718 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
719 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400720 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800721
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400722 cache->purgeUnlockedResources();
bsalomon0ea80f42015-02-11 10:49:59 -0800723 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
724 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
725 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
726 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400727 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800728}
729
bsalomon3582d3e2015-02-13 14:20:05 -0800730// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
731void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
732/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400733 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800734 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400735 GrGpu* gpu = mock.gpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800736
737 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500738 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800739 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800740 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800741
742 size_t size = resource->gpuMemorySize();
743 for (int i = 0; i < 2; ++i) {
744 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800745 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800746 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500747 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500748 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400749 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800750 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
751 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
752 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
753 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400754 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800755
756 // Once it is unrefed, it should become available as scratch.
757 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800758 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
759 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
760 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
761 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400762 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400763 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800764 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800765 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800766 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500767 REPORTER_ASSERT(reporter,
768 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800769
770 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700771 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800772 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800773 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800774 } else {
775 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800776 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800777 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
778 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
779 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
780 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400781 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800782 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800783 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500784 REPORTER_ASSERT(reporter,
785 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800786
787 // now when it is unrefed it should die since it has no key.
788 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800789 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
790 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
791 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
792 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400793 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800794 }
bsalomon8b79d232014-11-10 10:19:06 -0800795 }
bsalomonc2f35b72015-01-23 07:19:22 -0800796}
797
798static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400799 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800800 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400801 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -0800802
bsalomon8b79d232014-11-10 10:19:06 -0800803 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500804 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700805 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400806 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500807 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700808 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400809 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800810 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800811 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800812 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400813 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800814
815 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800816 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800817
bsalomon0ea80f42015-02-11 10:49:59 -0800818 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800819 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
Greg Danielda642612021-02-09 18:04:02 -0500820 // As long as there are outstanding refs on the resources they will not be in the scratch map
821 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon0ea80f42015-02-11 10:49:59 -0800822 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800823 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800824 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800825
bsalomon63c992f2015-01-23 12:47:59 -0800826 // Our refs mean that the resources are non purgeable.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400827 cache->purgeUnlockedResources();
bsalomon8b79d232014-11-10 10:19:06 -0800828 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800829 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800830
831 // Unref but don't purge
832 a->unref();
833 b->unref();
834 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
Greg Danielda642612021-02-09 18:04:02 -0500835 // Since we removed the refs to the resources they will now be in the scratch map
bsalomon0ea80f42015-02-11 10:49:59 -0800836 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800837
bsalomon63c992f2015-01-23 12:47:59 -0800838 // Purge again. This time resources should be purgeable.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400839 cache->purgeUnlockedResources();
bsalomon8b79d232014-11-10 10:19:06 -0800840 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800841 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
842 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800843}
844
bsalomon10e23ca2014-11-25 05:52:06 -0800845static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400846 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800847 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400848 GrGpu* gpu = mock.gpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800849
bsalomon10e23ca2014-11-25 05:52:06 -0800850 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500851 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800852 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500853 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800854 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800855 a->unref();
856 b->unref();
857
bsalomon1c60dfe2015-01-21 09:32:40 -0800858 GrScratchKey scratchKey;
859 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800860 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800861 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400862 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800863
bsalomon0ea80f42015-02-11 10:49:59 -0800864 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800865 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800866 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800867 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
868 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800869
870 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400871 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800872 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800873 // It's still alive, but not cached by scratch key anymore
874 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800875 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
876 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800877
878 // The cache should immediately delete it when it's unrefed since it isn't accessible.
879 find->unref();
880 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800881 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
882 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800883
884 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400885 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800886 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800887 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800888 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
889 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800890
891 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800892 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800893 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800894 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
895 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800896
897 find->unref();
898 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800899 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
900 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800901}
902
bsalomon1c60dfe2015-01-21 09:32:40 -0800903static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400904 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800905 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400906 GrGpu* gpu = mock.gpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800907
908 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500909 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800910 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500911 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800912 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800913 a->unref();
914 b->unref();
915
916 GrScratchKey scratchKey;
917 // Ensure that scratch key comparison and assignment is consistent.
918 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800919 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800920 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800921 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800922 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
923 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
924 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
925 scratchKey = scratchKey1;
926 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
927 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
928 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
929 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
930 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
931 scratchKey = scratchKey2;
932 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
933 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
934 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
935 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
936 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
937
938 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800939 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800940 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400941 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800942
943 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800944 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400945 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700946 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800947 find->unref();
948
949 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400950 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700951 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800952 REPORTER_ASSERT(reporter, find == a || find == b);
953
Robert Phillipsaee18c92019-09-06 11:48:27 -0400954 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700955 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800956 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
957 REPORTER_ASSERT(reporter, find2 != find);
958 find2->unref();
959 find->unref();
960}
961
bsalomon8718aaf2015-02-19 07:24:21 -0800962static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400963 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800964 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400965 GrGpu* gpu = mock.gpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000966
bsalomon8718aaf2015-02-19 07:24:21 -0800967 GrUniqueKey key;
968 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700969
bsalomon8718aaf2015-02-19 07:24:21 -0800970 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400971 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700972
bsalomonf99e9612015-02-19 08:24:16 -0800973 // Set key on resource a.
974 a->resourcePriv().setUniqueKey(key);
975 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
976 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800977
bsalomonf99e9612015-02-19 08:24:16 -0800978 // Make sure that redundantly setting a's key works.
979 a->resourcePriv().setUniqueKey(key);
980 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800981 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800982 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
983 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800984 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
985
bsalomonf99e9612015-02-19 08:24:16 -0800986 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400987 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800988 b->resourcePriv().setUniqueKey(key);
989 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
990 b->unref();
991
992 // Still have two resources because a is still reffed.
993 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
994 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
995 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
996
997 a->unref();
998 // Now a should be gone.
999 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
1000 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
1001 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1002
1003 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
1004 // Also make b be unreffed when replacement occurs.
1005 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -04001006 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -08001007 GrUniqueKey differentKey;
1008 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -08001009 c->resourcePriv().setUniqueKey(differentKey);
1010 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
1011 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
1012 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1013 // c replaces b and b should be immediately purged.
1014 c->resourcePriv().setUniqueKey(key);
1015 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
1016 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
1017 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1018
1019 // c shouldn't be purged because it is ref'ed.
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001020 cache->purgeUnlockedResources();
bsalomonf99e9612015-02-19 08:24:16 -08001021 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
1022 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
1023 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1024
1025 // Drop the ref on c, it should be kept alive because it has a unique key.
1026 c->unref();
1027 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
1028 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
1029 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1030
1031 // Verify that we can find c, then remove its unique key. It should get purged immediately.
1032 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
1033 c->resourcePriv().removeUniqueKey();
1034 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -08001035 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1036 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -08001037 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -07001038
1039 {
1040 GrUniqueKey key2;
1041 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001042 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -07001043 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001044 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001045 d->resourcePriv().setUniqueKey(key2);
1046 }
1047
1048 GrUniqueKey key3;
1049 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001050 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001051 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001052}
1053
bsalomon8b79d232014-11-10 10:19:06 -08001054static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001055 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001056 auto dContext = mock.dContext();
bsalomon0ea80f42015-02-11 10:49:59 -08001057 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001058 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -08001059
bsalomon8718aaf2015-02-19 07:24:21 -08001060 GrUniqueKey key1, key2, key3;
1061 make_unique_key<0>(&key1, 1);
1062 make_unique_key<0>(&key2, 2);
1063 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001064
bsalomon23e619c2015-02-06 11:54:28 -08001065 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001066 TestResource* a = new TestResource(gpu);
1067 TestResource* b = new TestResource(gpu);
1068 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001069 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001070 a->resourcePriv().setUniqueKey(key1);
1071 b->resourcePriv().setUniqueKey(key2);
1072 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001073 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001074 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001075 c->unref();
1076
bsalomon8718aaf2015-02-19 07:24:21 -08001077 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1078 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1079 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001080 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001081
bsalomon8718aaf2015-02-19 07:24:21 -08001082 typedef GrUniqueKeyInvalidatedMessage Msg;
Robert Phillipse7a959d2021-03-11 14:44:42 -05001083 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001084
1085 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001086 Bus::Post(Msg(key1, dContext->priv().contextID()));
1087 Bus::Post(Msg(key2, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001088 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001089 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001090 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1091 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001092 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001093 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001094
1095 // Invalidate the third.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001096 Bus::Post(Msg(key3, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001097 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001098 // we still have a ref on b, c should be recycled as scratch.
1099 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001100 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001101
bsalomon23e619c2015-02-06 11:54:28 -08001102 // make b purgeable. It should be immediately deleted since it has no key.
1103 b->unref();
1104 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1105
1106 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1107 GrScratchKey scratchKey;
1108 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001109 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001110 REPORTER_ASSERT(reporter, scratch == c);
1111 SkSafeUnref(scratch);
1112
1113 // Get rid of c.
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001114 cache->purgeUnlockedResources();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001115 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001116 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001117 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1118 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001119 REPORTER_ASSERT(reporter, !scratch);
1120 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001121}
1122
bsalomon71cb0c22014-11-14 12:10:14 -08001123static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001124 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -08001125 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001126 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -08001127
bsalomon8718aaf2015-02-19 07:24:21 -08001128 GrUniqueKey key1, key2;
1129 make_unique_key<0>(&key1, 1);
1130 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001131
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001132 sk_sp<TestResource> a(new TestResource(gpu));
1133 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001134 a->resourcePriv().setUniqueKey(key1);
1135 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001136
bsalomonc2f35b72015-01-23 07:19:22 -08001137 // Make a cycle
1138 a->setUnrefWhenDestroyed(b);
1139 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001140
bsalomonc2f35b72015-01-23 07:19:22 -08001141 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001142
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001143 TestResource* unownedA = a.release();
1144 unownedA->unref();
1145 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001146
bsalomonc2f35b72015-01-23 07:19:22 -08001147 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001148
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001149 cache->purgeUnlockedResources();
bsalomonc2f35b72015-01-23 07:19:22 -08001150 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001151
bsalomonc2f35b72015-01-23 07:19:22 -08001152 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001153 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001154 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001155
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001156 cache->purgeUnlockedResources();
bsalomonc2f35b72015-01-23 07:19:22 -08001157 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001158}
1159
bsalomonddf30e62015-02-19 11:38:44 -08001160static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1161 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001162 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001163 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001164
1165 SkRandom random;
1166
1167 // Run the test 2*kCount times;
1168 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001169 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001170 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001171 GrGpu* gpu = mock.gpu();
bsalomonddf30e62015-02-19 11:38:44 -08001172
1173 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001174 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001175
Robert Phillipscf39f372019-09-03 10:29:20 -04001176 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001177
1178 SkTDArray<int> shouldPurgeIdxs;
1179 int purgeableCnt = 0;
1180 SkTDArray<GrGpuResource*> resourcesToUnref;
1181
1182 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1183 // unpurgeable resources.
1184 for (int j = 0; j < kCount; ++j) {
1185 GrUniqueKey key;
1186 make_unique_key<0>(&key, j);
1187
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001188 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001189 r->resourcePriv().setUniqueKey(key);
1190 if (random.nextU() % kLockedFreq) {
1191 // Make this is purgeable.
1192 r->unref();
1193 ++purgeableCnt;
1194 if (purgeableCnt <= kNumToPurge) {
1195 *shouldPurgeIdxs.append() = j;
1196 }
1197 } else {
1198 *resourcesToUnref.append() = r;
1199 }
1200 }
1201
1202 // Verify that the correct resources were purged.
1203 int currShouldPurgeIdx = 0;
1204 for (int j = 0; j < kCount; ++j) {
1205 GrUniqueKey key;
1206 make_unique_key<0>(&key, j);
1207 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1208 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1209 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1210 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001211 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001212 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001213 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001214 }
1215 SkSafeUnref(res);
1216 }
1217
1218 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1219 resourcesToUnref[j]->unref();
1220 }
1221 }
1222}
1223
Brian Salomon5e150852017-03-22 14:53:13 -04001224static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001225 Mock mock(1000000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001226 auto dContext = mock.dContext();
Brian Salomon5e150852017-03-22 14:53:13 -04001227 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001228 GrGpu* gpu = mock.gpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001229
1230 static constexpr int kCnts[] = {1, 10, 1024};
1231 auto nowish = []() {
1232 // We sleep so that we ensure we get a value that is greater than the last call to
1233 // GrStdSteadyClock::now().
1234 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1235 auto result = GrStdSteadyClock::now();
1236 // Also sleep afterwards so we don't get this value again.
1237 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1238 return result;
1239 };
1240
1241 for (int cnt : kCnts) {
1242 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1243 new GrStdSteadyClock::time_point[cnt]);
1244 {
1245 // Insert resources and get time points between each addition.
1246 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001247 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001248 GrUniqueKey k;
1249 make_unique_key<1>(&k, i);
1250 r->resourcePriv().setUniqueKey(k);
1251 r->unref();
1252 timeStamps.get()[i] = nowish();
1253 }
1254
1255 // Purge based on the time points between resource additions. Each purge should remove
1256 // the oldest resource.
1257 for (int i = 0; i < cnt; ++i) {
1258 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1259 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1260 for (int j = 0; j < i; ++j) {
1261 GrUniqueKey k;
1262 make_unique_key<1>(&k, j);
1263 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1264 REPORTER_ASSERT(reporter, !SkToBool(r));
1265 SkSafeUnref(r);
1266 }
1267 }
1268
1269 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001270 cache->purgeUnlockedResources();
Brian Salomon5e150852017-03-22 14:53:13 -04001271 }
1272
1273 // Do a similar test but where we leave refs on some resources to prevent them from being
1274 // purged.
1275 {
1276 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1277 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001278 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001279 GrUniqueKey k;
1280 make_unique_key<1>(&k, i);
1281 r->resourcePriv().setUniqueKey(k);
1282 // Leave a ref on every other resource, beginning with the first.
1283 if (SkToBool(i & 0x1)) {
1284 refedResources.get()[i / 2] = r;
1285 } else {
1286 r->unref();
1287 }
1288 timeStamps.get()[i] = nowish();
1289 }
1290
1291 for (int i = 0; i < cnt; ++i) {
1292 // Should get a resource purged every other frame.
1293 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1294 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1295 }
1296
1297 // Unref all the resources that we kept refs on in the first loop.
1298 for (int i = 0; i < (cnt / 2); ++i) {
1299 refedResources.get()[i]->unref();
1300 cache->purgeResourcesNotUsedSince(nowish());
1301 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1302 }
1303
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001304 cache->purgeUnlockedResources();
1305 }
1306
1307 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1308
1309 // Do a similar test where we alternate adding scratch and uniquely keyed resources, but
1310 // then purge old scratch resources.
1311 {
1312 for (int i = 0; i < cnt; ++i) {
1313 const bool isScratch = (i % 2 == 0);
1314 const SkBudgeted budgeted = SkBudgeted::kYes;
1315 const TestResource::SimulatedProperty property = TestResource::kA_SimulatedProperty;
1316 TestResource* r = isScratch ? TestResource::CreateScratch(gpu, budgeted, property)
1317 : new TestResource(gpu, budgeted, property);
1318 if (!isScratch) {
1319 GrUniqueKey k;
1320 make_unique_key<1>(&k, i);
1321 r->resourcePriv().setUniqueKey(k);
1322 }
1323 r->unref();
1324 timeStamps.get()[i] = nowish();
1325 }
1326
1327 for (int i = 0; i < cnt; ++i) {
1328 // Should get a resource purged every other frame, since the uniquely keyed
1329 // resources will not be considered.
1330 cache->purgeResourcesNotUsedSince(timeStamps[i], /*scratchResourcesOnly=*/true);
1331 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1332 }
1333 // Unref remaining resources
1334 cache->purgeResourcesNotUsedSince(nowish());
Brian Salomon5e150852017-03-22 14:53:13 -04001335 }
1336
1337 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1338
Robert Phillipse94b4e12020-07-23 13:54:35 -04001339 // Verify that calling flush() on a context with nothing to do will not trigger resource
Brian Salomon5e150852017-03-22 14:53:13 -04001340 // eviction
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001341 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001342 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001343 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001344 GrUniqueKey k;
1345 make_unique_key<1>(&k, i);
1346 r->resourcePriv().setUniqueKey(k);
1347 r->unref();
1348 }
1349 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001350 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001351 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1352 cache->purgeResourcesNotUsedSince(nowish());
1353 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1354 }
1355}
1356
Derek Sollenberger5480a182017-05-25 16:43:59 -04001357static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001358 Mock mock(100);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001359 auto dContext = mock.dContext();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001360 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001361 GrGpu* gpu = mock.gpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001362
1363 enum TestsCase {
1364 kOnlyScratch_TestCase = 0,
1365 kPartialScratch_TestCase = 1,
1366 kAllScratch_TestCase = 2,
1367 kPartial_TestCase = 3,
1368 kAll_TestCase = 4,
1369 kNone_TestCase = 5,
1370 kEndTests_TestCase = kNone_TestCase + 1
1371 };
1372
1373 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1374
1375 GrUniqueKey key1, key2, key3;
1376 make_unique_key<0>(&key1, 1);
1377 make_unique_key<0>(&key2, 2);
1378 make_unique_key<0>(&key3, 3);
1379
1380 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001381 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1382 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1383 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001384
1385 unique1->resourcePriv().setUniqueKey(key1);
1386 unique2->resourcePriv().setUniqueKey(key2);
1387 unique3->resourcePriv().setUniqueKey(key3);
1388
Derek Sollenberger5480a182017-05-25 16:43:59 -04001389 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001390 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001391 TestResource::kA_SimulatedProperty,
1392 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001393 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001394 TestResource::kB_SimulatedProperty,
1395 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001396
1397 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1398 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1399 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1400
1401 // Add resources to the purgeable queue
1402 unique1->unref();
1403 scratch1->unref();
1404 unique2->unref();
1405 scratch2->unref();
1406 unique3->unref();
1407
1408 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1409 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1410 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1411
1412 switch(testCase) {
1413 case kOnlyScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001414 dContext->purgeUnlockedResources(14, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001415 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1416 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1417 break;
1418 }
1419 case kPartialScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001420 dContext->purgeUnlockedResources(3, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001421 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1422 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1423 break;
1424 }
1425 case kAllScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001426 dContext->purgeUnlockedResources(50, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001427 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1428 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1429 break;
1430 }
1431 case kPartial_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001432 dContext->purgeUnlockedResources(13, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001433 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1434 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1435 break;
1436 }
1437 case kAll_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001438 dContext->purgeUnlockedResources(50, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001439 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1440 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1441 break;
1442 }
1443 case kNone_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001444 dContext->purgeUnlockedResources(0, true);
1445 dContext->purgeUnlockedResources(0, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001446 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1447 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1448 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1449 break;
1450 }
Brian Salomon23356442018-11-30 15:33:19 -05001451 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001452
1453 // ensure all are purged before the next
Michael Ludwig9d1cc052021-06-09 20:49:48 -04001454 dContext->priv().getResourceCache()->purgeUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001455 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1456 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1457
1458 }
1459}
1460
senorblanco84cd6212015-08-04 10:01:58 -07001461static void test_custom_data(skiatest::Reporter* reporter) {
1462 GrUniqueKey key1, key2;
1463 make_unique_key<0>(&key1, 1);
1464 make_unique_key<0>(&key2, 2);
1465 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001466 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001467 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1468 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1469
1470 // Test that copying a key also takes a ref on its custom data.
1471 GrUniqueKey key3 = key1;
1472 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1473}
1474
bsalomonc6363ef2015-09-24 07:07:40 -07001475static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001476 Mock mock(300);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001477 auto dContext = mock.dContext();
1478 GrGpu* gpu = mock.gpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001479
1480 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001481 dContext->abandonContext();
bsalomonc6363ef2015-09-24 07:07:40 -07001482
1483 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1484
1485 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1486
robertphillips8abb3702016-08-31 14:04:06 -07001487 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001488 resource->getUniqueKey();
1489 resource->wasDestroyed();
1490 resource->gpuMemorySize();
1491 resource->getContext();
1492
bsalomonc6363ef2015-09-24 07:07:40 -07001493 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001494 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001495 resource->resourcePriv().makeBudgeted();
1496 resource->resourcePriv().makeUnbudgeted();
1497 resource->resourcePriv().removeScratchKey();
1498 GrUniqueKey key;
1499 make_unique_key<0>(&key, 1);
1500 resource->resourcePriv().setUniqueKey(key);
1501 resource->resourcePriv().removeUniqueKey();
1502}
1503
Brian Salomon1090da62017-01-06 12:04:19 -05001504static void test_tags(skiatest::Reporter* reporter) {
1505#ifdef SK_DEBUG
1506 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1507 static constexpr int kLastTagIdx = 10;
1508 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1509
Robert Phillipscf39f372019-09-03 10:29:20 -04001510 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001511 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001512 GrGpu* gpu = mock.gpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001513
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001514 // tag strings are expected to be long lived
1515 std::vector<SkString> tagStrings;
1516
Brian Salomon1090da62017-01-06 12:04:19 -05001517 SkString tagStr;
1518 int tagIdx = 0;
1519 int currTagCnt = 0;
1520
1521 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001522
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001523 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001524 GrUniqueKey key;
1525 if (currTagCnt == tagIdx) {
1526 tagIdx += 1;
1527 currTagCnt = 0;
1528 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001529 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001530 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001531 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001532 resource->resourcePriv().setUniqueKey(key);
1533 }
1534 SkASSERT(kLastTagIdx == tagIdx);
1535 SkASSERT(currTagCnt == kLastTagIdx);
1536
1537 // Test i = 0 to exercise unused tag string.
1538 for (int i = 0; i <= kLastTagIdx; ++i) {
1539 tagStr.printf("tag%d", i);
1540 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1541 }
1542#endif
1543}
1544
Robert Phillipsddc21482019-10-16 14:30:09 -04001545static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001546 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001547 auto dContext = mock.dContext();
Greg Danielc27eb722018-08-10 09:48:08 -04001548 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001549 GrGpu* gpu = mock.gpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001550
Robert Phillipsddc21482019-10-16 14:30:09 -04001551 GrBackendTexture backends[3];
1552 GrTexture* wrapped[3];
1553 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001554
Robert Phillipsddc21482019-10-16 14:30:09 -04001555 auto releaseProc = [](void* ctx) {
1556 int* index = (int*) ctx;
1557 *index = 1;
1558 };
Greg Danielc27eb722018-08-10 09:48:08 -04001559
Robert Phillipsddc21482019-10-16 14:30:09 -04001560 for (int i = 0; i < 3; ++i) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001561 backends[i] = dContext->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001562 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001563 wrapped[i] = gpu->wrapBackendTexture(backends[i],
Robert Phillipsddc21482019-10-16 14:30:09 -04001564 GrWrapOwnership::kBorrow_GrWrapOwnership,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001565 (i < 2) ? GrWrapCacheable::kYes : GrWrapCacheable::kNo,
1566 GrIOType::kRead_GrIOType)
1567 .release();
Robert Phillipsddc21482019-10-16 14:30:09 -04001568 wrapped[i]->setRelease(releaseProc, &freed[i]);
1569 }
1570
1571 cache->insertDelayedTextureUnref(wrapped[0]);
1572 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001573
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001574 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1575 // is because inserting it as a cross-context resource actually holds a ref until the
1576 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001577 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001578
Robert Phillipsddc21482019-10-16 14:30:09 -04001579 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001580
1581 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001582 wrapped[0]->unref();
1583 wrapped[1]->unref();
1584 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001585
Robert Phillipsddc21482019-10-16 14:30:09 -04001586 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001587
1588 // This should free nothing since no messages were sent.
1589 cache->purgeAsNeeded();
1590
Robert Phillipsddc21482019-10-16 14:30:09 -04001591 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1592
Greg Danielc27eb722018-08-10 09:48:08 -04001593 // Send message to free the first resource
Robert Phillipsd074b622021-03-15 08:49:24 -04001594 GrTextureFreedMessage msg1{wrapped[0], dContext->directContextID()};
1595 SkMessageBus<GrTextureFreedMessage, GrDirectContext::DirectContextID>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001596 cache->purgeAsNeeded();
1597
Robert Phillipsddc21482019-10-16 14:30:09 -04001598 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1599 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001600
Robert Phillipsd074b622021-03-15 08:49:24 -04001601 GrTextureFreedMessage msg2{wrapped[2], dContext->directContextID()};
1602 SkMessageBus<GrTextureFreedMessage, GrDirectContext::DirectContextID>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001603 cache->purgeAsNeeded();
1604
Robert Phillipsddc21482019-10-16 14:30:09 -04001605 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1606 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001607
1608 mock.reset();
1609
Robert Phillipsddc21482019-10-16 14:30:09 -04001610 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001611}
1612
Brian Salomondcfca432017-11-15 15:48:03 -05001613DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001614 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001615 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001616 test_purge_unlocked(reporter);
Greg Daniel1fd8ac82020-12-11 11:22:01 -05001617 test_purge_command_buffer_usage(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001618 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001619 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001620 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001621 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001622 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001623 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001624 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001625 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001626 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001627 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001628 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001629 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001630 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001631 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001632 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001633 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001634}
1635
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001636// This simulates a portion of Chrome's context abandonment processing.
1637// Please see: crbug.com/1011368 and crbug.com/1014993
1638DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
Robert Phillipsd074b622021-03-15 08:49:24 -04001639 auto dContext = ctxInfo.directContext();
1640 GrGpu* gpu = dContext->priv().getGpu();
1641 GrResourceCache* cache = dContext->priv().getResourceCache();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001642
Robert Phillipsd074b622021-03-15 08:49:24 -04001643 GrBackendTexture backend = dContext->createBackendTexture(16, 16,
1644 SkColorType::kRGBA_8888_SkColorType,
1645 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001646 GrTexture* tex = gpu->wrapBackendTexture(backend,
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001647 GrWrapOwnership::kBorrow_GrWrapOwnership,
1648 GrWrapCacheable::kYes,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001649 GrIOType::kRead_GrIOType)
1650 .release();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001651
1652 auto releaseProc = [](void* ctx) {
1653 int* index = (int*) ctx;
1654 *index = 1;
1655 };
1656
1657 int freed = 0;
1658
1659 tex->setRelease(releaseProc, &freed);
1660
1661 cache->insertDelayedTextureUnref(tex);
1662
1663 // Now only the cache is holding a ref to this texture
1664 tex->unref();
1665
1666 REPORTER_ASSERT(reporter, 0 == freed);
1667
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001668 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1669 // for all the backends for consistency.
Robert Phillipsd074b622021-03-15 08:49:24 -04001670 dContext->deleteBackendTexture(backend);
1671 dContext->abandonContext();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001672
1673 REPORTER_ASSERT(reporter, 1 == freed);
1674
1675 // In the past, creating this message could cause an exception due to
1676 // an un-safe downcast from GrTexture to GrGpuResource
Robert Phillipsd074b622021-03-15 08:49:24 -04001677 GrTextureFreedMessage msg{tex, dContext->directContextID()};
1678 SkMessageBus<GrTextureFreedMessage, GrDirectContext::DirectContextID>::Post(msg);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001679
Greg Danielf0e04f02019-12-04 15:17:54 -05001680 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillipsd074b622021-03-15 08:49:24 -04001681 dContext->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001682}
1683
Robert Phillipsd6214d42016-11-07 08:23:48 -05001684////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001685static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001686 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001687 SkISize dims,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001688 int sampleCnt) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001689 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomon7e67dca2020-07-21 09:27:25 -04001690 return provider->createTexture(dims, format, renderable, sampleCnt, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -04001691 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001692}
1693
Robert Phillipse94b4e12020-07-23 13:54:35 -04001694static sk_sp<GrTextureProxy> make_mipmap_proxy(GrRecordingContext* rContext,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001695 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001696 SkISize dims,
Robert Phillipse78b7252017-04-06 07:59:41 -04001697 int sampleCnt) {
Robert Phillipse94b4e12020-07-23 13:54:35 -04001698 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
1699 const GrCaps* caps = rContext->priv().caps();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001700
Robert Phillipsd6214d42016-11-07 08:23:48 -05001701
Robert Phillips0a15cc62019-07-30 12:49:10 -04001702 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1703 GrRenderable::kNo);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001704
Brian Salomon7e67dca2020-07-21 09:27:25 -04001705 return proxyProvider->createProxy(format, dims, renderable, sampleCnt, GrMipmapped::kYes,
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001706 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001707}
1708
1709// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1710// Texture-only, both-RT-and-Texture and MIPmapped
1711DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001712 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001713 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001714 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001715
Brian Salomona56a7462020-02-07 14:17:25 -05001716 static constexpr SkISize kSize = {64, 64};
1717 static constexpr auto kArea = kSize.area();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001718
Robert Phillipsd6214d42016-11-07 08:23:48 -05001719 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001720 {
1721 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001722
Brian Salomona56a7462020-02-07 14:17:25 -05001723 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001724 size_t size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001725 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001726
Greg Daniel6fa62e22019-08-07 15:52:37 -04001727 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001728 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001729 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001730 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001731 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001732 kArea*4 == size || // msaa4 failed
1733 kArea*4*sampleCount == size || // auto-resolving
1734 kArea*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001735 }
1736
Brian Salomona56a7462020-02-07 14:17:25 -05001737 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001738 size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001739 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001740 }
1741
Robert Phillipsd6214d42016-11-07 08:23:48 -05001742 // Mipmapped versions
Brian Salomon69100f02020-07-21 10:49:25 -04001743 if (caps->mipmapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001744 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001745
Brian Salomona56a7462020-02-07 14:17:25 -05001746 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, 1);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001747 size_t size = proxy->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001748 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001749
Greg Daniel6fa62e22019-08-07 15:52:37 -04001750 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001751 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001752 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, sampleCount);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001753 size = proxy->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001754 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001755 kArea*4 + (kArea*4)/3 == size || // msaa4 failed
1756 kArea*4*sampleCount + (kArea*4)/3 == size || // auto-resolving
1757 kArea*4*(sampleCount+1) + (kArea*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001758 }
Robert Phillips1b352562017-04-05 18:56:21 +00001759
Brian Salomona56a7462020-02-07 14:17:25 -05001760 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, 1);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001761 size = proxy->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001762 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001763 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001764}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001765
Adlai Hollere1c8a382021-04-08 15:30:12 -04001766DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PurgeToMakeHeadroom, reporter, ctxInfo) {
1767 constexpr size_t kTexSize = 16 * 16 * 4;
1768
1769 auto dContext = ctxInfo.directContext();
1770 dContext->setResourceCacheLimit(2 * kTexSize);
1771 auto resourceProvider = dContext->priv().resourceProvider();
1772 auto resourceCache = dContext->priv().getResourceCache();
1773 for (bool success : { true, false }) {
1774 reporter->push(SkString(success ? "success" : "failure"));
1775
1776 resourceCache->releaseAll();
1777 REPORTER_ASSERT(reporter, resourceCache->getBudgetedResourceBytes() == 0);
1778
1779 // Make one unpurgeable texture and one purgeable texture.
1780 auto lockedTex = make_normal_texture(resourceProvider, GrRenderable::kNo, {16, 16}, 1);
1781 REPORTER_ASSERT(reporter, lockedTex->gpuMemorySize() == kTexSize);
1782
1783 // N.b. this surface is renderable so "reuseScratchTextures = false" won't mess us up.
1784 auto purgeableTex = make_normal_texture(resourceProvider, GrRenderable::kYes, {16, 16}, 1);
1785 if (success) {
1786 purgeableTex = nullptr;
1787 }
1788
1789 size_t expectedPurgeable = success ? kTexSize : 0;
1790 REPORTER_ASSERT(reporter, expectedPurgeable == resourceCache->getPurgeableBytes(),
1791 "%zu vs %zu", expectedPurgeable, resourceCache->getPurgeableBytes());
1792 REPORTER_ASSERT(reporter, success == resourceCache->purgeToMakeHeadroom(kTexSize));
1793 size_t expectedBudgeted = success ? kTexSize : (2 * kTexSize);
1794 REPORTER_ASSERT(reporter, expectedBudgeted == resourceCache->getBudgetedResourceBytes(),
1795 "%zu vs %zu", expectedBudgeted, resourceCache->getBudgetedResourceBytes());
1796 reporter->pop();
1797 }
1798}
1799
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001800#if GR_GPU_STATS
1801DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001802 auto context = ctxInfo.directContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001803 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001804
1805 // Helper that determines if cache is overbudget.
1806 auto overbudget = [context] {
1807 int uNum;
1808 size_t uSize;
1809 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001810 size_t bSize = context->getResourceCacheLimit();
1811 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001812 };
1813
1814 // Helper that does a trivial draw to a surface.
1815 auto drawToSurf = [](SkSurface* surf) {
1816 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1817 };
1818
1819 // Helper that checks whether a flush has occurred between calls.
1820 int baseFlushCount = 0;
1821 auto getFlushCountDelta = [context, &baseFlushCount]() {
Greg Danielfe159622020-04-10 17:43:51 +00001822 int cur = context->priv().getGpu()->stats()->numSubmitToGpus();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001823 int delta = cur - baseFlushCount;
1824 baseFlushCount = cur;
1825 return delta;
1826 };
1827
1828 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1829 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1830 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1831
1832 drawToSurf(surf1.get());
1833 drawToSurf(surf2.get());
1834
1835 // Flush each surface once to ensure that their backing stores are allocated.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001836 surf1->flushAndSubmit();
1837 surf2->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001838 REPORTER_ASSERT(reporter, overbudget());
1839 getFlushCountDelta();
1840
1841 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1842 drawToSurf(surf1.get());
1843 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1844 drawToSurf(surf2.get());
1845 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1846 REPORTER_ASSERT(reporter, overbudget());
1847
1848 // Make surf1 purgeable. Drawing to surf2 should flush.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001849 surf1->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001850 surf1.reset();
1851 drawToSurf(surf2.get());
1852 REPORTER_ASSERT(reporter, getFlushCountDelta());
1853 REPORTER_ASSERT(reporter, overbudget());
1854}
1855#endif