blob: 857286ab6c319e812a7a4638b5f43d1fe9930a2d [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 here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
Brian Salomon5e150852017-03-22 14:53:13 -040012#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000014#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000015#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080017#include "GrGpuResourceCacheAccess.h"
18#include "GrGpuResourcePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040020#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070023#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
25
bsalomonbcf0a522014-10-08 08:40:09 -070026#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027#include "SkGr.h"
28#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050029#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070030#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000031#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032
33static const int gWidth = 640;
34static const int gHeight = 480;
35
36////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070038 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080039 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040040 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080041 desc.fFlags = kRenderTarget_GrSurfaceFlag;
42 desc.fWidth = gWidth;
43 desc.fHeight = gHeight;
44 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070045 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080046 SkCanvas* canvas = surface->getCanvas();
47
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
49
50 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000051 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000052 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040053 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000055 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070056 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 int oldMaxNum;
59 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000060 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000061
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062 // Set the cache limits so we can fit 10 "src" images and the
63 // max number of textures doesn't matter
64 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000065 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000068 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000069
70 for (int i = 0; i < 100; ++i) {
71 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040072 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000073
74 // "modify" the src texture
75 src.notifyPixelsChanged();
76
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000077 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070078 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000079
80 // we should never go over the size limit
81 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
82 }
83
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000084 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000085}
86
bsalomon11abd8d2016-10-14 08:13:48 -070087static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
88 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
89 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
90 return false;
91 }
92 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
93}
94
Robert Phillipsc0192e32017-09-21 12:00:26 -040095static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
96 return rt->renderTargetPriv().getStencilAttachment();
97}
98
99static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
100 int size, int sampleCount, SkBudgeted budgeted) {
101 GrSurfaceDesc desc;
102 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc0192e32017-09-21 12:00:26 -0400103 desc.fWidth = size;
104 desc.fHeight = size;
105 desc.fConfig = kRGBA_8888_GrPixelConfig;
106 desc.fSampleCnt = sampleCount;
107
108 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
109 if (!tex || !tex->asRenderTarget()) {
110 return nullptr;
111 }
112
113 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
114 return nullptr;
115 }
116 SkASSERT(get_SB(tex->asRenderTarget()));
117
118 return sk_ref_sp(tex->asRenderTarget());
119}
120
bsalomon11abd8d2016-10-14 08:13:48 -0700121// This currently fails on ES3 ANGLE contexts
122DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000123 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700124 GrContext* context = ctxInfo.grContext();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400125 if (context->contextPriv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700126 return;
127 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128
Robert Phillips6be756b2018-01-16 15:07:54 -0500129 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400130
Brian Salomonbdecacf2018-02-02 20:32:49 -0500131 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400132 REPORTER_ASSERT(reporter, smallRT0);
133
134 {
135 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
137 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400138
Brian Salomonbdecacf2018-02-02 20:32:49 -0500139 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 }
141
Robert Phillipsc0192e32017-09-21 12:00:26 -0400142 {
143 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400145 REPORTER_ASSERT(reporter, smallRT2);
146
147 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800148 }
149
Robert Phillipsc0192e32017-09-21 12:00:26 -0400150 {
151 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500152 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400153 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800154
Robert Phillipsc0192e32017-09-21 12:00:26 -0400155 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 }
bsalomon02a44a42015-02-19 09:09:00 -0800157
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400158 int smallSampleCount =
159 context->contextPriv().caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500160 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700161 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500162 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
163 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800164#ifdef SK_BUILD_FOR_ANDROID
165 if (!smallMSAART0) {
166 // The nexus player seems to fail to create MSAA textures.
167 return;
168 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400169#else
170 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800171#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400172
173 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
174
175 {
176 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500177 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
178 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400179 SkBudgeted::kNo);
180 REPORTER_ASSERT(reporter, smallMSAART1);
181
182 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800183 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400184
Brian Salomonbdecacf2018-02-02 20:32:49 -0500185 // But one with a larger sample count should not. (Also check that the two requests didn't
186 // rounded up to the same actual sample count or else they could share.).
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400187 int bigSampleCount = context->contextPriv().caps()->getRenderTargetSampleCount(
188 5, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500189 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500190 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
191 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400192 SkBudgeted::kNo);
193 REPORTER_ASSERT(reporter, smallMSAART2);
194
195 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800196 }
197 }
198}
199
bsalomon68d91342016-04-12 09:59:58 -0700200DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700201 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500202 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500203 GrGpu* gpu = context->contextPriv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700204 // this test is only valid for GL
205 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700206 return;
207 }
208
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500209 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700210 static const int kW = 100;
211 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700212
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500213 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
214 kRGBA_8888_GrPixelConfig,
215 false, GrMipMapped::kNo);
216 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
217 kRGBA_8888_GrPixelConfig,
218 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500219 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
220 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
221 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
222 return;
223 }
jvanverth672bb7f2015-07-13 07:19:57 -0700224
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225 context->resetContext();
226
Robert Phillips6be756b2018-01-16 15:07:54 -0500227 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
Robert Phillips6be756b2018-01-16 15:07:54 -0500230 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500231 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
Brian Osman85d34b22017-05-10 12:06:26 -0400233 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
234 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700235 return;
236 }
237
halcanary96fcdcc2015-08-27 07:41:13 -0700238 borrowed.reset(nullptr);
239 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
241 context->flush();
242
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500243 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
244 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700245
246 REPORTER_ASSERT(reporter, borrowedIsAlive);
247 REPORTER_ASSERT(reporter, !adoptedIsAlive);
248
Brian Salomone64b0642018-03-07 11:47:54 -0500249 if (borrowedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500250 gpu->deleteTestingOnlyBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500251 }
252 if (adoptedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500253 gpu->deleteTestingOnlyBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500254 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700255
256 context->resetContext();
257}
258
bsalomon6d3fe022014-07-25 08:35:45 -0700259class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800260 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000261public:
robertphillips6e83ac72015-08-13 05:19:14 -0700262 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700263
bsalomon1c60dfe2015-01-21 09:32:40 -0800264 /** Property that distinctly categorizes the resource.
265 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800266 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800267
kkinnunen2e6055b2016-04-22 01:48:29 -0700268 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
269 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700270 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800271 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700272 , fProperty(kA_SimulatedProperty)
273 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800274 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700275 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800276 }
277
kkinnunen2e6055b2016-04-22 01:48:29 -0700278 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
279 SimulatedProperty property) {
280 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800281 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700282 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
283 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284 }
285
Brian Salomond3b65972017-03-22 12:05:03 -0400286 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800287 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800288 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289 }
290
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000291 void setSize(size_t size) {
292 fSize = size;
293 this->didChangeGpuMemorySize();
294 }
295
bsalomon33435572014-11-05 14:47:41 -0800296 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000297
bsalomon71cb0c22014-11-14 12:10:14 -0800298 void setUnrefWhenDestroyed(TestResource* resource) {
299 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000300 }
301
bsalomon1c60dfe2015-01-21 09:32:40 -0800302 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
303 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
304 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800305 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
306 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800307 }
308 }
309
310 static size_t ExpectedScratchKeySize() {
311 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
312 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000313private:
bsalomon24db3b12015-01-23 04:24:04 -0800314 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800315
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
317 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700318 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800319 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700320 , fProperty(property)
321 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800322 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700323 this->registerWithCache(budgeted);
324 }
325
326 // Constructor for simulating resources that wrap backend objects.
327 TestResource(GrGpu* gpu, size_t size)
328 : INHERITED(gpu)
329 , fToDelete(nullptr)
330 , fSize(size)
331 , fProperty(kA_SimulatedProperty)
332 , fIsScratch(false) {
333 ++fNumAlive;
334 this->registerWithCacheWrapped();
335 }
336
337 void computeScratchKey(GrScratchKey* key) const override {
338 if (fIsScratch) {
339 ComputeScratchKey(fProperty, key);
340 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800341 }
342
mtklein36352bf2015-03-25 18:17:31 -0700343 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400344 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800345
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000346 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000347 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800348 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800349 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700350 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700351 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000352};
bsalomon33435572014-11-05 14:47:41 -0800353int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000354
bsalomonc2f35b72015-01-23 07:19:22 -0800355class Mock {
356public:
357 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400358 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800359 SkASSERT(fContext);
360 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500361 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800362 cache->purgeAllUnlocked();
363 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800364 }
bsalomonc2f35b72015-01-23 07:19:22 -0800365
Robert Phillips6be756b2018-01-16 15:07:54 -0500366 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800367
Hal Canary342b7ac2016-11-04 11:49:42 -0400368 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800369
370private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400371 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800372};
373
374static void test_no_key(skiatest::Reporter* reporter) {
375 Mock mock(10, 30000);
376 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800377 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500378 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800379
380 // Create a bunch of resources with no keys
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500381 TestResource* a = new TestResource(gpu);
382 TestResource* b = new TestResource(gpu);
383 TestResource* c = new TestResource(gpu);
384 TestResource* d = new TestResource(gpu);
bsalomon71cb0c22014-11-14 12:10:14 -0800385 a->setSize(11);
386 b->setSize(12);
387 c->setSize(13);
388 d->setSize(14);
389
390 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800392 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800393 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800396 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800397 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
398
bsalomon8718aaf2015-02-19 07:24:21 -0800399 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 a->unref();
402 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800405 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 c->unref();
408 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800410 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800411 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800412
413 d->unref();
414 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800415 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
416 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800417
418 b->unref();
419 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800420 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
421 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800422}
423
bsalomon24db3b12015-01-23 04:24:04 -0800424// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500425template <int>
426static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800427 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500428 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800429 builder[0] = data;
430}
431
Robert Phillips6eba0632018-03-28 12:25:42 -0400432static void test_purge_unlocked(skiatest::Reporter* reporter) {
433 Mock mock(10, 30000);
434 GrContext* context = mock.context();
435 GrResourceCache* cache = mock.cache();
436 GrGpu* gpu = context->contextPriv().getGpu();
437
438 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
439 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
440 TestResource::kA_SimulatedProperty);
441 a->setSize(11);
442
443 GrUniqueKey uniqueKey;
444 make_unique_key<0>(&uniqueKey, 0);
445
446 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
447 TestResource::kA_SimulatedProperty);
448 b->setSize(12);
449 b->resourcePriv().setUniqueKey(uniqueKey);
450
451 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
452 TestResource::kA_SimulatedProperty);
453 c->setSize(13);
454
455 GrUniqueKey uniqueKey2;
456 make_unique_key<0>(&uniqueKey2, 1);
457
458 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
459 TestResource::kA_SimulatedProperty);
460 d->setSize(14);
461 d->resourcePriv().setUniqueKey(uniqueKey2);
462
463
464 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
465 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
466 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
467 d->gpuMemorySize() == cache->getResourceBytes());
468
469 // Should be safe to purge without deleting the resources since we still have refs.
470 cache->purgeUnlockedResources(false);
471 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
472
473 // Unref them all. Since they all have keys they should remain in the cache.
474
475 a->unref();
476 b->unref();
477 c->unref();
478 d->unref();
479 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
480 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
482 d->gpuMemorySize() == cache->getResourceBytes());
483
484 // Purge only the two scratch resources
485 cache->purgeUnlockedResources(true);
486
487 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
488 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
489 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
490 cache->getResourceBytes());
491
492 // Purge the uniquely keyed resources
493 cache->purgeUnlockedResources(false);
494
495 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
496 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
497 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
498}
499
bsalomon84c8e622014-11-17 09:33:27 -0800500static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800501 Mock mock(10, 300);
502 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800503 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500504 GrGpu* gpu = context->contextPriv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800505
bsalomon8718aaf2015-02-19 07:24:21 -0800506 GrUniqueKey uniqueKey;
507 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800508
bsalomon8718aaf2015-02-19 07:24:21 -0800509 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800510 TestResource* scratch =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500511 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800512 scratch->setSize(10);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500513 TestResource* unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800514 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800515 unique->resourcePriv().setUniqueKey(uniqueKey);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500516 TestResource* wrapped = TestResource::CreateWrapped(gpu);
bsalomon5236cf42015-01-14 10:42:08 -0800517 wrapped->setSize(12);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500518 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800519 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800520
Brian Osman0562eb92017-05-08 11:16:39 -0400521 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800522 GrUniqueKey uniqueKey2;
523 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800524 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400525 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
526 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
527
528 // Remove the extra ref we just added.
529 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800530
531 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800533 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800534 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800535 cache->getResourceBytes());
536 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800537 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800538 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400539 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800540
bsalomon63c992f2015-01-23 12:47:59 -0800541 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800542 cache->purgeAllUnlocked();
543 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800544 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800545 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800546 cache->getResourceBytes());
547 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800548 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800549 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400550 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800551
552 // Unreffing the wrapped resource should free it right away.
553 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800554 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800555 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800556 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400557 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800558
bsalomon84c8e622014-11-17 09:33:27 -0800559 // Now try freeing the budgeted resources first
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500560 wrapped = TestResource::CreateWrapped(gpu);
bsalomondace19e2014-11-17 07:34:06 -0800561 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800562 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400563 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800564 cache->purgeAllUnlocked();
565 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800566 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800567 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
568 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
569 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400570 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800571
572 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400573 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800574 cache->purgeAllUnlocked();
575 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800576 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800577 cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400580 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800581
582 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800583 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
584 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
586 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400587 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800588
589 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800590 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
591 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
592 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
593 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400594 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800595}
596
bsalomon5236cf42015-01-14 10:42:08 -0800597static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800598 Mock mock(10, 30000);
599 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800600 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500601 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800602
bsalomon8718aaf2015-02-19 07:24:21 -0800603 GrUniqueKey uniqueKey;
604 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800605
606 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800607 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800608 TestResource* wrapped;
609 TestResource* unbudgeted;
610
611 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500612 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
kkinnunen2e6055b2016-04-22 01:48:29 -0700613 TestResource::kB_SimulatedProperty);
614
bsalomon5236cf42015-01-14 10:42:08 -0800615 scratch->setSize(10);
616 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800617 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
618 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
619 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
620 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400621 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800622
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500623 unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800624 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800625 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800626 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800627 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
628 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
629 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
630 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400631 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800632
bsalomon0ea80f42015-02-11 10:49:59 -0800633 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500634 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800635 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
636 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
637 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
638 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400639 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800640
641 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800642 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
643 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
644 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
645 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400646 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800647
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500648 wrapped = TestResource::CreateWrapped(gpu, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800649 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
650 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
651 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
652 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400653 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800654
655 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800656 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
657 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
658 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
659 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400660 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800661
bsalomon0ea80f42015-02-11 10:49:59 -0800662 cache->purgeAllUnlocked();
663 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
664 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
665 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
666 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400667 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800668}
669
bsalomon3582d3e2015-02-13 14:20:05 -0800670// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
671void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
672/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800673 Mock mock(10, 300);
674 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800675 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500676 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800677
678 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500679 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800680 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800681 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800682
683 size_t size = resource->gpuMemorySize();
684 for (int i = 0; i < 2; ++i) {
685 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800686 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800687 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800688 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700689 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800690 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
691 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
692 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
693 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400694 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800695
696 // Once it is unrefed, it should become available as scratch.
697 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800698 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
699 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
700 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
701 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400702 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700703 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800704 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800705 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800706 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800707 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800708
709 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700710 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800711 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800712 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800713 } else {
714 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800715 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800716 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
717 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
718 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
719 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400720 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800721 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800722 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800723 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800724
725 // now when it is unrefed it should die since it has no key.
726 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800727 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
728 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
729 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
730 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400731 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800732 }
bsalomon8b79d232014-11-10 10:19:06 -0800733 }
bsalomonc2f35b72015-01-23 07:19:22 -0800734}
735
736static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
737 Mock mock(5, 30000);
738 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800739 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500740 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800741
bsalomon8b79d232014-11-10 10:19:06 -0800742 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500743 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700744 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800745 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500746 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700747 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800748 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800749 a->setSize(11);
750 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800751 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800752 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800753 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700754 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800755
756 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800757 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758
bsalomon0ea80f42015-02-11 10:49:59 -0800759 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800760 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
762 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800763 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800764 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800765
bsalomon63c992f2015-01-23 12:47:59 -0800766 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800767 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800768 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800769 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800770
771 // Unref but don't purge
772 a->unref();
773 b->unref();
774 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800775 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800776
bsalomon63c992f2015-01-23 12:47:59 -0800777 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800778 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800779 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800780 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
781 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800782}
783
bsalomon10e23ca2014-11-25 05:52:06 -0800784static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800785 Mock mock(5, 30000);
786 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800787 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500788 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800789
bsalomon10e23ca2014-11-25 05:52:06 -0800790 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500791 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500793 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800794 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800795 a->unref();
796 b->unref();
797
bsalomon1c60dfe2015-01-21 09:32:40 -0800798 GrScratchKey scratchKey;
799 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800800 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800801 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700802 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800803
bsalomon0ea80f42015-02-11 10:49:59 -0800804 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800805 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800806 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800807 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
808 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800809
810 // Find the first resource and remove its scratch key
811 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700812 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800813 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800814 // It's still alive, but not cached by scratch key anymore
815 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800816 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
817 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800818
819 // The cache should immediately delete it when it's unrefed since it isn't accessible.
820 find->unref();
821 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800822 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
823 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800824
825 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700826 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800827 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800828 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800829 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
830 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800831
832 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800833 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800834 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800835 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
836 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800837
838 find->unref();
839 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800840 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
841 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800842}
843
bsalomon1c60dfe2015-01-21 09:32:40 -0800844static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800845 Mock mock(5, 30000);
846 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800847 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500848 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800849
850 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500851 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800852 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500853 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800854 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800855 a->unref();
856 b->unref();
857
858 GrScratchKey scratchKey;
859 // Ensure that scratch key comparison and assignment is consistent.
860 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800861 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800862 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800863 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800864 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
865 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
866 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
867 scratchKey = scratchKey1;
868 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
869 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
870 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
871 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
872 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
873 scratchKey = scratchKey2;
874 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
875 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
876 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
877 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
878 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
879
880 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800881 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800882 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700883 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800884
885 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800886 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700887 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700888 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800889 find->unref();
890
891 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700892 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700893 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800894 REPORTER_ASSERT(reporter, find == a || find == b);
895
robertphillips6e83ac72015-08-13 05:19:14 -0700896 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700897 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800898 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
899 REPORTER_ASSERT(reporter, find2 != find);
900 find2->unref();
901 find->unref();
902}
903
bsalomon8718aaf2015-02-19 07:24:21 -0800904static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800905 Mock mock(5, 30000);
906 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800907 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500908 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000909
bsalomon8718aaf2015-02-19 07:24:21 -0800910 GrUniqueKey key;
911 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700912
bsalomon8718aaf2015-02-19 07:24:21 -0800913 // Create two resources that we will attempt to register with the same unique key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500914 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -0800915 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700916
bsalomonf99e9612015-02-19 08:24:16 -0800917 // Set key on resource a.
918 a->resourcePriv().setUniqueKey(key);
919 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
920 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800921
bsalomonf99e9612015-02-19 08:24:16 -0800922 // Make sure that redundantly setting a's key works.
923 a->resourcePriv().setUniqueKey(key);
924 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800925 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800926 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
927 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800928 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
929
bsalomonf99e9612015-02-19 08:24:16 -0800930 // Create resource b and set the same key. It should replace a's unique key cache entry.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500931 TestResource* b = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800932 b->setSize(12);
933 b->resourcePriv().setUniqueKey(key);
934 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
935 b->unref();
936
937 // Still have two resources because a is still reffed.
938 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
939 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
940 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
941
942 a->unref();
943 // Now a should be gone.
944 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
945 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
946 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
947
948 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
949 // Also make b be unreffed when replacement occurs.
950 b->unref();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500951 TestResource* c = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800952 GrUniqueKey differentKey;
953 make_unique_key<0>(&differentKey, 1);
954 c->setSize(13);
955 c->resourcePriv().setUniqueKey(differentKey);
956 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
957 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
958 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
959 // c replaces b and b should be immediately purged.
960 c->resourcePriv().setUniqueKey(key);
961 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
962 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
963 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
964
965 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800966 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800967 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
968 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
969 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
970
971 // Drop the ref on c, it should be kept alive because it has a unique key.
972 c->unref();
973 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
974 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
975 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
976
977 // Verify that we can find c, then remove its unique key. It should get purged immediately.
978 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
979 c->resourcePriv().removeUniqueKey();
980 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800981 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
982 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800983 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700984
985 {
986 GrUniqueKey key2;
987 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500988 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700989 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700990 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700991 d->resourcePriv().setUniqueKey(key2);
992 }
993
994 GrUniqueKey key3;
995 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400996 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700997 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000998}
999
bsalomon8b79d232014-11-10 10:19:06 -08001000static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001001 Mock mock(5, 30000);
1002 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001003 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001004 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001005
bsalomon8718aaf2015-02-19 07:24:21 -08001006 GrUniqueKey key1, key2, key3;
1007 make_unique_key<0>(&key1, 1);
1008 make_unique_key<0>(&key2, 2);
1009 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -07001010
bsalomon23e619c2015-02-06 11:54:28 -08001011 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001012 TestResource* a = new TestResource(gpu);
1013 TestResource* b = new TestResource(gpu);
1014 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -08001015 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -08001016 a->resourcePriv().setUniqueKey(key1);
1017 b->resourcePriv().setUniqueKey(key2);
1018 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -08001019 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -08001020 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001021 c->unref();
1022
bsalomon8718aaf2015-02-19 07:24:21 -08001023 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1024 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1025 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001026 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001027
bsalomon8718aaf2015-02-19 07:24:21 -08001028 typedef GrUniqueKeyInvalidatedMessage Msg;
1029 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001030
1031 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
1032 Bus::Post(Msg(key1));
1033 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -08001034 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001035 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001036 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1037 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001038 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001039 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001040
1041 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -08001042 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -08001043 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001044 // we still have a ref on b, c should be recycled as scratch.
1045 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001046 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001047
bsalomon23e619c2015-02-06 11:54:28 -08001048 // make b purgeable. It should be immediately deleted since it has no key.
1049 b->unref();
1050 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1051
1052 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1053 GrScratchKey scratchKey;
1054 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -07001055 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -08001056 REPORTER_ASSERT(reporter, scratch == c);
1057 SkSafeUnref(scratch);
1058
1059 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001060 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -07001061 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -08001062 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001063 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1064 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001065 REPORTER_ASSERT(reporter, !scratch);
1066 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001067}
1068
bsalomon71cb0c22014-11-14 12:10:14 -08001069static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001070 Mock mock(3, 30000);
1071 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001072 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001073 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001074
bsalomon8718aaf2015-02-19 07:24:21 -08001075 GrUniqueKey key1, key2;
1076 make_unique_key<0>(&key1, 1);
1077 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001078
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001079 TestResource* a = new TestResource(gpu);
1080 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001081 a->resourcePriv().setUniqueKey(key1);
1082 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001083
bsalomonc2f35b72015-01-23 07:19:22 -08001084 // Make a cycle
1085 a->setUnrefWhenDestroyed(b);
1086 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001087
bsalomonc2f35b72015-01-23 07:19:22 -08001088 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001089
bsalomonc2f35b72015-01-23 07:19:22 -08001090 a->unref();
1091 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001092
bsalomonc2f35b72015-01-23 07:19:22 -08001093 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001094
bsalomon0ea80f42015-02-11 10:49:59 -08001095 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001096 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001097
bsalomonc2f35b72015-01-23 07:19:22 -08001098 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001099 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001100 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001101
bsalomon0ea80f42015-02-11 10:49:59 -08001102 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001103 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001104}
1105
bsalomon8b79d232014-11-10 10:19:06 -08001106static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001107 GrUniqueKey key1, key2;
1108 make_unique_key<0>(&key1, 1);
1109 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001110
1111 // Test changing resources sizes (both increase & decrease).
1112 {
bsalomonc2f35b72015-01-23 07:19:22 -08001113 Mock mock(3, 30000);
1114 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001115 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001116 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001117
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001118 TestResource* a = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001119 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001120 a->unref();
1121
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001122 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001123 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001124 b->unref();
1125
bsalomon0ea80f42015-02-11 10:49:59 -08001126 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1127 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001128 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001129 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001130 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001131 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001132 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001133 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001134 find1->setSize(50);
1135 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001136
bsalomon0ea80f42015-02-11 10:49:59 -08001137 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1138 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001139 }
1140
1141 // Test increasing a resources size beyond the cache budget.
1142 {
bsalomonc2f35b72015-01-23 07:19:22 -08001143 Mock mock(2, 300);
1144 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001145 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001146 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001147
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001148 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001149 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001150 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001151 a->unref();
1152
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001153 TestResource* b = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001154 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001155 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001156 b->unref();
1157
bsalomon0ea80f42015-02-11 10:49:59 -08001158 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1159 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001160
bsalomon8b79d232014-11-10 10:19:06 -08001161 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001162 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001163 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001164 find2->setSize(201);
1165 }
bsalomon8718aaf2015-02-19 07:24:21 -08001166 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001167
bsalomon0ea80f42015-02-11 10:49:59 -08001168 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1169 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001170 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001171}
1172
bsalomonddf30e62015-02-19 11:38:44 -08001173static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1174 static const int kCount = 50;
1175 static const int kBudgetCnt = kCount / 2;
1176 static const int kLockedFreq = 8;
1177 static const int kBudgetSize = 0x80000000;
1178
1179 SkRandom random;
1180
1181 // Run the test 2*kCount times;
1182 for (int i = 0; i < 2 * kCount; ++i ) {
1183 Mock mock(kBudgetCnt, kBudgetSize);
1184 GrContext* context = mock.context();
1185 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001186 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001187
1188 // Pick a random number of resources to add before the timestamp will wrap.
1189 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1190
1191 static const int kNumToPurge = kCount - kBudgetCnt;
1192
1193 SkTDArray<int> shouldPurgeIdxs;
1194 int purgeableCnt = 0;
1195 SkTDArray<GrGpuResource*> resourcesToUnref;
1196
1197 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1198 // unpurgeable resources.
1199 for (int j = 0; j < kCount; ++j) {
1200 GrUniqueKey key;
1201 make_unique_key<0>(&key, j);
1202
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001203 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001204 r->resourcePriv().setUniqueKey(key);
1205 if (random.nextU() % kLockedFreq) {
1206 // Make this is purgeable.
1207 r->unref();
1208 ++purgeableCnt;
1209 if (purgeableCnt <= kNumToPurge) {
1210 *shouldPurgeIdxs.append() = j;
1211 }
1212 } else {
1213 *resourcesToUnref.append() = r;
1214 }
1215 }
1216
1217 // Verify that the correct resources were purged.
1218 int currShouldPurgeIdx = 0;
1219 for (int j = 0; j < kCount; ++j) {
1220 GrUniqueKey key;
1221 make_unique_key<0>(&key, j);
1222 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1223 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1224 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1225 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001226 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001227 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001228 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001229 }
1230 SkSafeUnref(res);
1231 }
1232
1233 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1234 resourcesToUnref[j]->unref();
1235 }
1236 }
1237}
1238
bsalomon3f324322015-04-08 11:01:54 -07001239static void test_flush(skiatest::Reporter* reporter) {
1240 Mock mock(1000000, 1000000);
1241 GrContext* context = mock.context();
1242 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001243 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon3f324322015-04-08 11:01:54 -07001244
1245 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1246 // power of two here to keep things simpler.
1247 static const int kFlushCount = 16;
1248 cache->setLimits(1000000, 1000000, kFlushCount);
1249
1250 {
1251 // Insert a resource and send a flush notification kFlushCount times.
1252 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001253 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001254 GrUniqueKey k;
1255 make_unique_key<1>(&k, i);
1256 r->resourcePriv().setUniqueKey(k);
1257 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001258 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001259 }
1260
1261 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001262 for (int i = 0; i < kFlushCount; ++i) {
1263 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001264 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1265 for (int j = 0; j < i; ++j) {
1266 GrUniqueKey k;
1267 make_unique_key<1>(&k, j);
1268 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1269 REPORTER_ASSERT(reporter, !SkToBool(r));
1270 SkSafeUnref(r);
1271 }
bsalomon3f324322015-04-08 11:01:54 -07001272 }
1273
1274 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1275 cache->purgeAllUnlocked();
1276 }
1277
1278 // Do a similar test but where we leave refs on some resources to prevent them from being
1279 // purged.
1280 {
1281 GrGpuResource* refedResources[kFlushCount >> 1];
1282 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001283 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001284 GrUniqueKey k;
1285 make_unique_key<1>(&k, i);
1286 r->resourcePriv().setUniqueKey(k);
1287 // Leave a ref on every other resource, beginning with the first.
1288 if (SkToBool(i & 0x1)) {
1289 refedResources[i/2] = r;
1290 } else {
1291 r->unref();
1292 }
bsalomonb77a9072016-09-07 10:02:04 -07001293 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001294 }
1295
1296 for (int i = 0; i < kFlushCount; ++i) {
1297 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001298 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001299 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001300 }
1301
1302 // Unref all the resources that we kept refs on in the first loop.
1303 for (int i = 0; i < kFlushCount >> 1; ++i) {
1304 refedResources[i]->unref();
1305 }
1306
bsalomone2e87f32016-09-22 12:42:11 -07001307 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1308 // kFlushCount full flushes.
1309 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001310 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001311 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001312 }
1313 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1314
1315 cache->purgeAllUnlocked();
1316 }
1317
1318 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001319
1320 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1321 // eviction.
1322 context->flush();
1323 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001324 TestResource* r = new TestResource(gpu);
bsalomondc438982016-08-31 11:53:49 -07001325 GrUniqueKey k;
1326 make_unique_key<1>(&k, i);
1327 r->resourcePriv().setUniqueKey(k);
1328 r->unref();
1329 }
1330 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1331 for (int i = 0; i < 10 * kFlushCount; ++i) {
1332 context->flush();
1333 }
1334 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001335}
1336
Brian Salomon5e150852017-03-22 14:53:13 -04001337static void test_time_purge(skiatest::Reporter* reporter) {
1338 Mock mock(1000000, 1000000);
1339 GrContext* context = mock.context();
1340 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001341 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001342
1343 static constexpr int kCnts[] = {1, 10, 1024};
1344 auto nowish = []() {
1345 // We sleep so that we ensure we get a value that is greater than the last call to
1346 // GrStdSteadyClock::now().
1347 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1348 auto result = GrStdSteadyClock::now();
1349 // Also sleep afterwards so we don't get this value again.
1350 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1351 return result;
1352 };
1353
1354 for (int cnt : kCnts) {
1355 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1356 new GrStdSteadyClock::time_point[cnt]);
1357 {
1358 // Insert resources and get time points between each addition.
1359 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001360 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001361 GrUniqueKey k;
1362 make_unique_key<1>(&k, i);
1363 r->resourcePriv().setUniqueKey(k);
1364 r->unref();
1365 timeStamps.get()[i] = nowish();
1366 }
1367
1368 // Purge based on the time points between resource additions. Each purge should remove
1369 // the oldest resource.
1370 for (int i = 0; i < cnt; ++i) {
1371 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1372 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1373 for (int j = 0; j < i; ++j) {
1374 GrUniqueKey k;
1375 make_unique_key<1>(&k, j);
1376 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1377 REPORTER_ASSERT(reporter, !SkToBool(r));
1378 SkSafeUnref(r);
1379 }
1380 }
1381
1382 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1383 cache->purgeAllUnlocked();
1384 }
1385
1386 // Do a similar test but where we leave refs on some resources to prevent them from being
1387 // purged.
1388 {
1389 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1390 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001391 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001392 GrUniqueKey k;
1393 make_unique_key<1>(&k, i);
1394 r->resourcePriv().setUniqueKey(k);
1395 // Leave a ref on every other resource, beginning with the first.
1396 if (SkToBool(i & 0x1)) {
1397 refedResources.get()[i / 2] = r;
1398 } else {
1399 r->unref();
1400 }
1401 timeStamps.get()[i] = nowish();
1402 }
1403
1404 for (int i = 0; i < cnt; ++i) {
1405 // Should get a resource purged every other frame.
1406 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1407 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1408 }
1409
1410 // Unref all the resources that we kept refs on in the first loop.
1411 for (int i = 0; i < (cnt / 2); ++i) {
1412 refedResources.get()[i]->unref();
1413 cache->purgeResourcesNotUsedSince(nowish());
1414 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1415 }
1416
1417 cache->purgeAllUnlocked();
1418 }
1419
1420 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1421
1422 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1423 // eviction
1424 context->flush();
1425 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001426 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001427 GrUniqueKey k;
1428 make_unique_key<1>(&k, i);
1429 r->resourcePriv().setUniqueKey(k);
1430 r->unref();
1431 }
1432 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1433 context->flush();
1434 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1435 cache->purgeResourcesNotUsedSince(nowish());
1436 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1437 }
1438}
1439
Derek Sollenberger5480a182017-05-25 16:43:59 -04001440static void test_partial_purge(skiatest::Reporter* reporter) {
1441 Mock mock(6, 100);
1442 GrContext* context = mock.context();
1443 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001444 GrGpu* gpu = context->contextPriv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001445
1446 enum TestsCase {
1447 kOnlyScratch_TestCase = 0,
1448 kPartialScratch_TestCase = 1,
1449 kAllScratch_TestCase = 2,
1450 kPartial_TestCase = 3,
1451 kAll_TestCase = 4,
1452 kNone_TestCase = 5,
1453 kEndTests_TestCase = kNone_TestCase + 1
1454 };
1455
1456 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1457
1458 GrUniqueKey key1, key2, key3;
1459 make_unique_key<0>(&key1, 1);
1460 make_unique_key<0>(&key2, 2);
1461 make_unique_key<0>(&key3, 3);
1462
1463 // Add three unique resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001464 TestResource *unique1 = new TestResource(gpu);
1465 TestResource *unique2 = new TestResource(gpu);
1466 TestResource *unique3 = new TestResource(gpu);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001467
1468 unique1->resourcePriv().setUniqueKey(key1);
1469 unique2->resourcePriv().setUniqueKey(key2);
1470 unique3->resourcePriv().setUniqueKey(key3);
1471
1472 unique1->setSize(10);
1473 unique2->setSize(11);
1474 unique3->setSize(12);
1475
1476 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001477 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001478 TestResource::kA_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001479 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001480 TestResource::kB_SimulatedProperty);
1481 scratch1->setSize(13);
1482 scratch2->setSize(14);
1483
1484
1485 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1486 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1487 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1488
1489 // Add resources to the purgeable queue
1490 unique1->unref();
1491 scratch1->unref();
1492 unique2->unref();
1493 scratch2->unref();
1494 unique3->unref();
1495
1496 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1497 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1498 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1499
1500 switch(testCase) {
1501 case kOnlyScratch_TestCase: {
1502 context->purgeUnlockedResources(14, true);
1503 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1504 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1505 break;
1506 }
1507 case kPartialScratch_TestCase: {
1508 context->purgeUnlockedResources(3, true);
1509 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1510 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1511 break;
1512 }
1513 case kAllScratch_TestCase: {
1514 context->purgeUnlockedResources(50, true);
1515 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1516 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1517 break;
1518 }
1519 case kPartial_TestCase: {
1520 context->purgeUnlockedResources(13, false);
1521 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1522 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1523 break;
1524 }
1525 case kAll_TestCase: {
1526 context->purgeUnlockedResources(50, false);
1527 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1528 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1529 break;
1530 }
1531 case kNone_TestCase: {
1532 context->purgeUnlockedResources(0, true);
1533 context->purgeUnlockedResources(0, false);
1534 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1535 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1536 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1537 break;
1538 }
1539 };
1540
1541 // ensure all are purged before the next
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001542 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001543 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1544 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1545
1546 }
1547}
1548
bsalomon10e23ca2014-11-25 05:52:06 -08001549static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001550 // Set the cache size to double the resource count because we're going to create 2x that number
1551 // resources, using two different key domains. Add a little slop to the bytes because we resize
1552 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001553 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001554
bsalomonc2f35b72015-01-23 07:19:22 -08001555 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1556 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001557 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001558 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001559
1560 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001561 GrUniqueKey key1, key2;
1562 make_unique_key<1>(&key1, i);
1563 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001564
bsalomon24db3b12015-01-23 04:24:04 -08001565 TestResource* resource;
1566
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001567 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001568 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001569 resource->setSize(1);
1570 resource->unref();
1571
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001572 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001573 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001574 resource->setSize(1);
1575 resource->unref();
1576 }
1577
1578 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001579 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001580 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1581 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1582 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1583 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001584 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001585 GrUniqueKey key1, key2;
1586 make_unique_key<1>(&key1, i);
1587 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001588
bsalomon8718aaf2015-02-19 07:24:21 -08001589 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1590 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001591 }
1592
bsalomon0ea80f42015-02-11 10:49:59 -08001593 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001594 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001595 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001596 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1597 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1598 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1599 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001600
1601 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001602 GrUniqueKey key1, key2;
1603 make_unique_key<1>(&key1, i);
1604 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001605
bsalomon8718aaf2015-02-19 07:24:21 -08001606 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1607 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001608 }
1609}
1610
senorblanco84cd6212015-08-04 10:01:58 -07001611static void test_custom_data(skiatest::Reporter* reporter) {
1612 GrUniqueKey key1, key2;
1613 make_unique_key<0>(&key1, 1);
1614 make_unique_key<0>(&key2, 2);
1615 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001616 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001617 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1618 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1619
1620 // Test that copying a key also takes a ref on its custom data.
1621 GrUniqueKey key3 = key1;
1622 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1623}
1624
bsalomonc6363ef2015-09-24 07:07:40 -07001625static void test_abandoned(skiatest::Reporter* reporter) {
1626 Mock mock(10, 300);
1627 GrContext* context = mock.context();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001628 GrGpu* gpu = context->contextPriv().getGpu();
1629
1630 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001631 context->abandonContext();
1632
1633 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1634
1635 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1636
robertphillips8abb3702016-08-31 14:04:06 -07001637 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001638 resource->getUniqueKey();
1639 resource->wasDestroyed();
1640 resource->gpuMemorySize();
1641 resource->getContext();
1642
1643 resource->abandon();
1644 resource->resourcePriv().getScratchKey();
1645 resource->resourcePriv().isBudgeted();
1646 resource->resourcePriv().makeBudgeted();
1647 resource->resourcePriv().makeUnbudgeted();
1648 resource->resourcePriv().removeScratchKey();
1649 GrUniqueKey key;
1650 make_unique_key<0>(&key, 1);
1651 resource->resourcePriv().setUniqueKey(key);
1652 resource->resourcePriv().removeUniqueKey();
1653}
1654
Brian Salomon1090da62017-01-06 12:04:19 -05001655static void test_tags(skiatest::Reporter* reporter) {
1656#ifdef SK_DEBUG
1657 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1658 static constexpr int kLastTagIdx = 10;
1659 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1660
1661 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1662 GrContext* context = mock.context();
1663 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001664 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001665
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001666 // tag strings are expected to be long lived
1667 std::vector<SkString> tagStrings;
1668
Brian Salomon1090da62017-01-06 12:04:19 -05001669 SkString tagStr;
1670 int tagIdx = 0;
1671 int currTagCnt = 0;
1672
1673 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001674
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001675 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001676 GrUniqueKey key;
1677 if (currTagCnt == tagIdx) {
1678 tagIdx += 1;
1679 currTagCnt = 0;
1680 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001681 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001682 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001683 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001684 resource->resourcePriv().setUniqueKey(key);
1685 }
1686 SkASSERT(kLastTagIdx == tagIdx);
1687 SkASSERT(currTagCnt == kLastTagIdx);
1688
1689 // Test i = 0 to exercise unused tag string.
1690 for (int i = 0; i <= kLastTagIdx; ++i) {
1691 tagStr.printf("tag%d", i);
1692 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1693 }
1694#endif
1695}
1696
Brian Salomondcfca432017-11-15 15:48:03 -05001697DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001698 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001699 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001700 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001701 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001702 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001703 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001704 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001705 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001706 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001707 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001708 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001709 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001710 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001711 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001712 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001713 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001714 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001715 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001716 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001717 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001718 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001719}
1720
Robert Phillipsd6214d42016-11-07 08:23:48 -05001721////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001722static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001723 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001724 int width, int height,
1725 int sampleCnt) {
1726 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001727 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001728 desc.fWidth = width;
1729 desc.fHeight = height;
1730 desc.fConfig = kRGBA_8888_GrPixelConfig;
1731 desc.fSampleCnt = sampleCnt;
1732
Robert Phillipse78b7252017-04-06 07:59:41 -04001733 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001734}
1735
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001736static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001737 GrSurfaceDescFlags descFlags,
Robert Phillipse78b7252017-04-06 07:59:41 -04001738 int width, int height,
1739 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001740 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001741 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001742 desc.fWidth = width;
1743 desc.fHeight = height;
1744 desc.fConfig = kRGBA_8888_GrPixelConfig;
1745 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001746
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001747 auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1748 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001749
1750 return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001751}
1752
1753// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1754// Texture-only, both-RT-and-Texture and MIPmapped
1755DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1756 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001757 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001758 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001759
Robert Phillipsd6214d42016-11-07 08:23:48 -05001760 static const int kSize = 64;
1761
Robert Phillipsd6214d42016-11-07 08:23:48 -05001762 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001763 {
1764 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001765
Brian Salomonbdecacf2018-02-02 20:32:49 -05001766 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001767 size_t size = tex->gpuMemorySize();
1768 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1769
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001770 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1771 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001772 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001773 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001774 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001775 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001776 REPORTER_ASSERT(reporter,
1777 kSize*kSize*4 == size || // msaa4 failed
1778 kSize*kSize*4*sampleCount == size || // auto-resolving
1779 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001780 }
1781
Brian Salomonbdecacf2018-02-02 20:32:49 -05001782 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001783 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001784 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001785 }
1786
Robert Phillipsd6214d42016-11-07 08:23:48 -05001787
1788 // Mipmapped versions
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001789 if (context->contextPriv().caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001790 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001791
Brian Salomonbdecacf2018-02-02 20:32:49 -05001792 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001793 size_t size = proxy->gpuMemorySize();
1794 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1795
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001796 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1797 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001798 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001799 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001800 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001801 size = proxy->gpuMemorySize();
1802 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001803 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1804 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1805 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001806 }
Robert Phillips1b352562017-04-05 18:56:21 +00001807
Brian Salomonbdecacf2018-02-02 20:32:49 -05001808 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001809 size = proxy->gpuMemorySize();
1810 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1811 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001812}
1813
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001814#endif