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