blob: bc9bb18e422eba608e63d3108b999279845e24d8 [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) {
Brian Salomon4eb38b72019-08-05 12:58:39 -040092 auto format =
93 provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
Brian Salomona56a7462020-02-07 14:17:25 -050094 sk_sp<GrTexture> tex(provider->createTexture({size, size}, format, GrRenderable::kYes,
95 sampleCount, GrMipMapped::kNo, budgeted,
96 GrProtected::kNo));
Robert Phillipsc0192e32017-09-21 12:00:26 -040097 if (!tex || !tex->asRenderTarget()) {
98 return nullptr;
99 }
100
Chris Daltoneffee202019-07-01 22:28:03 -0600101 if (!provider->attachStencilAttachment(tex->asRenderTarget(), sampleCount)) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400102 return nullptr;
103 }
104 SkASSERT(get_SB(tex->asRenderTarget()));
105
106 return sk_ref_sp(tex->asRenderTarget());
107}
108
bsalomon11abd8d2016-10-14 08:13:48 -0700109// This currently fails on ES3 ANGLE contexts
110DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000111 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700112 GrContext* context = ctxInfo.grContext();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400113 const GrCaps* caps = context->priv().caps();
114
115 if (caps->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700116 return;
117 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400118
Robert Phillips9da87e02019-02-04 13:26:26 -0500119 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400120
Greg Daniel5c96db82019-07-09 14:06:58 -0400121 GrColorType grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400122 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400123
Brian Salomonbdecacf2018-02-02 20:32:49 -0500124 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400125 REPORTER_ASSERT(reporter, smallRT0);
126
127 {
128 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
130 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400131
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800133 }
134
Robert Phillipsc0192e32017-09-21 12:00:26 -0400135 {
136 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500137 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400138 REPORTER_ASSERT(reporter, smallRT2);
139
140 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800141 }
142
Robert Phillipsc0192e32017-09-21 12:00:26 -0400143 {
144 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500145 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400146 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800147
Robert Phillipsc0192e32017-09-21 12:00:26 -0400148 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800149 }
bsalomon02a44a42015-02-19 09:09:00 -0800150
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400151 int smallSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400152 context->priv().caps()->getRenderTargetSampleCount(2, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500153 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700154 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500155 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
156 smallSampleCount, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400157 REPORTER_ASSERT(reporter, smallMSAART0);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400158
159 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
160
161 {
162 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500163 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
164 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400165 SkBudgeted::kNo);
166 REPORTER_ASSERT(reporter, smallMSAART1);
167
168 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800169 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400170
Brian Salomonbdecacf2018-02-02 20:32:49 -0500171 // But one with a larger sample count should not. (Also check that the two requests didn't
172 // rounded up to the same actual sample count or else they could share.).
Greg Daniel6fa62e22019-08-07 15:52:37 -0400173 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(5, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500174 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500175 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
176 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400177 SkBudgeted::kNo);
178 REPORTER_ASSERT(reporter, smallMSAART2);
179
180 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800181 }
182 }
183}
184
bsalomon68d91342016-04-12 09:59:58 -0700185DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700186 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500187 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
188 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700189 // this test is only valid for GL
190 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700191 return;
192 }
193
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500194 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700195 static const int kW = 100;
196 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700197
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400198 backendTextures[0] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
199 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400200 GrMipMapped::kNo, GrRenderable::kNo,
201 GrProtected::kNo);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400202 backendTextures[1] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
203 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400204 GrMipMapped::kNo, GrRenderable::kNo,
205 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500206 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
207 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
208 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
209 return;
210 }
jvanverth672bb7f2015-07-13 07:19:57 -0700211
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 context->resetContext();
213
Robert Phillips6be756b2018-01-16 15:07:54 -0500214 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400215 backendTextures[0], GrColorType::kRGBA_8888,
216 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217
Robert Phillips6be756b2018-01-16 15:07:54 -0500218 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400219 backendTextures[1], GrColorType::kRGBA_8888,
220 kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221
Brian Osman85d34b22017-05-10 12:06:26 -0400222 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
223 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224 return;
225 }
226
halcanary96fcdcc2015-08-27 07:41:13 -0700227 borrowed.reset(nullptr);
228 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
230 context->flush();
231
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500232 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
233 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700234
235 REPORTER_ASSERT(reporter, borrowedIsAlive);
236 REPORTER_ASSERT(reporter, !adoptedIsAlive);
237
Brian Salomone64b0642018-03-07 11:47:54 -0500238 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400239 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500240 }
241 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400242 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500243 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244
245 context->resetContext();
246}
247
bsalomon6d3fe022014-07-25 08:35:45 -0700248class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800249 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000250public:
robertphillips6e83ac72015-08-13 05:19:14 -0700251 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700252
bsalomon1c60dfe2015-01-21 09:32:40 -0800253 /** Property that distinctly categorizes the resource.
254 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800255 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800256
kkinnunen2e6055b2016-04-22 01:48:29 -0700257 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
258 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700259 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800260 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700261 , fProperty(kA_SimulatedProperty)
262 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800263 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800265 }
266
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400268 SimulatedProperty property, size_t size = kDefaultSize) {
269 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800270 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500271 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
272 size_t size = kDefaultSize) {
273 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000274 }
275
Brian Salomond3b65972017-03-22 12:05:03 -0400276 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800277 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278 }
279
bsalomon33435572014-11-05 14:47:41 -0800280 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400282 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
283 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284 }
285
bsalomon1c60dfe2015-01-21 09:32:40 -0800286 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
287 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
288 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800289 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
290 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800291 }
292 }
293
294 static size_t ExpectedScratchKeySize() {
295 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
296 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000297private:
bsalomon24db3b12015-01-23 04:24:04 -0800298 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800299
Greg Danielda86e282018-06-13 09:41:19 -0400300 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
301 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700302 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700303 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400304 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 , fProperty(property)
306 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800307 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700308 this->registerWithCache(budgeted);
309 }
310
311 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500312 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
313 : INHERITED(gpu)
314 , fToDelete(nullptr)
315 , fSize(size)
316 , fProperty(kA_SimulatedProperty)
317 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700318 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500319 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700320 }
321
322 void computeScratchKey(GrScratchKey* key) const override {
323 if (fIsScratch) {
324 ComputeScratchKey(fProperty, key);
325 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800326 }
327
mtklein36352bf2015-03-25 18:17:31 -0700328 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400329 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800330
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400331 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000332 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800333 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800334 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700335 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700336 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000337};
bsalomon33435572014-11-05 14:47:41 -0800338int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000339
bsalomonc2f35b72015-01-23 07:19:22 -0800340class Mock {
341public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400342 Mock(size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400343 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800344 SkASSERT(fContext);
Robert Phillipscf39f372019-09-03 10:29:20 -0400345 fContext->setResourceCacheLimit(maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500346 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800347 cache->purgeAllUnlocked();
348 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800349 }
bsalomonc2f35b72015-01-23 07:19:22 -0800350
Robert Phillips9da87e02019-02-04 13:26:26 -0500351 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800352
Hal Canary342b7ac2016-11-04 11:49:42 -0400353 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800354
Greg Danielc27eb722018-08-10 09:48:08 -0400355 void reset() {
356 fContext.reset();
357 }
358
bsalomonc2f35b72015-01-23 07:19:22 -0800359private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400360 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800361};
362
363static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400364 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800365 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800366 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500367 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800368
369 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400370 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
371 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
372 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
373 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800374
375 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800376 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800377 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800378 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800379
380 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800381 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800382 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
383
bsalomon8718aaf2015-02-19 07:24:21 -0800384 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800385
386 a->unref();
387 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800388 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800389 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800390 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800391
392 c->unref();
393 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800394 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800395 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800396 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800397
398 d->unref();
399 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800400 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
401 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800402
403 b->unref();
404 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800405 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
406 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800407}
408
bsalomon24db3b12015-01-23 04:24:04 -0800409// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500410template <int>
411static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800412 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500413 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800414 builder[0] = data;
415}
416
Robert Phillips6eba0632018-03-28 12:25:42 -0400417static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400418 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400419 GrContext* context = mock.context();
420 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500421 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400422
423 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
424 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400425 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400426
427 GrUniqueKey uniqueKey;
428 make_unique_key<0>(&uniqueKey, 0);
429
430 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400431 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400432 b->resourcePriv().setUniqueKey(uniqueKey);
433
434 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400435 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400436
437 GrUniqueKey uniqueKey2;
438 make_unique_key<0>(&uniqueKey2, 1);
439
440 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400441 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400442 d->resourcePriv().setUniqueKey(uniqueKey2);
443
444
445 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
446 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
447 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
448 d->gpuMemorySize() == cache->getResourceBytes());
449
450 // Should be safe to purge without deleting the resources since we still have refs.
451 cache->purgeUnlockedResources(false);
452 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
453
454 // Unref them all. Since they all have keys they should remain in the cache.
455
456 a->unref();
457 b->unref();
458 c->unref();
459 d->unref();
460 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
461 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
462 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
463 d->gpuMemorySize() == cache->getResourceBytes());
464
465 // Purge only the two scratch resources
466 cache->purgeUnlockedResources(true);
467
468 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
469 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
470 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
471 cache->getResourceBytes());
472
473 // Purge the uniquely keyed resources
474 cache->purgeUnlockedResources(false);
475
476 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
477 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
478 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
479}
480
bsalomon84c8e622014-11-17 09:33:27 -0800481static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400482 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800483 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800484 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500485 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800486
bsalomon8718aaf2015-02-19 07:24:21 -0800487 GrUniqueKey uniqueKey;
488 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800489
bsalomon8718aaf2015-02-19 07:24:21 -0800490 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800491 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400492 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
493 10);
494 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800495 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500496 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
497 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
498 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800499
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500500 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800501 GrUniqueKey uniqueKey2;
502 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500503 GrUniqueKey uniqueKey3;
504 make_unique_key<0>(&uniqueKey3, 2);
505 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
506 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
507 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
508 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
509 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
510 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400511
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500512 // Remove the extra refs we just added.
513 SkSafeUnref(wrappedCacheableViaKey);
514 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800515
516 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500517 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800518 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500519 wrappedCacheable->gpuMemorySize() +
520 wrappedUncacheable->gpuMemorySize() +
521 unbudgeted->gpuMemorySize() ==
522 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800523 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800524 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800525 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400526 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800527
bsalomon63c992f2015-01-23 12:47:59 -0800528 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800529 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500530 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800531 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500532 wrappedCacheable->gpuMemorySize() +
533 wrappedUncacheable->gpuMemorySize() +
534 unbudgeted->gpuMemorySize() ==
535 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800536 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800537 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800538 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400539 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800540
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500541 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
542 // However, unreffing the uncacheable wrapped resource should free it.
543 wrappedCacheable->unref();
544 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400545 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800546 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500547 wrappedCacheable->gpuMemorySize() +
548 unbudgeted->gpuMemorySize() ==
549 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500550 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800551
bsalomon84c8e622014-11-17 09:33:27 -0800552 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500553 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800554 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500555 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
556 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
557 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800558 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500559 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500560
561 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
562 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
563 if (wrappedCacheableViaKey) {
564 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
565 wrappedCacheable->unref();
566 }
567 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
568 // resource should immediately delete it.
569 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
570
571 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500572 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
573 wrappedUncacheable->gpuMemorySize() +
574 unbudgeted->gpuMemorySize() ==
575 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
577 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400578 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800579
580 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400581 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800582 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500583 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
584 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
585 wrappedUncacheable->gpuMemorySize() ==
586 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800587 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
588 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400589 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800590
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500591 // Unreffing the wrapped resources (with no unique key) should free them right away.
592 wrappedUncacheable->unref();
593 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800594 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
595 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
596 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
597 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400598 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800599
600 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
602 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
603 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
604 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400605 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800606}
607
bsalomon5236cf42015-01-14 10:42:08 -0800608static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400609 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800610 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800611 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500612 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800613
bsalomon8718aaf2015-02-19 07:24:21 -0800614 GrUniqueKey uniqueKey;
615 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800616
617 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800618 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800619 TestResource* wrapped;
620 TestResource* unbudgeted;
621
622 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500623 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400624 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700625
bsalomon5236cf42015-01-14 10:42:08 -0800626 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800627 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
628 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
629 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
630 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400631 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800632
Greg Danielda86e282018-06-13 09:41:19 -0400633 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800634 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800635 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
637 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
638 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
639 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400640 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800641
bsalomon0ea80f42015-02-11 10:49:59 -0800642 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500643 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800644 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
645 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
646 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
647 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400648 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800649
650 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800651 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
652 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
653 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
654 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400655 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800656
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500657 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800658 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
659 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
660 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
661 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400662 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800663
664 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800665 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
666 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
667 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
668 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400669 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800670
bsalomon0ea80f42015-02-11 10:49:59 -0800671 cache->purgeAllUnlocked();
672 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
673 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
674 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
675 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400676 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800677}
678
bsalomon3582d3e2015-02-13 14:20:05 -0800679// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
680void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
681/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400682 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800683 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800684 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500685 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800686
687 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500688 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800689 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800690 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800691
692 size_t size = resource->gpuMemorySize();
693 for (int i = 0; i < 2; ++i) {
694 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800695 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800696 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500697 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500698 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400699 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800700 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
701 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
702 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
703 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400704 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800705
706 // Once it is unrefed, it should become available as scratch.
707 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800708 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
709 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
710 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
711 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400712 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400713 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800714 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800715 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800716 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500717 REPORTER_ASSERT(reporter,
718 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800719
720 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700721 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800722 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800723 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800724 } else {
725 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800726 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800727 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
728 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
729 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
730 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400731 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800732 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800733 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500734 REPORTER_ASSERT(reporter,
735 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800736
737 // now when it is unrefed it should die since it has no key.
738 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800739 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
740 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
741 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
742 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400743 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800744 }
bsalomon8b79d232014-11-10 10:19:06 -0800745 }
bsalomonc2f35b72015-01-23 07:19:22 -0800746}
747
748static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400749 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800750 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800751 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500752 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800753
bsalomon8b79d232014-11-10 10:19:06 -0800754 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500755 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700756 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400757 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500758 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700759 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400760 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800761 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800762 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800763 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400764 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800765
766 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800767 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800768
bsalomon0ea80f42015-02-11 10:49:59 -0800769 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800770 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800771 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
772 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800773 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800774 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800775
bsalomon63c992f2015-01-23 12:47:59 -0800776 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800777 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800778 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800779 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800780
781 // Unref but don't purge
782 a->unref();
783 b->unref();
784 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800785 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800786
bsalomon63c992f2015-01-23 12:47:59 -0800787 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800788 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800789 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800790 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
791 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800792}
793
bsalomon10e23ca2014-11-25 05:52:06 -0800794static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400795 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800796 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800797 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500798 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800799
bsalomon10e23ca2014-11-25 05:52:06 -0800800 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500801 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800802 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500803 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800804 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800805 a->unref();
806 b->unref();
807
bsalomon1c60dfe2015-01-21 09:32:40 -0800808 GrScratchKey scratchKey;
809 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800810 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800811 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400812 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800813
bsalomon0ea80f42015-02-11 10:49:59 -0800814 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800815 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800816 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800817 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
818 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800819
820 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400821 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800822 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800823 // It's still alive, but not cached by scratch key anymore
824 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800825 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
826 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800827
828 // The cache should immediately delete it when it's unrefed since it isn't accessible.
829 find->unref();
830 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800831 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
832 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800833
834 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400835 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800836 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800837 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800838 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
839 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800840
841 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800842 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800843 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800844 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
845 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800846
847 find->unref();
848 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800849 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
850 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800851}
852
bsalomon1c60dfe2015-01-21 09:32:40 -0800853static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400854 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800855 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800856 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500857 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800858
859 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500860 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800861 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500862 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800863 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800864 a->unref();
865 b->unref();
866
867 GrScratchKey scratchKey;
868 // Ensure that scratch key comparison and assignment is consistent.
869 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800870 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800871 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800872 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800873 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
874 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
875 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
876 scratchKey = scratchKey1;
877 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
878 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
879 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
880 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
881 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
882 scratchKey = scratchKey2;
883 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
884 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
885 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
886 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
887 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
888
889 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800890 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800891 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400892 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800893
894 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800895 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400896 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700897 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800898 find->unref();
899
900 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400901 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700902 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800903 REPORTER_ASSERT(reporter, find == a || find == b);
904
Robert Phillipsaee18c92019-09-06 11:48:27 -0400905 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700906 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800907 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
908 REPORTER_ASSERT(reporter, find2 != find);
909 find2->unref();
910 find->unref();
911}
912
bsalomon8718aaf2015-02-19 07:24:21 -0800913static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400914 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800915 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800916 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500917 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000918
bsalomon8718aaf2015-02-19 07:24:21 -0800919 GrUniqueKey key;
920 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700921
bsalomon8718aaf2015-02-19 07:24:21 -0800922 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400923 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700924
bsalomonf99e9612015-02-19 08:24:16 -0800925 // Set key on resource a.
926 a->resourcePriv().setUniqueKey(key);
927 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
928 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800929
bsalomonf99e9612015-02-19 08:24:16 -0800930 // Make sure that redundantly setting a's key works.
931 a->resourcePriv().setUniqueKey(key);
932 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800933 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800934 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
935 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800936 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
937
bsalomonf99e9612015-02-19 08:24:16 -0800938 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400939 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800940 b->resourcePriv().setUniqueKey(key);
941 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
942 b->unref();
943
944 // Still have two resources because a is still reffed.
945 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
946 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
947 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
948
949 a->unref();
950 // Now a should be gone.
951 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
952 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
953 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
954
955 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
956 // Also make b be unreffed when replacement occurs.
957 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400958 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800959 GrUniqueKey differentKey;
960 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800961 c->resourcePriv().setUniqueKey(differentKey);
962 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
963 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
964 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
965 // c replaces b and b should be immediately purged.
966 c->resourcePriv().setUniqueKey(key);
967 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
968 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
969 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
970
971 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800972 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800973 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
974 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
975 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
976
977 // Drop the ref on c, it should be kept alive because it has a unique key.
978 c->unref();
979 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
980 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
981 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
982
983 // Verify that we can find c, then remove its unique key. It should get purged immediately.
984 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
985 c->resourcePriv().removeUniqueKey();
986 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800987 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
988 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800989 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700990
991 {
992 GrUniqueKey key2;
993 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500994 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700995 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700996 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700997 d->resourcePriv().setUniqueKey(key2);
998 }
999
1000 GrUniqueKey key3;
1001 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001002 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001003 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001004}
1005
bsalomon8b79d232014-11-10 10:19:06 -08001006static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001007 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001008 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001009 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001010 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001011
bsalomon8718aaf2015-02-19 07:24:21 -08001012 GrUniqueKey key1, key2, key3;
1013 make_unique_key<0>(&key1, 1);
1014 make_unique_key<0>(&key2, 2);
1015 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001016
bsalomon23e619c2015-02-06 11:54:28 -08001017 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001018 TestResource* a = new TestResource(gpu);
1019 TestResource* b = new TestResource(gpu);
1020 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001021 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001022 a->resourcePriv().setUniqueKey(key1);
1023 b->resourcePriv().setUniqueKey(key2);
1024 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001025 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001026 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001027 c->unref();
1028
bsalomon8718aaf2015-02-19 07:24:21 -08001029 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1030 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1031 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001032 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001033
bsalomon8718aaf2015-02-19 07:24:21 -08001034 typedef GrUniqueKeyInvalidatedMessage Msg;
1035 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001036
1037 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001038 Bus::Post(Msg(key1, context->priv().contextID()));
1039 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001040 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001041 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001042 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1043 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001044 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001045 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001046
1047 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001048 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001049 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001050 // we still have a ref on b, c should be recycled as scratch.
1051 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001052 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001053
bsalomon23e619c2015-02-06 11:54:28 -08001054 // make b purgeable. It should be immediately deleted since it has no key.
1055 b->unref();
1056 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1057
1058 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1059 GrScratchKey scratchKey;
1060 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001061 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001062 REPORTER_ASSERT(reporter, scratch == c);
1063 SkSafeUnref(scratch);
1064
1065 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001066 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001067 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001068 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001069 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1070 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001071 REPORTER_ASSERT(reporter, !scratch);
1072 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001073}
1074
bsalomon71cb0c22014-11-14 12:10:14 -08001075static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001076 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001077 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001078 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001079 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001080
bsalomon8718aaf2015-02-19 07:24:21 -08001081 GrUniqueKey key1, key2;
1082 make_unique_key<0>(&key1, 1);
1083 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001084
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001085 sk_sp<TestResource> a(new TestResource(gpu));
1086 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001087 a->resourcePriv().setUniqueKey(key1);
1088 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001089
bsalomonc2f35b72015-01-23 07:19:22 -08001090 // Make a cycle
1091 a->setUnrefWhenDestroyed(b);
1092 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001093
bsalomonc2f35b72015-01-23 07:19:22 -08001094 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001095
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001096 TestResource* unownedA = a.release();
1097 unownedA->unref();
1098 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001099
bsalomonc2f35b72015-01-23 07:19:22 -08001100 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001101
bsalomon0ea80f42015-02-11 10:49:59 -08001102 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001103 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001104
bsalomonc2f35b72015-01-23 07:19:22 -08001105 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001106 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001107 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001108
bsalomon0ea80f42015-02-11 10:49:59 -08001109 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001110 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001111}
1112
bsalomonddf30e62015-02-19 11:38:44 -08001113static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1114 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001115 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001116 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001117
1118 SkRandom random;
1119
1120 // Run the test 2*kCount times;
1121 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001122 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001123 GrContext* context = mock.context();
1124 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001125 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001126
1127 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001128 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001129
Robert Phillipscf39f372019-09-03 10:29:20 -04001130 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001131
1132 SkTDArray<int> shouldPurgeIdxs;
1133 int purgeableCnt = 0;
1134 SkTDArray<GrGpuResource*> resourcesToUnref;
1135
1136 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1137 // unpurgeable resources.
1138 for (int j = 0; j < kCount; ++j) {
1139 GrUniqueKey key;
1140 make_unique_key<0>(&key, j);
1141
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001142 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001143 r->resourcePriv().setUniqueKey(key);
1144 if (random.nextU() % kLockedFreq) {
1145 // Make this is purgeable.
1146 r->unref();
1147 ++purgeableCnt;
1148 if (purgeableCnt <= kNumToPurge) {
1149 *shouldPurgeIdxs.append() = j;
1150 }
1151 } else {
1152 *resourcesToUnref.append() = r;
1153 }
1154 }
1155
1156 // Verify that the correct resources were purged.
1157 int currShouldPurgeIdx = 0;
1158 for (int j = 0; j < kCount; ++j) {
1159 GrUniqueKey key;
1160 make_unique_key<0>(&key, j);
1161 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1162 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1163 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1164 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001165 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001166 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001167 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001168 }
1169 SkSafeUnref(res);
1170 }
1171
1172 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1173 resourcesToUnref[j]->unref();
1174 }
1175 }
1176}
1177
Brian Salomon5e150852017-03-22 14:53:13 -04001178static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001179 Mock mock(1000000);
Brian Salomon5e150852017-03-22 14:53:13 -04001180 GrContext* context = mock.context();
1181 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001182 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001183
1184 static constexpr int kCnts[] = {1, 10, 1024};
1185 auto nowish = []() {
1186 // We sleep so that we ensure we get a value that is greater than the last call to
1187 // GrStdSteadyClock::now().
1188 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1189 auto result = GrStdSteadyClock::now();
1190 // Also sleep afterwards so we don't get this value again.
1191 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1192 return result;
1193 };
1194
1195 for (int cnt : kCnts) {
1196 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1197 new GrStdSteadyClock::time_point[cnt]);
1198 {
1199 // Insert resources and get time points between each addition.
1200 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001201 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001202 GrUniqueKey k;
1203 make_unique_key<1>(&k, i);
1204 r->resourcePriv().setUniqueKey(k);
1205 r->unref();
1206 timeStamps.get()[i] = nowish();
1207 }
1208
1209 // Purge based on the time points between resource additions. Each purge should remove
1210 // the oldest resource.
1211 for (int i = 0; i < cnt; ++i) {
1212 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1213 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1214 for (int j = 0; j < i; ++j) {
1215 GrUniqueKey k;
1216 make_unique_key<1>(&k, j);
1217 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1218 REPORTER_ASSERT(reporter, !SkToBool(r));
1219 SkSafeUnref(r);
1220 }
1221 }
1222
1223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1224 cache->purgeAllUnlocked();
1225 }
1226
1227 // Do a similar test but where we leave refs on some resources to prevent them from being
1228 // purged.
1229 {
1230 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1231 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001232 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001233 GrUniqueKey k;
1234 make_unique_key<1>(&k, i);
1235 r->resourcePriv().setUniqueKey(k);
1236 // Leave a ref on every other resource, beginning with the first.
1237 if (SkToBool(i & 0x1)) {
1238 refedResources.get()[i / 2] = r;
1239 } else {
1240 r->unref();
1241 }
1242 timeStamps.get()[i] = nowish();
1243 }
1244
1245 for (int i = 0; i < cnt; ++i) {
1246 // Should get a resource purged every other frame.
1247 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1248 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1249 }
1250
1251 // Unref all the resources that we kept refs on in the first loop.
1252 for (int i = 0; i < (cnt / 2); ++i) {
1253 refedResources.get()[i]->unref();
1254 cache->purgeResourcesNotUsedSince(nowish());
1255 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1256 }
1257
1258 cache->purgeAllUnlocked();
1259 }
1260
1261 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1262
1263 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1264 // eviction
1265 context->flush();
1266 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001267 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001268 GrUniqueKey k;
1269 make_unique_key<1>(&k, i);
1270 r->resourcePriv().setUniqueKey(k);
1271 r->unref();
1272 }
1273 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1274 context->flush();
1275 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1276 cache->purgeResourcesNotUsedSince(nowish());
1277 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1278 }
1279}
1280
Derek Sollenberger5480a182017-05-25 16:43:59 -04001281static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001282 Mock mock(100);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001283 GrContext* context = mock.context();
1284 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001285 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001286
1287 enum TestsCase {
1288 kOnlyScratch_TestCase = 0,
1289 kPartialScratch_TestCase = 1,
1290 kAllScratch_TestCase = 2,
1291 kPartial_TestCase = 3,
1292 kAll_TestCase = 4,
1293 kNone_TestCase = 5,
1294 kEndTests_TestCase = kNone_TestCase + 1
1295 };
1296
1297 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1298
1299 GrUniqueKey key1, key2, key3;
1300 make_unique_key<0>(&key1, 1);
1301 make_unique_key<0>(&key2, 2);
1302 make_unique_key<0>(&key3, 3);
1303
1304 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001305 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1306 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1307 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001308
1309 unique1->resourcePriv().setUniqueKey(key1);
1310 unique2->resourcePriv().setUniqueKey(key2);
1311 unique3->resourcePriv().setUniqueKey(key3);
1312
Derek Sollenberger5480a182017-05-25 16:43:59 -04001313 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001314 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001315 TestResource::kA_SimulatedProperty,
1316 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001317 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001318 TestResource::kB_SimulatedProperty,
1319 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001320
1321 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1322 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1323 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1324
1325 // Add resources to the purgeable queue
1326 unique1->unref();
1327 scratch1->unref();
1328 unique2->unref();
1329 scratch2->unref();
1330 unique3->unref();
1331
1332 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1333 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1334 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1335
1336 switch(testCase) {
1337 case kOnlyScratch_TestCase: {
1338 context->purgeUnlockedResources(14, true);
1339 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1340 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1341 break;
1342 }
1343 case kPartialScratch_TestCase: {
1344 context->purgeUnlockedResources(3, true);
1345 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1346 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1347 break;
1348 }
1349 case kAllScratch_TestCase: {
1350 context->purgeUnlockedResources(50, true);
1351 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1352 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1353 break;
1354 }
1355 case kPartial_TestCase: {
1356 context->purgeUnlockedResources(13, false);
1357 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1358 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1359 break;
1360 }
1361 case kAll_TestCase: {
1362 context->purgeUnlockedResources(50, false);
1363 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1364 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1365 break;
1366 }
1367 case kNone_TestCase: {
1368 context->purgeUnlockedResources(0, true);
1369 context->purgeUnlockedResources(0, false);
1370 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1371 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1372 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1373 break;
1374 }
Brian Salomon23356442018-11-30 15:33:19 -05001375 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001376
1377 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001378 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001379 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1380 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1381
1382 }
1383}
1384
senorblanco84cd6212015-08-04 10:01:58 -07001385static void test_custom_data(skiatest::Reporter* reporter) {
1386 GrUniqueKey key1, key2;
1387 make_unique_key<0>(&key1, 1);
1388 make_unique_key<0>(&key2, 2);
1389 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001390 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001391 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1392 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1393
1394 // Test that copying a key also takes a ref on its custom data.
1395 GrUniqueKey key3 = key1;
1396 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1397}
1398
bsalomonc6363ef2015-09-24 07:07:40 -07001399static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001400 Mock mock(300);
bsalomonc6363ef2015-09-24 07:07:40 -07001401 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001402 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001403
1404 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001405 context->abandonContext();
1406
1407 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1408
1409 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1410
robertphillips8abb3702016-08-31 14:04:06 -07001411 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001412 resource->getUniqueKey();
1413 resource->wasDestroyed();
1414 resource->gpuMemorySize();
1415 resource->getContext();
1416
bsalomonc6363ef2015-09-24 07:07:40 -07001417 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001418 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001419 resource->resourcePriv().makeBudgeted();
1420 resource->resourcePriv().makeUnbudgeted();
1421 resource->resourcePriv().removeScratchKey();
1422 GrUniqueKey key;
1423 make_unique_key<0>(&key, 1);
1424 resource->resourcePriv().setUniqueKey(key);
1425 resource->resourcePriv().removeUniqueKey();
1426}
1427
Brian Salomon1090da62017-01-06 12:04:19 -05001428static void test_tags(skiatest::Reporter* reporter) {
1429#ifdef SK_DEBUG
1430 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1431 static constexpr int kLastTagIdx = 10;
1432 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1433
Robert Phillipscf39f372019-09-03 10:29:20 -04001434 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001435 GrContext* context = mock.context();
1436 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001437 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001438
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001439 // tag strings are expected to be long lived
1440 std::vector<SkString> tagStrings;
1441
Brian Salomon1090da62017-01-06 12:04:19 -05001442 SkString tagStr;
1443 int tagIdx = 0;
1444 int currTagCnt = 0;
1445
1446 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001447
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001448 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001449 GrUniqueKey key;
1450 if (currTagCnt == tagIdx) {
1451 tagIdx += 1;
1452 currTagCnt = 0;
1453 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001454 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001455 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001456 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001457 resource->resourcePriv().setUniqueKey(key);
1458 }
1459 SkASSERT(kLastTagIdx == tagIdx);
1460 SkASSERT(currTagCnt == kLastTagIdx);
1461
1462 // Test i = 0 to exercise unused tag string.
1463 for (int i = 0; i <= kLastTagIdx; ++i) {
1464 tagStr.printf("tag%d", i);
1465 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1466 }
1467#endif
1468}
1469
Robert Phillipsddc21482019-10-16 14:30:09 -04001470static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001471 Mock mock(30000);
Greg Danielc27eb722018-08-10 09:48:08 -04001472 GrContext* context = mock.context();
1473 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001474 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001475
Robert Phillipsddc21482019-10-16 14:30:09 -04001476 GrBackendTexture backends[3];
1477 GrTexture* wrapped[3];
1478 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001479
Robert Phillipsddc21482019-10-16 14:30:09 -04001480 auto releaseProc = [](void* ctx) {
1481 int* index = (int*) ctx;
1482 *index = 1;
1483 };
Greg Danielc27eb722018-08-10 09:48:08 -04001484
Robert Phillipsddc21482019-10-16 14:30:09 -04001485 for (int i = 0; i < 3; ++i) {
1486 backends[i] = context->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
1487 GrMipMapped::kNo, GrRenderable::kNo);
1488 wrapped[i] = gpu->wrapBackendTexture(backends[i], GrColorType::kRGBA_8888,
1489 GrWrapOwnership::kBorrow_GrWrapOwnership,
1490 (i < 2) ? GrWrapCacheable::kYes
1491 : GrWrapCacheable::kNo,
1492 GrIOType::kRead_GrIOType).release();
1493 wrapped[i]->setRelease(releaseProc, &freed[i]);
1494 }
1495
1496 cache->insertDelayedTextureUnref(wrapped[0]);
1497 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001498
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001499 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1500 // is because inserting it as a cross-context resource actually holds a ref until the
1501 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001502 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001503
Robert Phillipsddc21482019-10-16 14:30:09 -04001504 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001505
1506 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001507 wrapped[0]->unref();
1508 wrapped[1]->unref();
1509 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001510
Robert Phillipsddc21482019-10-16 14:30:09 -04001511 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001512
1513 // This should free nothing since no messages were sent.
1514 cache->purgeAsNeeded();
1515
Robert Phillipsddc21482019-10-16 14:30:09 -04001516 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1517
Greg Danielc27eb722018-08-10 09:48:08 -04001518 // Send message to free the first resource
Robert Phillipsddc21482019-10-16 14:30:09 -04001519 GrTextureFreedMessage msg1{wrapped[0], context->priv().contextID()};
1520 SkMessageBus<GrTextureFreedMessage>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001521 cache->purgeAsNeeded();
1522
Robert Phillipsddc21482019-10-16 14:30:09 -04001523 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1524 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001525
Robert Phillipsddc21482019-10-16 14:30:09 -04001526 GrTextureFreedMessage msg2{wrapped[2], context->priv().contextID()};
1527 SkMessageBus<GrTextureFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001528 cache->purgeAsNeeded();
1529
Robert Phillipsddc21482019-10-16 14:30:09 -04001530 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1531 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001532
1533 mock.reset();
1534
Robert Phillipsddc21482019-10-16 14:30:09 -04001535 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001536}
1537
Brian Salomondcfca432017-11-15 15:48:03 -05001538DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001539 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001540 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001541 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001542 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001543 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001544 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001545 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001546 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001547 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001548 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001549 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001550 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001551 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001552 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001553 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001554 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001555 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001556 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001557 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001558}
1559
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001560// This simulates a portion of Chrome's context abandonment processing.
1561// Please see: crbug.com/1011368 and crbug.com/1014993
1562DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
1563 GrContext* context = ctxInfo.grContext();
1564 GrGpu* gpu = context->priv().getGpu();
1565 GrResourceCache* cache = context->priv().getResourceCache();
1566
1567 GrBackendTexture backend = context->createBackendTexture(16, 16,
1568 SkColorType::kRGBA_8888_SkColorType,
1569 GrMipMapped::kNo, GrRenderable::kNo);
1570 GrTexture* tex = gpu->wrapBackendTexture(backend, GrColorType::kRGBA_8888,
1571 GrWrapOwnership::kBorrow_GrWrapOwnership,
1572 GrWrapCacheable::kYes,
1573 GrIOType::kRead_GrIOType).release();
1574
1575 auto releaseProc = [](void* ctx) {
1576 int* index = (int*) ctx;
1577 *index = 1;
1578 };
1579
1580 int freed = 0;
1581
1582 tex->setRelease(releaseProc, &freed);
1583
1584 cache->insertDelayedTextureUnref(tex);
1585
1586 // Now only the cache is holding a ref to this texture
1587 tex->unref();
1588
1589 REPORTER_ASSERT(reporter, 0 == freed);
1590
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001591 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1592 // for all the backends for consistency.
1593 context->deleteBackendTexture(backend);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001594 context->abandonContext();
1595
1596 REPORTER_ASSERT(reporter, 1 == freed);
1597
1598 // In the past, creating this message could cause an exception due to
1599 // an un-safe downcast from GrTexture to GrGpuResource
1600 GrTextureFreedMessage msg{tex, context->priv().contextID()};
1601 SkMessageBus<GrTextureFreedMessage>::Post(msg);
1602
Greg Danielf0e04f02019-12-04 15:17:54 -05001603 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001604 context->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001605}
1606
Robert Phillipsd6214d42016-11-07 08:23:48 -05001607////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001608static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001609 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001610 SkISize dims,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001611 int sampleCnt) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001612 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomona56a7462020-02-07 14:17:25 -05001613 return provider->createTexture(dims, format, renderable, sampleCnt, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -04001614 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001615}
1616
Brian Salomona56a7462020-02-07 14:17:25 -05001617static sk_sp<GrTextureProxy> make_mipmap_proxy(GrContext* context,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001618 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001619 SkISize dims,
Robert Phillipse78b7252017-04-06 07:59:41 -04001620 int sampleCnt) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001621 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1622 const GrCaps* caps = context->priv().caps();
1623
Robert Phillipsd6214d42016-11-07 08:23:48 -05001624
Robert Phillips0a15cc62019-07-30 12:49:10 -04001625 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1626 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -05001627 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001628
Greg Daniel3a365112020-02-14 10:47:18 -05001629 return proxyProvider->createProxy(format, dims, swizzle, renderable, sampleCnt,
Brian Salomonbeb7f522019-08-30 16:19:42 -04001630 GrMipMapped::kYes, SkBackingFit::kExact, SkBudgeted::kYes,
1631 GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001632}
1633
1634// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1635// Texture-only, both-RT-and-Texture and MIPmapped
1636DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1637 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001638 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001639 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001640
Brian Salomona56a7462020-02-07 14:17:25 -05001641 static constexpr SkISize kSize = {64, 64};
1642 static constexpr auto kArea = kSize.area();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001643
Robert Phillipsd6214d42016-11-07 08:23:48 -05001644 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001645 {
1646 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001647
Brian Salomona56a7462020-02-07 14:17:25 -05001648 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001649 size_t size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001650 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001651
Greg Daniel6fa62e22019-08-07 15:52:37 -04001652 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001653 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001654 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001655 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001656 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001657 kArea*4 == size || // msaa4 failed
1658 kArea*4*sampleCount == size || // auto-resolving
1659 kArea*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001660 }
1661
Brian Salomona56a7462020-02-07 14:17:25 -05001662 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001663 size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001664 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001665 }
1666
Robert Phillipsd6214d42016-11-07 08:23:48 -05001667 // Mipmapped versions
Greg Daniel4065d452018-11-16 15:43:41 -05001668 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001669 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001670
Brian Salomona56a7462020-02-07 14:17:25 -05001671 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001672 size_t size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001673 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001674
Greg Daniel6fa62e22019-08-07 15:52:37 -04001675 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001676 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001677 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, sampleCount);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001678 size = proxy->gpuMemorySize(*caps);
Robert Phillipse78b7252017-04-06 07:59:41 -04001679 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001680 kArea*4 + (kArea*4)/3 == size || // msaa4 failed
1681 kArea*4*sampleCount + (kArea*4)/3 == size || // auto-resolving
1682 kArea*4*(sampleCount+1) + (kArea*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001683 }
Robert Phillips1b352562017-04-05 18:56:21 +00001684
Brian Salomona56a7462020-02-07 14:17:25 -05001685 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, 1);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04001686 size = proxy->gpuMemorySize(*caps);
Brian Salomona56a7462020-02-07 14:17:25 -05001687 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001688 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001689}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001690
1691#if GR_GPU_STATS
1692DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
1693 GrContext* context = ctxInfo.grContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001694 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001695
1696 // Helper that determines if cache is overbudget.
1697 auto overbudget = [context] {
1698 int uNum;
1699 size_t uSize;
1700 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001701 size_t bSize = context->getResourceCacheLimit();
1702 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001703 };
1704
1705 // Helper that does a trivial draw to a surface.
1706 auto drawToSurf = [](SkSurface* surf) {
1707 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1708 };
1709
1710 // Helper that checks whether a flush has occurred between calls.
1711 int baseFlushCount = 0;
1712 auto getFlushCountDelta = [context, &baseFlushCount]() {
1713 int cur = context->priv().getGpu()->stats()->numFinishFlushes();
1714 int delta = cur - baseFlushCount;
1715 baseFlushCount = cur;
1716 return delta;
1717 };
1718
1719 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1720 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1721 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1722
1723 drawToSurf(surf1.get());
1724 drawToSurf(surf2.get());
1725
1726 // Flush each surface once to ensure that their backing stores are allocated.
1727 surf1->flush();
1728 surf2->flush();
1729 REPORTER_ASSERT(reporter, overbudget());
1730 getFlushCountDelta();
1731
1732 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1733 drawToSurf(surf1.get());
1734 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1735 drawToSurf(surf2.get());
1736 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1737 REPORTER_ASSERT(reporter, overbudget());
1738
1739 // Make surf1 purgeable. Drawing to surf2 should flush.
1740 surf1->flush();
1741 surf1.reset();
1742 drawToSurf(surf2.get());
1743 REPORTER_ASSERT(reporter, getFlushCountDelta());
1744 REPORTER_ASSERT(reporter, overbudget());
1745}
1746#endif