blob: f6c8b1da1a1c9033d2419edf6d3609f0007595b7 [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
29 size_t initialCacheSize = context->getGpuTextureCacheBytes();
30
31 int oldMaxNum;
32 size_t oldMaxBytes;
33 context->getTextureCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000034
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000035 // Set the cache limits so we can fit 10 "src" images and the
36 // max number of textures doesn't matter
37 size_t maxCacheSize = initialCacheSize + 10*srcSize;
38 context->setTextureCacheLimits(1000, maxCacheSize);
39
40 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000041 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000042
43 for (int i = 0; i < 100; ++i) {
44 canvas->drawBitmap(src, 0, 0);
45 canvas->readPixels(size, &readback);
46
47 // "modify" the src texture
48 src.notifyPixelsChanged();
49
50 size_t curCacheSize = context->getGpuTextureCacheBytes();
51
52 // we should never go over the size limit
53 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
54 }
55
56 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes);
57}
58
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000059class TestResource : public GrCacheable {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000060 static const size_t kDefaultSize = 100;
61
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000062public:
63 SK_DECLARE_INST_COUNT(TestResource);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000064 TestResource(size_t size = kDefaultSize)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000065 : fCache(NULL)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000066 , fToDelete(NULL)
67 , fSize(size) {
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000068 ++fAlive;
69 }
70
71 ~TestResource() {
72 --fAlive;
73 if (NULL != fToDelete) {
74 // Breaks our little 2-element cycle below.
75 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
76 fCache->deleteResource(fToDelete->getCacheEntry());
77 }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000078 }
79
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000080 void setSize(size_t size) {
81 fSize = size;
82 this->didChangeGpuMemorySize();
83 }
84
85 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000086
87 bool isValidOnGpu() const SK_OVERRIDE { return true; }
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000088
89 static int alive() { return fAlive; }
90
91 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
92 fCache = cache;
93 fToDelete = resource;
94 }
95
96private:
97 GrResourceCache* fCache;
98 TestResource* fToDelete;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000099 size_t fSize;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000100 static int fAlive;
101
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000102 typedef GrCacheable INHERITED;
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000103};
104int TestResource::fAlive = 0;
105
106static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) {
107 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
108 GrCacheID::Key keyData;
109 keyData.fData64[0] = 5;
110 keyData.fData64[1] = 18;
111 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
112 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
113
114 GrResourceCache cache(5, 30000);
115
116 // 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 +0000117 TestResource* a = new TestResource();
118 TestResource* b = new TestResource();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000119 cache.addResource(key, a);
120 cache.addResource(key, b);
121 // Circle back.
122 a->setDeleteWhenDestroyed(&cache, b);
123 b->setDeleteWhenDestroyed(&cache, a);
124 a->unref();
125 b->unref();
126
127 // Add a third independent resource also with the same key.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000128 GrCacheable* r = new TestResource();
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000129 cache.addResource(key, r);
130 r->unref();
131
132 // Invalidate all three, all three should be purged and destroyed.
133 REPORTER_ASSERT(reporter, 3 == TestResource::alive());
134 const GrResourceInvalidatedMessage msg = { key };
135 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg);
136 cache.purgeAsNeeded();
137 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
138}
139
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000140static void test_cache_delete_on_destruction(skiatest::Reporter* reporter,
141 GrContext* context) {
142 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
143 GrCacheID::Key keyData;
144 keyData.fData64[0] = 5;
145 keyData.fData64[1] = 0;
146 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
147
148 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
149
150 {
151 {
152 GrResourceCache cache(3, 30000);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000153 TestResource* a = new TestResource();
154 TestResource* b = new TestResource();
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000155 cache.addResource(key, a);
156 cache.addResource(key, b);
157
158 a->setDeleteWhenDestroyed(&cache, b);
159 b->setDeleteWhenDestroyed(&cache, a);
160
161 a->unref();
162 b->unref();
163 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
164 }
165 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
166 }
167 {
168 GrResourceCache cache(3, 30000);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000169 TestResource* a = new TestResource();
170 TestResource* b = new TestResource();
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000171 cache.addResource(key, a);
172 cache.addResource(key, b);
173
174 a->setDeleteWhenDestroyed(&cache, b);
175 b->setDeleteWhenDestroyed(&cache, a);
176
177 a->unref();
178 b->unref();
179
180 cache.deleteResource(a->getCacheEntry());
181
182 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
183 }
184}
185
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000186static void test_resource_size_changed(skiatest::Reporter* reporter,
187 GrContext* context) {
188 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
189 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
190
191 GrCacheID::Key key1Data;
192 key1Data.fData64[0] = 0;
193 key1Data.fData64[1] = 0;
194 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
195
196 GrCacheID::Key key2Data;
197 key2Data.fData64[0] = 1;
198 key2Data.fData64[1] = 0;
199 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
200
201 // Test changing resources sizes (both increase & decrease).
202 {
203 GrResourceCache cache(2, 300);
204
205 TestResource* a = new TestResource(0);
206 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
207 cache.addResource(key1, a);
208 a->unref();
209
210 TestResource* b = new TestResource(0);
211 b->setSize(100);
212 cache.addResource(key2, b);
213 b->unref();
214
215 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
216 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
217
218 static_cast<TestResource*>(cache.find(key2))->setSize(200);
219 static_cast<TestResource*>(cache.find(key1))->setSize(50);
220
221 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
222 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
223 }
224
225 // Test increasing a resources size beyond the cache budget.
226 {
227 GrResourceCache cache(2, 300);
228
229 TestResource* a = new TestResource(100);
230 cache.addResource(key1, a);
231 a->unref();
232
233 TestResource* b = new TestResource(100);
234 cache.addResource(key2, b);
235 b->unref();
236
237 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
238 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
239
240 static_cast<TestResource*>(cache.find(key2))->setSize(201);
241 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
242
243 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
244 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
245 }
246
247 // Test changing the size of an exclusively-held resource.
248 {
249 GrResourceCache cache(2, 300);
250
251 TestResource* a = new TestResource(100);
252 cache.addResource(key1, a);
253 cache.makeExclusive(a->getCacheEntry());
254
255 TestResource* b = new TestResource(100);
256 cache.addResource(key2, b);
257 b->unref();
258
259 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
260 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
261 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
262
263 a->setSize(200);
264
265 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
266 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
267 // Internal resource cache validation will test the detached size (debug mode only).
268
269 cache.makeNonExclusive(a->getCacheEntry());
270 a->unref();
271
272 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
273 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
274 REPORTER_ASSERT(reporter, NULL != cache.find(key1));
275 // Internal resource cache validation will test the detached size (debug mode only).
276 }
277}
278
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000279////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000280DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000281 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
282 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
283 if (!GrContextFactory::IsRenderingGLContext(glType)) {
284 continue;
285 }
286 GrContext* context = factory->get(glType);
287 if (NULL == context) {
288 continue;
289 }
290
291 GrTextureDesc desc;
292 desc.fConfig = kSkia8888_GrPixelConfig;
293 desc.fFlags = kRenderTarget_GrTextureFlagBit;
294 desc.fWidth = gWidth;
295 desc.fHeight = gHeight;
296
297 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
298 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
299 SkCanvas canvas(device.get());
300
301 test_cache(reporter, context, &canvas);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000302 test_purge_invalidated(reporter, context);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000303 test_cache_delete_on_destruction(reporter, context);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000304 test_resource_size_changed(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000305 }
306}
307
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000308#endif