blob: 0d977796cdf5681335eec6ed6bf501f08ba5cf1f [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,
Robert Phillipsec325342017-10-30 18:02:48 +0000123 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700124 GrContext* context = ctxInfo.grContext();
Eric Karl5c779752017-05-08 12:02:07 -0700125 if (context->caps()->avoidStencilBuffers()) {
126 return;
127 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128
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,
Greg Daniel177e6952017-10-12 12:27:11 -0400217 GrMipMapped::kNo,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000218 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500219 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400220 backendTex1, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
223 kW,
224 kH,
225 kRGBA_8888_GrPixelConfig,
Greg Daniel177e6952017-10-12 12:27:11 -0400226 GrMipMapped::kNo,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000227 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500228 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400229 backendTex2, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
Brian Osman85d34b22017-05-10 12:06:26 -0400231 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
232 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233 return;
234 }
235
halcanary96fcdcc2015-08-27 07:41:13 -0700236 borrowed.reset(nullptr);
237 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700238
239 context->flush();
240
bsalomon091f60c2015-11-10 11:54:56 -0800241 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
242 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700243
244 REPORTER_ASSERT(reporter, borrowedIsAlive);
245 REPORTER_ASSERT(reporter, !adoptedIsAlive);
246
bsalomon67d76202015-11-11 12:40:42 -0800247 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
248 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700249
250 context->resetContext();
251}
252
bsalomon6d3fe022014-07-25 08:35:45 -0700253class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800254 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000255public:
robertphillips6e83ac72015-08-13 05:19:14 -0700256 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700257
bsalomon1c60dfe2015-01-21 09:32:40 -0800258 /** Property that distinctly categorizes the resource.
259 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800260 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800261
kkinnunen2e6055b2016-04-22 01:48:29 -0700262 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
263 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700264 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800265 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700266 , fProperty(kA_SimulatedProperty)
267 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800268 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700269 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800270 }
271
kkinnunen2e6055b2016-04-22 01:48:29 -0700272 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
273 SimulatedProperty property) {
274 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800275 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700276 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
277 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278 }
279
Brian Salomond3b65972017-03-22 12:05:03 -0400280 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800281 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800282 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283 }
284
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000285 void setSize(size_t size) {
286 fSize = size;
287 this->didChangeGpuMemorySize();
288 }
289
bsalomon33435572014-11-05 14:47:41 -0800290 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000291
bsalomon71cb0c22014-11-14 12:10:14 -0800292 void setUnrefWhenDestroyed(TestResource* resource) {
293 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294 }
295
bsalomon1c60dfe2015-01-21 09:32:40 -0800296 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
297 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
298 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800299 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
300 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800301 }
302 }
303
304 static size_t ExpectedScratchKeySize() {
305 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
306 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000307private:
bsalomon24db3b12015-01-23 04:24:04 -0800308 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800309
kkinnunen2e6055b2016-04-22 01:48:29 -0700310 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
311 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700312 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800313 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700314 , fProperty(property)
315 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800316 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700317 this->registerWithCache(budgeted);
318 }
319
320 // Constructor for simulating resources that wrap backend objects.
321 TestResource(GrGpu* gpu, size_t size)
322 : INHERITED(gpu)
323 , fToDelete(nullptr)
324 , fSize(size)
325 , fProperty(kA_SimulatedProperty)
326 , fIsScratch(false) {
327 ++fNumAlive;
328 this->registerWithCacheWrapped();
329 }
330
331 void computeScratchKey(GrScratchKey* key) const override {
332 if (fIsScratch) {
333 ComputeScratchKey(fProperty, key);
334 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800335 }
336
mtklein36352bf2015-03-25 18:17:31 -0700337 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800338
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000339 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000340 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800341 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800342 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700343 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700344 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000345};
bsalomon33435572014-11-05 14:47:41 -0800346int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000347
bsalomonc2f35b72015-01-23 07:19:22 -0800348class Mock {
349public:
350 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400351 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800352 SkASSERT(fContext);
353 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800354 GrResourceCache* cache = fContext->getResourceCache();
355 cache->purgeAllUnlocked();
356 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800357 }
bsalomonc2f35b72015-01-23 07:19:22 -0800358
bsalomon0ea80f42015-02-11 10:49:59 -0800359 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
Hal Canary342b7ac2016-11-04 11:49:42 -0400361 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800362
363private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400364 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800365};
366
367static void test_no_key(skiatest::Reporter* reporter) {
368 Mock mock(10, 30000);
369 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800370 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800371
372 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700373 TestResource* a = new TestResource(context->getGpu());
374 TestResource* b = new TestResource(context->getGpu());
375 TestResource* c = new TestResource(context->getGpu());
376 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800377 a->setSize(11);
378 b->setSize(12);
379 c->setSize(13);
380 d->setSize(14);
381
382 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800383 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800384 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800385 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386
387 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800388 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800389 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
390
bsalomon8718aaf2015-02-19 07:24:21 -0800391 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800392
393 a->unref();
394 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800395 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800396 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800397 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800398
399 c->unref();
400 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800401 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800402 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800403 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404
405 d->unref();
406 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800407 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
408 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800409
410 b->unref();
411 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
413 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800414}
415
bsalomon24db3b12015-01-23 04:24:04 -0800416// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500417template <int>
418static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800419 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500420 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800421 builder[0] = data;
422}
423
bsalomon84c8e622014-11-17 09:33:27 -0800424static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800425 Mock mock(10, 300);
426 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800427 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 GrUniqueKey uniqueKey;
430 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800431
bsalomon8718aaf2015-02-19 07:24:21 -0800432 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800433 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700434 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800435 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700436 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800437 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800438 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700439 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800440 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700441 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700442 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800443 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800444
Brian Osman0562eb92017-05-08 11:16:39 -0400445 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800446 GrUniqueKey uniqueKey2;
447 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800448 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400449 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
450 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
451
452 // Remove the extra ref we just added.
453 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800454
455 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800456 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800458 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800459 cache->getResourceBytes());
460 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800461 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800462 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400463 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800464
bsalomon63c992f2015-01-23 12:47:59 -0800465 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800466 cache->purgeAllUnlocked();
467 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800468 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800469 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800472 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400474 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800475
476 // Unreffing the wrapped resource should free it right away.
477 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800478 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800479 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800480 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400481 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800482
bsalomon84c8e622014-11-17 09:33:27 -0800483 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700484 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800485 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800486 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400487 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800488 cache->purgeAllUnlocked();
489 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800490 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800491 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
492 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
493 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400494 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800495
496 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400497 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800498 cache->purgeAllUnlocked();
499 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800500 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800501 cache->getResourceBytes());
502 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
503 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400504 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800505
506 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800507 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
508 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
509 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
510 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400511 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800512
513 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800514 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
515 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
516 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
517 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400518 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800519}
520
bsalomon5236cf42015-01-14 10:42:08 -0800521static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800522 Mock mock(10, 30000);
523 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800524 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800525
bsalomon8718aaf2015-02-19 07:24:21 -0800526 GrUniqueKey uniqueKey;
527 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800528
529 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800530 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800531 TestResource* wrapped;
532 TestResource* unbudgeted;
533
534 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700535 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
536 TestResource::kB_SimulatedProperty);
537
bsalomon5236cf42015-01-14 10:42:08 -0800538 scratch->setSize(10);
539 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800540 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
541 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
542 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
543 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400544 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800545
halcanary385fe4d2015-08-26 13:07:48 -0700546 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800547 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800548 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800549 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800550 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400554 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800555
bsalomon0ea80f42015-02-11 10:49:59 -0800556 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700557 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800558 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
559 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
560 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
561 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400562 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800563
564 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800565 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
566 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
567 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
568 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400569 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800570
kkinnunen2e6055b2016-04-22 01:48:29 -0700571 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800572 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
573 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
574 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
575 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400576 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800577
578 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800579 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
580 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
581 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
582 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400583 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800584
bsalomon0ea80f42015-02-11 10:49:59 -0800585 cache->purgeAllUnlocked();
586 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
587 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
588 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
589 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400590 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800591}
592
bsalomon3582d3e2015-02-13 14:20:05 -0800593// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
594void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
595/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800596 Mock mock(10, 300);
597 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800599
600 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700601 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
602 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800603 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800604 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800605
606 size_t size = resource->gpuMemorySize();
607 for (int i = 0; i < 2; ++i) {
608 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800609 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800610 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800611 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700612 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800613 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
614 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
615 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
616 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400617 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800618
619 // Once it is unrefed, it should become available as scratch.
620 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800621 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
622 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
623 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
624 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400625 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700626 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800627 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800628 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800629 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800630 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800631
632 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700633 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800634 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800635 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800636 } else {
637 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800638 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800639 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
640 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
641 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
642 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400643 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800644 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800645 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800646 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800647
648 // now when it is unrefed it should die since it has no key.
649 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800650 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
651 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
652 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
653 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400654 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800655 }
bsalomon8b79d232014-11-10 10:19:06 -0800656 }
bsalomonc2f35b72015-01-23 07:19:22 -0800657}
658
659static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
660 Mock mock(5, 30000);
661 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800662 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800663
bsalomon8b79d232014-11-10 10:19:06 -0800664 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800665 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700666 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800667 TestResource::kB_SimulatedProperty);
668 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700669 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800670 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800671 a->setSize(11);
672 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800673 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800674 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800675 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700676 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800677
678 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800679 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800680
bsalomon0ea80f42015-02-11 10:49:59 -0800681 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800682 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800683 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
684 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800685 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800686 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800687
bsalomon63c992f2015-01-23 12:47:59 -0800688 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800689 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800690 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800691 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800692
693 // Unref but don't purge
694 a->unref();
695 b->unref();
696 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800697 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800698
bsalomon63c992f2015-01-23 12:47:59 -0800699 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800700 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800701 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800702 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
703 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800704}
705
bsalomon10e23ca2014-11-25 05:52:06 -0800706static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800707 Mock mock(5, 30000);
708 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800709 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800710
bsalomon10e23ca2014-11-25 05:52:06 -0800711 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700712 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800713 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700714 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800715 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800716 a->unref();
717 b->unref();
718
bsalomon1c60dfe2015-01-21 09:32:40 -0800719 GrScratchKey scratchKey;
720 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800721 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800722 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700723 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800724
bsalomon0ea80f42015-02-11 10:49:59 -0800725 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800726 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800727 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800728 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
729 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800730
731 // Find the first resource and remove its scratch key
732 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700733 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800734 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800735 // It's still alive, but not cached by scratch key anymore
736 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800737 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
738 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800739
740 // The cache should immediately delete it when it's unrefed since it isn't accessible.
741 find->unref();
742 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800743 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
744 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800745
746 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700747 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800748 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800749 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800750 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
751 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800752
753 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800754 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800755 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800756 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
757 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800758
759 find->unref();
760 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
762 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800763}
764
bsalomon1c60dfe2015-01-21 09:32:40 -0800765static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800766 Mock mock(5, 30000);
767 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800768 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800769
770 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700771 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800772 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700773 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800774 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800775 a->unref();
776 b->unref();
777
778 GrScratchKey scratchKey;
779 // Ensure that scratch key comparison and assignment is consistent.
780 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800781 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800782 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800783 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800784 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
785 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
786 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
787 scratchKey = scratchKey1;
788 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
789 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
790 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
791 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
792 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
793 scratchKey = scratchKey2;
794 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
795 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
796 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
797 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
798 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
799
800 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800801 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800802 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700803 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800804
805 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800806 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700807 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700808 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800809 find->unref();
810
811 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700812 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700813 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800814 REPORTER_ASSERT(reporter, find == a || find == b);
815
robertphillips6e83ac72015-08-13 05:19:14 -0700816 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700817 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800818 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
819 REPORTER_ASSERT(reporter, find2 != find);
820 find2->unref();
821 find->unref();
822}
823
bsalomon8718aaf2015-02-19 07:24:21 -0800824static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800825 Mock mock(5, 30000);
826 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800827 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000828
bsalomon8718aaf2015-02-19 07:24:21 -0800829 GrUniqueKey key;
830 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700831
bsalomon8718aaf2015-02-19 07:24:21 -0800832 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700833 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800834 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700835
bsalomonf99e9612015-02-19 08:24:16 -0800836 // Set key on resource a.
837 a->resourcePriv().setUniqueKey(key);
838 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
839 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800840
bsalomonf99e9612015-02-19 08:24:16 -0800841 // Make sure that redundantly setting a's key works.
842 a->resourcePriv().setUniqueKey(key);
843 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800844 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800845 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
846 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800847 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
848
bsalomonf99e9612015-02-19 08:24:16 -0800849 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700850 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800851 b->setSize(12);
852 b->resourcePriv().setUniqueKey(key);
853 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
854 b->unref();
855
856 // Still have two resources because a is still reffed.
857 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
859 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
860
861 a->unref();
862 // Now a should be gone.
863 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
864 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
865 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
866
867 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
868 // Also make b be unreffed when replacement occurs.
869 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700870 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800871 GrUniqueKey differentKey;
872 make_unique_key<0>(&differentKey, 1);
873 c->setSize(13);
874 c->resourcePriv().setUniqueKey(differentKey);
875 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
876 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
877 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
878 // c replaces b and b should be immediately purged.
879 c->resourcePriv().setUniqueKey(key);
880 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
881 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
882 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
883
884 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800885 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800886 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
887 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
888 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
889
890 // Drop the ref on c, it should be kept alive because it has a unique key.
891 c->unref();
892 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
893 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
894 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
895
896 // Verify that we can find c, then remove its unique key. It should get purged immediately.
897 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
898 c->resourcePriv().removeUniqueKey();
899 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800900 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
901 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800902 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700903
904 {
905 GrUniqueKey key2;
906 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400907 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700908 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700909 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700910 d->resourcePriv().setUniqueKey(key2);
911 }
912
913 GrUniqueKey key3;
914 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400915 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700916 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000917}
918
bsalomon8b79d232014-11-10 10:19:06 -0800919static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800920 Mock mock(5, 30000);
921 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800922 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800923
bsalomon8718aaf2015-02-19 07:24:21 -0800924 GrUniqueKey key1, key2, key3;
925 make_unique_key<0>(&key1, 1);
926 make_unique_key<0>(&key2, 2);
927 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700928
bsalomon23e619c2015-02-06 11:54:28 -0800929 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700930 TestResource* a = new TestResource(context->getGpu());
931 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700932 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800933 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800934 a->resourcePriv().setUniqueKey(key1);
935 b->resourcePriv().setUniqueKey(key2);
936 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800937 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800938 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800939 c->unref();
940
bsalomon8718aaf2015-02-19 07:24:21 -0800941 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
942 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
943 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800944 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800945
bsalomon8718aaf2015-02-19 07:24:21 -0800946 typedef GrUniqueKeyInvalidatedMessage Msg;
947 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800948
949 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
950 Bus::Post(Msg(key1));
951 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800952 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800953 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800954 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
955 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800956 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800957 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800958
959 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800960 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800961 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800962 // we still have a ref on b, c should be recycled as scratch.
963 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800964 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800965
bsalomon23e619c2015-02-06 11:54:28 -0800966 // make b purgeable. It should be immediately deleted since it has no key.
967 b->unref();
968 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
969
970 // Make sure we actually get to c via it's scratch key, before we say goodbye.
971 GrScratchKey scratchKey;
972 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700973 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800974 REPORTER_ASSERT(reporter, scratch == c);
975 SkSafeUnref(scratch);
976
977 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800978 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700979 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800980 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800981 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
982 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800983 REPORTER_ASSERT(reporter, !scratch);
984 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800985}
986
bsalomon71cb0c22014-11-14 12:10:14 -0800987static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800988 Mock mock(3, 30000);
989 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800990 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800991
bsalomon8718aaf2015-02-19 07:24:21 -0800992 GrUniqueKey key1, key2;
993 make_unique_key<0>(&key1, 1);
994 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000995
halcanary385fe4d2015-08-26 13:07:48 -0700996 TestResource* a = new TestResource(context->getGpu());
997 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800998 a->resourcePriv().setUniqueKey(key1);
999 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001000
bsalomonc2f35b72015-01-23 07:19:22 -08001001 // Make a cycle
1002 a->setUnrefWhenDestroyed(b);
1003 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001004
bsalomonc2f35b72015-01-23 07:19:22 -08001005 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001006
bsalomonc2f35b72015-01-23 07:19:22 -08001007 a->unref();
1008 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001009
bsalomonc2f35b72015-01-23 07:19:22 -08001010 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001011
bsalomon0ea80f42015-02-11 10:49:59 -08001012 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001013 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001014
bsalomonc2f35b72015-01-23 07:19:22 -08001015 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001016 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001017 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001018
bsalomon0ea80f42015-02-11 10:49:59 -08001019 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001020 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001021}
1022
bsalomon8b79d232014-11-10 10:19:06 -08001023static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001024 GrUniqueKey key1, key2;
1025 make_unique_key<0>(&key1, 1);
1026 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001027
1028 // Test changing resources sizes (both increase & decrease).
1029 {
bsalomonc2f35b72015-01-23 07:19:22 -08001030 Mock mock(3, 30000);
1031 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001032 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001033
halcanary385fe4d2015-08-26 13:07:48 -07001034 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001035 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001036 a->unref();
1037
halcanary385fe4d2015-08-26 13:07:48 -07001038 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001039 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001040 b->unref();
1041
bsalomon0ea80f42015-02-11 10:49:59 -08001042 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1043 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001044 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001045 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001046 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001047 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001048 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001049 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001050 find1->setSize(50);
1051 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001052
bsalomon0ea80f42015-02-11 10:49:59 -08001053 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1054 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001055 }
1056
1057 // Test increasing a resources size beyond the cache budget.
1058 {
bsalomonc2f35b72015-01-23 07:19:22 -08001059 Mock mock(2, 300);
1060 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001061 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062
halcanary385fe4d2015-08-26 13:07:48 -07001063 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001064 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001065 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001066 a->unref();
1067
halcanary385fe4d2015-08-26 13:07:48 -07001068 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001069 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001070 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001071 b->unref();
1072
bsalomon0ea80f42015-02-11 10:49:59 -08001073 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1074 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001075
bsalomon8b79d232014-11-10 10:19:06 -08001076 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001077 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001078 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001079 find2->setSize(201);
1080 }
bsalomon8718aaf2015-02-19 07:24:21 -08001081 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001082
bsalomon0ea80f42015-02-11 10:49:59 -08001083 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1084 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001085 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001086}
1087
bsalomonddf30e62015-02-19 11:38:44 -08001088static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1089 static const int kCount = 50;
1090 static const int kBudgetCnt = kCount / 2;
1091 static const int kLockedFreq = 8;
1092 static const int kBudgetSize = 0x80000000;
1093
1094 SkRandom random;
1095
1096 // Run the test 2*kCount times;
1097 for (int i = 0; i < 2 * kCount; ++i ) {
1098 Mock mock(kBudgetCnt, kBudgetSize);
1099 GrContext* context = mock.context();
1100 GrResourceCache* cache = mock.cache();
1101
1102 // Pick a random number of resources to add before the timestamp will wrap.
1103 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1104
1105 static const int kNumToPurge = kCount - kBudgetCnt;
1106
1107 SkTDArray<int> shouldPurgeIdxs;
1108 int purgeableCnt = 0;
1109 SkTDArray<GrGpuResource*> resourcesToUnref;
1110
1111 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1112 // unpurgeable resources.
1113 for (int j = 0; j < kCount; ++j) {
1114 GrUniqueKey key;
1115 make_unique_key<0>(&key, j);
1116
halcanary385fe4d2015-08-26 13:07:48 -07001117 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001118 r->resourcePriv().setUniqueKey(key);
1119 if (random.nextU() % kLockedFreq) {
1120 // Make this is purgeable.
1121 r->unref();
1122 ++purgeableCnt;
1123 if (purgeableCnt <= kNumToPurge) {
1124 *shouldPurgeIdxs.append() = j;
1125 }
1126 } else {
1127 *resourcesToUnref.append() = r;
1128 }
1129 }
1130
1131 // Verify that the correct resources were purged.
1132 int currShouldPurgeIdx = 0;
1133 for (int j = 0; j < kCount; ++j) {
1134 GrUniqueKey key;
1135 make_unique_key<0>(&key, j);
1136 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1137 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1138 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1139 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001140 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001141 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001142 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001143 }
1144 SkSafeUnref(res);
1145 }
1146
1147 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1148 resourcesToUnref[j]->unref();
1149 }
1150 }
1151}
1152
bsalomon3f324322015-04-08 11:01:54 -07001153static void test_flush(skiatest::Reporter* reporter) {
1154 Mock mock(1000000, 1000000);
1155 GrContext* context = mock.context();
1156 GrResourceCache* cache = mock.cache();
1157
1158 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1159 // power of two here to keep things simpler.
1160 static const int kFlushCount = 16;
1161 cache->setLimits(1000000, 1000000, kFlushCount);
1162
1163 {
1164 // Insert a resource and send a flush notification kFlushCount times.
1165 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001166 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001167 GrUniqueKey k;
1168 make_unique_key<1>(&k, i);
1169 r->resourcePriv().setUniqueKey(k);
1170 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001171 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001172 }
1173
1174 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001175 for (int i = 0; i < kFlushCount; ++i) {
1176 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001177 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1178 for (int j = 0; j < i; ++j) {
1179 GrUniqueKey k;
1180 make_unique_key<1>(&k, j);
1181 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1182 REPORTER_ASSERT(reporter, !SkToBool(r));
1183 SkSafeUnref(r);
1184 }
bsalomon3f324322015-04-08 11:01:54 -07001185 }
1186
1187 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1188 cache->purgeAllUnlocked();
1189 }
1190
1191 // Do a similar test but where we leave refs on some resources to prevent them from being
1192 // purged.
1193 {
1194 GrGpuResource* refedResources[kFlushCount >> 1];
1195 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001196 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001197 GrUniqueKey k;
1198 make_unique_key<1>(&k, i);
1199 r->resourcePriv().setUniqueKey(k);
1200 // Leave a ref on every other resource, beginning with the first.
1201 if (SkToBool(i & 0x1)) {
1202 refedResources[i/2] = r;
1203 } else {
1204 r->unref();
1205 }
bsalomonb77a9072016-09-07 10:02:04 -07001206 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001207 }
1208
1209 for (int i = 0; i < kFlushCount; ++i) {
1210 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001211 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001212 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001213 }
1214
1215 // Unref all the resources that we kept refs on in the first loop.
1216 for (int i = 0; i < kFlushCount >> 1; ++i) {
1217 refedResources[i]->unref();
1218 }
1219
bsalomone2e87f32016-09-22 12:42:11 -07001220 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1221 // kFlushCount full flushes.
1222 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001223 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001224 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001225 }
1226 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1227
1228 cache->purgeAllUnlocked();
1229 }
1230
1231 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001232
1233 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1234 // eviction.
1235 context->flush();
1236 for (int i = 0; i < 10; ++i) {
1237 TestResource* r = new TestResource(context->getGpu());
1238 GrUniqueKey k;
1239 make_unique_key<1>(&k, i);
1240 r->resourcePriv().setUniqueKey(k);
1241 r->unref();
1242 }
1243 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1244 for (int i = 0; i < 10 * kFlushCount; ++i) {
1245 context->flush();
1246 }
1247 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001248}
1249
Brian Salomon5e150852017-03-22 14:53:13 -04001250static void test_time_purge(skiatest::Reporter* reporter) {
1251 Mock mock(1000000, 1000000);
1252 GrContext* context = mock.context();
1253 GrResourceCache* cache = mock.cache();
1254
1255 static constexpr int kCnts[] = {1, 10, 1024};
1256 auto nowish = []() {
1257 // We sleep so that we ensure we get a value that is greater than the last call to
1258 // GrStdSteadyClock::now().
1259 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1260 auto result = GrStdSteadyClock::now();
1261 // Also sleep afterwards so we don't get this value again.
1262 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1263 return result;
1264 };
1265
1266 for (int cnt : kCnts) {
1267 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1268 new GrStdSteadyClock::time_point[cnt]);
1269 {
1270 // Insert resources and get time points between each addition.
1271 for (int i = 0; i < cnt; ++i) {
1272 TestResource* r = new TestResource(context->getGpu());
1273 GrUniqueKey k;
1274 make_unique_key<1>(&k, i);
1275 r->resourcePriv().setUniqueKey(k);
1276 r->unref();
1277 timeStamps.get()[i] = nowish();
1278 }
1279
1280 // Purge based on the time points between resource additions. Each purge should remove
1281 // the oldest resource.
1282 for (int i = 0; i < cnt; ++i) {
1283 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1284 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1285 for (int j = 0; j < i; ++j) {
1286 GrUniqueKey k;
1287 make_unique_key<1>(&k, j);
1288 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1289 REPORTER_ASSERT(reporter, !SkToBool(r));
1290 SkSafeUnref(r);
1291 }
1292 }
1293
1294 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1295 cache->purgeAllUnlocked();
1296 }
1297
1298 // Do a similar test but where we leave refs on some resources to prevent them from being
1299 // purged.
1300 {
1301 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1302 for (int i = 0; i < cnt; ++i) {
1303 TestResource* r = new TestResource(context->getGpu());
1304 GrUniqueKey k;
1305 make_unique_key<1>(&k, i);
1306 r->resourcePriv().setUniqueKey(k);
1307 // Leave a ref on every other resource, beginning with the first.
1308 if (SkToBool(i & 0x1)) {
1309 refedResources.get()[i / 2] = r;
1310 } else {
1311 r->unref();
1312 }
1313 timeStamps.get()[i] = nowish();
1314 }
1315
1316 for (int i = 0; i < cnt; ++i) {
1317 // Should get a resource purged every other frame.
1318 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1319 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1320 }
1321
1322 // Unref all the resources that we kept refs on in the first loop.
1323 for (int i = 0; i < (cnt / 2); ++i) {
1324 refedResources.get()[i]->unref();
1325 cache->purgeResourcesNotUsedSince(nowish());
1326 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1327 }
1328
1329 cache->purgeAllUnlocked();
1330 }
1331
1332 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1333
1334 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1335 // eviction
1336 context->flush();
1337 for (int i = 0; i < 10; ++i) {
1338 TestResource* r = new TestResource(context->getGpu());
1339 GrUniqueKey k;
1340 make_unique_key<1>(&k, i);
1341 r->resourcePriv().setUniqueKey(k);
1342 r->unref();
1343 }
1344 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1345 context->flush();
1346 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1347 cache->purgeResourcesNotUsedSince(nowish());
1348 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1349 }
1350}
1351
Derek Sollenberger5480a182017-05-25 16:43:59 -04001352static void test_partial_purge(skiatest::Reporter* reporter) {
1353 Mock mock(6, 100);
1354 GrContext* context = mock.context();
1355 GrResourceCache* cache = mock.cache();
1356
1357 enum TestsCase {
1358 kOnlyScratch_TestCase = 0,
1359 kPartialScratch_TestCase = 1,
1360 kAllScratch_TestCase = 2,
1361 kPartial_TestCase = 3,
1362 kAll_TestCase = 4,
1363 kNone_TestCase = 5,
1364 kEndTests_TestCase = kNone_TestCase + 1
1365 };
1366
1367 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1368
1369 GrUniqueKey key1, key2, key3;
1370 make_unique_key<0>(&key1, 1);
1371 make_unique_key<0>(&key2, 2);
1372 make_unique_key<0>(&key3, 3);
1373
1374 // Add three unique resources to the cache.
1375 TestResource *unique1 = new TestResource(context->getGpu());
1376 TestResource *unique2 = new TestResource(context->getGpu());
1377 TestResource *unique3 = new TestResource(context->getGpu());
1378
1379 unique1->resourcePriv().setUniqueKey(key1);
1380 unique2->resourcePriv().setUniqueKey(key2);
1381 unique3->resourcePriv().setUniqueKey(key3);
1382
1383 unique1->setSize(10);
1384 unique2->setSize(11);
1385 unique3->setSize(12);
1386
1387 // Add two scratch resources to the cache.
1388 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1389 TestResource::kA_SimulatedProperty);
1390 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1391 TestResource::kB_SimulatedProperty);
1392 scratch1->setSize(13);
1393 scratch2->setSize(14);
1394
1395
1396 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1397 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1398 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1399
1400 // Add resources to the purgeable queue
1401 unique1->unref();
1402 scratch1->unref();
1403 unique2->unref();
1404 scratch2->unref();
1405 unique3->unref();
1406
1407 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1408 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1409 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1410
1411 switch(testCase) {
1412 case kOnlyScratch_TestCase: {
1413 context->purgeUnlockedResources(14, true);
1414 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1415 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1416 break;
1417 }
1418 case kPartialScratch_TestCase: {
1419 context->purgeUnlockedResources(3, true);
1420 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1421 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1422 break;
1423 }
1424 case kAllScratch_TestCase: {
1425 context->purgeUnlockedResources(50, true);
1426 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1427 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1428 break;
1429 }
1430 case kPartial_TestCase: {
1431 context->purgeUnlockedResources(13, false);
1432 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1433 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1434 break;
1435 }
1436 case kAll_TestCase: {
1437 context->purgeUnlockedResources(50, false);
1438 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1439 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1440 break;
1441 }
1442 case kNone_TestCase: {
1443 context->purgeUnlockedResources(0, true);
1444 context->purgeUnlockedResources(0, false);
1445 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1446 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1447 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1448 break;
1449 }
1450 };
1451
1452 // ensure all are purged before the next
1453 context->purgeAllUnlockedResources();
1454 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1455 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1456
1457 }
1458}
1459
bsalomon10e23ca2014-11-25 05:52:06 -08001460static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001461 // Set the cache size to double the resource count because we're going to create 2x that number
1462 // resources, using two different key domains. Add a little slop to the bytes because we resize
1463 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001464 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001465
bsalomonc2f35b72015-01-23 07:19:22 -08001466 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1467 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001468 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001469
1470 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001471 GrUniqueKey key1, key2;
1472 make_unique_key<1>(&key1, i);
1473 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001474
bsalomon24db3b12015-01-23 04:24:04 -08001475 TestResource* resource;
1476
halcanary385fe4d2015-08-26 13:07:48 -07001477 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001478 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001479 resource->setSize(1);
1480 resource->unref();
1481
halcanary385fe4d2015-08-26 13:07:48 -07001482 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001483 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001484 resource->setSize(1);
1485 resource->unref();
1486 }
1487
1488 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001489 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001490 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1491 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1492 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1493 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001494 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001495 GrUniqueKey key1, key2;
1496 make_unique_key<1>(&key1, i);
1497 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001498
bsalomon8718aaf2015-02-19 07:24:21 -08001499 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1500 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001501 }
1502
bsalomon0ea80f42015-02-11 10:49:59 -08001503 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001504 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001505 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001506 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1507 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1508 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1509 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001510
1511 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001512 GrUniqueKey key1, key2;
1513 make_unique_key<1>(&key1, i);
1514 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001515
bsalomon8718aaf2015-02-19 07:24:21 -08001516 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1517 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001518 }
1519}
1520
senorblanco84cd6212015-08-04 10:01:58 -07001521static void test_custom_data(skiatest::Reporter* reporter) {
1522 GrUniqueKey key1, key2;
1523 make_unique_key<0>(&key1, 1);
1524 make_unique_key<0>(&key2, 2);
1525 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001526 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001527 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1528 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1529
1530 // Test that copying a key also takes a ref on its custom data.
1531 GrUniqueKey key3 = key1;
1532 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1533}
1534
bsalomonc6363ef2015-09-24 07:07:40 -07001535static void test_abandoned(skiatest::Reporter* reporter) {
1536 Mock mock(10, 300);
1537 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001538 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001539 context->abandonContext();
1540
1541 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1542
1543 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1544
robertphillips8abb3702016-08-31 14:04:06 -07001545 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001546 resource->getUniqueKey();
1547 resource->wasDestroyed();
1548 resource->gpuMemorySize();
1549 resource->getContext();
1550
1551 resource->abandon();
1552 resource->resourcePriv().getScratchKey();
1553 resource->resourcePriv().isBudgeted();
1554 resource->resourcePriv().makeBudgeted();
1555 resource->resourcePriv().makeUnbudgeted();
1556 resource->resourcePriv().removeScratchKey();
1557 GrUniqueKey key;
1558 make_unique_key<0>(&key, 1);
1559 resource->resourcePriv().setUniqueKey(key);
1560 resource->resourcePriv().removeUniqueKey();
1561}
1562
Brian Salomon1090da62017-01-06 12:04:19 -05001563static void test_tags(skiatest::Reporter* reporter) {
1564#ifdef SK_DEBUG
1565 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1566 static constexpr int kLastTagIdx = 10;
1567 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1568
1569 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1570 GrContext* context = mock.context();
1571 GrResourceCache* cache = mock.cache();
1572
1573 SkString tagStr;
1574 int tagIdx = 0;
1575 int currTagCnt = 0;
1576
1577 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1578 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1579 GrUniqueKey key;
1580 if (currTagCnt == tagIdx) {
1581 tagIdx += 1;
1582 currTagCnt = 0;
1583 tagStr.printf("tag%d", tagIdx);
1584 }
1585 make_unique_key<1>(&key, i, tagStr.c_str());
1586 resource->resourcePriv().setUniqueKey(key);
1587 }
1588 SkASSERT(kLastTagIdx == tagIdx);
1589 SkASSERT(currTagCnt == kLastTagIdx);
1590
1591 // Test i = 0 to exercise unused tag string.
1592 for (int i = 0; i <= kLastTagIdx; ++i) {
1593 tagStr.printf("tag%d", i);
1594 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1595 }
1596#endif
1597}
1598
Brian Salomondcfca432017-11-15 15:48:03 -05001599DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001600 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001601 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001602 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001603 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001604 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001605 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001606 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001607 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001608 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001609 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001610 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001611 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001612 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001613 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001614 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001615 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001616 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001617 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001618 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001619 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001620}
1621
Robert Phillipsd6214d42016-11-07 08:23:48 -05001622////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001623static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001624 GrSurfaceFlags flags,
1625 int width, int height,
1626 int sampleCnt) {
1627 GrSurfaceDesc desc;
1628 desc.fFlags = flags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001629 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001630 desc.fWidth = width;
1631 desc.fHeight = height;
1632 desc.fConfig = kRGBA_8888_GrPixelConfig;
1633 desc.fSampleCnt = sampleCnt;
1634
Robert Phillipse78b7252017-04-06 07:59:41 -04001635 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001636}
1637
Robert Phillipse78b7252017-04-06 07:59:41 -04001638static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1639 GrSurfaceFlags flags,
1640 int width, int height,
1641 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001642 SkBitmap bm;
1643
1644 bm.allocN32Pixels(width, height, true);
1645 bm.eraseColor(SK_ColorBLUE);
1646
Brian Osman7b8400d2016-11-08 17:08:54 -05001647 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001648 SkASSERT(mipmaps);
1649 SkASSERT(mipmaps->countLevels() > 1);
1650
1651 int mipLevelCount = mipmaps->countLevels() + 1;
1652
1653 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1654
1655 texels[0].fPixels = bm.getPixels();
1656 texels[0].fRowBytes = bm.rowBytes();
1657
1658 for (int i = 1; i < mipLevelCount; ++i) {
1659 SkMipMap::Level generatedMipLevel;
1660 mipmaps->getLevel(i - 1, &generatedMipLevel);
1661 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1662 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1663 }
1664
1665 GrSurfaceDesc desc;
1666 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001667 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1668 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001669 desc.fWidth = width;
1670 desc.fHeight = height;
1671 desc.fConfig = kRGBA_8888_GrPixelConfig;
1672 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001673
Robert Phillips8e8c7552017-07-10 12:06:05 -04001674 return GrSurfaceProxy::MakeDeferredMipMap(provider, desc, SkBudgeted::kYes,
1675 texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001676}
1677
1678// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1679// Texture-only, both-RT-and-Texture and MIPmapped
1680DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1681 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001682 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001683
Robert Phillipsd6214d42016-11-07 08:23:48 -05001684 static const int kSize = 64;
1685
Robert Phillipsd6214d42016-11-07 08:23:48 -05001686 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001687 {
1688 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001689
Robert Phillipse78b7252017-04-06 07:59:41 -04001690 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1691 size_t size = tex->gpuMemorySize();
1692 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1693
Greg Daniel81e7bf82017-07-19 14:47:42 -04001694 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1695 if (sampleCount >= 4) {
1696 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1697 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001698 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001699 REPORTER_ASSERT(reporter,
1700 kSize*kSize*4 == size || // msaa4 failed
1701 kSize*kSize*4*sampleCount == size || // auto-resolving
1702 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001703 }
1704
1705 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001706 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001707 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001708 }
1709
Robert Phillipsd6214d42016-11-07 08:23:48 -05001710
1711 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001712 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001713 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001714
Robert Phillipse78b7252017-04-06 07:59:41 -04001715 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1716 size_t size = proxy->gpuMemorySize();
1717 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1718
Greg Daniel81e7bf82017-07-19 14:47:42 -04001719 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1720 if (sampleCount >= 4) {
1721 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1722 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001723 size = proxy->gpuMemorySize();
1724 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001725 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1726 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1727 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001728 }
Robert Phillips1b352562017-04-05 18:56:21 +00001729
Robert Phillipse78b7252017-04-06 07:59:41 -04001730 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1731 size = proxy->gpuMemorySize();
1732 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1733 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001734}
1735
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001736#endif