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