blob: dd122a0deafca29e21b4f878b0f6b9afa3085358 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon3f324322015-04-08 11:01:54 -07008// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
Brian Salomon5e150852017-03-22 14:53:13 -040012#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000014#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000015#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080017#include "GrGpuResourceCacheAccess.h"
18#include "GrGpuResourcePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040020#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070023#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
25
bsalomonbcf0a522014-10-08 08:40:09 -070026#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027#include "SkGr.h"
28#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050029#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070030#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000031#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032
33static const int gWidth = 640;
34static const int gHeight = 480;
35
36////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070038 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080039 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040040 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080041 desc.fFlags = kRenderTarget_GrSurfaceFlag;
42 desc.fWidth = gWidth;
43 desc.fHeight = gHeight;
44 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070045 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080046 SkCanvas* canvas = surface->getCanvas();
47
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000048 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
49
50 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000051 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000052 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040053 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000055 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070056 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 int oldMaxNum;
59 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000060 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000061
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062 // Set the cache limits so we can fit 10 "src" images and the
63 // max number of textures doesn't matter
64 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000065 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000068 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000069
70 for (int i = 0; i < 100; ++i) {
71 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040072 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000073
74 // "modify" the src texture
75 src.notifyPixelsChanged();
76
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000077 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070078 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000079
80 // we should never go over the size limit
81 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
82 }
83
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000084 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000085}
86
bsalomon11abd8d2016-10-14 08:13:48 -070087static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
88 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
89 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
90 return false;
91 }
92 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
93}
94
Robert Phillipsc0192e32017-09-21 12:00:26 -040095static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
96 return rt->renderTargetPriv().getStencilAttachment();
97}
98
99static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
100 int size, int sampleCount, SkBudgeted budgeted) {
101 GrSurfaceDesc desc;
102 desc.fFlags = kRenderTarget_GrSurfaceFlag;
103 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
104 desc.fWidth = size;
105 desc.fHeight = size;
106 desc.fConfig = kRGBA_8888_GrPixelConfig;
107 desc.fSampleCnt = sampleCount;
108
109 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
110 if (!tex || !tex->asRenderTarget()) {
111 return nullptr;
112 }
113
114 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
115 return nullptr;
116 }
117 SkASSERT(get_SB(tex->asRenderTarget()));
118
119 return sk_ref_sp(tex->asRenderTarget());
120}
121
bsalomon11abd8d2016-10-14 08:13:48 -0700122// This currently fails on ES3 ANGLE contexts
123DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000124 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700125 GrContext* context = ctxInfo.grContext();
Eric Karl5c779752017-05-08 12:02:07 -0700126 if (context->caps()->avoidStencilBuffers()) {
127 return;
128 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400129
Robert Phillips6be756b2018-01-16 15:07:54 -0500130 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400131
Robert Phillips6be756b2018-01-16 15:07:54 -0500132 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 0, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400133 REPORTER_ASSERT(reporter, smallRT0);
134
135 {
136 // Two budgeted RTs with the same desc should share a stencil buffer.
Robert Phillips6be756b2018-01-16 15:07:54 -0500137 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 0,
138 SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400139 REPORTER_ASSERT(reporter, smallRT1);
140
141 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 }
143
Robert Phillipsc0192e32017-09-21 12:00:26 -0400144 {
145 // An unbudgeted RT with the same desc should also share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500146 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 0, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400147 REPORTER_ASSERT(reporter, smallRT2);
148
149 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800150 }
151
Robert Phillipsc0192e32017-09-21 12:00:26 -0400152 {
153 // An RT with a much larger size should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500154 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 0, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400155 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800156
Robert Phillipsc0192e32017-09-21 12:00:26 -0400157 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800158 }
bsalomon02a44a42015-02-19 09:09:00 -0800159
Robert Phillipsc0192e32017-09-21 12:00:26 -0400160 int smallSampleCount = context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
161 if (smallSampleCount > 0) {
mtklein5f939ab2016-03-16 10:28:35 -0700162 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500163 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
164 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800165#ifdef SK_BUILD_FOR_ANDROID
166 if (!smallMSAART0) {
167 // The nexus player seems to fail to create MSAA textures.
168 return;
169 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400170#else
171 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800172#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400173
174 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
175
176 {
177 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500178 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
179 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400180 SkBudgeted::kNo);
181 REPORTER_ASSERT(reporter, smallMSAART1);
182
183 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800184 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400185
bsalomon02a44a42015-02-19 09:09:00 -0800186 // But not one with a larger sample count should not. (Also check that the request for 4
187 // samples didn't get rounded up to >= 8 or else they could share.).
Robert Phillipsc0192e32017-09-21 12:00:26 -0400188 int bigSampleCount = context->caps()->getSampleCount(8, kRGBA_8888_GrPixelConfig);
189 if (bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500190 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
191 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400192 SkBudgeted::kNo);
193 REPORTER_ASSERT(reporter, smallMSAART2);
194
195 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800196 }
197 }
198}
199
bsalomon68d91342016-04-12 09:59:58 -0700200DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700201 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500202 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
bsalomone63ffef2016-02-05 07:17:34 -0800203 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700204 // this test is only valid for GL
205 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700206 return;
207 }
208
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500209 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700210 static const int kW = 100;
211 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700212
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500213 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
214 kRGBA_8888_GrPixelConfig,
215 false, GrMipMapped::kNo);
216 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
217 kRGBA_8888_GrPixelConfig,
218 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500219 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
220 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
221 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
222 return;
223 }
jvanverth672bb7f2015-07-13 07:19:57 -0700224
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225 context->resetContext();
226
Robert Phillips6be756b2018-01-16 15:07:54 -0500227 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
Robert Phillips6be756b2018-01-16 15:07:54 -0500230 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500231 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
Brian Osman85d34b22017-05-10 12:06:26 -0400233 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
234 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700235 return;
236 }
237
halcanary96fcdcc2015-08-27 07:41:13 -0700238 borrowed.reset(nullptr);
239 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
241 context->flush();
242
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500243 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
244 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700245
246 REPORTER_ASSERT(reporter, borrowedIsAlive);
247 REPORTER_ASSERT(reporter, !adoptedIsAlive);
248
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500249 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[0]), !borrowedIsAlive);
250 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[1]), !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700251
252 context->resetContext();
253}
254
bsalomon6d3fe022014-07-25 08:35:45 -0700255class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000257public:
robertphillips6e83ac72015-08-13 05:19:14 -0700258 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700259
bsalomon1c60dfe2015-01-21 09:32:40 -0800260 /** Property that distinctly categorizes the resource.
261 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800262 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800263
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
265 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700266 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800267 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700268 , fProperty(kA_SimulatedProperty)
269 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800270 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700271 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800272 }
273
kkinnunen2e6055b2016-04-22 01:48:29 -0700274 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
275 SimulatedProperty property) {
276 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800277 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700278 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
279 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000280 }
281
Brian Salomond3b65972017-03-22 12:05:03 -0400282 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800283 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800284 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000285 }
286
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000287 void setSize(size_t size) {
288 fSize = size;
289 this->didChangeGpuMemorySize();
290 }
291
bsalomon33435572014-11-05 14:47:41 -0800292 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293
bsalomon71cb0c22014-11-14 12:10:14 -0800294 void setUnrefWhenDestroyed(TestResource* resource) {
295 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000296 }
297
bsalomon1c60dfe2015-01-21 09:32:40 -0800298 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
299 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
300 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800301 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
302 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800303 }
304 }
305
306 static size_t ExpectedScratchKeySize() {
307 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
308 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000309private:
bsalomon24db3b12015-01-23 04:24:04 -0800310 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800311
kkinnunen2e6055b2016-04-22 01:48:29 -0700312 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
313 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700314 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700316 , fProperty(property)
317 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800318 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700319 this->registerWithCache(budgeted);
320 }
321
322 // Constructor for simulating resources that wrap backend objects.
323 TestResource(GrGpu* gpu, size_t size)
324 : INHERITED(gpu)
325 , fToDelete(nullptr)
326 , fSize(size)
327 , fProperty(kA_SimulatedProperty)
328 , fIsScratch(false) {
329 ++fNumAlive;
330 this->registerWithCacheWrapped();
331 }
332
333 void computeScratchKey(GrScratchKey* key) const override {
334 if (fIsScratch) {
335 ComputeScratchKey(fProperty, key);
336 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800337 }
338
mtklein36352bf2015-03-25 18:17:31 -0700339 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800340
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000341 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000342 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800343 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800344 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700345 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700346 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000347};
bsalomon33435572014-11-05 14:47:41 -0800348int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000349
bsalomonc2f35b72015-01-23 07:19:22 -0800350class Mock {
351public:
352 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400353 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800354 SkASSERT(fContext);
355 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500356 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800357 cache->purgeAllUnlocked();
358 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800359 }
bsalomonc2f35b72015-01-23 07:19:22 -0800360
Robert Phillips6be756b2018-01-16 15:07:54 -0500361 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800362
Hal Canary342b7ac2016-11-04 11:49:42 -0400363 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800364
365private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400366 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800367};
368
369static void test_no_key(skiatest::Reporter* reporter) {
370 Mock mock(10, 30000);
371 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800372 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800373
374 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700375 TestResource* a = new TestResource(context->getGpu());
376 TestResource* b = new TestResource(context->getGpu());
377 TestResource* c = new TestResource(context->getGpu());
378 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800379 a->setSize(11);
380 b->setSize(12);
381 c->setSize(13);
382 d->setSize(14);
383
384 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800387 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800390 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800391 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
392
bsalomon8718aaf2015-02-19 07:24:21 -0800393 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800394
395 a->unref();
396 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800397 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800398 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 c->unref();
402 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800403 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800405 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 d->unref();
408 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411
412 b->unref();
413 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800414 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
415 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800416}
417
bsalomon24db3b12015-01-23 04:24:04 -0800418// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500419template <int>
420static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800421 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500422 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800423 builder[0] = data;
424}
425
bsalomon84c8e622014-11-17 09:33:27 -0800426static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800427 Mock mock(10, 300);
428 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800429 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800430
bsalomon8718aaf2015-02-19 07:24:21 -0800431 GrUniqueKey uniqueKey;
432 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800433
bsalomon8718aaf2015-02-19 07:24:21 -0800434 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800435 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700436 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800437 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700438 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800439 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800440 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700441 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800442 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700443 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700444 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800445 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800446
Brian Osman0562eb92017-05-08 11:16:39 -0400447 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800448 GrUniqueKey uniqueKey2;
449 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800450 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400451 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
452 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
453
454 // Remove the extra ref we just added.
455 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800456
457 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800458 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800459 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800460 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800461 cache->getResourceBytes());
462 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800463 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800464 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400465 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800466
bsalomon63c992f2015-01-23 12:47:59 -0800467 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800468 cache->purgeAllUnlocked();
469 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800470 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800471 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800472 cache->getResourceBytes());
473 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800474 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800475 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400476 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800477
478 // Unreffing the wrapped resource should free it right away.
479 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800481 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800482 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400483 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800484
bsalomon84c8e622014-11-17 09:33:27 -0800485 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700486 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800487 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800488 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400489 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800490 cache->purgeAllUnlocked();
491 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800492 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800493 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
494 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
495 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400496 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800497
498 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400499 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800500 cache->purgeAllUnlocked();
501 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800502 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800503 cache->getResourceBytes());
504 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
505 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400506 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800507
508 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800509 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
510 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
511 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
512 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400513 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800514
515 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800516 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
517 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
518 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
519 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400520 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800521}
522
bsalomon5236cf42015-01-14 10:42:08 -0800523static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800524 Mock mock(10, 30000);
525 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800526 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800527
bsalomon8718aaf2015-02-19 07:24:21 -0800528 GrUniqueKey uniqueKey;
529 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800530
531 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800532 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800533 TestResource* wrapped;
534 TestResource* unbudgeted;
535
536 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700537 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
538 TestResource::kB_SimulatedProperty);
539
bsalomon5236cf42015-01-14 10:42:08 -0800540 scratch->setSize(10);
541 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800542 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
543 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
544 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
545 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400546 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800547
halcanary385fe4d2015-08-26 13:07:48 -0700548 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800549 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800550 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800551 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800552 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
553 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
554 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
555 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400556 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800557
bsalomon0ea80f42015-02-11 10:49:59 -0800558 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700559 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800560 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
561 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
562 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
563 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400564 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800565
566 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800567 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
568 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
569 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
570 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400571 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800572
kkinnunen2e6055b2016-04-22 01:48:29 -0700573 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800574 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
575 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
576 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
577 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400578 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800579
580 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800581 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
582 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
583 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
584 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400585 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800586
bsalomon0ea80f42015-02-11 10:49:59 -0800587 cache->purgeAllUnlocked();
588 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
589 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
590 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400592 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800593}
594
bsalomon3582d3e2015-02-13 14:20:05 -0800595// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
596void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
597/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800598 Mock mock(10, 300);
599 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800600 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800601
602 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700603 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
604 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800605 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800606 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800607
608 size_t size = resource->gpuMemorySize();
609 for (int i = 0; i < 2; ++i) {
610 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800611 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800612 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800613 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700614 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800615 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
616 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
617 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
618 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400619 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800620
621 // Once it is unrefed, it should become available as scratch.
622 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800623 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
624 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
625 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
626 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400627 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700628 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800629 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800630 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800631 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800632 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800633
634 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700635 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800636 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800637 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800638 } else {
639 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800640 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800641 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
642 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
643 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
644 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400645 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800646 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800647 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800648 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800649
650 // now when it is unrefed it should die since it has no key.
651 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800652 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
653 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
654 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
655 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400656 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800657 }
bsalomon8b79d232014-11-10 10:19:06 -0800658 }
bsalomonc2f35b72015-01-23 07:19:22 -0800659}
660
661static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
662 Mock mock(5, 30000);
663 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800664 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800665
bsalomon8b79d232014-11-10 10:19:06 -0800666 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800667 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700668 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800669 TestResource::kB_SimulatedProperty);
670 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700671 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800672 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800673 a->setSize(11);
674 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800675 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800677 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700678 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800679
680 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800681 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800682
bsalomon0ea80f42015-02-11 10:49:59 -0800683 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800684 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800685 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
686 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800687 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800688 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800689
bsalomon63c992f2015-01-23 12:47:59 -0800690 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800691 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800692 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800693 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800694
695 // Unref but don't purge
696 a->unref();
697 b->unref();
698 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800699 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800700
bsalomon63c992f2015-01-23 12:47:59 -0800701 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800702 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800703 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800704 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
705 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800706}
707
bsalomon10e23ca2014-11-25 05:52:06 -0800708static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800709 Mock mock(5, 30000);
710 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800711 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800712
bsalomon10e23ca2014-11-25 05:52:06 -0800713 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700714 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800715 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700716 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800717 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800718 a->unref();
719 b->unref();
720
bsalomon1c60dfe2015-01-21 09:32:40 -0800721 GrScratchKey scratchKey;
722 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800723 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800724 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700725 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800726
bsalomon0ea80f42015-02-11 10:49:59 -0800727 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800728 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800729 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800730 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
731 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800732
733 // Find the first resource and remove its scratch key
734 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700735 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800736 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800737 // It's still alive, but not cached by scratch key anymore
738 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800739 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
740 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800741
742 // The cache should immediately delete it when it's unrefed since it isn't accessible.
743 find->unref();
744 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800745 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
746 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800747
748 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700749 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800750 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800751 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800752 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
753 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800754
755 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800756 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800757 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800758 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
759 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800760
761 find->unref();
762 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800763 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
764 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800765}
766
bsalomon1c60dfe2015-01-21 09:32:40 -0800767static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800768 Mock mock(5, 30000);
769 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800770 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800771
772 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700773 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800774 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700775 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800776 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800777 a->unref();
778 b->unref();
779
780 GrScratchKey scratchKey;
781 // Ensure that scratch key comparison and assignment is consistent.
782 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800783 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800784 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800785 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800786 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
787 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
788 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
789 scratchKey = scratchKey1;
790 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
791 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
792 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
793 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
794 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
795 scratchKey = scratchKey2;
796 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
797 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
798 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
799 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
800 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
801
802 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800803 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800804 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700805 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800806
807 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800808 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700809 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700810 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800811 find->unref();
812
813 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700814 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700815 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800816 REPORTER_ASSERT(reporter, find == a || find == b);
817
robertphillips6e83ac72015-08-13 05:19:14 -0700818 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700819 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800820 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
821 REPORTER_ASSERT(reporter, find2 != find);
822 find2->unref();
823 find->unref();
824}
825
bsalomon8718aaf2015-02-19 07:24:21 -0800826static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800827 Mock mock(5, 30000);
828 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800829 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000830
bsalomon8718aaf2015-02-19 07:24:21 -0800831 GrUniqueKey key;
832 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700833
bsalomon8718aaf2015-02-19 07:24:21 -0800834 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700835 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800836 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700837
bsalomonf99e9612015-02-19 08:24:16 -0800838 // Set key on resource a.
839 a->resourcePriv().setUniqueKey(key);
840 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
841 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800842
bsalomonf99e9612015-02-19 08:24:16 -0800843 // Make sure that redundantly setting a's key works.
844 a->resourcePriv().setUniqueKey(key);
845 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800846 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800847 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
848 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800849 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
850
bsalomonf99e9612015-02-19 08:24:16 -0800851 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700852 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800853 b->setSize(12);
854 b->resourcePriv().setUniqueKey(key);
855 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
856 b->unref();
857
858 // Still have two resources because a is still reffed.
859 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
860 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
861 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
862
863 a->unref();
864 // Now a should be gone.
865 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
866 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
867 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
868
869 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
870 // Also make b be unreffed when replacement occurs.
871 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700872 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800873 GrUniqueKey differentKey;
874 make_unique_key<0>(&differentKey, 1);
875 c->setSize(13);
876 c->resourcePriv().setUniqueKey(differentKey);
877 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
878 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
879 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
880 // c replaces b and b should be immediately purged.
881 c->resourcePriv().setUniqueKey(key);
882 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
883 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
884 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
885
886 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800887 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800888 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
889 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
890 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
891
892 // Drop the ref on c, it should be kept alive because it has a unique key.
893 c->unref();
894 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
895 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
896 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
897
898 // Verify that we can find c, then remove its unique key. It should get purged immediately.
899 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
900 c->resourcePriv().removeUniqueKey();
901 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800902 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
903 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800904 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700905
906 {
907 GrUniqueKey key2;
908 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400909 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700910 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700911 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700912 d->resourcePriv().setUniqueKey(key2);
913 }
914
915 GrUniqueKey key3;
916 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400917 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700918 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000919}
920
bsalomon8b79d232014-11-10 10:19:06 -0800921static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800922 Mock mock(5, 30000);
923 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800924 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800925
bsalomon8718aaf2015-02-19 07:24:21 -0800926 GrUniqueKey key1, key2, key3;
927 make_unique_key<0>(&key1, 1);
928 make_unique_key<0>(&key2, 2);
929 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700930
bsalomon23e619c2015-02-06 11:54:28 -0800931 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700932 TestResource* a = new TestResource(context->getGpu());
933 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700934 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800935 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800936 a->resourcePriv().setUniqueKey(key1);
937 b->resourcePriv().setUniqueKey(key2);
938 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800939 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800940 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800941 c->unref();
942
bsalomon8718aaf2015-02-19 07:24:21 -0800943 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
944 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
945 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800946 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800947
bsalomon8718aaf2015-02-19 07:24:21 -0800948 typedef GrUniqueKeyInvalidatedMessage Msg;
949 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800950
951 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
952 Bus::Post(Msg(key1));
953 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800954 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800955 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800956 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
957 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800958 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800959 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800960
961 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800962 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800963 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800964 // we still have a ref on b, c should be recycled as scratch.
965 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800966 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800967
bsalomon23e619c2015-02-06 11:54:28 -0800968 // make b purgeable. It should be immediately deleted since it has no key.
969 b->unref();
970 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
971
972 // Make sure we actually get to c via it's scratch key, before we say goodbye.
973 GrScratchKey scratchKey;
974 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700975 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800976 REPORTER_ASSERT(reporter, scratch == c);
977 SkSafeUnref(scratch);
978
979 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800980 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700981 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800982 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800983 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
984 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800985 REPORTER_ASSERT(reporter, !scratch);
986 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800987}
988
bsalomon71cb0c22014-11-14 12:10:14 -0800989static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800990 Mock mock(3, 30000);
991 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800992 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800993
bsalomon8718aaf2015-02-19 07:24:21 -0800994 GrUniqueKey key1, key2;
995 make_unique_key<0>(&key1, 1);
996 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000997
halcanary385fe4d2015-08-26 13:07:48 -0700998 TestResource* a = new TestResource(context->getGpu());
999 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001000 a->resourcePriv().setUniqueKey(key1);
1001 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001002
bsalomonc2f35b72015-01-23 07:19:22 -08001003 // Make a cycle
1004 a->setUnrefWhenDestroyed(b);
1005 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001006
bsalomonc2f35b72015-01-23 07:19:22 -08001007 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001008
bsalomonc2f35b72015-01-23 07:19:22 -08001009 a->unref();
1010 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001011
bsalomonc2f35b72015-01-23 07:19:22 -08001012 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001013
bsalomon0ea80f42015-02-11 10:49:59 -08001014 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001015 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001016
bsalomonc2f35b72015-01-23 07:19:22 -08001017 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001018 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001019 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001020
bsalomon0ea80f42015-02-11 10:49:59 -08001021 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001022 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001023}
1024
bsalomon8b79d232014-11-10 10:19:06 -08001025static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001026 GrUniqueKey key1, key2;
1027 make_unique_key<0>(&key1, 1);
1028 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001029
1030 // Test changing resources sizes (both increase & decrease).
1031 {
bsalomonc2f35b72015-01-23 07:19:22 -08001032 Mock mock(3, 30000);
1033 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001034 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001035
halcanary385fe4d2015-08-26 13:07:48 -07001036 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001037 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001038 a->unref();
1039
halcanary385fe4d2015-08-26 13:07:48 -07001040 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001041 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001042 b->unref();
1043
bsalomon0ea80f42015-02-11 10:49:59 -08001044 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1045 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001046 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001047 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001048 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001049 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001050 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001051 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001052 find1->setSize(50);
1053 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001054
bsalomon0ea80f42015-02-11 10:49:59 -08001055 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1056 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001057 }
1058
1059 // Test increasing a resources size beyond the cache budget.
1060 {
bsalomonc2f35b72015-01-23 07:19:22 -08001061 Mock mock(2, 300);
1062 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001063 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001064
halcanary385fe4d2015-08-26 13:07:48 -07001065 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001066 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001067 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001068 a->unref();
1069
halcanary385fe4d2015-08-26 13:07:48 -07001070 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001071 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001072 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001073 b->unref();
1074
bsalomon0ea80f42015-02-11 10:49:59 -08001075 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1076 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001077
bsalomon8b79d232014-11-10 10:19:06 -08001078 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001079 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001080 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001081 find2->setSize(201);
1082 }
bsalomon8718aaf2015-02-19 07:24:21 -08001083 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001084
bsalomon0ea80f42015-02-11 10:49:59 -08001085 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1086 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001087 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001088}
1089
bsalomonddf30e62015-02-19 11:38:44 -08001090static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1091 static const int kCount = 50;
1092 static const int kBudgetCnt = kCount / 2;
1093 static const int kLockedFreq = 8;
1094 static const int kBudgetSize = 0x80000000;
1095
1096 SkRandom random;
1097
1098 // Run the test 2*kCount times;
1099 for (int i = 0; i < 2 * kCount; ++i ) {
1100 Mock mock(kBudgetCnt, kBudgetSize);
1101 GrContext* context = mock.context();
1102 GrResourceCache* cache = mock.cache();
1103
1104 // Pick a random number of resources to add before the timestamp will wrap.
1105 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1106
1107 static const int kNumToPurge = kCount - kBudgetCnt;
1108
1109 SkTDArray<int> shouldPurgeIdxs;
1110 int purgeableCnt = 0;
1111 SkTDArray<GrGpuResource*> resourcesToUnref;
1112
1113 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1114 // unpurgeable resources.
1115 for (int j = 0; j < kCount; ++j) {
1116 GrUniqueKey key;
1117 make_unique_key<0>(&key, j);
1118
halcanary385fe4d2015-08-26 13:07:48 -07001119 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001120 r->resourcePriv().setUniqueKey(key);
1121 if (random.nextU() % kLockedFreq) {
1122 // Make this is purgeable.
1123 r->unref();
1124 ++purgeableCnt;
1125 if (purgeableCnt <= kNumToPurge) {
1126 *shouldPurgeIdxs.append() = j;
1127 }
1128 } else {
1129 *resourcesToUnref.append() = r;
1130 }
1131 }
1132
1133 // Verify that the correct resources were purged.
1134 int currShouldPurgeIdx = 0;
1135 for (int j = 0; j < kCount; ++j) {
1136 GrUniqueKey key;
1137 make_unique_key<0>(&key, j);
1138 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1139 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1140 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1141 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001142 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001143 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001144 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001145 }
1146 SkSafeUnref(res);
1147 }
1148
1149 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1150 resourcesToUnref[j]->unref();
1151 }
1152 }
1153}
1154
bsalomon3f324322015-04-08 11:01:54 -07001155static void test_flush(skiatest::Reporter* reporter) {
1156 Mock mock(1000000, 1000000);
1157 GrContext* context = mock.context();
1158 GrResourceCache* cache = mock.cache();
1159
1160 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1161 // power of two here to keep things simpler.
1162 static const int kFlushCount = 16;
1163 cache->setLimits(1000000, 1000000, kFlushCount);
1164
1165 {
1166 // Insert a resource and send a flush notification kFlushCount times.
1167 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001168 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001169 GrUniqueKey k;
1170 make_unique_key<1>(&k, i);
1171 r->resourcePriv().setUniqueKey(k);
1172 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001173 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001174 }
1175
1176 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001177 for (int i = 0; i < kFlushCount; ++i) {
1178 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001179 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1180 for (int j = 0; j < i; ++j) {
1181 GrUniqueKey k;
1182 make_unique_key<1>(&k, j);
1183 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1184 REPORTER_ASSERT(reporter, !SkToBool(r));
1185 SkSafeUnref(r);
1186 }
bsalomon3f324322015-04-08 11:01:54 -07001187 }
1188
1189 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1190 cache->purgeAllUnlocked();
1191 }
1192
1193 // Do a similar test but where we leave refs on some resources to prevent them from being
1194 // purged.
1195 {
1196 GrGpuResource* refedResources[kFlushCount >> 1];
1197 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001198 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001199 GrUniqueKey k;
1200 make_unique_key<1>(&k, i);
1201 r->resourcePriv().setUniqueKey(k);
1202 // Leave a ref on every other resource, beginning with the first.
1203 if (SkToBool(i & 0x1)) {
1204 refedResources[i/2] = r;
1205 } else {
1206 r->unref();
1207 }
bsalomonb77a9072016-09-07 10:02:04 -07001208 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001209 }
1210
1211 for (int i = 0; i < kFlushCount; ++i) {
1212 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001213 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001214 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001215 }
1216
1217 // Unref all the resources that we kept refs on in the first loop.
1218 for (int i = 0; i < kFlushCount >> 1; ++i) {
1219 refedResources[i]->unref();
1220 }
1221
bsalomone2e87f32016-09-22 12:42:11 -07001222 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1223 // kFlushCount full flushes.
1224 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001225 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001226 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001227 }
1228 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1229
1230 cache->purgeAllUnlocked();
1231 }
1232
1233 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001234
1235 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1236 // eviction.
1237 context->flush();
1238 for (int i = 0; i < 10; ++i) {
1239 TestResource* r = new TestResource(context->getGpu());
1240 GrUniqueKey k;
1241 make_unique_key<1>(&k, i);
1242 r->resourcePriv().setUniqueKey(k);
1243 r->unref();
1244 }
1245 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1246 for (int i = 0; i < 10 * kFlushCount; ++i) {
1247 context->flush();
1248 }
1249 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001250}
1251
Brian Salomon5e150852017-03-22 14:53:13 -04001252static void test_time_purge(skiatest::Reporter* reporter) {
1253 Mock mock(1000000, 1000000);
1254 GrContext* context = mock.context();
1255 GrResourceCache* cache = mock.cache();
1256
1257 static constexpr int kCnts[] = {1, 10, 1024};
1258 auto nowish = []() {
1259 // We sleep so that we ensure we get a value that is greater than the last call to
1260 // GrStdSteadyClock::now().
1261 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1262 auto result = GrStdSteadyClock::now();
1263 // Also sleep afterwards so we don't get this value again.
1264 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1265 return result;
1266 };
1267
1268 for (int cnt : kCnts) {
1269 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1270 new GrStdSteadyClock::time_point[cnt]);
1271 {
1272 // Insert resources and get time points between each addition.
1273 for (int i = 0; i < cnt; ++i) {
1274 TestResource* r = new TestResource(context->getGpu());
1275 GrUniqueKey k;
1276 make_unique_key<1>(&k, i);
1277 r->resourcePriv().setUniqueKey(k);
1278 r->unref();
1279 timeStamps.get()[i] = nowish();
1280 }
1281
1282 // Purge based on the time points between resource additions. Each purge should remove
1283 // the oldest resource.
1284 for (int i = 0; i < cnt; ++i) {
1285 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1286 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1287 for (int j = 0; j < i; ++j) {
1288 GrUniqueKey k;
1289 make_unique_key<1>(&k, j);
1290 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1291 REPORTER_ASSERT(reporter, !SkToBool(r));
1292 SkSafeUnref(r);
1293 }
1294 }
1295
1296 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1297 cache->purgeAllUnlocked();
1298 }
1299
1300 // Do a similar test but where we leave refs on some resources to prevent them from being
1301 // purged.
1302 {
1303 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1304 for (int i = 0; i < cnt; ++i) {
1305 TestResource* r = new TestResource(context->getGpu());
1306 GrUniqueKey k;
1307 make_unique_key<1>(&k, i);
1308 r->resourcePriv().setUniqueKey(k);
1309 // Leave a ref on every other resource, beginning with the first.
1310 if (SkToBool(i & 0x1)) {
1311 refedResources.get()[i / 2] = r;
1312 } else {
1313 r->unref();
1314 }
1315 timeStamps.get()[i] = nowish();
1316 }
1317
1318 for (int i = 0; i < cnt; ++i) {
1319 // Should get a resource purged every other frame.
1320 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1321 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1322 }
1323
1324 // Unref all the resources that we kept refs on in the first loop.
1325 for (int i = 0; i < (cnt / 2); ++i) {
1326 refedResources.get()[i]->unref();
1327 cache->purgeResourcesNotUsedSince(nowish());
1328 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1329 }
1330
1331 cache->purgeAllUnlocked();
1332 }
1333
1334 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1335
1336 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1337 // eviction
1338 context->flush();
1339 for (int i = 0; i < 10; ++i) {
1340 TestResource* r = new TestResource(context->getGpu());
1341 GrUniqueKey k;
1342 make_unique_key<1>(&k, i);
1343 r->resourcePriv().setUniqueKey(k);
1344 r->unref();
1345 }
1346 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1347 context->flush();
1348 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1349 cache->purgeResourcesNotUsedSince(nowish());
1350 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1351 }
1352}
1353
Derek Sollenberger5480a182017-05-25 16:43:59 -04001354static void test_partial_purge(skiatest::Reporter* reporter) {
1355 Mock mock(6, 100);
1356 GrContext* context = mock.context();
1357 GrResourceCache* cache = mock.cache();
1358
1359 enum TestsCase {
1360 kOnlyScratch_TestCase = 0,
1361 kPartialScratch_TestCase = 1,
1362 kAllScratch_TestCase = 2,
1363 kPartial_TestCase = 3,
1364 kAll_TestCase = 4,
1365 kNone_TestCase = 5,
1366 kEndTests_TestCase = kNone_TestCase + 1
1367 };
1368
1369 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1370
1371 GrUniqueKey key1, key2, key3;
1372 make_unique_key<0>(&key1, 1);
1373 make_unique_key<0>(&key2, 2);
1374 make_unique_key<0>(&key3, 3);
1375
1376 // Add three unique resources to the cache.
1377 TestResource *unique1 = new TestResource(context->getGpu());
1378 TestResource *unique2 = new TestResource(context->getGpu());
1379 TestResource *unique3 = new TestResource(context->getGpu());
1380
1381 unique1->resourcePriv().setUniqueKey(key1);
1382 unique2->resourcePriv().setUniqueKey(key2);
1383 unique3->resourcePriv().setUniqueKey(key3);
1384
1385 unique1->setSize(10);
1386 unique2->setSize(11);
1387 unique3->setSize(12);
1388
1389 // Add two scratch resources to the cache.
1390 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1391 TestResource::kA_SimulatedProperty);
1392 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1393 TestResource::kB_SimulatedProperty);
1394 scratch1->setSize(13);
1395 scratch2->setSize(14);
1396
1397
1398 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1399 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1400 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1401
1402 // Add resources to the purgeable queue
1403 unique1->unref();
1404 scratch1->unref();
1405 unique2->unref();
1406 scratch2->unref();
1407 unique3->unref();
1408
1409 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1410 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1411 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1412
1413 switch(testCase) {
1414 case kOnlyScratch_TestCase: {
1415 context->purgeUnlockedResources(14, true);
1416 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1417 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1418 break;
1419 }
1420 case kPartialScratch_TestCase: {
1421 context->purgeUnlockedResources(3, true);
1422 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1423 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1424 break;
1425 }
1426 case kAllScratch_TestCase: {
1427 context->purgeUnlockedResources(50, true);
1428 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1429 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1430 break;
1431 }
1432 case kPartial_TestCase: {
1433 context->purgeUnlockedResources(13, false);
1434 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1435 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1436 break;
1437 }
1438 case kAll_TestCase: {
1439 context->purgeUnlockedResources(50, false);
1440 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1441 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1442 break;
1443 }
1444 case kNone_TestCase: {
1445 context->purgeUnlockedResources(0, true);
1446 context->purgeUnlockedResources(0, false);
1447 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1448 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1449 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1450 break;
1451 }
1452 };
1453
1454 // ensure all are purged before the next
1455 context->purgeAllUnlockedResources();
1456 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1457 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1458
1459 }
1460}
1461
bsalomon10e23ca2014-11-25 05:52:06 -08001462static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001463 // Set the cache size to double the resource count because we're going to create 2x that number
1464 // resources, using two different key domains. Add a little slop to the bytes because we resize
1465 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001466 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001467
bsalomonc2f35b72015-01-23 07:19:22 -08001468 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1469 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001470 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001471
1472 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001473 GrUniqueKey key1, key2;
1474 make_unique_key<1>(&key1, i);
1475 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001476
bsalomon24db3b12015-01-23 04:24:04 -08001477 TestResource* resource;
1478
halcanary385fe4d2015-08-26 13:07:48 -07001479 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001480 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001481 resource->setSize(1);
1482 resource->unref();
1483
halcanary385fe4d2015-08-26 13:07:48 -07001484 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001485 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001486 resource->setSize(1);
1487 resource->unref();
1488 }
1489
1490 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001491 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001492 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1493 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1494 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1495 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001496 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001497 GrUniqueKey key1, key2;
1498 make_unique_key<1>(&key1, i);
1499 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001500
bsalomon8718aaf2015-02-19 07:24:21 -08001501 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1502 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001503 }
1504
bsalomon0ea80f42015-02-11 10:49:59 -08001505 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001506 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001507 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001508 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1509 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1510 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1511 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001512
1513 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001514 GrUniqueKey key1, key2;
1515 make_unique_key<1>(&key1, i);
1516 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001517
bsalomon8718aaf2015-02-19 07:24:21 -08001518 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1519 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001520 }
1521}
1522
senorblanco84cd6212015-08-04 10:01:58 -07001523static void test_custom_data(skiatest::Reporter* reporter) {
1524 GrUniqueKey key1, key2;
1525 make_unique_key<0>(&key1, 1);
1526 make_unique_key<0>(&key2, 2);
1527 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001528 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001529 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1530 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1531
1532 // Test that copying a key also takes a ref on its custom data.
1533 GrUniqueKey key3 = key1;
1534 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1535}
1536
bsalomonc6363ef2015-09-24 07:07:40 -07001537static void test_abandoned(skiatest::Reporter* reporter) {
1538 Mock mock(10, 300);
1539 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001540 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001541 context->abandonContext();
1542
1543 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1544
1545 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1546
robertphillips8abb3702016-08-31 14:04:06 -07001547 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001548 resource->getUniqueKey();
1549 resource->wasDestroyed();
1550 resource->gpuMemorySize();
1551 resource->getContext();
1552
1553 resource->abandon();
1554 resource->resourcePriv().getScratchKey();
1555 resource->resourcePriv().isBudgeted();
1556 resource->resourcePriv().makeBudgeted();
1557 resource->resourcePriv().makeUnbudgeted();
1558 resource->resourcePriv().removeScratchKey();
1559 GrUniqueKey key;
1560 make_unique_key<0>(&key, 1);
1561 resource->resourcePriv().setUniqueKey(key);
1562 resource->resourcePriv().removeUniqueKey();
1563}
1564
Brian Salomon1090da62017-01-06 12:04:19 -05001565static void test_tags(skiatest::Reporter* reporter) {
1566#ifdef SK_DEBUG
1567 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1568 static constexpr int kLastTagIdx = 10;
1569 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1570
1571 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1572 GrContext* context = mock.context();
1573 GrResourceCache* cache = mock.cache();
1574
1575 SkString tagStr;
1576 int tagIdx = 0;
1577 int currTagCnt = 0;
1578
1579 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1580 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1581 GrUniqueKey key;
1582 if (currTagCnt == tagIdx) {
1583 tagIdx += 1;
1584 currTagCnt = 0;
1585 tagStr.printf("tag%d", tagIdx);
1586 }
1587 make_unique_key<1>(&key, i, tagStr.c_str());
1588 resource->resourcePriv().setUniqueKey(key);
1589 }
1590 SkASSERT(kLastTagIdx == tagIdx);
1591 SkASSERT(currTagCnt == kLastTagIdx);
1592
1593 // Test i = 0 to exercise unused tag string.
1594 for (int i = 0; i <= kLastTagIdx; ++i) {
1595 tagStr.printf("tag%d", i);
1596 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1597 }
1598#endif
1599}
1600
Brian Salomondcfca432017-11-15 15:48:03 -05001601DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001602 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001603 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001604 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001605 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001606 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001607 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001608 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001609 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001610 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001611 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001612 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001613 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001614 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001615 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001616 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001617 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001618 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001619 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001620 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001621 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001622}
1623
Robert Phillipsd6214d42016-11-07 08:23:48 -05001624////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001625static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001626 GrSurfaceFlags flags,
1627 int width, int height,
1628 int sampleCnt) {
1629 GrSurfaceDesc desc;
1630 desc.fFlags = flags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001631 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001632 desc.fWidth = width;
1633 desc.fHeight = height;
1634 desc.fConfig = kRGBA_8888_GrPixelConfig;
1635 desc.fSampleCnt = sampleCnt;
1636
Robert Phillipse78b7252017-04-06 07:59:41 -04001637 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001638}
1639
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001640static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipse78b7252017-04-06 07:59:41 -04001641 GrSurfaceFlags flags,
1642 int width, int height,
1643 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001644 SkBitmap bm;
1645
1646 bm.allocN32Pixels(width, height, true);
1647 bm.eraseColor(SK_ColorBLUE);
1648
Brian Osman7b8400d2016-11-08 17:08:54 -05001649 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001650 SkASSERT(mipmaps);
1651 SkASSERT(mipmaps->countLevels() > 1);
1652
1653 int mipLevelCount = mipmaps->countLevels() + 1;
1654
1655 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1656
1657 texels[0].fPixels = bm.getPixels();
1658 texels[0].fRowBytes = bm.rowBytes();
1659
1660 for (int i = 1; i < mipLevelCount; ++i) {
1661 SkMipMap::Level generatedMipLevel;
1662 mipmaps->getLevel(i - 1, &generatedMipLevel);
1663 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1664 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1665 }
1666
1667 GrSurfaceDesc desc;
1668 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001669 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1670 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001671 desc.fWidth = width;
1672 desc.fHeight = height;
1673 desc.fConfig = kRGBA_8888_GrPixelConfig;
1674 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001675
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001676 return proxyProvider->createMipMapProxy(desc, SkBudgeted::kYes, texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001677}
1678
1679// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1680// Texture-only, both-RT-and-Texture and MIPmapped
1681DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1682 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001683 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001684 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001685
Robert Phillipsd6214d42016-11-07 08:23:48 -05001686 static const int kSize = 64;
1687
Robert Phillipsd6214d42016-11-07 08:23:48 -05001688 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001689 {
1690 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001691
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001692 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001693 size_t size = tex->gpuMemorySize();
1694 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1695
Greg Daniel81e7bf82017-07-19 14:47:42 -04001696 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1697 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001698 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001699 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001700 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001701 REPORTER_ASSERT(reporter,
1702 kSize*kSize*4 == size || // msaa4 failed
1703 kSize*kSize*4*sampleCount == size || // auto-resolving
1704 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001705 }
1706
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001707 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001708 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001709 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001710 }
1711
Robert Phillipsd6214d42016-11-07 08:23:48 -05001712
1713 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001714 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001715 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001716
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001717 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001718 size_t size = proxy->gpuMemorySize();
1719 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1720
Greg Daniel81e7bf82017-07-19 14:47:42 -04001721 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1722 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001723 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001724 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001725 size = proxy->gpuMemorySize();
1726 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001727 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1728 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1729 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001730 }
Robert Phillips1b352562017-04-05 18:56:21 +00001731
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001732 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001733 size = proxy->gpuMemorySize();
1734 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1735 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001736}
1737
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001738#endif