blob: d0666d5466a05d6f5abe356d9a80f04082bb8c1f [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);
jvanverth672bb7f2015-07-13 07:19:57 -0700214
bsalomon6dc6f5f2015-06-18 09:12:16 -0700215 context->resetContext();
216
Brian Osman32342f02017-03-04 08:12:46 -0500217 sk_sp<GrTexture> borrowed(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500218 backendTextures[0], kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700219
Brian Osman32342f02017-03-04 08:12:46 -0500220 sk_sp<GrTexture> adopted(context->resourceProvider()->wrapBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500221 backendTextures[1], kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700222
Brian Osman85d34b22017-05-10 12:06:26 -0400223 REPORTER_ASSERT(reporter, borrowed != nullptr && adopted != nullptr);
224 if (!borrowed || !adopted) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225 return;
226 }
227
halcanary96fcdcc2015-08-27 07:41:13 -0700228 borrowed.reset(nullptr);
229 adopted.reset(nullptr);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700230
231 context->flush();
232
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500233 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[0]);
234 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(backendTextures[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700235
236 REPORTER_ASSERT(reporter, borrowedIsAlive);
237 REPORTER_ASSERT(reporter, !adoptedIsAlive);
238
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500239 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[0]), !borrowedIsAlive);
240 gpu->deleteTestingOnlyBackendTexture(&(backendTextures[1]), !adoptedIsAlive);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700241
242 context->resetContext();
243}
244
bsalomon6d3fe022014-07-25 08:35:45 -0700245class TestResource : public GrGpuResource {
bsalomon1c60dfe2015-01-21 09:32:40 -0800246 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000247public:
robertphillips6e83ac72015-08-13 05:19:14 -0700248 static const size_t kDefaultSize = 100;
mtklein5f939ab2016-03-16 10:28:35 -0700249
bsalomon1c60dfe2015-01-21 09:32:40 -0800250 /** Property that distinctly categorizes the resource.
251 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800252 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800253
kkinnunen2e6055b2016-04-22 01:48:29 -0700254 TestResource(GrGpu* gpu, SkBudgeted budgeted = SkBudgeted::kYes, size_t size = kDefaultSize)
255 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700256 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 , fSize(size)
kkinnunen2e6055b2016-04-22 01:48:29 -0700258 , fProperty(kA_SimulatedProperty)
259 , fIsScratch(false) {
bsalomon5236cf42015-01-14 10:42:08 -0800260 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700261 this->registerWithCache(budgeted);
bsalomon5236cf42015-01-14 10:42:08 -0800262 }
263
kkinnunen2e6055b2016-04-22 01:48:29 -0700264 static TestResource* CreateScratch(GrGpu* gpu, SkBudgeted budgeted,
265 SimulatedProperty property) {
266 return new TestResource(gpu, budgeted, property, kScratchConstructor);
bsalomondace19e2014-11-17 07:34:06 -0800267 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700268 static TestResource* CreateWrapped(GrGpu* gpu, size_t size = kDefaultSize) {
269 return new TestResource(gpu, size);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000270 }
271
Brian Salomond3b65972017-03-22 12:05:03 -0400272 ~TestResource() override {
bsalomon33435572014-11-05 14:47:41 -0800273 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800274 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000275 }
276
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000277 void setSize(size_t size) {
278 fSize = size;
279 this->didChangeGpuMemorySize();
280 }
281
bsalomon33435572014-11-05 14:47:41 -0800282 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283
bsalomon71cb0c22014-11-14 12:10:14 -0800284 void setUnrefWhenDestroyed(TestResource* resource) {
285 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000286 }
287
bsalomon1c60dfe2015-01-21 09:32:40 -0800288 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
289 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
290 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800291 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
292 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800293 }
294 }
295
296 static size_t ExpectedScratchKeySize() {
297 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
298 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000299private:
bsalomon24db3b12015-01-23 04:24:04 -0800300 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800301
kkinnunen2e6055b2016-04-22 01:48:29 -0700302 TestResource(GrGpu* gpu, SkBudgeted budgeted, SimulatedProperty property, ScratchConstructor)
303 : INHERITED(gpu)
halcanary96fcdcc2015-08-27 07:41:13 -0700304 , fToDelete(nullptr)
bsalomon1c60dfe2015-01-21 09:32:40 -0800305 , fSize(kDefaultSize)
kkinnunen2e6055b2016-04-22 01:48:29 -0700306 , fProperty(property)
307 , fIsScratch(true) {
bsalomon1c60dfe2015-01-21 09:32:40 -0800308 ++fNumAlive;
kkinnunen2e6055b2016-04-22 01:48:29 -0700309 this->registerWithCache(budgeted);
310 }
311
312 // Constructor for simulating resources that wrap backend objects.
313 TestResource(GrGpu* gpu, size_t size)
314 : INHERITED(gpu)
315 , fToDelete(nullptr)
316 , fSize(size)
317 , fProperty(kA_SimulatedProperty)
318 , fIsScratch(false) {
319 ++fNumAlive;
320 this->registerWithCacheWrapped();
321 }
322
323 void computeScratchKey(GrScratchKey* key) const override {
324 if (fIsScratch) {
325 ComputeScratchKey(fProperty, key);
326 }
bsalomon1c60dfe2015-01-21 09:32:40 -0800327 }
328
mtklein36352bf2015-03-25 18:17:31 -0700329 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800330
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000331 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000332 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800333 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800334 SimulatedProperty fProperty;
kkinnunen2e6055b2016-04-22 01:48:29 -0700335 bool fIsScratch;
bsalomon6d3fe022014-07-25 08:35:45 -0700336 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000337};
bsalomon33435572014-11-05 14:47:41 -0800338int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000339
bsalomonc2f35b72015-01-23 07:19:22 -0800340class Mock {
341public:
342 Mock(int maxCnt, size_t maxBytes) {
Greg Daniel02611d92017-07-25 10:05:01 -0400343 fContext = GrContext::MakeMock(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -0800344 SkASSERT(fContext);
345 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800346 GrResourceCache* cache = fContext->getResourceCache();
347 cache->purgeAllUnlocked();
348 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800349 }
bsalomonc2f35b72015-01-23 07:19:22 -0800350
bsalomon0ea80f42015-02-11 10:49:59 -0800351 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800352
Hal Canary342b7ac2016-11-04 11:49:42 -0400353 GrContext* context() { return fContext.get(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800354
355private:
Hal Canary342b7ac2016-11-04 11:49:42 -0400356 sk_sp<GrContext> fContext;
bsalomonc2f35b72015-01-23 07:19:22 -0800357};
358
359static void test_no_key(skiatest::Reporter* reporter) {
360 Mock mock(10, 30000);
361 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800362 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800363
364 // Create a bunch of resources with no keys
halcanary385fe4d2015-08-26 13:07:48 -0700365 TestResource* a = new TestResource(context->getGpu());
366 TestResource* b = new TestResource(context->getGpu());
367 TestResource* c = new TestResource(context->getGpu());
368 TestResource* d = new TestResource(context->getGpu());
bsalomon71cb0c22014-11-14 12:10:14 -0800369 a->setSize(11);
370 b->setSize(12);
371 c->setSize(13);
372 d->setSize(14);
373
374 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800375 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800376 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800377 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800378
379 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800380 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800381 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
382
bsalomon8718aaf2015-02-19 07:24:21 -0800383 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800384
385 a->unref();
386 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800387 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800388 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800390
391 c->unref();
392 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800393 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800394 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800395 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800396
397 d->unref();
398 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800399 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
400 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800401
402 b->unref();
403 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800404 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
405 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800406}
407
bsalomon24db3b12015-01-23 04:24:04 -0800408// Each integer passed as a template param creates a new domain.
Brian Salomon1090da62017-01-06 12:04:19 -0500409template <int>
410static void make_unique_key(GrUniqueKey* key, int data, const char* tag = nullptr) {
bsalomon8718aaf2015-02-19 07:24:21 -0800411 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
Brian Salomon1090da62017-01-06 12:04:19 -0500412 GrUniqueKey::Builder builder(key, d, 1, tag);
bsalomon24db3b12015-01-23 04:24:04 -0800413 builder[0] = data;
414}
415
bsalomon84c8e622014-11-17 09:33:27 -0800416static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800417 Mock mock(10, 300);
418 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800419 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800420
bsalomon8718aaf2015-02-19 07:24:21 -0800421 GrUniqueKey uniqueKey;
422 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800423
bsalomon8718aaf2015-02-19 07:24:21 -0800424 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800425 TestResource* scratch =
kkinnunen2e6055b2016-04-22 01:48:29 -0700426 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes, TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800427 scratch->setSize(10);
halcanary385fe4d2015-08-26 13:07:48 -0700428 TestResource* unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800429 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800430 unique->resourcePriv().setUniqueKey(uniqueKey);
kkinnunen2e6055b2016-04-22 01:48:29 -0700431 TestResource* wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomon5236cf42015-01-14 10:42:08 -0800432 wrapped->setSize(12);
halcanary385fe4d2015-08-26 13:07:48 -0700433 TestResource* unbudgeted =
kkinnunen2e6055b2016-04-22 01:48:29 -0700434 new TestResource(context->getGpu(), SkBudgeted::kNo);
bsalomon84c8e622014-11-17 09:33:27 -0800435 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800436
Brian Osman0562eb92017-05-08 11:16:39 -0400437 // Make sure we can add a unique key to the wrapped resource
bsalomon8718aaf2015-02-19 07:24:21 -0800438 GrUniqueKey uniqueKey2;
439 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800440 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
Brian Osman0562eb92017-05-08 11:16:39 -0400441 GrGpuResource* wrappedViaKey = cache->findAndRefUniqueResource(uniqueKey2);
442 REPORTER_ASSERT(reporter, wrappedViaKey != nullptr);
443
444 // Remove the extra ref we just added.
445 wrappedViaKey->unref();
bsalomondace19e2014-11-17 07:34:06 -0800446
447 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800448 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800449 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800450 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800451 cache->getResourceBytes());
452 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800453 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800454 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400455 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800456
bsalomon63c992f2015-01-23 12:47:59 -0800457 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800458 cache->purgeAllUnlocked();
459 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800460 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800461 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800462 cache->getResourceBytes());
463 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800464 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800465 cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400466 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800467
468 // Unreffing the wrapped resource should free it right away.
469 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800470 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800471 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800472 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400473 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800474
bsalomon84c8e622014-11-17 09:33:27 -0800475 // Now try freeing the budgeted resources first
kkinnunen2e6055b2016-04-22 01:48:29 -0700476 wrapped = TestResource::CreateWrapped(context->getGpu());
bsalomondace19e2014-11-17 07:34:06 -0800477 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800478 unique->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400479 REPORTER_ASSERT(reporter, 11 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800480 cache->purgeAllUnlocked();
481 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800482 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800483 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
484 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
485 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400486 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800487
488 scratch->unref();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400489 REPORTER_ASSERT(reporter, 12 == cache->getPurgeableBytes());
bsalomon0ea80f42015-02-11 10:49:59 -0800490 cache->purgeAllUnlocked();
491 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800492 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800493 cache->getResourceBytes());
494 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
495 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400496 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800497
498 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800499 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
500 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
501 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
502 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400503 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800504
505 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800506 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
507 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
508 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
509 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400510 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomondace19e2014-11-17 07:34:06 -0800511}
512
bsalomon5236cf42015-01-14 10:42:08 -0800513static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800514 Mock mock(10, 30000);
515 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800516 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800517
bsalomon8718aaf2015-02-19 07:24:21 -0800518 GrUniqueKey uniqueKey;
519 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800520
521 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800522 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800523 TestResource* wrapped;
524 TestResource* unbudgeted;
525
526 // A large uncached or wrapped resource shouldn't evict anything.
kkinnunen2e6055b2016-04-22 01:48:29 -0700527 scratch = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
528 TestResource::kB_SimulatedProperty);
529
bsalomon5236cf42015-01-14 10:42:08 -0800530 scratch->setSize(10);
531 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400536 REPORTER_ASSERT(reporter, 10 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800537
halcanary385fe4d2015-08-26 13:07:48 -0700538 unique = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800539 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800540 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800541 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800542 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
543 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
544 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
545 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400546 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800547
bsalomon0ea80f42015-02-11 10:49:59 -0800548 size_t large = 2 * cache->getResourceBytes();
kkinnunen2e6055b2016-04-22 01:48:29 -0700549 unbudgeted = new TestResource(context->getGpu(), SkBudgeted::kNo, large);
bsalomon0ea80f42015-02-11 10:49:59 -0800550 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400554 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800555
556 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800557 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
558 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
559 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
560 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400561 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800562
kkinnunen2e6055b2016-04-22 01:48:29 -0700563 wrapped = TestResource::CreateWrapped(context->getGpu(), large);
bsalomon0ea80f42015-02-11 10:49:59 -0800564 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
565 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
566 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
567 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400568 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800569
570 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800571 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
572 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
573 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
574 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400575 REPORTER_ASSERT(reporter, 21 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800576
bsalomon0ea80f42015-02-11 10:49:59 -0800577 cache->purgeAllUnlocked();
578 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
579 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
580 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
581 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400582 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800583}
584
bsalomon3582d3e2015-02-13 14:20:05 -0800585// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
586void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
587/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800588 Mock mock(10, 300);
589 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800590 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800591
592 TestResource* resource =
kkinnunen2e6055b2016-04-22 01:48:29 -0700593 TestResource::CreateScratch(context->getGpu(), SkBudgeted::kNo,
594 TestResource::kA_SimulatedProperty);
bsalomonc2f35b72015-01-23 07:19:22 -0800595 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800596 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800597
598 size_t size = resource->gpuMemorySize();
599 for (int i = 0; i < 2; ++i) {
600 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800601 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800602 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800603 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().isBudgeted());
halcanary96fcdcc2015-08-27 07:41:13 -0700604 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomon0ea80f42015-02-11 10:49:59 -0800605 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
606 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
607 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
608 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400609 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800610
611 // Once it is unrefed, it should become available as scratch.
612 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800613 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
614 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
615 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
616 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400617 REPORTER_ASSERT(reporter, size == cache->getPurgeableBytes());
robertphillips6e83ac72015-08-13 05:19:14 -0700618 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
bsalomonc2f35b72015-01-23 07:19:22 -0800619 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800620 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800621 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800622 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800623
624 if (0 == i) {
mtklein5f939ab2016-03-16 10:28:35 -0700625 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
bsalomonc2f35b72015-01-23 07:19:22 -0800626 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800627 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800628 } else {
629 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800630 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800631 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
632 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
633 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
634 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400635 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800636 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800637 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon5ec26ae2016-02-25 08:33:02 -0800638 REPORTER_ASSERT(reporter, SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800639
640 // now when it is unrefed it should die since it has no key.
641 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800642 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
643 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
644 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
645 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
Derek Sollenbergeree479142017-05-24 11:41:33 -0400646 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800647 }
bsalomon8b79d232014-11-10 10:19:06 -0800648 }
bsalomonc2f35b72015-01-23 07:19:22 -0800649}
650
651static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
652 Mock mock(5, 30000);
653 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800654 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800655
bsalomon8b79d232014-11-10 10:19:06 -0800656 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800657 TestResource* a = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700658 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800659 TestResource::kB_SimulatedProperty);
660 TestResource* b = TestResource::CreateScratch(context->getGpu(),
kkinnunen2e6055b2016-04-22 01:48:29 -0700661 SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800662 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800663 a->setSize(11);
664 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800665 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800666 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800667 // Check for negative case consistency. (leaks upon test failure.)
halcanary96fcdcc2015-08-27 07:41:13 -0700668 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
bsalomon1c60dfe2015-01-21 09:32:40 -0800669
670 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800671 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800672
bsalomon0ea80f42015-02-11 10:49:59 -0800673 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800674 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800675 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
676 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800677 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800678 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800679
bsalomon63c992f2015-01-23 12:47:59 -0800680 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800681 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800682 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800683 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800684
685 // Unref but don't purge
686 a->unref();
687 b->unref();
688 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800689 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800690
bsalomon63c992f2015-01-23 12:47:59 -0800691 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800692 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800693 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800694 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
695 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800696}
697
bsalomon10e23ca2014-11-25 05:52:06 -0800698static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800699 Mock mock(5, 30000);
700 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800701 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800702
bsalomon10e23ca2014-11-25 05:52:06 -0800703 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700704 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800705 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700706 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800707 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800708 a->unref();
709 b->unref();
710
bsalomon1c60dfe2015-01-21 09:32:40 -0800711 GrScratchKey scratchKey;
712 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800713 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800714 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700715 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800716
bsalomon0ea80f42015-02-11 10:49:59 -0800717 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800718 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800719 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800720 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
721 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800722
723 // Find the first resource and remove its scratch key
724 GrGpuResource* find;
robertphillips6e83ac72015-08-13 05:19:14 -0700725 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800726 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800727 // It's still alive, but not cached by scratch key anymore
728 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800729 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
730 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800731
732 // The cache should immediately delete it when it's unrefed since it isn't accessible.
733 find->unref();
734 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800735 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
736 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800737
738 // Repeat for the second resource.
robertphillips6e83ac72015-08-13 05:19:14 -0700739 find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon3582d3e2015-02-13 14:20:05 -0800740 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800741 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800742 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
743 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800744
745 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800746 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800747 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800748 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
749 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800750
751 find->unref();
752 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800753 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
754 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800755}
756
bsalomon1c60dfe2015-01-21 09:32:40 -0800757static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800758 Mock mock(5, 30000);
759 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800760 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800761
762 // Create two resources that have the same scratch key.
kkinnunen2e6055b2016-04-22 01:48:29 -0700763 TestResource* a = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800764 TestResource::kB_SimulatedProperty);
kkinnunen2e6055b2016-04-22 01:48:29 -0700765 TestResource* b = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800766 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800767 a->unref();
768 b->unref();
769
770 GrScratchKey scratchKey;
771 // Ensure that scratch key comparison and assignment is consistent.
772 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800773 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800774 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800775 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800776 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
777 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
778 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
779 scratchKey = scratchKey1;
780 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
781 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
782 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
783 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
784 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
785 scratchKey = scratchKey2;
786 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
787 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
788 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
789 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
790 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
791
792 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800793 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800794 // (following leaks upon test failure).
halcanary96fcdcc2015-08-27 07:41:13 -0700795 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800796
797 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800798 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700799 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700800 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800801 find->unref();
802
803 scratchKey2 = scratchKey;
robertphillips6e83ac72015-08-13 05:19:14 -0700804 find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700805 REPORTER_ASSERT(reporter, find != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800806 REPORTER_ASSERT(reporter, find == a || find == b);
807
robertphillips6e83ac72015-08-13 05:19:14 -0700808 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700809 REPORTER_ASSERT(reporter, find2 != nullptr);
bsalomon1c60dfe2015-01-21 09:32:40 -0800810 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
811 REPORTER_ASSERT(reporter, find2 != find);
812 find2->unref();
813 find->unref();
814}
815
bsalomon8718aaf2015-02-19 07:24:21 -0800816static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800817 Mock mock(5, 30000);
818 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800819 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000820
bsalomon8718aaf2015-02-19 07:24:21 -0800821 GrUniqueKey key;
822 make_unique_key<0>(&key, 0);
mtklein5f939ab2016-03-16 10:28:35 -0700823
bsalomon8718aaf2015-02-19 07:24:21 -0800824 // Create two resources that we will attempt to register with the same unique key.
halcanary385fe4d2015-08-26 13:07:48 -0700825 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -0800826 a->setSize(11);
mtklein5f939ab2016-03-16 10:28:35 -0700827
bsalomonf99e9612015-02-19 08:24:16 -0800828 // Set key on resource a.
829 a->resourcePriv().setUniqueKey(key);
830 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
831 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800832
bsalomonf99e9612015-02-19 08:24:16 -0800833 // Make sure that redundantly setting a's key works.
834 a->resourcePriv().setUniqueKey(key);
835 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800836 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800837 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
838 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800839 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
840
bsalomonf99e9612015-02-19 08:24:16 -0800841 // Create resource b and set the same key. It should replace a's unique key cache entry.
halcanary385fe4d2015-08-26 13:07:48 -0700842 TestResource* b = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800843 b->setSize(12);
844 b->resourcePriv().setUniqueKey(key);
845 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
846 b->unref();
847
848 // Still have two resources because a is still reffed.
849 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
850 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
851 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
852
853 a->unref();
854 // Now a should be gone.
855 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
856 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
857 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
858
859 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
860 // Also make b be unreffed when replacement occurs.
861 b->unref();
halcanary385fe4d2015-08-26 13:07:48 -0700862 TestResource* c = new TestResource(context->getGpu());
bsalomonf99e9612015-02-19 08:24:16 -0800863 GrUniqueKey differentKey;
864 make_unique_key<0>(&differentKey, 1);
865 c->setSize(13);
866 c->resourcePriv().setUniqueKey(differentKey);
867 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
868 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
869 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
870 // c replaces b and b should be immediately purged.
871 c->resourcePriv().setUniqueKey(key);
872 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
873 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
874 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
875
876 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800877 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800878 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
879 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
880 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
881
882 // Drop the ref on c, it should be kept alive because it has a unique key.
883 c->unref();
884 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
885 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
886 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
887
888 // Verify that we can find c, then remove its unique key. It should get purged immediately.
889 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
890 c->resourcePriv().removeUniqueKey();
891 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800892 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
893 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800894 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700895
896 {
897 GrUniqueKey key2;
898 make_unique_key<0>(&key2, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400899 sk_sp<TestResource> d(new TestResource(context->getGpu()));
senorblanco84cd6212015-08-04 10:01:58 -0700900 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -0700901 key2.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -0700902 d->resourcePriv().setUniqueKey(key2);
903 }
904
905 GrUniqueKey key3;
906 make_unique_key<0>(&key3, 0);
Hal Canary342b7ac2016-11-04 11:49:42 -0400907 sk_sp<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
senorblanco84cd6212015-08-04 10:01:58 -0700908 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000909}
910
bsalomon8b79d232014-11-10 10:19:06 -0800911static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800912 Mock mock(5, 30000);
913 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800914 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800915
bsalomon8718aaf2015-02-19 07:24:21 -0800916 GrUniqueKey key1, key2, key3;
917 make_unique_key<0>(&key1, 1);
918 make_unique_key<0>(&key2, 2);
919 make_unique_key<0>(&key3, 3);
mtklein5f939ab2016-03-16 10:28:35 -0700920
bsalomon23e619c2015-02-06 11:54:28 -0800921 // Add three resources to the cache. Only c is usable as scratch.
halcanary385fe4d2015-08-26 13:07:48 -0700922 TestResource* a = new TestResource(context->getGpu());
923 TestResource* b = new TestResource(context->getGpu());
kkinnunen2e6055b2016-04-22 01:48:29 -0700924 TestResource* c = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
bsalomon23e619c2015-02-06 11:54:28 -0800925 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800926 a->resourcePriv().setUniqueKey(key1);
927 b->resourcePriv().setUniqueKey(key2);
928 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800929 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800930 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800931 c->unref();
932
bsalomon8718aaf2015-02-19 07:24:21 -0800933 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
934 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
935 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800936 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800937
bsalomon8718aaf2015-02-19 07:24:21 -0800938 typedef GrUniqueKeyInvalidatedMessage Msg;
939 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800940
941 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
942 Bus::Post(Msg(key1));
943 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800944 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800945 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800946 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
947 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800948 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800949 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800950
951 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800952 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800953 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800954 // we still have a ref on b, c should be recycled as scratch.
955 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800956 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800957
bsalomon23e619c2015-02-06 11:54:28 -0800958 // make b purgeable. It should be immediately deleted since it has no key.
959 b->unref();
960 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
961
962 // Make sure we actually get to c via it's scratch key, before we say goodbye.
963 GrScratchKey scratchKey;
964 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
robertphillips6e83ac72015-08-13 05:19:14 -0700965 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon23e619c2015-02-06 11:54:28 -0800966 REPORTER_ASSERT(reporter, scratch == c);
967 SkSafeUnref(scratch);
968
969 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800970 cache->purgeAllUnlocked();
robertphillips6e83ac72015-08-13 05:19:14 -0700971 scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
bsalomon71cb0c22014-11-14 12:10:14 -0800972 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800973 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
974 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800975 REPORTER_ASSERT(reporter, !scratch);
976 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800977}
978
bsalomon71cb0c22014-11-14 12:10:14 -0800979static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800980 Mock mock(3, 30000);
981 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800982 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800983
bsalomon8718aaf2015-02-19 07:24:21 -0800984 GrUniqueKey key1, key2;
985 make_unique_key<0>(&key1, 1);
986 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000987
halcanary385fe4d2015-08-26 13:07:48 -0700988 TestResource* a = new TestResource(context->getGpu());
989 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -0800990 a->resourcePriv().setUniqueKey(key1);
991 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800992
bsalomonc2f35b72015-01-23 07:19:22 -0800993 // Make a cycle
994 a->setUnrefWhenDestroyed(b);
995 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800996
bsalomonc2f35b72015-01-23 07:19:22 -0800997 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800998
bsalomonc2f35b72015-01-23 07:19:22 -0800999 a->unref();
1000 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -08001001
bsalomonc2f35b72015-01-23 07:19:22 -08001002 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001003
bsalomon0ea80f42015-02-11 10:49:59 -08001004 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001005 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -08001006
bsalomonc2f35b72015-01-23 07:19:22 -08001007 // Break the cycle
halcanary96fcdcc2015-08-27 07:41:13 -07001008 a->setUnrefWhenDestroyed(nullptr);
bsalomonc2f35b72015-01-23 07:19:22 -08001009 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -08001010
bsalomon0ea80f42015-02-11 10:49:59 -08001011 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -08001012 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +00001013}
1014
bsalomon8b79d232014-11-10 10:19:06 -08001015static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -08001016 GrUniqueKey key1, key2;
1017 make_unique_key<0>(&key1, 1);
1018 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001019
1020 // Test changing resources sizes (both increase & decrease).
1021 {
bsalomonc2f35b72015-01-23 07:19:22 -08001022 Mock mock(3, 30000);
1023 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001024 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001025
halcanary385fe4d2015-08-26 13:07:48 -07001026 TestResource* a = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001027 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001028 a->unref();
1029
halcanary385fe4d2015-08-26 13:07:48 -07001030 TestResource* b = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001031 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001032 b->unref();
1033
bsalomon0ea80f42015-02-11 10:49:59 -08001034 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1035 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -08001036 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001037 sk_sp<TestResource> find2(
bsalomon8718aaf2015-02-19 07:24:21 -08001038 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001039 find2->setSize(200);
Hal Canary342b7ac2016-11-04 11:49:42 -04001040 sk_sp<TestResource> find1(
bsalomon8718aaf2015-02-19 07:24:21 -08001041 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -08001042 find1->setSize(50);
1043 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001044
bsalomon0ea80f42015-02-11 10:49:59 -08001045 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1046 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001047 }
1048
1049 // Test increasing a resources size beyond the cache budget.
1050 {
bsalomonc2f35b72015-01-23 07:19:22 -08001051 Mock mock(2, 300);
1052 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001053 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001054
halcanary385fe4d2015-08-26 13:07:48 -07001055 TestResource* a = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001056 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001057 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001058 a->unref();
1059
halcanary385fe4d2015-08-26 13:07:48 -07001060 TestResource* b = new TestResource(context->getGpu());
bsalomon8b79d232014-11-10 10:19:06 -08001061 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001062 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001063 b->unref();
1064
bsalomon0ea80f42015-02-11 10:49:59 -08001065 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1066 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001067
bsalomon8b79d232014-11-10 10:19:06 -08001068 {
Hal Canary342b7ac2016-11-04 11:49:42 -04001069 sk_sp<TestResource> find2(static_cast<TestResource*>(
bsalomon8718aaf2015-02-19 07:24:21 -08001070 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001071 find2->setSize(201);
1072 }
bsalomon8718aaf2015-02-19 07:24:21 -08001073 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001074
bsalomon0ea80f42015-02-11 10:49:59 -08001075 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1076 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001077 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001078}
1079
bsalomonddf30e62015-02-19 11:38:44 -08001080static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1081 static const int kCount = 50;
1082 static const int kBudgetCnt = kCount / 2;
1083 static const int kLockedFreq = 8;
1084 static const int kBudgetSize = 0x80000000;
1085
1086 SkRandom random;
1087
1088 // Run the test 2*kCount times;
1089 for (int i = 0; i < 2 * kCount; ++i ) {
1090 Mock mock(kBudgetCnt, kBudgetSize);
1091 GrContext* context = mock.context();
1092 GrResourceCache* cache = mock.cache();
1093
1094 // Pick a random number of resources to add before the timestamp will wrap.
1095 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1096
1097 static const int kNumToPurge = kCount - kBudgetCnt;
1098
1099 SkTDArray<int> shouldPurgeIdxs;
1100 int purgeableCnt = 0;
1101 SkTDArray<GrGpuResource*> resourcesToUnref;
1102
1103 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1104 // unpurgeable resources.
1105 for (int j = 0; j < kCount; ++j) {
1106 GrUniqueKey key;
1107 make_unique_key<0>(&key, j);
1108
halcanary385fe4d2015-08-26 13:07:48 -07001109 TestResource* r = new TestResource(context->getGpu());
bsalomonddf30e62015-02-19 11:38:44 -08001110 r->resourcePriv().setUniqueKey(key);
1111 if (random.nextU() % kLockedFreq) {
1112 // Make this is purgeable.
1113 r->unref();
1114 ++purgeableCnt;
1115 if (purgeableCnt <= kNumToPurge) {
1116 *shouldPurgeIdxs.append() = j;
1117 }
1118 } else {
1119 *resourcesToUnref.append() = r;
1120 }
1121 }
1122
1123 // Verify that the correct resources were purged.
1124 int currShouldPurgeIdx = 0;
1125 for (int j = 0; j < kCount; ++j) {
1126 GrUniqueKey key;
1127 make_unique_key<0>(&key, j);
1128 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1129 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1130 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1131 ++currShouldPurgeIdx;
halcanary96fcdcc2015-08-27 07:41:13 -07001132 REPORTER_ASSERT(reporter, nullptr == res);
bsalomonddf30e62015-02-19 11:38:44 -08001133 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001134 REPORTER_ASSERT(reporter, nullptr != res);
bsalomonddf30e62015-02-19 11:38:44 -08001135 }
1136 SkSafeUnref(res);
1137 }
1138
1139 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1140 resourcesToUnref[j]->unref();
1141 }
1142 }
1143}
1144
bsalomon3f324322015-04-08 11:01:54 -07001145static void test_flush(skiatest::Reporter* reporter) {
1146 Mock mock(1000000, 1000000);
1147 GrContext* context = mock.context();
1148 GrResourceCache* cache = mock.cache();
1149
1150 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1151 // power of two here to keep things simpler.
1152 static const int kFlushCount = 16;
1153 cache->setLimits(1000000, 1000000, kFlushCount);
1154
1155 {
1156 // Insert a resource and send a flush notification kFlushCount times.
1157 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001158 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001159 GrUniqueKey k;
1160 make_unique_key<1>(&k, i);
1161 r->resourcePriv().setUniqueKey(k);
1162 r->unref();
bsalomonb77a9072016-09-07 10:02:04 -07001163 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001164 }
1165
1166 // Send flush notifications to the cache. Each flush should purge the oldest resource.
bsalomone2e87f32016-09-22 12:42:11 -07001167 for (int i = 0; i < kFlushCount; ++i) {
1168 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001169 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1170 for (int j = 0; j < i; ++j) {
1171 GrUniqueKey k;
1172 make_unique_key<1>(&k, j);
1173 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1174 REPORTER_ASSERT(reporter, !SkToBool(r));
1175 SkSafeUnref(r);
1176 }
bsalomon3f324322015-04-08 11:01:54 -07001177 }
1178
1179 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1180 cache->purgeAllUnlocked();
1181 }
1182
1183 // Do a similar test but where we leave refs on some resources to prevent them from being
1184 // purged.
1185 {
1186 GrGpuResource* refedResources[kFlushCount >> 1];
1187 for (int i = 0; i < kFlushCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -07001188 TestResource* r = new TestResource(context->getGpu());
bsalomon3f324322015-04-08 11:01:54 -07001189 GrUniqueKey k;
1190 make_unique_key<1>(&k, i);
1191 r->resourcePriv().setUniqueKey(k);
1192 // Leave a ref on every other resource, beginning with the first.
1193 if (SkToBool(i & 0x1)) {
1194 refedResources[i/2] = r;
1195 } else {
1196 r->unref();
1197 }
bsalomonb77a9072016-09-07 10:02:04 -07001198 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001199 }
1200
1201 for (int i = 0; i < kFlushCount; ++i) {
1202 // Should get a resource purged every other flush.
bsalomonb77a9072016-09-07 10:02:04 -07001203 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomone2e87f32016-09-22 12:42:11 -07001204 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001205 }
1206
1207 // Unref all the resources that we kept refs on in the first loop.
1208 for (int i = 0; i < kFlushCount >> 1; ++i) {
1209 refedResources[i]->unref();
1210 }
1211
bsalomone2e87f32016-09-22 12:42:11 -07001212 // After kFlushCount + 1 flushes they all will have sat in the purgeable queue for
1213 // kFlushCount full flushes.
1214 for (int i = 0; i < kFlushCount + 1; ++i) {
bsalomon3f324322015-04-08 11:01:54 -07001215 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
bsalomonb77a9072016-09-07 10:02:04 -07001216 cache->notifyFlushOccurred(GrResourceCache::kExternal);
bsalomon3f324322015-04-08 11:01:54 -07001217 }
1218 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1219
1220 cache->purgeAllUnlocked();
1221 }
1222
1223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomondc438982016-08-31 11:53:49 -07001224
1225 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1226 // eviction.
1227 context->flush();
1228 for (int i = 0; i < 10; ++i) {
1229 TestResource* r = new TestResource(context->getGpu());
1230 GrUniqueKey k;
1231 make_unique_key<1>(&k, i);
1232 r->resourcePriv().setUniqueKey(k);
1233 r->unref();
1234 }
1235 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1236 for (int i = 0; i < 10 * kFlushCount; ++i) {
1237 context->flush();
1238 }
1239 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
bsalomon3f324322015-04-08 11:01:54 -07001240}
1241
Brian Salomon5e150852017-03-22 14:53:13 -04001242static void test_time_purge(skiatest::Reporter* reporter) {
1243 Mock mock(1000000, 1000000);
1244 GrContext* context = mock.context();
1245 GrResourceCache* cache = mock.cache();
1246
1247 static constexpr int kCnts[] = {1, 10, 1024};
1248 auto nowish = []() {
1249 // We sleep so that we ensure we get a value that is greater than the last call to
1250 // GrStdSteadyClock::now().
1251 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1252 auto result = GrStdSteadyClock::now();
1253 // Also sleep afterwards so we don't get this value again.
1254 std::this_thread::sleep_for(GrStdSteadyClock::duration(5));
1255 return result;
1256 };
1257
1258 for (int cnt : kCnts) {
1259 std::unique_ptr<GrStdSteadyClock::time_point[]> timeStamps(
1260 new GrStdSteadyClock::time_point[cnt]);
1261 {
1262 // Insert resources and get time points between each addition.
1263 for (int i = 0; i < cnt; ++i) {
1264 TestResource* r = new TestResource(context->getGpu());
1265 GrUniqueKey k;
1266 make_unique_key<1>(&k, i);
1267 r->resourcePriv().setUniqueKey(k);
1268 r->unref();
1269 timeStamps.get()[i] = nowish();
1270 }
1271
1272 // Purge based on the time points between resource additions. Each purge should remove
1273 // the oldest resource.
1274 for (int i = 0; i < cnt; ++i) {
1275 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1276 REPORTER_ASSERT(reporter, cnt - i - 1 == cache->getResourceCount());
1277 for (int j = 0; j < i; ++j) {
1278 GrUniqueKey k;
1279 make_unique_key<1>(&k, j);
1280 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1281 REPORTER_ASSERT(reporter, !SkToBool(r));
1282 SkSafeUnref(r);
1283 }
1284 }
1285
1286 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1287 cache->purgeAllUnlocked();
1288 }
1289
1290 // Do a similar test but where we leave refs on some resources to prevent them from being
1291 // purged.
1292 {
1293 std::unique_ptr<GrGpuResource* []> refedResources(new GrGpuResource*[cnt / 2]);
1294 for (int i = 0; i < cnt; ++i) {
1295 TestResource* r = new TestResource(context->getGpu());
1296 GrUniqueKey k;
1297 make_unique_key<1>(&k, i);
1298 r->resourcePriv().setUniqueKey(k);
1299 // Leave a ref on every other resource, beginning with the first.
1300 if (SkToBool(i & 0x1)) {
1301 refedResources.get()[i / 2] = r;
1302 } else {
1303 r->unref();
1304 }
1305 timeStamps.get()[i] = nowish();
1306 }
1307
1308 for (int i = 0; i < cnt; ++i) {
1309 // Should get a resource purged every other frame.
1310 cache->purgeResourcesNotUsedSince(timeStamps[i]);
1311 REPORTER_ASSERT(reporter, cnt - i / 2 - 1 == cache->getResourceCount());
1312 }
1313
1314 // Unref all the resources that we kept refs on in the first loop.
1315 for (int i = 0; i < (cnt / 2); ++i) {
1316 refedResources.get()[i]->unref();
1317 cache->purgeResourcesNotUsedSince(nowish());
1318 REPORTER_ASSERT(reporter, cnt / 2 - i - 1 == cache->getResourceCount());
1319 }
1320
1321 cache->purgeAllUnlocked();
1322 }
1323
1324 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1325
1326 // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
1327 // eviction
1328 context->flush();
1329 for (int i = 0; i < 10; ++i) {
1330 TestResource* r = new TestResource(context->getGpu());
1331 GrUniqueKey k;
1332 make_unique_key<1>(&k, i);
1333 r->resourcePriv().setUniqueKey(k);
1334 r->unref();
1335 }
1336 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1337 context->flush();
1338 REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
1339 cache->purgeResourcesNotUsedSince(nowish());
1340 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1341 }
1342}
1343
Derek Sollenberger5480a182017-05-25 16:43:59 -04001344static void test_partial_purge(skiatest::Reporter* reporter) {
1345 Mock mock(6, 100);
1346 GrContext* context = mock.context();
1347 GrResourceCache* cache = mock.cache();
1348
1349 enum TestsCase {
1350 kOnlyScratch_TestCase = 0,
1351 kPartialScratch_TestCase = 1,
1352 kAllScratch_TestCase = 2,
1353 kPartial_TestCase = 3,
1354 kAll_TestCase = 4,
1355 kNone_TestCase = 5,
1356 kEndTests_TestCase = kNone_TestCase + 1
1357 };
1358
1359 for (int testCase = 0; testCase < kEndTests_TestCase; testCase++) {
1360
1361 GrUniqueKey key1, key2, key3;
1362 make_unique_key<0>(&key1, 1);
1363 make_unique_key<0>(&key2, 2);
1364 make_unique_key<0>(&key3, 3);
1365
1366 // Add three unique resources to the cache.
1367 TestResource *unique1 = new TestResource(context->getGpu());
1368 TestResource *unique2 = new TestResource(context->getGpu());
1369 TestResource *unique3 = new TestResource(context->getGpu());
1370
1371 unique1->resourcePriv().setUniqueKey(key1);
1372 unique2->resourcePriv().setUniqueKey(key2);
1373 unique3->resourcePriv().setUniqueKey(key3);
1374
1375 unique1->setSize(10);
1376 unique2->setSize(11);
1377 unique3->setSize(12);
1378
1379 // Add two scratch resources to the cache.
1380 TestResource *scratch1 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1381 TestResource::kA_SimulatedProperty);
1382 TestResource *scratch2 = TestResource::CreateScratch(context->getGpu(), SkBudgeted::kYes,
1383 TestResource::kB_SimulatedProperty);
1384 scratch1->setSize(13);
1385 scratch2->setSize(14);
1386
1387
1388 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1389 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1390 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1391
1392 // Add resources to the purgeable queue
1393 unique1->unref();
1394 scratch1->unref();
1395 unique2->unref();
1396 scratch2->unref();
1397 unique3->unref();
1398
1399 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1400 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1401 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1402
1403 switch(testCase) {
1404 case kOnlyScratch_TestCase: {
1405 context->purgeUnlockedResources(14, true);
1406 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1407 REPORTER_ASSERT(reporter, 33 == cache->getBudgetedResourceBytes());
1408 break;
1409 }
1410 case kPartialScratch_TestCase: {
1411 context->purgeUnlockedResources(3, true);
1412 REPORTER_ASSERT(reporter, 4 == cache->getBudgetedResourceCount());
1413 REPORTER_ASSERT(reporter, 47 == cache->getBudgetedResourceBytes());
1414 break;
1415 }
1416 case kAllScratch_TestCase: {
1417 context->purgeUnlockedResources(50, true);
1418 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1419 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1420 break;
1421 }
1422 case kPartial_TestCase: {
1423 context->purgeUnlockedResources(13, false);
1424 REPORTER_ASSERT(reporter, 3 == cache->getBudgetedResourceCount());
1425 REPORTER_ASSERT(reporter, 37 == cache->getBudgetedResourceBytes());
1426 break;
1427 }
1428 case kAll_TestCase: {
1429 context->purgeUnlockedResources(50, false);
1430 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1431 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
1432 break;
1433 }
1434 case kNone_TestCase: {
1435 context->purgeUnlockedResources(0, true);
1436 context->purgeUnlockedResources(0, false);
1437 REPORTER_ASSERT(reporter, 5 == cache->getBudgetedResourceCount());
1438 REPORTER_ASSERT(reporter, 60 == cache->getBudgetedResourceBytes());
1439 REPORTER_ASSERT(reporter, 60 == cache->getPurgeableBytes());
1440 break;
1441 }
1442 };
1443
1444 // ensure all are purged before the next
1445 context->purgeAllUnlockedResources();
1446 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
1447 REPORTER_ASSERT(reporter, 0 == cache->getPurgeableBytes());
1448
1449 }
1450}
1451
bsalomon10e23ca2014-11-25 05:52:06 -08001452static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001453 // Set the cache size to double the resource count because we're going to create 2x that number
1454 // resources, using two different key domains. Add a little slop to the bytes because we resize
1455 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001456 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001457
bsalomonc2f35b72015-01-23 07:19:22 -08001458 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1459 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001460 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001461
1462 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001463 GrUniqueKey key1, key2;
1464 make_unique_key<1>(&key1, i);
1465 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001466
bsalomon24db3b12015-01-23 04:24:04 -08001467 TestResource* resource;
1468
halcanary385fe4d2015-08-26 13:07:48 -07001469 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001470 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001471 resource->setSize(1);
1472 resource->unref();
1473
halcanary385fe4d2015-08-26 13:07:48 -07001474 resource = new TestResource(context->getGpu());
bsalomon8718aaf2015-02-19 07:24:21 -08001475 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001476 resource->setSize(1);
1477 resource->unref();
1478 }
1479
1480 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001481 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001482 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1483 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1484 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1485 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001486 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001487 GrUniqueKey key1, key2;
1488 make_unique_key<1>(&key1, i);
1489 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001490
bsalomon8718aaf2015-02-19 07:24:21 -08001491 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1492 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001493 }
1494
bsalomon0ea80f42015-02-11 10:49:59 -08001495 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001496 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
Derek Sollenbergeree479142017-05-24 11:41:33 -04001497 REPORTER_ASSERT(reporter, cache->getPurgeableBytes() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001498 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1499 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1500 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1501 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001502
1503 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001504 GrUniqueKey key1, key2;
1505 make_unique_key<1>(&key1, i);
1506 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001507
bsalomon8718aaf2015-02-19 07:24:21 -08001508 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1509 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001510 }
1511}
1512
senorblanco84cd6212015-08-04 10:01:58 -07001513static void test_custom_data(skiatest::Reporter* reporter) {
1514 GrUniqueKey key1, key2;
1515 make_unique_key<0>(&key1, 1);
1516 make_unique_key<0>(&key2, 2);
1517 int foo = 4132;
bungeman38d909e2016-08-02 14:40:46 -07001518 key1.setCustomData(SkData::MakeWithCopy(&foo, sizeof(foo)));
senorblanco84cd6212015-08-04 10:01:58 -07001519 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1520 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1521
1522 // Test that copying a key also takes a ref on its custom data.
1523 GrUniqueKey key3 = key1;
1524 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1525}
1526
bsalomonc6363ef2015-09-24 07:07:40 -07001527static void test_abandoned(skiatest::Reporter* reporter) {
1528 Mock mock(10, 300);
1529 GrContext* context = mock.context();
Hal Canary342b7ac2016-11-04 11:49:42 -04001530 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
bsalomonc6363ef2015-09-24 07:07:40 -07001531 context->abandonContext();
1532
1533 REPORTER_ASSERT(reporter, resource->wasDestroyed());
1534
1535 // Call all the public methods on resource in the abandoned state. They shouldn't crash.
1536
robertphillips8abb3702016-08-31 14:04:06 -07001537 resource->uniqueID();
bsalomonc6363ef2015-09-24 07:07:40 -07001538 resource->getUniqueKey();
1539 resource->wasDestroyed();
1540 resource->gpuMemorySize();
1541 resource->getContext();
1542
1543 resource->abandon();
1544 resource->resourcePriv().getScratchKey();
1545 resource->resourcePriv().isBudgeted();
1546 resource->resourcePriv().makeBudgeted();
1547 resource->resourcePriv().makeUnbudgeted();
1548 resource->resourcePriv().removeScratchKey();
1549 GrUniqueKey key;
1550 make_unique_key<0>(&key, 1);
1551 resource->resourcePriv().setUniqueKey(key);
1552 resource->resourcePriv().removeUniqueKey();
1553}
1554
Brian Salomon1090da62017-01-06 12:04:19 -05001555static void test_tags(skiatest::Reporter* reporter) {
1556#ifdef SK_DEBUG
1557 // We will insert 1 resource with tag "tag1", 2 with "tag2", and so on, up through kLastTagIdx.
1558 static constexpr int kLastTagIdx = 10;
1559 static constexpr int kNumResources = kLastTagIdx * (kLastTagIdx + 1) / 2;
1560
1561 Mock mock(kNumResources, kNumResources * TestResource::kDefaultSize);
1562 GrContext* context = mock.context();
1563 GrResourceCache* cache = mock.cache();
1564
1565 SkString tagStr;
1566 int tagIdx = 0;
1567 int currTagCnt = 0;
1568
1569 for (int i = 0; i < kNumResources; ++i, ++currTagCnt) {
1570 sk_sp<GrGpuResource> resource(new TestResource(context->getGpu()));
1571 GrUniqueKey key;
1572 if (currTagCnt == tagIdx) {
1573 tagIdx += 1;
1574 currTagCnt = 0;
1575 tagStr.printf("tag%d", tagIdx);
1576 }
1577 make_unique_key<1>(&key, i, tagStr.c_str());
1578 resource->resourcePriv().setUniqueKey(key);
1579 }
1580 SkASSERT(kLastTagIdx == tagIdx);
1581 SkASSERT(currTagCnt == kLastTagIdx);
1582
1583 // Test i = 0 to exercise unused tag string.
1584 for (int i = 0; i <= kLastTagIdx; ++i) {
1585 tagStr.printf("tag%d", i);
1586 REPORTER_ASSERT(reporter, cache->countUniqueKeysWithTag(tagStr.c_str()) == i);
1587 }
1588#endif
1589}
1590
Brian Salomondcfca432017-11-15 15:48:03 -05001591DEF_GPUTEST(ResourceCacheMisc, reporter, /* options */) {
bsalomon8b79d232014-11-10 10:19:06 -08001592 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001593 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001594 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001595 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001596 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001597 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001598 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001599 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001600 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001601 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001602 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001603 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001604 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001605 test_flush(reporter);
Brian Salomon5e150852017-03-22 14:53:13 -04001606 test_time_purge(reporter);
Derek Sollenberger5480a182017-05-25 16:43:59 -04001607 test_partial_purge(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001608 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001609 test_custom_data(reporter);
bsalomonc6363ef2015-09-24 07:07:40 -07001610 test_abandoned(reporter);
Brian Salomon1090da62017-01-06 12:04:19 -05001611 test_tags(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001612}
1613
Robert Phillipsd6214d42016-11-07 08:23:48 -05001614////////////////////////////////////////////////////////////////////////////////
Brian Osman32342f02017-03-04 08:12:46 -05001615static sk_sp<GrTexture> make_normal_texture(GrResourceProvider* provider,
Robert Phillipsd6214d42016-11-07 08:23:48 -05001616 GrSurfaceFlags flags,
1617 int width, int height,
1618 int sampleCnt) {
1619 GrSurfaceDesc desc;
1620 desc.fFlags = flags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001621 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001622 desc.fWidth = width;
1623 desc.fHeight = height;
1624 desc.fConfig = kRGBA_8888_GrPixelConfig;
1625 desc.fSampleCnt = sampleCnt;
1626
Robert Phillipse78b7252017-04-06 07:59:41 -04001627 return provider->createTexture(desc, SkBudgeted::kYes);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001628}
1629
Robert Phillipse78b7252017-04-06 07:59:41 -04001630static sk_sp<GrTextureProxy> make_mipmap_proxy(GrResourceProvider* provider,
1631 GrSurfaceFlags flags,
1632 int width, int height,
1633 int sampleCnt) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001634 SkBitmap bm;
1635
1636 bm.allocN32Pixels(width, height, true);
1637 bm.eraseColor(SK_ColorBLUE);
1638
Brian Osman7b8400d2016-11-08 17:08:54 -05001639 sk_sp<SkMipMap> mipmaps(SkMipMap::Build(bm, SkDestinationSurfaceColorMode::kLegacy, nullptr));
Robert Phillipsd6214d42016-11-07 08:23:48 -05001640 SkASSERT(mipmaps);
1641 SkASSERT(mipmaps->countLevels() > 1);
1642
1643 int mipLevelCount = mipmaps->countLevels() + 1;
1644
1645 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
1646
1647 texels[0].fPixels = bm.getPixels();
1648 texels[0].fRowBytes = bm.rowBytes();
1649
1650 for (int i = 1; i < mipLevelCount; ++i) {
1651 SkMipMap::Level generatedMipLevel;
1652 mipmaps->getLevel(i - 1, &generatedMipLevel);
1653 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
1654 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
1655 }
1656
1657 GrSurfaceDesc desc;
1658 desc.fFlags = flags;
Robert Phillipse44ef102017-07-21 15:37:19 -04001659 desc.fOrigin = (flags & kRenderTarget_GrSurfaceFlag) ? kBottomLeft_GrSurfaceOrigin
1660 : kTopLeft_GrSurfaceOrigin;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001661 desc.fWidth = width;
1662 desc.fHeight = height;
1663 desc.fConfig = kRGBA_8888_GrPixelConfig;
1664 desc.fSampleCnt = sampleCnt;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001665
Robert Phillips8e8c7552017-07-10 12:06:05 -04001666 return GrSurfaceProxy::MakeDeferredMipMap(provider, desc, SkBudgeted::kYes,
1667 texels.get(), mipLevelCount);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001668}
1669
1670// Exercise GrSurface::gpuMemorySize for different combos of MSAA, RT-only,
1671// Texture-only, both-RT-and-Texture and MIPmapped
1672DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GPUMemorySize, reporter, ctxInfo) {
1673 GrContext* context = ctxInfo.grContext();
Brian Osman32342f02017-03-04 08:12:46 -05001674 GrResourceProvider* provider = context->resourceProvider();
Robert Phillipsd6214d42016-11-07 08:23:48 -05001675
Robert Phillipsd6214d42016-11-07 08:23:48 -05001676 static const int kSize = 64;
1677
Robert Phillipsd6214d42016-11-07 08:23:48 -05001678 // Normal versions
Robert Phillipse78b7252017-04-06 07:59:41 -04001679 {
1680 sk_sp<GrTexture> tex;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001681
Robert Phillipse78b7252017-04-06 07:59:41 -04001682 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1683 size_t size = tex->gpuMemorySize();
1684 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
1685
Greg Daniel81e7bf82017-07-19 14:47:42 -04001686 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1687 if (sampleCount >= 4) {
1688 tex = make_normal_texture(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1689 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001690 size = tex->gpuMemorySize();
Greg Daniel81e7bf82017-07-19 14:47:42 -04001691 REPORTER_ASSERT(reporter,
1692 kSize*kSize*4 == size || // msaa4 failed
1693 kSize*kSize*4*sampleCount == size || // auto-resolving
1694 kSize*kSize*4*(sampleCount+1) == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001695 }
1696
1697 tex = make_normal_texture(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001698 size = tex->gpuMemorySize();
Robert Phillipse78b7252017-04-06 07:59:41 -04001699 REPORTER_ASSERT(reporter, kSize*kSize*4 == size);
Robert Phillipsd6214d42016-11-07 08:23:48 -05001700 }
1701
Robert Phillipsd6214d42016-11-07 08:23:48 -05001702
1703 // Mipmapped versions
Brian Osman48c99192017-06-02 08:45:06 -04001704 if (context->caps()->mipMapSupport()) {
Robert Phillipse78b7252017-04-06 07:59:41 -04001705 sk_sp<GrTextureProxy> proxy;
Robert Phillipsd6214d42016-11-07 08:23:48 -05001706
Robert Phillipse78b7252017-04-06 07:59:41 -04001707 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize, 0);
1708 size_t size = proxy->gpuMemorySize();
1709 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1710
Greg Daniel81e7bf82017-07-19 14:47:42 -04001711 size_t sampleCount = (size_t)context->caps()->getSampleCount(4, kRGBA_8888_GrPixelConfig);
1712 if (sampleCount >= 4) {
1713 proxy = make_mipmap_proxy(provider, kRenderTarget_GrSurfaceFlag, kSize, kSize,
1714 sampleCount);
Robert Phillipse78b7252017-04-06 07:59:41 -04001715 size = proxy->gpuMemorySize();
1716 REPORTER_ASSERT(reporter,
Greg Daniel81e7bf82017-07-19 14:47:42 -04001717 kSize*kSize*4+(kSize*kSize*4)/3 == size || // msaa4 failed
1718 kSize*kSize*4*sampleCount+(kSize*kSize*4)/3 == size || // auto-resolving
1719 kSize*kSize*4*(sampleCount+1)+(kSize*kSize*4)/3 == size); // explicit resolve buffer
Robert Phillipse78b7252017-04-06 07:59:41 -04001720 }
Robert Phillips1b352562017-04-05 18:56:21 +00001721
Robert Phillipse78b7252017-04-06 07:59:41 -04001722 proxy = make_mipmap_proxy(provider, kNone_GrSurfaceFlags, kSize, kSize, 0);
1723 size = proxy->gpuMemorySize();
1724 REPORTER_ASSERT(reporter, kSize*kSize*4+(kSize*kSize*4)/3 == size);
1725 }
Robert Phillipsd6214d42016-11-07 08:23:48 -05001726}
1727
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001728#endif