blob: 2ceb567688e6ffe5a1e6ab82116e5058244e7d7b [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();
Eric Karl5c779752017-05-08 12:02:07 -0700125 if (context->caps()->avoidStencilBuffers()) {
126 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 Salomonbdecacf2018-02-02 20:32:49 -0500158 int smallSampleCount = context->caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
159 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700160 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500161 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
162 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800163#ifdef SK_BUILD_FOR_ANDROID
164 if (!smallMSAART0) {
165 // The nexus player seems to fail to create MSAA textures.
166 return;
167 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168#else
169 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800170#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400171
172 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
173
174 {
175 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500176 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
177 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400178 SkBudgeted::kNo);
179 REPORTER_ASSERT(reporter, smallMSAART1);
180
181 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800182 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400183
Brian Salomonbdecacf2018-02-02 20:32:49 -0500184 // But one with a larger sample count should not. (Also check that the two requests didn't
185 // rounded up to the same actual sample count or else they could share.).
186 int bigSampleCount =
187 context->caps()->getRenderTargetSampleCount(5, kRGBA_8888_GrPixelConfig);
188 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
190 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400191 SkBudgeted::kNo);
192 REPORTER_ASSERT(reporter, smallMSAART2);
193
194 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800195 }
196 }
197}
198
bsalomon68d91342016-04-12 09:59:58 -0700199DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700200 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500201 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500202 GrGpu* gpu = context->contextPriv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700203 // this test is only valid for GL
204 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205 return;
206 }
207
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500208 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700209 static const int kW = 100;
210 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700211
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500212 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
213 kRGBA_8888_GrPixelConfig,
214 false, GrMipMapped::kNo);
215 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
216 kRGBA_8888_GrPixelConfig,
217 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500218 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
219 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
220 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
221 return;
222 }
jvanverth672bb7f2015-07-13 07:19:57 -0700223
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224 context->resetContext();
225
Robert Phillips6be756b2018-01-16 15:07:54 -0500226 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500227 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
Robert Phillips6be756b2018-01-16 15:07:54 -0500229 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500230 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231
Brian Osman85d34b22017-05-10 12:06:26 -0400232 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
233 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700234 return;
235 }
236
halcanary96fcdcc2015-08-27 07:41:13 -0700237 borrowed.reset(nullptr);
238 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700239
240 context->flush();
241
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500242 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
243 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244
245 REPORTER_ASSERT(reporter, borrowedIsAlive);
246 REPORTER_ASSERT(reporter, !adoptedIsAlive);
247
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500248 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[0]), !borrowedIsAlive);
249 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[1]), !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700250
251 context->resetContext();
252}
253
bsalomon6d3fe022014-07-25 08:35:45 -0700254class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800255 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000256public:
robertphillips6e83ac72015-08-13 05:19:14 -0700257 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700258
bsalomon1c60dfe2015-01-21 09:32:40 -0800259 /** Property that distinctly categorizes the resource.
260 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800261 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800262
kkinnunen2e6055b2016-04-22 01:48:29 -0700263 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
264 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700265 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800266 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 , fProperty(kA_SimulatedProperty)
268 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800269 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700270 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800271 }
272
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
274 SimulatedProperty property) {
275 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800276 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700277 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
278 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000279 }
280
Brian Salomond3b65972017-03-22 12:05:03 -0400281 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800282 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800283 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284 }
285
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000286 void setSize(size_t size) {
287 fSize = size;
288 this->didChangeGpuMemorySize();
289 }
290
bsalomon33435572014-11-05 14:47:41 -0800291 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000292
bsalomon71cb0c22014-11-14 12:10:14 -0800293 void setUnrefWhenDestroyed(TestResource* resource) {
294 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000295 }
296
bsalomon1c60dfe2015-01-21 09:32:40 -0800297 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
298 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
299 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800300 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
301 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800302 }
303 }
304
305 static size_t ExpectedScratchKeySize() {
306 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
307 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000308private:
bsalomon24db3b12015-01-23 04:24:04 -0800309 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800310
kkinnunen2e6055b2016-04-22 01:48:29 -0700311 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
312 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700313 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 , fProperty(property)
316 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800317 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700318 this->registerWithCache(budgeted);
319 }
320
321 // Constructor for simulating resources that wrap backend objects.
322 TestResource(GrGpu* gpu, size_t size)
323 : INHERITED(gpu)
324 , fToDelete(nullptr)
325 , fSize(size)
326 , fProperty(kA_SimulatedProperty)
327 , fIsScratch(false) {
328 ++fNumAlive;
329 this->registerWithCacheWrapped();
330 }
331
332 void computeScratchKey(GrScratchKey* key) const override {
333 if (fIsScratch) {
334 ComputeScratchKey(fProperty, key);
335 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800336 }
337
mtklein36352bf2015-03-25 18:17:31 -0700338 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800339
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000340 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000341 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800342 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800343 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700344 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700345 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000346};
bsalomon33435572014-11-05 14:47:41 -0800347int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000348
bsalomonc2f35b72015-01-23 07:19:22 -0800349class Mock {
350public:
351 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400352 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800353 SkASSERT(fContext);
354 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500355 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800356 cache->purgeAllUnlocked();
357 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800358 }
bsalomonc2f35b72015-01-23 07:19:22 -0800359
Robert Phillips6be756b2018-01-16 15:07:54 -0500360 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800361
Hal Canary342b7ac2016-11-04 11:49:42 -0400362 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800363
364private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400365 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800366};
367
368static void test_no_key(skiatest::Reporter* reporter) {
369 Mock mock(10, 30000);
370 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800371 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500372 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800373
374 // Create a bunch of resources with no keys
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500375 TestResource* a = new TestResource(gpu);
376 TestResource* b = new TestResource(gpu);
377 TestResource* c = new TestResource(gpu);
378 TestResource* d = new TestResource(gpu);
bsalomon71cb0c22014-11-14 12:10:14 -0800379 a->setSize(11);
380 b->setSize(12);
381 c->setSize(13);
382 d->setSize(14);
383
384 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800387 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800390 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800391 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
392
bsalomon8718aaf2015-02-19 07:24:21 -0800393 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 a->unref();
396 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800398 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 c->unref();
402 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800405 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 d->unref();
408 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411
412 b->unref();
413 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800414 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
415 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800416}
417
bsalomon24db3b12015-01-23 04:24:04 -0800418// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500419template <int>
420static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800421 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500422 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800423 builder[0] = data;
424}
425
bsalomon84c8e622014-11-17 09:33:27 -0800426static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800427 Mock mock(10, 300);
428 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800429 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500430 GrGpu* gpu = context->contextPriv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800431
bsalomon8718aaf2015-02-19 07:24:21 -0800432 GrUniqueKey uniqueKey;
433 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800434
bsalomon8718aaf2015-02-19 07:24:21 -0800435 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800436 TestResource* scratch =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500437 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800438 scratch->setSize(10);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500439 TestResource* unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800440 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800441 unique->resourcePriv().setUniqueKey(uniqueKey);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500442 TestResource* wrapped = TestResource::CreateWrapped(gpu);
bsalomon5236cf42015-01-14 10:42:08 -0800443 wrapped->setSize(12);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500444 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800445 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800446
Brian Osman0562eb92017-05-08 11:16:39 -0400447 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800448 GrUniqueKey uniqueKey2;
449 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800450 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400451 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
452 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
453
454 // Remove the extra ref we just added.
455 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800456
457 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800458 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800459 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800460 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800461 cache->getResourceBytes());
462 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800463 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800464 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400465 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800466
bsalomon63c992f2015-01-23 12:47:59 -0800467 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800468 cache->purgeAllUnlocked();
469 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800470 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800471 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800472 cache->getResourceBytes());
473 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800474 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800475 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400476 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800477
478 // Unreffing the wrapped resource should free it right away.
479 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800481 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800482 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400483 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800484
bsalomon84c8e622014-11-17 09:33:27 -0800485 // Now try freeing the budgeted resources first
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500486 wrapped = TestResource::CreateWrapped(gpu);
bsalomondace19e2014-11-17 07:34:06 -0800487 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800488 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400489 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800490 cache->purgeAllUnlocked();
491 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800492 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800493 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
494 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
495 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400496 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800497
498 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400499 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800500 cache->purgeAllUnlocked();
501 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800502 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800503 cache->getResourceBytes());
504 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
505 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400506 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800507
508 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800509 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
510 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
511 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
512 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400513 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800514
515 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800516 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
517 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
518 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
519 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400520 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800521}
522
bsalomon5236cf42015-01-14 10:42:08 -0800523static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800524 Mock mock(10, 30000);
525 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800526 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500527 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800528
bsalomon8718aaf2015-02-19 07:24:21 -0800529 GrUniqueKey uniqueKey;
530 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800531
532 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800533 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800534 TestResource* wrapped;
535 TestResource* unbudgeted;
536
537 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500538 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
kkinnunen2e6055b2016-04-22 01:48:29 -0700539 TestResource::kB_SimulatedProperty);
540
bsalomon5236cf42015-01-14 10:42:08 -0800541 scratch->setSize(10);
542 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800543 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
544 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
545 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
546 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400547 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800548
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500549 unique = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800550 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800551 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800552 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800553 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
554 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
555 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
556 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400557 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800558
bsalomon0ea80f42015-02-11 10:49:59 -0800559 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500560 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800561 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
562 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
563 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
564 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400565 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800566
567 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800568 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
569 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
570 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
571 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400572 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800573
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500574 wrapped = TestResource::CreateWrapped(gpu, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
576 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
577 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
578 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400579 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800580
581 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800582 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400586 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800587
bsalomon0ea80f42015-02-11 10:49:59 -0800588 cache->purgeAllUnlocked();
589 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
590 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
592 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400593 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800594}
595
bsalomon3582d3e2015-02-13 14:20:05 -0800596// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
597void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
598/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800599 Mock mock(10, 300);
600 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500602 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800603
604 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500605 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800606 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800607 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800608
609 size_t size = resource->gpuMemorySize();
610 for (int i = 0; i < 2; ++i) {
611 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800612 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800613 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800614 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700615 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800616 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
617 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
618 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
619 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400620 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800621
622 // Once it is unrefed, it should become available as scratch.
623 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800624 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
625 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
626 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
627 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400628 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700629 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800630 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800631 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800632 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800633 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800634
635 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700636 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800637 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800638 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800639 } else {
640 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800641 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800642 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
643 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
644 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
645 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400646 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800647 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800648 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800649 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800650
651 // now when it is unrefed it should die since it has no key.
652 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800653 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
654 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
655 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
656 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400657 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800658 }
bsalomon8b79d232014-11-10 10:19:06 -0800659 }
bsalomonc2f35b72015-01-23 07:19:22 -0800660}
661
662static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
663 Mock mock(5, 30000);
664 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800665 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500666 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800667
bsalomon8b79d232014-11-10 10:19:06 -0800668 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500669 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700670 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800671 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500672 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700673 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800674 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800675 a->setSize(11);
676 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800677 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800678 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800679 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700680 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800681
682 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800683 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800684
bsalomon0ea80f42015-02-11 10:49:59 -0800685 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800686 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800687 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
688 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800689 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800690 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800691
bsalomon63c992f2015-01-23 12:47:59 -0800692 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800693 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800694 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800695 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800696
697 // Unref but don't purge
698 a->unref();
699 b->unref();
700 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800701 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800702
bsalomon63c992f2015-01-23 12:47:59 -0800703 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800704 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800705 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800706 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
707 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800708}
709
bsalomon10e23ca2014-11-25 05:52:06 -0800710static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800711 Mock mock(5, 30000);
712 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800713 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500714 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800715
bsalomon10e23ca2014-11-25 05:52:06 -0800716 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500717 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800718 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500719 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800720 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800721 a->unref();
722 b->unref();
723
bsalomon1c60dfe2015-01-21 09:32:40 -0800724 GrScratchKey scratchKey;
725 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800726 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800727 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700728 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800729
bsalomon0ea80f42015-02-11 10:49:59 -0800730 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800731 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800732 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800733 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
734 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800735
736 // Find the first resource and remove its scratch key
737 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700738 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800739 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800740 // It's still alive, but not cached by scratch key anymore
741 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800742 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
743 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800744
745 // The cache should immediately delete it when it's unrefed since it isn't accessible.
746 find->unref();
747 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800748 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
749 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800750
751 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700752 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800753 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800754 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800755 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
756 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800757
758 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800759 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800760 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
762 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800763
764 find->unref();
765 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800766 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
767 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800768}
769
bsalomon1c60dfe2015-01-21 09:32:40 -0800770static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800771 Mock mock(5, 30000);
772 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800773 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500774 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800775
776 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500777 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800778 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500779 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800780 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800781 a->unref();
782 b->unref();
783
784 GrScratchKey scratchKey;
785 // Ensure that scratch key comparison and assignment is consistent.
786 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800787 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800788 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800789 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800790 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
791 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
792 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
793 scratchKey = scratchKey1;
794 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
795 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
796 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
797 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
798 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
799 scratchKey = scratchKey2;
800 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
801 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
802 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
803 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
804 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
805
806 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800807 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800808 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700809 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800810
811 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800812 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700813 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700814 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800815 find->unref();
816
817 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700818 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700819 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800820 REPORTER_ASSERT(reporter, find == a || find == b);
821
robertphillips6e83ac72015-08-13 05:19:14 -0700822 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700823 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800824 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
825 REPORTER_ASSERT(reporter, find2 != find);
826 find2->unref();
827 find->unref();
828}
829
bsalomon8718aaf2015-02-19 07:24:21 -0800830static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800831 Mock mock(5, 30000);
832 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800833 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500834 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000835
bsalomon8718aaf2015-02-19 07:24:21 -0800836 GrUniqueKey key;
837 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700838
bsalomon8718aaf2015-02-19 07:24:21 -0800839 // Create two resources that we will attempt to register with the same unique key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500840 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -0800841 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700842
bsalomonf99e9612015-02-19 08:24:16 -0800843 // Set key on resource a.
844 a->resourcePriv().setUniqueKey(key);
845 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
846 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800847
bsalomonf99e9612015-02-19 08:24:16 -0800848 // Make sure that redundantly setting a's key works.
849 a->resourcePriv().setUniqueKey(key);
850 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800851 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800852 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
853 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800854 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
855
bsalomonf99e9612015-02-19 08:24:16 -0800856 // Create resource b and set the same key. It should replace a's unique key cache entry.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500857 TestResource* b = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800858 b->setSize(12);
859 b->resourcePriv().setUniqueKey(key);
860 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
861 b->unref();
862
863 // Still have two resources because a is still reffed.
864 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
865 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
866 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
867
868 a->unref();
869 // Now a should be gone.
870 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
871 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
872 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
873
874 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
875 // Also make b be unreffed when replacement occurs.
876 b->unref();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500877 TestResource* c = new TestResource(gpu);
bsalomonf99e9612015-02-19 08:24:16 -0800878 GrUniqueKey differentKey;
879 make_unique_key<0>(&differentKey, 1);
880 c->setSize(13);
881 c->resourcePriv().setUniqueKey(differentKey);
882 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
883 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
884 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
885 // c replaces b and b should be immediately purged.
886 c->resourcePriv().setUniqueKey(key);
887 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
888 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
889 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
890
891 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800892 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800893 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
894 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
895 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
896
897 // Drop the ref on c, it should be kept alive because it has a unique key.
898 c->unref();
899 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
900 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
901 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
902
903 // Verify that we can find c, then remove its unique key. It should get purged immediately.
904 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
905 c->resourcePriv().removeUniqueKey();
906 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800907 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
908 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800909 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700910
911 {
912 GrUniqueKey key2;
913 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500914 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700915 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700916 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700917 d->resourcePriv().setUniqueKey(key2);
918 }
919
920 GrUniqueKey key3;
921 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400922 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700923 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000924}
925
bsalomon8b79d232014-11-10 10:19:06 -0800926static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800927 Mock mock(5, 30000);
928 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800929 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500930 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800931
bsalomon8718aaf2015-02-19 07:24:21 -0800932 GrUniqueKey key1, key2, key3;
933 make_unique_key<0>(&key1, 1);
934 make_unique_key<0>(&key2, 2);
935 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700936
bsalomon23e619c2015-02-06 11:54:28 -0800937 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500938 TestResource* a = new TestResource(gpu);
939 TestResource* b = new TestResource(gpu);
940 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800941 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800942 a->resourcePriv().setUniqueKey(key1);
943 b->resourcePriv().setUniqueKey(key2);
944 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800945 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800946 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800947 c->unref();
948
bsalomon8718aaf2015-02-19 07:24:21 -0800949 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
950 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
951 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800952 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800953
bsalomon8718aaf2015-02-19 07:24:21 -0800954 typedef GrUniqueKeyInvalidatedMessage Msg;
955 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800956
957 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
958 Bus::Post(Msg(key1));
959 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800960 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800961 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800962 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
963 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800964 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800965 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800966
967 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800968 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800969 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800970 // we still have a ref on b, c should be recycled as scratch.
971 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800972 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800973
bsalomon23e619c2015-02-06 11:54:28 -0800974 // make b purgeable. It should be immediately deleted since it has no key.
975 b->unref();
976 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
977
978 // Make sure we actually get to c via it's scratch key, before we say goodbye.
979 GrScratchKey scratchKey;
980 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700981 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800982 REPORTER_ASSERT(reporter, scratch == c);
983 SkSafeUnref(scratch);
984
985 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800986 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700987 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800988 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800989 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
990 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800991 REPORTER_ASSERT(reporter, !scratch);
992 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800993}
994
bsalomon71cb0c22014-11-14 12:10:14 -0800995static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800996 Mock mock(3, 30000);
997 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800998 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500999 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001000
bsalomon8718aaf2015-02-19 07:24:21 -08001001 GrUniqueKey key1, key2;
1002 make_unique_key<0>(&key1, 1);
1003 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001004
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001005 TestResource* a = new TestResource(gpu);
1006 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001007 a->resourcePriv().setUniqueKey(key1);
1008 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001009
bsalomonc2f35b72015-01-23 07:19:22 -08001010 // Make a cycle
1011 a->setUnrefWhenDestroyed(b);
1012 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001013
bsalomonc2f35b72015-01-23 07:19:22 -08001014 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001015
bsalomonc2f35b72015-01-23 07:19:22 -08001016 a->unref();
1017 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001018
bsalomonc2f35b72015-01-23 07:19:22 -08001019 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001020
bsalomon0ea80f42015-02-11 10:49:59 -08001021 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001022 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001023
bsalomonc2f35b72015-01-23 07:19:22 -08001024 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001025 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001026 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001027
bsalomon0ea80f42015-02-11 10:49:59 -08001028 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001029 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001030}
1031
bsalomon8b79d232014-11-10 10:19:06 -08001032static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001033 GrUniqueKey key1, key2;
1034 make_unique_key<0>(&key1, 1);
1035 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001036
1037 // Test changing resources sizes (both increase & decrease).
1038 {
bsalomonc2f35b72015-01-23 07:19:22 -08001039 Mock mock(3, 30000);
1040 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001041 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001042 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001043
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001044 TestResource* a = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001045 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001046 a->unref();
1047
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001048 TestResource* b = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001049 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001050 b->unref();
1051
bsalomon0ea80f42015-02-11 10:49:59 -08001052 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1053 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001054 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001055 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001056 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001057 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001058 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001059 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001060 find1->setSize(50);
1061 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062
bsalomon0ea80f42015-02-11 10:49:59 -08001063 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1064 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001065 }
1066
1067 // Test increasing a resources size beyond the cache budget.
1068 {
bsalomonc2f35b72015-01-23 07:19:22 -08001069 Mock mock(2, 300);
1070 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001071 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001072 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001073
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001074 TestResource* a = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001075 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001076 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001077 a->unref();
1078
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001079 TestResource* b = new TestResource(gpu);
bsalomon8b79d232014-11-10 10:19:06 -08001080 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001081 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001082 b->unref();
1083
bsalomon0ea80f42015-02-11 10:49:59 -08001084 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1085 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001086
bsalomon8b79d232014-11-10 10:19:06 -08001087 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001088 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001089 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001090 find2->setSize(201);
1091 }
bsalomon8718aaf2015-02-19 07:24:21 -08001092 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001093
bsalomon0ea80f42015-02-11 10:49:59 -08001094 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1095 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001096 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001097}
1098
bsalomonddf30e62015-02-19 11:38:44 -08001099static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1100 static const int kCount = 50;
1101 static const int kBudgetCnt = kCount / 2;
1102 static const int kLockedFreq = 8;
1103 static const int kBudgetSize = 0x80000000;
1104
1105 SkRandom random;
1106
1107 // Run the test 2*kCount times;
1108 for (int i = 0; i < 2 * kCount; ++i ) {
1109 Mock mock(kBudgetCnt, kBudgetSize);
1110 GrContext* context = mock.context();
1111 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001112 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001113
1114 // Pick a random number of resources to add before the timestamp will wrap.
1115 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1116
1117 static const int kNumToPurge = kCount - kBudgetCnt;
1118
1119 SkTDArray<int> shouldPurgeIdxs;
1120 int purgeableCnt = 0;
1121 SkTDArray<GrGpuResource*> resourcesToUnref;
1122
1123 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1124 // unpurgeable resources.
1125 for (int j = 0; j < kCount; ++j) {
1126 GrUniqueKey key;
1127 make_unique_key<0>(&key, j);
1128
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001129 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001130 r->resourcePriv().setUniqueKey(key);
1131 if (random.nextU() % kLockedFreq) {
1132 // Make this is purgeable.
1133 r->unref();
1134 ++purgeableCnt;
1135 if (purgeableCnt <= kNumToPurge) {
1136 *shouldPurgeIdxs.append() = j;
1137 }
1138 } else {
1139 *resourcesToUnref.append() = r;
1140 }
1141 }
1142
1143 // Verify that the correct resources were purged.
1144 int currShouldPurgeIdx = 0;
1145 for (int j = 0; j < kCount; ++j) {
1146 GrUniqueKey key;
1147 make_unique_key<0>(&key, j);
1148 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1149 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1150 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1151 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001152 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001153 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001154 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001155 }
1156 SkSafeUnref(res);
1157 }
1158
1159 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1160 resourcesToUnref[j]->unref();
1161 }
1162 }
1163}
1164
bsalomon3f324322015-04-08 11:01:54 -07001165static void test_flush(skiatest::Reporter* reporter) {
1166 Mock mock(1000000, 1000000);
1167 GrContext* context = mock.context();
1168 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001169 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon3f324322015-04-08 11:01:54 -07001170
1171 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1172 // power of two here to keep things simpler.
1173 static const int kFlushCount = 16;
1174 cache->setLimits(1000000, 1000000, kFlushCount);
1175
1176 {
1177 // Insert a resource and send a flush notification kFlushCount times.
1178 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001179 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001180 GrUniqueKey k;
1181 make_unique_key<1>(&k, i);
1182 r->resourcePriv().setUniqueKey(k);
1183 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001184 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001185 }
1186
1187 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001188 for (int i = 0; i < kFlushCount; ++i) {
1189 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001190 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1191 for (int j = 0; j < i; ++j) {
1192 GrUniqueKey k;
1193 make_unique_key<1>(&k, j);
1194 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1195 REPORTER_ASSERT(reporter, !SkToBool(r));
1196 SkSafeUnref(r);
1197 }
bsalomon3f324322015-04-08 11:01:54 -07001198 }
1199
1200 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1201 cache->purgeAllUnlocked();
1202 }
1203
1204 // Do a similar test but where we leave refs on some resources to prevent them from being
1205 // purged.
1206 {
1207 GrGpuResource* refedResources[kFlushCount >> 1];
1208 for (int i = 0; i < kFlushCount; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001209 TestResource* r = new TestResource(gpu);
bsalomon3f324322015-04-08 11:01:54 -07001210 GrUniqueKey k;
1211 make_unique_key<1>(&k, i);
1212 r->resourcePriv().setUniqueKey(k);
1213 // Leave a ref on every other resource, beginning with the first.
1214 if (SkToBool(i & 0x1)) {
1215 refedResources[i/2] = r;
1216 } else {
1217 r->unref();
1218 }
bsalomonb77a9072016-09-07 10:02:04 -07001219 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001220 }
1221
1222 for (int i = 0; i < kFlushCount; ++i) {
1223 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001224 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001225 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001226 }
1227
1228 // Unref all the resources that we kept refs on in the first loop.
1229 for (int i = 0; i < kFlushCount >> 1; ++i) {
1230 refedResources[i]->unref();
1231 }
1232
bsalomone2e87f32016-09-22 12:42:11 -07001233 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1234 // kFlushCount full flushes.
1235 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001236 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001237 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001238 }
1239 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1240
1241 cache->purgeAllUnlocked();
1242 }
1243
1244 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001245
1246 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1247 // eviction.
1248 context->flush();
1249 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001250 TestResource* r = new TestResource(gpu);
bsalomondc438982016-08-31 11:53:49 -07001251 GrUniqueKey k;
1252 make_unique_key<1>(&k, i);
1253 r->resourcePriv().setUniqueKey(k);
1254 r->unref();
1255 }
1256 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1257 for (int i = 0; i < 10 * kFlushCount; ++i) {
1258 context->flush();
1259 }
1260 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001261}
1262
Brian Salomon5e150852017-03-22 14:53:13 -04001263static void test_time_purge(skiatest::Reporter* reporter) {
1264 Mock mock(1000000, 1000000);
1265 GrContext* context = mock.context();
1266 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001267 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001268
1269 static constexpr int kCnts[] = {1, 10, 1024};
1270 auto nowish = []() {
1271 // We sleep so that we ensure we get a value that is greater than the last call to
1272 // GrStdSteadyClock::now().
1273 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1274 auto result = GrStdSteadyClock::now();
1275 // Also sleep afterwards so we don't get this value again.
1276 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1277 return result;
1278 };
1279
1280 for (int cnt : kCnts) {
1281 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1282 new GrStdSteadyClock::time_point[cnt]);
1283 {
1284 // Insert resources and get time points between each addition.
1285 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001286 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001287 GrUniqueKey k;
1288 make_unique_key<1>(&k, i);
1289 r->resourcePriv().setUniqueKey(k);
1290 r->unref();
1291 timeStamps.get()[i] = nowish();
1292 }
1293
1294 // Purge based on the time points between resource additions. Each purge should remove
1295 // the oldest resource.
1296 for (int i = 0; i < cnt; ++i) {
1297 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1298 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1299 for (int j = 0; j < i; ++j) {
1300 GrUniqueKey k;
1301 make_unique_key<1>(&k, j);
1302 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1303 REPORTER_ASSERT(reporter, !SkToBool(r));
1304 SkSafeUnref(r);
1305 }
1306 }
1307
1308 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1309 cache->purgeAllUnlocked();
1310 }
1311
1312 // Do a similar test but where we leave refs on some resources to prevent them from being
1313 // purged.
1314 {
1315 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1316 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001317 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001318 GrUniqueKey k;
1319 make_unique_key<1>(&k, i);
1320 r->resourcePriv().setUniqueKey(k);
1321 // Leave a ref on every other resource, beginning with the first.
1322 if (SkToBool(i & 0x1)) {
1323 refedResources.get()[i / 2] = r;
1324 } else {
1325 r->unref();
1326 }
1327 timeStamps.get()[i] = nowish();
1328 }
1329
1330 for (int i = 0; i < cnt; ++i) {
1331 // Should get a resource purged every other frame.
1332 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1333 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1334 }
1335
1336 // Unref all the resources that we kept refs on in the first loop.
1337 for (int i = 0; i < (cnt / 2); ++i) {
1338 refedResources.get()[i]->unref();
1339 cache->purgeResourcesNotUsedSince(nowish());
1340 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1341 }
1342
1343 cache->purgeAllUnlocked();
1344 }
1345
1346 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1347
1348 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1349 // eviction
1350 context->flush();
1351 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001352 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001353 GrUniqueKey k;
1354 make_unique_key<1>(&k, i);
1355 r->resourcePriv().setUniqueKey(k);
1356 r->unref();
1357 }
1358 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1359 context->flush();
1360 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1361 cache->purgeResourcesNotUsedSince(nowish());
1362 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1363 }
1364}
1365
Derek Sollenberger5480a182017-05-25 16:43:59 -04001366static void test_partial_purge(skiatest::Reporter* reporter) {
1367 Mock mock(6, 100);
1368 GrContext* context = mock.context();
1369 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001370 GrGpu* gpu = context->contextPriv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001371
1372 enum TestsCase {
1373 kOnlyScratch_TestCase = 0,
1374 kPartialScratch_TestCase = 1,
1375 kAllScratch_TestCase = 2,
1376 kPartial_TestCase = 3,
1377 kAll_TestCase = 4,
1378 kNone_TestCase = 5,
1379 kEndTests_TestCase = kNone_TestCase + 1
1380 };
1381
1382 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1383
1384 GrUniqueKey key1, key2, key3;
1385 make_unique_key<0>(&key1, 1);
1386 make_unique_key<0>(&key2, 2);
1387 make_unique_key<0>(&key3, 3);
1388
1389 // Add three unique resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001390 TestResource *unique1 = new TestResource(gpu);
1391 TestResource *unique2 = new TestResource(gpu);
1392 TestResource *unique3 = new TestResource(gpu);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001393
1394 unique1->resourcePriv().setUniqueKey(key1);
1395 unique2->resourcePriv().setUniqueKey(key2);
1396 unique3->resourcePriv().setUniqueKey(key3);
1397
1398 unique1->setSize(10);
1399 unique2->setSize(11);
1400 unique3->setSize(12);
1401
1402 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001403 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001404 TestResource::kA_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001405 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Derek Sollenberger5480a182017-05-25 16:43:59 -04001406 TestResource::kB_SimulatedProperty);
1407 scratch1->setSize(13);
1408 scratch2->setSize(14);
1409
1410
1411 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1412 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1413 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1414
1415 // Add resources to the purgeable queue
1416 unique1->unref();
1417 scratch1->unref();
1418 unique2->unref();
1419 scratch2->unref();
1420 unique3->unref();
1421
1422 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1423 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1424 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1425
1426 switch(testCase) {
1427 case kOnlyScratch_TestCase: {
1428 context->purgeUnlockedResources(14, true);
1429 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1430 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1431 break;
1432 }
1433 case kPartialScratch_TestCase: {
1434 context->purgeUnlockedResources(3, true);
1435 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1436 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1437 break;
1438 }
1439 case kAllScratch_TestCase: {
1440 context->purgeUnlockedResources(50, true);
1441 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1442 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1443 break;
1444 }
1445 case kPartial_TestCase: {
1446 context->purgeUnlockedResources(13, false);
1447 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1448 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1449 break;
1450 }
1451 case kAll_TestCase: {
1452 context->purgeUnlockedResources(50, false);
1453 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1454 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1455 break;
1456 }
1457 case kNone_TestCase: {
1458 context->purgeUnlockedResources(0, true);
1459 context->purgeUnlockedResources(0, false);
1460 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1461 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1462 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1463 break;
1464 }
1465 };
1466
1467 // ensure all are purged before the next
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001468 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001469 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1470 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1471
1472 }
1473}
1474
bsalomon10e23ca2014-11-25 05:52:06 -08001475static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001476 // Set the cache size to double the resource count because we're going to create 2x that number
1477 // resources, using two different key domains. Add a little slop to the bytes because we resize
1478 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001479 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001480
bsalomonc2f35b72015-01-23 07:19:22 -08001481 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1482 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001483 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001484 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001485
1486 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001487 GrUniqueKey key1, key2;
1488 make_unique_key<1>(&key1, i);
1489 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001490
bsalomon24db3b12015-01-23 04:24:04 -08001491 TestResource* resource;
1492
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001493 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001494 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001495 resource->setSize(1);
1496 resource->unref();
1497
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001498 resource = new TestResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -08001499 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001500 resource->setSize(1);
1501 resource->unref();
1502 }
1503
1504 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001505 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001506 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1507 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1508 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1509 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001510 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001511 GrUniqueKey key1, key2;
1512 make_unique_key<1>(&key1, i);
1513 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001514
bsalomon8718aaf2015-02-19 07:24:21 -08001515 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1516 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001517 }
1518
bsalomon0ea80f42015-02-11 10:49:59 -08001519 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001520 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001521 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001522 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1523 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1524 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1525 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001526
1527 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001528 GrUniqueKey key1, key2;
1529 make_unique_key<1>(&key1, i);
1530 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001531
bsalomon8718aaf2015-02-19 07:24:21 -08001532 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1533 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001534 }
1535}
1536
senorblanco84cd6212015-08-04 10:01:58 -07001537static void test_custom_data(skiatest::Reporter* reporter) {
1538 GrUniqueKey key1, key2;
1539 make_unique_key<0>(&key1, 1);
1540 make_unique_key<0>(&key2, 2);
1541 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001542 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001543 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1544 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1545
1546 // Test that copying a key also takes a ref on its custom data.
1547 GrUniqueKey key3 = key1;
1548 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1549}
1550
bsalomonc6363ef2015-09-24 07:07:40 -07001551static void test_abandoned(skiatest::Reporter* reporter) {
1552 Mock mock(10, 300);
1553 GrContext* context = mock.context();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001554 GrGpu* gpu = context->contextPriv().getGpu();
1555
1556 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001557 context->abandonContext();
1558
1559 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1560
1561 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1562
robertphillips8abb3702016-08-31 14:04:06 -07001563 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001564 resource->getUniqueKey();
1565 resource->wasDestroyed();
1566 resource->gpuMemorySize();
1567 resource->getContext();
1568
1569 resource->abandon();
1570 resource->resourcePriv().getScratchKey();
1571 resource->resourcePriv().isBudgeted();
1572 resource->resourcePriv().makeBudgeted();
1573 resource->resourcePriv().makeUnbudgeted();
1574 resource->resourcePriv().removeScratchKey();
1575 GrUniqueKey key;
1576 make_unique_key<0>(&key, 1);
1577 resource->resourcePriv().setUniqueKey(key);
1578 resource->resourcePriv().removeUniqueKey();
1579}
1580
Brian Salomon1090da62017-01-06 12:04:19 -05001581static void test_tags(skiatest::Reporter* reporter) {
1582#ifdef SK_DEBUG
1583 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1584 static constexpr int kLastTagIdx = 10;
1585 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1586
1587 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1588 GrContext* context = mock.context();
1589 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001590 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001591
1592 SkString tagStr;
1593 int tagIdx = 0;
1594 int currTagCnt = 0;
1595
1596 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001597 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001598 GrUniqueKey key;
1599 if (currTagCnt == tagIdx) {
1600 tagIdx += 1;
1601 currTagCnt = 0;
1602 tagStr.printf("tag%d", tagIdx);
1603 }
1604 make_unique_key<1>(&key, i, tagStr.c_str());
1605 resource->resourcePriv().setUniqueKey(key);
1606 }
1607 SkASSERT(kLastTagIdx == tagIdx);
1608 SkASSERT(currTagCnt == kLastTagIdx);
1609
1610 // Test i = 0 to exercise unused tag string.
1611 for (int i = 0; i <= kLastTagIdx; ++i) {
1612 tagStr.printf("tag%d", i);
1613 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1614 }
1615#endif
1616}
1617
Brian Salomondcfca432017-11-15 15:48:03 -05001618DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001619 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001620 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001621 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001622 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001623 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001624 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001625 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001626 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001627 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001628 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001629 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001630 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001631 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001632 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001633 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001634 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001635 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001636 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001637 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001638 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001639}
1640
Robert Phillipsd6214d42016-11-07 08:23:48 -05001641////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001642static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001643 GrSurfaceFlags flags,
1644 int width, int height,
1645 int sampleCnt) {
1646 GrSurfaceDesc desc;
1647 desc.fFlags = flags;
1648 desc.fWidth = width;
1649 desc.fHeight = height;
1650 desc.fConfig = kRGBA_8888_GrPixelConfig;
1651 desc.fSampleCnt = sampleCnt;
1652
Robert Phillipse78b7252017-04-06 07:59:41 -04001653 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001654}
1655
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001656static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipse78b7252017-04-06 07:59:41 -04001657 GrSurfaceFlags flags,
1658 int width, int height,
1659 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001660 GrSurfaceDesc desc;
1661 desc.fFlags = flags;
1662 desc.fWidth = width;
1663 desc.fHeight = height;
1664 desc.fConfig = kRGBA_8888_GrPixelConfig;
1665 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001666
Brian Salomon2a4f9832018-03-03 22:43:43 -05001667 auto origin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1668 : kTopLeft_GrSurfaceOrigin;
1669
1670 return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001671}
1672
1673// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1674// Texture-only, both-RT-and-Texture and MIPmapped
1675DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1676 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001677 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001678 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001679
Robert Phillipsd6214d42016-11-07 08:23:48 -05001680 static const int kSize = 64;
1681
Robert Phillipsd6214d42016-11-07 08:23:48 -05001682 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001683 {
1684 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001685
Brian Salomonbdecacf2018-02-02 20:32:49 -05001686 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001687 size_t size = tex->gpuMemorySize();
1688 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1689
Brian Salomonbdecacf2018-02-02 20:32:49 -05001690 size_t sampleCount =
1691 (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001692 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001693 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001694 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001695 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001696 REPORTER_ASSERT(reporter,
1697 kSize*kSize*4 == size || // msaa4 failed
1698 kSize*kSize*4*sampleCount == size || // auto-resolving
1699 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001700 }
1701
Brian Salomonbdecacf2018-02-02 20:32:49 -05001702 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001703 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001704 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001705 }
1706
Robert Phillipsd6214d42016-11-07 08:23:48 -05001707
1708 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001709 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001710 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001711
Brian Salomonbdecacf2018-02-02 20:32:49 -05001712 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001713 size_t size = proxy->gpuMemorySize();
1714 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1715
Brian Salomonbdecacf2018-02-02 20:32:49 -05001716 size_t sampleCount =
1717 (size_t)context->caps()->getRenderTargetSampleCount(4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001718 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001719 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001720 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001721 size = proxy->gpuMemorySize();
1722 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001723 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1724 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1725 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001726 }
Robert Phillips1b352562017-04-05 18:56:21 +00001727
Brian Salomonbdecacf2018-02-02 20:32:49 -05001728 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001729 size = proxy->gpuMemorySize();
1730 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1731 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001732}
1733
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001734#endif