2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "GrContextFactory.h"
12 #include "GrResourceCache.h"
13 #include "SkSurface.h"
16 static const int gWidth = 640;
17 static const int gHeight = 480;
19 ////////////////////////////////////////////////////////////////////////////////
20 static void test_cache(skiatest::Reporter* reporter,
23 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
26 src.allocN32Pixels(size.width(), size.height());
27 src.eraseColor(SK_ColorBLACK);
28 size_t srcSize = src.getSize();
30 size_t initialCacheSize;
31 context->getResourceCacheUsage(NULL, &initialCacheSize);
35 context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
37 // 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;
40 context->setResourceCacheLimits(1000, maxCacheSize);
43 readback.allocN32Pixels(size.width(), size.height());
45 for (int i = 0; i < 100; ++i) {
46 canvas->drawBitmap(src, 0, 0);
47 canvas->readPixels(size, &readback);
49 // "modify" the src texture
50 src.notifyPixelsChanged();
53 context->getResourceCacheUsage(NULL, &curCacheSize);
55 // we should never go over the size limit
56 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
59 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
62 class TestResource : public GrGpuResource {
63 static const size_t kDefaultSize = 100;
66 SK_DECLARE_INST_COUNT(TestResource);
67 TestResource(GrGpu* gpu, size_t size = kDefaultSize)
68 : INHERITED(gpu, false)
73 this->registerWithCache();
79 // Breaks our little 2-element cycle below.
80 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
81 fCache->deleteResource(fToDelete->getCacheEntry());
86 void setSize(size_t size) {
88 this->didChangeGpuMemorySize();
91 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
93 static int alive() { return fAlive; }
95 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
101 GrResourceCache* fCache;
102 TestResource* fToDelete;
106 typedef GrGpuResource INHERITED;
108 int TestResource::fAlive = 0;
110 static 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);
118 GrResourceCache cache(5, 30000);
120 // Add two resources with the same key that delete each other from the cache when destroyed.
121 TestResource* a = new TestResource(context->getGpu());
122 TestResource* b = new TestResource(context->getGpu());
123 cache.addResource(key, a);
124 cache.addResource(key, b);
126 a->setDeleteWhenDestroyed(&cache, b);
127 b->setDeleteWhenDestroyed(&cache, a);
131 // Add a third independent resource also with the same key.
132 GrGpuResource* r = new TestResource(context->getGpu());
133 cache.addResource(key, r);
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());
144 static 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();
152 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
156 GrResourceCache cache(3, 30000);
157 TestResource* a = new TestResource(context->getGpu());
158 TestResource* b = new TestResource(context->getGpu());
159 cache.addResource(key, a);
160 cache.addResource(key, b);
162 a->setDeleteWhenDestroyed(&cache, b);
163 b->setDeleteWhenDestroyed(&cache, a);
167 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
169 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
172 GrResourceCache cache(3, 30000);
173 TestResource* a = new TestResource(context->getGpu());
174 TestResource* b = new TestResource(context->getGpu());
175 cache.addResource(key, a);
176 cache.addResource(key, b);
178 a->setDeleteWhenDestroyed(&cache, b);
179 b->setDeleteWhenDestroyed(&cache, a);
184 cache.deleteResource(a->getCacheEntry());
186 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
190 static void test_resource_size_changed(skiatest::Reporter* reporter,
191 GrContext* context) {
192 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
193 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
195 GrCacheID::Key key1Data;
196 key1Data.fData64[0] = 0;
197 key1Data.fData64[1] = 0;
198 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
200 GrCacheID::Key key2Data;
201 key2Data.fData64[0] = 1;
202 key2Data.fData64[1] = 0;
203 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
205 // Test changing resources sizes (both increase & decrease).
207 GrResourceCache cache(2, 300);
209 TestResource* a = new TestResource(context->getGpu());
210 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
211 cache.addResource(key1, a);
214 TestResource* b = new TestResource(context->getGpu());
216 cache.addResource(key2, b);
219 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
220 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
222 static_cast<TestResource*>(cache.find(key2))->setSize(200);
223 static_cast<TestResource*>(cache.find(key1))->setSize(50);
225 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
226 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
229 // Test increasing a resources size beyond the cache budget.
231 GrResourceCache cache(2, 300);
233 TestResource* a = new TestResource(context->getGpu(), 100);
234 cache.addResource(key1, a);
237 TestResource* b = new TestResource(context->getGpu(), 100);
238 cache.addResource(key2, b);
241 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
242 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
244 static_cast<TestResource*>(cache.find(key2))->setSize(201);
245 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
247 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
248 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
251 // Test changing the size of an exclusively-held resource.
253 GrResourceCache cache(2, 300);
255 TestResource* a = new TestResource(context->getGpu(), 100);
256 cache.addResource(key1, a);
257 cache.makeExclusive(a->getCacheEntry());
259 TestResource* b = new TestResource(context->getGpu(), 100);
260 cache.addResource(key2, b);
263 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
264 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
265 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
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).
273 cache.makeNonExclusive(a->getCacheEntry());
276 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
277 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
278 REPORTER_ASSERT(reporter, cache.find(key1));
279 // Internal resource cache validation will test the detached size (debug mode only).
283 ////////////////////////////////////////////////////////////////////////////////
284 DEF_GPUTEST(ResourceCache, reporter, factory) {
285 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
286 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
287 if (!GrContextFactory::IsRenderingGLContext(glType)) {
290 GrContext* context = factory->get(glType);
291 if (NULL == context) {
296 desc.fConfig = kSkia8888_GrPixelConfig;
297 desc.fFlags = kRenderTarget_GrTextureFlagBit;
298 desc.fWidth = gWidth;
299 desc.fHeight = gHeight;
300 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
301 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info));
303 test_cache(reporter, context, surface->getCanvas());
304 test_purge_invalidated(reporter, context);
305 test_cache_delete_on_destruction(reporter, context);
306 test_resource_size_changed(reporter, context);