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" |
13 #include "SkGpuDevice.h" | 14 #include "SkGpuDevice.h" |
14 | 15 |
15 static const int gWidth = 640; | 16 static const int gWidth = 640; |
16 static const int gHeight = 480; | 17 static const int gHeight = 480; |
17 | 18 |
18 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
19 static void test_cache(skiatest::Reporter* reporter, | 20 static void test_cache(skiatest::Reporter* reporter, |
20 GrContext* context, | 21 GrContext* context, |
21 SkCanvas* canvas) { | 22 SkCanvas* canvas) { |
22 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); | 23 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
(...skipping 28 matching lines...) Expand all Loading... |
51 | 52 |
52 size_t curCacheSize = context->getGpuTextureCacheBytes(); | 53 size_t curCacheSize = context->getGpuTextureCacheBytes(); |
53 | 54 |
54 // we should never go over the size limit | 55 // we should never go over the size limit |
55 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); | 56 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
56 } | 57 } |
57 | 58 |
58 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); | 59 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); |
59 } | 60 } |
60 | 61 |
| 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 |
61 //////////////////////////////////////////////////////////////////////////////// | 135 //////////////////////////////////////////////////////////////////////////////// |
62 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { | 136 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { |
63 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 137 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
64 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 138 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
65 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 139 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
66 continue; | 140 continue; |
67 } | 141 } |
68 GrContext* context = factory->get(glType); | 142 GrContext* context = factory->get(glType); |
69 if (NULL == context) { | 143 if (NULL == context) { |
70 continue; | 144 continue; |
71 } | 145 } |
72 | 146 |
73 GrTextureDesc desc; | 147 GrTextureDesc desc; |
74 desc.fConfig = kSkia8888_GrPixelConfig; | 148 desc.fConfig = kSkia8888_GrPixelConfig; |
75 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 149 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
76 desc.fWidth = gWidth; | 150 desc.fWidth = gWidth; |
77 desc.fHeight = gHeight; | 151 desc.fHeight = gHeight; |
78 | 152 |
79 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); | 153 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); |
80 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); | 154 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); |
81 SkCanvas canvas(device.get()); | 155 SkCanvas canvas(device.get()); |
82 | 156 |
83 test_cache(reporter, context, &canvas); | 157 test_cache(reporter, context, &canvas); |
| 158 test_purge_invalidated(reporter, context); |
84 } | 159 } |
85 } | 160 } |
86 | 161 |
87 //////////////////////////////////////////////////////////////////////////////// | 162 //////////////////////////////////////////////////////////////////////////////// |
88 #include "TestClassDef.h" | 163 #include "TestClassDef.h" |
89 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) | 164 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) |
90 | 165 |
91 #endif | 166 #endif |
OLD | NEW |