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