blob: 27d197143dd94a64c9dbb242acd8cdd41785b61a [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon3f324322015-04-08 11:01:54 -07008// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
Brian Salomon5e150852017-03-22 14:53:13 -040012#include <thread>
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000014#include "GrContextPriv.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000015#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080017#include "GrGpuResourceCacheAccess.h"
18#include "GrGpuResourcePriv.h"
Robert Phillipsc0192e32017-09-21 12:00:26 -040019#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040023#include "GrTexture.h"
24
bsalomonbcf0a522014-10-08 08:40:09 -070025#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080026#include "SkGr.h"
27#include "SkMessageBus.h"
Robert Phillipsd6214d42016-11-07 08:23:48 -050028#include "SkMipMap.h"
reed69f6f002014-09-18 06:09:44 -070029#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000030#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031
32static const int gWidth = 640;
33static const int gHeight = 480;
34
35////////////////////////////////////////////////////////////////////////////////
bsalomon68d91342016-04-12 09:59:58 -070036DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070037 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080038 GrSurfaceDesc desc;
Brian Osman777b5632016-10-14 09:16:21 -040039 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080040 desc.fFlags = kRenderTarget_GrSurfaceFlag;
41 desc.fWidth = gWidth;
42 desc.fHeight = gHeight;
43 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
reede8f30622016-03-23 18:59:25 -070044 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen15302832015-12-01 04:35:26 -080045 SkCanvas* canvas = surface->getCanvas();
46
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
48
49 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000050 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000051 src.eraseColor(SK_ColorBLACK);
Mike Reedf0ffb892017-10-03 14:47:21 -040052 size_t srcSize = src.computeByteSize();
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000054 size_t initialCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070055 context->getResourceCacheUsage(nullptr, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000056
57 int oldMaxNum;
58 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000059 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000060
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000061 // Set the cache limits so we can fit 10 "src" images and the
62 // max number of textures doesn't matter
63 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000064 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000065
66 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000067 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000068
69 for (int i = 0; i < 100; ++i) {
70 canvas->drawBitmap(src, 0, 0);
Mike Reedf1942192017-07-21 14:24:29 -040071 surface->readPixels(readback, 0, 0);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000072
73 // "modify" the src texture
74 src.notifyPixelsChanged();
75
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000076 size_t curCacheSize;
halcanary96fcdcc2015-08-27 07:41:13 -070077 context->getResourceCacheUsage(nullptr, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000078
79 // we should never go over the size limit
80 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
81 }
82
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000083 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000084}
85
bsalomon11abd8d2016-10-14 08:13:48 -070086static bool is_rendering_and_not_angle_es3(sk_gpu_test::GrContextFactory::ContextType type) {
87 if (type == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES3_ContextType ||
88 type == sk_gpu_test::GrContextFactory::kANGLE_GL_ES3_ContextType) {
89 return false;
90 }
91 return sk_gpu_test::GrContextFactory::IsRenderingContext(type);
92}
93
Robert Phillipsc0192e32017-09-21 12:00:26 -040094static GrStencilAttachment* get_SB(GrRenderTarget* rt) {
95 return rt->renderTargetPriv().getStencilAttachment();
96}
97
98static sk_sp<GrRenderTarget> create_RT_with_SB(GrResourceProvider* provider,
99 int size, int sampleCount, SkBudgeted budgeted) {
100 GrSurfaceDesc desc;
101 desc.fFlags = kRenderTarget_GrSurfaceFlag;
102 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
103 desc.fWidth = size;
104 desc.fHeight = size;
105 desc.fConfig = kRGBA_8888_GrPixelConfig;
106 desc.fSampleCnt = sampleCount;
107
108 sk_sp<GrTexture> tex(provider->createTexture(desc, budgeted));
109 if (!tex || !tex->asRenderTarget()) {
110 return nullptr;
111 }
112
113 if (!provider->attachStencilAttachment(tex->asRenderTarget())) {
114 return nullptr;
115 }
116 SkASSERT(get_SB(tex->asRenderTarget()));
117
118 return sk_ref_sp(tex->asRenderTarget());
119}
120
bsalomon11abd8d2016-10-14 08:13:48 -0700121// This currently fails on ES3 ANGLE contexts
122DEF_GPUTEST_FOR_CONTEXTS(ResourceCacheStencilBuffers, &is_rendering_and_not_angle_es3, reporter,
Robert Phillipsec325342017-10-30 18:02:48 +0000123 ctxInfo, nullptr) {
bsalomon8b7451a2016-05-11 06:33:06 -0700124 GrContext* context = ctxInfo.grContext();
Eric Karl5c779752017-05-08 12:02:07 -0700125 if (context->caps()->avoidStencilBuffers()) {
126 return;
127 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400128
129 GrResourceProvider* provider = context->resourceProvider();
130
131 sk_sp<GrRenderTarget> smallRT0 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kYes);
132 REPORTER_ASSERT(reporter, smallRT0);
133
134 {
135 // Two budgeted RTs with the same desc should share a stencil buffer.
136 sk_sp<GrRenderTarget> smallRT1 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kYes);
137 REPORTER_ASSERT(reporter, smallRT1);
138
139 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 }
141
Robert Phillipsc0192e32017-09-21 12:00:26 -0400142 {
143 // An unbudgeted RT with the same desc should also share.
144 sk_sp<GrRenderTarget> smallRT2 = create_RT_with_SB(provider, 4, 0, SkBudgeted::kNo);
145 REPORTER_ASSERT(reporter, smallRT2);
146
147 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) == get_SB(smallRT2.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800148 }
149
Robert Phillipsc0192e32017-09-21 12:00:26 -0400150 {
151 // An RT with a much larger size should not share.
152 sk_sp<GrRenderTarget> bigRT = create_RT_with_SB(provider, 400, 0, SkBudgeted::kNo);
153 REPORTER_ASSERT(reporter, bigRT);
bsalomon02a44a42015-02-19 09:09:00 -0800154
Robert Phillipsc0192e32017-09-21 12:00:26 -0400155 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(bigRT.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 }
bsalomon02a44a42015-02-19 09:09:00 -0800157
Robert Phillipsc0192e32017-09-21 12:00:26 -0400158 int smallSampleCount = context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
159 if (smallSampleCount > 0) {
mtklein5f939ab2016-03-16 10:28:35 -0700160 // An RT with a different sample count should not share.
Robert Phillipsc0192e32017-09-21 12:00:26 -0400161 sk_sp<GrRenderTarget> smallMSAART0 = create_RT_with_SB(provider, 4, smallSampleCount,
162 SkBudgeted::kNo);
bsalomonb602d4d2015-02-19 12:05:58 -0800163#ifdef SK_BUILD_FOR_ANDROID
164 if (!smallMSAART0) {
165 // The nexus player seems to fail to create MSAA textures.
166 return;
167 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400168#else
169 REPORTER_ASSERT(reporter, smallMSAART0);
bsalomonb602d4d2015-02-19 12:05:58 -0800170#endif
Robert Phillipsc0192e32017-09-21 12:00:26 -0400171
172 REPORTER_ASSERT(reporter, get_SB(smallRT0.get()) != get_SB(smallMSAART0.get()));
173
174 {
175 // A second MSAA RT should share with the first MSAA RT.
176 sk_sp<GrRenderTarget> smallMSAART1 = create_RT_with_SB(provider, 4, smallSampleCount,
177 SkBudgeted::kNo);
178 REPORTER_ASSERT(reporter, smallMSAART1);
179
180 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) == get_SB(smallMSAART1.get()));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800181 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400182
bsalomon02a44a42015-02-19 09:09:00 -0800183 // But not one with a larger sample count should not. (Also check that the request for 4
184 // samples didn't get rounded up to >= 8 or else they could share.).
Robert Phillipsc0192e32017-09-21 12:00:26 -0400185 int bigSampleCount = context->caps()->getSampleCount(8, kRGBA_8888_GrPixelConfig);
186 if (bigSampleCount != smallSampleCount) {
187 sk_sp<GrRenderTarget> smallMSAART2 = create_RT_with_SB(provider, 4, bigSampleCount,
188 SkBudgeted::kNo);
189 REPORTER_ASSERT(reporter, smallMSAART2);
190
191 REPORTER_ASSERT(reporter, get_SB(smallMSAART0.get()) != get_SB(smallMSAART2.get()));
bsalomon02a44a42015-02-19 09:09:00 -0800192 }
193 }
194}
195
bsalomon68d91342016-04-12 09:59:58 -0700196DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceCacheWrappedResources, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700197 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -0800198 GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700199 // this test is only valid for GL
200 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700201 return;
202 }
203
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500204 GrBackendTexture backendTextures[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205 static const int kW = 100;
206 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700207
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500208 backendTextures[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
209 kRGBA_8888_GrPixelConfig,
210 false, GrMipMapped::kNo);
211 backendTextures[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
212 kRGBA_8888_GrPixelConfig,
213 false, GrMipMapped::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -0500214 REPORTER_ASSERT(reporter, backendTextures[0].isValid());
215 REPORTER_ASSERT(reporter, backendTextures[1].isValid());
216 if (!backendTextures[0].isValid() || !backendTextures[1].isValid()) {
217 return;
218 }
jvanverth672bb7f2015-07-13 07:19:57 -0700219
bsalomon6dc6f5f2015-06-18 09:12:16 -0700220 context->resetContext();
221
Brian Osman32342f02017-03-04 08:12:46 -0500222 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500223 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224
Brian Osman32342f02017-03-04 08:12:46 -0500225 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500226 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227
Brian Osman85d34b22017-05-10 12:06:26 -0400228 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
229 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230 return;
231 }
232
halcanary96fcdcc2015-08-27 07:41:13 -0700233 borrowed.reset(nullptr);
234 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700235
236 context->flush();
237
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500238 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
239 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700240
241 REPORTER_ASSERT(reporter, borrowedIsAlive);
242 REPORTER_ASSERT(reporter, !adoptedIsAlive);
243
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500244 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[0]), !borrowedIsAlive);
245 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[1]), !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700246
247 context->resetContext();
248}
249
bsalomon6d3fe022014-07-25 08:35:45 -0700250class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800251 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000252public:
robertphillips6e83ac72015-08-13 05:19:14 -0700253 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700254
bsalomon1c60dfe2015-01-21 09:32:40 -0800255 /** Property that distinctly categorizes the resource.
256 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800257 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800258
kkinnunen2e6055b2016-04-22 01:48:29 -0700259 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
260 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700261 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800262 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700263 , fProperty(kA_SimulatedProperty)
264 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800265 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700266 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800267 }
268
kkinnunen2e6055b2016-04-22 01:48:29 -0700269 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
270 SimulatedProperty property) {
271 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800272 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700273 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
274 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000275 }
276
Brian Salomond3b65972017-03-22 12:05:03 -0400277 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800278 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800279 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000280 }
281
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000282 void setSize(size_t size) {
283 fSize = size;
284 this->didChangeGpuMemorySize();
285 }
286
bsalomon33435572014-11-05 14:47:41 -0800287 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000288
bsalomon71cb0c22014-11-14 12:10:14 -0800289 void setUnrefWhenDestroyed(TestResource* resource) {
290 SkRefCnt_SafeAssign(fToDelete, 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
kkinnunen2e6055b2016-04-22 01:48:29 -0700307 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
308 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700309 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800310 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700311 , fProperty(property)
312 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800313 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700314 this->registerWithCache(budgeted);
315 }
316
317 // Constructor for simulating resources that wrap backend objects.
318 TestResource(GrGpu* gpu, size_t size)
319 : INHERITED(gpu)
320 , fToDelete(nullptr)
321 , fSize(size)
322 , fProperty(kA_SimulatedProperty)
323 , fIsScratch(false) {
324 ++fNumAlive;
325 this->registerWithCacheWrapped();
326 }
327
328 void computeScratchKey(GrScratchKey* key) const override {
329 if (fIsScratch) {
330 ComputeScratchKey(fProperty, key);
331 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800332 }
333
mtklein36352bf2015-03-25 18:17:31 -0700334 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800335
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000336 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000337 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800338 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800339 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700340 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700341 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000342};
bsalomon33435572014-11-05 14:47:41 -0800343int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000344
bsalomonc2f35b72015-01-23 07:19:22 -0800345class Mock {
346public:
347 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400348 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800349 SkASSERT(fContext);
350 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800351 GrResourceCache* cache = fContext->getResourceCache();
352 cache->purgeAllUnlocked();
353 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800354 }
bsalomonc2f35b72015-01-23 07:19:22 -0800355
bsalomon0ea80f42015-02-11 10:49:59 -0800356 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800357
Hal Canary342b7ac2016-11-04 11:49:42 -0400358 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800359
360private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400361 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800362};
363
364static void test_no_key(skiatest::Reporter* reporter) {
365 Mock mock(10, 30000);
366 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800367 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800368
369 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700370 TestResource* a = new TestResource(context->getGpu());
371 TestResource* b = new TestResource(context->getGpu());
372 TestResource* c = new TestResource(context->getGpu());
373 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800374 a->setSize(11);
375 b->setSize(12);
376 c->setSize(13);
377 d->setSize(14);
378
379 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800380 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800381 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800382 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800385 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
387
bsalomon8718aaf2015-02-19 07:24:21 -0800388 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800389
390 a->unref();
391 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800392 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800393 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800394 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800395
396 c->unref();
397 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800398 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800399 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800400 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800401
402 d->unref();
403 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800404 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
405 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406
407 b->unref();
408 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800411}
412
bsalomon24db3b12015-01-23 04:24:04 -0800413// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500414template <int>
415static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800416 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500417 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800418 builder[0] = data;
419}
420
bsalomon84c8e622014-11-17 09:33:27 -0800421static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800422 Mock mock(10, 300);
423 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800424 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800425
bsalomon8718aaf2015-02-19 07:24:21 -0800426 GrUniqueKey uniqueKey;
427 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800430 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700431 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800432 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700433 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800434 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800435 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700436 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800437 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700438 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700439 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800440 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800441
Brian Osman0562eb92017-05-08 11:16:39 -0400442 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800443 GrUniqueKey uniqueKey2;
444 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800445 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400446 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
447 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
448
449 // Remove the extra ref we just added.
450 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800451
452 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800453 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800454 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800455 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800456 cache->getResourceBytes());
457 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800458 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800459 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400460 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800461
bsalomon63c992f2015-01-23 12:47:59 -0800462 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800463 cache->purgeAllUnlocked();
464 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800465 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800466 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800467 cache->getResourceBytes());
468 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800470 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400471 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800472
473 // Unreffing the wrapped resource should free it right away.
474 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800475 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800476 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800477 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400478 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800479
bsalomon84c8e622014-11-17 09:33:27 -0800480 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700481 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800482 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800483 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400484 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800485 cache->purgeAllUnlocked();
486 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800487 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800488 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
489 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
490 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400491 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800492
493 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400494 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800495 cache->purgeAllUnlocked();
496 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800497 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800498 cache->getResourceBytes());
499 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
500 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400501 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800502
503 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800504 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
505 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
506 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
507 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400508 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800509
510 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800511 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
512 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400515 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800516}
517
bsalomon5236cf42015-01-14 10:42:08 -0800518static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800519 Mock mock(10, 30000);
520 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800521 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800522
bsalomon8718aaf2015-02-19 07:24:21 -0800523 GrUniqueKey uniqueKey;
524 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800525
526 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800527 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800528 TestResource* wrapped;
529 TestResource* unbudgeted;
530
531 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700532 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
533 TestResource::kB_SimulatedProperty);
534
bsalomon5236cf42015-01-14 10:42:08 -0800535 scratch->setSize(10);
536 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800537 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
538 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
539 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
540 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400541 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800542
halcanary385fe4d2015-08-26 13:07:48 -0700543 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800544 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800545 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800546 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800547 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
548 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
549 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
550 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400551 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800552
bsalomon0ea80f42015-02-11 10:49:59 -0800553 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700554 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800555 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
556 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
557 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
558 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400559 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800560
561 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800562 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
563 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
564 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
565 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400566 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800567
kkinnunen2e6055b2016-04-22 01:48:29 -0700568 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
570 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
571 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
572 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400573 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800574
575 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800576 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400580 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800581
bsalomon0ea80f42015-02-11 10:49:59 -0800582 cache->purgeAllUnlocked();
583 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
584 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
586 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400587 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800588}
589
bsalomon3582d3e2015-02-13 14:20:05 -0800590// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
591void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
592/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800593 Mock mock(10, 300);
594 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800595 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800596
597 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700598 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
599 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800600 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800601 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800602
603 size_t size = resource->gpuMemorySize();
604 for (int i = 0; i < 2; ++i) {
605 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800606 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800607 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800608 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700609 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800610 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
611 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
612 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400614 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800615
616 // Once it is unrefed, it should become available as scratch.
617 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800618 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
619 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
620 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
621 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400622 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700623 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800624 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800625 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800626 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800627 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800628
629 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700630 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800631 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800632 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800633 } else {
634 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800635 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
637 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
638 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
639 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400640 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800641 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800642 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800643 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800644
645 // now when it is unrefed it should die since it has no key.
646 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800647 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());
bsalomonc2f35b72015-01-23 07:19:22 -0800652 }
bsalomon8b79d232014-11-10 10:19:06 -0800653 }
bsalomonc2f35b72015-01-23 07:19:22 -0800654}
655
656static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
657 Mock mock(5, 30000);
658 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800659 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800660
bsalomon8b79d232014-11-10 10:19:06 -0800661 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800662 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700663 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800664 TestResource::kB_SimulatedProperty);
665 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700666 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800667 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800668 a->setSize(11);
669 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800670 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800671 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800672 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700673 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800674
675 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800677
bsalomon0ea80f42015-02-11 10:49:59 -0800678 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800679 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800680 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
681 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800682 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800683 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800684
bsalomon63c992f2015-01-23 12:47:59 -0800685 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800686 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800687 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800688 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800689
690 // Unref but don't purge
691 a->unref();
692 b->unref();
693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800694 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800695
bsalomon63c992f2015-01-23 12:47:59 -0800696 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800697 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800698 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800699 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
700 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800701}
702
bsalomon10e23ca2014-11-25 05:52:06 -0800703static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800704 Mock mock(5, 30000);
705 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800706 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800707
bsalomon10e23ca2014-11-25 05:52:06 -0800708 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700709 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800710 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700711 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800712 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800713 a->unref();
714 b->unref();
715
bsalomon1c60dfe2015-01-21 09:32:40 -0800716 GrScratchKey scratchKey;
717 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800718 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800719 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700720 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800721
bsalomon0ea80f42015-02-11 10:49:59 -0800722 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800723 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800724 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800725 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
726 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800727
728 // Find the first resource and remove its scratch key
729 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700730 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800731 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800732 // It's still alive, but not cached by scratch key anymore
733 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800734 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
735 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800736
737 // The cache should immediately delete it when it's unrefed since it isn't accessible.
738 find->unref();
739 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800740 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
741 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800742
743 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700744 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800745 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800746 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800747 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
748 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800749
750 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800751 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800752 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800753 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
754 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800755
756 find->unref();
757 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800758 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
759 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800760}
761
bsalomon1c60dfe2015-01-21 09:32:40 -0800762static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800763 Mock mock(5, 30000);
764 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800765 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800766
767 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700768 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800769 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700770 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800771 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800772 a->unref();
773 b->unref();
774
775 GrScratchKey scratchKey;
776 // Ensure that scratch key comparison and assignment is consistent.
777 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800778 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800779 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800780 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800781 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
782 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
783 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
784 scratchKey = scratchKey1;
785 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
786 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
787 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
788 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
789 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
790 scratchKey = scratchKey2;
791 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
792 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
793 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
794 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
795 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
796
797 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800798 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800799 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700800 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800801
802 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800803 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700804 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700805 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800806 find->unref();
807
808 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700809 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700810 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800811 REPORTER_ASSERT(reporter, find == a || find == b);
812
robertphillips6e83ac72015-08-13 05:19:14 -0700813 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700814 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800815 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
816 REPORTER_ASSERT(reporter, find2 != find);
817 find2->unref();
818 find->unref();
819}
820
bsalomon8718aaf2015-02-19 07:24:21 -0800821static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800822 Mock mock(5, 30000);
823 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800824 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000825
bsalomon8718aaf2015-02-19 07:24:21 -0800826 GrUniqueKey key;
827 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700828
bsalomon8718aaf2015-02-19 07:24:21 -0800829 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700830 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800831 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700832
bsalomonf99e9612015-02-19 08:24:16 -0800833 // Set key on resource a.
834 a->resourcePriv().setUniqueKey(key);
835 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
836 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800837
bsalomonf99e9612015-02-19 08:24:16 -0800838 // Make sure that redundantly setting a's key works.
839 a->resourcePriv().setUniqueKey(key);
840 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800841 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800842 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
843 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800844 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
845
bsalomonf99e9612015-02-19 08:24:16 -0800846 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700847 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800848 b->setSize(12);
849 b->resourcePriv().setUniqueKey(key);
850 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
851 b->unref();
852
853 // Still have two resources because a is still reffed.
854 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
855 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
856 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
857
858 a->unref();
859 // Now a should be gone.
860 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
861 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
862 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
863
864 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
865 // Also make b be unreffed when replacement occurs.
866 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700867 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800868 GrUniqueKey differentKey;
869 make_unique_key<0>(&differentKey, 1);
870 c->setSize(13);
871 c->resourcePriv().setUniqueKey(differentKey);
872 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
873 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
874 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
875 // c replaces b and b should be immediately purged.
876 c->resourcePriv().setUniqueKey(key);
877 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
878 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
879 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
880
881 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800882 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800883 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
884 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
885 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
886
887 // Drop the ref on c, it should be kept alive because it has a unique key.
888 c->unref();
889 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
890 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
891 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
892
893 // Verify that we can find c, then remove its unique key. It should get purged immediately.
894 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
895 c->resourcePriv().removeUniqueKey();
896 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800897 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
898 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800899 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700900
901 {
902 GrUniqueKey key2;
903 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400904 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700905 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700906 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700907 d->resourcePriv().setUniqueKey(key2);
908 }
909
910 GrUniqueKey key3;
911 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400912 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700913 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000914}
915
bsalomon8b79d232014-11-10 10:19:06 -0800916static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800917 Mock mock(5, 30000);
918 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800919 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800920
bsalomon8718aaf2015-02-19 07:24:21 -0800921 GrUniqueKey key1, key2, key3;
922 make_unique_key<0>(&key1, 1);
923 make_unique_key<0>(&key2, 2);
924 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700925
bsalomon23e619c2015-02-06 11:54:28 -0800926 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700927 TestResource* a = new TestResource(context->getGpu());
928 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700929 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800930 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800931 a->resourcePriv().setUniqueKey(key1);
932 b->resourcePriv().setUniqueKey(key2);
933 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800934 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800935 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800936 c->unref();
937
bsalomon8718aaf2015-02-19 07:24:21 -0800938 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
939 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
940 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800941 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800942
bsalomon8718aaf2015-02-19 07:24:21 -0800943 typedef GrUniqueKeyInvalidatedMessage Msg;
944 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800945
946 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
947 Bus::Post(Msg(key1));
948 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800949 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800950 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800951 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
952 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800953 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800954 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800955
956 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800957 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800958 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800959 // we still have a ref on b, c should be recycled as scratch.
960 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800961 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800962
bsalomon23e619c2015-02-06 11:54:28 -0800963 // make b purgeable. It should be immediately deleted since it has no key.
964 b->unref();
965 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
966
967 // Make sure we actually get to c via it's scratch key, before we say goodbye.
968 GrScratchKey scratchKey;
969 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700970 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800971 REPORTER_ASSERT(reporter, scratch == c);
972 SkSafeUnref(scratch);
973
974 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800975 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700976 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800977 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800978 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
979 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800980 REPORTER_ASSERT(reporter, !scratch);
981 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800982}
983
bsalomon71cb0c22014-11-14 12:10:14 -0800984static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800985 Mock mock(3, 30000);
986 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800987 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800988
bsalomon8718aaf2015-02-19 07:24:21 -0800989 GrUniqueKey key1, key2;
990 make_unique_key<0>(&key1, 1);
991 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000992
halcanary385fe4d2015-08-26 13:07:48 -0700993 TestResource* a = new TestResource(context->getGpu());
994 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800995 a->resourcePriv().setUniqueKey(key1);
996 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800997
bsalomonc2f35b72015-01-23 07:19:22 -0800998 // Make a cycle
999 a->setUnrefWhenDestroyed(b);
1000 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -08001001
bsalomonc2f35b72015-01-23 07:19:22 -08001002 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001003
bsalomonc2f35b72015-01-23 07:19:22 -08001004 a->unref();
1005 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001006
bsalomonc2f35b72015-01-23 07:19:22 -08001007 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001008
bsalomon0ea80f42015-02-11 10:49:59 -08001009 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001010 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001011
bsalomonc2f35b72015-01-23 07:19:22 -08001012 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001013 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001014 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001015
bsalomon0ea80f42015-02-11 10:49:59 -08001016 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001017 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001018}
1019
bsalomon8b79d232014-11-10 10:19:06 -08001020static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001021 GrUniqueKey key1, key2;
1022 make_unique_key<0>(&key1, 1);
1023 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001024
1025 // Test changing resources sizes (both increase & decrease).
1026 {
bsalomonc2f35b72015-01-23 07:19:22 -08001027 Mock mock(3, 30000);
1028 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001029 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001030
halcanary385fe4d2015-08-26 13:07:48 -07001031 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001032 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001033 a->unref();
1034
halcanary385fe4d2015-08-26 13:07:48 -07001035 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001036 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001037 b->unref();
1038
bsalomon0ea80f42015-02-11 10:49:59 -08001039 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1040 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001041 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001042 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001043 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001044 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001045 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001046 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001047 find1->setSize(50);
1048 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001049
bsalomon0ea80f42015-02-11 10:49:59 -08001050 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1051 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001052 }
1053
1054 // Test increasing a resources size beyond the cache budget.
1055 {
bsalomonc2f35b72015-01-23 07:19:22 -08001056 Mock mock(2, 300);
1057 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001058 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001059
halcanary385fe4d2015-08-26 13:07:48 -07001060 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001061 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001062 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001063 a->unref();
1064
halcanary385fe4d2015-08-26 13:07:48 -07001065 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001066 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001067 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001068 b->unref();
1069
bsalomon0ea80f42015-02-11 10:49:59 -08001070 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1071 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001072
bsalomon8b79d232014-11-10 10:19:06 -08001073 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001074 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001075 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001076 find2->setSize(201);
1077 }
bsalomon8718aaf2015-02-19 07:24:21 -08001078 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001079
bsalomon0ea80f42015-02-11 10:49:59 -08001080 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1081 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001082 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001083}
1084
bsalomonddf30e62015-02-19 11:38:44 -08001085static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1086 static const int kCount = 50;
1087 static const int kBudgetCnt = kCount / 2;
1088 static const int kLockedFreq = 8;
1089 static const int kBudgetSize = 0x80000000;
1090
1091 SkRandom random;
1092
1093 // Run the test 2*kCount times;
1094 for (int i = 0; i < 2 * kCount; ++i ) {
1095 Mock mock(kBudgetCnt, kBudgetSize);
1096 GrContext* context = mock.context();
1097 GrResourceCache* cache = mock.cache();
1098
1099 // Pick a random number of resources to add before the timestamp will wrap.
1100 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1101
1102 static const int kNumToPurge = kCount - kBudgetCnt;
1103
1104 SkTDArray<int> shouldPurgeIdxs;
1105 int purgeableCnt = 0;
1106 SkTDArray<GrGpuResource*> resourcesToUnref;
1107
1108 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1109 // unpurgeable resources.
1110 for (int j = 0; j < kCount; ++j) {
1111 GrUniqueKey key;
1112 make_unique_key<0>(&key, j);
1113
halcanary385fe4d2015-08-26 13:07:48 -07001114 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001115 r->resourcePriv().setUniqueKey(key);
1116 if (random.nextU() % kLockedFreq) {
1117 // Make this is purgeable.
1118 r->unref();
1119 ++purgeableCnt;
1120 if (purgeableCnt <= kNumToPurge) {
1121 *shouldPurgeIdxs.append() = j;
1122 }
1123 } else {
1124 *resourcesToUnref.append() = r;
1125 }
1126 }
1127
1128 // Verify that the correct resources were purged.
1129 int currShouldPurgeIdx = 0;
1130 for (int j = 0; j < kCount; ++j) {
1131 GrUniqueKey key;
1132 make_unique_key<0>(&key, j);
1133 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1134 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1135 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1136 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001137 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001138 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001139 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001140 }
1141 SkSafeUnref(res);
1142 }
1143
1144 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1145 resourcesToUnref[j]->unref();
1146 }
1147 }
1148}
1149
bsalomon3f324322015-04-08 11:01:54 -07001150static void test_flush(skiatest::Reporter* reporter) {
1151 Mock mock(1000000, 1000000);
1152 GrContext* context = mock.context();
1153 GrResourceCache* cache = mock.cache();
1154
1155 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1156 // power of two here to keep things simpler.
1157 static const int kFlushCount = 16;
1158 cache->setLimits(1000000, 1000000, kFlushCount);
1159
1160 {
1161 // Insert a resource and send a flush notification kFlushCount times.
1162 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001163 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001164 GrUniqueKey k;
1165 make_unique_key<1>(&k, i);
1166 r->resourcePriv().setUniqueKey(k);
1167 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001168 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001169 }
1170
1171 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001172 for (int i = 0; i < kFlushCount; ++i) {
1173 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001174 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1175 for (int j = 0; j < i; ++j) {
1176 GrUniqueKey k;
1177 make_unique_key<1>(&k, j);
1178 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1179 REPORTER_ASSERT(reporter, !SkToBool(r));
1180 SkSafeUnref(r);
1181 }
bsalomon3f324322015-04-08 11:01:54 -07001182 }
1183
1184 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1185 cache->purgeAllUnlocked();
1186 }
1187
1188 // Do a similar test but where we leave refs on some resources to prevent them from being
1189 // purged.
1190 {
1191 GrGpuResource* refedResources[kFlushCount >> 1];
1192 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001193 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001194 GrUniqueKey k;
1195 make_unique_key<1>(&k, i);
1196 r->resourcePriv().setUniqueKey(k);
1197 // Leave a ref on every other resource, beginning with the first.
1198 if (SkToBool(i & 0x1)) {
1199 refedResources[i/2] = r;
1200 } else {
1201 r->unref();
1202 }
bsalomonb77a9072016-09-07 10:02:04 -07001203 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001204 }
1205
1206 for (int i = 0; i < kFlushCount; ++i) {
1207 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001208 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001209 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001210 }
1211
1212 // Unref all the resources that we kept refs on in the first loop.
1213 for (int i = 0; i < kFlushCount >> 1; ++i) {
1214 refedResources[i]->unref();
1215 }
1216
bsalomone2e87f32016-09-22 12:42:11 -07001217 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1218 // kFlushCount full flushes.
1219 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001220 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001221 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001222 }
1223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1224
1225 cache->purgeAllUnlocked();
1226 }
1227
1228 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001229
1230 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1231 // eviction.
1232 context->flush();
1233 for (int i = 0; i < 10; ++i) {
1234 TestResource* r = new TestResource(context->getGpu());
1235 GrUniqueKey k;
1236 make_unique_key<1>(&k, i);
1237 r->resourcePriv().setUniqueKey(k);
1238 r->unref();
1239 }
1240 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1241 for (int i = 0; i < 10 * kFlushCount; ++i) {
1242 context->flush();
1243 }
1244 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001245}
1246
Brian Salomon5e150852017-03-22 14:53:13 -04001247static void test_time_purge(skiatest::Reporter* reporter) {
1248 Mock mock(1000000, 1000000);
1249 GrContext* context = mock.context();
1250 GrResourceCache* cache = mock.cache();
1251
1252 static constexpr int kCnts[] = {1, 10, 1024};
1253 auto nowish = []() {
1254 // We sleep so that we ensure we get a value that is greater than the last call to
1255 // GrStdSteadyClock::now().
1256 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1257 auto result = GrStdSteadyClock::now();
1258 // Also sleep afterwards so we don't get this value again.
1259 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1260 return result;
1261 };
1262
1263 for (int cnt : kCnts) {
1264 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1265 new GrStdSteadyClock::time_point[cnt]);
1266 {
1267 // Insert resources and get time points between each addition.
1268 for (int i = 0; i < cnt; ++i) {
1269 TestResource* r = new TestResource(context->getGpu());
1270 GrUniqueKey k;
1271 make_unique_key<1>(&k, i);
1272 r->resourcePriv().setUniqueKey(k);
1273 r->unref();
1274 timeStamps.get()[i] = nowish();
1275 }
1276
1277 // Purge based on the time points between resource additions. Each purge should remove
1278 // the oldest resource.
1279 for (int i = 0; i < cnt; ++i) {
1280 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1281 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1282 for (int j = 0; j < i; ++j) {
1283 GrUniqueKey k;
1284 make_unique_key<1>(&k, j);
1285 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1286 REPORTER_ASSERT(reporter, !SkToBool(r));
1287 SkSafeUnref(r);
1288 }
1289 }
1290
1291 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1292 cache->purgeAllUnlocked();
1293 }
1294
1295 // Do a similar test but where we leave refs on some resources to prevent them from being
1296 // purged.
1297 {
1298 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1299 for (int i = 0; i < cnt; ++i) {
1300 TestResource* r = new TestResource(context->getGpu());
1301 GrUniqueKey k;
1302 make_unique_key<1>(&k, i);
1303 r->resourcePriv().setUniqueKey(k);
1304 // Leave a ref on every other resource, beginning with the first.
1305 if (SkToBool(i & 0x1)) {
1306 refedResources.get()[i / 2] = r;
1307 } else {
1308 r->unref();
1309 }
1310 timeStamps.get()[i] = nowish();
1311 }
1312
1313 for (int i = 0; i < cnt; ++i) {
1314 // Should get a resource purged every other frame.
1315 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1316 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1317 }
1318
1319 // Unref all the resources that we kept refs on in the first loop.
1320 for (int i = 0; i < (cnt / 2); ++i) {
1321 refedResources.get()[i]->unref();
1322 cache->purgeResourcesNotUsedSince(nowish());
1323 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1324 }
1325
1326 cache->purgeAllUnlocked();
1327 }
1328
1329 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1330
1331 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1332 // eviction
1333 context->flush();
1334 for (int i = 0; i < 10; ++i) {
1335 TestResource* r = new TestResource(context->getGpu());
1336 GrUniqueKey k;
1337 make_unique_key<1>(&k, i);
1338 r->resourcePriv().setUniqueKey(k);
1339 r->unref();
1340 }
1341 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1342 context->flush();
1343 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1344 cache->purgeResourcesNotUsedSince(nowish());
1345 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1346 }
1347}
1348
Derek Sollenberger5480a182017-05-25 16:43:59 -04001349static void test_partial_purge(skiatest::Reporter* reporter) {
1350 Mock mock(6, 100);
1351 GrContext* context = mock.context();
1352 GrResourceCache* cache = mock.cache();
1353
1354 enum TestsCase {
1355 kOnlyScratch_TestCase = 0,
1356 kPartialScratch_TestCase = 1,
1357 kAllScratch_TestCase = 2,
1358 kPartial_TestCase = 3,
1359 kAll_TestCase = 4,
1360 kNone_TestCase = 5,
1361 kEndTests_TestCase = kNone_TestCase + 1
1362 };
1363
1364 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1365
1366 GrUniqueKey key1, key2, key3;
1367 make_unique_key<0>(&key1, 1);
1368 make_unique_key<0>(&key2, 2);
1369 make_unique_key<0>(&key3, 3);
1370
1371 // Add three unique resources to the cache.
1372 TestResource *unique1 = new TestResource(context->getGpu());
1373 TestResource *unique2 = new TestResource(context->getGpu());
1374 TestResource *unique3 = new TestResource(context->getGpu());
1375
1376 unique1->resourcePriv().setUniqueKey(key1);
1377 unique2->resourcePriv().setUniqueKey(key2);
1378 unique3->resourcePriv().setUniqueKey(key3);
1379
1380 unique1->setSize(10);
1381 unique2->setSize(11);
1382 unique3->setSize(12);
1383
1384 // Add two scratch resources to the cache.
1385 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1386 TestResource::kA_SimulatedProperty);
1387 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1388 TestResource::kB_SimulatedProperty);
1389 scratch1->setSize(13);
1390 scratch2->setSize(14);
1391
1392
1393 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1394 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1395 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1396
1397 // Add resources to the purgeable queue
1398 unique1->unref();
1399 scratch1->unref();
1400 unique2->unref();
1401 scratch2->unref();
1402 unique3->unref();
1403
1404 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1405 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1406 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1407
1408 switch(testCase) {
1409 case kOnlyScratch_TestCase: {
1410 context->purgeUnlockedResources(14, true);
1411 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1412 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1413 break;
1414 }
1415 case kPartialScratch_TestCase: {
1416 context->purgeUnlockedResources(3, true);
1417 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1418 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1419 break;
1420 }
1421 case kAllScratch_TestCase: {
1422 context->purgeUnlockedResources(50, true);
1423 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1424 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1425 break;
1426 }
1427 case kPartial_TestCase: {
1428 context->purgeUnlockedResources(13, false);
1429 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1430 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1431 break;
1432 }
1433 case kAll_TestCase: {
1434 context->purgeUnlockedResources(50, false);
1435 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1436 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1437 break;
1438 }
1439 case kNone_TestCase: {
1440 context->purgeUnlockedResources(0, true);
1441 context->purgeUnlockedResources(0, false);
1442 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1443 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1444 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1445 break;
1446 }
1447 };
1448
1449 // ensure all are purged before the next
1450 context->purgeAllUnlockedResources();
1451 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1452 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1453
1454 }
1455}
1456
bsalomon10e23ca2014-11-25 05:52:06 -08001457static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001458 // Set the cache size to double the resource count because we're going to create 2x that number
1459 // resources, using two different key domains. Add a little slop to the bytes because we resize
1460 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001461 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001462
bsalomonc2f35b72015-01-23 07:19:22 -08001463 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1464 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001465 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001466
1467 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001468 GrUniqueKey key1, key2;
1469 make_unique_key<1>(&key1, i);
1470 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001471
bsalomon24db3b12015-01-23 04:24:04 -08001472 TestResource* resource;
1473
halcanary385fe4d2015-08-26 13:07:48 -07001474 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001475 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001476 resource->setSize(1);
1477 resource->unref();
1478
halcanary385fe4d2015-08-26 13:07:48 -07001479 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001480 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001481 resource->setSize(1);
1482 resource->unref();
1483 }
1484
1485 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001486 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001487 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1488 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1489 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1490 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001491 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001492 GrUniqueKey key1, key2;
1493 make_unique_key<1>(&key1, i);
1494 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001495
bsalomon8718aaf2015-02-19 07:24:21 -08001496 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1497 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001498 }
1499
bsalomon0ea80f42015-02-11 10:49:59 -08001500 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001501 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001502 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001503 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1504 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1505 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1506 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001507
1508 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001509 GrUniqueKey key1, key2;
1510 make_unique_key<1>(&key1, i);
1511 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001512
bsalomon8718aaf2015-02-19 07:24:21 -08001513 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1514 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001515 }
1516}
1517
senorblanco84cd6212015-08-04 10:01:58 -07001518static void test_custom_data(skiatest::Reporter* reporter) {
1519 GrUniqueKey key1, key2;
1520 make_unique_key<0>(&key1, 1);
1521 make_unique_key<0>(&key2, 2);
1522 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001523 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001524 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1525 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1526
1527 // Test that copying a key also takes a ref on its custom data.
1528 GrUniqueKey key3 = key1;
1529 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1530}
1531
bsalomonc6363ef2015-09-24 07:07:40 -07001532static void test_abandoned(skiatest::Reporter* reporter) {
1533 Mock mock(10, 300);
1534 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001535 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001536 context->abandonContext();
1537
1538 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1539
1540 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1541
robertphillips8abb3702016-08-31 14:04:06 -07001542 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001543 resource->getUniqueKey();
1544 resource->wasDestroyed();
1545 resource->gpuMemorySize();
1546 resource->getContext();
1547
1548 resource->abandon();
1549 resource->resourcePriv().getScratchKey();
1550 resource->resourcePriv().isBudgeted();
1551 resource->resourcePriv().makeBudgeted();
1552 resource->resourcePriv().makeUnbudgeted();
1553 resource->resourcePriv().removeScratchKey();
1554 GrUniqueKey key;
1555 make_unique_key<0>(&key, 1);
1556 resource->resourcePriv().setUniqueKey(key);
1557 resource->resourcePriv().removeUniqueKey();
1558}
1559
Brian Salomon1090da62017-01-06 12:04:19 -05001560static void test_tags(skiatest::Reporter* reporter) {
1561#ifdef SK_DEBUG
1562 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1563 static constexpr int kLastTagIdx = 10;
1564 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1565
1566 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1567 GrContext* context = mock.context();
1568 GrResourceCache* cache = mock.cache();
1569
1570 SkString tagStr;
1571 int tagIdx = 0;
1572 int currTagCnt = 0;
1573
1574 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1575 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1576 GrUniqueKey key;
1577 if (currTagCnt == tagIdx) {
1578 tagIdx += 1;
1579 currTagCnt = 0;
1580 tagStr.printf("tag%d", tagIdx);
1581 }
1582 make_unique_key<1>(&key, i, tagStr.c_str());
1583 resource->resourcePriv().setUniqueKey(key);
1584 }
1585 SkASSERT(kLastTagIdx == tagIdx);
1586 SkASSERT(currTagCnt == kLastTagIdx);
1587
1588 // Test i = 0 to exercise unused tag string.
1589 for (int i = 0; i <= kLastTagIdx; ++i) {
1590 tagStr.printf("tag%d", i);
1591 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1592 }
1593#endif
1594}
1595
Brian Salomondcfca432017-11-15 15:48:03 -05001596DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001597 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001598 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001599 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001600 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001601 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001602 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001603 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001604 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001605 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001606 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001607 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001608 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001609 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001610 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001611 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001612 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001613 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001614 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001615 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001616 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001617}
1618
Robert Phillipsd6214d42016-11-07 08:23:48 -05001619////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001620static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001621 GrSurfaceFlags flags,
1622 int width, int height,
1623 int sampleCnt) {
1624 GrSurfaceDesc desc;
1625 desc.fFlags = flags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001626 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001627 desc.fWidth = width;
1628 desc.fHeight = height;
1629 desc.fConfig = kRGBA_8888_GrPixelConfig;
1630 desc.fSampleCnt = sampleCnt;
1631
Robert Phillipse78b7252017-04-06 07:59:41 -04001632 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001633}
1634
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001635static sk_sp<GrTextureProxy> make_mipmap_proxy(GrProxyProvider* provider,
Robert Phillipse78b7252017-04-06 07:59:41 -04001636 GrSurfaceFlags flags,
1637 int width, int height,
1638 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001639 SkBitmap bm;
1640
1641 bm.allocN32Pixels(width, height, true);
1642 bm.eraseColor(SK_ColorBLUE);
1643
Brian Osman7b8400d2016-11-08 17:08:54 -05001644 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001645 SkASSERT(mipmaps);
1646 SkASSERT(mipmaps->countLevels() > 1);
1647
1648 int mipLevelCount = mipmaps->countLevels() + 1;
1649
1650 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1651
1652 texels[0].fPixels = bm.getPixels();
1653 texels[0].fRowBytes = bm.rowBytes();
1654
1655 for (int i = 1; i < mipLevelCount; ++i) {
1656 SkMipMap::Level generatedMipLevel;
1657 mipmaps->getLevel(i - 1, &generatedMipLevel);
1658 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1659 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1660 }
1661
1662 GrSurfaceDesc desc;
1663 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001664 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1665 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001666 desc.fWidth = width;
1667 desc.fHeight = height;
1668 desc.fConfig = kRGBA_8888_GrPixelConfig;
1669 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001670
Robert Phillips8e8c7552017-07-10 12:06:05 -04001671 return GrSurfaceProxy::MakeDeferredMipMap(provider, desc, SkBudgeted::kYes,
1672 texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001673}
1674
1675// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1676// Texture-only, both-RT-and-Texture and MIPmapped
1677DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1678 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001679 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
1680 GrResourceProvider* resourceProvider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001681
Robert Phillipsd6214d42016-11-07 08:23:48 -05001682 static const int kSize = 64;
1683
Robert Phillipsd6214d42016-11-07 08:23:48 -05001684 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001685 {
1686 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001687
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001688 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001689 size_t size = tex->gpuMemorySize();
1690 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1691
Greg Daniel81e7bf82017-07-19 14:47:42 -04001692 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1693 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001694 tex = make_normal_texture(resourceProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001695 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001696 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001697 REPORTER_ASSERT(reporter,
1698 kSize*kSize*4 == size || // msaa4 failed
1699 kSize*kSize*4*sampleCount == size || // auto-resolving
1700 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001701 }
1702
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001703 tex = make_normal_texture(resourceProvider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001704 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001705 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001706 }
1707
Robert Phillipsd6214d42016-11-07 08:23:48 -05001708
1709 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001710 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001711 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001712
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001713 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001714 size_t size = proxy->gpuMemorySize();
1715 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1716
Greg Daniel81e7bf82017-07-19 14:47:42 -04001717 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1718 if (sampleCount >= 4) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001719 proxy = make_mipmap_proxy(proxyProvider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001720 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001721 size = proxy->gpuMemorySize();
1722 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001723 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1724 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1725 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001726 }
Robert Phillips1b352562017-04-05 18:56:21 +00001727
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001728 proxy = make_mipmap_proxy(proxyProvider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipse78b7252017-04-06 07:59:41 -04001729 size = proxy->gpuMemorySize();
1730 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1731 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001732}
1733
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001734#endif