blob: bd989cd2a93d91731dde5b259de616bf4c241869 [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 "SkTypes.h"
9
Brian Salomon5e150852017-03-22 14:53:13 -040010#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070011#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000013#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070014#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourceCacheAccess.h"
16#include "GrGpuResourcePriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050017#include "GrProxyProvider.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040018#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080019#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070020#include "GrResourceProvider.h"
Robert Phillips646e4292017-06-13 12:44:56 -040021#include "GrTexture.h"
22
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);
Mike Reedf0ffb892017-10-03 14:47:21 -040050 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000051
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);
Mike Reedf1942192017-07-21 14:24:29 -040069 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000070
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
Robert Phillipsc0192e32017-09-21 12:00:26 -040092static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
93 return rt->renderTargetPriv().getStencilAttachment();
94}
95
96static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
97 int size, int sampleCount, SkBudgeted budgeted) {
98 GrSurfaceDesc desc;
99 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc0192e32017-09-21 12:00:26 -0400100 desc.fWidth = size;
101 desc.fHeight = size;
102 desc.fConfig = kRGBA_8888_GrPixelConfig;
103 desc.fSampleCnt = sampleCount;
104
105 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
106 if (!tex || !tex->asRenderTarget()) {
107 return nullptr;
108 }
109
110 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
111 return nullptr;
112 }
113 SkASSERT(get_SB(tex->asRenderTarget()));
114
115 return sk_ref_sp(tex->asRenderTarget());
116}
117
bsalomon11abd8d2016-10-14 08:13:48 -0700118// This currently fails on ES3 ANGLE contexts
119DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000120 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700121 GrContext* context = ctxInfo.grContext();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400122 if (context->contextPriv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700123 return;
124 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400125
Robert Phillips6be756b2018-01-16 15:07:54 -0500126 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsc0192e32017-09-21 12:00:26 -0400127
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400129 REPORTER_ASSERT(reporter, smallRT0);
130
131 {
132 // Two budgeted RTs with the same desc should share a stencil buffer.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kYes);
134 REPORTER_ASSERT(reporter, smallRT1);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400135
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800137 }
138
Robert Phillipsc0192e32017-09-21 12:00:26 -0400139 {
140 // An unbudgeted RT with the same desc should also share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(resourceProvider, 4, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400142 REPORTER_ASSERT(reporter, smallRT2);
143
144 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800145 }
146
Robert Phillipsc0192e32017-09-21 12:00:26 -0400147 {
148 // An RT with a much larger size should not share.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500149 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(resourceProvider, 400, 1, SkBudgeted::kNo);
Robert Phillipsc0192e32017-09-21 12:00:26 -0400150 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800151
Robert Phillipsc0192e32017-09-21 12:00:26 -0400152 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800153 }
bsalomon02a44a42015-02-19 09:09:00 -0800154
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400155 int smallSampleCount =
156 context->contextPriv().caps()->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500157 if (smallSampleCount > 1) {
mtklein5f939ab2016-03-16 10:28:35 -0700158 // An RT with a different sample count should not share.
Robert Phillips6be756b2018-01-16 15:07:54 -0500159 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(resourceProvider, 4,
160 smallSampleCount, SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800161#ifdef SK_BUILD_FOR_ANDROID
162 if (!smallMSAART0) {
163 // The nexus player seems to fail to create MSAA textures.
164 return;
165 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400166#else
167 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800168#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400169
170 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
171
172 {
173 // A second MSAA RT should share with the first MSAA RT.
Robert Phillips6be756b2018-01-16 15:07:54 -0500174 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(resourceProvider, 4,
175 smallSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400176 SkBudgeted::kNo);
177 REPORTER_ASSERT(reporter, smallMSAART1);
178
179 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800180 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400181
Brian Salomonbdecacf2018-02-02 20:32:49 -0500182 // But one with a larger sample count should not. (Also check that the two requests didn't
183 // rounded up to the same actual sample count or else they could share.).
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400184 int bigSampleCount = context->contextPriv().caps()->getRenderTargetSampleCount(
185 5, kRGBA_8888_GrPixelConfig);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500186 if (bigSampleCount > 0 && bigSampleCount != smallSampleCount) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500187 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(resourceProvider, 4,
188 bigSampleCount,
Robert Phillipsc0192e32017-09-21 12:00:26 -0400189 SkBudgeted::kNo);
190 REPORTER_ASSERT(reporter, smallMSAART2);
191
192 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800193 }
194 }
195}
196
bsalomon68d91342016-04-12 09:59:58 -0700197DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700198 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500199 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500200 GrGpu* gpu = context->contextPriv().getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700201 // this test is only valid for GL
202 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700203 return;
204 }
205
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500206 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700207 static const int kW = 100;
208 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700209
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500210 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
211 kRGBA_8888_GrPixelConfig,
212 false, GrMipMapped::kNo);
213 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
214 kRGBA_8888_GrPixelConfig,
215 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500216 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
217 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
218 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
219 return;
220 }
jvanverth672bb7f2015-07-13 07:19:57 -0700221
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222 context->resetContext();
223
Robert Phillips6be756b2018-01-16 15:07:54 -0500224 sk_sp<GrTexture> borrowed(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500225 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700226
Robert Phillips6be756b2018-01-16 15:07:54 -0500227 sk_sp<GrTexture> adopted(resourceProvider->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700229
Brian Osman85d34b22017-05-10 12:06:26 -0400230 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
231 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232 return;
233 }
234
halcanary96fcdcc2015-08-27 07:41:13 -0700235 borrowed.reset(nullptr);
236 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700237
238 context->flush();
239
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500240 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
241 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700242
243 REPORTER_ASSERT(reporter, borrowedIsAlive);
244 REPORTER_ASSERT(reporter, !adoptedIsAlive);
245
Brian Salomone64b0642018-03-07 11:47:54 -0500246 if (borrowedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500247 gpu->deleteTestingOnlyBackendTexture(backendTextures[0]);
Brian Salomone64b0642018-03-07 11:47:54 -0500248 }
249 if (adoptedIsAlive) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500250 gpu->deleteTestingOnlyBackendTexture(backendTextures[1]);
Brian Salomone64b0642018-03-07 11:47:54 -0500251 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700252
253 context->resetContext();
254}
255
bsalomon6d3fe022014-07-25 08:35:45 -0700256class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000258public:
robertphillips6e83ac72015-08-13 05:19:14 -0700259 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700260
bsalomon1c60dfe2015-01-21 09:32:40 -0800261 /** Property that distinctly categorizes the resource.
262 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800263 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800264
kkinnunen2e6055b2016-04-22 01:48:29 -0700265 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
266 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700267 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800268 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700269 , fProperty(kA_SimulatedProperty)
270 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800271 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700272 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800273 }
274
kkinnunen2e6055b2016-04-22 01:48:29 -0700275 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
Greg Danielda86e282018-06-13 09:41:19 -0400276 SimulatedProperty property, size_t size = kDefaultSize) {
277 return new TestResource(gpu, budgeted, property, kScratchConstructor, size);
bsalomondace19e2014-11-17 07:34:06 -0800278 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700279 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
280 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
Brian Salomond3b65972017-03-22 12:05:03 -0400283 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800284 --fNumAlive;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000285 }
286
bsalomon33435572014-11-05 14:47:41 -0800287 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000288
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400289 void setUnrefWhenDestroyed(sk_sp<TestResource> resource) {
290 fToDelete = std::move(resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000291 }
292
bsalomon1c60dfe2015-01-21 09:32:40 -0800293 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
294 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
295 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800296 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
297 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800298 }
299 }
300
301 static size_t ExpectedScratchKeySize() {
302 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
303 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000304private:
bsalomon24db3b12015-01-23 04:24:04 -0800305 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800306
Greg Danielda86e282018-06-13 09:41:19 -0400307 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor,
308 size_t size = kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700309 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700310 , fToDelete(nullptr)
Greg Danielda86e282018-06-13 09:41:19 -0400311 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700312 , fProperty(property)
313 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800314 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700315 this->registerWithCache(budgeted);
316 }
317
318 // Constructor for simulating resources that wrap backend objects.
319 TestResource(GrGpu* gpu, size_t size)
320 : INHERITED(gpu)
321 , fToDelete(nullptr)
322 , fSize(size)
323 , fProperty(kA_SimulatedProperty)
324 , fIsScratch(false) {
325 ++fNumAlive;
326 this->registerWithCacheWrapped();
327 }
328
329 void computeScratchKey(GrScratchKey* key) const override {
330 if (fIsScratch) {
331 ComputeScratchKey(fProperty, key);
332 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800333 }
334
mtklein36352bf2015-03-25 18:17:31 -0700335 size_t onGpuMemorySize() const override { return fSize; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400336 const char* getResourceType() const override { return "Test"; }
bsalomon69ed47f2014-11-12 11:13:39 -0800337
Ben Wagner97c6a0e2018-07-11 14:56:22 -0400338 sk_sp<TestResource> fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000339 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800340 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800341 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700342 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700343 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000344};
bsalomon33435572014-11-05 14:47:41 -0800345int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000346
bsalomonc2f35b72015-01-23 07:19:22 -0800347class Mock {
348public:
349 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400350 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800351 SkASSERT(fContext);
352 fContext->setResourceCacheLimits(maxCnt, maxBytes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500353 GrResourceCache* cache = fContext->contextPriv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800354 cache->purgeAllUnlocked();
355 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800356 }
bsalomonc2f35b72015-01-23 07:19:22 -0800357
Robert Phillips6be756b2018-01-16 15:07:54 -0500358 GrResourceCache* cache() { return fContext->contextPriv().getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800359
Hal Canary342b7ac2016-11-04 11:49:42 -0400360 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800361
Greg Danielc27eb722018-08-10 09:48:08 -0400362 void reset() {
363 fContext.reset();
364 }
365
bsalomonc2f35b72015-01-23 07:19:22 -0800366private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400367 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800368};
369
370static void test_no_key(skiatest::Reporter* reporter) {
371 Mock mock(10, 30000);
372 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800373 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500374 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon71cb0c22014-11-14 12:10:14 -0800375
376 // Create a bunch of resources with no keys
Greg Danielda86e282018-06-13 09:41:19 -0400377 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
378 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
379 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13 );
380 TestResource* d = new TestResource(gpu, SkBudgeted::kYes, 14 );
bsalomon71cb0c22014-11-14 12:10:14 -0800381
382 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800383 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800384 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800385 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386
387 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800388 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800389 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
390
bsalomon8718aaf2015-02-19 07:24:21 -0800391 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800392
393 a->unref();
394 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800395 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800396 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800397 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800398
399 c->unref();
400 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800401 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800402 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800403 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800404
405 d->unref();
406 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800407 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
408 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800409
410 b->unref();
411 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
413 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800414}
415
bsalomon24db3b12015-01-23 04:24:04 -0800416// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500417template <int>
418static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800419 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500420 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800421 builder[0] = data;
422}
423
Robert Phillips6eba0632018-03-28 12:25:42 -0400424static void test_purge_unlocked(skiatest::Reporter* reporter) {
425 Mock mock(10, 30000);
426 GrContext* context = mock.context();
427 GrResourceCache* cache = mock.cache();
428 GrGpu* gpu = context->contextPriv().getGpu();
429
430 // Create two resource w/ a unique key and two w/o but all of which have scratch keys.
431 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400432 TestResource::kA_SimulatedProperty, 11);
Robert Phillips6eba0632018-03-28 12:25:42 -0400433
434 GrUniqueKey uniqueKey;
435 make_unique_key<0>(&uniqueKey, 0);
436
437 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400438 TestResource::kA_SimulatedProperty, 12);
Robert Phillips6eba0632018-03-28 12:25:42 -0400439 b->resourcePriv().setUniqueKey(uniqueKey);
440
441 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400442 TestResource::kA_SimulatedProperty, 13);
Robert Phillips6eba0632018-03-28 12:25:42 -0400443
444 GrUniqueKey uniqueKey2;
445 make_unique_key<0>(&uniqueKey2, 1);
446
447 TestResource* d = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400448 TestResource::kA_SimulatedProperty, 14);
Robert Phillips6eba0632018-03-28 12:25:42 -0400449 d->resourcePriv().setUniqueKey(uniqueKey2);
450
451
452 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
453 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
454 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
455 d->gpuMemorySize() == cache->getResourceBytes());
456
457 // Should be safe to purge without deleting the resources since we still have refs.
458 cache->purgeUnlockedResources(false);
459 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
460
461 // Unref them all. Since they all have keys they should remain in the cache.
462
463 a->unref();
464 b->unref();
465 c->unref();
466 d->unref();
467 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
468 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
469 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
470 d->gpuMemorySize() == cache->getResourceBytes());
471
472 // Purge only the two scratch resources
473 cache->purgeUnlockedResources(true);
474
475 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
476 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
477 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
478 cache->getResourceBytes());
479
480 // Purge the uniquely keyed resources
481 cache->purgeUnlockedResources(false);
482
483 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
484 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
485 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
486}
487
bsalomon84c8e622014-11-17 09:33:27 -0800488static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800489 Mock mock(10, 300);
490 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800491 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500492 GrGpu* gpu = context->contextPriv().getGpu();
bsalomondace19e2014-11-17 07:34:06 -0800493
bsalomon8718aaf2015-02-19 07:24:21 -0800494 GrUniqueKey uniqueKey;
495 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800496
bsalomon8718aaf2015-02-19 07:24:21 -0800497 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800498 TestResource* scratch =
Greg Danielda86e282018-06-13 09:41:19 -0400499 TestResource::CreateScratch(gpu, SkBudgeted::kYes, TestResource::kB_SimulatedProperty,
500 10);
501 TestResource* unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800502 unique->resourcePriv().setUniqueKey(uniqueKey);
Greg Danielda86e282018-06-13 09:41:19 -0400503 TestResource* wrapped = TestResource::CreateWrapped(gpu, 12);
504 TestResource* unbudgeted = new TestResource(gpu, SkBudgeted::kNo, 13);
bsalomondace19e2014-11-17 07:34:06 -0800505
Brian Osman0562eb92017-05-08 11:16:39 -0400506 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800507 GrUniqueKey uniqueKey2;
508 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800509 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400510 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
511 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
512
513 // Remove the extra ref we just added.
514 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800515
516 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800517 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800518 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800519 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800520 cache->getResourceBytes());
521 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800522 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800523 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400524 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800525
bsalomon63c992f2015-01-23 12:47:59 -0800526 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800527 cache->purgeAllUnlocked();
528 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800529 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800530 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800531 cache->getResourceBytes());
532 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800533 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800534 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400535 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800536
Greg Daniel303e83e2018-09-10 14:10:19 -0400537 // Unreffing the wrapped resource with a unique key shouldn't free it right away.
bsalomondace19e2014-11-17 07:34:06 -0800538 wrapped->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400539 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800540 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
Greg Daniel303e83e2018-09-10 14:10:19 -0400541 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
542 cache->getResourceBytes());
543 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800544
bsalomon84c8e622014-11-17 09:33:27 -0800545 // Now try freeing the budgeted resources first
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500546 wrapped = TestResource::CreateWrapped(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -0800547 unique->unref();
Greg Daniel303e83e2018-09-10 14:10:19 -0400548 REPORTER_ASSERT(reporter, 23 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800549 cache->purgeAllUnlocked();
550 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800551 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800552 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
553 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
554 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400555 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800556
557 scratch->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400558 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800559 cache->purgeAllUnlocked();
560 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800561 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800562 cache->getResourceBytes());
563 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
564 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400565 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800566
Greg Daniel303e83e2018-09-10 14:10:19 -0400567 // Unreffing the wrapped resource (with no unique key) should free it right away.
bsalomondace19e2014-11-17 07:34:06 -0800568 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
570 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
571 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
572 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400573 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800574
575 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400580 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800581}
582
bsalomon5236cf42015-01-14 10:42:08 -0800583static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800584 Mock mock(10, 30000);
585 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800586 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500587 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon5236cf42015-01-14 10:42:08 -0800588
bsalomon8718aaf2015-02-19 07:24:21 -0800589 GrUniqueKey uniqueKey;
590 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800591
592 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800593 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800594 TestResource* wrapped;
595 TestResource* unbudgeted;
596
597 // A large uncached or wrapped resource shouldn't evict anything.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500598 scratch = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400599 TestResource::kB_SimulatedProperty, 10);
kkinnunen2e6055b2016-04-22 01:48:29 -0700600
bsalomon5236cf42015-01-14 10:42:08 -0800601 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800602 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
603 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
604 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
605 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400606 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800607
Greg Danielda86e282018-06-13 09:41:19 -0400608 unique = new TestResource(gpu, SkBudgeted::kYes, 11);
bsalomonf99e9612015-02-19 08:24:16 -0800609 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800610 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800611 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
612 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
613 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
614 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400615 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800616
bsalomon0ea80f42015-02-11 10:49:59 -0800617 size_t large = 2 * cache->getResourceBytes();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500618 unbudgeted = new TestResource(gpu, SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800619 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
620 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
621 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
622 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400623 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800624
625 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800626 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
627 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
628 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
629 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400630 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800631
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500632 wrapped = TestResource::CreateWrapped(gpu, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800633 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
634 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
635 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
636 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400637 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800638
639 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800640 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
641 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
642 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
643 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400644 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800645
bsalomon0ea80f42015-02-11 10:49:59 -0800646 cache->purgeAllUnlocked();
647 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
648 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
649 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
650 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400651 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800652}
653
bsalomon3582d3e2015-02-13 14:20:05 -0800654// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
655void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
656/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800657 Mock mock(10, 300);
658 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800659 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500660 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonc2f35b72015-01-23 07:19:22 -0800661
662 TestResource* resource =
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500663 TestResource::CreateScratch(gpu, SkBudgeted::kNo, TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800664 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800665 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800666
667 size_t size = resource->gpuMemorySize();
668 for (int i = 0; i < 2; ++i) {
669 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800670 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800671 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800672 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700673 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800674 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
675 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
676 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
677 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400678 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800679
680 // Once it is unrefed, it should become available as scratch.
681 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800682 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
683 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
684 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
685 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400686 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700687 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800688 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800689 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800690 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800691 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800692
693 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700694 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800695 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800696 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800697 } else {
698 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800699 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800700 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
701 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
702 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
703 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400704 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800705 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800706 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800707 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800708
709 // now when it is unrefed it should die since it has no key.
710 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800711 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
712 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
713 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
714 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400715 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800716 }
bsalomon8b79d232014-11-10 10:19:06 -0800717 }
bsalomonc2f35b72015-01-23 07:19:22 -0800718}
719
720static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
721 Mock mock(5, 30000);
722 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800723 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500724 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800725
bsalomon8b79d232014-11-10 10:19:06 -0800726 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500727 TestResource* a = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700728 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400729 TestResource::kB_SimulatedProperty, 11);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500730 TestResource* b = TestResource::CreateScratch(gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700731 SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -0400732 TestResource::kB_SimulatedProperty, 12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800733 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800734 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800735 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700736 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800737
738 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800739 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800740
bsalomon0ea80f42015-02-11 10:49:59 -0800741 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800742 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800743 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
744 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800745 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800746 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800747
bsalomon63c992f2015-01-23 12:47:59 -0800748 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800749 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800750 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800751 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800752
753 // Unref but don't purge
754 a->unref();
755 b->unref();
756 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800757 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800758
bsalomon63c992f2015-01-23 12:47:59 -0800759 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800760 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800761 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800762 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
763 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800764}
765
bsalomon10e23ca2014-11-25 05:52:06 -0800766static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800767 Mock mock(5, 30000);
768 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800769 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500770 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -0800771
bsalomon10e23ca2014-11-25 05:52:06 -0800772 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500773 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800774 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500775 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800776 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800777 a->unref();
778 b->unref();
779
bsalomon1c60dfe2015-01-21 09:32:40 -0800780 GrScratchKey scratchKey;
781 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800782 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800783 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700784 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800785
bsalomon0ea80f42015-02-11 10:49:59 -0800786 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800787 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800788 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800789 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
790 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800791
792 // Find the first resource and remove its scratch key
793 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700794 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800795 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800796 // It's still alive, but not cached by scratch key anymore
797 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800798 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
799 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800800
801 // The cache should immediately delete it when it's unrefed since it isn't accessible.
802 find->unref();
803 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800804 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
805 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800806
807 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700808 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800809 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800810 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800811 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
812 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800813
814 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800815 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800816 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800817 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
818 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800819
820 find->unref();
821 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800822 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
823 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800824}
825
bsalomon1c60dfe2015-01-21 09:32:40 -0800826static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800827 Mock mock(5, 30000);
828 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800829 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500830 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon1c60dfe2015-01-21 09:32:40 -0800831
832 // Create two resources that have the same scratch key.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500833 TestResource* a = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800834 TestResource::kB_SimulatedProperty);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500835 TestResource* b = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800836 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800837 a->unref();
838 b->unref();
839
840 GrScratchKey scratchKey;
841 // Ensure that scratch key comparison and assignment is consistent.
842 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800843 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800844 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800845 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800846 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
847 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
848 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
849 scratchKey = scratchKey1;
850 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
851 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
852 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
853 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
854 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
855 scratchKey = scratchKey2;
856 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
857 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
858 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
859 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
860 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
861
862 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800863 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800864 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700865 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800866
867 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800868 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700869 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700870 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800871 find->unref();
872
873 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700874 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700875 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800876 REPORTER_ASSERT(reporter, find == a || find == b);
877
robertphillips6e83ac72015-08-13 05:19:14 -0700878 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700879 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800880 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
881 REPORTER_ASSERT(reporter, find2 != find);
882 find2->unref();
883 find->unref();
884}
885
bsalomon8718aaf2015-02-19 07:24:21 -0800886static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800887 Mock mock(5, 30000);
888 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800889 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500890 GrGpu* gpu = context->contextPriv().getGpu();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000891
bsalomon8718aaf2015-02-19 07:24:21 -0800892 GrUniqueKey key;
893 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700894
bsalomon8718aaf2015-02-19 07:24:21 -0800895 // Create two resources that we will attempt to register with the same unique key.
Greg Danielda86e282018-06-13 09:41:19 -0400896 TestResource* a = new TestResource(gpu, SkBudgeted::kYes, 11);
mtklein5f939ab2016-03-16 10:28:35 -0700897
bsalomonf99e9612015-02-19 08:24:16 -0800898 // Set key on resource a.
899 a->resourcePriv().setUniqueKey(key);
900 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
901 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800902
bsalomonf99e9612015-02-19 08:24:16 -0800903 // Make sure that redundantly setting a's key works.
904 a->resourcePriv().setUniqueKey(key);
905 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800906 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800907 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
908 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800909 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
910
bsalomonf99e9612015-02-19 08:24:16 -0800911 // Create resource b and set the same key. It should replace a's unique key cache entry.
Greg Danielda86e282018-06-13 09:41:19 -0400912 TestResource* b = new TestResource(gpu, SkBudgeted::kYes, 12);
bsalomonf99e9612015-02-19 08:24:16 -0800913 b->resourcePriv().setUniqueKey(key);
914 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
915 b->unref();
916
917 // Still have two resources because a is still reffed.
918 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
919 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
920 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
921
922 a->unref();
923 // Now a should be gone.
924 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
925 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
926 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
927
928 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
929 // Also make b be unreffed when replacement occurs.
930 b->unref();
Greg Danielda86e282018-06-13 09:41:19 -0400931 TestResource* c = new TestResource(gpu, SkBudgeted::kYes, 13);
bsalomonf99e9612015-02-19 08:24:16 -0800932 GrUniqueKey differentKey;
933 make_unique_key<0>(&differentKey, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800934 c->resourcePriv().setUniqueKey(differentKey);
935 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
936 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
937 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
938 // c replaces b and b should be immediately purged.
939 c->resourcePriv().setUniqueKey(key);
940 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
941 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
942 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
943
944 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800945 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800946 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
947 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
948 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
949
950 // Drop the ref on c, it should be kept alive because it has a unique key.
951 c->unref();
952 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
953 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
954 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
955
956 // Verify that we can find c, then remove its unique key. It should get purged immediately.
957 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
958 c->resourcePriv().removeUniqueKey();
959 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800960 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
961 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800962 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700963
964 {
965 GrUniqueKey key2;
966 make_unique_key<0>(&key2, 0);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500967 sk_sp<TestResource> d(new TestResource(gpu));
senorblanco84cd6212015-08-04 10:01:58 -0700968 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700969 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700970 d->resourcePriv().setUniqueKey(key2);
971 }
972
973 GrUniqueKey key3;
974 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400975 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700976 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000977}
978
bsalomon8b79d232014-11-10 10:19:06 -0800979static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800980 Mock mock(5, 30000);
981 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800982 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500983 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800984
bsalomon8718aaf2015-02-19 07:24:21 -0800985 GrUniqueKey key1, key2, key3;
986 make_unique_key<0>(&key1, 1);
987 make_unique_key<0>(&key2, 2);
988 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700989
bsalomon23e619c2015-02-06 11:54:28 -0800990 // Add three resources to the cache. Only c is usable as scratch.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500991 TestResource* a = new TestResource(gpu);
992 TestResource* b = new TestResource(gpu);
993 TestResource* c = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800994 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800995 a->resourcePriv().setUniqueKey(key1);
996 b->resourcePriv().setUniqueKey(key2);
997 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800998 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800999 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -08001000 c->unref();
1001
bsalomon8718aaf2015-02-19 07:24:21 -08001002 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1003 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
1004 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001005 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -08001006
bsalomon8718aaf2015-02-19 07:24:21 -08001007 typedef GrUniqueKeyInvalidatedMessage Msg;
1008 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -08001009
1010 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
Brian Salomon238069b2018-07-11 15:58:57 -04001011 Bus::Post(Msg(key1, context->uniqueID()));
1012 Bus::Post(Msg(key2, context->uniqueID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001013 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001014 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -08001015 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1016 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -08001017 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001018 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -08001019
1020 // Invalidate the third.
Brian Salomon238069b2018-07-11 15:58:57 -04001021 Bus::Post(Msg(key3, context->uniqueID()));
bsalomon0ea80f42015-02-11 10:49:59 -08001022 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -08001023 // we still have a ref on b, c should be recycled as scratch.
1024 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -08001025 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -08001026
bsalomon23e619c2015-02-06 11:54:28 -08001027 // make b purgeable. It should be immediately deleted since it has no key.
1028 b->unref();
1029 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1030
1031 // Make sure we actually get to c via it's scratch key, before we say goodbye.
1032 GrScratchKey scratchKey;
1033 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -07001034 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -08001035 REPORTER_ASSERT(reporter, scratch == c);
1036 SkSafeUnref(scratch);
1037
1038 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -08001039 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -07001040 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -08001041 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -08001042 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1043 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -08001044 REPORTER_ASSERT(reporter, !scratch);
1045 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -08001046}
1047
bsalomon71cb0c22014-11-14 12:10:14 -08001048static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -08001049 Mock mock(3, 30000);
1050 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001051 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001052 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -08001053
bsalomon8718aaf2015-02-19 07:24:21 -08001054 GrUniqueKey key1, key2;
1055 make_unique_key<0>(&key1, 1);
1056 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001057
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001058 sk_sp<TestResource> a(new TestResource(gpu));
1059 sk_sp<TestResource> b(new TestResource(gpu));
bsalomon8718aaf2015-02-19 07:24:21 -08001060 a->resourcePriv().setUniqueKey(key1);
1061 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -08001062
bsalomonc2f35b72015-01-23 07:19:22 -08001063 // Make a cycle
1064 a->setUnrefWhenDestroyed(b);
1065 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001066
bsalomonc2f35b72015-01-23 07:19:22 -08001067 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001068
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001069 TestResource* unownedA = a.release();
1070 unownedA->unref();
1071 b.reset();
bsalomon8b79d232014-11-10 10:19:06 -08001072
bsalomonc2f35b72015-01-23 07:19:22 -08001073 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001074
bsalomon0ea80f42015-02-11 10:49:59 -08001075 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001076 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001077
bsalomonc2f35b72015-01-23 07:19:22 -08001078 // Break the cycle
Ben Wagner97c6a0e2018-07-11 14:56:22 -04001079 unownedA->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001080 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001081
bsalomon0ea80f42015-02-11 10:49:59 -08001082 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001083 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001084}
1085
bsalomonddf30e62015-02-19 11:38:44 -08001086static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1087 static const int kCount = 50;
1088 static const int kBudgetCnt = kCount / 2;
1089 static const int kLockedFreq = 8;
1090 static const int kBudgetSize = 0x80000000;
1091
1092 SkRandom random;
1093
1094 // Run the test 2*kCount times;
1095 for (int i = 0; i < 2 * kCount; ++i ) {
1096 Mock mock(kBudgetCnt, kBudgetSize);
1097 GrContext* context = mock.context();
1098 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001099 GrGpu* gpu = context->contextPriv().getGpu();
bsalomonddf30e62015-02-19 11:38:44 -08001100
1101 // Pick a random number of resources to add before the timestamp will wrap.
Ben Wagnerb0897652018-06-15 15:37:57 +00001102 cache->changeTimestamp(UINT32_MAX - random.nextULessThan(kCount + 1));
bsalomonddf30e62015-02-19 11:38:44 -08001103
1104 static const int kNumToPurge = kCount - kBudgetCnt;
1105
1106 SkTDArray<int> shouldPurgeIdxs;
1107 int purgeableCnt = 0;
1108 SkTDArray<GrGpuResource*> resourcesToUnref;
1109
1110 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1111 // unpurgeable resources.
1112 for (int j = 0; j < kCount; ++j) {
1113 GrUniqueKey key;
1114 make_unique_key<0>(&key, j);
1115
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001116 TestResource* r = new TestResource(gpu);
bsalomonddf30e62015-02-19 11:38:44 -08001117 r->resourcePriv().setUniqueKey(key);
1118 if (random.nextU() % kLockedFreq) {
1119 // Make this is purgeable.
1120 r->unref();
1121 ++purgeableCnt;
1122 if (purgeableCnt <= kNumToPurge) {
1123 *shouldPurgeIdxs.append() = j;
1124 }
1125 } else {
1126 *resourcesToUnref.append() = r;
1127 }
1128 }
1129
1130 // Verify that the correct resources were purged.
1131 int currShouldPurgeIdx = 0;
1132 for (int j = 0; j < kCount; ++j) {
1133 GrUniqueKey key;
1134 make_unique_key<0>(&key, j);
1135 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1136 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1137 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1138 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001139 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001140 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001141 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001142 }
1143 SkSafeUnref(res);
1144 }
1145
1146 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1147 resourcesToUnref[j]->unref();
1148 }
1149 }
1150}
1151
Brian Salomon5e150852017-03-22 14:53:13 -04001152static void test_time_purge(skiatest::Reporter* reporter) {
1153 Mock mock(1000000, 1000000);
1154 GrContext* context = mock.context();
1155 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001156 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon5e150852017-03-22 14:53:13 -04001157
1158 static constexpr int kCnts[] = {1, 10, 1024};
1159 auto nowish = []() {
1160 // We sleep so that we ensure we get a value that is greater than the last call to
1161 // GrStdSteadyClock::now().
1162 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1163 auto result = GrStdSteadyClock::now();
1164 // Also sleep afterwards so we don't get this value again.
1165 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1166 return result;
1167 };
1168
1169 for (int cnt : kCnts) {
1170 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1171 new GrStdSteadyClock::time_point[cnt]);
1172 {
1173 // Insert resources and get time points between each addition.
1174 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001175 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001176 GrUniqueKey k;
1177 make_unique_key<1>(&k, i);
1178 r->resourcePriv().setUniqueKey(k);
1179 r->unref();
1180 timeStamps.get()[i] = nowish();
1181 }
1182
1183 // Purge based on the time points between resource additions. Each purge should remove
1184 // the oldest resource.
1185 for (int i = 0; i < cnt; ++i) {
1186 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1187 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1188 for (int j = 0; j < i; ++j) {
1189 GrUniqueKey k;
1190 make_unique_key<1>(&k, j);
1191 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1192 REPORTER_ASSERT(reporter, !SkToBool(r));
1193 SkSafeUnref(r);
1194 }
1195 }
1196
1197 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1198 cache->purgeAllUnlocked();
1199 }
1200
1201 // Do a similar test but where we leave refs on some resources to prevent them from being
1202 // purged.
1203 {
1204 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1205 for (int i = 0; i < cnt; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001206 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001207 GrUniqueKey k;
1208 make_unique_key<1>(&k, i);
1209 r->resourcePriv().setUniqueKey(k);
1210 // Leave a ref on every other resource, beginning with the first.
1211 if (SkToBool(i & 0x1)) {
1212 refedResources.get()[i / 2] = r;
1213 } else {
1214 r->unref();
1215 }
1216 timeStamps.get()[i] = nowish();
1217 }
1218
1219 for (int i = 0; i < cnt; ++i) {
1220 // Should get a resource purged every other frame.
1221 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1222 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1223 }
1224
1225 // Unref all the resources that we kept refs on in the first loop.
1226 for (int i = 0; i < (cnt / 2); ++i) {
1227 refedResources.get()[i]->unref();
1228 cache->purgeResourcesNotUsedSince(nowish());
1229 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1230 }
1231
1232 cache->purgeAllUnlocked();
1233 }
1234
1235 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1236
1237 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1238 // eviction
1239 context->flush();
1240 for (int i = 0; i < 10; ++i) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001241 TestResource* r = new TestResource(gpu);
Brian Salomon5e150852017-03-22 14:53:13 -04001242 GrUniqueKey k;
1243 make_unique_key<1>(&k, i);
1244 r->resourcePriv().setUniqueKey(k);
1245 r->unref();
1246 }
1247 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1248 context->flush();
1249 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1250 cache->purgeResourcesNotUsedSince(nowish());
1251 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1252 }
1253}
1254
Derek Sollenberger5480a182017-05-25 16:43:59 -04001255static void test_partial_purge(skiatest::Reporter* reporter) {
1256 Mock mock(6, 100);
1257 GrContext* context = mock.context();
1258 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001259 GrGpu* gpu = context->contextPriv().getGpu();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001260
1261 enum TestsCase {
1262 kOnlyScratch_TestCase = 0,
1263 kPartialScratch_TestCase = 1,
1264 kAllScratch_TestCase = 2,
1265 kPartial_TestCase = 3,
1266 kAll_TestCase = 4,
1267 kNone_TestCase = 5,
1268 kEndTests_TestCase = kNone_TestCase + 1
1269 };
1270
1271 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1272
1273 GrUniqueKey key1, key2, key3;
1274 make_unique_key<0>(&key1, 1);
1275 make_unique_key<0>(&key2, 2);
1276 make_unique_key<0>(&key3, 3);
1277
1278 // Add three unique resources to the cache.
Greg Danielda86e282018-06-13 09:41:19 -04001279 TestResource *unique1 = new TestResource(gpu, SkBudgeted::kYes, 10);
1280 TestResource *unique2 = new TestResource(gpu, SkBudgeted::kYes, 11);
1281 TestResource *unique3 = new TestResource(gpu, SkBudgeted::kYes, 12);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001282
1283 unique1->resourcePriv().setUniqueKey(key1);
1284 unique2->resourcePriv().setUniqueKey(key2);
1285 unique3->resourcePriv().setUniqueKey(key3);
1286
Derek Sollenberger5480a182017-05-25 16:43:59 -04001287 // Add two scratch resources to the cache.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001288 TestResource *scratch1 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001289 TestResource::kA_SimulatedProperty,
1290 13);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001291 TestResource *scratch2 = TestResource::CreateScratch(gpu, SkBudgeted::kYes,
Greg Danielda86e282018-06-13 09:41:19 -04001292 TestResource::kB_SimulatedProperty,
1293 14);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001294
1295 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1296 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1297 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1298
1299 // Add resources to the purgeable queue
1300 unique1->unref();
1301 scratch1->unref();
1302 unique2->unref();
1303 scratch2->unref();
1304 unique3->unref();
1305
1306 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1307 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1308 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1309
1310 switch(testCase) {
1311 case kOnlyScratch_TestCase: {
1312 context->purgeUnlockedResources(14, true);
1313 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1314 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1315 break;
1316 }
1317 case kPartialScratch_TestCase: {
1318 context->purgeUnlockedResources(3, true);
1319 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1320 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1321 break;
1322 }
1323 case kAllScratch_TestCase: {
1324 context->purgeUnlockedResources(50, true);
1325 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1326 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1327 break;
1328 }
1329 case kPartial_TestCase: {
1330 context->purgeUnlockedResources(13, false);
1331 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1332 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1333 break;
1334 }
1335 case kAll_TestCase: {
1336 context->purgeUnlockedResources(50, false);
1337 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1338 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1339 break;
1340 }
1341 case kNone_TestCase: {
1342 context->purgeUnlockedResources(0, true);
1343 context->purgeUnlockedResources(0, false);
1344 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1345 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1346 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1347 break;
1348 }
1349 };
1350
1351 // ensure all are purged before the next
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001352 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Derek Sollenberger5480a182017-05-25 16:43:59 -04001353 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1354 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1355
1356 }
1357}
1358
bsalomon10e23ca2014-11-25 05:52:06 -08001359static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001360 // Set the cache size to double the resource count because we're going to create 2x that number
1361 // resources, using two different key domains. Add a little slop to the bytes because we resize
1362 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001363 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001364
bsalomonc2f35b72015-01-23 07:19:22 -08001365 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1366 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001367 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001368 GrGpu* gpu = context->contextPriv().getGpu();
bsalomon10e23ca2014-11-25 05:52:06 -08001369
1370 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001371 GrUniqueKey key1, key2;
1372 make_unique_key<1>(&key1, i);
1373 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001374
bsalomon24db3b12015-01-23 04:24:04 -08001375 TestResource* resource;
1376
Greg Danielda86e282018-06-13 09:41:19 -04001377 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001378 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001379 resource->unref();
1380
Greg Danielda86e282018-06-13 09:41:19 -04001381 resource = new TestResource(gpu, SkBudgeted::kYes, 1);
bsalomon8718aaf2015-02-19 07:24:21 -08001382 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001383 resource->unref();
1384 }
1385
1386 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001387 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001388 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1389 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1390 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1391 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001392 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001393 GrUniqueKey key1, key2;
1394 make_unique_key<1>(&key1, i);
1395 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001396
bsalomon8718aaf2015-02-19 07:24:21 -08001397 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1398 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001399 }
1400
bsalomon0ea80f42015-02-11 10:49:59 -08001401 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001402 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001403 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001404 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1405 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1406 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1407 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001408
1409 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001410 GrUniqueKey key1, key2;
1411 make_unique_key<1>(&key1, i);
1412 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001413
bsalomon8718aaf2015-02-19 07:24:21 -08001414 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1415 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001416 }
1417}
1418
senorblanco84cd6212015-08-04 10:01:58 -07001419static void test_custom_data(skiatest::Reporter* reporter) {
1420 GrUniqueKey key1, key2;
1421 make_unique_key<0>(&key1, 1);
1422 make_unique_key<0>(&key2, 2);
1423 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001424 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001425 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1426 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1427
1428 // Test that copying a key also takes a ref on its custom data.
1429 GrUniqueKey key3 = key1;
1430 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1431}
1432
bsalomonc6363ef2015-09-24 07:07:40 -07001433static void test_abandoned(skiatest::Reporter* reporter) {
1434 Mock mock(10, 300);
1435 GrContext* context = mock.context();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001436 GrGpu* gpu = context->contextPriv().getGpu();
1437
1438 sk_sp<GrGpuResource> resource(new TestResource(gpu));
bsalomonc6363ef2015-09-24 07:07:40 -07001439 context->abandonContext();
1440
1441 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1442
1443 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1444
robertphillips8abb3702016-08-31 14:04:06 -07001445 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001446 resource->getUniqueKey();
1447 resource->wasDestroyed();
1448 resource->gpuMemorySize();
1449 resource->getContext();
1450
1451 resource->abandon();
1452 resource->resourcePriv().getScratchKey();
1453 resource->resourcePriv().isBudgeted();
1454 resource->resourcePriv().makeBudgeted();
1455 resource->resourcePriv().makeUnbudgeted();
1456 resource->resourcePriv().removeScratchKey();
1457 GrUniqueKey key;
1458 make_unique_key<0>(&key, 1);
1459 resource->resourcePriv().setUniqueKey(key);
1460 resource->resourcePriv().removeUniqueKey();
1461}
1462
Brian Salomon1090da62017-01-06 12:04:19 -05001463static void test_tags(skiatest::Reporter* reporter) {
1464#ifdef SK_DEBUG
1465 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1466 static constexpr int kLastTagIdx = 10;
1467 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1468
1469 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1470 GrContext* context = mock.context();
1471 GrResourceCache* cache = mock.cache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001472 GrGpu* gpu = context->contextPriv().getGpu();
Brian Salomon1090da62017-01-06 12:04:19 -05001473
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001474 // tag strings are expected to be long lived
1475 std::vector<SkString> tagStrings;
1476
Brian Salomon1090da62017-01-06 12:04:19 -05001477 SkString tagStr;
1478 int tagIdx = 0;
1479 int currTagCnt = 0;
1480
1481 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001482
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001483 sk_sp<GrGpuResource> resource(new TestResource(gpu));
Brian Salomon1090da62017-01-06 12:04:19 -05001484 GrUniqueKey key;
1485 if (currTagCnt == tagIdx) {
1486 tagIdx += 1;
1487 currTagCnt = 0;
1488 tagStr.printf("tag%d", tagIdx);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001489 tagStrings.emplace_back(tagStr);
Brian Salomon1090da62017-01-06 12:04:19 -05001490 }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -04001491 make_unique_key<1>(&key, i, tagStrings.back().c_str());
Brian Salomon1090da62017-01-06 12:04:19 -05001492 resource->resourcePriv().setUniqueKey(key);
1493 }
1494 SkASSERT(kLastTagIdx == tagIdx);
1495 SkASSERT(currTagCnt == kLastTagIdx);
1496
1497 // Test i = 0 to exercise unused tag string.
1498 for (int i = 0; i <= kLastTagIdx; ++i) {
1499 tagStr.printf("tag%d", i);
1500 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1501 }
1502#endif
1503}
1504
Greg Danielc27eb722018-08-10 09:48:08 -04001505static void test_free_resource_messages(skiatest::Reporter* reporter) {
1506 Mock mock(10, 30000);
1507 GrContext* context = mock.context();
1508 GrResourceCache* cache = mock.cache();
1509 GrGpu* gpu = context->contextPriv().getGpu();
1510
1511 TestResource* wrapped1 = TestResource::CreateWrapped(gpu, 12);
1512 cache->insertCrossContextGpuResource(wrapped1);
1513
1514 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1515
1516 TestResource* wrapped2 = TestResource::CreateWrapped(gpu, 12);
1517 cache->insertCrossContextGpuResource(wrapped2);
1518
1519 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1520
1521 // Have only ref waiting on message.
1522 wrapped1->unref();
1523 wrapped2->unref();
1524
1525 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
1526
1527 // This should free nothing since no messages were sent.
1528 cache->purgeAsNeeded();
1529
1530 // Send message to free the first resource
1531 GrGpuResourceFreedMessage msg { wrapped1, context->uniqueID() };
1532 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
1533 cache->purgeAsNeeded();
1534
1535 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
1536
1537 mock.reset();
1538
1539 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
1540}
1541
1542
Brian Salomondcfca432017-11-15 15:48:03 -05001543DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001544 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001545 test_no_key(reporter);
Robert Phillips6eba0632018-03-28 12:25:42 -04001546 test_purge_unlocked(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001547 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001548 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001549 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001550 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001551 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001552 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001553 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001554 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001555 test_cache_chained_purge(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001556 test_timestamp_wrap(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001557 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001558 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001559 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001560 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001561 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001562 test_tags(reporter);
Greg Danielc27eb722018-08-10 09:48:08 -04001563 test_free_resource_messages(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001564}
1565
Robert Phillipsd6214d42016-11-07 08:23:48 -05001566////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001567static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001568 GrSurfaceDescFlags descFlags,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001569 int width, int height,
1570 int sampleCnt) {
1571 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001572 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001573 desc.fWidth = width;
1574 desc.fHeight = height;
1575 desc.fConfig = kRGBA_8888_GrPixelConfig;
1576 desc.fSampleCnt = sampleCnt;
1577
Robert Phillipse78b7252017-04-06 07:59:41 -04001578 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001579}
1580
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001581static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* proxyProvider,
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001582 GrSurfaceDescFlags descFlags,
Robert Phillipse78b7252017-04-06 07:59:41 -04001583 int width, int height,
1584 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001585 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001586 desc.fFlags = descFlags;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001587 desc.fWidth = width;
1588 desc.fHeight = height;
1589 desc.fConfig = kRGBA_8888_GrPixelConfig;
1590 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001591
Robert Phillipsfe0253f2018-03-16 16:47:25 -04001592 auto origin = (descFlags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1593 : kTopLeft_GrSurfaceOrigin;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001594
1595 return proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001596}
1597
1598// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1599// Texture-only, both-RT-and-Texture and MIPmapped
1600DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1601 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001602 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001603 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001604
Robert Phillipsd6214d42016-11-07 08:23:48 -05001605 static const int kSize = 64;
1606
Robert Phillipsd6214d42016-11-07 08:23:48 -05001607 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001608 {
1609 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001610
Brian Salomonbdecacf2018-02-02 20:32:49 -05001611 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001612 size_t size = tex->gpuMemorySize();
1613 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1614
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001615 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1616 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001617 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001618 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001619 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001620 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001621 REPORTER_ASSERT(reporter,
1622 kSize*kSize*4 == size || // msaa4 failed
1623 kSize*kSize*4*sampleCount == size || // auto-resolving
1624 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001625 }
1626
Brian Salomonbdecacf2018-02-02 20:32:49 -05001627 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001629 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001630 }
1631
Robert Phillipsd6214d42016-11-07 08:23:48 -05001632
1633 // Mipmapped versions
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001634 if (context->contextPriv().caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001635 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001636
Brian Salomonbdecacf2018-02-02 20:32:49 -05001637 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001638 size_t size = proxy->gpuMemorySize();
1639 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1640
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001641 size_t sampleCount = (size_t)context->contextPriv().caps()->getRenderTargetSampleCount(
1642 4, kRGBA_8888_GrPixelConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -04001643 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001644 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001645 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001646 size = proxy->gpuMemorySize();
1647 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001648 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1649 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1650 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001651 }
Robert Phillips1b352562017-04-05 18:56:21 +00001652
Brian Salomonbdecacf2018-02-02 20:32:49 -05001653 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 1);
Robert Phillipse78b7252017-04-06 07:59:41 -04001654 size = proxy->gpuMemorySize();
1655 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1656 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001657}