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 "SkGpuDevice.h" | 13 #include "SkGpuDevice.h" |
| 14 #include "GrResourceCache.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); |
23 | 24 |
(...skipping 27 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 TestResource(GrGpu* gpu, int* cacheCount) |
| 66 : INHERITED(gpu, false) |
| 67 , fCache(NULL) |
| 68 , fToDelete(NULL) |
| 69 , fCacheCount(cacheCount) { |
| 70 ++*fCacheCount; |
| 71 } |
| 72 |
| 73 ~TestResource() { |
| 74 --*fCacheCount; |
| 75 |
| 76 if (NULL != fToDelete) { |
| 77 fToDelete->setDeletedResource(NULL, NULL); |
| 78 fCache->deleteResource(fToDelete); |
| 79 } |
| 80 this->release(); |
| 81 } |
| 82 |
| 83 void setDeletedResource(GrResourceCache* cache, TestResource* resource) { |
| 84 fCache = cache; |
| 85 fToDelete = resource; |
| 86 } |
| 87 |
| 88 virtual size_t sizeInBytes() const SK_OVERRIDE { |
| 89 return 100; |
| 90 } |
| 91 |
| 92 private: |
| 93 GrResourceCache* fCache; |
| 94 TestResource* fToDelete; |
| 95 int* fCacheCount; |
| 96 |
| 97 typedef GrResource INHERITED; |
| 98 }; |
| 99 |
| 100 SK_DEFINE_INST_COUNT(TestResource); |
| 101 |
| 102 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, |
| 103 GrContext* context) { |
| 104 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 105 GrCacheID::Key keyData; |
| 106 keyData.fData64[0] = 5; |
| 107 keyData.fData64[1] = 0; |
| 108 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 109 |
| 110 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 111 |
| 112 { |
| 113 int cacheCount = 0; |
| 114 { |
| 115 GrResourceCache cache(3, 30000); |
| 116 TestResource* a = new TestResource(context->getGpu(), &cacheCount); |
| 117 TestResource* b = new TestResource(context->getGpu(), &cacheCount); |
| 118 cache.addResource(key, a); |
| 119 cache.addResource(key, b); |
| 120 |
| 121 a->setDeletedResource(&cache, b); |
| 122 b->setDeletedResource(&cache, a); |
| 123 |
| 124 a->unref(); |
| 125 b->unref(); |
| 126 REPORTER_ASSERT(reporter, cacheCount == 2); |
| 127 } |
| 128 REPORTER_ASSERT(reporter, cacheCount == 0); |
| 129 } |
| 130 { |
| 131 int cacheCount = 0; |
| 132 GrResourceCache cache(3, 30000); |
| 133 TestResource* a = new TestResource(context->getGpu(), &cacheCount); |
| 134 TestResource* b = new TestResource(context->getGpu(), &cacheCount); |
| 135 cache.addResource(key, a); |
| 136 cache.addResource(key, b); |
| 137 |
| 138 a->setDeletedResource(&cache, b); |
| 139 b->setDeletedResource(&cache, a); |
| 140 |
| 141 a->unref(); |
| 142 b->unref(); |
| 143 |
| 144 cache.deleteResource(a); |
| 145 |
| 146 REPORTER_ASSERT(reporter, cacheCount == 0); |
| 147 } |
| 148 } |
| 149 |
| 150 static void test_purge_invalidated(skiatest::Reporter* reporter, |
| 151 GrContext* context) { |
| 152 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 153 GrCacheID::Key keyData; |
| 154 keyData.fData64[0] = 5; |
| 155 keyData.fData64[1] = 18; |
| 156 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 157 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 158 |
| 159 { |
| 160 int cacheCount = 0; |
| 161 GrResourceCache cache(5, 30000); |
| 162 TestResource* a = new TestResource(context->getGpu(), &cacheCount); |
| 163 cache.addResource(key, a); |
| 164 TestResource* b = new TestResource(context->getGpu(), &cacheCount); |
| 165 cache.addResource(key, b); |
| 166 a->setDeletedResource(&cache, b); |
| 167 b->setDeletedResource(&cache, a); |
| 168 a->unref(); |
| 169 b->unref(); |
| 170 |
| 171 GrResource* r = new TestResource(context->getGpu(), &cacheCount); |
| 172 cache.addResource(key, r); |
| 173 r->unref(); |
| 174 |
| 175 |
| 176 REPORTER_ASSERT(reporter, cacheCount == 3); |
| 177 GrResourceInvalidatedMessage msg = { key }; |
| 178 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); |
| 179 cache.purgeAsNeeded(); |
| 180 |
| 181 REPORTER_ASSERT(reporter, cacheCount == 0); |
| 182 } |
| 183 } |
| 184 |
| 185 |
| 186 |
61 //////////////////////////////////////////////////////////////////////////////// | 187 //////////////////////////////////////////////////////////////////////////////// |
62 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { | 188 static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { |
63 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 189 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
64 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 190 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
65 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 191 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
66 continue; | 192 continue; |
67 } | 193 } |
68 GrContext* context = factory->get(glType); | 194 GrContext* context = factory->get(glType); |
69 if (NULL == context) { | 195 if (NULL == context) { |
70 continue; | 196 continue; |
71 } | 197 } |
72 | 198 |
73 GrTextureDesc desc; | 199 GrTextureDesc desc; |
74 desc.fConfig = kSkia8888_GrPixelConfig; | 200 desc.fConfig = kSkia8888_GrPixelConfig; |
75 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 201 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
76 desc.fWidth = gWidth; | 202 desc.fWidth = gWidth; |
77 desc.fHeight = gHeight; | 203 desc.fHeight = gHeight; |
78 | 204 |
79 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); | 205 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NUL
L, 0)); |
80 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); | 206 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, textu
re.get()))); |
81 SkCanvas canvas(device.get()); | 207 SkCanvas canvas(device.get()); |
82 | 208 |
83 test_cache(reporter, context, &canvas); | 209 test_cache(reporter, context, &canvas); |
| 210 test_cache_delete_on_destruction(reporter, context); |
| 211 test_purge_invalidated(reporter, context); |
84 } | 212 } |
85 } | 213 } |
86 | 214 |
87 //////////////////////////////////////////////////////////////////////////////// | 215 //////////////////////////////////////////////////////////////////////////////// |
88 #include "TestClassDef.h" | 216 #include "TestClassDef.h" |
89 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) | 217 DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) |
90 | 218 |
91 #endif | 219 #endif |
OLD | NEW |