blob: f92ba87d6284352143990403523e569868c76a44 [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()) {
82 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
83 }
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()) {
87 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
88 }
89
bsalomon02a44a42015-02-19 09:09:00 -080090 REPORTER_ASSERT(reporter, smallRT0 && smallRT1 &&
91 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -080092 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
93 smallRT1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -080094
95 // An unbudgeted RT with the same desc should also share.
96 SkAutoTUnref<GrTexture> smallRT2(context->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -080097 if (smallRT2 && smallRT2->asRenderTarget()) {
98 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
99 }
bsalomon02a44a42015-02-19 09:09:00 -0800100 REPORTER_ASSERT(reporter, smallRT0 && smallRT2 &&
101 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -0800102 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
103 smallRT2->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800104
105 // An RT with a much larger size should not share.
106 GrSurfaceDesc bigDesc;
107 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
108 bigDesc.fConfig = kSkia8888_GrPixelConfig;
109 bigDesc.fWidth = 400;
110 bigDesc.fHeight = 200;
111 bigDesc.fSampleCnt = 0;
112 SkAutoTUnref<GrTexture> bigRT(context->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 if (bigRT && bigRT->asRenderTarget()) {
114 bigRT->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
115 }
bsalomon02a44a42015-02-19 09:09:00 -0800116 REPORTER_ASSERT(reporter, smallRT0 && bigRT &&
117 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -0800118 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
119 bigRT->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800120
121 if (context->getMaxSampleCount() >= 4) {
122 // An RT with a different sample count should not share.
123 GrSurfaceDesc smallMSAADesc = smallDesc;
124 smallMSAADesc.fSampleCnt = 4;
125 SkAutoTUnref<GrTexture> smallMSAART0(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800126 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
127 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
128 }
bsalomonb602d4d2015-02-19 12:05:58 -0800129#ifdef SK_BUILD_FOR_ANDROID
130 if (!smallMSAART0) {
131 // The nexus player seems to fail to create MSAA textures.
132 return;
133 }
134#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800135 REPORTER_ASSERT(reporter,
136 smallRT0 && smallMSAART0 &&
137 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
138 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
139 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800140 // A second MSAA RT should share with the first MSAA RT.
141 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800142 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
143 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
144 }
145 REPORTER_ASSERT(reporter,
146 smallMSAART0 && smallMSAART1 &&
147 smallMSAART0->asRenderTarget() &&
148 smallMSAART1->asRenderTarget() &&
149 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
150 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800151 // But not one with a larger sample count should not. (Also check that the request for 4
152 // samples didn't get rounded up to >= 8 or else they could share.).
153 if (context->getMaxSampleCount() >= 8 && smallMSAART0 && smallMSAART0->asRenderTarget() &&
154 smallMSAART0->asRenderTarget()->numSamples() < 8) {
155 smallMSAADesc.fSampleCnt = 8;
156 smallMSAART1.reset(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800157 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
158 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
159 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
160 }
161 REPORTER_ASSERT(reporter,
162 smallMSAART0 && smallMSAART1 &&
163 smallMSAART0->asRenderTarget() &&
164 smallMSAART1->asRenderTarget() &&
165 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
166 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800167 }
168 }
169}
170
bsalomon6d3fe022014-07-25 08:35:45 -0700171class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000172 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800173 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000174public:
175 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800176 /** Property that distinctly categorizes the resource.
177 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800178 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800179
bsalomon5236cf42015-01-14 10:42:08 -0800180 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
181 : INHERITED(gpu, lifeCycle)
182 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800183 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800184 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800185 ++fNumAlive;
186 this->registerWithCache();
187 }
188
189 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
190 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800191 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800192 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800193 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800194 ++fNumAlive;
195 this->registerWithCache();
196 }
197
bsalomon8b79d232014-11-10 10:19:06 -0800198 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800199 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000200 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800201 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800202 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800203 ++fNumAlive;
204 this->registerWithCache();
205 }
206
bsalomon23e619c2015-02-06 11:54:28 -0800207 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800208 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000209 }
210
211 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800212 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800213 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000214 }
215
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000216 void setSize(size_t size) {
217 fSize = size;
218 this->didChangeGpuMemorySize();
219 }
220
bsalomon33435572014-11-05 14:47:41 -0800221 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000222
bsalomon71cb0c22014-11-14 12:10:14 -0800223 void setUnrefWhenDestroyed(TestResource* resource) {
224 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000225 }
226
bsalomon1c60dfe2015-01-21 09:32:40 -0800227 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
228 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
229 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800230 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
231 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800232 }
233 }
234
235 static size_t ExpectedScratchKeySize() {
236 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
237 }
238
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000239private:
bsalomon24db3b12015-01-23 04:24:04 -0800240 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800241
bsalomonc2f35b72015-01-23 07:19:22 -0800242 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
243 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800244 , fToDelete(NULL)
245 , fSize(kDefaultSize)
246 , fProperty(property) {
247 GrScratchKey scratchKey;
248 ComputeScratchKey(fProperty, &scratchKey);
249 this->setScratchKey(scratchKey);
250 ++fNumAlive;
251 this->registerWithCache();
252 }
253
mtklein36352bf2015-03-25 18:17:31 -0700254 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800255
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000256 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000257 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800258 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800259 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700260 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000261};
bsalomon33435572014-11-05 14:47:41 -0800262int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000263
bsalomonc2f35b72015-01-23 07:19:22 -0800264class Mock {
265public:
266 Mock(int maxCnt, size_t maxBytes) {
267 fContext.reset(GrContext::CreateMockContext());
268 SkASSERT(fContext);
269 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800270 GrResourceCache* cache = fContext->getResourceCache();
271 cache->purgeAllUnlocked();
272 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800273 }
bsalomonc2f35b72015-01-23 07:19:22 -0800274
bsalomon0ea80f42015-02-11 10:49:59 -0800275 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800276
277 GrContext* context() { return fContext; }
278
279private:
280 SkAutoTUnref<GrContext> fContext;
281};
282
283static void test_no_key(skiatest::Reporter* reporter) {
284 Mock mock(10, 30000);
285 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800286 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800287
288 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800289 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
290 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
291 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
292 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800293 a->setSize(11);
294 b->setSize(12);
295 c->setSize(13);
296 d->setSize(14);
297
298 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800299 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800300 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800301 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800302
303 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800304 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800305 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
306
bsalomon8718aaf2015-02-19 07:24:21 -0800307 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800308
309 a->unref();
310 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800311 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800312 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800313 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800314
315 c->unref();
316 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800317 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800318 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800319 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800320
321 d->unref();
322 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800323 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
324 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800325
326 b->unref();
327 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800328 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
329 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800330}
331
bsalomon24db3b12015-01-23 04:24:04 -0800332// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800333template <int> static void make_unique_key(GrUniqueKey* key, int data) {
334 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
335 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800336 builder[0] = data;
337}
338
bsalomon84c8e622014-11-17 09:33:27 -0800339static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800340 Mock mock(10, 300);
341 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800342 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800343
bsalomon8718aaf2015-02-19 07:24:21 -0800344 GrUniqueKey uniqueKey;
345 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800346
bsalomon8718aaf2015-02-19 07:24:21 -0800347 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800348 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800349 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800350 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800351 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
352 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800353 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800354 TestResource* wrapped = SkNEW_ARGS(TestResource,
355 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
356 wrapped->setSize(12);
357 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
358 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800359 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800360
bsalomon8718aaf2015-02-19 07:24:21 -0800361 // Make sure we can't add a unique key to the wrapped resource
362 GrUniqueKey uniqueKey2;
363 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800364 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800365 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800366
367 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800368 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800369 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800370 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800371 cache->getResourceBytes());
372 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800373 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800374 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800375
bsalomon63c992f2015-01-23 12:47:59 -0800376 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800377 cache->purgeAllUnlocked();
378 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800379 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800380 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800381 cache->getResourceBytes());
382 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800383 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800384 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800385
386 // Unreffing the wrapped resource should free it right away.
387 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800388 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800389 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800390 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800391
bsalomon84c8e622014-11-17 09:33:27 -0800392 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800393 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800394 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800395 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800396 cache->purgeAllUnlocked();
397 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800398 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800399 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
400 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
401 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800402
403 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800404 cache->purgeAllUnlocked();
405 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800406 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800407 cache->getResourceBytes());
408 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
409 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800410
411 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800412 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
413 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
414 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
415 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800416
417 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800418 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
419 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
420 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
421 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800422}
423
bsalomon5236cf42015-01-14 10:42:08 -0800424static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800425 Mock mock(10, 30000);
426 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800427 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800428
bsalomon8718aaf2015-02-19 07:24:21 -0800429 GrUniqueKey uniqueKey;
430 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800431
432 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800433 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800434 TestResource* wrapped;
435 TestResource* unbudgeted;
436
437 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800438 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800439 scratch->setSize(10);
440 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800441 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
442 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
443 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
444 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800445
bsalomon8718aaf2015-02-19 07:24:21 -0800446 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
447 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800448 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800449 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800450 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
451 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
452 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
453 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800454
bsalomon0ea80f42015-02-11 10:49:59 -0800455 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800456 unbudgeted = SkNEW_ARGS(TestResource,
457 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800458 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
459 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
460 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
461 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800462
463 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800464 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
465 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
466 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
467 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800468
469 wrapped = SkNEW_ARGS(TestResource,
470 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800471 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
472 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
473 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
474 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800475
476 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800477 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
478 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
479 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
480 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800481
bsalomon0ea80f42015-02-11 10:49:59 -0800482 cache->purgeAllUnlocked();
483 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
484 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
485 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
486 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800487}
488
bsalomon3582d3e2015-02-13 14:20:05 -0800489// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
490void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
491/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800492 Mock mock(10, 300);
493 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800494 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800495
496 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800497 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800498 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800499 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800500
501 size_t size = resource->gpuMemorySize();
502 for (int i = 0; i < 2; ++i) {
503 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800504 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800505 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800506 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800507 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
508 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
509 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
510 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
511 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800512
513 // Once it is unrefed, it should become available as scratch.
514 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800515 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
516 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
517 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
518 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
519 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800520 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800521 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800522 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800523 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800524
525 if (0 == i) {
526 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
527 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800528 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800529 } else {
530 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800531 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800532 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800536 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800537 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800538 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800539
540 // now when it is unrefed it should die since it has no key.
541 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800542 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
543 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
544 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
545 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800546 }
bsalomon8b79d232014-11-10 10:19:06 -0800547 }
bsalomonc2f35b72015-01-23 07:19:22 -0800548}
549
550static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
551 Mock mock(5, 30000);
552 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800553 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800554
bsalomon8b79d232014-11-10 10:19:06 -0800555 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800556 TestResource* a = TestResource::CreateScratch(context->getGpu(),
557 TestResource::kB_SimulatedProperty);
558 TestResource* b = TestResource::CreateScratch(context->getGpu(),
559 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800560 a->setSize(11);
561 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800562 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800563 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800564 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800565 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800566
567 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800568 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800569
bsalomon0ea80f42015-02-11 10:49:59 -0800570 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800571 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800572 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
573 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800574 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800575 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800576
bsalomon63c992f2015-01-23 12:47:59 -0800577 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800578 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800579 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800580 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800581
582 // Unref but don't purge
583 a->unref();
584 b->unref();
585 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800586 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800587
bsalomon63c992f2015-01-23 12:47:59 -0800588 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800589 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800590 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800591 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
592 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800593}
594
bsalomon10e23ca2014-11-25 05:52:06 -0800595static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800596 Mock mock(5, 30000);
597 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800598 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800599
bsalomon10e23ca2014-11-25 05:52:06 -0800600 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800601 TestResource* a = TestResource::CreateScratch(context->getGpu(),
602 TestResource::kB_SimulatedProperty);
603 TestResource* b = TestResource::CreateScratch(context->getGpu(),
604 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800605 a->unref();
606 b->unref();
607
bsalomon1c60dfe2015-01-21 09:32:40 -0800608 GrScratchKey scratchKey;
609 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800610 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800611 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800612 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800613
bsalomon0ea80f42015-02-11 10:49:59 -0800614 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800615 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800616 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800617 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
618 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800619
620 // Find the first resource and remove its scratch key
621 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800622 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800623 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800624 // It's still alive, but not cached by scratch key anymore
625 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800626 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
627 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800628
629 // The cache should immediately delete it when it's unrefed since it isn't accessible.
630 find->unref();
631 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800632 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
633 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800634
635 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800636 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800637 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800638 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800639 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
640 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800641
642 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800643 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800644 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800645 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
646 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800647
648 find->unref();
649 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800650 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
651 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800652}
653
bsalomon1c60dfe2015-01-21 09:32:40 -0800654static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800655 Mock mock(5, 30000);
656 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800657 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800658
659 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800660 TestResource* a = TestResource::CreateScratch(context->getGpu(),
661 TestResource::kB_SimulatedProperty);
662 TestResource* b = TestResource::CreateScratch(context->getGpu(),
663 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800664 a->unref();
665 b->unref();
666
667 GrScratchKey scratchKey;
668 // Ensure that scratch key comparison and assignment is consistent.
669 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800670 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800671 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800672 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800673 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
674 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
675 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
676 scratchKey = scratchKey1;
677 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
678 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
679 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
680 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
681 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
682 scratchKey = scratchKey2;
683 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
684 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
685 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
686 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
687 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
688
689 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800690 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800691 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800692 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800693
694 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800695 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800696 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800697 REPORTER_ASSERT(reporter, find != NULL);
698 find->unref();
699
700 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800701 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800702 REPORTER_ASSERT(reporter, find != NULL);
703 REPORTER_ASSERT(reporter, find == a || find == b);
704
bsalomon0ea80f42015-02-11 10:49:59 -0800705 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800706 REPORTER_ASSERT(reporter, find2 != NULL);
707 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
708 REPORTER_ASSERT(reporter, find2 != find);
709 find2->unref();
710 find->unref();
711}
712
bsalomon8718aaf2015-02-19 07:24:21 -0800713static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800714 Mock mock(5, 30000);
715 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800716 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000717
bsalomon8718aaf2015-02-19 07:24:21 -0800718 GrUniqueKey key;
719 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800720
bsalomon8718aaf2015-02-19 07:24:21 -0800721 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800722 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800723 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800724
bsalomonf99e9612015-02-19 08:24:16 -0800725 // Set key on resource a.
726 a->resourcePriv().setUniqueKey(key);
727 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
728 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800729
bsalomonf99e9612015-02-19 08:24:16 -0800730 // Make sure that redundantly setting a's key works.
731 a->resourcePriv().setUniqueKey(key);
732 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800733 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800734 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
735 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800736 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
737
bsalomonf99e9612015-02-19 08:24:16 -0800738 // Create resource b and set the same key. It should replace a's unique key cache entry.
739 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
740 b->setSize(12);
741 b->resourcePriv().setUniqueKey(key);
742 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
743 b->unref();
744
745 // Still have two resources because a is still reffed.
746 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
747 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
749
750 a->unref();
751 // Now a should be gone.
752 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
753 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
754 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
755
756 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
757 // Also make b be unreffed when replacement occurs.
758 b->unref();
759 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
760 GrUniqueKey differentKey;
761 make_unique_key<0>(&differentKey, 1);
762 c->setSize(13);
763 c->resourcePriv().setUniqueKey(differentKey);
764 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
765 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
766 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
767 // c replaces b and b should be immediately purged.
768 c->resourcePriv().setUniqueKey(key);
769 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
770 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
771 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
772
773 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800774 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800775 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
776 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
777 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
778
779 // Drop the ref on c, it should be kept alive because it has a unique key.
780 c->unref();
781 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
782 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
783 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
784
785 // Verify that we can find c, then remove its unique key. It should get purged immediately.
786 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
787 c->resourcePriv().removeUniqueKey();
788 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800789 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
790 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800791 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000792}
793
bsalomon8b79d232014-11-10 10:19:06 -0800794static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800795 Mock mock(5, 30000);
796 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800797 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800798
bsalomon8718aaf2015-02-19 07:24:21 -0800799 GrUniqueKey key1, key2, key3;
800 make_unique_key<0>(&key1, 1);
801 make_unique_key<0>(&key2, 2);
802 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800803
bsalomon23e619c2015-02-06 11:54:28 -0800804 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800805 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
806 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800807 TestResource* c = TestResource::CreateScratch(context->getGpu(),
808 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800809 a->resourcePriv().setUniqueKey(key1);
810 b->resourcePriv().setUniqueKey(key2);
811 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800812 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800813 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800814 c->unref();
815
bsalomon8718aaf2015-02-19 07:24:21 -0800816 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
817 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
818 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800819 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800820
bsalomon8718aaf2015-02-19 07:24:21 -0800821 typedef GrUniqueKeyInvalidatedMessage Msg;
822 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800823
824 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
825 Bus::Post(Msg(key1));
826 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800827 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800828 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800829 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
830 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800831 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800832 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800833
834 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800835 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800836 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800837 // we still have a ref on b, c should be recycled as scratch.
838 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800839 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800840
bsalomon23e619c2015-02-06 11:54:28 -0800841 // make b purgeable. It should be immediately deleted since it has no key.
842 b->unref();
843 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
844
845 // Make sure we actually get to c via it's scratch key, before we say goodbye.
846 GrScratchKey scratchKey;
847 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800848 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800849 REPORTER_ASSERT(reporter, scratch == c);
850 SkSafeUnref(scratch);
851
852 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800853 cache->purgeAllUnlocked();
854 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800855 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800856 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
857 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800858 REPORTER_ASSERT(reporter, !scratch);
859 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800860}
861
bsalomon71cb0c22014-11-14 12:10:14 -0800862static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800863 Mock mock(3, 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;
868 make_unique_key<0>(&key1, 1);
869 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000870
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000871
bsalomonc2f35b72015-01-23 07:19:22 -0800872 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
873 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800874 a->resourcePriv().setUniqueKey(key1);
875 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800876
bsalomonc2f35b72015-01-23 07:19:22 -0800877 // Make a cycle
878 a->setUnrefWhenDestroyed(b);
879 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800880
bsalomonc2f35b72015-01-23 07:19:22 -0800881 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800882
bsalomonc2f35b72015-01-23 07:19:22 -0800883 a->unref();
884 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800885
bsalomonc2f35b72015-01-23 07:19:22 -0800886 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800887
bsalomon0ea80f42015-02-11 10:49:59 -0800888 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800889 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800890
bsalomonc2f35b72015-01-23 07:19:22 -0800891 // Break the cycle
892 a->setUnrefWhenDestroyed(NULL);
893 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800894
bsalomon0ea80f42015-02-11 10:49:59 -0800895 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800896 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000897}
898
bsalomon8b79d232014-11-10 10:19:06 -0800899static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800900 GrUniqueKey key1, key2;
901 make_unique_key<0>(&key1, 1);
902 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000903
904 // Test changing resources sizes (both increase & decrease).
905 {
bsalomonc2f35b72015-01-23 07:19:22 -0800906 Mock mock(3, 30000);
907 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800908 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000909
bsalomon5236cf42015-01-14 10:42:08 -0800910 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800911 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000912 a->unref();
913
bsalomon5236cf42015-01-14 10:42:08 -0800914 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800915 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000916 b->unref();
917
bsalomon0ea80f42015-02-11 10:49:59 -0800918 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
919 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800920 {
bsalomon8718aaf2015-02-19 07:24:21 -0800921 SkAutoTUnref<TestResource> find2(
922 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800923 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800924 SkAutoTUnref<TestResource> find1(
925 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800926 find1->setSize(50);
927 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000928
bsalomon0ea80f42015-02-11 10:49:59 -0800929 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
930 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000931 }
932
933 // Test increasing a resources size beyond the cache budget.
934 {
bsalomonc2f35b72015-01-23 07:19:22 -0800935 Mock mock(2, 300);
936 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800937 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000938
bsalomon5236cf42015-01-14 10:42:08 -0800939 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800940 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800941 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000942 a->unref();
943
bsalomon5236cf42015-01-14 10:42:08 -0800944 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800945 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800946 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000947 b->unref();
948
bsalomon0ea80f42015-02-11 10:49:59 -0800949 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
950 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000951
bsalomon8b79d232014-11-10 10:19:06 -0800952 {
bsalomon8718aaf2015-02-19 07:24:21 -0800953 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
954 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800955 find2->setSize(201);
956 }
bsalomon8718aaf2015-02-19 07:24:21 -0800957 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000958
bsalomon0ea80f42015-02-11 10:49:59 -0800959 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
960 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000961 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000962}
963
bsalomonddf30e62015-02-19 11:38:44 -0800964static void test_timestamp_wrap(skiatest::Reporter* reporter) {
965 static const int kCount = 50;
966 static const int kBudgetCnt = kCount / 2;
967 static const int kLockedFreq = 8;
968 static const int kBudgetSize = 0x80000000;
969
970 SkRandom random;
971
972 // Run the test 2*kCount times;
973 for (int i = 0; i < 2 * kCount; ++i ) {
974 Mock mock(kBudgetCnt, kBudgetSize);
975 GrContext* context = mock.context();
976 GrResourceCache* cache = mock.cache();
977
978 // Pick a random number of resources to add before the timestamp will wrap.
979 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
980
981 static const int kNumToPurge = kCount - kBudgetCnt;
982
983 SkTDArray<int> shouldPurgeIdxs;
984 int purgeableCnt = 0;
985 SkTDArray<GrGpuResource*> resourcesToUnref;
986
987 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
988 // unpurgeable resources.
989 for (int j = 0; j < kCount; ++j) {
990 GrUniqueKey key;
991 make_unique_key<0>(&key, j);
992
993 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
994 r->resourcePriv().setUniqueKey(key);
995 if (random.nextU() % kLockedFreq) {
996 // Make this is purgeable.
997 r->unref();
998 ++purgeableCnt;
999 if (purgeableCnt <= kNumToPurge) {
1000 *shouldPurgeIdxs.append() = j;
1001 }
1002 } else {
1003 *resourcesToUnref.append() = r;
1004 }
1005 }
1006
1007 // Verify that the correct resources were purged.
1008 int currShouldPurgeIdx = 0;
1009 for (int j = 0; j < kCount; ++j) {
1010 GrUniqueKey key;
1011 make_unique_key<0>(&key, j);
1012 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1013 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1014 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1015 ++currShouldPurgeIdx;
1016 REPORTER_ASSERT(reporter, NULL == res);
1017 } else {
1018 REPORTER_ASSERT(reporter, NULL != res);
1019 }
1020 SkSafeUnref(res);
1021 }
1022
1023 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1024 resourcesToUnref[j]->unref();
1025 }
1026 }
1027}
1028
bsalomon3f324322015-04-08 11:01:54 -07001029static void test_flush(skiatest::Reporter* reporter) {
1030 Mock mock(1000000, 1000000);
1031 GrContext* context = mock.context();
1032 GrResourceCache* cache = mock.cache();
1033
1034 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1035 // power of two here to keep things simpler.
1036 static const int kFlushCount = 16;
1037 cache->setLimits(1000000, 1000000, kFlushCount);
1038
1039 {
1040 // Insert a resource and send a flush notification kFlushCount times.
1041 for (int i = 0; i < kFlushCount; ++i) {
1042 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1043 GrUniqueKey k;
1044 make_unique_key<1>(&k, i);
1045 r->resourcePriv().setUniqueKey(k);
1046 r->unref();
1047 cache->notifyFlushOccurred();
1048 }
1049
1050 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1051 for (int i = 0; i < kFlushCount - 1; ++i) {
1052 // The first resource was purged after the last flush in the initial loop, hence the -1.
1053 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1054 for (int j = 0; j < i; ++j) {
1055 GrUniqueKey k;
1056 make_unique_key<1>(&k, j);
1057 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1058 REPORTER_ASSERT(reporter, !SkToBool(r));
1059 SkSafeUnref(r);
1060 }
1061 cache->notifyFlushOccurred();
1062 }
1063
1064 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1065 cache->purgeAllUnlocked();
1066 }
1067
1068 // Do a similar test but where we leave refs on some resources to prevent them from being
1069 // purged.
1070 {
1071 GrGpuResource* refedResources[kFlushCount >> 1];
1072 for (int i = 0; i < kFlushCount; ++i) {
1073 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1074 GrUniqueKey k;
1075 make_unique_key<1>(&k, i);
1076 r->resourcePriv().setUniqueKey(k);
1077 // Leave a ref on every other resource, beginning with the first.
1078 if (SkToBool(i & 0x1)) {
1079 refedResources[i/2] = r;
1080 } else {
1081 r->unref();
1082 }
1083 cache->notifyFlushOccurred();
1084 }
1085
1086 for (int i = 0; i < kFlushCount; ++i) {
1087 // Should get a resource purged every other flush.
1088 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1089 cache->notifyFlushOccurred();
1090 }
1091
1092 // Unref all the resources that we kept refs on in the first loop.
1093 for (int i = 0; i < kFlushCount >> 1; ++i) {
1094 refedResources[i]->unref();
1095 }
1096
1097 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1098 // get kFlushCount additional flushes. Then everything should be purged.
1099 for (int i = 0; i < kFlushCount; ++i) {
1100 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1101 cache->notifyFlushOccurred();
1102 }
1103 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1104
1105 cache->purgeAllUnlocked();
1106 }
1107
1108 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1109}
1110
bsalomon10e23ca2014-11-25 05:52:06 -08001111static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001112 // Set the cache size to double the resource count because we're going to create 2x that number
1113 // resources, using two different key domains. Add a little slop to the bytes because we resize
1114 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001115 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001116
bsalomonc2f35b72015-01-23 07:19:22 -08001117 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1118 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001119 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001120
1121 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001122 GrUniqueKey key1, key2;
1123 make_unique_key<1>(&key1, i);
1124 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001125
bsalomon24db3b12015-01-23 04:24:04 -08001126 TestResource* resource;
1127
bsalomon10e23ca2014-11-25 05:52:06 -08001128 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001129 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001130 resource->setSize(1);
1131 resource->unref();
1132
bsalomon10e23ca2014-11-25 05:52:06 -08001133 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001134 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001135 resource->setSize(1);
1136 resource->unref();
1137 }
1138
1139 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001140 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1141 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1142 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1143 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001144 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001145 GrUniqueKey key1, key2;
1146 make_unique_key<1>(&key1, i);
1147 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001148
bsalomon8718aaf2015-02-19 07:24:21 -08001149 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1150 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001151 }
1152
bsalomon0ea80f42015-02-11 10:49:59 -08001153 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001154 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001155 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1156 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1157 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1158 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001159
1160 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001161 GrUniqueKey key1, key2;
1162 make_unique_key<1>(&key1, i);
1163 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001164
bsalomon8718aaf2015-02-19 07:24:21 -08001165 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1166 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001167 }
1168}
1169
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001170////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001171DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001172 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1173 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1174 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1175 continue;
1176 }
1177 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001178 if (NULL == context) {
1179 continue;
1180 }
bsalomonf2703d82014-10-28 14:33:06 -07001181 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001182 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001183 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001184 desc.fWidth = gWidth;
1185 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001186 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001187 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1188 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001189 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001190 test_stencil_buffers(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001191 }
bsalomon33435572014-11-05 14:47:41 -08001192
bsalomon8b79d232014-11-10 10:19:06 -08001193 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001194 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001195 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001196 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001197 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001198 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001199 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001200 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001201 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001202 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001203 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001204 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001205 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001206 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001207 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001208}
1209
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001210#endif