blob: af33f2d9bc46fb3b239d7a92d2f563ad468c3559 [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 Phillipsc0192e32017-09-21 12:00:26 -040019#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040023#include "GrTexture.h"
24
bsalomonbcf0a522014-10-08 08:40:09 -070025#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080026#include "SkGr.h"
27#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050028#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070029#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000030#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031
32static const int gWidth = 640;
33static const int gHeight = 480;
34
35////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070036DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070037 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080038 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040039 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080040 desc.fFlags = kRenderTarget_GrSurfaceFlag;
41 desc.fWidth = gWidth;
42 desc.fHeight = gHeight;
43 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070044 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080045 SkCanvas* canvas = surface->getCanvas();
46
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
48
49 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000050 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000051 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040052 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000054 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070055 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000056
57 int oldMaxNum;
58 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000059 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000060
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000061 // Set the cache limits so we can fit 10 "src" images and the
62 // max number of textures doesn't matter
63 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000064 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000065
66 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000067 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000068
69 for (int i = 0; i < 100; ++i) {
70 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040071 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000072
73 // "modify" the src texture
74 src.notifyPixelsChanged();
75
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000076 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070077 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000078
79 // we should never go over the size limit
80 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
81 }
82
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000083 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000084}
85
bsalomon11abd8d2016-10-14 08:13:48 -070086static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
87 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
88 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
89 return false;
90 }
91 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
92}
93
Robert Phillipsc0192e32017-09-21 12:00:26 -040094static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
95 return rt->renderTargetPriv().getStencilAttachment();
96}
97
98static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
99 int size, int sampleCount, SkBudgeted budgeted) {
100 GrSurfaceDesc desc;
101 desc.fFlags = kRenderTarget_GrSurfaceFlag;
102 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
103 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,
123 ctxInfo) {
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
129 GrResourceProvider* provider = context->resourceProvider();
130
131 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kYes);
132 REPORTER_ASSERT(reporter, smallRT0);
133
134 {
135 // Two budgeted RTs with the same desc should share a stencil buffer.
136 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kYes);
137 REPORTER_ASSERT(reporter, smallRT1);
138
139 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.
144 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kNo);
145 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.
152 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(provider, 400, 0, SkBudgeted::kNo);
153 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
Robert Phillipsc0192e32017-09-21 12:00:26 -0400158 int smallSampleCount = context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
159 if (smallSampleCount > 0) {
mtklein5f939ab2016-03-16 10:28:35 -0700160 // An RT with a different sample count should not share.
Robert Phillipsc0192e32017-09-21 12:00:26 -0400161 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(provider, 4, smallSampleCount,
162 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.
176 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(provider, 4, smallSampleCount,
177 SkBudgeted::kNo);
178 REPORTER_ASSERT(reporter, smallMSAART1);
179
180 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800181 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400182
bsalomon02a44a42015-02-19 09:09:00 -0800183 // But not one with a larger sample count should not. (Also check that the request for 4
184 // samples didn't get rounded up to >= 8 or else they could share.).
Robert Phillipsc0192e32017-09-21 12:00:26 -0400185 int bigSampleCount = context->caps()->getSampleCount(8, kRGBA_8888_GrPixelConfig);
186 if (bigSampleCount != smallSampleCount) {
187 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(provider, 4, bigSampleCount,
188 SkBudgeted::kNo);
189 REPORTER_ASSERT(reporter, smallMSAART2);
190
191 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800192 }
193 }
194}
195
bsalomon68d91342016-04-12 09:59:58 -0700196DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700197 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800198 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700199 // this test is only valid for GL
200 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700201 return;
202 }
203
Brian Osman85d34b22017-05-10 12:06:26 -0400204 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205 static const int kW = 100;
206 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700207
bsalomon091f60c2015-11-10 11:54:56 -0800208 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
209 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700210
bsalomon6dc6f5f2015-06-18 09:12:16 -0700211 context->resetContext();
212
Greg Daniel7ef28f32017-04-20 16:41:55 +0000213 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
214 kW,
215 kH,
216 kRGBA_8888_GrPixelConfig,
217 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500218 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400219 backendTex1, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220
Greg Daniel7ef28f32017-04-20 16:41:55 +0000221 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
222 kW,
223 kH,
224 kRGBA_8888_GrPixelConfig,
225 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500226 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400227 backendTex2, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228
Brian Osman85d34b22017-05-10 12:06:26 -0400229 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
230 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700231 return;
232 }
233
halcanary96fcdcc2015-08-27 07:41:13 -0700234 borrowed.reset(nullptr);
235 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700236
237 context->flush();
238
bsalomon091f60c2015-11-10 11:54:56 -0800239 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
240 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241
242 REPORTER_ASSERT(reporter, borrowedIsAlive);
243 REPORTER_ASSERT(reporter, !adoptedIsAlive);
244
bsalomon67d76202015-11-11 12:40:42 -0800245 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
246 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700247
248 context->resetContext();
249}
250
bsalomon6d3fe022014-07-25 08:35:45 -0700251class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800252 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000253public:
robertphillips6e83ac72015-08-13 05:19:14 -0700254 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700255
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 /** Property that distinctly categorizes the resource.
257 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800258 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800259
kkinnunen2e6055b2016-04-22 01:48:29 -0700260 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
261 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700262 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800263 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 , fProperty(kA_SimulatedProperty)
265 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800266 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700267 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800268 }
269
kkinnunen2e6055b2016-04-22 01:48:29 -0700270 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
271 SimulatedProperty property) {
272 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800273 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700274 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
275 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000276 }
277
Brian Salomond3b65972017-03-22 12:05:03 -0400278 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800279 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800280 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000283 void setSize(size_t size) {
284 fSize = size;
285 this->didChangeGpuMemorySize();
286 }
287
bsalomon33435572014-11-05 14:47:41 -0800288 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289
bsalomon71cb0c22014-11-14 12:10:14 -0800290 void setUnrefWhenDestroyed(TestResource* resource) {
291 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000292 }
293
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
295 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
296 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800297 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
298 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800299 }
300 }
301
302 static size_t ExpectedScratchKeySize() {
303 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
304 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000305private:
bsalomon24db3b12015-01-23 04:24:04 -0800306 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800307
kkinnunen2e6055b2016-04-22 01:48:29 -0700308 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
309 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700310 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800311 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700312 , fProperty(property)
313 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 this->registerWithCache(budgeted);
316 }
317
318 // Constructor for simulating resources that wrap backend objects.
319 TestResource(GrGpu* gpu, size_t size)
320 : INHERITED(gpu)
321 , fToDelete(nullptr)
322 , fSize(size)
323 , fProperty(kA_SimulatedProperty)
324 , fIsScratch(false) {
325 ++fNumAlive;
326 this->registerWithCacheWrapped();
327 }
328
329 void computeScratchKey(GrScratchKey* key) const override {
330 if (fIsScratch) {
331 ComputeScratchKey(fProperty, key);
332 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800333 }
334
mtklein36352bf2015-03-25 18:17:31 -0700335 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800336
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000337 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000338 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800339 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800340 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700341 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700342 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000343};
bsalomon33435572014-11-05 14:47:41 -0800344int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000345
bsalomonc2f35b72015-01-23 07:19:22 -0800346class Mock {
347public:
348 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400349 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800350 SkASSERT(fContext);
351 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800352 GrResourceCache* cache = fContext->getResourceCache();
353 cache->purgeAllUnlocked();
354 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800355 }
bsalomonc2f35b72015-01-23 07:19:22 -0800356
bsalomon0ea80f42015-02-11 10:49:59 -0800357 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800358
Hal Canary342b7ac2016-11-04 11:49:42 -0400359 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
361private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400362 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800363};
364
365static void test_no_key(skiatest::Reporter* reporter) {
366 Mock mock(10, 30000);
367 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800368 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800369
370 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700371 TestResource* a = new TestResource(context->getGpu());
372 TestResource* b = new TestResource(context->getGpu());
373 TestResource* c = new TestResource(context->getGpu());
374 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800375 a->setSize(11);
376 b->setSize(12);
377 c->setSize(13);
378 d->setSize(14);
379
380 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800381 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800382 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800383 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800384
385 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800386 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800387 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
388
bsalomon8718aaf2015-02-19 07:24:21 -0800389 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800390
391 a->unref();
392 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800393 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800394 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800395 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800396
397 c->unref();
398 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800399 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800400 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800401 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800402
403 d->unref();
404 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800405 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
406 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800407
408 b->unref();
409 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800410 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
411 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800412}
413
bsalomon24db3b12015-01-23 04:24:04 -0800414// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500415template <int>
416static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800417 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500418 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800419 builder[0] = data;
420}
421
bsalomon84c8e622014-11-17 09:33:27 -0800422static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800423 Mock mock(10, 300);
424 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800425 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800426
bsalomon8718aaf2015-02-19 07:24:21 -0800427 GrUniqueKey uniqueKey;
428 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800429
bsalomon8718aaf2015-02-19 07:24:21 -0800430 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800431 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700432 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800433 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700434 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800435 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800436 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700437 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800438 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700439 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700440 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800441 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800442
Brian Osman0562eb92017-05-08 11:16:39 -0400443 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800444 GrUniqueKey uniqueKey2;
445 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800446 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400447 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
448 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
449
450 // Remove the extra ref we just added.
451 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800452
453 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800454 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800455 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800456 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800457 cache->getResourceBytes());
458 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800459 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800460 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400461 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800462
bsalomon63c992f2015-01-23 12:47:59 -0800463 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800464 cache->purgeAllUnlocked();
465 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800466 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800467 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800468 cache->getResourceBytes());
469 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800470 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800471 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400472 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800473
474 // Unreffing the wrapped resource should free it right away.
475 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800476 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800477 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800478 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400479 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800480
bsalomon84c8e622014-11-17 09:33:27 -0800481 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700482 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800483 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800484 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400485 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800486 cache->purgeAllUnlocked();
487 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800488 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800489 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
490 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
491 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400492 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800493
494 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400495 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800496 cache->purgeAllUnlocked();
497 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800498 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800499 cache->getResourceBytes());
500 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
501 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400502 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800503
504 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800505 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
506 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
507 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
508 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400509 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800510
511 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800512 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
513 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
515 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400516 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800517}
518
bsalomon5236cf42015-01-14 10:42:08 -0800519static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800520 Mock mock(10, 30000);
521 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800522 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800523
bsalomon8718aaf2015-02-19 07:24:21 -0800524 GrUniqueKey uniqueKey;
525 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800526
527 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800528 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800529 TestResource* wrapped;
530 TestResource* unbudgeted;
531
532 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700533 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
534 TestResource::kB_SimulatedProperty);
535
bsalomon5236cf42015-01-14 10:42:08 -0800536 scratch->setSize(10);
537 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800538 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
539 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
540 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
541 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400542 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800543
halcanary385fe4d2015-08-26 13:07:48 -0700544 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800545 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800546 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800547 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800548 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
549 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
550 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
551 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400552 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800553
bsalomon0ea80f42015-02-11 10:49:59 -0800554 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700555 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800556 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
557 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
558 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
559 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400560 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800561
562 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800563 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
564 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
565 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
566 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400567 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800568
kkinnunen2e6055b2016-04-22 01:48:29 -0700569 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800570 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
571 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
572 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
573 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400574 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800575
576 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800577 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
578 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
579 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
580 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400581 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800582
bsalomon0ea80f42015-02-11 10:49:59 -0800583 cache->purgeAllUnlocked();
584 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
585 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
586 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
587 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400588 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800589}
590
bsalomon3582d3e2015-02-13 14:20:05 -0800591// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
592void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
593/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800594 Mock mock(10, 300);
595 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800596 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800597
598 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700599 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
600 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800601 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800602 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800603
604 size_t size = resource->gpuMemorySize();
605 for (int i = 0; i < 2; ++i) {
606 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800607 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800608 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800609 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700610 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800611 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
612 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
614 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400615 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800616
617 // Once it is unrefed, it should become available as scratch.
618 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800619 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
620 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
621 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
622 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400623 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700624 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800625 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800626 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800627 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800628 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800629
630 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700631 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800632 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800633 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800634 } else {
635 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800636 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800637 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
638 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
639 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
640 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400641 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800642 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800643 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800644 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800645
646 // now when it is unrefed it should die since it has no key.
647 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800648 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
649 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
650 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
651 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400652 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800653 }
bsalomon8b79d232014-11-10 10:19:06 -0800654 }
bsalomonc2f35b72015-01-23 07:19:22 -0800655}
656
657static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
658 Mock mock(5, 30000);
659 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800660 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800661
bsalomon8b79d232014-11-10 10:19:06 -0800662 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800663 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700664 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800665 TestResource::kB_SimulatedProperty);
666 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700667 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800668 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800669 a->setSize(11);
670 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800671 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800672 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800673 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700674 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800675
676 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800677 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800678
bsalomon0ea80f42015-02-11 10:49:59 -0800679 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800680 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800681 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
682 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800683 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800684 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800685
bsalomon63c992f2015-01-23 12:47:59 -0800686 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800687 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800688 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800689 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800690
691 // Unref but don't purge
692 a->unref();
693 b->unref();
694 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800695 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800696
bsalomon63c992f2015-01-23 12:47:59 -0800697 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800698 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800699 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800700 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
701 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800702}
703
bsalomon10e23ca2014-11-25 05:52:06 -0800704static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800705 Mock mock(5, 30000);
706 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800707 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800708
bsalomon10e23ca2014-11-25 05:52:06 -0800709 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700710 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800711 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700712 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800713 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800714 a->unref();
715 b->unref();
716
bsalomon1c60dfe2015-01-21 09:32:40 -0800717 GrScratchKey scratchKey;
718 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800719 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800720 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700721 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800722
bsalomon0ea80f42015-02-11 10:49:59 -0800723 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800724 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800725 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800726 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
727 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800728
729 // Find the first resource and remove its scratch key
730 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700731 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800732 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800733 // It's still alive, but not cached by scratch key anymore
734 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800735 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
736 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800737
738 // The cache should immediately delete it when it's unrefed since it isn't accessible.
739 find->unref();
740 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800741 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
742 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800743
744 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700745 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800746 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800747 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800748 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
749 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800750
751 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800752 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800753 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800754 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
755 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800756
757 find->unref();
758 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800759 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
760 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800761}
762
bsalomon1c60dfe2015-01-21 09:32:40 -0800763static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800764 Mock mock(5, 30000);
765 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800766 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800767
768 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700769 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800770 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700771 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800772 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800773 a->unref();
774 b->unref();
775
776 GrScratchKey scratchKey;
777 // Ensure that scratch key comparison and assignment is consistent.
778 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800779 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800780 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800781 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800782 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
783 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
784 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
785 scratchKey = scratchKey1;
786 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
787 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
788 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
789 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
790 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
791 scratchKey = scratchKey2;
792 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
793 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
794 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
795 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
796 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
797
798 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800799 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800800 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700801 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800802
803 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800804 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700805 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700806 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800807 find->unref();
808
809 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700810 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700811 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800812 REPORTER_ASSERT(reporter, find == a || find == b);
813
robertphillips6e83ac72015-08-13 05:19:14 -0700814 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700815 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800816 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
817 REPORTER_ASSERT(reporter, find2 != find);
818 find2->unref();
819 find->unref();
820}
821
bsalomon8718aaf2015-02-19 07:24:21 -0800822static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800823 Mock mock(5, 30000);
824 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800825 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000826
bsalomon8718aaf2015-02-19 07:24:21 -0800827 GrUniqueKey key;
828 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700829
bsalomon8718aaf2015-02-19 07:24:21 -0800830 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700831 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800832 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700833
bsalomonf99e9612015-02-19 08:24:16 -0800834 // Set key on resource a.
835 a->resourcePriv().setUniqueKey(key);
836 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
837 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800838
bsalomonf99e9612015-02-19 08:24:16 -0800839 // Make sure that redundantly setting a's key works.
840 a->resourcePriv().setUniqueKey(key);
841 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800842 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800843 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
844 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800845 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
846
bsalomonf99e9612015-02-19 08:24:16 -0800847 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700848 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800849 b->setSize(12);
850 b->resourcePriv().setUniqueKey(key);
851 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
852 b->unref();
853
854 // Still have two resources because a is still reffed.
855 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
856 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
857 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
858
859 a->unref();
860 // Now a should be gone.
861 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
862 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
863 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
864
865 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
866 // Also make b be unreffed when replacement occurs.
867 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700868 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800869 GrUniqueKey differentKey;
870 make_unique_key<0>(&differentKey, 1);
871 c->setSize(13);
872 c->resourcePriv().setUniqueKey(differentKey);
873 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
874 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
875 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
876 // c replaces b and b should be immediately purged.
877 c->resourcePriv().setUniqueKey(key);
878 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
879 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
880 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
881
882 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800883 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800884 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
885 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
886 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
887
888 // Drop the ref on c, it should be kept alive because it has a unique key.
889 c->unref();
890 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
891 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
892 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
893
894 // Verify that we can find c, then remove its unique key. It should get purged immediately.
895 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
896 c->resourcePriv().removeUniqueKey();
897 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800898 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
899 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800900 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700901
902 {
903 GrUniqueKey key2;
904 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400905 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700906 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700907 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700908 d->resourcePriv().setUniqueKey(key2);
909 }
910
911 GrUniqueKey key3;
912 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400913 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700914 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000915}
916
bsalomon8b79d232014-11-10 10:19:06 -0800917static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800918 Mock mock(5, 30000);
919 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800920 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800921
bsalomon8718aaf2015-02-19 07:24:21 -0800922 GrUniqueKey key1, key2, key3;
923 make_unique_key<0>(&key1, 1);
924 make_unique_key<0>(&key2, 2);
925 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700926
bsalomon23e619c2015-02-06 11:54:28 -0800927 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700928 TestResource* a = new TestResource(context->getGpu());
929 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700930 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800931 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800932 a->resourcePriv().setUniqueKey(key1);
933 b->resourcePriv().setUniqueKey(key2);
934 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800935 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800936 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800937 c->unref();
938
bsalomon8718aaf2015-02-19 07:24:21 -0800939 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
940 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
941 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800942 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800943
bsalomon8718aaf2015-02-19 07:24:21 -0800944 typedef GrUniqueKeyInvalidatedMessage Msg;
945 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800946
947 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
948 Bus::Post(Msg(key1));
949 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800950 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800951 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800952 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
953 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800954 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800955 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800956
957 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800958 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800959 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800960 // we still have a ref on b, c should be recycled as scratch.
961 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800962 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800963
bsalomon23e619c2015-02-06 11:54:28 -0800964 // make b purgeable. It should be immediately deleted since it has no key.
965 b->unref();
966 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
967
968 // Make sure we actually get to c via it's scratch key, before we say goodbye.
969 GrScratchKey scratchKey;
970 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700971 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800972 REPORTER_ASSERT(reporter, scratch == c);
973 SkSafeUnref(scratch);
974
975 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800976 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700977 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800978 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800979 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
980 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800981 REPORTER_ASSERT(reporter, !scratch);
982 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800983}
984
bsalomon71cb0c22014-11-14 12:10:14 -0800985static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800986 Mock mock(3, 30000);
987 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800988 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800989
bsalomon8718aaf2015-02-19 07:24:21 -0800990 GrUniqueKey key1, key2;
991 make_unique_key<0>(&key1, 1);
992 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000993
halcanary385fe4d2015-08-26 13:07:48 -0700994 TestResource* a = new TestResource(context->getGpu());
995 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800996 a->resourcePriv().setUniqueKey(key1);
997 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800998
bsalomonc2f35b72015-01-23 07:19:22 -0800999 // Make a cycle
1000 a->setUnrefWhenDestroyed(b);
1001 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001002
bsalomonc2f35b72015-01-23 07:19:22 -08001003 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001004
bsalomonc2f35b72015-01-23 07:19:22 -08001005 a->unref();
1006 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001007
bsalomonc2f35b72015-01-23 07:19:22 -08001008 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001009
bsalomon0ea80f42015-02-11 10:49:59 -08001010 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001011 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001012
bsalomonc2f35b72015-01-23 07:19:22 -08001013 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001014 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001015 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001016
bsalomon0ea80f42015-02-11 10:49:59 -08001017 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001018 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001019}
1020
bsalomon8b79d232014-11-10 10:19:06 -08001021static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001022 GrUniqueKey key1, key2;
1023 make_unique_key<0>(&key1, 1);
1024 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001025
1026 // Test changing resources sizes (both increase & decrease).
1027 {
bsalomonc2f35b72015-01-23 07:19:22 -08001028 Mock mock(3, 30000);
1029 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001030 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001031
halcanary385fe4d2015-08-26 13:07:48 -07001032 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001033 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001034 a->unref();
1035
halcanary385fe4d2015-08-26 13:07:48 -07001036 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001037 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001038 b->unref();
1039
bsalomon0ea80f42015-02-11 10:49:59 -08001040 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1041 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001042 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001043 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001044 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001045 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001046 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001047 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001048 find1->setSize(50);
1049 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001050
bsalomon0ea80f42015-02-11 10:49:59 -08001051 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1052 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001053 }
1054
1055 // Test increasing a resources size beyond the cache budget.
1056 {
bsalomonc2f35b72015-01-23 07:19:22 -08001057 Mock mock(2, 300);
1058 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001059 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001060
halcanary385fe4d2015-08-26 13:07:48 -07001061 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001062 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001063 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001064 a->unref();
1065
halcanary385fe4d2015-08-26 13:07:48 -07001066 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001067 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001068 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001069 b->unref();
1070
bsalomon0ea80f42015-02-11 10:49:59 -08001071 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1072 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001073
bsalomon8b79d232014-11-10 10:19:06 -08001074 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001075 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001076 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001077 find2->setSize(201);
1078 }
bsalomon8718aaf2015-02-19 07:24:21 -08001079 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001080
bsalomon0ea80f42015-02-11 10:49:59 -08001081 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1082 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001083 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001084}
1085
bsalomonddf30e62015-02-19 11:38:44 -08001086static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1087 static const int kCount = 50;
1088 static const int kBudgetCnt = kCount / 2;
1089 static const int kLockedFreq = 8;
1090 static const int kBudgetSize = 0x80000000;
1091
1092 SkRandom random;
1093
1094 // Run the test 2*kCount times;
1095 for (int i = 0; i < 2 * kCount; ++i ) {
1096 Mock mock(kBudgetCnt, kBudgetSize);
1097 GrContext* context = mock.context();
1098 GrResourceCache* cache = mock.cache();
1099
1100 // Pick a random number of resources to add before the timestamp will wrap.
1101 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1102
1103 static const int kNumToPurge = kCount - kBudgetCnt;
1104
1105 SkTDArray<int> shouldPurgeIdxs;
1106 int purgeableCnt = 0;
1107 SkTDArray<GrGpuResource*> resourcesToUnref;
1108
1109 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1110 // unpurgeable resources.
1111 for (int j = 0; j < kCount; ++j) {
1112 GrUniqueKey key;
1113 make_unique_key<0>(&key, j);
1114
halcanary385fe4d2015-08-26 13:07:48 -07001115 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001116 r->resourcePriv().setUniqueKey(key);
1117 if (random.nextU() % kLockedFreq) {
1118 // Make this is purgeable.
1119 r->unref();
1120 ++purgeableCnt;
1121 if (purgeableCnt <= kNumToPurge) {
1122 *shouldPurgeIdxs.append() = j;
1123 }
1124 } else {
1125 *resourcesToUnref.append() = r;
1126 }
1127 }
1128
1129 // Verify that the correct resources were purged.
1130 int currShouldPurgeIdx = 0;
1131 for (int j = 0; j < kCount; ++j) {
1132 GrUniqueKey key;
1133 make_unique_key<0>(&key, j);
1134 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1135 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1136 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1137 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001138 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001139 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001140 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001141 }
1142 SkSafeUnref(res);
1143 }
1144
1145 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1146 resourcesToUnref[j]->unref();
1147 }
1148 }
1149}
1150
bsalomon3f324322015-04-08 11:01:54 -07001151static void test_flush(skiatest::Reporter* reporter) {
1152 Mock mock(1000000, 1000000);
1153 GrContext* context = mock.context();
1154 GrResourceCache* cache = mock.cache();
1155
1156 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1157 // power of two here to keep things simpler.
1158 static const int kFlushCount = 16;
1159 cache->setLimits(1000000, 1000000, kFlushCount);
1160
1161 {
1162 // Insert a resource and send a flush notification kFlushCount times.
1163 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001164 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001165 GrUniqueKey k;
1166 make_unique_key<1>(&k, i);
1167 r->resourcePriv().setUniqueKey(k);
1168 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001169 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001170 }
1171
1172 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001173 for (int i = 0; i < kFlushCount; ++i) {
1174 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001175 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1176 for (int j = 0; j < i; ++j) {
1177 GrUniqueKey k;
1178 make_unique_key<1>(&k, j);
1179 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1180 REPORTER_ASSERT(reporter, !SkToBool(r));
1181 SkSafeUnref(r);
1182 }
bsalomon3f324322015-04-08 11:01:54 -07001183 }
1184
1185 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1186 cache->purgeAllUnlocked();
1187 }
1188
1189 // Do a similar test but where we leave refs on some resources to prevent them from being
1190 // purged.
1191 {
1192 GrGpuResource* refedResources[kFlushCount >> 1];
1193 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001194 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001195 GrUniqueKey k;
1196 make_unique_key<1>(&k, i);
1197 r->resourcePriv().setUniqueKey(k);
1198 // Leave a ref on every other resource, beginning with the first.
1199 if (SkToBool(i & 0x1)) {
1200 refedResources[i/2] = r;
1201 } else {
1202 r->unref();
1203 }
bsalomonb77a9072016-09-07 10:02:04 -07001204 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001205 }
1206
1207 for (int i = 0; i < kFlushCount; ++i) {
1208 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001209 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001210 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001211 }
1212
1213 // Unref all the resources that we kept refs on in the first loop.
1214 for (int i = 0; i < kFlushCount >> 1; ++i) {
1215 refedResources[i]->unref();
1216 }
1217
bsalomone2e87f32016-09-22 12:42:11 -07001218 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1219 // kFlushCount full flushes.
1220 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001221 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001222 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001223 }
1224 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1225
1226 cache->purgeAllUnlocked();
1227 }
1228
1229 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001230
1231 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1232 // eviction.
1233 context->flush();
1234 for (int i = 0; i < 10; ++i) {
1235 TestResource* r = new TestResource(context->getGpu());
1236 GrUniqueKey k;
1237 make_unique_key<1>(&k, i);
1238 r->resourcePriv().setUniqueKey(k);
1239 r->unref();
1240 }
1241 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1242 for (int i = 0; i < 10 * kFlushCount; ++i) {
1243 context->flush();
1244 }
1245 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001246}
1247
Brian Salomon5e150852017-03-22 14:53:13 -04001248static void test_time_purge(skiatest::Reporter* reporter) {
1249 Mock mock(1000000, 1000000);
1250 GrContext* context = mock.context();
1251 GrResourceCache* cache = mock.cache();
1252
1253 static constexpr int kCnts[] = {1, 10, 1024};
1254 auto nowish = []() {
1255 // We sleep so that we ensure we get a value that is greater than the last call to
1256 // GrStdSteadyClock::now().
1257 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1258 auto result = GrStdSteadyClock::now();
1259 // Also sleep afterwards so we don't get this value again.
1260 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1261 return result;
1262 };
1263
1264 for (int cnt : kCnts) {
1265 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1266 new GrStdSteadyClock::time_point[cnt]);
1267 {
1268 // Insert resources and get time points between each addition.
1269 for (int i = 0; i < cnt; ++i) {
1270 TestResource* r = new TestResource(context->getGpu());
1271 GrUniqueKey k;
1272 make_unique_key<1>(&k, i);
1273 r->resourcePriv().setUniqueKey(k);
1274 r->unref();
1275 timeStamps.get()[i] = nowish();
1276 }
1277
1278 // Purge based on the time points between resource additions. Each purge should remove
1279 // the oldest resource.
1280 for (int i = 0; i < cnt; ++i) {
1281 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1282 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1283 for (int j = 0; j < i; ++j) {
1284 GrUniqueKey k;
1285 make_unique_key<1>(&k, j);
1286 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1287 REPORTER_ASSERT(reporter, !SkToBool(r));
1288 SkSafeUnref(r);
1289 }
1290 }
1291
1292 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1293 cache->purgeAllUnlocked();
1294 }
1295
1296 // Do a similar test but where we leave refs on some resources to prevent them from being
1297 // purged.
1298 {
1299 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1300 for (int i = 0; i < cnt; ++i) {
1301 TestResource* r = new TestResource(context->getGpu());
1302 GrUniqueKey k;
1303 make_unique_key<1>(&k, i);
1304 r->resourcePriv().setUniqueKey(k);
1305 // Leave a ref on every other resource, beginning with the first.
1306 if (SkToBool(i & 0x1)) {
1307 refedResources.get()[i / 2] = r;
1308 } else {
1309 r->unref();
1310 }
1311 timeStamps.get()[i] = nowish();
1312 }
1313
1314 for (int i = 0; i < cnt; ++i) {
1315 // Should get a resource purged every other frame.
1316 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1317 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1318 }
1319
1320 // Unref all the resources that we kept refs on in the first loop.
1321 for (int i = 0; i < (cnt / 2); ++i) {
1322 refedResources.get()[i]->unref();
1323 cache->purgeResourcesNotUsedSince(nowish());
1324 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1325 }
1326
1327 cache->purgeAllUnlocked();
1328 }
1329
1330 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1331
1332 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1333 // eviction
1334 context->flush();
1335 for (int i = 0; i < 10; ++i) {
1336 TestResource* r = new TestResource(context->getGpu());
1337 GrUniqueKey k;
1338 make_unique_key<1>(&k, i);
1339 r->resourcePriv().setUniqueKey(k);
1340 r->unref();
1341 }
1342 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1343 context->flush();
1344 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1345 cache->purgeResourcesNotUsedSince(nowish());
1346 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1347 }
1348}
1349
Derek Sollenberger5480a182017-05-25 16:43:59 -04001350static void test_partial_purge(skiatest::Reporter* reporter) {
1351 Mock mock(6, 100);
1352 GrContext* context = mock.context();
1353 GrResourceCache* cache = mock.cache();
1354
1355 enum TestsCase {
1356 kOnlyScratch_TestCase = 0,
1357 kPartialScratch_TestCase = 1,
1358 kAllScratch_TestCase = 2,
1359 kPartial_TestCase = 3,
1360 kAll_TestCase = 4,
1361 kNone_TestCase = 5,
1362 kEndTests_TestCase = kNone_TestCase + 1
1363 };
1364
1365 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1366
1367 GrUniqueKey key1, key2, key3;
1368 make_unique_key<0>(&key1, 1);
1369 make_unique_key<0>(&key2, 2);
1370 make_unique_key<0>(&key3, 3);
1371
1372 // Add three unique resources to the cache.
1373 TestResource *unique1 = new TestResource(context->getGpu());
1374 TestResource *unique2 = new TestResource(context->getGpu());
1375 TestResource *unique3 = new TestResource(context->getGpu());
1376
1377 unique1->resourcePriv().setUniqueKey(key1);
1378 unique2->resourcePriv().setUniqueKey(key2);
1379 unique3->resourcePriv().setUniqueKey(key3);
1380
1381 unique1->setSize(10);
1382 unique2->setSize(11);
1383 unique3->setSize(12);
1384
1385 // Add two scratch resources to the cache.
1386 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1387 TestResource::kA_SimulatedProperty);
1388 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1389 TestResource::kB_SimulatedProperty);
1390 scratch1->setSize(13);
1391 scratch2->setSize(14);
1392
1393
1394 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1395 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1396 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1397
1398 // Add resources to the purgeable queue
1399 unique1->unref();
1400 scratch1->unref();
1401 unique2->unref();
1402 scratch2->unref();
1403 unique3->unref();
1404
1405 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1406 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1407 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1408
1409 switch(testCase) {
1410 case kOnlyScratch_TestCase: {
1411 context->purgeUnlockedResources(14, true);
1412 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1413 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1414 break;
1415 }
1416 case kPartialScratch_TestCase: {
1417 context->purgeUnlockedResources(3, true);
1418 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1419 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1420 break;
1421 }
1422 case kAllScratch_TestCase: {
1423 context->purgeUnlockedResources(50, true);
1424 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1425 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1426 break;
1427 }
1428 case kPartial_TestCase: {
1429 context->purgeUnlockedResources(13, false);
1430 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1431 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1432 break;
1433 }
1434 case kAll_TestCase: {
1435 context->purgeUnlockedResources(50, false);
1436 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1437 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1438 break;
1439 }
1440 case kNone_TestCase: {
1441 context->purgeUnlockedResources(0, true);
1442 context->purgeUnlockedResources(0, false);
1443 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1444 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1445 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1446 break;
1447 }
1448 };
1449
1450 // ensure all are purged before the next
1451 context->purgeAllUnlockedResources();
1452 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1453 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1454
1455 }
1456}
1457
bsalomon10e23ca2014-11-25 05:52:06 -08001458static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001459 // Set the cache size to double the resource count because we're going to create 2x that number
1460 // resources, using two different key domains. Add a little slop to the bytes because we resize
1461 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001462 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001463
bsalomonc2f35b72015-01-23 07:19:22 -08001464 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1465 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001466 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001467
1468 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001469 GrUniqueKey key1, key2;
1470 make_unique_key<1>(&key1, i);
1471 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001472
bsalomon24db3b12015-01-23 04:24:04 -08001473 TestResource* resource;
1474
halcanary385fe4d2015-08-26 13:07:48 -07001475 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001476 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001477 resource->setSize(1);
1478 resource->unref();
1479
halcanary385fe4d2015-08-26 13:07:48 -07001480 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001481 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001482 resource->setSize(1);
1483 resource->unref();
1484 }
1485
1486 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001487 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001488 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1489 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1490 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1491 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001492 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001493 GrUniqueKey key1, key2;
1494 make_unique_key<1>(&key1, i);
1495 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001496
bsalomon8718aaf2015-02-19 07:24:21 -08001497 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1498 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001499 }
1500
bsalomon0ea80f42015-02-11 10:49:59 -08001501 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001502 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001503 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001504 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1505 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1506 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1507 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001508
1509 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001510 GrUniqueKey key1, key2;
1511 make_unique_key<1>(&key1, i);
1512 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001513
bsalomon8718aaf2015-02-19 07:24:21 -08001514 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1515 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001516 }
1517}
1518
senorblanco84cd6212015-08-04 10:01:58 -07001519static void test_custom_data(skiatest::Reporter* reporter) {
1520 GrUniqueKey key1, key2;
1521 make_unique_key<0>(&key1, 1);
1522 make_unique_key<0>(&key2, 2);
1523 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001524 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001525 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1526 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1527
1528 // Test that copying a key also takes a ref on its custom data.
1529 GrUniqueKey key3 = key1;
1530 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1531}
1532
bsalomonc6363ef2015-09-24 07:07:40 -07001533static void test_abandoned(skiatest::Reporter* reporter) {
1534 Mock mock(10, 300);
1535 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001536 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001537 context->abandonContext();
1538
1539 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1540
1541 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1542
robertphillips8abb3702016-08-31 14:04:06 -07001543 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001544 resource->getUniqueKey();
1545 resource->wasDestroyed();
1546 resource->gpuMemorySize();
1547 resource->getContext();
1548
1549 resource->abandon();
1550 resource->resourcePriv().getScratchKey();
1551 resource->resourcePriv().isBudgeted();
1552 resource->resourcePriv().makeBudgeted();
1553 resource->resourcePriv().makeUnbudgeted();
1554 resource->resourcePriv().removeScratchKey();
1555 GrUniqueKey key;
1556 make_unique_key<0>(&key, 1);
1557 resource->resourcePriv().setUniqueKey(key);
1558 resource->resourcePriv().removeUniqueKey();
1559}
1560
Brian Salomon1090da62017-01-06 12:04:19 -05001561static void test_tags(skiatest::Reporter* reporter) {
1562#ifdef SK_DEBUG
1563 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1564 static constexpr int kLastTagIdx = 10;
1565 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1566
1567 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1568 GrContext* context = mock.context();
1569 GrResourceCache* cache = mock.cache();
1570
1571 SkString tagStr;
1572 int tagIdx = 0;
1573 int currTagCnt = 0;
1574
1575 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1576 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1577 GrUniqueKey key;
1578 if (currTagCnt == tagIdx) {
1579 tagIdx += 1;
1580 currTagCnt = 0;
1581 tagStr.printf("tag%d", tagIdx);
1582 }
1583 make_unique_key<1>(&key, i, tagStr.c_str());
1584 resource->resourcePriv().setUniqueKey(key);
1585 }
1586 SkASSERT(kLastTagIdx == tagIdx);
1587 SkASSERT(currTagCnt == kLastTagIdx);
1588
1589 // Test i = 0 to exercise unused tag string.
1590 for (int i = 0; i <= kLastTagIdx; ++i) {
1591 tagStr.printf("tag%d", i);
1592 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1593 }
1594#endif
1595}
1596
kkinnunen15302832015-12-01 04:35:26 -08001597DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001598 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001599 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001600 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001601 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001602 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001603 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001604 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001605 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001606 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001607 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001608 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001609 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001610 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001611 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001612 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001613 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001614 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001615 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001616 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001617 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001618}
1619
Robert Phillipsd6214d42016-11-07 08:23:48 -05001620////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001621static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001622 GrSurfaceFlags flags,
1623 int width, int height,
1624 int sampleCnt) {
1625 GrSurfaceDesc desc;
1626 desc.fFlags = flags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001627 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628 desc.fWidth = width;
1629 desc.fHeight = height;
1630 desc.fConfig = kRGBA_8888_GrPixelConfig;
1631 desc.fSampleCnt = sampleCnt;
1632
Robert Phillipse78b7252017-04-06 07:59:41 -04001633 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001634}
1635
Robert Phillipse78b7252017-04-06 07:59:41 -04001636static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1637 GrSurfaceFlags flags,
1638 int width, int height,
1639 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001640 SkBitmap bm;
1641
1642 bm.allocN32Pixels(width, height, true);
1643 bm.eraseColor(SK_ColorBLUE);
1644
Brian Osman7b8400d2016-11-08 17:08:54 -05001645 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001646 SkASSERT(mipmaps);
1647 SkASSERT(mipmaps->countLevels() > 1);
1648
1649 int mipLevelCount = mipmaps->countLevels() + 1;
1650
1651 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1652
1653 texels[0].fPixels = bm.getPixels();
1654 texels[0].fRowBytes = bm.rowBytes();
1655
1656 for (int i = 1; i < mipLevelCount; ++i) {
1657 SkMipMap::Level generatedMipLevel;
1658 mipmaps->getLevel(i - 1, &generatedMipLevel);
1659 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1660 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1661 }
1662
1663 GrSurfaceDesc desc;
1664 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001665 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1666 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001667 desc.fWidth = width;
1668 desc.fHeight = height;
1669 desc.fConfig = kRGBA_8888_GrPixelConfig;
1670 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001671
Robert Phillips8e8c7552017-07-10 12:06:05 -04001672 return GrSurfaceProxy::MakeDeferredMipMap(provider, desc, SkBudgeted::kYes,
1673 texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001674}
1675
1676// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1677// Texture-only, both-RT-and-Texture and MIPmapped
1678DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1679 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001680 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001681
Robert Phillipsd6214d42016-11-07 08:23:48 -05001682 static const int kSize = 64;
1683
Robert Phillipsd6214d42016-11-07 08:23:48 -05001684 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001685 {
1686 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001687
Robert Phillipse78b7252017-04-06 07:59:41 -04001688 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1689 size_t size = tex->gpuMemorySize();
1690 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1691
Greg Daniel81e7bf82017-07-19 14:47:42 -04001692 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1693 if (sampleCount >= 4) {
1694 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1695 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001696 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001697 REPORTER_ASSERT(reporter,
1698 kSize*kSize*4 == size || // msaa4 failed
1699 kSize*kSize*4*sampleCount == size || // auto-resolving
1700 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001701 }
1702
1703 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001704 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001705 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001706 }
1707
Robert Phillipsd6214d42016-11-07 08:23:48 -05001708
1709 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001710 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001711 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001712
Robert Phillipse78b7252017-04-06 07:59:41 -04001713 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1714 size_t size = proxy->gpuMemorySize();
1715 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1716
Greg Daniel81e7bf82017-07-19 14:47:42 -04001717 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1718 if (sampleCount >= 4) {
1719 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1720 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
Robert Phillipse78b7252017-04-06 07:59:41 -04001728 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1729 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