blob: f0e7bfa28428837dfceabcf113d9c89f459155ef [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"
bsalomon6dc6f5f2015-06-18 09:12:16 -070015#include "gl/GrGLInterface.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"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTarget.h"
20#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070022#include "GrTest.h"
bsalomonbcf0a522014-10-08 08:40:09 -070023#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080024#include "SkGr.h"
25#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070026#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000027#include "Test.h"
bsalomon6dc6f5f2015-06-18 09:12:16 -070028#include "../src/gpu/gl/GrGLDefines.h"
29#include "../src/gpu/gl/GrGLUtil.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000030
31static const int gWidth = 640;
32static const int gHeight = 480;
33
34////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080035static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000036 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
37
38 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000039 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000040 src.eraseColor(SK_ColorBLACK);
41 size_t srcSize = src.getSize();
42
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000043 size_t initialCacheSize;
44 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000045
46 int oldMaxNum;
47 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000048 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000049
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050 // Set the cache limits so we can fit 10 "src" images and the
51 // max number of textures doesn't matter
52 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000053 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
55 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000056 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000057
58 for (int i = 0; i < 100; ++i) {
59 canvas->drawBitmap(src, 0, 0);
60 canvas->readPixels(size, &readback);
61
62 // "modify" the src texture
63 src.notifyPixelsChanged();
64
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000065 size_t curCacheSize;
66 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000067
68 // we should never go over the size limit
69 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
70 }
71
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000072 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000073}
74
bsalomon02a44a42015-02-19 09:09:00 -080075static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) {
76 GrSurfaceDesc smallDesc;
77 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
78 smallDesc.fConfig = kSkia8888_GrPixelConfig;
79 smallDesc.fWidth = 4;
80 smallDesc.fHeight = 4;
81 smallDesc.fSampleCnt = 0;
82
bsalomond309e7a2015-04-30 14:18:54 -070083 GrTextureProvider* cache = context->textureProvider();
bsalomon02a44a42015-02-19 09:09:00 -080084 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomond309e7a2015-04-30 14:18:54 -070085 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080086 if (smallRT0 && smallRT0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070087 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080088 }
89
bsalomond309e7a2015-04-30 14:18:54 -070090 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080091 if (smallRT1 && smallRT1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070092 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080093 }
94
egdaniel8dc7c3a2015-04-16 11:22:42 -070095 REPORTER_ASSERT(reporter,
96 smallRT0 && smallRT1 &&
97 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
98 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
99 smallRT1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800100
101 // An unbudgeted RT with the same desc should also share.
bsalomond309e7a2015-04-30 14:18:54 -0700102 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800103 if (smallRT2 && smallRT2->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700104 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800105 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700106 REPORTER_ASSERT(reporter,
107 smallRT0 && smallRT2 &&
108 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
109 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
110 smallRT2->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800111
112 // An RT with a much larger size should not share.
113 GrSurfaceDesc bigDesc;
114 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
115 bigDesc.fConfig = kSkia8888_GrPixelConfig;
116 bigDesc.fWidth = 400;
117 bigDesc.fHeight = 200;
118 bigDesc.fSampleCnt = 0;
bsalomond309e7a2015-04-30 14:18:54 -0700119 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800120 if (bigRT && bigRT->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700121 bigRT->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800122 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700123 REPORTER_ASSERT(reporter,
124 smallRT0 && bigRT &&
125 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
126 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
127 bigRT->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800128
bsalomon76228632015-05-29 08:02:10 -0700129 if (context->caps()->maxSampleCount() >= 4) {
bsalomon02a44a42015-02-19 09:09:00 -0800130 // An RT with a different sample count should not share.
131 GrSurfaceDesc smallMSAADesc = smallDesc;
132 smallMSAADesc.fSampleCnt = 4;
bsalomond309e7a2015-04-30 14:18:54 -0700133 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800134 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700135 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800136 }
bsalomonb602d4d2015-02-19 12:05:58 -0800137#ifdef SK_BUILD_FOR_ANDROID
138 if (!smallMSAART0) {
139 // The nexus player seems to fail to create MSAA textures.
140 return;
141 }
142#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800143 REPORTER_ASSERT(reporter,
144 smallRT0 && smallMSAART0 &&
145 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700146 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
147 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800148 // A second MSAA RT should share with the first MSAA RT.
bsalomond309e7a2015-04-30 14:18:54 -0700149 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800150 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700151 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800152 }
153 REPORTER_ASSERT(reporter,
154 smallMSAART0 && smallMSAART1 &&
155 smallMSAART0->asRenderTarget() &&
156 smallMSAART1->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700157 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
158 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800159 // But not one with a larger sample count should not. (Also check that the request for 4
160 // samples didn't get rounded up to >= 8 or else they could share.).
bsalomon76228632015-05-29 08:02:10 -0700161 if (context->caps()->maxSampleCount() >= 8 &&
162 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700163 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800164 smallMSAADesc.fSampleCnt = 8;
bsalomond309e7a2015-04-30 14:18:54 -0700165 smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
166 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800167 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700168 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800169 }
170 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700171 smallMSAART0 && smallMSAART1 &&
172 smallMSAART0->asRenderTarget() &&
173 smallMSAART1->asRenderTarget() &&
174 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
175 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800176 }
177 }
178}
179
bsalomon6dc6f5f2015-06-18 09:12:16 -0700180static void test_wrapped_resources(skiatest::Reporter* reporter, GrContext* context) {
181 GrTestTarget tt;
182 context->getTestTarget(&tt);
183
184 const GrGLInterface* gl = tt.glInterface();
185 if (!gl) {
186 return;
187 }
188
189 GrGLuint texIDs[2];
190 static const int kW = 100;
191 static const int kH = 100;
192 GR_GL_CALL(gl, GenTextures(2, texIDs));
193 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0));
194 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
195 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texIDs[0]));
196 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, kW, kH, 0, GR_GL_RGBA,
197 GR_GL_UNSIGNED_BYTE, NULL));
198 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texIDs[1]));
199 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, kW, kH, 0, GR_GL_RGBA,
200 GR_GL_UNSIGNED_BYTE, NULL));
201 context->resetContext();
202
203 GrBackendTextureDesc desc;
204 desc.fConfig = kBGRA_8888_GrPixelConfig;
205 desc.fWidth = kW;
206 desc.fHeight = kH;
207
208 desc.fTextureHandle = texIDs[0];
209 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
210 desc, kBorrow_GrWrapOwnership));
211
212 desc.fTextureHandle = texIDs[1];
213 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
214 desc, kAdopt_GrWrapOwnership));
215
216 REPORTER_ASSERT(reporter, SkToBool(borrowed) && SkToBool(adopted));
217 if (!SkToBool(borrowed) || !SkToBool(adopted)) {
218 return;
219 }
220
221 borrowed.reset(NULL);
222 adopted.reset(NULL);
223
224 context->flush();
225
226 GrGLboolean borrowedIsAlive;
227 GrGLboolean adoptedIsAlive;
228 GR_GL_CALL_RET(gl, borrowedIsAlive, IsTexture(texIDs[0]));
229 GR_GL_CALL_RET(gl, adoptedIsAlive, IsTexture(texIDs[1]));
230
231 REPORTER_ASSERT(reporter, borrowedIsAlive);
232 REPORTER_ASSERT(reporter, !adoptedIsAlive);
233
234 GR_GL_CALL(gl, GenTextures(1, &texIDs[0]));
235
236 context->resetContext();
237}
238
bsalomon6d3fe022014-07-25 08:35:45 -0700239class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000240 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800241 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000242public:
243 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800244 /** Property that distinctly categorizes the resource.
245 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800246 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800247
bsalomon5236cf42015-01-14 10:42:08 -0800248 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
249 : INHERITED(gpu, lifeCycle)
250 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800251 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800252 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800253 ++fNumAlive;
254 this->registerWithCache();
255 }
256
257 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
258 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800259 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800260 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800261 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800262 ++fNumAlive;
263 this->registerWithCache();
264 }
265
bsalomon8b79d232014-11-10 10:19:06 -0800266 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800267 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000268 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800269 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800270 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800271 ++fNumAlive;
272 this->registerWithCache();
273 }
274
bsalomon23e619c2015-02-06 11:54:28 -0800275 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800276 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000277 }
278
279 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800280 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800281 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000282 }
283
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000284 void setSize(size_t size) {
285 fSize = size;
286 this->didChangeGpuMemorySize();
287 }
288
bsalomon33435572014-11-05 14:47:41 -0800289 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000290
bsalomon71cb0c22014-11-14 12:10:14 -0800291 void setUnrefWhenDestroyed(TestResource* resource) {
292 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000293 }
294
bsalomon1c60dfe2015-01-21 09:32:40 -0800295 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
296 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
297 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800298 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
299 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800300 }
301 }
302
303 static size_t ExpectedScratchKeySize() {
304 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
305 }
306
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000307private:
bsalomon24db3b12015-01-23 04:24:04 -0800308 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800309
bsalomonc2f35b72015-01-23 07:19:22 -0800310 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
311 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800312 , fToDelete(NULL)
313 , fSize(kDefaultSize)
314 , fProperty(property) {
315 GrScratchKey scratchKey;
316 ComputeScratchKey(fProperty, &scratchKey);
317 this->setScratchKey(scratchKey);
318 ++fNumAlive;
319 this->registerWithCache();
320 }
321
mtklein36352bf2015-03-25 18:17:31 -0700322 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800323
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000324 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000325 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800326 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800327 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700328 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000329};
bsalomon33435572014-11-05 14:47:41 -0800330int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000331
bsalomonc2f35b72015-01-23 07:19:22 -0800332class Mock {
333public:
334 Mock(int maxCnt, size_t maxBytes) {
335 fContext.reset(GrContext::CreateMockContext());
336 SkASSERT(fContext);
337 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800338 GrResourceCache* cache = fContext->getResourceCache();
339 cache->purgeAllUnlocked();
340 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800341 }
bsalomonc2f35b72015-01-23 07:19:22 -0800342
bsalomon0ea80f42015-02-11 10:49:59 -0800343 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800344
345 GrContext* context() { return fContext; }
346
347private:
348 SkAutoTUnref<GrContext> fContext;
349};
350
351static void test_no_key(skiatest::Reporter* reporter) {
352 Mock mock(10, 30000);
353 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800354 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800355
356 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800357 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
358 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
359 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
360 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800361 a->setSize(11);
362 b->setSize(12);
363 c->setSize(13);
364 d->setSize(14);
365
366 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800367 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800368 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800369 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800370
371 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800372 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800373 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
374
bsalomon8718aaf2015-02-19 07:24:21 -0800375 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800376
377 a->unref();
378 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800379 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800380 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800381 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800382
383 c->unref();
384 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800386 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800387 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800388
389 d->unref();
390 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
392 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800393
394 b->unref();
395 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800396 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
397 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800398}
399
bsalomon24db3b12015-01-23 04:24:04 -0800400// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800401template <int> static void make_unique_key(GrUniqueKey* key, int data) {
402 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
403 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800404 builder[0] = data;
405}
406
bsalomon84c8e622014-11-17 09:33:27 -0800407static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800408 Mock mock(10, 300);
409 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800410 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800411
bsalomon8718aaf2015-02-19 07:24:21 -0800412 GrUniqueKey uniqueKey;
413 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800414
bsalomon8718aaf2015-02-19 07:24:21 -0800415 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800416 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800417 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800418 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800419 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
420 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800421 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800422 TestResource* wrapped = SkNEW_ARGS(TestResource,
bsalomon6dc6f5f2015-06-18 09:12:16 -0700423 (context->getGpu(), GrGpuResource::kBorrowed_LifeCycle));
bsalomon5236cf42015-01-14 10:42:08 -0800424 wrapped->setSize(12);
425 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
426 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800427 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 // Make sure we can't add a unique key to the wrapped resource
430 GrUniqueKey uniqueKey2;
431 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800432 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800433 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800434
435 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800436 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800437 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800438 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800439 cache->getResourceBytes());
440 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800441 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800442 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800443
bsalomon63c992f2015-01-23 12:47:59 -0800444 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800445 cache->purgeAllUnlocked();
446 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800447 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800448 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800449 cache->getResourceBytes());
450 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800451 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800452 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800453
454 // Unreffing the wrapped resource should free it right away.
455 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800456 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800458 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800459
bsalomon84c8e622014-11-17 09:33:27 -0800460 // Now try freeing the budgeted resources first
bsalomon6dc6f5f2015-06-18 09:12:16 -0700461 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kBorrowed_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800462 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800463 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800464 cache->purgeAllUnlocked();
465 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800466 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800467 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
468 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800470
471 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800472 cache->purgeAllUnlocked();
473 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800474 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800475 cache->getResourceBytes());
476 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
477 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800478
479 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
482 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
483 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800484
485 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800486 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
487 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
488 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
489 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800490}
491
bsalomon5236cf42015-01-14 10:42:08 -0800492static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800493 Mock mock(10, 30000);
494 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800495 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800496
bsalomon8718aaf2015-02-19 07:24:21 -0800497 GrUniqueKey uniqueKey;
498 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800499
500 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800501 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800502 TestResource* wrapped;
503 TestResource* unbudgeted;
504
505 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800506 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800507 scratch->setSize(10);
508 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800509 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
510 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
511 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
512 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800513
bsalomon8718aaf2015-02-19 07:24:21 -0800514 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
515 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800516 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800517 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800518 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
519 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
520 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
521 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800522
bsalomon0ea80f42015-02-11 10:49:59 -0800523 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800524 unbudgeted = SkNEW_ARGS(TestResource,
525 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800526 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
527 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
528 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
529 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800530
531 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800536
537 wrapped = SkNEW_ARGS(TestResource,
bsalomon6dc6f5f2015-06-18 09:12:16 -0700538 (context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800539 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
540 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
541 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
542 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800543
544 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800545 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
546 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
547 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
548 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800549
bsalomon0ea80f42015-02-11 10:49:59 -0800550 cache->purgeAllUnlocked();
551 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
552 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
553 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
554 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800555}
556
bsalomon3582d3e2015-02-13 14:20:05 -0800557// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
558void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
559/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800560 Mock mock(10, 300);
561 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800562 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800563
564 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800565 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800566 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800567 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800568
569 size_t size = resource->gpuMemorySize();
570 for (int i = 0; i < 2; ++i) {
571 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800572 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800573 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800574 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800575 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
576 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
577 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
578 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
579 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800580
581 // Once it is unrefed, it should become available as scratch.
582 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800583 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
584 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
585 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
586 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
587 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800588 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800589 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800590 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800591 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800592
593 if (0 == i) {
594 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
595 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800596 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800597 } else {
598 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800599 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800600 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
601 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
602 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
603 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800604 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800605 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800606 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800607
608 // now when it is unrefed it should die since it has no key.
609 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800610 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
611 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
612 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
613 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800614 }
bsalomon8b79d232014-11-10 10:19:06 -0800615 }
bsalomonc2f35b72015-01-23 07:19:22 -0800616}
617
618static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
619 Mock mock(5, 30000);
620 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800621 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800622
bsalomon8b79d232014-11-10 10:19:06 -0800623 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800624 TestResource* a = TestResource::CreateScratch(context->getGpu(),
625 TestResource::kB_SimulatedProperty);
626 TestResource* b = TestResource::CreateScratch(context->getGpu(),
627 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800628 a->setSize(11);
629 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800630 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800631 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800632 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800633 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800634
635 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800636 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800637
bsalomon0ea80f42015-02-11 10:49:59 -0800638 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800639 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800640 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
641 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800642 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800643 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800644
bsalomon63c992f2015-01-23 12:47:59 -0800645 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800646 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800647 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800648 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800649
650 // Unref but don't purge
651 a->unref();
652 b->unref();
653 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800654 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800655
bsalomon63c992f2015-01-23 12:47:59 -0800656 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800657 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800658 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800659 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
660 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800661}
662
bsalomon10e23ca2014-11-25 05:52:06 -0800663static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800664 Mock mock(5, 30000);
665 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800666 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800667
bsalomon10e23ca2014-11-25 05:52:06 -0800668 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800669 TestResource* a = TestResource::CreateScratch(context->getGpu(),
670 TestResource::kB_SimulatedProperty);
671 TestResource* b = TestResource::CreateScratch(context->getGpu(),
672 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800673 a->unref();
674 b->unref();
675
bsalomon1c60dfe2015-01-21 09:32:40 -0800676 GrScratchKey scratchKey;
677 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800678 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800679 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800680 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800681
bsalomon0ea80f42015-02-11 10:49:59 -0800682 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800683 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800684 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800685 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
686 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800687
688 // Find the first resource and remove its scratch key
689 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800690 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800691 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800692 // It's still alive, but not cached by scratch key anymore
693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800694 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
695 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800696
697 // The cache should immediately delete it when it's unrefed since it isn't accessible.
698 find->unref();
699 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800700 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
701 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800702
703 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800704 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800705 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800706 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800707 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
708 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800709
710 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800711 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800712 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800713 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
714 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800715
716 find->unref();
717 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800718 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
719 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800720}
721
bsalomon1c60dfe2015-01-21 09:32:40 -0800722static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800723 Mock mock(5, 30000);
724 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800725 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800726
727 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800728 TestResource* a = TestResource::CreateScratch(context->getGpu(),
729 TestResource::kB_SimulatedProperty);
730 TestResource* b = TestResource::CreateScratch(context->getGpu(),
731 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800732 a->unref();
733 b->unref();
734
735 GrScratchKey scratchKey;
736 // Ensure that scratch key comparison and assignment is consistent.
737 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800738 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800739 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800740 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800741 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
742 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
743 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
744 scratchKey = scratchKey1;
745 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
746 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
747 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
748 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
749 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
750 scratchKey = scratchKey2;
751 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
752 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
753 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
754 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
755 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
756
757 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800758 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800759 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800760 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800761
762 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800763 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800764 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800765 REPORTER_ASSERT(reporter, find != NULL);
766 find->unref();
767
768 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800769 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800770 REPORTER_ASSERT(reporter, find != NULL);
771 REPORTER_ASSERT(reporter, find == a || find == b);
772
bsalomon0ea80f42015-02-11 10:49:59 -0800773 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800774 REPORTER_ASSERT(reporter, find2 != NULL);
775 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
776 REPORTER_ASSERT(reporter, find2 != find);
777 find2->unref();
778 find->unref();
779}
780
bsalomon8718aaf2015-02-19 07:24:21 -0800781static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800782 Mock mock(5, 30000);
783 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800784 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000785
bsalomon8718aaf2015-02-19 07:24:21 -0800786 GrUniqueKey key;
787 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800788
bsalomon8718aaf2015-02-19 07:24:21 -0800789 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800790 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800791 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800792
bsalomonf99e9612015-02-19 08:24:16 -0800793 // Set key on resource a.
794 a->resourcePriv().setUniqueKey(key);
795 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
796 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800797
bsalomonf99e9612015-02-19 08:24:16 -0800798 // Make sure that redundantly setting a's key works.
799 a->resourcePriv().setUniqueKey(key);
800 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800801 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800802 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
803 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800804 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
805
bsalomonf99e9612015-02-19 08:24:16 -0800806 // Create resource b and set the same key. It should replace a's unique key cache entry.
807 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
808 b->setSize(12);
809 b->resourcePriv().setUniqueKey(key);
810 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
811 b->unref();
812
813 // Still have two resources because a is still reffed.
814 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
815 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
816 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
817
818 a->unref();
819 // Now a should be gone.
820 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
821 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
822 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
823
824 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
825 // Also make b be unreffed when replacement occurs.
826 b->unref();
827 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
828 GrUniqueKey differentKey;
829 make_unique_key<0>(&differentKey, 1);
830 c->setSize(13);
831 c->resourcePriv().setUniqueKey(differentKey);
832 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
833 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
834 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
835 // c replaces b and b should be immediately purged.
836 c->resourcePriv().setUniqueKey(key);
837 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
838 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
839 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
840
841 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800842 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800843 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
844 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
845 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
846
847 // Drop the ref on c, it should be kept alive because it has a unique key.
848 c->unref();
849 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
850 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
851 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
852
853 // Verify that we can find c, then remove its unique key. It should get purged immediately.
854 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
855 c->resourcePriv().removeUniqueKey();
856 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800857 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
858 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800859 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000860}
861
bsalomon8b79d232014-11-10 10:19:06 -0800862static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800863 Mock mock(5, 30000);
864 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800865 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800866
bsalomon8718aaf2015-02-19 07:24:21 -0800867 GrUniqueKey key1, key2, key3;
868 make_unique_key<0>(&key1, 1);
869 make_unique_key<0>(&key2, 2);
870 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800871
bsalomon23e619c2015-02-06 11:54:28 -0800872 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800873 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
874 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800875 TestResource* c = TestResource::CreateScratch(context->getGpu(),
876 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800877 a->resourcePriv().setUniqueKey(key1);
878 b->resourcePriv().setUniqueKey(key2);
879 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800880 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800881 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800882 c->unref();
883
bsalomon8718aaf2015-02-19 07:24:21 -0800884 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
885 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
886 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800887 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800888
bsalomon8718aaf2015-02-19 07:24:21 -0800889 typedef GrUniqueKeyInvalidatedMessage Msg;
890 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800891
892 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
893 Bus::Post(Msg(key1));
894 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800895 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800896 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800897 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
898 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800899 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800900 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800901
902 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800903 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800904 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800905 // we still have a ref on b, c should be recycled as scratch.
906 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800907 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800908
bsalomon23e619c2015-02-06 11:54:28 -0800909 // make b purgeable. It should be immediately deleted since it has no key.
910 b->unref();
911 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
912
913 // Make sure we actually get to c via it's scratch key, before we say goodbye.
914 GrScratchKey scratchKey;
915 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800916 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800917 REPORTER_ASSERT(reporter, scratch == c);
918 SkSafeUnref(scratch);
919
920 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800921 cache->purgeAllUnlocked();
922 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800923 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800924 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
925 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800926 REPORTER_ASSERT(reporter, !scratch);
927 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800928}
929
bsalomon71cb0c22014-11-14 12:10:14 -0800930static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800931 Mock mock(3, 30000);
932 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800933 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800934
bsalomon8718aaf2015-02-19 07:24:21 -0800935 GrUniqueKey key1, key2;
936 make_unique_key<0>(&key1, 1);
937 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000938
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000939
bsalomonc2f35b72015-01-23 07:19:22 -0800940 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
941 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800942 a->resourcePriv().setUniqueKey(key1);
943 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800944
bsalomonc2f35b72015-01-23 07:19:22 -0800945 // Make a cycle
946 a->setUnrefWhenDestroyed(b);
947 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800948
bsalomonc2f35b72015-01-23 07:19:22 -0800949 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800950
bsalomonc2f35b72015-01-23 07:19:22 -0800951 a->unref();
952 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800953
bsalomonc2f35b72015-01-23 07:19:22 -0800954 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800955
bsalomon0ea80f42015-02-11 10:49:59 -0800956 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800957 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800958
bsalomonc2f35b72015-01-23 07:19:22 -0800959 // Break the cycle
960 a->setUnrefWhenDestroyed(NULL);
961 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800962
bsalomon0ea80f42015-02-11 10:49:59 -0800963 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800964 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000965}
966
bsalomon8b79d232014-11-10 10:19:06 -0800967static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800968 GrUniqueKey key1, key2;
969 make_unique_key<0>(&key1, 1);
970 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000971
972 // Test changing resources sizes (both increase & decrease).
973 {
bsalomonc2f35b72015-01-23 07:19:22 -0800974 Mock mock(3, 30000);
975 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800976 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000977
bsalomon5236cf42015-01-14 10:42:08 -0800978 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800979 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000980 a->unref();
981
bsalomon5236cf42015-01-14 10:42:08 -0800982 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800983 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000984 b->unref();
985
bsalomon0ea80f42015-02-11 10:49:59 -0800986 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
987 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800988 {
bsalomon8718aaf2015-02-19 07:24:21 -0800989 SkAutoTUnref<TestResource> find2(
990 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800991 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800992 SkAutoTUnref<TestResource> find1(
993 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800994 find1->setSize(50);
995 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000996
bsalomon0ea80f42015-02-11 10:49:59 -0800997 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
998 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000999 }
1000
1001 // Test increasing a resources size beyond the cache budget.
1002 {
bsalomonc2f35b72015-01-23 07:19:22 -08001003 Mock mock(2, 300);
1004 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001005 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001006
bsalomon5236cf42015-01-14 10:42:08 -08001007 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -08001008 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001009 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001010 a->unref();
1011
bsalomon5236cf42015-01-14 10:42:08 -08001012 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -08001013 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -08001014 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001015 b->unref();
1016
bsalomon0ea80f42015-02-11 10:49:59 -08001017 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
1018 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001019
bsalomon8b79d232014-11-10 10:19:06 -08001020 {
bsalomon8718aaf2015-02-19 07:24:21 -08001021 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
1022 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -08001023 find2->setSize(201);
1024 }
bsalomon8718aaf2015-02-19 07:24:21 -08001025 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001026
bsalomon0ea80f42015-02-11 10:49:59 -08001027 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
1028 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001029 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +00001030}
1031
bsalomonddf30e62015-02-19 11:38:44 -08001032static void test_timestamp_wrap(skiatest::Reporter* reporter) {
1033 static const int kCount = 50;
1034 static const int kBudgetCnt = kCount / 2;
1035 static const int kLockedFreq = 8;
1036 static const int kBudgetSize = 0x80000000;
1037
1038 SkRandom random;
1039
1040 // Run the test 2*kCount times;
1041 for (int i = 0; i < 2 * kCount; ++i ) {
1042 Mock mock(kBudgetCnt, kBudgetSize);
1043 GrContext* context = mock.context();
1044 GrResourceCache* cache = mock.cache();
1045
1046 // Pick a random number of resources to add before the timestamp will wrap.
1047 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
1048
1049 static const int kNumToPurge = kCount - kBudgetCnt;
1050
1051 SkTDArray<int> shouldPurgeIdxs;
1052 int purgeableCnt = 0;
1053 SkTDArray<GrGpuResource*> resourcesToUnref;
1054
1055 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
1056 // unpurgeable resources.
1057 for (int j = 0; j < kCount; ++j) {
1058 GrUniqueKey key;
1059 make_unique_key<0>(&key, j);
1060
1061 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1062 r->resourcePriv().setUniqueKey(key);
1063 if (random.nextU() % kLockedFreq) {
1064 // Make this is purgeable.
1065 r->unref();
1066 ++purgeableCnt;
1067 if (purgeableCnt <= kNumToPurge) {
1068 *shouldPurgeIdxs.append() = j;
1069 }
1070 } else {
1071 *resourcesToUnref.append() = r;
1072 }
1073 }
1074
1075 // Verify that the correct resources were purged.
1076 int currShouldPurgeIdx = 0;
1077 for (int j = 0; j < kCount; ++j) {
1078 GrUniqueKey key;
1079 make_unique_key<0>(&key, j);
1080 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1081 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1082 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1083 ++currShouldPurgeIdx;
1084 REPORTER_ASSERT(reporter, NULL == res);
1085 } else {
1086 REPORTER_ASSERT(reporter, NULL != res);
1087 }
1088 SkSafeUnref(res);
1089 }
1090
1091 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1092 resourcesToUnref[j]->unref();
1093 }
1094 }
1095}
1096
bsalomon3f324322015-04-08 11:01:54 -07001097static void test_flush(skiatest::Reporter* reporter) {
1098 Mock mock(1000000, 1000000);
1099 GrContext* context = mock.context();
1100 GrResourceCache* cache = mock.cache();
1101
1102 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1103 // power of two here to keep things simpler.
1104 static const int kFlushCount = 16;
1105 cache->setLimits(1000000, 1000000, kFlushCount);
1106
1107 {
1108 // Insert a resource and send a flush notification kFlushCount times.
1109 for (int i = 0; i < kFlushCount; ++i) {
1110 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1111 GrUniqueKey k;
1112 make_unique_key<1>(&k, i);
1113 r->resourcePriv().setUniqueKey(k);
1114 r->unref();
1115 cache->notifyFlushOccurred();
1116 }
1117
1118 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1119 for (int i = 0; i < kFlushCount - 1; ++i) {
1120 // The first resource was purged after the last flush in the initial loop, hence the -1.
1121 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1122 for (int j = 0; j < i; ++j) {
1123 GrUniqueKey k;
1124 make_unique_key<1>(&k, j);
1125 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1126 REPORTER_ASSERT(reporter, !SkToBool(r));
1127 SkSafeUnref(r);
1128 }
1129 cache->notifyFlushOccurred();
1130 }
1131
1132 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1133 cache->purgeAllUnlocked();
1134 }
1135
1136 // Do a similar test but where we leave refs on some resources to prevent them from being
1137 // purged.
1138 {
1139 GrGpuResource* refedResources[kFlushCount >> 1];
1140 for (int i = 0; i < kFlushCount; ++i) {
1141 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1142 GrUniqueKey k;
1143 make_unique_key<1>(&k, i);
1144 r->resourcePriv().setUniqueKey(k);
1145 // Leave a ref on every other resource, beginning with the first.
1146 if (SkToBool(i & 0x1)) {
1147 refedResources[i/2] = r;
1148 } else {
1149 r->unref();
1150 }
1151 cache->notifyFlushOccurred();
1152 }
1153
1154 for (int i = 0; i < kFlushCount; ++i) {
1155 // Should get a resource purged every other flush.
1156 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1157 cache->notifyFlushOccurred();
1158 }
1159
1160 // Unref all the resources that we kept refs on in the first loop.
1161 for (int i = 0; i < kFlushCount >> 1; ++i) {
1162 refedResources[i]->unref();
1163 }
1164
1165 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1166 // get kFlushCount additional flushes. Then everything should be purged.
1167 for (int i = 0; i < kFlushCount; ++i) {
1168 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1169 cache->notifyFlushOccurred();
1170 }
1171 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1172
1173 cache->purgeAllUnlocked();
1174 }
1175
1176 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1177}
1178
bsalomon10e23ca2014-11-25 05:52:06 -08001179static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001180 // Set the cache size to double the resource count because we're going to create 2x that number
1181 // resources, using two different key domains. Add a little slop to the bytes because we resize
1182 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001183 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001184
bsalomonc2f35b72015-01-23 07:19:22 -08001185 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1186 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001187 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001188
1189 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001190 GrUniqueKey key1, key2;
1191 make_unique_key<1>(&key1, i);
1192 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001193
bsalomon24db3b12015-01-23 04:24:04 -08001194 TestResource* resource;
1195
bsalomon10e23ca2014-11-25 05:52:06 -08001196 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001197 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001198 resource->setSize(1);
1199 resource->unref();
1200
bsalomon10e23ca2014-11-25 05:52:06 -08001201 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001202 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001203 resource->setSize(1);
1204 resource->unref();
1205 }
1206
1207 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001208 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1209 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1210 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1211 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001212 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001213 GrUniqueKey key1, key2;
1214 make_unique_key<1>(&key1, i);
1215 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001216
bsalomon8718aaf2015-02-19 07:24:21 -08001217 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1218 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001219 }
1220
bsalomon0ea80f42015-02-11 10:49:59 -08001221 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001222 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001223 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1224 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1225 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1226 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001227
1228 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001229 GrUniqueKey key1, key2;
1230 make_unique_key<1>(&key1, i);
1231 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001232
bsalomon8718aaf2015-02-19 07:24:21 -08001233 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1234 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001235 }
1236}
1237
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001238////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001239DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001240 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1241 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1242 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1243 continue;
1244 }
1245 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001246 if (NULL == context) {
1247 continue;
1248 }
bsalomonf2703d82014-10-28 14:33:06 -07001249 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001250 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001251 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001252 desc.fWidth = gWidth;
1253 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001254 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001255 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1256 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001257 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001258 test_stencil_buffers(reporter, context);
bsalomon6dc6f5f2015-06-18 09:12:16 -07001259 test_wrapped_resources(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001260 }
bsalomon33435572014-11-05 14:47:41 -08001261
bsalomon8b79d232014-11-10 10:19:06 -08001262 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001263 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001264 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001265 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001266 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001267 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001268 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001269 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001270 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001271 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001272 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001273 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001274 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001275 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001276 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001277}
1278
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001279#endif