blob: 9bd5415cf55613ad12b18920cda26794b3937f11 [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
bsalomon3f324322015-04-08 11:01:54 -07008#include "SkTypes.h"
9
bsalomonbcf0a522014-10-08 08:40:09 -070010#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000012#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourceCacheAccess.h"
15#include "GrGpuResourcePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040017#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080018#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070019#include "GrResourceProvider.h"
Robert Phillips646e4292017-06-13 12:44:56 -040020#include "GrTexture.h"
21
bsalomonbcf0a522014-10-08 08:40:09 -070022#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080023#include "SkGr.h"
24#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050025#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070026#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000027#include "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
106 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
107 if (!tex || !tex->asRenderTarget()) {
108 return nullptr;
109 }
110
111 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
112 return nullptr;
113 }
114 SkASSERT(get_SB(tex->asRenderTarget()));
115
116 return sk_ref_sp(tex->asRenderTarget());
117}
118
bsalomon11abd8d2016-10-14 08:13:48 -0700119// This currently fails on ES3 ANGLE contexts
120DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000121 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700122 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500123 if (context->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700124 return;
125 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400126
Robert Phillips9da87e02019-02-04 13:26:26 -0500127 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400130 REPORTER_ASSERT(reporter, smallRT0);
131
132 {
133 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500134 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
135 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400136
Brian Salomonbdecacf2018-02-02 20:32:49 -0500137 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800138 }
139
Robert Phillipsc0192e32017-09-21 12:00:26 -0400140 {
141 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500142 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400143 REPORTER_ASSERT(reporter, smallRT2);
144
145 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800146 }
147
Robert Phillipsc0192e32017-09-21 12:00:26 -0400148 {
149 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500150 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400151 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800152
Robert Phillipsc0192e32017-09-21 12:00:26 -0400153 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800154 }
bsalomon02a44a42015-02-19 09:09:00 -0800155
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400156 int smallSampleCount =
Robert Phillips9da87e02019-02-04 13:26:26 -0500157 context->priv().caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500158 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700159 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500160 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
161 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800162#ifdef SK_BUILD_FOR_ANDROID
163 if (!smallMSAART0) {
164 // The nexus player seems to fail to create MSAA textures.
165 return;
166 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400167#else
168 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800169#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400170
171 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
172
173 {
174 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500175 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
176 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400177 SkBudgeted::kNo);
178 REPORTER_ASSERT(reporter, smallMSAART1);
179
180 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800181 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400182
Brian Salomonbdecacf2018-02-02 20:32:49 -0500183 // But one with a larger sample count should not. (Also check that the two requests didn't
184 // rounded up to the same actual sample count or else they could share.).
Robert Phillips9da87e02019-02-04 13:26:26 -0500185 int bigSampleCount = context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400186 5, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500187 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500188 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
189 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400190 SkBudgeted::kNo);
191 REPORTER_ASSERT(reporter, smallMSAART2);
192
193 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800194 }
195 }
196}
197
bsalomon68d91342016-04-12 09:59:58 -0700198DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700199 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500200 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
201 GrGpu* gpu = context->priv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700202 // this test is only valid for GL
203 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700204 return;
205 }
206
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500207 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700208 static const int kW = 100;
209 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700210
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500211 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -0400212 GrColorType::kRGBA_8888,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500213 false, GrMipMapped::kNo);
214 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
Robert Phillips646f6372018-09-25 09:31:10 -0400215 GrColorType::kRGBA_8888,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500216 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500217 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
218 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
219 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
220 return;
221 }
jvanverth672bb7f2015-07-13 07:19:57 -0700222
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223 context->resetContext();
224
Robert Phillips6be756b2018-01-16 15:07:54 -0500225 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500226 backendTextures[0], kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227
Robert Phillips6be756b2018-01-16 15:07:54 -0500228 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500229 backendTextures[1], kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
Brian Osman85d34b22017-05-10 12:06:26 -0400231 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
232 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233 return;
234 }
235
halcanary96fcdcc2015-08-27 07:41:13 -0700236 borrowed.reset(nullptr);
237 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700238
239 context->flush();
240
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500241 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
242 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700243
244 REPORTER_ASSERT(reporter, borrowedIsAlive);
245 REPORTER_ASSERT(reporter, !adoptedIsAlive);
246
Brian Salomone64b0642018-03-07 11:47:54 -0500247 if (borrowedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500248 gpu->deleteTestingOnlyBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500249 }
250 if (adoptedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500251 gpu->deleteTestingOnlyBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500252 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700253
254 context->resetContext();
255}
256
bsalomon6d3fe022014-07-25 08:35:45 -0700257class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800258 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000259public:
robertphillips6e83ac72015-08-13 05:19:14 -0700260 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700261
bsalomon1c60dfe2015-01-21 09:32:40 -0800262 /** Property that distinctly categorizes the resource.
263 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800264 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800265
kkinnunen2e6055b2016-04-22 01:48:29 -0700266 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
267 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700268 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800269 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700270 , fProperty(kA_SimulatedProperty)
271 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800272 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800274 }
275
kkinnunen2e6055b2016-04-22 01:48:29 -0700276 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400277 SimulatedProperty property, size_t size = kDefaultSize) {
278 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800279 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500280 static TestResource* CreateWrapped(GrGpu* gpu, GrWrapCacheable cacheable,
281 size_t size = kDefaultSize) {
282 return new TestResource(gpu, cacheable, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283 }
284
Brian Salomond3b65972017-03-22 12:05:03 -0400285 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800286 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000287 }
288
bsalomon33435572014-11-05 14:47:41 -0800289 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000290
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400291 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
292 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293 }
294
bsalomon1c60dfe2015-01-21 09:32:40 -0800295 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
296 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
297 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800298 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
299 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800300 }
301 }
302
303 static size_t ExpectedScratchKeySize() {
304 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
305 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000306private:
bsalomon24db3b12015-01-23 04:24:04 -0800307 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800308
Greg Danielda86e282018-06-13 09:41:19 -0400309 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
310 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700311 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700312 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400313 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700314 , fProperty(property)
315 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800316 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700317 this->registerWithCache(budgeted);
318 }
319
320 // Constructor for simulating resources that wrap backend objects.
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500321 TestResource(GrGpu* gpu, GrWrapCacheable cacheable, size_t size)
322 : INHERITED(gpu)
323 , fToDelete(nullptr)
324 , fSize(size)
325 , fProperty(kA_SimulatedProperty)
326 , fIsScratch(false) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700327 ++fNumAlive;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500328 this->registerWithCacheWrapped(cacheable);
kkinnunen2e6055b2016-04-22 01:48:29 -0700329 }
330
331 void computeScratchKey(GrScratchKey* key) const override {
332 if (fIsScratch) {
333 ComputeScratchKey(fProperty, key);
334 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800335 }
336
mtklein36352bf2015-03-25 18:17:31 -0700337 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400338 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800339
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400340 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000341 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800342 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800343 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700344 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700345 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000346};
bsalomon33435572014-11-05 14:47:41 -0800347int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000348
bsalomonc2f35b72015-01-23 07:19:22 -0800349class Mock {
350public:
351 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400352 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800353 SkASSERT(fContext);
354 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips9da87e02019-02-04 13:26:26 -0500355 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800356 cache->purgeAllUnlocked();
357 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800358 }
bsalomonc2f35b72015-01-23 07:19:22 -0800359
Robert Phillips9da87e02019-02-04 13:26:26 -0500360 GrResourceCache* cache() { return fContext->priv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800361
Hal Canary342b7ac2016-11-04 11:49:42 -0400362 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800363
Greg Danielc27eb722018-08-10 09:48:08 -0400364 void reset() {
365 fContext.reset();
366 }
367
bsalomonc2f35b72015-01-23 07:19:22 -0800368private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400369 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800370};
371
372static void test_no_key(skiatest::Reporter* reporter) {
373 Mock mock(10, 30000);
374 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800375 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500376 GrGpu* gpu = context->priv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800377
378 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400379 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
380 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
381 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
382 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800387 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800390 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800391 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
392
bsalomon8718aaf2015-02-19 07:24:21 -0800393 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 a->unref();
396 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800398 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 c->unref();
402 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800405 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 d->unref();
408 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411
412 b->unref();
413 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800414 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
415 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800416}
417
bsalomon24db3b12015-01-23 04:24:04 -0800418// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500419template <int>
420static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800421 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500422 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800423 builder[0] = data;
424}
425
Robert Phillips6eba0632018-03-28 12:25:42 -0400426static void test_purge_unlocked(skiatest::Reporter* reporter) {
427 Mock mock(10, 30000);
428 GrContext* context = mock.context();
429 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500430 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6eba0632018-03-28 12:25:42 -0400431
432 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
433 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400434 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400435
436 GrUniqueKey uniqueKey;
437 make_unique_key<0>(&uniqueKey, 0);
438
439 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400440 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400441 b->resourcePriv().setUniqueKey(uniqueKey);
442
443 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400444 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400445
446 GrUniqueKey uniqueKey2;
447 make_unique_key<0>(&uniqueKey2, 1);
448
449 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400450 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400451 d->resourcePriv().setUniqueKey(uniqueKey2);
452
453
454 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
455 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
456 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
457 d->gpuMemorySize() == cache->getResourceBytes());
458
459 // Should be safe to purge without deleting the resources since we still have refs.
460 cache->purgeUnlockedResources(false);
461 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
462
463 // Unref them all. Since they all have keys they should remain in the cache.
464
465 a->unref();
466 b->unref();
467 c->unref();
468 d->unref();
469 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
470 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
471 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
472 d->gpuMemorySize() == cache->getResourceBytes());
473
474 // Purge only the two scratch resources
475 cache->purgeUnlockedResources(true);
476
477 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
478 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
479 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
480 cache->getResourceBytes());
481
482 // Purge the uniquely keyed resources
483 cache->purgeUnlockedResources(false);
484
485 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
486 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
487 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
488}
489
bsalomon84c8e622014-11-17 09:33:27 -0800490static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800491 Mock mock(10, 300);
492 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800493 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500494 GrGpu* gpu = context->priv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800495
bsalomon8718aaf2015-02-19 07:24:21 -0800496 GrUniqueKey uniqueKey;
497 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800498
bsalomon8718aaf2015-02-19 07:24:21 -0800499 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800500 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400501 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
502 10);
503 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800504 unique->resourcePriv().setUniqueKey(uniqueKey);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500505 TestResource* wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, 12);
506 TestResource* wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo, 13);
507 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 14);
bsalomondace19e2014-11-17 07:34:06 -0800508
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500509 // Make sure we can add a unique key to the wrapped resources
bsalomon8718aaf2015-02-19 07:24:21 -0800510 GrUniqueKey uniqueKey2;
511 make_unique_key<0>(&uniqueKey2, 1);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500512 GrUniqueKey uniqueKey3;
513 make_unique_key<0>(&uniqueKey3, 2);
514 wrappedCacheable->resourcePriv().setUniqueKey(uniqueKey2);
515 wrappedUncacheable->resourcePriv().setUniqueKey(uniqueKey3);
516 GrGpuResource* wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
517 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
518 GrGpuResource* wrappedUncacheableViaKey = cache->findAndRefUniqueResource(uniqueKey3);
519 REPORTER_ASSERT(reporter, wrappedUncacheableViaKey);
Brian Osman0562eb92017-05-08 11:16:39 -0400520
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500521 // Remove the extra refs we just added.
522 SkSafeUnref(wrappedCacheableViaKey);
523 SkSafeUnref(wrappedUncacheableViaKey);
bsalomondace19e2014-11-17 07:34:06 -0800524
525 // Make sure sizes are as we expect
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500526 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800527 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500528 wrappedCacheable->gpuMemorySize() +
529 wrappedUncacheable->gpuMemorySize() +
530 unbudgeted->gpuMemorySize() ==
531 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800533 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800534 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400535 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800536
bsalomon63c992f2015-01-23 12:47:59 -0800537 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800538 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500539 REPORTER_ASSERT(reporter, 5 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800540 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500541 wrappedCacheable->gpuMemorySize() +
542 wrappedUncacheable->gpuMemorySize() +
543 unbudgeted->gpuMemorySize() ==
544 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800545 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800546 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800547 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400548 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800549
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500550 // Unreffing the cacheable wrapped resource with a unique key shouldn't free it right away.
551 // However, unreffing the uncacheable wrapped resource should free it.
552 wrappedCacheable->unref();
553 wrappedUncacheable->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400554 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800555 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500556 wrappedCacheable->gpuMemorySize() +
557 unbudgeted->gpuMemorySize() ==
558 cache->getResourceBytes());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500559 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800560
bsalomon84c8e622014-11-17 09:33:27 -0800561 // Now try freeing the budgeted resources first
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500562 wrappedUncacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
bsalomon8718aaf2015-02-19 07:24:21 -0800563 unique->unref();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500564 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
565 // This will free 'unique' but not wrappedCacheable which has a key. That requires the key to be
566 // removed to be freed.
bsalomon0ea80f42015-02-11 10:49:59 -0800567 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500568 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
Brian Salomon9bc76d92019-01-24 12:18:33 -0500569
570 wrappedCacheableViaKey = cache->findAndRefUniqueResource(uniqueKey2);
571 REPORTER_ASSERT(reporter, wrappedCacheableViaKey);
572 if (wrappedCacheableViaKey) {
573 wrappedCacheableViaKey->resourcePriv().removeUniqueKey();
574 wrappedCacheable->unref();
575 }
576 // We shouldn't have to call purgeAllUnlocked as removing the key on a wrapped cacheable
577 // resource should immediately delete it.
578 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
579
580 wrappedCacheable = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500581 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
582 wrappedUncacheable->gpuMemorySize() +
583 unbudgeted->gpuMemorySize() ==
584 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800585 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
586 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400587 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800588
589 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400590 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800591 cache->purgeAllUnlocked();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500592 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
593 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrappedCacheable->gpuMemorySize() +
594 wrappedUncacheable->gpuMemorySize() ==
595 cache->getResourceBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800596 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
597 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400598 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800599
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500600 // Unreffing the wrapped resources (with no unique key) should free them right away.
601 wrappedUncacheable->unref();
602 wrappedCacheable->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800603 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
604 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
605 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
606 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400607 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800608
609 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800610 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
611 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
612 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400614 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800615}
616
bsalomon5236cf42015-01-14 10:42:08 -0800617static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800618 Mock mock(10, 30000);
619 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800620 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500621 GrGpu* gpu = context->priv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800622
bsalomon8718aaf2015-02-19 07:24:21 -0800623 GrUniqueKey uniqueKey;
624 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800625
626 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800627 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800628 TestResource* wrapped;
629 TestResource* unbudgeted;
630
631 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500632 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400633 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700634
bsalomon5236cf42015-01-14 10:42:08 -0800635 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
637 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
638 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
639 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400640 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800641
Greg Danielda86e282018-06-13 09:41:19 -0400642 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800643 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800644 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800645 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
646 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
647 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
648 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400649 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800650
bsalomon0ea80f42015-02-11 10:49:59 -0800651 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500652 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800653 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
654 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
655 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
656 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400657 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800658
659 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800660 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
661 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
662 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
663 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400664 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800665
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500666 wrapped = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800667 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
668 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
669 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
670 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400671 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800672
673 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800674 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
675 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
676 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
677 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400678 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800679
bsalomon0ea80f42015-02-11 10:49:59 -0800680 cache->purgeAllUnlocked();
681 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
682 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
683 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
684 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400685 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800686}
687
bsalomon3582d3e2015-02-13 14:20:05 -0800688// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
689void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
690/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800691 Mock mock(10, 300);
692 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800693 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500694 GrGpu* gpu = context->priv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800695
696 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500697 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800698 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800699 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800700
701 size_t size = resource->gpuMemorySize();
702 for (int i = 0; i < 2; ++i) {
703 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800704 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800705 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500706 REPORTER_ASSERT(reporter, GrBudgetedType::kUnbudgetedUncacheable ==
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500707 resource->resourcePriv().budgetedType());
Chris Daltond004e0b2018-09-27 09:28:03 -0600708 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomon0ea80f42015-02-11 10:49:59 -0800709 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
710 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
711 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
712 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400713 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800714
715 // Once it is unrefed, it should become available as scratch.
716 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800717 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
718 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
719 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
720 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400721 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
Chris Daltond004e0b2018-09-27 09:28:03 -0600722 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomonc2f35b72015-01-23 07:19:22 -0800723 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800724 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800725 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500726 REPORTER_ASSERT(reporter,
727 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800728
729 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700730 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800731 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800732 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800733 } else {
734 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800735 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800736 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
737 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
738 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
739 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400740 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800741 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800742 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500743 REPORTER_ASSERT(reporter,
744 GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
bsalomonc2f35b72015-01-23 07:19:22 -0800745
746 // now when it is unrefed it should die since it has no key.
747 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800748 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
749 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
750 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
751 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400752 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800753 }
bsalomon8b79d232014-11-10 10:19:06 -0800754 }
bsalomonc2f35b72015-01-23 07:19:22 -0800755}
756
757static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
758 Mock mock(5, 30000);
759 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800760 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500761 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800762
bsalomon8b79d232014-11-10 10:19:06 -0800763 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500764 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700765 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400766 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500767 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700768 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400769 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800770 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800771 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800772 // Check for negative case consistency. (leaks upon test failure.)
Chris Daltond004e0b2018-09-27 09:28:03 -0600773 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone));
bsalomon1c60dfe2015-01-21 09:32:40 -0800774
775 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800776 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800777
bsalomon0ea80f42015-02-11 10:49:59 -0800778 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800779 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800780 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
781 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800782 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800783 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800784
bsalomon63c992f2015-01-23 12:47:59 -0800785 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800786 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800787 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800788 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800789
790 // Unref but don't purge
791 a->unref();
792 b->unref();
793 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800794 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800795
bsalomon63c992f2015-01-23 12:47:59 -0800796 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800797 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800798 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800799 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
800 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800801}
802
bsalomon10e23ca2014-11-25 05:52:06 -0800803static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800804 Mock mock(5, 30000);
805 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800806 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500807 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800808
bsalomon10e23ca2014-11-25 05:52:06 -0800809 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500810 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800811 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500812 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800813 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800814 a->unref();
815 b->unref();
816
bsalomon1c60dfe2015-01-21 09:32:40 -0800817 GrScratchKey scratchKey;
818 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800819 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800820 // (following leaks upon test failure).
Chris Daltond004e0b2018-09-27 09:28:03 -0600821 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800822
bsalomon0ea80f42015-02-11 10:49:59 -0800823 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800824 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800825 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800826 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
827 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800828
829 // Find the first resource and remove its scratch key
830 GrGpuResource* find;
Chris Daltond004e0b2018-09-27 09:28:03 -0600831 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon3582d3e2015-02-13 14:20:05 -0800832 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800833 // It's still alive, but not cached by scratch key anymore
834 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800835 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
836 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800837
838 // The cache should immediately delete it when it's unrefed since it isn't accessible.
839 find->unref();
840 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800841 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
842 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800843
844 // Repeat for the second resource.
Chris Daltond004e0b2018-09-27 09:28:03 -0600845 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon3582d3e2015-02-13 14:20:05 -0800846 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800847 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800848 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
849 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800850
851 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800852 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800853 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800854 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
855 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800856
857 find->unref();
858 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800859 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
860 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800861}
862
bsalomon1c60dfe2015-01-21 09:32:40 -0800863static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800864 Mock mock(5, 30000);
865 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800866 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500867 GrGpu* gpu = context->priv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800868
869 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500870 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800871 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500872 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800873 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800874 a->unref();
875 b->unref();
876
877 GrScratchKey scratchKey;
878 // Ensure that scratch key comparison and assignment is consistent.
879 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800880 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800881 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800882 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800883 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
884 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
885 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
886 scratchKey = scratchKey1;
887 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
888 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
889 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
890 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
891 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
892 scratchKey = scratchKey2;
893 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
894 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
895 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
896 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
897 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
898
899 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800900 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800901 // (following leaks upon test failure).
Chris Daltond004e0b2018-09-27 09:28:03 -0600902 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800903
904 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800905 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
Chris Daltond004e0b2018-09-27 09:28:03 -0600906 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700907 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800908 find->unref();
909
910 scratchKey2 = scratchKey;
Chris Daltond004e0b2018-09-27 09:28:03 -0600911 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700912 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800913 REPORTER_ASSERT(reporter, find == a || find == b);
914
Chris Daltond004e0b2018-09-27 09:28:03 -0600915 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
halcanary96fcdcc2015-08-27 07:41:13 -0700916 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800917 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
918 REPORTER_ASSERT(reporter, find2 != find);
919 find2->unref();
920 find->unref();
921}
922
bsalomon8718aaf2015-02-19 07:24:21 -0800923static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800924 Mock mock(5, 30000);
925 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800926 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500927 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000928
bsalomon8718aaf2015-02-19 07:24:21 -0800929 GrUniqueKey key;
930 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700931
bsalomon8718aaf2015-02-19 07:24:21 -0800932 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400933 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700934
bsalomonf99e9612015-02-19 08:24:16 -0800935 // Set key on resource a.
936 a->resourcePriv().setUniqueKey(key);
937 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
938 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800939
bsalomonf99e9612015-02-19 08:24:16 -0800940 // Make sure that redundantly setting a's key works.
941 a->resourcePriv().setUniqueKey(key);
942 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800943 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800944 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
945 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800946 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
947
bsalomonf99e9612015-02-19 08:24:16 -0800948 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400949 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800950 b->resourcePriv().setUniqueKey(key);
951 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
952 b->unref();
953
954 // Still have two resources because a is still reffed.
955 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
956 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
957 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
958
959 a->unref();
960 // Now a should be gone.
961 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
962 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
963 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
964
965 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
966 // Also make b be unreffed when replacement occurs.
967 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400968 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800969 GrUniqueKey differentKey;
970 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800971 c->resourcePriv().setUniqueKey(differentKey);
972 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
973 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
974 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
975 // c replaces b and b should be immediately purged.
976 c->resourcePriv().setUniqueKey(key);
977 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
978 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
979 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
980
981 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800982 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800983 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
984 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
985 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
986
987 // Drop the ref on c, it should be kept alive because it has a unique key.
988 c->unref();
989 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
990 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
991 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
992
993 // Verify that we can find c, then remove its unique key. It should get purged immediately.
994 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
995 c->resourcePriv().removeUniqueKey();
996 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800997 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
998 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800999 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -07001000
1001 {
1002 GrUniqueKey key2;
1003 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001004 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -07001005 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001006 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001007 d->resourcePriv().setUniqueKey(key2);
1008 }
1009
1010 GrUniqueKey key3;
1011 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -04001012 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -07001013 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001014}
1015
bsalomon8b79d232014-11-10 10:19:06 -08001016static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001017 Mock mock(5, 30000);
1018 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001019 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001020 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001021
bsalomon8718aaf2015-02-19 07:24:21 -08001022 GrUniqueKey key1, key2, key3;
1023 make_unique_key<0>(&key1, 1);
1024 make_unique_key<0>(&key2, 2);
1025 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001026
bsalomon23e619c2015-02-06 11:54:28 -08001027 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001028 TestResource* a = new TestResource(gpu);
1029 TestResource* b = new TestResource(gpu);
1030 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001031 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001032 a->resourcePriv().setUniqueKey(key1);
1033 b->resourcePriv().setUniqueKey(key2);
1034 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001035 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001036 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001037 c->unref();
1038
bsalomon8718aaf2015-02-19 07:24:21 -08001039 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1040 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1041 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001042 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001043
bsalomon8718aaf2015-02-19 07:24:21 -08001044 typedef GrUniqueKeyInvalidatedMessage Msg;
1045 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001046
1047 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Robert Phillips9da87e02019-02-04 13:26:26 -05001048 Bus::Post(Msg(key1, context->priv().contextID()));
1049 Bus::Post(Msg(key2, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001050 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001051 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001052 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1053 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001054 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001055 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001056
1057 // Invalidate the third.
Robert Phillips9da87e02019-02-04 13:26:26 -05001058 Bus::Post(Msg(key3, context->priv().contextID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001059 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001060 // we still have a ref on b, c should be recycled as scratch.
1061 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001062 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001063
bsalomon23e619c2015-02-06 11:54:28 -08001064 // make b purgeable. It should be immediately deleted since it has no key.
1065 b->unref();
1066 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1067
1068 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1069 GrScratchKey scratchKey;
1070 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
Chris Daltond004e0b2018-09-27 09:28:03 -06001071 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon23e619c2015-02-06 11:54:28 -08001072 REPORTER_ASSERT(reporter, scratch == c);
1073 SkSafeUnref(scratch);
1074
1075 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001076 cache->purgeAllUnlocked();
Chris Daltond004e0b2018-09-27 09:28:03 -06001077 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, GrResourceCache::ScratchFlags::kNone);
bsalomon71cb0c22014-11-14 12:10:14 -08001078 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001079 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1080 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001081 REPORTER_ASSERT(reporter, !scratch);
1082 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001083}
1084
bsalomon71cb0c22014-11-14 12:10:14 -08001085static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001086 Mock mock(3, 30000);
1087 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001088 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001089 GrGpu* gpu = context->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001090
bsalomon8718aaf2015-02-19 07:24:21 -08001091 GrUniqueKey key1, key2;
1092 make_unique_key<0>(&key1, 1);
1093 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001094
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001095 sk_sp<TestResource> a(new TestResource(gpu));
1096 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001097 a->resourcePriv().setUniqueKey(key1);
1098 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001099
bsalomonc2f35b72015-01-23 07:19:22 -08001100 // Make a cycle
1101 a->setUnrefWhenDestroyed(b);
1102 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001103
bsalomonc2f35b72015-01-23 07:19:22 -08001104 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001105
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001106 TestResource* unownedA = a.release();
1107 unownedA->unref();
1108 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001109
bsalomonc2f35b72015-01-23 07:19:22 -08001110 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001111
bsalomon0ea80f42015-02-11 10:49:59 -08001112 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001113 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001114
bsalomonc2f35b72015-01-23 07:19:22 -08001115 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001116 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001117 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001118
bsalomon0ea80f42015-02-11 10:49:59 -08001119 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001120 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001121}
1122
bsalomonddf30e62015-02-19 11:38:44 -08001123static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1124 static const int kCount = 50;
1125 static const int kBudgetCnt = kCount / 2;
1126 static const int kLockedFreq = 8;
1127 static const int kBudgetSize = 0x80000000;
1128
1129 SkRandom random;
1130
1131 // Run the test 2*kCount times;
1132 for (int i = 0; i < 2 * kCount; ++i ) {
1133 Mock mock(kBudgetCnt, kBudgetSize);
1134 GrContext* context = mock.context();
1135 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001136 GrGpu* gpu = context->priv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001137
1138 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001139 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001140
1141 static const int kNumToPurge = kCount - kBudgetCnt;
1142
1143 SkTDArray<int> shouldPurgeIdxs;
1144 int purgeableCnt = 0;
1145 SkTDArray<GrGpuResource*> resourcesToUnref;
1146
1147 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1148 // unpurgeable resources.
1149 for (int j = 0; j < kCount; ++j) {
1150 GrUniqueKey key;
1151 make_unique_key<0>(&key, j);
1152
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001153 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001154 r->resourcePriv().setUniqueKey(key);
1155 if (random.nextU() % kLockedFreq) {
1156 // Make this is purgeable.
1157 r->unref();
1158 ++purgeableCnt;
1159 if (purgeableCnt <= kNumToPurge) {
1160 *shouldPurgeIdxs.append() = j;
1161 }
1162 } else {
1163 *resourcesToUnref.append() = r;
1164 }
1165 }
1166
1167 // Verify that the correct resources were purged.
1168 int currShouldPurgeIdx = 0;
1169 for (int j = 0; j < kCount; ++j) {
1170 GrUniqueKey key;
1171 make_unique_key<0>(&key, j);
1172 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1173 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1174 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1175 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001176 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001177 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001178 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001179 }
1180 SkSafeUnref(res);
1181 }
1182
1183 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1184 resourcesToUnref[j]->unref();
1185 }
1186 }
1187}
1188
Brian Salomon5e150852017-03-22 14:53:13 -04001189static void test_time_purge(skiatest::Reporter* reporter) {
1190 Mock mock(1000000, 1000000);
1191 GrContext* context = mock.context();
1192 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001193 GrGpu* gpu = context->priv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001194
1195 static constexpr int kCnts[] = {1, 10, 1024};
1196 auto nowish = []() {
1197 // We sleep so that we ensure we get a value that is greater than the last call to
1198 // GrStdSteadyClock::now().
1199 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1200 auto result = GrStdSteadyClock::now();
1201 // Also sleep afterwards so we don't get this value again.
1202 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1203 return result;
1204 };
1205
1206 for (int cnt : kCnts) {
1207 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1208 new GrStdSteadyClock::time_point[cnt]);
1209 {
1210 // Insert resources and get time points between each addition.
1211 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001212 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001213 GrUniqueKey k;
1214 make_unique_key<1>(&k, i);
1215 r->resourcePriv().setUniqueKey(k);
1216 r->unref();
1217 timeStamps.get()[i] = nowish();
1218 }
1219
1220 // Purge based on the time points between resource additions. Each purge should remove
1221 // the oldest resource.
1222 for (int i = 0; i < cnt; ++i) {
1223 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1224 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1225 for (int j = 0; j < i; ++j) {
1226 GrUniqueKey k;
1227 make_unique_key<1>(&k, j);
1228 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1229 REPORTER_ASSERT(reporter, !SkToBool(r));
1230 SkSafeUnref(r);
1231 }
1232 }
1233
1234 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1235 cache->purgeAllUnlocked();
1236 }
1237
1238 // Do a similar test but where we leave refs on some resources to prevent them from being
1239 // purged.
1240 {
1241 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1242 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001243 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001244 GrUniqueKey k;
1245 make_unique_key<1>(&k, i);
1246 r->resourcePriv().setUniqueKey(k);
1247 // Leave a ref on every other resource, beginning with the first.
1248 if (SkToBool(i & 0x1)) {
1249 refedResources.get()[i / 2] = r;
1250 } else {
1251 r->unref();
1252 }
1253 timeStamps.get()[i] = nowish();
1254 }
1255
1256 for (int i = 0; i < cnt; ++i) {
1257 // Should get a resource purged every other frame.
1258 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1259 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1260 }
1261
1262 // Unref all the resources that we kept refs on in the first loop.
1263 for (int i = 0; i < (cnt / 2); ++i) {
1264 refedResources.get()[i]->unref();
1265 cache->purgeResourcesNotUsedSince(nowish());
1266 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1267 }
1268
1269 cache->purgeAllUnlocked();
1270 }
1271
1272 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1273
1274 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1275 // eviction
1276 context->flush();
1277 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001278 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001279 GrUniqueKey k;
1280 make_unique_key<1>(&k, i);
1281 r->resourcePriv().setUniqueKey(k);
1282 r->unref();
1283 }
1284 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1285 context->flush();
1286 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1287 cache->purgeResourcesNotUsedSince(nowish());
1288 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1289 }
1290}
1291
Derek Sollenberger5480a182017-05-25 16:43:59 -04001292static void test_partial_purge(skiatest::Reporter* reporter) {
1293 Mock mock(6, 100);
1294 GrContext* context = mock.context();
1295 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001296 GrGpu* gpu = context->priv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001297
1298 enum TestsCase {
1299 kOnlyScratch_TestCase = 0,
1300 kPartialScratch_TestCase = 1,
1301 kAllScratch_TestCase = 2,
1302 kPartial_TestCase = 3,
1303 kAll_TestCase = 4,
1304 kNone_TestCase = 5,
1305 kEndTests_TestCase = kNone_TestCase + 1
1306 };
1307
1308 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1309
1310 GrUniqueKey key1, key2, key3;
1311 make_unique_key<0>(&key1, 1);
1312 make_unique_key<0>(&key2, 2);
1313 make_unique_key<0>(&key3, 3);
1314
1315 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001316 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1317 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1318 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001319
1320 unique1->resourcePriv().setUniqueKey(key1);
1321 unique2->resourcePriv().setUniqueKey(key2);
1322 unique3->resourcePriv().setUniqueKey(key3);
1323
Derek Sollenberger5480a182017-05-25 16:43:59 -04001324 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001325 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001326 TestResource::kA_SimulatedProperty,
1327 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001328 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001329 TestResource::kB_SimulatedProperty,
1330 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001331
1332 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1333 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1334 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1335
1336 // Add resources to the purgeable queue
1337 unique1->unref();
1338 scratch1->unref();
1339 unique2->unref();
1340 scratch2->unref();
1341 unique3->unref();
1342
1343 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1344 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1345 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1346
1347 switch(testCase) {
1348 case kOnlyScratch_TestCase: {
1349 context->purgeUnlockedResources(14, true);
1350 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1351 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1352 break;
1353 }
1354 case kPartialScratch_TestCase: {
1355 context->purgeUnlockedResources(3, true);
1356 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1357 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1358 break;
1359 }
1360 case kAllScratch_TestCase: {
1361 context->purgeUnlockedResources(50, true);
1362 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1363 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1364 break;
1365 }
1366 case kPartial_TestCase: {
1367 context->purgeUnlockedResources(13, false);
1368 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1369 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1370 break;
1371 }
1372 case kAll_TestCase: {
1373 context->purgeUnlockedResources(50, false);
1374 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1375 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1376 break;
1377 }
1378 case kNone_TestCase: {
1379 context->purgeUnlockedResources(0, true);
1380 context->purgeUnlockedResources(0, false);
1381 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1382 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1383 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1384 break;
1385 }
Brian Salomon23356442018-11-30 15:33:19 -05001386 }
Derek Sollenberger5480a182017-05-25 16:43:59 -04001387
1388 // ensure all are purged before the next
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001389 context->priv().testingOnly_purgeAllUnlockedResources();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001390 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1391 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1392
1393 }
1394}
1395
bsalomon10e23ca2014-11-25 05:52:06 -08001396static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001397 // Set the cache size to double the resource count because we're going to create 2x that number
1398 // resources, using two different key domains. Add a little slop to the bytes because we resize
1399 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001400 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001401
bsalomonc2f35b72015-01-23 07:19:22 -08001402 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1403 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001404 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001405 GrGpu* gpu = context->priv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001406
1407 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001408 GrUniqueKey key1, key2;
1409 make_unique_key<1>(&key1, i);
1410 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001411
bsalomon24db3b12015-01-23 04:24:04 -08001412 TestResource* resource;
1413
Greg Danielda86e282018-06-13 09:41:19 -04001414 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001415 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001416 resource->unref();
1417
Greg Danielda86e282018-06-13 09:41:19 -04001418 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001419 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001420 resource->unref();
1421 }
1422
1423 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001424 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001425 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1426 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1427 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1428 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001429 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001430 GrUniqueKey key1, key2;
1431 make_unique_key<1>(&key1, i);
1432 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001433
bsalomon8718aaf2015-02-19 07:24:21 -08001434 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1435 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001436 }
1437
bsalomon0ea80f42015-02-11 10:49:59 -08001438 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001439 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001440 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001441 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1442 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1443 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1444 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001445
1446 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001447 GrUniqueKey key1, key2;
1448 make_unique_key<1>(&key1, i);
1449 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001450
bsalomon8718aaf2015-02-19 07:24:21 -08001451 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1452 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001453 }
1454}
1455
senorblanco84cd6212015-08-04 10:01:58 -07001456static void test_custom_data(skiatest::Reporter* reporter) {
1457 GrUniqueKey key1, key2;
1458 make_unique_key<0>(&key1, 1);
1459 make_unique_key<0>(&key2, 2);
1460 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001461 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001462 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1463 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1464
1465 // Test that copying a key also takes a ref on its custom data.
1466 GrUniqueKey key3 = key1;
1467 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1468}
1469
bsalomonc6363ef2015-09-24 07:07:40 -07001470static void test_abandoned(skiatest::Reporter* reporter) {
1471 Mock mock(10, 300);
1472 GrContext* context = mock.context();
Robert Phillips9da87e02019-02-04 13:26:26 -05001473 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001474
1475 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001476 context->abandonContext();
1477
1478 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1479
1480 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1481
robertphillips8abb3702016-08-31 14:04:06 -07001482 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001483 resource->getUniqueKey();
1484 resource->wasDestroyed();
1485 resource->gpuMemorySize();
1486 resource->getContext();
1487
bsalomonc6363ef2015-09-24 07:07:40 -07001488 resource->resourcePriv().getScratchKey();
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001489 resource->resourcePriv().budgetedType();
bsalomonc6363ef2015-09-24 07:07:40 -07001490 resource->resourcePriv().makeBudgeted();
1491 resource->resourcePriv().makeUnbudgeted();
1492 resource->resourcePriv().removeScratchKey();
1493 GrUniqueKey key;
1494 make_unique_key<0>(&key, 1);
1495 resource->resourcePriv().setUniqueKey(key);
1496 resource->resourcePriv().removeUniqueKey();
1497}
1498
Brian Salomon1090da62017-01-06 12:04:19 -05001499static void test_tags(skiatest::Reporter* reporter) {
1500#ifdef SK_DEBUG
1501 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1502 static constexpr int kLastTagIdx = 10;
1503 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1504
1505 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1506 GrContext* context = mock.context();
1507 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001508 GrGpu* gpu = context->priv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001509
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001510 // tag strings are expected to be long lived
1511 std::vector<SkString> tagStrings;
1512
Brian Salomon1090da62017-01-06 12:04:19 -05001513 SkString tagStr;
1514 int tagIdx = 0;
1515 int currTagCnt = 0;
1516
1517 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001518
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001519 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001520 GrUniqueKey key;
1521 if (currTagCnt == tagIdx) {
1522 tagIdx += 1;
1523 currTagCnt = 0;
1524 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001525 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001526 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001527 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001528 resource->resourcePriv().setUniqueKey(key);
1529 }
1530 SkASSERT(kLastTagIdx == tagIdx);
1531 SkASSERT(currTagCnt == kLastTagIdx);
1532
1533 // Test i = 0 to exercise unused tag string.
1534 for (int i = 0; i <= kLastTagIdx; ++i) {
1535 tagStr.printf("tag%d", i);
1536 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1537 }
1538#endif
1539}
1540
Greg Danielc27eb722018-08-10 09:48:08 -04001541static void test_free_resource_messages(skiatest::Reporter* reporter) {
1542 Mock mock(10, 30000);
1543 GrContext* context = mock.context();
1544 GrResourceCache* cache = mock.cache();
Robert Phillips9da87e02019-02-04 13:26:26 -05001545 GrGpu* gpu = context->priv().getGpu();
Greg Danielc27eb722018-08-10 09:48:08 -04001546
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001547 TestResource* wrapped1 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Greg Danielc27eb722018-08-10 09:48:08 -04001548 cache->insertCrossContextGpuResource(wrapped1);
1549
1550 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1551
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001552 TestResource* wrapped2 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kYes);
Greg Danielc27eb722018-08-10 09:48:08 -04001553 cache->insertCrossContextGpuResource(wrapped2);
1554
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001555 // An uncacheable cross-context should not be purged as soon as we drop our ref. This
1556 // is because inserting it as a cross-context resource actually holds a ref until the
1557 // message is received.
1558 TestResource* wrapped3 = TestResource::CreateWrapped(gpu, GrWrapCacheable::kNo);
1559 cache->insertCrossContextGpuResource(wrapped3);
1560
1561 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001562
1563 // Have only ref waiting on message.
1564 wrapped1->unref();
1565 wrapped2->unref();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001566 wrapped3->unref();
Greg Danielc27eb722018-08-10 09:48:08 -04001567
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001568 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
Greg Danielc27eb722018-08-10 09:48:08 -04001569
1570 // This should free nothing since no messages were sent.
1571 cache->purgeAsNeeded();
1572
1573 // Send message to free the first resource
Robert Phillips9da87e02019-02-04 13:26:26 -05001574 GrGpuResourceFreedMessage msg1{wrapped1, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001575 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg1);
1576 cache->purgeAsNeeded();
1577
1578 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1579
Robert Phillips9da87e02019-02-04 13:26:26 -05001580 GrGpuResourceFreedMessage msg2{wrapped3, context->priv().contextID()};
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001581 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg2);
Greg Danielc27eb722018-08-10 09:48:08 -04001582 cache->purgeAsNeeded();
1583
1584 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1585
1586 mock.reset();
1587
1588 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
1589}
1590
1591
Brian Salomondcfca432017-11-15 15:48:03 -05001592DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001593 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001594 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001595 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001596 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001597 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001598 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001599 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001600 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001601 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001602 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001603 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001604 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001605 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001606 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001607 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001608 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001609 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001610 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001611 test_tags(reporter);
Greg Danielc27eb722018-08-10 09:48:08 -04001612 test_free_resource_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001613}
1614
Robert Phillipsd6214d42016-11-07 08:23:48 -05001615////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001616static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001617 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001618 int width, int height,
1619 int sampleCnt) {
1620 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001621 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001622 desc.fWidth = width;
1623 desc.fHeight = height;
1624 desc.fConfig = kRGBA_8888_GrPixelConfig;
1625 desc.fSampleCnt = sampleCnt;
1626
Robert Phillipse78b7252017-04-06 07:59:41 -04001627 return provider->createTexture(desc, SkBudgeted::kYes);
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}