blob: 9860994d1922ce09a60f065aa0c82e12a2aef8be [file] [log] [blame]
Romain Guydda570202010-07-06 11:39:32 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_UI_LAYER_CACHE_H
18#define ANDROID_UI_LAYER_CACHE_H
19
20#include "Layer.h"
21#include "GenerationCache.h"
22
23namespace android {
24namespace uirenderer {
25
Romain Guyf18fd992010-07-08 11:45:51 -070026///////////////////////////////////////////////////////////////////////////////
27// Defines
28///////////////////////////////////////////////////////////////////////////////
29
30// Debug
31#define DEBUG_LAYERS 0
32
33// Debug
34#if DEBUG_LAYERS
35 #define LAYER_LOGD(...) LOGD(__VA_ARGS__)
36#else
37 #define LAYER_LOGD(...)
38#endif
39
40///////////////////////////////////////////////////////////////////////////////
41// Cache
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guydda570202010-07-06 11:39:32 -070044class LayerCache: public OnEntryRemoved<LayerSize, Layer*> {
45public:
Romain Guyfb8b7632010-08-23 21:05:08 -070046 LayerCache();
Romain Guydda570202010-07-06 11:39:32 -070047 LayerCache(uint32_t maxByteSize);
48 ~LayerCache();
49
50 /**
51 * Used as a callback when an entry is removed from the cache.
52 * Do not invoke directly.
53 */
Romain Guyf7f93552010-07-08 19:17:03 -070054 void operator()(LayerSize& size, Layer*& layer);
Romain Guydda570202010-07-06 11:39:32 -070055
56 /**
Romain Guyf18fd992010-07-08 11:45:51 -070057 * Returns the layer of specified dimensions. If not suitable layer
58 * can be found, a new one is created and returned. If creating a new
59 * layer fails, NULL is returned.
60 *
61 * When a layer is obtained from the cache, it is removed and the total
62 * size of the cache goes down.
63 *
64 * @param size The dimensions of the desired layer
65 * @param previousFbo The name of the FBO to bind to if creating a new
66 * layer fails
Romain Guydda570202010-07-06 11:39:32 -070067 */
Romain Guyf18fd992010-07-08 11:45:51 -070068 Layer* get(LayerSize& size, GLuint previousFbo);
Romain Guydda570202010-07-06 11:39:32 -070069 /**
70 * Adds the layer to the cache. The layer will not be added if there is
71 * not enough space available.
72 *
Romain Guyf18fd992010-07-08 11:45:51 -070073 * @param size The dimensions of the layer
74 * @param layer The layer to add to the cache
75 *
Romain Guydda570202010-07-06 11:39:32 -070076 * @return True if the layer was added, false otherwise.
77 */
78 bool put(LayerSize& size, Layer* layer);
79 /**
80 * Clears the cache. This causes all layers to be deleted.
81 */
82 void clear();
83
84 /**
85 * Sets the maximum size of the cache in bytes.
86 */
87 void setMaxSize(uint32_t maxSize);
88 /**
89 * Returns the maximum size of the cache in bytes.
90 */
91 uint32_t getMaxSize();
92 /**
93 * Returns the current size of the cache in bytes.
94 */
95 uint32_t getSize();
96
97private:
98 void deleteLayer(Layer* layer);
99
Romain Guy6c818932010-07-07 15:15:32 -0700100 GenerationCache<LayerSize, Layer*> mCache;
101 uint32_t mIdGenerator;
Romain Guydda570202010-07-06 11:39:32 -0700102
103 uint32_t mSize;
104 uint32_t mMaxSize;
105}; // class LayerCache
106
107}; // namespace uirenderer
108}; // namespace android
109
110#endif // ANDROID_UI_LAYER_CACHE_H