blob: 17b21bd72087afadc3d1d576d0393571a8bc16de [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 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040038 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080039 desc.fFlags = kRenderTarget_GrSurfaceFlag;
40 desc.fWidth = gWidth;
41 desc.fHeight = gHeight;
42 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070043 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080044 SkCanvas* canvas = surface->getCanvas();
45
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000046 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
47
48 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000049 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040051 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000052
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000053 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070054 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000055
56 int oldMaxNum;
57 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000058 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000059
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060 // Set the cache limits so we can fit 10 "src" images and the
61 // max number of textures doesn't matter
62 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000063 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000066 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000067
68 for (int i = 0; i < 100; ++i) {
69 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040070 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000071
72 // "modify" the src texture
73 src.notifyPixelsChanged();
74
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000075 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070076 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000077
78 // we should never go over the size limit
79 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
80 }
81
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000082 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000083}
84
bsalomon11abd8d2016-10-14 08:13:48 -070085static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
86 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
87 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
88 return false;
89 }
90 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
91}
92
Robert Phillipsc0192e32017-09-21 12:00:26 -040093static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
94 return rt->renderTargetPriv().getStencilAttachment();
95}
96
97static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
98 int size, int sampleCount, SkBudgeted budgeted) {
99 GrSurfaceDesc desc;
100 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc0192e32017-09-21 12:00:26 -0400101 desc.fWidth = size;
102 desc.fHeight = size;
103 desc.fConfig = kRGBA_8888_GrPixelConfig;
104 desc.fSampleCnt = sampleCount;
105
Robert Phillips9313aa72019-04-09 18:41:27 -0400106 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted,
107 GrResourceProvider::Flags::kNoPendingIO));
Robert Phillipsc0192e32017-09-21 12:00:26 -0400108 if (!tex || !tex->asRenderTarget()) {
109 return nullptr;
110 }
111
112 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
113 return nullptr;
114 }
115 SkASSERT(get_SB(tex->asRenderTarget()));
116
117 return sk_ref_sp(tex->asRenderTarget());
118}
119
bsalomon11abd8d2016-10-14 08:13:48 -0700120// This currently fails on ES3 ANGLE contexts
121DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000122 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700123 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500124 if (context->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700125 return;
126 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400127
Robert Phillips9da87e02019-02-04 13:26:26 -0500128 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400129
Brian Salomonbdecacf2018-02-02 20:32:49 -0500130 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400131 REPORTER_ASSERT(reporter, smallRT0);
132
133 {
134 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
136 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400137
Brian Salomonbdecacf2018-02-02 20:32:49 -0500138 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800139 }
140
Robert Phillipsc0192e32017-09-21 12:00:26 -0400141 {
142 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400144 REPORTER_ASSERT(reporter, smallRT2);
145
146 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147 }
148
Robert Phillipsc0192e32017-09-21 12:00:26 -0400149 {
150 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500151 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400152 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800153
Robert Phillipsc0192e32017-09-21 12:00:26 -0400154 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800155 }
bsalomon02a44a42015-02-19 09:09:00 -0800156
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400157 int smallSampleCount =
Robert Phillips9da87e02019-02-04 13:26:26 -0500158 context->priv().caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500159 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700160 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500161 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
162 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800163#ifdef SK_BUILD_FOR_ANDROID
164 if (!smallMSAART0) {
165 // The nexus player seems to fail to create MSAA textures.
166 return;
167 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168#else
169 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800170#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400171
172 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
173
174 {
175 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500176 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
177 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400178 SkBudgeted::kNo);
179 REPORTER_ASSERT(reporter, smallMSAART1);
180
181 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800182 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400183
Brian Salomonbdecacf2018-02-02 20:32:49 -0500184 // But one with a larger sample count should not. (Also check that the two requests didn't
185 // rounded up to the same actual sample count or else they could share.).
Robert Phillips9da87e02019-02-04 13:26:26 -0500186 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400187 5, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500188 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
190 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400191 SkBudgeted::kNo);
192 REPORTER_ASSERT(reporter, smallMSAART2);
193
194 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800195 }
196 }
197}
198
bsalomon68d91342016-04-12 09:59:58 -0700199DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700200 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500201 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
202 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700203 // this test is only valid for GL
204 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205 return;
206 }
207
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500208 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700209 static const int kW = 100;
210 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700211
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400212 backendTextures[0] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
213 GrMipMapped::kNo, GrRenderable::kNo);
214 backendTextures[1] = context->createBackendTexture(kW, kH, kRGBA_8888_SkColorType,
215 GrMipMapped::kNo, GrRenderable::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500216 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
217 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
218 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
219 return;
220 }
jvanverth672bb7f2015-07-13 07:19:57 -0700221
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222 context->resetContext();
223
Robert Phillips6be756b2018-01-16 15:07:54 -0500224 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500225 backendTextures[0], kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700226
Robert Phillips6be756b2018-01-16 15:07:54 -0500227 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500228 backendTextures[1], kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
Brian Osman85d34b22017-05-10 12:06:26 -0400230 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
231 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232 return;
233 }
234
halcanary96fcdcc2015-08-27 07:41:13 -0700235 borrowed.reset(nullptr);
236 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700237
238 context->flush();
239
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500240 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
241 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700242
243 REPORTER_ASSERT(reporter, borrowedIsAlive);
244 REPORTER_ASSERT(reporter, !adoptedIsAlive);
245
Brian Salomone64b0642018-03-07 11:47:54 -0500246 if (borrowedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400247 context->deleteBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500248 }
249 if (adoptedIsAlive) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400250 context->deleteBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500251 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700252
253 context->resetContext();
254}
255
bsalomon6d3fe022014-07-25 08:35:45 -0700256class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000258public:
robertphillips6e83ac72015-08-13 05:19:14 -0700259 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700260
bsalomon1c60dfe2015-01-21 09:32:40 -0800261 /** Property that distinctly categorizes the resource.
262 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800263 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800264
kkinnunen2e6055b2016-04-22 01:48:29 -0700265 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
266 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700267 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800268 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700269 , fProperty(kA_SimulatedProperty)
270 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800271 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700272 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800273 }
274
kkinnunen2e6055b2016-04-22 01:48:29 -0700275 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400276 SimulatedProperty property, size_t size = kDefaultSize) {
277 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800278 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500279 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
280 size_t size = kDefaultSize) {
281 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000282 }
283
Brian Salomond3b65972017-03-22 12:05:03 -0400284 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800285 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000286 }
287
bsalomon33435572014-11-05 14:47:41 -0800288 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400290 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
291 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000292 }
293
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
295 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
296 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800297 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
298 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800299 }
300 }
301
302 static size_t ExpectedScratchKeySize() {
303 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
304 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000305private:
bsalomon24db3b12015-01-23 04:24:04 -0800306 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800307
Greg Danielda86e282018-06-13 09:41:19 -0400308 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
309 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700310 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700311 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400312 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700313 , fProperty(property)
314 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 this->registerWithCache(budgeted);
317 }
318
319 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500320 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
321 : INHERITED(gpu)
322 , fToDelete(nullptr)
323 , fSize(size)
324 , fProperty(kA_SimulatedProperty)
325 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700326 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500327 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700328 }
329
330 void computeScratchKey(GrScratchKey* key) const override {
331 if (fIsScratch) {
332 ComputeScratchKey(fProperty, key);
333 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800334 }
335
mtklein36352bf2015-03-25 18:17:31 -0700336 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400337 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800338
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400339 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000340 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800341 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800342 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700343 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700344 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000345};
bsalomon33435572014-11-05 14:47:41 -0800346int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000347
bsalomonc2f35b72015-01-23 07:19:22 -0800348class Mock {
349public:
350 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400351 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800352 SkASSERT(fContext);
353 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500354 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800355 cache->purgeAllUnlocked();
356 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800357 }
bsalomonc2f35b72015-01-23 07:19:22 -0800358
Robert Phillips9da87e02019-02-04 13:26:26 -0500359 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
Hal Canary342b7ac2016-11-04 11:49:42 -0400361 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800362
Greg Danielc27eb722018-08-10 09:48:08 -0400363 void reset() {
364 fContext.reset();
365 }
366
bsalomonc2f35b72015-01-23 07:19:22 -0800367private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400368 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800369};
370
371static void test_no_key(skiatest::Reporter* reporter) {
372 Mock mock(10, 30000);
373 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800374 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500375 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400378 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
379 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
380 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
381 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800384 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800385 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800386 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800387
388 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
391
bsalomon8718aaf2015-02-19 07:24:21 -0800392 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800393
394 a->unref();
395 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800396 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800397 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800398 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399
400 c->unref();
401 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800403 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800404 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800405
406 d->unref();
407 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800408 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
409 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 b->unref();
412 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800413 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
414 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415}
416
bsalomon24db3b12015-01-23 04:24:04 -0800417// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500418template <int>
419static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800420 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500421 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800422 builder[0] = data;
423}
424
Robert Phillips6eba0632018-03-28 12:25:42 -0400425static void test_purge_unlocked(skiatest::Reporter* reporter) {
426 Mock mock(10, 30000);
427 GrContext* context = mock.context();
428 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500429 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400430
431 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
432 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400433 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400434
435 GrUniqueKey uniqueKey;
436 make_unique_key<0>(&uniqueKey, 0);
437
438 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400439 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400440 b->resourcePriv().setUniqueKey(uniqueKey);
441
442 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400443 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400444
445 GrUniqueKey uniqueKey2;
446 make_unique_key<0>(&uniqueKey2, 1);
447
448 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400449 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400450 d->resourcePriv().setUniqueKey(uniqueKey2);
451
452
453 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
454 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
455 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
456 d->gpuMemorySize() == cache->getResourceBytes());
457
458 // Should be safe to purge without deleting the resources since we still have refs.
459 cache->purgeUnlockedResources(false);
460 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
461
462 // Unref them all. Since they all have keys they should remain in the cache.
463
464 a->unref();
465 b->unref();
466 c->unref();
467 d->unref();
468 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
469 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
470 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
471 d->gpuMemorySize() == cache->getResourceBytes());
472
473 // Purge only the two scratch resources
474 cache->purgeUnlockedResources(true);
475
476 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
477 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
478 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
479 cache->getResourceBytes());
480
481 // Purge the uniquely keyed resources
482 cache->purgeUnlockedResources(false);
483
484 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
485 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
486 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
487}
488
bsalomon84c8e622014-11-17 09:33:27 -0800489static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800490 Mock mock(10, 300);
491 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800492 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500493 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800494
bsalomon8718aaf2015-02-19 07:24:21 -0800495 GrUniqueKey uniqueKey;
496 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800497
bsalomon8718aaf2015-02-19 07:24:21 -0800498 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800499 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400500 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
501 10);
502 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800503 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500504 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
505 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
506 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800507
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500508 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800509 GrUniqueKey uniqueKey2;
510 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500511 GrUniqueKey uniqueKey3;
512 make_unique_key<0>(&uniqueKey3, 2);
513 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
514 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
515 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
516 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
517 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
518 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400519
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500520 // Remove the extra refs we just added.
521 SkSafeUnref(wrappedCacheableViaKey);
522 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800523
524 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500525 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800526 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500527 wrappedCacheable->gpuMemorySize() +
528 wrappedUncacheable->gpuMemorySize() +
529 unbudgeted->gpuMemorySize() ==
530 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800531 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800532 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800533 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400534 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800535
bsalomon63c992f2015-01-23 12:47:59 -0800536 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800537 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500538 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800539 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500540 wrappedCacheable->gpuMemorySize() +
541 wrappedUncacheable->gpuMemorySize() +
542 unbudgeted->gpuMemorySize() ==
543 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800544 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800545 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800546 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400547 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800548
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500549 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
550 // However, unreffing the uncacheable wrapped resource should free it.
551 wrappedCacheable->unref();
552 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400553 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800554 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500555 wrappedCacheable->gpuMemorySize() +
556 unbudgeted->gpuMemorySize() ==
557 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500558 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800559
bsalomon84c8e622014-11-17 09:33:27 -0800560 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500561 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800562 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500563 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
564 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
565 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800566 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500567 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500568
569 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
570 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
571 if (wrappedCacheableViaKey) {
572 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
573 wrappedCacheable->unref();
574 }
575 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
576 // resource should immediately delete it.
577 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
578
579 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500580 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
581 wrappedUncacheable->gpuMemorySize() +
582 unbudgeted->gpuMemorySize() ==
583 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800584 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400586 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800587
588 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400589 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800590 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500591 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
592 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
593 wrappedUncacheable->gpuMemorySize() ==
594 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800595 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
596 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400597 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800598
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500599 // Unreffing the wrapped resources (with no unique key) should free them right away.
600 wrappedUncacheable->unref();
601 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800602 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
603 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
604 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
605 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400606 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800607
608 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800609 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
610 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
611 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
612 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400613 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800614}
615
bsalomon5236cf42015-01-14 10:42:08 -0800616static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800617 Mock mock(10, 30000);
618 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800619 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500620 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800621
bsalomon8718aaf2015-02-19 07:24:21 -0800622 GrUniqueKey uniqueKey;
623 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800624
625 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800626 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800627 TestResource* wrapped;
628 TestResource* unbudgeted;
629
630 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500631 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400632 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700633
bsalomon5236cf42015-01-14 10:42:08 -0800634 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
636 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
637 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
638 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400639 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800640
Greg Danielda86e282018-06-13 09:41:19 -0400641 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800642 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800643 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800644 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
645 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
646 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
647 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400648 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800649
bsalomon0ea80f42015-02-11 10:49:59 -0800650 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500651 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800652 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
653 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
654 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
655 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400656 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800657
658 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800659 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
660 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
661 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
662 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400663 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800664
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500665 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800666 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
667 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
668 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
669 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400670 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800671
672 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800673 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
674 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
675 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
676 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400677 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800678
bsalomon0ea80f42015-02-11 10:49:59 -0800679 cache->purgeAllUnlocked();
680 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
681 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
682 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
683 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400684 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800685}
686
bsalomon3582d3e2015-02-13 14:20:05 -0800687// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
688void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
689/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800690 Mock mock(10, 300);
691 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800692 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500693 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800694
695 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500696 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800697 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800698 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800699
700 size_t size = resource->gpuMemorySize();
701 for (int i = 0; i < 2; ++i) {
702 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800703 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800704 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500705 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500706 resource->resourcePriv().budgetedType());
Chris Daltond004e0b2018-09-27 09:28:03 -0600707 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomon0ea80f42015-02-11 10:49:59 -0800708 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
709 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
710 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
711 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400712 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800713
714 // Once it is unrefed, it should become available as scratch.
715 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800716 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
717 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
718 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
719 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400720 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Chris Daltond004e0b2018-09-27 09:28:03 -0600721 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomonc2f35b72015-01-23 07:19:22 -0800722 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800723 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800724 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500725 REPORTER_ASSERT(reporter,
726 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800727
728 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700729 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800730 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800731 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800732 } else {
733 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800734 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800735 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
736 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
737 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
738 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400739 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800740 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800741 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500742 REPORTER_ASSERT(reporter,
743 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800744
745 // now when it is unrefed it should die since it has no key.
746 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800747 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
748 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
749 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
750 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400751 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800752 }
bsalomon8b79d232014-11-10 10:19:06 -0800753 }
bsalomonc2f35b72015-01-23 07:19:22 -0800754}
755
756static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
757 Mock mock(5, 30000);
758 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800759 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500760 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800761
bsalomon8b79d232014-11-10 10:19:06 -0800762 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500763 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700764 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400765 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500766 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700767 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400768 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800769 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800770 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800771 // Check for negative case consistency. (leaks upon test failure.)
Chris Daltond004e0b2018-09-27 09:28:03 -0600772 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomon1c60dfe2015-01-21 09:32:40 -0800773
774 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800775 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800776
bsalomon0ea80f42015-02-11 10:49:59 -0800777 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800778 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800779 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
780 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800781 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800782 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800783
bsalomon63c992f2015-01-23 12:47:59 -0800784 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800785 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800786 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800787 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800788
789 // Unref but don't purge
790 a->unref();
791 b->unref();
792 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800793 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800794
bsalomon63c992f2015-01-23 12:47:59 -0800795 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800796 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800797 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800798 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
799 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800800}
801
bsalomon10e23ca2014-11-25 05:52:06 -0800802static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800803 Mock mock(5, 30000);
804 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800805 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500806 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800807
bsalomon10e23ca2014-11-25 05:52:06 -0800808 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500809 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800810 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500811 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800812 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800813 a->unref();
814 b->unref();
815
bsalomon1c60dfe2015-01-21 09:32:40 -0800816 GrScratchKey scratchKey;
817 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800818 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800819 // (following leaks upon test failure).
Chris Daltond004e0b2018-09-27 09:28:03 -0600820 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800821
bsalomon0ea80f42015-02-11 10:49:59 -0800822 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800823 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800824 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800825 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
826 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800827
828 // Find the first resource and remove its scratch key
829 GrGpuResource* find;
Chris Daltond004e0b2018-09-27 09:28:03 -0600830 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon3582d3e2015-02-13 14:20:05 -0800831 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800832 // It's still alive, but not cached by scratch key anymore
833 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800834 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
835 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800836
837 // The cache should immediately delete it when it's unrefed since it isn't accessible.
838 find->unref();
839 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800840 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
841 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800842
843 // Repeat for the second resource.
Chris Daltond004e0b2018-09-27 09:28:03 -0600844 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon3582d3e2015-02-13 14:20:05 -0800845 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800846 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800847 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
848 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800849
850 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800851 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800852 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800853 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
854 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800855
856 find->unref();
857 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800858 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
859 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800860}
861
bsalomon1c60dfe2015-01-21 09:32:40 -0800862static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800863 Mock mock(5, 30000);
864 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800865 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500866 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800867
868 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500869 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800870 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500871 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800872 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800873 a->unref();
874 b->unref();
875
876 GrScratchKey scratchKey;
877 // Ensure that scratch key comparison and assignment is consistent.
878 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800879 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800880 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800881 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800882 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
883 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
884 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
885 scratchKey = scratchKey1;
886 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
887 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
888 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
889 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
890 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
891 scratchKey = scratchKey2;
892 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
893 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
894 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
895 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
896 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
897
898 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800899 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800900 // (following leaks upon test failure).
Chris Daltond004e0b2018-09-27 09:28:03 -0600901 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800902
903 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800904 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Chris Daltond004e0b2018-09-27 09:28:03 -0600905 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700906 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800907 find->unref();
908
909 scratchKey2 = scratchKey;
Chris Daltond004e0b2018-09-27 09:28:03 -0600910 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700911 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800912 REPORTER_ASSERT(reporter, find == a || find == b);
913
Chris Daltond004e0b2018-09-27 09:28:03 -0600914 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700915 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800916 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
917 REPORTER_ASSERT(reporter, find2 != find);
918 find2->unref();
919 find->unref();
920}
921
bsalomon8718aaf2015-02-19 07:24:21 -0800922static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800923 Mock mock(5, 30000);
924 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800925 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500926 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000927
bsalomon8718aaf2015-02-19 07:24:21 -0800928 GrUniqueKey key;
929 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700930
bsalomon8718aaf2015-02-19 07:24:21 -0800931 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400932 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700933
bsalomonf99e9612015-02-19 08:24:16 -0800934 // Set key on resource a.
935 a->resourcePriv().setUniqueKey(key);
936 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
937 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800938
bsalomonf99e9612015-02-19 08:24:16 -0800939 // Make sure that redundantly setting a's key works.
940 a->resourcePriv().setUniqueKey(key);
941 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800942 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800943 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
944 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800945 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
946
bsalomonf99e9612015-02-19 08:24:16 -0800947 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400948 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800949 b->resourcePriv().setUniqueKey(key);
950 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
951 b->unref();
952
953 // Still have two resources because a is still reffed.
954 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
955 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
956 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
957
958 a->unref();
959 // Now a should be gone.
960 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
961 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
962 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
963
964 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
965 // Also make b be unreffed when replacement occurs.
966 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400967 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800968 GrUniqueKey differentKey;
969 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800970 c->resourcePriv().setUniqueKey(differentKey);
971 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
972 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
973 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
974 // c replaces b and b should be immediately purged.
975 c->resourcePriv().setUniqueKey(key);
976 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
977 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
978 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
979
980 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800981 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800982 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
983 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
984 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
985
986 // Drop the ref on c, it should be kept alive because it has a unique key.
987 c->unref();
988 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
989 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
990 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
991
992 // Verify that we can find c, then remove its unique key. It should get purged immediately.
993 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
994 c->resourcePriv().removeUniqueKey();
995 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800996 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
997 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800998 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700999
1000 {
1001 GrUniqueKey key2;
1002 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001003 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -07001004 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001005 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001006 d->resourcePriv().setUniqueKey(key2);
1007 }
1008
1009 GrUniqueKey key3;
1010 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001011 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001012 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001013}
1014
bsalomon8b79d232014-11-10 10:19:06 -08001015static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001016 Mock mock(5, 30000);
1017 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001018 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001019 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001020
bsalomon8718aaf2015-02-19 07:24:21 -08001021 GrUniqueKey key1, key2, key3;
1022 make_unique_key<0>(&key1, 1);
1023 make_unique_key<0>(&key2, 2);
1024 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001025
bsalomon23e619c2015-02-06 11:54:28 -08001026 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001027 TestResource* a = new TestResource(gpu);
1028 TestResource* b = new TestResource(gpu);
1029 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001030 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001031 a->resourcePriv().setUniqueKey(key1);
1032 b->resourcePriv().setUniqueKey(key2);
1033 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001034 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001035 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001036 c->unref();
1037
bsalomon8718aaf2015-02-19 07:24:21 -08001038 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1039 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1040 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001041 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001042
bsalomon8718aaf2015-02-19 07:24:21 -08001043 typedef GrUniqueKeyInvalidatedMessage Msg;
1044 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001045
1046 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001047 Bus::Post(Msg(key1, context->priv().contextID()));
1048 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001049 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001050 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001051 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1052 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001053 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001054 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001055
1056 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001057 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001058 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001059 // we still have a ref on b, c should be recycled as scratch.
1060 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001061 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001062
bsalomon23e619c2015-02-06 11:54:28 -08001063 // make b purgeable. It should be immediately deleted since it has no key.
1064 b->unref();
1065 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1066
1067 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1068 GrScratchKey scratchKey;
1069 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Chris Daltond004e0b2018-09-27 09:28:03 -06001070 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon23e619c2015-02-06 11:54:28 -08001071 REPORTER_ASSERT(reporter, scratch == c);
1072 SkSafeUnref(scratch);
1073
1074 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001075 cache->purgeAllUnlocked();
Chris Daltond004e0b2018-09-27 09:28:03 -06001076 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon71cb0c22014-11-14 12:10:14 -08001077 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001078 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1079 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001080 REPORTER_ASSERT(reporter, !scratch);
1081 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001082}
1083
bsalomon71cb0c22014-11-14 12:10:14 -08001084static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001085 Mock mock(3, 30000);
1086 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001087 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001088 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001089
bsalomon8718aaf2015-02-19 07:24:21 -08001090 GrUniqueKey key1, key2;
1091 make_unique_key<0>(&key1, 1);
1092 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001093
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001094 sk_sp<TestResource> a(new TestResource(gpu));
1095 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001096 a->resourcePriv().setUniqueKey(key1);
1097 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001098
bsalomonc2f35b72015-01-23 07:19:22 -08001099 // Make a cycle
1100 a->setUnrefWhenDestroyed(b);
1101 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001102
bsalomonc2f35b72015-01-23 07:19:22 -08001103 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001104
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001105 TestResource* unownedA = a.release();
1106 unownedA->unref();
1107 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001108
bsalomonc2f35b72015-01-23 07:19:22 -08001109 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001110
bsalomon0ea80f42015-02-11 10:49:59 -08001111 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001112 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001113
bsalomonc2f35b72015-01-23 07:19:22 -08001114 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001115 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001116 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001117
bsalomon0ea80f42015-02-11 10:49:59 -08001118 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001119 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001120}
1121
bsalomonddf30e62015-02-19 11:38:44 -08001122static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1123 static const int kCount = 50;
1124 static const int kBudgetCnt = kCount / 2;
1125 static const int kLockedFreq = 8;
1126 static const int kBudgetSize = 0x80000000;
1127
1128 SkRandom random;
1129
1130 // Run the test 2*kCount times;
1131 for (int i = 0; i < 2 * kCount; ++i ) {
1132 Mock mock(kBudgetCnt, kBudgetSize);
1133 GrContext* context = mock.context();
1134 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001135 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001136
1137 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001138 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001139
1140 static const int kNumToPurge = kCount - kBudgetCnt;
1141
1142 SkTDArray<int> shouldPurgeIdxs;
1143 int purgeableCnt = 0;
1144 SkTDArray<GrGpuResource*> resourcesToUnref;
1145
1146 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1147 // unpurgeable resources.
1148 for (int j = 0; j < kCount; ++j) {
1149 GrUniqueKey key;
1150 make_unique_key<0>(&key, j);
1151
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001152 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001153 r->resourcePriv().setUniqueKey(key);
1154 if (random.nextU() % kLockedFreq) {
1155 // Make this is purgeable.
1156 r->unref();
1157 ++purgeableCnt;
1158 if (purgeableCnt <= kNumToPurge) {
1159 *shouldPurgeIdxs.append() = j;
1160 }
1161 } else {
1162 *resourcesToUnref.append() = r;
1163 }
1164 }
1165
1166 // Verify that the correct resources were purged.
1167 int currShouldPurgeIdx = 0;
1168 for (int j = 0; j < kCount; ++j) {
1169 GrUniqueKey key;
1170 make_unique_key<0>(&key, j);
1171 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1172 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1173 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1174 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001175 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001176 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001177 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001178 }
1179 SkSafeUnref(res);
1180 }
1181
1182 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1183 resourcesToUnref[j]->unref();
1184 }
1185 }
1186}
1187
Brian Salomon5e150852017-03-22 14:53:13 -04001188static void test_time_purge(skiatest::Reporter* reporter) {
1189 Mock mock(1000000, 1000000);
1190 GrContext* context = mock.context();
1191 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001192 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001193
1194 static constexpr int kCnts[] = {1, 10, 1024};
1195 auto nowish = []() {
1196 // We sleep so that we ensure we get a value that is greater than the last call to
1197 // GrStdSteadyClock::now().
1198 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1199 auto result = GrStdSteadyClock::now();
1200 // Also sleep afterwards so we don't get this value again.
1201 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1202 return result;
1203 };
1204
1205 for (int cnt : kCnts) {
1206 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1207 new GrStdSteadyClock::time_point[cnt]);
1208 {
1209 // Insert resources and get time points between each addition.
1210 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001211 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001212 GrUniqueKey k;
1213 make_unique_key<1>(&k, i);
1214 r->resourcePriv().setUniqueKey(k);
1215 r->unref();
1216 timeStamps.get()[i] = nowish();
1217 }
1218
1219 // Purge based on the time points between resource additions. Each purge should remove
1220 // the oldest resource.
1221 for (int i = 0; i < cnt; ++i) {
1222 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1223 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1224 for (int j = 0; j < i; ++j) {
1225 GrUniqueKey k;
1226 make_unique_key<1>(&k, j);
1227 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1228 REPORTER_ASSERT(reporter, !SkToBool(r));
1229 SkSafeUnref(r);
1230 }
1231 }
1232
1233 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1234 cache->purgeAllUnlocked();
1235 }
1236
1237 // Do a similar test but where we leave refs on some resources to prevent them from being
1238 // purged.
1239 {
1240 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1241 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001242 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001243 GrUniqueKey k;
1244 make_unique_key<1>(&k, i);
1245 r->resourcePriv().setUniqueKey(k);
1246 // Leave a ref on every other resource, beginning with the first.
1247 if (SkToBool(i & 0x1)) {
1248 refedResources.get()[i / 2] = r;
1249 } else {
1250 r->unref();
1251 }
1252 timeStamps.get()[i] = nowish();
1253 }
1254
1255 for (int i = 0; i < cnt; ++i) {
1256 // Should get a resource purged every other frame.
1257 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1258 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1259 }
1260
1261 // Unref all the resources that we kept refs on in the first loop.
1262 for (int i = 0; i < (cnt / 2); ++i) {
1263 refedResources.get()[i]->unref();
1264 cache->purgeResourcesNotUsedSince(nowish());
1265 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1266 }
1267
1268 cache->purgeAllUnlocked();
1269 }
1270
1271 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1272
1273 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1274 // eviction
1275 context->flush();
1276 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001277 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001278 GrUniqueKey k;
1279 make_unique_key<1>(&k, i);
1280 r->resourcePriv().setUniqueKey(k);
1281 r->unref();
1282 }
1283 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1284 context->flush();
1285 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1286 cache->purgeResourcesNotUsedSince(nowish());
1287 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1288 }
1289}
1290
Derek Sollenberger5480a182017-05-25 16:43:59 -04001291static void test_partial_purge(skiatest::Reporter* reporter) {
1292 Mock mock(6, 100);
1293 GrContext* context = mock.context();
1294 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001295 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001296
1297 enum TestsCase {
1298 kOnlyScratch_TestCase = 0,
1299 kPartialScratch_TestCase = 1,
1300 kAllScratch_TestCase = 2,
1301 kPartial_TestCase = 3,
1302 kAll_TestCase = 4,
1303 kNone_TestCase = 5,
1304 kEndTests_TestCase = kNone_TestCase + 1
1305 };
1306
1307 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1308
1309 GrUniqueKey key1, key2, key3;
1310 make_unique_key<0>(&key1, 1);
1311 make_unique_key<0>(&key2, 2);
1312 make_unique_key<0>(&key3, 3);
1313
1314 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001315 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1316 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1317 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001318
1319 unique1->resourcePriv().setUniqueKey(key1);
1320 unique2->resourcePriv().setUniqueKey(key2);
1321 unique3->resourcePriv().setUniqueKey(key3);
1322
Derek Sollenberger5480a182017-05-25 16:43:59 -04001323 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001324 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001325 TestResource::kA_SimulatedProperty,
1326 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001327 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001328 TestResource::kB_SimulatedProperty,
1329 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001330
1331 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1332 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1333 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1334
1335 // Add resources to the purgeable queue
1336 unique1->unref();
1337 scratch1->unref();
1338 unique2->unref();
1339 scratch2->unref();
1340 unique3->unref();
1341
1342 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1343 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1344 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1345
1346 switch(testCase) {
1347 case kOnlyScratch_TestCase: {
1348 context->purgeUnlockedResources(14, true);
1349 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1350 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1351 break;
1352 }
1353 case kPartialScratch_TestCase: {
1354 context->purgeUnlockedResources(3, true);
1355 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1356 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1357 break;
1358 }
1359 case kAllScratch_TestCase: {
1360 context->purgeUnlockedResources(50, true);
1361 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1362 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1363 break;
1364 }
1365 case kPartial_TestCase: {
1366 context->purgeUnlockedResources(13, false);
1367 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1368 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1369 break;
1370 }
1371 case kAll_TestCase: {
1372 context->purgeUnlockedResources(50, false);
1373 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1374 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1375 break;
1376 }
1377 case kNone_TestCase: {
1378 context->purgeUnlockedResources(0, true);
1379 context->purgeUnlockedResources(0, false);
1380 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1381 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1382 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1383 break;
1384 }
Brian Salomon23356442018-11-30 15:33:19 -05001385 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001386
1387 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001388 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001389 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1390 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1391
1392 }
1393}
1394
bsalomon10e23ca2014-11-25 05:52:06 -08001395static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001396 // Set the cache size to double the resource count because we're going to create 2x that number
1397 // resources, using two different key domains. Add a little slop to the bytes because we resize
1398 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001399 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001400
bsalomonc2f35b72015-01-23 07:19:22 -08001401 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1402 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001403 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001404 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001405
1406 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001407 GrUniqueKey key1, key2;
1408 make_unique_key<1>(&key1, i);
1409 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001410
bsalomon24db3b12015-01-23 04:24:04 -08001411 TestResource* resource;
1412
Greg Danielda86e282018-06-13 09:41:19 -04001413 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001414 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001415 resource->unref();
1416
Greg Danielda86e282018-06-13 09:41:19 -04001417 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001418 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001419 resource->unref();
1420 }
1421
1422 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001423 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001424 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1425 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1426 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1427 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001428 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001429 GrUniqueKey key1, key2;
1430 make_unique_key<1>(&key1, i);
1431 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001432
bsalomon8718aaf2015-02-19 07:24:21 -08001433 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1434 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001435 }
1436
bsalomon0ea80f42015-02-11 10:49:59 -08001437 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001438 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001439 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001440 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1441 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1442 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1443 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001444
1445 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001446 GrUniqueKey key1, key2;
1447 make_unique_key<1>(&key1, i);
1448 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001449
bsalomon8718aaf2015-02-19 07:24:21 -08001450 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1451 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001452 }
1453}
1454
senorblanco84cd6212015-08-04 10:01:58 -07001455static void test_custom_data(skiatest::Reporter* reporter) {
1456 GrUniqueKey key1, key2;
1457 make_unique_key<0>(&key1, 1);
1458 make_unique_key<0>(&key2, 2);
1459 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001460 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001461 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1462 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1463
1464 // Test that copying a key also takes a ref on its custom data.
1465 GrUniqueKey key3 = key1;
1466 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1467}
1468
bsalomonc6363ef2015-09-24 07:07:40 -07001469static void test_abandoned(skiatest::Reporter* reporter) {
1470 Mock mock(10, 300);
1471 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001472 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001473
1474 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001475 context->abandonContext();
1476
1477 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1478
1479 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1480
robertphillips8abb3702016-08-31 14:04:06 -07001481 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001482 resource->getUniqueKey();
1483 resource->wasDestroyed();
1484 resource->gpuMemorySize();
1485 resource->getContext();
1486
bsalomonc6363ef2015-09-24 07:07:40 -07001487 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001488 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001489 resource->resourcePriv().makeBudgeted();
1490 resource->resourcePriv().makeUnbudgeted();
1491 resource->resourcePriv().removeScratchKey();
1492 GrUniqueKey key;
1493 make_unique_key<0>(&key, 1);
1494 resource->resourcePriv().setUniqueKey(key);
1495 resource->resourcePriv().removeUniqueKey();
1496}
1497
Brian Salomon1090da62017-01-06 12:04:19 -05001498static void test_tags(skiatest::Reporter* reporter) {
1499#ifdef SK_DEBUG
1500 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1501 static constexpr int kLastTagIdx = 10;
1502 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1503
1504 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1505 GrContext* context = mock.context();
1506 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001507 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001508
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001509 // tag strings are expected to be long lived
1510 std::vector<SkString> tagStrings;
1511
Brian Salomon1090da62017-01-06 12:04:19 -05001512 SkString tagStr;
1513 int tagIdx = 0;
1514 int currTagCnt = 0;
1515
1516 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001517
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001518 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001519 GrUniqueKey key;
1520 if (currTagCnt == tagIdx) {
1521 tagIdx += 1;
1522 currTagCnt = 0;
1523 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001524 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001525 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001526 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001527 resource->resourcePriv().setUniqueKey(key);
1528 }
1529 SkASSERT(kLastTagIdx == tagIdx);
1530 SkASSERT(currTagCnt == kLastTagIdx);
1531
1532 // Test i = 0 to exercise unused tag string.
1533 for (int i = 0; i <= kLastTagIdx; ++i) {
1534 tagStr.printf("tag%d", i);
1535 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1536 }
1537#endif
1538}
1539
Greg Danielc27eb722018-08-10 09:48:08 -04001540static void test_free_resource_messages(skiatest::Reporter* reporter) {
1541 Mock mock(10, 30000);
1542 GrContext* context = mock.context();
1543 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001544 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001545
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001546 TestResource* wrapped1 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomon876a0172019-03-08 11:12:14 -05001547 cache->insertDelayedResourceUnref(wrapped1);
Greg Danielc27eb722018-08-10 09:48:08 -04001548
1549 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1550
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001551 TestResource* wrapped2 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomon876a0172019-03-08 11:12:14 -05001552 cache->insertDelayedResourceUnref(wrapped2);
Greg Danielc27eb722018-08-10 09:48:08 -04001553
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001554 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1555 // is because inserting it as a cross-context resource actually holds a ref until the
1556 // message is received.
1557 TestResource* wrapped3 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
Brian Salomon876a0172019-03-08 11:12:14 -05001558 cache->insertDelayedResourceUnref(wrapped3);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001559
1560 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001561
1562 // Have only ref waiting on message.
1563 wrapped1->unref();
1564 wrapped2->unref();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001565 wrapped3->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001566
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001567 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001568
1569 // This should free nothing since no messages were sent.
1570 cache->purgeAsNeeded();
1571
1572 // Send message to free the first resource
Robert Phillips9da87e02019-02-04 13:26:26 -05001573 GrGpuResourceFreedMessage msg1{wrapped1, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001574 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg1);
1575 cache->purgeAsNeeded();
1576
1577 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1578
Robert Phillips9da87e02019-02-04 13:26:26 -05001579 GrGpuResourceFreedMessage msg2{wrapped3, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001580 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001581 cache->purgeAsNeeded();
1582
1583 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1584
1585 mock.reset();
1586
1587 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
1588}
1589
1590
Brian Salomondcfca432017-11-15 15:48:03 -05001591DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001592 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001593 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001594 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001595 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001596 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001597 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001598 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001599 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001600 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001601 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001602 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001603 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001604 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001605 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001606 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001607 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001608 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001609 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001610 test_tags(reporter);
Greg Danielc27eb722018-08-10 09:48:08 -04001611 test_free_resource_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001612}
1613
Robert Phillipsd6214d42016-11-07 08:23:48 -05001614////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001615static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001616 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001617 int width, int height,
1618 int sampleCnt) {
1619 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001620 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001621 desc.fWidth = width;
1622 desc.fHeight = height;
1623 desc.fConfig = kRGBA_8888_GrPixelConfig;
1624 desc.fSampleCnt = sampleCnt;
1625
Robert Phillips9313aa72019-04-09 18:41:27 -04001626 return provider->createTexture(desc, SkBudgeted::kYes,
1627 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628}
1629
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001630static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -05001631 const GrCaps* caps,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001632 GrSurfaceDescFlags descFlags,
Robert Phillipse78b7252017-04-06 07:59:41 -04001633 int width, int height,
1634 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001635 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001636 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001637 desc.fWidth = width;
1638 desc.fHeight = height;
1639 desc.fConfig = kRGBA_8888_GrPixelConfig;
1640 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001641
Greg Daniel4065d452018-11-16 15:43:41 -05001642 const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001643 auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1644 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001645
Greg Daniel4065d452018-11-16 15:43:41 -05001646 return proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001647}
1648
1649// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1650// Texture-only, both-RT-and-Texture and MIPmapped
1651DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1652 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001653 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1654 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001655
Robert Phillipsd6214d42016-11-07 08:23:48 -05001656 static const int kSize = 64;
1657
Robert Phillipsd6214d42016-11-07 08:23:48 -05001658 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001659 {
1660 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001661
Brian Salomonbdecacf2018-02-02 20:32:49 -05001662 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001663 size_t size = tex->gpuMemorySize();
1664 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1665
Robert Phillips9da87e02019-02-04 13:26:26 -05001666 size_t sampleCount = (size_t)context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001667 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001668 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001669 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001670 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001671 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001672 REPORTER_ASSERT(reporter,
1673 kSize*kSize*4 == size || // msaa4 failed
1674 kSize*kSize*4*sampleCount == size || // auto-resolving
1675 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001676 }
1677
Brian Salomonbdecacf2018-02-02 20:32:49 -05001678 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001679 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001680 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001681 }
1682
Robert Phillipsd6214d42016-11-07 08:23:48 -05001683
1684 // Mipmapped versions
Robert Phillips9da87e02019-02-04 13:26:26 -05001685 const GrCaps* caps = context->priv().caps();
Greg Daniel4065d452018-11-16 15:43:41 -05001686 if (caps->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001687 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001688
Greg Daniel4065d452018-11-16 15:43:41 -05001689 proxy = make_mipmap_proxy(proxyProvider, caps, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1690 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001691 size_t size = proxy->gpuMemorySize();
1692 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1693
Robert Phillips9da87e02019-02-04 13:26:26 -05001694 size_t sampleCount = (size_t)context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001695 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001696 if (sampleCount >= 4) {
Greg Daniel4065d452018-11-16 15:43:41 -05001697 proxy = make_mipmap_proxy(proxyProvider, caps, kRenderTarget_GrSurfaceFlag, kSize,
1698 kSize, sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001699 size = proxy->gpuMemorySize();
1700 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001701 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1702 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1703 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001704 }
Robert Phillips1b352562017-04-05 18:56:21 +00001705
Greg Daniel4065d452018-11-16 15:43:41 -05001706 proxy = make_mipmap_proxy(proxyProvider, caps, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001707 size = proxy->gpuMemorySize();
1708 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1709 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001710}
Brian Salomon8cefa3e2019-04-04 11:39:55 -04001711
1712#if GR_GPU_STATS
1713DEF_GPUTEST_FOR_MOCK_CONTEXT(OverbudgetFlush, reporter, ctxInfo) {
1714 GrContext* context = ctxInfo.grContext();
1715 context->setResourceCacheLimits(1, 1);
1716
1717 // Helper that determines if cache is overbudget.
1718 auto overbudget = [context] {
1719 int uNum;
1720 size_t uSize;
1721 context->getResourceCacheUsage(&uNum, &uSize);
1722 int bNum;
1723 size_t bSize;
1724 context->getResourceCacheLimits(&bNum, &bSize);
1725 return uNum > bNum || uSize > bSize;
1726 };
1727
1728 // Helper that does a trivial draw to a surface.
1729 auto drawToSurf = [](SkSurface* surf) {
1730 surf->getCanvas()->drawRect(SkRect::MakeWH(1,1), SkPaint());
1731 };
1732
1733 // Helper that checks whether a flush has occurred between calls.
1734 int baseFlushCount = 0;
1735 auto getFlushCountDelta = [context, &baseFlushCount]() {
1736 int cur = context->priv().getGpu()->stats()->numFinishFlushes();
1737 int delta = cur - baseFlushCount;
1738 baseFlushCount = cur;
1739 return delta;
1740 };
1741
1742 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1743 auto surf1 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1744 auto surf2 = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
1745
1746 drawToSurf(surf1.get());
1747 drawToSurf(surf2.get());
1748
1749 // Flush each surface once to ensure that their backing stores are allocated.
1750 surf1->flush();
1751 surf2->flush();
1752 REPORTER_ASSERT(reporter, overbudget());
1753 getFlushCountDelta();
1754
1755 // Nothing should be purgeable so drawing to either surface doesn't cause a flush.
1756 drawToSurf(surf1.get());
1757 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1758 drawToSurf(surf2.get());
1759 REPORTER_ASSERT(reporter, !getFlushCountDelta());
1760 REPORTER_ASSERT(reporter, overbudget());
1761
1762 // Make surf1 purgeable. Drawing to surf2 should flush.
1763 surf1->flush();
1764 surf1.reset();
1765 drawToSurf(surf2.get());
1766 REPORTER_ASSERT(reporter, getFlushCountDelta());
1767 REPORTER_ASSERT(reporter, overbudget());
1768}
1769#endif