blob: 726b57c792bbb909d092db47682b512d7c431a60 [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 Guya2341a92010-09-08 18:04:33 -070019#include <utils/threads.h>
20
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// Defines
31///////////////////////////////////////////////////////////////////////////////
32
33#define GRADIENT_TEXTURE_HEIGHT 2
34#define GRADIENT_BYTES_PER_PIXEL 4
35
36///////////////////////////////////////////////////////////////////////////////
37// Functions
38///////////////////////////////////////////////////////////////////////////////
39
40template<typename T>
41static inline T min(T a, T b) {
42 return a < b ? a : b;
43}
44
45///////////////////////////////////////////////////////////////////////////////
Romain Guyc0ac1932010-07-19 18:43:02 -070046// Constructors/destructor
47///////////////////////////////////////////////////////////////////////////////
48
Romain Guyfb8b7632010-08-23 21:05:08 -070049GradientCache::GradientCache():
Romain Guy6203f6c2011-08-01 18:56:21 -070050 mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070051 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) {
Romain Guyc9855a52011-01-21 21:14:15 -080054 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070055 setMaxSize(MB(atof(property)));
56 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080057 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070058 }
59
Romain Guy8dcfd5e2012-07-20 11:36:03 -070060 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
61
Romain Guyfb8b7632010-08-23 21:05:08 -070062 mCache.setOnEntryRemovedListener(this);
63}
64
Romain Guyc0ac1932010-07-19 18:43:02 -070065GradientCache::GradientCache(uint32_t maxByteSize):
Romain Guy6203f6c2011-08-01 18:56:21 -070066 mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyc0ac1932010-07-19 18:43:02 -070067 mSize(0), mMaxSize(maxByteSize) {
68 mCache.setOnEntryRemovedListener(this);
69}
70
71GradientCache::~GradientCache() {
72 mCache.clear();
73}
74
75///////////////////////////////////////////////////////////////////////////////
76// Size management
77///////////////////////////////////////////////////////////////////////////////
78
79uint32_t GradientCache::getSize() {
80 return mSize;
81}
82
83uint32_t GradientCache::getMaxSize() {
84 return mMaxSize;
85}
86
87void GradientCache::setMaxSize(uint32_t maxSize) {
88 mMaxSize = maxSize;
89 while (mSize > mMaxSize) {
90 mCache.removeOldest();
91 }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95// Callbacks
96///////////////////////////////////////////////////////////////////////////////
97
Romain Guy6203f6c2011-08-01 18:56:21 -070098void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) {
99 if (texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700100 const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL;
Romain Guyc0ac1932010-07-19 18:43:02 -0700101 mSize -= size;
102 }
103
104 if (texture) {
105 glDeleteTextures(1, &texture->id);
106 delete texture;
107 }
108}
109
110///////////////////////////////////////////////////////////////////////////////
111// Caching
112///////////////////////////////////////////////////////////////////////////////
113
Romain Guy42e1e0d2012-07-30 14:47:51 -0700114Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guyc0ac1932010-07-19 18:43:02 -0700115
Romain Guy42e1e0d2012-07-30 14:47:51 -0700116 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700117 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700118
Romain Guy6203f6c2011-08-01 18:56:21 -0700119 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700120 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800121 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700122
123 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800124}
125
126void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700127 mCache.clear();
128}
129
Romain Guy42e1e0d2012-07-30 14:47:51 -0700130void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
131 GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700132 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700133
Romain Guy320d46b2012-08-08 16:05:42 -0700134 if (!Caches::getInstance().extensions.hasNPot()) {
135 width = 1 << (31 - __builtin_clz(width));
136 }
137
138 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700139 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
Romain Guy6203f6c2011-08-01 18:56:21 -0700150Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700151 uint32_t* colors, float* positions, int count) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700152
Romain Guy42e1e0d2012-07-30 14:47:51 -0700153 GradientInfo info;
154 getGradientInfo(colors, count, info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700155
Romain Guy42e1e0d2012-07-30 14:47:51 -0700156 Texture* texture = new Texture;
157 texture->width = info.width;
158 texture->height = GRADIENT_TEXTURE_HEIGHT;
159 texture->blend = info.hasAlpha;
160 texture->generation = 1;
Romain Guyc0ac1932010-07-19 18:43:02 -0700161
162 // Asume the cache is always big enough
Romain Guy42e1e0d2012-07-30 14:47:51 -0700163 const uint32_t size = texture->width * texture->height * GRADIENT_BYTES_PER_PIXEL;
Romain Guyc0ac1932010-07-19 18:43:02 -0700164 while (mSize + size > mMaxSize) {
165 mCache.removeOldest();
166 }
167
Romain Guy42e1e0d2012-07-30 14:47:51 -0700168 generateTexture(colors, positions, count, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700169
170 mSize += size;
Romain Guy6203f6c2011-08-01 18:56:21 -0700171 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700172
173 return texture;
174}
175
Romain Guy42e1e0d2012-07-30 14:47:51 -0700176void 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 *p++ = uint8_t(startR * oppAmount + endR * amount);
221 *p++ = uint8_t(startG * oppAmount + endG * amount);
222 *p++ = uint8_t(startB * oppAmount + endB * amount);
223 *p++ = uint8_t(startA * oppAmount + endA * amount);
Romain Guyc0ac1932010-07-19 18:43:02 -0700224 }
225
Romain Guy42e1e0d2012-07-30 14:47:51 -0700226 for (int i = 1; i < GRADIENT_TEXTURE_HEIGHT; i++) {
227 memcpy(pixels + width * i, pixels, rowBytes);
228 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700229
230 glGenTextures(1, &texture->id);
231
232 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700233 glPixelStorei(GL_UNPACK_ALIGNMENT, GRADIENT_BYTES_PER_PIXEL);
Romain Guyc0ac1932010-07-19 18:43:02 -0700234
Romain Guy42e1e0d2012-07-30 14:47:51 -0700235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, texture->height, 0,
236 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Romain Guyc0ac1932010-07-19 18:43:02 -0700237
Romain Guy39d252a2011-12-12 18:14:06 -0800238 texture->setFilter(GL_LINEAR);
239 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700240}
241
242}; // namespace uirenderer
243}; // namespace android