blob: cd589054ad2b724ecd9ef8cd2d0942512a619feb [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"
bsalomonbcf0a522014-10-08 08:40:09 -070021#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080022#include "SkGr.h"
23#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070024#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000025#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000026
27static const int gWidth = 640;
28static const int gHeight = 480;
29
30////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080031static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
33
34 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000035 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000036 src.eraseColor(SK_ColorBLACK);
37 size_t srcSize = src.getSize();
38
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000039 size_t initialCacheSize;
40 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041
42 int oldMaxNum;
43 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000044 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000045
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000046 // Set the cache limits so we can fit 10 "src" images and the
47 // max number of textures doesn't matter
48 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000049 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050
51 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000052 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053
54 for (int i = 0; i < 100; ++i) {
55 canvas->drawBitmap(src, 0, 0);
56 canvas->readPixels(size, &readback);
57
58 // "modify" the src texture
59 src.notifyPixelsChanged();
60
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000061 size_t curCacheSize;
62 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000063
64 // we should never go over the size limit
65 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
66 }
67
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000068 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000069}
70
bsalomon02a44a42015-02-19 09:09:00 -080071static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) {
72 GrSurfaceDesc smallDesc;
73 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
74 smallDesc.fConfig = kSkia8888_GrPixelConfig;
75 smallDesc.fWidth = 4;
76 smallDesc.fHeight = 4;
77 smallDesc.fSampleCnt = 0;
78
79 // Test that two budgeted RTs with the same desc share a stencil buffer.
80 SkAutoTUnref<GrTexture> smallRT0(context->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080081 if (smallRT0 && smallRT0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070082 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080083 }
84
bsalomon02a44a42015-02-19 09:09:00 -080085 SkAutoTUnref<GrTexture> smallRT1(context->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080086 if (smallRT1 && smallRT1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070087 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080088 }
89
egdaniel8dc7c3a2015-04-16 11:22:42 -070090 REPORTER_ASSERT(reporter,
91 smallRT0 && smallRT1 &&
92 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
93 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
94 smallRT1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -080095
96 // An unbudgeted RT with the same desc should also share.
97 SkAutoTUnref<GrTexture> smallRT2(context->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -080098 if (smallRT2 && smallRT2->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070099 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800100 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700101 REPORTER_ASSERT(reporter,
102 smallRT0 && smallRT2 &&
103 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
104 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
105 smallRT2->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800106
107 // An RT with a much larger size should not share.
108 GrSurfaceDesc bigDesc;
109 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
110 bigDesc.fConfig = kSkia8888_GrPixelConfig;
111 bigDesc.fWidth = 400;
112 bigDesc.fHeight = 200;
113 bigDesc.fSampleCnt = 0;
114 SkAutoTUnref<GrTexture> bigRT(context->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800115 if (bigRT && bigRT->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700116 bigRT->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800117 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700118 REPORTER_ASSERT(reporter,
119 smallRT0 && bigRT &&
120 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
121 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
122 bigRT->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800123
124 if (context->getMaxSampleCount() >= 4) {
125 // An RT with a different sample count should not share.
126 GrSurfaceDesc smallMSAADesc = smallDesc;
127 smallMSAADesc.fSampleCnt = 4;
128 SkAutoTUnref<GrTexture> smallMSAART0(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800129 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700130 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800131 }
bsalomonb602d4d2015-02-19 12:05:58 -0800132#ifdef SK_BUILD_FOR_ANDROID
133 if (!smallMSAART0) {
134 // The nexus player seems to fail to create MSAA textures.
135 return;
136 }
137#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800138 REPORTER_ASSERT(reporter,
139 smallRT0 && smallMSAART0 &&
140 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700141 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
142 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800143 // A second MSAA RT should share with the first MSAA RT.
144 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800145 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700146 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800147 }
148 REPORTER_ASSERT(reporter,
149 smallMSAART0 && smallMSAART1 &&
150 smallMSAART0->asRenderTarget() &&
151 smallMSAART1->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700152 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
153 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800154 // But not one with a larger sample count should not. (Also check that the request for 4
155 // samples didn't get rounded up to >= 8 or else they could share.).
156 if (context->getMaxSampleCount() >= 8 && smallMSAART0 && smallMSAART0->asRenderTarget() &&
157 smallMSAART0->asRenderTarget()->numSamples() < 8) {
158 smallMSAADesc.fSampleCnt = 8;
159 smallMSAART1.reset(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800160 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
161 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700162 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800163 }
164 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700165 smallMSAART0 && smallMSAART1 &&
166 smallMSAART0->asRenderTarget() &&
167 smallMSAART1->asRenderTarget() &&
168 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
169 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800170 }
171 }
172}
173
bsalomon6d3fe022014-07-25 08:35:45 -0700174class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000175 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800176 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000177public:
178 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800179 /** Property that distinctly categorizes the resource.
180 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800181 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800182
bsalomon5236cf42015-01-14 10:42:08 -0800183 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
184 : INHERITED(gpu, lifeCycle)
185 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800186 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800187 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800188 ++fNumAlive;
189 this->registerWithCache();
190 }
191
192 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
193 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800194 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800195 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800196 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800197 ++fNumAlive;
198 this->registerWithCache();
199 }
200
bsalomon8b79d232014-11-10 10:19:06 -0800201 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800202 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000203 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800204 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800205 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800206 ++fNumAlive;
207 this->registerWithCache();
208 }
209
bsalomon23e619c2015-02-06 11:54:28 -0800210 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800211 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000212 }
213
214 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800215 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800216 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000217 }
218
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000219 void setSize(size_t size) {
220 fSize = size;
221 this->didChangeGpuMemorySize();
222 }
223
bsalomon33435572014-11-05 14:47:41 -0800224 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000225
bsalomon71cb0c22014-11-14 12:10:14 -0800226 void setUnrefWhenDestroyed(TestResource* resource) {
227 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000228 }
229
bsalomon1c60dfe2015-01-21 09:32:40 -0800230 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
231 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
232 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800233 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
234 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800235 }
236 }
237
238 static size_t ExpectedScratchKeySize() {
239 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
240 }
241
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000242private:
bsalomon24db3b12015-01-23 04:24:04 -0800243 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800244
bsalomonc2f35b72015-01-23 07:19:22 -0800245 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
246 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800247 , fToDelete(NULL)
248 , fSize(kDefaultSize)
249 , fProperty(property) {
250 GrScratchKey scratchKey;
251 ComputeScratchKey(fProperty, &scratchKey);
252 this->setScratchKey(scratchKey);
253 ++fNumAlive;
254 this->registerWithCache();
255 }
256
mtklein36352bf2015-03-25 18:17:31 -0700257 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800258
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000259 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000260 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800261 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800262 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700263 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000264};
bsalomon33435572014-11-05 14:47:41 -0800265int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000266
bsalomonc2f35b72015-01-23 07:19:22 -0800267class Mock {
268public:
269 Mock(int maxCnt, size_t maxBytes) {
270 fContext.reset(GrContext::CreateMockContext());
271 SkASSERT(fContext);
272 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800273 GrResourceCache* cache = fContext->getResourceCache();
274 cache->purgeAllUnlocked();
275 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800276 }
bsalomonc2f35b72015-01-23 07:19:22 -0800277
bsalomon0ea80f42015-02-11 10:49:59 -0800278 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800279
280 GrContext* context() { return fContext; }
281
282private:
283 SkAutoTUnref<GrContext> fContext;
284};
285
286static void test_no_key(skiatest::Reporter* reporter) {
287 Mock mock(10, 30000);
288 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800289 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800290
291 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800292 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
293 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
294 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
295 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800296 a->setSize(11);
297 b->setSize(12);
298 c->setSize(13);
299 d->setSize(14);
300
301 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800302 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800303 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800304 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800305
306 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800307 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800308 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
309
bsalomon8718aaf2015-02-19 07:24:21 -0800310 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800311
312 a->unref();
313 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800314 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800315 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800316 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800317
318 c->unref();
319 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800320 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800321 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800322 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800323
324 d->unref();
325 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800326 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
327 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800328
329 b->unref();
330 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800331 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
332 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800333}
334
bsalomon24db3b12015-01-23 04:24:04 -0800335// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800336template <int> static void make_unique_key(GrUniqueKey* key, int data) {
337 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
338 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800339 builder[0] = data;
340}
341
bsalomon84c8e622014-11-17 09:33:27 -0800342static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800343 Mock mock(10, 300);
344 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800345 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800346
bsalomon8718aaf2015-02-19 07:24:21 -0800347 GrUniqueKey uniqueKey;
348 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800349
bsalomon8718aaf2015-02-19 07:24:21 -0800350 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800351 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800352 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800353 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800354 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
355 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800356 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800357 TestResource* wrapped = SkNEW_ARGS(TestResource,
358 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
359 wrapped->setSize(12);
360 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
361 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800362 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800363
bsalomon8718aaf2015-02-19 07:24:21 -0800364 // Make sure we can't add a unique key to the wrapped resource
365 GrUniqueKey uniqueKey2;
366 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800367 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800368 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800369
370 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800371 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800372 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800373 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800374 cache->getResourceBytes());
375 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800376 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800377 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800378
bsalomon63c992f2015-01-23 12:47:59 -0800379 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800380 cache->purgeAllUnlocked();
381 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800382 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800383 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800384 cache->getResourceBytes());
385 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800386 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800387 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800388
389 // Unreffing the wrapped resource should free it right away.
390 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800391 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800392 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800393 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800394
bsalomon84c8e622014-11-17 09:33:27 -0800395 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800396 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800397 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800398 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800399 cache->purgeAllUnlocked();
400 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800401 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800402 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
403 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
404 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800405
406 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800407 cache->purgeAllUnlocked();
408 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800409 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800410 cache->getResourceBytes());
411 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
412 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800413
414 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800415 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
416 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
417 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
418 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800419
420 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800421 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
422 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
423 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
424 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800425}
426
bsalomon5236cf42015-01-14 10:42:08 -0800427static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800428 Mock mock(10, 30000);
429 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800430 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800431
bsalomon8718aaf2015-02-19 07:24:21 -0800432 GrUniqueKey uniqueKey;
433 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800434
435 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800436 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800437 TestResource* wrapped;
438 TestResource* unbudgeted;
439
440 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800441 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800442 scratch->setSize(10);
443 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800444 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
445 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
446 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
447 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800448
bsalomon8718aaf2015-02-19 07:24:21 -0800449 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
450 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800451 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800452 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800453 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
454 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
455 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
456 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800457
bsalomon0ea80f42015-02-11 10:49:59 -0800458 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800459 unbudgeted = SkNEW_ARGS(TestResource,
460 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800461 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
462 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
463 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
464 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800465
466 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800467 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
468 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
469 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
470 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800471
472 wrapped = SkNEW_ARGS(TestResource,
473 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800474 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
475 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
476 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
477 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800478
479 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800480 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
482 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
483 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800484
bsalomon0ea80f42015-02-11 10:49:59 -0800485 cache->purgeAllUnlocked();
486 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());
bsalomon5236cf42015-01-14 10:42:08 -0800490}
491
bsalomon3582d3e2015-02-13 14:20:05 -0800492// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
493void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
494/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800495 Mock mock(10, 300);
496 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800497 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800498
499 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800500 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800501 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800502 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800503
504 size_t size = resource->gpuMemorySize();
505 for (int i = 0; i < 2; ++i) {
506 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800507 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800508 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800509 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800510 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
511 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
512 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
513 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800515
516 // Once it is unrefed, it should become available as scratch.
517 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800518 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
519 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
520 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
521 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
522 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800523 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800524 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800525 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800526 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800527
528 if (0 == i) {
529 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
530 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800531 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800532 } else {
533 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800534 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800535 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
536 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
537 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
538 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800539 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800540 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800541 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800542
543 // now when it is unrefed it should die since it has no key.
544 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800545 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
546 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
547 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
548 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800549 }
bsalomon8b79d232014-11-10 10:19:06 -0800550 }
bsalomonc2f35b72015-01-23 07:19:22 -0800551}
552
553static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
554 Mock mock(5, 30000);
555 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800556 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800557
bsalomon8b79d232014-11-10 10:19:06 -0800558 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800559 TestResource* a = TestResource::CreateScratch(context->getGpu(),
560 TestResource::kB_SimulatedProperty);
561 TestResource* b = TestResource::CreateScratch(context->getGpu(),
562 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800563 a->setSize(11);
564 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800565 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800566 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800567 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800568 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800569
570 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800571 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800572
bsalomon0ea80f42015-02-11 10:49:59 -0800573 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800574 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800575 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
576 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800577 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800578 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800579
bsalomon63c992f2015-01-23 12:47:59 -0800580 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800581 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800582 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800583 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800584
585 // Unref but don't purge
586 a->unref();
587 b->unref();
588 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800589 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800590
bsalomon63c992f2015-01-23 12:47:59 -0800591 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800592 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800593 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800594 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
595 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800596}
597
bsalomon10e23ca2014-11-25 05:52:06 -0800598static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800599 Mock mock(5, 30000);
600 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800601 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800602
bsalomon10e23ca2014-11-25 05:52:06 -0800603 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800604 TestResource* a = TestResource::CreateScratch(context->getGpu(),
605 TestResource::kB_SimulatedProperty);
606 TestResource* b = TestResource::CreateScratch(context->getGpu(),
607 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800608 a->unref();
609 b->unref();
610
bsalomon1c60dfe2015-01-21 09:32:40 -0800611 GrScratchKey scratchKey;
612 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800613 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800614 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800615 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800616
bsalomon0ea80f42015-02-11 10:49:59 -0800617 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800618 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800619 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800620 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
621 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800622
623 // Find the first resource and remove its scratch key
624 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800625 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800626 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800627 // It's still alive, but not cached by scratch key anymore
628 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800629 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
630 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800631
632 // The cache should immediately delete it when it's unrefed since it isn't accessible.
633 find->unref();
634 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800635 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
636 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800637
638 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800639 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800640 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800641 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800642 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
643 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800644
645 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800646 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800647 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800648 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
649 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800650
651 find->unref();
652 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800653 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
654 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800655}
656
bsalomon1c60dfe2015-01-21 09:32:40 -0800657static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800658 Mock mock(5, 30000);
659 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800660 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800661
662 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800663 TestResource* a = TestResource::CreateScratch(context->getGpu(),
664 TestResource::kB_SimulatedProperty);
665 TestResource* b = TestResource::CreateScratch(context->getGpu(),
666 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800667 a->unref();
668 b->unref();
669
670 GrScratchKey scratchKey;
671 // Ensure that scratch key comparison and assignment is consistent.
672 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800673 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800674 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800675 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800676 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
677 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
678 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
679 scratchKey = scratchKey1;
680 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
681 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
682 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
683 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
684 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
685 scratchKey = scratchKey2;
686 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
687 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
688 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
689 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
690 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
691
692 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800693 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800694 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800695 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800696
697 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800698 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800699 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800700 REPORTER_ASSERT(reporter, find != NULL);
701 find->unref();
702
703 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800704 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800705 REPORTER_ASSERT(reporter, find != NULL);
706 REPORTER_ASSERT(reporter, find == a || find == b);
707
bsalomon0ea80f42015-02-11 10:49:59 -0800708 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800709 REPORTER_ASSERT(reporter, find2 != NULL);
710 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
711 REPORTER_ASSERT(reporter, find2 != find);
712 find2->unref();
713 find->unref();
714}
715
bsalomon8718aaf2015-02-19 07:24:21 -0800716static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800717 Mock mock(5, 30000);
718 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800719 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000720
bsalomon8718aaf2015-02-19 07:24:21 -0800721 GrUniqueKey key;
722 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800723
bsalomon8718aaf2015-02-19 07:24:21 -0800724 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800725 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800726 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800727
bsalomonf99e9612015-02-19 08:24:16 -0800728 // Set key on resource a.
729 a->resourcePriv().setUniqueKey(key);
730 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
731 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800732
bsalomonf99e9612015-02-19 08:24:16 -0800733 // Make sure that redundantly setting a's key works.
734 a->resourcePriv().setUniqueKey(key);
735 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800736 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800737 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
738 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800739 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
740
bsalomonf99e9612015-02-19 08:24:16 -0800741 // Create resource b and set the same key. It should replace a's unique key cache entry.
742 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
743 b->setSize(12);
744 b->resourcePriv().setUniqueKey(key);
745 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
746 b->unref();
747
748 // Still have two resources because a is still reffed.
749 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
750 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
752
753 a->unref();
754 // Now a should be gone.
755 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
756 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
757 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
758
759 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
760 // Also make b be unreffed when replacement occurs.
761 b->unref();
762 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
763 GrUniqueKey differentKey;
764 make_unique_key<0>(&differentKey, 1);
765 c->setSize(13);
766 c->resourcePriv().setUniqueKey(differentKey);
767 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
768 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
769 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
770 // c replaces b and b should be immediately purged.
771 c->resourcePriv().setUniqueKey(key);
772 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
773 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
774 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
775
776 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800777 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800778 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
779 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
780 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
781
782 // Drop the ref on c, it should be kept alive because it has a unique key.
783 c->unref();
784 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
785 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
786 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
787
788 // Verify that we can find c, then remove its unique key. It should get purged immediately.
789 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
790 c->resourcePriv().removeUniqueKey();
791 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800792 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
793 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800794 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000795}
796
bsalomon8b79d232014-11-10 10:19:06 -0800797static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800798 Mock mock(5, 30000);
799 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800800 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800801
bsalomon8718aaf2015-02-19 07:24:21 -0800802 GrUniqueKey key1, key2, key3;
803 make_unique_key<0>(&key1, 1);
804 make_unique_key<0>(&key2, 2);
805 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800806
bsalomon23e619c2015-02-06 11:54:28 -0800807 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800808 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
809 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800810 TestResource* c = TestResource::CreateScratch(context->getGpu(),
811 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800812 a->resourcePriv().setUniqueKey(key1);
813 b->resourcePriv().setUniqueKey(key2);
814 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800815 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800816 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800817 c->unref();
818
bsalomon8718aaf2015-02-19 07:24:21 -0800819 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
820 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
821 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800822 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800823
bsalomon8718aaf2015-02-19 07:24:21 -0800824 typedef GrUniqueKeyInvalidatedMessage Msg;
825 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800826
827 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
828 Bus::Post(Msg(key1));
829 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800830 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800831 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800832 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
833 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800834 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800835 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800836
837 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800838 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800839 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800840 // we still have a ref on b, c should be recycled as scratch.
841 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800842 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800843
bsalomon23e619c2015-02-06 11:54:28 -0800844 // make b purgeable. It should be immediately deleted since it has no key.
845 b->unref();
846 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
847
848 // Make sure we actually get to c via it's scratch key, before we say goodbye.
849 GrScratchKey scratchKey;
850 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800851 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800852 REPORTER_ASSERT(reporter, scratch == c);
853 SkSafeUnref(scratch);
854
855 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800856 cache->purgeAllUnlocked();
857 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800858 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800859 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
860 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800861 REPORTER_ASSERT(reporter, !scratch);
862 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800863}
864
bsalomon71cb0c22014-11-14 12:10:14 -0800865static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800866 Mock mock(3, 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;
871 make_unique_key<0>(&key1, 1);
872 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000873
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000874
bsalomonc2f35b72015-01-23 07:19:22 -0800875 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
876 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800877 a->resourcePriv().setUniqueKey(key1);
878 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800879
bsalomonc2f35b72015-01-23 07:19:22 -0800880 // Make a cycle
881 a->setUnrefWhenDestroyed(b);
882 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800883
bsalomonc2f35b72015-01-23 07:19:22 -0800884 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800885
bsalomonc2f35b72015-01-23 07:19:22 -0800886 a->unref();
887 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800888
bsalomonc2f35b72015-01-23 07:19:22 -0800889 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800890
bsalomon0ea80f42015-02-11 10:49:59 -0800891 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800892 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800893
bsalomonc2f35b72015-01-23 07:19:22 -0800894 // Break the cycle
895 a->setUnrefWhenDestroyed(NULL);
896 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800897
bsalomon0ea80f42015-02-11 10:49:59 -0800898 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800899 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000900}
901
bsalomon8b79d232014-11-10 10:19:06 -0800902static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800903 GrUniqueKey key1, key2;
904 make_unique_key<0>(&key1, 1);
905 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000906
907 // Test changing resources sizes (both increase & decrease).
908 {
bsalomonc2f35b72015-01-23 07:19:22 -0800909 Mock mock(3, 30000);
910 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800911 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000912
bsalomon5236cf42015-01-14 10:42:08 -0800913 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800914 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000915 a->unref();
916
bsalomon5236cf42015-01-14 10:42:08 -0800917 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800918 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000919 b->unref();
920
bsalomon0ea80f42015-02-11 10:49:59 -0800921 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
922 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800923 {
bsalomon8718aaf2015-02-19 07:24:21 -0800924 SkAutoTUnref<TestResource> find2(
925 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800926 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800927 SkAutoTUnref<TestResource> find1(
928 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800929 find1->setSize(50);
930 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000931
bsalomon0ea80f42015-02-11 10:49:59 -0800932 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
933 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000934 }
935
936 // Test increasing a resources size beyond the cache budget.
937 {
bsalomonc2f35b72015-01-23 07:19:22 -0800938 Mock mock(2, 300);
939 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800940 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000941
bsalomon5236cf42015-01-14 10:42:08 -0800942 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800943 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800944 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000945 a->unref();
946
bsalomon5236cf42015-01-14 10:42:08 -0800947 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800948 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800949 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000950 b->unref();
951
bsalomon0ea80f42015-02-11 10:49:59 -0800952 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
953 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000954
bsalomon8b79d232014-11-10 10:19:06 -0800955 {
bsalomon8718aaf2015-02-19 07:24:21 -0800956 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
957 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800958 find2->setSize(201);
959 }
bsalomon8718aaf2015-02-19 07:24:21 -0800960 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000961
bsalomon0ea80f42015-02-11 10:49:59 -0800962 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
963 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000964 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000965}
966
bsalomonddf30e62015-02-19 11:38:44 -0800967static void test_timestamp_wrap(skiatest::Reporter* reporter) {
968 static const int kCount = 50;
969 static const int kBudgetCnt = kCount / 2;
970 static const int kLockedFreq = 8;
971 static const int kBudgetSize = 0x80000000;
972
973 SkRandom random;
974
975 // Run the test 2*kCount times;
976 for (int i = 0; i < 2 * kCount; ++i ) {
977 Mock mock(kBudgetCnt, kBudgetSize);
978 GrContext* context = mock.context();
979 GrResourceCache* cache = mock.cache();
980
981 // Pick a random number of resources to add before the timestamp will wrap.
982 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
983
984 static const int kNumToPurge = kCount - kBudgetCnt;
985
986 SkTDArray<int> shouldPurgeIdxs;
987 int purgeableCnt = 0;
988 SkTDArray<GrGpuResource*> resourcesToUnref;
989
990 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
991 // unpurgeable resources.
992 for (int j = 0; j < kCount; ++j) {
993 GrUniqueKey key;
994 make_unique_key<0>(&key, j);
995
996 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
997 r->resourcePriv().setUniqueKey(key);
998 if (random.nextU() % kLockedFreq) {
999 // Make this is purgeable.
1000 r->unref();
1001 ++purgeableCnt;
1002 if (purgeableCnt <= kNumToPurge) {
1003 *shouldPurgeIdxs.append() = j;
1004 }
1005 } else {
1006 *resourcesToUnref.append() = r;
1007 }
1008 }
1009
1010 // Verify that the correct resources were purged.
1011 int currShouldPurgeIdx = 0;
1012 for (int j = 0; j < kCount; ++j) {
1013 GrUniqueKey key;
1014 make_unique_key<0>(&key, j);
1015 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1016 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1017 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1018 ++currShouldPurgeIdx;
1019 REPORTER_ASSERT(reporter, NULL == res);
1020 } else {
1021 REPORTER_ASSERT(reporter, NULL != res);
1022 }
1023 SkSafeUnref(res);
1024 }
1025
1026 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1027 resourcesToUnref[j]->unref();
1028 }
1029 }
1030}
1031
bsalomon3f324322015-04-08 11:01:54 -07001032static void test_flush(skiatest::Reporter* reporter) {
1033 Mock mock(1000000, 1000000);
1034 GrContext* context = mock.context();
1035 GrResourceCache* cache = mock.cache();
1036
1037 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1038 // power of two here to keep things simpler.
1039 static const int kFlushCount = 16;
1040 cache->setLimits(1000000, 1000000, kFlushCount);
1041
1042 {
1043 // Insert a resource and send a flush notification kFlushCount times.
1044 for (int i = 0; i < kFlushCount; ++i) {
1045 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1046 GrUniqueKey k;
1047 make_unique_key<1>(&k, i);
1048 r->resourcePriv().setUniqueKey(k);
1049 r->unref();
1050 cache->notifyFlushOccurred();
1051 }
1052
1053 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1054 for (int i = 0; i < kFlushCount - 1; ++i) {
1055 // The first resource was purged after the last flush in the initial loop, hence the -1.
1056 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1057 for (int j = 0; j < i; ++j) {
1058 GrUniqueKey k;
1059 make_unique_key<1>(&k, j);
1060 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1061 REPORTER_ASSERT(reporter, !SkToBool(r));
1062 SkSafeUnref(r);
1063 }
1064 cache->notifyFlushOccurred();
1065 }
1066
1067 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1068 cache->purgeAllUnlocked();
1069 }
1070
1071 // Do a similar test but where we leave refs on some resources to prevent them from being
1072 // purged.
1073 {
1074 GrGpuResource* refedResources[kFlushCount >> 1];
1075 for (int i = 0; i < kFlushCount; ++i) {
1076 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1077 GrUniqueKey k;
1078 make_unique_key<1>(&k, i);
1079 r->resourcePriv().setUniqueKey(k);
1080 // Leave a ref on every other resource, beginning with the first.
1081 if (SkToBool(i & 0x1)) {
1082 refedResources[i/2] = r;
1083 } else {
1084 r->unref();
1085 }
1086 cache->notifyFlushOccurred();
1087 }
1088
1089 for (int i = 0; i < kFlushCount; ++i) {
1090 // Should get a resource purged every other flush.
1091 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1092 cache->notifyFlushOccurred();
1093 }
1094
1095 // Unref all the resources that we kept refs on in the first loop.
1096 for (int i = 0; i < kFlushCount >> 1; ++i) {
1097 refedResources[i]->unref();
1098 }
1099
1100 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1101 // get kFlushCount additional flushes. Then everything should be purged.
1102 for (int i = 0; i < kFlushCount; ++i) {
1103 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1104 cache->notifyFlushOccurred();
1105 }
1106 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1107
1108 cache->purgeAllUnlocked();
1109 }
1110
1111 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1112}
1113
bsalomon10e23ca2014-11-25 05:52:06 -08001114static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001115 // Set the cache size to double the resource count because we're going to create 2x that number
1116 // resources, using two different key domains. Add a little slop to the bytes because we resize
1117 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001118 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001119
bsalomonc2f35b72015-01-23 07:19:22 -08001120 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1121 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001122 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001123
1124 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001125 GrUniqueKey key1, key2;
1126 make_unique_key<1>(&key1, i);
1127 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001128
bsalomon24db3b12015-01-23 04:24:04 -08001129 TestResource* resource;
1130
bsalomon10e23ca2014-11-25 05:52:06 -08001131 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001132 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001133 resource->setSize(1);
1134 resource->unref();
1135
bsalomon10e23ca2014-11-25 05:52:06 -08001136 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001137 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001138 resource->setSize(1);
1139 resource->unref();
1140 }
1141
1142 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001143 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1144 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1145 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1146 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001147 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001148 GrUniqueKey key1, key2;
1149 make_unique_key<1>(&key1, i);
1150 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001151
bsalomon8718aaf2015-02-19 07:24:21 -08001152 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1153 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001154 }
1155
bsalomon0ea80f42015-02-11 10:49:59 -08001156 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001157 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001158 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1159 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1160 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1161 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001162
1163 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001164 GrUniqueKey key1, key2;
1165 make_unique_key<1>(&key1, i);
1166 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001167
bsalomon8718aaf2015-02-19 07:24:21 -08001168 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1169 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001170 }
1171}
1172
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001173////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001174DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001175 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1176 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1177 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1178 continue;
1179 }
1180 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001181 if (NULL == context) {
1182 continue;
1183 }
bsalomonf2703d82014-10-28 14:33:06 -07001184 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001185 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001186 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001187 desc.fWidth = gWidth;
1188 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001189 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001190 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1191 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001192 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001193 test_stencil_buffers(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001194 }
bsalomon33435572014-11-05 14:47:41 -08001195
bsalomon8b79d232014-11-10 10:19:06 -08001196 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001197 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001198 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001199 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001200 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001201 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001202 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001203 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001204 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001205 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001206 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001207 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001208 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001209 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001210 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001211}
1212
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001213#endif