blob: 78c11243de08de8c8e745fdc87761e76e74e4727 [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
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000010#include "GrContextFactory.h"
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000011#include "GrResourceCache.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000012#include "SkGpuDevice.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014
15static const int gWidth = 640;
16static const int gHeight = 480;
17
18////////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000019static void test_cache(skiatest::Reporter* reporter,
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000020 GrContext* context,
21 SkCanvas* canvas) {
22 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
23
24 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000025 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000026 src.eraseColor(SK_ColorBLACK);
27 size_t srcSize = src.getSize();
28
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000029 size_t initialCacheSize;
30 context->getResourceCacheUsage(NULL, &initialCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000031
32 int oldMaxNum;
33 size_t oldMaxBytes;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000034 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000035
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000036 // Set the cache limits so we can fit 10 "src" images and the
37 // max number of textures doesn't matter
38 size_t maxCacheSize = initialCacheSize + 10*srcSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000039 context->setResourceCacheLimits(1000, maxCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000040
41 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000042 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000043
44 for (int i = 0; i < 100; ++i) {
45 canvas->drawBitmap(src, 0, 0);
46 canvas->readPixels(size, &readback);
47
48 // "modify" the src texture
49 src.notifyPixelsChanged();
50
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000051 size_t curCacheSize;
52 context->getResourceCacheUsage(NULL, &curCacheSize);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000053
54 // we should never go over the size limit
55 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
56 }
57
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000058 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000059}
60
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000061class TestResource : public GrCacheable {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000062 static const size_t kDefaultSize = 100;
63
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000064public:
65 SK_DECLARE_INST_COUNT(TestResource);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000066 TestResource(size_t size = kDefaultSize)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000067 : fCache(NULL)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000068 , fToDelete(NULL)
69 , fSize(size) {
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000070 ++fAlive;
71 }
72
73 ~TestResource() {
74 --fAlive;
75 if (NULL != fToDelete) {
76 // Breaks our little 2-element cycle below.
77 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
78 fCache->deleteResource(fToDelete->getCacheEntry());
79 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000080 }
81
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000082 void setSize(size_t size) {
83 fSize = size;
84 this->didChangeGpuMemorySize();
85 }
86
87 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000088
89 bool isValidOnGpu() const SK_OVERRIDE { return true; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000090
91 static int alive() { return fAlive; }
92
93 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
94 fCache = cache;
95 fToDelete = resource;
96 }
97
98private:
99 GrResourceCache* fCache;
100 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000101 size_t fSize;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000102 static int fAlive;
103
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000104 typedef GrCacheable INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000105};
106int TestResource::fAlive = 0;
107
108static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) {
109 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
110 GrCacheID::Key keyData;
111 keyData.fData64[0] = 5;
112 keyData.fData64[1] = 18;
113 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
114 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
115
116 GrResourceCache cache(5, 30000);
117
118 // Add two resources with the same key that delete each other from the cache when destroyed.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000119 TestResource* a = new TestResource();
120 TestResource* b = new TestResource();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000121 cache.addResource(key, a);
122 cache.addResource(key, b);
123 // Circle back.
124 a->setDeleteWhenDestroyed(&cache, b);
125 b->setDeleteWhenDestroyed(&cache, a);
126 a->unref();
127 b->unref();
128
129 // Add a third independent resource also with the same key.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000130 GrCacheable* r = new TestResource();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000131 cache.addResource(key, r);
132 r->unref();
133
134 // Invalidate all three, all three should be purged and destroyed.
135 REPORTER_ASSERT(reporter, 3 == TestResource::alive());
136 const GrResourceInvalidatedMessage msg = { key };
137 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg);
138 cache.purgeAsNeeded();
139 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
140}
141
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000142static void test_cache_delete_on_destruction(skiatest::Reporter* reporter,
143 GrContext* context) {
144 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
145 GrCacheID::Key keyData;
146 keyData.fData64[0] = 5;
147 keyData.fData64[1] = 0;
148 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
149
150 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
151
152 {
153 {
154 GrResourceCache cache(3, 30000);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000155 TestResource* a = new TestResource();
156 TestResource* b = new TestResource();
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000157 cache.addResource(key, a);
158 cache.addResource(key, b);
159
160 a->setDeleteWhenDestroyed(&cache, b);
161 b->setDeleteWhenDestroyed(&cache, a);
162
163 a->unref();
164 b->unref();
165 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
166 }
167 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
168 }
169 {
170 GrResourceCache cache(3, 30000);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000171 TestResource* a = new TestResource();
172 TestResource* b = new TestResource();
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000173 cache.addResource(key, a);
174 cache.addResource(key, b);
175
176 a->setDeleteWhenDestroyed(&cache, b);
177 b->setDeleteWhenDestroyed(&cache, a);
178
179 a->unref();
180 b->unref();
181
182 cache.deleteResource(a->getCacheEntry());
183
184 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
185 }
186}
187
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000188static void test_resource_size_changed(skiatest::Reporter* reporter,
189 GrContext* context) {
190 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
191 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
192
193 GrCacheID::Key key1Data;
194 key1Data.fData64[0] = 0;
195 key1Data.fData64[1] = 0;
196 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
197
198 GrCacheID::Key key2Data;
199 key2Data.fData64[0] = 1;
200 key2Data.fData64[1] = 0;
201 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
202
203 // Test changing resources sizes (both increase & decrease).
204 {
205 GrResourceCache cache(2, 300);
206
207 TestResource* a = new TestResource(0);
208 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
209 cache.addResource(key1, a);
210 a->unref();
211
212 TestResource* b = new TestResource(0);
213 b->setSize(100);
214 cache.addResource(key2, b);
215 b->unref();
216
217 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
218 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
219
220 static_cast<TestResource*>(cache.find(key2))->setSize(200);
221 static_cast<TestResource*>(cache.find(key1))->setSize(50);
222
223 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
224 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
225 }
226
227 // Test increasing a resources size beyond the cache budget.
228 {
229 GrResourceCache cache(2, 300);
230
231 TestResource* a = new TestResource(100);
232 cache.addResource(key1, a);
233 a->unref();
234
235 TestResource* b = new TestResource(100);
236 cache.addResource(key2, b);
237 b->unref();
238
239 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
240 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
241
242 static_cast<TestResource*>(cache.find(key2))->setSize(201);
243 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
244
245 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
246 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
247 }
248
249 // Test changing the size of an exclusively-held resource.
250 {
251 GrResourceCache cache(2, 300);
252
253 TestResource* a = new TestResource(100);
254 cache.addResource(key1, a);
255 cache.makeExclusive(a->getCacheEntry());
256
257 TestResource* b = new TestResource(100);
258 cache.addResource(key2, b);
259 b->unref();
260
261 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
262 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
263 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
264
265 a->setSize(200);
266
267 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
268 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
269 // Internal resource cache validation will test the detached size (debug mode only).
270
271 cache.makeNonExclusive(a->getCacheEntry());
272 a->unref();
273
274 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
275 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
276 REPORTER_ASSERT(reporter, NULL != cache.find(key1));
277 // Internal resource cache validation will test the detached size (debug mode only).
278 }
279}
280
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000281////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000282DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000283 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
284 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
285 if (!GrContextFactory::IsRenderingGLContext(glType)) {
286 continue;
287 }
288 GrContext* context = factory->get(glType);
289 if (NULL == context) {
290 continue;
291 }
292
293 GrTextureDesc desc;
294 desc.fConfig = kSkia8888_GrPixelConfig;
295 desc.fFlags = kRenderTarget_GrTextureFlagBit;
296 desc.fWidth = gWidth;
297 desc.fHeight = gHeight;
298
299 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
300 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
301 SkCanvas canvas(device.get());
302
303 test_cache(reporter, context, &canvas);
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