blob: b27aaea9f762e96e8a39da59929c5fbb050b5d5a [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"
bsalomon0ea80f42015-02-11 10:49:59 -080019#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070020#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070021#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040022#include "GrTexture.h"
23
bsalomonbcf0a522014-10-08 08:40:09 -070024#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080025#include "SkGr.h"
26#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050027#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070028#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000029#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000030
31static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070036 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080037 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040038 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080039 desc.fFlags = kRenderTarget_GrSurfaceFlag;
40 desc.fWidth = gWidth;
41 desc.fHeight = gHeight;
42 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070043 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080044 SkCanvas* canvas = surface->getCanvas();
45
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000046 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
47
48 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000049 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050 src.eraseColor(SK_ColorBLACK);
51 size_t srcSize = src.getSize();
52
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000053 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070054 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000055
56 int oldMaxNum;
57 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000058 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000059
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060 // Set the cache limits so we can fit 10 "src" images and the
61 // max number of textures doesn't matter
62 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000063 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000066 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000067
68 for (int i = 0; i < 100; ++i) {
69 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040070 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000071
72 // "modify" the src texture
73 src.notifyPixelsChanged();
74
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000075 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070076 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000077
78 // we should never go over the size limit
79 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
80 }
81
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000082 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000083}
84
bsalomon11abd8d2016-10-14 08:13:48 -070085static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
86 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
87 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
88 return false;
89 }
90 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
91}
92
93// This currently fails on ES3 ANGLE contexts
94DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
95 ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070096 GrContext* context = ctxInfo.grContext();
bsalomon02a44a42015-02-19 09:09:00 -080097 GrSurfaceDesc smallDesc;
98 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -040099 smallDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800100 smallDesc.fWidth = 4;
101 smallDesc.fHeight = 4;
102 smallDesc.fSampleCnt = 0;
103
Eric Karl5c779752017-05-08 12:02:07 -0700104 if (context->caps()->avoidStencilBuffers()) {
105 return;
106 }
egdanielec00d942015-09-14 12:56:10 -0700107 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800108 // Test that two budgeted RTs with the same desc share a stencil buffer.
Brian Osman32342f02017-03-04 08:12:46 -0500109 sk_sp<GrTexture> smallRT0(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800110 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700111 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800112 }
113
Brian Osman32342f02017-03-04 08:12:46 -0500114 sk_sp<GrTexture> smallRT1(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800115 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700116 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800117 }
118
egdaniel8dc7c3a2015-04-16 11:22:42 -0700119 REPORTER_ASSERT(reporter,
120 smallRT0 && smallRT1 &&
121 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700122 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
123 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800124
125 // An unbudgeted RT with the same desc should also share.
Brian Osman32342f02017-03-04 08:12:46 -0500126 sk_sp<GrTexture> smallRT2(resourceProvider->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800127 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700128 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800129 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700130 REPORTER_ASSERT(reporter,
131 smallRT0 && smallRT2 &&
132 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700133 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
134 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800135
136 // An RT with a much larger size should not share.
137 GrSurfaceDesc bigDesc;
138 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400139 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800140 bigDesc.fWidth = 400;
141 bigDesc.fHeight = 200;
142 bigDesc.fSampleCnt = 0;
Brian Osman32342f02017-03-04 08:12:46 -0500143 sk_sp<GrTexture> bigRT(resourceProvider->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800144 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700145 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800146 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700147 REPORTER_ASSERT(reporter,
148 smallRT0 && bigRT &&
149 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700150 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
151 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800152
Greg Daniel81e7bf82017-07-19 14:47:42 -0400153 int supportedSampleCount = context->caps()->getSampleCount(4, smallDesc.fConfig);
154 if (supportedSampleCount > 0) {
mtklein5f939ab2016-03-16 10:28:35 -0700155 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800156 GrSurfaceDesc smallMSAADesc = smallDesc;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400157 smallMSAADesc.fSampleCnt = supportedSampleCount;
Brian Osman32342f02017-03-04 08:12:46 -0500158 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
159 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800160 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700161 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800162 }
bsalomonb602d4d2015-02-19 12:05:58 -0800163#ifdef SK_BUILD_FOR_ANDROID
164 if (!smallMSAART0) {
165 // The nexus player seems to fail to create MSAA textures.
166 return;
167 }
168#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800169 REPORTER_ASSERT(reporter,
170 smallRT0 && smallMSAART0 &&
171 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700172 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
173 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800174 // A second MSAA RT should share with the first MSAA RT.
Brian Osman32342f02017-03-04 08:12:46 -0500175 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
176 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800177 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700178 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800179 }
180 REPORTER_ASSERT(reporter,
181 smallMSAART0 && smallMSAART1 &&
182 smallMSAART0->asRenderTarget() &&
183 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700184 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
185 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
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.).
Greg Daniel81e7bf82017-07-19 14:47:42 -0400188 supportedSampleCount = context->caps()->getSampleCount(8, smallDesc.fConfig);
189 if (supportedSampleCount != smallMSAADesc.fSampleCnt &&
190 smallMSAART0 && smallMSAART0->asRenderTarget()) {
191 smallMSAADesc.fSampleCnt = supportedSampleCount;
Robert Phillipse78b7252017-04-06 07:59:41 -0400192 smallMSAART1 = resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo);
Hal Canary342b7ac2016-11-04 11:49:42 -0400193 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500194 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800195 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700196 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800197 }
198 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700199 smallMSAART0 && smallMSAART1 &&
200 smallMSAART0->asRenderTarget() &&
201 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700202 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
203 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800204 }
205 }
206}
207
bsalomon68d91342016-04-12 09:59:58 -0700208DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700209 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800210 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700211 // this test is only valid for GL
212 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700213 return;
214 }
215
Brian Osman85d34b22017-05-10 12:06:26 -0400216 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217 static const int kW = 100;
218 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700219
bsalomon091f60c2015-11-10 11:54:56 -0800220 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
221 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700222
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223 context->resetContext();
224
Greg Daniel7ef28f32017-04-20 16:41:55 +0000225 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
226 kW,
227 kH,
228 kRGBA_8888_GrPixelConfig,
229 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500230 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Brian Salomond17f6582017-07-19 18:28:58 -0400231 backendTex1, kTopLeft_GrSurfaceOrigin, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232
Greg Daniel7ef28f32017-04-20 16:41:55 +0000233 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
234 kW,
235 kH,
236 kRGBA_8888_GrPixelConfig,
237 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500238 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Brian Salomond17f6582017-07-19 18:28:58 -0400239 backendTex2, kTopLeft_GrSurfaceOrigin, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
Brian Osman85d34b22017-05-10 12:06:26 -0400241 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
242 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700243 return;
244 }
245
halcanary96fcdcc2015-08-27 07:41:13 -0700246 borrowed.reset(nullptr);
247 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700248
249 context->flush();
250
bsalomon091f60c2015-11-10 11:54:56 -0800251 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
252 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700253
254 REPORTER_ASSERT(reporter, borrowedIsAlive);
255 REPORTER_ASSERT(reporter, !adoptedIsAlive);
256
bsalomon67d76202015-11-11 12:40:42 -0800257 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
258 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700259
260 context->resetContext();
261}
262
bsalomon6d3fe022014-07-25 08:35:45 -0700263class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800264 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000265public:
robertphillips6e83ac72015-08-13 05:19:14 -0700266 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700267
bsalomon1c60dfe2015-01-21 09:32:40 -0800268 /** Property that distinctly categorizes the resource.
269 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800270 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800271
kkinnunen2e6055b2016-04-22 01:48:29 -0700272 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
273 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700274 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800275 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700276 , fProperty(kA_SimulatedProperty)
277 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800278 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700279 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800280 }
281
kkinnunen2e6055b2016-04-22 01:48:29 -0700282 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
283 SimulatedProperty property) {
284 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800285 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700286 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
287 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000288 }
289
Brian Salomond3b65972017-03-22 12:05:03 -0400290 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800291 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800292 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293 }
294
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000295 void setSize(size_t size) {
296 fSize = size;
297 this->didChangeGpuMemorySize();
298 }
299
bsalomon33435572014-11-05 14:47:41 -0800300 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000301
bsalomon71cb0c22014-11-14 12:10:14 -0800302 void setUnrefWhenDestroyed(TestResource* resource) {
303 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000304 }
305
bsalomon1c60dfe2015-01-21 09:32:40 -0800306 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
307 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
308 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800309 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
310 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800311 }
312 }
313
314 static size_t ExpectedScratchKeySize() {
315 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
316 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000317private:
bsalomon24db3b12015-01-23 04:24:04 -0800318 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800319
kkinnunen2e6055b2016-04-22 01:48:29 -0700320 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
321 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700322 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800323 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700324 , fProperty(property)
325 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800326 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700327 this->registerWithCache(budgeted);
328 }
329
330 // Constructor for simulating resources that wrap backend objects.
331 TestResource(GrGpu* gpu, size_t size)
332 : INHERITED(gpu)
333 , fToDelete(nullptr)
334 , fSize(size)
335 , fProperty(kA_SimulatedProperty)
336 , fIsScratch(false) {
337 ++fNumAlive;
338 this->registerWithCacheWrapped();
339 }
340
341 void computeScratchKey(GrScratchKey* key) const override {
342 if (fIsScratch) {
343 ComputeScratchKey(fProperty, key);
344 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800345 }
346
mtklein36352bf2015-03-25 18:17:31 -0700347 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800348
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000349 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000350 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800351 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800352 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700353 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700354 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000355};
bsalomon33435572014-11-05 14:47:41 -0800356int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000357
bsalomonc2f35b72015-01-23 07:19:22 -0800358class Mock {
359public:
360 Mock(int maxCnt, size_t maxBytes) {
Brian Salomon91a3e522017-06-23 10:58:19 -0400361 fContext.reset(GrContext::Create(kMock_GrBackend, (GrBackendContext) nullptr));
bsalomonc2f35b72015-01-23 07:19:22 -0800362 SkASSERT(fContext);
363 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800364 GrResourceCache* cache = fContext->getResourceCache();
365 cache->purgeAllUnlocked();
366 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800367 }
bsalomonc2f35b72015-01-23 07:19:22 -0800368
bsalomon0ea80f42015-02-11 10:49:59 -0800369 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800370
Hal Canary342b7ac2016-11-04 11:49:42 -0400371 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800372
373private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400374 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800375};
376
377static void test_no_key(skiatest::Reporter* reporter) {
378 Mock mock(10, 30000);
379 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800380 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800381
382 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700383 TestResource* a = new TestResource(context->getGpu());
384 TestResource* b = new TestResource(context->getGpu());
385 TestResource* c = new TestResource(context->getGpu());
386 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800387 a->setSize(11);
388 b->setSize(12);
389 c->setSize(13);
390 d->setSize(14);
391
392 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800393 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800394 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800395 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800396
397 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800398 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800399 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
400
bsalomon8718aaf2015-02-19 07:24:21 -0800401 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800402
403 a->unref();
404 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800405 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800406 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800407 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800408
409 c->unref();
410 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800411 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800412 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800413 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800414
415 d->unref();
416 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800417 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
418 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800419
420 b->unref();
421 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800422 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
423 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800424}
425
bsalomon24db3b12015-01-23 04:24:04 -0800426// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500427template <int>
428static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800429 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500430 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800431 builder[0] = data;
432}
433
bsalomon84c8e622014-11-17 09:33:27 -0800434static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800435 Mock mock(10, 300);
436 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800437 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800438
bsalomon8718aaf2015-02-19 07:24:21 -0800439 GrUniqueKey uniqueKey;
440 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800441
bsalomon8718aaf2015-02-19 07:24:21 -0800442 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800443 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700444 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800445 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700446 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800447 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800448 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700449 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800450 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700451 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700452 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800453 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800454
Brian Osman0562eb92017-05-08 11:16:39 -0400455 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800456 GrUniqueKey uniqueKey2;
457 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800458 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400459 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
460 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
461
462 // Remove the extra ref we just added.
463 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800464
465 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800466 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800467 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800468 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800469 cache->getResourceBytes());
470 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800471 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800472 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400473 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800474
bsalomon63c992f2015-01-23 12:47:59 -0800475 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800476 cache->purgeAllUnlocked();
477 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800478 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800479 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800480 cache->getResourceBytes());
481 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800482 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800483 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400484 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800485
486 // Unreffing the wrapped resource should free it right away.
487 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800488 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800489 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800490 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400491 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800492
bsalomon84c8e622014-11-17 09:33:27 -0800493 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700494 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800495 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800496 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400497 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800498 cache->purgeAllUnlocked();
499 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800500 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800501 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
502 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
503 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400504 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800505
506 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400507 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800508 cache->purgeAllUnlocked();
509 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800510 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800511 cache->getResourceBytes());
512 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400514 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800515
516 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800517 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
518 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
519 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
520 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400521 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800522
523 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800524 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
525 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
526 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
527 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400528 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800529}
530
bsalomon5236cf42015-01-14 10:42:08 -0800531static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800532 Mock mock(10, 30000);
533 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800534 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800535
bsalomon8718aaf2015-02-19 07:24:21 -0800536 GrUniqueKey uniqueKey;
537 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800538
539 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800540 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800541 TestResource* wrapped;
542 TestResource* unbudgeted;
543
544 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700545 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
546 TestResource::kB_SimulatedProperty);
547
bsalomon5236cf42015-01-14 10:42:08 -0800548 scratch->setSize(10);
549 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800550 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400554 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800555
halcanary385fe4d2015-08-26 13:07:48 -0700556 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800557 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800558 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800559 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800560 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
561 REPORTER_ASSERT(reporter, 21 == 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
bsalomon0ea80f42015-02-11 10:49:59 -0800566 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700567 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800568 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
569 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
570 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
571 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400572 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800573
574 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
576 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
577 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
578 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400579 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800580
kkinnunen2e6055b2016-04-22 01:48:29 -0700581 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800582 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400586 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800587
588 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800589 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
590 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
591 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
592 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400593 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800594
bsalomon0ea80f42015-02-11 10:49:59 -0800595 cache->purgeAllUnlocked();
596 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
597 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
598 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
599 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400600 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800601}
602
bsalomon3582d3e2015-02-13 14:20:05 -0800603// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
604void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
605/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800606 Mock mock(10, 300);
607 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800608 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800609
610 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700611 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
612 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800613 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800614 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800615
616 size_t size = resource->gpuMemorySize();
617 for (int i = 0; i < 2; ++i) {
618 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800619 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800620 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800621 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700622 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800623 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
624 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
625 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
626 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400627 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800628
629 // Once it is unrefed, it should become available as scratch.
630 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800631 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
632 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
633 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
634 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400635 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700636 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800637 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800638 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800639 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800640 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800641
642 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700643 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800644 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800645 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800646 } else {
647 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800648 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800649 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
650 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
651 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
652 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400653 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800654 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800655 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800656 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800657
658 // now when it is unrefed it should die since it has no key.
659 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800660 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
661 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
662 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
663 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400664 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800665 }
bsalomon8b79d232014-11-10 10:19:06 -0800666 }
bsalomonc2f35b72015-01-23 07:19:22 -0800667}
668
669static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
670 Mock mock(5, 30000);
671 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800672 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800673
bsalomon8b79d232014-11-10 10:19:06 -0800674 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800675 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700676 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800677 TestResource::kB_SimulatedProperty);
678 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700679 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800680 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800681 a->setSize(11);
682 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800683 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800684 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800685 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700686 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800687
688 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800689 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800690
bsalomon0ea80f42015-02-11 10:49:59 -0800691 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800692 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800693 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
694 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800695 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800696 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800697
bsalomon63c992f2015-01-23 12:47:59 -0800698 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800699 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800700 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800701 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800702
703 // Unref but don't purge
704 a->unref();
705 b->unref();
706 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800707 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800708
bsalomon63c992f2015-01-23 12:47:59 -0800709 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800710 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800711 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800712 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
713 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800714}
715
bsalomon10e23ca2014-11-25 05:52:06 -0800716static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800717 Mock mock(5, 30000);
718 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800719 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800720
bsalomon10e23ca2014-11-25 05:52:06 -0800721 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700722 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800723 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700724 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800725 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800726 a->unref();
727 b->unref();
728
bsalomon1c60dfe2015-01-21 09:32:40 -0800729 GrScratchKey scratchKey;
730 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800731 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800732 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700733 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800734
bsalomon0ea80f42015-02-11 10:49:59 -0800735 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800736 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800737 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800738 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
739 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800740
741 // Find the first resource and remove its scratch key
742 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700743 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800744 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800745 // It's still alive, but not cached by scratch key anymore
746 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800747 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
748 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800749
750 // The cache should immediately delete it when it's unrefed since it isn't accessible.
751 find->unref();
752 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800753 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
754 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800755
756 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700757 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800758 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800759 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800760 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
761 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800762
763 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800764 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800765 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800766 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
767 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800768
769 find->unref();
770 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800771 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
772 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800773}
774
bsalomon1c60dfe2015-01-21 09:32:40 -0800775static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800776 Mock mock(5, 30000);
777 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800778 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800779
780 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700781 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800782 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700783 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800784 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800785 a->unref();
786 b->unref();
787
788 GrScratchKey scratchKey;
789 // Ensure that scratch key comparison and assignment is consistent.
790 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800791 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800792 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800793 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800794 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
795 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
796 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
797 scratchKey = scratchKey1;
798 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
799 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
800 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
801 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
802 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
803 scratchKey = scratchKey2;
804 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
805 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
806 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
807 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
808 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
809
810 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800811 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800812 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700813 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800814
815 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800816 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700817 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700818 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800819 find->unref();
820
821 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700822 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700823 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800824 REPORTER_ASSERT(reporter, find == a || find == b);
825
robertphillips6e83ac72015-08-13 05:19:14 -0700826 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700827 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800828 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
829 REPORTER_ASSERT(reporter, find2 != find);
830 find2->unref();
831 find->unref();
832}
833
bsalomon8718aaf2015-02-19 07:24:21 -0800834static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800835 Mock mock(5, 30000);
836 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800837 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000838
bsalomon8718aaf2015-02-19 07:24:21 -0800839 GrUniqueKey key;
840 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700841
bsalomon8718aaf2015-02-19 07:24:21 -0800842 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700843 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800844 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700845
bsalomonf99e9612015-02-19 08:24:16 -0800846 // Set key on resource a.
847 a->resourcePriv().setUniqueKey(key);
848 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
849 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800850
bsalomonf99e9612015-02-19 08:24:16 -0800851 // Make sure that redundantly setting a's key works.
852 a->resourcePriv().setUniqueKey(key);
853 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800854 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800855 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
856 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800857 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
858
bsalomonf99e9612015-02-19 08:24:16 -0800859 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700860 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800861 b->setSize(12);
862 b->resourcePriv().setUniqueKey(key);
863 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
864 b->unref();
865
866 // Still have two resources because a is still reffed.
867 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
868 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
869 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
870
871 a->unref();
872 // Now a should be gone.
873 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
874 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
875 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
876
877 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
878 // Also make b be unreffed when replacement occurs.
879 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700880 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800881 GrUniqueKey differentKey;
882 make_unique_key<0>(&differentKey, 1);
883 c->setSize(13);
884 c->resourcePriv().setUniqueKey(differentKey);
885 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
886 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
887 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
888 // c replaces b and b should be immediately purged.
889 c->resourcePriv().setUniqueKey(key);
890 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
891 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
892 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
893
894 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800895 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800896 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
897 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
898 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
899
900 // Drop the ref on c, it should be kept alive because it has a unique key.
901 c->unref();
902 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
903 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
904 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
905
906 // Verify that we can find c, then remove its unique key. It should get purged immediately.
907 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
908 c->resourcePriv().removeUniqueKey();
909 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800910 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
911 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800912 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700913
914 {
915 GrUniqueKey key2;
916 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400917 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700918 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700919 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700920 d->resourcePriv().setUniqueKey(key2);
921 }
922
923 GrUniqueKey key3;
924 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400925 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700926 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000927}
928
bsalomon8b79d232014-11-10 10:19:06 -0800929static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800930 Mock mock(5, 30000);
931 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800932 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800933
bsalomon8718aaf2015-02-19 07:24:21 -0800934 GrUniqueKey key1, key2, key3;
935 make_unique_key<0>(&key1, 1);
936 make_unique_key<0>(&key2, 2);
937 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700938
bsalomon23e619c2015-02-06 11:54:28 -0800939 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700940 TestResource* a = new TestResource(context->getGpu());
941 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700942 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800943 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800944 a->resourcePriv().setUniqueKey(key1);
945 b->resourcePriv().setUniqueKey(key2);
946 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800947 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800948 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800949 c->unref();
950
bsalomon8718aaf2015-02-19 07:24:21 -0800951 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
952 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
953 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800954 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800955
bsalomon8718aaf2015-02-19 07:24:21 -0800956 typedef GrUniqueKeyInvalidatedMessage Msg;
957 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800958
959 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
960 Bus::Post(Msg(key1));
961 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800962 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800963 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800964 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
965 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800966 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800967 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800968
969 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800970 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800971 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800972 // we still have a ref on b, c should be recycled as scratch.
973 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800974 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800975
bsalomon23e619c2015-02-06 11:54:28 -0800976 // make b purgeable. It should be immediately deleted since it has no key.
977 b->unref();
978 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
979
980 // Make sure we actually get to c via it's scratch key, before we say goodbye.
981 GrScratchKey scratchKey;
982 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700983 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800984 REPORTER_ASSERT(reporter, scratch == c);
985 SkSafeUnref(scratch);
986
987 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800988 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700989 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800990 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800991 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
992 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800993 REPORTER_ASSERT(reporter, !scratch);
994 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800995}
996
bsalomon71cb0c22014-11-14 12:10:14 -0800997static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800998 Mock mock(3, 30000);
999 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001000 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -08001001
bsalomon8718aaf2015-02-19 07:24:21 -08001002 GrUniqueKey key1, key2;
1003 make_unique_key<0>(&key1, 1);
1004 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001005
halcanary385fe4d2015-08-26 13:07:48 -07001006 TestResource* a = new TestResource(context->getGpu());
1007 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001008 a->resourcePriv().setUniqueKey(key1);
1009 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001010
bsalomonc2f35b72015-01-23 07:19:22 -08001011 // Make a cycle
1012 a->setUnrefWhenDestroyed(b);
1013 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001014
bsalomonc2f35b72015-01-23 07:19:22 -08001015 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001016
bsalomonc2f35b72015-01-23 07:19:22 -08001017 a->unref();
1018 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001019
bsalomonc2f35b72015-01-23 07:19:22 -08001020 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001021
bsalomon0ea80f42015-02-11 10:49:59 -08001022 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001023 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001024
bsalomonc2f35b72015-01-23 07:19:22 -08001025 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001026 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001027 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001028
bsalomon0ea80f42015-02-11 10:49:59 -08001029 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001030 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001031}
1032
bsalomon8b79d232014-11-10 10:19:06 -08001033static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001034 GrUniqueKey key1, key2;
1035 make_unique_key<0>(&key1, 1);
1036 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001037
1038 // Test changing resources sizes (both increase & decrease).
1039 {
bsalomonc2f35b72015-01-23 07:19:22 -08001040 Mock mock(3, 30000);
1041 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001042 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001043
halcanary385fe4d2015-08-26 13:07:48 -07001044 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001045 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001046 a->unref();
1047
halcanary385fe4d2015-08-26 13:07:48 -07001048 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001049 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001050 b->unref();
1051
bsalomon0ea80f42015-02-11 10:49:59 -08001052 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1053 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001054 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001055 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001056 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001057 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001058 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001059 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001060 find1->setSize(50);
1061 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001062
bsalomon0ea80f42015-02-11 10:49:59 -08001063 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1064 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001065 }
1066
1067 // Test increasing a resources size beyond the cache budget.
1068 {
bsalomonc2f35b72015-01-23 07:19:22 -08001069 Mock mock(2, 300);
1070 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001071 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001072
halcanary385fe4d2015-08-26 13:07:48 -07001073 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001074 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001075 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001076 a->unref();
1077
halcanary385fe4d2015-08-26 13:07:48 -07001078 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001079 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001080 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001081 b->unref();
1082
bsalomon0ea80f42015-02-11 10:49:59 -08001083 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1084 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001085
bsalomon8b79d232014-11-10 10:19:06 -08001086 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001087 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001088 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001089 find2->setSize(201);
1090 }
bsalomon8718aaf2015-02-19 07:24:21 -08001091 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001092
bsalomon0ea80f42015-02-11 10:49:59 -08001093 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1094 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001095 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001096}
1097
bsalomonddf30e62015-02-19 11:38:44 -08001098static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1099 static const int kCount = 50;
1100 static const int kBudgetCnt = kCount / 2;
1101 static const int kLockedFreq = 8;
1102 static const int kBudgetSize = 0x80000000;
1103
1104 SkRandom random;
1105
1106 // Run the test 2*kCount times;
1107 for (int i = 0; i < 2 * kCount; ++i ) {
1108 Mock mock(kBudgetCnt, kBudgetSize);
1109 GrContext* context = mock.context();
1110 GrResourceCache* cache = mock.cache();
1111
1112 // Pick a random number of resources to add before the timestamp will wrap.
1113 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1114
1115 static const int kNumToPurge = kCount - kBudgetCnt;
1116
1117 SkTDArray<int> shouldPurgeIdxs;
1118 int purgeableCnt = 0;
1119 SkTDArray<GrGpuResource*> resourcesToUnref;
1120
1121 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1122 // unpurgeable resources.
1123 for (int j = 0; j < kCount; ++j) {
1124 GrUniqueKey key;
1125 make_unique_key<0>(&key, j);
1126
halcanary385fe4d2015-08-26 13:07:48 -07001127 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001128 r->resourcePriv().setUniqueKey(key);
1129 if (random.nextU() % kLockedFreq) {
1130 // Make this is purgeable.
1131 r->unref();
1132 ++purgeableCnt;
1133 if (purgeableCnt <= kNumToPurge) {
1134 *shouldPurgeIdxs.append() = j;
1135 }
1136 } else {
1137 *resourcesToUnref.append() = r;
1138 }
1139 }
1140
1141 // Verify that the correct resources were purged.
1142 int currShouldPurgeIdx = 0;
1143 for (int j = 0; j < kCount; ++j) {
1144 GrUniqueKey key;
1145 make_unique_key<0>(&key, j);
1146 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1147 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1148 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1149 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001150 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001151 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001152 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001153 }
1154 SkSafeUnref(res);
1155 }
1156
1157 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1158 resourcesToUnref[j]->unref();
1159 }
1160 }
1161}
1162
bsalomon3f324322015-04-08 11:01:54 -07001163static void test_flush(skiatest::Reporter* reporter) {
1164 Mock mock(1000000, 1000000);
1165 GrContext* context = mock.context();
1166 GrResourceCache* cache = mock.cache();
1167
1168 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1169 // power of two here to keep things simpler.
1170 static const int kFlushCount = 16;
1171 cache->setLimits(1000000, 1000000, kFlushCount);
1172
1173 {
1174 // Insert a resource and send a flush notification kFlushCount times.
1175 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001176 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001177 GrUniqueKey k;
1178 make_unique_key<1>(&k, i);
1179 r->resourcePriv().setUniqueKey(k);
1180 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001181 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001182 }
1183
1184 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001185 for (int i = 0; i < kFlushCount; ++i) {
1186 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001187 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1188 for (int j = 0; j < i; ++j) {
1189 GrUniqueKey k;
1190 make_unique_key<1>(&k, j);
1191 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1192 REPORTER_ASSERT(reporter, !SkToBool(r));
1193 SkSafeUnref(r);
1194 }
bsalomon3f324322015-04-08 11:01:54 -07001195 }
1196
1197 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1198 cache->purgeAllUnlocked();
1199 }
1200
1201 // Do a similar test but where we leave refs on some resources to prevent them from being
1202 // purged.
1203 {
1204 GrGpuResource* refedResources[kFlushCount >> 1];
1205 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001206 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001207 GrUniqueKey k;
1208 make_unique_key<1>(&k, i);
1209 r->resourcePriv().setUniqueKey(k);
1210 // Leave a ref on every other resource, beginning with the first.
1211 if (SkToBool(i & 0x1)) {
1212 refedResources[i/2] = r;
1213 } else {
1214 r->unref();
1215 }
bsalomonb77a9072016-09-07 10:02:04 -07001216 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001217 }
1218
1219 for (int i = 0; i < kFlushCount; ++i) {
1220 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001221 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001222 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001223 }
1224
1225 // Unref all the resources that we kept refs on in the first loop.
1226 for (int i = 0; i < kFlushCount >> 1; ++i) {
1227 refedResources[i]->unref();
1228 }
1229
bsalomone2e87f32016-09-22 12:42:11 -07001230 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1231 // kFlushCount full flushes.
1232 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001233 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001234 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001235 }
1236 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1237
1238 cache->purgeAllUnlocked();
1239 }
1240
1241 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001242
1243 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1244 // eviction.
1245 context->flush();
1246 for (int i = 0; i < 10; ++i) {
1247 TestResource* r = new TestResource(context->getGpu());
1248 GrUniqueKey k;
1249 make_unique_key<1>(&k, i);
1250 r->resourcePriv().setUniqueKey(k);
1251 r->unref();
1252 }
1253 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1254 for (int i = 0; i < 10 * kFlushCount; ++i) {
1255 context->flush();
1256 }
1257 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001258}
1259
Brian Salomon5e150852017-03-22 14:53:13 -04001260static void test_time_purge(skiatest::Reporter* reporter) {
1261 Mock mock(1000000, 1000000);
1262 GrContext* context = mock.context();
1263 GrResourceCache* cache = mock.cache();
1264
1265 static constexpr int kCnts[] = {1, 10, 1024};
1266 auto nowish = []() {
1267 // We sleep so that we ensure we get a value that is greater than the last call to
1268 // GrStdSteadyClock::now().
1269 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1270 auto result = GrStdSteadyClock::now();
1271 // Also sleep afterwards so we don't get this value again.
1272 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1273 return result;
1274 };
1275
1276 for (int cnt : kCnts) {
1277 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1278 new GrStdSteadyClock::time_point[cnt]);
1279 {
1280 // Insert resources and get time points between each addition.
1281 for (int i = 0; i < cnt; ++i) {
1282 TestResource* r = new TestResource(context->getGpu());
1283 GrUniqueKey k;
1284 make_unique_key<1>(&k, i);
1285 r->resourcePriv().setUniqueKey(k);
1286 r->unref();
1287 timeStamps.get()[i] = nowish();
1288 }
1289
1290 // Purge based on the time points between resource additions. Each purge should remove
1291 // the oldest resource.
1292 for (int i = 0; i < cnt; ++i) {
1293 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1294 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1295 for (int j = 0; j < i; ++j) {
1296 GrUniqueKey k;
1297 make_unique_key<1>(&k, j);
1298 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1299 REPORTER_ASSERT(reporter, !SkToBool(r));
1300 SkSafeUnref(r);
1301 }
1302 }
1303
1304 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1305 cache->purgeAllUnlocked();
1306 }
1307
1308 // Do a similar test but where we leave refs on some resources to prevent them from being
1309 // purged.
1310 {
1311 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1312 for (int i = 0; i < cnt; ++i) {
1313 TestResource* r = new TestResource(context->getGpu());
1314 GrUniqueKey k;
1315 make_unique_key<1>(&k, i);
1316 r->resourcePriv().setUniqueKey(k);
1317 // Leave a ref on every other resource, beginning with the first.
1318 if (SkToBool(i & 0x1)) {
1319 refedResources.get()[i / 2] = r;
1320 } else {
1321 r->unref();
1322 }
1323 timeStamps.get()[i] = nowish();
1324 }
1325
1326 for (int i = 0; i < cnt; ++i) {
1327 // Should get a resource purged every other frame.
1328 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1329 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1330 }
1331
1332 // Unref all the resources that we kept refs on in the first loop.
1333 for (int i = 0; i < (cnt / 2); ++i) {
1334 refedResources.get()[i]->unref();
1335 cache->purgeResourcesNotUsedSince(nowish());
1336 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1337 }
1338
1339 cache->purgeAllUnlocked();
1340 }
1341
1342 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1343
1344 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1345 // eviction
1346 context->flush();
1347 for (int i = 0; i < 10; ++i) {
1348 TestResource* r = new TestResource(context->getGpu());
1349 GrUniqueKey k;
1350 make_unique_key<1>(&k, i);
1351 r->resourcePriv().setUniqueKey(k);
1352 r->unref();
1353 }
1354 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1355 context->flush();
1356 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1357 cache->purgeResourcesNotUsedSince(nowish());
1358 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1359 }
1360}
1361
Derek Sollenberger5480a182017-05-25 16:43:59 -04001362static void test_partial_purge(skiatest::Reporter* reporter) {
1363 Mock mock(6, 100);
1364 GrContext* context = mock.context();
1365 GrResourceCache* cache = mock.cache();
1366
1367 enum TestsCase {
1368 kOnlyScratch_TestCase = 0,
1369 kPartialScratch_TestCase = 1,
1370 kAllScratch_TestCase = 2,
1371 kPartial_TestCase = 3,
1372 kAll_TestCase = 4,
1373 kNone_TestCase = 5,
1374 kEndTests_TestCase = kNone_TestCase + 1
1375 };
1376
1377 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1378
1379 GrUniqueKey key1, key2, key3;
1380 make_unique_key<0>(&key1, 1);
1381 make_unique_key<0>(&key2, 2);
1382 make_unique_key<0>(&key3, 3);
1383
1384 // Add three unique resources to the cache.
1385 TestResource *unique1 = new TestResource(context->getGpu());
1386 TestResource *unique2 = new TestResource(context->getGpu());
1387 TestResource *unique3 = new TestResource(context->getGpu());
1388
1389 unique1->resourcePriv().setUniqueKey(key1);
1390 unique2->resourcePriv().setUniqueKey(key2);
1391 unique3->resourcePriv().setUniqueKey(key3);
1392
1393 unique1->setSize(10);
1394 unique2->setSize(11);
1395 unique3->setSize(12);
1396
1397 // Add two scratch resources to the cache.
1398 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1399 TestResource::kA_SimulatedProperty);
1400 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1401 TestResource::kB_SimulatedProperty);
1402 scratch1->setSize(13);
1403 scratch2->setSize(14);
1404
1405
1406 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1407 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1408 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1409
1410 // Add resources to the purgeable queue
1411 unique1->unref();
1412 scratch1->unref();
1413 unique2->unref();
1414 scratch2->unref();
1415 unique3->unref();
1416
1417 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1418 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1419 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1420
1421 switch(testCase) {
1422 case kOnlyScratch_TestCase: {
1423 context->purgeUnlockedResources(14, true);
1424 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1425 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1426 break;
1427 }
1428 case kPartialScratch_TestCase: {
1429 context->purgeUnlockedResources(3, true);
1430 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1431 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1432 break;
1433 }
1434 case kAllScratch_TestCase: {
1435 context->purgeUnlockedResources(50, true);
1436 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1437 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1438 break;
1439 }
1440 case kPartial_TestCase: {
1441 context->purgeUnlockedResources(13, false);
1442 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1443 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1444 break;
1445 }
1446 case kAll_TestCase: {
1447 context->purgeUnlockedResources(50, false);
1448 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1449 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1450 break;
1451 }
1452 case kNone_TestCase: {
1453 context->purgeUnlockedResources(0, true);
1454 context->purgeUnlockedResources(0, false);
1455 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1456 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1457 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1458 break;
1459 }
1460 };
1461
1462 // ensure all are purged before the next
1463 context->purgeAllUnlockedResources();
1464 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1465 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1466
1467 }
1468}
1469
bsalomon10e23ca2014-11-25 05:52:06 -08001470static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001471 // Set the cache size to double the resource count because we're going to create 2x that number
1472 // resources, using two different key domains. Add a little slop to the bytes because we resize
1473 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001474 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001475
bsalomonc2f35b72015-01-23 07:19:22 -08001476 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1477 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001478 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001479
1480 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001481 GrUniqueKey key1, key2;
1482 make_unique_key<1>(&key1, i);
1483 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001484
bsalomon24db3b12015-01-23 04:24:04 -08001485 TestResource* resource;
1486
halcanary385fe4d2015-08-26 13:07:48 -07001487 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001488 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001489 resource->setSize(1);
1490 resource->unref();
1491
halcanary385fe4d2015-08-26 13:07:48 -07001492 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001493 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001494 resource->setSize(1);
1495 resource->unref();
1496 }
1497
1498 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001499 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001500 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1501 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1502 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1503 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001504 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001505 GrUniqueKey key1, key2;
1506 make_unique_key<1>(&key1, i);
1507 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001508
bsalomon8718aaf2015-02-19 07:24:21 -08001509 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1510 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001511 }
1512
bsalomon0ea80f42015-02-11 10:49:59 -08001513 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001514 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001515 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001516 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1517 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1518 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1519 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001520
1521 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001522 GrUniqueKey key1, key2;
1523 make_unique_key<1>(&key1, i);
1524 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001525
bsalomon8718aaf2015-02-19 07:24:21 -08001526 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1527 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001528 }
1529}
1530
senorblanco84cd6212015-08-04 10:01:58 -07001531static void test_custom_data(skiatest::Reporter* reporter) {
1532 GrUniqueKey key1, key2;
1533 make_unique_key<0>(&key1, 1);
1534 make_unique_key<0>(&key2, 2);
1535 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001536 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001537 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1538 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1539
1540 // Test that copying a key also takes a ref on its custom data.
1541 GrUniqueKey key3 = key1;
1542 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1543}
1544
bsalomonc6363ef2015-09-24 07:07:40 -07001545static void test_abandoned(skiatest::Reporter* reporter) {
1546 Mock mock(10, 300);
1547 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001548 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001549 context->abandonContext();
1550
1551 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1552
1553 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1554
robertphillips8abb3702016-08-31 14:04:06 -07001555 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001556 resource->getUniqueKey();
1557 resource->wasDestroyed();
1558 resource->gpuMemorySize();
1559 resource->getContext();
1560
1561 resource->abandon();
1562 resource->resourcePriv().getScratchKey();
1563 resource->resourcePriv().isBudgeted();
1564 resource->resourcePriv().makeBudgeted();
1565 resource->resourcePriv().makeUnbudgeted();
1566 resource->resourcePriv().removeScratchKey();
1567 GrUniqueKey key;
1568 make_unique_key<0>(&key, 1);
1569 resource->resourcePriv().setUniqueKey(key);
1570 resource->resourcePriv().removeUniqueKey();
1571}
1572
Brian Salomon1090da62017-01-06 12:04:19 -05001573static void test_tags(skiatest::Reporter* reporter) {
1574#ifdef SK_DEBUG
1575 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1576 static constexpr int kLastTagIdx = 10;
1577 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1578
1579 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1580 GrContext* context = mock.context();
1581 GrResourceCache* cache = mock.cache();
1582
1583 SkString tagStr;
1584 int tagIdx = 0;
1585 int currTagCnt = 0;
1586
1587 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1588 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1589 GrUniqueKey key;
1590 if (currTagCnt == tagIdx) {
1591 tagIdx += 1;
1592 currTagCnt = 0;
1593 tagStr.printf("tag%d", tagIdx);
1594 }
1595 make_unique_key<1>(&key, i, tagStr.c_str());
1596 resource->resourcePriv().setUniqueKey(key);
1597 }
1598 SkASSERT(kLastTagIdx == tagIdx);
1599 SkASSERT(currTagCnt == kLastTagIdx);
1600
1601 // Test i = 0 to exercise unused tag string.
1602 for (int i = 0; i <= kLastTagIdx; ++i) {
1603 tagStr.printf("tag%d", i);
1604 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1605 }
1606#endif
1607}
1608
kkinnunen15302832015-12-01 04:35:26 -08001609DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001610 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001611 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001612 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001613 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001614 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001615 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001616 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001617 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001618 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001619 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001620 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001621 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001622 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001623 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001624 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001625 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001626 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001627 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001628 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001629 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001630}
1631
Robert Phillipsd6214d42016-11-07 08:23:48 -05001632////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001633static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001634 GrSurfaceFlags flags,
1635 int width, int height,
1636 int sampleCnt) {
1637 GrSurfaceDesc desc;
1638 desc.fFlags = flags;
1639 desc.fWidth = width;
1640 desc.fHeight = height;
1641 desc.fConfig = kRGBA_8888_GrPixelConfig;
1642 desc.fSampleCnt = sampleCnt;
1643
Robert Phillipse78b7252017-04-06 07:59:41 -04001644 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001645}
1646
Robert Phillipse78b7252017-04-06 07:59:41 -04001647static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1648 GrSurfaceFlags flags,
1649 int width, int height,
1650 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001651 SkBitmap bm;
1652
1653 bm.allocN32Pixels(width, height, true);
1654 bm.eraseColor(SK_ColorBLUE);
1655
Brian Osman7b8400d2016-11-08 17:08:54 -05001656 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001657 SkASSERT(mipmaps);
1658 SkASSERT(mipmaps->countLevels() > 1);
1659
1660 int mipLevelCount = mipmaps->countLevels() + 1;
1661
1662 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1663
1664 texels[0].fPixels = bm.getPixels();
1665 texels[0].fRowBytes = bm.rowBytes();
1666
1667 for (int i = 1; i < mipLevelCount; ++i) {
1668 SkMipMap::Level generatedMipLevel;
1669 mipmaps->getLevel(i - 1, &generatedMipLevel);
1670 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1671 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1672 }
1673
1674 GrSurfaceDesc desc;
1675 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001676 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1677 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001678 desc.fWidth = width;
1679 desc.fHeight = height;
1680 desc.fConfig = kRGBA_8888_GrPixelConfig;
1681 desc.fSampleCnt = sampleCnt;
1682 desc.fIsMipMapped = true;
1683
Robert Phillips8e8c7552017-07-10 12:06:05 -04001684 return GrSurfaceProxy::MakeDeferredMipMap(provider, desc, SkBudgeted::kYes,
1685 texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001686}
1687
1688// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1689// Texture-only, both-RT-and-Texture and MIPmapped
1690DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1691 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001692 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001693
Robert Phillipsd6214d42016-11-07 08:23:48 -05001694 static const int kSize = 64;
1695
Robert Phillipsd6214d42016-11-07 08:23:48 -05001696 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001697 {
1698 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001699
Robert Phillipse78b7252017-04-06 07:59:41 -04001700 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1701 size_t size = tex->gpuMemorySize();
1702 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1703
Greg Daniel81e7bf82017-07-19 14:47:42 -04001704 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1705 if (sampleCount >= 4) {
1706 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1707 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001708 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001709 REPORTER_ASSERT(reporter,
1710 kSize*kSize*4 == size || // msaa4 failed
1711 kSize*kSize*4*sampleCount == size || // auto-resolving
1712 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001713 }
1714
1715 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001716 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001717 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001718 }
1719
Robert Phillipsd6214d42016-11-07 08:23:48 -05001720
1721 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001722 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001723 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001724
Robert Phillipse78b7252017-04-06 07:59:41 -04001725 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1726 size_t size = proxy->gpuMemorySize();
1727 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1728
Greg Daniel81e7bf82017-07-19 14:47:42 -04001729 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1730 if (sampleCount >= 4) {
1731 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1732 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001733 size = proxy->gpuMemorySize();
1734 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001735 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1736 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1737 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001738 }
Robert Phillips1b352562017-04-05 18:56:21 +00001739
Robert Phillipse78b7252017-04-06 07:59:41 -04001740 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1741 size = proxy->gpuMemorySize();
1742 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1743 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001744}
1745
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001746#endif