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 "DeviceInfo.h" 22 #include "GradientCache.h" 23 #include "Properties.h" 24 25 #include <cutils/properties.h> 26 27 namespace android { 28 namespace uirenderer { 29 30 /////////////////////////////////////////////////////////////////////////////// 31 // Functions 32 /////////////////////////////////////////////////////////////////////////////// 33 34 template <typename T> 35 static inline T min(T a, T b) { 36 return a < b ? a : b; 37 } 38 39 /////////////////////////////////////////////////////////////////////////////// 40 // Cache entry 41 /////////////////////////////////////////////////////////////////////////////// 42 43 hash_t GradientCacheEntry::hash() const { 44 uint32_t hash = JenkinsHashMix(0, count); 45 for (uint32_t i = 0; i < count; i++) { 46 hash = JenkinsHashMix(hash, android::hash_type(colors[i])); 47 hash = JenkinsHashMix(hash, android::hash_type(positions[i])); 48 } 49 return JenkinsHashWhiten(hash); 50 } 51 52 int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 53 int deltaInt = int(lhs.count) - int(rhs.count); 54 if (deltaInt != 0) return deltaInt; 55 56 deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t)); 57 if (deltaInt != 0) return deltaInt; 58 59 return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float)); 60 } 61 62 /////////////////////////////////////////////////////////////////////////////// 63 // Constructors/destructor 64 /////////////////////////////////////////////////////////////////////////////// 65 66 GradientCache::GradientCache(const Extensions& extensions) 67 : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity) 68 , mSize(0) 69 , mMaxSize(MB(1)) 70 , mUseFloatTexture(extensions.hasFloatTextures()) 71 , mHasNpot(extensions.hasNPot()) 72 , mHasLinearBlending(extensions.hasLinearBlending()) { 73 mMaxTextureSize = DeviceInfo::get()->maxTextureSize(); 74 75 mCache.setOnEntryRemovedListener(this); 76 } 77 78 GradientCache::~GradientCache() { 79 mCache.clear(); 80 } 81 82 /////////////////////////////////////////////////////////////////////////////// 83 // Size management 84 /////////////////////////////////////////////////////////////////////////////// 85 86 uint32_t GradientCache::getSize() { 87 return mSize; 88 } 89 90 uint32_t GradientCache::getMaxSize() { 91 return mMaxSize; 92 } 93 94 /////////////////////////////////////////////////////////////////////////////// 95 // Callbacks 96 /////////////////////////////////////////////////////////////////////////////// 97 98 void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) { 99 if (texture) { 100 mSize -= texture->objectSize(); 101 texture->deleteTexture(); 102 delete texture; 103 } 104 } 105 106 /////////////////////////////////////////////////////////////////////////////// 107 // Caching 108 /////////////////////////////////////////////////////////////////////////////// 109 110 Texture* GradientCache::get(uint32_t* colors, float* positions, int count) { 111 GradientCacheEntry gradient(colors, positions, count); 112 Texture* texture = mCache.get(gradient); 113 114 if (!texture) { 115 texture = addLinearGradient(gradient, colors, positions, count); 116 } 117 118 return texture; 119 } 120 121 void GradientCache::clear() { 122 mCache.clear(); 123 } 124 125 void GradientCache::getGradientInfo(const uint32_t* colors, const int count, 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 147 Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, uint32_t* colors, 148 float* positions, int count) { 149 GradientInfo info; 150 getGradientInfo(colors, count, info); 151 152 Texture* texture = new Texture(Caches::getInstance()); 153 texture->blend = info.hasAlpha; 154 texture->generation = 1; 155 156 // Assume the cache is always big enough 157 const uint32_t size = info.width * 2 * bytesPerPixel(); 158 while (getSize() + size > mMaxSize) { 159 LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(), 160 "Ran out of things to remove from the cache? getSize() = %" PRIu32 161 ", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32, 162 getSize(), size, mMaxSize, info.width); 163 } 164 165 generateTexture(colors, positions, info.width, 2, texture); 166 167 mSize += size; 168 LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(), 169 "size != texture->objectSize(), size %" PRIu32 170 ", 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 178 size_t GradientCache::bytesPerPixel() const { 179 // We use 4 channels (RGBA) 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) 185 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t)); 186 } 187 188 void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end, float amount, 189 uint8_t*& dst) const { 190 float oppAmount = 1.0f - amount; 191 float a = start.a * oppAmount + end.a * amount; 192 *dst++ = uint8_t(OECF(start.r * oppAmount + end.r * amount) * 255.0f); 193 *dst++ = uint8_t(OECF(start.g * oppAmount + end.g * amount) * 255.0f); 194 *dst++ = uint8_t(OECF(start.b * oppAmount + end.b * amount) * 255.0f); 195 *dst++ = uint8_t(a * 255.0f); 196 } 197 198 void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end, float amount, 199 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 // We want to stay linear 205 *d++ = (start.r * oppAmount + end.r * amount); 206 *d++ = (start.g * oppAmount + end.g * amount); 207 *d++ = (start.b * oppAmount + end.b * amount); 208 #else 209 *d++ = OECF(start.r * oppAmount + end.r * amount); 210 *d++ = OECF(start.g * oppAmount + end.g * amount); 211 *d++ = OECF(start.b * oppAmount + end.b * amount); 212 #endif 213 *d++ = a; 214 dst += 4 * sizeof(float); 215 } 216 217 void GradientCache::generateTexture(uint32_t* colors, float* positions, const uint32_t width, 218 const uint32_t height, Texture* texture) { 219 const GLsizei rowBytes = width * sourceBytesPerPixel(); 220 uint8_t pixels[rowBytes * height]; 221 222 static ChannelMixer gMixers[] = { 223 // colors are stored gamma-encoded 224 &android::uirenderer::GradientCache::mixBytes, 225 // colors are stored in linear (linear blending on) 226 // or gamma-encoded (linear blending off) 227 &android::uirenderer::GradientCache::mixFloats, 228 }; 229 ChannelMixer mix = gMixers[mUseFloatTexture]; 230 231 FloatColor start; 232 start.set(colors[0]); 233 234 FloatColor end; 235 end.set(colors[1]); 236 237 int currentPos = 1; 238 float startPos = positions[0]; 239 float distance = positions[1] - startPos; 240 241 uint8_t* dst = pixels; 242 for (uint32_t x = 0; x < width; x++) { 243 float pos = x / float(width - 1); 244 if (pos > positions[currentPos]) { 245 start = end; 246 startPos = positions[currentPos]; 247 248 currentPos++; 249 250 end.set(colors[currentPos]); 251 distance = positions[currentPos] - startPos; 252 } 253 254 float amount = (pos - startPos) / distance; 255 (this->*mix)(start, end, amount, dst); 256 } 257 258 memcpy(pixels + rowBytes, pixels, rowBytes); 259 260 if (mUseFloatTexture) { 261 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels); 262 } else { 263 GLint internalFormat = mHasLinearBlending ? GL_SRGB8_ALPHA8 : GL_RGBA; 264 texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 265 } 266 267 texture->setFilter(GL_LINEAR); 268 texture->setWrap(GL_CLAMP_TO_EDGE); 269 } 270 271 }; // namespace uirenderer 272 }; // namespace android 273