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