blob: 71fb4e38b196f807b5ef1d7d51ce376be0ce2992 [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
bsalomond309e7a2015-04-30 14:18:54 -070079 GrTextureProvider* cache = context->textureProvider();
bsalomon02a44a42015-02-19 09:09:00 -080080 // Test that two budgeted RTs with the same desc share a stencil buffer.
bsalomond309e7a2015-04-30 14:18:54 -070081 SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080082 if (smallRT0 && smallRT0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070083 smallRT0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080084 }
85
bsalomond309e7a2015-04-30 14:18:54 -070086 SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true));
bsalomon6bc1b5f2015-02-23 09:06:38 -080087 if (smallRT1 && smallRT1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070088 smallRT1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -080089 }
90
egdaniel8dc7c3a2015-04-16 11:22:42 -070091 REPORTER_ASSERT(reporter,
92 smallRT0 && smallRT1 &&
93 smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
94 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
95 smallRT1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -080096
97 // An unbudgeted RT with the same desc should also share.
bsalomond309e7a2015-04-30 14:18:54 -070098 SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -080099 if (smallRT2 && smallRT2->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700100 smallRT2->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800101 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700102 REPORTER_ASSERT(reporter,
103 smallRT0 && smallRT2 &&
104 smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
105 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
106 smallRT2->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800107
108 // An RT with a much larger size should not share.
109 GrSurfaceDesc bigDesc;
110 bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
111 bigDesc.fConfig = kSkia8888_GrPixelConfig;
112 bigDesc.fWidth = 400;
113 bigDesc.fHeight = 200;
114 bigDesc.fSampleCnt = 0;
bsalomond309e7a2015-04-30 14:18:54 -0700115 SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800116 if (bigRT && bigRT->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700117 bigRT->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800118 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700119 REPORTER_ASSERT(reporter,
120 smallRT0 && bigRT &&
121 smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
122 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
123 bigRT->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800124
125 if (context->getMaxSampleCount() >= 4) {
126 // An RT with a different sample count should not share.
127 GrSurfaceDesc smallMSAADesc = smallDesc;
128 smallMSAADesc.fSampleCnt = 4;
bsalomond309e7a2015-04-30 14:18:54 -0700129 SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800130 if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700131 smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800132 }
bsalomonb602d4d2015-02-19 12:05:58 -0800133#ifdef SK_BUILD_FOR_ANDROID
134 if (!smallMSAART0) {
135 // The nexus player seems to fail to create MSAA textures.
136 return;
137 }
138#endif
bsalomon6bc1b5f2015-02-23 09:06:38 -0800139 REPORTER_ASSERT(reporter,
140 smallRT0 && smallMSAART0 &&
141 smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700142 smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
143 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800144 // A second MSAA RT should share with the first MSAA RT.
bsalomond309e7a2015-04-30 14:18:54 -0700145 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800146 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700147 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800148 }
149 REPORTER_ASSERT(reporter,
150 smallMSAART0 && smallMSAART1 &&
151 smallMSAART0->asRenderTarget() &&
152 smallMSAART1->asRenderTarget() &&
egdaniel8dc7c3a2015-04-16 11:22:42 -0700153 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() ==
154 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800155 // But not one with a larger sample count should not. (Also check that the request for 4
156 // samples didn't get rounded up to >= 8 or else they could share.).
157 if (context->getMaxSampleCount() >= 8 && smallMSAART0 && smallMSAART0->asRenderTarget() &&
158 smallMSAART0->asRenderTarget()->numSamples() < 8) {
159 smallMSAADesc.fSampleCnt = 8;
bsalomond309e7a2015-04-30 14:18:54 -0700160 smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
161 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800162 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700163 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800164 }
165 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700166 smallMSAART0 && smallMSAART1 &&
167 smallMSAART0->asRenderTarget() &&
168 smallMSAART1->asRenderTarget() &&
169 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
170 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800171 }
172 }
173}
174
bsalomon6d3fe022014-07-25 08:35:45 -0700175class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000176 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800177 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000178public:
179 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800180 /** Property that distinctly categorizes the resource.
181 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800182 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800183
bsalomon5236cf42015-01-14 10:42:08 -0800184 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
185 : INHERITED(gpu, lifeCycle)
186 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800187 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800188 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800189 ++fNumAlive;
190 this->registerWithCache();
191 }
192
193 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
194 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800195 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800196 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800197 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800198 ++fNumAlive;
199 this->registerWithCache();
200 }
201
bsalomon8b79d232014-11-10 10:19:06 -0800202 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800203 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000204 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800205 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800206 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800207 ++fNumAlive;
208 this->registerWithCache();
209 }
210
bsalomon23e619c2015-02-06 11:54:28 -0800211 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800212 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000213 }
214
215 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800216 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800217 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000218 }
219
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000220 void setSize(size_t size) {
221 fSize = size;
222 this->didChangeGpuMemorySize();
223 }
224
bsalomon33435572014-11-05 14:47:41 -0800225 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000226
bsalomon71cb0c22014-11-14 12:10:14 -0800227 void setUnrefWhenDestroyed(TestResource* resource) {
228 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000229 }
230
bsalomon1c60dfe2015-01-21 09:32:40 -0800231 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
232 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
233 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800234 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
235 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800236 }
237 }
238
239 static size_t ExpectedScratchKeySize() {
240 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
241 }
242
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000243private:
bsalomon24db3b12015-01-23 04:24:04 -0800244 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800245
bsalomonc2f35b72015-01-23 07:19:22 -0800246 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
247 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800248 , fToDelete(NULL)
249 , fSize(kDefaultSize)
250 , fProperty(property) {
251 GrScratchKey scratchKey;
252 ComputeScratchKey(fProperty, &scratchKey);
253 this->setScratchKey(scratchKey);
254 ++fNumAlive;
255 this->registerWithCache();
256 }
257
mtklein36352bf2015-03-25 18:17:31 -0700258 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800259
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000260 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000261 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800262 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800263 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700264 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000265};
bsalomon33435572014-11-05 14:47:41 -0800266int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000267
bsalomonc2f35b72015-01-23 07:19:22 -0800268class Mock {
269public:
270 Mock(int maxCnt, size_t maxBytes) {
271 fContext.reset(GrContext::CreateMockContext());
272 SkASSERT(fContext);
273 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800274 GrResourceCache* cache = fContext->getResourceCache();
275 cache->purgeAllUnlocked();
276 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800277 }
bsalomonc2f35b72015-01-23 07:19:22 -0800278
bsalomon0ea80f42015-02-11 10:49:59 -0800279 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800280
281 GrContext* context() { return fContext; }
282
283private:
284 SkAutoTUnref<GrContext> fContext;
285};
286
287static void test_no_key(skiatest::Reporter* reporter) {
288 Mock mock(10, 30000);
289 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800290 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800291
292 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800293 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
294 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
295 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
296 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800297 a->setSize(11);
298 b->setSize(12);
299 c->setSize(13);
300 d->setSize(14);
301
302 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800303 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800304 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800305 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800306
307 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800308 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800309 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
310
bsalomon8718aaf2015-02-19 07:24:21 -0800311 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800312
313 a->unref();
314 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800315 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800316 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800317 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800318
319 c->unref();
320 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800321 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800322 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800323 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800324
325 d->unref();
326 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800327 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
328 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800329
330 b->unref();
331 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800332 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
333 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800334}
335
bsalomon24db3b12015-01-23 04:24:04 -0800336// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800337template <int> static void make_unique_key(GrUniqueKey* key, int data) {
338 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
339 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800340 builder[0] = data;
341}
342
bsalomon84c8e622014-11-17 09:33:27 -0800343static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800344 Mock mock(10, 300);
345 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800346 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800347
bsalomon8718aaf2015-02-19 07:24:21 -0800348 GrUniqueKey uniqueKey;
349 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800350
bsalomon8718aaf2015-02-19 07:24:21 -0800351 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800352 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800353 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800354 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800355 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
356 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800357 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800358 TestResource* wrapped = SkNEW_ARGS(TestResource,
359 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
360 wrapped->setSize(12);
361 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
362 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800363 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800364
bsalomon8718aaf2015-02-19 07:24:21 -0800365 // Make sure we can't add a unique key to the wrapped resource
366 GrUniqueKey uniqueKey2;
367 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800368 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800369 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800370
371 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800372 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800373 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800374 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800375 cache->getResourceBytes());
376 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800377 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800378 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800379
bsalomon63c992f2015-01-23 12:47:59 -0800380 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800381 cache->purgeAllUnlocked();
382 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800383 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800384 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800385 cache->getResourceBytes());
386 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800387 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800388 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800389
390 // Unreffing the wrapped resource should free it right away.
391 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800392 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800393 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800394 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800395
bsalomon84c8e622014-11-17 09:33:27 -0800396 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800397 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800398 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800399 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800400 cache->purgeAllUnlocked();
401 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800402 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800403 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
404 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
405 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800406
407 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800408 cache->purgeAllUnlocked();
409 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800410 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800411 cache->getResourceBytes());
412 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
413 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800414
415 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800416 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
417 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
418 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
419 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800420
421 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800422 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
423 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
424 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
425 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800426}
427
bsalomon5236cf42015-01-14 10:42:08 -0800428static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800429 Mock mock(10, 30000);
430 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800431 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800432
bsalomon8718aaf2015-02-19 07:24:21 -0800433 GrUniqueKey uniqueKey;
434 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800435
436 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800437 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800438 TestResource* wrapped;
439 TestResource* unbudgeted;
440
441 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800442 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800443 scratch->setSize(10);
444 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800445 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
446 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
447 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
448 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800449
bsalomon8718aaf2015-02-19 07:24:21 -0800450 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
451 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800452 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800453 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800454 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
455 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
456 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
457 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800458
bsalomon0ea80f42015-02-11 10:49:59 -0800459 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800460 unbudgeted = SkNEW_ARGS(TestResource,
461 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800462 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
463 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
464 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
465 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800466
467 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800468 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
469 REPORTER_ASSERT(reporter, 21 == 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 = SkNEW_ARGS(TestResource,
474 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800475 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
476 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
477 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
478 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800479
480 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800481 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
482 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
483 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
484 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800485
bsalomon0ea80f42015-02-11 10:49:59 -0800486 cache->purgeAllUnlocked();
487 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
488 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
489 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
490 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800491}
492
bsalomon3582d3e2015-02-13 14:20:05 -0800493// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
494void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
495/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800496 Mock mock(10, 300);
497 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800498 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800499
500 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800501 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800502 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800503 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800504
505 size_t size = resource->gpuMemorySize();
506 for (int i = 0; i < 2; ++i) {
507 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800508 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800509 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800510 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800511 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
512 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
513 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
514 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
515 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800516
517 // Once it is unrefed, it should become available as scratch.
518 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800519 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
520 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
521 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
522 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
523 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800524 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800525 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800526 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800527 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800528
529 if (0 == i) {
530 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
531 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800532 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800533 } else {
534 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800535 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800536 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
537 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
538 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
539 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800540 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800541 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800542 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800543
544 // now when it is unrefed it should die since it has no key.
545 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800546 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
547 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
548 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
549 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800550 }
bsalomon8b79d232014-11-10 10:19:06 -0800551 }
bsalomonc2f35b72015-01-23 07:19:22 -0800552}
553
554static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
555 Mock mock(5, 30000);
556 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800557 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800558
bsalomon8b79d232014-11-10 10:19:06 -0800559 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800560 TestResource* a = TestResource::CreateScratch(context->getGpu(),
561 TestResource::kB_SimulatedProperty);
562 TestResource* b = TestResource::CreateScratch(context->getGpu(),
563 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800564 a->setSize(11);
565 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800566 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800567 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800568 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800569 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800570
571 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800572 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800573
bsalomon0ea80f42015-02-11 10:49:59 -0800574 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800575 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800576 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
577 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800578 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800579 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800580
bsalomon63c992f2015-01-23 12:47:59 -0800581 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800582 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800583 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800584 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800585
586 // Unref but don't purge
587 a->unref();
588 b->unref();
589 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800590 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800591
bsalomon63c992f2015-01-23 12:47:59 -0800592 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800593 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800594 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800595 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
596 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800597}
598
bsalomon10e23ca2014-11-25 05:52:06 -0800599static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800600 Mock mock(5, 30000);
601 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800602 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800603
bsalomon10e23ca2014-11-25 05:52:06 -0800604 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800605 TestResource* a = TestResource::CreateScratch(context->getGpu(),
606 TestResource::kB_SimulatedProperty);
607 TestResource* b = TestResource::CreateScratch(context->getGpu(),
608 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800609 a->unref();
610 b->unref();
611
bsalomon1c60dfe2015-01-21 09:32:40 -0800612 GrScratchKey scratchKey;
613 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800614 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800615 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800616 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800617
bsalomon0ea80f42015-02-11 10:49:59 -0800618 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800619 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800620 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800621 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
622 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800623
624 // Find the first resource and remove its scratch key
625 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800626 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800627 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800628 // It's still alive, but not cached by scratch key anymore
629 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800630 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
631 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800632
633 // The cache should immediately delete it when it's unrefed since it isn't accessible.
634 find->unref();
635 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800636 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
637 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800638
639 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800640 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800641 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800642 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800643 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
644 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800645
646 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800647 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800648 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800649 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
650 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800651
652 find->unref();
653 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800654 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
655 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800656}
657
bsalomon1c60dfe2015-01-21 09:32:40 -0800658static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800659 Mock mock(5, 30000);
660 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800661 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800662
663 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800664 TestResource* a = TestResource::CreateScratch(context->getGpu(),
665 TestResource::kB_SimulatedProperty);
666 TestResource* b = TestResource::CreateScratch(context->getGpu(),
667 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800668 a->unref();
669 b->unref();
670
671 GrScratchKey scratchKey;
672 // Ensure that scratch key comparison and assignment is consistent.
673 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800674 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800675 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800676 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800677 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
678 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
679 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
680 scratchKey = scratchKey1;
681 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
682 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
683 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
684 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
685 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
686 scratchKey = scratchKey2;
687 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
688 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
689 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
690 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
691 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
692
693 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800694 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800695 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800696 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800697
698 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800699 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800700 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800701 REPORTER_ASSERT(reporter, find != NULL);
702 find->unref();
703
704 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800705 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800706 REPORTER_ASSERT(reporter, find != NULL);
707 REPORTER_ASSERT(reporter, find == a || find == b);
708
bsalomon0ea80f42015-02-11 10:49:59 -0800709 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800710 REPORTER_ASSERT(reporter, find2 != NULL);
711 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
712 REPORTER_ASSERT(reporter, find2 != find);
713 find2->unref();
714 find->unref();
715}
716
bsalomon8718aaf2015-02-19 07:24:21 -0800717static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800718 Mock mock(5, 30000);
719 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800720 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000721
bsalomon8718aaf2015-02-19 07:24:21 -0800722 GrUniqueKey key;
723 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800724
bsalomon8718aaf2015-02-19 07:24:21 -0800725 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800726 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800727 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800728
bsalomonf99e9612015-02-19 08:24:16 -0800729 // Set key on resource a.
730 a->resourcePriv().setUniqueKey(key);
731 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
732 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800733
bsalomonf99e9612015-02-19 08:24:16 -0800734 // Make sure that redundantly setting a's key works.
735 a->resourcePriv().setUniqueKey(key);
736 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800737 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800738 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
739 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800740 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
741
bsalomonf99e9612015-02-19 08:24:16 -0800742 // Create resource b and set the same key. It should replace a's unique key cache entry.
743 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
744 b->setSize(12);
745 b->resourcePriv().setUniqueKey(key);
746 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
747 b->unref();
748
749 // Still have two resources because a is still reffed.
750 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
751 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
752 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
753
754 a->unref();
755 // Now a should be gone.
756 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
757 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
758 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
759
760 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
761 // Also make b be unreffed when replacement occurs.
762 b->unref();
763 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
764 GrUniqueKey differentKey;
765 make_unique_key<0>(&differentKey, 1);
766 c->setSize(13);
767 c->resourcePriv().setUniqueKey(differentKey);
768 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
769 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
770 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
771 // c replaces b and b should be immediately purged.
772 c->resourcePriv().setUniqueKey(key);
773 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
774 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
775 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
776
777 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800778 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800779 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
780 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
781 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
782
783 // Drop the ref on c, it should be kept alive because it has a unique key.
784 c->unref();
785 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
786 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
787 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
788
789 // Verify that we can find c, then remove its unique key. It should get purged immediately.
790 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
791 c->resourcePriv().removeUniqueKey();
792 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800793 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
794 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800795 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000796}
797
bsalomon8b79d232014-11-10 10:19:06 -0800798static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800799 Mock mock(5, 30000);
800 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800801 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800802
bsalomon8718aaf2015-02-19 07:24:21 -0800803 GrUniqueKey key1, key2, key3;
804 make_unique_key<0>(&key1, 1);
805 make_unique_key<0>(&key2, 2);
806 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800807
bsalomon23e619c2015-02-06 11:54:28 -0800808 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800809 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
810 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800811 TestResource* c = TestResource::CreateScratch(context->getGpu(),
812 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800813 a->resourcePriv().setUniqueKey(key1);
814 b->resourcePriv().setUniqueKey(key2);
815 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800816 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800817 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800818 c->unref();
819
bsalomon8718aaf2015-02-19 07:24:21 -0800820 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
821 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
822 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800823 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800824
bsalomon8718aaf2015-02-19 07:24:21 -0800825 typedef GrUniqueKeyInvalidatedMessage Msg;
826 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800827
828 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
829 Bus::Post(Msg(key1));
830 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800831 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800832 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800833 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
834 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800835 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800836 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800837
838 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800839 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800840 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800841 // we still have a ref on b, c should be recycled as scratch.
842 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800843 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800844
bsalomon23e619c2015-02-06 11:54:28 -0800845 // make b purgeable. It should be immediately deleted since it has no key.
846 b->unref();
847 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
848
849 // Make sure we actually get to c via it's scratch key, before we say goodbye.
850 GrScratchKey scratchKey;
851 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800852 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800853 REPORTER_ASSERT(reporter, scratch == c);
854 SkSafeUnref(scratch);
855
856 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800857 cache->purgeAllUnlocked();
858 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800859 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800860 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
861 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800862 REPORTER_ASSERT(reporter, !scratch);
863 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800864}
865
bsalomon71cb0c22014-11-14 12:10:14 -0800866static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800867 Mock mock(3, 30000);
868 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800869 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800870
bsalomon8718aaf2015-02-19 07:24:21 -0800871 GrUniqueKey key1, key2;
872 make_unique_key<0>(&key1, 1);
873 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000874
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000875
bsalomonc2f35b72015-01-23 07:19:22 -0800876 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
877 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800878 a->resourcePriv().setUniqueKey(key1);
879 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800880
bsalomonc2f35b72015-01-23 07:19:22 -0800881 // Make a cycle
882 a->setUnrefWhenDestroyed(b);
883 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800884
bsalomonc2f35b72015-01-23 07:19:22 -0800885 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800886
bsalomonc2f35b72015-01-23 07:19:22 -0800887 a->unref();
888 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800889
bsalomonc2f35b72015-01-23 07:19:22 -0800890 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800891
bsalomon0ea80f42015-02-11 10:49:59 -0800892 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800893 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800894
bsalomonc2f35b72015-01-23 07:19:22 -0800895 // Break the cycle
896 a->setUnrefWhenDestroyed(NULL);
897 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800898
bsalomon0ea80f42015-02-11 10:49:59 -0800899 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800900 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000901}
902
bsalomon8b79d232014-11-10 10:19:06 -0800903static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800904 GrUniqueKey key1, key2;
905 make_unique_key<0>(&key1, 1);
906 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000907
908 // Test changing resources sizes (both increase & decrease).
909 {
bsalomonc2f35b72015-01-23 07:19:22 -0800910 Mock mock(3, 30000);
911 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800912 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000913
bsalomon5236cf42015-01-14 10:42:08 -0800914 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800915 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000916 a->unref();
917
bsalomon5236cf42015-01-14 10:42:08 -0800918 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800919 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000920 b->unref();
921
bsalomon0ea80f42015-02-11 10:49:59 -0800922 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
923 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800924 {
bsalomon8718aaf2015-02-19 07:24:21 -0800925 SkAutoTUnref<TestResource> find2(
926 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800927 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800928 SkAutoTUnref<TestResource> find1(
929 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800930 find1->setSize(50);
931 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000932
bsalomon0ea80f42015-02-11 10:49:59 -0800933 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
934 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000935 }
936
937 // Test increasing a resources size beyond the cache budget.
938 {
bsalomonc2f35b72015-01-23 07:19:22 -0800939 Mock mock(2, 300);
940 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800941 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000942
bsalomon5236cf42015-01-14 10:42:08 -0800943 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800944 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800945 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000946 a->unref();
947
bsalomon5236cf42015-01-14 10:42:08 -0800948 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800949 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800950 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000951 b->unref();
952
bsalomon0ea80f42015-02-11 10:49:59 -0800953 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
954 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000955
bsalomon8b79d232014-11-10 10:19:06 -0800956 {
bsalomon8718aaf2015-02-19 07:24:21 -0800957 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
958 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800959 find2->setSize(201);
960 }
bsalomon8718aaf2015-02-19 07:24:21 -0800961 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000962
bsalomon0ea80f42015-02-11 10:49:59 -0800963 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
964 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000965 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000966}
967
bsalomonddf30e62015-02-19 11:38:44 -0800968static void test_timestamp_wrap(skiatest::Reporter* reporter) {
969 static const int kCount = 50;
970 static const int kBudgetCnt = kCount / 2;
971 static const int kLockedFreq = 8;
972 static const int kBudgetSize = 0x80000000;
973
974 SkRandom random;
975
976 // Run the test 2*kCount times;
977 for (int i = 0; i < 2 * kCount; ++i ) {
978 Mock mock(kBudgetCnt, kBudgetSize);
979 GrContext* context = mock.context();
980 GrResourceCache* cache = mock.cache();
981
982 // Pick a random number of resources to add before the timestamp will wrap.
983 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
984
985 static const int kNumToPurge = kCount - kBudgetCnt;
986
987 SkTDArray<int> shouldPurgeIdxs;
988 int purgeableCnt = 0;
989 SkTDArray<GrGpuResource*> resourcesToUnref;
990
991 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
992 // unpurgeable resources.
993 for (int j = 0; j < kCount; ++j) {
994 GrUniqueKey key;
995 make_unique_key<0>(&key, j);
996
997 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
998 r->resourcePriv().setUniqueKey(key);
999 if (random.nextU() % kLockedFreq) {
1000 // Make this is purgeable.
1001 r->unref();
1002 ++purgeableCnt;
1003 if (purgeableCnt <= kNumToPurge) {
1004 *shouldPurgeIdxs.append() = j;
1005 }
1006 } else {
1007 *resourcesToUnref.append() = r;
1008 }
1009 }
1010
1011 // Verify that the correct resources were purged.
1012 int currShouldPurgeIdx = 0;
1013 for (int j = 0; j < kCount; ++j) {
1014 GrUniqueKey key;
1015 make_unique_key<0>(&key, j);
1016 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1017 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1018 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1019 ++currShouldPurgeIdx;
1020 REPORTER_ASSERT(reporter, NULL == res);
1021 } else {
1022 REPORTER_ASSERT(reporter, NULL != res);
1023 }
1024 SkSafeUnref(res);
1025 }
1026
1027 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1028 resourcesToUnref[j]->unref();
1029 }
1030 }
1031}
1032
bsalomon3f324322015-04-08 11:01:54 -07001033static void test_flush(skiatest::Reporter* reporter) {
1034 Mock mock(1000000, 1000000);
1035 GrContext* context = mock.context();
1036 GrResourceCache* cache = mock.cache();
1037
1038 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1039 // power of two here to keep things simpler.
1040 static const int kFlushCount = 16;
1041 cache->setLimits(1000000, 1000000, kFlushCount);
1042
1043 {
1044 // Insert a resource and send a flush notification kFlushCount times.
1045 for (int i = 0; i < kFlushCount; ++i) {
1046 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1047 GrUniqueKey k;
1048 make_unique_key<1>(&k, i);
1049 r->resourcePriv().setUniqueKey(k);
1050 r->unref();
1051 cache->notifyFlushOccurred();
1052 }
1053
1054 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1055 for (int i = 0; i < kFlushCount - 1; ++i) {
1056 // The first resource was purged after the last flush in the initial loop, hence the -1.
1057 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1058 for (int j = 0; j < i; ++j) {
1059 GrUniqueKey k;
1060 make_unique_key<1>(&k, j);
1061 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1062 REPORTER_ASSERT(reporter, !SkToBool(r));
1063 SkSafeUnref(r);
1064 }
1065 cache->notifyFlushOccurred();
1066 }
1067
1068 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1069 cache->purgeAllUnlocked();
1070 }
1071
1072 // Do a similar test but where we leave refs on some resources to prevent them from being
1073 // purged.
1074 {
1075 GrGpuResource* refedResources[kFlushCount >> 1];
1076 for (int i = 0; i < kFlushCount; ++i) {
1077 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1078 GrUniqueKey k;
1079 make_unique_key<1>(&k, i);
1080 r->resourcePriv().setUniqueKey(k);
1081 // Leave a ref on every other resource, beginning with the first.
1082 if (SkToBool(i & 0x1)) {
1083 refedResources[i/2] = r;
1084 } else {
1085 r->unref();
1086 }
1087 cache->notifyFlushOccurred();
1088 }
1089
1090 for (int i = 0; i < kFlushCount; ++i) {
1091 // Should get a resource purged every other flush.
1092 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1093 cache->notifyFlushOccurred();
1094 }
1095
1096 // Unref all the resources that we kept refs on in the first loop.
1097 for (int i = 0; i < kFlushCount >> 1; ++i) {
1098 refedResources[i]->unref();
1099 }
1100
1101 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1102 // get kFlushCount additional flushes. Then everything should be purged.
1103 for (int i = 0; i < kFlushCount; ++i) {
1104 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1105 cache->notifyFlushOccurred();
1106 }
1107 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1108
1109 cache->purgeAllUnlocked();
1110 }
1111
1112 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1113}
1114
bsalomon10e23ca2014-11-25 05:52:06 -08001115static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001116 // Set the cache size to double the resource count because we're going to create 2x that number
1117 // resources, using two different key domains. Add a little slop to the bytes because we resize
1118 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001119 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001120
bsalomonc2f35b72015-01-23 07:19:22 -08001121 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1122 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001123 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001124
1125 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001126 GrUniqueKey key1, key2;
1127 make_unique_key<1>(&key1, i);
1128 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001129
bsalomon24db3b12015-01-23 04:24:04 -08001130 TestResource* resource;
1131
bsalomon10e23ca2014-11-25 05:52:06 -08001132 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001133 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001134 resource->setSize(1);
1135 resource->unref();
1136
bsalomon10e23ca2014-11-25 05:52:06 -08001137 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001138 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001139 resource->setSize(1);
1140 resource->unref();
1141 }
1142
1143 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001144 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1145 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1146 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1147 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001148 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001149 GrUniqueKey key1, key2;
1150 make_unique_key<1>(&key1, i);
1151 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001152
bsalomon8718aaf2015-02-19 07:24:21 -08001153 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1154 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001155 }
1156
bsalomon0ea80f42015-02-11 10:49:59 -08001157 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001158 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001159 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1160 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1161 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1162 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001163
1164 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001165 GrUniqueKey key1, key2;
1166 make_unique_key<1>(&key1, i);
1167 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001168
bsalomon8718aaf2015-02-19 07:24:21 -08001169 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1170 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001171 }
1172}
1173
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001174////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001175DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001176 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1177 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1178 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1179 continue;
1180 }
1181 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001182 if (NULL == context) {
1183 continue;
1184 }
bsalomonf2703d82014-10-28 14:33:06 -07001185 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001186 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001187 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001188 desc.fWidth = gWidth;
1189 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001190 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001191 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1192 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001193 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001194 test_stencil_buffers(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001195 }
bsalomon33435572014-11-05 14:47:41 -08001196
bsalomon8b79d232014-11-10 10:19:06 -08001197 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001198 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001199 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001200 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001201 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001202 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001203 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001204 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001205 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001206 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001207 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001208 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001209 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001210 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001211 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001212}
1213
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001214#endif