| 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 #include "Test.h" | 8 #include "Test.h" |
| 9 | 9 |
| 10 // This is a GPU test | 10 // This is a GPU test |
| 11 #if SK_SUPPORT_GPU | 11 #if SK_SUPPORT_GPU |
| 12 #include "GrContextFactory.h" | 12 #include "GrContextFactory.h" |
| 13 #include "GrResourceCache.h" | |
| 14 #include "SkGpuDevice.h" | 13 #include "SkGpuDevice.h" |
| 15 | 14 |
| 16 static const int gWidth = 640; | 15 static const int gWidth = 640; |
| 17 static const int gHeight = 480; | 16 static const int gHeight = 480; |
| 18 | 17 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
| 20 static void test_cache(skiatest::Reporter* reporter, | 19 static void test_cache(skiatest::Reporter* reporter, |
| 21 GrContext* context, | 20 GrContext* context, |
| 22 SkCanvas* canvas) { | 21 SkCanvas* canvas) { |
| 23 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); | 22 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 52 | 51 |
| 53 size_t curCacheSize = context->getGpuTextureCacheBytes(); | 52 size_t curCacheSize = context->getGpuTextureCacheBytes(); |
| 54 | 53 |
| 55 // we should never go over the size limit | 54 // we should never go over the size limit |
| 56 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); | 55 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 57 } | 56 } |
| 58 | 57 |
| 59 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); | 58 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); |
| 60 } | 59 } |
| 61 | 60 |
| 62 class TestResource : public GrResource { | |
| 63 public: | |
| 64 SK_DECLARE_INST_COUNT(TestResource); | |
| 65 explicit TestResource(GrGpu* gpu) | |
| 66 : INHERITED(gpu, false) | |
| 67 , fCache(NULL) | |
| 68 , fToDelete(NULL) { | |
| 69 ++fAlive; | |
| 70 } | |
| 71 | |
| 72 ~TestResource() { | |
| 73 --fAlive; | |
| 74 if (NULL != fToDelete) { | |
| 75 // Breaks our little 2-element cycle below. | |
| 76 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | |
| 77 fCache->deleteResource(fToDelete->getCacheEntry()); | |
| 78 } | |
| 79 this->release(); | |
| 80 } | |
| 81 | |
| 82 size_t sizeInBytes() const SK_OVERRIDE { return 100; } | |
| 83 | |
| 84 static int alive() { return fAlive; } | |
| 85 | |
| 86 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource)
{ | |
| 87 fCache = cache; | |
| 88 fToDelete = resource; | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 GrResourceCache* fCache; | |
| 93 TestResource* fToDelete; | |
| 94 static int fAlive; | |
| 95 | |
| 96 typedef GrResource INHERITED; | |
| 97 }; | |
| 98 SK_DEFINE_INST_COUNT(TestResource); | |
| 99 int TestResource::fAlive = 0; | |
| 100 | |
| 101 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { | |
| 102 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | |
| 103 GrCacheID::Key keyData; | |
| 104 keyData.fData64[0] = 5; | |
| 105 keyData.fData64[1] = 18; | |
| 106 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | |
| 107 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | |
| 108 | |
| 109 GrResourceCache cache(5, 30000); | |
| 110 | |
| 111 // Add two resources with the same key that delete each other from the cache
when destroyed. | |
| 112 TestResource* a = new TestResource(context->getGpu()); | |
| 113 TestResource* b = new TestResource(context->getGpu()); | |
| 114 cache.addResource(key, a); | |
| 115 cache.addResource(key, b); | |
| 116 // Circle back. | |
| 117 a->setDeleteWhenDestroyed(&cache, b); | |
| 118 b->setDeleteWhenDestroyed(&cache, a); | |
| 119 a->unref(); | |
| 120 b->unref(); | |
| 121 | |
| 122 // Add a third independent resource also with the same key. | |
| 123 GrResource* r = new TestResource(context->getGpu()); | |
| 124 cache.addResource(key, r); | |
| 125 r->unref(); | |
| 126 | |
| 127 // Invalidate all three, all three should be purged and destroyed. | |
| 128 REPORTER_ASSERT(reporter, 3 == TestResource::alive()); | |
| 129 const GrResourceInvalidatedMessage msg = { key }; | |
| 130 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); | |
| 131 cache.purgeAsNeeded(); | |
| 132 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); | |
| 133 } | |
| 134 | |
| 135 //////////////////////////////////////////////////////////////////////////////// | 61 //////////////////////////////////////////////////////////////////////////////// |
| 136 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { | 62 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { |
| 137 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 63 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 138 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 64 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
| 139 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 65 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 140 continue; | 66 continue; |
| 141 } | 67 } |
| 142 GrContext* context = factory->get(glType); | 68 GrContext* context = factory->get(glType); |
| 143 if (NULL == context) { | 69 if (NULL == context) { |
| 144 continue; | 70 continue; |
| 145 } | 71 } |
| 146 | 72 |
| 147 GrTextureDesc desc; | 73 GrTextureDesc desc; |
| 148 desc.fConfig = kSkia8888_GrPixelConfig; | 74 desc.fConfig = kSkia8888_GrPixelConfig; |
| 149 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 75 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 150 desc.fWidth = gWidth; | 76 desc.fWidth = gWidth; |
| 151 desc.fHeight = gHeight; | 77 desc.fHeight = gHeight; |
| 152 | 78 |
| 153 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); | 79 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); |
| 154 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); | 80 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); |
| 155 SkCanvas canvas(device.get()); | 81 SkCanvas canvas(device.get()); |
| 156 | 82 |
| 157 test_cache(reporter, context, &canvas); | 83 test_cache(reporter, context, &canvas); |
| 158 test_purge_invalidated(reporter, context); | |
| 159 } | 84 } |
| 160 } | 85 } |
| 161 | 86 |
| 162 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
| 163 #include "TestClassDef.h" | 88 #include "TestClassDef.h" |
| 164 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) | 89 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) |
| 165 | 90 |
| 166 #endif | 91 #endif |
| OLD | NEW |