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 | // Functions |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | template<typename T> |
| 34 | static inline T min(T a, T b) { |
| 35 | return a < b ? a : b; |
| 36 | } |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 39 | // Cache entry |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | hash_t GradientCacheEntry::hash() const { |
| 43 | uint32_t hash = JenkinsHashMix(0, count); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 44 | for (uint32_t i = 0; i < count; i++) { |
| 45 | hash = JenkinsHashMix(hash, android::hash_type(colors[i])); |
| 46 | hash = JenkinsHashMix(hash, android::hash_type(positions[i])); |
| 47 | } |
| 48 | return JenkinsHashWhiten(hash); |
| 49 | } |
| 50 | |
| 51 | int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { |
| 52 | int deltaInt = int(lhs.count) - int(rhs.count); |
| 53 | if (deltaInt != 0) return deltaInt; |
| 54 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 55 | deltaInt = memcmp(lhs.colors, rhs.colors, lhs.count * sizeof(uint32_t)); |
| 56 | if (deltaInt != 0) return deltaInt; |
| 57 | |
| 58 | return memcmp(lhs.positions, rhs.positions, lhs.count * sizeof(float)); |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 62 | // Constructors/destructor |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 65 | GradientCache::GradientCache(): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 66 | mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 67 | mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) { |
| 68 | char property[PROPERTY_VALUE_MAX]; |
| 69 | if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 70 | INIT_LOGD(" Setting gradient cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 71 | setMaxSize(MB(atof(property))); |
| 72 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 73 | 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] | 74 | } |
| 75 | |
Mathias Agopian | a8557d2 | 2012-08-31 19:52:30 -0700 | [diff] [blame] | 76 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 77 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 78 | mCache.setOnEntryRemovedListener(this); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 79 | |
| 80 | const Extensions& extensions = Extensions::getInstance(); |
Romain Guy | 7f43076 | 2013-06-13 14:29:40 -0700 | [diff] [blame] | 81 | mUseFloatTexture = extensions.hasFloatTextures(); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 82 | mHasNpot = extensions.hasNPot(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 85 | GradientCache::GradientCache(uint32_t maxByteSize): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 86 | mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity), |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 87 | mSize(0), mMaxSize(maxByteSize) { |
| 88 | mCache.setOnEntryRemovedListener(this); |
| 89 | } |
| 90 | |
| 91 | GradientCache::~GradientCache() { |
| 92 | mCache.clear(); |
| 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | // Size management |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | uint32_t GradientCache::getSize() { |
| 100 | return mSize; |
| 101 | } |
| 102 | |
| 103 | uint32_t GradientCache::getMaxSize() { |
| 104 | return mMaxSize; |
| 105 | } |
| 106 | |
| 107 | void GradientCache::setMaxSize(uint32_t maxSize) { |
| 108 | mMaxSize = maxSize; |
| 109 | while (mSize > mMaxSize) { |
| 110 | mCache.removeOldest(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | // Callbacks |
| 116 | /////////////////////////////////////////////////////////////////////////////// |
| 117 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 118 | void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) { |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 119 | if (texture) { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 120 | const uint32_t size = texture->width * texture->height * bytesPerPixel(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 121 | mSize -= size; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 122 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 123 | texture->deleteTexture(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 124 | delete texture; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | // Caching |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 132 | Texture* GradientCache::get(uint32_t* colors, float* positions, int count) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 133 | GradientCacheEntry gradient(colors, positions, count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 134 | Texture* texture = mCache.get(gradient); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 135 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 136 | if (!texture) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 137 | texture = addLinearGradient(gradient, colors, positions, count); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 138 | } |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 139 | |
| 140 | return texture; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void GradientCache::clear() { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 144 | mCache.clear(); |
| 145 | } |
| 146 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 147 | void GradientCache::getGradientInfo(const uint32_t* colors, const int count, |
| 148 | GradientInfo& info) { |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 149 | uint32_t width = 256 * (count - 1); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 150 | |
Romain Guy | 95aeff8 | 2013-04-12 16:32:05 -0700 | [diff] [blame] | 151 | // If the npot extension is not supported we cannot use non-clamp |
| 152 | // wrap modes. We therefore find the nearest largest power of 2 |
| 153 | // unless width is already a power of 2 |
| 154 | if (!mHasNpot && (width & (width - 1)) != 0) { |
| 155 | width = 1 << (32 - __builtin_clz(width)); |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 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 | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 176 | Texture* texture = new Texture(); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 177 | texture->width = info.width; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 178 | texture->height = 2; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 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 | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 183 | const uint32_t size = texture->width * texture->height * bytesPerPixel(); |
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 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 188 | generateTexture(colors, positions, 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 | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 196 | size_t GradientCache::bytesPerPixel() const { |
| 197 | // We use 4 channels (RGBA) |
| 198 | return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t)); |
| 199 | } |
| 200 | |
| 201 | void GradientCache::splitToBytes(uint32_t inColor, GradientColor& outColor) const { |
| 202 | outColor.r = (inColor >> 16) & 0xff; |
| 203 | outColor.g = (inColor >> 8) & 0xff; |
| 204 | outColor.b = (inColor >> 0) & 0xff; |
| 205 | outColor.a = (inColor >> 24) & 0xff; |
| 206 | } |
| 207 | |
| 208 | void GradientCache::splitToFloats(uint32_t inColor, GradientColor& outColor) const { |
| 209 | outColor.r = ((inColor >> 16) & 0xff) / 255.0f; |
| 210 | outColor.g = ((inColor >> 8) & 0xff) / 255.0f; |
| 211 | outColor.b = ((inColor >> 0) & 0xff) / 255.0f; |
| 212 | outColor.a = ((inColor >> 24) & 0xff) / 255.0f; |
| 213 | } |
| 214 | |
| 215 | void GradientCache::mixBytes(GradientColor& start, GradientColor& end, float amount, |
| 216 | uint8_t*& dst) const { |
| 217 | float oppAmount = 1.0f - amount; |
| 218 | const float alpha = start.a * oppAmount + end.a * amount; |
| 219 | const float a = alpha / 255.0f; |
| 220 | |
| 221 | *dst++ = uint8_t(a * (start.r * oppAmount + end.r * amount)); |
| 222 | *dst++ = uint8_t(a * (start.g * oppAmount + end.g * amount)); |
| 223 | *dst++ = uint8_t(a * (start.b * oppAmount + end.b * amount)); |
| 224 | *dst++ = uint8_t(alpha); |
| 225 | } |
| 226 | |
| 227 | void GradientCache::mixFloats(GradientColor& start, GradientColor& end, float amount, |
| 228 | uint8_t*& dst) const { |
| 229 | float oppAmount = 1.0f - amount; |
| 230 | const float a = start.a * oppAmount + end.a * amount; |
| 231 | |
| 232 | float* d = (float*) dst; |
| 233 | *d++ = a * (start.r * oppAmount + end.r * amount); |
| 234 | *d++ = a * (start.g * oppAmount + end.g * amount); |
| 235 | *d++ = a * (start.b * oppAmount + end.b * amount); |
| 236 | *d++ = a; |
| 237 | |
| 238 | dst += 4 * sizeof(float); |
| 239 | } |
| 240 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 241 | void GradientCache::generateTexture(uint32_t* colors, float* positions, Texture* texture) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 242 | const uint32_t width = texture->width; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 243 | const GLsizei rowBytes = width * bytesPerPixel(); |
| 244 | uint8_t pixels[rowBytes * texture->height]; |
| 245 | |
| 246 | static ChannelSplitter gSplitters[] = { |
| 247 | &android::uirenderer::GradientCache::splitToBytes, |
| 248 | &android::uirenderer::GradientCache::splitToFloats, |
| 249 | }; |
| 250 | ChannelSplitter split = gSplitters[mUseFloatTexture]; |
| 251 | |
| 252 | static ChannelMixer gMixers[] = { |
| 253 | &android::uirenderer::GradientCache::mixBytes, |
| 254 | &android::uirenderer::GradientCache::mixFloats, |
| 255 | }; |
| 256 | ChannelMixer mix = gMixers[mUseFloatTexture]; |
| 257 | |
| 258 | GradientColor start; |
| 259 | (this->*split)(colors[0], start); |
| 260 | |
| 261 | GradientColor end; |
| 262 | (this->*split)(colors[1], end); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 263 | |
| 264 | int currentPos = 1; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 265 | float startPos = positions[0]; |
| 266 | float distance = positions[1] - startPos; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 267 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 268 | uint8_t* dst = pixels; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 269 | for (uint32_t x = 0; x < width; x++) { |
| 270 | float pos = x / float(width - 1); |
| 271 | if (pos > positions[currentPos]) { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 272 | start = end; |
| 273 | startPos = positions[currentPos]; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 274 | |
| 275 | currentPos++; |
| 276 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 277 | (this->*split)(colors[currentPos], end); |
| 278 | distance = positions[currentPos] - startPos; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 281 | float amount = (pos - startPos) / distance; |
| 282 | (this->*mix)(start, end, amount, dst); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 285 | memcpy(pixels + rowBytes, pixels, rowBytes); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 286 | |
| 287 | glGenTextures(1, &texture->id); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 288 | Caches::getInstance().bindTexture(texture->id); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 289 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 290 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 291 | if (mUseFloatTexture) { |
| 292 | // We have to use GL_RGBA16F because GL_RGBA32F does not support filtering |
| 293 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, texture->height, 0, |
| 294 | GL_RGBA, GL_FLOAT, pixels); |
| 295 | } else { |
| 296 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, texture->height, 0, |
| 297 | GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 298 | } |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 299 | |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 300 | texture->setFilter(GL_LINEAR); |
| 301 | texture->setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | }; // namespace uirenderer |
| 305 | }; // namespace android |