blob: 8889ce15f4e95066e21b628abe2d3cf2c6f5f155 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
bsalomon3f324322015-04-08 11:01:54 -07009
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrGpu.h"
13#include "src/gpu/GrGpuResourceCacheAccess.h"
14#include "src/gpu/GrGpuResourcePriv.h"
15#include "src/gpu/GrProxyProvider.h"
Robert Phillipse94b4e12020-07-23 13:54:35 -040016#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040017#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrResourceCache.h"
19#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/gpu/GrContextFactory.h"
Robert Phillips646e4292017-06-13 12:44:56 -040022
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkCanvas.h"
24#include "include/core/SkSurface.h"
Ben Wagner21bca282019-05-15 10:15:52 -040025#include "src/core/SkMessageBus.h"
Mike Reed13711eb2020-07-14 17:16:32 -040026#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/SkGr.h"
28#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040029#include "tests/TestUtils.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000030
Hal Canary8a001442018-09-19 11:31:27 -040031#include <thread>
32
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000033static const int gWidth = 640;
34static const int gHeight = 480;
35
36////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040038 auto context = ctxInfo.directContext();
kkinnunen15302832015-12-01 04:35:26 -080039 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070040 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080041 SkCanvas* canvas = surface->getCanvas();
42
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000043 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
44
45 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000046 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040048 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000049
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000050 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070051 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000052
Robert Phillipscf39f372019-09-03 10:29:20 -040053 size_t oldMaxBytes = context->getResourceCacheLimit();
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000054
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000055 // Set the cache limits so we can fit 10 "src" images and the
56 // max number of textures doesn't matter
57 size_t maxCacheSize = initialCacheSize + 10*srcSize;
Robert Phillipscf39f372019-09-03 10:29:20 -040058 context->setResourceCacheLimit(maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000059
60 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000061 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062
63 for (int i = 0; i < 100; ++i) {
64 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040065 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 // "modify" the src texture
68 src.notifyPixelsChanged();
69
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000070 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070071 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000072
73 // we should never go over the size limit
74 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
75 }
76
Robert Phillipscf39f372019-09-03 10:29:20 -040077 context->setResourceCacheLimit(oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000078}
79
bsalomon11abd8d2016-10-14 08:13:48 -070080static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
81 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
82 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
83 return false;
84 }
85 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
86}
87
Brian Salomonf7f54332020-07-28 09:23:35 -040088static GrStencilAttachment* get_SB(GrRenderTarget* rt) { return rt->getStencilAttachment(); }
Robert Phillipsc0192e32017-09-21 12:00:26 -040089
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,
Brian Salomon7e67dca2020-07-21 09:27:25 -040095 sampleCount, GrMipmapped::kNo, budgeted,
Brian Salomona56a7462020-02-07 14:17:25 -050096 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) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400112 auto context = ctxInfo.directContext();
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) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400186 auto context = ctxInfo.directContext();
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
Greg Danielc1ad77c2020-05-06 11:40:03 -0400198 CreateBackendTexture(context, &backendTextures[0], kW, kH, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400199 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400200 GrProtected::kNo);
201 CreateBackendTexture(context, &backendTextures[1], kW, kH, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400202 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400203 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500204 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
205 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
206 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
207 return;
208 }
jvanverth672bb7f2015-07-13 07:19:57 -0700209
bsalomon6dc6f5f2015-06-18 09:12:16 -0700210 context->resetContext();
211
Robert Phillips6be756b2018-01-16 15:07:54 -0500212 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400213 backendTextures[0], kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700214
Robert Phillips6be756b2018-01-16 15:07:54 -0500215 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400216 backendTextures[1], kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217
Brian Osman85d34b22017-05-10 12:06:26 -0400218 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
219 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220 return;
221 }
222
halcanary96fcdcc2015-08-27 07:41:13 -0700223 borrowed.reset(nullptr);
224 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225
Greg Daniel0a2464f2020-05-14 15:45:44 -0400226 context->flushAndSubmit();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
229 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
231 REPORTER_ASSERT(reporter, borrowedIsAlive);
232 REPORTER_ASSERT(reporter, !adoptedIsAlive);
233
Brian Salomone64b0642018-03-07 11:47:54 -0500234 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400235 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500236 }
237 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400238 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500239 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
241 context->resetContext();
242}
243
bsalomon6d3fe022014-07-25 08:35:45 -0700244class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800245 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000246public:
robertphillips6e83ac72015-08-13 05:19:14 -0700247 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700248
bsalomon1c60dfe2015-01-21 09:32:40 -0800249 /** Property that distinctly categorizes the resource.
250 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800251 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800252
kkinnunen2e6055b2016-04-22 01:48:29 -0700253 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
254 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700255 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700257 , fProperty(kA_SimulatedProperty)
258 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800259 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700260 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800261 }
262
kkinnunen2e6055b2016-04-22 01:48:29 -0700263 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400264 SimulatedProperty property, size_t size = kDefaultSize) {
265 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800266 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500267 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
268 size_t size = kDefaultSize) {
269 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000270 }
271
Brian Salomond3b65972017-03-22 12:05:03 -0400272 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800273 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000274 }
275
bsalomon33435572014-11-05 14:47:41 -0800276 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000277
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400278 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
279 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000280 }
281
bsalomon1c60dfe2015-01-21 09:32:40 -0800282 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
283 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
284 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800285 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
286 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800287 }
288 }
289
290 static size_t ExpectedScratchKeySize() {
291 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
292 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293private:
bsalomon24db3b12015-01-23 04:24:04 -0800294 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800295
Greg Danielda86e282018-06-13 09:41:19 -0400296 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
297 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700298 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700299 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400300 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700301 , fProperty(property)
302 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800303 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700304 this->registerWithCache(budgeted);
305 }
306
307 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500308 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
309 : INHERITED(gpu)
310 , fToDelete(nullptr)
311 , fSize(size)
312 , fProperty(kA_SimulatedProperty)
313 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700314 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500315 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 }
317
318 void computeScratchKey(GrScratchKey* key) const override {
319 if (fIsScratch) {
320 ComputeScratchKey(fProperty, key);
321 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800322 }
323
mtklein36352bf2015-03-25 18:17:31 -0700324 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400325 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800326
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400327 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000328 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800329 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800330 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700331 bool fIsScratch;
John Stiles7571f9e2020-09-02 22:42:33 -0400332 using INHERITED = GrGpuResource;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000333};
bsalomon33435572014-11-05 14:47:41 -0800334int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000335
bsalomonc2f35b72015-01-23 07:19:22 -0800336class Mock {
337public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400338 Mock(size_t maxBytes) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400339 fDContext = GrDirectContext::MakeMock(nullptr);
340 SkASSERT(fDContext);
341 fDContext->setResourceCacheLimit(maxBytes);
342 GrResourceCache* cache = fDContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800343 cache->purgeAllUnlocked();
344 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800345 }
bsalomonc2f35b72015-01-23 07:19:22 -0800346
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400347 GrResourceCache* cache() { return fDContext->priv().getResourceCache(); }
348 GrGpu* gpu() { return fDContext->priv().getGpu(); }
349 GrDirectContext* dContext() { return fDContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800350
Greg Danielc27eb722018-08-10 09:48:08 -0400351 void reset() {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400352 fDContext.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400353 }
354
bsalomonc2f35b72015-01-23 07:19:22 -0800355private:
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400356 sk_sp<GrDirectContext> fDContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800357};
358
359static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400360 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800361 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400362 GrGpu* gpu = mock.gpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800363
364 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400365 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
366 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
367 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
368 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800369
370 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800371 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800372 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800373 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800374
375 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800376 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800377 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
378
bsalomon8718aaf2015-02-19 07:24:21 -0800379 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800380
381 a->unref();
382 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800383 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800384 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800385 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386
387 c->unref();
388 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800389 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800391 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800392
393 d->unref();
394 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800395 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
396 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800397
398 b->unref();
399 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800400 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
401 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800402}
403
bsalomon24db3b12015-01-23 04:24:04 -0800404// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500405template <int>
406static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800407 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500408 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800409 builder[0] = data;
410}
411
Robert Phillips6eba0632018-03-28 12:25:42 -0400412static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400413 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400414 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400415 GrGpu* gpu = mock.gpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400416
417 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
418 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400419 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400420
421 GrUniqueKey uniqueKey;
422 make_unique_key<0>(&uniqueKey, 0);
423
424 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400425 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400426 b->resourcePriv().setUniqueKey(uniqueKey);
427
428 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400429 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400430
431 GrUniqueKey uniqueKey2;
432 make_unique_key<0>(&uniqueKey2, 1);
433
434 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400435 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400436 d->resourcePriv().setUniqueKey(uniqueKey2);
437
438
439 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
440 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
441 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
442 d->gpuMemorySize() == cache->getResourceBytes());
443
444 // Should be safe to purge without deleting the resources since we still have refs.
445 cache->purgeUnlockedResources(false);
446 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
447
448 // Unref them all. Since they all have keys they should remain in the cache.
449
450 a->unref();
451 b->unref();
452 c->unref();
453 d->unref();
454 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
455 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
456 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
457 d->gpuMemorySize() == cache->getResourceBytes());
458
459 // Purge only the two scratch resources
460 cache->purgeUnlockedResources(true);
461
462 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
463 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
464 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
465 cache->getResourceBytes());
466
467 // Purge the uniquely keyed resources
468 cache->purgeUnlockedResources(false);
469
470 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
471 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
472 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
473}
474
bsalomon84c8e622014-11-17 09:33:27 -0800475static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400476 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800477 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400478 GrGpu* gpu = mock.gpu();
bsalomondace19e2014-11-17 07:34:06 -0800479
bsalomon8718aaf2015-02-19 07:24:21 -0800480 GrUniqueKey uniqueKey;
481 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800482
bsalomon8718aaf2015-02-19 07:24:21 -0800483 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800484 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400485 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
486 10);
487 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800488 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500489 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
490 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
491 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800492
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500493 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800494 GrUniqueKey uniqueKey2;
495 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500496 GrUniqueKey uniqueKey3;
497 make_unique_key<0>(&uniqueKey3, 2);
498 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
499 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
500 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
501 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
502 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
503 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400504
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500505 // Remove the extra refs we just added.
506 SkSafeUnref(wrappedCacheableViaKey);
507 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800508
509 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500510 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800511 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500512 wrappedCacheable->gpuMemorySize() +
513 wrappedUncacheable->gpuMemorySize() +
514 unbudgeted->gpuMemorySize() ==
515 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800516 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800517 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800518 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400519 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800520
bsalomon63c992f2015-01-23 12:47:59 -0800521 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800522 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500523 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800524 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500525 wrappedCacheable->gpuMemorySize() +
526 wrappedUncacheable->gpuMemorySize() +
527 unbudgeted->gpuMemorySize() ==
528 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800529 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800530 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800531 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400532 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800533
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500534 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
535 // However, unreffing the uncacheable wrapped resource should free it.
536 wrappedCacheable->unref();
537 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400538 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800539 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500540 wrappedCacheable->gpuMemorySize() +
541 unbudgeted->gpuMemorySize() ==
542 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500543 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800544
bsalomon84c8e622014-11-17 09:33:27 -0800545 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500546 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800547 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500548 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
549 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
550 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800551 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500552 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500553
554 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
555 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
556 if (wrappedCacheableViaKey) {
557 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
558 wrappedCacheable->unref();
559 }
560 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
561 // resource should immediately delete it.
562 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
563
564 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500565 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
566 wrappedUncacheable->gpuMemorySize() +
567 unbudgeted->gpuMemorySize() ==
568 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
570 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400571 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800572
573 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400574 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800575 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500576 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
578 wrappedUncacheable->gpuMemorySize() ==
579 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800580 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
581 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400582 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800583
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500584 // Unreffing the wrapped resources (with no unique key) should free them right away.
585 wrappedUncacheable->unref();
586 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800587 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
588 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
589 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
590 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400591 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800592
593 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800594 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
595 REPORTER_ASSERT(reporter, 0 == 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());
bsalomondace19e2014-11-17 07:34:06 -0800599}
600
bsalomon5236cf42015-01-14 10:42:08 -0800601static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400602 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800603 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400604 GrGpu* gpu = mock.gpu();
bsalomon5236cf42015-01-14 10:42:08 -0800605
bsalomon8718aaf2015-02-19 07:24:21 -0800606 GrUniqueKey uniqueKey;
607 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800608
609 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800610 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800611 TestResource* wrapped;
612 TestResource* unbudgeted;
613
614 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500615 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400616 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700617
bsalomon5236cf42015-01-14 10:42:08 -0800618 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800619 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
620 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
621 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
622 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400623 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800624
Greg Danielda86e282018-06-13 09:41:19 -0400625 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800626 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800627 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800628 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
629 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
630 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
631 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400632 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800633
bsalomon0ea80f42015-02-11 10:49:59 -0800634 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500635 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
637 REPORTER_ASSERT(reporter, 21 + large == 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
642 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800643 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
644 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
645 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
646 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400647 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800648
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500649 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800650 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
651 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
652 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
653 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400654 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800655
656 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800657 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
658 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
659 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
660 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400661 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800662
bsalomon0ea80f42015-02-11 10:49:59 -0800663 cache->purgeAllUnlocked();
664 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
665 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
666 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
667 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400668 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800669}
670
bsalomon3582d3e2015-02-13 14:20:05 -0800671// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
672void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
673/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400674 Mock mock(300);
bsalomon0ea80f42015-02-11 10:49:59 -0800675 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400676 GrGpu* gpu = mock.gpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800677
678 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500679 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800680 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800681 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800682
683 size_t size = resource->gpuMemorySize();
684 for (int i = 0; i < 2; ++i) {
685 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800686 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800687 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500688 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500689 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400690 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800691 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
692 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
693 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
694 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400695 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800696
697 // Once it is unrefed, it should become available as scratch.
698 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800699 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
700 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
701 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
702 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400703 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400704 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800705 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800706 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800707 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500708 REPORTER_ASSERT(reporter,
709 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800710
711 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700712 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800713 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800714 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800715 } else {
716 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800717 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800718 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
719 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
720 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
721 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400722 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800723 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800724 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500725 REPORTER_ASSERT(reporter,
726 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800727
728 // now when it is unrefed it should die since it has no key.
729 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800730 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
731 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
732 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
733 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400734 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800735 }
bsalomon8b79d232014-11-10 10:19:06 -0800736 }
bsalomonc2f35b72015-01-23 07:19:22 -0800737}
738
739static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400740 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800741 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400742 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -0800743
bsalomon8b79d232014-11-10 10:19:06 -0800744 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500745 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700746 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400747 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500748 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700749 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400750 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800751 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800752 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800753 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400754 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800755
756 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800757 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758
bsalomon0ea80f42015-02-11 10:49:59 -0800759 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800760 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
762 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800763 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800764 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800765
bsalomon63c992f2015-01-23 12:47:59 -0800766 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800767 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800768 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800769 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800770
771 // Unref but don't purge
772 a->unref();
773 b->unref();
774 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800775 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800776
bsalomon63c992f2015-01-23 12:47:59 -0800777 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800778 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800779 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800780 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
781 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800782}
783
bsalomon10e23ca2014-11-25 05:52:06 -0800784static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400785 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800786 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400787 GrGpu* gpu = mock.gpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800788
bsalomon10e23ca2014-11-25 05:52:06 -0800789 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500790 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800791 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500792 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800793 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800794 a->unref();
795 b->unref();
796
bsalomon1c60dfe2015-01-21 09:32:40 -0800797 GrScratchKey scratchKey;
798 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800799 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800800 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400801 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800802
bsalomon0ea80f42015-02-11 10:49:59 -0800803 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800804 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800805 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800806 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
807 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800808
809 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400810 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800811 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800812 // It's still alive, but not cached by scratch key anymore
813 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800814 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
815 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800816
817 // The cache should immediately delete it when it's unrefed since it isn't accessible.
818 find->unref();
819 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800820 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
821 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800822
823 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400824 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800825 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800826 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800827 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
828 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800829
830 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800831 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800832 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800833 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
834 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800835
836 find->unref();
837 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800838 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
839 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800840}
841
bsalomon1c60dfe2015-01-21 09:32:40 -0800842static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400843 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800844 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400845 GrGpu* gpu = mock.gpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800846
847 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500848 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800849 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500850 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800851 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800852 a->unref();
853 b->unref();
854
855 GrScratchKey scratchKey;
856 // Ensure that scratch key comparison and assignment is consistent.
857 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800858 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800859 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800860 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800861 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
862 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
863 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
864 scratchKey = scratchKey1;
865 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
866 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
867 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
868 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
869 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
870 scratchKey = scratchKey2;
871 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
872 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
873 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
874 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
875 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
876
877 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800878 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800879 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400880 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800881
882 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800883 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400884 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700885 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800886 find->unref();
887
888 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400889 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700890 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800891 REPORTER_ASSERT(reporter, find == a || find == b);
892
Robert Phillipsaee18c92019-09-06 11:48:27 -0400893 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700894 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800895 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
896 REPORTER_ASSERT(reporter, find2 != find);
897 find2->unref();
898 find->unref();
899}
900
bsalomon8718aaf2015-02-19 07:24:21 -0800901static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400902 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -0800903 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400904 GrGpu* gpu = mock.gpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000905
bsalomon8718aaf2015-02-19 07:24:21 -0800906 GrUniqueKey key;
907 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700908
bsalomon8718aaf2015-02-19 07:24:21 -0800909 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400910 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700911
bsalomonf99e9612015-02-19 08:24:16 -0800912 // Set key on resource a.
913 a->resourcePriv().setUniqueKey(key);
914 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
915 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800916
bsalomonf99e9612015-02-19 08:24:16 -0800917 // Make sure that redundantly setting a's key works.
918 a->resourcePriv().setUniqueKey(key);
919 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800920 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800921 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
922 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800923 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
924
bsalomonf99e9612015-02-19 08:24:16 -0800925 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400926 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800927 b->resourcePriv().setUniqueKey(key);
928 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
929 b->unref();
930
931 // Still have two resources because a is still reffed.
932 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
933 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
934 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
935
936 a->unref();
937 // Now a should be gone.
938 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
939 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
940 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
941
942 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
943 // Also make b be unreffed when replacement occurs.
944 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400945 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800946 GrUniqueKey differentKey;
947 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800948 c->resourcePriv().setUniqueKey(differentKey);
949 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
950 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
951 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
952 // c replaces b and b should be immediately purged.
953 c->resourcePriv().setUniqueKey(key);
954 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
955 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
956 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
957
958 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800959 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800960 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
961 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
962 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
963
964 // Drop the ref on c, it should be kept alive because it has a unique key.
965 c->unref();
966 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
967 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
968 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
969
970 // Verify that we can find c, then remove its unique key. It should get purged immediately.
971 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
972 c->resourcePriv().removeUniqueKey();
973 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800974 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
975 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800976 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700977
978 {
979 GrUniqueKey key2;
980 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500981 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700982 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700983 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700984 d->resourcePriv().setUniqueKey(key2);
985 }
986
987 GrUniqueKey key3;
988 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400989 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700990 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000991}
992
bsalomon8b79d232014-11-10 10:19:06 -0800993static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400994 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400995 auto dContext = mock.dContext();
bsalomon0ea80f42015-02-11 10:49:59 -0800996 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400997 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -0800998
bsalomon8718aaf2015-02-19 07:24:21 -0800999 GrUniqueKey key1, key2, key3;
1000 make_unique_key<0>(&key1, 1);
1001 make_unique_key<0>(&key2, 2);
1002 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001003
bsalomon23e619c2015-02-06 11:54:28 -08001004 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001005 TestResource* a = new TestResource(gpu);
1006 TestResource* b = new TestResource(gpu);
1007 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001008 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001009 a->resourcePriv().setUniqueKey(key1);
1010 b->resourcePriv().setUniqueKey(key2);
1011 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001012 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001013 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001014 c->unref();
1015
bsalomon8718aaf2015-02-19 07:24:21 -08001016 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1017 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1018 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001019 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001020
bsalomon8718aaf2015-02-19 07:24:21 -08001021 typedef GrUniqueKeyInvalidatedMessage Msg;
1022 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001023
1024 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001025 Bus::Post(Msg(key1, dContext->priv().contextID()));
1026 Bus::Post(Msg(key2, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001027 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001028 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001029 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1030 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001031 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001032 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001033
1034 // Invalidate the third.
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001035 Bus::Post(Msg(key3, dContext->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001036 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001037 // we still have a ref on b, c should be recycled as scratch.
1038 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001039 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001040
bsalomon23e619c2015-02-06 11:54:28 -08001041 // make b purgeable. It should be immediately deleted since it has no key.
1042 b->unref();
1043 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1044
1045 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1046 GrScratchKey scratchKey;
1047 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001048 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001049 REPORTER_ASSERT(reporter, scratch == c);
1050 SkSafeUnref(scratch);
1051
1052 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001053 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001054 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001055 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001056 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1057 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001058 REPORTER_ASSERT(reporter, !scratch);
1059 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001060}
1061
bsalomon71cb0c22014-11-14 12:10:14 -08001062static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001063 Mock mock(30000);
bsalomon0ea80f42015-02-11 10:49:59 -08001064 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001065 GrGpu* gpu = mock.gpu();
bsalomon8b79d232014-11-10 10:19:06 -08001066
bsalomon8718aaf2015-02-19 07:24:21 -08001067 GrUniqueKey key1, key2;
1068 make_unique_key<0>(&key1, 1);
1069 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001070
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001071 sk_sp<TestResource> a(new TestResource(gpu));
1072 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001073 a->resourcePriv().setUniqueKey(key1);
1074 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001075
bsalomonc2f35b72015-01-23 07:19:22 -08001076 // Make a cycle
1077 a->setUnrefWhenDestroyed(b);
1078 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001079
bsalomonc2f35b72015-01-23 07:19:22 -08001080 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001081
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001082 TestResource* unownedA = a.release();
1083 unownedA->unref();
1084 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001085
bsalomonc2f35b72015-01-23 07:19:22 -08001086 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001087
bsalomon0ea80f42015-02-11 10:49:59 -08001088 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001089 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001090
bsalomonc2f35b72015-01-23 07:19:22 -08001091 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001092 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001093 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001094
bsalomon0ea80f42015-02-11 10:49:59 -08001095 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001096 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001097}
1098
bsalomonddf30e62015-02-19 11:38:44 -08001099static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1100 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001101 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001102 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001103
1104 SkRandom random;
1105
1106 // Run the test 2*kCount times;
1107 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001108 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001109 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001110 GrGpu* gpu = mock.gpu();
bsalomonddf30e62015-02-19 11:38:44 -08001111
1112 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001113 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001114
Robert Phillipscf39f372019-09-03 10:29:20 -04001115 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001116
1117 SkTDArray<int> shouldPurgeIdxs;
1118 int purgeableCnt = 0;
1119 SkTDArray<GrGpuResource*> resourcesToUnref;
1120
1121 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1122 // unpurgeable resources.
1123 for (int j = 0; j < kCount; ++j) {
1124 GrUniqueKey key;
1125 make_unique_key<0>(&key, j);
1126
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001127 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001128 r->resourcePriv().setUniqueKey(key);
1129 if (random.nextU() % kLockedFreq) {
1130 // Make this is purgeable.
1131 r->unref();
1132 ++purgeableCnt;
1133 if (purgeableCnt <= kNumToPurge) {
1134 *shouldPurgeIdxs.append() = j;
1135 }
1136 } else {
1137 *resourcesToUnref.append() = r;
1138 }
1139 }
1140
1141 // Verify that the correct resources were purged.
1142 int currShouldPurgeIdx = 0;
1143 for (int j = 0; j < kCount; ++j) {
1144 GrUniqueKey key;
1145 make_unique_key<0>(&key, j);
1146 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1147 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1148 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1149 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001150 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001151 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001152 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001153 }
1154 SkSafeUnref(res);
1155 }
1156
1157 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1158 resourcesToUnref[j]->unref();
1159 }
1160 }
1161}
1162
Brian Salomon5e150852017-03-22 14:53:13 -04001163static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001164 Mock mock(1000000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001165 auto dContext = mock.dContext();
Brian Salomon5e150852017-03-22 14:53:13 -04001166 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001167 GrGpu* gpu = mock.gpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001168
1169 static constexpr int kCnts[] = {1, 10, 1024};
1170 auto nowish = []() {
1171 // We sleep so that we ensure we get a value that is greater than the last call to
1172 // GrStdSteadyClock::now().
1173 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1174 auto result = GrStdSteadyClock::now();
1175 // Also sleep afterwards so we don't get this value again.
1176 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1177 return result;
1178 };
1179
1180 for (int cnt : kCnts) {
1181 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1182 new GrStdSteadyClock::time_point[cnt]);
1183 {
1184 // Insert resources and get time points between each addition.
1185 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001186 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001187 GrUniqueKey k;
1188 make_unique_key<1>(&k, i);
1189 r->resourcePriv().setUniqueKey(k);
1190 r->unref();
1191 timeStamps.get()[i] = nowish();
1192 }
1193
1194 // Purge based on the time points between resource additions. Each purge should remove
1195 // the oldest resource.
1196 for (int i = 0; i < cnt; ++i) {
1197 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1198 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1199 for (int j = 0; j < i; ++j) {
1200 GrUniqueKey k;
1201 make_unique_key<1>(&k, j);
1202 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1203 REPORTER_ASSERT(reporter, !SkToBool(r));
1204 SkSafeUnref(r);
1205 }
1206 }
1207
1208 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1209 cache->purgeAllUnlocked();
1210 }
1211
1212 // Do a similar test but where we leave refs on some resources to prevent them from being
1213 // purged.
1214 {
1215 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1216 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001217 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001218 GrUniqueKey k;
1219 make_unique_key<1>(&k, i);
1220 r->resourcePriv().setUniqueKey(k);
1221 // Leave a ref on every other resource, beginning with the first.
1222 if (SkToBool(i & 0x1)) {
1223 refedResources.get()[i / 2] = r;
1224 } else {
1225 r->unref();
1226 }
1227 timeStamps.get()[i] = nowish();
1228 }
1229
1230 for (int i = 0; i < cnt; ++i) {
1231 // Should get a resource purged every other frame.
1232 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1233 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1234 }
1235
1236 // Unref all the resources that we kept refs on in the first loop.
1237 for (int i = 0; i < (cnt / 2); ++i) {
1238 refedResources.get()[i]->unref();
1239 cache->purgeResourcesNotUsedSince(nowish());
1240 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1241 }
1242
1243 cache->purgeAllUnlocked();
1244 }
1245
1246 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1247
Robert Phillipse94b4e12020-07-23 13:54:35 -04001248 // Verify that calling flush() on a context with nothing to do will not trigger resource
Brian Salomon5e150852017-03-22 14:53:13 -04001249 // eviction
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001250 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001251 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001252 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001253 GrUniqueKey k;
1254 make_unique_key<1>(&k, i);
1255 r->resourcePriv().setUniqueKey(k);
1256 r->unref();
1257 }
1258 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001259 dContext->flushAndSubmit();
Brian Salomon5e150852017-03-22 14:53:13 -04001260 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1261 cache->purgeResourcesNotUsedSince(nowish());
1262 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1263 }
1264}
1265
Derek Sollenberger5480a182017-05-25 16:43:59 -04001266static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001267 Mock mock(100);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001268 auto dContext = mock.dContext();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001269 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001270 GrGpu* gpu = mock.gpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001271
1272 enum TestsCase {
1273 kOnlyScratch_TestCase = 0,
1274 kPartialScratch_TestCase = 1,
1275 kAllScratch_TestCase = 2,
1276 kPartial_TestCase = 3,
1277 kAll_TestCase = 4,
1278 kNone_TestCase = 5,
1279 kEndTests_TestCase = kNone_TestCase + 1
1280 };
1281
1282 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1283
1284 GrUniqueKey key1, key2, key3;
1285 make_unique_key<0>(&key1, 1);
1286 make_unique_key<0>(&key2, 2);
1287 make_unique_key<0>(&key3, 3);
1288
1289 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001290 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1291 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1292 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001293
1294 unique1->resourcePriv().setUniqueKey(key1);
1295 unique2->resourcePriv().setUniqueKey(key2);
1296 unique3->resourcePriv().setUniqueKey(key3);
1297
Derek Sollenberger5480a182017-05-25 16:43:59 -04001298 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001299 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001300 TestResource::kA_SimulatedProperty,
1301 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001302 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001303 TestResource::kB_SimulatedProperty,
1304 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001305
1306 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1307 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1308 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1309
1310 // Add resources to the purgeable queue
1311 unique1->unref();
1312 scratch1->unref();
1313 unique2->unref();
1314 scratch2->unref();
1315 unique3->unref();
1316
1317 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1318 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1319 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1320
1321 switch(testCase) {
1322 case kOnlyScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001323 dContext->purgeUnlockedResources(14, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001324 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1325 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1326 break;
1327 }
1328 case kPartialScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001329 dContext->purgeUnlockedResources(3, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001330 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1331 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1332 break;
1333 }
1334 case kAllScratch_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001335 dContext->purgeUnlockedResources(50, true);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001336 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1337 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1338 break;
1339 }
1340 case kPartial_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001341 dContext->purgeUnlockedResources(13, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001342 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1343 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1344 break;
1345 }
1346 case kAll_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001347 dContext->purgeUnlockedResources(50, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001348 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1349 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1350 break;
1351 }
1352 case kNone_TestCase: {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001353 dContext->purgeUnlockedResources(0, true);
1354 dContext->purgeUnlockedResources(0, false);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001355 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1356 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1357 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1358 break;
1359 }
Brian Salomon23356442018-11-30 15:33:19 -05001360 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001361
1362 // ensure all are purged before the next
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001363 dContext->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001364 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1365 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1366
1367 }
1368}
1369
senorblanco84cd6212015-08-04 10:01:58 -07001370static void test_custom_data(skiatest::Reporter* reporter) {
1371 GrUniqueKey key1, key2;
1372 make_unique_key<0>(&key1, 1);
1373 make_unique_key<0>(&key2, 2);
1374 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001375 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001376 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1377 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1378
1379 // Test that copying a key also takes a ref on its custom data.
1380 GrUniqueKey key3 = key1;
1381 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1382}
1383
bsalomonc6363ef2015-09-24 07:07:40 -07001384static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001385 Mock mock(300);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001386 auto dContext = mock.dContext();
1387 GrGpu* gpu = mock.gpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001388
1389 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001390 dContext->abandonContext();
bsalomonc6363ef2015-09-24 07:07:40 -07001391
1392 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1393
1394 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1395
robertphillips8abb3702016-08-31 14:04:06 -07001396 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001397 resource->getUniqueKey();
1398 resource->wasDestroyed();
1399 resource->gpuMemorySize();
1400 resource->getContext();
1401
bsalomonc6363ef2015-09-24 07:07:40 -07001402 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001403 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001404 resource->resourcePriv().makeBudgeted();
1405 resource->resourcePriv().makeUnbudgeted();
1406 resource->resourcePriv().removeScratchKey();
1407 GrUniqueKey key;
1408 make_unique_key<0>(&key, 1);
1409 resource->resourcePriv().setUniqueKey(key);
1410 resource->resourcePriv().removeUniqueKey();
1411}
1412
Brian Salomon1090da62017-01-06 12:04:19 -05001413static void test_tags(skiatest::Reporter* reporter) {
1414#ifdef SK_DEBUG
1415 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1416 static constexpr int kLastTagIdx = 10;
1417 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1418
Robert Phillipscf39f372019-09-03 10:29:20 -04001419 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001420 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001421 GrGpu* gpu = mock.gpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001422
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001423 // tag strings are expected to be long lived
1424 std::vector<SkString> tagStrings;
1425
Brian Salomon1090da62017-01-06 12:04:19 -05001426 SkString tagStr;
1427 int tagIdx = 0;
1428 int currTagCnt = 0;
1429
1430 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001431
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001432 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001433 GrUniqueKey key;
1434 if (currTagCnt == tagIdx) {
1435 tagIdx += 1;
1436 currTagCnt = 0;
1437 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001438 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001439 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001440 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001441 resource->resourcePriv().setUniqueKey(key);
1442 }
1443 SkASSERT(kLastTagIdx == tagIdx);
1444 SkASSERT(currTagCnt == kLastTagIdx);
1445
1446 // Test i = 0 to exercise unused tag string.
1447 for (int i = 0; i <= kLastTagIdx; ++i) {
1448 tagStr.printf("tag%d", i);
1449 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1450 }
1451#endif
1452}
1453
Robert Phillipsddc21482019-10-16 14:30:09 -04001454static void test_free_texture_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001455 Mock mock(30000);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001456 auto dContext = mock.dContext();
Greg Danielc27eb722018-08-10 09:48:08 -04001457 GrResourceCache* cache = mock.cache();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001458 GrGpu* gpu = mock.gpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001459
Robert Phillipsddc21482019-10-16 14:30:09 -04001460 GrBackendTexture backends[3];
1461 GrTexture* wrapped[3];
1462 int freed[3] = { 0, 0, 0 };
Greg Danielc27eb722018-08-10 09:48:08 -04001463
Robert Phillipsddc21482019-10-16 14:30:09 -04001464 auto releaseProc = [](void* ctx) {
1465 int* index = (int*) ctx;
1466 *index = 1;
1467 };
Greg Danielc27eb722018-08-10 09:48:08 -04001468
Robert Phillipsddc21482019-10-16 14:30:09 -04001469 for (int i = 0; i < 3; ++i) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001470 backends[i] = dContext->createBackendTexture(16, 16, SkColorType::kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001471 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001472 wrapped[i] = gpu->wrapBackendTexture(backends[i],
Robert Phillipsddc21482019-10-16 14:30:09 -04001473 GrWrapOwnership::kBorrow_GrWrapOwnership,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001474 (i < 2) ? GrWrapCacheable::kYes : GrWrapCacheable::kNo,
1475 GrIOType::kRead_GrIOType)
1476 .release();
Robert Phillipsddc21482019-10-16 14:30:09 -04001477 wrapped[i]->setRelease(releaseProc, &freed[i]);
1478 }
1479
1480 cache->insertDelayedTextureUnref(wrapped[0]);
1481 cache->insertDelayedTextureUnref(wrapped[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001482
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001483 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1484 // is because inserting it as a cross-context resource actually holds a ref until the
1485 // message is received.
Robert Phillipsddc21482019-10-16 14:30:09 -04001486 cache->insertDelayedTextureUnref(wrapped[2]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001487
Robert Phillipsddc21482019-10-16 14:30:09 -04001488 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001489
1490 // Have only ref waiting on message.
Robert Phillipsddc21482019-10-16 14:30:09 -04001491 wrapped[0]->unref();
1492 wrapped[1]->unref();
1493 wrapped[2]->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001494
Robert Phillipsddc21482019-10-16 14:30:09 -04001495 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001496
1497 // This should free nothing since no messages were sent.
1498 cache->purgeAsNeeded();
1499
Robert Phillipsddc21482019-10-16 14:30:09 -04001500 REPORTER_ASSERT(reporter, 0 == (freed[0] + freed[1] + freed[2]));
1501
Greg Danielc27eb722018-08-10 09:48:08 -04001502 // Send message to free the first resource
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001503 GrTextureFreedMessage msg1{wrapped[0], dContext->priv().contextID()};
Robert Phillipsddc21482019-10-16 14:30:09 -04001504 SkMessageBus<GrTextureFreedMessage>::Post(msg1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001505 cache->purgeAsNeeded();
1506
Robert Phillipsddc21482019-10-16 14:30:09 -04001507 REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
1508 REPORTER_ASSERT(reporter, 1 == freed[0]);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001509
Robert Phillips0c5bb2f2020-07-17 15:40:13 -04001510 GrTextureFreedMessage msg2{wrapped[2], dContext->priv().contextID()};
Robert Phillipsddc21482019-10-16 14:30:09 -04001511 SkMessageBus<GrTextureFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001512 cache->purgeAsNeeded();
1513
Robert Phillipsddc21482019-10-16 14:30:09 -04001514 REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
1515 REPORTER_ASSERT(reporter, 0 == freed[1]);
Greg Danielc27eb722018-08-10 09:48:08 -04001516
1517 mock.reset();
1518
Robert Phillipsddc21482019-10-16 14:30:09 -04001519 REPORTER_ASSERT(reporter, 3 == (freed[0] + freed[1] + freed[2]));
Greg Danielc27eb722018-08-10 09:48:08 -04001520}
1521
Brian Salomondcfca432017-11-15 15:48:03 -05001522DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001523 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001524 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001525 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001526 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001527 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001528 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001529 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001530 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001531 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001532 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001533 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001534 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001535 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001536 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001537 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001538 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001539 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001540 test_tags(reporter);
Robert Phillipsddc21482019-10-16 14:30:09 -04001541 test_free_texture_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001542}
1543
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001544// This simulates a portion of Chrome's context abandonment processing.
1545// Please see: crbug.com/1011368 and crbug.com/1014993
1546DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001547 auto context = ctxInfo.directContext();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001548 GrGpu* gpu = context->priv().getGpu();
1549 GrResourceCache* cache = context->priv().getResourceCache();
1550
1551 GrBackendTexture backend = context->createBackendTexture(16, 16,
1552 SkColorType::kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001553 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001554 GrTexture* tex = gpu->wrapBackendTexture(backend,
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001555 GrWrapOwnership::kBorrow_GrWrapOwnership,
1556 GrWrapCacheable::kYes,
Brian Salomon8a78e9c2020-03-27 10:42:15 -04001557 GrIOType::kRead_GrIOType)
1558 .release();
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001559
1560 auto releaseProc = [](void* ctx) {
1561 int* index = (int*) ctx;
1562 *index = 1;
1563 };
1564
1565 int freed = 0;
1566
1567 tex->setRelease(releaseProc, &freed);
1568
1569 cache->insertDelayedTextureUnref(tex);
1570
1571 // Now only the cache is holding a ref to this texture
1572 tex->unref();
1573
1574 REPORTER_ASSERT(reporter, 0 == freed);
1575
Greg Daniel1a5d2d52019-12-04 11:14:29 -05001576 // We must delete the backend texture before abandoning the context in vulkan. We just do it
1577 // for all the backends for consistency.
1578 context->deleteBackendTexture(backend);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001579 context->abandonContext();
1580
1581 REPORTER_ASSERT(reporter, 1 == freed);
1582
1583 // In the past, creating this message could cause an exception due to
1584 // an un-safe downcast from GrTexture to GrGpuResource
1585 GrTextureFreedMessage msg{tex, context->priv().contextID()};
1586 SkMessageBus<GrTextureFreedMessage>::Post(msg);
1587
Greg Danielf0e04f02019-12-04 15:17:54 -05001588 // This doesn't actually do anything but it does trigger us to read messages
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001589 context->purgeUnlockedResources(false);
Robert Phillips1dfc77c2019-10-16 16:39:45 -04001590}
1591
Robert Phillipsd6214d42016-11-07 08:23:48 -05001592////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001593static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001594 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001595 SkISize dims,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001596 int sampleCnt) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001597 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomon7e67dca2020-07-21 09:27:25 -04001598 return provider->createTexture(dims, format, renderable, sampleCnt, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -04001599 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001600}
1601
Robert Phillipse94b4e12020-07-23 13:54:35 -04001602static sk_sp<GrTextureProxy> make_mipmap_proxy(GrRecordingContext* rContext,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001603 GrRenderable renderable,
Brian Salomona56a7462020-02-07 14:17:25 -05001604 SkISize dims,
Robert Phillipse78b7252017-04-06 07:59:41 -04001605 int sampleCnt) {
Robert Phillipse94b4e12020-07-23 13:54:35 -04001606 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
1607 const GrCaps* caps = rContext->priv().caps();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001608
Robert Phillipsd6214d42016-11-07 08:23:48 -05001609
Robert Phillips0a15cc62019-07-30 12:49:10 -04001610 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1611 GrRenderable::kNo);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001612
Brian Salomon7e67dca2020-07-21 09:27:25 -04001613 return proxyProvider->createProxy(format, dims, renderable, sampleCnt, GrMipmapped::kYes,
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001614 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001615}
1616
1617// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1618// Texture-only, both-RT-and-Texture and MIPmapped
1619DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001620 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001621 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001622 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001623
Brian Salomona56a7462020-02-07 14:17:25 -05001624 static constexpr SkISize kSize = {64, 64};
1625 static constexpr auto kArea = kSize.area();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001626
Robert Phillipsd6214d42016-11-07 08:23:48 -05001627 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001628 {
1629 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001630
Brian Salomona56a7462020-02-07 14:17:25 -05001631 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001632 size_t size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001633 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001634
Greg Daniel6fa62e22019-08-07 15:52:37 -04001635 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001636 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001637 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001638 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001639 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001640 kArea*4 == size || // msaa4 failed
1641 kArea*4*sampleCount == size || // auto-resolving
1642 kArea*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001643 }
1644
Brian Salomona56a7462020-02-07 14:17:25 -05001645 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001646 size = tex->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001647 REPORTER_ASSERT(reporter, kArea*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001648 }
1649
Robert Phillipsd6214d42016-11-07 08:23:48 -05001650 // Mipmapped versions
Brian Salomon69100f02020-07-21 10:49:25 -04001651 if (caps->mipmapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001652 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001653
Brian Salomona56a7462020-02-07 14:17:25 -05001654 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, 1);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001655 size_t size = proxy->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001656 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001657
Greg Daniel6fa62e22019-08-07 15:52:37 -04001658 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001659 if (sampleCount >= 4) {
Brian Salomona56a7462020-02-07 14:17:25 -05001660 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, sampleCount);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001661 size = proxy->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001662 REPORTER_ASSERT(reporter,
Brian Salomona56a7462020-02-07 14:17:25 -05001663 kArea*4 + (kArea*4)/3 == size || // msaa4 failed
1664 kArea*4*sampleCount + (kArea*4)/3 == size || // auto-resolving
1665 kArea*4*(sampleCount+1) + (kArea*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001666 }
Robert Phillips1b352562017-04-05 18:56:21 +00001667
Brian Salomona56a7462020-02-07 14:17:25 -05001668 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, 1);
Greg Daniel0eca74c2020-10-01 13:46:00 -04001669 size = proxy->gpuMemorySize();
Brian Salomona56a7462020-02-07 14:17:25 -05001670 REPORTER_ASSERT(reporter, kArea*4 + (kArea*4)/3 == size);
Robert Phillipse78b7252017-04-06 07:59:41 -04001671 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001672}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001673
1674#if GR_GPU_STATS
1675DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001676 auto context = ctxInfo.directContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001677 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001678
1679 // Helper that determines if cache is overbudget.
1680 auto overbudget = [context] {
1681 int uNum;
1682 size_t uSize;
1683 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001684 size_t bSize = context->getResourceCacheLimit();
1685 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001686 };
1687
1688 // Helper that does a trivial draw to a surface.
1689 auto drawToSurf = [](SkSurface* surf) {
1690 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1691 };
1692
1693 // Helper that checks whether a flush has occurred between calls.
1694 int baseFlushCount = 0;
1695 auto getFlushCountDelta = [context, &baseFlushCount]() {
Greg Danielfe159622020-04-10 17:43:51 +00001696 int cur = context->priv().getGpu()->stats()->numSubmitToGpus();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001697 int delta = cur - baseFlushCount;
1698 baseFlushCount = cur;
1699 return delta;
1700 };
1701
1702 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1703 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1704 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1705
1706 drawToSurf(surf1.get());
1707 drawToSurf(surf2.get());
1708
1709 // Flush each surface once to ensure that their backing stores are allocated.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001710 surf1->flushAndSubmit();
1711 surf2->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001712 REPORTER_ASSERT(reporter, overbudget());
1713 getFlushCountDelta();
1714
1715 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1716 drawToSurf(surf1.get());
1717 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1718 drawToSurf(surf2.get());
1719 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1720 REPORTER_ASSERT(reporter, overbudget());
1721
1722 // Make surf1 purgeable. Drawing to surf2 should flush.
Greg Daniel0a2464f2020-05-14 15:45:44 -04001723 surf1->flushAndSubmit();
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001724 surf1.reset();
1725 drawToSurf(surf2.get());
1726 REPORTER_ASSERT(reporter, getFlushCountDelta());
1727 REPORTER_ASSERT(reporter, overbudget());
1728}
1729#endif