| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 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 GrResource { | 59 class TestResource : public GrCacheable { |
| 60 public: | 60 public: |
| 61 SK_DECLARE_INST_COUNT(TestResource); | 61 SK_DECLARE_INST_COUNT(TestResource); |
| 62 explicit TestResource(GrGpu* gpu) | 62 TestResource() |
| 63 : INHERITED(gpu, false) | 63 : fCache(NULL) |
| 64 , fCache(NULL) | |
| 65 , fToDelete(NULL) { | 64 , fToDelete(NULL) { |
| 66 ++fAlive; | 65 ++fAlive; |
| 67 } | 66 } |
| 68 | 67 |
| 69 ~TestResource() { | 68 ~TestResource() { |
| 70 --fAlive; | 69 --fAlive; |
| 71 if (NULL != fToDelete) { | 70 if (NULL != fToDelete) { |
| 72 // Breaks our little 2-element cycle below. | 71 // Breaks our little 2-element cycle below. |
| 73 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | 72 fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
| 74 fCache->deleteResource(fToDelete->getCacheEntry()); | 73 fCache->deleteResource(fToDelete->getCacheEntry()); |
| 75 } | 74 } |
| 76 this->release(); | |
| 77 } | 75 } |
| 78 | 76 |
| 79 size_t sizeInBytes() const SK_OVERRIDE { return 100; } | 77 size_t gpuMemorySize() const SK_OVERRIDE { return 100; } |
| 78 |
| 79 bool isValidOnGpu() const SK_OVERRIDE { return true; } |
| 80 | 80 |
| 81 static int alive() { return fAlive; } | 81 static int alive() { return fAlive; } |
| 82 | 82 |
| 83 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource)
{ | 83 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource)
{ |
| 84 fCache = cache; | 84 fCache = cache; |
| 85 fToDelete = resource; | 85 fToDelete = resource; |
| 86 } | 86 } |
| 87 | 87 |
| 88 private: | 88 private: |
| 89 GrResourceCache* fCache; | 89 GrResourceCache* fCache; |
| 90 TestResource* fToDelete; | 90 TestResource* fToDelete; |
| 91 static int fAlive; | 91 static int fAlive; |
| 92 | 92 |
| 93 typedef GrResource INHERITED; | 93 typedef GrCacheable INHERITED; |
| 94 }; | 94 }; |
| 95 int TestResource::fAlive = 0; | 95 int TestResource::fAlive = 0; |
| 96 | 96 |
| 97 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { | 97 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { |
| 98 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 98 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 99 GrCacheID::Key keyData; | 99 GrCacheID::Key keyData; |
| 100 keyData.fData64[0] = 5; | 100 keyData.fData64[0] = 5; |
| 101 keyData.fData64[1] = 18; | 101 keyData.fData64[1] = 18; |
| 102 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 102 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 103 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 103 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 104 | 104 |
| 105 GrResourceCache cache(5, 30000); | 105 GrResourceCache cache(5, 30000); |
| 106 | 106 |
| 107 // Add two resources with the same key that delete each other from the cache
when destroyed. | 107 // Add two resources with the same key that delete each other from the cache
when destroyed. |
| 108 TestResource* a = new TestResource(context->getGpu()); | 108 TestResource* a = new TestResource(); |
| 109 TestResource* b = new TestResource(context->getGpu()); | 109 TestResource* b = new TestResource(); |
| 110 cache.addResource(key, a); | 110 cache.addResource(key, a); |
| 111 cache.addResource(key, b); | 111 cache.addResource(key, b); |
| 112 // Circle back. | 112 // Circle back. |
| 113 a->setDeleteWhenDestroyed(&cache, b); | 113 a->setDeleteWhenDestroyed(&cache, b); |
| 114 b->setDeleteWhenDestroyed(&cache, a); | 114 b->setDeleteWhenDestroyed(&cache, a); |
| 115 a->unref(); | 115 a->unref(); |
| 116 b->unref(); | 116 b->unref(); |
| 117 | 117 |
| 118 // Add a third independent resource also with the same key. | 118 // Add a third independent resource also with the same key. |
| 119 GrResource* r = new TestResource(context->getGpu()); | 119 GrCacheable* r = new TestResource(); |
| 120 cache.addResource(key, r); | 120 cache.addResource(key, r); |
| 121 r->unref(); | 121 r->unref(); |
| 122 | 122 |
| 123 // Invalidate all three, all three should be purged and destroyed. | 123 // Invalidate all three, all three should be purged and destroyed. |
| 124 REPORTER_ASSERT(reporter, 3 == TestResource::alive()); | 124 REPORTER_ASSERT(reporter, 3 == TestResource::alive()); |
| 125 const GrResourceInvalidatedMessage msg = { key }; | 125 const GrResourceInvalidatedMessage msg = { key }; |
| 126 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); | 126 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); |
| 127 cache.purgeAsNeeded(); | 127 cache.purgeAsNeeded(); |
| 128 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | 128 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, | 131 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, |
| 132 GrContext* context) { | 132 GrContext* context) { |
| 133 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 133 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 134 GrCacheID::Key keyData; | 134 GrCacheID::Key keyData; |
| 135 keyData.fData64[0] = 5; | 135 keyData.fData64[0] = 5; |
| 136 keyData.fData64[1] = 0; | 136 keyData.fData64[1] = 0; |
| 137 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 137 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 138 | 138 |
| 139 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 139 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 140 | 140 |
| 141 { | 141 { |
| 142 { | 142 { |
| 143 GrResourceCache cache(3, 30000); | 143 GrResourceCache cache(3, 30000); |
| 144 TestResource* a = new TestResource(context->getGpu()); | 144 TestResource* a = new TestResource(); |
| 145 TestResource* b = new TestResource(context->getGpu()); | 145 TestResource* b = new TestResource(); |
| 146 cache.addResource(key, a); | 146 cache.addResource(key, a); |
| 147 cache.addResource(key, b); | 147 cache.addResource(key, b); |
| 148 | 148 |
| 149 a->setDeleteWhenDestroyed(&cache, b); | 149 a->setDeleteWhenDestroyed(&cache, b); |
| 150 b->setDeleteWhenDestroyed(&cache, a); | 150 b->setDeleteWhenDestroyed(&cache, a); |
| 151 | 151 |
| 152 a->unref(); | 152 a->unref(); |
| 153 b->unref(); | 153 b->unref(); |
| 154 REPORTER_ASSERT(reporter, 2 == TestResource::alive()); | 154 REPORTER_ASSERT(reporter, 2 == TestResource::alive()); |
| 155 } | 155 } |
| 156 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | 156 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 157 } | 157 } |
| 158 { | 158 { |
| 159 GrResourceCache cache(3, 30000); | 159 GrResourceCache cache(3, 30000); |
| 160 TestResource* a = new TestResource(context->getGpu()); | 160 TestResource* a = new TestResource(); |
| 161 TestResource* b = new TestResource(context->getGpu()); | 161 TestResource* b = new TestResource(); |
| 162 cache.addResource(key, a); | 162 cache.addResource(key, a); |
| 163 cache.addResource(key, b); | 163 cache.addResource(key, b); |
| 164 | 164 |
| 165 a->setDeleteWhenDestroyed(&cache, b); | 165 a->setDeleteWhenDestroyed(&cache, b); |
| 166 b->setDeleteWhenDestroyed(&cache, a); | 166 b->setDeleteWhenDestroyed(&cache, a); |
| 167 | 167 |
| 168 a->unref(); | 168 a->unref(); |
| 169 b->unref(); | 169 b->unref(); |
| 170 | 170 |
| 171 cache.deleteResource(a->getCacheEntry()); | 171 cache.deleteResource(a->getCacheEntry()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 196 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); | 196 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); |
| 197 SkCanvas canvas(device.get()); | 197 SkCanvas canvas(device.get()); |
| 198 | 198 |
| 199 test_cache(reporter, context, &canvas); | 199 test_cache(reporter, context, &canvas); |
| 200 test_purge_invalidated(reporter, context); | 200 test_purge_invalidated(reporter, context); |
| 201 test_cache_delete_on_destruction(reporter, context); | 201 test_cache_delete_on_destruction(reporter, context); |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 #endif | 205 #endif |
| OLD | NEW |