blob: 3a11bb9c2e2a0182e04e395fe0130e60f3bab342 [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 Osman766fcbb2017-03-13 09:33:09 -0400215 GrBackendObject texHandles[3];
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);
Brian Osman766fcbb2017-03-13 09:33:09 -0400221 texHandles[2] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700222
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223 context->resetContext();
224
Greg Daniel7ef28f32017-04-20 16:41:55 +0000225 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
226 kW,
227 kH,
228 kRGBA_8888_GrPixelConfig,
229 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500230 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000231 backendTex1, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
232 kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233
Greg Daniel7ef28f32017-04-20 16:41:55 +0000234 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
235 kW,
236 kH,
237 kRGBA_8888_GrPixelConfig,
238 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500239 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000240 backendTex2, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
241 kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700242
Greg Daniel7ef28f32017-04-20 16:41:55 +0000243 GrBackendTexture backendTex3 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
244 kW,
245 kH,
246 kRGBA_8888_GrPixelConfig,
247 texHandles[2]);
Brian Osman766fcbb2017-03-13 09:33:09 -0400248 sk_sp<GrTexture> adoptedAndCached(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000249 backendTex3, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
250 kAdoptAndCache_GrWrapOwnership));
Brian Osman766fcbb2017-03-13 09:33:09 -0400251
252 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr &&
253 adoptedAndCached != nullptr);
254 if (!borrowed || !adopted || !adoptedAndCached) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700255 return;
256 }
257
halcanary96fcdcc2015-08-27 07:41:13 -0700258 borrowed.reset(nullptr);
259 adopted.reset(nullptr);
Brian Osman766fcbb2017-03-13 09:33:09 -0400260 adoptedAndCached.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700261
262 context->flush();
263
bsalomon091f60c2015-11-10 11:54:56 -0800264 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
265 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
Brian Osman766fcbb2017-03-13 09:33:09 -0400266 bool adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700267
268 REPORTER_ASSERT(reporter, borrowedIsAlive);
269 REPORTER_ASSERT(reporter, !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400270 REPORTER_ASSERT(reporter, adoptedAndCachedIsAlive); // Still alive because it's in the cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700271
bsalomon67d76202015-11-11 12:40:42 -0800272 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
273 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400274 // We can't delete texHandles[2] - we've given control of the lifetime to the context/cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700275
276 context->resetContext();
Brian Osman766fcbb2017-03-13 09:33:09 -0400277
278 // Purge the cache. This should force texHandles[2] to be deleted
279 context->getResourceCache()->purgeAllUnlocked();
280 adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
281 REPORTER_ASSERT(reporter, !adoptedAndCachedIsAlive);
282 gpu->deleteTestingOnlyBackendTexture(texHandles[2], !adoptedAndCachedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700283}
284
bsalomon6d3fe022014-07-25 08:35:45 -0700285class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800286 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000287public:
robertphillips6e83ac72015-08-13 05:19:14 -0700288 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700289
bsalomon1c60dfe2015-01-21 09:32:40 -0800290 /** Property that distinctly categorizes the resource.
291 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800292 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800293
kkinnunen2e6055b2016-04-22 01:48:29 -0700294 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
295 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700296 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800297 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700298 , fProperty(kA_SimulatedProperty)
299 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800300 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700301 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800302 }
303
kkinnunen2e6055b2016-04-22 01:48:29 -0700304 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
305 SimulatedProperty property) {
306 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800307 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700308 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
309 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000310 }
311
Brian Salomond3b65972017-03-22 12:05:03 -0400312 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800313 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800314 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000315 }
316
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000317 void setSize(size_t size) {
318 fSize = size;
319 this->didChangeGpuMemorySize();
320 }
321
bsalomon33435572014-11-05 14:47:41 -0800322 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000323
bsalomon71cb0c22014-11-14 12:10:14 -0800324 void setUnrefWhenDestroyed(TestResource* resource) {
325 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000326 }
327
bsalomon1c60dfe2015-01-21 09:32:40 -0800328 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
329 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
330 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800331 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
332 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800333 }
334 }
335
336 static size_t ExpectedScratchKeySize() {
337 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
338 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000339private:
bsalomon24db3b12015-01-23 04:24:04 -0800340 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800341
kkinnunen2e6055b2016-04-22 01:48:29 -0700342 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
343 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700344 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800345 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700346 , fProperty(property)
347 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800348 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700349 this->registerWithCache(budgeted);
350 }
351
352 // Constructor for simulating resources that wrap backend objects.
353 TestResource(GrGpu* gpu, size_t size)
354 : INHERITED(gpu)
355 , fToDelete(nullptr)
356 , fSize(size)
357 , fProperty(kA_SimulatedProperty)
358 , fIsScratch(false) {
359 ++fNumAlive;
360 this->registerWithCacheWrapped();
361 }
362
363 void computeScratchKey(GrScratchKey* key) const override {
364 if (fIsScratch) {
365 ComputeScratchKey(fProperty, key);
366 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800367 }
368
mtklein36352bf2015-03-25 18:17:31 -0700369 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800370
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000371 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000372 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800373 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800374 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700375 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700376 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000377};
bsalomon33435572014-11-05 14:47:41 -0800378int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000379
bsalomonc2f35b72015-01-23 07:19:22 -0800380class Mock {
381public:
382 Mock(int maxCnt, size_t maxBytes) {
383 fContext.reset(GrContext::CreateMockContext());
384 SkASSERT(fContext);
385 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800386 GrResourceCache* cache = fContext->getResourceCache();
387 cache->purgeAllUnlocked();
388 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800389 }
bsalomonc2f35b72015-01-23 07:19:22 -0800390
bsalomon0ea80f42015-02-11 10:49:59 -0800391 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800392
Hal Canary342b7ac2016-11-04 11:49:42 -0400393 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800394
395private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400396 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800397};
398
399static void test_no_key(skiatest::Reporter* reporter) {
400 Mock mock(10, 30000);
401 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800402 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800403
404 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700405 TestResource* a = new TestResource(context->getGpu());
406 TestResource* b = new TestResource(context->getGpu());
407 TestResource* c = new TestResource(context->getGpu());
408 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800409 a->setSize(11);
410 b->setSize(12);
411 c->setSize(13);
412 d->setSize(14);
413
414 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800415 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800416 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800417 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800418
419 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800420 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800421 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
422
bsalomon8718aaf2015-02-19 07:24:21 -0800423 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800424
425 a->unref();
426 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800427 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800428 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800429 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800430
431 c->unref();
432 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800433 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800434 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800435 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800436
437 d->unref();
438 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800439 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
440 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800441
442 b->unref();
443 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800444 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
445 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800446}
447
bsalomon24db3b12015-01-23 04:24:04 -0800448// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500449template <int>
450static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800451 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500452 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800453 builder[0] = data;
454}
455
bsalomon84c8e622014-11-17 09:33:27 -0800456static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800457 Mock mock(10, 300);
458 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800459 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800460
bsalomon8718aaf2015-02-19 07:24:21 -0800461 GrUniqueKey uniqueKey;
462 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800463
bsalomon8718aaf2015-02-19 07:24:21 -0800464 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800465 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700466 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800467 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700468 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800469 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800470 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700471 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800472 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700473 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700474 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800475 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800476
Brian Osman0562eb92017-05-08 11:16:39 -0400477 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800478 GrUniqueKey uniqueKey2;
479 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800480 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400481 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
482 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
483
484 // Remove the extra ref we just added.
485 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800486
487 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800488 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800489 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800490 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800491 cache->getResourceBytes());
492 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800493 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800494 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800495
bsalomon63c992f2015-01-23 12:47:59 -0800496 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800497 cache->purgeAllUnlocked();
498 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800499 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800500 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800501 cache->getResourceBytes());
502 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800503 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800504 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800505
506 // Unreffing the wrapped resource should free it right away.
507 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800508 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800509 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800510 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800511
bsalomon84c8e622014-11-17 09:33:27 -0800512 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700513 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800514 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800515 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800516 cache->purgeAllUnlocked();
517 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800518 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800519 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
520 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
521 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800522
523 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800524 cache->purgeAllUnlocked();
525 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800526 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800527 cache->getResourceBytes());
528 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
529 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800530
531 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800536
537 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800538 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
539 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
540 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
541 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800542}
543
bsalomon5236cf42015-01-14 10:42:08 -0800544static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800545 Mock mock(10, 30000);
546 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800547 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800548
bsalomon8718aaf2015-02-19 07:24:21 -0800549 GrUniqueKey uniqueKey;
550 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800551
552 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800553 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800554 TestResource* wrapped;
555 TestResource* unbudgeted;
556
557 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700558 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
559 TestResource::kB_SimulatedProperty);
560
bsalomon5236cf42015-01-14 10:42:08 -0800561 scratch->setSize(10);
562 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800563 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
564 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
565 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
566 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800567
halcanary385fe4d2015-08-26 13:07:48 -0700568 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800569 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800570 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800571 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800572 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
573 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
574 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
575 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800576
bsalomon0ea80f42015-02-11 10:49:59 -0800577 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700578 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800579 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
580 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
581 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
582 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800583
584 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800585 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
586 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
587 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
588 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800589
kkinnunen2e6055b2016-04-22 01:48:29 -0700590 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800591 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
592 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
593 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
594 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800595
596 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800597 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
598 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
599 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
600 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800601
bsalomon0ea80f42015-02-11 10:49:59 -0800602 cache->purgeAllUnlocked();
603 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
604 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
605 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
606 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800607}
608
bsalomon3582d3e2015-02-13 14:20:05 -0800609// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
610void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
611/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800612 Mock mock(10, 300);
613 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800614 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800615
616 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700617 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
618 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800619 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800620 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800621
622 size_t size = resource->gpuMemorySize();
623 for (int i = 0; i < 2; ++i) {
624 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800625 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800626 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800627 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700628 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800629 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
630 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
631 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
632 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800633
634 // Once it is unrefed, it should become available as scratch.
635 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
637 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
638 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
639 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700640 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800641 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800642 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800643 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800644 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800645
646 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700647 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800648 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800649 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800650 } else {
651 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800652 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800653 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
654 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
655 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
656 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800657 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800658 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800659 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800660
661 // now when it is unrefed it should die since it has no key.
662 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800663 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
664 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
665 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
666 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800667 }
bsalomon8b79d232014-11-10 10:19:06 -0800668 }
bsalomonc2f35b72015-01-23 07:19:22 -0800669}
670
671static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
672 Mock mock(5, 30000);
673 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800674 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800675
bsalomon8b79d232014-11-10 10:19:06 -0800676 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800677 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700678 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800679 TestResource::kB_SimulatedProperty);
680 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700681 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800682 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800683 a->setSize(11);
684 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800685 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800686 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800687 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700688 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800689
690 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800691 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800692
bsalomon0ea80f42015-02-11 10:49:59 -0800693 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800694 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800695 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
696 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800697 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800698 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800699
bsalomon63c992f2015-01-23 12:47:59 -0800700 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800701 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800702 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800703 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800704
705 // Unref but don't purge
706 a->unref();
707 b->unref();
708 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800709 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800710
bsalomon63c992f2015-01-23 12:47:59 -0800711 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800712 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800713 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800714 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
715 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800716}
717
bsalomon10e23ca2014-11-25 05:52:06 -0800718static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800719 Mock mock(5, 30000);
720 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800721 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800722
bsalomon10e23ca2014-11-25 05:52:06 -0800723 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700724 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800725 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700726 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800727 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800728 a->unref();
729 b->unref();
730
bsalomon1c60dfe2015-01-21 09:32:40 -0800731 GrScratchKey scratchKey;
732 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800733 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800734 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700735 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800736
bsalomon0ea80f42015-02-11 10:49:59 -0800737 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800738 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800739 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800740 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
741 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800742
743 // Find the first resource and remove its scratch key
744 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700745 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800746 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800747 // It's still alive, but not cached by scratch key anymore
748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800749 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
750 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800751
752 // The cache should immediately delete it when it's unrefed since it isn't accessible.
753 find->unref();
754 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800755 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
756 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800757
758 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700759 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800760 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800761 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800762 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
763 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800764
765 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800766 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800767 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800768 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
769 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800770
771 find->unref();
772 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800773 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
774 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800775}
776
bsalomon1c60dfe2015-01-21 09:32:40 -0800777static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800778 Mock mock(5, 30000);
779 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800780 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800781
782 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700783 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800784 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700785 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800786 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800787 a->unref();
788 b->unref();
789
790 GrScratchKey scratchKey;
791 // Ensure that scratch key comparison and assignment is consistent.
792 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800793 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800794 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800795 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800796 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
797 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
798 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
799 scratchKey = scratchKey1;
800 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
801 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
802 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
803 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
804 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
805 scratchKey = scratchKey2;
806 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
807 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
808 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
809 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
810 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
811
812 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800813 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800814 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700815 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800816
817 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800818 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700819 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700820 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800821 find->unref();
822
823 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700824 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700825 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800826 REPORTER_ASSERT(reporter, find == a || find == b);
827
robertphillips6e83ac72015-08-13 05:19:14 -0700828 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700829 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800830 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
831 REPORTER_ASSERT(reporter, find2 != find);
832 find2->unref();
833 find->unref();
834}
835
bsalomon8718aaf2015-02-19 07:24:21 -0800836static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800837 Mock mock(5, 30000);
838 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800839 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000840
bsalomon8718aaf2015-02-19 07:24:21 -0800841 GrUniqueKey key;
842 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700843
bsalomon8718aaf2015-02-19 07:24:21 -0800844 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700845 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800846 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700847
bsalomonf99e9612015-02-19 08:24:16 -0800848 // Set key on resource a.
849 a->resourcePriv().setUniqueKey(key);
850 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
851 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800852
bsalomonf99e9612015-02-19 08:24:16 -0800853 // Make sure that redundantly setting a's key works.
854 a->resourcePriv().setUniqueKey(key);
855 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800856 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800857 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800859 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
860
bsalomonf99e9612015-02-19 08:24:16 -0800861 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700862 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800863 b->setSize(12);
864 b->resourcePriv().setUniqueKey(key);
865 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
866 b->unref();
867
868 // Still have two resources because a is still reffed.
869 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
870 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
871 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
872
873 a->unref();
874 // Now a should be gone.
875 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
876 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
877 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
878
879 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
880 // Also make b be unreffed when replacement occurs.
881 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700882 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800883 GrUniqueKey differentKey;
884 make_unique_key<0>(&differentKey, 1);
885 c->setSize(13);
886 c->resourcePriv().setUniqueKey(differentKey);
887 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
888 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
889 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
890 // c replaces b and b should be immediately purged.
891 c->resourcePriv().setUniqueKey(key);
892 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
893 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
894 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
895
896 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800897 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800898 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
899 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
900 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
901
902 // Drop the ref on c, it should be kept alive because it has a unique key.
903 c->unref();
904 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
905 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
906 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
907
908 // Verify that we can find c, then remove its unique key. It should get purged immediately.
909 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
910 c->resourcePriv().removeUniqueKey();
911 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800912 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
913 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800914 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700915
916 {
917 GrUniqueKey key2;
918 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400919 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700920 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700921 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700922 d->resourcePriv().setUniqueKey(key2);
923 }
924
925 GrUniqueKey key3;
926 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400927 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700928 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000929}
930
bsalomon8b79d232014-11-10 10:19:06 -0800931static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800932 Mock mock(5, 30000);
933 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800934 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800935
bsalomon8718aaf2015-02-19 07:24:21 -0800936 GrUniqueKey key1, key2, key3;
937 make_unique_key<0>(&key1, 1);
938 make_unique_key<0>(&key2, 2);
939 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700940
bsalomon23e619c2015-02-06 11:54:28 -0800941 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700942 TestResource* a = new TestResource(context->getGpu());
943 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700944 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800945 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800946 a->resourcePriv().setUniqueKey(key1);
947 b->resourcePriv().setUniqueKey(key2);
948 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800949 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800950 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800951 c->unref();
952
bsalomon8718aaf2015-02-19 07:24:21 -0800953 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
954 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
955 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800956 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800957
bsalomon8718aaf2015-02-19 07:24:21 -0800958 typedef GrUniqueKeyInvalidatedMessage Msg;
959 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800960
961 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
962 Bus::Post(Msg(key1));
963 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800964 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800965 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800966 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
967 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800968 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800969 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800970
971 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800972 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800973 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800974 // we still have a ref on b, c should be recycled as scratch.
975 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800976 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800977
bsalomon23e619c2015-02-06 11:54:28 -0800978 // make b purgeable. It should be immediately deleted since it has no key.
979 b->unref();
980 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
981
982 // Make sure we actually get to c via it's scratch key, before we say goodbye.
983 GrScratchKey scratchKey;
984 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700985 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800986 REPORTER_ASSERT(reporter, scratch == c);
987 SkSafeUnref(scratch);
988
989 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800990 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700991 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800992 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800993 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
994 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800995 REPORTER_ASSERT(reporter, !scratch);
996 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800997}
998
bsalomon71cb0c22014-11-14 12:10:14 -0800999static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001000 Mock mock(3, 30000);
1001 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001002 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -08001003
bsalomon8718aaf2015-02-19 07:24:21 -08001004 GrUniqueKey key1, key2;
1005 make_unique_key<0>(&key1, 1);
1006 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001007
halcanary385fe4d2015-08-26 13:07:48 -07001008 TestResource* a = new TestResource(context->getGpu());
1009 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001010 a->resourcePriv().setUniqueKey(key1);
1011 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001012
bsalomonc2f35b72015-01-23 07:19:22 -08001013 // Make a cycle
1014 a->setUnrefWhenDestroyed(b);
1015 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001016
bsalomonc2f35b72015-01-23 07:19:22 -08001017 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001018
bsalomonc2f35b72015-01-23 07:19:22 -08001019 a->unref();
1020 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001021
bsalomonc2f35b72015-01-23 07:19:22 -08001022 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001023
bsalomon0ea80f42015-02-11 10:49:59 -08001024 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001025 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001026
bsalomonc2f35b72015-01-23 07:19:22 -08001027 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001028 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001029 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001030
bsalomon0ea80f42015-02-11 10:49:59 -08001031 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001032 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001033}
1034
bsalomon8b79d232014-11-10 10:19:06 -08001035static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001036 GrUniqueKey key1, key2;
1037 make_unique_key<0>(&key1, 1);
1038 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001039
1040 // Test changing resources sizes (both increase & decrease).
1041 {
bsalomonc2f35b72015-01-23 07:19:22 -08001042 Mock mock(3, 30000);
1043 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001044 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001045
halcanary385fe4d2015-08-26 13:07:48 -07001046 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001047 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001048 a->unref();
1049
halcanary385fe4d2015-08-26 13:07:48 -07001050 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001051 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001052 b->unref();
1053
bsalomon0ea80f42015-02-11 10:49:59 -08001054 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1055 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001056 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001057 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001058 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001059 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001060 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001061 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001062 find1->setSize(50);
1063 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001064
bsalomon0ea80f42015-02-11 10:49:59 -08001065 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1066 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001067 }
1068
1069 // Test increasing a resources size beyond the cache budget.
1070 {
bsalomonc2f35b72015-01-23 07:19:22 -08001071 Mock mock(2, 300);
1072 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001073 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001074
halcanary385fe4d2015-08-26 13:07:48 -07001075 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001076 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001077 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001078 a->unref();
1079
halcanary385fe4d2015-08-26 13:07:48 -07001080 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001081 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001082 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001083 b->unref();
1084
bsalomon0ea80f42015-02-11 10:49:59 -08001085 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1086 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001087
bsalomon8b79d232014-11-10 10:19:06 -08001088 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001089 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001090 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001091 find2->setSize(201);
1092 }
bsalomon8718aaf2015-02-19 07:24:21 -08001093 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001094
bsalomon0ea80f42015-02-11 10:49:59 -08001095 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1096 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001097 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001098}
1099
bsalomonddf30e62015-02-19 11:38:44 -08001100static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1101 static const int kCount = 50;
1102 static const int kBudgetCnt = kCount / 2;
1103 static const int kLockedFreq = 8;
1104 static const int kBudgetSize = 0x80000000;
1105
1106 SkRandom random;
1107
1108 // Run the test 2*kCount times;
1109 for (int i = 0; i < 2 * kCount; ++i ) {
1110 Mock mock(kBudgetCnt, kBudgetSize);
1111 GrContext* context = mock.context();
1112 GrResourceCache* cache = mock.cache();
1113
1114 // Pick a random number of resources to add before the timestamp will wrap.
1115 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1116
1117 static const int kNumToPurge = kCount - kBudgetCnt;
1118
1119 SkTDArray<int> shouldPurgeIdxs;
1120 int purgeableCnt = 0;
1121 SkTDArray<GrGpuResource*> resourcesToUnref;
1122
1123 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1124 // unpurgeable resources.
1125 for (int j = 0; j < kCount; ++j) {
1126 GrUniqueKey key;
1127 make_unique_key<0>(&key, j);
1128
halcanary385fe4d2015-08-26 13:07:48 -07001129 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001130 r->resourcePriv().setUniqueKey(key);
1131 if (random.nextU() % kLockedFreq) {
1132 // Make this is purgeable.
1133 r->unref();
1134 ++purgeableCnt;
1135 if (purgeableCnt <= kNumToPurge) {
1136 *shouldPurgeIdxs.append() = j;
1137 }
1138 } else {
1139 *resourcesToUnref.append() = r;
1140 }
1141 }
1142
1143 // Verify that the correct resources were purged.
1144 int currShouldPurgeIdx = 0;
1145 for (int j = 0; j < kCount; ++j) {
1146 GrUniqueKey key;
1147 make_unique_key<0>(&key, j);
1148 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1149 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1150 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1151 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001152 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001153 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001154 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001155 }
1156 SkSafeUnref(res);
1157 }
1158
1159 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1160 resourcesToUnref[j]->unref();
1161 }
1162 }
1163}
1164
bsalomon3f324322015-04-08 11:01:54 -07001165static void test_flush(skiatest::Reporter* reporter) {
1166 Mock mock(1000000, 1000000);
1167 GrContext* context = mock.context();
1168 GrResourceCache* cache = mock.cache();
1169
1170 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1171 // power of two here to keep things simpler.
1172 static const int kFlushCount = 16;
1173 cache->setLimits(1000000, 1000000, kFlushCount);
1174
1175 {
1176 // Insert a resource and send a flush notification kFlushCount times.
1177 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001178 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001179 GrUniqueKey k;
1180 make_unique_key<1>(&k, i);
1181 r->resourcePriv().setUniqueKey(k);
1182 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001183 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001184 }
1185
1186 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001187 for (int i = 0; i < kFlushCount; ++i) {
1188 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001189 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1190 for (int j = 0; j < i; ++j) {
1191 GrUniqueKey k;
1192 make_unique_key<1>(&k, j);
1193 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1194 REPORTER_ASSERT(reporter, !SkToBool(r));
1195 SkSafeUnref(r);
1196 }
bsalomon3f324322015-04-08 11:01:54 -07001197 }
1198
1199 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1200 cache->purgeAllUnlocked();
1201 }
1202
1203 // Do a similar test but where we leave refs on some resources to prevent them from being
1204 // purged.
1205 {
1206 GrGpuResource* refedResources[kFlushCount >> 1];
1207 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001208 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001209 GrUniqueKey k;
1210 make_unique_key<1>(&k, i);
1211 r->resourcePriv().setUniqueKey(k);
1212 // Leave a ref on every other resource, beginning with the first.
1213 if (SkToBool(i & 0x1)) {
1214 refedResources[i/2] = r;
1215 } else {
1216 r->unref();
1217 }
bsalomonb77a9072016-09-07 10:02:04 -07001218 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001219 }
1220
1221 for (int i = 0; i < kFlushCount; ++i) {
1222 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001223 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001224 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001225 }
1226
1227 // Unref all the resources that we kept refs on in the first loop.
1228 for (int i = 0; i < kFlushCount >> 1; ++i) {
1229 refedResources[i]->unref();
1230 }
1231
bsalomone2e87f32016-09-22 12:42:11 -07001232 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1233 // kFlushCount full flushes.
1234 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001235 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001236 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001237 }
1238 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1239
1240 cache->purgeAllUnlocked();
1241 }
1242
1243 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001244
1245 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1246 // eviction.
1247 context->flush();
1248 for (int i = 0; i < 10; ++i) {
1249 TestResource* r = new TestResource(context->getGpu());
1250 GrUniqueKey k;
1251 make_unique_key<1>(&k, i);
1252 r->resourcePriv().setUniqueKey(k);
1253 r->unref();
1254 }
1255 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1256 for (int i = 0; i < 10 * kFlushCount; ++i) {
1257 context->flush();
1258 }
1259 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001260}
1261
Brian Salomon5e150852017-03-22 14:53:13 -04001262static void test_time_purge(skiatest::Reporter* reporter) {
1263 Mock mock(1000000, 1000000);
1264 GrContext* context = mock.context();
1265 GrResourceCache* cache = mock.cache();
1266
1267 static constexpr int kCnts[] = {1, 10, 1024};
1268 auto nowish = []() {
1269 // We sleep so that we ensure we get a value that is greater than the last call to
1270 // GrStdSteadyClock::now().
1271 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1272 auto result = GrStdSteadyClock::now();
1273 // Also sleep afterwards so we don't get this value again.
1274 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1275 return result;
1276 };
1277
1278 for (int cnt : kCnts) {
1279 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1280 new GrStdSteadyClock::time_point[cnt]);
1281 {
1282 // Insert resources and get time points between each addition.
1283 for (int i = 0; i < cnt; ++i) {
1284 TestResource* r = new TestResource(context->getGpu());
1285 GrUniqueKey k;
1286 make_unique_key<1>(&k, i);
1287 r->resourcePriv().setUniqueKey(k);
1288 r->unref();
1289 timeStamps.get()[i] = nowish();
1290 }
1291
1292 // Purge based on the time points between resource additions. Each purge should remove
1293 // the oldest resource.
1294 for (int i = 0; i < cnt; ++i) {
1295 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1296 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1297 for (int j = 0; j < i; ++j) {
1298 GrUniqueKey k;
1299 make_unique_key<1>(&k, j);
1300 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1301 REPORTER_ASSERT(reporter, !SkToBool(r));
1302 SkSafeUnref(r);
1303 }
1304 }
1305
1306 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1307 cache->purgeAllUnlocked();
1308 }
1309
1310 // Do a similar test but where we leave refs on some resources to prevent them from being
1311 // purged.
1312 {
1313 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1314 for (int i = 0; i < cnt; ++i) {
1315 TestResource* r = new TestResource(context->getGpu());
1316 GrUniqueKey k;
1317 make_unique_key<1>(&k, i);
1318 r->resourcePriv().setUniqueKey(k);
1319 // Leave a ref on every other resource, beginning with the first.
1320 if (SkToBool(i & 0x1)) {
1321 refedResources.get()[i / 2] = r;
1322 } else {
1323 r->unref();
1324 }
1325 timeStamps.get()[i] = nowish();
1326 }
1327
1328 for (int i = 0; i < cnt; ++i) {
1329 // Should get a resource purged every other frame.
1330 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1331 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1332 }
1333
1334 // Unref all the resources that we kept refs on in the first loop.
1335 for (int i = 0; i < (cnt / 2); ++i) {
1336 refedResources.get()[i]->unref();
1337 cache->purgeResourcesNotUsedSince(nowish());
1338 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1339 }
1340
1341 cache->purgeAllUnlocked();
1342 }
1343
1344 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1345
1346 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1347 // eviction
1348 context->flush();
1349 for (int i = 0; i < 10; ++i) {
1350 TestResource* r = new TestResource(context->getGpu());
1351 GrUniqueKey k;
1352 make_unique_key<1>(&k, i);
1353 r->resourcePriv().setUniqueKey(k);
1354 r->unref();
1355 }
1356 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1357 context->flush();
1358 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1359 cache->purgeResourcesNotUsedSince(nowish());
1360 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1361 }
1362}
1363
bsalomon10e23ca2014-11-25 05:52:06 -08001364static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001365 // Set the cache size to double the resource count because we're going to create 2x that number
1366 // resources, using two different key domains. Add a little slop to the bytes because we resize
1367 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001368 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001369
bsalomonc2f35b72015-01-23 07:19:22 -08001370 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1371 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001372 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001373
1374 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001375 GrUniqueKey key1, key2;
1376 make_unique_key<1>(&key1, i);
1377 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001378
bsalomon24db3b12015-01-23 04:24:04 -08001379 TestResource* resource;
1380
halcanary385fe4d2015-08-26 13:07:48 -07001381 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001382 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001383 resource->setSize(1);
1384 resource->unref();
1385
halcanary385fe4d2015-08-26 13:07:48 -07001386 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001387 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001388 resource->setSize(1);
1389 resource->unref();
1390 }
1391
1392 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001393 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1394 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1395 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1396 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001397 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001398 GrUniqueKey key1, key2;
1399 make_unique_key<1>(&key1, i);
1400 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001401
bsalomon8718aaf2015-02-19 07:24:21 -08001402 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1403 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001404 }
1405
bsalomon0ea80f42015-02-11 10:49:59 -08001406 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001407 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001408 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1409 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1410 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1411 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001412
1413 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001414 GrUniqueKey key1, key2;
1415 make_unique_key<1>(&key1, i);
1416 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001417
bsalomon8718aaf2015-02-19 07:24:21 -08001418 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1419 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001420 }
1421}
1422
senorblanco84cd6212015-08-04 10:01:58 -07001423static void test_custom_data(skiatest::Reporter* reporter) {
1424 GrUniqueKey key1, key2;
1425 make_unique_key<0>(&key1, 1);
1426 make_unique_key<0>(&key2, 2);
1427 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001428 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001429 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1430 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1431
1432 // Test that copying a key also takes a ref on its custom data.
1433 GrUniqueKey key3 = key1;
1434 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1435}
1436
bsalomonc6363ef2015-09-24 07:07:40 -07001437static void test_abandoned(skiatest::Reporter* reporter) {
1438 Mock mock(10, 300);
1439 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001440 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001441 context->abandonContext();
1442
1443 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1444
1445 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1446
robertphillips8abb3702016-08-31 14:04:06 -07001447 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001448 resource->getUniqueKey();
1449 resource->wasDestroyed();
1450 resource->gpuMemorySize();
1451 resource->getContext();
1452
1453 resource->abandon();
1454 resource->resourcePriv().getScratchKey();
1455 resource->resourcePriv().isBudgeted();
1456 resource->resourcePriv().makeBudgeted();
1457 resource->resourcePriv().makeUnbudgeted();
1458 resource->resourcePriv().removeScratchKey();
1459 GrUniqueKey key;
1460 make_unique_key<0>(&key, 1);
1461 resource->resourcePriv().setUniqueKey(key);
1462 resource->resourcePriv().removeUniqueKey();
1463}
1464
Brian Salomon1090da62017-01-06 12:04:19 -05001465static void test_tags(skiatest::Reporter* reporter) {
1466#ifdef SK_DEBUG
1467 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1468 static constexpr int kLastTagIdx = 10;
1469 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1470
1471 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1472 GrContext* context = mock.context();
1473 GrResourceCache* cache = mock.cache();
1474
1475 SkString tagStr;
1476 int tagIdx = 0;
1477 int currTagCnt = 0;
1478
1479 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1480 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1481 GrUniqueKey key;
1482 if (currTagCnt == tagIdx) {
1483 tagIdx += 1;
1484 currTagCnt = 0;
1485 tagStr.printf("tag%d", tagIdx);
1486 }
1487 make_unique_key<1>(&key, i, tagStr.c_str());
1488 resource->resourcePriv().setUniqueKey(key);
1489 }
1490 SkASSERT(kLastTagIdx == tagIdx);
1491 SkASSERT(currTagCnt == kLastTagIdx);
1492
1493 // Test i = 0 to exercise unused tag string.
1494 for (int i = 0; i <= kLastTagIdx; ++i) {
1495 tagStr.printf("tag%d", i);
1496 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1497 }
1498#endif
1499}
1500
kkinnunen15302832015-12-01 04:35:26 -08001501DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001502 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001503 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001504 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001505 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001506 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001507 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001508 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001509 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001510 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001511 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001512 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001513 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001514 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001515 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001516 test_time_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001517 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001518 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001519 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001520 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001521}
1522
Robert Phillipsd6214d42016-11-07 08:23:48 -05001523////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001524static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001525 GrSurfaceFlags flags,
1526 int width, int height,
1527 int sampleCnt) {
1528 GrSurfaceDesc desc;
1529 desc.fFlags = flags;
1530 desc.fWidth = width;
1531 desc.fHeight = height;
1532 desc.fConfig = kRGBA_8888_GrPixelConfig;
1533 desc.fSampleCnt = sampleCnt;
1534
Robert Phillipse78b7252017-04-06 07:59:41 -04001535 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001536}
1537
Robert Phillipse78b7252017-04-06 07:59:41 -04001538static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1539 GrSurfaceFlags flags,
1540 int width, int height,
1541 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001542 SkBitmap bm;
1543
1544 bm.allocN32Pixels(width, height, true);
1545 bm.eraseColor(SK_ColorBLUE);
1546
Brian Osman7b8400d2016-11-08 17:08:54 -05001547 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001548 SkASSERT(mipmaps);
1549 SkASSERT(mipmaps->countLevels() > 1);
1550
1551 int mipLevelCount = mipmaps->countLevels() + 1;
1552
1553 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1554
1555 texels[0].fPixels = bm.getPixels();
1556 texels[0].fRowBytes = bm.rowBytes();
1557
1558 for (int i = 1; i < mipLevelCount; ++i) {
1559 SkMipMap::Level generatedMipLevel;
1560 mipmaps->getLevel(i - 1, &generatedMipLevel);
1561 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1562 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1563 }
1564
1565 GrSurfaceDesc desc;
1566 desc.fFlags = flags;
1567 desc.fWidth = width;
1568 desc.fHeight = height;
1569 desc.fConfig = kRGBA_8888_GrPixelConfig;
1570 desc.fSampleCnt = sampleCnt;
1571 desc.fIsMipMapped = true;
1572
Robert Phillipse78b7252017-04-06 07:59:41 -04001573 return provider->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001574}
1575
1576// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1577// Texture-only, both-RT-and-Texture and MIPmapped
1578DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1579 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001580 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001581
Robert Phillipsd6214d42016-11-07 08:23:48 -05001582 static const int kSize = 64;
1583
Robert Phillipsd6214d42016-11-07 08:23:48 -05001584 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001585 {
1586 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001587
Robert Phillipse78b7252017-04-06 07:59:41 -04001588 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1589 size_t size = tex->gpuMemorySize();
1590 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1591
1592 if (context->caps()->maxSampleCount() >= 4) {
1593 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1594 size = tex->gpuMemorySize();
1595 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1596 kSize*kSize*4*4 == size || // auto-resolving
1597 kSize*kSize*4*5 == size); // explicit resolve buffer
1598 }
1599
1600 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001601 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001602 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001603 }
1604
Robert Phillipsd6214d42016-11-07 08:23:48 -05001605
1606 // Mipmapped versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001607 {
1608 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001609
Robert Phillipse78b7252017-04-06 07:59:41 -04001610 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1611 size_t size = proxy->gpuMemorySize();
1612 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1613
1614 if (context->caps()->maxSampleCount() >= 4) {
1615 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1616 size = proxy->gpuMemorySize();
1617 REPORTER_ASSERT(reporter,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001618 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1619 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1620 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001621 }
Robert Phillips1b352562017-04-05 18:56:21 +00001622
Robert Phillipse78b7252017-04-06 07:59:41 -04001623 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1624 size = proxy->gpuMemorySize();
1625 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1626 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001627}
1628
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001629#endif