blob: 668defc716a54436bdc4d2cace6f488ec90d039a [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 Guy059e12c2012-11-28 17:35:51 -080022#include <utils/LruCache.h>
Derek Sollenberger029f6432012-03-05 16:48:32 -050023#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080024#include <utils/Vector.h>
25
Romain Guyc15008e2010-11-10 11:59:15 -080026#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070027
28namespace android {
29namespace uirenderer {
30
Tom Hudson2dc236b2014-10-15 15:46:42 -040031class Texture;
32
Chet Haased98aa2d2010-10-25 15:47:32 -070033///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070038#if DEBUG_TEXTURES
Steve Block5baa3a62011-12-20 16:23:08 +000039 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
Chet Haased98aa2d2010-10-25 15:47:32 -070040#else
41 #define TEXTURE_LOGD(...)
42#endif
43
Romain Guy9e108412010-11-09 14:35:20 -080044///////////////////////////////////////////////////////////////////////////////
45// Classes
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy121e22422010-07-01 18:26:52 -070048/**
49 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
50 * Any texture added to the cache causing the cache to grow beyond the maximum
51 * allowed size will also cause the oldest texture to be kicked out.
52 */
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040053class TextureCache: public OnEntryRemoved<const SkPixelRef*, Texture*> {
Romain Guyce0537b2010-06-29 21:05:21 -070054public:
Romain Guyfb8b7632010-08-23 21:05:08 -070055 TextureCache();
Romain Guy7d139ba2010-07-02 11:20:34 -070056 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070057 ~TextureCache();
58
Romain Guy121e22422010-07-01 18:26:52 -070059 /**
60 * Used as a callback when an entry is removed from the cache.
61 * Do not invoke directly.
62 */
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040063 void operator()(const SkPixelRef*& pixelRef, Texture*& texture);
Romain Guyce0537b2010-06-29 21:05:21 -070064
Romain Guy121e22422010-07-01 18:26:52 -070065 /**
John Reck860d1552014-04-11 19:15:05 -070066 * Resets all Textures to not be marked as in use
67 */
68 void resetMarkInUse();
69
70 /**
71 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
72 * acquired for the bitmap, false otherwise. If a Texture was acquired it is
73 * marked as in use.
74 */
75 bool prefetchAndMarkInUse(const SkBitmap* bitmap);
76
77 /**
Romain Guy121e22422010-07-01 18:26:52 -070078 * Returns the texture associated with the specified bitmap. If the texture
79 * cannot be found in the cache, a new texture is generated.
80 */
Chris Craikd218a922014-01-02 17:13:34 -080081 Texture* get(const SkBitmap* bitmap);
Romain Guy121e22422010-07-01 18:26:52 -070082 /**
Romain Guye651cc62012-05-14 19:44:40 -070083 * Returns the texture associated with the specified bitmap. The generated
84 * texture is not kept in the cache. The caller must destroy the texture.
85 */
Chris Craikd218a922014-01-02 17:13:34 -080086 Texture* getTransient(const SkBitmap* bitmap);
Romain Guye651cc62012-05-14 19:44:40 -070087 /**
Romain Guyfe48f652010-11-11 15:36:56 -080088 * Removes the texture associated with the specified bitmap.
89 * Upon remove the texture is freed.
Romain Guy121e22422010-07-01 18:26:52 -070090 */
Chris Craikd218a922014-01-02 17:13:34 -080091 void remove(const SkBitmap* bitmap);
Romain Guy121e22422010-07-01 18:26:52 -070092 /**
Romain Guyfe48f652010-11-11 15:36:56 -080093 * Removes the texture associated with the specified bitmap. This is meant
94 * to be called from threads that are not the EGL context thread.
95 */
Chris Craikd218a922014-01-02 17:13:34 -080096 void removeDeferred(const SkBitmap* bitmap);
Romain Guyfe48f652010-11-11 15:36:56 -080097 /**
98 * Process deferred removals.
99 */
100 void clearGarbage();
101
102 /**
Romain Guy121e22422010-07-01 18:26:52 -0700103 * Clears the cache. This causes all textures to be deleted.
104 */
Romain Guyce0537b2010-06-29 21:05:21 -0700105 void clear();
106
Romain Guy121e22422010-07-01 18:26:52 -0700107 /**
108 * Sets the maximum size of the cache in bytes.
109 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700110 void setMaxSize(uint32_t maxSize);
Romain Guy121e22422010-07-01 18:26:52 -0700111 /**
112 * Returns the maximum size of the cache in bytes.
113 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700114 uint32_t getMaxSize();
Romain Guy121e22422010-07-01 18:26:52 -0700115 /**
116 * Returns the current size of the cache in bytes.
117 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700118 uint32_t getSize();
Romain Guy121e22422010-07-01 18:26:52 -0700119
Romain Guyeca0ca22011-11-04 15:12:29 -0700120 /**
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 /**
126 * Indicates the percentage of the cache to retain when a
127 * memory trim is requested (see Caches::flush).
128 */
129 void setFlushRate(float flushRate);
130
Romain Guyce0537b2010-06-29 21:05:21 -0700131private:
John Reck860d1552014-04-11 19:15:05 -0700132
133 bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
134
135 Texture* getCachedTexture(const SkBitmap* bitmap);
136
Romain Guy121e22422010-07-01 18:26:52 -0700137 /**
138 * Generates the texture from a bitmap into the specified texture structure.
139 *
140 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
141 * no new texture is generated.
142 */
Chris Craikd218a922014-01-02 17:13:34 -0800143 void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -0700144
Chris Craikd218a922014-01-02 17:13:34 -0800145 void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height);
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800146 void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700147 GLsizei width, GLsizei height, GLenum type, const GLvoid * data);
Romain Guy8c749f82010-09-22 14:13:32 -0700148
Romain Guyfb8b7632010-08-23 21:05:08 -0700149 void init();
150
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400151 LruCache<const SkPixelRef*, Texture*> mCache;
Romain Guy121e22422010-07-01 18:26:52 -0700152
Romain Guy7d139ba2010-07-02 11:20:34 -0700153 uint32_t mSize;
154 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700155 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700156
Romain Guyeca0ca22011-11-04 15:12:29 -0700157 float mFlushRate;
158
Romain Guye190aa62010-11-10 19:01:29 -0800159 bool mDebugEnabled;
160
Chris Craikd218a922014-01-02 17:13:34 -0800161 Vector<const SkBitmap*> mGarbage;
Romain Guy9aaa8262010-09-08 15:15:43 -0700162 mutable Mutex mLock;
Romain Guyce0537b2010-06-29 21:05:21 -0700163}; // class TextureCache
164
165}; // namespace uirenderer
166}; // namespace android
167
Romain Guy5b3b3522010-10-27 18:57:51 -0700168#endif // ANDROID_HWUI_TEXTURE_CACHE_H