blob: 8da2b9ea8d28b1a2182fd6d77e006dbe7a5d1f3d [file] [log] [blame]
robertphillips952841b2014-06-30 08:26:50 -07001/*
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 Phillipscfaeec42014-07-13 12:00:50 -040013#include "SkPictureRecorder.h"
robertphillips952841b2014-06-30 08:26:50 -070014#include "Test.h"
15
16static const int kNumLayers = 5;
17
18class GetNumLayers {
19public:
20 static int NumLayers(GrLayerCache* cache) {
21 return cache->numLayers();
22 }
23};
24
25// Add several layers to the cache
26static 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());
robertphillips21048b52014-07-15 19:46:35 -070042 REPORTER_ASSERT(reporter, !layers[i]->isAtlased());
robertphillips952841b2014-06-30 08:26:50 -070043 }
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!
51DEF_GPUTEST(GpuLayerCache, reporter, factory) {
bsalomone904c092014-07-17 10:50:59 -070052 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
53 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
robertphillips952841b2014-06-30 08:26:50 -070054
bsalomone904c092014-07-17 10:50:59 -070055 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
56 continue;
robertphillips952841b2014-06-30 08:26:50 -070057 }
robertphillips952841b2014-06-30 08:26:50 -070058
bsalomone904c092014-07-17 10:50:59 -070059 GrContext* context = factory->get(glCtxType);
robertphillips952841b2014-06-30 08:26:50 -070060
bsalomone904c092014-07-17 10:50:59 -070061 if (NULL == context) {
62 continue;
63 }
robertphillips952841b2014-06-30 08:26:50 -070064
bsalomone904c092014-07-17 10:50:59 -070065 SkPictureRecorder recorder;
66 recorder.beginRecording(1, 1);
67 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
robertphillips952841b2014-06-30 08:26:50 -070068
bsalomone904c092014-07-17 10:50:59 -070069 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
robertphillips952841b2014-06-30 08:26:50 -070088 REPORTER_ASSERT(reporter, NULL != layer->texture());
bsalomone904c092014-07-17 10:50:59 -070089#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 {
robertphillips952841b2014-06-30 08:26:50 -070095#endif
robertphillips21048b52014-07-15 19:46:35 -070096 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips952841b2014-06-30 08:26:50 -070097#if USE_ATLAS
bsalomone904c092014-07-17 10:50:59 -070098 }
99#endif
robertphillips952841b2014-06-30 08:26:50 -0700100 }
bsalomone904c092014-07-17 10:50:59 -0700101
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
robertphillips952841b2014-06-30 08:26:50 -0700144#endif
145 }
robertphillips952841b2014-06-30 08:26:50 -0700146}
147
148#endif