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