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