blob: 43f087004fc42b0dc31b77767dde4abaf247dfb5 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon3f324322015-04-08 11:01:54 -07008// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9#include "SkTypes.h"
10
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourceCacheAccess.h"
17#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTarget.h"
19#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070021#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070022#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080023#include "SkGr.h"
24#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070025#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000026#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000027
28static const int gWidth = 640;
29static const int gHeight = 480;
30
31////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080032static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000033 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
34
35 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000036 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000037 src.eraseColor(SK_ColorBLACK);
38 size_t srcSize = src.getSize();
39
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000040 size_t initialCacheSize;
41 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000042
43 int oldMaxNum;
44 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000045 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000046
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047 // Set the cache limits so we can fit 10 "src" images and the
48 // max number of textures doesn't matter
49 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000050 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000051
52 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000053 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
55 for (int i = 0; i < 100; ++i) {
56 canvas->drawBitmap(src, 0, 0);
57 canvas->readPixels(size, &readback);
58
59 // "modify" the src texture
60 src.notifyPixelsChanged();
61
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000062 size_t curCacheSize;
63 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000064
65 // we should never go over the size limit
66 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
67 }
68
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000069 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000070}
71
bsalomon02a44a42015-02-19 09:09:00 -080072static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) {
73 GrSurfaceDesc smallDesc;
74 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
75 smallDesc.fConfig = kSkia8888_GrPixelConfig;
76 smallDesc.fWidth = 4;
77 smallDesc.fHeight = 4;
78 smallDesc.fSampleCnt = 0;
79
bsalomond309e7a2015-04-30 14:18:54 -070080 GrTextureProvider* cache = context->textureProvider();
bsalomon02a44a42015-02-19 09:09:00 -080081 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomond309e7a2015-04-30 14:18:54 -070082 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080083 if (smallRT0 && smallRT0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070084 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080085 }
86
bsalomond309e7a2015-04-30 14:18:54 -070087 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080088 if (smallRT1 && smallRT1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070089 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080090 }
91
egdaniel8dc7c3a2015-04-16 11:22:42 -070092 REPORTER_ASSERT(reporter,
93 smallRT0 && smallRT1 &&
94 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
95 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
96 smallRT1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -080097
98 // An unbudgeted RT with the same desc should also share.
bsalomond309e7a2015-04-30 14:18:54 -070099 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800100 if (smallRT2 && smallRT2->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700101 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800102 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700103 REPORTER_ASSERT(reporter,
104 smallRT0 && smallRT2 &&
105 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
106 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
107 smallRT2->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800108
109 // An RT with a much larger size should not share.
110 GrSurfaceDesc bigDesc;
111 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
112 bigDesc.fConfig = kSkia8888_GrPixelConfig;
113 bigDesc.fWidth = 400;
114 bigDesc.fHeight = 200;
115 bigDesc.fSampleCnt = 0;
bsalomond309e7a2015-04-30 14:18:54 -0700116 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800117 if (bigRT && bigRT->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700118 bigRT->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800119 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700120 REPORTER_ASSERT(reporter,
121 smallRT0 && bigRT &&
122 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
123 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
124 bigRT->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800125
bsalomon76228632015-05-29 08:02:10 -0700126 if (context->caps()->maxSampleCount() >= 4) {
bsalomon02a44a42015-02-19 09:09:00 -0800127 // An RT with a different sample count should not share.
128 GrSurfaceDesc smallMSAADesc = smallDesc;
129 smallMSAADesc.fSampleCnt = 4;
bsalomond309e7a2015-04-30 14:18:54 -0700130 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800131 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700132 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800133 }
bsalomonb602d4d2015-02-19 12:05:58 -0800134#ifdef SK_BUILD_FOR_ANDROID
135 if (!smallMSAART0) {
136 // The nexus player seems to fail to create MSAA textures.
137 return;
138 }
139#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 REPORTER_ASSERT(reporter,
141 smallRT0 && smallMSAART0 &&
142 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700143 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
144 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800145 // A second MSAA RT should share with the first MSAA RT.
bsalomond309e7a2015-04-30 14:18:54 -0700146 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700148 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800149 }
150 REPORTER_ASSERT(reporter,
151 smallMSAART0 && smallMSAART1 &&
152 smallMSAART0->asRenderTarget() &&
153 smallMSAART1->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700154 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
155 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800156 // But not one with a larger sample count should not. (Also check that the request for 4
157 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700158 if (context->caps()->maxSampleCount() >= 8 &&
159 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700160 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800161 smallMSAADesc.fSampleCnt = 8;
bsalomond309e7a2015-04-30 14:18:54 -0700162 smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
163 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800164 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700165 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800166 }
167 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700168 smallMSAART0 && smallMSAART1 &&
169 smallMSAART0->asRenderTarget() &&
170 smallMSAART1->asRenderTarget() &&
171 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
172 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800173 }
174 }
175}
176
bsalomon6dc6f5f2015-06-18 09:12:16 -0700177static void test_wrapped_resources(skiatest::Reporter* reporter, GrContext* context) {
jvanverth672bb7f2015-07-13 07:19:57 -0700178 const GrGpu* gpu = context->getGpu();
jvanvertheeb8d992015-07-15 10:16:56 -0700179 // this test is only valid for GL
180 if (!gpu || !gpu->glContextForTesting()) {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700181 return;
182 }
183
jvanverth672bb7f2015-07-13 07:19:57 -0700184 GrBackendObject texIDs[2];
bsalomon6dc6f5f2015-06-18 09:12:16 -0700185 static const int kW = 100;
186 static const int kH = 100;
jvanverth672bb7f2015-07-13 07:19:57 -0700187
jvanverth88957922015-07-14 11:02:52 -0700188 texIDs[0] = gpu->createTestingOnlyBackendTexture(NULL, kW, kH, kRGBA_8888_GrPixelConfig);
189 texIDs[1] = gpu->createTestingOnlyBackendTexture(NULL, kW, kH, kRGBA_8888_GrPixelConfig);
jvanverth672bb7f2015-07-13 07:19:57 -0700190
bsalomon6dc6f5f2015-06-18 09:12:16 -0700191 context->resetContext();
192
193 GrBackendTextureDesc desc;
194 desc.fConfig = kBGRA_8888_GrPixelConfig;
195 desc.fWidth = kW;
196 desc.fHeight = kH;
197
198 desc.fTextureHandle = texIDs[0];
199 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700200 desc, kBorrow_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700201
202 desc.fTextureHandle = texIDs[1];
203 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
jvanverth3e5f5552015-07-16 07:46:07 -0700204 desc, kAdopt_GrWrapOwnership));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700205
206 REPORTER_ASSERT(reporter, SkToBool(borrowed) && SkToBool(adopted));
207 if (!SkToBool(borrowed) || !SkToBool(adopted)) {
208 return;
209 }
210
211 borrowed.reset(NULL);
212 adopted.reset(NULL);
213
214 context->flush();
215
jvanverth88957922015-07-14 11:02:52 -0700216 bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texIDs[0]);
217 bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texIDs[1]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218
219 REPORTER_ASSERT(reporter, borrowedIsAlive);
220 REPORTER_ASSERT(reporter, !adoptedIsAlive);
221
jvanverth88957922015-07-14 11:02:52 -0700222 gpu->deleteTestingOnlyBackendTexture(texIDs[0]);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223
224 context->resetContext();
225}
226
bsalomon6d3fe022014-07-25 08:35:45 -0700227class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000228 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800229 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000230public:
mtklein2766c002015-06-26 11:45:03 -0700231
bsalomon1c60dfe2015-01-21 09:32:40 -0800232 /** Property that distinctly categorizes the resource.
233 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800234 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800235
bsalomon5236cf42015-01-14 10:42:08 -0800236 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
237 : INHERITED(gpu, lifeCycle)
238 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800239 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800240 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800241 ++fNumAlive;
242 this->registerWithCache();
243 }
244
245 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
246 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800247 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800248 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800249 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800250 ++fNumAlive;
251 this->registerWithCache();
252 }
253
bsalomon8b79d232014-11-10 10:19:06 -0800254 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800255 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000256 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800257 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800258 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800259 ++fNumAlive;
260 this->registerWithCache();
261 }
262
bsalomon23e619c2015-02-06 11:54:28 -0800263 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800264 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000265 }
266
267 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800268 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800269 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000270 }
271
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000272 void setSize(size_t size) {
273 fSize = size;
274 this->didChangeGpuMemorySize();
275 }
276
bsalomon33435572014-11-05 14:47:41 -0800277 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000278
bsalomon71cb0c22014-11-14 12:10:14 -0800279 void setUnrefWhenDestroyed(TestResource* resource) {
280 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281 }
282
bsalomon1c60dfe2015-01-21 09:32:40 -0800283 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
284 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
285 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800286 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
287 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800288 }
289 }
290
291 static size_t ExpectedScratchKeySize() {
292 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
293 }
294
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000295private:
bsalomon24db3b12015-01-23 04:24:04 -0800296 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800297
bsalomonc2f35b72015-01-23 07:19:22 -0800298 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
299 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800300 , fToDelete(NULL)
301 , fSize(kDefaultSize)
302 , fProperty(property) {
303 GrScratchKey scratchKey;
304 ComputeScratchKey(fProperty, &scratchKey);
305 this->setScratchKey(scratchKey);
306 ++fNumAlive;
307 this->registerWithCache();
308 }
309
mtklein36352bf2015-03-25 18:17:31 -0700310 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800311
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000312 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000313 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800314 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800315 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700316 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000317};
bsalomon33435572014-11-05 14:47:41 -0800318int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000319
bsalomonc2f35b72015-01-23 07:19:22 -0800320class Mock {
321public:
322 Mock(int maxCnt, size_t maxBytes) {
323 fContext.reset(GrContext::CreateMockContext());
324 SkASSERT(fContext);
325 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800326 GrResourceCache* cache = fContext->getResourceCache();
327 cache->purgeAllUnlocked();
328 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800329 }
bsalomonc2f35b72015-01-23 07:19:22 -0800330
bsalomon0ea80f42015-02-11 10:49:59 -0800331 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800332
333 GrContext* context() { return fContext; }
334
335private:
336 SkAutoTUnref<GrContext> fContext;
337};
338
339static void test_no_key(skiatest::Reporter* reporter) {
340 Mock mock(10, 30000);
341 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800342 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800343
344 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800345 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
346 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
347 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
348 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800349 a->setSize(11);
350 b->setSize(12);
351 c->setSize(13);
352 d->setSize(14);
353
354 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800355 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800356 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800357 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800358
359 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800360 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800361 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
362
bsalomon8718aaf2015-02-19 07:24:21 -0800363 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800364
365 a->unref();
366 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800367 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800368 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800369 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800370
371 c->unref();
372 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800373 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800374 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800375 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 d->unref();
378 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800379 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
380 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800381
382 b->unref();
383 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800384 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
385 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800386}
387
bsalomon24db3b12015-01-23 04:24:04 -0800388// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800389template <int> static void make_unique_key(GrUniqueKey* key, int data) {
390 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
391 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800392 builder[0] = data;
393}
394
bsalomon84c8e622014-11-17 09:33:27 -0800395static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800396 Mock mock(10, 300);
397 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800398 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800399
bsalomon8718aaf2015-02-19 07:24:21 -0800400 GrUniqueKey uniqueKey;
401 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800402
bsalomon8718aaf2015-02-19 07:24:21 -0800403 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800404 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800405 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800406 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800407 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
408 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800409 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800410 TestResource* wrapped = SkNEW_ARGS(TestResource,
bsalomon6dc6f5f2015-06-18 09:12:16 -0700411 (context->getGpu(), GrGpuResource::kBorrowed_LifeCycle));
bsalomon5236cf42015-01-14 10:42:08 -0800412 wrapped->setSize(12);
413 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
414 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800415 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800416
bsalomon8718aaf2015-02-19 07:24:21 -0800417 // Make sure we can't add a unique key to the wrapped resource
418 GrUniqueKey uniqueKey2;
419 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800420 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800421 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800422
423 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800424 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800425 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800426 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800427 cache->getResourceBytes());
428 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800429 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800430 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800431
bsalomon63c992f2015-01-23 12:47:59 -0800432 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800433 cache->purgeAllUnlocked();
434 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800435 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800436 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800437 cache->getResourceBytes());
438 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800439 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800440 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800441
442 // Unreffing the wrapped resource should free it right away.
443 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800444 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800445 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800446 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800447
bsalomon84c8e622014-11-17 09:33:27 -0800448 // Now try freeing the budgeted resources first
bsalomon6dc6f5f2015-06-18 09:12:16 -0700449 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kBorrowed_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800450 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800451 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800452 cache->purgeAllUnlocked();
453 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800454 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800455 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
456 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800458
459 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800460 cache->purgeAllUnlocked();
461 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800462 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800463 cache->getResourceBytes());
464 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
465 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800466
467 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800468 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
469 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
470 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
471 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800472
473 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800474 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
475 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
476 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
477 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800478}
479
bsalomon5236cf42015-01-14 10:42:08 -0800480static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800481 Mock mock(10, 30000);
482 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800483 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800484
bsalomon8718aaf2015-02-19 07:24:21 -0800485 GrUniqueKey uniqueKey;
486 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800487
488 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800489 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800490 TestResource* wrapped;
491 TestResource* unbudgeted;
492
493 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800494 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800495 scratch->setSize(10);
496 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800497 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
498 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
499 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
500 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800501
bsalomon8718aaf2015-02-19 07:24:21 -0800502 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
503 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800504 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800505 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800506 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
507 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
508 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
509 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800510
bsalomon0ea80f42015-02-11 10:49:59 -0800511 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800512 unbudgeted = SkNEW_ARGS(TestResource,
513 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800514 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
515 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
516 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
517 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800518
519 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800520 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
521 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
522 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
523 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800524
525 wrapped = SkNEW_ARGS(TestResource,
bsalomon6dc6f5f2015-06-18 09:12:16 -0700526 (context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800527 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
528 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
529 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
530 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800531
532 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800533 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
534 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
535 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
536 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800537
bsalomon0ea80f42015-02-11 10:49:59 -0800538 cache->purgeAllUnlocked();
539 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
540 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
541 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
542 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800543}
544
bsalomon3582d3e2015-02-13 14:20:05 -0800545// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
546void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
547/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800548 Mock mock(10, 300);
549 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800550 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800551
552 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800553 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800554 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800555 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800556
557 size_t size = resource->gpuMemorySize();
558 for (int i = 0; i < 2; ++i) {
559 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800560 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800561 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800562 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800563 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
564 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
565 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
566 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
567 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800568
569 // Once it is unrefed, it should become available as scratch.
570 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800571 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
572 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
573 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
574 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
575 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800576 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800577 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800578 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800579 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800580
581 if (0 == i) {
582 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
583 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800584 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800585 } else {
586 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800587 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
589 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
590 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
591 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800592 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800593 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800594 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800595
596 // now when it is unrefed it should die since it has no key.
597 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
599 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
600 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
601 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800602 }
bsalomon8b79d232014-11-10 10:19:06 -0800603 }
bsalomonc2f35b72015-01-23 07:19:22 -0800604}
605
606static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
607 Mock mock(5, 30000);
608 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800609 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800610
bsalomon8b79d232014-11-10 10:19:06 -0800611 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800612 TestResource* a = TestResource::CreateScratch(context->getGpu(),
613 TestResource::kB_SimulatedProperty);
614 TestResource* b = TestResource::CreateScratch(context->getGpu(),
615 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800616 a->setSize(11);
617 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800618 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800619 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800620 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800621 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800622
623 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800624 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800625
bsalomon0ea80f42015-02-11 10:49:59 -0800626 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800627 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800628 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
629 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800630 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800631 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800632
bsalomon63c992f2015-01-23 12:47:59 -0800633 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800634 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800635 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800636 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800637
638 // Unref but don't purge
639 a->unref();
640 b->unref();
641 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800642 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800643
bsalomon63c992f2015-01-23 12:47:59 -0800644 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800645 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800646 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800647 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
648 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800649}
650
bsalomon10e23ca2014-11-25 05:52:06 -0800651static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800652 Mock mock(5, 30000);
653 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800654 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800655
bsalomon10e23ca2014-11-25 05:52:06 -0800656 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800657 TestResource* a = TestResource::CreateScratch(context->getGpu(),
658 TestResource::kB_SimulatedProperty);
659 TestResource* b = TestResource::CreateScratch(context->getGpu(),
660 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800661 a->unref();
662 b->unref();
663
bsalomon1c60dfe2015-01-21 09:32:40 -0800664 GrScratchKey scratchKey;
665 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800666 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800667 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800668 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800669
bsalomon0ea80f42015-02-11 10:49:59 -0800670 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800671 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800672 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800673 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
674 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800675
676 // Find the first resource and remove its scratch key
677 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800678 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800679 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800680 // It's still alive, but not cached by scratch key anymore
681 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800682 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
683 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800684
685 // The cache should immediately delete it when it's unrefed since it isn't accessible.
686 find->unref();
687 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800688 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
689 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800690
691 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800692 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800693 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800694 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800695 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
696 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800697
698 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800699 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800700 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800701 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
702 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800703
704 find->unref();
705 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800706 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
707 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800708}
709
bsalomon1c60dfe2015-01-21 09:32:40 -0800710static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800711 Mock mock(5, 30000);
712 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800713 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800714
715 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800716 TestResource* a = TestResource::CreateScratch(context->getGpu(),
717 TestResource::kB_SimulatedProperty);
718 TestResource* b = TestResource::CreateScratch(context->getGpu(),
719 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800720 a->unref();
721 b->unref();
722
723 GrScratchKey scratchKey;
724 // Ensure that scratch key comparison and assignment is consistent.
725 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800726 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800727 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800728 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800729 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
730 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
731 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
732 scratchKey = scratchKey1;
733 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
734 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
735 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
736 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
737 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
738 scratchKey = scratchKey2;
739 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
740 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
741 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
742 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
743 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
744
745 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800746 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800747 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800748 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800749
750 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800751 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800752 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800753 REPORTER_ASSERT(reporter, find != NULL);
754 find->unref();
755
756 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800757 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800758 REPORTER_ASSERT(reporter, find != NULL);
759 REPORTER_ASSERT(reporter, find == a || find == b);
760
bsalomon0ea80f42015-02-11 10:49:59 -0800761 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800762 REPORTER_ASSERT(reporter, find2 != NULL);
763 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
764 REPORTER_ASSERT(reporter, find2 != find);
765 find2->unref();
766 find->unref();
767}
768
bsalomon8718aaf2015-02-19 07:24:21 -0800769static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800770 Mock mock(5, 30000);
771 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800772 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000773
bsalomon8718aaf2015-02-19 07:24:21 -0800774 GrUniqueKey key;
775 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800776
bsalomon8718aaf2015-02-19 07:24:21 -0800777 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800778 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800779 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800780
bsalomonf99e9612015-02-19 08:24:16 -0800781 // Set key on resource a.
782 a->resourcePriv().setUniqueKey(key);
783 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
784 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800785
bsalomonf99e9612015-02-19 08:24:16 -0800786 // Make sure that redundantly setting a's key works.
787 a->resourcePriv().setUniqueKey(key);
788 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800789 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800790 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
791 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800792 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
793
bsalomonf99e9612015-02-19 08:24:16 -0800794 // Create resource b and set the same key. It should replace a's unique key cache entry.
795 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
796 b->setSize(12);
797 b->resourcePriv().setUniqueKey(key);
798 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
799 b->unref();
800
801 // Still have two resources because a is still reffed.
802 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
803 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
804 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
805
806 a->unref();
807 // Now a should be gone.
808 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
809 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
810 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
811
812 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
813 // Also make b be unreffed when replacement occurs.
814 b->unref();
815 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
816 GrUniqueKey differentKey;
817 make_unique_key<0>(&differentKey, 1);
818 c->setSize(13);
819 c->resourcePriv().setUniqueKey(differentKey);
820 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
821 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
822 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
823 // c replaces b and b should be immediately purged.
824 c->resourcePriv().setUniqueKey(key);
825 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
826 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
827 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
828
829 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800830 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800831 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
832 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
833 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
834
835 // Drop the ref on c, it should be kept alive because it has a unique key.
836 c->unref();
837 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
838 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
839 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
840
841 // Verify that we can find c, then remove its unique key. It should get purged immediately.
842 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
843 c->resourcePriv().removeUniqueKey();
844 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800845 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
846 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800847 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
senorblanco84cd6212015-08-04 10:01:58 -0700848
849 {
850 GrUniqueKey key2;
851 make_unique_key<0>(&key2, 0);
852 SkAutoTUnref<TestResource> d(SkNEW_ARGS(TestResource, (context->getGpu())));
853 int foo = 4132;
854 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
855 key2.setCustomData(data.get());
856 d->resourcePriv().setUniqueKey(key2);
857 }
858
859 GrUniqueKey key3;
860 make_unique_key<0>(&key3, 0);
861 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
862 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000863}
864
bsalomon8b79d232014-11-10 10:19:06 -0800865static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800866 Mock mock(5, 30000);
867 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800868 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800869
bsalomon8718aaf2015-02-19 07:24:21 -0800870 GrUniqueKey key1, key2, key3;
871 make_unique_key<0>(&key1, 1);
872 make_unique_key<0>(&key2, 2);
873 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800874
bsalomon23e619c2015-02-06 11:54:28 -0800875 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800876 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
877 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800878 TestResource* c = TestResource::CreateScratch(context->getGpu(),
879 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800880 a->resourcePriv().setUniqueKey(key1);
881 b->resourcePriv().setUniqueKey(key2);
882 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800883 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800884 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800885 c->unref();
886
bsalomon8718aaf2015-02-19 07:24:21 -0800887 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
888 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
889 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800890 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800891
bsalomon8718aaf2015-02-19 07:24:21 -0800892 typedef GrUniqueKeyInvalidatedMessage Msg;
893 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800894
895 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
896 Bus::Post(Msg(key1));
897 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800898 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800899 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800900 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
901 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800902 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800903 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800904
905 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800906 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800907 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800908 // we still have a ref on b, c should be recycled as scratch.
909 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800910 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800911
bsalomon23e619c2015-02-06 11:54:28 -0800912 // make b purgeable. It should be immediately deleted since it has no key.
913 b->unref();
914 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
915
916 // Make sure we actually get to c via it's scratch key, before we say goodbye.
917 GrScratchKey scratchKey;
918 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800919 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800920 REPORTER_ASSERT(reporter, scratch == c);
921 SkSafeUnref(scratch);
922
923 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800924 cache->purgeAllUnlocked();
925 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800926 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800927 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
928 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800929 REPORTER_ASSERT(reporter, !scratch);
930 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800931}
932
bsalomon71cb0c22014-11-14 12:10:14 -0800933static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800934 Mock mock(3, 30000);
935 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800936 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800937
bsalomon8718aaf2015-02-19 07:24:21 -0800938 GrUniqueKey key1, key2;
939 make_unique_key<0>(&key1, 1);
940 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000941
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000942
bsalomonc2f35b72015-01-23 07:19:22 -0800943 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
944 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800945 a->resourcePriv().setUniqueKey(key1);
946 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800947
bsalomonc2f35b72015-01-23 07:19:22 -0800948 // Make a cycle
949 a->setUnrefWhenDestroyed(b);
950 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800951
bsalomonc2f35b72015-01-23 07:19:22 -0800952 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800953
bsalomonc2f35b72015-01-23 07:19:22 -0800954 a->unref();
955 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800956
bsalomonc2f35b72015-01-23 07:19:22 -0800957 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800958
bsalomon0ea80f42015-02-11 10:49:59 -0800959 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800960 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800961
bsalomonc2f35b72015-01-23 07:19:22 -0800962 // Break the cycle
963 a->setUnrefWhenDestroyed(NULL);
964 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800965
bsalomon0ea80f42015-02-11 10:49:59 -0800966 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800967 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000968}
969
bsalomon8b79d232014-11-10 10:19:06 -0800970static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800971 GrUniqueKey key1, key2;
972 make_unique_key<0>(&key1, 1);
973 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000974
975 // Test changing resources sizes (both increase & decrease).
976 {
bsalomonc2f35b72015-01-23 07:19:22 -0800977 Mock mock(3, 30000);
978 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800979 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000980
bsalomon5236cf42015-01-14 10:42:08 -0800981 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800982 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000983 a->unref();
984
bsalomon5236cf42015-01-14 10:42:08 -0800985 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800986 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000987 b->unref();
988
bsalomon0ea80f42015-02-11 10:49:59 -0800989 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
990 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800991 {
bsalomon8718aaf2015-02-19 07:24:21 -0800992 SkAutoTUnref<TestResource> find2(
993 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800994 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800995 SkAutoTUnref<TestResource> find1(
996 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800997 find1->setSize(50);
998 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000999
bsalomon0ea80f42015-02-11 10:49:59 -08001000 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
1001 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001002 }
1003
1004 // Test increasing a resources size beyond the cache budget.
1005 {
bsalomonc2f35b72015-01-23 07:19:22 -08001006 Mock mock(2, 300);
1007 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001008 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001009
bsalomon5236cf42015-01-14 10:42:08 -08001010 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -08001011 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001012 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001013 a->unref();
1014
bsalomon5236cf42015-01-14 10:42:08 -08001015 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -08001016 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001017 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001018 b->unref();
1019
bsalomon0ea80f42015-02-11 10:49:59 -08001020 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1021 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001022
bsalomon8b79d232014-11-10 10:19:06 -08001023 {
bsalomon8718aaf2015-02-19 07:24:21 -08001024 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1025 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001026 find2->setSize(201);
1027 }
bsalomon8718aaf2015-02-19 07:24:21 -08001028 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001029
bsalomon0ea80f42015-02-11 10:49:59 -08001030 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1031 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001032 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001033}
1034
bsalomonddf30e62015-02-19 11:38:44 -08001035static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1036 static const int kCount = 50;
1037 static const int kBudgetCnt = kCount / 2;
1038 static const int kLockedFreq = 8;
1039 static const int kBudgetSize = 0x80000000;
1040
1041 SkRandom random;
1042
1043 // Run the test 2*kCount times;
1044 for (int i = 0; i < 2 * kCount; ++i ) {
1045 Mock mock(kBudgetCnt, kBudgetSize);
1046 GrContext* context = mock.context();
1047 GrResourceCache* cache = mock.cache();
1048
1049 // Pick a random number of resources to add before the timestamp will wrap.
1050 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1051
1052 static const int kNumToPurge = kCount - kBudgetCnt;
1053
1054 SkTDArray<int> shouldPurgeIdxs;
1055 int purgeableCnt = 0;
1056 SkTDArray<GrGpuResource*> resourcesToUnref;
1057
1058 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1059 // unpurgeable resources.
1060 for (int j = 0; j < kCount; ++j) {
1061 GrUniqueKey key;
1062 make_unique_key<0>(&key, j);
1063
1064 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1065 r->resourcePriv().setUniqueKey(key);
1066 if (random.nextU() % kLockedFreq) {
1067 // Make this is purgeable.
1068 r->unref();
1069 ++purgeableCnt;
1070 if (purgeableCnt <= kNumToPurge) {
1071 *shouldPurgeIdxs.append() = j;
1072 }
1073 } else {
1074 *resourcesToUnref.append() = r;
1075 }
1076 }
1077
1078 // Verify that the correct resources were purged.
1079 int currShouldPurgeIdx = 0;
1080 for (int j = 0; j < kCount; ++j) {
1081 GrUniqueKey key;
1082 make_unique_key<0>(&key, j);
1083 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1084 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1085 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1086 ++currShouldPurgeIdx;
1087 REPORTER_ASSERT(reporter, NULL == res);
1088 } else {
1089 REPORTER_ASSERT(reporter, NULL != res);
1090 }
1091 SkSafeUnref(res);
1092 }
1093
1094 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1095 resourcesToUnref[j]->unref();
1096 }
1097 }
1098}
1099
bsalomon3f324322015-04-08 11:01:54 -07001100static void test_flush(skiatest::Reporter* reporter) {
1101 Mock mock(1000000, 1000000);
1102 GrContext* context = mock.context();
1103 GrResourceCache* cache = mock.cache();
1104
1105 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1106 // power of two here to keep things simpler.
1107 static const int kFlushCount = 16;
1108 cache->setLimits(1000000, 1000000, kFlushCount);
1109
1110 {
1111 // Insert a resource and send a flush notification kFlushCount times.
1112 for (int i = 0; i < kFlushCount; ++i) {
1113 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1114 GrUniqueKey k;
1115 make_unique_key<1>(&k, i);
1116 r->resourcePriv().setUniqueKey(k);
1117 r->unref();
1118 cache->notifyFlushOccurred();
1119 }
1120
1121 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1122 for (int i = 0; i < kFlushCount - 1; ++i) {
1123 // The first resource was purged after the last flush in the initial loop, hence the -1.
1124 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1125 for (int j = 0; j < i; ++j) {
1126 GrUniqueKey k;
1127 make_unique_key<1>(&k, j);
1128 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1129 REPORTER_ASSERT(reporter, !SkToBool(r));
1130 SkSafeUnref(r);
1131 }
1132 cache->notifyFlushOccurred();
1133 }
1134
1135 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1136 cache->purgeAllUnlocked();
1137 }
1138
1139 // Do a similar test but where we leave refs on some resources to prevent them from being
1140 // purged.
1141 {
1142 GrGpuResource* refedResources[kFlushCount >> 1];
1143 for (int i = 0; i < kFlushCount; ++i) {
1144 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1145 GrUniqueKey k;
1146 make_unique_key<1>(&k, i);
1147 r->resourcePriv().setUniqueKey(k);
1148 // Leave a ref on every other resource, beginning with the first.
1149 if (SkToBool(i & 0x1)) {
1150 refedResources[i/2] = r;
1151 } else {
1152 r->unref();
1153 }
1154 cache->notifyFlushOccurred();
1155 }
1156
1157 for (int i = 0; i < kFlushCount; ++i) {
1158 // Should get a resource purged every other flush.
1159 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1160 cache->notifyFlushOccurred();
1161 }
1162
1163 // Unref all the resources that we kept refs on in the first loop.
1164 for (int i = 0; i < kFlushCount >> 1; ++i) {
1165 refedResources[i]->unref();
1166 }
1167
1168 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1169 // get kFlushCount additional flushes. Then everything should be purged.
1170 for (int i = 0; i < kFlushCount; ++i) {
1171 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1172 cache->notifyFlushOccurred();
1173 }
1174 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1175
1176 cache->purgeAllUnlocked();
1177 }
1178
1179 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1180}
1181
bsalomon10e23ca2014-11-25 05:52:06 -08001182static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001183 // Set the cache size to double the resource count because we're going to create 2x that number
1184 // resources, using two different key domains. Add a little slop to the bytes because we resize
1185 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001186 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001187
bsalomonc2f35b72015-01-23 07:19:22 -08001188 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1189 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001190 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001191
1192 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001193 GrUniqueKey key1, key2;
1194 make_unique_key<1>(&key1, i);
1195 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001196
bsalomon24db3b12015-01-23 04:24:04 -08001197 TestResource* resource;
1198
bsalomon10e23ca2014-11-25 05:52:06 -08001199 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001200 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001201 resource->setSize(1);
1202 resource->unref();
1203
bsalomon10e23ca2014-11-25 05:52:06 -08001204 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001205 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001206 resource->setSize(1);
1207 resource->unref();
1208 }
1209
1210 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001211 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1212 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1213 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1214 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001215 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001216 GrUniqueKey key1, key2;
1217 make_unique_key<1>(&key1, i);
1218 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001219
bsalomon8718aaf2015-02-19 07:24:21 -08001220 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1221 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001222 }
1223
bsalomon0ea80f42015-02-11 10:49:59 -08001224 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001225 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001226 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1227 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1228 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1229 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001230
1231 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001232 GrUniqueKey key1, key2;
1233 make_unique_key<1>(&key1, i);
1234 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001235
bsalomon8718aaf2015-02-19 07:24:21 -08001236 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1237 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001238 }
1239}
1240
senorblanco84cd6212015-08-04 10:01:58 -07001241static void test_custom_data(skiatest::Reporter* reporter) {
1242 GrUniqueKey key1, key2;
1243 make_unique_key<0>(&key1, 1);
1244 make_unique_key<0>(&key2, 2);
1245 int foo = 4132;
1246 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
1247 key1.setCustomData(data.get());
1248 REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
1249 REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
1250
1251 // Test that copying a key also takes a ref on its custom data.
1252 GrUniqueKey key3 = key1;
1253 REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
1254}
1255
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001256////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001257DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001258 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1259 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1260 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1261 continue;
1262 }
1263 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001264 if (NULL == context) {
1265 continue;
1266 }
bsalomonf2703d82014-10-28 14:33:06 -07001267 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001268 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001269 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001270 desc.fWidth = gWidth;
1271 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001272 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001273 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1274 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001275 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001276 test_stencil_buffers(reporter, context);
bsalomon6dc6f5f2015-06-18 09:12:16 -07001277 test_wrapped_resources(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001278 }
bsalomon33435572014-11-05 14:47:41 -08001279
bsalomon8b79d232014-11-10 10:19:06 -08001280 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001281 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001282 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001283 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001284 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001285 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001286 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001287 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001288 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001289 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001290 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001291 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001292 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001293 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001294 test_large_resource_count(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07001295 test_custom_data(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001296}
1297
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001298#endif