blob: 71c1b2c66dce2de5705305856058b18cdde707bf [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 Phillips0c5bb2f2020-07-17 15:40:13 -0400340 fDContext = GrDirectContext::MakeMock(nullptr);
341 SkASSERT(fDContext);
342 fDContext->setResourceCacheLimit(maxBytes);
343 GrResourceCache* cache = fDContext->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 Phillips0c5bb2f2020-07-17 15:40:13 -0400348 GrResourceCache* cache() { return fDContext->priv().getResourceCache(); }
349 GrGpu* gpu() { return fDContext->priv().getGpu(); }
350 GrDirectContext* dContext() { return fDContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800351
Greg Danielc27eb722018-08-10 09:48:08 -0400352 void reset() {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400353 fDContext.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400354 }
355
bsalomonc2f35b72015-01-23 07:19:22 -0800356private:
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400357 sk_sp<GrDirectContext> fDContext;
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);
bsalomon0ea80f42015-02-11 10:49:59 -0800362 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400363 GrGpu* gpu = mock.gpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800364
365 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400366 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
367 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
368 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
369 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800370
371 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800372 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800373 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800374 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800375
376 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800377 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800378 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
379
bsalomon8718aaf2015-02-19 07:24:21 -0800380 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800381
382 a->unref();
383 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800384 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800385 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800386 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800387
388 c->unref();
389 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800390 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800391 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800392 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800393
394 d->unref();
395 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800396 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
397 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800398
399 b->unref();
400 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800401 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
402 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800403}
404
bsalomon24db3b12015-01-23 04:24:04 -0800405// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500406template <int>
407static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800408 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500409 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800410 builder[0] = data;
411}
412
Robert Phillips6eba0632018-03-28 12:25:42 -0400413static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400414 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400415 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400416 GrGpu* gpu = mock.gpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400417
418 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
419 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400420 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400421
422 GrUniqueKey uniqueKey;
423 make_unique_key<0>(&uniqueKey, 0);
424
425 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400426 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400427 b->resourcePriv().setUniqueKey(uniqueKey);
428
429 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400430 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400431
432 GrUniqueKey uniqueKey2;
433 make_unique_key<0>(&uniqueKey2, 1);
434
435 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400436 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400437 d->resourcePriv().setUniqueKey(uniqueKey2);
438
439
440 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
441 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
442 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
443 d->gpuMemorySize() == cache->getResourceBytes());
444
445 // Should be safe to purge without deleting the resources since we still have refs.
446 cache->purgeUnlockedResources(false);
447 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
448
449 // Unref them all. Since they all have keys they should remain in the cache.
450
451 a->unref();
452 b->unref();
453 c->unref();
454 d->unref();
455 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
456 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
457 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
458 d->gpuMemorySize() == cache->getResourceBytes());
459
460 // Purge only the two scratch resources
461 cache->purgeUnlockedResources(true);
462
463 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
464 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
465 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
466 cache->getResourceBytes());
467
468 // Purge the uniquely keyed resources
469 cache->purgeUnlockedResources(false);
470
471 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
472 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
473 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
474}
475
bsalomon84c8e622014-11-17 09:33:27 -0800476static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400477 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800478 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400479 GrGpu* gpu = mock.gpu();
bsalomondace19e2014-11-17 07:34:06 -0800480
bsalomon8718aaf2015-02-19 07:24:21 -0800481 GrUniqueKey uniqueKey;
482 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800483
bsalomon8718aaf2015-02-19 07:24:21 -0800484 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800485 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400486 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
487 10);
488 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800489 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500490 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
491 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
492 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800493
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500494 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800495 GrUniqueKey uniqueKey2;
496 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500497 GrUniqueKey uniqueKey3;
498 make_unique_key<0>(&uniqueKey3, 2);
499 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
500 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
501 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
502 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
503 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
504 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400505
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500506 // Remove the extra refs we just added.
507 SkSafeUnref(wrappedCacheableViaKey);
508 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800509
510 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500511 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800512 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500513 wrappedCacheable->gpuMemorySize() +
514 wrappedUncacheable->gpuMemorySize() +
515 unbudgeted->gpuMemorySize() ==
516 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800517 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800518 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800519 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400520 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800521
bsalomon63c992f2015-01-23 12:47:59 -0800522 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800523 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500524 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800525 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500526 wrappedCacheable->gpuMemorySize() +
527 wrappedUncacheable->gpuMemorySize() +
528 unbudgeted->gpuMemorySize() ==
529 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800530 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800531 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800532 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400533 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800534
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500535 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
536 // However, unreffing the uncacheable wrapped resource should free it.
537 wrappedCacheable->unref();
538 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400539 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800540 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500541 wrappedCacheable->gpuMemorySize() +
542 unbudgeted->gpuMemorySize() ==
543 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500544 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800545
bsalomon84c8e622014-11-17 09:33:27 -0800546 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500547 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800548 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500549 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
550 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
551 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800552 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500553 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500554
555 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
556 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
557 if (wrappedCacheableViaKey) {
558 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
559 wrappedCacheable->unref();
560 }
561 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
562 // resource should immediately delete it.
563 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
564
565 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500566 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
567 wrappedUncacheable->gpuMemorySize() +
568 unbudgeted->gpuMemorySize() ==
569 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800570 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
571 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400572 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800573
574 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400575 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800576 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500577 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
578 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
579 wrappedUncacheable->gpuMemorySize() ==
580 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800581 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
582 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400583 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800584
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500585 // Unreffing the wrapped resources (with no unique key) should free them right away.
586 wrappedUncacheable->unref();
587 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
589 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
590 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400592 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800593
594 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800595 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
596 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
597 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
598 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400599 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800600}
601
bsalomon5236cf42015-01-14 10:42:08 -0800602static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400603 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800604 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400605 GrGpu* gpu = mock.gpu();
bsalomon5236cf42015-01-14 10:42:08 -0800606
bsalomon8718aaf2015-02-19 07:24:21 -0800607 GrUniqueKey uniqueKey;
608 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800609
610 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800611 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800612 TestResource* wrapped;
613 TestResource* unbudgeted;
614
615 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500616 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400617 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700618
bsalomon5236cf42015-01-14 10:42:08 -0800619 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800620 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
621 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
622 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
623 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400624 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800625
Greg Danielda86e282018-06-13 09:41:19 -0400626 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800627 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800628 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800629 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
630 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
631 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
632 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400633 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800634
bsalomon0ea80f42015-02-11 10:49:59 -0800635 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500636 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800637 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
638 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
639 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
640 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400641 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800642
643 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800644 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
645 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
646 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
647 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400648 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800649
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500650 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800651 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
652 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
653 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
654 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400655 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800656
657 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800658 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
659 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
660 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
661 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400662 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800663
bsalomon0ea80f42015-02-11 10:49:59 -0800664 cache->purgeAllUnlocked();
665 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
666 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
667 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
668 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400669 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800670}
671
bsalomon3582d3e2015-02-13 14:20:05 -0800672// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
673void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
674/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400675 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800676 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400677 GrGpu* gpu = mock.gpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800678
679 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500680 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800681 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800682 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800683
684 size_t size = resource->gpuMemorySize();
685 for (int i = 0; i < 2; ++i) {
686 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800687 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800688 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500689 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500690 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400691 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800692 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
693 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
694 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
695 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400696 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800697
698 // Once it is unrefed, it should become available as scratch.
699 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800700 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
701 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
702 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
703 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400704 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400705 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800706 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800707 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800708 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500709 REPORTER_ASSERT(reporter,
710 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800711
712 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700713 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800714 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800715 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800716 } else {
717 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800718 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800719 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
720 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
721 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
722 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400723 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800724 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800725 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500726 REPORTER_ASSERT(reporter,
727 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800728
729 // now when it is unrefed it should die since it has no key.
730 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800731 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
732 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
733 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
734 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400735 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800736 }
bsalomon8b79d232014-11-10 10:19:06 -0800737 }
bsalomonc2f35b72015-01-23 07:19:22 -0800738}
739
740static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400741 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800742 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400743 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -0800744
bsalomon8b79d232014-11-10 10:19:06 -0800745 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500746 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700747 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400748 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500749 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700750 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400751 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800752 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800753 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800754 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400755 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800756
757 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800758 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800759
bsalomon0ea80f42015-02-11 10:49:59 -0800760 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800761 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800762 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
763 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800764 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800765 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800766
bsalomon63c992f2015-01-23 12:47:59 -0800767 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800768 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800769 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800770 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800771
772 // Unref but don't purge
773 a->unref();
774 b->unref();
775 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800776 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800777
bsalomon63c992f2015-01-23 12:47:59 -0800778 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800779 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800780 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800781 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
782 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800783}
784
bsalomon10e23ca2014-11-25 05:52:06 -0800785static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400786 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800787 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400788 GrGpu* gpu = mock.gpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800789
bsalomon10e23ca2014-11-25 05:52:06 -0800790 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500791 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500793 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800794 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800795 a->unref();
796 b->unref();
797
bsalomon1c60dfe2015-01-21 09:32:40 -0800798 GrScratchKey scratchKey;
799 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800800 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800801 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400802 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800803
bsalomon0ea80f42015-02-11 10:49:59 -0800804 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800805 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800806 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800807 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
808 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800809
810 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400811 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800812 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800813 // It's still alive, but not cached by scratch key anymore
814 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800815 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
816 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800817
818 // The cache should immediately delete it when it's unrefed since it isn't accessible.
819 find->unref();
820 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800821 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
822 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800823
824 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400825 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800826 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800827 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800828 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
829 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800830
831 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800832 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800833 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800834 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
835 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800836
837 find->unref();
838 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800839 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
840 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800841}
842
bsalomon1c60dfe2015-01-21 09:32:40 -0800843static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400844 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800845 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400846 GrGpu* gpu = mock.gpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800847
848 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500849 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800850 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500851 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800852 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800853 a->unref();
854 b->unref();
855
856 GrScratchKey scratchKey;
857 // Ensure that scratch key comparison and assignment is consistent.
858 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800859 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800860 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800861 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800862 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
863 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
864 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
865 scratchKey = scratchKey1;
866 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
867 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
868 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
869 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
870 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
871 scratchKey = scratchKey2;
872 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
873 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
874 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
875 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
876 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
877
878 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800879 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800880 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400881 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800882
883 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800884 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400885 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700886 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800887 find->unref();
888
889 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400890 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700891 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800892 REPORTER_ASSERT(reporter, find == a || find == b);
893
Robert Phillipsaee18c92019-09-06 11:48:27 -0400894 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700895 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800896 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
897 REPORTER_ASSERT(reporter, find2 != find);
898 find2->unref();
899 find->unref();
900}
901
bsalomon8718aaf2015-02-19 07:24:21 -0800902static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400903 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800904 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400905 GrGpu* gpu = mock.gpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000906
bsalomon8718aaf2015-02-19 07:24:21 -0800907 GrUniqueKey key;
908 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700909
bsalomon8718aaf2015-02-19 07:24:21 -0800910 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400911 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700912
bsalomonf99e9612015-02-19 08:24:16 -0800913 // Set key on resource a.
914 a->resourcePriv().setUniqueKey(key);
915 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
916 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800917
bsalomonf99e9612015-02-19 08:24:16 -0800918 // Make sure that redundantly setting a's key works.
919 a->resourcePriv().setUniqueKey(key);
920 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800921 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800922 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
923 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800924 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
925
bsalomonf99e9612015-02-19 08:24:16 -0800926 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400927 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800928 b->resourcePriv().setUniqueKey(key);
929 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
930 b->unref();
931
932 // Still have two resources because a is still reffed.
933 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
934 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
935 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
936
937 a->unref();
938 // Now a should be gone.
939 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
940 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
941 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
942
943 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
944 // Also make b be unreffed when replacement occurs.
945 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400946 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800947 GrUniqueKey differentKey;
948 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800949 c->resourcePriv().setUniqueKey(differentKey);
950 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
951 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
952 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
953 // c replaces b and b should be immediately purged.
954 c->resourcePriv().setUniqueKey(key);
955 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
956 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
957 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
958
959 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800960 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800961 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
962 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
963 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
964
965 // Drop the ref on c, it should be kept alive because it has a unique key.
966 c->unref();
967 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
968 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
969 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
970
971 // Verify that we can find c, then remove its unique key. It should get purged immediately.
972 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
973 c->resourcePriv().removeUniqueKey();
974 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800975 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
976 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800977 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700978
979 {
980 GrUniqueKey key2;
981 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500982 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700983 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700984 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700985 d->resourcePriv().setUniqueKey(key2);
986 }
987
988 GrUniqueKey key3;
989 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400990 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700991 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000992}
993
bsalomon8b79d232014-11-10 10:19:06 -0800994static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400995 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400996 auto dContext = mock.dContext();
bsalomon0ea80f42015-02-11 10:49:59 -0800997 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400998 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -0800999
bsalomon8718aaf2015-02-19 07:24:21 -08001000 GrUniqueKey key1, key2, key3;
1001 make_unique_key<0>(&key1, 1);
1002 make_unique_key<0>(&key2, 2);
1003 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001004
bsalomon23e619c2015-02-06 11:54:28 -08001005 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001006 TestResource* a = new TestResource(gpu);
1007 TestResource* b = new TestResource(gpu);
1008 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001009 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001010 a->resourcePriv().setUniqueKey(key1);
1011 b->resourcePriv().setUniqueKey(key2);
1012 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001013 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001014 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001015 c->unref();
1016
bsalomon8718aaf2015-02-19 07:24:21 -08001017 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1018 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1019 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001020 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001021
bsalomon8718aaf2015-02-19 07:24:21 -08001022 typedef GrUniqueKeyInvalidatedMessage Msg;
1023 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001024
1025 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001026 Bus::Post(Msg(key1, dContext->priv().contextID()));
1027 Bus::Post(Msg(key2, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001028 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001029 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001030 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1031 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001032 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001033 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001034
1035 // Invalidate the third.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001036 Bus::Post(Msg(key3, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001037 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001038 // we still have a ref on b, c should be recycled as scratch.
1039 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001040 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001041
bsalomon23e619c2015-02-06 11:54:28 -08001042 // make b purgeable. It should be immediately deleted since it has no key.
1043 b->unref();
1044 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1045
1046 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1047 GrScratchKey scratchKey;
1048 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001049 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001050 REPORTER_ASSERT(reporter, scratch == c);
1051 SkSafeUnref(scratch);
1052
1053 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001054 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001055 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001056 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001057 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1058 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001059 REPORTER_ASSERT(reporter, !scratch);
1060 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001061}
1062
bsalomon71cb0c22014-11-14 12:10:14 -08001063static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001064 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -08001065 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001066 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -08001067
bsalomon8718aaf2015-02-19 07:24:21 -08001068 GrUniqueKey key1, key2;
1069 make_unique_key<0>(&key1, 1);
1070 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001071
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001072 sk_sp<TestResource> a(new TestResource(gpu));
1073 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001074 a->resourcePriv().setUniqueKey(key1);
1075 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001076
bsalomonc2f35b72015-01-23 07:19:22 -08001077 // Make a cycle
1078 a->setUnrefWhenDestroyed(b);
1079 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001080
bsalomonc2f35b72015-01-23 07:19:22 -08001081 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001082
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001083 TestResource* unownedA = a.release();
1084 unownedA->unref();
1085 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001086
bsalomonc2f35b72015-01-23 07:19:22 -08001087 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001088
bsalomon0ea80f42015-02-11 10:49:59 -08001089 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001090 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001091
bsalomonc2f35b72015-01-23 07:19:22 -08001092 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001093 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001094 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001095
bsalomon0ea80f42015-02-11 10:49:59 -08001096 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001097 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001098}
1099
bsalomonddf30e62015-02-19 11:38:44 -08001100static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1101 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001102 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001103 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001104
1105 SkRandom random;
1106
1107 // Run the test 2*kCount times;
1108 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001109 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001110 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001111 GrGpu* gpu = mock.gpu();
bsalomonddf30e62015-02-19 11:38:44 -08001112
1113 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001114 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001115
Robert Phillipscf39f372019-09-03 10:29:20 -04001116 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001117
1118 SkTDArray<int> shouldPurgeIdxs;
1119 int purgeableCnt = 0;
1120 SkTDArray<GrGpuResource*> resourcesToUnref;
1121
1122 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1123 // unpurgeable resources.
1124 for (int j = 0; j < kCount; ++j) {
1125 GrUniqueKey key;
1126 make_unique_key<0>(&key, j);
1127
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001128 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001129 r->resourcePriv().setUniqueKey(key);
1130 if (random.nextU() % kLockedFreq) {
1131 // Make this is purgeable.
1132 r->unref();
1133 ++purgeableCnt;
1134 if (purgeableCnt <= kNumToPurge) {
1135 *shouldPurgeIdxs.append() = j;
1136 }
1137 } else {
1138 *resourcesToUnref.append() = r;
1139 }
1140 }
1141
1142 // Verify that the correct resources were purged.
1143 int currShouldPurgeIdx = 0;
1144 for (int j = 0; j < kCount; ++j) {
1145 GrUniqueKey key;
1146 make_unique_key<0>(&key, j);
1147 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1148 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1149 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1150 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001151 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001152 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001153 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001154 }
1155 SkSafeUnref(res);
1156 }
1157
1158 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1159 resourcesToUnref[j]->unref();
1160 }
1161 }
1162}
1163
Brian Salomon5e150852017-03-22 14:53:13 -04001164static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001165 Mock mock(1000000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001166 auto dContext = mock.dContext();
Brian Salomon5e150852017-03-22 14:53:13 -04001167 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001168 GrGpu* gpu = mock.gpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001169
1170 static constexpr int kCnts[] = {1, 10, 1024};
1171 auto nowish = []() {
1172 // We sleep so that we ensure we get a value that is greater than the last call to
1173 // GrStdSteadyClock::now().
1174 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1175 auto result = GrStdSteadyClock::now();
1176 // Also sleep afterwards so we don't get this value again.
1177 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1178 return result;
1179 };
1180
1181 for (int cnt : kCnts) {
1182 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1183 new GrStdSteadyClock::time_point[cnt]);
1184 {
1185 // Insert resources and get time points between each addition.
1186 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001187 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001188 GrUniqueKey k;
1189 make_unique_key<1>(&k, i);
1190 r->resourcePriv().setUniqueKey(k);
1191 r->unref();
1192 timeStamps.get()[i] = nowish();
1193 }
1194
1195 // Purge based on the time points between resource additions. Each purge should remove
1196 // the oldest resource.
1197 for (int i = 0; i < cnt; ++i) {
1198 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1199 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1200 for (int j = 0; j < i; ++j) {
1201 GrUniqueKey k;
1202 make_unique_key<1>(&k, j);
1203 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1204 REPORTER_ASSERT(reporter, !SkToBool(r));
1205 SkSafeUnref(r);
1206 }
1207 }
1208
1209 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1210 cache->purgeAllUnlocked();
1211 }
1212
1213 // Do a similar test but where we leave refs on some resources to prevent them from being
1214 // purged.
1215 {
1216 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1217 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001218 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001219 GrUniqueKey k;
1220 make_unique_key<1>(&k, i);
1221 r->resourcePriv().setUniqueKey(k);
1222 // Leave a ref on every other resource, beginning with the first.
1223 if (SkToBool(i & 0x1)) {
1224 refedResources.get()[i / 2] = r;
1225 } else {
1226 r->unref();
1227 }
1228 timeStamps.get()[i] = nowish();
1229 }
1230
1231 for (int i = 0; i < cnt; ++i) {
1232 // Should get a resource purged every other frame.
1233 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1234 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1235 }
1236
1237 // Unref all the resources that we kept refs on in the first loop.
1238 for (int i = 0; i < (cnt / 2); ++i) {
1239 refedResources.get()[i]->unref();
1240 cache->purgeResourcesNotUsedSince(nowish());
1241 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1242 }
1243
1244 cache->purgeAllUnlocked();
1245 }
1246
1247 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1248
1249 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1250 // eviction
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001251 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001252 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001253 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001254 GrUniqueKey k;
1255 make_unique_key<1>(&k, i);
1256 r->resourcePriv().setUniqueKey(k);
1257 r->unref();
1258 }
1259 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001260 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001261 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1262 cache->purgeResourcesNotUsedSince(nowish());
1263 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1264 }
1265}
1266
Derek Sollenberger5480a182017-05-25 16:43:59 -04001267static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001268 Mock mock(100);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001269 auto dContext = mock.dContext();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001270 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001271 GrGpu* gpu = mock.gpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001272
1273 enum TestsCase {
1274 kOnlyScratch_TestCase = 0,
1275 kPartialScratch_TestCase = 1,
1276 kAllScratch_TestCase = 2,
1277 kPartial_TestCase = 3,
1278 kAll_TestCase = 4,
1279 kNone_TestCase = 5,
1280 kEndTests_TestCase = kNone_TestCase + 1
1281 };
1282
1283 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1284
1285 GrUniqueKey key1, key2, key3;
1286 make_unique_key<0>(&key1, 1);
1287 make_unique_key<0>(&key2, 2);
1288 make_unique_key<0>(&key3, 3);
1289
1290 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001291 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1292 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1293 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001294
1295 unique1->resourcePriv().setUniqueKey(key1);
1296 unique2->resourcePriv().setUniqueKey(key2);
1297 unique3->resourcePriv().setUniqueKey(key3);
1298
Derek Sollenberger5480a182017-05-25 16:43:59 -04001299 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001300 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001301 TestResource::kA_SimulatedProperty,
1302 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001303 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001304 TestResource::kB_SimulatedProperty,
1305 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001306
1307 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1308 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1309 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1310
1311 // Add resources to the purgeable queue
1312 unique1->unref();
1313 scratch1->unref();
1314 unique2->unref();
1315 scratch2->unref();
1316 unique3->unref();
1317
1318 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1319 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1320 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1321
1322 switch(testCase) {
1323 case kOnlyScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001324 dContext->purgeUnlockedResources(14, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001325 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1326 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1327 break;
1328 }
1329 case kPartialScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001330 dContext->purgeUnlockedResources(3, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001331 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1332 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1333 break;
1334 }
1335 case kAllScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001336 dContext->purgeUnlockedResources(50, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001337 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1338 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1339 break;
1340 }
1341 case kPartial_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001342 dContext->purgeUnlockedResources(13, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001343 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1344 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1345 break;
1346 }
1347 case kAll_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001348 dContext->purgeUnlockedResources(50, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001349 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1350 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1351 break;
1352 }
1353 case kNone_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001354 dContext->purgeUnlockedResources(0, true);
1355 dContext->purgeUnlockedResources(0, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001356 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1357 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1358 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1359 break;
1360 }
Brian Salomon23356442018-11-30 15:33:19 -05001361 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001362
1363 // ensure all are purged before the next
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001364 dContext->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001365 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1366 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1367
1368 }
1369}
1370
senorblanco84cd6212015-08-04 10:01:58 -07001371static void test_custom_data(skiatest::Reporter* reporter) {
1372 GrUniqueKey key1, key2;
1373 make_unique_key<0>(&key1, 1);
1374 make_unique_key<0>(&key2, 2);
1375 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001376 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001377 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1378 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1379
1380 // Test that copying a key also takes a ref on its custom data.
1381 GrUniqueKey key3 = key1;
1382 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1383}
1384
bsalomonc6363ef2015-09-24 07:07:40 -07001385static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001386 Mock mock(300);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001387 auto dContext = mock.dContext();
1388 GrGpu* gpu = mock.gpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001389
1390 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001391 dContext->abandonContext();
bsalomonc6363ef2015-09-24 07:07:40 -07001392
1393 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1394
1395 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1396
robertphillips8abb3702016-08-31 14:04:06 -07001397 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001398 resource->getUniqueKey();
1399 resource->wasDestroyed();
1400 resource->gpuMemorySize();
1401 resource->getContext();
1402
bsalomonc6363ef2015-09-24 07:07:40 -07001403 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001404 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001405 resource->resourcePriv().makeBudgeted();
1406 resource->resourcePriv().makeUnbudgeted();
1407 resource->resourcePriv().removeScratchKey();
1408 GrUniqueKey key;
1409 make_unique_key<0>(&key, 1);
1410 resource->resourcePriv().setUniqueKey(key);
1411 resource->resourcePriv().removeUniqueKey();
1412}
1413
Brian Salomon1090da62017-01-06 12:04:19 -05001414static void test_tags(skiatest::Reporter* reporter) {
1415#ifdef SK_DEBUG
1416 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1417 static constexpr int kLastTagIdx = 10;
1418 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1419
Robert Phillipscf39f372019-09-03 10:29:20 -04001420 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001421 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001422 GrGpu* gpu = mock.gpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001423
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001424 // tag strings are expected to be long lived
1425 std::vector<SkString> tagStrings;
1426
Brian Salomon1090da62017-01-06 12:04:19 -05001427 SkString tagStr;
1428 int tagIdx = 0;
1429 int currTagCnt = 0;
1430
1431 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001432
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001433 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001434 GrUniqueKey key;
1435 if (currTagCnt == tagIdx) {
1436 tagIdx += 1;
1437 currTagCnt = 0;
1438 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001439 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001440 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001441 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001442 resource->resourcePriv().setUniqueKey(key);
1443 }
1444 SkASSERT(kLastTagIdx == tagIdx);
1445 SkASSERT(currTagCnt == kLastTagIdx);
1446
1447 // Test i = 0 to exercise unused tag string.
1448 for (int i = 0; i <= kLastTagIdx; ++i) {
1449 tagStr.printf("tag%d", i);
1450 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1451 }
1452#endif
1453}
1454
Robert Phillipsddc21482019-10-16 14:30:09 -04001455static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001456 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001457 auto dContext = mock.dContext();
Greg Danielc27eb722018-08-10 09:48:08 -04001458 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001459 GrGpu* gpu = mock.gpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001460
Robert Phillipsddc21482019-10-16 14:30:09 -04001461 GrBackendTexture backends[3];
1462 GrTexture* wrapped[3];
1463 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001464
Robert Phillipsddc21482019-10-16 14:30:09 -04001465 auto releaseProc = [](void* ctx) {
1466 int* index = (int*) ctx;
1467 *index = 1;
1468 };
Greg Danielc27eb722018-08-10 09:48:08 -04001469
Robert Phillipsddc21482019-10-16 14:30:09 -04001470 for (int i = 0; i < 3; ++i) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001471 backends[i] = dContext->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
1472 GrMipMapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001473 wrapped[i] = gpu->wrapBackendTexture(backends[i],
Robert Phillipsddc21482019-10-16 14:30:09 -04001474 GrWrapOwnership::kBorrow_GrWrapOwnership,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001475 (i < 2) ? GrWrapCacheable::kYes : GrWrapCacheable::kNo,
1476 GrIOType::kRead_GrIOType)
1477 .release();
Robert Phillipsddc21482019-10-16 14:30:09 -04001478 wrapped[i]->setRelease(releaseProc, &freed[i]);
1479 }
1480
1481 cache->insertDelayedTextureUnref(wrapped[0]);
1482 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001483
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001484 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1485 // is because inserting it as a cross-context resource actually holds a ref until the
1486 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001487 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001488
Robert Phillipsddc21482019-10-16 14:30:09 -04001489 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001490
1491 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001492 wrapped[0]->unref();
1493 wrapped[1]->unref();
1494 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001495
Robert Phillipsddc21482019-10-16 14:30:09 -04001496 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001497
1498 // This should free nothing since no messages were sent.
1499 cache->purgeAsNeeded();
1500
Robert Phillipsddc21482019-10-16 14:30:09 -04001501 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1502
Greg Danielc27eb722018-08-10 09:48:08 -04001503 // Send message to free the first resource
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001504 GrTextureFreedMessage msg1{wrapped[0], dContext->priv().contextID()};
Robert Phillipsddc21482019-10-16 14:30:09 -04001505 SkMessageBus<GrTextureFreedMessage>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001506 cache->purgeAsNeeded();
1507
Robert Phillipsddc21482019-10-16 14:30:09 -04001508 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1509 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001510
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001511 GrTextureFreedMessage msg2{wrapped[2], dContext->priv().contextID()};
Robert Phillipsddc21482019-10-16 14:30:09 -04001512 SkMessageBus<GrTextureFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001513 cache->purgeAsNeeded();
1514
Robert Phillipsddc21482019-10-16 14:30:09 -04001515 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1516 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001517
1518 mock.reset();
1519
Robert Phillipsddc21482019-10-16 14:30:09 -04001520 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001521}
1522
Brian Salomondcfca432017-11-15 15:48:03 -05001523DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001524 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001525 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001526 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001527 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001528 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001529 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001530 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001531 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001532 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001533 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001534 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001535 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001536 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001537 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001538 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001539 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001540 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001541 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001542 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001543}
1544
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001545// This simulates a portion of Chrome's context abandonment processing.
1546// Please see: crbug.com/1011368 and crbug.com/1014993
1547DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001548 auto context = ctxInfo.directContext();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001549 GrGpu* gpu = context->priv().getGpu();
1550 GrResourceCache* cache = context->priv().getResourceCache();
1551
1552 GrBackendTexture backend = context->createBackendTexture(16, 16,
1553 SkColorType::kRGBA_8888_SkColorType,
1554 GrMipMapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001555 GrTexture* tex = gpu->wrapBackendTexture(backend,
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001556 GrWrapOwnership::kBorrow_GrWrapOwnership,
1557 GrWrapCacheable::kYes,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001558 GrIOType::kRead_GrIOType)
1559 .release();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001560
1561 auto releaseProc = [](void* ctx) {
1562 int* index = (int*) ctx;
1563 *index = 1;
1564 };
1565
1566 int freed = 0;
1567
1568 tex->setRelease(releaseProc, &freed);
1569
1570 cache->insertDelayedTextureUnref(tex);
1571
1572 // Now only the cache is holding a ref to this texture
1573 tex->unref();
1574
1575 REPORTER_ASSERT(reporter, 0 == freed);
1576
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001577 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1578 // for all the backends for consistency.
1579 context->deleteBackendTexture(backend);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001580 context->abandonContext();
1581
1582 REPORTER_ASSERT(reporter, 1 == freed);
1583
1584 // In the past, creating this message could cause an exception due to
1585 // an un-safe downcast from GrTexture to GrGpuResource
1586 GrTextureFreedMessage msg{tex, context->priv().contextID()};
1587 SkMessageBus<GrTextureFreedMessage>::Post(msg);
1588
Greg Danielf0e04f02019-12-04 15:17:54 -05001589 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001590 context->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001591}
1592
Robert Phillipsd6214d42016-11-07 08:23:48 -05001593////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001594static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001595 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001596 SkISize dims,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001597 int sampleCnt) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001598 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomona56a7462020-02-07 14:17:25 -05001599 return provider->createTexture(dims, format, renderable, sampleCnt, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -04001600 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001601}
1602
Brian Salomona56a7462020-02-07 14:17:25 -05001603static sk_sp<GrTextureProxy> make_mipmap_proxy(GrContext* context,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001604 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001605 SkISize dims,
Robert Phillipse78b7252017-04-06 07:59:41 -04001606 int sampleCnt) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001607 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1608 const GrCaps* caps = context->priv().caps();
1609
Robert Phillipsd6214d42016-11-07 08:23:48 -05001610
Robert Phillips0a15cc62019-07-30 12:49:10 -04001611 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1612 GrRenderable::kNo);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001613
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001614 return proxyProvider->createProxy(format, dims, renderable, sampleCnt, GrMipMapped::kYes,
1615 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001616}
1617
1618// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1619// Texture-only, both-RT-and-Texture and MIPmapped
1620DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001621 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001622 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001623 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001624
Brian Salomona56a7462020-02-07 14:17:25 -05001625 static constexpr SkISize kSize = {64, 64};
1626 static constexpr auto kArea = kSize.area();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001627
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001629 {
1630 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001631
Brian Salomona56a7462020-02-07 14:17:25 -05001632 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001633 size_t size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001634 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001635
Greg Daniel6fa62e22019-08-07 15:52:37 -04001636 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001637 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001638 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001639 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001640 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001641 kArea*4 == size || // msaa4 failed
1642 kArea*4*sampleCount == size || // auto-resolving
1643 kArea*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001644 }
1645
Brian Salomona56a7462020-02-07 14:17:25 -05001646 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001647 size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001648 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001649 }
1650
Robert Phillipsd6214d42016-11-07 08:23:48 -05001651 // Mipmapped versions
Greg Daniel4065d452018-11-16 15:43:41 -05001652 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001653 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001654
Brian Salomona56a7462020-02-07 14:17:25 -05001655 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001656 size_t size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001657 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001658
Greg Daniel6fa62e22019-08-07 15:52:37 -04001659 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001660 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001661 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, sampleCount);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001662 size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001663 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001664 kArea*4 + (kArea*4)/3 == size || // msaa4 failed
1665 kArea*4*sampleCount + (kArea*4)/3 == size || // auto-resolving
1666 kArea*4*(sampleCount+1) + (kArea*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001667 }
Robert Phillips1b352562017-04-05 18:56:21 +00001668
Brian Salomona56a7462020-02-07 14:17:25 -05001669 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001670 size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001671 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001672 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001673}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001674
1675#if GR_GPU_STATS
1676DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001677 auto context = ctxInfo.directContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001678 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001679
1680 // Helper that determines if cache is overbudget.
1681 auto overbudget = [context] {
1682 int uNum;
1683 size_t uSize;
1684 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001685 size_t bSize = context->getResourceCacheLimit();
1686 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001687 };
1688
1689 // Helper that does a trivial draw to a surface.
1690 auto drawToSurf = [](SkSurface* surf) {
1691 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1692 };
1693
1694 // Helper that checks whether a flush has occurred between calls.
1695 int baseFlushCount = 0;
1696 auto getFlushCountDelta = [context, &baseFlushCount]() {
Greg Danielfe159622020-04-10 17:43:51 +00001697 int cur = context->priv().getGpu()->stats()->numSubmitToGpus();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001698 int delta = cur - baseFlushCount;
1699 baseFlushCount = cur;
1700 return delta;
1701 };
1702
1703 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1704 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1705 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1706
1707 drawToSurf(surf1.get());
1708 drawToSurf(surf2.get());
1709
1710 // Flush each surface once to ensure that their backing stores are allocated.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001711 surf1->flushAndSubmit();
1712 surf2->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001713 REPORTER_ASSERT(reporter, overbudget());
1714 getFlushCountDelta();
1715
1716 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1717 drawToSurf(surf1.get());
1718 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1719 drawToSurf(surf2.get());
1720 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1721 REPORTER_ASSERT(reporter, overbudget());
1722
1723 // Make surf1 purgeable. Drawing to surf2 should flush.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001724 surf1->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001725 surf1.reset();
1726 drawToSurf(surf2.get());
1727 REPORTER_ASSERT(reporter, getFlushCountDelta());
1728 REPORTER_ASSERT(reporter, overbudget());
1729}
1730#endif