blob: e344092696569ee669e966fa5204e191e44b0d12 [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
Brian Salomon5e150852017-03-22 14:53:13 -040010#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070011#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000013#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070014#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourceCacheAccess.h"
16#include "GrGpuResourcePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050017#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040018#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080019#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070020#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070021#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040022#include "GrTexture.h"
23
bsalomonbcf0a522014-10-08 08:40:09 -070024#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080025#include "SkGr.h"
26#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050027#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070028#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000029#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000030
31static 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();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400123 if (context->contextPriv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700124 return;
125 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400126
Robert Phillips6be756b2018-01-16 15:07:54 -0500127 GrResourceProvider* resourceProvider = context->contextPriv().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 =
157 context->contextPriv().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.).
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400185 int bigSampleCount = context->contextPriv().caps()->getRenderTargetSampleCount(
186 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 Phillips6be756b2018-01-16 15:07:54 -0500200 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500201 GrGpu* gpu = context->contextPriv().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,
212 kRGBA_8888_GrPixelConfig,
213 false, GrMipMapped::kNo);
214 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
215 kRGBA_8888_GrPixelConfig,
216 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(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500226 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227
Robert Phillips6be756b2018-01-16 15:07:54 -0500228 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500229 backendTextures[1], kAdopt_GrWrapOwnership));
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 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700280 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
281 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000282 }
283
Brian Salomond3b65972017-03-22 12:05:03 -0400284 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800285 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000286 }
287
bsalomon33435572014-11-05 14:47:41 -0800288 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400290 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
291 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000292 }
293
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
295 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
296 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800297 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
298 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800299 }
300 }
301
302 static size_t ExpectedScratchKeySize() {
303 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
304 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000305private:
bsalomon24db3b12015-01-23 04:24:04 -0800306 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800307
Greg Danielda86e282018-06-13 09:41:19 -0400308 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
309 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700310 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700311 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400312 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700313 , fProperty(property)
314 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 this->registerWithCache(budgeted);
317 }
318
319 // Constructor for simulating resources that wrap backend objects.
320 TestResource(GrGpu* gpu, size_t size)
321 : INHERITED(gpu)
322 , fToDelete(nullptr)
323 , fSize(size)
324 , fProperty(kA_SimulatedProperty)
325 , fIsScratch(false) {
326 ++fNumAlive;
327 this->registerWithCacheWrapped();
328 }
329
330 void computeScratchKey(GrScratchKey* key) const override {
331 if (fIsScratch) {
332 ComputeScratchKey(fProperty, key);
333 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800334 }
335
mtklein36352bf2015-03-25 18:17:31 -0700336 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400337 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800338
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400339 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000340 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800341 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800342 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700343 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700344 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000345};
bsalomon33435572014-11-05 14:47:41 -0800346int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000347
bsalomonc2f35b72015-01-23 07:19:22 -0800348class Mock {
349public:
350 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400351 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800352 SkASSERT(fContext);
353 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500354 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800355 cache->purgeAllUnlocked();
356 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800357 }
bsalomonc2f35b72015-01-23 07:19:22 -0800358
Robert Phillips6be756b2018-01-16 15:07:54 -0500359 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
Hal Canary342b7ac2016-11-04 11:49:42 -0400361 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800362
Greg Danielc27eb722018-08-10 09:48:08 -0400363 void reset() {
364 fContext.reset();
365 }
366
bsalomonc2f35b72015-01-23 07:19:22 -0800367private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400368 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800369};
370
371static void test_no_key(skiatest::Reporter* reporter) {
372 Mock mock(10, 30000);
373 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800374 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500375 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400378 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
379 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
380 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
381 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800384 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800385 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800386 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800387
388 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800390 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
391
bsalomon8718aaf2015-02-19 07:24:21 -0800392 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800393
394 a->unref();
395 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800396 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800397 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800398 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800399
400 c->unref();
401 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800402 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800403 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800404 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800405
406 d->unref();
407 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800408 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
409 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 b->unref();
412 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800413 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
414 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415}
416
bsalomon24db3b12015-01-23 04:24:04 -0800417// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500418template <int>
419static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800420 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500421 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800422 builder[0] = data;
423}
424
Robert Phillips6eba0632018-03-28 12:25:42 -0400425static void test_purge_unlocked(skiatest::Reporter* reporter) {
426 Mock mock(10, 30000);
427 GrContext* context = mock.context();
428 GrResourceCache* cache = mock.cache();
429 GrGpu* gpu = context->contextPriv().getGpu();
430
431 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
432 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400433 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400434
435 GrUniqueKey uniqueKey;
436 make_unique_key<0>(&uniqueKey, 0);
437
438 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400439 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400440 b->resourcePriv().setUniqueKey(uniqueKey);
441
442 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400443 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400444
445 GrUniqueKey uniqueKey2;
446 make_unique_key<0>(&uniqueKey2, 1);
447
448 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400449 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400450 d->resourcePriv().setUniqueKey(uniqueKey2);
451
452
453 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
454 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
455 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
456 d->gpuMemorySize() == cache->getResourceBytes());
457
458 // Should be safe to purge without deleting the resources since we still have refs.
459 cache->purgeUnlockedResources(false);
460 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
461
462 // Unref them all. Since they all have keys they should remain in the cache.
463
464 a->unref();
465 b->unref();
466 c->unref();
467 d->unref();
468 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
469 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
470 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
471 d->gpuMemorySize() == cache->getResourceBytes());
472
473 // Purge only the two scratch resources
474 cache->purgeUnlockedResources(true);
475
476 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
477 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
478 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
479 cache->getResourceBytes());
480
481 // Purge the uniquely keyed resources
482 cache->purgeUnlockedResources(false);
483
484 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
485 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
486 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
487}
488
bsalomon84c8e622014-11-17 09:33:27 -0800489static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800490 Mock mock(10, 300);
491 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800492 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500493 GrGpu* gpu = context->contextPriv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800494
bsalomon8718aaf2015-02-19 07:24:21 -0800495 GrUniqueKey uniqueKey;
496 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800497
bsalomon8718aaf2015-02-19 07:24:21 -0800498 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800499 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400500 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
501 10);
502 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800503 unique->resourcePriv().setUniqueKey(uniqueKey);
Greg Danielda86e282018-06-13 09:41:19 -0400504 TestResource* wrapped = TestResource::CreateWrapped(gpu, 12);
505 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 13);
bsalomondace19e2014-11-17 07:34:06 -0800506
Brian Osman0562eb92017-05-08 11:16:39 -0400507 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800508 GrUniqueKey uniqueKey2;
509 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800510 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400511 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
512 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
513
514 // Remove the extra ref we just added.
515 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800516
517 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800518 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800519 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800520 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800521 cache->getResourceBytes());
522 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800523 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800524 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400525 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800526
bsalomon63c992f2015-01-23 12:47:59 -0800527 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800528 cache->purgeAllUnlocked();
529 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800530 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800531 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800532 cache->getResourceBytes());
533 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800534 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800535 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400536 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800537
538 // Unreffing the wrapped resource should free it right away.
539 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800540 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800541 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800542 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400543 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800544
bsalomon84c8e622014-11-17 09:33:27 -0800545 // Now try freeing the budgeted resources first
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500546 wrapped = TestResource::CreateWrapped(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800547 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400548 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800549 cache->purgeAllUnlocked();
550 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800551 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800552 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
553 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
554 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400555 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800556
557 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400558 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800559 cache->purgeAllUnlocked();
560 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800561 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800562 cache->getResourceBytes());
563 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
564 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400565 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800566
567 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800568 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
569 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
570 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
571 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400572 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800573
574 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
576 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
577 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
578 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400579 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800580}
581
bsalomon5236cf42015-01-14 10:42:08 -0800582static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800583 Mock mock(10, 30000);
584 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800585 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500586 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800587
bsalomon8718aaf2015-02-19 07:24:21 -0800588 GrUniqueKey uniqueKey;
589 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800590
591 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800592 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800593 TestResource* wrapped;
594 TestResource* unbudgeted;
595
596 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500597 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400598 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700599
bsalomon5236cf42015-01-14 10:42:08 -0800600 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
602 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
603 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
604 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400605 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800606
Greg Danielda86e282018-06-13 09:41:19 -0400607 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800608 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800609 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800610 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
611 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
612 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
613 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400614 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800615
bsalomon0ea80f42015-02-11 10:49:59 -0800616 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500617 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800618 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
619 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
620 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
621 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400622 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800623
624 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800625 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
626 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
627 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
628 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400629 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800630
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500631 wrapped = TestResource::CreateWrapped(gpu, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800632 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
633 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
634 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
635 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400636 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800637
638 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800639 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
640 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
641 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
642 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400643 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800644
bsalomon0ea80f42015-02-11 10:49:59 -0800645 cache->purgeAllUnlocked();
646 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
647 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
648 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
649 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400650 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800651}
652
bsalomon3582d3e2015-02-13 14:20:05 -0800653// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
654void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
655/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800656 Mock mock(10, 300);
657 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800658 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500659 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800660
661 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500662 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800663 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800664 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800665
666 size_t size = resource->gpuMemorySize();
667 for (int i = 0; i < 2; ++i) {
668 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800669 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800670 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800671 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700672 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800673 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
674 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
675 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
676 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400677 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800678
679 // Once it is unrefed, it should become available as scratch.
680 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800681 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
682 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
683 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
684 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400685 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700686 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800687 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800688 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800689 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800690 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800691
692 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700693 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800694 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800695 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800696 } else {
697 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800698 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800699 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
700 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
701 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
702 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400703 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800704 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800705 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800706 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800707
708 // now when it is unrefed it should die since it has no key.
709 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800710 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
711 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
712 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
713 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400714 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800715 }
bsalomon8b79d232014-11-10 10:19:06 -0800716 }
bsalomonc2f35b72015-01-23 07:19:22 -0800717}
718
719static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
720 Mock mock(5, 30000);
721 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800722 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500723 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800724
bsalomon8b79d232014-11-10 10:19:06 -0800725 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500726 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700727 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400728 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500729 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700730 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400731 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800732 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800733 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800734 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700735 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800736
737 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800738 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800739
bsalomon0ea80f42015-02-11 10:49:59 -0800740 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800741 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800742 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
743 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800744 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800745 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800746
bsalomon63c992f2015-01-23 12:47:59 -0800747 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800748 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800749 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800750 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800751
752 // Unref but don't purge
753 a->unref();
754 b->unref();
755 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800756 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800757
bsalomon63c992f2015-01-23 12:47:59 -0800758 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800759 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800760 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
762 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800763}
764
bsalomon10e23ca2014-11-25 05:52:06 -0800765static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800766 Mock mock(5, 30000);
767 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800768 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500769 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800770
bsalomon10e23ca2014-11-25 05:52:06 -0800771 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500772 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800773 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500774 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800775 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800776 a->unref();
777 b->unref();
778
bsalomon1c60dfe2015-01-21 09:32:40 -0800779 GrScratchKey scratchKey;
780 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800781 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800782 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700783 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800784
bsalomon0ea80f42015-02-11 10:49:59 -0800785 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800786 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800787 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800788 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
789 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800790
791 // Find the first resource and remove its scratch key
792 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700793 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800794 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800795 // It's still alive, but not cached by scratch key anymore
796 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800797 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
798 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800799
800 // The cache should immediately delete it when it's unrefed since it isn't accessible.
801 find->unref();
802 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800803 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
804 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800805
806 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700807 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800808 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800809 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800810 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
811 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800812
813 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800814 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800815 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800816 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
817 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800818
819 find->unref();
820 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800821 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
822 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800823}
824
bsalomon1c60dfe2015-01-21 09:32:40 -0800825static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800826 Mock mock(5, 30000);
827 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800828 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500829 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800830
831 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500832 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800833 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500834 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800835 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800836 a->unref();
837 b->unref();
838
839 GrScratchKey scratchKey;
840 // Ensure that scratch key comparison and assignment is consistent.
841 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800842 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800843 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800844 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800845 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
846 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
847 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
848 scratchKey = scratchKey1;
849 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
850 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
851 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
852 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
853 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
854 scratchKey = scratchKey2;
855 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
856 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
857 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
858 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
859 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
860
861 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800862 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800863 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700864 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800865
866 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800867 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700868 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700869 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800870 find->unref();
871
872 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700873 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700874 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800875 REPORTER_ASSERT(reporter, find == a || find == b);
876
robertphillips6e83ac72015-08-13 05:19:14 -0700877 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700878 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800879 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
880 REPORTER_ASSERT(reporter, find2 != find);
881 find2->unref();
882 find->unref();
883}
884
bsalomon8718aaf2015-02-19 07:24:21 -0800885static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800886 Mock mock(5, 30000);
887 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800888 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500889 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000890
bsalomon8718aaf2015-02-19 07:24:21 -0800891 GrUniqueKey key;
892 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700893
bsalomon8718aaf2015-02-19 07:24:21 -0800894 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400895 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700896
bsalomonf99e9612015-02-19 08:24:16 -0800897 // Set key on resource a.
898 a->resourcePriv().setUniqueKey(key);
899 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
900 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800901
bsalomonf99e9612015-02-19 08:24:16 -0800902 // Make sure that redundantly setting a's key works.
903 a->resourcePriv().setUniqueKey(key);
904 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800905 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800906 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
907 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800908 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
909
bsalomonf99e9612015-02-19 08:24:16 -0800910 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400911 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800912 b->resourcePriv().setUniqueKey(key);
913 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
914 b->unref();
915
916 // Still have two resources because a is still reffed.
917 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
918 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
919 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
920
921 a->unref();
922 // Now a should be gone.
923 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
924 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
925 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
926
927 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
928 // Also make b be unreffed when replacement occurs.
929 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400930 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800931 GrUniqueKey differentKey;
932 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800933 c->resourcePriv().setUniqueKey(differentKey);
934 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
935 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
936 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
937 // c replaces b and b should be immediately purged.
938 c->resourcePriv().setUniqueKey(key);
939 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
940 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
941 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
942
943 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800944 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800945 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
946 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
947 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
948
949 // Drop the ref on c, it should be kept alive because it has a unique key.
950 c->unref();
951 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
952 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
953 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
954
955 // Verify that we can find c, then remove its unique key. It should get purged immediately.
956 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
957 c->resourcePriv().removeUniqueKey();
958 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800959 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
960 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800961 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700962
963 {
964 GrUniqueKey key2;
965 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500966 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700967 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700968 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700969 d->resourcePriv().setUniqueKey(key2);
970 }
971
972 GrUniqueKey key3;
973 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400974 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700975 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000976}
977
bsalomon8b79d232014-11-10 10:19:06 -0800978static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800979 Mock mock(5, 30000);
980 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800981 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500982 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800983
bsalomon8718aaf2015-02-19 07:24:21 -0800984 GrUniqueKey key1, key2, key3;
985 make_unique_key<0>(&key1, 1);
986 make_unique_key<0>(&key2, 2);
987 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700988
bsalomon23e619c2015-02-06 11:54:28 -0800989 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500990 TestResource* a = new TestResource(gpu);
991 TestResource* b = new TestResource(gpu);
992 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800993 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800994 a->resourcePriv().setUniqueKey(key1);
995 b->resourcePriv().setUniqueKey(key2);
996 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800997 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800998 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800999 c->unref();
1000
bsalomon8718aaf2015-02-19 07:24:21 -08001001 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1002 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1003 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001004 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001005
bsalomon8718aaf2015-02-19 07:24:21 -08001006 typedef GrUniqueKeyInvalidatedMessage Msg;
1007 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001008
1009 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Brian Salomon238069b2018-07-11 15:58:57 -04001010 Bus::Post(Msg(key1, context->uniqueID()));
1011 Bus::Post(Msg(key2, context->uniqueID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001012 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001013 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001014 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1015 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001016 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001017 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001018
1019 // Invalidate the third.
Brian Salomon238069b2018-07-11 15:58:57 -04001020 Bus::Post(Msg(key3, context->uniqueID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001021 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001022 // we still have a ref on b, c should be recycled as scratch.
1023 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001024 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001025
bsalomon23e619c2015-02-06 11:54:28 -08001026 // make b purgeable. It should be immediately deleted since it has no key.
1027 b->unref();
1028 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1029
1030 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1031 GrScratchKey scratchKey;
1032 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -07001033 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -08001034 REPORTER_ASSERT(reporter, scratch == c);
1035 SkSafeUnref(scratch);
1036
1037 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001038 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -07001039 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -08001040 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001041 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1042 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001043 REPORTER_ASSERT(reporter, !scratch);
1044 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001045}
1046
bsalomon71cb0c22014-11-14 12:10:14 -08001047static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001048 Mock mock(3, 30000);
1049 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001050 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001051 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001052
bsalomon8718aaf2015-02-19 07:24:21 -08001053 GrUniqueKey key1, key2;
1054 make_unique_key<0>(&key1, 1);
1055 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001056
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001057 sk_sp<TestResource> a(new TestResource(gpu));
1058 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001059 a->resourcePriv().setUniqueKey(key1);
1060 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001061
bsalomonc2f35b72015-01-23 07:19:22 -08001062 // Make a cycle
1063 a->setUnrefWhenDestroyed(b);
1064 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001065
bsalomonc2f35b72015-01-23 07:19:22 -08001066 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001067
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001068 TestResource* unownedA = a.release();
1069 unownedA->unref();
1070 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001071
bsalomonc2f35b72015-01-23 07:19:22 -08001072 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001073
bsalomon0ea80f42015-02-11 10:49:59 -08001074 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001075 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001076
bsalomonc2f35b72015-01-23 07:19:22 -08001077 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001078 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001079 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001080
bsalomon0ea80f42015-02-11 10:49:59 -08001081 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001082 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001083}
1084
bsalomonddf30e62015-02-19 11:38:44 -08001085static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1086 static const int kCount = 50;
1087 static const int kBudgetCnt = kCount / 2;
1088 static const int kLockedFreq = 8;
1089 static const int kBudgetSize = 0x80000000;
1090
1091 SkRandom random;
1092
1093 // Run the test 2*kCount times;
1094 for (int i = 0; i < 2 * kCount; ++i ) {
1095 Mock mock(kBudgetCnt, kBudgetSize);
1096 GrContext* context = mock.context();
1097 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001098 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001099
1100 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001101 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001102
1103 static const int kNumToPurge = kCount - kBudgetCnt;
1104
1105 SkTDArray<int> shouldPurgeIdxs;
1106 int purgeableCnt = 0;
1107 SkTDArray<GrGpuResource*> resourcesToUnref;
1108
1109 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1110 // unpurgeable resources.
1111 for (int j = 0; j < kCount; ++j) {
1112 GrUniqueKey key;
1113 make_unique_key<0>(&key, j);
1114
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001115 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001116 r->resourcePriv().setUniqueKey(key);
1117 if (random.nextU() % kLockedFreq) {
1118 // Make this is purgeable.
1119 r->unref();
1120 ++purgeableCnt;
1121 if (purgeableCnt <= kNumToPurge) {
1122 *shouldPurgeIdxs.append() = j;
1123 }
1124 } else {
1125 *resourcesToUnref.append() = r;
1126 }
1127 }
1128
1129 // Verify that the correct resources were purged.
1130 int currShouldPurgeIdx = 0;
1131 for (int j = 0; j < kCount; ++j) {
1132 GrUniqueKey key;
1133 make_unique_key<0>(&key, j);
1134 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1135 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1136 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1137 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001138 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001139 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001140 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001141 }
1142 SkSafeUnref(res);
1143 }
1144
1145 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1146 resourcesToUnref[j]->unref();
1147 }
1148 }
1149}
1150
bsalomon3f324322015-04-08 11:01:54 -07001151static void test_flush(skiatest::Reporter* reporter) {
1152 Mock mock(1000000, 1000000);
1153 GrContext* context = mock.context();
1154 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001155 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon3f324322015-04-08 11:01:54 -07001156
1157 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1158 // power of two here to keep things simpler.
1159 static const int kFlushCount = 16;
1160 cache->setLimits(1000000, 1000000, kFlushCount);
1161
1162 {
1163 // Insert a resource and send a flush notification kFlushCount times.
1164 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001165 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001166 GrUniqueKey k;
1167 make_unique_key<1>(&k, i);
1168 r->resourcePriv().setUniqueKey(k);
1169 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001170 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001171 }
1172
1173 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001174 for (int i = 0; i < kFlushCount; ++i) {
1175 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001176 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1177 for (int j = 0; j < i; ++j) {
1178 GrUniqueKey k;
1179 make_unique_key<1>(&k, j);
1180 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1181 REPORTER_ASSERT(reporter, !SkToBool(r));
1182 SkSafeUnref(r);
1183 }
bsalomon3f324322015-04-08 11:01:54 -07001184 }
1185
1186 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1187 cache->purgeAllUnlocked();
1188 }
1189
1190 // Do a similar test but where we leave refs on some resources to prevent them from being
1191 // purged.
1192 {
1193 GrGpuResource* refedResources[kFlushCount >> 1];
1194 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001195 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001196 GrUniqueKey k;
1197 make_unique_key<1>(&k, i);
1198 r->resourcePriv().setUniqueKey(k);
1199 // Leave a ref on every other resource, beginning with the first.
1200 if (SkToBool(i & 0x1)) {
1201 refedResources[i/2] = r;
1202 } else {
1203 r->unref();
1204 }
bsalomonb77a9072016-09-07 10:02:04 -07001205 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001206 }
1207
1208 for (int i = 0; i < kFlushCount; ++i) {
1209 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001210 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001211 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001212 }
1213
1214 // Unref all the resources that we kept refs on in the first loop.
1215 for (int i = 0; i < kFlushCount >> 1; ++i) {
1216 refedResources[i]->unref();
1217 }
1218
bsalomone2e87f32016-09-22 12:42:11 -07001219 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1220 // kFlushCount full flushes.
1221 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001222 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001223 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001224 }
1225 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1226
1227 cache->purgeAllUnlocked();
1228 }
1229
1230 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001231
1232 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1233 // eviction.
1234 context->flush();
1235 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001236 TestResource* r = new TestResource(gpu);
bsalomondc438982016-08-31 11:53:49 -07001237 GrUniqueKey k;
1238 make_unique_key<1>(&k, i);
1239 r->resourcePriv().setUniqueKey(k);
1240 r->unref();
1241 }
1242 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1243 for (int i = 0; i < 10 * kFlushCount; ++i) {
1244 context->flush();
1245 }
1246 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001247}
1248
Brian Salomon5e150852017-03-22 14:53:13 -04001249static void test_time_purge(skiatest::Reporter* reporter) {
1250 Mock mock(1000000, 1000000);
1251 GrContext* context = mock.context();
1252 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001253 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001254
1255 static constexpr int kCnts[] = {1, 10, 1024};
1256 auto nowish = []() {
1257 // We sleep so that we ensure we get a value that is greater than the last call to
1258 // GrStdSteadyClock::now().
1259 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1260 auto result = GrStdSteadyClock::now();
1261 // Also sleep afterwards so we don't get this value again.
1262 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1263 return result;
1264 };
1265
1266 for (int cnt : kCnts) {
1267 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1268 new GrStdSteadyClock::time_point[cnt]);
1269 {
1270 // Insert resources and get time points between each addition.
1271 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001272 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001273 GrUniqueKey k;
1274 make_unique_key<1>(&k, i);
1275 r->resourcePriv().setUniqueKey(k);
1276 r->unref();
1277 timeStamps.get()[i] = nowish();
1278 }
1279
1280 // Purge based on the time points between resource additions. Each purge should remove
1281 // the oldest resource.
1282 for (int i = 0; i < cnt; ++i) {
1283 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1284 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1285 for (int j = 0; j < i; ++j) {
1286 GrUniqueKey k;
1287 make_unique_key<1>(&k, j);
1288 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1289 REPORTER_ASSERT(reporter, !SkToBool(r));
1290 SkSafeUnref(r);
1291 }
1292 }
1293
1294 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1295 cache->purgeAllUnlocked();
1296 }
1297
1298 // Do a similar test but where we leave refs on some resources to prevent them from being
1299 // purged.
1300 {
1301 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1302 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001303 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001304 GrUniqueKey k;
1305 make_unique_key<1>(&k, i);
1306 r->resourcePriv().setUniqueKey(k);
1307 // Leave a ref on every other resource, beginning with the first.
1308 if (SkToBool(i & 0x1)) {
1309 refedResources.get()[i / 2] = r;
1310 } else {
1311 r->unref();
1312 }
1313 timeStamps.get()[i] = nowish();
1314 }
1315
1316 for (int i = 0; i < cnt; ++i) {
1317 // Should get a resource purged every other frame.
1318 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1319 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1320 }
1321
1322 // Unref all the resources that we kept refs on in the first loop.
1323 for (int i = 0; i < (cnt / 2); ++i) {
1324 refedResources.get()[i]->unref();
1325 cache->purgeResourcesNotUsedSince(nowish());
1326 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1327 }
1328
1329 cache->purgeAllUnlocked();
1330 }
1331
1332 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1333
1334 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1335 // eviction
1336 context->flush();
1337 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001338 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001339 GrUniqueKey k;
1340 make_unique_key<1>(&k, i);
1341 r->resourcePriv().setUniqueKey(k);
1342 r->unref();
1343 }
1344 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1345 context->flush();
1346 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1347 cache->purgeResourcesNotUsedSince(nowish());
1348 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1349 }
1350}
1351
Derek Sollenberger5480a182017-05-25 16:43:59 -04001352static void test_partial_purge(skiatest::Reporter* reporter) {
1353 Mock mock(6, 100);
1354 GrContext* context = mock.context();
1355 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001356 GrGpu* gpu = context->contextPriv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001357
1358 enum TestsCase {
1359 kOnlyScratch_TestCase = 0,
1360 kPartialScratch_TestCase = 1,
1361 kAllScratch_TestCase = 2,
1362 kPartial_TestCase = 3,
1363 kAll_TestCase = 4,
1364 kNone_TestCase = 5,
1365 kEndTests_TestCase = kNone_TestCase + 1
1366 };
1367
1368 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1369
1370 GrUniqueKey key1, key2, key3;
1371 make_unique_key<0>(&key1, 1);
1372 make_unique_key<0>(&key2, 2);
1373 make_unique_key<0>(&key3, 3);
1374
1375 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001376 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1377 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1378 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001379
1380 unique1->resourcePriv().setUniqueKey(key1);
1381 unique2->resourcePriv().setUniqueKey(key2);
1382 unique3->resourcePriv().setUniqueKey(key3);
1383
Derek Sollenberger5480a182017-05-25 16:43:59 -04001384 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001385 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001386 TestResource::kA_SimulatedProperty,
1387 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001388 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001389 TestResource::kB_SimulatedProperty,
1390 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001391
1392 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1393 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1394 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1395
1396 // Add resources to the purgeable queue
1397 unique1->unref();
1398 scratch1->unref();
1399 unique2->unref();
1400 scratch2->unref();
1401 unique3->unref();
1402
1403 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1404 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1405 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1406
1407 switch(testCase) {
1408 case kOnlyScratch_TestCase: {
1409 context->purgeUnlockedResources(14, true);
1410 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1411 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1412 break;
1413 }
1414 case kPartialScratch_TestCase: {
1415 context->purgeUnlockedResources(3, true);
1416 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1417 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1418 break;
1419 }
1420 case kAllScratch_TestCase: {
1421 context->purgeUnlockedResources(50, true);
1422 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1423 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1424 break;
1425 }
1426 case kPartial_TestCase: {
1427 context->purgeUnlockedResources(13, false);
1428 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1429 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1430 break;
1431 }
1432 case kAll_TestCase: {
1433 context->purgeUnlockedResources(50, false);
1434 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1435 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1436 break;
1437 }
1438 case kNone_TestCase: {
1439 context->purgeUnlockedResources(0, true);
1440 context->purgeUnlockedResources(0, false);
1441 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1442 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1443 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1444 break;
1445 }
1446 };
1447
1448 // ensure all are purged before the next
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001449 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001450 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1451 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1452
1453 }
1454}
1455
bsalomon10e23ca2014-11-25 05:52:06 -08001456static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001457 // Set the cache size to double the resource count because we're going to create 2x that number
1458 // resources, using two different key domains. Add a little slop to the bytes because we resize
1459 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001460 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001461
bsalomonc2f35b72015-01-23 07:19:22 -08001462 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1463 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001464 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001465 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001466
1467 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001468 GrUniqueKey key1, key2;
1469 make_unique_key<1>(&key1, i);
1470 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001471
bsalomon24db3b12015-01-23 04:24:04 -08001472 TestResource* resource;
1473
Greg Danielda86e282018-06-13 09:41:19 -04001474 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001475 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001476 resource->unref();
1477
Greg Danielda86e282018-06-13 09:41:19 -04001478 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001479 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001480 resource->unref();
1481 }
1482
1483 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001484 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001485 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1486 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1487 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1488 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001489 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001490 GrUniqueKey key1, key2;
1491 make_unique_key<1>(&key1, i);
1492 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001493
bsalomon8718aaf2015-02-19 07:24:21 -08001494 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1495 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001496 }
1497
bsalomon0ea80f42015-02-11 10:49:59 -08001498 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001499 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001500 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001501 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1502 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1503 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1504 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001505
1506 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001507 GrUniqueKey key1, key2;
1508 make_unique_key<1>(&key1, i);
1509 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001510
bsalomon8718aaf2015-02-19 07:24:21 -08001511 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1512 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001513 }
1514}
1515
senorblanco84cd6212015-08-04 10:01:58 -07001516static void test_custom_data(skiatest::Reporter* reporter) {
1517 GrUniqueKey key1, key2;
1518 make_unique_key<0>(&key1, 1);
1519 make_unique_key<0>(&key2, 2);
1520 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001521 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001522 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1523 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1524
1525 // Test that copying a key also takes a ref on its custom data.
1526 GrUniqueKey key3 = key1;
1527 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1528}
1529
bsalomonc6363ef2015-09-24 07:07:40 -07001530static void test_abandoned(skiatest::Reporter* reporter) {
1531 Mock mock(10, 300);
1532 GrContext* context = mock.context();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001533 GrGpu* gpu = context->contextPriv().getGpu();
1534
1535 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001536 context->abandonContext();
1537
1538 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1539
1540 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1541
robertphillips8abb3702016-08-31 14:04:06 -07001542 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001543 resource->getUniqueKey();
1544 resource->wasDestroyed();
1545 resource->gpuMemorySize();
1546 resource->getContext();
1547
1548 resource->abandon();
1549 resource->resourcePriv().getScratchKey();
1550 resource->resourcePriv().isBudgeted();
1551 resource->resourcePriv().makeBudgeted();
1552 resource->resourcePriv().makeUnbudgeted();
1553 resource->resourcePriv().removeScratchKey();
1554 GrUniqueKey key;
1555 make_unique_key<0>(&key, 1);
1556 resource->resourcePriv().setUniqueKey(key);
1557 resource->resourcePriv().removeUniqueKey();
1558}
1559
Brian Salomon1090da62017-01-06 12:04:19 -05001560static void test_tags(skiatest::Reporter* reporter) {
1561#ifdef SK_DEBUG
1562 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1563 static constexpr int kLastTagIdx = 10;
1564 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1565
1566 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1567 GrContext* context = mock.context();
1568 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001569 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001570
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001571 // tag strings are expected to be long lived
1572 std::vector<SkString> tagStrings;
1573
Brian Salomon1090da62017-01-06 12:04:19 -05001574 SkString tagStr;
1575 int tagIdx = 0;
1576 int currTagCnt = 0;
1577
1578 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001579
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001580 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001581 GrUniqueKey key;
1582 if (currTagCnt == tagIdx) {
1583 tagIdx += 1;
1584 currTagCnt = 0;
1585 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001586 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001587 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001588 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001589 resource->resourcePriv().setUniqueKey(key);
1590 }
1591 SkASSERT(kLastTagIdx == tagIdx);
1592 SkASSERT(currTagCnt == kLastTagIdx);
1593
1594 // Test i = 0 to exercise unused tag string.
1595 for (int i = 0; i <= kLastTagIdx; ++i) {
1596 tagStr.printf("tag%d", i);
1597 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1598 }
1599#endif
1600}
1601
Greg Danielc27eb722018-08-10 09:48:08 -04001602static void test_free_resource_messages(skiatest::Reporter* reporter) {
1603 Mock mock(10, 30000);
1604 GrContext* context = mock.context();
1605 GrResourceCache* cache = mock.cache();
1606 GrGpu* gpu = context->contextPriv().getGpu();
1607
1608 TestResource* wrapped1 = TestResource::CreateWrapped(gpu, 12);
1609 cache->insertCrossContextGpuResource(wrapped1);
1610
1611 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1612
1613 TestResource* wrapped2 = TestResource::CreateWrapped(gpu, 12);
1614 cache->insertCrossContextGpuResource(wrapped2);
1615
1616 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1617
1618 // Have only ref waiting on message.
1619 wrapped1->unref();
1620 wrapped2->unref();
1621
1622 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1623
1624 // This should free nothing since no messages were sent.
1625 cache->purgeAsNeeded();
1626
1627 // Send message to free the first resource
1628 GrGpuResourceFreedMessage msg { wrapped1, context->uniqueID() };
1629 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
1630 cache->purgeAsNeeded();
1631
1632 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1633
1634 mock.reset();
1635
1636 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
1637}
1638
1639
Brian Salomondcfca432017-11-15 15:48:03 -05001640DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001641 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001642 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001643 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001644 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001645 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001646 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001647 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001648 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001649 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001650 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001651 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001652 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001653 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001654 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001655 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001656 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001657 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001658 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001659 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001660 test_tags(reporter);
Greg Danielc27eb722018-08-10 09:48:08 -04001661 test_free_resource_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001662}
1663
Robert Phillipsd6214d42016-11-07 08:23:48 -05001664////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001665static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001666 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001667 int width, int height,
1668 int sampleCnt) {
1669 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001670 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001671 desc.fWidth = width;
1672 desc.fHeight = height;
1673 desc.fConfig = kRGBA_8888_GrPixelConfig;
1674 desc.fSampleCnt = sampleCnt;
1675
Robert Phillipse78b7252017-04-06 07:59:41 -04001676 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001677}
1678
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001679static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001680 GrSurfaceDescFlags descFlags,
Robert Phillipse78b7252017-04-06 07:59:41 -04001681 int width, int height,
1682 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001683 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001684 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001685 desc.fWidth = width;
1686 desc.fHeight = height;
1687 desc.fConfig = kRGBA_8888_GrPixelConfig;
1688 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001689
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001690 auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1691 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001692
1693 return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001694}
1695
1696// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1697// Texture-only, both-RT-and-Texture and MIPmapped
1698DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1699 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001700 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001701 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001702
Robert Phillipsd6214d42016-11-07 08:23:48 -05001703 static const int kSize = 64;
1704
Robert Phillipsd6214d42016-11-07 08:23:48 -05001705 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001706 {
1707 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001708
Brian Salomonbdecacf2018-02-02 20:32:49 -05001709 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001710 size_t size = tex->gpuMemorySize();
1711 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1712
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001713 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1714 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001715 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001716 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001717 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001718 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001719 REPORTER_ASSERT(reporter,
1720 kSize*kSize*4 == size || // msaa4 failed
1721 kSize*kSize*4*sampleCount == size || // auto-resolving
1722 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001723 }
1724
Brian Salomonbdecacf2018-02-02 20:32:49 -05001725 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001726 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001727 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001728 }
1729
Robert Phillipsd6214d42016-11-07 08:23:48 -05001730
1731 // Mipmapped versions
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001732 if (context->contextPriv().caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001733 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001734
Brian Salomonbdecacf2018-02-02 20:32:49 -05001735 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001736 size_t size = proxy->gpuMemorySize();
1737 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1738
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001739 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1740 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001741 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001742 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001743 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001744 size = proxy->gpuMemorySize();
1745 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001746 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1747 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1748 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001749 }
Robert Phillips1b352562017-04-05 18:56:21 +00001750
Brian Salomonbdecacf2018-02-02 20:32:49 -05001751 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001752 size = proxy->gpuMemorySize();
1753 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1754 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001755}