TextureCache.h revision efb4b06493fe7b1604c762a448b13c7af2845a8d
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 <cutils/compiler.h>
23
24#include <utils/LruCache.h>
25#include <utils/Mutex.h>
26
27#include "Debug.h"
28
29#include <vector>
30#include <unordered_map>
31
32namespace android {
33
34class Bitmap;
35
36namespace uirenderer {
37
38class Texture;
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
44// Debug
45#if DEBUG_TEXTURES
46    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
47#else
48    #define TEXTURE_LOGD(...)
49#endif
50
51///////////////////////////////////////////////////////////////////////////////
52// Classes
53///////////////////////////////////////////////////////////////////////////////
54
55/**
56 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
57 * Any texture added to the cache causing the cache to grow beyond the maximum
58 * allowed size will also cause the oldest texture to be kicked out.
59 */
60class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
61public:
62    TextureCache();
63    ~TextureCache();
64
65    /**
66     * Used as a callback when an entry is removed from the cache.
67     * Do not invoke directly.
68     */
69    void operator()(uint32_t&, Texture*& texture) override;
70
71    /**
72     * Resets all Textures to not be marked as in use
73     */
74    void resetMarkInUse(void* ownerToken);
75
76    /**
77     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
78     * acquired for the bitmap, false otherwise. If a Texture was acquired it is
79     * marked as in use.
80     */
81    bool prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap);
82
83    /**
84     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
85     * acquired for the bitmap, false otherwise. Does not mark the Texture
86     * as in use and won't update currently in-use Textures.
87     */
88    bool prefetch(Bitmap* bitmap);
89
90    /**
91     * Returns the texture associated with the specified bitmap from within the cache.
92     * If the texture cannot be found in the cache, a new texture is generated.
93     */
94    Texture* get(Bitmap* bitmap);
95
96    /**
97     * Removes the texture associated with the specified pixelRef. This is meant
98     * to be called from threads that are not the EGL context thread.
99     */
100    ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
101    /**
102     * Process deferred removals.
103     */
104    void clearGarbage();
105
106    /**
107     * Clears the cache. This causes all textures to be deleted.
108     */
109    void clear();
110
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
126private:
127    bool canMakeTextureFromBitmap(Bitmap* bitmap);
128
129    Texture* getCachedTexture(Bitmap* bitmap);
130    Texture* createTexture(Bitmap* bitmap);
131
132    LruCache<uint32_t, Texture*> mCache;
133
134    uint32_t mSize;
135    const uint32_t mMaxSize;
136    GLint mMaxTextureSize;
137
138    const float mFlushRate;
139
140    bool mDebugEnabled;
141
142    std::vector<uint32_t> mGarbage;
143    std::unordered_map<uint32_t, std::unique_ptr<Texture>> mHardwareTextures;
144    mutable Mutex mLock;
145}; // class TextureCache
146
147}; // namespace uirenderer
148}; // namespace android
149
150#endif // ANDROID_HWUI_TEXTURE_CACHE_H
151