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