Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 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 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
| 20 | |
| 21 | #include "TextureCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame^] | 22 | #include "Properties.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Constructors/destructor |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame^] | 31 | TextureCache::TextureCache(): |
| 32 | mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), |
| 33 | mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) { |
| 34 | char property[PROPERTY_VALUE_MAX]; |
| 35 | if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { |
| 36 | LOGD(" Setting texture cache size to %sMB", property); |
| 37 | setMaxSize(MB(atof(property))); |
| 38 | } else { |
| 39 | LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); |
| 40 | } |
| 41 | |
| 42 | init(); |
| 43 | } |
| 44 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 45 | TextureCache::TextureCache(uint32_t maxByteSize): |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 46 | mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 47 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame^] | 48 | init(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TextureCache::~TextureCache() { |
| 52 | mCache.clear(); |
| 53 | } |
| 54 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame^] | 55 | void TextureCache::init() { |
| 56 | mCache.setOnEntryRemovedListener(this); |
| 57 | |
| 58 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 59 | LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); |
| 60 | } |
| 61 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | // Size management |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 66 | uint32_t TextureCache::getSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 67 | return mSize; |
| 68 | } |
| 69 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 70 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 71 | return mMaxSize; |
| 72 | } |
| 73 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 74 | void TextureCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 75 | mMaxSize = maxSize; |
| 76 | while (mSize > mMaxSize) { |
| 77 | mCache.removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | // Callbacks |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 85 | void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 86 | if (bitmap) { |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 87 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 88 | mSize -= size; |
| 89 | } |
| 90 | |
| 91 | if (texture) { |
| 92 | glDeleteTextures(1, &texture->id); |
| 93 | delete texture; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | // Caching |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 101 | Texture* TextureCache::get(SkBitmap* bitmap) { |
| 102 | Texture* texture = mCache.get(bitmap); |
| 103 | if (!texture) { |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 104 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
| 105 | LOGW("Bitmap too large to be uploaded into a texture"); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 109 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 110 | // Don't even try to cache a bitmap that's bigger than the cache |
| 111 | if (size < mMaxSize) { |
| 112 | while (mSize + size > mMaxSize) { |
| 113 | mCache.removeOldest(); |
| 114 | } |
| 115 | } |
| 116 | |
Romain Guy | 364703c | 2010-06-30 15:51:03 -0700 | [diff] [blame] | 117 | texture = new Texture; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 118 | generateTexture(bitmap, texture, false); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 119 | |
| 120 | if (size < mMaxSize) { |
| 121 | mSize += size; |
| 122 | mCache.put(bitmap, texture); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 123 | } else { |
| 124 | texture->cleanup = true; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 125 | } |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 126 | } else if (bitmap->getGenerationID() != texture->generation) { |
| 127 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 128 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 129 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 130 | return texture; |
| 131 | } |
| 132 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 133 | void TextureCache::remove(SkBitmap* bitmap) { |
| 134 | mCache.remove(bitmap); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void TextureCache::clear() { |
| 138 | mCache.clear(); |
| 139 | } |
| 140 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 141 | void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 142 | SkAutoLockPixels alp(*bitmap); |
| 143 | if (!bitmap->readyToDraw()) { |
| 144 | LOGE("Cannot generate texture from bitmap"); |
| 145 | return; |
| 146 | } |
| 147 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 148 | if (!regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 149 | texture->generation = bitmap->getGenerationID(); |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 150 | texture->width = bitmap->width(); |
| 151 | texture->height = bitmap->height(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 152 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 153 | glGenTextures(1, &texture->id); |
| 154 | } |
| 155 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 156 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 157 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 158 | |
| 159 | switch (bitmap->getConfig()) { |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 160 | case SkBitmap::kA8_Config: |
| 161 | texture->blend = true; |
| 162 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 163 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 164 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 165 | case SkBitmap::kRGB_565_Config: |
| 166 | texture->blend = false; |
| 167 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 168 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
| 169 | break; |
| 170 | case SkBitmap::kARGB_8888_Config: |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 171 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 172 | GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
Romain Guy | e9e7fd0 | 2010-08-19 14:45:42 -0700 | [diff] [blame] | 173 | // Do this after calling getPixels() to make sure Skia's deferred |
| 174 | // decoding happened |
| 175 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 176 | break; |
| 177 | default: |
| 178 | break; |
| 179 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 180 | |
| 181 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 182 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 183 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 184 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 185 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | }; // namespace uirenderer |
| 189 | }; // namespace android |