2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "GrContext.h"
11 #include "GrContextFactory.h"
12 #include "GrLayerCache.h"
13 #include "SkPictureRecorder.h"
18 static int NumLayers(GrLayerCache* cache) {
19 return cache->numLayers();
21 static void Purge(GrLayerCache* cache, uint32_t pictureID) {
22 cache->purge(pictureID);
26 // Add several layers to the cache
27 static void create_layers(skiatest::Reporter* reporter,
29 const SkPicture& picture,
33 for (int i = 0; i < numToAdd; ++i) {
34 GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(),
35 idOffset+i+1, idOffset+i+2,
39 REPORTER_ASSERT(reporter, layer);
40 GrCachedLayer* temp = cache->findLayer(picture.uniqueID(), idOffset+i+1, idOffset+i+2,
41 SkIPoint::Make(0, 0), SkMatrix::I());
42 REPORTER_ASSERT(reporter, temp == layer);
44 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1);
46 REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID());
47 REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1);
48 REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2);
49 REPORTER_ASSERT(reporter, layer->ctm() == SkMatrix::I());
50 REPORTER_ASSERT(reporter, NULL == layer->texture());
51 REPORTER_ASSERT(reporter, NULL == layer->paint());
52 REPORTER_ASSERT(reporter, !layer->isAtlased());
55 cache->trackPicture(&picture);
58 static void lock_layer(skiatest::Reporter* reporter,
60 GrCachedLayer* layer) {
61 // Make the layer 512x512 (so it can be atlased)
65 desc.fConfig = kSkia8888_GrPixelConfig;
67 bool needsRerendering = cache->lock(layer, desc, false);
68 REPORTER_ASSERT(reporter, needsRerendering);
70 needsRerendering = cache->lock(layer, desc, false);
71 REPORTER_ASSERT(reporter, !needsRerendering);
73 REPORTER_ASSERT(reporter, layer->texture());
74 REPORTER_ASSERT(reporter, layer->locked());
77 // This test case exercises the public API of the GrLayerCache class.
78 // In particular it checks its interaction with the resource cache (w.r.t.
79 // locking & unlocking textures).
80 // TODO: need to add checks on VRAM usage!
81 DEF_GPUTEST(GpuLayerCache, reporter, factory) {
82 static const int kInitialNumLayers = 5;
84 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
85 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
87 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
91 GrContext* context = factory->get(glCtxType);
93 if (NULL == context) {
97 SkPictureRecorder recorder;
98 recorder.beginRecording(1, 1);
99 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
101 GrLayerCache cache(context);
103 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
105 for (int i = 0; i < kInitialNumLayers; ++i) {
106 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2,
107 SkIPoint::Make(0, 0), SkMatrix::I());
108 REPORTER_ASSERT(reporter, layer);
110 lock_layer(reporter, &cache, layer);
112 // The first 4 layers should be in the atlas (and thus have non-empty
115 REPORTER_ASSERT(reporter, layer->isAtlased());
117 // The 5th layer couldn't fit in the atlas
118 REPORTER_ASSERT(reporter, !layer->isAtlased());
122 // Unlock the textures
123 for (int i = 0; i < kInitialNumLayers; ++i) {
124 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2,
125 SkIPoint::Make(0, 0), SkMatrix::I());
126 REPORTER_ASSERT(reporter, layer);
130 for (int i = 0; i < kInitialNumLayers; ++i) {
131 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2,
132 SkIPoint::Make(0, 0), SkMatrix::I());
133 REPORTER_ASSERT(reporter, layer);
135 REPORTER_ASSERT(reporter, !layer->locked());
136 // The first 4 layers should still be in the atlas.
138 REPORTER_ASSERT(reporter, layer->texture());
139 REPORTER_ASSERT(reporter, layer->isAtlased());
141 // The final layer should be unlocked.
142 REPORTER_ASSERT(reporter, NULL == layer->texture());
143 REPORTER_ASSERT(reporter, !layer->isAtlased());
148 // Add an additional layer. Since all the layers are unlocked this
149 // will force out the first atlased layer
150 create_layers(reporter, &cache, *picture, 1, kInitialNumLayers);
151 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(),
152 kInitialNumLayers+1, kInitialNumLayers+2,
153 SkIPoint::Make(0, 0), SkMatrix::I());
154 REPORTER_ASSERT(reporter, layer);
156 lock_layer(reporter, &cache, layer);
160 for (int i = 0; i < kInitialNumLayers+1; ++i) {
161 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2,
162 SkIPoint::Make(0, 0), SkMatrix::I());
163 // 3 old layers plus the new one should be in the atlas.
164 if (1 == i || 2 == i || 3 == i || 5 == i) {
165 REPORTER_ASSERT(reporter, layer);
166 REPORTER_ASSERT(reporter, !layer->locked());
167 REPORTER_ASSERT(reporter, layer->texture());
168 REPORTER_ASSERT(reporter, layer->isAtlased());
170 // The one that was never atlased should still be around
171 REPORTER_ASSERT(reporter, layer);
173 REPORTER_ASSERT(reporter, NULL == layer->texture());
174 REPORTER_ASSERT(reporter, !layer->isAtlased());
176 // The one bumped out of the atlas (i.e., 0) should be gone
177 REPORTER_ASSERT(reporter, NULL == layer);
181 //--------------------------------------------------------------------
182 // Free them all SkGpuDevice-style. This will not free up the
183 // atlas' texture but will eliminate all the layers.
184 TestingAccess::Purge(&cache, picture->uniqueID());
186 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
187 // TODO: add VRAM/resource cache check here
189 //--------------------------------------------------------------------
190 // Test out the GrContext-style purge. This should remove all the layers
192 // Re-create the layers
193 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
195 // Free them again GrContext-style. This should free up everything.
198 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
199 // TODO: add VRAM/resource cache check here
201 //--------------------------------------------------------------------
202 // Test out the MessageBus-style purge. This will not free the atlas
203 // but should eliminate the free-floating layers.
204 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
207 cache.processDeletedPictures();
209 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
210 // TODO: add VRAM/resource cache check here