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