blob: 18ec6e72a35bc7f0fc5037257e525a8372722bcd [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
bsalomon76228632015-05-29 08:02:10 -0700125 if (context->caps()->maxSampleCount() >= 4) {
bsalomon02a44a42015-02-19 09:09:00 -0800126 // 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.).
bsalomon76228632015-05-29 08:02:10 -0700157 if (context->caps()->maxSampleCount() >= 8 &&
158 smallMSAART0 && smallMSAART0->asRenderTarget() &&
vbuzinovdded6962015-06-12 08:59:45 -0700159 smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
bsalomon02a44a42015-02-19 09:09:00 -0800160 smallMSAADesc.fSampleCnt = 8;
bsalomond309e7a2015-04-30 14:18:54 -0700161 smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
162 SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800163 if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700164 smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800165 }
166 REPORTER_ASSERT(reporter,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700167 smallMSAART0 && smallMSAART1 &&
168 smallMSAART0->asRenderTarget() &&
169 smallMSAART1->asRenderTarget() &&
170 smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() !=
171 smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800172 }
173 }
174}
175
bsalomon6d3fe022014-07-25 08:35:45 -0700176class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000177 static const size_t kDefaultSize = 100;
bsalomon1c60dfe2015-01-21 09:32:40 -0800178 enum ScratchConstructor { kScratchConstructor };
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000179public:
180 SK_DECLARE_INST_COUNT(TestResource);
bsalomon1c60dfe2015-01-21 09:32:40 -0800181 /** Property that distinctly categorizes the resource.
182 * For example, textures have width, height, ... */
bsalomon23e619c2015-02-06 11:54:28 -0800183 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
bsalomon1c60dfe2015-01-21 09:32:40 -0800184
bsalomon5236cf42015-01-14 10:42:08 -0800185 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
186 : INHERITED(gpu, lifeCycle)
187 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800188 , fSize(size)
bsalomon23e619c2015-02-06 11:54:28 -0800189 , fProperty(kA_SimulatedProperty) {
bsalomon5236cf42015-01-14 10:42:08 -0800190 ++fNumAlive;
191 this->registerWithCache();
192 }
193
194 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
195 : INHERITED(gpu, lifeCycle)
bsalomondace19e2014-11-17 07:34:06 -0800196 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800197 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800198 , fProperty(kA_SimulatedProperty) {
bsalomondace19e2014-11-17 07:34:06 -0800199 ++fNumAlive;
200 this->registerWithCache();
201 }
202
bsalomon8b79d232014-11-10 10:19:06 -0800203 TestResource(GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -0800204 : INHERITED(gpu, kCached_LifeCycle)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000205 , fToDelete(NULL)
bsalomon1c60dfe2015-01-21 09:32:40 -0800206 , fSize(kDefaultSize)
bsalomon23e619c2015-02-06 11:54:28 -0800207 , fProperty(kA_SimulatedProperty) {
bsalomon8b79d232014-11-10 10:19:06 -0800208 ++fNumAlive;
209 this->registerWithCache();
210 }
211
bsalomon23e619c2015-02-06 11:54:28 -0800212 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
bsalomonc2f35b72015-01-23 07:19:22 -0800213 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000214 }
215
216 ~TestResource() {
bsalomon33435572014-11-05 14:47:41 -0800217 --fNumAlive;
bsalomon71cb0c22014-11-14 12:10:14 -0800218 SkSafeUnref(fToDelete);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000219 }
220
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000221 void setSize(size_t size) {
222 fSize = size;
223 this->didChangeGpuMemorySize();
224 }
225
bsalomon33435572014-11-05 14:47:41 -0800226 static int NumAlive() { return fNumAlive; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000227
bsalomon71cb0c22014-11-14 12:10:14 -0800228 void setUnrefWhenDestroyed(TestResource* resource) {
229 SkRefCnt_SafeAssign(fToDelete, resource);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000230 }
231
bsalomon1c60dfe2015-01-21 09:32:40 -0800232 static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
233 static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
234 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
bsalomon24db3b12015-01-23 04:24:04 -0800235 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
236 builder[i] = static_cast<uint32_t>(i + property);
bsalomon1c60dfe2015-01-21 09:32:40 -0800237 }
238 }
239
240 static size_t ExpectedScratchKeySize() {
241 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
242 }
243
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000244private:
bsalomon24db3b12015-01-23 04:24:04 -0800245 static const int kScratchKeyFieldCnt = 6;
bsalomon1c60dfe2015-01-21 09:32:40 -0800246
bsalomonc2f35b72015-01-23 07:19:22 -0800247 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
248 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
bsalomon1c60dfe2015-01-21 09:32:40 -0800249 , fToDelete(NULL)
250 , fSize(kDefaultSize)
251 , fProperty(property) {
252 GrScratchKey scratchKey;
253 ComputeScratchKey(fProperty, &scratchKey);
254 this->setScratchKey(scratchKey);
255 ++fNumAlive;
256 this->registerWithCache();
257 }
258
mtklein36352bf2015-03-25 18:17:31 -0700259 size_t onGpuMemorySize() const override { return fSize; }
bsalomon69ed47f2014-11-12 11:13:39 -0800260
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000261 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000262 size_t fSize;
bsalomon33435572014-11-05 14:47:41 -0800263 static int fNumAlive;
bsalomon1c60dfe2015-01-21 09:32:40 -0800264 SimulatedProperty fProperty;
bsalomon6d3fe022014-07-25 08:35:45 -0700265 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000266};
bsalomon33435572014-11-05 14:47:41 -0800267int TestResource::fNumAlive = 0;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000268
bsalomonc2f35b72015-01-23 07:19:22 -0800269class Mock {
270public:
271 Mock(int maxCnt, size_t maxBytes) {
272 fContext.reset(GrContext::CreateMockContext());
273 SkASSERT(fContext);
274 fContext->setResourceCacheLimits(maxCnt, maxBytes);
bsalomon0ea80f42015-02-11 10:49:59 -0800275 GrResourceCache* cache = fContext->getResourceCache();
276 cache->purgeAllUnlocked();
277 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800278 }
bsalomonc2f35b72015-01-23 07:19:22 -0800279
bsalomon0ea80f42015-02-11 10:49:59 -0800280 GrResourceCache* cache() { return fContext->getResourceCache(); }
bsalomonc2f35b72015-01-23 07:19:22 -0800281
282 GrContext* context() { return fContext; }
283
284private:
285 SkAutoTUnref<GrContext> fContext;
286};
287
288static void test_no_key(skiatest::Reporter* reporter) {
289 Mock mock(10, 30000);
290 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800291 GrResourceCache* cache = mock.cache();
bsalomon71cb0c22014-11-14 12:10:14 -0800292
293 // Create a bunch of resources with no keys
bsalomon5236cf42015-01-14 10:42:08 -0800294 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
295 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
296 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
297 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon71cb0c22014-11-14 12:10:14 -0800298 a->setSize(11);
299 b->setSize(12);
300 c->setSize(13);
301 d->setSize(14);
302
303 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800304 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800305 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800306 d->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800307
308 // Should be safe to purge without deleting the resources since we still have refs.
bsalomon0ea80f42015-02-11 10:49:59 -0800309 cache->purgeAllUnlocked();
bsalomon71cb0c22014-11-14 12:10:14 -0800310 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
311
bsalomon8718aaf2015-02-19 07:24:21 -0800312 // Since the resources have neither unique nor scratch keys, delete immediately upon unref.
bsalomon71cb0c22014-11-14 12:10:14 -0800313
314 a->unref();
315 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800316 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800317 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800318 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800319
320 c->unref();
321 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800322 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800323 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800324 cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800325
326 d->unref();
327 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800328 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
329 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800330
331 b->unref();
332 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800333 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
334 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800335}
336
bsalomon24db3b12015-01-23 04:24:04 -0800337// Each integer passed as a template param creates a new domain.
bsalomon8718aaf2015-02-19 07:24:21 -0800338template <int> static void make_unique_key(GrUniqueKey* key, int data) {
339 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
340 GrUniqueKey::Builder builder(key, d, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800341 builder[0] = data;
342}
343
bsalomon84c8e622014-11-17 09:33:27 -0800344static void test_budgeting(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800345 Mock mock(10, 300);
346 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800347 GrResourceCache* cache = mock.cache();
bsalomondace19e2014-11-17 07:34:06 -0800348
bsalomon8718aaf2015-02-19 07:24:21 -0800349 GrUniqueKey uniqueKey;
350 make_unique_key<0>(&uniqueKey, 0);
bsalomondace19e2014-11-17 07:34:06 -0800351
bsalomon8718aaf2015-02-19 07:24:21 -0800352 // Create a scratch, a unique, and a wrapped resource
bsalomon1c60dfe2015-01-21 09:32:40 -0800353 TestResource* scratch =
bsalomon23e619c2015-02-06 11:54:28 -0800354 TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomondace19e2014-11-17 07:34:06 -0800355 scratch->setSize(10);
bsalomon8718aaf2015-02-19 07:24:21 -0800356 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
357 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800358 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon5236cf42015-01-14 10:42:08 -0800359 TestResource* wrapped = SkNEW_ARGS(TestResource,
360 (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
361 wrapped->setSize(12);
362 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
363 (context->getGpu(), GrGpuResource::kUncached_LifeCycle));
bsalomon84c8e622014-11-17 09:33:27 -0800364 unbudgeted->setSize(13);
bsalomondace19e2014-11-17 07:34:06 -0800365
bsalomon8718aaf2015-02-19 07:24:21 -0800366 // Make sure we can't add a unique key to the wrapped resource
367 GrUniqueKey uniqueKey2;
368 make_unique_key<0>(&uniqueKey2, 1);
bsalomonf99e9612015-02-19 08:24:16 -0800369 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
bsalomon8718aaf2015-02-19 07:24:21 -0800370 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2));
bsalomondace19e2014-11-17 07:34:06 -0800371
372 // Make sure sizes are as we expect
bsalomon0ea80f42015-02-11 10:49:59 -0800373 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800374 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800375 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800376 cache->getResourceBytes());
377 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800378 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800379 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800380
bsalomon63c992f2015-01-23 12:47:59 -0800381 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800382 cache->purgeAllUnlocked();
383 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800384 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon84c8e622014-11-17 09:33:27 -0800385 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800386 cache->getResourceBytes());
387 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800388 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800389 cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800390
391 // Unreffing the wrapped resource should free it right away.
392 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800393 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon8718aaf2015-02-19 07:24:21 -0800394 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800395 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800396
bsalomon84c8e622014-11-17 09:33:27 -0800397 // Now try freeing the budgeted resources first
bsalomon5236cf42015-01-14 10:42:08 -0800398 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
bsalomondace19e2014-11-17 07:34:06 -0800399 scratch->setSize(12);
bsalomon8718aaf2015-02-19 07:24:21 -0800400 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800401 cache->purgeAllUnlocked();
402 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800403 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
bsalomon0ea80f42015-02-11 10:49:59 -0800404 unbudgeted->gpuMemorySize() == cache->getResourceBytes());
405 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
406 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800407
408 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800409 cache->purgeAllUnlocked();
410 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon84c8e622014-11-17 09:33:27 -0800411 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800412 cache->getResourceBytes());
413 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
414 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800415
416 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800417 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
418 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
419 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
420 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon84c8e622014-11-17 09:33:27 -0800421
422 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800423 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
424 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
425 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
426 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomondace19e2014-11-17 07:34:06 -0800427}
428
bsalomon5236cf42015-01-14 10:42:08 -0800429static void test_unbudgeted(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800430 Mock mock(10, 30000);
431 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800432 GrResourceCache* cache = mock.cache();
bsalomon5236cf42015-01-14 10:42:08 -0800433
bsalomon8718aaf2015-02-19 07:24:21 -0800434 GrUniqueKey uniqueKey;
435 make_unique_key<0>(&uniqueKey, 0);
bsalomon5236cf42015-01-14 10:42:08 -0800436
437 TestResource* scratch;
bsalomon8718aaf2015-02-19 07:24:21 -0800438 TestResource* unique;
bsalomon5236cf42015-01-14 10:42:08 -0800439 TestResource* wrapped;
440 TestResource* unbudgeted;
441
442 // A large uncached or wrapped resource shouldn't evict anything.
bsalomon23e619c2015-02-06 11:54:28 -0800443 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
bsalomon5236cf42015-01-14 10:42:08 -0800444 scratch->setSize(10);
445 scratch->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800446 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
447 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
448 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
449 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800450
bsalomon8718aaf2015-02-19 07:24:21 -0800451 unique = SkNEW_ARGS(TestResource, (context->getGpu()));
452 unique->setSize(11);
bsalomonf99e9612015-02-19 08:24:16 -0800453 unique->resourcePriv().setUniqueKey(uniqueKey);
bsalomon8718aaf2015-02-19 07:24:21 -0800454 unique->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800455 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
456 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
457 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
458 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800459
bsalomon0ea80f42015-02-11 10:49:59 -0800460 size_t large = 2 * cache->getResourceBytes();
bsalomon5236cf42015-01-14 10:42:08 -0800461 unbudgeted = SkNEW_ARGS(TestResource,
462 (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800463 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
464 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
465 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
466 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800467
468 unbudgeted->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800469 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
470 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
472 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800473
474 wrapped = SkNEW_ARGS(TestResource,
475 (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
bsalomon0ea80f42015-02-11 10:49:59 -0800476 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
477 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
478 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
479 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800480
481 wrapped->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800482 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
483 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
484 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
485 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800486
bsalomon0ea80f42015-02-11 10:49:59 -0800487 cache->purgeAllUnlocked();
488 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
489 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
490 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
491 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomon5236cf42015-01-14 10:42:08 -0800492}
493
bsalomon3582d3e2015-02-13 14:20:05 -0800494// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
495void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
496/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800497 Mock mock(10, 300);
498 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800499 GrResourceCache* cache = mock.cache();
bsalomonc2f35b72015-01-23 07:19:22 -0800500
501 TestResource* resource =
bsalomon23e619c2015-02-06 11:54:28 -0800502 TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
bsalomonc2f35b72015-01-23 07:19:22 -0800503 GrScratchKey key;
bsalomon23e619c2015-02-06 11:54:28 -0800504 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
bsalomonc2f35b72015-01-23 07:19:22 -0800505
506 size_t size = resource->gpuMemorySize();
507 for (int i = 0; i < 2; ++i) {
508 // Since this resource is unbudgeted, it should not be reachable as scratch.
bsalomon3582d3e2015-02-13 14:20:05 -0800509 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800510 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800511 REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
bsalomon0ea80f42015-02-11 10:49:59 -0800512 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
513 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
514 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
515 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
516 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800517
518 // Once it is unrefed, it should become available as scratch.
519 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800520 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
521 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
522 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
523 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
524 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
bsalomonc2f35b72015-01-23 07:19:22 -0800525 REPORTER_ASSERT(reporter, resource);
bsalomon3582d3e2015-02-13 14:20:05 -0800526 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
bsalomonc2f35b72015-01-23 07:19:22 -0800527 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800528 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800529
530 if (0 == i) {
531 // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
532 // the above tests again.
bsalomon3582d3e2015-02-13 14:20:05 -0800533 resource->resourcePriv().makeUnbudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800534 } else {
535 // After the second time around, try removing the scratch key
bsalomon3582d3e2015-02-13 14:20:05 -0800536 resource->resourcePriv().removeScratchKey();
bsalomon0ea80f42015-02-11 10:49:59 -0800537 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
538 REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
539 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
540 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
bsalomon3582d3e2015-02-13 14:20:05 -0800541 REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
bsalomonc2f35b72015-01-23 07:19:22 -0800542 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
bsalomon3582d3e2015-02-13 14:20:05 -0800543 REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
bsalomonc2f35b72015-01-23 07:19:22 -0800544
545 // now when it is unrefed it should die since it has no key.
546 resource->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800547 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
548 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
549 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
550 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
bsalomonc2f35b72015-01-23 07:19:22 -0800551 }
bsalomon8b79d232014-11-10 10:19:06 -0800552 }
bsalomonc2f35b72015-01-23 07:19:22 -0800553}
554
555static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
556 Mock mock(5, 30000);
557 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800558 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800559
bsalomon8b79d232014-11-10 10:19:06 -0800560 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800561 TestResource* a = TestResource::CreateScratch(context->getGpu(),
562 TestResource::kB_SimulatedProperty);
563 TestResource* b = TestResource::CreateScratch(context->getGpu(),
564 TestResource::kB_SimulatedProperty);
bsalomon8b79d232014-11-10 10:19:06 -0800565 a->setSize(11);
566 b->setSize(12);
bsalomon1c60dfe2015-01-21 09:32:40 -0800567 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800568 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800569 // Check for negative case consistency. (leaks upon test failure.)
bsalomon0ea80f42015-02-11 10:49:59 -0800570 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
bsalomon1c60dfe2015-01-21 09:32:40 -0800571
572 GrScratchKey scratchKey;
bsalomon23e619c2015-02-06 11:54:28 -0800573 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800574
bsalomon0ea80f42015-02-11 10:49:59 -0800575 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon8b79d232014-11-10 10:19:06 -0800576 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800577 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
578 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon71cb0c22014-11-14 12:10:14 -0800579 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
bsalomon0ea80f42015-02-11 10:49:59 -0800580 cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800581
bsalomon63c992f2015-01-23 12:47:59 -0800582 // Our refs mean that the resources are non purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800583 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800584 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800585 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800586
587 // Unref but don't purge
588 a->unref();
589 b->unref();
590 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800591 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800592
bsalomon63c992f2015-01-23 12:47:59 -0800593 // Purge again. This time resources should be purgeable.
bsalomon0ea80f42015-02-11 10:49:59 -0800594 cache->purgeAllUnlocked();
bsalomon8b79d232014-11-10 10:19:06 -0800595 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800596 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
597 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
bsalomon8b79d232014-11-10 10:19:06 -0800598}
599
bsalomon10e23ca2014-11-25 05:52:06 -0800600static void test_remove_scratch_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800601 Mock mock(5, 30000);
602 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800603 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -0800604
bsalomon10e23ca2014-11-25 05:52:06 -0800605 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800606 TestResource* a = TestResource::CreateScratch(context->getGpu(),
607 TestResource::kB_SimulatedProperty);
608 TestResource* b = TestResource::CreateScratch(context->getGpu(),
609 TestResource::kB_SimulatedProperty);
bsalomon10e23ca2014-11-25 05:52:06 -0800610 a->unref();
611 b->unref();
612
bsalomon1c60dfe2015-01-21 09:32:40 -0800613 GrScratchKey scratchKey;
614 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800615 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800616 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800617 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800618
bsalomon0ea80f42015-02-11 10:49:59 -0800619 // Scratch resources are registered with GrResourceCache just by existing. There are 2.
bsalomon23e619c2015-02-06 11:54:28 -0800620 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon10e23ca2014-11-25 05:52:06 -0800621 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800622 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
623 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800624
625 // Find the first resource and remove its scratch key
626 GrGpuResource* find;
bsalomon0ea80f42015-02-11 10:49:59 -0800627 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800628 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800629 // It's still alive, but not cached by scratch key anymore
630 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800631 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
632 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800633
634 // The cache should immediately delete it when it's unrefed since it isn't accessible.
635 find->unref();
636 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800637 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
638 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800639
640 // Repeat for the second resource.
bsalomon0ea80f42015-02-11 10:49:59 -0800641 find = cache->findAndRefScratchResource(scratchKey);
bsalomon3582d3e2015-02-13 14:20:05 -0800642 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800643 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800644 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
645 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800646
647 // Should be able to call this multiple times with no problem.
bsalomon3582d3e2015-02-13 14:20:05 -0800648 find->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -0800649 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800650 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
651 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800652
653 find->unref();
654 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800655 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
656 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
bsalomon10e23ca2014-11-25 05:52:06 -0800657}
658
bsalomon1c60dfe2015-01-21 09:32:40 -0800659static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800660 Mock mock(5, 30000);
661 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800662 GrResourceCache* cache = mock.cache();
bsalomon1c60dfe2015-01-21 09:32:40 -0800663
664 // Create two resources that have the same scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800665 TestResource* a = TestResource::CreateScratch(context->getGpu(),
666 TestResource::kB_SimulatedProperty);
667 TestResource* b = TestResource::CreateScratch(context->getGpu(),
668 TestResource::kB_SimulatedProperty);
bsalomon1c60dfe2015-01-21 09:32:40 -0800669 a->unref();
670 b->unref();
671
672 GrScratchKey scratchKey;
673 // Ensure that scratch key comparison and assignment is consistent.
674 GrScratchKey scratchKey1;
bsalomon23e619c2015-02-06 11:54:28 -0800675 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
bsalomon1c60dfe2015-01-21 09:32:40 -0800676 GrScratchKey scratchKey2;
bsalomon23e619c2015-02-06 11:54:28 -0800677 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800678 REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
679 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
680 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
681 scratchKey = scratchKey1;
682 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
683 REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
684 REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
685 REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
686 REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
687 scratchKey = scratchKey2;
688 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
689 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
690 REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
691 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
692 REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
693
694 // Ensure that scratch key lookup is correct for negative case.
bsalomon23e619c2015-02-06 11:54:28 -0800695 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800696 // (following leaks upon test failure).
bsalomon0ea80f42015-02-11 10:49:59 -0800697 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
bsalomon1c60dfe2015-01-21 09:32:40 -0800698
699 // Find the first resource with a scratch key and a copy of a scratch key.
bsalomon23e619c2015-02-06 11:54:28 -0800700 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800701 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
bsalomon1c60dfe2015-01-21 09:32:40 -0800702 REPORTER_ASSERT(reporter, find != NULL);
703 find->unref();
704
705 scratchKey2 = scratchKey;
bsalomon0ea80f42015-02-11 10:49:59 -0800706 find = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800707 REPORTER_ASSERT(reporter, find != NULL);
708 REPORTER_ASSERT(reporter, find == a || find == b);
709
bsalomon0ea80f42015-02-11 10:49:59 -0800710 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
bsalomon1c60dfe2015-01-21 09:32:40 -0800711 REPORTER_ASSERT(reporter, find2 != NULL);
712 REPORTER_ASSERT(reporter, find2 == a || find2 == b);
713 REPORTER_ASSERT(reporter, find2 != find);
714 find2->unref();
715 find->unref();
716}
717
bsalomon8718aaf2015-02-19 07:24:21 -0800718static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800719 Mock mock(5, 30000);
720 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800721 GrResourceCache* cache = mock.cache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000722
bsalomon8718aaf2015-02-19 07:24:21 -0800723 GrUniqueKey key;
724 make_unique_key<0>(&key, 0);
bsalomon8b79d232014-11-10 10:19:06 -0800725
bsalomon8718aaf2015-02-19 07:24:21 -0800726 // Create two resources that we will attempt to register with the same unique key.
bsalomon5236cf42015-01-14 10:42:08 -0800727 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800728 a->setSize(11);
bsalomon71cb0c22014-11-14 12:10:14 -0800729
bsalomonf99e9612015-02-19 08:24:16 -0800730 // Set key on resource a.
731 a->resourcePriv().setUniqueKey(key);
732 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
733 a->unref();
bsalomon71cb0c22014-11-14 12:10:14 -0800734
bsalomonf99e9612015-02-19 08:24:16 -0800735 // Make sure that redundantly setting a's key works.
736 a->resourcePriv().setUniqueKey(key);
737 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
bsalomon8b79d232014-11-10 10:19:06 -0800738 a->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800739 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
740 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
bsalomon71cb0c22014-11-14 12:10:14 -0800741 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
742
bsalomonf99e9612015-02-19 08:24:16 -0800743 // Create resource b and set the same key. It should replace a's unique key cache entry.
744 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
745 b->setSize(12);
746 b->resourcePriv().setUniqueKey(key);
747 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
748 b->unref();
749
750 // Still have two resources because a is still reffed.
751 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
752 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
753 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
754
755 a->unref();
756 // Now a should be gone.
757 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
758 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
759 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
760
761 // Now replace b with c, but make sure c can start with one unique key and change it to b's key.
762 // Also make b be unreffed when replacement occurs.
763 b->unref();
764 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
765 GrUniqueKey differentKey;
766 make_unique_key<0>(&differentKey, 1);
767 c->setSize(13);
768 c->resourcePriv().setUniqueKey(differentKey);
769 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
770 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
771 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
772 // c replaces b and b should be immediately purged.
773 c->resourcePriv().setUniqueKey(key);
774 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
775 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
776 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
777
778 // c shouldn't be purged because it is ref'ed.
bsalomon0ea80f42015-02-11 10:49:59 -0800779 cache->purgeAllUnlocked();
bsalomonf99e9612015-02-19 08:24:16 -0800780 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
781 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
782 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
783
784 // Drop the ref on c, it should be kept alive because it has a unique key.
785 c->unref();
786 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
787 REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
788 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
789
790 // Verify that we can find c, then remove its unique key. It should get purged immediately.
791 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
792 c->resourcePriv().removeUniqueKey();
793 c->unref();
bsalomon0ea80f42015-02-11 10:49:59 -0800794 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
795 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon33435572014-11-05 14:47:41 -0800796 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000797}
798
bsalomon8b79d232014-11-10 10:19:06 -0800799static void test_purge_invalidated(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800800 Mock mock(5, 30000);
801 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800802 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800803
bsalomon8718aaf2015-02-19 07:24:21 -0800804 GrUniqueKey key1, key2, key3;
805 make_unique_key<0>(&key1, 1);
806 make_unique_key<0>(&key2, 2);
807 make_unique_key<0>(&key3, 3);
bsalomon8b79d232014-11-10 10:19:06 -0800808
bsalomon23e619c2015-02-06 11:54:28 -0800809 // Add three resources to the cache. Only c is usable as scratch.
bsalomon5236cf42015-01-14 10:42:08 -0800810 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
811 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon23e619c2015-02-06 11:54:28 -0800812 TestResource* c = TestResource::CreateScratch(context->getGpu(),
813 TestResource::kA_SimulatedProperty);
bsalomon8718aaf2015-02-19 07:24:21 -0800814 a->resourcePriv().setUniqueKey(key1);
815 b->resourcePriv().setUniqueKey(key2);
816 c->resourcePriv().setUniqueKey(key3);
bsalomon8b79d232014-11-10 10:19:06 -0800817 a->unref();
bsalomon23e619c2015-02-06 11:54:28 -0800818 // hold b until *after* the message is sent.
bsalomon8b79d232014-11-10 10:19:06 -0800819 c->unref();
820
bsalomon8718aaf2015-02-19 07:24:21 -0800821 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
822 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
823 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800824 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
bsalomon23e619c2015-02-06 11:54:28 -0800825
bsalomon8718aaf2015-02-19 07:24:21 -0800826 typedef GrUniqueKeyInvalidatedMessage Msg;
827 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
bsalomon23e619c2015-02-06 11:54:28 -0800828
829 // Invalidate two of the three, they should be purged and no longer accessible via their keys.
830 Bus::Post(Msg(key1));
831 Bus::Post(Msg(key2));
bsalomon0ea80f42015-02-11 10:49:59 -0800832 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800833 // a should be deleted now, but we still have a ref on b.
bsalomon8718aaf2015-02-19 07:24:21 -0800834 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
835 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon23e619c2015-02-06 11:54:28 -0800836 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800837 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
bsalomon8b79d232014-11-10 10:19:06 -0800838
839 // Invalidate the third.
bsalomon23e619c2015-02-06 11:54:28 -0800840 Bus::Post(Msg(key3));
bsalomon0ea80f42015-02-11 10:49:59 -0800841 cache->purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800842 // we still have a ref on b, c should be recycled as scratch.
843 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8718aaf2015-02-19 07:24:21 -0800844 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
bsalomon71cb0c22014-11-14 12:10:14 -0800845
bsalomon23e619c2015-02-06 11:54:28 -0800846 // make b purgeable. It should be immediately deleted since it has no key.
847 b->unref();
848 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
849
850 // Make sure we actually get to c via it's scratch key, before we say goodbye.
851 GrScratchKey scratchKey;
852 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
bsalomon0ea80f42015-02-11 10:49:59 -0800853 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon23e619c2015-02-06 11:54:28 -0800854 REPORTER_ASSERT(reporter, scratch == c);
855 SkSafeUnref(scratch);
856
857 // Get rid of c.
bsalomon0ea80f42015-02-11 10:49:59 -0800858 cache->purgeAllUnlocked();
859 scratch = cache->findAndRefScratchResource(scratchKey);
bsalomon71cb0c22014-11-14 12:10:14 -0800860 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
bsalomon0ea80f42015-02-11 10:49:59 -0800861 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
862 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
bsalomon23e619c2015-02-06 11:54:28 -0800863 REPORTER_ASSERT(reporter, !scratch);
864 SkSafeUnref(scratch);
bsalomon8b79d232014-11-10 10:19:06 -0800865}
866
bsalomon71cb0c22014-11-14 12:10:14 -0800867static void test_cache_chained_purge(skiatest::Reporter* reporter) {
bsalomonc2f35b72015-01-23 07:19:22 -0800868 Mock mock(3, 30000);
869 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800870 GrResourceCache* cache = mock.cache();
bsalomon8b79d232014-11-10 10:19:06 -0800871
bsalomon8718aaf2015-02-19 07:24:21 -0800872 GrUniqueKey key1, key2;
873 make_unique_key<0>(&key1, 1);
874 make_unique_key<0>(&key2, 2);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000875
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000876
bsalomonc2f35b72015-01-23 07:19:22 -0800877 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
878 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800879 a->resourcePriv().setUniqueKey(key1);
880 b->resourcePriv().setUniqueKey(key2);
bsalomon820dd6c2014-11-05 12:09:45 -0800881
bsalomonc2f35b72015-01-23 07:19:22 -0800882 // Make a cycle
883 a->setUnrefWhenDestroyed(b);
884 b->setUnrefWhenDestroyed(a);
bsalomon71cb0c22014-11-14 12:10:14 -0800885
bsalomonc2f35b72015-01-23 07:19:22 -0800886 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800887
bsalomonc2f35b72015-01-23 07:19:22 -0800888 a->unref();
889 b->unref();
bsalomon8b79d232014-11-10 10:19:06 -0800890
bsalomonc2f35b72015-01-23 07:19:22 -0800891 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800892
bsalomon0ea80f42015-02-11 10:49:59 -0800893 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800894 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon8b79d232014-11-10 10:19:06 -0800895
bsalomonc2f35b72015-01-23 07:19:22 -0800896 // Break the cycle
897 a->setUnrefWhenDestroyed(NULL);
898 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
bsalomon33435572014-11-05 14:47:41 -0800899
bsalomon0ea80f42015-02-11 10:49:59 -0800900 cache->purgeAllUnlocked();
bsalomonc2f35b72015-01-23 07:19:22 -0800901 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000902}
903
bsalomon8b79d232014-11-10 10:19:06 -0800904static void test_resource_size_changed(skiatest::Reporter* reporter) {
bsalomon8718aaf2015-02-19 07:24:21 -0800905 GrUniqueKey key1, key2;
906 make_unique_key<0>(&key1, 1);
907 make_unique_key<0>(&key2, 2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000908
909 // Test changing resources sizes (both increase & decrease).
910 {
bsalomonc2f35b72015-01-23 07:19:22 -0800911 Mock mock(3, 30000);
912 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800913 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000914
bsalomon5236cf42015-01-14 10:42:08 -0800915 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800916 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000917 a->unref();
918
bsalomon5236cf42015-01-14 10:42:08 -0800919 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -0800920 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000921 b->unref();
922
bsalomon0ea80f42015-02-11 10:49:59 -0800923 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
924 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
bsalomon8b79d232014-11-10 10:19:06 -0800925 {
bsalomon8718aaf2015-02-19 07:24:21 -0800926 SkAutoTUnref<TestResource> find2(
927 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800928 find2->setSize(200);
bsalomon8718aaf2015-02-19 07:24:21 -0800929 SkAutoTUnref<TestResource> find1(
930 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
bsalomon8b79d232014-11-10 10:19:06 -0800931 find1->setSize(50);
932 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000933
bsalomon0ea80f42015-02-11 10:49:59 -0800934 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
935 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000936 }
937
938 // Test increasing a resources size beyond the cache budget.
939 {
bsalomonc2f35b72015-01-23 07:19:22 -0800940 Mock mock(2, 300);
941 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -0800942 GrResourceCache* cache = mock.cache();
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000943
bsalomon5236cf42015-01-14 10:42:08 -0800944 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800945 a->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800946 a->resourcePriv().setUniqueKey(key1);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000947 a->unref();
948
bsalomon5236cf42015-01-14 10:42:08 -0800949 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8b79d232014-11-10 10:19:06 -0800950 b->setSize(100);
bsalomon8718aaf2015-02-19 07:24:21 -0800951 b->resourcePriv().setUniqueKey(key2);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000952 b->unref();
953
bsalomon0ea80f42015-02-11 10:49:59 -0800954 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
955 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000956
bsalomon8b79d232014-11-10 10:19:06 -0800957 {
bsalomon8718aaf2015-02-19 07:24:21 -0800958 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
959 cache->findAndRefUniqueResource(key2)));
bsalomon8b79d232014-11-10 10:19:06 -0800960 find2->setSize(201);
961 }
bsalomon8718aaf2015-02-19 07:24:21 -0800962 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000963
bsalomon0ea80f42015-02-11 10:49:59 -0800964 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
965 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000966 }
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000967}
968
bsalomonddf30e62015-02-19 11:38:44 -0800969static void test_timestamp_wrap(skiatest::Reporter* reporter) {
970 static const int kCount = 50;
971 static const int kBudgetCnt = kCount / 2;
972 static const int kLockedFreq = 8;
973 static const int kBudgetSize = 0x80000000;
974
975 SkRandom random;
976
977 // Run the test 2*kCount times;
978 for (int i = 0; i < 2 * kCount; ++i ) {
979 Mock mock(kBudgetCnt, kBudgetSize);
980 GrContext* context = mock.context();
981 GrResourceCache* cache = mock.cache();
982
983 // Pick a random number of resources to add before the timestamp will wrap.
984 cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
985
986 static const int kNumToPurge = kCount - kBudgetCnt;
987
988 SkTDArray<int> shouldPurgeIdxs;
989 int purgeableCnt = 0;
990 SkTDArray<GrGpuResource*> resourcesToUnref;
991
992 // Add kCount resources, holding onto resources at random so we have a mix of purgeable and
993 // unpurgeable resources.
994 for (int j = 0; j < kCount; ++j) {
995 GrUniqueKey key;
996 make_unique_key<0>(&key, j);
997
998 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
999 r->resourcePriv().setUniqueKey(key);
1000 if (random.nextU() % kLockedFreq) {
1001 // Make this is purgeable.
1002 r->unref();
1003 ++purgeableCnt;
1004 if (purgeableCnt <= kNumToPurge) {
1005 *shouldPurgeIdxs.append() = j;
1006 }
1007 } else {
1008 *resourcesToUnref.append() = r;
1009 }
1010 }
1011
1012 // Verify that the correct resources were purged.
1013 int currShouldPurgeIdx = 0;
1014 for (int j = 0; j < kCount; ++j) {
1015 GrUniqueKey key;
1016 make_unique_key<0>(&key, j);
1017 GrGpuResource* res = cache->findAndRefUniqueResource(key);
1018 if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
1019 shouldPurgeIdxs[currShouldPurgeIdx] == j) {
1020 ++currShouldPurgeIdx;
1021 REPORTER_ASSERT(reporter, NULL == res);
1022 } else {
1023 REPORTER_ASSERT(reporter, NULL != res);
1024 }
1025 SkSafeUnref(res);
1026 }
1027
1028 for (int j = 0; j < resourcesToUnref.count(); ++j) {
1029 resourcesToUnref[j]->unref();
1030 }
1031 }
1032}
1033
bsalomon3f324322015-04-08 11:01:54 -07001034static void test_flush(skiatest::Reporter* reporter) {
1035 Mock mock(1000000, 1000000);
1036 GrContext* context = mock.context();
1037 GrResourceCache* cache = mock.cache();
1038
1039 // The current cache impl will round the max flush count to the next power of 2. So we choose a
1040 // power of two here to keep things simpler.
1041 static const int kFlushCount = 16;
1042 cache->setLimits(1000000, 1000000, kFlushCount);
1043
1044 {
1045 // Insert a resource and send a flush notification kFlushCount times.
1046 for (int i = 0; i < kFlushCount; ++i) {
1047 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1048 GrUniqueKey k;
1049 make_unique_key<1>(&k, i);
1050 r->resourcePriv().setUniqueKey(k);
1051 r->unref();
1052 cache->notifyFlushOccurred();
1053 }
1054
1055 // Send flush notifications to the cache. Each flush should purge the oldest resource.
1056 for (int i = 0; i < kFlushCount - 1; ++i) {
1057 // The first resource was purged after the last flush in the initial loop, hence the -1.
1058 REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
1059 for (int j = 0; j < i; ++j) {
1060 GrUniqueKey k;
1061 make_unique_key<1>(&k, j);
1062 GrGpuResource* r = cache->findAndRefUniqueResource(k);
1063 REPORTER_ASSERT(reporter, !SkToBool(r));
1064 SkSafeUnref(r);
1065 }
1066 cache->notifyFlushOccurred();
1067 }
1068
1069 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1070 cache->purgeAllUnlocked();
1071 }
1072
1073 // Do a similar test but where we leave refs on some resources to prevent them from being
1074 // purged.
1075 {
1076 GrGpuResource* refedResources[kFlushCount >> 1];
1077 for (int i = 0; i < kFlushCount; ++i) {
1078 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu()));
1079 GrUniqueKey k;
1080 make_unique_key<1>(&k, i);
1081 r->resourcePriv().setUniqueKey(k);
1082 // Leave a ref on every other resource, beginning with the first.
1083 if (SkToBool(i & 0x1)) {
1084 refedResources[i/2] = r;
1085 } else {
1086 r->unref();
1087 }
1088 cache->notifyFlushOccurred();
1089 }
1090
1091 for (int i = 0; i < kFlushCount; ++i) {
1092 // Should get a resource purged every other flush.
1093 REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
1094 cache->notifyFlushOccurred();
1095 }
1096
1097 // Unref all the resources that we kept refs on in the first loop.
1098 for (int i = 0; i < kFlushCount >> 1; ++i) {
1099 refedResources[i]->unref();
1100 }
1101
1102 // When we unref'ed them their timestamps got updated. So nothing should be purged until we
1103 // get kFlushCount additional flushes. Then everything should be purged.
1104 for (int i = 0; i < kFlushCount; ++i) {
1105 REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
1106 cache->notifyFlushOccurred();
1107 }
1108 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1109
1110 cache->purgeAllUnlocked();
1111 }
1112
1113 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
1114}
1115
bsalomon10e23ca2014-11-25 05:52:06 -08001116static void test_large_resource_count(skiatest::Reporter* reporter) {
bsalomon10e23ca2014-11-25 05:52:06 -08001117 // Set the cache size to double the resource count because we're going to create 2x that number
1118 // resources, using two different key domains. Add a little slop to the bytes because we resize
1119 // down to 1 byte after creating the resource.
bsalomonc2f35b72015-01-23 07:19:22 -08001120 static const int kResourceCnt = 2000;
bsalomon10e23ca2014-11-25 05:52:06 -08001121
bsalomonc2f35b72015-01-23 07:19:22 -08001122 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
1123 GrContext* context = mock.context();
bsalomon0ea80f42015-02-11 10:49:59 -08001124 GrResourceCache* cache = mock.cache();
bsalomon10e23ca2014-11-25 05:52:06 -08001125
1126 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001127 GrUniqueKey key1, key2;
1128 make_unique_key<1>(&key1, i);
1129 make_unique_key<2>(&key2, i);
bsalomon10e23ca2014-11-25 05:52:06 -08001130
bsalomon24db3b12015-01-23 04:24:04 -08001131 TestResource* resource;
1132
bsalomon10e23ca2014-11-25 05:52:06 -08001133 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001134 resource->resourcePriv().setUniqueKey(key1);
bsalomon10e23ca2014-11-25 05:52:06 -08001135 resource->setSize(1);
1136 resource->unref();
1137
bsalomon10e23ca2014-11-25 05:52:06 -08001138 resource = SkNEW_ARGS(TestResource, (context->getGpu()));
bsalomon8718aaf2015-02-19 07:24:21 -08001139 resource->resourcePriv().setUniqueKey(key2);
bsalomon10e23ca2014-11-25 05:52:06 -08001140 resource->setSize(1);
1141 resource->unref();
1142 }
1143
1144 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
bsalomon0ea80f42015-02-11 10:49:59 -08001145 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
1146 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
1147 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
1148 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
bsalomon10e23ca2014-11-25 05:52:06 -08001149 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001150 GrUniqueKey key1, key2;
1151 make_unique_key<1>(&key1, i);
1152 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001153
bsalomon8718aaf2015-02-19 07:24:21 -08001154 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
1155 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001156 }
1157
bsalomon0ea80f42015-02-11 10:49:59 -08001158 cache->purgeAllUnlocked();
bsalomon10e23ca2014-11-25 05:52:06 -08001159 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
bsalomon0ea80f42015-02-11 10:49:59 -08001160 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
1161 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
1162 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
1163 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
bsalomon10e23ca2014-11-25 05:52:06 -08001164
1165 for (int i = 0; i < kResourceCnt; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -08001166 GrUniqueKey key1, key2;
1167 make_unique_key<1>(&key1, i);
1168 make_unique_key<2>(&key2, i);
bsalomon24db3b12015-01-23 04:24:04 -08001169
bsalomon8718aaf2015-02-19 07:24:21 -08001170 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
1171 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
bsalomon10e23ca2014-11-25 05:52:06 -08001172 }
1173}
1174
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +00001175////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00001176DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001177 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
1178 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
1179 if (!GrContextFactory::IsRenderingGLContext(glType)) {
1180 continue;
1181 }
1182 GrContext* context = factory->get(glType);
bsalomonfdcf2c02014-11-05 12:30:32 -08001183 if (NULL == context) {
1184 continue;
1185 }
bsalomonf2703d82014-10-28 14:33:06 -07001186 GrSurfaceDesc desc;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001187 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -07001188 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001189 desc.fWidth = gWidth;
1190 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -07001191 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
bsalomonafe30052015-01-16 07:32:33 -08001192 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1193 SkSurface::kNo_Budgeted, info));
reed69f6f002014-09-18 06:09:44 -07001194 test_cache(reporter, context, surface->getCanvas());
bsalomon02a44a42015-02-19 09:09:00 -08001195 test_stencil_buffers(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001196 }
bsalomon33435572014-11-05 14:47:41 -08001197
bsalomon8b79d232014-11-10 10:19:06 -08001198 // The below tests create their own mock contexts.
bsalomon71cb0c22014-11-14 12:10:14 -08001199 test_no_key(reporter);
bsalomon84c8e622014-11-17 09:33:27 -08001200 test_budgeting(reporter);
bsalomon5236cf42015-01-14 10:42:08 -08001201 test_unbudgeted(reporter);
bsalomonc2f35b72015-01-23 07:19:22 -08001202 test_unbudgeted_to_scratch(reporter);
bsalomon8718aaf2015-02-19 07:24:21 -08001203 test_duplicate_unique_key(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001204 test_duplicate_scratch_key(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001205 test_remove_scratch_key(reporter);
bsalomon1c60dfe2015-01-21 09:32:40 -08001206 test_scratch_key_consistency(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001207 test_purge_invalidated(reporter);
bsalomon71cb0c22014-11-14 12:10:14 -08001208 test_cache_chained_purge(reporter);
bsalomon8b79d232014-11-10 10:19:06 -08001209 test_resource_size_changed(reporter);
bsalomonddf30e62015-02-19 11:38:44 -08001210 test_timestamp_wrap(reporter);
bsalomon3f324322015-04-08 11:01:54 -07001211 test_flush(reporter);
bsalomon10e23ca2014-11-25 05:52:06 -08001212 test_large_resource_count(reporter);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001213}
1214
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001215#endif