Home | History | Annotate | Download | only in hwui
      1 /*
      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_HWUI_LAYER_CACHE_H
     18 #define ANDROID_HWUI_LAYER_CACHE_H
     19 
     20 #include "Debug.h"
     21 #include "Layer.h"
     22 #include "utils/SortedList.h"
     23 
     24 namespace android {
     25 namespace uirenderer {
     26 
     27 class RenderState;
     28 
     29 ///////////////////////////////////////////////////////////////////////////////
     30 // Defines
     31 ///////////////////////////////////////////////////////////////////////////////
     32 
     33 #if DEBUG_LAYERS
     34     #define LAYER_LOGD(...) ALOGD(__VA_ARGS__)
     35 #else
     36     #define LAYER_LOGD(...)
     37 #endif
     38 
     39 ///////////////////////////////////////////////////////////////////////////////
     40 // Cache
     41 ///////////////////////////////////////////////////////////////////////////////
     42 
     43 class LayerCache {
     44 public:
     45     LayerCache();
     46     ~LayerCache();
     47 
     48     /**
     49      * Returns a layer large enough for the specified dimensions. If no suitable
     50      * layer can be found, a new one is created and returned. If creating a new
     51      * layer fails, NULL is returned.
     52      *
     53      * When a layer is obtained from the cache, it is removed and the total
     54      * size of the cache goes down.
     55      *
     56      * @param width The desired width of the layer
     57      * @param height The desired height of the layer
     58      */
     59     Layer* get(RenderState& renderState, const uint32_t width, const uint32_t height);
     60 
     61     /**
     62      * Adds the layer to the cache. The layer will not be added if there is
     63      * not enough space available. Adding a layer can cause other layers to
     64      * be removed from the cache.
     65      *
     66      * @param layer The layer to add to the cache
     67      *
     68      * @return True if the layer was added, false otherwise.
     69      */
     70     bool put(Layer* layer);
     71     /**
     72      * Clears the cache. This causes all layers to be deleted.
     73      */
     74     void clear();
     75 
     76     /**
     77      * Sets the maximum size of the cache in bytes.
     78      */
     79     void setMaxSize(uint32_t maxSize);
     80     /**
     81      * Returns the maximum size of the cache in bytes.
     82      */
     83     uint32_t getMaxSize();
     84     /**
     85      * Returns the current size of the cache in bytes.
     86      */
     87     uint32_t getSize();
     88 
     89     size_t getCount();
     90 
     91     /**
     92      * Prints out the content of the cache.
     93      */
     94     void dump();
     95 
     96 private:
     97     struct LayerEntry {
     98         LayerEntry():
     99             mLayer(nullptr), mWidth(0), mHeight(0) {
    100         }
    101 
    102         LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(nullptr) {
    103             mWidth = Layer::computeIdealWidth(layerWidth);
    104             mHeight = Layer::computeIdealHeight(layerHeight);
    105         }
    106 
    107         LayerEntry(Layer* layer):
    108             mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
    109         }
    110 
    111         static int compare(const LayerEntry& lhs, const LayerEntry& rhs);
    112 
    113         bool operator==(const LayerEntry& other) const {
    114             return compare(*this, other) == 0;
    115         }
    116 
    117         bool operator!=(const LayerEntry& other) const {
    118             return compare(*this, other) != 0;
    119         }
    120 
    121         friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) {
    122             return LayerEntry::compare(lhs, rhs) < 0;
    123         }
    124 
    125         friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) {
    126             return LayerEntry::compare(lhs, rhs);
    127         }
    128 
    129         Layer* mLayer;
    130         uint32_t mWidth;
    131         uint32_t mHeight;
    132     }; // struct LayerEntry
    133 
    134     void deleteLayer(Layer* layer);
    135 
    136     SortedList<LayerEntry> mCache;
    137 
    138     uint32_t mSize;
    139     uint32_t mMaxSize;
    140 }; // class LayerCache
    141 
    142 }; // namespace uirenderer
    143 }; // namespace android
    144 
    145 #endif // ANDROID_HWUI_LAYER_CACHE_H
    146