GradientCache.cpp revision 9fe7e16399aa9739b63ce9add1d04fd8ef00678f
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#include <utils/JenkinsHash.h> 18 19#include "Caches.h" 20#include "Debug.h" 21#include "GradientCache.h" 22#include "Properties.h" 23 24#include <cutils/properties.h> 25 26namespace android { 27namespace uirenderer { 28 29/////////////////////////////////////////////////////////////////////////////// 30// Functions 31/////////////////////////////////////////////////////////////////////////////// 32 33template<typename T> 34static inline T min(T a, T b) { 35 return a < b ? a : b; 36} 37 38/////////////////////////////////////////////////////////////////////////////// 39// Cache entry 40/////////////////////////////////////////////////////////////////////////////// 41 42hash_t GradientCacheEntry::hash() const { 43 uint32_t hash = JenkinsHashMix(0, count); 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 51int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 52 int deltaInt = int(lhs.count) - int(rhs.count); 53 if (deltaInt != 0) return deltaInt; 54 55 deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t)); 56 if (deltaInt != 0) return deltaInt; 57 58 return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float)); 59} 60 61/////////////////////////////////////////////////////////////////////////////// 62// Constructors/destructor 63/////////////////////////////////////////////////////////////////////////////// 64 65GradientCache::GradientCache(Extensions& extensions) 66 : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity) 67 , mSize(0) 68 , mMaxSize(Properties::gradientCacheSize) 69 , mUseFloatTexture(extensions.hasFloatTextures()) 70 , mHasNpot(extensions.hasNPot()) 71 , mHasSRGB(extensions.hasSRGB()) { 72 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 73 74 mCache.setOnEntryRemovedListener(this); 75} 76 77GradientCache::~GradientCache() { 78 mCache.clear(); 79} 80 81/////////////////////////////////////////////////////////////////////////////// 82// Size management 83/////////////////////////////////////////////////////////////////////////////// 84 85uint32_t GradientCache::getSize() { 86 return mSize; 87} 88 89uint32_t GradientCache::getMaxSize() { 90 return mMaxSize; 91} 92 93/////////////////////////////////////////////////////////////////////////////// 94// Callbacks 95/////////////////////////////////////////////////////////////////////////////// 96 97void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) { 98 if (texture) { 99 mSize -= texture->objectSize(); 100 texture->deleteTexture(); 101 delete texture; 102 } 103} 104 105/////////////////////////////////////////////////////////////////////////////// 106// Caching 107/////////////////////////////////////////////////////////////////////////////// 108 109Texture* GradientCache::get(uint32_t* colors, float* positions, int count) { 110 GradientCacheEntry gradient(colors, positions, count); 111 Texture* texture = mCache.get(gradient); 112 113 if (!texture) { 114 texture = addLinearGradient(gradient, colors, positions, count); 115 } 116 117 return texture; 118} 119 120void GradientCache::clear() { 121 mCache.clear(); 122} 123 124void GradientCache::getGradientInfo(const uint32_t* colors, const int count, 125 GradientInfo& info) { 126 uint32_t width = 256 * (count - 1); 127 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)); 133 } 134 135 bool hasAlpha = false; 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 147Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, 148 uint32_t* colors, float* positions, int count) { 149 150 GradientInfo info; 151 getGradientInfo(colors, count, info); 152 153 Texture* texture = new Texture(Caches::getInstance()); 154 texture->blend = info.hasAlpha; 155 texture->generation = 1; 156 157 // Assume the cache is always big enough 158 const uint32_t size = info.width * 2 * bytesPerPixel(); 159 while (getSize() + size > mMaxSize) { 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); 164 } 165 166 generateTexture(colors, positions, info.width, 2, texture); 167 168 mSize += size; 169 LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(), 170 "size != texture->objectSize(), size %" PRIu32 ", objectSize %d" 171 " width = %" PRIu32 " bytesPerPixel() = %zu", 172 size, texture->objectSize(), info.width, bytesPerPixel()); 173 mCache.put(gradient, texture); 174 175 return texture; 176} 177 178size_t GradientCache::bytesPerPixel() const { 179 // We use 4 channels (RGBA) 180 return 4 * (mUseFloatTexture ? /* fp16 */ 2 : sizeof(uint8_t)); 181} 182 183size_t GradientCache::sourceBytesPerPixel() const { 184 // We use 4 channels (RGBA) and upload from floats (not half floats) 185 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t)); 186} 187 188void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end, 189 float amount, uint8_t*& dst) const { 190 float oppAmount = 1.0f - amount; 191 float a = start.a * oppAmount + end.a * amount; 192 *dst++ = uint8_t(a * OECF_sRGB((start.r * oppAmount + end.r * amount)) * 255.0f); 193 *dst++ = uint8_t(a * OECF_sRGB((start.g * oppAmount + end.g * amount)) * 255.0f); 194 *dst++ = uint8_t(a * OECF_sRGB((start.b * oppAmount + end.b * amount)) * 255.0f); 195 *dst++ = uint8_t(a * 255.0f); 196} 197 198void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end, 199 float amount, uint8_t*& dst) const { 200 float oppAmount = 1.0f - amount; 201 float a = start.a * oppAmount + end.a * amount; 202 float* d = (float*) dst; 203#ifdef ANDROID_ENABLE_LINEAR_BLENDING 204 *d++ = a * (start.r * oppAmount + end.r * amount); 205 *d++ = a * (start.g * oppAmount + end.g * amount); 206 *d++ = a * (start.b * oppAmount + end.b * amount); 207#else 208 // What we're doing to the alpha channel here is technically incorrect 209 // but reproduces Android's old behavior when the alpha was pre-multiplied 210 // with gamma-encoded colors 211 a = EOCF_sRGB(a); 212 *d++ = a * OECF_sRGB(start.r * oppAmount + end.r * amount); 213 *d++ = a * OECF_sRGB(start.g * oppAmount + end.g * amount); 214 *d++ = a * OECF_sRGB(start.b * oppAmount + end.b * amount); 215#endif 216 *d++ = a; 217 dst += 4 * sizeof(float); 218} 219 220void GradientCache::generateTexture(uint32_t* colors, float* positions, 221 const uint32_t width, const uint32_t height, Texture* texture) { 222 const GLsizei rowBytes = width * sourceBytesPerPixel(); 223 uint8_t pixels[rowBytes * height]; 224 225 static ChannelMixer gMixers[] = { 226 // colors are stored gamma-encoded 227 &android::uirenderer::GradientCache::mixBytes, 228 // colors are stored in linear (linear blending on) 229 // or gamma-encoded (linear blending off) 230 &android::uirenderer::GradientCache::mixFloats, 231 }; 232 ChannelMixer mix = gMixers[mUseFloatTexture]; 233 234 FloatColor start; 235 start.setUnPreMultipliedSRGB(colors[0]); 236 237 FloatColor end; 238 end.setUnPreMultipliedSRGB(colors[1]); 239 240 int currentPos = 1; 241 float startPos = positions[0]; 242 float distance = positions[1] - startPos; 243 244 uint8_t* dst = pixels; 245 for (uint32_t x = 0; x < width; x++) { 246 float pos = x / float(width - 1); 247 if (pos > positions[currentPos]) { 248 start = end; 249 startPos = positions[currentPos]; 250 251 currentPos++; 252 253 end.setUnPreMultipliedSRGB(colors[currentPos]); 254 distance = positions[currentPos] - startPos; 255 } 256 257 float amount = (pos - startPos) / distance; 258 (this->*mix)(start, end, amount, dst); 259 } 260 261 memcpy(pixels + rowBytes, pixels, rowBytes); 262 263 if (mUseFloatTexture) { 264 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels); 265 } else { 266 GLint internalFormat = mHasSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA; 267 texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 268 } 269 270 texture->setFilter(GL_LINEAR); 271 texture->setWrap(GL_CLAMP_TO_EDGE); 272} 273 274}; // namespace uirenderer 275}; // namespace android 276