blob: 08f60ea1c5e449413806532f5f0064fabb45e5e9 [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"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTarget.h"
20#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070023#include "GrTest.h"
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 Reed12e946b2017-04-17 10:53:29 -040070 canvas->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
bsalomon76228632015-05-29 08:02:10 -0700153 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700154 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800155 GrSurfaceDesc smallMSAADesc = smallDesc;
156 smallMSAADesc.fSampleCnt = 4;
Brian Osman32342f02017-03-04 08:12:46 -0500157 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
158 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800159 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700160 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800161 }
bsalomonb602d4d2015-02-19 12:05:58 -0800162#ifdef SK_BUILD_FOR_ANDROID
163 if (!smallMSAART0) {
164 // The nexus player seems to fail to create MSAA textures.
165 return;
166 }
167#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800168 REPORTER_ASSERT(reporter,
169 smallRT0 && smallMSAART0 &&
170 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700171 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
172 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800173 // A second MSAA RT should share with the first MSAA RT.
Brian Osman32342f02017-03-04 08:12:46 -0500174 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
175 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800176 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700177 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800178 }
179 REPORTER_ASSERT(reporter,
180 smallMSAART0 && smallMSAART1 &&
181 smallMSAART0->asRenderTarget() &&
182 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700183 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
184 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800185 // But not one with a larger sample count should not. (Also check that the request for 4
186 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700187 if (context->caps()->maxSampleCount() >= 8 &&
188 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700189 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800190 smallMSAADesc.fSampleCnt = 8;
Robert Phillipse78b7252017-04-06 07:59:41 -0400191 smallMSAART1 = resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo);
Hal Canary342b7ac2016-11-04 11:49:42 -0400192 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500193 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800194 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700195 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800196 }
197 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700198 smallMSAART0 && smallMSAART1 &&
199 smallMSAART0->asRenderTarget() &&
200 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700201 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
202 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800203 }
204 }
205}
206
bsalomon68d91342016-04-12 09:59:58 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700208 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800209 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700210 // this test is only valid for GL
211 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 return;
213 }
214
Brian Osman85d34b22017-05-10 12:06:26 -0400215 GrBackendObject texHandles[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216 static const int kW = 100;
217 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700218
bsalomon091f60c2015-11-10 11:54:56 -0800219 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
220 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700221
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222 context->resetContext();
223
Greg Daniel7ef28f32017-04-20 16:41:55 +0000224 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
225 kW,
226 kH,
227 kRGBA_8888_GrPixelConfig,
228 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500229 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000230 backendTex1, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
231 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(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000239 backendTex2, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
240 kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241
Brian Osman85d34b22017-05-10 12:06:26 -0400242 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
243 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700244 return;
245 }
246
halcanary96fcdcc2015-08-27 07:41:13 -0700247 borrowed.reset(nullptr);
248 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700249
250 context->flush();
251
bsalomon091f60c2015-11-10 11:54:56 -0800252 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
253 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700254
255 REPORTER_ASSERT(reporter, borrowedIsAlive);
256 REPORTER_ASSERT(reporter, !adoptedIsAlive);
257
bsalomon67d76202015-11-11 12:40:42 -0800258 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
259 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700260
261 context->resetContext();
262}
263
bsalomon6d3fe022014-07-25 08:35:45 -0700264class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800265 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000266public:
robertphillips6e83ac72015-08-13 05:19:14 -0700267 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700268
bsalomon1c60dfe2015-01-21 09:32:40 -0800269 /** Property that distinctly categorizes the resource.
270 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800271 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800272
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
274 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700275 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800276 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700277 , fProperty(kA_SimulatedProperty)
278 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800279 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700280 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800281 }
282
kkinnunen2e6055b2016-04-22 01:48:29 -0700283 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
284 SimulatedProperty property) {
285 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800286 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700287 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
288 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000289 }
290
Brian Salomond3b65972017-03-22 12:05:03 -0400291 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800292 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800293 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000294 }
295
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000296 void setSize(size_t size) {
297 fSize = size;
298 this->didChangeGpuMemorySize();
299 }
300
bsalomon33435572014-11-05 14:47:41 -0800301 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000302
bsalomon71cb0c22014-11-14 12:10:14 -0800303 void setUnrefWhenDestroyed(TestResource* resource) {
304 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000305 }
306
bsalomon1c60dfe2015-01-21 09:32:40 -0800307 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
308 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
309 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800310 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
311 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800312 }
313 }
314
315 static size_t ExpectedScratchKeySize() {
316 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
317 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000318private:
bsalomon24db3b12015-01-23 04:24:04 -0800319 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800320
kkinnunen2e6055b2016-04-22 01:48:29 -0700321 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
322 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700323 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800324 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700325 , fProperty(property)
326 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800327 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700328 this->registerWithCache(budgeted);
329 }
330
331 // Constructor for simulating resources that wrap backend objects.
332 TestResource(GrGpu* gpu, size_t size)
333 : INHERITED(gpu)
334 , fToDelete(nullptr)
335 , fSize(size)
336 , fProperty(kA_SimulatedProperty)
337 , fIsScratch(false) {
338 ++fNumAlive;
339 this->registerWithCacheWrapped();
340 }
341
342 void computeScratchKey(GrScratchKey* key) const override {
343 if (fIsScratch) {
344 ComputeScratchKey(fProperty, key);
345 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800346 }
347
mtklein36352bf2015-03-25 18:17:31 -0700348 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800349
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000350 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000351 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800352 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800353 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700354 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700355 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000356};
bsalomon33435572014-11-05 14:47:41 -0800357int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000358
bsalomonc2f35b72015-01-23 07:19:22 -0800359class Mock {
360public:
361 Mock(int maxCnt, size_t maxBytes) {
362 fContext.reset(GrContext::CreateMockContext());
363 SkASSERT(fContext);
364 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800365 GrResourceCache* cache = fContext->getResourceCache();
366 cache->purgeAllUnlocked();
367 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800368 }
bsalomonc2f35b72015-01-23 07:19:22 -0800369
bsalomon0ea80f42015-02-11 10:49:59 -0800370 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800371
Hal Canary342b7ac2016-11-04 11:49:42 -0400372 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800373
374private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400375 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800376};
377
378static void test_no_key(skiatest::Reporter* reporter) {
379 Mock mock(10, 30000);
380 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800381 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700384 TestResource* a = new TestResource(context->getGpu());
385 TestResource* b = new TestResource(context->getGpu());
386 TestResource* c = new TestResource(context->getGpu());
387 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800388 a->setSize(11);
389 b->setSize(12);
390 c->setSize(13);
391 d->setSize(14);
392
393 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800394 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800395 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800396 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800397
398 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800400 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
401
bsalomon8718aaf2015-02-19 07:24:21 -0800402 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800403
404 a->unref();
405 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800406 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800407 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800408 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800409
410 c->unref();
411 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800413 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800414 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415
416 d->unref();
417 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800418 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
419 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800420
421 b->unref();
422 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800423 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
424 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800425}
426
bsalomon24db3b12015-01-23 04:24:04 -0800427// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500428template <int>
429static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800430 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500431 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800432 builder[0] = data;
433}
434
bsalomon84c8e622014-11-17 09:33:27 -0800435static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800436 Mock mock(10, 300);
437 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800438 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800439
bsalomon8718aaf2015-02-19 07:24:21 -0800440 GrUniqueKey uniqueKey;
441 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800442
bsalomon8718aaf2015-02-19 07:24:21 -0800443 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800444 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700445 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800446 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700447 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800448 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800449 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700450 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800451 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700452 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700453 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800454 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800455
Brian Osman0562eb92017-05-08 11:16:39 -0400456 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800457 GrUniqueKey uniqueKey2;
458 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800459 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400460 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
461 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
462
463 // Remove the extra ref we just added.
464 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800465
466 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800467 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800468 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800469 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800472 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400474 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800475
bsalomon63c992f2015-01-23 12:47:59 -0800476 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800477 cache->purgeAllUnlocked();
478 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800479 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800480 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800481 cache->getResourceBytes());
482 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800483 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800484 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400485 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800486
487 // Unreffing the wrapped resource should free it right away.
488 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800489 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800490 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800491 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400492 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800493
bsalomon84c8e622014-11-17 09:33:27 -0800494 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700495 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800496 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800497 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400498 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800499 cache->purgeAllUnlocked();
500 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800501 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800502 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
503 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
504 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400505 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800506
507 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400508 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800509 cache->purgeAllUnlocked();
510 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800511 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800512 cache->getResourceBytes());
513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400515 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800516
517 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800518 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
519 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
520 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
521 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400522 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800523
524 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800525 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
526 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
527 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
528 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400529 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800530}
531
bsalomon5236cf42015-01-14 10:42:08 -0800532static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800533 Mock mock(10, 30000);
534 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800535 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800536
bsalomon8718aaf2015-02-19 07:24:21 -0800537 GrUniqueKey uniqueKey;
538 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800539
540 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800541 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800542 TestResource* wrapped;
543 TestResource* unbudgeted;
544
545 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700546 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
547 TestResource::kB_SimulatedProperty);
548
bsalomon5236cf42015-01-14 10:42:08 -0800549 scratch->setSize(10);
550 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800551 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
552 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
553 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
554 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400555 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800556
halcanary385fe4d2015-08-26 13:07:48 -0700557 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800558 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800559 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800560 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800561 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
562 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
563 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
564 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400565 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800566
bsalomon0ea80f42015-02-11 10:49:59 -0800567 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700568 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
570 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
571 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
572 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400573 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800574
575 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400580 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800581
kkinnunen2e6055b2016-04-22 01:48:29 -0700582 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800583 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
584 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
585 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
586 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400587 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800588
589 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800590 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
591 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
592 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
593 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400594 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800595
bsalomon0ea80f42015-02-11 10:49:59 -0800596 cache->purgeAllUnlocked();
597 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
598 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
599 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
600 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400601 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800602}
603
bsalomon3582d3e2015-02-13 14:20:05 -0800604// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
605void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
606/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800607 Mock mock(10, 300);
608 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800609 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800610
611 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700612 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
613 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800614 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800615 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800616
617 size_t size = resource->gpuMemorySize();
618 for (int i = 0; i < 2; ++i) {
619 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800620 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800621 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800622 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700623 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800624 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
625 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
626 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
627 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400628 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800629
630 // Once it is unrefed, it should become available as scratch.
631 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800632 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
633 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
634 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
635 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400636 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700637 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800638 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800639 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800640 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800641 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800642
643 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700644 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800645 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800646 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800647 } else {
648 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800649 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800650 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
651 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
652 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
653 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400654 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800655 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800656 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800657 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800658
659 // now when it is unrefed it should die since it has no key.
660 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800661 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
662 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
663 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
664 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400665 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800666 }
bsalomon8b79d232014-11-10 10:19:06 -0800667 }
bsalomonc2f35b72015-01-23 07:19:22 -0800668}
669
670static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
671 Mock mock(5, 30000);
672 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800673 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800674
bsalomon8b79d232014-11-10 10:19:06 -0800675 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700677 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800678 TestResource::kB_SimulatedProperty);
679 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700680 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800681 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800682 a->setSize(11);
683 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800684 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800685 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800686 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700687 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800688
689 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800690 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800691
bsalomon0ea80f42015-02-11 10:49:59 -0800692 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800694 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
695 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800696 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800697 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800698
bsalomon63c992f2015-01-23 12:47:59 -0800699 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800700 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800701 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800702 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800703
704 // Unref but don't purge
705 a->unref();
706 b->unref();
707 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800708 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800709
bsalomon63c992f2015-01-23 12:47:59 -0800710 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800711 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800712 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800713 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
714 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800715}
716
bsalomon10e23ca2014-11-25 05:52:06 -0800717static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800718 Mock mock(5, 30000);
719 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800720 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800721
bsalomon10e23ca2014-11-25 05:52:06 -0800722 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700723 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800724 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700725 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800726 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800727 a->unref();
728 b->unref();
729
bsalomon1c60dfe2015-01-21 09:32:40 -0800730 GrScratchKey scratchKey;
731 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800732 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800733 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700734 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800735
bsalomon0ea80f42015-02-11 10:49:59 -0800736 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800737 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800738 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800739 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
740 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800741
742 // Find the first resource and remove its scratch key
743 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700744 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800745 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800746 // It's still alive, but not cached by scratch key anymore
747 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800748 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
749 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800750
751 // The cache should immediately delete it when it's unrefed since it isn't accessible.
752 find->unref();
753 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800754 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
755 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800756
757 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700758 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800759 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800760 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800761 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
762 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800763
764 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800765 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800766 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800767 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
768 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800769
770 find->unref();
771 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800772 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
773 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800774}
775
bsalomon1c60dfe2015-01-21 09:32:40 -0800776static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800777 Mock mock(5, 30000);
778 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800779 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800780
781 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700782 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800783 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700784 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800785 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800786 a->unref();
787 b->unref();
788
789 GrScratchKey scratchKey;
790 // Ensure that scratch key comparison and assignment is consistent.
791 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800793 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800794 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800795 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
796 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
797 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
798 scratchKey = scratchKey1;
799 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
800 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
801 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
802 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
803 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
804 scratchKey = scratchKey2;
805 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
806 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
807 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
808 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
809 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
810
811 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800812 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800813 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700814 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800815
816 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800817 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700818 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700819 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800820 find->unref();
821
822 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700823 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700824 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800825 REPORTER_ASSERT(reporter, find == a || find == b);
826
robertphillips6e83ac72015-08-13 05:19:14 -0700827 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700828 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800829 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
830 REPORTER_ASSERT(reporter, find2 != find);
831 find2->unref();
832 find->unref();
833}
834
bsalomon8718aaf2015-02-19 07:24:21 -0800835static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800836 Mock mock(5, 30000);
837 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800838 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000839
bsalomon8718aaf2015-02-19 07:24:21 -0800840 GrUniqueKey key;
841 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700842
bsalomon8718aaf2015-02-19 07:24:21 -0800843 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700844 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800845 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700846
bsalomonf99e9612015-02-19 08:24:16 -0800847 // Set key on resource a.
848 a->resourcePriv().setUniqueKey(key);
849 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
850 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800851
bsalomonf99e9612015-02-19 08:24:16 -0800852 // Make sure that redundantly setting a's key works.
853 a->resourcePriv().setUniqueKey(key);
854 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800855 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800856 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
857 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800858 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
859
bsalomonf99e9612015-02-19 08:24:16 -0800860 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700861 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800862 b->setSize(12);
863 b->resourcePriv().setUniqueKey(key);
864 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
865 b->unref();
866
867 // Still have two resources because a is still reffed.
868 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
869 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
870 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
871
872 a->unref();
873 // Now a should be gone.
874 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
875 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
876 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
877
878 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
879 // Also make b be unreffed when replacement occurs.
880 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700881 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800882 GrUniqueKey differentKey;
883 make_unique_key<0>(&differentKey, 1);
884 c->setSize(13);
885 c->resourcePriv().setUniqueKey(differentKey);
886 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
887 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
888 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
889 // c replaces b and b should be immediately purged.
890 c->resourcePriv().setUniqueKey(key);
891 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
892 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
893 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
894
895 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800896 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800897 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
898 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
899 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
900
901 // Drop the ref on c, it should be kept alive because it has a unique key.
902 c->unref();
903 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
904 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
905 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
906
907 // Verify that we can find c, then remove its unique key. It should get purged immediately.
908 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
909 c->resourcePriv().removeUniqueKey();
910 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800911 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
912 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800913 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700914
915 {
916 GrUniqueKey key2;
917 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400918 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700919 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700920 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700921 d->resourcePriv().setUniqueKey(key2);
922 }
923
924 GrUniqueKey key3;
925 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400926 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700927 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000928}
929
bsalomon8b79d232014-11-10 10:19:06 -0800930static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800931 Mock mock(5, 30000);
932 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800933 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800934
bsalomon8718aaf2015-02-19 07:24:21 -0800935 GrUniqueKey key1, key2, key3;
936 make_unique_key<0>(&key1, 1);
937 make_unique_key<0>(&key2, 2);
938 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700939
bsalomon23e619c2015-02-06 11:54:28 -0800940 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700941 TestResource* a = new TestResource(context->getGpu());
942 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700943 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800944 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800945 a->resourcePriv().setUniqueKey(key1);
946 b->resourcePriv().setUniqueKey(key2);
947 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800948 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800949 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800950 c->unref();
951
bsalomon8718aaf2015-02-19 07:24:21 -0800952 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
953 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
954 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800955 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800956
bsalomon8718aaf2015-02-19 07:24:21 -0800957 typedef GrUniqueKeyInvalidatedMessage Msg;
958 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800959
960 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
961 Bus::Post(Msg(key1));
962 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800963 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800964 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800965 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
966 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800967 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800968 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800969
970 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800971 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800972 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800973 // we still have a ref on b, c should be recycled as scratch.
974 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800975 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800976
bsalomon23e619c2015-02-06 11:54:28 -0800977 // make b purgeable. It should be immediately deleted since it has no key.
978 b->unref();
979 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
980
981 // Make sure we actually get to c via it's scratch key, before we say goodbye.
982 GrScratchKey scratchKey;
983 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700984 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800985 REPORTER_ASSERT(reporter, scratch == c);
986 SkSafeUnref(scratch);
987
988 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800989 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700990 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800991 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800992 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
993 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800994 REPORTER_ASSERT(reporter, !scratch);
995 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800996}
997
bsalomon71cb0c22014-11-14 12:10:14 -0800998static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800999 Mock mock(3, 30000);
1000 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001001 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -08001002
bsalomon8718aaf2015-02-19 07:24:21 -08001003 GrUniqueKey key1, key2;
1004 make_unique_key<0>(&key1, 1);
1005 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001006
halcanary385fe4d2015-08-26 13:07:48 -07001007 TestResource* a = new TestResource(context->getGpu());
1008 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001009 a->resourcePriv().setUniqueKey(key1);
1010 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001011
bsalomonc2f35b72015-01-23 07:19:22 -08001012 // Make a cycle
1013 a->setUnrefWhenDestroyed(b);
1014 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001015
bsalomonc2f35b72015-01-23 07:19:22 -08001016 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001017
bsalomonc2f35b72015-01-23 07:19:22 -08001018 a->unref();
1019 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001020
bsalomonc2f35b72015-01-23 07:19:22 -08001021 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001022
bsalomon0ea80f42015-02-11 10:49:59 -08001023 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001024 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001025
bsalomonc2f35b72015-01-23 07:19:22 -08001026 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001027 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001028 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001029
bsalomon0ea80f42015-02-11 10:49:59 -08001030 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001031 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001032}
1033
bsalomon8b79d232014-11-10 10:19:06 -08001034static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001035 GrUniqueKey key1, key2;
1036 make_unique_key<0>(&key1, 1);
1037 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001038
1039 // Test changing resources sizes (both increase & decrease).
1040 {
bsalomonc2f35b72015-01-23 07:19:22 -08001041 Mock mock(3, 30000);
1042 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001043 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001044
halcanary385fe4d2015-08-26 13:07:48 -07001045 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001046 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001047 a->unref();
1048
halcanary385fe4d2015-08-26 13:07:48 -07001049 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001050 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001051 b->unref();
1052
bsalomon0ea80f42015-02-11 10:49:59 -08001053 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1054 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001055 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001056 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001057 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001058 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001059 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001060 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001061 find1->setSize(50);
1062 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001063
bsalomon0ea80f42015-02-11 10:49:59 -08001064 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1065 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001066 }
1067
1068 // Test increasing a resources size beyond the cache budget.
1069 {
bsalomonc2f35b72015-01-23 07:19:22 -08001070 Mock mock(2, 300);
1071 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001072 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001073
halcanary385fe4d2015-08-26 13:07:48 -07001074 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001075 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001076 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001077 a->unref();
1078
halcanary385fe4d2015-08-26 13:07:48 -07001079 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001080 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001081 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001082 b->unref();
1083
bsalomon0ea80f42015-02-11 10:49:59 -08001084 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1085 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001086
bsalomon8b79d232014-11-10 10:19:06 -08001087 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001088 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001089 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001090 find2->setSize(201);
1091 }
bsalomon8718aaf2015-02-19 07:24:21 -08001092 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001093
bsalomon0ea80f42015-02-11 10:49:59 -08001094 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1095 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001096 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001097}
1098
bsalomonddf30e62015-02-19 11:38:44 -08001099static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1100 static const int kCount = 50;
1101 static const int kBudgetCnt = kCount / 2;
1102 static const int kLockedFreq = 8;
1103 static const int kBudgetSize = 0x80000000;
1104
1105 SkRandom random;
1106
1107 // Run the test 2*kCount times;
1108 for (int i = 0; i < 2 * kCount; ++i ) {
1109 Mock mock(kBudgetCnt, kBudgetSize);
1110 GrContext* context = mock.context();
1111 GrResourceCache* cache = mock.cache();
1112
1113 // Pick a random number of resources to add before the timestamp will wrap.
1114 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1115
1116 static const int kNumToPurge = kCount - kBudgetCnt;
1117
1118 SkTDArray<int> shouldPurgeIdxs;
1119 int purgeableCnt = 0;
1120 SkTDArray<GrGpuResource*> resourcesToUnref;
1121
1122 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1123 // unpurgeable resources.
1124 for (int j = 0; j < kCount; ++j) {
1125 GrUniqueKey key;
1126 make_unique_key<0>(&key, j);
1127
halcanary385fe4d2015-08-26 13:07:48 -07001128 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001129 r->resourcePriv().setUniqueKey(key);
1130 if (random.nextU() % kLockedFreq) {
1131 // Make this is purgeable.
1132 r->unref();
1133 ++purgeableCnt;
1134 if (purgeableCnt <= kNumToPurge) {
1135 *shouldPurgeIdxs.append() = j;
1136 }
1137 } else {
1138 *resourcesToUnref.append() = r;
1139 }
1140 }
1141
1142 // Verify that the correct resources were purged.
1143 int currShouldPurgeIdx = 0;
1144 for (int j = 0; j < kCount; ++j) {
1145 GrUniqueKey key;
1146 make_unique_key<0>(&key, j);
1147 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1148 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1149 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1150 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001151 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001152 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001153 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001154 }
1155 SkSafeUnref(res);
1156 }
1157
1158 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1159 resourcesToUnref[j]->unref();
1160 }
1161 }
1162}
1163
bsalomon3f324322015-04-08 11:01:54 -07001164static void test_flush(skiatest::Reporter* reporter) {
1165 Mock mock(1000000, 1000000);
1166 GrContext* context = mock.context();
1167 GrResourceCache* cache = mock.cache();
1168
1169 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1170 // power of two here to keep things simpler.
1171 static const int kFlushCount = 16;
1172 cache->setLimits(1000000, 1000000, kFlushCount);
1173
1174 {
1175 // Insert a resource and send a flush notification kFlushCount times.
1176 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001177 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001178 GrUniqueKey k;
1179 make_unique_key<1>(&k, i);
1180 r->resourcePriv().setUniqueKey(k);
1181 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001182 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001183 }
1184
1185 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001186 for (int i = 0; i < kFlushCount; ++i) {
1187 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001188 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1189 for (int j = 0; j < i; ++j) {
1190 GrUniqueKey k;
1191 make_unique_key<1>(&k, j);
1192 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1193 REPORTER_ASSERT(reporter, !SkToBool(r));
1194 SkSafeUnref(r);
1195 }
bsalomon3f324322015-04-08 11:01:54 -07001196 }
1197
1198 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1199 cache->purgeAllUnlocked();
1200 }
1201
1202 // Do a similar test but where we leave refs on some resources to prevent them from being
1203 // purged.
1204 {
1205 GrGpuResource* refedResources[kFlushCount >> 1];
1206 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001207 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001208 GrUniqueKey k;
1209 make_unique_key<1>(&k, i);
1210 r->resourcePriv().setUniqueKey(k);
1211 // Leave a ref on every other resource, beginning with the first.
1212 if (SkToBool(i & 0x1)) {
1213 refedResources[i/2] = r;
1214 } else {
1215 r->unref();
1216 }
bsalomonb77a9072016-09-07 10:02:04 -07001217 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001218 }
1219
1220 for (int i = 0; i < kFlushCount; ++i) {
1221 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001222 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001223 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001224 }
1225
1226 // Unref all the resources that we kept refs on in the first loop.
1227 for (int i = 0; i < kFlushCount >> 1; ++i) {
1228 refedResources[i]->unref();
1229 }
1230
bsalomone2e87f32016-09-22 12:42:11 -07001231 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1232 // kFlushCount full flushes.
1233 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001234 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001235 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001236 }
1237 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1238
1239 cache->purgeAllUnlocked();
1240 }
1241
1242 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001243
1244 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1245 // eviction.
1246 context->flush();
1247 for (int i = 0; i < 10; ++i) {
1248 TestResource* r = new TestResource(context->getGpu());
1249 GrUniqueKey k;
1250 make_unique_key<1>(&k, i);
1251 r->resourcePriv().setUniqueKey(k);
1252 r->unref();
1253 }
1254 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1255 for (int i = 0; i < 10 * kFlushCount; ++i) {
1256 context->flush();
1257 }
1258 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001259}
1260
Brian Salomon5e150852017-03-22 14:53:13 -04001261static void test_time_purge(skiatest::Reporter* reporter) {
1262 Mock mock(1000000, 1000000);
1263 GrContext* context = mock.context();
1264 GrResourceCache* cache = mock.cache();
1265
1266 static constexpr int kCnts[] = {1, 10, 1024};
1267 auto nowish = []() {
1268 // We sleep so that we ensure we get a value that is greater than the last call to
1269 // GrStdSteadyClock::now().
1270 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1271 auto result = GrStdSteadyClock::now();
1272 // Also sleep afterwards so we don't get this value again.
1273 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1274 return result;
1275 };
1276
1277 for (int cnt : kCnts) {
1278 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1279 new GrStdSteadyClock::time_point[cnt]);
1280 {
1281 // Insert resources and get time points between each addition.
1282 for (int i = 0; i < cnt; ++i) {
1283 TestResource* r = new TestResource(context->getGpu());
1284 GrUniqueKey k;
1285 make_unique_key<1>(&k, i);
1286 r->resourcePriv().setUniqueKey(k);
1287 r->unref();
1288 timeStamps.get()[i] = nowish();
1289 }
1290
1291 // Purge based on the time points between resource additions. Each purge should remove
1292 // the oldest resource.
1293 for (int i = 0; i < cnt; ++i) {
1294 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1295 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1296 for (int j = 0; j < i; ++j) {
1297 GrUniqueKey k;
1298 make_unique_key<1>(&k, j);
1299 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1300 REPORTER_ASSERT(reporter, !SkToBool(r));
1301 SkSafeUnref(r);
1302 }
1303 }
1304
1305 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1306 cache->purgeAllUnlocked();
1307 }
1308
1309 // Do a similar test but where we leave refs on some resources to prevent them from being
1310 // purged.
1311 {
1312 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1313 for (int i = 0; i < cnt; ++i) {
1314 TestResource* r = new TestResource(context->getGpu());
1315 GrUniqueKey k;
1316 make_unique_key<1>(&k, i);
1317 r->resourcePriv().setUniqueKey(k);
1318 // Leave a ref on every other resource, beginning with the first.
1319 if (SkToBool(i & 0x1)) {
1320 refedResources.get()[i / 2] = r;
1321 } else {
1322 r->unref();
1323 }
1324 timeStamps.get()[i] = nowish();
1325 }
1326
1327 for (int i = 0; i < cnt; ++i) {
1328 // Should get a resource purged every other frame.
1329 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1330 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1331 }
1332
1333 // Unref all the resources that we kept refs on in the first loop.
1334 for (int i = 0; i < (cnt / 2); ++i) {
1335 refedResources.get()[i]->unref();
1336 cache->purgeResourcesNotUsedSince(nowish());
1337 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1338 }
1339
1340 cache->purgeAllUnlocked();
1341 }
1342
1343 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1344
1345 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1346 // eviction
1347 context->flush();
1348 for (int i = 0; i < 10; ++i) {
1349 TestResource* r = new TestResource(context->getGpu());
1350 GrUniqueKey k;
1351 make_unique_key<1>(&k, i);
1352 r->resourcePriv().setUniqueKey(k);
1353 r->unref();
1354 }
1355 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1356 context->flush();
1357 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1358 cache->purgeResourcesNotUsedSince(nowish());
1359 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1360 }
1361}
1362
Derek Sollenberger5480a182017-05-25 16:43:59 -04001363static void test_partial_purge(skiatest::Reporter* reporter) {
1364 Mock mock(6, 100);
1365 GrContext* context = mock.context();
1366 GrResourceCache* cache = mock.cache();
1367
1368 enum TestsCase {
1369 kOnlyScratch_TestCase = 0,
1370 kPartialScratch_TestCase = 1,
1371 kAllScratch_TestCase = 2,
1372 kPartial_TestCase = 3,
1373 kAll_TestCase = 4,
1374 kNone_TestCase = 5,
1375 kEndTests_TestCase = kNone_TestCase + 1
1376 };
1377
1378 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1379
1380 GrUniqueKey key1, key2, key3;
1381 make_unique_key<0>(&key1, 1);
1382 make_unique_key<0>(&key2, 2);
1383 make_unique_key<0>(&key3, 3);
1384
1385 // Add three unique resources to the cache.
1386 TestResource *unique1 = new TestResource(context->getGpu());
1387 TestResource *unique2 = new TestResource(context->getGpu());
1388 TestResource *unique3 = new TestResource(context->getGpu());
1389
1390 unique1->resourcePriv().setUniqueKey(key1);
1391 unique2->resourcePriv().setUniqueKey(key2);
1392 unique3->resourcePriv().setUniqueKey(key3);
1393
1394 unique1->setSize(10);
1395 unique2->setSize(11);
1396 unique3->setSize(12);
1397
1398 // Add two scratch resources to the cache.
1399 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1400 TestResource::kA_SimulatedProperty);
1401 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1402 TestResource::kB_SimulatedProperty);
1403 scratch1->setSize(13);
1404 scratch2->setSize(14);
1405
1406
1407 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1408 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1409 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1410
1411 // Add resources to the purgeable queue
1412 unique1->unref();
1413 scratch1->unref();
1414 unique2->unref();
1415 scratch2->unref();
1416 unique3->unref();
1417
1418 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1419 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1420 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1421
1422 switch(testCase) {
1423 case kOnlyScratch_TestCase: {
1424 context->purgeUnlockedResources(14, true);
1425 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1426 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1427 break;
1428 }
1429 case kPartialScratch_TestCase: {
1430 context->purgeUnlockedResources(3, true);
1431 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1432 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1433 break;
1434 }
1435 case kAllScratch_TestCase: {
1436 context->purgeUnlockedResources(50, true);
1437 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1438 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1439 break;
1440 }
1441 case kPartial_TestCase: {
1442 context->purgeUnlockedResources(13, false);
1443 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1444 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1445 break;
1446 }
1447 case kAll_TestCase: {
1448 context->purgeUnlockedResources(50, false);
1449 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1450 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1451 break;
1452 }
1453 case kNone_TestCase: {
1454 context->purgeUnlockedResources(0, true);
1455 context->purgeUnlockedResources(0, false);
1456 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1457 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1458 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1459 break;
1460 }
1461 };
1462
1463 // ensure all are purged before the next
1464 context->purgeAllUnlockedResources();
1465 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1466 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1467
1468 }
1469}
1470
bsalomon10e23ca2014-11-25 05:52:06 -08001471static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001472 // Set the cache size to double the resource count because we're going to create 2x that number
1473 // resources, using two different key domains. Add a little slop to the bytes because we resize
1474 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001475 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001476
bsalomonc2f35b72015-01-23 07:19:22 -08001477 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1478 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001479 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001480
1481 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001482 GrUniqueKey key1, key2;
1483 make_unique_key<1>(&key1, i);
1484 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001485
bsalomon24db3b12015-01-23 04:24:04 -08001486 TestResource* resource;
1487
halcanary385fe4d2015-08-26 13:07:48 -07001488 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001489 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001490 resource->setSize(1);
1491 resource->unref();
1492
halcanary385fe4d2015-08-26 13:07:48 -07001493 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001494 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001495 resource->setSize(1);
1496 resource->unref();
1497 }
1498
1499 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001500 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001501 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1502 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1503 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1504 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001505 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001506 GrUniqueKey key1, key2;
1507 make_unique_key<1>(&key1, i);
1508 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001509
bsalomon8718aaf2015-02-19 07:24:21 -08001510 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1511 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001512 }
1513
bsalomon0ea80f42015-02-11 10:49:59 -08001514 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001515 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001516 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001517 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1518 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1519 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1520 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001521
1522 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001523 GrUniqueKey key1, key2;
1524 make_unique_key<1>(&key1, i);
1525 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001526
bsalomon8718aaf2015-02-19 07:24:21 -08001527 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1528 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001529 }
1530}
1531
senorblanco84cd6212015-08-04 10:01:58 -07001532static void test_custom_data(skiatest::Reporter* reporter) {
1533 GrUniqueKey key1, key2;
1534 make_unique_key<0>(&key1, 1);
1535 make_unique_key<0>(&key2, 2);
1536 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001537 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001538 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1539 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1540
1541 // Test that copying a key also takes a ref on its custom data.
1542 GrUniqueKey key3 = key1;
1543 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1544}
1545
bsalomonc6363ef2015-09-24 07:07:40 -07001546static void test_abandoned(skiatest::Reporter* reporter) {
1547 Mock mock(10, 300);
1548 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001549 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001550 context->abandonContext();
1551
1552 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1553
1554 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1555
robertphillips8abb3702016-08-31 14:04:06 -07001556 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001557 resource->getUniqueKey();
1558 resource->wasDestroyed();
1559 resource->gpuMemorySize();
1560 resource->getContext();
1561
1562 resource->abandon();
1563 resource->resourcePriv().getScratchKey();
1564 resource->resourcePriv().isBudgeted();
1565 resource->resourcePriv().makeBudgeted();
1566 resource->resourcePriv().makeUnbudgeted();
1567 resource->resourcePriv().removeScratchKey();
1568 GrUniqueKey key;
1569 make_unique_key<0>(&key, 1);
1570 resource->resourcePriv().setUniqueKey(key);
1571 resource->resourcePriv().removeUniqueKey();
1572}
1573
Brian Salomon1090da62017-01-06 12:04:19 -05001574static void test_tags(skiatest::Reporter* reporter) {
1575#ifdef SK_DEBUG
1576 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1577 static constexpr int kLastTagIdx = 10;
1578 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1579
1580 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1581 GrContext* context = mock.context();
1582 GrResourceCache* cache = mock.cache();
1583
1584 SkString tagStr;
1585 int tagIdx = 0;
1586 int currTagCnt = 0;
1587
1588 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1589 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1590 GrUniqueKey key;
1591 if (currTagCnt == tagIdx) {
1592 tagIdx += 1;
1593 currTagCnt = 0;
1594 tagStr.printf("tag%d", tagIdx);
1595 }
1596 make_unique_key<1>(&key, i, tagStr.c_str());
1597 resource->resourcePriv().setUniqueKey(key);
1598 }
1599 SkASSERT(kLastTagIdx == tagIdx);
1600 SkASSERT(currTagCnt == kLastTagIdx);
1601
1602 // Test i = 0 to exercise unused tag string.
1603 for (int i = 0; i <= kLastTagIdx; ++i) {
1604 tagStr.printf("tag%d", i);
1605 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1606 }
1607#endif
1608}
1609
kkinnunen15302832015-12-01 04:35:26 -08001610DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001611 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001612 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001613 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001614 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001615 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001616 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001617 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001618 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001619 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001620 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001621 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001622 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001623 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001624 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001625 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001626 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001627 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001628 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001629 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001630 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001631}
1632
Robert Phillipsd6214d42016-11-07 08:23:48 -05001633////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001634static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001635 GrSurfaceFlags flags,
1636 int width, int height,
1637 int sampleCnt) {
1638 GrSurfaceDesc desc;
1639 desc.fFlags = flags;
1640 desc.fWidth = width;
1641 desc.fHeight = height;
1642 desc.fConfig = kRGBA_8888_GrPixelConfig;
1643 desc.fSampleCnt = sampleCnt;
1644
Robert Phillipse78b7252017-04-06 07:59:41 -04001645 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001646}
1647
Robert Phillipse78b7252017-04-06 07:59:41 -04001648static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1649 GrSurfaceFlags flags,
1650 int width, int height,
1651 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001652 SkBitmap bm;
1653
1654 bm.allocN32Pixels(width, height, true);
1655 bm.eraseColor(SK_ColorBLUE);
1656
Brian Osman7b8400d2016-11-08 17:08:54 -05001657 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001658 SkASSERT(mipmaps);
1659 SkASSERT(mipmaps->countLevels() > 1);
1660
1661 int mipLevelCount = mipmaps->countLevels() + 1;
1662
1663 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1664
1665 texels[0].fPixels = bm.getPixels();
1666 texels[0].fRowBytes = bm.rowBytes();
1667
1668 for (int i = 1; i < mipLevelCount; ++i) {
1669 SkMipMap::Level generatedMipLevel;
1670 mipmaps->getLevel(i - 1, &generatedMipLevel);
1671 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1672 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1673 }
1674
1675 GrSurfaceDesc desc;
1676 desc.fFlags = flags;
1677 desc.fWidth = width;
1678 desc.fHeight = height;
1679 desc.fConfig = kRGBA_8888_GrPixelConfig;
1680 desc.fSampleCnt = sampleCnt;
1681 desc.fIsMipMapped = true;
1682
Robert Phillipse78b7252017-04-06 07:59:41 -04001683 return provider->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001684}
1685
1686// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1687// Texture-only, both-RT-and-Texture and MIPmapped
1688DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1689 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001690 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001691
Robert Phillipsd6214d42016-11-07 08:23:48 -05001692 static const int kSize = 64;
1693
Robert Phillipsd6214d42016-11-07 08:23:48 -05001694 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001695 {
1696 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001697
Robert Phillipse78b7252017-04-06 07:59:41 -04001698 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1699 size_t size = tex->gpuMemorySize();
1700 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1701
1702 if (context->caps()->maxSampleCount() >= 4) {
1703 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1704 size = tex->gpuMemorySize();
1705 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1706 kSize*kSize*4*4 == size || // auto-resolving
1707 kSize*kSize*4*5 == size); // explicit resolve buffer
1708 }
1709
1710 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001711 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001712 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001713 }
1714
Robert Phillipsd6214d42016-11-07 08:23:48 -05001715
1716 // Mipmapped versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001717 {
1718 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001719
Robert Phillipse78b7252017-04-06 07:59:41 -04001720 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1721 size_t size = proxy->gpuMemorySize();
1722 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1723
1724 if (context->caps()->maxSampleCount() >= 4) {
1725 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1726 size = proxy->gpuMemorySize();
1727 REPORTER_ASSERT(reporter,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001728 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1729 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1730 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001731 }
Robert Phillips1b352562017-04-05 18:56:21 +00001732
Robert Phillipse78b7252017-04-06 07:59:41 -04001733 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1734 size = proxy->gpuMemorySize();
1735 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1736 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001737}
1738
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001739#endif