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/Mutex.h>
23#include <utils/Vector.h>
24
25#include "Debug.h"
26#include "Texture.h"
27#include "utils/GenerationCache.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
36// Debug
37#if DEBUG_TEXTURES
38    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
39#else
40    #define TEXTURE_LOGD(...)
41#endif
42
43///////////////////////////////////////////////////////////////////////////////
44// Classes
45///////////////////////////////////////////////////////////////////////////////
46
47/**
48 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
49 * Any texture added to the cache causing the cache to grow beyond the maximum
50 * allowed size will also cause the oldest texture to be kicked out.
51 */
52class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
53public:
54    TextureCache();
55    TextureCache(uint32_t maxByteSize);
56    ~TextureCache();
57
58    /**
59     * Used as a callback when an entry is removed from the cache.
60     * Do not invoke directly.
61     */
62    void operator()(SkBitmap*& bitmap, Texture*& texture);
63
64    /**
65     * Returns the texture associated with the specified bitmap. If the texture
66     * cannot be found in the cache, a new texture is generated.
67     */
68    Texture* get(SkBitmap* bitmap);
69    /**
70     * Returns the texture associated with the specified bitmap. The generated
71     * texture is not kept in the cache. The caller must destroy the texture.
72     */
73    Texture* getTransient(SkBitmap* bitmap);
74    /**
75     * Removes the texture associated with the specified bitmap.
76     * Upon remove the texture is freed.
77     */
78    void remove(SkBitmap* bitmap);
79    /**
80     * Removes the texture associated with the specified bitmap. This is meant
81     * to be called from threads that are not the EGL context thread.
82     */
83    void removeDeferred(SkBitmap* bitmap);
84    /**
85     * Process deferred removals.
86     */
87    void clearGarbage();
88
89    /**
90     * Clears the cache. This causes all textures to be deleted.
91     */
92    void clear();
93
94    /**
95     * Sets the maximum size of the cache in bytes.
96     */
97    void setMaxSize(uint32_t maxSize);
98    /**
99     * Returns the maximum size of the cache in bytes.
100     */
101    uint32_t getMaxSize();
102    /**
103     * Returns the current size of the cache in bytes.
104     */
105    uint32_t getSize();
106
107    /**
108     * Partially flushes the cache. The amount of memory freed by a flush
109     * is defined by the flush rate.
110     */
111    void flush();
112    /**
113     * Indicates the percentage of the cache to retain when a
114     * memory trim is requested (see Caches::flush).
115     */
116    void setFlushRate(float flushRate);
117
118private:
119    /**
120     * Generates the texture from a bitmap into the specified texture structure.
121     *
122     * @param regenerate If true, the bitmap data is reuploaded into the texture, but
123     *        no new texture is generated.
124     */
125    void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
126
127    void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
128    void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
129            GLenum type, const GLvoid * data);
130
131    void init();
132
133    GenerationCache<SkBitmap*, Texture*> mCache;
134
135    uint32_t mSize;
136    uint32_t mMaxSize;
137    GLint mMaxTextureSize;
138
139    float mFlushRate;
140
141    bool mDebugEnabled;
142
143    Vector<SkBitmap*> mGarbage;
144    mutable Mutex mLock;
145}; // class TextureCache
146
147}; // namespace uirenderer
148}; // namespace android
149
150#endif // ANDROID_HWUI_TEXTURE_CACHE_H
151