TextureCache.h revision 6ad690e16f8e139bfd29a035b52ab616d813a74b
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();
59
60    /**
61     * Used as a callback when an entry is removed from the cache.
62     * Do not invoke directly.
63     */
64    void operator()(uint32_t&, Texture*& texture) override;
65
66    /**
67     * Resets all Textures to not be marked as in use
68     */
69    void resetMarkInUse();
70
71    /**
72     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
73     * acquired for the bitmap, false otherwise. If a Texture was acquired it is
74     * marked as in use.
75     */
76    bool prefetchAndMarkInUse(const SkBitmap* bitmap);
77
78    /**
79     * Returns the texture associated with the specified bitmap from either within the cache, or
80     * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
81     */
82    Texture* get(const SkBitmap* bitmap) {
83        return get(bitmap, AtlasUsageType::Use);
84    }
85
86    /**
87     * Returns the texture associated with the specified bitmap. If the texture cannot be found in
88     * the cache, a new texture is generated, even if it resides in the AssetAtlas.
89     */
90    Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
91        return get(bitmap, AtlasUsageType::Bypass);
92    }
93
94    /**
95     * Removes the texture associated with the specified pixelRef. This is meant
96     * to be called from threads that are not the EGL context thread.
97     */
98    ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
99    /**
100     * Process deferred removals.
101     */
102    void clearGarbage();
103
104    /**
105     * Clears the cache. This causes all textures to be deleted.
106     */
107    void clear();
108
109    /**
110     * Sets the maximum size of the cache in bytes.
111     */
112    void setMaxSize(uint32_t maxSize);
113    /**
114     * Returns the maximum size of the cache in bytes.
115     */
116    uint32_t getMaxSize();
117    /**
118     * Returns the current size of the cache in bytes.
119     */
120    uint32_t getSize();
121
122    /**
123     * Partially flushes the cache. The amount of memory freed by a flush
124     * is defined by the flush rate.
125     */
126    void flush();
127    /**
128     * Indicates the percentage of the cache to retain when a
129     * memory trim is requested (see Caches::flush).
130     */
131    void setFlushRate(float flushRate);
132
133    void setAssetAtlas(AssetAtlas* assetAtlas);
134
135private:
136    enum class AtlasUsageType {
137        Use,
138        Bypass,
139    };
140
141    bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
142
143    Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
144    Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
145
146    /**
147     * Generates the texture from a bitmap into the specified texture structure.
148     *
149     * @param regenerate If true, the bitmap data is reuploaded into the texture, but
150     *        no new texture is generated.
151     */
152    void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false);
153
154    void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height);
155    void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
156            GLsizei width, GLsizei height, GLenum type, const GLvoid * data);
157
158    LruCache<uint32_t, Texture*> mCache;
159
160    uint32_t mSize;
161    uint32_t mMaxSize;
162    GLint mMaxTextureSize;
163
164    float mFlushRate;
165
166    bool mDebugEnabled;
167
168    Vector<uint32_t> mGarbage;
169    mutable Mutex mLock;
170
171    AssetAtlas* mAssetAtlas;
172}; // class TextureCache
173
174}; // namespace uirenderer
175}; // namespace android
176
177#endif // ANDROID_HWUI_TEXTURE_CACHE_H
178