blob: 5590c868bf6f9c0533cc005528b6b42685f5fa17 [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
egdanielec00d942015-09-14 12:56:10 -0700104 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800105 // Test that two budgeted RTs with the same desc share a stencil buffer.
Brian Osman32342f02017-03-04 08:12:46 -0500106 sk_sp<GrTexture> smallRT0(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800107 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700108 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800109 }
110
Brian Osman32342f02017-03-04 08:12:46 -0500111 sk_sp<GrTexture> smallRT1(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800112 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700113 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800114 }
115
egdaniel8dc7c3a2015-04-16 11:22:42 -0700116 REPORTER_ASSERT(reporter,
117 smallRT0 && smallRT1 &&
118 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700119 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
120 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800121
122 // An unbudgeted RT with the same desc should also share.
Brian Osman32342f02017-03-04 08:12:46 -0500123 sk_sp<GrTexture> smallRT2(resourceProvider->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800124 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700125 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800126 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700127 REPORTER_ASSERT(reporter,
128 smallRT0 && smallRT2 &&
129 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700130 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
131 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800132
133 // An RT with a much larger size should not share.
134 GrSurfaceDesc bigDesc;
135 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400136 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800137 bigDesc.fWidth = 400;
138 bigDesc.fHeight = 200;
139 bigDesc.fSampleCnt = 0;
Brian Osman32342f02017-03-04 08:12:46 -0500140 sk_sp<GrTexture> bigRT(resourceProvider->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800141 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700142 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800143 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700144 REPORTER_ASSERT(reporter,
145 smallRT0 && bigRT &&
146 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700147 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
148 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800149
bsalomon76228632015-05-29 08:02:10 -0700150 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700151 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800152 GrSurfaceDesc smallMSAADesc = smallDesc;
153 smallMSAADesc.fSampleCnt = 4;
Brian Osman32342f02017-03-04 08:12:46 -0500154 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
155 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700157 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800158 }
bsalomonb602d4d2015-02-19 12:05:58 -0800159#ifdef SK_BUILD_FOR_ANDROID
160 if (!smallMSAART0) {
161 // The nexus player seems to fail to create MSAA textures.
162 return;
163 }
164#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800165 REPORTER_ASSERT(reporter,
166 smallRT0 && smallMSAART0 &&
167 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700168 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
169 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800170 // A second MSAA RT should share with the first MSAA RT.
Brian Osman32342f02017-03-04 08:12:46 -0500171 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
172 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800173 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700174 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800175 }
176 REPORTER_ASSERT(reporter,
177 smallMSAART0 && smallMSAART1 &&
178 smallMSAART0->asRenderTarget() &&
179 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700180 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
181 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800182 // But not one with a larger sample count should not. (Also check that the request for 4
183 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700184 if (context->caps()->maxSampleCount() >= 8 &&
185 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700186 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800187 smallMSAADesc.fSampleCnt = 8;
Robert Phillipse78b7252017-04-06 07:59:41 -0400188 smallMSAART1 = resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo);
Hal Canary342b7ac2016-11-04 11:49:42 -0400189 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500190 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800191 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700192 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800193 }
194 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700195 smallMSAART0 && smallMSAART1 &&
196 smallMSAART0->asRenderTarget() &&
197 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700198 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
199 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800200 }
201 }
202}
203
bsalomon68d91342016-04-12 09:59:58 -0700204DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700205 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800206 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700207 // this test is only valid for GL
208 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700209 return;
210 }
211
Brian Osman766fcbb2017-03-13 09:33:09 -0400212 GrBackendObject texHandles[3];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700213 static const int kW = 100;
214 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700215
bsalomon091f60c2015-11-10 11:54:56 -0800216 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
217 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
Brian Osman766fcbb2017-03-13 09:33:09 -0400218 texHandles[2] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700219
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220 context->resetContext();
221
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222 GrBackendTexture backendTex1 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
223 kW,
224 kH,
225 kRGBA_8888_GrPixelConfig,
226 texHandles[0]);
Brian Osman32342f02017-03-04 08:12:46 -0500227 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000228 backendTex1, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
229 kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
Greg Daniel7ef28f32017-04-20 16:41:55 +0000231 GrBackendTexture backendTex2 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
232 kW,
233 kH,
234 kRGBA_8888_GrPixelConfig,
235 texHandles[1]);
Brian Osman32342f02017-03-04 08:12:46 -0500236 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000237 backendTex2, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
238 kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700239
Greg Daniel7ef28f32017-04-20 16:41:55 +0000240 GrBackendTexture backendTex3 = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
241 kW,
242 kH,
243 kRGBA_8888_GrPixelConfig,
244 texHandles[2]);
Brian Osman766fcbb2017-03-13 09:33:09 -0400245 sk_sp<GrTexture> adoptedAndCached(context->resourceProvider()->wrapBackendTexture(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000246 backendTex3, kTopLeft_GrSurfaceOrigin, kNone_GrBackendTextureFlag, 0,
247 kAdoptAndCache_GrWrapOwnership));
Brian Osman766fcbb2017-03-13 09:33:09 -0400248
249 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr &&
250 adoptedAndCached != nullptr);
251 if (!borrowed || !adopted || !adoptedAndCached) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700252 return;
253 }
254
halcanary96fcdcc2015-08-27 07:41:13 -0700255 borrowed.reset(nullptr);
256 adopted.reset(nullptr);
Brian Osman766fcbb2017-03-13 09:33:09 -0400257 adoptedAndCached.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700258
259 context->flush();
260
bsalomon091f60c2015-11-10 11:54:56 -0800261 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
262 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
Brian Osman766fcbb2017-03-13 09:33:09 -0400263 bool adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700264
265 REPORTER_ASSERT(reporter, borrowedIsAlive);
266 REPORTER_ASSERT(reporter, !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400267 REPORTER_ASSERT(reporter, adoptedAndCachedIsAlive); // Still alive because it's in the cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700268
bsalomon67d76202015-11-11 12:40:42 -0800269 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
270 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400271 // We can't delete texHandles[2] - we've given control of the lifetime to the context/cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700272
273 context->resetContext();
Brian Osman766fcbb2017-03-13 09:33:09 -0400274
275 // Purge the cache. This should force texHandles[2] to be deleted
276 context->getResourceCache()->purgeAllUnlocked();
277 adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
278 REPORTER_ASSERT(reporter, !adoptedAndCachedIsAlive);
279 gpu->deleteTestingOnlyBackendTexture(texHandles[2], !adoptedAndCachedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700280}
281
bsalomon6d3fe022014-07-25 08:35:45 -0700282class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800283 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000284public:
robertphillips6e83ac72015-08-13 05:19:14 -0700285 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700286
bsalomon1c60dfe2015-01-21 09:32:40 -0800287 /** Property that distinctly categorizes the resource.
288 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800289 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800290
kkinnunen2e6055b2016-04-22 01:48:29 -0700291 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
292 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700293 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800294 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700295 , fProperty(kA_SimulatedProperty)
296 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800297 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700298 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800299 }
300
kkinnunen2e6055b2016-04-22 01:48:29 -0700301 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
302 SimulatedProperty property) {
303 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800304 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
306 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000307 }
308
Brian Salomond3b65972017-03-22 12:05:03 -0400309 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800310 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800311 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000312 }
313
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000314 void setSize(size_t size) {
315 fSize = size;
316 this->didChangeGpuMemorySize();
317 }
318
bsalomon33435572014-11-05 14:47:41 -0800319 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000320
bsalomon71cb0c22014-11-14 12:10:14 -0800321 void setUnrefWhenDestroyed(TestResource* resource) {
322 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000323 }
324
bsalomon1c60dfe2015-01-21 09:32:40 -0800325 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
326 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
327 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800328 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
329 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800330 }
331 }
332
333 static size_t ExpectedScratchKeySize() {
334 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
335 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000336private:
bsalomon24db3b12015-01-23 04:24:04 -0800337 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800338
kkinnunen2e6055b2016-04-22 01:48:29 -0700339 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
340 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700341 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800342 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700343 , fProperty(property)
344 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800345 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700346 this->registerWithCache(budgeted);
347 }
348
349 // Constructor for simulating resources that wrap backend objects.
350 TestResource(GrGpu* gpu, size_t size)
351 : INHERITED(gpu)
352 , fToDelete(nullptr)
353 , fSize(size)
354 , fProperty(kA_SimulatedProperty)
355 , fIsScratch(false) {
356 ++fNumAlive;
357 this->registerWithCacheWrapped();
358 }
359
360 void computeScratchKey(GrScratchKey* key) const override {
361 if (fIsScratch) {
362 ComputeScratchKey(fProperty, key);
363 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800364 }
365
mtklein36352bf2015-03-25 18:17:31 -0700366 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800367
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000368 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000369 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800370 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800371 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700372 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700373 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000374};
bsalomon33435572014-11-05 14:47:41 -0800375int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000376
bsalomonc2f35b72015-01-23 07:19:22 -0800377class Mock {
378public:
379 Mock(int maxCnt, size_t maxBytes) {
380 fContext.reset(GrContext::CreateMockContext());
381 SkASSERT(fContext);
382 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800383 GrResourceCache* cache = fContext->getResourceCache();
384 cache->purgeAllUnlocked();
385 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 }
bsalomonc2f35b72015-01-23 07:19:22 -0800387
bsalomon0ea80f42015-02-11 10:49:59 -0800388 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800389
Hal Canary342b7ac2016-11-04 11:49:42 -0400390 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800391
392private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400393 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800394};
395
396static void test_no_key(skiatest::Reporter* reporter) {
397 Mock mock(10, 30000);
398 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800399 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800400
401 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700402 TestResource* a = new TestResource(context->getGpu());
403 TestResource* b = new TestResource(context->getGpu());
404 TestResource* c = new TestResource(context->getGpu());
405 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800406 a->setSize(11);
407 b->setSize(12);
408 c->setSize(13);
409 d->setSize(14);
410
411 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800413 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800414 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800415
416 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800417 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800418 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
419
bsalomon8718aaf2015-02-19 07:24:21 -0800420 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800421
422 a->unref();
423 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800424 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800425 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800426 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800427
428 c->unref();
429 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800430 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800431 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800432 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800433
434 d->unref();
435 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800436 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
437 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800438
439 b->unref();
440 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800441 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
442 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800443}
444
bsalomon24db3b12015-01-23 04:24:04 -0800445// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500446template <int>
447static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800448 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500449 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800450 builder[0] = data;
451}
452
bsalomon84c8e622014-11-17 09:33:27 -0800453static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800454 Mock mock(10, 300);
455 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800456 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800457
bsalomon8718aaf2015-02-19 07:24:21 -0800458 GrUniqueKey uniqueKey;
459 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800460
bsalomon8718aaf2015-02-19 07:24:21 -0800461 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800462 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700463 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800464 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700465 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800466 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800467 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700468 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800469 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700470 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700471 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800472 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800473
Brian Osman0562eb92017-05-08 11:16:39 -0400474 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800475 GrUniqueKey uniqueKey2;
476 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800477 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400478 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
479 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
480
481 // Remove the extra ref we just added.
482 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800483
484 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800485 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800486 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800487 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800488 cache->getResourceBytes());
489 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800490 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800491 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800492
bsalomon63c992f2015-01-23 12:47:59 -0800493 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800494 cache->purgeAllUnlocked();
495 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800496 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800497 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800498 cache->getResourceBytes());
499 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800500 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800501 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800502
503 // Unreffing the wrapped resource should free it right away.
504 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800505 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800506 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800507 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800508
bsalomon84c8e622014-11-17 09:33:27 -0800509 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700510 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800511 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800512 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800513 cache->purgeAllUnlocked();
514 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800515 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800516 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
517 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
518 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800519
520 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800521 cache->purgeAllUnlocked();
522 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800523 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800524 cache->getResourceBytes());
525 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
526 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800527
528 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800529 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
530 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
531 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
532 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800533
534 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800535 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
536 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
537 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
538 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800539}
540
bsalomon5236cf42015-01-14 10:42:08 -0800541static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800542 Mock mock(10, 30000);
543 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800544 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800545
bsalomon8718aaf2015-02-19 07:24:21 -0800546 GrUniqueKey uniqueKey;
547 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800548
549 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800550 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800551 TestResource* wrapped;
552 TestResource* unbudgeted;
553
554 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700555 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
556 TestResource::kB_SimulatedProperty);
557
bsalomon5236cf42015-01-14 10:42:08 -0800558 scratch->setSize(10);
559 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800560 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
561 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
562 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
563 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800564
halcanary385fe4d2015-08-26 13:07:48 -0700565 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800566 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800567 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800568 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
570 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
571 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
572 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800573
bsalomon0ea80f42015-02-11 10:49:59 -0800574 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700575 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800580
581 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800582 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
583 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
584 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
585 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800586
kkinnunen2e6055b2016-04-22 01:48:29 -0700587 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
589 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
590 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800592
593 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800594 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
595 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
596 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
597 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800598
bsalomon0ea80f42015-02-11 10:49:59 -0800599 cache->purgeAllUnlocked();
600 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
601 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
602 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
603 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800604}
605
bsalomon3582d3e2015-02-13 14:20:05 -0800606// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
607void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
608/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800609 Mock mock(10, 300);
610 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800611 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800612
613 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700614 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
615 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800616 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800617 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800618
619 size_t size = resource->gpuMemorySize();
620 for (int i = 0; i < 2; ++i) {
621 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800622 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800623 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800624 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700625 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800626 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
627 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
628 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
629 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800630
631 // Once it is unrefed, it should become available as scratch.
632 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800633 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
634 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
635 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
636 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700637 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800638 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800639 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800640 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800641 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800642
643 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700644 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800645 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800646 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800647 } else {
648 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800649 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800650 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
651 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
652 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
653 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800654 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800655 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800656 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800657
658 // now when it is unrefed it should die since it has no key.
659 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800660 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
661 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
662 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
663 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800664 }
bsalomon8b79d232014-11-10 10:19:06 -0800665 }
bsalomonc2f35b72015-01-23 07:19:22 -0800666}
667
668static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
669 Mock mock(5, 30000);
670 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800671 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800672
bsalomon8b79d232014-11-10 10:19:06 -0800673 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800674 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700675 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource::kB_SimulatedProperty);
677 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700678 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800679 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800680 a->setSize(11);
681 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800682 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800683 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800684 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700685 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800686
687 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800688 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800689
bsalomon0ea80f42015-02-11 10:49:59 -0800690 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800691 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800692 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
693 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800694 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800695 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800696
bsalomon63c992f2015-01-23 12:47:59 -0800697 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800698 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800699 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800700 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800701
702 // Unref but don't purge
703 a->unref();
704 b->unref();
705 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800706 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800707
bsalomon63c992f2015-01-23 12:47:59 -0800708 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800709 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800710 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800711 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
712 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800713}
714
bsalomon10e23ca2014-11-25 05:52:06 -0800715static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800716 Mock mock(5, 30000);
717 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800718 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800719
bsalomon10e23ca2014-11-25 05:52:06 -0800720 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700721 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800722 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700723 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800724 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800725 a->unref();
726 b->unref();
727
bsalomon1c60dfe2015-01-21 09:32:40 -0800728 GrScratchKey scratchKey;
729 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800730 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800731 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700732 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800733
bsalomon0ea80f42015-02-11 10:49:59 -0800734 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800735 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800736 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800737 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
738 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800739
740 // Find the first resource and remove its scratch key
741 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700742 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800743 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800744 // It's still alive, but not cached by scratch key anymore
745 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800746 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
747 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800748
749 // The cache should immediately delete it when it's unrefed since it isn't accessible.
750 find->unref();
751 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800752 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
753 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800754
755 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700756 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800757 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800758 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800759 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
760 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800761
762 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800763 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800764 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800765 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
766 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800767
768 find->unref();
769 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800770 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
771 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800772}
773
bsalomon1c60dfe2015-01-21 09:32:40 -0800774static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800775 Mock mock(5, 30000);
776 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800777 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800778
779 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700780 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800781 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700782 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800783 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800784 a->unref();
785 b->unref();
786
787 GrScratchKey scratchKey;
788 // Ensure that scratch key comparison and assignment is consistent.
789 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800790 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800791 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800792 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800793 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
794 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
795 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
796 scratchKey = scratchKey1;
797 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
798 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
799 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
800 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
801 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
802 scratchKey = scratchKey2;
803 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
804 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
805 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
806 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
807 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
808
809 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800810 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800811 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700812 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800813
814 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800815 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700816 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700817 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800818 find->unref();
819
820 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700821 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700822 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800823 REPORTER_ASSERT(reporter, find == a || find == b);
824
robertphillips6e83ac72015-08-13 05:19:14 -0700825 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700826 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800827 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
828 REPORTER_ASSERT(reporter, find2 != find);
829 find2->unref();
830 find->unref();
831}
832
bsalomon8718aaf2015-02-19 07:24:21 -0800833static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800834 Mock mock(5, 30000);
835 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800836 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000837
bsalomon8718aaf2015-02-19 07:24:21 -0800838 GrUniqueKey key;
839 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700840
bsalomon8718aaf2015-02-19 07:24:21 -0800841 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700842 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800843 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700844
bsalomonf99e9612015-02-19 08:24:16 -0800845 // Set key on resource a.
846 a->resourcePriv().setUniqueKey(key);
847 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
848 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800849
bsalomonf99e9612015-02-19 08:24:16 -0800850 // Make sure that redundantly setting a's key works.
851 a->resourcePriv().setUniqueKey(key);
852 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800853 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800854 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
855 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800856 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
857
bsalomonf99e9612015-02-19 08:24:16 -0800858 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700859 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800860 b->setSize(12);
861 b->resourcePriv().setUniqueKey(key);
862 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
863 b->unref();
864
865 // Still have two resources because a is still reffed.
866 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
867 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
868 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
869
870 a->unref();
871 // Now a should be gone.
872 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
873 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
874 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
875
876 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
877 // Also make b be unreffed when replacement occurs.
878 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700879 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800880 GrUniqueKey differentKey;
881 make_unique_key<0>(&differentKey, 1);
882 c->setSize(13);
883 c->resourcePriv().setUniqueKey(differentKey);
884 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
885 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
886 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
887 // c replaces b and b should be immediately purged.
888 c->resourcePriv().setUniqueKey(key);
889 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
890 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
891 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
892
893 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800894 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800895 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
896 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
897 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
898
899 // Drop the ref on c, it should be kept alive because it has a unique key.
900 c->unref();
901 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
902 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
903 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
904
905 // Verify that we can find c, then remove its unique key. It should get purged immediately.
906 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
907 c->resourcePriv().removeUniqueKey();
908 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800909 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
910 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800911 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700912
913 {
914 GrUniqueKey key2;
915 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400916 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700917 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700918 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700919 d->resourcePriv().setUniqueKey(key2);
920 }
921
922 GrUniqueKey key3;
923 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400924 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700925 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000926}
927
bsalomon8b79d232014-11-10 10:19:06 -0800928static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800929 Mock mock(5, 30000);
930 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800931 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800932
bsalomon8718aaf2015-02-19 07:24:21 -0800933 GrUniqueKey key1, key2, key3;
934 make_unique_key<0>(&key1, 1);
935 make_unique_key<0>(&key2, 2);
936 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700937
bsalomon23e619c2015-02-06 11:54:28 -0800938 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700939 TestResource* a = new TestResource(context->getGpu());
940 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700941 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800942 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800943 a->resourcePriv().setUniqueKey(key1);
944 b->resourcePriv().setUniqueKey(key2);
945 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800946 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800947 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800948 c->unref();
949
bsalomon8718aaf2015-02-19 07:24:21 -0800950 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
951 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
952 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800953 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800954
bsalomon8718aaf2015-02-19 07:24:21 -0800955 typedef GrUniqueKeyInvalidatedMessage Msg;
956 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800957
958 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
959 Bus::Post(Msg(key1));
960 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800961 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800962 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800963 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
964 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800965 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800966 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800967
968 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800969 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800970 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800971 // we still have a ref on b, c should be recycled as scratch.
972 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800973 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800974
bsalomon23e619c2015-02-06 11:54:28 -0800975 // make b purgeable. It should be immediately deleted since it has no key.
976 b->unref();
977 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
978
979 // Make sure we actually get to c via it's scratch key, before we say goodbye.
980 GrScratchKey scratchKey;
981 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700982 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800983 REPORTER_ASSERT(reporter, scratch == c);
984 SkSafeUnref(scratch);
985
986 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800987 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700988 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800989 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800990 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
991 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800992 REPORTER_ASSERT(reporter, !scratch);
993 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800994}
995
bsalomon71cb0c22014-11-14 12:10:14 -0800996static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800997 Mock mock(3, 30000);
998 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800999 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -08001000
bsalomon8718aaf2015-02-19 07:24:21 -08001001 GrUniqueKey key1, key2;
1002 make_unique_key<0>(&key1, 1);
1003 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001004
halcanary385fe4d2015-08-26 13:07:48 -07001005 TestResource* a = new TestResource(context->getGpu());
1006 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001007 a->resourcePriv().setUniqueKey(key1);
1008 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001009
bsalomonc2f35b72015-01-23 07:19:22 -08001010 // Make a cycle
1011 a->setUnrefWhenDestroyed(b);
1012 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001013
bsalomonc2f35b72015-01-23 07:19:22 -08001014 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001015
bsalomonc2f35b72015-01-23 07:19:22 -08001016 a->unref();
1017 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001018
bsalomonc2f35b72015-01-23 07:19:22 -08001019 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001020
bsalomon0ea80f42015-02-11 10:49:59 -08001021 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001022 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001023
bsalomonc2f35b72015-01-23 07:19:22 -08001024 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001025 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001026 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001027
bsalomon0ea80f42015-02-11 10:49:59 -08001028 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001029 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001030}
1031
bsalomon8b79d232014-11-10 10:19:06 -08001032static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001033 GrUniqueKey key1, key2;
1034 make_unique_key<0>(&key1, 1);
1035 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001036
1037 // Test changing resources sizes (both increase & decrease).
1038 {
bsalomonc2f35b72015-01-23 07:19:22 -08001039 Mock mock(3, 30000);
1040 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001041 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001042
halcanary385fe4d2015-08-26 13:07:48 -07001043 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001044 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001045 a->unref();
1046
halcanary385fe4d2015-08-26 13:07:48 -07001047 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001048 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001049 b->unref();
1050
bsalomon0ea80f42015-02-11 10:49:59 -08001051 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1052 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001053 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001054 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001055 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001056 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001057 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001058 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001059 find1->setSize(50);
1060 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001061
bsalomon0ea80f42015-02-11 10:49:59 -08001062 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1063 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001064 }
1065
1066 // Test increasing a resources size beyond the cache budget.
1067 {
bsalomonc2f35b72015-01-23 07:19:22 -08001068 Mock mock(2, 300);
1069 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001070 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001071
halcanary385fe4d2015-08-26 13:07:48 -07001072 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001073 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001074 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001075 a->unref();
1076
halcanary385fe4d2015-08-26 13:07:48 -07001077 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001078 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001079 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001080 b->unref();
1081
bsalomon0ea80f42015-02-11 10:49:59 -08001082 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1083 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001084
bsalomon8b79d232014-11-10 10:19:06 -08001085 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001086 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001087 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001088 find2->setSize(201);
1089 }
bsalomon8718aaf2015-02-19 07:24:21 -08001090 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001091
bsalomon0ea80f42015-02-11 10:49:59 -08001092 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1093 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001094 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001095}
1096
bsalomonddf30e62015-02-19 11:38:44 -08001097static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1098 static const int kCount = 50;
1099 static const int kBudgetCnt = kCount / 2;
1100 static const int kLockedFreq = 8;
1101 static const int kBudgetSize = 0x80000000;
1102
1103 SkRandom random;
1104
1105 // Run the test 2*kCount times;
1106 for (int i = 0; i < 2 * kCount; ++i ) {
1107 Mock mock(kBudgetCnt, kBudgetSize);
1108 GrContext* context = mock.context();
1109 GrResourceCache* cache = mock.cache();
1110
1111 // Pick a random number of resources to add before the timestamp will wrap.
1112 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1113
1114 static const int kNumToPurge = kCount - kBudgetCnt;
1115
1116 SkTDArray<int> shouldPurgeIdxs;
1117 int purgeableCnt = 0;
1118 SkTDArray<GrGpuResource*> resourcesToUnref;
1119
1120 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1121 // unpurgeable resources.
1122 for (int j = 0; j < kCount; ++j) {
1123 GrUniqueKey key;
1124 make_unique_key<0>(&key, j);
1125
halcanary385fe4d2015-08-26 13:07:48 -07001126 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001127 r->resourcePriv().setUniqueKey(key);
1128 if (random.nextU() % kLockedFreq) {
1129 // Make this is purgeable.
1130 r->unref();
1131 ++purgeableCnt;
1132 if (purgeableCnt <= kNumToPurge) {
1133 *shouldPurgeIdxs.append() = j;
1134 }
1135 } else {
1136 *resourcesToUnref.append() = r;
1137 }
1138 }
1139
1140 // Verify that the correct resources were purged.
1141 int currShouldPurgeIdx = 0;
1142 for (int j = 0; j < kCount; ++j) {
1143 GrUniqueKey key;
1144 make_unique_key<0>(&key, j);
1145 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1146 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1147 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1148 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001149 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001150 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001151 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001152 }
1153 SkSafeUnref(res);
1154 }
1155
1156 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1157 resourcesToUnref[j]->unref();
1158 }
1159 }
1160}
1161
bsalomon3f324322015-04-08 11:01:54 -07001162static void test_flush(skiatest::Reporter* reporter) {
1163 Mock mock(1000000, 1000000);
1164 GrContext* context = mock.context();
1165 GrResourceCache* cache = mock.cache();
1166
1167 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1168 // power of two here to keep things simpler.
1169 static const int kFlushCount = 16;
1170 cache->setLimits(1000000, 1000000, kFlushCount);
1171
1172 {
1173 // Insert a resource and send a flush notification kFlushCount times.
1174 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001175 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001176 GrUniqueKey k;
1177 make_unique_key<1>(&k, i);
1178 r->resourcePriv().setUniqueKey(k);
1179 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001180 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001181 }
1182
1183 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001184 for (int i = 0; i < kFlushCount; ++i) {
1185 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001186 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1187 for (int j = 0; j < i; ++j) {
1188 GrUniqueKey k;
1189 make_unique_key<1>(&k, j);
1190 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1191 REPORTER_ASSERT(reporter, !SkToBool(r));
1192 SkSafeUnref(r);
1193 }
bsalomon3f324322015-04-08 11:01:54 -07001194 }
1195
1196 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1197 cache->purgeAllUnlocked();
1198 }
1199
1200 // Do a similar test but where we leave refs on some resources to prevent them from being
1201 // purged.
1202 {
1203 GrGpuResource* refedResources[kFlushCount >> 1];
1204 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001205 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001206 GrUniqueKey k;
1207 make_unique_key<1>(&k, i);
1208 r->resourcePriv().setUniqueKey(k);
1209 // Leave a ref on every other resource, beginning with the first.
1210 if (SkToBool(i & 0x1)) {
1211 refedResources[i/2] = r;
1212 } else {
1213 r->unref();
1214 }
bsalomonb77a9072016-09-07 10:02:04 -07001215 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001216 }
1217
1218 for (int i = 0; i < kFlushCount; ++i) {
1219 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001220 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001221 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001222 }
1223
1224 // Unref all the resources that we kept refs on in the first loop.
1225 for (int i = 0; i < kFlushCount >> 1; ++i) {
1226 refedResources[i]->unref();
1227 }
1228
bsalomone2e87f32016-09-22 12:42:11 -07001229 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1230 // kFlushCount full flushes.
1231 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001232 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001233 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001234 }
1235 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1236
1237 cache->purgeAllUnlocked();
1238 }
1239
1240 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001241
1242 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1243 // eviction.
1244 context->flush();
1245 for (int i = 0; i < 10; ++i) {
1246 TestResource* r = new TestResource(context->getGpu());
1247 GrUniqueKey k;
1248 make_unique_key<1>(&k, i);
1249 r->resourcePriv().setUniqueKey(k);
1250 r->unref();
1251 }
1252 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1253 for (int i = 0; i < 10 * kFlushCount; ++i) {
1254 context->flush();
1255 }
1256 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001257}
1258
Brian Salomon5e150852017-03-22 14:53:13 -04001259static void test_time_purge(skiatest::Reporter* reporter) {
1260 Mock mock(1000000, 1000000);
1261 GrContext* context = mock.context();
1262 GrResourceCache* cache = mock.cache();
1263
1264 static constexpr int kCnts[] = {1, 10, 1024};
1265 auto nowish = []() {
1266 // We sleep so that we ensure we get a value that is greater than the last call to
1267 // GrStdSteadyClock::now().
1268 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1269 auto result = GrStdSteadyClock::now();
1270 // Also sleep afterwards so we don't get this value again.
1271 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1272 return result;
1273 };
1274
1275 for (int cnt : kCnts) {
1276 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1277 new GrStdSteadyClock::time_point[cnt]);
1278 {
1279 // Insert resources and get time points between each addition.
1280 for (int i = 0; i < cnt; ++i) {
1281 TestResource* r = new TestResource(context->getGpu());
1282 GrUniqueKey k;
1283 make_unique_key<1>(&k, i);
1284 r->resourcePriv().setUniqueKey(k);
1285 r->unref();
1286 timeStamps.get()[i] = nowish();
1287 }
1288
1289 // Purge based on the time points between resource additions. Each purge should remove
1290 // the oldest resource.
1291 for (int i = 0; i < cnt; ++i) {
1292 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1293 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1294 for (int j = 0; j < i; ++j) {
1295 GrUniqueKey k;
1296 make_unique_key<1>(&k, j);
1297 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1298 REPORTER_ASSERT(reporter, !SkToBool(r));
1299 SkSafeUnref(r);
1300 }
1301 }
1302
1303 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1304 cache->purgeAllUnlocked();
1305 }
1306
1307 // Do a similar test but where we leave refs on some resources to prevent them from being
1308 // purged.
1309 {
1310 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1311 for (int i = 0; i < cnt; ++i) {
1312 TestResource* r = new TestResource(context->getGpu());
1313 GrUniqueKey k;
1314 make_unique_key<1>(&k, i);
1315 r->resourcePriv().setUniqueKey(k);
1316 // Leave a ref on every other resource, beginning with the first.
1317 if (SkToBool(i & 0x1)) {
1318 refedResources.get()[i / 2] = r;
1319 } else {
1320 r->unref();
1321 }
1322 timeStamps.get()[i] = nowish();
1323 }
1324
1325 for (int i = 0; i < cnt; ++i) {
1326 // Should get a resource purged every other frame.
1327 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1328 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1329 }
1330
1331 // Unref all the resources that we kept refs on in the first loop.
1332 for (int i = 0; i < (cnt / 2); ++i) {
1333 refedResources.get()[i]->unref();
1334 cache->purgeResourcesNotUsedSince(nowish());
1335 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1336 }
1337
1338 cache->purgeAllUnlocked();
1339 }
1340
1341 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1342
1343 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1344 // eviction
1345 context->flush();
1346 for (int i = 0; i < 10; ++i) {
1347 TestResource* r = new TestResource(context->getGpu());
1348 GrUniqueKey k;
1349 make_unique_key<1>(&k, i);
1350 r->resourcePriv().setUniqueKey(k);
1351 r->unref();
1352 }
1353 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1354 context->flush();
1355 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1356 cache->purgeResourcesNotUsedSince(nowish());
1357 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1358 }
1359}
1360
bsalomon10e23ca2014-11-25 05:52:06 -08001361static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001362 // Set the cache size to double the resource count because we're going to create 2x that number
1363 // resources, using two different key domains. Add a little slop to the bytes because we resize
1364 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001365 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001366
bsalomonc2f35b72015-01-23 07:19:22 -08001367 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1368 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001369 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001370
1371 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001372 GrUniqueKey key1, key2;
1373 make_unique_key<1>(&key1, i);
1374 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001375
bsalomon24db3b12015-01-23 04:24:04 -08001376 TestResource* resource;
1377
halcanary385fe4d2015-08-26 13:07:48 -07001378 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001379 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001380 resource->setSize(1);
1381 resource->unref();
1382
halcanary385fe4d2015-08-26 13:07:48 -07001383 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001384 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001385 resource->setSize(1);
1386 resource->unref();
1387 }
1388
1389 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001390 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1391 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1392 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1393 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001394 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001395 GrUniqueKey key1, key2;
1396 make_unique_key<1>(&key1, i);
1397 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001398
bsalomon8718aaf2015-02-19 07:24:21 -08001399 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1400 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001401 }
1402
bsalomon0ea80f42015-02-11 10:49:59 -08001403 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001404 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001405 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1406 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1407 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1408 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001409
1410 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001411 GrUniqueKey key1, key2;
1412 make_unique_key<1>(&key1, i);
1413 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001414
bsalomon8718aaf2015-02-19 07:24:21 -08001415 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1416 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001417 }
1418}
1419
senorblanco84cd6212015-08-04 10:01:58 -07001420static void test_custom_data(skiatest::Reporter* reporter) {
1421 GrUniqueKey key1, key2;
1422 make_unique_key<0>(&key1, 1);
1423 make_unique_key<0>(&key2, 2);
1424 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001425 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001426 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1427 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1428
1429 // Test that copying a key also takes a ref on its custom data.
1430 GrUniqueKey key3 = key1;
1431 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1432}
1433
bsalomonc6363ef2015-09-24 07:07:40 -07001434static void test_abandoned(skiatest::Reporter* reporter) {
1435 Mock mock(10, 300);
1436 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001437 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001438 context->abandonContext();
1439
1440 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1441
1442 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1443
robertphillips8abb3702016-08-31 14:04:06 -07001444 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001445 resource->getUniqueKey();
1446 resource->wasDestroyed();
1447 resource->gpuMemorySize();
1448 resource->getContext();
1449
1450 resource->abandon();
1451 resource->resourcePriv().getScratchKey();
1452 resource->resourcePriv().isBudgeted();
1453 resource->resourcePriv().makeBudgeted();
1454 resource->resourcePriv().makeUnbudgeted();
1455 resource->resourcePriv().removeScratchKey();
1456 GrUniqueKey key;
1457 make_unique_key<0>(&key, 1);
1458 resource->resourcePriv().setUniqueKey(key);
1459 resource->resourcePriv().removeUniqueKey();
1460}
1461
Brian Salomon1090da62017-01-06 12:04:19 -05001462static void test_tags(skiatest::Reporter* reporter) {
1463#ifdef SK_DEBUG
1464 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1465 static constexpr int kLastTagIdx = 10;
1466 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1467
1468 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1469 GrContext* context = mock.context();
1470 GrResourceCache* cache = mock.cache();
1471
1472 SkString tagStr;
1473 int tagIdx = 0;
1474 int currTagCnt = 0;
1475
1476 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1477 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1478 GrUniqueKey key;
1479 if (currTagCnt == tagIdx) {
1480 tagIdx += 1;
1481 currTagCnt = 0;
1482 tagStr.printf("tag%d", tagIdx);
1483 }
1484 make_unique_key<1>(&key, i, tagStr.c_str());
1485 resource->resourcePriv().setUniqueKey(key);
1486 }
1487 SkASSERT(kLastTagIdx == tagIdx);
1488 SkASSERT(currTagCnt == kLastTagIdx);
1489
1490 // Test i = 0 to exercise unused tag string.
1491 for (int i = 0; i <= kLastTagIdx; ++i) {
1492 tagStr.printf("tag%d", i);
1493 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1494 }
1495#endif
1496}
1497
kkinnunen15302832015-12-01 04:35:26 -08001498DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001499 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001500 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001501 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001502 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001503 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001504 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001505 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001506 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001507 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001508 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001509 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001510 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001511 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001512 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001513 test_time_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001514 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001515 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001516 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001517 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001518}
1519
Robert Phillipsd6214d42016-11-07 08:23:48 -05001520////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001521static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001522 GrSurfaceFlags flags,
1523 int width, int height,
1524 int sampleCnt) {
1525 GrSurfaceDesc desc;
1526 desc.fFlags = flags;
1527 desc.fWidth = width;
1528 desc.fHeight = height;
1529 desc.fConfig = kRGBA_8888_GrPixelConfig;
1530 desc.fSampleCnt = sampleCnt;
1531
Robert Phillipse78b7252017-04-06 07:59:41 -04001532 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001533}
1534
Robert Phillipse78b7252017-04-06 07:59:41 -04001535static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1536 GrSurfaceFlags flags,
1537 int width, int height,
1538 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001539 SkBitmap bm;
1540
1541 bm.allocN32Pixels(width, height, true);
1542 bm.eraseColor(SK_ColorBLUE);
1543
Brian Osman7b8400d2016-11-08 17:08:54 -05001544 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001545 SkASSERT(mipmaps);
1546 SkASSERT(mipmaps->countLevels() > 1);
1547
1548 int mipLevelCount = mipmaps->countLevels() + 1;
1549
1550 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1551
1552 texels[0].fPixels = bm.getPixels();
1553 texels[0].fRowBytes = bm.rowBytes();
1554
1555 for (int i = 1; i < mipLevelCount; ++i) {
1556 SkMipMap::Level generatedMipLevel;
1557 mipmaps->getLevel(i - 1, &generatedMipLevel);
1558 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1559 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1560 }
1561
1562 GrSurfaceDesc desc;
1563 desc.fFlags = flags;
1564 desc.fWidth = width;
1565 desc.fHeight = height;
1566 desc.fConfig = kRGBA_8888_GrPixelConfig;
1567 desc.fSampleCnt = sampleCnt;
1568 desc.fIsMipMapped = true;
1569
Robert Phillipse78b7252017-04-06 07:59:41 -04001570 return provider->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001571}
1572
1573// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1574// Texture-only, both-RT-and-Texture and MIPmapped
1575DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1576 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001577 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001578
Robert Phillipsd6214d42016-11-07 08:23:48 -05001579 static const int kSize = 64;
1580
Robert Phillipsd6214d42016-11-07 08:23:48 -05001581 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001582 {
1583 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001584
Robert Phillipse78b7252017-04-06 07:59:41 -04001585 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1586 size_t size = tex->gpuMemorySize();
1587 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1588
1589 if (context->caps()->maxSampleCount() >= 4) {
1590 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1591 size = tex->gpuMemorySize();
1592 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1593 kSize*kSize*4*4 == size || // auto-resolving
1594 kSize*kSize*4*5 == size); // explicit resolve buffer
1595 }
1596
1597 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001598 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001599 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001600 }
1601
Robert Phillipsd6214d42016-11-07 08:23:48 -05001602
1603 // Mipmapped versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001604 {
1605 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001606
Robert Phillipse78b7252017-04-06 07:59:41 -04001607 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1608 size_t size = proxy->gpuMemorySize();
1609 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1610
1611 if (context->caps()->maxSampleCount() >= 4) {
1612 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1613 size = proxy->gpuMemorySize();
1614 REPORTER_ASSERT(reporter,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001615 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1616 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1617 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001618 }
Robert Phillips1b352562017-04-05 18:56:21 +00001619
Robert Phillipse78b7252017-04-06 07:59:41 -04001620 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1621 size = proxy->gpuMemorySize();
1622 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1623 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001624}
1625
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001626#endif