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