| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "GrContext.h" | 10 #include "GrContext.h" |
| 11 #include "GrContextFactory.h" | 11 #include "GrContextFactory.h" |
| 12 #include "GrLayerCache.h" | 12 #include "GrLayerCache.h" |
| 13 #include "SkPictureRecorder.h" | 13 #include "SkPictureRecorder.h" |
| 14 #include "Test.h" | 14 #include "Test.h" |
| 15 | 15 |
| 16 class TestingAccess { | 16 class TestingAccess { |
| 17 public: | 17 public: |
| 18 static unsigned NumLayers(GrLayerCache* cache) { | 18 static int NumLayers(GrLayerCache* cache) { |
| 19 return cache->numLayers(); | 19 return cache->numLayers(); |
| 20 } | 20 } |
| 21 static void Purge(GrLayerCache* cache, uint32_t pictureID) { | 21 static void Purge(GrLayerCache* cache, uint32_t pictureID) { |
| 22 cache->purge(pictureID); | 22 cache->purge(pictureID); |
| 23 } | 23 } |
| 24 static int Uses(GrCachedLayer* layer) { | 24 static int Uses(GrCachedLayer* layer) { |
| 25 return layer->uses(); | 25 return layer->uses(); |
| 26 } | 26 } |
| 27 static GrCachedLayer* Find(GrLayerCache* cache, uint32_t pictureID, | 27 static GrCachedLayer* Find(GrLayerCache* cache, uint32_t pictureID, |
| 28 const SkMatrix& initialMat, | 28 const SkMatrix& initialMat, |
| 29 const unsigned* key, int keySize) { | 29 const int* key, int keySize) { |
| 30 return cache->findLayer(pictureID, initialMat, key, keySize); | 30 return cache->findLayer(pictureID, initialMat, key, keySize); |
| 31 } | 31 } |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 // Add several layers to the cache | 34 // Add several layers to the cache |
| 35 static void create_layers(skiatest::Reporter* reporter, | 35 static void create_layers(skiatest::Reporter* reporter, |
| 36 GrLayerCache* cache, | 36 GrLayerCache* cache, |
| 37 const SkPicture& picture, | 37 const SkPicture& picture, |
| 38 unsigned numToAdd, | 38 int numToAdd, |
| 39 unsigned idOffset) { | 39 int idOffset) { |
| 40 | 40 |
| 41 for (unsigned i = 0; i < numToAdd; ++i) { | 41 for (int i = 0; i < numToAdd; ++i) { |
| 42 unsigned indices[1] = { idOffset+i+1 }; | 42 int indices[1] = { idOffset+i+1 }; |
| 43 GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(), | 43 GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(), |
| 44 idOffset+i+1, idOffset+i
+2, | 44 idOffset+i+1, idOffset+i
+2, |
| 45 SkIRect::MakeEmpty(), | 45 SkIRect::MakeEmpty(), |
| 46 SkIRect::MakeEmpty(), | 46 SkIRect::MakeEmpty(), |
| 47 SkMatrix::I(), | 47 SkMatrix::I(), |
| 48 indices, 1, | 48 indices, 1, |
| 49 NULL); | 49 NULL); |
| 50 REPORTER_ASSERT(reporter, layer); | 50 REPORTER_ASSERT(reporter, layer); |
| 51 GrCachedLayer* temp = TestingAccess::Find(cache, picture.uniqueID(), SkM
atrix::I(), | 51 GrCachedLayer* temp = TestingAccess::Find(cache, picture.uniqueID(), SkM
atrix::I(), |
| 52 indices, 1); | 52 indices, 1); |
| 53 REPORTER_ASSERT(reporter, temp == layer); | 53 REPORTER_ASSERT(reporter, temp == layer); |
| 54 | 54 |
| 55 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset +
i + 1); | 55 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset +
i + 1); |
| 56 | 56 |
| 57 REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID()); | 57 REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID()); |
| 58 REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1); | 58 REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1); |
| 59 REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2); | 59 REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2); |
| 60 REPORTER_ASSERT(reporter, NULL == layer->texture()); | 60 REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 61 REPORTER_ASSERT(reporter, NULL == layer->paint()); | 61 REPORTER_ASSERT(reporter, NULL == layer->paint()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 88 cache->addUse(layer); | 88 cache->addUse(layer); |
| 89 | 89 |
| 90 REPORTER_ASSERT(reporter, 1 == TestingAccess::Uses(layer)); | 90 REPORTER_ASSERT(reporter, 1 == TestingAccess::Uses(layer)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // This test case exercises the public API of the GrLayerCache class. | 93 // This test case exercises the public API of the GrLayerCache class. |
| 94 // In particular it checks its interaction with the resource cache (w.r.t. | 94 // In particular it checks its interaction with the resource cache (w.r.t. |
| 95 // locking & unlocking textures). | 95 // locking & unlocking textures). |
| 96 // TODO: need to add checks on VRAM usage! | 96 // TODO: need to add checks on VRAM usage! |
| 97 DEF_GPUTEST(GpuLayerCache, reporter, factory) { | 97 DEF_GPUTEST(GpuLayerCache, reporter, factory) { |
| 98 static const unsigned kInitialNumLayers = 5; | 98 static const int kInitialNumLayers = 5; |
| 99 | 99 |
| 100 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { | 100 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 101 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext
Type) i; | 101 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext
Type) i; |
| 102 | 102 |
| 103 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { | 103 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 104 continue; | 104 continue; |
| 105 } | 105 } |
| 106 | 106 |
| 107 GrContext* context = factory->get(glCtxType); | 107 GrContext* context = factory->get(glCtxType); |
| 108 | 108 |
| 109 if (NULL == context) { | 109 if (NULL == context) { |
| 110 continue; | 110 continue; |
| 111 } | 111 } |
| 112 | 112 |
| 113 SkPictureRecorder recorder; | 113 SkPictureRecorder recorder; |
| 114 SkCanvas* c = recorder.beginRecording(1, 1); | 114 SkCanvas* c = recorder.beginRecording(1, 1); |
| 115 // Draw something, anything, to prevent an empty-picture optimizatio
n, | 115 // Draw something, anything, to prevent an empty-picture optimizatio
n, |
| 116 // which is a singleton and never purged. | 116 // which is a singleton and never purged. |
| 117 c->drawRect(SkRect::MakeWH(1,1), SkPaint()); | 117 c->drawRect(SkRect::MakeWH(1,1), SkPaint()); |
| 118 SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); | 118 SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
| 119 | 119 |
| 120 GrLayerCache cache(context); | 120 GrLayerCache cache(context); |
| 121 | 121 |
| 122 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); | 122 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); |
| 123 | 123 |
| 124 for (unsigned i = 0; i < kInitialNumLayers; ++i) { | 124 for (int i = 0; i < kInitialNumLayers; ++i) { |
| 125 unsigned indices[1] = { i + 1 }; | 125 int indices[1] = { i + 1 }; |
| 126 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), | 126 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), |
| 127 indices, 1); | 127 indices, 1); |
| 128 REPORTER_ASSERT(reporter, layer); | 128 REPORTER_ASSERT(reporter, layer); |
| 129 | 129 |
| 130 lock_layer(reporter, &cache, layer); | 130 lock_layer(reporter, &cache, layer); |
| 131 | 131 |
| 132 // The first 4 layers should be in the atlas (and thus have non-empt
y | 132 // The first 4 layers should be in the atlas (and thus have non-empt
y |
| 133 // rects) | 133 // rects) |
| 134 if (i < 4) { | 134 if (i < 4) { |
| 135 REPORTER_ASSERT(reporter, layer->isAtlased()); | 135 REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 136 } else { | 136 } else { |
| 137 // The 5th layer couldn't fit in the atlas | 137 // The 5th layer couldn't fit in the atlas |
| 138 REPORTER_ASSERT(reporter, !layer->isAtlased()); | 138 REPORTER_ASSERT(reporter, !layer->isAtlased()); |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Unlock the textures | 142 // Unlock the textures |
| 143 for (unsigned i = 0; i < kInitialNumLayers; ++i) { | 143 for (int i = 0; i < kInitialNumLayers; ++i) { |
| 144 unsigned indices[1] = { i+1 }; | 144 int indices[1] = { i+1 }; |
| 145 | 145 |
| 146 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), | 146 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), |
| 147 indices, 1); | 147 indices, 1); |
| 148 REPORTER_ASSERT(reporter, layer); | 148 REPORTER_ASSERT(reporter, layer); |
| 149 cache.removeUse(layer); | 149 cache.removeUse(layer); |
| 150 } | 150 } |
| 151 | 151 |
| 152 for (unsigned i = 0; i < kInitialNumLayers; ++i) { | 152 for (int i = 0; i < kInitialNumLayers; ++i) { |
| 153 unsigned indices[1] = { i+1 }; | 153 int indices[1] = { i+1 }; |
| 154 | 154 |
| 155 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), | 155 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), |
| 156 indices, 1); | 156 indices, 1); |
| 157 REPORTER_ASSERT(reporter, layer); | 157 REPORTER_ASSERT(reporter, layer); |
| 158 | 158 |
| 159 // All the layers should be unlocked | 159 // All the layers should be unlocked |
| 160 REPORTER_ASSERT(reporter, !layer->locked()); | 160 REPORTER_ASSERT(reporter, !layer->locked()); |
| 161 | 161 |
| 162 // When hoisted layers aren't cached they are aggressively removed | 162 // When hoisted layers aren't cached they are aggressively removed |
| 163 // from the atlas | 163 // from the atlas |
| 164 #if GR_CACHE_HOISTED_LAYERS | 164 #if GR_CACHE_HOISTED_LAYERS |
| 165 // The first 4 layers should still be in the atlas. | 165 // The first 4 layers should still be in the atlas. |
| 166 if (i < 4) { | 166 if (i < 4) { |
| 167 REPORTER_ASSERT(reporter, layer->texture()); | 167 REPORTER_ASSERT(reporter, layer->texture()); |
| 168 REPORTER_ASSERT(reporter, layer->isAtlased()); | 168 REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 169 } else { | 169 } else { |
| 170 #endif | 170 #endif |
| 171 // The final layer should not be atlased. | 171 // The final layer should not be atlased. |
| 172 REPORTER_ASSERT(reporter, NULL == layer->texture()); | 172 REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 173 REPORTER_ASSERT(reporter, !layer->isAtlased()); | 173 REPORTER_ASSERT(reporter, !layer->isAtlased()); |
| 174 #if GR_CACHE_HOISTED_LAYERS | 174 #if GR_CACHE_HOISTED_LAYERS |
| 175 } | 175 } |
| 176 #endif | 176 #endif |
| 177 } | 177 } |
| 178 | 178 |
| 179 { | 179 { |
| 180 unsigned indices[1] = { kInitialNumLayers+1 }; | 180 int indices[1] = { kInitialNumLayers+1 }; |
| 181 | 181 |
| 182 // Add an additional layer. Since all the layers are unlocked this | 182 // Add an additional layer. Since all the layers are unlocked this |
| 183 // will force out the first atlased layer | 183 // will force out the first atlased layer |
| 184 create_layers(reporter, &cache, *picture, 1, kInitialNumLayers); | 184 create_layers(reporter, &cache, *picture, 1, kInitialNumLayers); |
| 185 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), | 185 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), |
| 186 indices, 1); | 186 indices, 1); |
| 187 REPORTER_ASSERT(reporter, layer); | 187 REPORTER_ASSERT(reporter, layer); |
| 188 | 188 |
| 189 lock_layer(reporter, &cache, layer); | 189 lock_layer(reporter, &cache, layer); |
| 190 cache.removeUse(layer); | 190 cache.removeUse(layer); |
| 191 } | 191 } |
| 192 | 192 |
| 193 for (unsigned i = 0; i < kInitialNumLayers+1; ++i) { | 193 for (int i = 0; i < kInitialNumLayers+1; ++i) { |
| 194 unsigned indices[1] = { i+1 }; | 194 int indices[1] = { i+1 }; |
| 195 | 195 |
| 196 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), | 196 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID
(), SkMatrix::I(), |
| 197 indices, 1); | 197 indices, 1); |
| 198 #if GR_CACHE_HOISTED_LAYERS | 198 #if GR_CACHE_HOISTED_LAYERS |
| 199 // 3 old layers plus the new one should be in the atlas. | 199 // 3 old layers plus the new one should be in the atlas. |
| 200 if (1 == i || 2 == i || 3 == i || 5 == i) { | 200 if (1 == i || 2 == i || 3 == i || 5 == i) { |
| 201 REPORTER_ASSERT(reporter, layer); | 201 REPORTER_ASSERT(reporter, layer); |
| 202 REPORTER_ASSERT(reporter, !layer->locked()); | 202 REPORTER_ASSERT(reporter, !layer->locked()); |
| 203 REPORTER_ASSERT(reporter, layer->texture()); | 203 REPORTER_ASSERT(reporter, layer->texture()); |
| 204 REPORTER_ASSERT(reporter, layer->isAtlased()); | 204 REPORTER_ASSERT(reporter, layer->isAtlased()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 244 |
| 245 picture.reset(NULL); | 245 picture.reset(NULL); |
| 246 cache.processDeletedPictures(); | 246 cache.processDeletedPictures(); |
| 247 | 247 |
| 248 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); | 248 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); |
| 249 // TODO: add VRAM/resource cache check here | 249 // TODO: add VRAM/resource cache check here |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 #endif | 253 #endif |
| OLD | NEW |