OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #if SK_SUPPORT_GPU | 8 #if SK_SUPPORT_GPU |
9 | 9 |
10 #include "GrContextFactory.h" | 10 #include "GrContextFactory.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 size_t curCacheSize = context->getGpuTextureCacheBytes(); | 50 size_t curCacheSize = context->getGpuTextureCacheBytes(); |
51 | 51 |
52 // we should never go over the size limit | 52 // we should never go over the size limit |
53 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); | 53 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
54 } | 54 } |
55 | 55 |
56 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); | 56 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); |
57 } | 57 } |
58 | 58 |
59 class TestResource : public GrCacheable { | 59 class TestResource : public GrCacheable { |
60 static const size_t kDefaultSize = 100; | |
61 | |
60 public: | 62 public: |
61 SK_DECLARE_INST_COUNT(TestResource); | 63 SK_DECLARE_INST_COUNT(TestResource); |
62 TestResource() | 64 TestResource(size_t size = kDefaultSize) |
63 : fCache(NULL) | 65 : fCache(NULL) |
64 , fToDelete(NULL) { | 66 , fToDelete(NULL) |
67 , fSize(size) { | |
65 ++fAlive; | 68 ++fAlive; |
66 } | 69 } |
67 | 70 |
68 ~TestResource() { | 71 ~TestResource() { |
69 --fAlive; | 72 --fAlive; |
70 if (NULL != fToDelete) { | 73 if (NULL != fToDelete) { |
71 // Breaks our little 2-element cycle below. | 74 // Breaks our little 2-element cycle below. |
72 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | 75 fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
73 fCache->deleteResource(fToDelete->getCacheEntry()); | 76 fCache->deleteResource(fToDelete->getCacheEntry()); |
74 } | 77 } |
75 } | 78 } |
76 | 79 |
77 size_t gpuMemorySize() const SK_OVERRIDE { return 100; } | 80 void setSize(size_t size) { |
81 fSize = size; | |
82 this->didChangeGpuMemorySize(); | |
83 } | |
84 | |
85 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; } | |
78 | 86 |
79 bool isValidOnGpu() const SK_OVERRIDE { return true; } | 87 bool isValidOnGpu() const SK_OVERRIDE { return true; } |
80 | 88 |
81 static int alive() { return fAlive; } | 89 static int alive() { return fAlive; } |
82 | 90 |
83 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) { | 91 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) { |
84 fCache = cache; | 92 fCache = cache; |
85 fToDelete = resource; | 93 fToDelete = resource; |
86 } | 94 } |
87 | 95 |
88 private: | 96 private: |
89 GrResourceCache* fCache; | 97 GrResourceCache* fCache; |
90 TestResource* fToDelete; | 98 TestResource* fToDelete; |
99 size_t fSize; | |
91 static int fAlive; | 100 static int fAlive; |
92 | 101 |
93 typedef GrCacheable INHERITED; | 102 typedef GrCacheable INHERITED; |
94 }; | 103 }; |
95 int TestResource::fAlive = 0; | 104 int TestResource::fAlive = 0; |
96 | 105 |
97 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont ext) { | 106 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont ext) { |
98 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 107 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
99 GrCacheID::Key keyData; | 108 GrCacheID::Key keyData; |
100 keyData.fData64[0] = 5; | 109 keyData.fData64[0] = 5; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 | 176 |
168 a->unref(); | 177 a->unref(); |
169 b->unref(); | 178 b->unref(); |
170 | 179 |
171 cache.deleteResource(a->getCacheEntry()); | 180 cache.deleteResource(a->getCacheEntry()); |
172 | 181 |
173 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | 182 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
174 } | 183 } |
175 } | 184 } |
176 | 185 |
186 static 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(); | |
robertphillips
2014/04/30 19:06:54
Does this actually change the resource size with t
Chris Dalton
2014/04/30 19:08:32
Good point. I can upload one more patchset.
| |
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(); | |
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 // Test increasing a resources size beyond the cache budget. | |
225 { | |
226 GrResourceCache cache(2, 300); | |
227 | |
228 TestResource* a = new TestResource(100); | |
229 cache.addResource(key1, a); | |
230 a->unref(); | |
231 | |
232 TestResource* b = new TestResource(100); | |
233 cache.addResource(key2, b); | |
234 b->unref(); | |
235 | |
236 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); | |
237 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | |
238 | |
239 static_cast<TestResource*>(cache.find(key2))->setSize(201); | |
240 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); | |
241 | |
242 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes()); | |
243 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount()); | |
244 } | |
245 // Test changing the size of an exclusively-held resource. | |
246 { | |
247 GrResourceCache cache(2, 300); | |
248 | |
249 TestResource* a = new TestResource(100); | |
250 cache.addResource(key1, a); | |
251 cache.makeExclusive(a->getCacheEntry()); | |
252 | |
253 TestResource* b = new TestResource(100); | |
254 cache.addResource(key2, b); | |
255 b->unref(); | |
256 | |
257 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); | |
258 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | |
259 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); | |
260 | |
261 a->setSize(200); | |
262 | |
263 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); | |
264 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | |
265 // Internal resource cache validation will test the detached size (debug mode only). | |
266 | |
267 cache.makeNonExclusive(a->getCacheEntry()); | |
268 | |
269 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); | |
270 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | |
271 REPORTER_ASSERT(reporter, NULL != cache.find(key1)); | |
272 // Internal resource cache validation will test the detached size (debug mode only). | |
273 } | |
274 } | |
275 | |
177 //////////////////////////////////////////////////////////////////////////////// | 276 //////////////////////////////////////////////////////////////////////////////// |
178 DEF_GPUTEST(ResourceCache, reporter, factory) { | 277 DEF_GPUTEST(ResourceCache, reporter, factory) { |
179 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 278 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
180 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); | 279 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); |
181 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 280 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
182 continue; | 281 continue; |
183 } | 282 } |
184 GrContext* context = factory->get(glType); | 283 GrContext* context = factory->get(glType); |
185 if (NULL == context) { | 284 if (NULL == context) { |
186 continue; | 285 continue; |
187 } | 286 } |
188 | 287 |
189 GrTextureDesc desc; | 288 GrTextureDesc desc; |
190 desc.fConfig = kSkia8888_GrPixelConfig; | 289 desc.fConfig = kSkia8888_GrPixelConfig; |
191 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 290 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
192 desc.fWidth = gWidth; | 291 desc.fWidth = gWidth; |
193 desc.fHeight = gHeight; | 292 desc.fHeight = gHeight; |
194 | 293 |
195 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL L, 0)); | 294 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL L, 0)); |
196 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu re.get()))); | 295 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu re.get()))); |
197 SkCanvas canvas(device.get()); | 296 SkCanvas canvas(device.get()); |
198 | 297 |
199 test_cache(reporter, context, &canvas); | 298 test_cache(reporter, context, &canvas); |
200 test_purge_invalidated(reporter, context); | 299 test_purge_invalidated(reporter, context); |
201 test_cache_delete_on_destruction(reporter, context); | 300 test_cache_delete_on_destruction(reporter, context); |
301 test_resource_size_changed(reporter, context); | |
202 } | 302 } |
203 } | 303 } |
204 | 304 |
205 #endif | 305 #endif |
OLD | NEW |