blob: c07c488dc2c1198d70f61eb6f2519958ddd57cc3 [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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
bsalomon3f324322015-04-08 11:01:54 -07009
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrGpu.h"
13#include "src/gpu/GrGpuResourceCacheAccess.h"
14#include "src/gpu/GrGpuResourcePriv.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/GrResourceCache.h"
18#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/gpu/GrContextFactory.h"
Robert Phillips646e4292017-06-13 12:44:56 -040021
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkCanvas.h"
23#include "include/core/SkSurface.h"
Ben Wagner21bca282019-05-15 10:15:52 -040024#include "src/core/SkMessageBus.h"
Mike Reed13711eb2020-07-14 17:16:32 -040025#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/SkGr.h"
27#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040028#include "tests/TestUtils.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000029
Hal Canary8a001442018-09-19 11:31:27 -040030#include <thread>
31
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032static const int gWidth = 640;
33static const int gHeight = 480;
34
35////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070036DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040037 auto context = ctxInfo.directContext();
kkinnunen15302832015-12-01 04:35:26 -080038 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070039 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080040 SkCanvas* canvas = surface->getCanvas();
41
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000042 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
43
44 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000045 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000046 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040047 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000049 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070050 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000051
Robert Phillipscf39f372019-09-03 10:29:20 -040052 size_t oldMaxBytes = context->getResourceCacheLimit();
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000053
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054 // Set the cache limits so we can fit 10 "src" images and the
55 // max number of textures doesn't matter
56 size_t maxCacheSize = initialCacheSize + 10*srcSize;
Robert Phillipscf39f372019-09-03 10:29:20 -040057 context->setResourceCacheLimit(maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000058
59 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000060 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000061
62 for (int i = 0; i < 100; ++i) {
63 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040064 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000065
66 // "modify" the src texture
67 src.notifyPixelsChanged();
68
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000069 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070070 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000071
72 // we should never go over the size limit
73 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
74 }
75
Robert Phillipscf39f372019-09-03 10:29:20 -040076 context->setResourceCacheLimit(oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000077}
78
bsalomon11abd8d2016-10-14 08:13:48 -070079static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
80 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
81 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
82 return false;
83 }
84 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
85}
86
Robert Phillipsc0192e32017-09-21 12:00:26 -040087static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
88 return rt->renderTargetPriv().getStencilAttachment();
89}
90
91static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
92 int size, int sampleCount, SkBudgeted budgeted) {
Brian Salomon4eb38b72019-08-05 12:58:39 -040093 auto format =
94 provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
Brian Salomona56a7462020-02-07 14:17:25 -050095 sk_sp<GrTexture> tex(provider->createTexture({size, size}, format, GrRenderable::kYes,
96 sampleCount, GrMipMapped::kNo, budgeted,
97 GrProtected::kNo));
Robert Phillipsc0192e32017-09-21 12:00:26 -040098 if (!tex || !tex->asRenderTarget()) {
99 return nullptr;
100 }
101
Chris Daltoneffee202019-07-01 22:28:03 -0600102 if (!provider->attachStencilAttachment(tex->asRenderTarget(), sampleCount)) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400103 return nullptr;
104 }
105 SkASSERT(get_SB(tex->asRenderTarget()));
106
107 return sk_ref_sp(tex->asRenderTarget());
108}
109
bsalomon11abd8d2016-10-14 08:13:48 -0700110// This currently fails on ES3 ANGLE contexts
111DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000112 ctxInfo, nullptr) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400113 auto context = ctxInfo.directContext();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400114 const GrCaps* caps = context->priv().caps();
115
116 if (caps->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700117 return;
118 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400119
Robert Phillips9da87e02019-02-04 13:26:26 -0500120 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400121
Greg Daniel5c96db82019-07-09 14:06:58 -0400122 GrColorType grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400123 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400124
Brian Salomonbdecacf2018-02-02 20:32:49 -0500125 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400126 REPORTER_ASSERT(reporter, smallRT0);
127
128 {
129 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
131 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400132
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800134 }
135
Robert Phillipsc0192e32017-09-21 12:00:26 -0400136 {
137 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500138 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400139 REPORTER_ASSERT(reporter, smallRT2);
140
141 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 }
143
Robert Phillipsc0192e32017-09-21 12:00:26 -0400144 {
145 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500146 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400147 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800148
Robert Phillipsc0192e32017-09-21 12:00:26 -0400149 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800150 }
bsalomon02a44a42015-02-19 09:09:00 -0800151
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400152 int smallSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400153 context->priv().caps()->getRenderTargetSampleCount(2, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500154 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700155 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500156 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
157 smallSampleCount, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400158 REPORTER_ASSERT(reporter, smallMSAART0);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400159
160 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
161
162 {
163 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500164 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
165 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400166 SkBudgeted::kNo);
167 REPORTER_ASSERT(reporter, smallMSAART1);
168
169 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800170 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400171
Brian Salomonbdecacf2018-02-02 20:32:49 -0500172 // But one with a larger sample count should not. (Also check that the two requests didn't
173 // rounded up to the same actual sample count or else they could share.).
Greg Daniel6fa62e22019-08-07 15:52:37 -0400174 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(5, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500175 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500176 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
177 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400178 SkBudgeted::kNo);
179 REPORTER_ASSERT(reporter, smallMSAART2);
180
181 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800182 }
183 }
184}
185
bsalomon68d91342016-04-12 09:59:58 -0700186DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400187 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500188 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
189 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700190 // this test is only valid for GL
191 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700192 return;
193 }
194
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500195 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700196 static const int kW = 100;
197 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700198
Greg Danielc1ad77c2020-05-06 11:40:03 -0400199 CreateBackendTexture(context, &backendTextures[0], kW, kH, kRGBA_8888_SkColorType,
200 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo,
201 GrProtected::kNo);
202 CreateBackendTexture(context, &backendTextures[1], kW, kH, kRGBA_8888_SkColorType,
203 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo,
204 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500205 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
206 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
207 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
208 return;
209 }
jvanverth672bb7f2015-07-13 07:19:57 -0700210
bsalomon6dc6f5f2015-06-18 09:12:16 -0700211 context->resetContext();
212
Robert Phillips6be756b2018-01-16 15:07:54 -0500213 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400214 backendTextures[0], kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700215
Robert Phillips6be756b2018-01-16 15:07:54 -0500216 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400217 backendTextures[1], kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218
Brian Osman85d34b22017-05-10 12:06:26 -0400219 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
220 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221 return;
222 }
223
halcanary96fcdcc2015-08-27 07:41:13 -0700224 borrowed.reset(nullptr);
225 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700226
Greg Daniel0a2464f2020-05-14 15:45:44 -0400227 context->flushAndSubmit();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500229 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
230 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
232 REPORTER_ASSERT(reporter, borrowedIsAlive);
233 REPORTER_ASSERT(reporter, !adoptedIsAlive);
234
Brian Salomone64b0642018-03-07 11:47:54 -0500235 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400236 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500237 }
238 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400239 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500240 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241
242 context->resetContext();
243}
244
bsalomon6d3fe022014-07-25 08:35:45 -0700245class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800246 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000247public:
robertphillips6e83ac72015-08-13 05:19:14 -0700248 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700249
bsalomon1c60dfe2015-01-21 09:32:40 -0800250 /** Property that distinctly categorizes the resource.
251 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800252 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800253
kkinnunen2e6055b2016-04-22 01:48:29 -0700254 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
255 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700256 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700258 , fProperty(kA_SimulatedProperty)
259 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800260 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700261 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800262 }
263
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400265 SimulatedProperty property, size_t size = kDefaultSize) {
266 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800267 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500268 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
269 size_t size = kDefaultSize) {
270 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000271 }
272
Brian Salomond3b65972017-03-22 12:05:03 -0400273 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800274 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000275 }
276
bsalomon33435572014-11-05 14:47:41 -0800277 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400279 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
280 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
bsalomon1c60dfe2015-01-21 09:32:40 -0800283 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
284 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
285 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800286 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
287 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800288 }
289 }
290
291 static size_t ExpectedScratchKeySize() {
292 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
293 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294private:
bsalomon24db3b12015-01-23 04:24:04 -0800295 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800296
Greg Danielda86e282018-06-13 09:41:19 -0400297 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
298 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700299 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700300 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400301 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700302 , fProperty(property)
303 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800304 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 this->registerWithCache(budgeted);
306 }
307
308 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500309 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
310 : INHERITED(gpu)
311 , fToDelete(nullptr)
312 , fSize(size)
313 , fProperty(kA_SimulatedProperty)
314 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500316 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700317 }
318
319 void computeScratchKey(GrScratchKey* key) const override {
320 if (fIsScratch) {
321 ComputeScratchKey(fProperty, key);
322 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800323 }
324
mtklein36352bf2015-03-25 18:17:31 -0700325 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400326 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800327
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400328 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000329 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800330 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800331 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700332 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700333 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000334};
bsalomon33435572014-11-05 14:47:41 -0800335int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000336
bsalomonc2f35b72015-01-23 07:19:22 -0800337class Mock {
338public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400339 Mock(size_t maxBytes) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400340 fContext = GrDirectContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800341 SkASSERT(fContext);
Robert Phillipscf39f372019-09-03 10:29:20 -0400342 fContext->setResourceCacheLimit(maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500343 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800344 cache->purgeAllUnlocked();
345 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800346 }
bsalomonc2f35b72015-01-23 07:19:22 -0800347
Robert Phillips9da87e02019-02-04 13:26:26 -0500348 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800349
Hal Canary342b7ac2016-11-04 11:49:42 -0400350 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800351
Greg Danielc27eb722018-08-10 09:48:08 -0400352 void reset() {
353 fContext.reset();
354 }
355
bsalomonc2f35b72015-01-23 07:19:22 -0800356private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400357 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800358};
359
360static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400361 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800362 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800363 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500364 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800365
366 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400367 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
368 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
369 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
370 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800371
372 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800373 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800374 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800375 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800378 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800379 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
380
bsalomon8718aaf2015-02-19 07:24:21 -0800381 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 a->unref();
384 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800387 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 c->unref();
390 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800392 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800393 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 d->unref();
396 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
398 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399
400 b->unref();
401 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
403 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404}
405
bsalomon24db3b12015-01-23 04:24:04 -0800406// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500407template <int>
408static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800409 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500410 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800411 builder[0] = data;
412}
413
Robert Phillips6eba0632018-03-28 12:25:42 -0400414static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400415 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400416 GrContext* context = mock.context();
417 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500418 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400419
420 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
421 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400422 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400423
424 GrUniqueKey uniqueKey;
425 make_unique_key<0>(&uniqueKey, 0);
426
427 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400428 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400429 b->resourcePriv().setUniqueKey(uniqueKey);
430
431 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400432 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400433
434 GrUniqueKey uniqueKey2;
435 make_unique_key<0>(&uniqueKey2, 1);
436
437 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400438 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400439 d->resourcePriv().setUniqueKey(uniqueKey2);
440
441
442 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
443 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
444 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
445 d->gpuMemorySize() == cache->getResourceBytes());
446
447 // Should be safe to purge without deleting the resources since we still have refs.
448 cache->purgeUnlockedResources(false);
449 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
450
451 // Unref them all. Since they all have keys they should remain in the cache.
452
453 a->unref();
454 b->unref();
455 c->unref();
456 d->unref();
457 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
458 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
459 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
460 d->gpuMemorySize() == cache->getResourceBytes());
461
462 // Purge only the two scratch resources
463 cache->purgeUnlockedResources(true);
464
465 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
466 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
467 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
468 cache->getResourceBytes());
469
470 // Purge the uniquely keyed resources
471 cache->purgeUnlockedResources(false);
472
473 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
474 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
475 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
476}
477
bsalomon84c8e622014-11-17 09:33:27 -0800478static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400479 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800480 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800481 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500482 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800483
bsalomon8718aaf2015-02-19 07:24:21 -0800484 GrUniqueKey uniqueKey;
485 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800486
bsalomon8718aaf2015-02-19 07:24:21 -0800487 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800488 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400489 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
490 10);
491 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800492 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500493 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
494 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
495 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800496
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500497 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800498 GrUniqueKey uniqueKey2;
499 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500500 GrUniqueKey uniqueKey3;
501 make_unique_key<0>(&uniqueKey3, 2);
502 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
503 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
504 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
505 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
506 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
507 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400508
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500509 // Remove the extra refs we just added.
510 SkSafeUnref(wrappedCacheableViaKey);
511 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800512
513 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500514 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800515 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500516 wrappedCacheable->gpuMemorySize() +
517 wrappedUncacheable->gpuMemorySize() +
518 unbudgeted->gpuMemorySize() ==
519 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800520 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800521 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800522 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400523 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800524
bsalomon63c992f2015-01-23 12:47:59 -0800525 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800526 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500527 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800528 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500529 wrappedCacheable->gpuMemorySize() +
530 wrappedUncacheable->gpuMemorySize() +
531 unbudgeted->gpuMemorySize() ==
532 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800533 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800534 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800535 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400536 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800537
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500538 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
539 // However, unreffing the uncacheable wrapped resource should free it.
540 wrappedCacheable->unref();
541 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400542 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800543 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500544 wrappedCacheable->gpuMemorySize() +
545 unbudgeted->gpuMemorySize() ==
546 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500547 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800548
bsalomon84c8e622014-11-17 09:33:27 -0800549 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500550 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800551 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500552 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
553 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
554 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800555 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500556 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500557
558 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
559 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
560 if (wrappedCacheableViaKey) {
561 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
562 wrappedCacheable->unref();
563 }
564 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
565 // resource should immediately delete it.
566 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
567
568 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500569 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
570 wrappedUncacheable->gpuMemorySize() +
571 unbudgeted->gpuMemorySize() ==
572 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800573 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
574 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400575 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800576
577 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400578 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800579 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500580 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
581 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
582 wrappedUncacheable->gpuMemorySize() ==
583 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800584 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400586 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800587
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500588 // Unreffing the wrapped resources (with no unique key) should free them right away.
589 wrappedUncacheable->unref();
590 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800591 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
592 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
593 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
594 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400595 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800596
597 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
599 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
600 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
601 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400602 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800603}
604
bsalomon5236cf42015-01-14 10:42:08 -0800605static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400606 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800607 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800608 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500609 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800610
bsalomon8718aaf2015-02-19 07:24:21 -0800611 GrUniqueKey uniqueKey;
612 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800613
614 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800615 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800616 TestResource* wrapped;
617 TestResource* unbudgeted;
618
619 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500620 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400621 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700622
bsalomon5236cf42015-01-14 10:42:08 -0800623 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800624 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
625 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
626 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
627 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400628 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800629
Greg Danielda86e282018-06-13 09:41:19 -0400630 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800631 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800632 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800633 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
634 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
635 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
636 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400637 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800638
bsalomon0ea80f42015-02-11 10:49:59 -0800639 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500640 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800641 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
642 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
643 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
644 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400645 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800646
647 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800648 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
649 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
650 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
651 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400652 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800653
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500654 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800655 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
656 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
657 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
658 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400659 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800660
661 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800662 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
663 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
664 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
665 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400666 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800667
bsalomon0ea80f42015-02-11 10:49:59 -0800668 cache->purgeAllUnlocked();
669 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
670 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
671 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
672 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400673 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800674}
675
bsalomon3582d3e2015-02-13 14:20:05 -0800676// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
677void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
678/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400679 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800680 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800681 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500682 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800683
684 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500685 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800686 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800687 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800688
689 size_t size = resource->gpuMemorySize();
690 for (int i = 0; i < 2; ++i) {
691 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800692 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800693 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500694 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500695 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400696 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800697 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
698 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
699 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
700 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400701 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800702
703 // Once it is unrefed, it should become available as scratch.
704 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800705 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
706 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
707 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
708 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400709 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400710 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800711 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800712 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800713 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500714 REPORTER_ASSERT(reporter,
715 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800716
717 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700718 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800719 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800720 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800721 } else {
722 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800723 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800724 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
725 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
726 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
727 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400728 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800729 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800730 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500731 REPORTER_ASSERT(reporter,
732 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800733
734 // now when it is unrefed it should die since it has no key.
735 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800736 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
737 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
738 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
739 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400740 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800741 }
bsalomon8b79d232014-11-10 10:19:06 -0800742 }
bsalomonc2f35b72015-01-23 07:19:22 -0800743}
744
745static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400746 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800747 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800748 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500749 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800750
bsalomon8b79d232014-11-10 10:19:06 -0800751 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500752 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700753 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400754 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500755 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700756 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400757 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800759 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800760 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400761 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800762
763 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800764 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800765
bsalomon0ea80f42015-02-11 10:49:59 -0800766 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800767 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800768 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
769 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800770 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800771 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800772
bsalomon63c992f2015-01-23 12:47:59 -0800773 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800774 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800775 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800776 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800777
778 // Unref but don't purge
779 a->unref();
780 b->unref();
781 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800782 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800783
bsalomon63c992f2015-01-23 12:47:59 -0800784 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800785 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800786 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800787 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
788 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800789}
790
bsalomon10e23ca2014-11-25 05:52:06 -0800791static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400792 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800793 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800794 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500795 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800796
bsalomon10e23ca2014-11-25 05:52:06 -0800797 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500798 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800799 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500800 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800801 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800802 a->unref();
803 b->unref();
804
bsalomon1c60dfe2015-01-21 09:32:40 -0800805 GrScratchKey scratchKey;
806 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800807 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800808 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400809 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800810
bsalomon0ea80f42015-02-11 10:49:59 -0800811 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800812 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800813 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800814 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
815 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800816
817 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400818 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800819 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800820 // It's still alive, but not cached by scratch key anymore
821 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800822 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
823 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800824
825 // The cache should immediately delete it when it's unrefed since it isn't accessible.
826 find->unref();
827 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800828 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
829 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800830
831 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400832 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800833 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800834 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800835 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
836 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800837
838 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800839 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800840 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800841 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
842 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800843
844 find->unref();
845 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800846 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
847 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800848}
849
bsalomon1c60dfe2015-01-21 09:32:40 -0800850static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400851 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800852 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800853 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500854 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800855
856 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500857 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800858 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500859 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800860 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800861 a->unref();
862 b->unref();
863
864 GrScratchKey scratchKey;
865 // Ensure that scratch key comparison and assignment is consistent.
866 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800867 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800868 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800869 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800870 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
871 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
872 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
873 scratchKey = scratchKey1;
874 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
875 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
876 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
877 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
878 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
879 scratchKey = scratchKey2;
880 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
881 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
882 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
883 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
884 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
885
886 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800887 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800888 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400889 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800890
891 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800892 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400893 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700894 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800895 find->unref();
896
897 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400898 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700899 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800900 REPORTER_ASSERT(reporter, find == a || find == b);
901
Robert Phillipsaee18c92019-09-06 11:48:27 -0400902 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700903 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800904 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
905 REPORTER_ASSERT(reporter, find2 != find);
906 find2->unref();
907 find->unref();
908}
909
bsalomon8718aaf2015-02-19 07:24:21 -0800910static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400911 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800912 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800913 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500914 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000915
bsalomon8718aaf2015-02-19 07:24:21 -0800916 GrUniqueKey key;
917 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700918
bsalomon8718aaf2015-02-19 07:24:21 -0800919 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400920 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700921
bsalomonf99e9612015-02-19 08:24:16 -0800922 // Set key on resource a.
923 a->resourcePriv().setUniqueKey(key);
924 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
925 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800926
bsalomonf99e9612015-02-19 08:24:16 -0800927 // Make sure that redundantly setting a's key works.
928 a->resourcePriv().setUniqueKey(key);
929 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800930 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800931 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
932 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800933 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
934
bsalomonf99e9612015-02-19 08:24:16 -0800935 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400936 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800937 b->resourcePriv().setUniqueKey(key);
938 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
939 b->unref();
940
941 // Still have two resources because a is still reffed.
942 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
943 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
944 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
945
946 a->unref();
947 // Now a should be gone.
948 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
949 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
950 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
951
952 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
953 // Also make b be unreffed when replacement occurs.
954 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400955 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800956 GrUniqueKey differentKey;
957 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800958 c->resourcePriv().setUniqueKey(differentKey);
959 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
960 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
961 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
962 // c replaces b and b should be immediately purged.
963 c->resourcePriv().setUniqueKey(key);
964 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
965 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
966 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
967
968 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800969 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800970 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
971 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
972 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
973
974 // Drop the ref on c, it should be kept alive because it has a unique key.
975 c->unref();
976 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
977 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
978 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
979
980 // Verify that we can find c, then remove its unique key. It should get purged immediately.
981 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
982 c->resourcePriv().removeUniqueKey();
983 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800984 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
985 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800986 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700987
988 {
989 GrUniqueKey key2;
990 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500991 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700992 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700993 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700994 d->resourcePriv().setUniqueKey(key2);
995 }
996
997 GrUniqueKey key3;
998 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400999 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001000 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001001}
1002
bsalomon8b79d232014-11-10 10:19:06 -08001003static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001004 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001005 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001006 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001007 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001008
bsalomon8718aaf2015-02-19 07:24:21 -08001009 GrUniqueKey key1, key2, key3;
1010 make_unique_key<0>(&key1, 1);
1011 make_unique_key<0>(&key2, 2);
1012 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001013
bsalomon23e619c2015-02-06 11:54:28 -08001014 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001015 TestResource* a = new TestResource(gpu);
1016 TestResource* b = new TestResource(gpu);
1017 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001018 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001019 a->resourcePriv().setUniqueKey(key1);
1020 b->resourcePriv().setUniqueKey(key2);
1021 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001022 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001023 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001024 c->unref();
1025
bsalomon8718aaf2015-02-19 07:24:21 -08001026 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1027 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1028 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001029 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001030
bsalomon8718aaf2015-02-19 07:24:21 -08001031 typedef GrUniqueKeyInvalidatedMessage Msg;
1032 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001033
1034 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001035 Bus::Post(Msg(key1, context->priv().contextID()));
1036 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001037 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001038 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001039 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1040 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001041 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001042 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001043
1044 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001045 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001046 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001047 // we still have a ref on b, c should be recycled as scratch.
1048 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001049 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001050
bsalomon23e619c2015-02-06 11:54:28 -08001051 // make b purgeable. It should be immediately deleted since it has no key.
1052 b->unref();
1053 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1054
1055 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1056 GrScratchKey scratchKey;
1057 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001058 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001059 REPORTER_ASSERT(reporter, scratch == c);
1060 SkSafeUnref(scratch);
1061
1062 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001063 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001064 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001065 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001066 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1067 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001068 REPORTER_ASSERT(reporter, !scratch);
1069 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001070}
1071
bsalomon71cb0c22014-11-14 12:10:14 -08001072static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001073 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001074 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001075 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001076 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001077
bsalomon8718aaf2015-02-19 07:24:21 -08001078 GrUniqueKey key1, key2;
1079 make_unique_key<0>(&key1, 1);
1080 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001081
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001082 sk_sp<TestResource> a(new TestResource(gpu));
1083 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001084 a->resourcePriv().setUniqueKey(key1);
1085 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001086
bsalomonc2f35b72015-01-23 07:19:22 -08001087 // Make a cycle
1088 a->setUnrefWhenDestroyed(b);
1089 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001090
bsalomonc2f35b72015-01-23 07:19:22 -08001091 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001092
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001093 TestResource* unownedA = a.release();
1094 unownedA->unref();
1095 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001096
bsalomonc2f35b72015-01-23 07:19:22 -08001097 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001098
bsalomon0ea80f42015-02-11 10:49:59 -08001099 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001100 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001101
bsalomonc2f35b72015-01-23 07:19:22 -08001102 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001103 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001104 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001105
bsalomon0ea80f42015-02-11 10:49:59 -08001106 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001107 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001108}
1109
bsalomonddf30e62015-02-19 11:38:44 -08001110static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1111 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001112 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001113 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001114
1115 SkRandom random;
1116
1117 // Run the test 2*kCount times;
1118 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001119 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001120 GrContext* context = mock.context();
1121 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001122 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001123
1124 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001125 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001126
Robert Phillipscf39f372019-09-03 10:29:20 -04001127 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001128
1129 SkTDArray<int> shouldPurgeIdxs;
1130 int purgeableCnt = 0;
1131 SkTDArray<GrGpuResource*> resourcesToUnref;
1132
1133 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1134 // unpurgeable resources.
1135 for (int j = 0; j < kCount; ++j) {
1136 GrUniqueKey key;
1137 make_unique_key<0>(&key, j);
1138
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001139 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001140 r->resourcePriv().setUniqueKey(key);
1141 if (random.nextU() % kLockedFreq) {
1142 // Make this is purgeable.
1143 r->unref();
1144 ++purgeableCnt;
1145 if (purgeableCnt <= kNumToPurge) {
1146 *shouldPurgeIdxs.append() = j;
1147 }
1148 } else {
1149 *resourcesToUnref.append() = r;
1150 }
1151 }
1152
1153 // Verify that the correct resources were purged.
1154 int currShouldPurgeIdx = 0;
1155 for (int j = 0; j < kCount; ++j) {
1156 GrUniqueKey key;
1157 make_unique_key<0>(&key, j);
1158 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1159 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1160 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1161 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001162 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001163 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001164 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001165 }
1166 SkSafeUnref(res);
1167 }
1168
1169 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1170 resourcesToUnref[j]->unref();
1171 }
1172 }
1173}
1174
Brian Salomon5e150852017-03-22 14:53:13 -04001175static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001176 Mock mock(1000000);
Brian Salomon5e150852017-03-22 14:53:13 -04001177 GrContext* context = mock.context();
1178 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001179 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001180
1181 static constexpr int kCnts[] = {1, 10, 1024};
1182 auto nowish = []() {
1183 // We sleep so that we ensure we get a value that is greater than the last call to
1184 // GrStdSteadyClock::now().
1185 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1186 auto result = GrStdSteadyClock::now();
1187 // Also sleep afterwards so we don't get this value again.
1188 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1189 return result;
1190 };
1191
1192 for (int cnt : kCnts) {
1193 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1194 new GrStdSteadyClock::time_point[cnt]);
1195 {
1196 // Insert resources and get time points between each addition.
1197 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001198 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001199 GrUniqueKey k;
1200 make_unique_key<1>(&k, i);
1201 r->resourcePriv().setUniqueKey(k);
1202 r->unref();
1203 timeStamps.get()[i] = nowish();
1204 }
1205
1206 // Purge based on the time points between resource additions. Each purge should remove
1207 // the oldest resource.
1208 for (int i = 0; i < cnt; ++i) {
1209 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1210 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1211 for (int j = 0; j < i; ++j) {
1212 GrUniqueKey k;
1213 make_unique_key<1>(&k, j);
1214 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1215 REPORTER_ASSERT(reporter, !SkToBool(r));
1216 SkSafeUnref(r);
1217 }
1218 }
1219
1220 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1221 cache->purgeAllUnlocked();
1222 }
1223
1224 // Do a similar test but where we leave refs on some resources to prevent them from being
1225 // purged.
1226 {
1227 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1228 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001229 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001230 GrUniqueKey k;
1231 make_unique_key<1>(&k, i);
1232 r->resourcePriv().setUniqueKey(k);
1233 // Leave a ref on every other resource, beginning with the first.
1234 if (SkToBool(i & 0x1)) {
1235 refedResources.get()[i / 2] = r;
1236 } else {
1237 r->unref();
1238 }
1239 timeStamps.get()[i] = nowish();
1240 }
1241
1242 for (int i = 0; i < cnt; ++i) {
1243 // Should get a resource purged every other frame.
1244 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1245 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1246 }
1247
1248 // Unref all the resources that we kept refs on in the first loop.
1249 for (int i = 0; i < (cnt / 2); ++i) {
1250 refedResources.get()[i]->unref();
1251 cache->purgeResourcesNotUsedSince(nowish());
1252 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1253 }
1254
1255 cache->purgeAllUnlocked();
1256 }
1257
1258 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1259
1260 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1261 // eviction
Greg Daniel0a2464f2020-05-14 15:45:44 -04001262 context->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001263 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001264 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001265 GrUniqueKey k;
1266 make_unique_key<1>(&k, i);
1267 r->resourcePriv().setUniqueKey(k);
1268 r->unref();
1269 }
1270 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
Greg Daniel0a2464f2020-05-14 15:45:44 -04001271 context->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001272 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1273 cache->purgeResourcesNotUsedSince(nowish());
1274 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1275 }
1276}
1277
Derek Sollenberger5480a182017-05-25 16:43:59 -04001278static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001279 Mock mock(100);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001280 GrContext* context = mock.context();
1281 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001282 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001283
1284 enum TestsCase {
1285 kOnlyScratch_TestCase = 0,
1286 kPartialScratch_TestCase = 1,
1287 kAllScratch_TestCase = 2,
1288 kPartial_TestCase = 3,
1289 kAll_TestCase = 4,
1290 kNone_TestCase = 5,
1291 kEndTests_TestCase = kNone_TestCase + 1
1292 };
1293
1294 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1295
1296 GrUniqueKey key1, key2, key3;
1297 make_unique_key<0>(&key1, 1);
1298 make_unique_key<0>(&key2, 2);
1299 make_unique_key<0>(&key3, 3);
1300
1301 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001302 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1303 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1304 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001305
1306 unique1->resourcePriv().setUniqueKey(key1);
1307 unique2->resourcePriv().setUniqueKey(key2);
1308 unique3->resourcePriv().setUniqueKey(key3);
1309
Derek Sollenberger5480a182017-05-25 16:43:59 -04001310 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001311 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001312 TestResource::kA_SimulatedProperty,
1313 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001314 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001315 TestResource::kB_SimulatedProperty,
1316 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001317
1318 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1319 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1320 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1321
1322 // Add resources to the purgeable queue
1323 unique1->unref();
1324 scratch1->unref();
1325 unique2->unref();
1326 scratch2->unref();
1327 unique3->unref();
1328
1329 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1330 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1331 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1332
1333 switch(testCase) {
1334 case kOnlyScratch_TestCase: {
1335 context->purgeUnlockedResources(14, true);
1336 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1337 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1338 break;
1339 }
1340 case kPartialScratch_TestCase: {
1341 context->purgeUnlockedResources(3, true);
1342 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1343 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1344 break;
1345 }
1346 case kAllScratch_TestCase: {
1347 context->purgeUnlockedResources(50, true);
1348 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1349 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1350 break;
1351 }
1352 case kPartial_TestCase: {
1353 context->purgeUnlockedResources(13, false);
1354 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1355 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1356 break;
1357 }
1358 case kAll_TestCase: {
1359 context->purgeUnlockedResources(50, false);
1360 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1361 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1362 break;
1363 }
1364 case kNone_TestCase: {
1365 context->purgeUnlockedResources(0, true);
1366 context->purgeUnlockedResources(0, false);
1367 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1368 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1369 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1370 break;
1371 }
Brian Salomon23356442018-11-30 15:33:19 -05001372 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001373
1374 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001375 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001376 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1377 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1378
1379 }
1380}
1381
senorblanco84cd6212015-08-04 10:01:58 -07001382static void test_custom_data(skiatest::Reporter* reporter) {
1383 GrUniqueKey key1, key2;
1384 make_unique_key<0>(&key1, 1);
1385 make_unique_key<0>(&key2, 2);
1386 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001387 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001388 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1389 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1390
1391 // Test that copying a key also takes a ref on its custom data.
1392 GrUniqueKey key3 = key1;
1393 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1394}
1395
bsalomonc6363ef2015-09-24 07:07:40 -07001396static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001397 Mock mock(300);
bsalomonc6363ef2015-09-24 07:07:40 -07001398 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001399 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001400
1401 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001402 context->abandonContext();
1403
1404 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1405
1406 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1407
robertphillips8abb3702016-08-31 14:04:06 -07001408 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001409 resource->getUniqueKey();
1410 resource->wasDestroyed();
1411 resource->gpuMemorySize();
1412 resource->getContext();
1413
bsalomonc6363ef2015-09-24 07:07:40 -07001414 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001415 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001416 resource->resourcePriv().makeBudgeted();
1417 resource->resourcePriv().makeUnbudgeted();
1418 resource->resourcePriv().removeScratchKey();
1419 GrUniqueKey key;
1420 make_unique_key<0>(&key, 1);
1421 resource->resourcePriv().setUniqueKey(key);
1422 resource->resourcePriv().removeUniqueKey();
1423}
1424
Brian Salomon1090da62017-01-06 12:04:19 -05001425static void test_tags(skiatest::Reporter* reporter) {
1426#ifdef SK_DEBUG
1427 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1428 static constexpr int kLastTagIdx = 10;
1429 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1430
Robert Phillipscf39f372019-09-03 10:29:20 -04001431 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001432 GrContext* context = mock.context();
1433 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001434 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001435
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001436 // tag strings are expected to be long lived
1437 std::vector<SkString> tagStrings;
1438
Brian Salomon1090da62017-01-06 12:04:19 -05001439 SkString tagStr;
1440 int tagIdx = 0;
1441 int currTagCnt = 0;
1442
1443 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001444
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001445 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001446 GrUniqueKey key;
1447 if (currTagCnt == tagIdx) {
1448 tagIdx += 1;
1449 currTagCnt = 0;
1450 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001451 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001452 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001453 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001454 resource->resourcePriv().setUniqueKey(key);
1455 }
1456 SkASSERT(kLastTagIdx == tagIdx);
1457 SkASSERT(currTagCnt == kLastTagIdx);
1458
1459 // Test i = 0 to exercise unused tag string.
1460 for (int i = 0; i <= kLastTagIdx; ++i) {
1461 tagStr.printf("tag%d", i);
1462 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1463 }
1464#endif
1465}
1466
Robert Phillipsddc21482019-10-16 14:30:09 -04001467static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001468 Mock mock(30000);
Greg Danielc27eb722018-08-10 09:48:08 -04001469 GrContext* context = mock.context();
1470 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001471 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001472
Robert Phillipsddc21482019-10-16 14:30:09 -04001473 GrBackendTexture backends[3];
1474 GrTexture* wrapped[3];
1475 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001476
Robert Phillipsddc21482019-10-16 14:30:09 -04001477 auto releaseProc = [](void* ctx) {
1478 int* index = (int*) ctx;
1479 *index = 1;
1480 };
Greg Danielc27eb722018-08-10 09:48:08 -04001481
Robert Phillipsddc21482019-10-16 14:30:09 -04001482 for (int i = 0; i < 3; ++i) {
1483 backends[i] = context->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
1484 GrMipMapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001485 wrapped[i] = gpu->wrapBackendTexture(backends[i],
Robert Phillipsddc21482019-10-16 14:30:09 -04001486 GrWrapOwnership::kBorrow_GrWrapOwnership,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001487 (i < 2) ? GrWrapCacheable::kYes : GrWrapCacheable::kNo,
1488 GrIOType::kRead_GrIOType)
1489 .release();
Robert Phillipsddc21482019-10-16 14:30:09 -04001490 wrapped[i]->setRelease(releaseProc, &freed[i]);
1491 }
1492
1493 cache->insertDelayedTextureUnref(wrapped[0]);
1494 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001495
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001496 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1497 // is because inserting it as a cross-context resource actually holds a ref until the
1498 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001499 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001500
Robert Phillipsddc21482019-10-16 14:30:09 -04001501 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001502
1503 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001504 wrapped[0]->unref();
1505 wrapped[1]->unref();
1506 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001507
Robert Phillipsddc21482019-10-16 14:30:09 -04001508 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001509
1510 // This should free nothing since no messages were sent.
1511 cache->purgeAsNeeded();
1512
Robert Phillipsddc21482019-10-16 14:30:09 -04001513 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1514
Greg Danielc27eb722018-08-10 09:48:08 -04001515 // Send message to free the first resource
Robert Phillipsddc21482019-10-16 14:30:09 -04001516 GrTextureFreedMessage msg1{wrapped[0], context->priv().contextID()};
1517 SkMessageBus<GrTextureFreedMessage>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001518 cache->purgeAsNeeded();
1519
Robert Phillipsddc21482019-10-16 14:30:09 -04001520 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1521 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001522
Robert Phillipsddc21482019-10-16 14:30:09 -04001523 GrTextureFreedMessage msg2{wrapped[2], context->priv().contextID()};
1524 SkMessageBus<GrTextureFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001525 cache->purgeAsNeeded();
1526
Robert Phillipsddc21482019-10-16 14:30:09 -04001527 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1528 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001529
1530 mock.reset();
1531
Robert Phillipsddc21482019-10-16 14:30:09 -04001532 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001533}
1534
Brian Salomondcfca432017-11-15 15:48:03 -05001535DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001536 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001537 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001538 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001539 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001540 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001541 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001542 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001543 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001544 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001545 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001546 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001547 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001548 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001549 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001550 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001551 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001552 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001553 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001554 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001555}
1556
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001557// This simulates a portion of Chrome's context abandonment processing.
1558// Please see: crbug.com/1011368 and crbug.com/1014993
1559DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001560 auto context = ctxInfo.directContext();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001561 GrGpu* gpu = context->priv().getGpu();
1562 GrResourceCache* cache = context->priv().getResourceCache();
1563
1564 GrBackendTexture backend = context->createBackendTexture(16, 16,
1565 SkColorType::kRGBA_8888_SkColorType,
1566 GrMipMapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001567 GrTexture* tex = gpu->wrapBackendTexture(backend,
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001568 GrWrapOwnership::kBorrow_GrWrapOwnership,
1569 GrWrapCacheable::kYes,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001570 GrIOType::kRead_GrIOType)
1571 .release();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001572
1573 auto releaseProc = [](void* ctx) {
1574 int* index = (int*) ctx;
1575 *index = 1;
1576 };
1577
1578 int freed = 0;
1579
1580 tex->setRelease(releaseProc, &freed);
1581
1582 cache->insertDelayedTextureUnref(tex);
1583
1584 // Now only the cache is holding a ref to this texture
1585 tex->unref();
1586
1587 REPORTER_ASSERT(reporter, 0 == freed);
1588
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001589 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1590 // for all the backends for consistency.
1591 context->deleteBackendTexture(backend);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001592 context->abandonContext();
1593
1594 REPORTER_ASSERT(reporter, 1 == freed);
1595
1596 // In the past, creating this message could cause an exception due to
1597 // an un-safe downcast from GrTexture to GrGpuResource
1598 GrTextureFreedMessage msg{tex, context->priv().contextID()};
1599 SkMessageBus<GrTextureFreedMessage>::Post(msg);
1600
Greg Danielf0e04f02019-12-04 15:17:54 -05001601 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001602 context->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001603}
1604
Robert Phillipsd6214d42016-11-07 08:23:48 -05001605////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001606static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001607 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001608 SkISize dims,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001609 int sampleCnt) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001610 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomona56a7462020-02-07 14:17:25 -05001611 return provider->createTexture(dims, format, renderable, sampleCnt, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -04001612 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001613}
1614
Brian Salomona56a7462020-02-07 14:17:25 -05001615static sk_sp<GrTextureProxy> make_mipmap_proxy(GrContext* context,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001616 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001617 SkISize dims,
Robert Phillipse78b7252017-04-06 07:59:41 -04001618 int sampleCnt) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001619 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1620 const GrCaps* caps = context->priv().caps();
1621
Robert Phillipsd6214d42016-11-07 08:23:48 -05001622
Robert Phillips0a15cc62019-07-30 12:49:10 -04001623 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1624 GrRenderable::kNo);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001625
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001626 return proxyProvider->createProxy(format, dims, renderable, sampleCnt, GrMipMapped::kYes,
1627 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628}
1629
1630// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1631// Texture-only, both-RT-and-Texture and MIPmapped
1632DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001633 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001634 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001635 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001636
Brian Salomona56a7462020-02-07 14:17:25 -05001637 static constexpr SkISize kSize = {64, 64};
1638 static constexpr auto kArea = kSize.area();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001639
Robert Phillipsd6214d42016-11-07 08:23:48 -05001640 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001641 {
1642 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001643
Brian Salomona56a7462020-02-07 14:17:25 -05001644 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001645 size_t size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001646 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001647
Greg Daniel6fa62e22019-08-07 15:52:37 -04001648 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001649 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001650 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001651 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001652 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001653 kArea*4 == size || // msaa4 failed
1654 kArea*4*sampleCount == size || // auto-resolving
1655 kArea*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001656 }
1657
Brian Salomona56a7462020-02-07 14:17:25 -05001658 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001659 size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001660 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001661 }
1662
Robert Phillipsd6214d42016-11-07 08:23:48 -05001663 // Mipmapped versions
Greg Daniel4065d452018-11-16 15:43:41 -05001664 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001665 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001666
Brian Salomona56a7462020-02-07 14:17:25 -05001667 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001668 size_t size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001669 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001670
Greg Daniel6fa62e22019-08-07 15:52:37 -04001671 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001672 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001673 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, sampleCount);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001674 size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001675 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001676 kArea*4 + (kArea*4)/3 == size || // msaa4 failed
1677 kArea*4*sampleCount + (kArea*4)/3 == size || // auto-resolving
1678 kArea*4*(sampleCount+1) + (kArea*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001679 }
Robert Phillips1b352562017-04-05 18:56:21 +00001680
Brian Salomona56a7462020-02-07 14:17:25 -05001681 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001682 size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001683 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001684 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001685}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001686
1687#if GR_GPU_STATS
1688DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001689 auto context = ctxInfo.directContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001690 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001691
1692 // Helper that determines if cache is overbudget.
1693 auto overbudget = [context] {
1694 int uNum;
1695 size_t uSize;
1696 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001697 size_t bSize = context->getResourceCacheLimit();
1698 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001699 };
1700
1701 // Helper that does a trivial draw to a surface.
1702 auto drawToSurf = [](SkSurface* surf) {
1703 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1704 };
1705
1706 // Helper that checks whether a flush has occurred between calls.
1707 int baseFlushCount = 0;
1708 auto getFlushCountDelta = [context, &baseFlushCount]() {
Greg Danielfe159622020-04-10 17:43:51 +00001709 int cur = context->priv().getGpu()->stats()->numSubmitToGpus();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001710 int delta = cur - baseFlushCount;
1711 baseFlushCount = cur;
1712 return delta;
1713 };
1714
1715 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1716 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1717 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1718
1719 drawToSurf(surf1.get());
1720 drawToSurf(surf2.get());
1721
1722 // Flush each surface once to ensure that their backing stores are allocated.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001723 surf1->flushAndSubmit();
1724 surf2->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001725 REPORTER_ASSERT(reporter, overbudget());
1726 getFlushCountDelta();
1727
1728 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1729 drawToSurf(surf1.get());
1730 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1731 drawToSurf(surf2.get());
1732 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1733 REPORTER_ASSERT(reporter, overbudget());
1734
1735 // Make surf1 purgeable. Drawing to surf2 should flush.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001736 surf1->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001737 surf1.reset();
1738 drawToSurf(surf2.get());
1739 REPORTER_ASSERT(reporter, getFlushCountDelta());
1740 REPORTER_ASSERT(reporter, overbudget());
1741}
1742#endif