LayerCache.h revision 5f0c6a483900f3989f4d2a8f913cf5b6a9777d03
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_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
26class LayerCache: public OnEntryRemoved<LayerSize, Layer*> {
27public:
28    LayerCache(uint32_t maxByteSize);
29    ~LayerCache();
30
31    /**
32     * Used as a callback when an entry is removed from the cache.
33     * Do not invoke directly.
34     */
35    void operator()(LayerSize& bitmap, Layer*& texture);
36
37    /**
38     * Returns the layer of specified dimensions, NULL if cannot be found.
39     */
40    Layer* get(LayerSize& size);
41    /**
42     * Adds the layer to the cache. The layer will not be added if there is
43     * not enough space available.
44     *
45     * @return True if the layer was added, false otherwise.
46     */
47    bool put(LayerSize& size, Layer* layer);
48    /**
49     * Clears the cache. This causes all layers to be deleted.
50     */
51    void clear();
52
53    /**
54     * Sets the maximum size of the cache in bytes.
55     */
56    void setMaxSize(uint32_t maxSize);
57    /**
58     * Returns the maximum size of the cache in bytes.
59     */
60    uint32_t getMaxSize();
61    /**
62     * Returns the current size of the cache in bytes.
63     */
64    uint32_t getSize();
65
66private:
67    void deleteLayer(Layer* layer);
68
69    GenerationMultiCache<LayerSize, Layer*> mCache;
70
71    uint32_t mSize;
72    uint32_t mMaxSize;
73}; // class LayerCache
74
75}; // namespace uirenderer
76}; // namespace android
77
78#endif // ANDROID_UI_LAYER_CACHE_H
79