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 | |
| 16 | static const int kNumLayers = 5; |
| 17 | |
| 18 | class GetNumLayers { |
| 19 | public: |
| 20 | static int NumLayers(GrLayerCache* cache) { |
| 21 | return cache->numLayers(); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | // Add several layers to the cache |
| 26 | static void create_layers(skiatest::Reporter* reporter, |
| 27 | GrLayerCache* cache, |
| 28 | const SkPicture& picture) { |
| 29 | GrCachedLayer* layers[kNumLayers]; |
| 30 | |
| 31 | for (int i = 0; i < kNumLayers; ++i) { |
| 32 | layers[i] = cache->findLayerOrCreate(&picture, i); |
| 33 | REPORTER_ASSERT(reporter, NULL != layers[i]); |
| 34 | GrCachedLayer* layer = cache->findLayer(&picture, i); |
| 35 | REPORTER_ASSERT(reporter, layer == layers[i]); |
| 36 | |
| 37 | REPORTER_ASSERT(reporter, GetNumLayers::NumLayers(cache) == i+1); |
| 38 | |
| 39 | REPORTER_ASSERT(reporter, picture.uniqueID() == layers[i]->pictureID()); |
| 40 | REPORTER_ASSERT(reporter, layers[i]->layerID() == i); |
| 41 | REPORTER_ASSERT(reporter, NULL == layers[i]->texture()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, !layers[i]->isAtlased()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | } |
| 46 | |
| 47 | // This test case exercises the public API of the GrLayerCache class. |
| 48 | // In particular it checks its interaction with the resource cache (w.r.t. |
| 49 | // locking & unlocking textures). |
| 50 | // TODO: need to add checks on VRAM usage! |
| 51 | DEF_GPUTEST(GpuLayerCache, reporter, factory) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 52 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 53 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 54 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 55 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 56 | continue; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 57 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 58 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 59 | GrContext* context = factory->get(glCtxType); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 60 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 61 | if (NULL == context) { |
| 62 | continue; |
| 63 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 64 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 65 | SkPictureRecorder recorder; |
| 66 | recorder.beginRecording(1, 1); |
| 67 | SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 68 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 69 | GrLayerCache cache(context); |
| 70 | |
| 71 | create_layers(reporter, &cache, *picture); |
| 72 | |
| 73 | // Lock the layers making them all 512x512 |
| 74 | GrTextureDesc desc; |
| 75 | desc.fWidth = 512; |
| 76 | desc.fHeight = 512; |
| 77 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 78 | |
| 79 | for (int i = 0; i < kNumLayers; ++i) { |
| 80 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 81 | REPORTER_ASSERT(reporter, NULL != layer); |
| 82 | |
| 83 | bool foundInCache = cache.lock(layer, desc); |
| 84 | REPORTER_ASSERT(reporter, !foundInCache); |
| 85 | foundInCache = cache.lock(layer, desc); |
| 86 | REPORTER_ASSERT(reporter, foundInCache); |
| 87 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, NULL != layer->texture()); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 89 | #if USE_ATLAS |
| 90 | // The first 4 layers should be in the atlas (and thus have non-empty |
| 91 | // rects) |
| 92 | if (i < 4) { |
| 93 | REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 94 | } else { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 95 | #endif |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 97 | #if USE_ATLAS |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 98 | } |
| 99 | #endif |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 100 | } |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 101 | |
| 102 | // Unlock the textures |
| 103 | for (int i = 0; i < kNumLayers; ++i) { |
| 104 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 105 | REPORTER_ASSERT(reporter, NULL != layer); |
| 106 | |
| 107 | cache.unlock(layer); |
| 108 | } |
| 109 | |
| 110 | for (int i = 0; i < kNumLayers; ++i) { |
| 111 | GrCachedLayer* layer = cache.findLayer(picture, i); |
| 112 | REPORTER_ASSERT(reporter, NULL != layer); |
| 113 | |
| 114 | #if USE_ATLAS |
| 115 | // The first 4 layers should be in the atlas (and thus do not |
| 116 | // currently unlock). The final layer should be unlocked. |
| 117 | if (i < 4) { |
| 118 | REPORTER_ASSERT(reporter, NULL != layer->texture()); |
| 119 | REPORTER_ASSERT(reporter, layer->isAtlased()); |
| 120 | } else { |
| 121 | #endif |
| 122 | REPORTER_ASSERT(reporter, NULL == layer->texture()); |
| 123 | REPORTER_ASSERT(reporter, !layer->isAtlased()); |
| 124 | #if USE_ATLAS |
| 125 | } |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | // Free them all SkGpuDevice-style. This will not free up the |
| 130 | // atlas' texture but will eliminate all the layers. |
| 131 | cache.purge(picture); |
| 132 | |
| 133 | REPORTER_ASSERT(reporter, GetNumLayers::NumLayers(&cache) == 0); |
| 134 | // TODO: add VRAM/resource cache check here |
| 135 | #if 0 |
| 136 | // Re-create the layers |
| 137 | create_layers(reporter, &cache, picture); |
| 138 | |
| 139 | // Free them again GrContext-style. This should free up everything. |
| 140 | cache.freeAll(); |
| 141 | |
| 142 | REPORTER_ASSERT(reporter, GetNumLayers::NumLayers(&cache) == 0); |
| 143 | // TODO: add VRAM/resource cache check here |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 144 | #endif |
| 145 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #endif |