blob: 8051191d4b18e97a08df4341e04027111e6d5278 [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
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourceCacheAccess.h"
17#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTarget.h"
19#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070023#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080024#include "SkGr.h"
25#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050026#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070027#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000028#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000029
30static const int gWidth = 640;
31static const int gHeight = 480;
32
33////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070034DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070035 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080036 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040037 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080038 desc.fFlags = kRenderTarget_GrSurfaceFlag;
39 desc.fWidth = gWidth;
40 desc.fHeight = gHeight;
41 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070042 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080043 SkCanvas* canvas = surface->getCanvas();
44
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
46
47 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000048 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000049 src.eraseColor(SK_ColorBLACK);
50 size_t srcSize = src.getSize();
51
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000052 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070053 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
55 int oldMaxNum;
56 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000057 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000058
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000059 // Set the cache limits so we can fit 10 "src" images and the
60 // max number of textures doesn't matter
61 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000062 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000063
64 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000065 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066
67 for (int i = 0; i < 100; ++i) {
68 canvas->drawBitmap(src, 0, 0);
69 canvas->readPixels(size, &readback);
70
71 // "modify" the src texture
72 src.notifyPixelsChanged();
73
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000074 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070075 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000076
77 // we should never go over the size limit
78 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
79 }
80
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000081 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000082}
83
bsalomon11abd8d2016-10-14 08:13:48 -070084static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
85 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
86 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
87 return false;
88 }
89 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
90}
91
92// This currently fails on ES3 ANGLE contexts
93DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
94 ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070095 GrContext* context = ctxInfo.grContext();
bsalomon02a44a42015-02-19 09:09:00 -080096 GrSurfaceDesc smallDesc;
97 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -040098 smallDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -080099 smallDesc.fWidth = 4;
100 smallDesc.fHeight = 4;
101 smallDesc.fSampleCnt = 0;
102
egdanielec00d942015-09-14 12:56:10 -0700103 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon02a44a42015-02-19 09:09:00 -0800104 // Test that two budgeted RTs with the same desc share a stencil buffer.
Brian Osman32342f02017-03-04 08:12:46 -0500105 sk_sp<GrTexture> smallRT0(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800106 if (smallRT0 && smallRT0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700107 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800108 }
109
Brian Osman32342f02017-03-04 08:12:46 -0500110 sk_sp<GrTexture> smallRT1(resourceProvider->createTexture(smallDesc, SkBudgeted::kYes));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800111 if (smallRT1 && smallRT1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700112 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 }
114
egdaniel8dc7c3a2015-04-16 11:22:42 -0700115 REPORTER_ASSERT(reporter,
116 smallRT0 && smallRT1 &&
117 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700118 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
119 resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800120
121 // An unbudgeted RT with the same desc should also share.
Brian Osman32342f02017-03-04 08:12:46 -0500122 sk_sp<GrTexture> smallRT2(resourceProvider->createTexture(smallDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800123 if (smallRT2 && smallRT2->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700124 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800125 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700126 REPORTER_ASSERT(reporter,
127 smallRT0 && smallRT2 &&
128 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700129 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
130 resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800131
132 // An RT with a much larger size should not share.
133 GrSurfaceDesc bigDesc;
134 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Brian Osman777b5632016-10-14 09:16:21 -0400135 bigDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon02a44a42015-02-19 09:09:00 -0800136 bigDesc.fWidth = 400;
137 bigDesc.fHeight = 200;
138 bigDesc.fSampleCnt = 0;
Brian Osman32342f02017-03-04 08:12:46 -0500139 sk_sp<GrTexture> bigRT(resourceProvider->createTexture(bigDesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 if (bigRT && bigRT->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700141 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700143 REPORTER_ASSERT(reporter,
144 smallRT0 && bigRT &&
145 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700146 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
147 resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800148
bsalomon76228632015-05-29 08:02:10 -0700149 if (context->caps()->maxSampleCount() >= 4) {
mtklein5f939ab2016-03-16 10:28:35 -0700150 // An RT with a different sample count should not share.
bsalomon02a44a42015-02-19 09:09:00 -0800151 GrSurfaceDesc smallMSAADesc = smallDesc;
152 smallMSAADesc.fSampleCnt = 4;
Brian Osman32342f02017-03-04 08:12:46 -0500153 sk_sp<GrTexture> smallMSAART0(resourceProvider->createTexture(smallMSAADesc,
154 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800155 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700156 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800157 }
bsalomonb602d4d2015-02-19 12:05:58 -0800158#ifdef SK_BUILD_FOR_ANDROID
159 if (!smallMSAART0) {
160 // The nexus player seems to fail to create MSAA textures.
161 return;
162 }
163#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800164 REPORTER_ASSERT(reporter,
165 smallRT0 && smallMSAART0 &&
166 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700167 resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
168 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800169 // A second MSAA RT should share with the first MSAA RT.
Brian Osman32342f02017-03-04 08:12:46 -0500170 sk_sp<GrTexture> smallMSAART1(resourceProvider->createTexture(smallMSAADesc,
171 SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800172 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700173 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800174 }
175 REPORTER_ASSERT(reporter,
176 smallMSAART0 && smallMSAART1 &&
177 smallMSAART0->asRenderTarget() &&
178 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700179 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
180 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800181 // But not one with a larger sample count should not. (Also check that the request for 4
182 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700183 if (context->caps()->maxSampleCount() >= 8 &&
184 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700185 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800186 smallMSAADesc.fSampleCnt = 8;
Brian Osman32342f02017-03-04 08:12:46 -0500187 smallMSAART1.reset(resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
Hal Canary342b7ac2016-11-04 11:49:42 -0400188 sk_sp<GrTexture> smallMSAART1(
Brian Osman32342f02017-03-04 08:12:46 -0500189 resourceProvider->createTexture(smallMSAADesc, SkBudgeted::kNo));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800190 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdanielec00d942015-09-14 12:56:10 -0700191 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
bsalomon6bc1b5f2015-02-23 09:06:38 -0800192 }
193 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700194 smallMSAART0 && smallMSAART1 &&
195 smallMSAART0->asRenderTarget() &&
196 smallMSAART1->asRenderTarget() &&
egdanielec00d942015-09-14 12:56:10 -0700197 resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
198 resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
bsalomon02a44a42015-02-19 09:09:00 -0800199 }
200 }
201}
202
bsalomon68d91342016-04-12 09:59:58 -0700203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700204 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800205 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700206 // this test is only valid for GL
207 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700208 return;
209 }
210
Brian Osman766fcbb2017-03-13 09:33:09 -0400211 GrBackendObject texHandles[3];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212 static const int kW = 100;
213 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700214
bsalomon091f60c2015-11-10 11:54:56 -0800215 texHandles[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
216 texHandles[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
Brian Osman766fcbb2017-03-13 09:33:09 -0400217 texHandles[2] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700218
bsalomon6dc6f5f2015-06-18 09:12:16 -0700219 context->resetContext();
220
221 GrBackendTextureDesc desc;
222 desc.fConfig = kBGRA_8888_GrPixelConfig;
223 desc.fWidth = kW;
224 desc.fHeight = kH;
225
bsalomon091f60c2015-11-10 11:54:56 -0800226 desc.fTextureHandle = texHandles[0];
Brian Osman32342f02017-03-04 08:12:46 -0500227 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
bungeman6bd52842016-10-27 09:30:08 -0700228 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
bsalomon091f60c2015-11-10 11:54:56 -0800230 desc.fTextureHandle = texHandles[1];
Brian Osman32342f02017-03-04 08:12:46 -0500231 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
bungeman6bd52842016-10-27 09:30:08 -0700232 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700233
Brian Osman766fcbb2017-03-13 09:33:09 -0400234 desc.fTextureHandle = texHandles[2];
235 sk_sp<GrTexture> adoptedAndCached(context->resourceProvider()->wrapBackendTexture(
236 desc, kAdoptAndCache_GrWrapOwnership));
237
238 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr &&
239 adoptedAndCached != nullptr);
240 if (!borrowed || !adopted || !adoptedAndCached) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241 return;
242 }
243
halcanary96fcdcc2015-08-27 07:41:13 -0700244 borrowed.reset(nullptr);
245 adopted.reset(nullptr);
Brian Osman766fcbb2017-03-13 09:33:09 -0400246 adoptedAndCached.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700247
248 context->flush();
249
bsalomon091f60c2015-11-10 11:54:56 -0800250 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[0]);
251 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[1]);
Brian Osman766fcbb2017-03-13 09:33:09 -0400252 bool adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700253
254 REPORTER_ASSERT(reporter, borrowedIsAlive);
255 REPORTER_ASSERT(reporter, !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400256 REPORTER_ASSERT(reporter, adoptedAndCachedIsAlive); // Still alive because it's in the cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700257
bsalomon67d76202015-11-11 12:40:42 -0800258 gpu->deleteTestingOnlyBackendTexture(texHandles[0], !borrowedIsAlive);
259 gpu->deleteTestingOnlyBackendTexture(texHandles[1], !adoptedIsAlive);
Brian Osman766fcbb2017-03-13 09:33:09 -0400260 // We can't delete texHandles[2] - we've given control of the lifetime to the context/cache
bsalomon6dc6f5f2015-06-18 09:12:16 -0700261
262 context->resetContext();
Brian Osman766fcbb2017-03-13 09:33:09 -0400263
264 // Purge the cache. This should force texHandles[2] to be deleted
265 context->getResourceCache()->purgeAllUnlocked();
266 adoptedAndCachedIsAlive = gpu->isTestingOnlyBackendTexture(texHandles[2]);
267 REPORTER_ASSERT(reporter, !adoptedAndCachedIsAlive);
268 gpu->deleteTestingOnlyBackendTexture(texHandles[2], !adoptedAndCachedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700269}
270
bsalomon6d3fe022014-07-25 08:35:45 -0700271class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800272 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000273public:
robertphillips6e83ac72015-08-13 05:19:14 -0700274 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700275
bsalomon1c60dfe2015-01-21 09:32:40 -0800276 /** Property that distinctly categorizes the resource.
277 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800278 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800279
kkinnunen2e6055b2016-04-22 01:48:29 -0700280 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
281 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700282 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800283 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700284 , fProperty(kA_SimulatedProperty)
285 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800286 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700287 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800288 }
289
kkinnunen2e6055b2016-04-22 01:48:29 -0700290 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
291 SimulatedProperty property) {
292 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800293 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700294 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
295 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000296 }
297
298 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800299 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800300 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000301 }
302
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000303 void setSize(size_t size) {
304 fSize = size;
305 this->didChangeGpuMemorySize();
306 }
307
bsalomon33435572014-11-05 14:47:41 -0800308 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000309
bsalomon71cb0c22014-11-14 12:10:14 -0800310 void setUnrefWhenDestroyed(TestResource* resource) {
311 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000312 }
313
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
315 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
316 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800317 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
318 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800319 }
320 }
321
322 static size_t ExpectedScratchKeySize() {
323 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
324 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000325private:
bsalomon24db3b12015-01-23 04:24:04 -0800326 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800327
kkinnunen2e6055b2016-04-22 01:48:29 -0700328 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
329 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700330 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800331 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700332 , fProperty(property)
333 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800334 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700335 this->registerWithCache(budgeted);
336 }
337
338 // Constructor for simulating resources that wrap backend objects.
339 TestResource(GrGpu* gpu, size_t size)
340 : INHERITED(gpu)
341 , fToDelete(nullptr)
342 , fSize(size)
343 , fProperty(kA_SimulatedProperty)
344 , fIsScratch(false) {
345 ++fNumAlive;
346 this->registerWithCacheWrapped();
347 }
348
349 void computeScratchKey(GrScratchKey* key) const override {
350 if (fIsScratch) {
351 ComputeScratchKey(fProperty, key);
352 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800353 }
354
mtklein36352bf2015-03-25 18:17:31 -0700355 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800356
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000357 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000358 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800359 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800360 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700361 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700362 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000363};
bsalomon33435572014-11-05 14:47:41 -0800364int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000365
bsalomonc2f35b72015-01-23 07:19:22 -0800366class Mock {
367public:
368 Mock(int maxCnt, size_t maxBytes) {
369 fContext.reset(GrContext::CreateMockContext());
370 SkASSERT(fContext);
371 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800372 GrResourceCache* cache = fContext->getResourceCache();
373 cache->purgeAllUnlocked();
374 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800375 }
bsalomonc2f35b72015-01-23 07:19:22 -0800376
bsalomon0ea80f42015-02-11 10:49:59 -0800377 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800378
Hal Canary342b7ac2016-11-04 11:49:42 -0400379 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800380
381private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400382 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800383};
384
385static void test_no_key(skiatest::Reporter* reporter) {
386 Mock mock(10, 30000);
387 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800388 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800389
390 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700391 TestResource* a = new TestResource(context->getGpu());
392 TestResource* b = new TestResource(context->getGpu());
393 TestResource* c = new TestResource(context->getGpu());
394 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800395 a->setSize(11);
396 b->setSize(12);
397 c->setSize(13);
398 d->setSize(14);
399
400 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800401 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800402 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800403 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404
405 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800406 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800407 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
408
bsalomon8718aaf2015-02-19 07:24:21 -0800409 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 a->unref();
412 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800413 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800414 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800415 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800416
417 c->unref();
418 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800419 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800420 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800421 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800422
423 d->unref();
424 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800425 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
426 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800427
428 b->unref();
429 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800430 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
431 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800432}
433
bsalomon24db3b12015-01-23 04:24:04 -0800434// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500435template <int>
436static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800437 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500438 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800439 builder[0] = data;
440}
441
bsalomon84c8e622014-11-17 09:33:27 -0800442static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800443 Mock mock(10, 300);
444 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800445 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800446
bsalomon8718aaf2015-02-19 07:24:21 -0800447 GrUniqueKey uniqueKey;
448 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800449
bsalomon8718aaf2015-02-19 07:24:21 -0800450 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800451 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700452 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800453 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700454 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800455 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800456 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700457 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800458 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700459 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700460 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800461 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800462
bsalomon8718aaf2015-02-19 07:24:21 -0800463 // Make sure we can't add a unique key to the wrapped resource
464 GrUniqueKey uniqueKey2;
465 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800466 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
halcanary96fcdcc2015-08-27 07:41:13 -0700467 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800468
469 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800470 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800471 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800472 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800473 cache->getResourceBytes());
474 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800475 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800476 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800477
bsalomon63c992f2015-01-23 12:47:59 -0800478 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800479 cache->purgeAllUnlocked();
480 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800481 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800482 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800483 cache->getResourceBytes());
484 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800485 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800486 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800487
488 // Unreffing the wrapped resource should free it right away.
489 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800490 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800491 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800492 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800493
bsalomon84c8e622014-11-17 09:33:27 -0800494 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700495 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800496 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800497 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800498 cache->purgeAllUnlocked();
499 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800500 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800501 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
502 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
503 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800504
505 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800506 cache->purgeAllUnlocked();
507 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800508 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800509 cache->getResourceBytes());
510 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
511 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800512
513 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800514 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
515 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
516 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
517 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800518
519 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800520 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
521 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
522 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
523 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800524}
525
bsalomon5236cf42015-01-14 10:42:08 -0800526static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800527 Mock mock(10, 30000);
528 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800529 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800530
bsalomon8718aaf2015-02-19 07:24:21 -0800531 GrUniqueKey uniqueKey;
532 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800533
534 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800535 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800536 TestResource* wrapped;
537 TestResource* unbudgeted;
538
539 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700540 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
541 TestResource::kB_SimulatedProperty);
542
bsalomon5236cf42015-01-14 10:42:08 -0800543 scratch->setSize(10);
544 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800545 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
546 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
547 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
548 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800549
halcanary385fe4d2015-08-26 13:07:48 -0700550 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800551 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800552 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800553 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800554 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
555 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
556 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
557 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800558
bsalomon0ea80f42015-02-11 10:49:59 -0800559 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700560 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800561 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
562 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
563 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
564 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800565
566 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800567 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
568 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
569 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
570 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800571
kkinnunen2e6055b2016-04-22 01:48:29 -0700572 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800573 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
574 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
575 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
576 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800577
578 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800579 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
580 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
581 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
582 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800583
bsalomon0ea80f42015-02-11 10:49:59 -0800584 cache->purgeAllUnlocked();
585 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
586 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
587 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
588 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800589}
590
bsalomon3582d3e2015-02-13 14:20:05 -0800591// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
592void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
593/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800594 Mock mock(10, 300);
595 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800596 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800597
598 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700599 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
600 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800601 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800602 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800603
604 size_t size = resource->gpuMemorySize();
605 for (int i = 0; i < 2; ++i) {
606 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800607 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800608 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800609 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700610 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800611 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
612 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
614 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800615
616 // Once it is unrefed, it should become available as scratch.
617 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800618 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
619 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
620 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
621 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700622 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800623 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800624 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800625 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800626 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800627
628 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700629 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800630 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800631 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800632 } else {
633 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800634 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
636 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
637 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
638 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800639 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
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 // now when it is unrefed it should die since it has no key.
644 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800645 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
646 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
647 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
648 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800649 }
bsalomon8b79d232014-11-10 10:19:06 -0800650 }
bsalomonc2f35b72015-01-23 07:19:22 -0800651}
652
653static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
654 Mock mock(5, 30000);
655 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800656 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800657
bsalomon8b79d232014-11-10 10:19:06 -0800658 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800659 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700660 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800661 TestResource::kB_SimulatedProperty);
662 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700663 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800664 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800665 a->setSize(11);
666 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800667 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800668 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800669 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700670 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800671
672 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800673 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800674
bsalomon0ea80f42015-02-11 10:49:59 -0800675 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800676 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800677 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
678 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800679 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800680 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800681
bsalomon63c992f2015-01-23 12:47:59 -0800682 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800683 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800684 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800685 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800686
687 // Unref but don't purge
688 a->unref();
689 b->unref();
690 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800691 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800692
bsalomon63c992f2015-01-23 12:47:59 -0800693 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800694 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800695 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800696 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
697 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800698}
699
bsalomon10e23ca2014-11-25 05:52:06 -0800700static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800701 Mock mock(5, 30000);
702 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800703 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800704
bsalomon10e23ca2014-11-25 05:52:06 -0800705 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700706 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800707 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700708 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800709 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800710 a->unref();
711 b->unref();
712
bsalomon1c60dfe2015-01-21 09:32:40 -0800713 GrScratchKey scratchKey;
714 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800715 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800716 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700717 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800718
bsalomon0ea80f42015-02-11 10:49:59 -0800719 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800720 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800721 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800722 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
723 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800724
725 // Find the first resource and remove its scratch key
726 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700727 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800728 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800729 // It's still alive, but not cached by scratch key anymore
730 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800731 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
732 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800733
734 // The cache should immediately delete it when it's unrefed since it isn't accessible.
735 find->unref();
736 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800737 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
738 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800739
740 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700741 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800742 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800743 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800744 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
745 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800746
747 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800748 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800749 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800750 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
751 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800752
753 find->unref();
754 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800755 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
756 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800757}
758
bsalomon1c60dfe2015-01-21 09:32:40 -0800759static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800760 Mock mock(5, 30000);
761 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800762 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800763
764 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700765 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800766 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700767 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800768 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800769 a->unref();
770 b->unref();
771
772 GrScratchKey scratchKey;
773 // Ensure that scratch key comparison and assignment is consistent.
774 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800775 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800776 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800777 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800778 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
779 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
780 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
781 scratchKey = scratchKey1;
782 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
783 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
784 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
785 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
786 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
787 scratchKey = scratchKey2;
788 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
789 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
790 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
791 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
792 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
793
794 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800795 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800796 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700797 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800798
799 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800800 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700801 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700802 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800803 find->unref();
804
805 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700806 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700807 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800808 REPORTER_ASSERT(reporter, find == a || find == b);
809
robertphillips6e83ac72015-08-13 05:19:14 -0700810 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700811 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800812 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
813 REPORTER_ASSERT(reporter, find2 != find);
814 find2->unref();
815 find->unref();
816}
817
bsalomon8718aaf2015-02-19 07:24:21 -0800818static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800819 Mock mock(5, 30000);
820 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800821 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000822
bsalomon8718aaf2015-02-19 07:24:21 -0800823 GrUniqueKey key;
824 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700825
bsalomon8718aaf2015-02-19 07:24:21 -0800826 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700827 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800828 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700829
bsalomonf99e9612015-02-19 08:24:16 -0800830 // Set key on resource a.
831 a->resourcePriv().setUniqueKey(key);
832 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
833 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800834
bsalomonf99e9612015-02-19 08:24:16 -0800835 // Make sure that redundantly setting a's key works.
836 a->resourcePriv().setUniqueKey(key);
837 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800838 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800839 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
840 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800841 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
842
bsalomonf99e9612015-02-19 08:24:16 -0800843 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700844 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800845 b->setSize(12);
846 b->resourcePriv().setUniqueKey(key);
847 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
848 b->unref();
849
850 // Still have two resources because a is still reffed.
851 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
852 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
853 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
854
855 a->unref();
856 // Now a should be gone.
857 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
859 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
860
861 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
862 // Also make b be unreffed when replacement occurs.
863 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700864 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800865 GrUniqueKey differentKey;
866 make_unique_key<0>(&differentKey, 1);
867 c->setSize(13);
868 c->resourcePriv().setUniqueKey(differentKey);
869 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
870 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
871 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
872 // c replaces b and b should be immediately purged.
873 c->resourcePriv().setUniqueKey(key);
874 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
875 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
876 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
877
878 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800879 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800880 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
881 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
882 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
883
884 // Drop the ref on c, it should be kept alive because it has a unique key.
885 c->unref();
886 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
887 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
888 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
889
890 // Verify that we can find c, then remove its unique key. It should get purged immediately.
891 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
892 c->resourcePriv().removeUniqueKey();
893 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800894 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
895 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800896 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700897
898 {
899 GrUniqueKey key2;
900 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400901 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700902 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700903 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700904 d->resourcePriv().setUniqueKey(key2);
905 }
906
907 GrUniqueKey key3;
908 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400909 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700910 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000911}
912
bsalomon8b79d232014-11-10 10:19:06 -0800913static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800914 Mock mock(5, 30000);
915 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800916 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800917
bsalomon8718aaf2015-02-19 07:24:21 -0800918 GrUniqueKey key1, key2, key3;
919 make_unique_key<0>(&key1, 1);
920 make_unique_key<0>(&key2, 2);
921 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700922
bsalomon23e619c2015-02-06 11:54:28 -0800923 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700924 TestResource* a = new TestResource(context->getGpu());
925 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700926 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800927 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800928 a->resourcePriv().setUniqueKey(key1);
929 b->resourcePriv().setUniqueKey(key2);
930 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800931 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800932 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800933 c->unref();
934
bsalomon8718aaf2015-02-19 07:24:21 -0800935 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
936 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
937 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800938 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800939
bsalomon8718aaf2015-02-19 07:24:21 -0800940 typedef GrUniqueKeyInvalidatedMessage Msg;
941 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800942
943 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
944 Bus::Post(Msg(key1));
945 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800946 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800947 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800948 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
949 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800950 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800951 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800952
953 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800954 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800955 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800956 // we still have a ref on b, c should be recycled as scratch.
957 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800958 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800959
bsalomon23e619c2015-02-06 11:54:28 -0800960 // make b purgeable. It should be immediately deleted since it has no key.
961 b->unref();
962 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
963
964 // Make sure we actually get to c via it's scratch key, before we say goodbye.
965 GrScratchKey scratchKey;
966 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700967 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800968 REPORTER_ASSERT(reporter, scratch == c);
969 SkSafeUnref(scratch);
970
971 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800972 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700973 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800974 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800975 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
976 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800977 REPORTER_ASSERT(reporter, !scratch);
978 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800979}
980
bsalomon71cb0c22014-11-14 12:10:14 -0800981static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800982 Mock mock(3, 30000);
983 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800984 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800985
bsalomon8718aaf2015-02-19 07:24:21 -0800986 GrUniqueKey key1, key2;
987 make_unique_key<0>(&key1, 1);
988 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000989
halcanary385fe4d2015-08-26 13:07:48 -0700990 TestResource* a = new TestResource(context->getGpu());
991 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800992 a->resourcePriv().setUniqueKey(key1);
993 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800994
bsalomonc2f35b72015-01-23 07:19:22 -0800995 // Make a cycle
996 a->setUnrefWhenDestroyed(b);
997 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800998
bsalomonc2f35b72015-01-23 07:19:22 -0800999 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001000
bsalomonc2f35b72015-01-23 07:19:22 -08001001 a->unref();
1002 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001003
bsalomonc2f35b72015-01-23 07:19:22 -08001004 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001005
bsalomon0ea80f42015-02-11 10:49:59 -08001006 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001007 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001008
bsalomonc2f35b72015-01-23 07:19:22 -08001009 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001010 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001011 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001012
bsalomon0ea80f42015-02-11 10:49:59 -08001013 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001014 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001015}
1016
bsalomon8b79d232014-11-10 10:19:06 -08001017static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001018 GrUniqueKey key1, key2;
1019 make_unique_key<0>(&key1, 1);
1020 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001021
1022 // Test changing resources sizes (both increase & decrease).
1023 {
bsalomonc2f35b72015-01-23 07:19:22 -08001024 Mock mock(3, 30000);
1025 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001026 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001027
halcanary385fe4d2015-08-26 13:07:48 -07001028 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001029 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001030 a->unref();
1031
halcanary385fe4d2015-08-26 13:07:48 -07001032 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001033 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001034 b->unref();
1035
bsalomon0ea80f42015-02-11 10:49:59 -08001036 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1037 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001038 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001039 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001040 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001041 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001042 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001043 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001044 find1->setSize(50);
1045 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001046
bsalomon0ea80f42015-02-11 10:49:59 -08001047 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1048 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001049 }
1050
1051 // Test increasing a resources size beyond the cache budget.
1052 {
bsalomonc2f35b72015-01-23 07:19:22 -08001053 Mock mock(2, 300);
1054 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001055 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001056
halcanary385fe4d2015-08-26 13:07:48 -07001057 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001058 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001059 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001060 a->unref();
1061
halcanary385fe4d2015-08-26 13:07:48 -07001062 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001063 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001064 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001065 b->unref();
1066
bsalomon0ea80f42015-02-11 10:49:59 -08001067 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1068 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001069
bsalomon8b79d232014-11-10 10:19:06 -08001070 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001071 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001072 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001073 find2->setSize(201);
1074 }
bsalomon8718aaf2015-02-19 07:24:21 -08001075 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001076
bsalomon0ea80f42015-02-11 10:49:59 -08001077 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1078 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001079 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001080}
1081
bsalomonddf30e62015-02-19 11:38:44 -08001082static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1083 static const int kCount = 50;
1084 static const int kBudgetCnt = kCount / 2;
1085 static const int kLockedFreq = 8;
1086 static const int kBudgetSize = 0x80000000;
1087
1088 SkRandom random;
1089
1090 // Run the test 2*kCount times;
1091 for (int i = 0; i < 2 * kCount; ++i ) {
1092 Mock mock(kBudgetCnt, kBudgetSize);
1093 GrContext* context = mock.context();
1094 GrResourceCache* cache = mock.cache();
1095
1096 // Pick a random number of resources to add before the timestamp will wrap.
1097 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1098
1099 static const int kNumToPurge = kCount - kBudgetCnt;
1100
1101 SkTDArray<int> shouldPurgeIdxs;
1102 int purgeableCnt = 0;
1103 SkTDArray<GrGpuResource*> resourcesToUnref;
1104
1105 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1106 // unpurgeable resources.
1107 for (int j = 0; j < kCount; ++j) {
1108 GrUniqueKey key;
1109 make_unique_key<0>(&key, j);
1110
halcanary385fe4d2015-08-26 13:07:48 -07001111 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001112 r->resourcePriv().setUniqueKey(key);
1113 if (random.nextU() % kLockedFreq) {
1114 // Make this is purgeable.
1115 r->unref();
1116 ++purgeableCnt;
1117 if (purgeableCnt <= kNumToPurge) {
1118 *shouldPurgeIdxs.append() = j;
1119 }
1120 } else {
1121 *resourcesToUnref.append() = r;
1122 }
1123 }
1124
1125 // Verify that the correct resources were purged.
1126 int currShouldPurgeIdx = 0;
1127 for (int j = 0; j < kCount; ++j) {
1128 GrUniqueKey key;
1129 make_unique_key<0>(&key, j);
1130 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1131 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1132 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1133 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001134 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001135 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001136 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001137 }
1138 SkSafeUnref(res);
1139 }
1140
1141 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1142 resourcesToUnref[j]->unref();
1143 }
1144 }
1145}
1146
bsalomon3f324322015-04-08 11:01:54 -07001147static void test_flush(skiatest::Reporter* reporter) {
1148 Mock mock(1000000, 1000000);
1149 GrContext* context = mock.context();
1150 GrResourceCache* cache = mock.cache();
1151
1152 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1153 // power of two here to keep things simpler.
1154 static const int kFlushCount = 16;
1155 cache->setLimits(1000000, 1000000, kFlushCount);
1156
1157 {
1158 // Insert a resource and send a flush notification kFlushCount times.
1159 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001160 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001161 GrUniqueKey k;
1162 make_unique_key<1>(&k, i);
1163 r->resourcePriv().setUniqueKey(k);
1164 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001165 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001166 }
1167
1168 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001169 for (int i = 0; i < kFlushCount; ++i) {
1170 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001171 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1172 for (int j = 0; j < i; ++j) {
1173 GrUniqueKey k;
1174 make_unique_key<1>(&k, j);
1175 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1176 REPORTER_ASSERT(reporter, !SkToBool(r));
1177 SkSafeUnref(r);
1178 }
bsalomon3f324322015-04-08 11:01:54 -07001179 }
1180
1181 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1182 cache->purgeAllUnlocked();
1183 }
1184
1185 // Do a similar test but where we leave refs on some resources to prevent them from being
1186 // purged.
1187 {
1188 GrGpuResource* refedResources[kFlushCount >> 1];
1189 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001190 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001191 GrUniqueKey k;
1192 make_unique_key<1>(&k, i);
1193 r->resourcePriv().setUniqueKey(k);
1194 // Leave a ref on every other resource, beginning with the first.
1195 if (SkToBool(i & 0x1)) {
1196 refedResources[i/2] = r;
1197 } else {
1198 r->unref();
1199 }
bsalomonb77a9072016-09-07 10:02:04 -07001200 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001201 }
1202
1203 for (int i = 0; i < kFlushCount; ++i) {
1204 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001205 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001206 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001207 }
1208
1209 // Unref all the resources that we kept refs on in the first loop.
1210 for (int i = 0; i < kFlushCount >> 1; ++i) {
1211 refedResources[i]->unref();
1212 }
1213
bsalomone2e87f32016-09-22 12:42:11 -07001214 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1215 // kFlushCount full flushes.
1216 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001217 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001218 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001219 }
1220 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1221
1222 cache->purgeAllUnlocked();
1223 }
1224
1225 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001226
1227 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1228 // eviction.
1229 context->flush();
1230 for (int i = 0; i < 10; ++i) {
1231 TestResource* r = new TestResource(context->getGpu());
1232 GrUniqueKey k;
1233 make_unique_key<1>(&k, i);
1234 r->resourcePriv().setUniqueKey(k);
1235 r->unref();
1236 }
1237 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1238 for (int i = 0; i < 10 * kFlushCount; ++i) {
1239 context->flush();
1240 }
1241 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001242}
1243
bsalomon10e23ca2014-11-25 05:52:06 -08001244static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001245 // Set the cache size to double the resource count because we're going to create 2x that number
1246 // resources, using two different key domains. Add a little slop to the bytes because we resize
1247 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001248 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001249
bsalomonc2f35b72015-01-23 07:19:22 -08001250 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1251 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001252 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001253
1254 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001255 GrUniqueKey key1, key2;
1256 make_unique_key<1>(&key1, i);
1257 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001258
bsalomon24db3b12015-01-23 04:24:04 -08001259 TestResource* resource;
1260
halcanary385fe4d2015-08-26 13:07:48 -07001261 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001262 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001263 resource->setSize(1);
1264 resource->unref();
1265
halcanary385fe4d2015-08-26 13:07:48 -07001266 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001267 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001268 resource->setSize(1);
1269 resource->unref();
1270 }
1271
1272 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001273 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1274 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1275 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1276 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001277 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001278 GrUniqueKey key1, key2;
1279 make_unique_key<1>(&key1, i);
1280 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001281
bsalomon8718aaf2015-02-19 07:24:21 -08001282 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1283 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001284 }
1285
bsalomon0ea80f42015-02-11 10:49:59 -08001286 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001287 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001288 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1289 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1290 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1291 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001292
1293 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001294 GrUniqueKey key1, key2;
1295 make_unique_key<1>(&key1, i);
1296 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001297
bsalomon8718aaf2015-02-19 07:24:21 -08001298 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1299 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001300 }
1301}
1302
senorblanco84cd6212015-08-04 10:01:58 -07001303static void test_custom_data(skiatest::Reporter* reporter) {
1304 GrUniqueKey key1, key2;
1305 make_unique_key<0>(&key1, 1);
1306 make_unique_key<0>(&key2, 2);
1307 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001308 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001309 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1310 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1311
1312 // Test that copying a key also takes a ref on its custom data.
1313 GrUniqueKey key3 = key1;
1314 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1315}
1316
bsalomonc6363ef2015-09-24 07:07:40 -07001317static void test_abandoned(skiatest::Reporter* reporter) {
1318 Mock mock(10, 300);
1319 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001320 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001321 context->abandonContext();
1322
1323 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1324
1325 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1326
robertphillips8abb3702016-08-31 14:04:06 -07001327 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001328 resource->getUniqueKey();
1329 resource->wasDestroyed();
1330 resource->gpuMemorySize();
1331 resource->getContext();
1332
1333 resource->abandon();
1334 resource->resourcePriv().getScratchKey();
1335 resource->resourcePriv().isBudgeted();
1336 resource->resourcePriv().makeBudgeted();
1337 resource->resourcePriv().makeUnbudgeted();
1338 resource->resourcePriv().removeScratchKey();
1339 GrUniqueKey key;
1340 make_unique_key<0>(&key, 1);
1341 resource->resourcePriv().setUniqueKey(key);
1342 resource->resourcePriv().removeUniqueKey();
1343}
1344
Brian Salomon1090da62017-01-06 12:04:19 -05001345static void test_tags(skiatest::Reporter* reporter) {
1346#ifdef SK_DEBUG
1347 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1348 static constexpr int kLastTagIdx = 10;
1349 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1350
1351 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1352 GrContext* context = mock.context();
1353 GrResourceCache* cache = mock.cache();
1354
1355 SkString tagStr;
1356 int tagIdx = 0;
1357 int currTagCnt = 0;
1358
1359 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1360 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1361 GrUniqueKey key;
1362 if (currTagCnt == tagIdx) {
1363 tagIdx += 1;
1364 currTagCnt = 0;
1365 tagStr.printf("tag%d", tagIdx);
1366 }
1367 make_unique_key<1>(&key, i, tagStr.c_str());
1368 resource->resourcePriv().setUniqueKey(key);
1369 }
1370 SkASSERT(kLastTagIdx == tagIdx);
1371 SkASSERT(currTagCnt == kLastTagIdx);
1372
1373 // Test i = 0 to exercise unused tag string.
1374 for (int i = 0; i <= kLastTagIdx; ++i) {
1375 tagStr.printf("tag%d", i);
1376 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1377 }
1378#endif
1379}
1380
kkinnunen15302832015-12-01 04:35:26 -08001381DEF_GPUTEST(ResourceCacheMisc, reporter, factory) {
bsalomon8b79d232014-11-10 10:19:06 -08001382 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001383 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001384 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001385 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001386 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001387 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001388 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001389 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001390 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001391 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001392 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001393 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001394 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001395 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001396 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001397 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001398 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001399 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001400}
1401
Robert Phillipsd6214d42016-11-07 08:23:48 -05001402////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001403static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001404 GrSurfaceFlags flags,
1405 int width, int height,
1406 int sampleCnt) {
1407 GrSurfaceDesc desc;
1408 desc.fFlags = flags;
1409 desc.fWidth = width;
1410 desc.fHeight = height;
1411 desc.fConfig = kRGBA_8888_GrPixelConfig;
1412 desc.fSampleCnt = sampleCnt;
1413
1414 return sk_sp<GrTexture>(provider->createTexture(desc, SkBudgeted::kYes));
1415}
1416
Brian Osman32342f02017-03-04 08:12:46 -05001417static sk_sp<GrTexture> make_mipmap_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001418 GrSurfaceFlags flags,
1419 int width, int height,
1420 int sampleCnt) {
1421 SkBitmap bm;
1422
1423 bm.allocN32Pixels(width, height, true);
1424 bm.eraseColor(SK_ColorBLUE);
1425
Brian Osman7b8400d2016-11-08 17:08:54 -05001426 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001427 SkASSERT(mipmaps);
1428 SkASSERT(mipmaps->countLevels() > 1);
1429
1430 int mipLevelCount = mipmaps->countLevels() + 1;
1431
1432 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1433
1434 texels[0].fPixels = bm.getPixels();
1435 texels[0].fRowBytes = bm.rowBytes();
1436
1437 for (int i = 1; i < mipLevelCount; ++i) {
1438 SkMipMap::Level generatedMipLevel;
1439 mipmaps->getLevel(i - 1, &generatedMipLevel);
1440 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1441 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1442 }
1443
1444 GrSurfaceDesc desc;
1445 desc.fFlags = flags;
1446 desc.fWidth = width;
1447 desc.fHeight = height;
1448 desc.fConfig = kRGBA_8888_GrPixelConfig;
1449 desc.fSampleCnt = sampleCnt;
1450 desc.fIsMipMapped = true;
1451
1452 return sk_sp<GrTexture>(provider->createMipMappedTexture(desc, SkBudgeted::kYes,
1453 texels.get(), mipLevelCount));
1454}
1455
1456// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1457// Texture-only, both-RT-and-Texture and MIPmapped
1458DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1459 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001460 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001461
Robert Phillipsd6214d42016-11-07 08:23:48 -05001462 static const int kSize = 64;
1463
1464 sk_sp<GrTexture> tex;
1465
1466 // Normal versions
1467 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1468 size_t size = tex->gpuMemorySize();
1469 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1470
1471 if (context->caps()->maxSampleCount() >= 4) {
1472 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1473 size = tex->gpuMemorySize();
1474 REPORTER_ASSERT(reporter, kSize*kSize*4 == size || // msaa4 failed
1475 kSize*kSize*4*4 == size || // auto-resolving
1476 kSize*kSize*4*5 == size); // explicit resolve buffer
1477 }
1478
1479 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1480 size = tex->gpuMemorySize();
1481 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1482
1483 // Mipmapped versions
1484 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1485 size = tex->gpuMemorySize();
1486 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1487
1488 if (context->caps()->maxSampleCount() >= 4) {
1489 tex = make_mipmap_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 4);
1490 size = tex->gpuMemorySize();
1491 REPORTER_ASSERT(reporter,
1492 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1493 kSize*kSize*4*4+(kSize*kSize*4)/3 == size || // auto-resolving
1494 kSize*kSize*4*5+(kSize*kSize*4)/3 == size); // explicit resolve buffer
1495 }
1496
1497 tex = make_mipmap_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1498 size = tex->gpuMemorySize();
1499 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1500}
1501
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001502#endif