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