• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "DeviceInfo.h"
24 
25 #include <cutils/properties.h>
26 
27 namespace android {
28 namespace uirenderer {
29 
30 ///////////////////////////////////////////////////////////////////////////////
31 // Functions
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 template<typename T>
min(T a,T b)35 static inline T min(T a, T b) {
36     return a < b ? a : b;
37 }
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // Cache entry
41 ///////////////////////////////////////////////////////////////////////////////
42 
hash() const43 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 
compare(const GradientCacheEntry & lhs,const GradientCacheEntry & rhs)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 
GradientCache(const Extensions & extensions)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 
~GradientCache()78 GradientCache::~GradientCache() {
79     mCache.clear();
80 }
81 
82 ///////////////////////////////////////////////////////////////////////////////
83 // Size management
84 ///////////////////////////////////////////////////////////////////////////////
85 
getSize()86 uint32_t GradientCache::getSize() {
87     return mSize;
88 }
89 
getMaxSize()90 uint32_t GradientCache::getMaxSize() {
91     return mMaxSize;
92 }
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 // Callbacks
96 ///////////////////////////////////////////////////////////////////////////////
97 
operator ()(GradientCacheEntry &,Texture * & texture)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 
get(uint32_t * colors,float * positions,int count)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 
clear()121 void GradientCache::clear() {
122     mCache.clear();
123 }
124 
getGradientInfo(const uint32_t * colors,const int count,GradientInfo & info)125 void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
126         GradientInfo& info) {
127     uint32_t width = 256 * (count - 1);
128 
129     // If the npot extension is not supported we cannot use non-clamp
130     // wrap modes. We therefore find the nearest largest power of 2
131     // unless width is already a power of 2
132     if (!mHasNpot && (width & (width - 1)) != 0) {
133         width = 1 << (32 - __builtin_clz(width));
134     }
135 
136     bool hasAlpha = false;
137     for (int i = 0; i < count; i++) {
138         if (((colors[i] >> 24) & 0xff) < 255) {
139             hasAlpha = true;
140             break;
141         }
142     }
143 
144     info.width = min(width, uint32_t(mMaxTextureSize));
145     info.hasAlpha = hasAlpha;
146 }
147 
addLinearGradient(GradientCacheEntry & gradient,uint32_t * colors,float * positions,int count)148 Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
149         uint32_t* colors, float* positions, int count) {
150 
151     GradientInfo info;
152     getGradientInfo(colors, count, info);
153 
154     Texture* texture = new Texture(Caches::getInstance());
155     texture->blend = info.hasAlpha;
156     texture->generation = 1;
157 
158     // Assume the cache is always big enough
159     const uint32_t size = info.width * 2 * bytesPerPixel();
160     while (getSize() + size > mMaxSize) {
161         LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
162                 "Ran out of things to remove from the cache? getSize() = %" PRIu32
163                 ", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32,
164                 getSize(), size, mMaxSize, info.width);
165     }
166 
167     generateTexture(colors, positions, info.width, 2, texture);
168 
169     mSize += size;
170     LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(),
171             "size != texture->objectSize(), size %" PRIu32 ", objectSize %d"
172             " width = %" PRIu32 " bytesPerPixel() = %zu",
173             size, texture->objectSize(), info.width, bytesPerPixel());
174     mCache.put(gradient, texture);
175 
176     return texture;
177 }
178 
bytesPerPixel() const179 size_t GradientCache::bytesPerPixel() const {
180     // We use 4 channels (RGBA)
181     return 4 * (mUseFloatTexture ? /* fp16 */ 2 : sizeof(uint8_t));
182 }
183 
sourceBytesPerPixel() const184 size_t GradientCache::sourceBytesPerPixel() const {
185     // We use 4 channels (RGBA) and upload from floats (not half floats)
186     return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
187 }
188 
mixBytes(const FloatColor & start,const FloatColor & end,float amount,uint8_t * & dst) const189 void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end,
190         float amount, uint8_t*& dst) const {
191     float oppAmount = 1.0f - amount;
192     float a = start.a * oppAmount + end.a * amount;
193     *dst++ = uint8_t(OECF(start.r * oppAmount + end.r * amount) * 255.0f);
194     *dst++ = uint8_t(OECF(start.g * oppAmount + end.g * amount) * 255.0f);
195     *dst++ = uint8_t(OECF(start.b * oppAmount + end.b * amount) * 255.0f);
196     *dst++ = uint8_t(a * 255.0f);
197 }
198 
mixFloats(const FloatColor & start,const FloatColor & end,float amount,uint8_t * & dst) const199 void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end,
200         float amount, uint8_t*& dst) const {
201     float oppAmount = 1.0f - amount;
202     float a = start.a * oppAmount + end.a * amount;
203     float* d = (float*) dst;
204 #ifdef ANDROID_ENABLE_LINEAR_BLENDING
205     // We want to stay linear
206     *d++ = (start.r * oppAmount + end.r * amount);
207     *d++ = (start.g * oppAmount + end.g * amount);
208     *d++ = (start.b * oppAmount + end.b * amount);
209 #else
210     *d++ = OECF(start.r * oppAmount + end.r * amount);
211     *d++ = OECF(start.g * oppAmount + end.g * amount);
212     *d++ = OECF(start.b * oppAmount + end.b * amount);
213 #endif
214     *d++ = a;
215     dst += 4 * sizeof(float);
216 }
217 
generateTexture(uint32_t * colors,float * positions,const uint32_t width,const uint32_t height,Texture * texture)218 void GradientCache::generateTexture(uint32_t* colors, float* positions,
219         const uint32_t width, const uint32_t height, Texture* texture) {
220     const GLsizei rowBytes = width * sourceBytesPerPixel();
221     uint8_t pixels[rowBytes * height];
222 
223     static ChannelMixer gMixers[] = {
224             // colors are stored gamma-encoded
225             &android::uirenderer::GradientCache::mixBytes,
226             // colors are stored in linear (linear blending on)
227             // or gamma-encoded (linear blending off)
228             &android::uirenderer::GradientCache::mixFloats,
229     };
230     ChannelMixer mix = gMixers[mUseFloatTexture];
231 
232     FloatColor start;
233     start.set(colors[0]);
234 
235     FloatColor end;
236     end.set(colors[1]);
237 
238     int currentPos = 1;
239     float startPos = positions[0];
240     float distance = positions[1] - startPos;
241 
242     uint8_t* dst = pixels;
243     for (uint32_t x = 0; x < width; x++) {
244         float pos = x / float(width - 1);
245         if (pos > positions[currentPos]) {
246             start = end;
247             startPos = positions[currentPos];
248 
249             currentPos++;
250 
251             end.set(colors[currentPos]);
252             distance = positions[currentPos] - startPos;
253         }
254 
255         float amount = (pos - startPos) / distance;
256         (this->*mix)(start, end, amount, dst);
257     }
258 
259     memcpy(pixels + rowBytes, pixels, rowBytes);
260 
261     if (mUseFloatTexture) {
262         texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
263     } else {
264         GLint internalFormat = mHasLinearBlending ? GL_SRGB8_ALPHA8 : GL_RGBA;
265         texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
266     }
267 
268     texture->setFilter(GL_LINEAR);
269     texture->setWrap(GL_CLAMP_TO_EDGE);
270 }
271 
272 }; // namespace uirenderer
273 }; // namespace android
274