blob: b6e1eb7c890709087676fa280f8e3f9cddbb6cd0 [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
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00008#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009
bsalomonbcf0a522014-10-08 08:40:09 -070010#include "GrContext.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#include "GrContextFactory.h"
bsalomonbcf0a522014-10-08 08:40:09 -070012#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourceCacheAccess.h"
14#include "GrGpuResourcePriv.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080015#include "GrRenderTarget.h"
16#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
bsalomonbcf0a522014-10-08 08:40:09 -070018#include "SkCanvas.h"
bsalomon71cb0c22014-11-14 12:10:14 -080019#include "SkGr.h"
20#include "SkMessageBus.h"
reed69f6f002014-09-18 06:09:44 -070021#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000022#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000023
24static const int gWidth = 640;
25static const int gHeight = 480;
26
27////////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -080028static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000029 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
30
31 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000032 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000033 src.eraseColor(SK_ColorBLACK);
34 size_t srcSize = src.getSize();
35
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000036 size_t initialCacheSize;
37 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000038
39 int oldMaxNum;
40 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000041 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000042
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000043 // Set the cache limits so we can fit 10 "src" images and the
44 // max number of textures doesn't matter
45 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000046 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000047
48 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000049 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000050
51 for (int i = 0; i < 100; ++i) {
52 canvas->drawBitmap(src, 0, 0);
53 canvas->readPixels(size, &readback);
54
55 // "modify" the src texture
56 src.notifyPixelsChanged();
57
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000058 size_t curCacheSize;
59 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060
61 // we should never go over the size limit
62 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
63 }
64
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000065 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000066}
67
bsalomon02a44a42015-02-19 09:09:00 -080068static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) {
69 GrSurfaceDesc smallDesc;
70 smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
71 smallDesc.fConfig = kSkia8888_GrPixelConfig;
72 smallDesc.fWidth = 4;
73 smallDesc.fHeight = 4;
74 smallDesc.fSampleCnt = 0;
75
76 // Test that two budgeted RTs with the same desc share a stencil buffer.
77 SkAutoTUnref<GrTexture> smallRT0(context->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080078 if (smallRT0 && smallRT0->asRenderTarget()) {
79 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
80 }
81
bsalomon02a44a42015-02-19 09:09:00 -080082 SkAutoTUnref<GrTexture> smallRT1(context->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080083 if (smallRT1 && smallRT1->asRenderTarget()) {
84 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
85 }
86
bsalomon02a44a42015-02-19 09:09:00 -080087 REPORTER_ASSERT(reporter, smallRT0 && smallRT1 &&
88 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -080089 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
90 smallRT1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -080091
92 // An unbudgeted RT with the same desc should also share.
93 SkAutoTUnref<GrTexture> smallRT2(context->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -080094 if (smallRT2 && smallRT2->asRenderTarget()) {
95 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
96 }
bsalomon02a44a42015-02-19 09:09:00 -080097 REPORTER_ASSERT(reporter, smallRT0 && smallRT2 &&
98 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -080099 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
100 smallRT2->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800101
102 // An RT with a much larger size should not share.
103 GrSurfaceDesc bigDesc;
104 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
105 bigDesc.fConfig = kSkia8888_GrPixelConfig;
106 bigDesc.fWidth = 400;
107 bigDesc.fHeight = 200;
108 bigDesc.fSampleCnt = 0;
109 SkAutoTUnref<GrTexture> bigRT(context->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800110 if (bigRT && bigRT->asRenderTarget()) {
111 bigRT->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
112 }
bsalomon02a44a42015-02-19 09:09:00 -0800113 REPORTER_ASSERT(reporter, smallRT0 && bigRT &&
114 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
bsalomon6bc1b5f2015-02-23 09:06:38 -0800115 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
116 bigRT->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800117
118 if (context->getMaxSampleCount() >= 4) {
119 // An RT with a different sample count should not share.
120 GrSurfaceDesc smallMSAADesc = smallDesc;
121 smallMSAADesc.fSampleCnt = 4;
122 SkAutoTUnref<GrTexture> smallMSAART0(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800123 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
124 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
125 }
bsalomonb602d4d2015-02-19 12:05:58 -0800126#ifdef SK_BUILD_FOR_ANDROID
127 if (!smallMSAART0) {
128 // The nexus player seems to fail to create MSAA textures.
129 return;
130 }
131#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800132 REPORTER_ASSERT(reporter,
133 smallRT0 && smallMSAART0 &&
134 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
135 smallRT0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
136 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800137 // A second MSAA RT should share with the first MSAA RT.
138 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800139 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
140 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
141 }
142 REPORTER_ASSERT(reporter,
143 smallMSAART0 && smallMSAART1 &&
144 smallMSAART0->asRenderTarget() &&
145 smallMSAART1->asRenderTarget() &&
146 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer() ==
147 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800148 // But not one with a larger sample count should not. (Also check that the request for 4
149 // samples didn't get rounded up to >= 8 or else they could share.).
150 if (context->getMaxSampleCount() >= 8 && smallMSAART0 && smallMSAART0->asRenderTarget() &&
151 smallMSAART0->asRenderTarget()->numSamples() < 8) {
152 smallMSAADesc.fSampleCnt = 8;
153 smallMSAART1.reset(context->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800154 SkAutoTUnref<GrTexture> smallMSAART1(context->createTexture(smallMSAADesc, false));
155 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
156 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilBuffer();
157 }
158 REPORTER_ASSERT(reporter,
159 smallMSAART0 && smallMSAART1 &&
160 smallMSAART0->asRenderTarget() &&
161 smallMSAART1->asRenderTarget() &&
162 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilBuffer() !=
163 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -0800164 }
165 }
166}
167
bsalomon6d3fe022014-07-25 08:35:45 -0700168class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000169 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800170 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000171public:
172 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800173 /** Property that distinctly categorizes the resource.
174 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800175 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800176
bsalomon5236cf42015-01-14 10:42:08 -0800177 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
178 : INHERITED(gpu, lifeCycle)
179 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800180 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800181 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800182 ++fNumAlive;
183 this->registerWithCache();
184 }
185
186 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
187 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800188 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800189 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800190 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800191 ++fNumAlive;
192 this->registerWithCache();
193 }
194
bsalomon8b79d232014-11-10 10:19:06 -0800195 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800196 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000197 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800198 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800199 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800200 ++fNumAlive;
201 this->registerWithCache();
202 }
203
bsalomon23e619c2015-02-06 11:54:28 -0800204 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800205 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000206 }
207
208 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800209 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800210 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000211 }
212
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000213 void setSize(size_t size) {
214 fSize = size;
215 this->didChangeGpuMemorySize();
216 }
217
bsalomon33435572014-11-05 14:47:41 -0800218 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000219
bsalomon71cb0c22014-11-14 12:10:14 -0800220 void setUnrefWhenDestroyed(TestResource* resource) {
221 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000222 }
223
bsalomon1c60dfe2015-01-21 09:32:40 -0800224 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
225 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
226 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800227 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
228 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800229 }
230 }
231
232 static size_t ExpectedScratchKeySize() {
233 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
234 }
235
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000236private:
bsalomon24db3b12015-01-23 04:24:04 -0800237 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800238
bsalomonc2f35b72015-01-23 07:19:22 -0800239 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
240 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800241 , fToDelete(NULL)
242 , fSize(kDefaultSize)
243 , fProperty(property) {
244 GrScratchKey scratchKey;
245 ComputeScratchKey(fProperty, &scratchKey);
246 this->setScratchKey(scratchKey);
247 ++fNumAlive;
248 this->registerWithCache();
249 }
250
bsalomon69ed47f2014-11-12 11:13:39 -0800251 size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
252
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000253 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000254 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800255 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800256 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700257 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000258};
bsalomon33435572014-11-05 14:47:41 -0800259int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000260
bsalomonc2f35b72015-01-23 07:19:22 -0800261class Mock {
262public:
263 Mock(int maxCnt, size_t maxBytes) {
264 fContext.reset(GrContext::CreateMockContext());
265 SkASSERT(fContext);
266 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800267 GrResourceCache* cache = fContext->getResourceCache();
268 cache->purgeAllUnlocked();
269 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800270 }
bsalomonc2f35b72015-01-23 07:19:22 -0800271
bsalomon0ea80f42015-02-11 10:49:59 -0800272 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800273
274 GrContext* context() { return fContext; }
275
276private:
277 SkAutoTUnref<GrContext> fContext;
278};
279
280static void test_no_key(skiatest::Reporter* reporter) {
281 Mock mock(10, 30000);
282 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800283 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800284
285 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800286 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
287 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
288 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
289 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800290 a->setSize(11);
291 b->setSize(12);
292 c->setSize(13);
293 d->setSize(14);
294
295 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800296 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800297 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800298 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800299
300 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800301 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800302 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
303
bsalomon8718aaf2015-02-19 07:24:21 -0800304 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800305
306 a->unref();
307 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800308 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800309 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800310 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800311
312 c->unref();
313 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800314 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800315 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800316 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800317
318 d->unref();
319 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800320 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
321 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800322
323 b->unref();
324 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800325 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
326 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800327}
328
bsalomon24db3b12015-01-23 04:24:04 -0800329// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800330template <int> static void make_unique_key(GrUniqueKey* key, int data) {
331 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
332 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800333 builder[0] = data;
334}
335
bsalomon84c8e622014-11-17 09:33:27 -0800336static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800337 Mock mock(10, 300);
338 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800339 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800340
bsalomon8718aaf2015-02-19 07:24:21 -0800341 GrUniqueKey uniqueKey;
342 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800343
bsalomon8718aaf2015-02-19 07:24:21 -0800344 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800345 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800346 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800347 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800348 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
349 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800350 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800351 TestResource* wrapped = SkNEW_ARGS(TestResource,
352 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
353 wrapped->setSize(12);
354 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
355 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800356 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800357
bsalomon8718aaf2015-02-19 07:24:21 -0800358 // Make sure we can't add a unique key to the wrapped resource
359 GrUniqueKey uniqueKey2;
360 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800361 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800362 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800363
364 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800365 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800366 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800367 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800368 cache->getResourceBytes());
369 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800370 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800371 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800372
bsalomon63c992f2015-01-23 12:47:59 -0800373 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800374 cache->purgeAllUnlocked();
375 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800376 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800377 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800378 cache->getResourceBytes());
379 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800380 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800381 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800382
383 // Unreffing the wrapped resource should free it right away.
384 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800385 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800386 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800387 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800388
bsalomon84c8e622014-11-17 09:33:27 -0800389 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800390 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800391 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800392 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800393 cache->purgeAllUnlocked();
394 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800395 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800396 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
397 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
398 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800399
400 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800401 cache->purgeAllUnlocked();
402 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800403 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800404 cache->getResourceBytes());
405 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
406 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800407
408 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800409 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
410 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
411 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
412 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800413
414 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800415 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
416 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
417 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
418 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800419}
420
bsalomon5236cf42015-01-14 10:42:08 -0800421static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800422 Mock mock(10, 30000);
423 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800424 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800425
bsalomon8718aaf2015-02-19 07:24:21 -0800426 GrUniqueKey uniqueKey;
427 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800428
429 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800430 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800431 TestResource* wrapped;
432 TestResource* unbudgeted;
433
434 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800435 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800436 scratch->setSize(10);
437 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800438 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
439 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
440 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
441 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800442
bsalomon8718aaf2015-02-19 07:24:21 -0800443 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
444 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800445 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800446 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800447 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
448 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
449 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
450 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800451
bsalomon0ea80f42015-02-11 10:49:59 -0800452 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800453 unbudgeted = SkNEW_ARGS(TestResource,
454 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800455 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
456 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
457 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
458 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800459
460 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800461 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
462 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
463 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
464 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800465
466 wrapped = SkNEW_ARGS(TestResource,
467 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800468 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
469 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
470 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
471 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800472
473 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800474 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
475 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
476 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
477 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800478
bsalomon0ea80f42015-02-11 10:49:59 -0800479 cache->purgeAllUnlocked();
480 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
481 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
482 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
483 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800484}
485
bsalomon3582d3e2015-02-13 14:20:05 -0800486// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
487void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
488/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800489 Mock mock(10, 300);
490 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800491 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800492
493 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800494 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800495 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800496 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800497
498 size_t size = resource->gpuMemorySize();
499 for (int i = 0; i < 2; ++i) {
500 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800501 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800502 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800503 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800504 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
505 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
506 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
507 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
508 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800509
510 // Once it is unrefed, it should become available as scratch.
511 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800512 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
513 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
514 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
515 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
516 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800517 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800518 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800519 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800520 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800521
522 if (0 == i) {
523 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
524 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800525 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800526 } else {
527 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800528 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800529 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
530 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
531 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
532 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800533 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800534 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800535 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800536
537 // now when it is unrefed it should die since it has no key.
538 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800539 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
540 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
541 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
542 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800543 }
bsalomon8b79d232014-11-10 10:19:06 -0800544 }
bsalomonc2f35b72015-01-23 07:19:22 -0800545}
546
547static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
548 Mock mock(5, 30000);
549 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800550 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800551
bsalomon8b79d232014-11-10 10:19:06 -0800552 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800553 TestResource* a = TestResource::CreateScratch(context->getGpu(),
554 TestResource::kB_SimulatedProperty);
555 TestResource* b = TestResource::CreateScratch(context->getGpu(),
556 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800557 a->setSize(11);
558 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800559 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800560 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800561 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800562 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800563
564 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800565 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800566
bsalomon0ea80f42015-02-11 10:49:59 -0800567 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800568 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800569 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
570 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800571 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800572 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800573
bsalomon63c992f2015-01-23 12:47:59 -0800574 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800575 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800576 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800577 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800578
579 // Unref but don't purge
580 a->unref();
581 b->unref();
582 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800583 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800584
bsalomon63c992f2015-01-23 12:47:59 -0800585 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800586 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800587 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800588 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
589 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800590}
591
bsalomon10e23ca2014-11-25 05:52:06 -0800592static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800593 Mock mock(5, 30000);
594 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800595 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800596
bsalomon10e23ca2014-11-25 05:52:06 -0800597 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800598 TestResource* a = TestResource::CreateScratch(context->getGpu(),
599 TestResource::kB_SimulatedProperty);
600 TestResource* b = TestResource::CreateScratch(context->getGpu(),
601 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800602 a->unref();
603 b->unref();
604
bsalomon1c60dfe2015-01-21 09:32:40 -0800605 GrScratchKey scratchKey;
606 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800607 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800608 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800609 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800610
bsalomon0ea80f42015-02-11 10:49:59 -0800611 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800612 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800613 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800614 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
615 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800616
617 // Find the first resource and remove its scratch key
618 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800619 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800620 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800621 // It's still alive, but not cached by scratch key anymore
622 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800623 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
624 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800625
626 // The cache should immediately delete it when it's unrefed since it isn't accessible.
627 find->unref();
628 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800629 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
630 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800631
632 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800633 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800634 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800635 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800636 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
637 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800638
639 // Should be able to call this multiple times with no problem.
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 find->unref();
646 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800647 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
648 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800649}
650
bsalomon1c60dfe2015-01-21 09:32:40 -0800651static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800652 Mock mock(5, 30000);
653 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800654 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800655
656 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800657 TestResource* a = TestResource::CreateScratch(context->getGpu(),
658 TestResource::kB_SimulatedProperty);
659 TestResource* b = TestResource::CreateScratch(context->getGpu(),
660 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800661 a->unref();
662 b->unref();
663
664 GrScratchKey scratchKey;
665 // Ensure that scratch key comparison and assignment is consistent.
666 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800667 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800668 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800669 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800670 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
671 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
672 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
673 scratchKey = scratchKey1;
674 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
675 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
676 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
677 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
678 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
679 scratchKey = scratchKey2;
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
686 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800687 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800688 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800689 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800690
691 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800692 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800693 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800694 REPORTER_ASSERT(reporter, find != NULL);
695 find->unref();
696
697 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800698 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800699 REPORTER_ASSERT(reporter, find != NULL);
700 REPORTER_ASSERT(reporter, find == a || find == b);
701
bsalomon0ea80f42015-02-11 10:49:59 -0800702 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800703 REPORTER_ASSERT(reporter, find2 != NULL);
704 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
705 REPORTER_ASSERT(reporter, find2 != find);
706 find2->unref();
707 find->unref();
708}
709
bsalomon8718aaf2015-02-19 07:24:21 -0800710static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800711 Mock mock(5, 30000);
712 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800713 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000714
bsalomon8718aaf2015-02-19 07:24:21 -0800715 GrUniqueKey key;
716 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800717
bsalomon8718aaf2015-02-19 07:24:21 -0800718 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800719 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800720 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800721
bsalomonf99e9612015-02-19 08:24:16 -0800722 // Set key on resource a.
723 a->resourcePriv().setUniqueKey(key);
724 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
725 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800726
bsalomonf99e9612015-02-19 08:24:16 -0800727 // Make sure that redundantly setting a's key works.
728 a->resourcePriv().setUniqueKey(key);
729 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800730 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800731 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
732 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800733 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
734
bsalomonf99e9612015-02-19 08:24:16 -0800735 // Create resource b and set the same key. It should replace a's unique key cache entry.
736 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
737 b->setSize(12);
738 b->resourcePriv().setUniqueKey(key);
739 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
740 b->unref();
741
742 // Still have two resources because a is still reffed.
743 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
744 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
745 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
746
747 a->unref();
748 // Now a should be gone.
749 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
750 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
751 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
752
753 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
754 // Also make b be unreffed when replacement occurs.
755 b->unref();
756 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
757 GrUniqueKey differentKey;
758 make_unique_key<0>(&differentKey, 1);
759 c->setSize(13);
760 c->resourcePriv().setUniqueKey(differentKey);
761 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
762 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
763 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
764 // c replaces b and b should be immediately purged.
765 c->resourcePriv().setUniqueKey(key);
766 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
767 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
768 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
769
770 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800771 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800772 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
773 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
774 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
775
776 // Drop the ref on c, it should be kept alive because it has a unique key.
777 c->unref();
778 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
779 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
780 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
781
782 // Verify that we can find c, then remove its unique key. It should get purged immediately.
783 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
784 c->resourcePriv().removeUniqueKey();
785 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800786 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
787 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800788 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000789}
790
bsalomon8b79d232014-11-10 10:19:06 -0800791static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800792 Mock mock(5, 30000);
793 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800794 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800795
bsalomon8718aaf2015-02-19 07:24:21 -0800796 GrUniqueKey key1, key2, key3;
797 make_unique_key<0>(&key1, 1);
798 make_unique_key<0>(&key2, 2);
799 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800800
bsalomon23e619c2015-02-06 11:54:28 -0800801 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800802 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
803 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800804 TestResource* c = TestResource::CreateScratch(context->getGpu(),
805 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800806 a->resourcePriv().setUniqueKey(key1);
807 b->resourcePriv().setUniqueKey(key2);
808 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800809 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800810 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800811 c->unref();
812
bsalomon8718aaf2015-02-19 07:24:21 -0800813 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
814 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
815 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800816 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800817
bsalomon8718aaf2015-02-19 07:24:21 -0800818 typedef GrUniqueKeyInvalidatedMessage Msg;
819 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800820
821 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
822 Bus::Post(Msg(key1));
823 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800824 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800825 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800826 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
827 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800828 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800829 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800830
831 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800832 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800833 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800834 // we still have a ref on b, c should be recycled as scratch.
835 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800836 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800837
bsalomon23e619c2015-02-06 11:54:28 -0800838 // make b purgeable. It should be immediately deleted since it has no key.
839 b->unref();
840 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
841
842 // Make sure we actually get to c via it's scratch key, before we say goodbye.
843 GrScratchKey scratchKey;
844 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800845 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800846 REPORTER_ASSERT(reporter, scratch == c);
847 SkSafeUnref(scratch);
848
849 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800850 cache->purgeAllUnlocked();
851 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800852 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800853 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
854 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800855 REPORTER_ASSERT(reporter, !scratch);
856 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800857}
858
bsalomon71cb0c22014-11-14 12:10:14 -0800859static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800860 Mock mock(3, 30000);
861 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800862 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800863
bsalomon8718aaf2015-02-19 07:24:21 -0800864 GrUniqueKey key1, key2;
865 make_unique_key<0>(&key1, 1);
866 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000867
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000868
bsalomonc2f35b72015-01-23 07:19:22 -0800869 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
870 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800871 a->resourcePriv().setUniqueKey(key1);
872 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800873
bsalomonc2f35b72015-01-23 07:19:22 -0800874 // Make a cycle
875 a->setUnrefWhenDestroyed(b);
876 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800877
bsalomonc2f35b72015-01-23 07:19:22 -0800878 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800879
bsalomonc2f35b72015-01-23 07:19:22 -0800880 a->unref();
881 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800882
bsalomonc2f35b72015-01-23 07:19:22 -0800883 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800884
bsalomon0ea80f42015-02-11 10:49:59 -0800885 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800886 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800887
bsalomonc2f35b72015-01-23 07:19:22 -0800888 // Break the cycle
889 a->setUnrefWhenDestroyed(NULL);
890 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800891
bsalomon0ea80f42015-02-11 10:49:59 -0800892 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800893 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000894}
895
bsalomon8b79d232014-11-10 10:19:06 -0800896static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800897 GrUniqueKey key1, key2;
898 make_unique_key<0>(&key1, 1);
899 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000900
901 // Test changing resources sizes (both increase & decrease).
902 {
bsalomonc2f35b72015-01-23 07:19:22 -0800903 Mock mock(3, 30000);
904 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800905 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000906
bsalomon5236cf42015-01-14 10:42:08 -0800907 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800908 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000909 a->unref();
910
bsalomon5236cf42015-01-14 10:42:08 -0800911 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800912 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000913 b->unref();
914
bsalomon0ea80f42015-02-11 10:49:59 -0800915 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
916 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800917 {
bsalomon8718aaf2015-02-19 07:24:21 -0800918 SkAutoTUnref<TestResource> find2(
919 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800920 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800921 SkAutoTUnref<TestResource> find1(
922 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800923 find1->setSize(50);
924 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000925
bsalomon0ea80f42015-02-11 10:49:59 -0800926 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
927 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000928 }
929
930 // Test increasing a resources size beyond the cache budget.
931 {
bsalomonc2f35b72015-01-23 07:19:22 -0800932 Mock mock(2, 300);
933 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800934 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000935
bsalomon5236cf42015-01-14 10:42:08 -0800936 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800937 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800938 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000939 a->unref();
940
bsalomon5236cf42015-01-14 10:42:08 -0800941 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800942 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800943 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000944 b->unref();
945
bsalomon0ea80f42015-02-11 10:49:59 -0800946 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
947 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000948
bsalomon8b79d232014-11-10 10:19:06 -0800949 {
bsalomon8718aaf2015-02-19 07:24:21 -0800950 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
951 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800952 find2->setSize(201);
953 }
bsalomon8718aaf2015-02-19 07:24:21 -0800954 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000955
bsalomon0ea80f42015-02-11 10:49:59 -0800956 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
957 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000958 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000959}
960
bsalomonddf30e62015-02-19 11:38:44 -0800961static void test_timestamp_wrap(skiatest::Reporter* reporter) {
962 static const int kCount = 50;
963 static const int kBudgetCnt = kCount / 2;
964 static const int kLockedFreq = 8;
965 static const int kBudgetSize = 0x80000000;
966
967 SkRandom random;
968
969 // Run the test 2*kCount times;
970 for (int i = 0; i < 2 * kCount; ++i ) {
971 Mock mock(kBudgetCnt, kBudgetSize);
972 GrContext* context = mock.context();
973 GrResourceCache* cache = mock.cache();
974
975 // Pick a random number of resources to add before the timestamp will wrap.
976 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
977
978 static const int kNumToPurge = kCount - kBudgetCnt;
979
980 SkTDArray<int> shouldPurgeIdxs;
981 int purgeableCnt = 0;
982 SkTDArray<GrGpuResource*> resourcesToUnref;
983
984 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
985 // unpurgeable resources.
986 for (int j = 0; j < kCount; ++j) {
987 GrUniqueKey key;
988 make_unique_key<0>(&key, j);
989
990 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
991 r->resourcePriv().setUniqueKey(key);
992 if (random.nextU() % kLockedFreq) {
993 // Make this is purgeable.
994 r->unref();
995 ++purgeableCnt;
996 if (purgeableCnt <= kNumToPurge) {
997 *shouldPurgeIdxs.append() = j;
998 }
999 } else {
1000 *resourcesToUnref.append() = r;
1001 }
1002 }
1003
1004 // Verify that the correct resources were purged.
1005 int currShouldPurgeIdx = 0;
1006 for (int j = 0; j < kCount; ++j) {
1007 GrUniqueKey key;
1008 make_unique_key<0>(&key, j);
1009 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1010 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1011 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1012 ++currShouldPurgeIdx;
1013 REPORTER_ASSERT(reporter, NULL == res);
1014 } else {
1015 REPORTER_ASSERT(reporter, NULL != res);
1016 }
1017 SkSafeUnref(res);
1018 }
1019
1020 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1021 resourcesToUnref[j]->unref();
1022 }
1023 }
1024}
1025
bsalomon10e23ca2014-11-25 05:52:06 -08001026static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001027 // Set the cache size to double the resource count because we're going to create 2x that number
1028 // resources, using two different key domains. Add a little slop to the bytes because we resize
1029 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001030 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001031
bsalomonc2f35b72015-01-23 07:19:22 -08001032 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1033 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001034 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001035
1036 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001037 GrUniqueKey key1, key2;
1038 make_unique_key<1>(&key1, i);
1039 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001040
bsalomon24db3b12015-01-23 04:24:04 -08001041 TestResource* resource;
1042
bsalomon10e23ca2014-11-25 05:52:06 -08001043 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001044 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001045 resource->setSize(1);
1046 resource->unref();
1047
bsalomon10e23ca2014-11-25 05:52:06 -08001048 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001049 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001050 resource->setSize(1);
1051 resource->unref();
1052 }
1053
1054 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001055 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1056 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1057 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1058 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001059 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001060 GrUniqueKey key1, key2;
1061 make_unique_key<1>(&key1, i);
1062 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001063
bsalomon8718aaf2015-02-19 07:24:21 -08001064 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1065 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001066 }
1067
bsalomon0ea80f42015-02-11 10:49:59 -08001068 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001069 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001070 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1071 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1072 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1073 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001074
1075 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001076 GrUniqueKey key1, key2;
1077 make_unique_key<1>(&key1, i);
1078 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001079
bsalomon8718aaf2015-02-19 07:24:21 -08001080 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1081 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001082 }
1083}
1084
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001085////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001086DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001087 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1088 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1089 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1090 continue;
1091 }
1092 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001093 if (NULL == context) {
1094 continue;
1095 }
bsalomonf2703d82014-10-28 14:33:06 -07001096 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001097 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001098 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001099 desc.fWidth = gWidth;
1100 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001101 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001102 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1103 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001104 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001105 test_stencil_buffers(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001106 }
bsalomon33435572014-11-05 14:47:41 -08001107
bsalomon8b79d232014-11-10 10:19:06 -08001108 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001109 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001110 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001111 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001112 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001113 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001114 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001115 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001116 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001117 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001118 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001119 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001120 test_timestamp_wrap(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001121 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001122}
1123
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001124#endif