TextureCache.h revision 5baa3a62a97544669fba6d65a11c07f252e654dd
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_TEXTURE_CACHE_H
18#define ANDROID_HWUI_TEXTURE_CACHE_H
19
20#include <SkBitmap.h>
21
22#include <utils/Vector.h>
23
24#include "Debug.h"
25#include "Texture.h"
26#include "utils/GenerationCache.h"
27
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Defines
33///////////////////////////////////////////////////////////////////////////////
34
35// Debug
36#if DEBUG_TEXTURES
37    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
38#else
39    #define TEXTURE_LOGD(...)
40#endif
41
42///////////////////////////////////////////////////////////////////////////////
43// Classes
44///////////////////////////////////////////////////////////////////////////////
45
46/**
47 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
48 * Any texture added to the cache causing the cache to grow beyond the maximum
49 * allowed size will also cause the oldest texture to be kicked out.
50 */
51class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
52public:
53    TextureCache();
54    TextureCache(uint32_t maxByteSize);
55    ~TextureCache();
56
57    /**
58     * Used as a callback when an entry is removed from the cache.
59     * Do not invoke directly.
60     */
61    void operator()(SkBitmap*& bitmap, Texture*& texture);
62
63    /**
64     * Returns the texture associated with the specified bitmap. If the texture
65     * cannot be found in the cache, a new texture is generated.
66     */
67    Texture* get(SkBitmap* bitmap);
68    /**
69     * Removes the texture associated with the specified bitmap.
70     * Upon remove the texture is freed.
71     */
72    void remove(SkBitmap* bitmap);
73    /**
74     * Removes the texture associated with the specified bitmap. This is meant
75     * to be called from threads that are not the EGL context thread.
76     */
77    void removeDeferred(SkBitmap* bitmap);
78    /**
79     * Process deferred removals.
80     */
81    void clearGarbage();
82
83    /**
84     * Clears the cache. This causes all textures to be deleted.
85     */
86    void clear();
87
88    /**
89     * Sets the maximum size of the cache in bytes.
90     */
91    void setMaxSize(uint32_t maxSize);
92    /**
93     * Returns the maximum size of the cache in bytes.
94     */
95    uint32_t getMaxSize();
96    /**
97     * Returns the current size of the cache in bytes.
98     */
99    uint32_t getSize();
100
101    /**
102     * Partially flushes the cache. The amount of memory freed by a flush
103     * is defined by the flush rate.
104     */
105    void flush();
106    /**
107     * Indicates the percentage of the cache to retain when a
108     * memory trim is requested (see Caches::flush).
109     */
110    void setFlushRate(float flushRate);
111
112private:
113    /**
114     * Generates the texture from a bitmap into the specified texture structure.
115     *
116     * @param regenerate If true, the bitmap data is reuploaded into the texture, but
117     *        no new texture is generated.
118     */
119    void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
120
121    void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
122    void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
123            GLenum type, const GLvoid * data);
124
125    void init();
126
127    GenerationCache<SkBitmap*, Texture*> mCache;
128
129    uint32_t mSize;
130    uint32_t mMaxSize;
131    GLint mMaxTextureSize;
132
133    float mFlushRate;
134
135    bool mDebugEnabled;
136
137    Vector<SkBitmap*> mGarbage;
138    mutable Mutex mLock;
139}; // class TextureCache
140
141}; // namespace uirenderer
142}; // namespace android
143
144#endif // ANDROID_HWUI_TEXTURE_CACHE_H
145