blob: eec9ed16939e1d4e8c4c563dc2b3a57bd48518c7 [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
Romain Guy059e12c2012-11-28 17:35:51 -080017#include <utils/JenkinsHash.h>
Romain Guya2341a92010-09-08 18:04:33 -070018
Romain Guy320d46b2012-08-08 16:05:42 -070019#include "Caches.h"
Romain Guyc9855a52011-01-21 21:14:15 -080020#include "Debug.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070021#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070022#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070023
John Reck6b507802015-11-03 10:09:59 -080024#include <cutils/properties.h>
25
Romain Guyc0ac1932010-07-19 18:43:02 -070026namespace 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
Chris Craik51d6a3d2014-12-22 17:16:56 -080055 deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t));
Romain Guy059e12c2012-11-28 17:35:51 -080056 if (deltaInt != 0) return deltaInt;
57
Chris Craik51d6a3d2014-12-22 17:16:56 -080058 return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float));
Romain Guy059e12c2012-11-28 17:35:51 -080059}
60
61///////////////////////////////////////////////////////////////////////////////
Romain Guyc0ac1932010-07-19 18:43:02 -070062// Constructors/destructor
63///////////////////////////////////////////////////////////////////////////////
64
Chris Craik117bdbc2015-02-05 10:12:38 -080065GradientCache::GradientCache(Extensions& extensions)
66 : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity)
67 , mSize(0)
68 , mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE))
69 , mUseFloatTexture(extensions.hasFloatTextures())
70 , mHasNpot(extensions.hasNPot()){
Romain Guyfb8b7632010-08-23 21:05:08 -070071 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -080072 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080073 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070074 setMaxSize(MB(atof(property)));
75 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080076 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070077 }
78
Mathias Agopiana8557d22012-08-31 19:52:30 -070079 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guy8dcfd5e2012-07-20 11:36:03 -070080
Romain Guyfb8b7632010-08-23 21:05:08 -070081 mCache.setOnEntryRemovedListener(this);
Romain Guyc0ac1932010-07-19 18:43:02 -070082}
83
84GradientCache::~GradientCache() {
85 mCache.clear();
86}
87
88///////////////////////////////////////////////////////////////////////////////
89// Size management
90///////////////////////////////////////////////////////////////////////////////
91
92uint32_t GradientCache::getSize() {
93 return mSize;
94}
95
96uint32_t GradientCache::getMaxSize() {
97 return mMaxSize;
98}
99
100void GradientCache::setMaxSize(uint32_t maxSize) {
101 mMaxSize = maxSize;
102 while (mSize > mMaxSize) {
103 mCache.removeOldest();
104 }
105}
106
107///////////////////////////////////////////////////////////////////////////////
108// Callbacks
109///////////////////////////////////////////////////////////////////////////////
110
Chris Craike63f7c622013-10-17 10:30:55 -0700111void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) {
Romain Guy6203f6c2011-08-01 18:56:21 -0700112 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800113 mSize -= texture->objectSize();
Romain Guybe1b1272013-06-06 14:02:54 -0700114 texture->deleteTexture();
Romain Guyc0ac1932010-07-19 18:43:02 -0700115 delete texture;
116 }
117}
118
119///////////////////////////////////////////////////////////////////////////////
120// Caching
121///////////////////////////////////////////////////////////////////////////////
122
Romain Guy42e1e0d2012-07-30 14:47:51 -0700123Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700124 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700125 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700126
Romain Guy6203f6c2011-08-01 18:56:21 -0700127 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700128 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800129 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700130
131 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800132}
133
134void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700135 mCache.clear();
136}
137
Romain Guy42e1e0d2012-07-30 14:47:51 -0700138void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
139 GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700140 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700141
Romain Guy95aeff82013-04-12 16:32:05 -0700142 // If the npot extension is not supported we cannot use non-clamp
143 // wrap modes. We therefore find the nearest largest power of 2
144 // unless width is already a power of 2
145 if (!mHasNpot && (width & (width - 1)) != 0) {
146 width = 1 << (32 - __builtin_clz(width));
Romain Guy320d46b2012-08-08 16:05:42 -0700147 }
148
149 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700150 for (int i = 0; i < count; i++) {
151 if (((colors[i] >> 24) & 0xff) < 255) {
152 hasAlpha = true;
153 break;
154 }
155 }
156
157 info.width = min(width, uint32_t(mMaxTextureSize));
158 info.hasAlpha = hasAlpha;
159}
160
Romain Guy6203f6c2011-08-01 18:56:21 -0700161Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700162 uint32_t* colors, float* positions, int count) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700163
Romain Guy42e1e0d2012-07-30 14:47:51 -0700164 GradientInfo info;
165 getGradientInfo(colors, count, info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700166
Chris Craik8e93a7c2015-02-23 13:07:57 -0800167 Texture* texture = new Texture(Caches::getInstance());
Romain Guy42e1e0d2012-07-30 14:47:51 -0700168 texture->blend = info.hasAlpha;
169 texture->generation = 1;
Romain Guyc0ac1932010-07-19 18:43:02 -0700170
John Reck83c9b5b2016-02-05 13:03:47 -0800171 // Assume the cache is always big enough
John Reck38e0c322015-11-10 12:19:17 -0800172 const uint32_t size = info.width * 2 * bytesPerPixel();
Romain Guy15a65bf2013-01-03 14:22:40 -0800173 while (getSize() + size > mMaxSize) {
John Reck83c9b5b2016-02-05 13:03:47 -0800174 LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
175 "Ran out of things to remove from the cache? getSize() = %" PRIu32
176 ", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32,
177 getSize(), size, mMaxSize, info.width);
Romain Guyc0ac1932010-07-19 18:43:02 -0700178 }
179
John Reck38e0c322015-11-10 12:19:17 -0800180 generateTexture(colors, positions, info.width, 2, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700181
182 mSize += size;
Romain Guy6203f6c2011-08-01 18:56:21 -0700183 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700184
185 return texture;
186}
187
Romain Guyb4880042013-04-05 11:17:55 -0700188size_t GradientCache::bytesPerPixel() const {
189 // We use 4 channels (RGBA)
190 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
191}
192
193void GradientCache::splitToBytes(uint32_t inColor, GradientColor& outColor) const {
194 outColor.r = (inColor >> 16) & 0xff;
195 outColor.g = (inColor >> 8) & 0xff;
196 outColor.b = (inColor >> 0) & 0xff;
197 outColor.a = (inColor >> 24) & 0xff;
198}
199
200void GradientCache::splitToFloats(uint32_t inColor, GradientColor& outColor) const {
201 outColor.r = ((inColor >> 16) & 0xff) / 255.0f;
202 outColor.g = ((inColor >> 8) & 0xff) / 255.0f;
203 outColor.b = ((inColor >> 0) & 0xff) / 255.0f;
204 outColor.a = ((inColor >> 24) & 0xff) / 255.0f;
205}
206
207void GradientCache::mixBytes(GradientColor& start, GradientColor& end, float amount,
208 uint8_t*& dst) const {
209 float oppAmount = 1.0f - amount;
210 const float alpha = start.a * oppAmount + end.a * amount;
211 const float a = alpha / 255.0f;
212
213 *dst++ = uint8_t(a * (start.r * oppAmount + end.r * amount));
214 *dst++ = uint8_t(a * (start.g * oppAmount + end.g * amount));
215 *dst++ = uint8_t(a * (start.b * oppAmount + end.b * amount));
216 *dst++ = uint8_t(alpha);
217}
218
219void GradientCache::mixFloats(GradientColor& start, GradientColor& end, float amount,
220 uint8_t*& dst) const {
221 float oppAmount = 1.0f - amount;
222 const float a = start.a * oppAmount + end.a * amount;
223
224 float* d = (float*) dst;
225 *d++ = a * (start.r * oppAmount + end.r * amount);
226 *d++ = a * (start.g * oppAmount + end.g * amount);
227 *d++ = a * (start.b * oppAmount + end.b * amount);
228 *d++ = a;
229
230 dst += 4 * sizeof(float);
231}
232
John Reck38e0c322015-11-10 12:19:17 -0800233void GradientCache::generateTexture(uint32_t* colors, float* positions,
234 const uint32_t width, const uint32_t height, Texture* texture) {
Romain Guyb4880042013-04-05 11:17:55 -0700235 const GLsizei rowBytes = width * bytesPerPixel();
John Reck38e0c322015-11-10 12:19:17 -0800236 uint8_t pixels[rowBytes * height];
Romain Guyb4880042013-04-05 11:17:55 -0700237
238 static ChannelSplitter gSplitters[] = {
239 &android::uirenderer::GradientCache::splitToBytes,
240 &android::uirenderer::GradientCache::splitToFloats,
241 };
242 ChannelSplitter split = gSplitters[mUseFloatTexture];
243
244 static ChannelMixer gMixers[] = {
245 &android::uirenderer::GradientCache::mixBytes,
246 &android::uirenderer::GradientCache::mixFloats,
247 };
248 ChannelMixer mix = gMixers[mUseFloatTexture];
249
250 GradientColor start;
251 (this->*split)(colors[0], start);
252
253 GradientColor end;
254 (this->*split)(colors[1], end);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700255
256 int currentPos = 1;
Romain Guyb4880042013-04-05 11:17:55 -0700257 float startPos = positions[0];
258 float distance = positions[1] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700259
Romain Guyb4880042013-04-05 11:17:55 -0700260 uint8_t* dst = pixels;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700261 for (uint32_t x = 0; x < width; x++) {
262 float pos = x / float(width - 1);
263 if (pos > positions[currentPos]) {
Romain Guyb4880042013-04-05 11:17:55 -0700264 start = end;
265 startPos = positions[currentPos];
Romain Guy42e1e0d2012-07-30 14:47:51 -0700266
267 currentPos++;
268
Romain Guyb4880042013-04-05 11:17:55 -0700269 (this->*split)(colors[currentPos], end);
270 distance = positions[currentPos] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700271 }
272
Romain Guyb4880042013-04-05 11:17:55 -0700273 float amount = (pos - startPos) / distance;
274 (this->*mix)(start, end, amount, dst);
Romain Guyc0ac1932010-07-19 18:43:02 -0700275 }
276
Romain Guyb4880042013-04-05 11:17:55 -0700277 memcpy(pixels + rowBytes, pixels, rowBytes);
Romain Guyc0ac1932010-07-19 18:43:02 -0700278
Romain Guyb4880042013-04-05 11:17:55 -0700279 if (mUseFloatTexture) {
280 // We have to use GL_RGBA16F because GL_RGBA32F does not support filtering
John Reck9372ac32016-01-19 11:46:52 -0800281 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700282 } else {
John Reck9372ac32016-01-19 11:46:52 -0800283 texture->upload(GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700284 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700285
Romain Guy39d252a2011-12-12 18:14:06 -0800286 texture->setFilter(GL_LINEAR);
287 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700288}
289
290}; // namespace uirenderer
291}; // namespace android