| 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(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 | 
| 177 //////////////////////////////////////////////////////////////////////////////// | 279 //////////////////////////////////////////////////////////////////////////////// | 
| 178 DEF_GPUTEST(ResourceCache, reporter, factory) { | 280 DEF_GPUTEST(ResourceCache, reporter, factory) { | 
| 179     for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 281     for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 
| 180         GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
     LContextType>(type); | 282         GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
     LContextType>(type); | 
| 181         if (!GrContextFactory::IsRenderingGLContext(glType)) { | 283         if (!GrContextFactory::IsRenderingGLContext(glType)) { | 
| 182             continue; | 284             continue; | 
| 183         } | 285         } | 
| 184         GrContext* context = factory->get(glType); | 286         GrContext* context = factory->get(glType); | 
| 185         if (NULL == context) { | 287         if (NULL == context) { | 
| 186             continue; | 288             continue; | 
| 187         } | 289         } | 
| 188 | 290 | 
| 189         GrTextureDesc desc; | 291         GrTextureDesc desc; | 
| 190         desc.fConfig = kSkia8888_GrPixelConfig; | 292         desc.fConfig = kSkia8888_GrPixelConfig; | 
| 191         desc.fFlags = kRenderTarget_GrTextureFlagBit; | 293         desc.fFlags = kRenderTarget_GrTextureFlagBit; | 
| 192         desc.fWidth = gWidth; | 294         desc.fWidth = gWidth; | 
| 193         desc.fHeight = gHeight; | 295         desc.fHeight = gHeight; | 
| 194 | 296 | 
| 195         SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
     L, 0)); | 297         SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
     L, 0)); | 
| 196         SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
     re.get()))); | 298         SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
     re.get()))); | 
| 197         SkCanvas canvas(device.get()); | 299         SkCanvas canvas(device.get()); | 
| 198 | 300 | 
| 199         test_cache(reporter, context, &canvas); | 301         test_cache(reporter, context, &canvas); | 
| 200         test_purge_invalidated(reporter, context); | 302         test_purge_invalidated(reporter, context); | 
| 201         test_cache_delete_on_destruction(reporter, context); | 303         test_cache_delete_on_destruction(reporter, context); | 
|  | 304         test_resource_size_changed(reporter, context); | 
| 202     } | 305     } | 
| 203 } | 306 } | 
| 204 | 307 | 
| 205 #endif | 308 #endif | 
| OLD | NEW | 
|---|