Romain Guy | c0ac193 | 2010-07-19 18:43:02 -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 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 19 | #include <utils/JenkinsHash.h> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 20 | #include <utils/threads.h> |
| 21 | |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 22 | #include "Caches.h" |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 23 | #include "Debug.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 24 | #include "GradientCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 31 | // Defines |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | #define GRADIENT_TEXTURE_HEIGHT 2 |
| 35 | #define GRADIENT_BYTES_PER_PIXEL 4 |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | // Functions |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
| 41 | template<typename T> |
| 42 | static inline T min(T a, T b) { |
| 43 | return a < b ? a : b; |
| 44 | } |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 47 | // Cache entry |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | hash_t GradientCacheEntry::hash() const { |
| 51 | uint32_t hash = JenkinsHashMix(0, count); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 52 | for (uint32_t i = 0; i < count; i++) { |
| 53 | hash = JenkinsHashMix(hash, android::hash_type(colors[i])); |
| 54 | hash = JenkinsHashMix(hash, android::hash_type(positions[i])); |
| 55 | } |
| 56 | return JenkinsHashWhiten(hash); |
| 57 | } |
| 58 | |
| 59 | int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { |
| 60 | int deltaInt = int(lhs.count) - int(rhs.count); |
| 61 | if (deltaInt != 0) return deltaInt; |
| 62 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 63 | deltaInt = memcmp(lhs.colors, rhs.colors, lhs.count * sizeof(uint32_t)); |
| 64 | if (deltaInt != 0) return deltaInt; |
| 65 | |
| 66 | return memcmp(lhs.positions, rhs.positions, lhs.count * sizeof(float)); |
| 67 | } |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 70 | // Constructors/destructor |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 73 | GradientCache::GradientCache(): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 74 | mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 75 | mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) { |
| 76 | char property[PROPERTY_VALUE_MAX]; |
| 77 | if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 78 | INIT_LOGD(" Setting gradient cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 79 | setMaxSize(MB(atof(property))); |
| 80 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 81 | INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Mathias Agopian | a8557d2 | 2012-08-31 19:52:30 -0700 | [diff] [blame] | 84 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 85 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 86 | mCache.setOnEntryRemovedListener(this); |
| 87 | } |
| 88 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 89 | GradientCache::GradientCache(uint32_t maxByteSize): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 90 | mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 91 | mSize(0), mMaxSize(maxByteSize) { |
| 92 | mCache.setOnEntryRemovedListener(this); |
| 93 | } |
| 94 | |
| 95 | GradientCache::~GradientCache() { |
| 96 | mCache.clear(); |
| 97 | } |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | // Size management |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | uint32_t GradientCache::getSize() { |
| 104 | return mSize; |
| 105 | } |
| 106 | |
| 107 | uint32_t GradientCache::getMaxSize() { |
| 108 | return mMaxSize; |
| 109 | } |
| 110 | |
| 111 | void GradientCache::setMaxSize(uint32_t maxSize) { |
| 112 | mMaxSize = maxSize; |
| 113 | while (mSize > mMaxSize) { |
| 114 | mCache.removeOldest(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////////////// |
| 119 | // Callbacks |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 122 | void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) { |
| 123 | if (texture) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 124 | const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 125 | mSize -= size; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 126 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 127 | glDeleteTextures(1, &texture->id); |
| 128 | delete texture; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | // Caching |
| 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 136 | Texture* GradientCache::get(uint32_t* colors, float* positions, int count) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 137 | GradientCacheEntry gradient(colors, positions, count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 138 | Texture* texture = mCache.get(gradient); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 139 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 140 | if (!texture) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 141 | texture = addLinearGradient(gradient, colors, positions, count); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 142 | } |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 143 | |
| 144 | return texture; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void GradientCache::clear() { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 148 | mCache.clear(); |
| 149 | } |
| 150 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 151 | void GradientCache::getGradientInfo(const uint32_t* colors, const int count, |
| 152 | GradientInfo& info) { |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 153 | uint32_t width = 256 * (count - 1); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 154 | |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 155 | if (!Caches::getInstance().extensions.hasNPot()) { |
| 156 | width = 1 << (31 - __builtin_clz(width)); |
| 157 | } |
| 158 | |
| 159 | bool hasAlpha = false; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 160 | for (int i = 0; i < count; i++) { |
| 161 | if (((colors[i] >> 24) & 0xff) < 255) { |
| 162 | hasAlpha = true; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | info.width = min(width, uint32_t(mMaxTextureSize)); |
| 168 | info.hasAlpha = hasAlpha; |
| 169 | } |
| 170 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 171 | Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 172 | uint32_t* colors, float* positions, int count) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 173 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 174 | GradientInfo info; |
| 175 | getGradientInfo(colors, count, info); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 176 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 177 | Texture* texture = new Texture; |
| 178 | texture->width = info.width; |
| 179 | texture->height = GRADIENT_TEXTURE_HEIGHT; |
| 180 | texture->blend = info.hasAlpha; |
| 181 | texture->generation = 1; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 182 | |
| 183 | // Asume the cache is always big enough |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 184 | const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL; |
Romain Guy | 15a65bf | 2013-01-03 14:22:40 -0800 | [diff] [blame^] | 185 | while (getSize() + size > mMaxSize) { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 186 | mCache.removeOldest(); |
| 187 | } |
| 188 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 189 | generateTexture(colors, positions, count, texture); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 190 | |
| 191 | mSize += size; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 192 | mCache.put(gradient, texture); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 193 | |
| 194 | return texture; |
| 195 | } |
| 196 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 197 | void GradientCache::generateTexture(uint32_t* colors, float* positions, |
| 198 | int count, Texture* texture) { |
| 199 | |
| 200 | const uint32_t width = texture->width; |
| 201 | const GLsizei rowBytes = width * GRADIENT_BYTES_PER_PIXEL; |
| 202 | uint32_t pixels[width * texture->height]; |
| 203 | |
| 204 | int currentPos = 1; |
| 205 | |
| 206 | float startA = (colors[0] >> 24) & 0xff; |
| 207 | float startR = (colors[0] >> 16) & 0xff; |
| 208 | float startG = (colors[0] >> 8) & 0xff; |
| 209 | float startB = (colors[0] >> 0) & 0xff; |
| 210 | |
| 211 | float endA = (colors[1] >> 24) & 0xff; |
| 212 | float endR = (colors[1] >> 16) & 0xff; |
| 213 | float endG = (colors[1] >> 8) & 0xff; |
| 214 | float endB = (colors[1] >> 0) & 0xff; |
| 215 | |
| 216 | float start = positions[0]; |
| 217 | float distance = positions[1] - start; |
| 218 | |
| 219 | uint8_t* p = (uint8_t*) pixels; |
| 220 | for (uint32_t x = 0; x < width; x++) { |
| 221 | float pos = x / float(width - 1); |
| 222 | if (pos > positions[currentPos]) { |
| 223 | startA = endA; |
| 224 | startR = endR; |
| 225 | startG = endG; |
| 226 | startB = endB; |
| 227 | start = positions[currentPos]; |
| 228 | |
| 229 | currentPos++; |
| 230 | |
| 231 | endA = (colors[currentPos] >> 24) & 0xff; |
| 232 | endR = (colors[currentPos] >> 16) & 0xff; |
| 233 | endG = (colors[currentPos] >> 8) & 0xff; |
| 234 | endB = (colors[currentPos] >> 0) & 0xff; |
| 235 | distance = positions[currentPos] - start; |
| 236 | } |
| 237 | |
| 238 | float amount = (pos - start) / distance; |
| 239 | float oppAmount = 1.0f - amount; |
| 240 | |
Romain Guy | d679b57 | 2012-08-29 21:49:00 -0700 | [diff] [blame] | 241 | const float alpha = startA * oppAmount + endA * amount; |
| 242 | const float a = alpha / 255.0f; |
| 243 | *p++ = uint8_t(a * (startR * oppAmount + endR * amount)); |
| 244 | *p++ = uint8_t(a * (startG * oppAmount + endG * amount)); |
| 245 | *p++ = uint8_t(a * (startB * oppAmount + endB * amount)); |
| 246 | *p++ = uint8_t(alpha); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 249 | for (int i = 1; i < GRADIENT_TEXTURE_HEIGHT; i++) { |
| 250 | memcpy(pixels + width * i, pixels, rowBytes); |
| 251 | } |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 252 | |
| 253 | glGenTextures(1, &texture->id); |
| 254 | |
| 255 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 256 | glPixelStorei(GL_UNPACK_ALIGNMENT, GRADIENT_BYTES_PER_PIXEL); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 257 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 258 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, texture->height, 0, |
| 259 | GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 260 | |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 261 | texture->setFilter(GL_LINEAR); |
| 262 | texture->setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | }; // namespace uirenderer |
| 266 | }; // namespace android |