blob: 9a4a129c7f31f4cc7367dbcad85010b47768e006 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00008#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009
reed69f6f002014-09-18 06:09:44 -070010#include "SkCanvas.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000011#include "GrContextFactory.h"
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000012#include "GrResourceCache.h"
reed69f6f002014-09-18 06:09:44 -070013#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000015
16static const int gWidth = 640;
17static const int gHeight = 480;
18
19////////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000020static void test_cache(skiatest::Reporter* reporter,
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000021 GrContext* context,
22 SkCanvas* canvas) {
23 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
24
25 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000026 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000027 src.eraseColor(SK_ColorBLACK);
28 size_t srcSize = src.getSize();
29
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000030 size_t initialCacheSize;
31 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000032
33 int oldMaxNum;
34 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000035 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000036
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000037 // Set the cache limits so we can fit 10 "src" images and the
38 // max number of textures doesn't matter
39 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000040 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000041
42 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000043 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000044
45 for (int i = 0; i < 100; ++i) {
46 canvas->drawBitmap(src, 0, 0);
47 canvas->readPixels(size, &readback);
48
49 // "modify" the src texture
50 src.notifyPixelsChanged();
51
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000052 size_t curCacheSize;
53 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000054
55 // we should never go over the size limit
56 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
57 }
58
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000059 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000060}
61
bsalomon6d3fe022014-07-25 08:35:45 -070062class TestResource : public GrGpuResource {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000063 static const size_t kDefaultSize = 100;
64
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000065public:
66 SK_DECLARE_INST_COUNT(TestResource);
bsalomonc44be0e2014-07-25 07:32:33 -070067 TestResource(GrGpu* gpu, size_t size = kDefaultSize)
68 : INHERITED(gpu, false)
69 , fCache(NULL)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000070 , fToDelete(NULL)
71 , fSize(size) {
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000072 ++fAlive;
bsalomon16961262014-08-26 14:01:07 -070073 this->registerWithCache();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000074 }
75
76 ~TestResource() {
77 --fAlive;
bsalomon49f085d2014-09-05 13:34:00 -070078 if (fToDelete) {
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000079 // Breaks our little 2-element cycle below.
80 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
81 fCache->deleteResource(fToDelete->getCacheEntry());
82 }
bsalomonc44be0e2014-07-25 07:32:33 -070083 this->release();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000084 }
85
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000086 void setSize(size_t size) {
87 fSize = size;
88 this->didChangeGpuMemorySize();
89 }
90
91 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000092
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000093 static int alive() { return fAlive; }
94
95 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
96 fCache = cache;
97 fToDelete = resource;
98 }
99
100private:
101 GrResourceCache* fCache;
102 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000103 size_t fSize;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000104 static int fAlive;
105
bsalomon6d3fe022014-07-25 08:35:45 -0700106 typedef GrGpuResource INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000107};
108int TestResource::fAlive = 0;
109
110static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) {
111 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
112 GrCacheID::Key keyData;
113 keyData.fData64[0] = 5;
114 keyData.fData64[1] = 18;
115 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
116 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
117
118 GrResourceCache cache(5, 30000);
119
120 // Add two resources with the same key that delete each other from the cache when destroyed.
bsalomonc44be0e2014-07-25 07:32:33 -0700121 TestResource* a = new TestResource(context->getGpu());
122 TestResource* b = new TestResource(context->getGpu());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000123 cache.addResource(key, a);
124 cache.addResource(key, b);
125 // Circle back.
126 a->setDeleteWhenDestroyed(&cache, b);
127 b->setDeleteWhenDestroyed(&cache, a);
128 a->unref();
129 b->unref();
130
131 // Add a third independent resource also with the same key.
bsalomon6d3fe022014-07-25 08:35:45 -0700132 GrGpuResource* r = new TestResource(context->getGpu());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000133 cache.addResource(key, r);
134 r->unref();
135
136 // Invalidate all three, all three should be purged and destroyed.
137 REPORTER_ASSERT(reporter, 3 == TestResource::alive());
138 const GrResourceInvalidatedMessage msg = { key };
139 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg);
140 cache.purgeAsNeeded();
141 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
142}
143
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000144static void test_cache_delete_on_destruction(skiatest::Reporter* reporter,
145 GrContext* context) {
146 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
147 GrCacheID::Key keyData;
148 keyData.fData64[0] = 5;
149 keyData.fData64[1] = 0;
150 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
151
152 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
153
154 {
155 {
156 GrResourceCache cache(3, 30000);
bsalomonc44be0e2014-07-25 07:32:33 -0700157 TestResource* a = new TestResource(context->getGpu());
158 TestResource* b = new TestResource(context->getGpu());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000159 cache.addResource(key, a);
160 cache.addResource(key, b);
161
162 a->setDeleteWhenDestroyed(&cache, b);
163 b->setDeleteWhenDestroyed(&cache, a);
164
165 a->unref();
166 b->unref();
167 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
168 }
169 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
170 }
171 {
172 GrResourceCache cache(3, 30000);
bsalomonc44be0e2014-07-25 07:32:33 -0700173 TestResource* a = new TestResource(context->getGpu());
174 TestResource* b = new TestResource(context->getGpu());
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000175 cache.addResource(key, a);
176 cache.addResource(key, b);
177
178 a->setDeleteWhenDestroyed(&cache, b);
179 b->setDeleteWhenDestroyed(&cache, a);
180
181 a->unref();
182 b->unref();
183
184 cache.deleteResource(a->getCacheEntry());
185
186 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
187 }
188}
189
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000190static void test_resource_size_changed(skiatest::Reporter* reporter,
191 GrContext* context) {
192 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
193 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
194
195 GrCacheID::Key key1Data;
196 key1Data.fData64[0] = 0;
197 key1Data.fData64[1] = 0;
198 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
199
200 GrCacheID::Key key2Data;
201 key2Data.fData64[0] = 1;
202 key2Data.fData64[1] = 0;
203 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
204
205 // Test changing resources sizes (both increase & decrease).
206 {
207 GrResourceCache cache(2, 300);
208
bsalomonc44be0e2014-07-25 07:32:33 -0700209 TestResource* a = new TestResource(context->getGpu());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000210 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
211 cache.addResource(key1, a);
212 a->unref();
213
bsalomonc44be0e2014-07-25 07:32:33 -0700214 TestResource* b = new TestResource(context->getGpu());
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000215 b->setSize(100);
216 cache.addResource(key2, b);
217 b->unref();
218
219 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
220 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
221
222 static_cast<TestResource*>(cache.find(key2))->setSize(200);
223 static_cast<TestResource*>(cache.find(key1))->setSize(50);
224
225 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
226 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
227 }
228
229 // Test increasing a resources size beyond the cache budget.
230 {
231 GrResourceCache cache(2, 300);
232
bsalomonc44be0e2014-07-25 07:32:33 -0700233 TestResource* a = new TestResource(context->getGpu(), 100);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000234 cache.addResource(key1, a);
235 a->unref();
236
bsalomonc44be0e2014-07-25 07:32:33 -0700237 TestResource* b = new TestResource(context->getGpu(), 100);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000238 cache.addResource(key2, b);
239 b->unref();
240
241 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
242 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
243
244 static_cast<TestResource*>(cache.find(key2))->setSize(201);
245 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
246
247 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
248 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
249 }
250
251 // Test changing the size of an exclusively-held resource.
252 {
253 GrResourceCache cache(2, 300);
254
bsalomonc44be0e2014-07-25 07:32:33 -0700255 TestResource* a = new TestResource(context->getGpu(), 100);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000256 cache.addResource(key1, a);
257 cache.makeExclusive(a->getCacheEntry());
258
bsalomonc44be0e2014-07-25 07:32:33 -0700259 TestResource* b = new TestResource(context->getGpu(), 100);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000260 cache.addResource(key2, b);
261 b->unref();
262
263 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
264 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
265 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
266
267 a->setSize(200);
268
269 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
270 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
271 // Internal resource cache validation will test the detached size (debug mode only).
272
273 cache.makeNonExclusive(a->getCacheEntry());
274 a->unref();
275
276 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
277 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
bsalomon49f085d2014-09-05 13:34:00 -0700278 REPORTER_ASSERT(reporter, cache.find(key1));
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000279 // Internal resource cache validation will test the detached size (debug mode only).
280 }
281}
282
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000283////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000284DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000285 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
286 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
287 if (!GrContextFactory::IsRenderingGLContext(glType)) {
288 continue;
289 }
290 GrContext* context = factory->get(glType);
291 if (NULL == context) {
292 continue;
293 }
294
295 GrTextureDesc desc;
296 desc.fConfig = kSkia8888_GrPixelConfig;
297 desc.fFlags = kRenderTarget_GrTextureFlagBit;
298 desc.fWidth = gWidth;
299 desc.fHeight = gHeight;
reed69f6f002014-09-18 06:09:44 -0700300 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
301 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info, 0));
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000302
reed69f6f002014-09-18 06:09:44 -0700303 test_cache(reporter, context, surface->getCanvas());
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000304 test_purge_invalidated(reporter, context);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000305 test_cache_delete_on_destruction(reporter, context);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000306 test_resource_size_changed(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000307 }
308}
309
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000310#endif