robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #if SK_SUPPORT_GPU |
| 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextFactory.h" |
| 12 | #include "GrLayerCache.h" |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 14 | #include "Test.h" |
| 15 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 16 | class TestingAccess { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 17 | public: |
| 18 | static int NumLayers(GrLayerCache* cache) { |
| 19 | return cache->numLayers(); |
| 20 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 21 | static void Purge(GrLayerCache* cache, uint32_t pictureID) { |
| 22 | cache->purge(pictureID); |
| 23 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | // Add several layers to the cache |
| 27 | static void create_layers(skiatest::Reporter* reporter, |
| 28 | GrLayerCache* cache, |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 29 | const SkPicture& picture, |
| 30 | int numToAdd, |
| 31 | int idOffset) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 32 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 33 | for (int i = 0; i < numToAdd; ++i) { |
| 34 | GrCachedLayer* layer = cache->findLayerOrCreate(&picture, idOffset+i); |
| 35 | REPORTER_ASSERT(reporter, NULL != layer); |
| 36 | GrCachedLayer* temp = cache->findLayer(&picture, idOffset+i); |
| 37 | REPORTER_ASSERT(reporter, temp == layer); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 38 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 40 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID()); |
| 42 | REPORTER_ASSERT(reporter, layer->layerID() == idOffset + i); |
| 43 | REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 44 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 45 | } |
| 46 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 47 | cache->trackPicture(&picture); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 48 | } |
| 49 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 50 | static void lock_layer(skiatest::Reporter* reporter, |
| 51 | GrLayerCache* cache, |
| 52 | GrCachedLayer* layer) { |
| 53 | // Make the layer 512x512 (so it can be atlased) |
| 54 | GrTextureDesc desc; |
| 55 | desc.fWidth = 512; |
| 56 | desc.fHeight = 512; |
| 57 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 58 | |
| 59 | bool foundInCache = cache->lock(layer, desc); |
| 60 | REPORTER_ASSERT(reporter, !foundInCache); |
| 61 | |
| 62 | foundInCache = cache->lock(layer, desc); |
| 63 | REPORTER_ASSERT(reporter, foundInCache); |
| 64 | |
| 65 | REPORTER_ASSERT(reporter, NULL != layer->texture()); |
| 66 | REPORTER_ASSERT(reporter, layer->locked()); |
| 67 | } |
| 68 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 69 | // This test case exercises the public API of the GrLayerCache class. |
| 70 | // In particular it checks its interaction with the resource cache (w.r.t. |
| 71 | // locking & unlocking textures). |
| 72 | // TODO: need to add checks on VRAM usage! |
| 73 | DEF_GPUTEST(GpuLayerCache, reporter, factory) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 74 | static const int kInitialNumLayers = 5; |
| 75 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 76 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 77 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 78 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 79 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 80 | continue; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 81 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 82 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 83 | GrContext* context = factory->get(glCtxType); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 84 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 85 | if (NULL == context) { |
| 86 | continue; |
| 87 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 88 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 89 | SkPictureRecorder recorder; |
| 90 | recorder.beginRecording(1, 1); |
| 91 | SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 92 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 93 | GrLayerCache cache(context); |
| 94 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 95 | create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 96 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 97 | for (int i = 0; i < kInitialNumLayers; ++i) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 98 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 99 | REPORTER_ASSERT(reporter, NULL != layer); |
| 100 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 101 | lock_layer(reporter, &cache, layer); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 102 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 103 | // The first 4 layers should be in the atlas (and thus have non-empty |
| 104 | // rects) |
| 105 | if (i < 4) { |
| 106 | REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 107 | } else { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 108 | // The 5th layer couldn't fit in the atlas |
| 109 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 110 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 111 | } |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 112 | |
| 113 | // Unlock the textures |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 114 | for (int i = 0; i < kInitialNumLayers; ++i) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 115 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 116 | REPORTER_ASSERT(reporter, NULL != layer); |
| 117 | |
| 118 | cache.unlock(layer); |
| 119 | } |
| 120 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 121 | for (int i = 0; i < kInitialNumLayers; ++i) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 122 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 123 | REPORTER_ASSERT(reporter, NULL != layer); |
| 124 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 125 | REPORTER_ASSERT(reporter, !layer->locked()); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 126 | // The first 4 layers should still be in the atlas. |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 127 | if (i < 4) { |
| 128 | REPORTER_ASSERT(reporter, NULL != layer->texture()); |
| 129 | REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 130 | } else { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 131 | // The final layer should be unlocked. |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 133 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 134 | } |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 137 | { |
| 138 | // Add an additional layer. Since all the layers are unlocked this |
| 139 | // will force out the first atlased layer |
| 140 | create_layers(reporter, &cache, *picture, 1, kInitialNumLayers); |
| 141 | GrCachedLayer* layer = cache.findLayer(picture, kInitialNumLayers); |
| 142 | REPORTER_ASSERT(reporter, NULL != layer); |
| 143 | |
| 144 | lock_layer(reporter, &cache, layer); |
| 145 | cache.unlock(layer); |
| 146 | } |
| 147 | |
| 148 | for (int i = 0; i < kInitialNumLayers+1; ++i) { |
| 149 | GrCachedLayer* layer = cache.findLayer(picture, i); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 150 | // 3 old layers plus the new one should be in the atlas. |
| 151 | if (1 == i || 2 == i || 3 == i || 5 == i) { |
| 152 | REPORTER_ASSERT(reporter, NULL != layer); |
| 153 | REPORTER_ASSERT(reporter, !layer->locked()); |
| 154 | REPORTER_ASSERT(reporter, NULL != layer->texture()); |
| 155 | REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 156 | } else if (4 == i) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 157 | // The one that was never atlased should still be around |
| 158 | REPORTER_ASSERT(reporter, NULL != layer); |
| 159 | |
| 160 | REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 161 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 162 | } else { |
| 163 | // The one bumped out of the atlas (i.e., 0) should be gone |
| 164 | REPORTER_ASSERT(reporter, NULL == layer); |
| 165 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 166 | } |
| 167 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 168 | //-------------------------------------------------------------------- |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 169 | // Free them all SkGpuDevice-style. This will not free up the |
| 170 | // atlas' texture but will eliminate all the layers. |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 171 | TestingAccess::Purge(&cache, picture->uniqueID()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 172 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 174 | // TODO: add VRAM/resource cache check here |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 175 | |
| 176 | //-------------------------------------------------------------------- |
| 177 | // Test out the GrContext-style purge. This should remove all the layers |
| 178 | // and the atlas. |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 179 | // Re-create the layers |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 180 | create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 181 | |
| 182 | // Free them again GrContext-style. This should free up everything. |
| 183 | cache.freeAll(); |
| 184 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 186 | // TODO: add VRAM/resource cache check here |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 187 | |
| 188 | //-------------------------------------------------------------------- |
| 189 | // Test out the MessageBus-style purge. This will not free the atlas |
| 190 | // but should eliminate the free-floating layers. |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 191 | create_layers(reporter, &cache, *picture, kInitialNumLayers, 0); |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 192 | |
| 193 | picture.reset(NULL); |
| 194 | cache.processDeletedPictures(); |
| 195 | |
| 196 | REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0); |
| 197 | // TODO: add VRAM/resource cache check here |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 198 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #endif |