1dda570201ac851dd85af3861f7e575721d3345daRomain Guy/*
2dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Copyright (C) 2010 The Android Open Source Project
3dda570201ac851dd85af3861f7e575721d3345daRomain Guy *
4dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5dda570201ac851dd85af3861f7e575721d3345daRomain Guy * you may not use this file except in compliance with the License.
6dda570201ac851dd85af3861f7e575721d3345daRomain Guy * You may obtain a copy of the License at
7dda570201ac851dd85af3861f7e575721d3345daRomain Guy *
8dda570201ac851dd85af3861f7e575721d3345daRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9dda570201ac851dd85af3861f7e575721d3345daRomain Guy *
10dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Unless required by applicable law or agreed to in writing, software
11dda570201ac851dd85af3861f7e575721d3345daRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12dda570201ac851dd85af3861f7e575721d3345daRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dda570201ac851dd85af3861f7e575721d3345daRomain Guy * See the License for the specific language governing permissions and
14dda570201ac851dd85af3861f7e575721d3345daRomain Guy * limitations under the License.
15dda570201ac851dd85af3861f7e575721d3345daRomain Guy */
16dda570201ac851dd85af3861f7e575721d3345daRomain Guy
175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#ifndef ANDROID_HWUI_LAYER_CACHE_H
185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#define ANDROID_HWUI_LAYER_CACHE_H
19dda570201ac851dd85af3861f7e575721d3345daRomain Guy
20c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#include "Debug.h"
21dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "Layer.h"
228550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy#include "utils/SortedList.h"
23dda570201ac851dd85af3861f7e575721d3345daRomain Guy
24dda570201ac851dd85af3861f7e575721d3345daRomain Guynamespace android {
25dda570201ac851dd85af3861f7e575721d3345daRomain Guynamespace uirenderer {
26dda570201ac851dd85af3861f7e575721d3345daRomain Guy
27f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy///////////////////////////////////////////////////////////////////////////////
28f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy// Defines
29f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy///////////////////////////////////////////////////////////////////////////////
30f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy
31f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy// Debug
32f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy#if DEBUG_LAYERS
335baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    #define LAYER_LOGD(...) ALOGD(__VA_ARGS__)
34f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy#else
35f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy    #define LAYER_LOGD(...)
36f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy#endif
37f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy
38f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy///////////////////////////////////////////////////////////////////////////////
39f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy// Cache
40f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy///////////////////////////////////////////////////////////////////////////////
41f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy
428550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guyclass LayerCache {
43dda570201ac851dd85af3861f7e575721d3345daRomain Guypublic:
44fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    LayerCache();
45dda570201ac851dd85af3861f7e575721d3345daRomain Guy    ~LayerCache();
46dda570201ac851dd85af3861f7e575721d3345daRomain Guy
47dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
488550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy     * Returns a layer large enough for the specified dimensions. If no suitable
498550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy     * layer can be found, a new one is created and returned. If creating a new
50f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     * layer fails, NULL is returned.
51f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     *
52f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     * When a layer is obtained from the cache, it is removed and the total
53f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     * size of the cache goes down.
54f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     *
558550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy     * @param width The desired width of the layer
568d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy     * @param height The desired height of the layer
57dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
588550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    Layer* get(const uint32_t width, const uint32_t height);
59eb99356a0548684a501766e6a524529ab93304c8Romain Guy
60dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
61dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * Adds the layer to the cache. The layer will not be added if there is
628550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy     * not enough space available. Adding a layer can cause other layers to
638550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy     * be removed from the cache.
64dda570201ac851dd85af3861f7e575721d3345daRomain Guy     *
65f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     * @param layer The layer to add to the cache
66f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy     *
67dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * @return True if the layer was added, false otherwise.
68dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
698550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    bool put(Layer* layer);
70dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
71dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * Clears the cache. This causes all layers to be deleted.
72dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
73dda570201ac851dd85af3861f7e575721d3345daRomain Guy    void clear();
74dda570201ac851dd85af3861f7e575721d3345daRomain Guy
75dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
76dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * Sets the maximum size of the cache in bytes.
77dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
78dda570201ac851dd85af3861f7e575721d3345daRomain Guy    void setMaxSize(uint32_t maxSize);
79dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
80dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * Returns the maximum size of the cache in bytes.
81dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
82dda570201ac851dd85af3861f7e575721d3345daRomain Guy    uint32_t getMaxSize();
83dda570201ac851dd85af3861f7e575721d3345daRomain Guy    /**
84dda570201ac851dd85af3861f7e575721d3345daRomain Guy     * Returns the current size of the cache in bytes.
85dda570201ac851dd85af3861f7e575721d3345daRomain Guy     */
86dda570201ac851dd85af3861f7e575721d3345daRomain Guy    uint32_t getSize();
87dda570201ac851dd85af3861f7e575721d3345daRomain Guy
88eea60692b060737faeaa02bb30f5b79e2202b482Romain Guy    /**
89eea60692b060737faeaa02bb30f5b79e2202b482Romain Guy     * Prints out the content of the cache.
90eea60692b060737faeaa02bb30f5b79e2202b482Romain Guy     */
91eea60692b060737faeaa02bb30f5b79e2202b482Romain Guy    void dump();
92eea60692b060737faeaa02bb30f5b79e2202b482Romain Guy
938d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guyprivate:
948550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    struct LayerEntry {
958550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        LayerEntry():
968550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy            mLayer(NULL), mWidth(0), mHeight(0) {
978550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        }
988550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
998550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
1002055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            mWidth = Layer::computeIdealWidth(layerWidth);
1012055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            mHeight = Layer::computeIdealHeight(layerHeight);
1028550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        }
1038550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
1048550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        LayerEntry(Layer* layer):
1059ace8f5e79e76893fe4ca9e4d10f6c4056330485Romain Guy            mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
1068550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        }
1078550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
108e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy        static int compare(const LayerEntry& lhs, const LayerEntry& rhs);
109e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy
110e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy        bool operator==(const LayerEntry& other) const {
111e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy            return compare(*this, other) == 0;
1128550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        }
1138550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
114e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy        bool operator!=(const LayerEntry& other) const {
115e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy            return compare(*this, other) != 0;
1168550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        }
1178550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
1188d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy        friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) {
1198d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy            return LayerEntry::compare(lhs, rhs) < 0;
1208d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy        }
1218d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy
1228d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy        friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) {
1238d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy            return LayerEntry::compare(lhs, rhs);
1248d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy        }
1258d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy
1268550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        Layer* mLayer;
1278550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        uint32_t mWidth;
1288550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy        uint32_t mHeight;
1298550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    }; // struct LayerEntry
1308550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy
131e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy    void deleteLayer(Layer* layer);
132e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy
1338550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    SortedList<LayerEntry> mCache;
134dda570201ac851dd85af3861f7e575721d3345daRomain Guy
135dda570201ac851dd85af3861f7e575721d3345daRomain Guy    uint32_t mSize;
136dda570201ac851dd85af3861f7e575721d3345daRomain Guy    uint32_t mMaxSize;
137dda570201ac851dd85af3861f7e575721d3345daRomain Guy}; // class LayerCache
138dda570201ac851dd85af3861f7e575721d3345daRomain Guy
139dda570201ac851dd85af3861f7e575721d3345daRomain Guy}; // namespace uirenderer
140dda570201ac851dd85af3861f7e575721d3345daRomain Guy}; // namespace android
141dda570201ac851dd85af3861f7e575721d3345daRomain Guy
1425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_LAYER_CACHE_H
143