TextureCache.h revision e84a208317e0ed388fcdad1e6743c7849acb51b0
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
48class AssetAtlas;
49
50/**
51 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
52 * Any texture added to the cache causing the cache to grow beyond the maximum
53 * allowed size will also cause the oldest texture to be kicked out.
54 */
55class TextureCache: public OnEntryRemoved<uint32_t, Texture*> {
56public:
57    TextureCache();
58    TextureCache(uint32_t maxByteSize);
59    ~TextureCache();
60
61    /**
62     * Used as a callback when an entry is removed from the cache.
63     * Do not invoke directly.
64     */
65    void operator()(uint32_t&, Texture*& texture) override;
66
67    /**
68     * Resets all Textures to not be marked as in use
69     */
70    void resetMarkInUse();
71
72    /**
73     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
74     * acquired for the bitmap, false otherwise. If a Texture was acquired it is
75     * marked as in use.
76     */
77    bool prefetchAndMarkInUse(const SkBitmap* bitmap);
78
79    /**
80     * Returns the texture associated with the specified bitmap. If the texture
81     * cannot be found in the cache, a new texture is generated.
82     */
83    Texture* get(const SkBitmap* bitmap);
84    /**
85     * Returns the texture associated with the specified bitmap. The generated
86     * texture is not kept in the cache. The caller must destroy the texture.
87     */
88    Texture* getTransient(const SkBitmap* bitmap);
89
90    /**
91     * Removes the texture associated with the specified bitmap. This is meant
92     * to be called from threads that are not the EGL context thread.
93     */
94    void releaseTexture(const SkBitmap* bitmap);
95    /**
96     * Process deferred removals.
97     */
98    void clearGarbage();
99
100    /**
101     * Clears the cache. This causes all textures to be deleted.
102     */
103    void clear();
104
105    /**
106     * Sets the maximum size of the cache in bytes.
107     */
108    void setMaxSize(uint32_t maxSize);
109    /**
110     * Returns the maximum size of the cache in bytes.
111     */
112    uint32_t getMaxSize();
113    /**
114     * Returns the current size of the cache in bytes.
115     */
116    uint32_t getSize();
117
118    /**
119     * Partially flushes the cache. The amount of memory freed by a flush
120     * is defined by the flush rate.
121     */
122    void flush();
123    /**
124     * Indicates the percentage of the cache to retain when a
125     * memory trim is requested (see Caches::flush).
126     */
127    void setFlushRate(float flushRate);
128
129    void setAssetAtlas(AssetAtlas* assetAtlas);
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<uint32_t, 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<uint32_t> mGarbage;
162    mutable Mutex mLock;
163
164    AssetAtlas* mAssetAtlas;
165}; // class TextureCache
166
167}; // namespace uirenderer
168}; // namespace android
169
170#endif // ANDROID_HWUI_TEXTURE_CACHE_H
171