blob: 27693df99b0f9d3657a79c62fc6777ec59e1a749 [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 Guyc15008e2010-11-10 11:59:15 -080022#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070023#include "Texture.h"
Romain Guy21b028a2010-10-08 18:43:58 -070024#include "utils/GenerationCache.h"
Romain Guyce0537b2010-06-29 21:05:21 -070025
26namespace android {
27namespace uirenderer {
28
Chet Haased98aa2d2010-10-25 15:47:32 -070029///////////////////////////////////////////////////////////////////////////////
30// Defines
31///////////////////////////////////////////////////////////////////////////////
32
33// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070034#if DEBUG_TEXTURES
35 #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__)
36#else
37 #define TEXTURE_LOGD(...)
38#endif
39
Romain Guy9e108412010-11-09 14:35:20 -080040///////////////////////////////////////////////////////////////////////////////
41// Classes
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy121e2242010-07-01 18:26:52 -070044/**
45 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
46 * Any texture added to the cache causing the cache to grow beyond the maximum
47 * allowed size will also cause the oldest texture to be kicked out.
48 */
Romain Guyce0537b2010-06-29 21:05:21 -070049class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
50public:
Romain Guyfb8b7632010-08-23 21:05:08 -070051 TextureCache();
Romain Guy7d139ba2010-07-02 11:20:34 -070052 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070053 ~TextureCache();
54
Romain Guy121e2242010-07-01 18:26:52 -070055 /**
56 * Used as a callback when an entry is removed from the cache.
57 * Do not invoke directly.
58 */
Romain Guydda57022010-07-06 11:39:32 -070059 void operator()(SkBitmap*& bitmap, Texture*& texture);
Romain Guyce0537b2010-06-29 21:05:21 -070060
Romain Guy121e2242010-07-01 18:26:52 -070061 /**
62 * Returns the texture associated with the specified bitmap. If the texture
63 * cannot be found in the cache, a new texture is generated.
64 */
Romain Guyce0537b2010-06-29 21:05:21 -070065 Texture* get(SkBitmap* bitmap);
Romain Guy121e2242010-07-01 18:26:52 -070066 /**
67 * Removes the texture associated with the specified bitmap. Returns NULL
68 * if the texture cannot be found. Upon remove the texture is freed.
69 */
70 void remove(SkBitmap* bitmap);
71 /**
72 * Clears the cache. This causes all textures to be deleted.
73 */
Romain Guyce0537b2010-06-29 21:05:21 -070074 void clear();
75
Romain Guy121e2242010-07-01 18:26:52 -070076 /**
77 * Sets the maximum size of the cache in bytes.
78 */
Romain Guy7d139ba2010-07-02 11:20:34 -070079 void setMaxSize(uint32_t maxSize);
Romain Guy121e2242010-07-01 18:26:52 -070080 /**
81 * Returns the maximum size of the cache in bytes.
82 */
Romain Guy7d139ba2010-07-02 11:20:34 -070083 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -070084 /**
85 * Returns the current size of the cache in bytes.
86 */
Romain Guy7d139ba2010-07-02 11:20:34 -070087 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -070088
Romain Guyce0537b2010-06-29 21:05:21 -070089private:
Romain Guy121e2242010-07-01 18:26:52 -070090 /**
91 * Generates the texture from a bitmap into the specified texture structure.
92 *
93 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
94 * no new texture is generated.
95 */
Romain Guyfe880942010-06-30 16:05:32 -070096 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -070097
Romain Guy5b3b3522010-10-27 18:57:51 -070098 void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
Romain Guy8c749f82010-09-22 14:13:32 -070099 void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
100 GLenum type, const GLvoid * data);
101
Romain Guyfb8b7632010-08-23 21:05:08 -0700102 void init();
103
Romain Guy6c818932010-07-07 15:15:32 -0700104 GenerationCache<SkBitmap*, Texture*> mCache;
Romain Guy121e2242010-07-01 18:26:52 -0700105
Romain Guy7d139ba2010-07-02 11:20:34 -0700106 uint32_t mSize;
107 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700108 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700109
Romain Guye190aa62010-11-10 19:01:29 -0800110 bool mDebugEnabled;
111
Romain Guya2341a92010-09-08 18:04:33 -0700112 /**
113 * Used to access mCache and mSize. All methods are accessed from a single
114 * thread except for remove().
115 */
Romain Guy9aaa8262010-09-08 15:15:43 -0700116 mutable Mutex mLock;
Romain Guyce0537b2010-06-29 21:05:21 -0700117}; // class TextureCache
118
119}; // namespace uirenderer
120}; // namespace android
121
Romain Guy5b3b3522010-10-27 18:57:51 -0700122#endif // ANDROID_HWUI_TEXTURE_CACHE_H