blob: 31a2e3d7e81f61c34b5f773176c7e06addcd0861 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_TEXTURE_CACHE_H
18#define ANDROID_HWUI_TEXTURE_CACHE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
20#include <SkBitmap.h>
21
Derek Sollenberger029f6432012-03-05 16:48:32 -050022#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080023#include <utils/Vector.h>
24
Romain Guyc15008e2010-11-10 11:59:15 -080025#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070026#include "Texture.h"
Romain Guy21b028a2010-10-08 18:43:58 -070027#include "utils/GenerationCache.h"
Romain Guyce0537b2010-06-29 21:05:21 -070028
29namespace android {
30namespace uirenderer {
31
Chet Haased98aa2d2010-10-25 15:47:32 -070032///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
36// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070037#if DEBUG_TEXTURES
Steve Block5baa3a62011-12-20 16:23:08 +000038 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
Chet Haased98aa2d2010-10-25 15:47:32 -070039#else
40 #define TEXTURE_LOGD(...)
41#endif
42
Romain Guy9e108412010-11-09 14:35:20 -080043///////////////////////////////////////////////////////////////////////////////
44// Classes
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy121e2242010-07-01 18:26:52 -070047/**
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 */
Romain Guyce0537b2010-06-29 21:05:21 -070052class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
53public:
Romain Guyfb8b7632010-08-23 21:05:08 -070054 TextureCache();
Romain Guy7d139ba2010-07-02 11:20:34 -070055 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070056 ~TextureCache();
57
Romain Guy121e2242010-07-01 18:26:52 -070058 /**
59 * Used as a callback when an entry is removed from the cache.
60 * Do not invoke directly.
61 */
Romain Guydda57022010-07-06 11:39:32 -070062 void operator()(SkBitmap*& bitmap, Texture*& texture);
Romain Guyce0537b2010-06-29 21:05:21 -070063
Romain Guy121e2242010-07-01 18:26:52 -070064 /**
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 */
Romain Guyce0537b2010-06-29 21:05:21 -070068 Texture* get(SkBitmap* bitmap);
Romain Guy121e2242010-07-01 18:26:52 -070069 /**
Romain Guye651cc62012-05-14 19:44:40 -070070 * 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 /**
Romain Guyfe48f652010-11-11 15:36:56 -080075 * Removes the texture associated with the specified bitmap.
76 * Upon remove the texture is freed.
Romain Guy121e2242010-07-01 18:26:52 -070077 */
78 void remove(SkBitmap* bitmap);
79 /**
Romain Guyfe48f652010-11-11 15:36:56 -080080 * 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 /**
Romain Guy121e2242010-07-01 18:26:52 -070090 * Clears the cache. This causes all textures to be deleted.
91 */
Romain Guyce0537b2010-06-29 21:05:21 -070092 void clear();
93
Romain Guy121e2242010-07-01 18:26:52 -070094 /**
95 * Sets the maximum size of the cache in bytes.
96 */
Romain Guy7d139ba2010-07-02 11:20:34 -070097 void setMaxSize(uint32_t maxSize);
Romain Guy121e2242010-07-01 18:26:52 -070098 /**
99 * Returns the maximum size of the cache in bytes.
100 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700101 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -0700102 /**
103 * Returns the current size of the cache in bytes.
104 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700105 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -0700106
Romain Guyeca0ca22011-11-04 15:12:29 -0700107 /**
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
Romain Guyce0537b2010-06-29 21:05:21 -0700118private:
Romain Guy121e2242010-07-01 18:26:52 -0700119 /**
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 */
Romain Guyfe880942010-06-30 16:05:32 -0700125 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -0700126
Romain Guy5b3b3522010-10-27 18:57:51 -0700127 void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
Romain Guy8c749f82010-09-22 14:13:32 -0700128 void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
129 GLenum type, const GLvoid * data);
130
Romain Guyfb8b7632010-08-23 21:05:08 -0700131 void init();
132
Romain Guy6c818932010-07-07 15:15:32 -0700133 GenerationCache<SkBitmap*, Texture*> mCache;
Romain Guy121e2242010-07-01 18:26:52 -0700134
Romain Guy7d139ba2010-07-02 11:20:34 -0700135 uint32_t mSize;
136 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700137 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700138
Romain Guyeca0ca22011-11-04 15:12:29 -0700139 float mFlushRate;
140
Romain Guye190aa62010-11-10 19:01:29 -0800141 bool mDebugEnabled;
142
Romain Guyfe48f652010-11-11 15:36:56 -0800143 Vector<SkBitmap*> mGarbage;
Romain Guy9aaa8262010-09-08 15:15:43 -0700144 mutable Mutex mLock;
Romain Guyce0537b2010-06-29 21:05:21 -0700145}; // class TextureCache
146
147}; // namespace uirenderer
148}; // namespace android
149
Romain Guy5b3b3522010-10-27 18:57:51 -0700150#endif // ANDROID_HWUI_TEXTURE_CACHE_H