blob: cba331f8b42b3a6da493ca727a95584a3968f80d [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:
robertphillipse99d4992014-12-03 07:33:57 -080018 static unsigned NumLayers(GrLayerCache* cache) {
robertphillips952841b2014-06-30 08:26:50 -070019 return cache->numLayers();
20 }
robertphillipsd771f6b2014-07-22 10:18:06 -070021 static void Purge(GrLayerCache* cache, uint32_t pictureID) {
22 cache->purge(pictureID);
23 }
robertphillips7bb9ed72014-10-10 11:38:29 -070024 static int Uses(GrCachedLayer* layer) {
25 return layer->uses();
26 }
robertphillips01d6e5f2014-12-01 09:09:27 -080027 static GrCachedLayer* Find(GrLayerCache* cache, uint32_t pictureID,
robertphillipse99d4992014-12-03 07:33:57 -080028 const SkMatrix& initialMat,
29 const unsigned* key, int keySize) {
robertphillips01d6e5f2014-12-01 09:09:27 -080030 return cache->findLayer(pictureID, initialMat, key, keySize);
31 }
robertphillips952841b2014-06-30 08:26:50 -070032};
33
34// Add several layers to the cache
35static void create_layers(skiatest::Reporter* reporter,
36 GrLayerCache* cache,
robertphillips320c9232014-07-29 06:07:19 -070037 const SkPicture& picture,
robertphillipse99d4992014-12-03 07:33:57 -080038 unsigned numToAdd,
39 unsigned idOffset) {
robertphillips952841b2014-06-30 08:26:50 -070040
robertphillipse99d4992014-12-03 07:33:57 -080041 for (unsigned i = 0; i < numToAdd; ++i) {
42 unsigned indices[1] = { idOffset+i+1 };
robertphillips6f294af2014-08-18 08:50:03 -070043 GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(),
robertphillips0c423322014-07-31 11:02:38 -070044 idOffset+i+1, idOffset+i+2,
robertphillips3aac6e02014-10-20 08:52:40 -070045 SkIRect::MakeEmpty(),
robertphillips4aa6dfc2014-09-17 07:50:47 -070046 SkMatrix::I(),
robertphillips01d6e5f2014-12-01 09:09:27 -080047 indices, 1,
robertphillips4aa6dfc2014-09-17 07:50:47 -070048 NULL);
bsalomon49f085d2014-09-05 13:34:00 -070049 REPORTER_ASSERT(reporter, layer);
robertphillips01d6e5f2014-12-01 09:09:27 -080050 GrCachedLayer* temp = TestingAccess::Find(cache, picture.uniqueID(), SkMatrix::I(),
51 indices, 1);
robertphillips320c9232014-07-29 06:07:19 -070052 REPORTER_ASSERT(reporter, temp == layer);
robertphillips952841b2014-06-30 08:26:50 -070053
robertphillips320c9232014-07-29 06:07:19 -070054 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1);
robertphillips952841b2014-06-30 08:26:50 -070055
robertphillips320c9232014-07-29 06:07:19 -070056 REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID());
robertphillips0c423322014-07-31 11:02:38 -070057 REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1);
58 REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2);
robertphillips320c9232014-07-29 06:07:19 -070059 REPORTER_ASSERT(reporter, NULL == layer->texture());
robertphillips4aa6dfc2014-09-17 07:50:47 -070060 REPORTER_ASSERT(reporter, NULL == layer->paint());
robertphillips320c9232014-07-29 06:07:19 -070061 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips952841b2014-06-30 08:26:50 -070062 }
robertphillips952841b2014-06-30 08:26:50 -070063}
64
robertphillips320c9232014-07-29 06:07:19 -070065static void lock_layer(skiatest::Reporter* reporter,
66 GrLayerCache* cache,
67 GrCachedLayer* layer) {
68 // Make the layer 512x512 (so it can be atlased)
bsalomonf2703d82014-10-28 14:33:06 -070069 GrSurfaceDesc desc;
robertphillips320c9232014-07-29 06:07:19 -070070 desc.fWidth = 512;
71 desc.fHeight = 512;
72 desc.fConfig = kSkia8888_GrPixelConfig;
73
robertphillipsfd61ed02014-10-28 07:21:44 -070074 bool needsRerendering;
75 bool inAtlas = cache->tryToAtlas(layer, desc, &needsRerendering);
76 if (!inAtlas) {
77 cache->lock(layer, desc, &needsRerendering);
78 }
robertphillips6f294af2014-08-18 08:50:03 -070079 REPORTER_ASSERT(reporter, needsRerendering);
robertphillips320c9232014-07-29 06:07:19 -070080
robertphillipsfd61ed02014-10-28 07:21:44 -070081 cache->lock(layer, desc, &needsRerendering);
robertphillips6f294af2014-08-18 08:50:03 -070082 REPORTER_ASSERT(reporter, !needsRerendering);
robertphillips320c9232014-07-29 06:07:19 -070083
bsalomon49f085d2014-09-05 13:34:00 -070084 REPORTER_ASSERT(reporter, layer->texture());
robertphillips320c9232014-07-29 06:07:19 -070085 REPORTER_ASSERT(reporter, layer->locked());
robertphillips7bb9ed72014-10-10 11:38:29 -070086
87 cache->addUse(layer);
88
89 REPORTER_ASSERT(reporter, 1 == TestingAccess::Uses(layer));
robertphillips320c9232014-07-29 06:07:19 -070090}
91
robertphillips952841b2014-06-30 08:26:50 -070092// This test case exercises the public API of the GrLayerCache class.
93// In particular it checks its interaction with the resource cache (w.r.t.
94// locking & unlocking textures).
95// TODO: need to add checks on VRAM usage!
96DEF_GPUTEST(GpuLayerCache, reporter, factory) {
robertphillips320c9232014-07-29 06:07:19 -070097 static const int kInitialNumLayers = 5;
98
bsalomone904c092014-07-17 10:50:59 -070099 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
100 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
robertphillips952841b2014-06-30 08:26:50 -0700101
bsalomone904c092014-07-17 10:50:59 -0700102 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
103 continue;
robertphillips952841b2014-06-30 08:26:50 -0700104 }
robertphillips952841b2014-06-30 08:26:50 -0700105
bsalomone904c092014-07-17 10:50:59 -0700106 GrContext* context = factory->get(glCtxType);
robertphillips952841b2014-06-30 08:26:50 -0700107
bsalomone904c092014-07-17 10:50:59 -0700108 if (NULL == context) {
109 continue;
110 }
robertphillips952841b2014-06-30 08:26:50 -0700111
bsalomone904c092014-07-17 10:50:59 -0700112 SkPictureRecorder recorder;
113 recorder.beginRecording(1, 1);
114 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
robertphillips952841b2014-06-30 08:26:50 -0700115
bsalomone904c092014-07-17 10:50:59 -0700116 GrLayerCache cache(context);
117
robertphillips320c9232014-07-29 06:07:19 -0700118 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
bsalomone904c092014-07-17 10:50:59 -0700119
robertphillipse99d4992014-12-03 07:33:57 -0800120 for (unsigned i = 0; i < kInitialNumLayers; ++i) {
121 unsigned indices[1] = { i + 1 };
robertphillips01d6e5f2014-12-01 09:09:27 -0800122 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
123 indices, 1);
bsalomon49f085d2014-09-05 13:34:00 -0700124 REPORTER_ASSERT(reporter, layer);
bsalomone904c092014-07-17 10:50:59 -0700125
robertphillips320c9232014-07-29 06:07:19 -0700126 lock_layer(reporter, &cache, layer);
bsalomone904c092014-07-17 10:50:59 -0700127
bsalomone904c092014-07-17 10:50:59 -0700128 // The first 4 layers should be in the atlas (and thus have non-empty
129 // rects)
130 if (i < 4) {
131 REPORTER_ASSERT(reporter, layer->isAtlased());
132 } else {
robertphillips320c9232014-07-29 06:07:19 -0700133 // The 5th layer couldn't fit in the atlas
134 REPORTER_ASSERT(reporter, !layer->isAtlased());
bsalomone904c092014-07-17 10:50:59 -0700135 }
robertphillips952841b2014-06-30 08:26:50 -0700136 }
bsalomone904c092014-07-17 10:50:59 -0700137
138 // Unlock the textures
robertphillipse99d4992014-12-03 07:33:57 -0800139 for (unsigned i = 0; i < kInitialNumLayers; ++i) {
140 unsigned indices[1] = { i+1 };
robertphillips01d6e5f2014-12-01 09:09:27 -0800141
142 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
143 indices, 1);
bsalomon49f085d2014-09-05 13:34:00 -0700144 REPORTER_ASSERT(reporter, layer);
robertphillips7bb9ed72014-10-10 11:38:29 -0700145 cache.removeUse(layer);
bsalomone904c092014-07-17 10:50:59 -0700146 }
147
robertphillipse99d4992014-12-03 07:33:57 -0800148 for (unsigned i = 0; i < kInitialNumLayers; ++i) {
149 unsigned indices[1] = { i+1 };
robertphillips01d6e5f2014-12-01 09:09:27 -0800150
151 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
152 indices, 1);
bsalomon49f085d2014-09-05 13:34:00 -0700153 REPORTER_ASSERT(reporter, layer);
bsalomone904c092014-07-17 10:50:59 -0700154
robertphillips7bb9ed72014-10-10 11:38:29 -0700155 // All the layers should be unlocked
robertphillips320c9232014-07-29 06:07:19 -0700156 REPORTER_ASSERT(reporter, !layer->locked());
robertphillips7bb9ed72014-10-10 11:38:29 -0700157
robertphillips4ab5a902014-10-29 13:56:02 -0700158 // When hoisted layers aren't cached they are aggressively removed
159 // from the atlas
160#if GR_CACHE_HOISTED_LAYERS
robertphillips320c9232014-07-29 06:07:19 -0700161 // The first 4 layers should still be in the atlas.
bsalomone904c092014-07-17 10:50:59 -0700162 if (i < 4) {
bsalomon49f085d2014-09-05 13:34:00 -0700163 REPORTER_ASSERT(reporter, layer->texture());
bsalomone904c092014-07-17 10:50:59 -0700164 REPORTER_ASSERT(reporter, layer->isAtlased());
165 } else {
robertphillips4ab5a902014-10-29 13:56:02 -0700166#endif
robertphillips7bb9ed72014-10-10 11:38:29 -0700167 // The final layer should not be atlased.
bsalomone904c092014-07-17 10:50:59 -0700168 REPORTER_ASSERT(reporter, NULL == layer->texture());
169 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips4ab5a902014-10-29 13:56:02 -0700170#if GR_CACHE_HOISTED_LAYERS
bsalomone904c092014-07-17 10:50:59 -0700171 }
robertphillips4ab5a902014-10-29 13:56:02 -0700172#endif
bsalomone904c092014-07-17 10:50:59 -0700173 }
174
robertphillips320c9232014-07-29 06:07:19 -0700175 {
robertphillipse99d4992014-12-03 07:33:57 -0800176 unsigned indices[1] = { kInitialNumLayers+1 };
robertphillips01d6e5f2014-12-01 09:09:27 -0800177
robertphillips320c9232014-07-29 06:07:19 -0700178 // Add an additional layer. Since all the layers are unlocked this
179 // will force out the first atlased layer
180 create_layers(reporter, &cache, *picture, 1, kInitialNumLayers);
robertphillips01d6e5f2014-12-01 09:09:27 -0800181 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
182 indices, 1);
bsalomon49f085d2014-09-05 13:34:00 -0700183 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700184
185 lock_layer(reporter, &cache, layer);
robertphillips7bb9ed72014-10-10 11:38:29 -0700186 cache.removeUse(layer);
robertphillips320c9232014-07-29 06:07:19 -0700187 }
188
robertphillipse99d4992014-12-03 07:33:57 -0800189 for (unsigned i = 0; i < kInitialNumLayers+1; ++i) {
190 unsigned indices[1] = { i+1 };
robertphillips01d6e5f2014-12-01 09:09:27 -0800191
192 GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
193 indices, 1);
robertphillips4ab5a902014-10-29 13:56:02 -0700194#if GR_CACHE_HOISTED_LAYERS
robertphillips320c9232014-07-29 06:07:19 -0700195 // 3 old layers plus the new one should be in the atlas.
196 if (1 == i || 2 == i || 3 == i || 5 == i) {
bsalomon49f085d2014-09-05 13:34:00 -0700197 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700198 REPORTER_ASSERT(reporter, !layer->locked());
bsalomon49f085d2014-09-05 13:34:00 -0700199 REPORTER_ASSERT(reporter, layer->texture());
robertphillips320c9232014-07-29 06:07:19 -0700200 REPORTER_ASSERT(reporter, layer->isAtlased());
201 } else if (4 == i) {
robertphillips4ab5a902014-10-29 13:56:02 -0700202#endif
robertphillips320c9232014-07-29 06:07:19 -0700203 // The one that was never atlased should still be around
bsalomon49f085d2014-09-05 13:34:00 -0700204 REPORTER_ASSERT(reporter, layer);
robertphillips320c9232014-07-29 06:07:19 -0700205
206 REPORTER_ASSERT(reporter, NULL == layer->texture());
207 REPORTER_ASSERT(reporter, !layer->isAtlased());
robertphillips4ab5a902014-10-29 13:56:02 -0700208#if GR_CACHE_HOISTED_LAYERS
robertphillips320c9232014-07-29 06:07:19 -0700209 } else {
210 // The one bumped out of the atlas (i.e., 0) should be gone
211 REPORTER_ASSERT(reporter, NULL == layer);
212 }
robertphillips4ab5a902014-10-29 13:56:02 -0700213#endif
robertphillips320c9232014-07-29 06:07:19 -0700214 }
215
robertphillipsd771f6b2014-07-22 10:18:06 -0700216 //--------------------------------------------------------------------
bsalomone904c092014-07-17 10:50:59 -0700217 // Free them all SkGpuDevice-style. This will not free up the
218 // atlas' texture but will eliminate all the layers.
robertphillipsd771f6b2014-07-22 10:18:06 -0700219 TestingAccess::Purge(&cache, picture->uniqueID());
bsalomone904c092014-07-17 10:50:59 -0700220
robertphillipsd771f6b2014-07-22 10:18:06 -0700221 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
bsalomone904c092014-07-17 10:50:59 -0700222 // TODO: add VRAM/resource cache check here
robertphillipsd771f6b2014-07-22 10:18:06 -0700223
224 //--------------------------------------------------------------------
225 // Test out the GrContext-style purge. This should remove all the layers
226 // and the atlas.
bsalomone904c092014-07-17 10:50:59 -0700227 // Re-create the layers
robertphillips320c9232014-07-29 06:07:19 -0700228 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
bsalomone904c092014-07-17 10:50:59 -0700229
230 // Free them again GrContext-style. This should free up everything.
231 cache.freeAll();
232
robertphillipsd771f6b2014-07-22 10:18:06 -0700233 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
bsalomone904c092014-07-17 10:50:59 -0700234 // TODO: add VRAM/resource cache check here
robertphillipsd771f6b2014-07-22 10:18:06 -0700235
236 //--------------------------------------------------------------------
237 // Test out the MessageBus-style purge. This will not free the atlas
238 // but should eliminate the free-floating layers.
robertphillips320c9232014-07-29 06:07:19 -0700239 create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
robertphillipsd771f6b2014-07-22 10:18:06 -0700240
241 picture.reset(NULL);
242 cache.processDeletedPictures();
243
244 REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
245 // TODO: add VRAM/resource cache check here
robertphillips952841b2014-06-30 08:26:50 -0700246 }
robertphillips952841b2014-06-30 08:26:50 -0700247}
248
249#endif