| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 size_t curCacheSize; | 51 size_t curCacheSize; |
| 52 context->getResourceCacheUsage(NULL, &curCacheSize); | 52 context->getResourceCacheUsage(NULL, &curCacheSize); |
| 53 | 53 |
| 54 // we should never go over the size limit | 54 // we should never go over the size limit |
| 55 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); | 55 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 56 } | 56 } |
| 57 | 57 |
| 58 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); | 58 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
| 59 } | 59 } |
| 60 | 60 |
| 61 class TestResource : public GrCacheable { | 61 class TestResource : public GrGpuObject { |
| 62 static const size_t kDefaultSize = 100; | 62 static const size_t kDefaultSize = 100; |
| 63 | 63 |
| 64 public: | 64 public: |
| 65 SK_DECLARE_INST_COUNT(TestResource); | 65 SK_DECLARE_INST_COUNT(TestResource); |
| 66 TestResource(size_t size = kDefaultSize) | 66 TestResource(GrGpu* gpu, size_t size = kDefaultSize) |
| 67 : fCache(NULL) | 67 : INHERITED(gpu, false) |
| 68 , fCache(NULL) |
| 68 , fToDelete(NULL) | 69 , fToDelete(NULL) |
| 69 , fSize(size) { | 70 , fSize(size) { |
| 70 ++fAlive; | 71 ++fAlive; |
| 71 } | 72 } |
| 72 | 73 |
| 73 ~TestResource() { | 74 ~TestResource() { |
| 74 --fAlive; | 75 --fAlive; |
| 75 if (NULL != fToDelete) { | 76 if (NULL != fToDelete) { |
| 76 // Breaks our little 2-element cycle below. | 77 // Breaks our little 2-element cycle below. |
| 77 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | 78 fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
| 78 fCache->deleteResource(fToDelete->getCacheEntry()); | 79 fCache->deleteResource(fToDelete->getCacheEntry()); |
| 79 } | 80 } |
| 81 this->release(); |
| 80 } | 82 } |
| 81 | 83 |
| 82 void setSize(size_t size) { | 84 void setSize(size_t size) { |
| 83 fSize = size; | 85 fSize = size; |
| 84 this->didChangeGpuMemorySize(); | 86 this->didChangeGpuMemorySize(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; } | 89 size_t gpuMemorySize() const SK_OVERRIDE { return fSize; } |
| 88 | 90 |
| 89 bool isValidOnGpu() const SK_OVERRIDE { return true; } | |
| 90 | |
| 91 static int alive() { return fAlive; } | 91 static int alive() { return fAlive; } |
| 92 | 92 |
| 93 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource)
{ | 93 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource)
{ |
| 94 fCache = cache; | 94 fCache = cache; |
| 95 fToDelete = resource; | 95 fToDelete = resource; |
| 96 } | 96 } |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 GrResourceCache* fCache; | 99 GrResourceCache* fCache; |
| 100 TestResource* fToDelete; | 100 TestResource* fToDelete; |
| 101 size_t fSize; | 101 size_t fSize; |
| 102 static int fAlive; | 102 static int fAlive; |
| 103 | 103 |
| 104 typedef GrCacheable INHERITED; | 104 typedef GrGpuObject INHERITED; |
| 105 }; | 105 }; |
| 106 int TestResource::fAlive = 0; | 106 int TestResource::fAlive = 0; |
| 107 | 107 |
| 108 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { | 108 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { |
| 109 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 109 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 110 GrCacheID::Key keyData; | 110 GrCacheID::Key keyData; |
| 111 keyData.fData64[0] = 5; | 111 keyData.fData64[0] = 5; |
| 112 keyData.fData64[1] = 18; | 112 keyData.fData64[1] = 18; |
| 113 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 113 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 114 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 114 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 115 | 115 |
| 116 GrResourceCache cache(5, 30000); | 116 GrResourceCache cache(5, 30000); |
| 117 | 117 |
| 118 // Add two resources with the same key that delete each other from the cache
when destroyed. | 118 // Add two resources with the same key that delete each other from the cache
when destroyed. |
| 119 TestResource* a = new TestResource(); | 119 TestResource* a = new TestResource(context->getGpu()); |
| 120 TestResource* b = new TestResource(); | 120 TestResource* b = new TestResource(context->getGpu()); |
| 121 cache.addResource(key, a); | 121 cache.addResource(key, a); |
| 122 cache.addResource(key, b); | 122 cache.addResource(key, b); |
| 123 // Circle back. | 123 // Circle back. |
| 124 a->setDeleteWhenDestroyed(&cache, b); | 124 a->setDeleteWhenDestroyed(&cache, b); |
| 125 b->setDeleteWhenDestroyed(&cache, a); | 125 b->setDeleteWhenDestroyed(&cache, a); |
| 126 a->unref(); | 126 a->unref(); |
| 127 b->unref(); | 127 b->unref(); |
| 128 | 128 |
| 129 // Add a third independent resource also with the same key. | 129 // Add a third independent resource also with the same key. |
| 130 GrCacheable* r = new TestResource(); | 130 GrGpuObject* r = new TestResource(context->getGpu()); |
| 131 cache.addResource(key, r); | 131 cache.addResource(key, r); |
| 132 r->unref(); | 132 r->unref(); |
| 133 | 133 |
| 134 // Invalidate all three, all three should be purged and destroyed. | 134 // Invalidate all three, all three should be purged and destroyed. |
| 135 REPORTER_ASSERT(reporter, 3 == TestResource::alive()); | 135 REPORTER_ASSERT(reporter, 3 == TestResource::alive()); |
| 136 const GrResourceInvalidatedMessage msg = { key }; | 136 const GrResourceInvalidatedMessage msg = { key }; |
| 137 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); | 137 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); |
| 138 cache.purgeAsNeeded(); | 138 cache.purgeAsNeeded(); |
| 139 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | 139 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, | 142 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, |
| 143 GrContext* context) { | 143 GrContext* context) { |
| 144 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 144 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 145 GrCacheID::Key keyData; | 145 GrCacheID::Key keyData; |
| 146 keyData.fData64[0] = 5; | 146 keyData.fData64[0] = 5; |
| 147 keyData.fData64[1] = 0; | 147 keyData.fData64[1] = 0; |
| 148 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 148 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 149 | 149 |
| 150 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 150 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 151 | 151 |
| 152 { | 152 { |
| 153 { | 153 { |
| 154 GrResourceCache cache(3, 30000); | 154 GrResourceCache cache(3, 30000); |
| 155 TestResource* a = new TestResource(); | 155 TestResource* a = new TestResource(context->getGpu()); |
| 156 TestResource* b = new TestResource(); | 156 TestResource* b = new TestResource(context->getGpu()); |
| 157 cache.addResource(key, a); | 157 cache.addResource(key, a); |
| 158 cache.addResource(key, b); | 158 cache.addResource(key, b); |
| 159 | 159 |
| 160 a->setDeleteWhenDestroyed(&cache, b); | 160 a->setDeleteWhenDestroyed(&cache, b); |
| 161 b->setDeleteWhenDestroyed(&cache, a); | 161 b->setDeleteWhenDestroyed(&cache, a); |
| 162 | 162 |
| 163 a->unref(); | 163 a->unref(); |
| 164 b->unref(); | 164 b->unref(); |
| 165 REPORTER_ASSERT(reporter, 2 == TestResource::alive()); | 165 REPORTER_ASSERT(reporter, 2 == TestResource::alive()); |
| 166 } | 166 } |
| 167 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | 167 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 168 } | 168 } |
| 169 { | 169 { |
| 170 GrResourceCache cache(3, 30000); | 170 GrResourceCache cache(3, 30000); |
| 171 TestResource* a = new TestResource(); | 171 TestResource* a = new TestResource(context->getGpu()); |
| 172 TestResource* b = new TestResource(); | 172 TestResource* b = new TestResource(context->getGpu()); |
| 173 cache.addResource(key, a); | 173 cache.addResource(key, a); |
| 174 cache.addResource(key, b); | 174 cache.addResource(key, b); |
| 175 | 175 |
| 176 a->setDeleteWhenDestroyed(&cache, b); | 176 a->setDeleteWhenDestroyed(&cache, b); |
| 177 b->setDeleteWhenDestroyed(&cache, a); | 177 b->setDeleteWhenDestroyed(&cache, a); |
| 178 | 178 |
| 179 a->unref(); | 179 a->unref(); |
| 180 b->unref(); | 180 b->unref(); |
| 181 | 181 |
| 182 cache.deleteResource(a->getCacheEntry()); | 182 cache.deleteResource(a->getCacheEntry()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 197 | 197 |
| 198 GrCacheID::Key key2Data; | 198 GrCacheID::Key key2Data; |
| 199 key2Data.fData64[0] = 1; | 199 key2Data.fData64[0] = 1; |
| 200 key2Data.fData64[1] = 0; | 200 key2Data.fData64[1] = 0; |
| 201 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); | 201 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); |
| 202 | 202 |
| 203 // Test changing resources sizes (both increase & decrease). | 203 // Test changing resources sizes (both increase & decrease). |
| 204 { | 204 { |
| 205 GrResourceCache cache(2, 300); | 205 GrResourceCache cache(2, 300); |
| 206 | 206 |
| 207 TestResource* a = new TestResource(0); | 207 TestResource* a = new TestResource(context->getGpu()); |
| 208 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. | 208 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. |
| 209 cache.addResource(key1, a); | 209 cache.addResource(key1, a); |
| 210 a->unref(); | 210 a->unref(); |
| 211 | 211 |
| 212 TestResource* b = new TestResource(0); | 212 TestResource* b = new TestResource(context->getGpu()); |
| 213 b->setSize(100); | 213 b->setSize(100); |
| 214 cache.addResource(key2, b); | 214 cache.addResource(key2, b); |
| 215 b->unref(); | 215 b->unref(); |
| 216 | 216 |
| 217 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); | 217 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 218 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | 218 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 219 | 219 |
| 220 static_cast<TestResource*>(cache.find(key2))->setSize(200); | 220 static_cast<TestResource*>(cache.find(key2))->setSize(200); |
| 221 static_cast<TestResource*>(cache.find(key1))->setSize(50); | 221 static_cast<TestResource*>(cache.find(key1))->setSize(50); |
| 222 | 222 |
| 223 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes()); | 223 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes()); |
| 224 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | 224 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Test increasing a resources size beyond the cache budget. | 227 // Test increasing a resources size beyond the cache budget. |
| 228 { | 228 { |
| 229 GrResourceCache cache(2, 300); | 229 GrResourceCache cache(2, 300); |
| 230 | 230 |
| 231 TestResource* a = new TestResource(100); | 231 TestResource* a = new TestResource(context->getGpu(), 100); |
| 232 cache.addResource(key1, a); | 232 cache.addResource(key1, a); |
| 233 a->unref(); | 233 a->unref(); |
| 234 | 234 |
| 235 TestResource* b = new TestResource(100); | 235 TestResource* b = new TestResource(context->getGpu(), 100); |
| 236 cache.addResource(key2, b); | 236 cache.addResource(key2, b); |
| 237 b->unref(); | 237 b->unref(); |
| 238 | 238 |
| 239 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); | 239 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 240 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | 240 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 241 | 241 |
| 242 static_cast<TestResource*>(cache.find(key2))->setSize(201); | 242 static_cast<TestResource*>(cache.find(key2))->setSize(201); |
| 243 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); | 243 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); |
| 244 | 244 |
| 245 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes()); | 245 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes()); |
| 246 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount()); | 246 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount()); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // Test changing the size of an exclusively-held resource. | 249 // Test changing the size of an exclusively-held resource. |
| 250 { | 250 { |
| 251 GrResourceCache cache(2, 300); | 251 GrResourceCache cache(2, 300); |
| 252 | 252 |
| 253 TestResource* a = new TestResource(100); | 253 TestResource* a = new TestResource(context->getGpu(), 100); |
| 254 cache.addResource(key1, a); | 254 cache.addResource(key1, a); |
| 255 cache.makeExclusive(a->getCacheEntry()); | 255 cache.makeExclusive(a->getCacheEntry()); |
| 256 | 256 |
| 257 TestResource* b = new TestResource(100); | 257 TestResource* b = new TestResource(context->getGpu(), 100); |
| 258 cache.addResource(key2, b); | 258 cache.addResource(key2, b); |
| 259 b->unref(); | 259 b->unref(); |
| 260 | 260 |
| 261 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); | 261 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 262 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); | 262 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 263 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); | 263 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); |
| 264 | 264 |
| 265 a->setSize(200); | 265 a->setSize(200); |
| 266 | 266 |
| 267 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); | 267 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 SkCanvas canvas(device.get()); | 301 SkCanvas canvas(device.get()); |
| 302 | 302 |
| 303 test_cache(reporter, context, &canvas); | 303 test_cache(reporter, context, &canvas); |
| 304 test_purge_invalidated(reporter, context); | 304 test_purge_invalidated(reporter, context); |
| 305 test_cache_delete_on_destruction(reporter, context); | 305 test_cache_delete_on_destruction(reporter, context); |
| 306 test_resource_size_changed(reporter, context); | 306 test_resource_size_changed(reporter, context); |
| 307 } | 307 } |
| 308 } | 308 } |
| 309 | 309 |
| 310 #endif | 310 #endif |
| OLD | NEW |