blob: 35a848725be67c7a070b46c7d1d46c83e2d0f54d [file] [log] [blame]
Romain Guyc0ac1932010-07-19 18:43:02 -07001/*
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
Romain Guy059e12c2012-11-28 17:35:51 -080019#include <utils/JenkinsHash.h>
Romain Guya2341a92010-09-08 18:04:33 -070020#include <utils/threads.h>
21
Romain Guy320d46b2012-08-08 16:05:42 -070022#include "Caches.h"
Romain Guyc9855a52011-01-21 21:14:15 -080023#include "Debug.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070024#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070025#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
Romain Guy42e1e0d2012-07-30 14:47:51 -070031// Defines
32///////////////////////////////////////////////////////////////////////////////
33
34#define GRADIENT_TEXTURE_HEIGHT 2
35#define GRADIENT_BYTES_PER_PIXEL 4
36
37///////////////////////////////////////////////////////////////////////////////
38// Functions
39///////////////////////////////////////////////////////////////////////////////
40
41template<typename T>
42static inline T min(T a, T b) {
43 return a < b ? a : b;
44}
45
46///////////////////////////////////////////////////////////////////////////////
Romain Guy059e12c2012-11-28 17:35:51 -080047// Cache entry
48///////////////////////////////////////////////////////////////////////////////
49
50hash_t GradientCacheEntry::hash() const {
51 uint32_t hash = JenkinsHashMix(0, count);
52 hash = JenkinsHashMix(hash, tileMode);
53 for (uint32_t i = 0; i < count; i++) {
54 hash = JenkinsHashMix(hash, android::hash_type(colors[i]));
55 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
56 }
57 return JenkinsHashWhiten(hash);
58}
59
60int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
61 int deltaInt = int(lhs.count) - int(rhs.count);
62 if (deltaInt != 0) return deltaInt;
63
64 deltaInt = lhs.tileMode - rhs.tileMode;
65 if (deltaInt != 0) return deltaInt;
66
67 deltaInt = memcmp(lhs.colors, rhs.colors, lhs.count * sizeof(uint32_t));
68 if (deltaInt != 0) return deltaInt;
69
70 return memcmp(lhs.positions, rhs.positions, lhs.count * sizeof(float));
71}
72
73///////////////////////////////////////////////////////////////////////////////
Romain Guyc0ac1932010-07-19 18:43:02 -070074// Constructors/destructor
75///////////////////////////////////////////////////////////////////////////////
76
Romain Guyfb8b7632010-08-23 21:05:08 -070077GradientCache::GradientCache():
Romain Guy059e12c2012-11-28 17:35:51 -080078 mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070079 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
80 char property[PROPERTY_VALUE_MAX];
81 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080082 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070083 setMaxSize(MB(atof(property)));
84 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080085 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070086 }
87
Mathias Agopiana8557d22012-08-31 19:52:30 -070088 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guy8dcfd5e2012-07-20 11:36:03 -070089
Romain Guyfb8b7632010-08-23 21:05:08 -070090 mCache.setOnEntryRemovedListener(this);
91}
92
Romain Guyc0ac1932010-07-19 18:43:02 -070093GradientCache::GradientCache(uint32_t maxByteSize):
Romain Guy059e12c2012-11-28 17:35:51 -080094 mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyc0ac1932010-07-19 18:43:02 -070095 mSize(0), mMaxSize(maxByteSize) {
96 mCache.setOnEntryRemovedListener(this);
97}
98
99GradientCache::~GradientCache() {
100 mCache.clear();
101}
102
103///////////////////////////////////////////////////////////////////////////////
104// Size management
105///////////////////////////////////////////////////////////////////////////////
106
107uint32_t GradientCache::getSize() {
108 return mSize;
109}
110
111uint32_t GradientCache::getMaxSize() {
112 return mMaxSize;
113}
114
115void GradientCache::setMaxSize(uint32_t maxSize) {
116 mMaxSize = maxSize;
117 while (mSize > mMaxSize) {
118 mCache.removeOldest();
119 }
120}
121
122///////////////////////////////////////////////////////////////////////////////
123// Callbacks
124///////////////////////////////////////////////////////////////////////////////
125
Romain Guy6203f6c2011-08-01 18:56:21 -0700126void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) {
127 if (texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700128 const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL;
Romain Guyc0ac1932010-07-19 18:43:02 -0700129 mSize -= size;
130 }
131
132 if (texture) {
133 glDeleteTextures(1, &texture->id);
134 delete texture;
135 }
136}
137
138///////////////////////////////////////////////////////////////////////////////
139// Caching
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy42e1e0d2012-07-30 14:47:51 -0700142Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guyc0ac1932010-07-19 18:43:02 -0700143
Romain Guy42e1e0d2012-07-30 14:47:51 -0700144 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700145 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700146
Romain Guy6203f6c2011-08-01 18:56:21 -0700147 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700148 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800149 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700150
151 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800152}
153
154void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700155 mCache.clear();
156}
157
Romain Guy42e1e0d2012-07-30 14:47:51 -0700158void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
159 GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700160 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700161
Romain Guy320d46b2012-08-08 16:05:42 -0700162 if (!Caches::getInstance().extensions.hasNPot()) {
163 width = 1 << (31 - __builtin_clz(width));
164 }
165
166 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700167 for (int i = 0; i < count; i++) {
168 if (((colors[i] >> 24) & 0xff) < 255) {
169 hasAlpha = true;
170 break;
171 }
172 }
173
174 info.width = min(width, uint32_t(mMaxTextureSize));
175 info.hasAlpha = hasAlpha;
176}
177
Romain Guy6203f6c2011-08-01 18:56:21 -0700178Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700179 uint32_t* colors, float* positions, int count) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700180
Romain Guy42e1e0d2012-07-30 14:47:51 -0700181 GradientInfo info;
182 getGradientInfo(colors, count, info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700183
Romain Guy42e1e0d2012-07-30 14:47:51 -0700184 Texture* texture = new Texture;
185 texture->width = info.width;
186 texture->height = GRADIENT_TEXTURE_HEIGHT;
187 texture->blend = info.hasAlpha;
188 texture->generation = 1;
Romain Guyc0ac1932010-07-19 18:43:02 -0700189
190 // Asume the cache is always big enough
Romain Guy42e1e0d2012-07-30 14:47:51 -0700191 const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL;
Romain Guyc0ac1932010-07-19 18:43:02 -0700192 while (mSize + size > mMaxSize) {
193 mCache.removeOldest();
194 }
195
Romain Guy42e1e0d2012-07-30 14:47:51 -0700196 generateTexture(colors, positions, count, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700197
198 mSize += size;
Romain Guy6203f6c2011-08-01 18:56:21 -0700199 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700200
201 return texture;
202}
203
Romain Guy42e1e0d2012-07-30 14:47:51 -0700204void GradientCache::generateTexture(uint32_t* colors, float* positions,
205 int count, Texture* texture) {
206
207 const uint32_t width = texture->width;
208 const GLsizei rowBytes = width * GRADIENT_BYTES_PER_PIXEL;
209 uint32_t pixels[width * texture->height];
210
211 int currentPos = 1;
212
213 float startA = (colors[0] >> 24) & 0xff;
214 float startR = (colors[0] >> 16) & 0xff;
215 float startG = (colors[0] >> 8) & 0xff;
216 float startB = (colors[0] >> 0) & 0xff;
217
218 float endA = (colors[1] >> 24) & 0xff;
219 float endR = (colors[1] >> 16) & 0xff;
220 float endG = (colors[1] >> 8) & 0xff;
221 float endB = (colors[1] >> 0) & 0xff;
222
223 float start = positions[0];
224 float distance = positions[1] - start;
225
226 uint8_t* p = (uint8_t*) pixels;
227 for (uint32_t x = 0; x < width; x++) {
228 float pos = x / float(width - 1);
229 if (pos > positions[currentPos]) {
230 startA = endA;
231 startR = endR;
232 startG = endG;
233 startB = endB;
234 start = positions[currentPos];
235
236 currentPos++;
237
238 endA = (colors[currentPos] >> 24) & 0xff;
239 endR = (colors[currentPos] >> 16) & 0xff;
240 endG = (colors[currentPos] >> 8) & 0xff;
241 endB = (colors[currentPos] >> 0) & 0xff;
242 distance = positions[currentPos] - start;
243 }
244
245 float amount = (pos - start) / distance;
246 float oppAmount = 1.0f - amount;
247
Romain Guyd679b572012-08-29 21:49:00 -0700248 const float alpha = startA * oppAmount + endA * amount;
249 const float a = alpha / 255.0f;
250 *p++ = uint8_t(a * (startR * oppAmount + endR * amount));
251 *p++ = uint8_t(a * (startG * oppAmount + endG * amount));
252 *p++ = uint8_t(a * (startB * oppAmount + endB * amount));
253 *p++ = uint8_t(alpha);
Romain Guyc0ac1932010-07-19 18:43:02 -0700254 }
255
Romain Guy42e1e0d2012-07-30 14:47:51 -0700256 for (int i = 1; i < GRADIENT_TEXTURE_HEIGHT; i++) {
257 memcpy(pixels + width * i, pixels, rowBytes);
258 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700259
260 glGenTextures(1, &texture->id);
261
262 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700263 glPixelStorei(GL_UNPACK_ALIGNMENT, GRADIENT_BYTES_PER_PIXEL);
Romain Guyc0ac1932010-07-19 18:43:02 -0700264
Romain Guy42e1e0d2012-07-30 14:47:51 -0700265 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, texture->height, 0,
266 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Romain Guyc0ac1932010-07-19 18:43:02 -0700267
Romain Guy39d252a2011-12-12 18:14:06 -0800268 texture->setFilter(GL_LINEAR);
269 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700270}
271
272}; // namespace uirenderer
273}; // namespace android