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 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 24 | #include <cutils/properties.h> |
| 25 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 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 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 55 | 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] | 56 | if (deltaInt != 0) return deltaInt; |
| 57 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 58 | return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float)); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 62 | // Constructors/destructor |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 65 | GradientCache::GradientCache(Extensions& extensions) |
| 66 | : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity) |
| 67 | , mSize(0) |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 68 | , mMaxSize(Properties::gradientCacheSize) |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 69 | , mUseFloatTexture(extensions.hasFloatTextures()) |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 70 | , mHasNpot(extensions.hasNPot()) |
| 71 | , mHasSRGB(extensions.hasSRGB()) { |
Mathias Agopian | a8557d2 | 2012-08-31 19:52:30 -0700 | [diff] [blame] | 72 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 73 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 74 | mCache.setOnEntryRemovedListener(this); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | GradientCache::~GradientCache() { |
| 78 | mCache.clear(); |
| 79 | } |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | // Size management |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | uint32_t GradientCache::getSize() { |
| 86 | return mSize; |
| 87 | } |
| 88 | |
| 89 | uint32_t GradientCache::getMaxSize() { |
| 90 | return mMaxSize; |
| 91 | } |
| 92 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | // Callbacks |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 97 | void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) { |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 98 | if (texture) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 99 | mSize -= texture->objectSize(); |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 100 | texture->deleteTexture(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 101 | delete texture; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | // Caching |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 109 | Texture* GradientCache::get(uint32_t* colors, float* positions, int count) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 110 | GradientCacheEntry gradient(colors, positions, count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 111 | Texture* texture = mCache.get(gradient); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 112 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 113 | if (!texture) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 114 | texture = addLinearGradient(gradient, colors, positions, count); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 115 | } |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 116 | |
| 117 | return texture; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void GradientCache::clear() { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 121 | mCache.clear(); |
| 122 | } |
| 123 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 124 | void GradientCache::getGradientInfo(const uint32_t* colors, const int count, |
| 125 | GradientInfo& info) { |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 126 | uint32_t width = 256 * (count - 1); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 127 | |
Romain Guy | 95aeff8 | 2013-04-12 16:32:05 -0700 | [diff] [blame] | 128 | // If the npot extension is not supported we cannot use non-clamp |
| 129 | // wrap modes. We therefore find the nearest largest power of 2 |
| 130 | // unless width is already a power of 2 |
| 131 | if (!mHasNpot && (width & (width - 1)) != 0) { |
| 132 | width = 1 << (32 - __builtin_clz(width)); |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool hasAlpha = false; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 136 | for (int i = 0; i < count; i++) { |
| 137 | if (((colors[i] >> 24) & 0xff) < 255) { |
| 138 | hasAlpha = true; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | info.width = min(width, uint32_t(mMaxTextureSize)); |
| 144 | info.hasAlpha = hasAlpha; |
| 145 | } |
| 146 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 147 | Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 148 | uint32_t* colors, float* positions, int count) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 149 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 150 | GradientInfo info; |
| 151 | getGradientInfo(colors, count, info); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 152 | |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 153 | Texture* texture = new Texture(Caches::getInstance()); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 154 | texture->blend = info.hasAlpha; |
| 155 | texture->generation = 1; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 156 | |
John Reck | 83c9b5b | 2016-02-05 13:03:47 -0800 | [diff] [blame] | 157 | // Assume the cache is always big enough |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 158 | const uint32_t size = info.width * 2 * bytesPerPixel(); |
Romain Guy | 15a65bf | 2013-01-03 14:22:40 -0800 | [diff] [blame] | 159 | while (getSize() + size > mMaxSize) { |
John Reck | 83c9b5b | 2016-02-05 13:03:47 -0800 | [diff] [blame] | 160 | LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(), |
| 161 | "Ran out of things to remove from the cache? getSize() = %" PRIu32 |
| 162 | ", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32, |
| 163 | getSize(), size, mMaxSize, info.width); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 164 | } |
| 165 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 166 | generateTexture(colors, positions, info.width, 2, texture); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 167 | |
| 168 | mSize += size; |
John Reck | 1d4e6a0 | 2016-02-11 13:22:25 -0800 | [diff] [blame] | 169 | LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(), |
John Reck | d61fd4e | 2016-02-11 14:35:08 -0800 | [diff] [blame] | 170 | "size != texture->objectSize(), size %" PRIu32 ", objectSize %d" |
| 171 | " width = %" PRIu32 " bytesPerPixel() = %zu", |
John Reck | 1d4e6a0 | 2016-02-11 13:22:25 -0800 | [diff] [blame] | 172 | size, texture->objectSize(), info.width, bytesPerPixel()); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 173 | mCache.put(gradient, texture); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 174 | |
| 175 | return texture; |
| 176 | } |
| 177 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 178 | size_t GradientCache::bytesPerPixel() const { |
| 179 | // We use 4 channels (RGBA) |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 180 | return 4 * (mUseFloatTexture ? /* fp16 */ 2 : sizeof(uint8_t)); |
| 181 | } |
| 182 | |
| 183 | size_t GradientCache::sourceBytesPerPixel() const { |
| 184 | // We use 4 channels (RGBA) and upload from floats (not half floats) |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 185 | return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t)); |
| 186 | } |
| 187 | |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 188 | void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end, |
| 189 | float amount, uint8_t*& dst) const { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 190 | float oppAmount = 1.0f - amount; |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 191 | *dst++ = uint8_t(OECF_sRGB(start.r * oppAmount + end.r * amount) * 255.0f); |
| 192 | *dst++ = uint8_t(OECF_sRGB(start.g * oppAmount + end.g * amount) * 255.0f); |
| 193 | *dst++ = uint8_t(OECF_sRGB(start.b * oppAmount + end.b * amount) * 255.0f); |
| 194 | *dst++ = uint8_t( (start.a * oppAmount + end.a * amount) * 255.0f); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 197 | void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end, |
| 198 | float amount, uint8_t*& dst) const { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 199 | float oppAmount = 1.0f - amount; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 200 | float* d = (float*) dst; |
Romain Guy | f9037da | 2016-10-12 18:29:06 -0700 | [diff] [blame] | 201 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 202 | *d++ = start.r * oppAmount + end.r * amount; |
| 203 | *d++ = start.g * oppAmount + end.g * amount; |
| 204 | *d++ = start.b * oppAmount + end.b * amount; |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 205 | #else |
| 206 | *d++ = OECF_sRGB(start.r * oppAmount + end.r * amount); |
| 207 | *d++ = OECF_sRGB(start.g * oppAmount + end.g * amount); |
| 208 | *d++ = OECF_sRGB(start.b * oppAmount + end.b * amount); |
| 209 | #endif |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 210 | *d++ = start.a * oppAmount + end.a * amount; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 211 | dst += 4 * sizeof(float); |
| 212 | } |
| 213 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 214 | void GradientCache::generateTexture(uint32_t* colors, float* positions, |
| 215 | const uint32_t width, const uint32_t height, Texture* texture) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 216 | const GLsizei rowBytes = width * sourceBytesPerPixel(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 217 | uint8_t pixels[rowBytes * height]; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 218 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 219 | static ChannelMixer gMixers[] = { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 220 | &android::uirenderer::GradientCache::mixBytes, // colors are stored gamma-encoded |
| 221 | &android::uirenderer::GradientCache::mixFloats, // colors are stored in linear |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 222 | }; |
| 223 | ChannelMixer mix = gMixers[mUseFloatTexture]; |
| 224 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 225 | FloatColor start; |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 226 | start.setSRGB(colors[0]); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 227 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 228 | FloatColor end; |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 229 | end.setSRGB(colors[1]); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 230 | |
| 231 | int currentPos = 1; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 232 | float startPos = positions[0]; |
| 233 | float distance = positions[1] - startPos; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 234 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 235 | uint8_t* dst = pixels; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 236 | for (uint32_t x = 0; x < width; x++) { |
| 237 | float pos = x / float(width - 1); |
| 238 | if (pos > positions[currentPos]) { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 239 | start = end; |
| 240 | startPos = positions[currentPos]; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 241 | |
| 242 | currentPos++; |
| 243 | |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 244 | end.setSRGB(colors[currentPos]); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 245 | distance = positions[currentPos] - startPos; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 248 | float amount = (pos - startPos) / distance; |
| 249 | (this->*mix)(start, end, amount, dst); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 252 | memcpy(pixels + rowBytes, pixels, rowBytes); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 253 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 254 | if (mUseFloatTexture) { |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 255 | texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 256 | } else { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 257 | GLint internalFormat = mHasSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA; |
| 258 | texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 259 | } |
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 |