blob: 8f79140c3981892b501a842239d7edbe9dc09056 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrGpuResourceCacheAccess.h"
15#include "src/gpu/GrGpuResourcePriv.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRenderTargetPriv.h"
18#include "src/gpu/GrResourceCache.h"
19#include "src/gpu/GrResourceProvider.h"
20#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 Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkMipMap.h"
26#include "src/gpu/SkGr.h"
27#include "tests/Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028
Hal Canary8a001442018-09-19 11:31:27 -040029#include <thread>
30
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070036 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080037 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070038 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080039 SkCanvas* canvas = surface->getCanvas();
40
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
42
43 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000044 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040046 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000048 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070049 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050
Robert Phillipscf39f372019-09-03 10:29:20 -040051 size_t oldMaxBytes = context->getResourceCacheLimit();
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000052
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053 // Set the cache limits so we can fit 10 "src" images and the
54 // max number of textures doesn't matter
55 size_t maxCacheSize = initialCacheSize + 10*srcSize;
Robert Phillipscf39f372019-09-03 10:29:20 -040056 context->setResourceCacheLimit(maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000059 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060
61 for (int i = 0; i < 100; ++i) {
62 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040063 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 // "modify" the src texture
66 src.notifyPixelsChanged();
67
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000068 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070069 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000070
71 // we should never go over the size limit
72 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
73 }
74
Robert Phillipscf39f372019-09-03 10:29:20 -040075 context->setResourceCacheLimit(oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000076}
77
bsalomon11abd8d2016-10-14 08:13:48 -070078static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
79 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
80 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
81 return false;
82 }
83 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
84}
85
Robert Phillipsc0192e32017-09-21 12:00:26 -040086static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
87 return rt->renderTargetPriv().getStencilAttachment();
88}
89
90static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
91 int size, int sampleCount, SkBudgeted budgeted) {
92 GrSurfaceDesc desc;
Robert Phillipsc0192e32017-09-21 12:00:26 -040093 desc.fWidth = size;
94 desc.fHeight = size;
Robert Phillipsc0192e32017-09-21 12:00:26 -040095
Brian Salomon4eb38b72019-08-05 12:58:39 -040096 auto format =
97 provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
98 sk_sp<GrTexture> tex(provider->createTexture(desc, format, GrRenderable::kYes, sampleCount,
Brian Salomona90382f2019-09-17 09:01:56 -040099 GrMipMapped::kNo, budgeted, GrProtected::kNo));
Robert Phillipsc0192e32017-09-21 12:00:26 -0400100 if (!tex || !tex->asRenderTarget()) {
101 return nullptr;
102 }
103
Chris Daltoneffee202019-07-01 22:28:03 -0600104 if (!provider->attachStencilAttachment(tex->asRenderTarget(), sampleCount)) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400105 return nullptr;
106 }
107 SkASSERT(get_SB(tex->asRenderTarget()));
108
109 return sk_ref_sp(tex->asRenderTarget());
110}
111
bsalomon11abd8d2016-10-14 08:13:48 -0700112// This currently fails on ES3 ANGLE contexts
113DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000114 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700115 GrContext* context = ctxInfo.grContext();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400116 const GrCaps* caps = context->priv().caps();
117
118 if (caps->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700119 return;
120 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400121
Robert Phillips9da87e02019-02-04 13:26:26 -0500122 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400123
Greg Daniel5c96db82019-07-09 14:06:58 -0400124 GrColorType grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400125 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400126
Brian Salomonbdecacf2018-02-02 20:32:49 -0500127 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128 REPORTER_ASSERT(reporter, smallRT0);
129
130 {
131 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
133 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400134
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800136 }
137
Robert Phillipsc0192e32017-09-21 12:00:26 -0400138 {
139 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500140 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400141 REPORTER_ASSERT(reporter, smallRT2);
142
143 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800144 }
145
Robert Phillipsc0192e32017-09-21 12:00:26 -0400146 {
147 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500148 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400149 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800150
Robert Phillipsc0192e32017-09-21 12:00:26 -0400151 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800152 }
bsalomon02a44a42015-02-19 09:09:00 -0800153
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400154 int smallSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400155 context->priv().caps()->getRenderTargetSampleCount(2, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500156 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700157 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500158 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
159 smallSampleCount, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400160 REPORTER_ASSERT(reporter, smallMSAART0);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400161
162 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
163
164 {
165 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500166 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
167 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168 SkBudgeted::kNo);
169 REPORTER_ASSERT(reporter, smallMSAART1);
170
171 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800172 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400173
Brian Salomonbdecacf2018-02-02 20:32:49 -0500174 // But one with a larger sample count should not. (Also check that the two requests didn't
175 // rounded up to the same actual sample count or else they could share.).
Greg Daniel6fa62e22019-08-07 15:52:37 -0400176 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(5, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500177 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500178 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
179 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400180 SkBudgeted::kNo);
181 REPORTER_ASSERT(reporter, smallMSAART2);
182
183 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800184 }
185 }
186}
187
bsalomon68d91342016-04-12 09:59:58 -0700188DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700189 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500190 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
191 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700192 // this test is only valid for GL
193 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700194 return;
195 }
196
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500197 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700198 static const int kW = 100;
199 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700200
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400201 backendTextures[0] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
202 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400203 GrMipMapped::kNo, GrRenderable::kNo,
204 GrProtected::kNo);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400205 backendTextures[1] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
206 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400207 GrMipMapped::kNo, GrRenderable::kNo,
208 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500209 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
210 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
211 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
212 return;
213 }
jvanverth672bb7f2015-07-13 07:19:57 -0700214
bsalomon6dc6f5f2015-06-18 09:12:16 -0700215 context->resetContext();
216
Robert Phillips6be756b2018-01-16 15:07:54 -0500217 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400218 backendTextures[0], GrColorType::kRGBA_8888,
219 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220
Robert Phillips6be756b2018-01-16 15:07:54 -0500221 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400222 backendTextures[1], GrColorType::kRGBA_8888,
223 kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224
Brian Osman85d34b22017-05-10 12:06:26 -0400225 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
226 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227 return;
228 }
229
halcanary96fcdcc2015-08-27 07:41:13 -0700230 borrowed.reset(nullptr);
231 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
233 context->flush();
234
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500235 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
236 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700237
238 REPORTER_ASSERT(reporter, borrowedIsAlive);
239 REPORTER_ASSERT(reporter, !adoptedIsAlive);
240
Brian Salomone64b0642018-03-07 11:47:54 -0500241 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400242 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500243 }
244 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400245 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500246 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700247
248 context->resetContext();
249}
250
bsalomon6d3fe022014-07-25 08:35:45 -0700251class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800252 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000253public:
robertphillips6e83ac72015-08-13 05:19:14 -0700254 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700255
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 /** Property that distinctly categorizes the resource.
257 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800258 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800259
kkinnunen2e6055b2016-04-22 01:48:29 -0700260 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
261 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700262 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800263 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 , fProperty(kA_SimulatedProperty)
265 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800266 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800268 }
269
kkinnunen2e6055b2016-04-22 01:48:29 -0700270 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400271 SimulatedProperty property, size_t size = kDefaultSize) {
272 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800273 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500274 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
275 size_t size = kDefaultSize) {
276 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000277 }
278
Brian Salomond3b65972017-03-22 12:05:03 -0400279 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800280 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
bsalomon33435572014-11-05 14:47:41 -0800283 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400285 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
286 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000287 }
288
bsalomon1c60dfe2015-01-21 09:32:40 -0800289 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
290 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
291 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800292 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
293 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 }
295 }
296
297 static size_t ExpectedScratchKeySize() {
298 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
299 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000300private:
bsalomon24db3b12015-01-23 04:24:04 -0800301 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800302
Greg Danielda86e282018-06-13 09:41:19 -0400303 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
304 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700306 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400307 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700308 , fProperty(property)
309 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800310 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700311 this->registerWithCache(budgeted);
312 }
313
314 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500315 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
316 : INHERITED(gpu)
317 , fToDelete(nullptr)
318 , fSize(size)
319 , fProperty(kA_SimulatedProperty)
320 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700321 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500322 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700323 }
324
325 void computeScratchKey(GrScratchKey* key) const override {
326 if (fIsScratch) {
327 ComputeScratchKey(fProperty, key);
328 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800329 }
330
mtklein36352bf2015-03-25 18:17:31 -0700331 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400332 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800333
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400334 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000335 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800336 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800337 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700338 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700339 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000340};
bsalomon33435572014-11-05 14:47:41 -0800341int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000342
bsalomonc2f35b72015-01-23 07:19:22 -0800343class Mock {
344public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400345 Mock(size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400346 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800347 SkASSERT(fContext);
Robert Phillipscf39f372019-09-03 10:29:20 -0400348 fContext->setResourceCacheLimit(maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500349 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800350 cache->purgeAllUnlocked();
351 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800352 }
bsalomonc2f35b72015-01-23 07:19:22 -0800353
Robert Phillips9da87e02019-02-04 13:26:26 -0500354 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800355
Hal Canary342b7ac2016-11-04 11:49:42 -0400356 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800357
Greg Danielc27eb722018-08-10 09:48:08 -0400358 void reset() {
359 fContext.reset();
360 }
361
bsalomonc2f35b72015-01-23 07:19:22 -0800362private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400363 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800364};
365
366static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400367 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800368 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800369 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500370 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800371
372 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400373 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
374 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
375 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
376 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800377
378 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800379 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800380 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800381 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800384 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800385 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
386
bsalomon8718aaf2015-02-19 07:24:21 -0800387 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 a->unref();
390 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800392 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800393 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 c->unref();
396 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800398 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 d->unref();
402 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
404 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800405
406 b->unref();
407 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800408 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
409 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800410}
411
bsalomon24db3b12015-01-23 04:24:04 -0800412// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500413template <int>
414static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800415 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500416 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800417 builder[0] = data;
418}
419
Robert Phillips6eba0632018-03-28 12:25:42 -0400420static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400421 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400422 GrContext* context = mock.context();
423 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500424 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400425
426 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
427 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400428 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400429
430 GrUniqueKey uniqueKey;
431 make_unique_key<0>(&uniqueKey, 0);
432
433 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400434 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400435 b->resourcePriv().setUniqueKey(uniqueKey);
436
437 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400438 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400439
440 GrUniqueKey uniqueKey2;
441 make_unique_key<0>(&uniqueKey2, 1);
442
443 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400444 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400445 d->resourcePriv().setUniqueKey(uniqueKey2);
446
447
448 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
449 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
450 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
451 d->gpuMemorySize() == cache->getResourceBytes());
452
453 // Should be safe to purge without deleting the resources since we still have refs.
454 cache->purgeUnlockedResources(false);
455 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
456
457 // Unref them all. Since they all have keys they should remain in the cache.
458
459 a->unref();
460 b->unref();
461 c->unref();
462 d->unref();
463 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
464 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
465 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
466 d->gpuMemorySize() == cache->getResourceBytes());
467
468 // Purge only the two scratch resources
469 cache->purgeUnlockedResources(true);
470
471 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
472 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
473 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
474 cache->getResourceBytes());
475
476 // Purge the uniquely keyed resources
477 cache->purgeUnlockedResources(false);
478
479 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
480 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
482}
483
bsalomon84c8e622014-11-17 09:33:27 -0800484static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400485 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800486 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800487 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500488 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800489
bsalomon8718aaf2015-02-19 07:24:21 -0800490 GrUniqueKey uniqueKey;
491 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800492
bsalomon8718aaf2015-02-19 07:24:21 -0800493 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800494 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400495 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
496 10);
497 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800498 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500499 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
500 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
501 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800502
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500503 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800504 GrUniqueKey uniqueKey2;
505 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500506 GrUniqueKey uniqueKey3;
507 make_unique_key<0>(&uniqueKey3, 2);
508 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
509 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
510 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
511 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
512 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
513 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400514
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500515 // Remove the extra refs we just added.
516 SkSafeUnref(wrappedCacheableViaKey);
517 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800518
519 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500520 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800521 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500522 wrappedCacheable->gpuMemorySize() +
523 wrappedUncacheable->gpuMemorySize() +
524 unbudgeted->gpuMemorySize() ==
525 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800526 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800527 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800528 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400529 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800530
bsalomon63c992f2015-01-23 12:47:59 -0800531 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800532 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500533 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800534 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500535 wrappedCacheable->gpuMemorySize() +
536 wrappedUncacheable->gpuMemorySize() +
537 unbudgeted->gpuMemorySize() ==
538 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800539 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800540 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800541 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400542 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800543
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500544 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
545 // However, unreffing the uncacheable wrapped resource should free it.
546 wrappedCacheable->unref();
547 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400548 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800549 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500550 wrappedCacheable->gpuMemorySize() +
551 unbudgeted->gpuMemorySize() ==
552 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500553 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800554
bsalomon84c8e622014-11-17 09:33:27 -0800555 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500556 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800557 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500558 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
559 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
560 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800561 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500562 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500563
564 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
565 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
566 if (wrappedCacheableViaKey) {
567 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
568 wrappedCacheable->unref();
569 }
570 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
571 // resource should immediately delete it.
572 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
573
574 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500575 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
576 wrappedUncacheable->gpuMemorySize() +
577 unbudgeted->gpuMemorySize() ==
578 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800579 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
580 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400581 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800582
583 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400584 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800585 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500586 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
587 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
588 wrappedUncacheable->gpuMemorySize() ==
589 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800590 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400592 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800593
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500594 // Unreffing the wrapped resources (with no unique key) should free them right away.
595 wrappedUncacheable->unref();
596 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800597 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
598 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
599 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
600 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400601 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800602
603 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800604 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
605 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
606 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
607 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400608 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800609}
610
bsalomon5236cf42015-01-14 10:42:08 -0800611static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400612 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800613 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800614 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500615 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800616
bsalomon8718aaf2015-02-19 07:24:21 -0800617 GrUniqueKey uniqueKey;
618 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800619
620 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800621 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800622 TestResource* wrapped;
623 TestResource* unbudgeted;
624
625 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500626 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400627 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700628
bsalomon5236cf42015-01-14 10:42:08 -0800629 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800630 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
631 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
632 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
633 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400634 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800635
Greg Danielda86e282018-06-13 09:41:19 -0400636 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800637 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800638 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800639 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
640 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
641 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
642 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400643 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800644
bsalomon0ea80f42015-02-11 10:49:59 -0800645 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500646 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800647 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
648 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
649 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
650 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400651 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800652
653 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800654 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
655 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
656 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
657 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400658 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800659
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500660 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800661 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
662 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
663 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
664 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400665 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800666
667 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800668 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
669 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
670 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
671 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400672 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800673
bsalomon0ea80f42015-02-11 10:49:59 -0800674 cache->purgeAllUnlocked();
675 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
676 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
677 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
678 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400679 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800680}
681
bsalomon3582d3e2015-02-13 14:20:05 -0800682// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
683void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
684/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400685 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800686 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800687 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500688 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800689
690 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500691 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800692 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800693 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800694
695 size_t size = resource->gpuMemorySize();
696 for (int i = 0; i < 2; ++i) {
697 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800698 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800699 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500700 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500701 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400702 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800703 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
704 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
705 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
706 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400707 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800708
709 // Once it is unrefed, it should become available as scratch.
710 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800711 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
712 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
713 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
714 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400715 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400716 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800717 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800718 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800719 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500720 REPORTER_ASSERT(reporter,
721 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800722
723 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700724 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800725 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800726 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800727 } else {
728 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800729 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800730 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
731 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
732 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
733 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400734 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800735 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800736 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500737 REPORTER_ASSERT(reporter,
738 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800739
740 // now when it is unrefed it should die since it has no key.
741 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800742 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
743 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
744 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
745 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400746 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800747 }
bsalomon8b79d232014-11-10 10:19:06 -0800748 }
bsalomonc2f35b72015-01-23 07:19:22 -0800749}
750
751static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400752 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800753 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800754 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500755 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800756
bsalomon8b79d232014-11-10 10:19:06 -0800757 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500758 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700759 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400760 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500761 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700762 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400763 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800764 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800765 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800766 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400767 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800768
769 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800770 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800771
bsalomon0ea80f42015-02-11 10:49:59 -0800772 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800773 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800774 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
775 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800776 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800777 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800778
bsalomon63c992f2015-01-23 12:47:59 -0800779 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800780 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800781 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800782 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800783
784 // Unref but don't purge
785 a->unref();
786 b->unref();
787 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800788 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800789
bsalomon63c992f2015-01-23 12:47:59 -0800790 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800791 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800792 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800793 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
794 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800795}
796
bsalomon10e23ca2014-11-25 05:52:06 -0800797static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400798 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800799 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800800 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500801 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800802
bsalomon10e23ca2014-11-25 05:52:06 -0800803 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500804 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800805 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500806 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800807 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800808 a->unref();
809 b->unref();
810
bsalomon1c60dfe2015-01-21 09:32:40 -0800811 GrScratchKey scratchKey;
812 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800813 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800814 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400815 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800816
bsalomon0ea80f42015-02-11 10:49:59 -0800817 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800818 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800819 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800820 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
821 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800822
823 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400824 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800825 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800826 // It's still alive, but not cached by scratch key anymore
827 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800828 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
829 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800830
831 // The cache should immediately delete it when it's unrefed since it isn't accessible.
832 find->unref();
833 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800834 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
835 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800836
837 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400838 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800839 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800840 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800841 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
842 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800843
844 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800845 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800846 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800847 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
848 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800849
850 find->unref();
851 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800852 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
853 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800854}
855
bsalomon1c60dfe2015-01-21 09:32:40 -0800856static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400857 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800858 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800859 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500860 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800861
862 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500863 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800864 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500865 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800866 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800867 a->unref();
868 b->unref();
869
870 GrScratchKey scratchKey;
871 // Ensure that scratch key comparison and assignment is consistent.
872 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800873 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800874 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800875 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800876 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
877 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
878 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
879 scratchKey = scratchKey1;
880 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
881 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
882 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
883 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
884 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
885 scratchKey = scratchKey2;
886 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
887 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
888 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
889 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
890 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
891
892 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800893 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800894 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400895 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800896
897 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800898 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400899 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700900 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800901 find->unref();
902
903 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400904 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700905 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800906 REPORTER_ASSERT(reporter, find == a || find == b);
907
Robert Phillipsaee18c92019-09-06 11:48:27 -0400908 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700909 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800910 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
911 REPORTER_ASSERT(reporter, find2 != find);
912 find2->unref();
913 find->unref();
914}
915
bsalomon8718aaf2015-02-19 07:24:21 -0800916static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400917 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800918 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800919 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500920 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000921
bsalomon8718aaf2015-02-19 07:24:21 -0800922 GrUniqueKey key;
923 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700924
bsalomon8718aaf2015-02-19 07:24:21 -0800925 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400926 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700927
bsalomonf99e9612015-02-19 08:24:16 -0800928 // Set key on resource a.
929 a->resourcePriv().setUniqueKey(key);
930 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
931 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800932
bsalomonf99e9612015-02-19 08:24:16 -0800933 // Make sure that redundantly setting a's key works.
934 a->resourcePriv().setUniqueKey(key);
935 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800936 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800937 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
938 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800939 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
940
bsalomonf99e9612015-02-19 08:24:16 -0800941 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400942 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800943 b->resourcePriv().setUniqueKey(key);
944 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
945 b->unref();
946
947 // Still have two resources because a is still reffed.
948 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
949 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
950 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
951
952 a->unref();
953 // Now a should be gone.
954 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
955 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
956 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
957
958 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
959 // Also make b be unreffed when replacement occurs.
960 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400961 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800962 GrUniqueKey differentKey;
963 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800964 c->resourcePriv().setUniqueKey(differentKey);
965 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
966 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
967 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
968 // c replaces b and b should be immediately purged.
969 c->resourcePriv().setUniqueKey(key);
970 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
971 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
972 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
973
974 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800975 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800976 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
977 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
978 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
979
980 // Drop the ref on c, it should be kept alive because it has a unique key.
981 c->unref();
982 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
983 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
984 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
985
986 // Verify that we can find c, then remove its unique key. It should get purged immediately.
987 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
988 c->resourcePriv().removeUniqueKey();
989 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800990 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
991 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800992 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700993
994 {
995 GrUniqueKey key2;
996 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500997 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700998 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700999 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001000 d->resourcePriv().setUniqueKey(key2);
1001 }
1002
1003 GrUniqueKey key3;
1004 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001005 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001006 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001007}
1008
bsalomon8b79d232014-11-10 10:19:06 -08001009static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001010 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001011 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001012 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001013 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001014
bsalomon8718aaf2015-02-19 07:24:21 -08001015 GrUniqueKey key1, key2, key3;
1016 make_unique_key<0>(&key1, 1);
1017 make_unique_key<0>(&key2, 2);
1018 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001019
bsalomon23e619c2015-02-06 11:54:28 -08001020 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001021 TestResource* a = new TestResource(gpu);
1022 TestResource* b = new TestResource(gpu);
1023 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001024 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001025 a->resourcePriv().setUniqueKey(key1);
1026 b->resourcePriv().setUniqueKey(key2);
1027 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001028 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001029 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001030 c->unref();
1031
bsalomon8718aaf2015-02-19 07:24:21 -08001032 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1033 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1034 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001035 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001036
bsalomon8718aaf2015-02-19 07:24:21 -08001037 typedef GrUniqueKeyInvalidatedMessage Msg;
1038 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001039
1040 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001041 Bus::Post(Msg(key1, context->priv().contextID()));
1042 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001043 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001044 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001045 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1046 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001047 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001048 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001049
1050 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001051 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001052 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001053 // we still have a ref on b, c should be recycled as scratch.
1054 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001055 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001056
bsalomon23e619c2015-02-06 11:54:28 -08001057 // make b purgeable. It should be immediately deleted since it has no key.
1058 b->unref();
1059 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1060
1061 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1062 GrScratchKey scratchKey;
1063 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001064 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001065 REPORTER_ASSERT(reporter, scratch == c);
1066 SkSafeUnref(scratch);
1067
1068 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001069 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001070 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001071 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001072 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1073 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001074 REPORTER_ASSERT(reporter, !scratch);
1075 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001076}
1077
bsalomon71cb0c22014-11-14 12:10:14 -08001078static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001079 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001080 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001081 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001082 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001083
bsalomon8718aaf2015-02-19 07:24:21 -08001084 GrUniqueKey key1, key2;
1085 make_unique_key<0>(&key1, 1);
1086 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001087
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001088 sk_sp<TestResource> a(new TestResource(gpu));
1089 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001090 a->resourcePriv().setUniqueKey(key1);
1091 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001092
bsalomonc2f35b72015-01-23 07:19:22 -08001093 // Make a cycle
1094 a->setUnrefWhenDestroyed(b);
1095 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001096
bsalomonc2f35b72015-01-23 07:19:22 -08001097 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001098
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001099 TestResource* unownedA = a.release();
1100 unownedA->unref();
1101 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001102
bsalomonc2f35b72015-01-23 07:19:22 -08001103 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001104
bsalomon0ea80f42015-02-11 10:49:59 -08001105 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001106 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001107
bsalomonc2f35b72015-01-23 07:19:22 -08001108 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001109 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001110 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001111
bsalomon0ea80f42015-02-11 10:49:59 -08001112 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001113 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001114}
1115
bsalomonddf30e62015-02-19 11:38:44 -08001116static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1117 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001118 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001119 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001120
1121 SkRandom random;
1122
1123 // Run the test 2*kCount times;
1124 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001125 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001126 GrContext* context = mock.context();
1127 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001128 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001129
1130 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001131 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001132
Robert Phillipscf39f372019-09-03 10:29:20 -04001133 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001134
1135 SkTDArray<int> shouldPurgeIdxs;
1136 int purgeableCnt = 0;
1137 SkTDArray<GrGpuResource*> resourcesToUnref;
1138
1139 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1140 // unpurgeable resources.
1141 for (int j = 0; j < kCount; ++j) {
1142 GrUniqueKey key;
1143 make_unique_key<0>(&key, j);
1144
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001145 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001146 r->resourcePriv().setUniqueKey(key);
1147 if (random.nextU() % kLockedFreq) {
1148 // Make this is purgeable.
1149 r->unref();
1150 ++purgeableCnt;
1151 if (purgeableCnt <= kNumToPurge) {
1152 *shouldPurgeIdxs.append() = j;
1153 }
1154 } else {
1155 *resourcesToUnref.append() = r;
1156 }
1157 }
1158
1159 // Verify that the correct resources were purged.
1160 int currShouldPurgeIdx = 0;
1161 for (int j = 0; j < kCount; ++j) {
1162 GrUniqueKey key;
1163 make_unique_key<0>(&key, j);
1164 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1165 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1166 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1167 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001168 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001169 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001170 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001171 }
1172 SkSafeUnref(res);
1173 }
1174
1175 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1176 resourcesToUnref[j]->unref();
1177 }
1178 }
1179}
1180
Brian Salomon5e150852017-03-22 14:53:13 -04001181static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001182 Mock mock(1000000);
Brian Salomon5e150852017-03-22 14:53:13 -04001183 GrContext* context = mock.context();
1184 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001185 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001186
1187 static constexpr int kCnts[] = {1, 10, 1024};
1188 auto nowish = []() {
1189 // We sleep so that we ensure we get a value that is greater than the last call to
1190 // GrStdSteadyClock::now().
1191 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1192 auto result = GrStdSteadyClock::now();
1193 // Also sleep afterwards so we don't get this value again.
1194 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1195 return result;
1196 };
1197
1198 for (int cnt : kCnts) {
1199 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1200 new GrStdSteadyClock::time_point[cnt]);
1201 {
1202 // Insert resources and get time points between each addition.
1203 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001204 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001205 GrUniqueKey k;
1206 make_unique_key<1>(&k, i);
1207 r->resourcePriv().setUniqueKey(k);
1208 r->unref();
1209 timeStamps.get()[i] = nowish();
1210 }
1211
1212 // Purge based on the time points between resource additions. Each purge should remove
1213 // the oldest resource.
1214 for (int i = 0; i < cnt; ++i) {
1215 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1216 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1217 for (int j = 0; j < i; ++j) {
1218 GrUniqueKey k;
1219 make_unique_key<1>(&k, j);
1220 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1221 REPORTER_ASSERT(reporter, !SkToBool(r));
1222 SkSafeUnref(r);
1223 }
1224 }
1225
1226 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1227 cache->purgeAllUnlocked();
1228 }
1229
1230 // Do a similar test but where we leave refs on some resources to prevent them from being
1231 // purged.
1232 {
1233 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1234 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001235 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001236 GrUniqueKey k;
1237 make_unique_key<1>(&k, i);
1238 r->resourcePriv().setUniqueKey(k);
1239 // Leave a ref on every other resource, beginning with the first.
1240 if (SkToBool(i & 0x1)) {
1241 refedResources.get()[i / 2] = r;
1242 } else {
1243 r->unref();
1244 }
1245 timeStamps.get()[i] = nowish();
1246 }
1247
1248 for (int i = 0; i < cnt; ++i) {
1249 // Should get a resource purged every other frame.
1250 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1251 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1252 }
1253
1254 // Unref all the resources that we kept refs on in the first loop.
1255 for (int i = 0; i < (cnt / 2); ++i) {
1256 refedResources.get()[i]->unref();
1257 cache->purgeResourcesNotUsedSince(nowish());
1258 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1259 }
1260
1261 cache->purgeAllUnlocked();
1262 }
1263
1264 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1265
1266 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1267 // eviction
1268 context->flush();
1269 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001270 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001271 GrUniqueKey k;
1272 make_unique_key<1>(&k, i);
1273 r->resourcePriv().setUniqueKey(k);
1274 r->unref();
1275 }
1276 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1277 context->flush();
1278 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1279 cache->purgeResourcesNotUsedSince(nowish());
1280 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1281 }
1282}
1283
Derek Sollenberger5480a182017-05-25 16:43:59 -04001284static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001285 Mock mock(100);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001286 GrContext* context = mock.context();
1287 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001288 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001289
1290 enum TestsCase {
1291 kOnlyScratch_TestCase = 0,
1292 kPartialScratch_TestCase = 1,
1293 kAllScratch_TestCase = 2,
1294 kPartial_TestCase = 3,
1295 kAll_TestCase = 4,
1296 kNone_TestCase = 5,
1297 kEndTests_TestCase = kNone_TestCase + 1
1298 };
1299
1300 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1301
1302 GrUniqueKey key1, key2, key3;
1303 make_unique_key<0>(&key1, 1);
1304 make_unique_key<0>(&key2, 2);
1305 make_unique_key<0>(&key3, 3);
1306
1307 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001308 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1309 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1310 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001311
1312 unique1->resourcePriv().setUniqueKey(key1);
1313 unique2->resourcePriv().setUniqueKey(key2);
1314 unique3->resourcePriv().setUniqueKey(key3);
1315
Derek Sollenberger5480a182017-05-25 16:43:59 -04001316 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001317 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001318 TestResource::kA_SimulatedProperty,
1319 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001320 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001321 TestResource::kB_SimulatedProperty,
1322 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001323
1324 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1325 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1326 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1327
1328 // Add resources to the purgeable queue
1329 unique1->unref();
1330 scratch1->unref();
1331 unique2->unref();
1332 scratch2->unref();
1333 unique3->unref();
1334
1335 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1336 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1337 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1338
1339 switch(testCase) {
1340 case kOnlyScratch_TestCase: {
1341 context->purgeUnlockedResources(14, true);
1342 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1343 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1344 break;
1345 }
1346 case kPartialScratch_TestCase: {
1347 context->purgeUnlockedResources(3, true);
1348 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1349 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1350 break;
1351 }
1352 case kAllScratch_TestCase: {
1353 context->purgeUnlockedResources(50, true);
1354 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1355 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1356 break;
1357 }
1358 case kPartial_TestCase: {
1359 context->purgeUnlockedResources(13, false);
1360 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1361 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1362 break;
1363 }
1364 case kAll_TestCase: {
1365 context->purgeUnlockedResources(50, false);
1366 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1367 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1368 break;
1369 }
1370 case kNone_TestCase: {
1371 context->purgeUnlockedResources(0, true);
1372 context->purgeUnlockedResources(0, false);
1373 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1374 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1375 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1376 break;
1377 }
Brian Salomon23356442018-11-30 15:33:19 -05001378 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001379
1380 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001381 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001382 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1383 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1384
1385 }
1386}
1387
senorblanco84cd6212015-08-04 10:01:58 -07001388static void test_custom_data(skiatest::Reporter* reporter) {
1389 GrUniqueKey key1, key2;
1390 make_unique_key<0>(&key1, 1);
1391 make_unique_key<0>(&key2, 2);
1392 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001393 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001394 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1395 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1396
1397 // Test that copying a key also takes a ref on its custom data.
1398 GrUniqueKey key3 = key1;
1399 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1400}
1401
bsalomonc6363ef2015-09-24 07:07:40 -07001402static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001403 Mock mock(300);
bsalomonc6363ef2015-09-24 07:07:40 -07001404 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001405 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001406
1407 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001408 context->abandonContext();
1409
1410 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1411
1412 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1413
robertphillips8abb3702016-08-31 14:04:06 -07001414 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001415 resource->getUniqueKey();
1416 resource->wasDestroyed();
1417 resource->gpuMemorySize();
1418 resource->getContext();
1419
bsalomonc6363ef2015-09-24 07:07:40 -07001420 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001421 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001422 resource->resourcePriv().makeBudgeted();
1423 resource->resourcePriv().makeUnbudgeted();
1424 resource->resourcePriv().removeScratchKey();
1425 GrUniqueKey key;
1426 make_unique_key<0>(&key, 1);
1427 resource->resourcePriv().setUniqueKey(key);
1428 resource->resourcePriv().removeUniqueKey();
1429}
1430
Brian Salomon1090da62017-01-06 12:04:19 -05001431static void test_tags(skiatest::Reporter* reporter) {
1432#ifdef SK_DEBUG
1433 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1434 static constexpr int kLastTagIdx = 10;
1435 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1436
Robert Phillipscf39f372019-09-03 10:29:20 -04001437 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001438 GrContext* context = mock.context();
1439 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001440 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001441
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001442 // tag strings are expected to be long lived
1443 std::vector<SkString> tagStrings;
1444
Brian Salomon1090da62017-01-06 12:04:19 -05001445 SkString tagStr;
1446 int tagIdx = 0;
1447 int currTagCnt = 0;
1448
1449 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001450
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001451 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001452 GrUniqueKey key;
1453 if (currTagCnt == tagIdx) {
1454 tagIdx += 1;
1455 currTagCnt = 0;
1456 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001457 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001458 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001459 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001460 resource->resourcePriv().setUniqueKey(key);
1461 }
1462 SkASSERT(kLastTagIdx == tagIdx);
1463 SkASSERT(currTagCnt == kLastTagIdx);
1464
1465 // Test i = 0 to exercise unused tag string.
1466 for (int i = 0; i <= kLastTagIdx; ++i) {
1467 tagStr.printf("tag%d", i);
1468 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1469 }
1470#endif
1471}
1472
Robert Phillipsddc21482019-10-16 14:30:09 -04001473static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001474 Mock mock(30000);
Greg Danielc27eb722018-08-10 09:48:08 -04001475 GrContext* context = mock.context();
1476 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001477 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001478
Robert Phillipsddc21482019-10-16 14:30:09 -04001479 GrBackendTexture backends[3];
1480 GrTexture* wrapped[3];
1481 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001482
Robert Phillipsddc21482019-10-16 14:30:09 -04001483 auto releaseProc = [](void* ctx) {
1484 int* index = (int*) ctx;
1485 *index = 1;
1486 };
Greg Danielc27eb722018-08-10 09:48:08 -04001487
Robert Phillipsddc21482019-10-16 14:30:09 -04001488 for (int i = 0; i < 3; ++i) {
1489 backends[i] = context->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
1490 GrMipMapped::kNo, GrRenderable::kNo);
1491 wrapped[i] = gpu->wrapBackendTexture(backends[i], GrColorType::kRGBA_8888,
1492 GrWrapOwnership::kBorrow_GrWrapOwnership,
1493 (i < 2) ? GrWrapCacheable::kYes
1494 : GrWrapCacheable::kNo,
1495 GrIOType::kRead_GrIOType).release();
1496 wrapped[i]->setRelease(releaseProc, &freed[i]);
1497 }
1498
1499 cache->insertDelayedTextureUnref(wrapped[0]);
1500 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001501
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001502 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1503 // is because inserting it as a cross-context resource actually holds a ref until the
1504 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001505 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001506
Robert Phillipsddc21482019-10-16 14:30:09 -04001507 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001508
1509 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001510 wrapped[0]->unref();
1511 wrapped[1]->unref();
1512 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001513
Robert Phillipsddc21482019-10-16 14:30:09 -04001514 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001515
1516 // This should free nothing since no messages were sent.
1517 cache->purgeAsNeeded();
1518
Robert Phillipsddc21482019-10-16 14:30:09 -04001519 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1520
Greg Danielc27eb722018-08-10 09:48:08 -04001521 // Send message to free the first resource
Robert Phillipsddc21482019-10-16 14:30:09 -04001522 GrTextureFreedMessage msg1{wrapped[0], context->priv().contextID()};
1523 SkMessageBus<GrTextureFreedMessage>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001524 cache->purgeAsNeeded();
1525
Robert Phillipsddc21482019-10-16 14:30:09 -04001526 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1527 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001528
Robert Phillipsddc21482019-10-16 14:30:09 -04001529 GrTextureFreedMessage msg2{wrapped[2], context->priv().contextID()};
1530 SkMessageBus<GrTextureFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001531 cache->purgeAsNeeded();
1532
Robert Phillipsddc21482019-10-16 14:30:09 -04001533 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1534 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001535
1536 mock.reset();
1537
Robert Phillipsddc21482019-10-16 14:30:09 -04001538 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001539}
1540
Brian Salomondcfca432017-11-15 15:48:03 -05001541DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001542 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001543 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001544 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001545 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001546 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001547 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001548 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001549 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001550 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001551 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001552 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001553 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001554 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001555 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001556 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001557 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001558 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001559 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001560 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001561}
1562
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001563// This simulates a portion of Chrome's context abandonment processing.
1564// Please see: crbug.com/1011368 and crbug.com/1014993
1565DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
1566 GrContext* context = ctxInfo.grContext();
1567 GrGpu* gpu = context->priv().getGpu();
1568 GrResourceCache* cache = context->priv().getResourceCache();
1569
1570 GrBackendTexture backend = context->createBackendTexture(16, 16,
1571 SkColorType::kRGBA_8888_SkColorType,
1572 GrMipMapped::kNo, GrRenderable::kNo);
1573 GrTexture* tex = gpu->wrapBackendTexture(backend, GrColorType::kRGBA_8888,
1574 GrWrapOwnership::kBorrow_GrWrapOwnership,
1575 GrWrapCacheable::kYes,
1576 GrIOType::kRead_GrIOType).release();
1577
1578 auto releaseProc = [](void* ctx) {
1579 int* index = (int*) ctx;
1580 *index = 1;
1581 };
1582
1583 int freed = 0;
1584
1585 tex->setRelease(releaseProc, &freed);
1586
1587 cache->insertDelayedTextureUnref(tex);
1588
1589 // Now only the cache is holding a ref to this texture
1590 tex->unref();
1591
1592 REPORTER_ASSERT(reporter, 0 == freed);
1593
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001594 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1595 // for all the backends for consistency.
1596 context->deleteBackendTexture(backend);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001597 context->abandonContext();
1598
1599 REPORTER_ASSERT(reporter, 1 == freed);
1600
1601 // In the past, creating this message could cause an exception due to
1602 // an un-safe downcast from GrTexture to GrGpuResource
1603 GrTextureFreedMessage msg{tex, context->priv().contextID()};
1604 SkMessageBus<GrTextureFreedMessage>::Post(msg);
1605
Greg Danielf0e04f02019-12-04 15:17:54 -05001606 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001607 context->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001608}
1609
Robert Phillipsd6214d42016-11-07 08:23:48 -05001610////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001611static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001612 GrRenderable renderable,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001613 int width, int height,
1614 int sampleCnt) {
1615 GrSurfaceDesc desc;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001616 desc.fWidth = width;
1617 desc.fHeight = height;
Brian Salomon4eb38b72019-08-05 12:58:39 -04001618 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomona90382f2019-09-17 09:01:56 -04001619 return provider->createTexture(desc, format, renderable, sampleCnt, GrMipMapped::kNo,
1620 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001621}
1622
Robert Phillips0a15cc62019-07-30 12:49:10 -04001623static sk_sp<GrTextureProxy> make_mipmap_proxy(GrContext * context,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001624 GrRenderable renderable,
Robert Phillipse78b7252017-04-06 07:59:41 -04001625 int width, int height,
1626 int sampleCnt) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001627 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1628 const GrCaps* caps = context->priv().caps();
1629
Robert Phillipsd6214d42016-11-07 08:23:48 -05001630 GrSurfaceDesc desc;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001631 desc.fWidth = width;
1632 desc.fHeight = height;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001633
Robert Phillips0a15cc62019-07-30 12:49:10 -04001634 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1635 GrRenderable::kNo);
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001636 auto origin = renderable == GrRenderable::kYes ? kBottomLeft_GrSurfaceOrigin
1637 : kTopLeft_GrSurfaceOrigin;
Greg Daniel47c20e82020-01-21 14:29:57 -05001638 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001639
Greg Daniel47c20e82020-01-21 14:29:57 -05001640 return proxyProvider->createProxy(format, desc, swizzle, renderable, sampleCnt, origin,
Brian Salomonbeb7f522019-08-30 16:19:42 -04001641 GrMipMapped::kYes, SkBackingFit::kExact, SkBudgeted::kYes,
1642 GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001643}
1644
1645// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1646// Texture-only, both-RT-and-Texture and MIPmapped
1647DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1648 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001649 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001650 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001651
Robert Phillipsd6214d42016-11-07 08:23:48 -05001652 static const int kSize = 64;
1653
Robert Phillipsd6214d42016-11-07 08:23:48 -05001654 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001655 {
1656 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001657
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001658 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001659 size_t size = tex->gpuMemorySize();
1660 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1661
Greg Daniel6fa62e22019-08-07 15:52:37 -04001662 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001663 if (sampleCount >= 4) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001664 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001665 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001666 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001667 REPORTER_ASSERT(reporter,
1668 kSize*kSize*4 == size || // msaa4 failed
1669 kSize*kSize*4*sampleCount == size || // auto-resolving
1670 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001671 }
1672
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001673 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001674 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001675 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001676 }
1677
Robert Phillipsd6214d42016-11-07 08:23:48 -05001678 // Mipmapped versions
Greg Daniel4065d452018-11-16 15:43:41 -05001679 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001680 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001681
Robert Phillips0a15cc62019-07-30 12:49:10 -04001682 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001683 size_t size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001684 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1685
Greg Daniel6fa62e22019-08-07 15:52:37 -04001686 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001687 if (sampleCount >= 4) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001688 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, kSize, sampleCount);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001689 size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001690 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001691 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1692 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1693 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001694 }
Robert Phillips1b352562017-04-05 18:56:21 +00001695
Robert Phillips0a15cc62019-07-30 12:49:10 -04001696 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001697 size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001698 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1699 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001700}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001701
1702#if GR_GPU_STATS
1703DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
1704 GrContext* context = ctxInfo.grContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001705 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001706
1707 // Helper that determines if cache is overbudget.
1708 auto overbudget = [context] {
1709 int uNum;
1710 size_t uSize;
1711 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001712 size_t bSize = context->getResourceCacheLimit();
1713 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001714 };
1715
1716 // Helper that does a trivial draw to a surface.
1717 auto drawToSurf = [](SkSurface* surf) {
1718 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1719 };
1720
1721 // Helper that checks whether a flush has occurred between calls.
1722 int baseFlushCount = 0;
1723 auto getFlushCountDelta = [context, &baseFlushCount]() {
1724 int cur = context->priv().getGpu()->stats()->numFinishFlushes();
1725 int delta = cur - baseFlushCount;
1726 baseFlushCount = cur;
1727 return delta;
1728 };
1729
1730 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1731 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1732 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1733
1734 drawToSurf(surf1.get());
1735 drawToSurf(surf2.get());
1736
1737 // Flush each surface once to ensure that their backing stores are allocated.
1738 surf1->flush();
1739 surf2->flush();
1740 REPORTER_ASSERT(reporter, overbudget());
1741 getFlushCountDelta();
1742
1743 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1744 drawToSurf(surf1.get());
1745 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1746 drawToSurf(surf2.get());
1747 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1748 REPORTER_ASSERT(reporter, overbudget());
1749
1750 // Make surf1 purgeable. Drawing to surf2 should flush.
1751 surf1->flush();
1752 surf1.reset();
1753 drawToSurf(surf2.get());
1754 REPORTER_ASSERT(reporter, getFlushCountDelta());
1755 REPORTER_ASSERT(reporter, overbudget());
1756}
1757#endif