TextureCache.h revision 272a685f17cc4828257e521a6f62b7b17870f75e
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
25#include "Debug.h"
26
27#include <vector>
28
29namespace android {
30namespace uirenderer {
31
32class Texture;
33
34///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
38// Debug
39#if DEBUG_TEXTURES
40    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
41#else
42    #define TEXTURE_LOGD(...)
43#endif
44
45///////////////////////////////////////////////////////////////////////////////
46// Classes
47///////////////////////////////////////////////////////////////////////////////
48
49class AssetAtlas;
50
51/**
52 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
53 * Any texture added to the cache causing the cache to grow beyond the maximum
54 * allowed size will also cause the oldest texture to be kicked out.
55 */
56class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
57public:
58    TextureCache();
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(void* ownerToken);
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(void* ownerToken, const SkBitmap* bitmap);
78
79    /**
80     * Returns the texture associated with the specified bitmap from either within the cache, or
81     * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
82     */
83    Texture* get(const SkBitmap* bitmap) {
84        return get(bitmap, AtlasUsageType::Use);
85    }
86
87    /**
88     * Returns the texture associated with the specified bitmap. If the texture cannot be found in
89     * the cache, a new texture is generated, even if it resides in the AssetAtlas.
90     */
91    Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
92        return get(bitmap, AtlasUsageType::Bypass);
93    }
94
95    /**
96     * Removes the texture associated with the specified pixelRef. This is meant
97     * to be called from threads that are not the EGL context thread.
98     */
99    ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
100    /**
101     * Process deferred removals.
102     */
103    void clearGarbage();
104
105    /**
106     * Clears the cache. This causes all textures to be deleted.
107     */
108    void clear();
109
110    /**
111     * Sets the maximum size of the cache in bytes.
112     */
113    void setMaxSize(uint32_t maxSize);
114    /**
115     * Returns the maximum size of the cache in bytes.
116     */
117    uint32_t getMaxSize();
118    /**
119     * Returns the current size of the cache in bytes.
120     */
121    uint32_t getSize();
122
123    /**
124     * Partially flushes the cache. The amount of memory freed by a flush
125     * is defined by the flush rate.
126     */
127    void flush();
128    /**
129     * Indicates the percentage of the cache to retain when a
130     * memory trim is requested (see Caches::flush).
131     */
132    void setFlushRate(float flushRate);
133
134    void setAssetAtlas(AssetAtlas* assetAtlas);
135
136private:
137    enum class AtlasUsageType {
138        Use,
139        Bypass,
140    };
141
142    bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
143
144    Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
145    Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
146
147    /**
148     * Generates the texture from a bitmap into the specified texture structure.
149     *
150     * @param regenerate If true, the bitmap data is reuploaded into the texture, but
151     *        no new texture is generated.
152     */
153    void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false);
154
155    void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height);
156    void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
157            GLsizei width, GLsizei height, GLenum type, const GLvoid * data);
158
159    LruCache<uint32_t, Texture*> mCache;
160
161    uint32_t mSize;
162    uint32_t mMaxSize;
163    GLint mMaxTextureSize;
164
165    float mFlushRate;
166
167    bool mDebugEnabled;
168
169    std::vector<uint32_t> mGarbage;
170    mutable Mutex mLock;
171
172    AssetAtlas* mAssetAtlas;
173}; // class TextureCache
174
175}; // namespace uirenderer
176}; // namespace android
177
178#endif // ANDROID_HWUI_TEXTURE_CACHE_H
179