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