TextureCache.h revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
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#include <unordered_map>
29
30namespace android {
31namespace uirenderer {
32
33class Texture;
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
39// Debug
40#if DEBUG_TEXTURES
41    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
42#else
43    #define TEXTURE_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
47// Classes
48///////////////////////////////////////////////////////////////////////////////
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(void* ownerToken);
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(void* ownerToken, const SkBitmap* bitmap);
77
78    /**
79     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
80     * acquired for the bitmap, false otherwise. Does not mark the Texture
81     * as in use and won't update currently in-use Textures.
82     */
83    bool prefetch(const SkBitmap* bitmap);
84
85    /**
86     * Returns the texture associated with the specified bitmap from within the cache.
87     * If the texture cannot be found in the cache, a new texture is generated.
88     */
89    Texture* get(const SkBitmap* bitmap);
90
91    /**
92     * Removes the texture associated with the specified pixelRef. This is meant
93     * to be called from threads that are not the EGL context thread.
94     */
95    ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
96    /**
97     * Process deferred removals.
98     */
99    void clearGarbage();
100
101    /**
102     * Clears the cache. This causes all textures to be deleted.
103     */
104    void clear();
105
106    /**
107     * Returns the maximum size of the cache in bytes.
108     */
109    uint32_t getMaxSize();
110    /**
111     * Returns the current size of the cache in bytes.
112     */
113    uint32_t getSize();
114
115    /**
116     * Partially flushes the cache. The amount of memory freed by a flush
117     * is defined by the flush rate.
118     */
119    void flush();
120
121private:
122    bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
123
124    Texture* getCachedTexture(const SkBitmap* bitmap);
125
126    LruCache<uint32_t, Texture*> mCache;
127
128    uint32_t mSize;
129    const uint32_t mMaxSize;
130    GLint mMaxTextureSize;
131
132    const float mFlushRate;
133
134    bool mDebugEnabled;
135
136    std::vector<uint32_t> mGarbage;
137    mutable Mutex mLock;
138}; // class TextureCache
139
140}; // namespace uirenderer
141}; // namespace android
142
143#endif // ANDROID_HWUI_TEXTURE_CACHE_H
144