blob: eef366c409f94a3307f883630db85dc1d7bd38a9 [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
Romain Guy320d46b2012-08-08 16:05:42 -070021#include "Caches.h"
Romain Guyc9855a52011-01-21 21:14:15 -080022#include "Debug.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070023#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070024#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
Romain Guy42e1e0d2012-07-30 14:47:51 -070030// Functions
31///////////////////////////////////////////////////////////////////////////////
32
33template<typename T>
34static inline T min(T a, T b) {
35 return a < b ? a : b;
36}
37
38///////////////////////////////////////////////////////////////////////////////
Romain Guy059e12c2012-11-28 17:35:51 -080039// Cache entry
40///////////////////////////////////////////////////////////////////////////////
41
42hash_t GradientCacheEntry::hash() const {
43 uint32_t hash = JenkinsHashMix(0, count);
Romain Guy059e12c2012-11-28 17:35:51 -080044 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]));
47 }
48 return JenkinsHashWhiten(hash);
49}
50
51int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
52 int deltaInt = int(lhs.count) - int(rhs.count);
53 if (deltaInt != 0) return deltaInt;
54
Romain Guy059e12c2012-11-28 17:35:51 -080055 deltaInt = memcmp(lhs.colors, rhs.colors, lhs.count * sizeof(uint32_t));
56 if (deltaInt != 0) return deltaInt;
57
58 return memcmp(lhs.positions, rhs.positions, lhs.count * sizeof(float));
59}
60
61///////////////////////////////////////////////////////////////////////////////
Romain Guyc0ac1932010-07-19 18:43:02 -070062// Constructors/destructor
63///////////////////////////////////////////////////////////////////////////////
64
Romain Guyfb8b7632010-08-23 21:05:08 -070065GradientCache::GradientCache():
Romain Guy059e12c2012-11-28 17:35:51 -080066 mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070067 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
68 char property[PROPERTY_VALUE_MAX];
69 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080070 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070071 setMaxSize(MB(atof(property)));
72 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080073 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070074 }
75
Mathias Agopiana8557d22012-08-31 19:52:30 -070076 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guy8dcfd5e2012-07-20 11:36:03 -070077
Romain Guyfb8b7632010-08-23 21:05:08 -070078 mCache.setOnEntryRemovedListener(this);
Romain Guyb4880042013-04-05 11:17:55 -070079
80 const Extensions& extensions = Extensions::getInstance();
81 mUseFloatTexture = extensions.getMajorGlVersion() >= 3;
82 mHasNpot = extensions.hasNPot();
Romain Guyfb8b7632010-08-23 21:05:08 -070083}
84
Romain Guyc0ac1932010-07-19 18:43:02 -070085GradientCache::GradientCache(uint32_t maxByteSize):
Romain Guy059e12c2012-11-28 17:35:51 -080086 mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyc0ac1932010-07-19 18:43:02 -070087 mSize(0), mMaxSize(maxByteSize) {
88 mCache.setOnEntryRemovedListener(this);
89}
90
91GradientCache::~GradientCache() {
92 mCache.clear();
93}
94
95///////////////////////////////////////////////////////////////////////////////
96// Size management
97///////////////////////////////////////////////////////////////////////////////
98
99uint32_t GradientCache::getSize() {
100 return mSize;
101}
102
103uint32_t GradientCache::getMaxSize() {
104 return mMaxSize;
105}
106
107void GradientCache::setMaxSize(uint32_t maxSize) {
108 mMaxSize = maxSize;
109 while (mSize > mMaxSize) {
110 mCache.removeOldest();
111 }
112}
113
114///////////////////////////////////////////////////////////////////////////////
115// Callbacks
116///////////////////////////////////////////////////////////////////////////////
117
Romain Guy6203f6c2011-08-01 18:56:21 -0700118void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) {
119 if (texture) {
Romain Guyb4880042013-04-05 11:17:55 -0700120 const uint32_t size = texture->width * texture->height * bytesPerPixel();
Romain Guyc0ac1932010-07-19 18:43:02 -0700121 mSize -= size;
Romain Guyc0ac1932010-07-19 18:43:02 -0700122
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 glDeleteTextures(1, &texture->id);
124 delete texture;
125 }
126}
127
128///////////////////////////////////////////////////////////////////////////////
129// Caching
130///////////////////////////////////////////////////////////////////////////////
131
Romain Guy42e1e0d2012-07-30 14:47:51 -0700132Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700133 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700134 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700135
Romain Guy6203f6c2011-08-01 18:56:21 -0700136 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700137 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800138 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700139
140 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800141}
142
143void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700144 mCache.clear();
145}
146
Romain Guy42e1e0d2012-07-30 14:47:51 -0700147void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
148 GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700149 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700150
Romain Guyb4880042013-04-05 11:17:55 -0700151 if (!mHasNpot) {
Romain Guy320d46b2012-08-08 16:05:42 -0700152 width = 1 << (31 - __builtin_clz(width));
153 }
154
155 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700156 for (int i = 0; i < count; i++) {
157 if (((colors[i] >> 24) & 0xff) < 255) {
158 hasAlpha = true;
159 break;
160 }
161 }
162
163 info.width = min(width, uint32_t(mMaxTextureSize));
164 info.hasAlpha = hasAlpha;
165}
166
Romain Guy6203f6c2011-08-01 18:56:21 -0700167Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700168 uint32_t* colors, float* positions, int count) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700169
Romain Guy42e1e0d2012-07-30 14:47:51 -0700170 GradientInfo info;
171 getGradientInfo(colors, count, info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700172
Romain Guy42e1e0d2012-07-30 14:47:51 -0700173 Texture* texture = new Texture;
174 texture->width = info.width;
Romain Guyb4880042013-04-05 11:17:55 -0700175 texture->height = 2;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700176 texture->blend = info.hasAlpha;
177 texture->generation = 1;
Romain Guyc0ac1932010-07-19 18:43:02 -0700178
179 // Asume the cache is always big enough
Romain Guyb4880042013-04-05 11:17:55 -0700180 const uint32_t size = texture->width * texture->height * bytesPerPixel();
Romain Guy15a65bf2013-01-03 14:22:40 -0800181 while (getSize() + size > mMaxSize) {
Romain Guyc0ac1932010-07-19 18:43:02 -0700182 mCache.removeOldest();
183 }
184
Romain Guy42e1e0d2012-07-30 14:47:51 -0700185 generateTexture(colors, positions, count, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700186
187 mSize += size;
Romain Guy6203f6c2011-08-01 18:56:21 -0700188 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700189
190 return texture;
191}
192
Romain Guyb4880042013-04-05 11:17:55 -0700193size_t GradientCache::bytesPerPixel() const {
194 // We use 4 channels (RGBA)
195 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
196}
197
198void GradientCache::splitToBytes(uint32_t inColor, GradientColor& outColor) const {
199 outColor.r = (inColor >> 16) & 0xff;
200 outColor.g = (inColor >> 8) & 0xff;
201 outColor.b = (inColor >> 0) & 0xff;
202 outColor.a = (inColor >> 24) & 0xff;
203}
204
205void GradientCache::splitToFloats(uint32_t inColor, GradientColor& outColor) const {
206 outColor.r = ((inColor >> 16) & 0xff) / 255.0f;
207 outColor.g = ((inColor >> 8) & 0xff) / 255.0f;
208 outColor.b = ((inColor >> 0) & 0xff) / 255.0f;
209 outColor.a = ((inColor >> 24) & 0xff) / 255.0f;
210}
211
212void GradientCache::mixBytes(GradientColor& start, GradientColor& end, float amount,
213 uint8_t*& dst) const {
214 float oppAmount = 1.0f - amount;
215 const float alpha = start.a * oppAmount + end.a * amount;
216 const float a = alpha / 255.0f;
217
218 *dst++ = uint8_t(a * (start.r * oppAmount + end.r * amount));
219 *dst++ = uint8_t(a * (start.g * oppAmount + end.g * amount));
220 *dst++ = uint8_t(a * (start.b * oppAmount + end.b * amount));
221 *dst++ = uint8_t(alpha);
222}
223
224void GradientCache::mixFloats(GradientColor& start, GradientColor& end, float amount,
225 uint8_t*& dst) const {
226 float oppAmount = 1.0f - amount;
227 const float a = start.a * oppAmount + end.a * amount;
228
229 float* d = (float*) dst;
230 *d++ = a * (start.r * oppAmount + end.r * amount);
231 *d++ = a * (start.g * oppAmount + end.g * amount);
232 *d++ = a * (start.b * oppAmount + end.b * amount);
233 *d++ = a;
234
235 dst += 4 * sizeof(float);
236}
237
Romain Guy42e1e0d2012-07-30 14:47:51 -0700238void GradientCache::generateTexture(uint32_t* colors, float* positions,
239 int count, Texture* texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700240 const uint32_t width = texture->width;
Romain Guyb4880042013-04-05 11:17:55 -0700241 const GLsizei rowBytes = width * bytesPerPixel();
242 uint8_t pixels[rowBytes * texture->height];
243
244 static ChannelSplitter gSplitters[] = {
245 &android::uirenderer::GradientCache::splitToBytes,
246 &android::uirenderer::GradientCache::splitToFloats,
247 };
248 ChannelSplitter split = gSplitters[mUseFloatTexture];
249
250 static ChannelMixer gMixers[] = {
251 &android::uirenderer::GradientCache::mixBytes,
252 &android::uirenderer::GradientCache::mixFloats,
253 };
254 ChannelMixer mix = gMixers[mUseFloatTexture];
255
256 GradientColor start;
257 (this->*split)(colors[0], start);
258
259 GradientColor end;
260 (this->*split)(colors[1], end);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700261
262 int currentPos = 1;
Romain Guyb4880042013-04-05 11:17:55 -0700263 float startPos = positions[0];
264 float distance = positions[1] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700265
Romain Guyb4880042013-04-05 11:17:55 -0700266 uint8_t* dst = pixels;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700267 for (uint32_t x = 0; x < width; x++) {
268 float pos = x / float(width - 1);
269 if (pos > positions[currentPos]) {
Romain Guyb4880042013-04-05 11:17:55 -0700270 start = end;
271 startPos = positions[currentPos];
Romain Guy42e1e0d2012-07-30 14:47:51 -0700272
273 currentPos++;
274
Romain Guyb4880042013-04-05 11:17:55 -0700275 (this->*split)(colors[currentPos], end);
276 distance = positions[currentPos] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700277 }
278
Romain Guyb4880042013-04-05 11:17:55 -0700279 float amount = (pos - startPos) / distance;
280 (this->*mix)(start, end, amount, dst);
Romain Guyc0ac1932010-07-19 18:43:02 -0700281 }
282
Romain Guyb4880042013-04-05 11:17:55 -0700283 memcpy(pixels + rowBytes, pixels, rowBytes);
Romain Guyc0ac1932010-07-19 18:43:02 -0700284
285 glGenTextures(1, &texture->id);
Romain Guyc0ac1932010-07-19 18:43:02 -0700286 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyb4880042013-04-05 11:17:55 -0700287 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Romain Guyc0ac1932010-07-19 18:43:02 -0700288
Romain Guyb4880042013-04-05 11:17:55 -0700289 if (mUseFloatTexture) {
290 // We have to use GL_RGBA16F because GL_RGBA32F does not support filtering
291 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, texture->height, 0,
292 GL_RGBA, GL_FLOAT, pixels);
293 } else {
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, texture->height, 0,
295 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
296 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700297
Romain Guy39d252a2011-12-12 18:14:06 -0800298 texture->setFilter(GL_LINEAR);
299 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700300}
301
302}; // namespace uirenderer
303}; // namespace android