blob: 83f31f5f06f0dbba1cc9e3e2751b61de7475f42c [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
robertphillipsd771f6b2014-07-22 10:18:06 -070016class TestingAccess {
robertphillips952841b2014-06-30 08:26:50 -070017public:
18 static int NumLayers(GrLayerCache* cache) {
19 return cache->numLayers();
20 }
robertphillipsd771f6b2014-07-22 10:18:06 -070021 static void Purge(GrLayerCache* cache, uint32_t pictureID) {
22 cache->purge(pictureID);
23 }
robertphillips952841b2014-06-30 08:26:50 -070024};
25
26// Add several layers to the cache
27static void create_layers(skiatest::Reporter* reporter,
28 GrLayerCache* cache,
robertphillips320c9232014-07-29 06:07:19 -070029 const SkPicture& picture,
30 int numToAdd,
31 int idOffset) {
robertphillips952841b2014-06-30 08:26:50 -070032
robertphillips320c9232014-07-29 06:07:19 -070033 for (int i = 0; i < numToAdd; ++i) {
robertphillips6f294af2014-08-18 08:50:03 -070034 GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(),
robertphillips0c423322014-07-31 11:02:38 -070035 idOffset+i+1, idOffset+i+2,
36 SkMatrix::I());
bsalomon49f085d2014-09-05 13:34:00 -070037 REPORTER_ASSERT(reporter, layer);
robertphillips6f294af2014-08-18 08:50:03 -070038 GrCachedLayer* temp = cache->findLayer(picture.uniqueID(), idOffset+i+1, idOffset+i+2, SkMatrix::I());
robertphillips320c9232014-07-29 06:07:19 -070039 REPORTER_ASSERT(reporter, temp == layer);
robertphillips952841b2014-06-30 08:26:50 -070040
robertphillips320c9232014-07-29 06:07:19 -070041 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1);
robertphillips952841b2014-06-30 08:26:50 -070042
robertphillips320c9232014-07-29 06:07:19 -070043 REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID());
robertphillips0c423322014-07-31 11:02:38 -070044 REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1);
45 REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2);
46 REPORTER_ASSERT(reporter, layer->ctm() == SkMatrix::I());
robertphillips320c9232014-07-29 06:07:19 -070047 REPORTER_ASSERT(reporter, NULL == layer->texture());
48 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips952841b2014-06-30 08:26:50 -070049 }
50
robertphillipsd771f6b2014-07-22 10:18:06 -070051 cache->trackPicture(&picture);
robertphillips952841b2014-06-30 08:26:50 -070052}
53
robertphillips320c9232014-07-29 06:07:19 -070054static void lock_layer(skiatest::Reporter* reporter,
55 GrLayerCache* cache,
56 GrCachedLayer* layer) {
57 // Make the layer 512x512 (so it can be atlased)
58 GrTextureDesc desc;
59 desc.fWidth = 512;
60 desc.fHeight = 512;
61 desc.fConfig = kSkia8888_GrPixelConfig;
62
robertphillips6f294af2014-08-18 08:50:03 -070063 bool needsRerendering = cache->lock(layer, desc, false);
64 REPORTER_ASSERT(reporter, needsRerendering);
robertphillips320c9232014-07-29 06:07:19 -070065
robertphillips6f294af2014-08-18 08:50:03 -070066 needsRerendering = cache->lock(layer, desc, false);
67 REPORTER_ASSERT(reporter, !needsRerendering);
robertphillips320c9232014-07-29 06:07:19 -070068
bsalomon49f085d2014-09-05 13:34:00 -070069 REPORTER_ASSERT(reporter, layer->texture());
robertphillips320c9232014-07-29 06:07:19 -070070 REPORTER_ASSERT(reporter, layer->locked());
71}
72
robertphillips952841b2014-06-30 08:26:50 -070073// This test case exercises the public API of the GrLayerCache class.
74// In particular it checks its interaction with the resource cache (w.r.t.
75// locking & unlocking textures).
76// TODO: need to add checks on VRAM usage!
77DEF_GPUTEST(GpuLayerCache, reporter, factory) {
robertphillips320c9232014-07-29 06:07:19 -070078 static const int kInitialNumLayers = 5;
79
bsalomone904c092014-07-17 10:50:59 -070080 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
81 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
robertphillips952841b2014-06-30 08:26:50 -070082
bsalomone904c092014-07-17 10:50:59 -070083 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
84 continue;
robertphillips952841b2014-06-30 08:26:50 -070085 }
robertphillips952841b2014-06-30 08:26:50 -070086
bsalomone904c092014-07-17 10:50:59 -070087 GrContext* context = factory->get(glCtxType);
robertphillips952841b2014-06-30 08:26:50 -070088
bsalomone904c092014-07-17 10:50:59 -070089 if (NULL == context) {
90 continue;
91 }
robertphillips952841b2014-06-30 08:26:50 -070092
bsalomone904c092014-07-17 10:50:59 -070093 SkPictureRecorder recorder;
94 recorder.beginRecording(1, 1);
95 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
robertphillips952841b2014-06-30 08:26:50 -070096
bsalomone904c092014-07-17 10:50:59 -070097 GrLayerCache cache(context);
98
robertphillips320c9232014-07-29 06:07:19 -070099 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
bsalomone904c092014-07-17 10:50:59 -0700100
robertphillips320c9232014-07-29 06:07:19 -0700101 for (int i = 0; i < kInitialNumLayers; ++i) {
robertphillips6f294af2014-08-18 08:50:03 -0700102 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
bsalomon49f085d2014-09-05 13:34:00 -0700103 REPORTER_ASSERT(reporter, layer);
bsalomone904c092014-07-17 10:50:59 -0700104
robertphillips320c9232014-07-29 06:07:19 -0700105 lock_layer(reporter, &cache, layer);
bsalomone904c092014-07-17 10:50:59 -0700106
bsalomone904c092014-07-17 10:50:59 -0700107 // The first 4 layers should be in the atlas (and thus have non-empty
108 // rects)
109 if (i < 4) {
110 REPORTER_ASSERT(reporter, layer->isAtlased());
111 } else {
robertphillips320c9232014-07-29 06:07:19 -0700112 // The 5th layer couldn't fit in the atlas
113 REPORTER_ASSERT(reporter, !layer->isAtlased());
bsalomone904c092014-07-17 10:50:59 -0700114 }
robertphillips952841b2014-06-30 08:26:50 -0700115 }
bsalomone904c092014-07-17 10:50:59 -0700116
117 // Unlock the textures
robertphillips320c9232014-07-29 06:07:19 -0700118 for (int i = 0; i < kInitialNumLayers; ++i) {
robertphillips6f294af2014-08-18 08:50:03 -0700119 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
bsalomon49f085d2014-09-05 13:34:00 -0700120 REPORTER_ASSERT(reporter, layer);
bsalomone904c092014-07-17 10:50:59 -0700121
122 cache.unlock(layer);
123 }
124
robertphillips320c9232014-07-29 06:07:19 -0700125 for (int i = 0; i < kInitialNumLayers; ++i) {
robertphillips6f294af2014-08-18 08:50:03 -0700126 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
bsalomon49f085d2014-09-05 13:34:00 -0700127 REPORTER_ASSERT(reporter, layer);
bsalomone904c092014-07-17 10:50:59 -0700128
robertphillips320c9232014-07-29 06:07:19 -0700129 REPORTER_ASSERT(reporter, !layer->locked());
robertphillips320c9232014-07-29 06:07:19 -0700130 // The first 4 layers should still be in the atlas.
bsalomone904c092014-07-17 10:50:59 -0700131 if (i < 4) {
bsalomon49f085d2014-09-05 13:34:00 -0700132 REPORTER_ASSERT(reporter, layer->texture());
bsalomone904c092014-07-17 10:50:59 -0700133 REPORTER_ASSERT(reporter, layer->isAtlased());
134 } else {
robertphillips320c9232014-07-29 06:07:19 -0700135 // The final layer should be unlocked.
bsalomone904c092014-07-17 10:50:59 -0700136 REPORTER_ASSERT(reporter, NULL == layer->texture());
137 REPORTER_ASSERT(reporter, !layer->isAtlased());
bsalomone904c092014-07-17 10:50:59 -0700138 }
bsalomone904c092014-07-17 10:50:59 -0700139 }
140
robertphillips320c9232014-07-29 06:07:19 -0700141 {
142 // Add an additional layer. Since all the layers are unlocked this
143 // will force out the first atlased layer
144 create_layers(reporter, &cache, *picture, 1, kInitialNumLayers);
robertphillips6f294af2014-08-18 08:50:03 -0700145 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(),
robertphillips0c423322014-07-31 11:02:38 -0700146 kInitialNumLayers+1, kInitialNumLayers+2,
147 SkMatrix::I());
bsalomon49f085d2014-09-05 13:34:00 -0700148 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700149
150 lock_layer(reporter, &cache, layer);
151 cache.unlock(layer);
152 }
153
154 for (int i = 0; i < kInitialNumLayers+1; ++i) {
robertphillips6f294af2014-08-18 08:50:03 -0700155 GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
robertphillips320c9232014-07-29 06:07:19 -0700156 // 3 old layers plus the new one should be in the atlas.
157 if (1 == i || 2 == i || 3 == i || 5 == i) {
bsalomon49f085d2014-09-05 13:34:00 -0700158 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700159 REPORTER_ASSERT(reporter, !layer->locked());
bsalomon49f085d2014-09-05 13:34:00 -0700160 REPORTER_ASSERT(reporter, layer->texture());
robertphillips320c9232014-07-29 06:07:19 -0700161 REPORTER_ASSERT(reporter, layer->isAtlased());
162 } else if (4 == i) {
robertphillips320c9232014-07-29 06:07:19 -0700163 // The one that was never atlased should still be around
bsalomon49f085d2014-09-05 13:34:00 -0700164 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700165
166 REPORTER_ASSERT(reporter, NULL == layer->texture());
167 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips320c9232014-07-29 06:07:19 -0700168 } else {
169 // The one bumped out of the atlas (i.e., 0) should be gone
170 REPORTER_ASSERT(reporter, NULL == layer);
171 }
robertphillips320c9232014-07-29 06:07:19 -0700172 }
173
robertphillipsd771f6b2014-07-22 10:18:06 -0700174 //--------------------------------------------------------------------
bsalomone904c092014-07-17 10:50:59 -0700175 // Free them all SkGpuDevice-style. This will not free up the
176 // atlas' texture but will eliminate all the layers.
robertphillipsd771f6b2014-07-22 10:18:06 -0700177 TestingAccess::Purge(&cache, picture->uniqueID());
bsalomone904c092014-07-17 10:50:59 -0700178
robertphillipsd771f6b2014-07-22 10:18:06 -0700179 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
bsalomone904c092014-07-17 10:50:59 -0700180 // TODO: add VRAM/resource cache check here
robertphillipsd771f6b2014-07-22 10:18:06 -0700181
182 //--------------------------------------------------------------------
183 // Test out the GrContext-style purge. This should remove all the layers
184 // and the atlas.
bsalomone904c092014-07-17 10:50:59 -0700185 // Re-create the layers
robertphillips320c9232014-07-29 06:07:19 -0700186 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
bsalomone904c092014-07-17 10:50:59 -0700187
188 // Free them again GrContext-style. This should free up everything.
189 cache.freeAll();
190
robertphillipsd771f6b2014-07-22 10:18:06 -0700191 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
bsalomone904c092014-07-17 10:50:59 -0700192 // TODO: add VRAM/resource cache check here
robertphillipsd771f6b2014-07-22 10:18:06 -0700193
194 //--------------------------------------------------------------------
195 // Test out the MessageBus-style purge. This will not free the atlas
196 // but should eliminate the free-floating layers.
robertphillips320c9232014-07-29 06:07:19 -0700197 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
robertphillipsd771f6b2014-07-22 10:18:06 -0700198
199 picture.reset(NULL);
200 cache.processDeletedPictures();
201
202 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
203 // TODO: add VRAM/resource cache check here
robertphillips952841b2014-06-30 08:26:50 -0700204 }
robertphillips952841b2014-06-30 08:26:50 -0700205}
206
207#endif