blob: ce924b4cbb6fb45dff240afe2291ebd23f984af5 [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
Romain Guyfe48f652010-11-11 15:36:56 -080022#include <utils/Vector.h>
23
Romain Guyc15008e2010-11-10 11:59:15 -080024#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070025#include "Texture.h"
Romain Guy21b028a2010-10-08 18:43:58 -070026#include "utils/GenerationCache.h"
Romain Guyce0537b2010-06-29 21:05:21 -070027
28namespace android {
29namespace uirenderer {
30
Chet Haased98aa2d2010-10-25 15:47:32 -070031///////////////////////////////////////////////////////////////////////////////
32// Defines
33///////////////////////////////////////////////////////////////////////////////
34
35// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070036#if DEBUG_TEXTURES
37 #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__)
38#else
39 #define TEXTURE_LOGD(...)
40#endif
41
Romain Guy9e108412010-11-09 14:35:20 -080042///////////////////////////////////////////////////////////////////////////////
43// Classes
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy121e2242010-07-01 18:26:52 -070046/**
47 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
48 * Any texture added to the cache causing the cache to grow beyond the maximum
49 * allowed size will also cause the oldest texture to be kicked out.
50 */
Romain Guyce0537b2010-06-29 21:05:21 -070051class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
52public:
Romain Guyfb8b7632010-08-23 21:05:08 -070053 TextureCache();
Romain Guy7d139ba2010-07-02 11:20:34 -070054 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070055 ~TextureCache();
56
Romain Guy121e2242010-07-01 18:26:52 -070057 /**
58 * Used as a callback when an entry is removed from the cache.
59 * Do not invoke directly.
60 */
Romain Guydda57022010-07-06 11:39:32 -070061 void operator()(SkBitmap*& bitmap, Texture*& texture);
Romain Guyce0537b2010-06-29 21:05:21 -070062
Romain Guy121e2242010-07-01 18:26:52 -070063 /**
64 * Returns the texture associated with the specified bitmap. If the texture
65 * cannot be found in the cache, a new texture is generated.
66 */
Romain Guyce0537b2010-06-29 21:05:21 -070067 Texture* get(SkBitmap* bitmap);
Romain Guy121e2242010-07-01 18:26:52 -070068 /**
Romain Guyfe48f652010-11-11 15:36:56 -080069 * Removes the texture associated with the specified bitmap.
70 * Upon remove the texture is freed.
Romain Guy121e2242010-07-01 18:26:52 -070071 */
72 void remove(SkBitmap* bitmap);
73 /**
Romain Guyfe48f652010-11-11 15:36:56 -080074 * Removes the texture associated with the specified bitmap. This is meant
75 * to be called from threads that are not the EGL context thread.
76 */
77 void removeDeferred(SkBitmap* bitmap);
78 /**
79 * Process deferred removals.
80 */
81 void clearGarbage();
82
83 /**
Romain Guy121e2242010-07-01 18:26:52 -070084 * Clears the cache. This causes all textures to be deleted.
85 */
Romain Guyce0537b2010-06-29 21:05:21 -070086 void clear();
87
Romain Guy121e2242010-07-01 18:26:52 -070088 /**
89 * Sets the maximum size of the cache in bytes.
90 */
Romain Guy7d139ba2010-07-02 11:20:34 -070091 void setMaxSize(uint32_t maxSize);
Romain Guy121e2242010-07-01 18:26:52 -070092 /**
93 * Returns the maximum size of the cache in bytes.
94 */
Romain Guy7d139ba2010-07-02 11:20:34 -070095 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -070096 /**
97 * Returns the current size of the cache in bytes.
98 */
Romain Guy7d139ba2010-07-02 11:20:34 -070099 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -0700100
Romain Guyeca0ca22011-11-04 15:12:29 -0700101 /**
102 * Partially flushes the cache. The amount of memory freed by a flush
103 * is defined by the flush rate.
104 */
105 void flush();
106 /**
107 * Indicates the percentage of the cache to retain when a
108 * memory trim is requested (see Caches::flush).
109 */
110 void setFlushRate(float flushRate);
111
Romain Guyce0537b2010-06-29 21:05:21 -0700112private:
Romain Guy121e2242010-07-01 18:26:52 -0700113 /**
114 * Generates the texture from a bitmap into the specified texture structure.
115 *
116 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
117 * no new texture is generated.
118 */
Romain Guyfe880942010-06-30 16:05:32 -0700119 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -0700120
Romain Guy5b3b3522010-10-27 18:57:51 -0700121 void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
Romain Guy8c749f82010-09-22 14:13:32 -0700122 void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
123 GLenum type, const GLvoid * data);
124
Romain Guyfb8b7632010-08-23 21:05:08 -0700125 void init();
126
Romain Guy6c818932010-07-07 15:15:32 -0700127 GenerationCache<SkBitmap*, Texture*> mCache;
Romain Guy121e2242010-07-01 18:26:52 -0700128
Romain Guy7d139ba2010-07-02 11:20:34 -0700129 uint32_t mSize;
130 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700131 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700132
Romain Guyeca0ca22011-11-04 15:12:29 -0700133 float mFlushRate;
134
Romain Guye190aa62010-11-10 19:01:29 -0800135 bool mDebugEnabled;
136
Romain Guyfe48f652010-11-11 15:36:56 -0800137 Vector<SkBitmap*> mGarbage;
Romain Guy9aaa8262010-09-08 15:15:43 -0700138 mutable Mutex mLock;
Romain Guyce0537b2010-06-29 21:05:21 -0700139}; // class TextureCache
140
141}; // namespace uirenderer
142}; // namespace android
143
Romain Guy5b3b3522010-10-27 18:57:51 -0700144#endif // ANDROID_HWUI_TEXTURE_CACHE_H