blob: 3d94c0f53a18701840f4a488c56125863d412413 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
bsalomon3f324322015-04-08 11:01:54 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrGpuResourceCacheAccess.h"
15#include "src/gpu/GrGpuResourcePriv.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRenderTargetPriv.h"
18#include "src/gpu/GrResourceCache.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "tools/gpu/GrContextFactory.h"
Robert Phillips646e4292017-06-13 12:44:56 -040021
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkCanvas.h"
23#include "include/core/SkSurface.h"
Ben Wagner21bca282019-05-15 10:15:52 -040024#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkMipMap.h"
26#include "src/gpu/SkGr.h"
27#include "tests/Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000028
Hal Canary8a001442018-09-19 11:31:27 -040029#include <thread>
30
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070036 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080037 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070038 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080039 SkCanvas* canvas = surface->getCanvas();
40
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
42
43 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000044 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040046 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000048 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070049 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050
Robert Phillipscf39f372019-09-03 10:29:20 -040051 size_t oldMaxBytes = context->getResourceCacheLimit();
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000052
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053 // Set the cache limits so we can fit 10 "src" images and the
54 // max number of textures doesn't matter
55 size_t maxCacheSize = initialCacheSize + 10*srcSize;
Robert Phillipscf39f372019-09-03 10:29:20 -040056 context->setResourceCacheLimit(maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000059 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060
61 for (int i = 0; i < 100; ++i) {
62 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040063 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 // "modify" the src texture
66 src.notifyPixelsChanged();
67
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000068 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070069 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000070
71 // we should never go over the size limit
72 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
73 }
74
Robert Phillipscf39f372019-09-03 10:29:20 -040075 context->setResourceCacheLimit(oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000076}
77
bsalomon11abd8d2016-10-14 08:13:48 -070078static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
79 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
80 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
81 return false;
82 }
83 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
84}
85
Robert Phillipsc0192e32017-09-21 12:00:26 -040086static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
87 return rt->renderTargetPriv().getStencilAttachment();
88}
89
90static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
91 int size, int sampleCount, SkBudgeted budgeted) {
92 GrSurfaceDesc desc;
Robert Phillipsc0192e32017-09-21 12:00:26 -040093 desc.fWidth = size;
94 desc.fHeight = size;
95 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillipsc0192e32017-09-21 12:00:26 -040096
Brian Salomon4eb38b72019-08-05 12:58:39 -040097 auto format =
98 provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kYes);
99 sk_sp<GrTexture> tex(provider->createTexture(desc, format, GrRenderable::kYes, sampleCount,
Brian Salomona90382f2019-09-17 09:01:56 -0400100 GrMipMapped::kNo, budgeted, GrProtected::kNo));
Robert Phillipsc0192e32017-09-21 12:00:26 -0400101 if (!tex || !tex->asRenderTarget()) {
102 return nullptr;
103 }
104
Chris Daltoneffee202019-07-01 22:28:03 -0600105 if (!provider->attachStencilAttachment(tex->asRenderTarget(), sampleCount)) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400106 return nullptr;
107 }
108 SkASSERT(get_SB(tex->asRenderTarget()));
109
110 return sk_ref_sp(tex->asRenderTarget());
111}
112
bsalomon11abd8d2016-10-14 08:13:48 -0700113// This currently fails on ES3 ANGLE contexts
114DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000115 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700116 GrContext* context = ctxInfo.grContext();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400117 const GrCaps* caps = context->priv().caps();
118
119 if (caps->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700120 return;
121 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400122
Robert Phillips9da87e02019-02-04 13:26:26 -0500123 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400124
Greg Daniel5c96db82019-07-09 14:06:58 -0400125 GrColorType grColorType = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400126 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400127
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400129 REPORTER_ASSERT(reporter, smallRT0);
130
131 {
132 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
134 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400135
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800137 }
138
Robert Phillipsc0192e32017-09-21 12:00:26 -0400139 {
140 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400142 REPORTER_ASSERT(reporter, smallRT2);
143
144 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800145 }
146
Robert Phillipsc0192e32017-09-21 12:00:26 -0400147 {
148 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500149 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400150 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800151
Robert Phillipsc0192e32017-09-21 12:00:26 -0400152 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800153 }
bsalomon02a44a42015-02-19 09:09:00 -0800154
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400155 int smallSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400156 context->priv().caps()->getRenderTargetSampleCount(2, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500157 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700158 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500159 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
160 smallSampleCount, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400161 REPORTER_ASSERT(reporter, smallMSAART0);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400162
163 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
164
165 {
166 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500167 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
168 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400169 SkBudgeted::kNo);
170 REPORTER_ASSERT(reporter, smallMSAART1);
171
172 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800173 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400174
Brian Salomonbdecacf2018-02-02 20:32:49 -0500175 // But one with a larger sample count should not. (Also check that the two requests didn't
176 // rounded up to the same actual sample count or else they could share.).
Greg Daniel6fa62e22019-08-07 15:52:37 -0400177 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(5, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500178 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500179 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
180 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400181 SkBudgeted::kNo);
182 REPORTER_ASSERT(reporter, smallMSAART2);
183
184 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800185 }
186 }
187}
188
bsalomon68d91342016-04-12 09:59:58 -0700189DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700190 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
192 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700193 // this test is only valid for GL
194 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700195 return;
196 }
197
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500198 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700199 static const int kW = 100;
200 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700201
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400202 backendTextures[0] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
203 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400204 GrMipMapped::kNo, GrRenderable::kNo,
205 GrProtected::kNo);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400206 backendTextures[1] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
207 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400208 GrMipMapped::kNo, GrRenderable::kNo,
209 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500210 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
211 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
212 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
213 return;
214 }
jvanverth672bb7f2015-07-13 07:19:57 -0700215
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216 context->resetContext();
217
Robert Phillips6be756b2018-01-16 15:07:54 -0500218 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400219 backendTextures[0], GrColorType::kRGBA_8888,
220 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221
Robert Phillips6be756b2018-01-16 15:07:54 -0500222 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400223 backendTextures[1], GrColorType::kRGBA_8888,
224 kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225
Brian Osman85d34b22017-05-10 12:06:26 -0400226 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
227 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228 return;
229 }
230
halcanary96fcdcc2015-08-27 07:41:13 -0700231 borrowed.reset(nullptr);
232 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233
234 context->flush();
235
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500236 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
237 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700238
239 REPORTER_ASSERT(reporter, borrowedIsAlive);
240 REPORTER_ASSERT(reporter, !adoptedIsAlive);
241
Brian Salomone64b0642018-03-07 11:47:54 -0500242 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400243 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500244 }
245 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400246 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500247 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700248
249 context->resetContext();
250}
251
bsalomon6d3fe022014-07-25 08:35:45 -0700252class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800253 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000254public:
robertphillips6e83ac72015-08-13 05:19:14 -0700255 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700256
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 /** Property that distinctly categorizes the resource.
258 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800259 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800260
kkinnunen2e6055b2016-04-22 01:48:29 -0700261 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
262 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700263 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800264 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700265 , fProperty(kA_SimulatedProperty)
266 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800267 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700268 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800269 }
270
kkinnunen2e6055b2016-04-22 01:48:29 -0700271 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400272 SimulatedProperty property, size_t size = kDefaultSize) {
273 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800274 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500275 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
276 size_t size = kDefaultSize) {
277 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278 }
279
Brian Salomond3b65972017-03-22 12:05:03 -0400280 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800281 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000282 }
283
bsalomon33435572014-11-05 14:47:41 -0800284 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000285
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400286 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
287 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000288 }
289
bsalomon1c60dfe2015-01-21 09:32:40 -0800290 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
291 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
292 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800293 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
294 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800295 }
296 }
297
298 static size_t ExpectedScratchKeySize() {
299 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
300 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000301private:
bsalomon24db3b12015-01-23 04:24:04 -0800302 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800303
Greg Danielda86e282018-06-13 09:41:19 -0400304 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
305 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700306 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700307 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400308 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700309 , fProperty(property)
310 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800311 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700312 this->registerWithCache(budgeted);
313 }
314
315 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500316 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
317 : INHERITED(gpu)
318 , fToDelete(nullptr)
319 , fSize(size)
320 , fProperty(kA_SimulatedProperty)
321 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700322 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500323 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700324 }
325
326 void computeScratchKey(GrScratchKey* key) const override {
327 if (fIsScratch) {
328 ComputeScratchKey(fProperty, key);
329 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800330 }
331
mtklein36352bf2015-03-25 18:17:31 -0700332 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400333 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800334
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400335 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000336 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800337 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800338 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700339 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700340 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000341};
bsalomon33435572014-11-05 14:47:41 -0800342int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000343
bsalomonc2f35b72015-01-23 07:19:22 -0800344class Mock {
345public:
Robert Phillipscf39f372019-09-03 10:29:20 -0400346 Mock(size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400347 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800348 SkASSERT(fContext);
Robert Phillipscf39f372019-09-03 10:29:20 -0400349 fContext->setResourceCacheLimit(maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500350 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800351 cache->purgeAllUnlocked();
352 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800353 }
bsalomonc2f35b72015-01-23 07:19:22 -0800354
Robert Phillips9da87e02019-02-04 13:26:26 -0500355 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800356
Hal Canary342b7ac2016-11-04 11:49:42 -0400357 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800358
Greg Danielc27eb722018-08-10 09:48:08 -0400359 void reset() {
360 fContext.reset();
361 }
362
bsalomonc2f35b72015-01-23 07:19:22 -0800363private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400364 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800365};
366
367static void test_no_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400368 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800369 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800370 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500371 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800372
373 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400374 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
375 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
376 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
377 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800378
379 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800380 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800381 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800382 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800385 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
387
bsalomon8718aaf2015-02-19 07:24:21 -0800388 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800389
390 a->unref();
391 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800392 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800393 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800394 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800395
396 c->unref();
397 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800398 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800399 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800400 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800401
402 d->unref();
403 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800404 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
405 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 b->unref();
408 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411}
412
bsalomon24db3b12015-01-23 04:24:04 -0800413// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500414template <int>
415static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800416 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500417 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800418 builder[0] = data;
419}
420
Robert Phillips6eba0632018-03-28 12:25:42 -0400421static void test_purge_unlocked(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400422 Mock mock(30000);
Robert Phillips6eba0632018-03-28 12:25:42 -0400423 GrContext* context = mock.context();
424 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500425 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400426
427 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
428 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400429 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400430
431 GrUniqueKey uniqueKey;
432 make_unique_key<0>(&uniqueKey, 0);
433
434 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400435 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400436 b->resourcePriv().setUniqueKey(uniqueKey);
437
438 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400439 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400440
441 GrUniqueKey uniqueKey2;
442 make_unique_key<0>(&uniqueKey2, 1);
443
444 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400445 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400446 d->resourcePriv().setUniqueKey(uniqueKey2);
447
448
449 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
450 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
451 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
452 d->gpuMemorySize() == cache->getResourceBytes());
453
454 // Should be safe to purge without deleting the resources since we still have refs.
455 cache->purgeUnlockedResources(false);
456 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
457
458 // Unref them all. Since they all have keys they should remain in the cache.
459
460 a->unref();
461 b->unref();
462 c->unref();
463 d->unref();
464 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
465 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
466 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
467 d->gpuMemorySize() == cache->getResourceBytes());
468
469 // Purge only the two scratch resources
470 cache->purgeUnlockedResources(true);
471
472 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
473 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
474 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
475 cache->getResourceBytes());
476
477 // Purge the uniquely keyed resources
478 cache->purgeUnlockedResources(false);
479
480 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
481 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
482 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
483}
484
bsalomon84c8e622014-11-17 09:33:27 -0800485static void test_budgeting(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400486 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800487 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800488 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500489 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800490
bsalomon8718aaf2015-02-19 07:24:21 -0800491 GrUniqueKey uniqueKey;
492 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800493
bsalomon8718aaf2015-02-19 07:24:21 -0800494 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800495 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400496 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
497 10);
498 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800499 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500500 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
501 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
502 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800503
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500504 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800505 GrUniqueKey uniqueKey2;
506 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500507 GrUniqueKey uniqueKey3;
508 make_unique_key<0>(&uniqueKey3, 2);
509 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
510 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
511 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
512 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
513 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
514 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400515
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500516 // Remove the extra refs we just added.
517 SkSafeUnref(wrappedCacheableViaKey);
518 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800519
520 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500521 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800522 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500523 wrappedCacheable->gpuMemorySize() +
524 wrappedUncacheable->gpuMemorySize() +
525 unbudgeted->gpuMemorySize() ==
526 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800527 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800528 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800529 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400530 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800531
bsalomon63c992f2015-01-23 12:47:59 -0800532 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800533 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500534 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800535 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500536 wrappedCacheable->gpuMemorySize() +
537 wrappedUncacheable->gpuMemorySize() +
538 unbudgeted->gpuMemorySize() ==
539 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800540 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800541 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800542 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400543 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800544
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500545 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
546 // However, unreffing the uncacheable wrapped resource should free it.
547 wrappedCacheable->unref();
548 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400549 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800550 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500551 wrappedCacheable->gpuMemorySize() +
552 unbudgeted->gpuMemorySize() ==
553 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500554 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800555
bsalomon84c8e622014-11-17 09:33:27 -0800556 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500557 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800558 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500559 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
560 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
561 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800562 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500563 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500564
565 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
566 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
567 if (wrappedCacheableViaKey) {
568 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
569 wrappedCacheable->unref();
570 }
571 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
572 // resource should immediately delete it.
573 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
574
575 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500576 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
577 wrappedUncacheable->gpuMemorySize() +
578 unbudgeted->gpuMemorySize() ==
579 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800580 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
581 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400582 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800583
584 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400585 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800586 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500587 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
588 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
589 wrappedUncacheable->gpuMemorySize() ==
590 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
592 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400593 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800594
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500595 // Unreffing the wrapped resources (with no unique key) should free them right away.
596 wrappedUncacheable->unref();
597 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
599 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
600 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
601 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400602 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800603
604 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800605 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
606 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
607 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
608 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400609 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800610}
611
bsalomon5236cf42015-01-14 10:42:08 -0800612static void test_unbudgeted(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400613 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800614 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800615 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500616 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800617
bsalomon8718aaf2015-02-19 07:24:21 -0800618 GrUniqueKey uniqueKey;
619 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800620
621 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800622 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800623 TestResource* wrapped;
624 TestResource* unbudgeted;
625
626 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500627 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400628 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700629
bsalomon5236cf42015-01-14 10:42:08 -0800630 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800631 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
632 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
633 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
634 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400635 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800636
Greg Danielda86e282018-06-13 09:41:19 -0400637 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800638 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800639 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800640 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
641 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
642 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
643 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400644 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800645
bsalomon0ea80f42015-02-11 10:49:59 -0800646 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500647 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800648 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
649 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
650 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
651 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400652 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800653
654 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800655 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
656 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
657 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
658 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400659 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800660
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500661 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800662 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
663 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
664 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
665 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400666 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800667
668 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800669 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
670 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
671 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
672 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400673 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800674
bsalomon0ea80f42015-02-11 10:49:59 -0800675 cache->purgeAllUnlocked();
676 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
677 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
678 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
679 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400680 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800681}
682
bsalomon3582d3e2015-02-13 14:20:05 -0800683// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
684void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
685/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400686 Mock mock(300);
bsalomonc2f35b72015-01-23 07:19:22 -0800687 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800688 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500689 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800690
691 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500692 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800693 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800694 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800695
696 size_t size = resource->gpuMemorySize();
697 for (int i = 0; i < 2; ++i) {
698 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800699 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800700 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500701 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500702 resource->resourcePriv().budgetedType());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400703 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(key));
bsalomon0ea80f42015-02-11 10:49:59 -0800704 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
705 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
706 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
707 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400708 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800709
710 // Once it is unrefed, it should become available as scratch.
711 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800712 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
713 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
714 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
715 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400716 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Robert Phillipsaee18c92019-09-06 11:48:27 -0400717 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800718 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800719 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800720 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500721 REPORTER_ASSERT(reporter,
722 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800723
724 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700725 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800726 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800727 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800728 } else {
729 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800730 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800731 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
732 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
733 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
734 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400735 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800736 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800737 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500738 REPORTER_ASSERT(reporter,
739 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800740
741 // now when it is unrefed it should die since it has no key.
742 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800743 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
744 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
745 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
746 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400747 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800748 }
bsalomon8b79d232014-11-10 10:19:06 -0800749 }
bsalomonc2f35b72015-01-23 07:19:22 -0800750}
751
752static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400753 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800754 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800755 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500756 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800757
bsalomon8b79d232014-11-10 10:19:06 -0800758 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500759 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700760 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400761 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500762 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700763 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400764 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800765 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800766 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800767 // Check for negative case consistency. (leaks upon test failure.)
Robert Phillipsaee18c92019-09-06 11:48:27 -0400768 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800769
770 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800771 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800772
bsalomon0ea80f42015-02-11 10:49:59 -0800773 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800774 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800775 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
776 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800777 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800778 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800779
bsalomon63c992f2015-01-23 12:47:59 -0800780 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800781 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800782 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800783 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800784
785 // Unref but don't purge
786 a->unref();
787 b->unref();
788 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800789 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800790
bsalomon63c992f2015-01-23 12:47:59 -0800791 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800792 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800793 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800794 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
795 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800796}
797
bsalomon10e23ca2014-11-25 05:52:06 -0800798static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400799 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800800 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800801 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500802 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800803
bsalomon10e23ca2014-11-25 05:52:06 -0800804 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500805 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800806 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500807 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800808 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800809 a->unref();
810 b->unref();
811
bsalomon1c60dfe2015-01-21 09:32:40 -0800812 GrScratchKey scratchKey;
813 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800814 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800815 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400816 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800817
bsalomon0ea80f42015-02-11 10:49:59 -0800818 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800819 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800820 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800821 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
822 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800823
824 // Find the first resource and remove its scratch key
Robert Phillipsaee18c92019-09-06 11:48:27 -0400825 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800826 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800827 // It's still alive, but not cached by scratch key anymore
828 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800829 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
830 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800831
832 // The cache should immediately delete it when it's unrefed since it isn't accessible.
833 find->unref();
834 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800835 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
836 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800837
838 // Repeat for the second resource.
Robert Phillipsaee18c92019-09-06 11:48:27 -0400839 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800840 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800841 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800842 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
843 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800844
845 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800846 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800847 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800848 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
849 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800850
851 find->unref();
852 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800853 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
854 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800855}
856
bsalomon1c60dfe2015-01-21 09:32:40 -0800857static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400858 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800859 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800860 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500861 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800862
863 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500864 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800865 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500866 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800867 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800868 a->unref();
869 b->unref();
870
871 GrScratchKey scratchKey;
872 // Ensure that scratch key comparison and assignment is consistent.
873 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800874 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800875 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800876 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800877 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
878 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
879 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
880 scratchKey = scratchKey1;
881 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
882 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
883 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
884 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
885 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
886 scratchKey = scratchKey2;
887 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
888 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
889 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
890 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
891 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
892
893 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800894 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800895 // (following leaks upon test failure).
Robert Phillipsaee18c92019-09-06 11:48:27 -0400896 REPORTER_ASSERT(reporter, !cache->findAndRefScratchResource(scratchKey));
bsalomon1c60dfe2015-01-21 09:32:40 -0800897
898 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800899 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -0400900 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
halcanary96fcdcc2015-08-27 07:41:13 -0700901 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800902 find->unref();
903
904 scratchKey2 = scratchKey;
Robert Phillipsaee18c92019-09-06 11:48:27 -0400905 find = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700906 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800907 REPORTER_ASSERT(reporter, find == a || find == b);
908
Robert Phillipsaee18c92019-09-06 11:48:27 -0400909 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700910 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800911 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
912 REPORTER_ASSERT(reporter, find2 != find);
913 find2->unref();
914 find->unref();
915}
916
bsalomon8718aaf2015-02-19 07:24:21 -0800917static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400918 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -0800919 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800920 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500921 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000922
bsalomon8718aaf2015-02-19 07:24:21 -0800923 GrUniqueKey key;
924 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700925
bsalomon8718aaf2015-02-19 07:24:21 -0800926 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400927 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700928
bsalomonf99e9612015-02-19 08:24:16 -0800929 // Set key on resource a.
930 a->resourcePriv().setUniqueKey(key);
931 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
932 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800933
bsalomonf99e9612015-02-19 08:24:16 -0800934 // Make sure that redundantly setting a's key works.
935 a->resourcePriv().setUniqueKey(key);
936 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800937 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800938 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
939 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800940 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
941
bsalomonf99e9612015-02-19 08:24:16 -0800942 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400943 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800944 b->resourcePriv().setUniqueKey(key);
945 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
946 b->unref();
947
948 // Still have two resources because a is still reffed.
949 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
950 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
951 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
952
953 a->unref();
954 // Now a should be gone.
955 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
956 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
957 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
958
959 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
960 // Also make b be unreffed when replacement occurs.
961 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400962 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800963 GrUniqueKey differentKey;
964 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800965 c->resourcePriv().setUniqueKey(differentKey);
966 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
967 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
968 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
969 // c replaces b and b should be immediately purged.
970 c->resourcePriv().setUniqueKey(key);
971 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
972 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
973 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
974
975 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800976 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800977 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
978 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
979 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
980
981 // Drop the ref on c, it should be kept alive because it has a unique key.
982 c->unref();
983 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
984 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
985 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
986
987 // Verify that we can find c, then remove its unique key. It should get purged immediately.
988 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
989 c->resourcePriv().removeUniqueKey();
990 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800991 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
992 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800993 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700994
995 {
996 GrUniqueKey key2;
997 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500998 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700999 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001000 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001001 d->resourcePriv().setUniqueKey(key2);
1002 }
1003
1004 GrUniqueKey key3;
1005 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001006 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001007 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001008}
1009
bsalomon8b79d232014-11-10 10:19:06 -08001010static void test_purge_invalidated(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001011 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001012 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001013 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001014 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001015
bsalomon8718aaf2015-02-19 07:24:21 -08001016 GrUniqueKey key1, key2, key3;
1017 make_unique_key<0>(&key1, 1);
1018 make_unique_key<0>(&key2, 2);
1019 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001020
bsalomon23e619c2015-02-06 11:54:28 -08001021 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001022 TestResource* a = new TestResource(gpu);
1023 TestResource* b = new TestResource(gpu);
1024 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001025 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001026 a->resourcePriv().setUniqueKey(key1);
1027 b->resourcePriv().setUniqueKey(key2);
1028 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001029 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001030 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001031 c->unref();
1032
bsalomon8718aaf2015-02-19 07:24:21 -08001033 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1034 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1035 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001036 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001037
bsalomon8718aaf2015-02-19 07:24:21 -08001038 typedef GrUniqueKeyInvalidatedMessage Msg;
1039 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001040
1041 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001042 Bus::Post(Msg(key1, context->priv().contextID()));
1043 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001044 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001045 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001046 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1047 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001048 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001049 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001050
1051 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001052 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001053 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001054 // we still have a ref on b, c should be recycled as scratch.
1055 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001056 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001057
bsalomon23e619c2015-02-06 11:54:28 -08001058 // make b purgeable. It should be immediately deleted since it has no key.
1059 b->unref();
1060 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1061
1062 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1063 GrScratchKey scratchKey;
1064 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Robert Phillipsaee18c92019-09-06 11:48:27 -04001065 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -08001066 REPORTER_ASSERT(reporter, scratch == c);
1067 SkSafeUnref(scratch);
1068
1069 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001070 cache->purgeAllUnlocked();
Robert Phillipsaee18c92019-09-06 11:48:27 -04001071 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -08001072 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001073 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1074 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001075 REPORTER_ASSERT(reporter, !scratch);
1076 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001077}
1078
bsalomon71cb0c22014-11-14 12:10:14 -08001079static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001080 Mock mock(30000);
bsalomonc2f35b72015-01-23 07:19:22 -08001081 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001082 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001083 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001084
bsalomon8718aaf2015-02-19 07:24:21 -08001085 GrUniqueKey key1, key2;
1086 make_unique_key<0>(&key1, 1);
1087 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001088
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001089 sk_sp<TestResource> a(new TestResource(gpu));
1090 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001091 a->resourcePriv().setUniqueKey(key1);
1092 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001093
bsalomonc2f35b72015-01-23 07:19:22 -08001094 // Make a cycle
1095 a->setUnrefWhenDestroyed(b);
1096 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001097
bsalomonc2f35b72015-01-23 07:19:22 -08001098 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001099
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001100 TestResource* unownedA = a.release();
1101 unownedA->unref();
1102 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001103
bsalomonc2f35b72015-01-23 07:19:22 -08001104 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001105
bsalomon0ea80f42015-02-11 10:49:59 -08001106 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001107 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001108
bsalomonc2f35b72015-01-23 07:19:22 -08001109 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001110 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001111 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001112
bsalomon0ea80f42015-02-11 10:49:59 -08001113 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001114 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001115}
1116
bsalomonddf30e62015-02-19 11:38:44 -08001117static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1118 static const int kCount = 50;
bsalomonddf30e62015-02-19 11:38:44 -08001119 static const int kLockedFreq = 8;
Robert Phillipscf39f372019-09-03 10:29:20 -04001120 static const int kBudgetSize = 0; // always over budget
bsalomonddf30e62015-02-19 11:38:44 -08001121
1122 SkRandom random;
1123
1124 // Run the test 2*kCount times;
1125 for (int i = 0; i < 2 * kCount; ++i ) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001126 Mock mock(kBudgetSize);
bsalomonddf30e62015-02-19 11:38:44 -08001127 GrContext* context = mock.context();
1128 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001129 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001130
1131 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001132 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001133
Robert Phillipscf39f372019-09-03 10:29:20 -04001134 static const int kNumToPurge = kCount;
bsalomonddf30e62015-02-19 11:38:44 -08001135
1136 SkTDArray<int> shouldPurgeIdxs;
1137 int purgeableCnt = 0;
1138 SkTDArray<GrGpuResource*> resourcesToUnref;
1139
1140 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1141 // unpurgeable resources.
1142 for (int j = 0; j < kCount; ++j) {
1143 GrUniqueKey key;
1144 make_unique_key<0>(&key, j);
1145
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001146 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001147 r->resourcePriv().setUniqueKey(key);
1148 if (random.nextU() % kLockedFreq) {
1149 // Make this is purgeable.
1150 r->unref();
1151 ++purgeableCnt;
1152 if (purgeableCnt <= kNumToPurge) {
1153 *shouldPurgeIdxs.append() = j;
1154 }
1155 } else {
1156 *resourcesToUnref.append() = r;
1157 }
1158 }
1159
1160 // Verify that the correct resources were purged.
1161 int currShouldPurgeIdx = 0;
1162 for (int j = 0; j < kCount; ++j) {
1163 GrUniqueKey key;
1164 make_unique_key<0>(&key, j);
1165 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1166 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1167 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1168 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001169 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001170 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001171 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001172 }
1173 SkSafeUnref(res);
1174 }
1175
1176 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1177 resourcesToUnref[j]->unref();
1178 }
1179 }
1180}
1181
Brian Salomon5e150852017-03-22 14:53:13 -04001182static void test_time_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001183 Mock mock(1000000);
Brian Salomon5e150852017-03-22 14:53:13 -04001184 GrContext* context = mock.context();
1185 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001186 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001187
1188 static constexpr int kCnts[] = {1, 10, 1024};
1189 auto nowish = []() {
1190 // We sleep so that we ensure we get a value that is greater than the last call to
1191 // GrStdSteadyClock::now().
1192 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1193 auto result = GrStdSteadyClock::now();
1194 // Also sleep afterwards so we don't get this value again.
1195 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1196 return result;
1197 };
1198
1199 for (int cnt : kCnts) {
1200 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1201 new GrStdSteadyClock::time_point[cnt]);
1202 {
1203 // Insert resources and get time points between each addition.
1204 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001205 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001206 GrUniqueKey k;
1207 make_unique_key<1>(&k, i);
1208 r->resourcePriv().setUniqueKey(k);
1209 r->unref();
1210 timeStamps.get()[i] = nowish();
1211 }
1212
1213 // Purge based on the time points between resource additions. Each purge should remove
1214 // the oldest resource.
1215 for (int i = 0; i < cnt; ++i) {
1216 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1217 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1218 for (int j = 0; j < i; ++j) {
1219 GrUniqueKey k;
1220 make_unique_key<1>(&k, j);
1221 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1222 REPORTER_ASSERT(reporter, !SkToBool(r));
1223 SkSafeUnref(r);
1224 }
1225 }
1226
1227 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1228 cache->purgeAllUnlocked();
1229 }
1230
1231 // Do a similar test but where we leave refs on some resources to prevent them from being
1232 // purged.
1233 {
1234 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1235 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001236 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001237 GrUniqueKey k;
1238 make_unique_key<1>(&k, i);
1239 r->resourcePriv().setUniqueKey(k);
1240 // Leave a ref on every other resource, beginning with the first.
1241 if (SkToBool(i & 0x1)) {
1242 refedResources.get()[i / 2] = r;
1243 } else {
1244 r->unref();
1245 }
1246 timeStamps.get()[i] = nowish();
1247 }
1248
1249 for (int i = 0; i < cnt; ++i) {
1250 // Should get a resource purged every other frame.
1251 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1252 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1253 }
1254
1255 // Unref all the resources that we kept refs on in the first loop.
1256 for (int i = 0; i < (cnt / 2); ++i) {
1257 refedResources.get()[i]->unref();
1258 cache->purgeResourcesNotUsedSince(nowish());
1259 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1260 }
1261
1262 cache->purgeAllUnlocked();
1263 }
1264
1265 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1266
1267 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1268 // eviction
1269 context->flush();
1270 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001271 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001272 GrUniqueKey k;
1273 make_unique_key<1>(&k, i);
1274 r->resourcePriv().setUniqueKey(k);
1275 r->unref();
1276 }
1277 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1278 context->flush();
1279 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1280 cache->purgeResourcesNotUsedSince(nowish());
1281 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1282 }
1283}
1284
Derek Sollenberger5480a182017-05-25 16:43:59 -04001285static void test_partial_purge(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001286 Mock mock(100);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001287 GrContext* context = mock.context();
1288 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001289 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001290
1291 enum TestsCase {
1292 kOnlyScratch_TestCase = 0,
1293 kPartialScratch_TestCase = 1,
1294 kAllScratch_TestCase = 2,
1295 kPartial_TestCase = 3,
1296 kAll_TestCase = 4,
1297 kNone_TestCase = 5,
1298 kEndTests_TestCase = kNone_TestCase + 1
1299 };
1300
1301 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1302
1303 GrUniqueKey key1, key2, key3;
1304 make_unique_key<0>(&key1, 1);
1305 make_unique_key<0>(&key2, 2);
1306 make_unique_key<0>(&key3, 3);
1307
1308 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001309 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1310 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1311 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001312
1313 unique1->resourcePriv().setUniqueKey(key1);
1314 unique2->resourcePriv().setUniqueKey(key2);
1315 unique3->resourcePriv().setUniqueKey(key3);
1316
Derek Sollenberger5480a182017-05-25 16:43:59 -04001317 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001318 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001319 TestResource::kA_SimulatedProperty,
1320 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001321 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001322 TestResource::kB_SimulatedProperty,
1323 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001324
1325 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1326 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1327 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1328
1329 // Add resources to the purgeable queue
1330 unique1->unref();
1331 scratch1->unref();
1332 unique2->unref();
1333 scratch2->unref();
1334 unique3->unref();
1335
1336 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1337 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1338 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1339
1340 switch(testCase) {
1341 case kOnlyScratch_TestCase: {
1342 context->purgeUnlockedResources(14, true);
1343 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1344 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1345 break;
1346 }
1347 case kPartialScratch_TestCase: {
1348 context->purgeUnlockedResources(3, true);
1349 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1350 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1351 break;
1352 }
1353 case kAllScratch_TestCase: {
1354 context->purgeUnlockedResources(50, true);
1355 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1356 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1357 break;
1358 }
1359 case kPartial_TestCase: {
1360 context->purgeUnlockedResources(13, false);
1361 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1362 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1363 break;
1364 }
1365 case kAll_TestCase: {
1366 context->purgeUnlockedResources(50, false);
1367 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1368 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1369 break;
1370 }
1371 case kNone_TestCase: {
1372 context->purgeUnlockedResources(0, true);
1373 context->purgeUnlockedResources(0, false);
1374 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1375 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1376 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1377 break;
1378 }
Brian Salomon23356442018-11-30 15:33:19 -05001379 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001380
1381 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001382 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001383 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1384 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1385
1386 }
1387}
1388
senorblanco84cd6212015-08-04 10:01:58 -07001389static void test_custom_data(skiatest::Reporter* reporter) {
1390 GrUniqueKey key1, key2;
1391 make_unique_key<0>(&key1, 1);
1392 make_unique_key<0>(&key2, 2);
1393 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001394 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001395 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1396 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1397
1398 // Test that copying a key also takes a ref on its custom data.
1399 GrUniqueKey key3 = key1;
1400 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1401}
1402
bsalomonc6363ef2015-09-24 07:07:40 -07001403static void test_abandoned(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001404 Mock mock(300);
bsalomonc6363ef2015-09-24 07:07:40 -07001405 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001406 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001407
1408 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001409 context->abandonContext();
1410
1411 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1412
1413 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1414
robertphillips8abb3702016-08-31 14:04:06 -07001415 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001416 resource->getUniqueKey();
1417 resource->wasDestroyed();
1418 resource->gpuMemorySize();
1419 resource->getContext();
1420
bsalomonc6363ef2015-09-24 07:07:40 -07001421 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001422 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001423 resource->resourcePriv().makeBudgeted();
1424 resource->resourcePriv().makeUnbudgeted();
1425 resource->resourcePriv().removeScratchKey();
1426 GrUniqueKey key;
1427 make_unique_key<0>(&key, 1);
1428 resource->resourcePriv().setUniqueKey(key);
1429 resource->resourcePriv().removeUniqueKey();
1430}
1431
Brian Salomon1090da62017-01-06 12:04:19 -05001432static void test_tags(skiatest::Reporter* reporter) {
1433#ifdef SK_DEBUG
1434 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1435 static constexpr int kLastTagIdx = 10;
1436 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1437
Robert Phillipscf39f372019-09-03 10:29:20 -04001438 Mock mock(kNumResources * TestResource::kDefaultSize);
Brian Salomon1090da62017-01-06 12:04:19 -05001439 GrContext* context = mock.context();
1440 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001441 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001442
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001443 // tag strings are expected to be long lived
1444 std::vector<SkString> tagStrings;
1445
Brian Salomon1090da62017-01-06 12:04:19 -05001446 SkString tagStr;
1447 int tagIdx = 0;
1448 int currTagCnt = 0;
1449
1450 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001451
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001452 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001453 GrUniqueKey key;
1454 if (currTagCnt == tagIdx) {
1455 tagIdx += 1;
1456 currTagCnt = 0;
1457 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001458 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001459 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001460 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001461 resource->resourcePriv().setUniqueKey(key);
1462 }
1463 SkASSERT(kLastTagIdx == tagIdx);
1464 SkASSERT(currTagCnt == kLastTagIdx);
1465
1466 // Test i = 0 to exercise unused tag string.
1467 for (int i = 0; i <= kLastTagIdx; ++i) {
1468 tagStr.printf("tag%d", i);
1469 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1470 }
1471#endif
1472}
1473
Greg Danielc27eb722018-08-10 09:48:08 -04001474static void test_free_resource_messages(skiatest::Reporter* reporter) {
Robert Phillipscf39f372019-09-03 10:29:20 -04001475 Mock mock(30000);
Greg Danielc27eb722018-08-10 09:48:08 -04001476 GrContext* context = mock.context();
1477 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001478 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001479
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001480 TestResource* wrapped1 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomon876a0172019-03-08 11:12:14 -05001481 cache->insertDelayedResourceUnref(wrapped1);
Greg Danielc27eb722018-08-10 09:48:08 -04001482
1483 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1484
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001485 TestResource* wrapped2 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomon876a0172019-03-08 11:12:14 -05001486 cache->insertDelayedResourceUnref(wrapped2);
Greg Danielc27eb722018-08-10 09:48:08 -04001487
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001488 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1489 // is because inserting it as a cross-context resource actually holds a ref until the
1490 // message is received.
1491 TestResource* wrapped3 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
Brian Salomon876a0172019-03-08 11:12:14 -05001492 cache->insertDelayedResourceUnref(wrapped3);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001493
1494 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001495
1496 // Have only ref waiting on message.
1497 wrapped1->unref();
1498 wrapped2->unref();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001499 wrapped3->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001500
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001501 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001502
1503 // This should free nothing since no messages were sent.
1504 cache->purgeAsNeeded();
1505
1506 // Send message to free the first resource
Robert Phillips9da87e02019-02-04 13:26:26 -05001507 GrGpuResourceFreedMessage msg1{wrapped1, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001508 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg1);
1509 cache->purgeAsNeeded();
1510
1511 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1512
Robert Phillips9da87e02019-02-04 13:26:26 -05001513 GrGpuResourceFreedMessage msg2{wrapped3, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001514 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001515 cache->purgeAsNeeded();
1516
1517 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1518
1519 mock.reset();
1520
1521 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
1522}
1523
1524
Brian Salomondcfca432017-11-15 15:48:03 -05001525DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001526 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001527 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001528 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001529 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001530 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001531 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001532 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001533 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001534 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001535 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001536 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001537 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001538 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001539 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001540 test_partial_purge(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001541 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001542 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001543 test_tags(reporter);
Greg Danielc27eb722018-08-10 09:48:08 -04001544 test_free_resource_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001545}
1546
Robert Phillipsd6214d42016-11-07 08:23:48 -05001547////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001548static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001549 GrRenderable renderable,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001550 int width, int height,
1551 int sampleCnt) {
1552 GrSurfaceDesc desc;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001553 desc.fWidth = width;
1554 desc.fHeight = height;
1555 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon4eb38b72019-08-05 12:58:39 -04001556 auto format = provider->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Brian Salomona90382f2019-09-17 09:01:56 -04001557 return provider->createTexture(desc, format, renderable, sampleCnt, GrMipMapped::kNo,
1558 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001559}
1560
Robert Phillips0a15cc62019-07-30 12:49:10 -04001561static sk_sp<GrTextureProxy> make_mipmap_proxy(GrContext * context,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001562 GrRenderable renderable,
Robert Phillipse78b7252017-04-06 07:59:41 -04001563 int width, int height,
1564 int sampleCnt) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001565 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1566 const GrCaps* caps = context->priv().caps();
1567
Robert Phillipsd6214d42016-11-07 08:23:48 -05001568 GrSurfaceDesc desc;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001569 desc.fWidth = width;
1570 desc.fHeight = height;
1571 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001572
Robert Phillips0a15cc62019-07-30 12:49:10 -04001573 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1574 GrRenderable::kNo);
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001575 auto origin = renderable == GrRenderable::kYes ? kBottomLeft_GrSurfaceOrigin
1576 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001577
Brian Salomonbeb7f522019-08-30 16:19:42 -04001578 return proxyProvider->createProxy(format, desc, renderable, sampleCnt, origin,
1579 GrMipMapped::kYes, SkBackingFit::kExact, SkBudgeted::kYes,
1580 GrProtected::kNo);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001581}
1582
1583// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1584// Texture-only, both-RT-and-Texture and MIPmapped
1585DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1586 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001587 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -04001588 const GrCaps* caps = context->priv().caps();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001589
Robert Phillipsd6214d42016-11-07 08:23:48 -05001590 static const int kSize = 64;
1591
Robert Phillipsd6214d42016-11-07 08:23:48 -05001592 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001593 {
1594 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001595
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001596 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001597 size_t size = tex->gpuMemorySize();
1598 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1599
Greg Daniel6fa62e22019-08-07 15:52:37 -04001600 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, tex->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001601 if (sampleCount >= 4) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001602 tex = make_normal_texture(resourceProvider, GrRenderable::kYes, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001603 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001604 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001605 REPORTER_ASSERT(reporter,
1606 kSize*kSize*4 == size || // msaa4 failed
1607 kSize*kSize*4*sampleCount == size || // auto-resolving
1608 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001609 }
1610
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001611 tex = make_normal_texture(resourceProvider, GrRenderable::kNo, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001612 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001613 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001614 }
1615
Robert Phillipsd6214d42016-11-07 08:23:48 -05001616 // Mipmapped versions
Greg Daniel4065d452018-11-16 15:43:41 -05001617 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001618 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001619
Robert Phillips0a15cc62019-07-30 12:49:10 -04001620 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001621 size_t size = proxy->gpuMemorySize();
1622 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1623
Greg Daniel6fa62e22019-08-07 15:52:37 -04001624 size_t sampleCount = (size_t)caps->getRenderTargetSampleCount(4, proxy->backendFormat());
Greg Daniel81e7bf82017-07-19 14:47:42 -04001625 if (sampleCount >= 4) {
Robert Phillips0a15cc62019-07-30 12:49:10 -04001626 proxy = make_mipmap_proxy(context, GrRenderable::kYes, kSize, kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001627 size = proxy->gpuMemorySize();
1628 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001629 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1630 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1631 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001632 }
Robert Phillips1b352562017-04-05 18:56:21 +00001633
Robert Phillips0a15cc62019-07-30 12:49:10 -04001634 proxy = make_mipmap_proxy(context, GrRenderable::kNo, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001635 size = proxy->gpuMemorySize();
1636 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1637 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001638}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001639
1640#if GR_GPU_STATS
1641DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
1642 GrContext* context = ctxInfo.grContext();
Robert Phillipscf39f372019-09-03 10:29:20 -04001643 context->setResourceCacheLimit(1);
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001644
1645 // Helper that determines if cache is overbudget.
1646 auto overbudget = [context] {
1647 int uNum;
1648 size_t uSize;
1649 context->getResourceCacheUsage(&uNum, &uSize);
Robert Phillipscf39f372019-09-03 10:29:20 -04001650 size_t bSize = context->getResourceCacheLimit();
1651 return uSize > bSize;
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001652 };
1653
1654 // Helper that does a trivial draw to a surface.
1655 auto drawToSurf = [](SkSurface* surf) {
1656 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1657 };
1658
1659 // Helper that checks whether a flush has occurred between calls.
1660 int baseFlushCount = 0;
1661 auto getFlushCountDelta = [context, &baseFlushCount]() {
1662 int cur = context->priv().getGpu()->stats()->numFinishFlushes();
1663 int delta = cur - baseFlushCount;
1664 baseFlushCount = cur;
1665 return delta;
1666 };
1667
1668 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1669 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1670 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1671
1672 drawToSurf(surf1.get());
1673 drawToSurf(surf2.get());
1674
1675 // Flush each surface once to ensure that their backing stores are allocated.
1676 surf1->flush();
1677 surf2->flush();
1678 REPORTER_ASSERT(reporter, overbudget());
1679 getFlushCountDelta();
1680
1681 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1682 drawToSurf(surf1.get());
1683 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1684 drawToSurf(surf2.get());
1685 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1686 REPORTER_ASSERT(reporter, overbudget());
1687
1688 // Make surf1 purgeable. Drawing to surf2 should flush.
1689 surf1->flush();
1690 surf1.reset();
1691 drawToSurf(surf2.get());
1692 REPORTER_ASSERT(reporter, getFlushCountDelta());
1693 REPORTER_ASSERT(reporter, overbudget());
1694}
1695#endif