2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <utils/JenkinsHash.h>
21 #include "GradientCache.h"
22 #include "Properties.h"
24 #include <cutils/properties.h>
27 namespace uirenderer {
29 ///////////////////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////////////////
34 static inline T min(T a, T b) {
38 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
42 hash_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]));
48 return JenkinsHashWhiten(hash);
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;
55 deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t));
56 if (deltaInt != 0) return deltaInt;
58 return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float));
61 ///////////////////////////////////////////////////////////////////////////////
62 // Constructors/destructor
63 ///////////////////////////////////////////////////////////////////////////////
65 GradientCache::GradientCache(Extensions& extensions)
66 : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity)
68 , mMaxSize(Properties::gradientCacheSize)
69 , mUseFloatTexture(extensions.hasFloatTextures())
70 , mHasNpot(extensions.hasNPot()){
71 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
73 mCache.setOnEntryRemovedListener(this);
76 GradientCache::~GradientCache() {
80 ///////////////////////////////////////////////////////////////////////////////
82 ///////////////////////////////////////////////////////////////////////////////
84 uint32_t GradientCache::getSize() {
88 uint32_t GradientCache::getMaxSize() {
92 ///////////////////////////////////////////////////////////////////////////////
94 ///////////////////////////////////////////////////////////////////////////////
96 void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) {
98 mSize -= texture->objectSize();
99 texture->deleteTexture();
104 ///////////////////////////////////////////////////////////////////////////////
106 ///////////////////////////////////////////////////////////////////////////////
108 Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
109 GradientCacheEntry gradient(colors, positions, count);
110 Texture* texture = mCache.get(gradient);
113 texture = addLinearGradient(gradient, colors, positions, count);
119 void GradientCache::clear() {
123 void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
124 GradientInfo& info) {
125 uint32_t width = 256 * (count - 1);
127 // If the npot extension is not supported we cannot use non-clamp
128 // wrap modes. We therefore find the nearest largest power of 2
129 // unless width is already a power of 2
130 if (!mHasNpot && (width & (width - 1)) != 0) {
131 width = 1 << (32 - __builtin_clz(width));
134 bool hasAlpha = false;
135 for (int i = 0; i < count; i++) {
136 if (((colors[i] >> 24) & 0xff) < 255) {
142 info.width = min(width, uint32_t(mMaxTextureSize));
143 info.hasAlpha = hasAlpha;
146 Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
147 uint32_t* colors, float* positions, int count) {
150 getGradientInfo(colors, count, info);
152 Texture* texture = new Texture(Caches::getInstance());
153 texture->blend = info.hasAlpha;
154 texture->generation = 1;
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);
165 generateTexture(colors, positions, info.width, 2, texture);
168 LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(),
169 "size != texture->objectSize(), size %" PRIu32 ", objectSize %d"
170 " width = %" PRIu32 " bytesPerPixel() = %zu",
171 size, texture->objectSize(), info.width, bytesPerPixel());
172 mCache.put(gradient, texture);
177 size_t GradientCache::bytesPerPixel() const {
178 // We use 4 channels (RGBA)
179 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
182 void GradientCache::splitToBytes(uint32_t inColor, GradientColor& outColor) const {
183 outColor.r = (inColor >> 16) & 0xff;
184 outColor.g = (inColor >> 8) & 0xff;
185 outColor.b = (inColor >> 0) & 0xff;
186 outColor.a = (inColor >> 24) & 0xff;
189 void GradientCache::splitToFloats(uint32_t inColor, GradientColor& outColor) const {
190 outColor.r = ((inColor >> 16) & 0xff) / 255.0f;
191 outColor.g = ((inColor >> 8) & 0xff) / 255.0f;
192 outColor.b = ((inColor >> 0) & 0xff) / 255.0f;
193 outColor.a = ((inColor >> 24) & 0xff) / 255.0f;
196 void GradientCache::mixBytes(GradientColor& start, GradientColor& end, float amount,
197 uint8_t*& dst) const {
198 float oppAmount = 1.0f - amount;
199 const float alpha = start.a * oppAmount + end.a * amount;
200 const float a = alpha / 255.0f;
202 *dst++ = uint8_t(a * (start.r * oppAmount + end.r * amount));
203 *dst++ = uint8_t(a * (start.g * oppAmount + end.g * amount));
204 *dst++ = uint8_t(a * (start.b * oppAmount + end.b * amount));
205 *dst++ = uint8_t(alpha);
208 void GradientCache::mixFloats(GradientColor& start, GradientColor& end, float amount,
209 uint8_t*& dst) const {
210 float oppAmount = 1.0f - amount;
211 const float a = start.a * oppAmount + end.a * amount;
213 float* d = (float*) dst;
214 *d++ = a * (start.r * oppAmount + end.r * amount);
215 *d++ = a * (start.g * oppAmount + end.g * amount);
216 *d++ = a * (start.b * oppAmount + end.b * amount);
219 dst += 4 * sizeof(float);
222 void GradientCache::generateTexture(uint32_t* colors, float* positions,
223 const uint32_t width, const uint32_t height, Texture* texture) {
224 const GLsizei rowBytes = width * bytesPerPixel();
225 uint8_t pixels[rowBytes * height];
227 static ChannelSplitter gSplitters[] = {
228 &android::uirenderer::GradientCache::splitToBytes,
229 &android::uirenderer::GradientCache::splitToFloats,
231 ChannelSplitter split = gSplitters[mUseFloatTexture];
233 static ChannelMixer gMixers[] = {
234 &android::uirenderer::GradientCache::mixBytes,
235 &android::uirenderer::GradientCache::mixFloats,
237 ChannelMixer mix = gMixers[mUseFloatTexture];
240 (this->*split)(colors[0], start);
243 (this->*split)(colors[1], end);
246 float startPos = positions[0];
247 float distance = positions[1] - startPos;
249 uint8_t* dst = pixels;
250 for (uint32_t x = 0; x < width; x++) {
251 float pos = x / float(width - 1);
252 if (pos > positions[currentPos]) {
254 startPos = positions[currentPos];
258 (this->*split)(colors[currentPos], end);
259 distance = positions[currentPos] - startPos;
262 float amount = (pos - startPos) / distance;
263 (this->*mix)(start, end, amount, dst);
266 memcpy(pixels + rowBytes, pixels, rowBytes);
268 if (mUseFloatTexture) {
269 // We have to use GL_RGBA16F because GL_RGBA32F does not support filtering
270 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
272 texture->upload(GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
275 texture->setFilter(GL_LINEAR);
276 texture->setWrap(GL_CLAMP_TO_EDGE);
279 }; // namespace uirenderer
280 }; // namespace android