blob: 88ae02b5524d1716b30e1d5602b87e6705737244 [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)
Chris Craik48a8f432016-02-05 15:59:29 -080068 , mMaxSize(Properties::gradientCacheSize)
Chris Craik117bdbc2015-02-05 10:12:38 -080069 , mUseFloatTexture(extensions.hasFloatTextures())
70 , mHasNpot(extensions.hasNPot()){
Mathias Agopiana8557d22012-08-31 19:52:30 -070071 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guy8dcfd5e2012-07-20 11:36:03 -070072
Romain Guyfb8b7632010-08-23 21:05:08 -070073 mCache.setOnEntryRemovedListener(this);
Romain Guyc0ac1932010-07-19 18:43:02 -070074}
75
76GradientCache::~GradientCache() {
77 mCache.clear();
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Size management
82///////////////////////////////////////////////////////////////////////////////
83
84uint32_t GradientCache::getSize() {
85 return mSize;
86}
87
88uint32_t GradientCache::getMaxSize() {
89 return mMaxSize;
90}
91
Romain Guyc0ac1932010-07-19 18:43:02 -070092///////////////////////////////////////////////////////////////////////////////
93// Callbacks
94///////////////////////////////////////////////////////////////////////////////
95
Chris Craike63f7c622013-10-17 10:30:55 -070096void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) {
Romain Guy6203f6c2011-08-01 18:56:21 -070097 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -080098 mSize -= texture->objectSize();
Romain Guybe1b1272013-06-06 14:02:54 -070099 texture->deleteTexture();
Romain Guyc0ac1932010-07-19 18:43:02 -0700100 delete texture;
101 }
102}
103
104///////////////////////////////////////////////////////////////////////////////
105// Caching
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guy42e1e0d2012-07-30 14:47:51 -0700108Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700109 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700110 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700111
Romain Guy6203f6c2011-08-01 18:56:21 -0700112 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700113 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800114 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700115
116 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800117}
118
119void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700120 mCache.clear();
121}
122
Romain Guy42e1e0d2012-07-30 14:47:51 -0700123void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
124 GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700125 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700126
Romain Guy95aeff82013-04-12 16:32:05 -0700127 // If the npot extension is not supported we cannot use non-clamp
128 // wrap modes. We therefore find the nearest largest power of 2
129 // unless width is already a power of 2
130 if (!mHasNpot && (width & (width - 1)) != 0) {
131 width = 1 << (32 - __builtin_clz(width));
Romain Guy320d46b2012-08-08 16:05:42 -0700132 }
133
134 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700135 for (int i = 0; i < count; i++) {
136 if (((colors[i] >> 24) & 0xff) < 255) {
137 hasAlpha = true;
138 break;
139 }
140 }
141
142 info.width = min(width, uint32_t(mMaxTextureSize));
143 info.hasAlpha = hasAlpha;
144}
145
Romain Guy6203f6c2011-08-01 18:56:21 -0700146Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700147 uint32_t* colors, float* positions, int count) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700148
Romain Guy42e1e0d2012-07-30 14:47:51 -0700149 GradientInfo info;
150 getGradientInfo(colors, count, info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700151
Chris Craik8e93a7c2015-02-23 13:07:57 -0800152 Texture* texture = new Texture(Caches::getInstance());
Romain Guy42e1e0d2012-07-30 14:47:51 -0700153 texture->blend = info.hasAlpha;
154 texture->generation = 1;
Romain Guyc0ac1932010-07-19 18:43:02 -0700155
John Reck83c9b5b2016-02-05 13:03:47 -0800156 // Assume the cache is always big enough
John Reck38e0c322015-11-10 12:19:17 -0800157 const uint32_t size = info.width * 2 * bytesPerPixel();
Romain Guy15a65bf2013-01-03 14:22:40 -0800158 while (getSize() + size > mMaxSize) {
John Reck83c9b5b2016-02-05 13:03:47 -0800159 LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
160 "Ran out of things to remove from the cache? getSize() = %" PRIu32
161 ", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32,
162 getSize(), size, mMaxSize, info.width);
Romain Guyc0ac1932010-07-19 18:43:02 -0700163 }
164
John Reck38e0c322015-11-10 12:19:17 -0800165 generateTexture(colors, positions, info.width, 2, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700166
167 mSize += size;
John Reck1d4e6a02016-02-11 13:22:25 -0800168 LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(),
169 "size != texture->objectSize(), size %" PRIu32 ", objectSize %" PRIu32
170 " width = %" PRIu32 " bytesPerPixel() = %" PRIu32,
171 size, texture->objectSize(), info.width, bytesPerPixel());
Romain Guy6203f6c2011-08-01 18:56:21 -0700172 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700173
174 return texture;
175}
176
Romain Guyb4880042013-04-05 11:17:55 -0700177size_t GradientCache::bytesPerPixel() const {
178 // We use 4 channels (RGBA)
179 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
180}
181
182void GradientCache::splitToBytes(uint32_t inColor, GradientColor& outColor) const {
183 outColor.r = (inColor >> 16) & 0xff;
184 outColor.g = (inColor >> 8) & 0xff;
185 outColor.b = (inColor >> 0) & 0xff;
186 outColor.a = (inColor >> 24) & 0xff;
187}
188
189void GradientCache::splitToFloats(uint32_t inColor, GradientColor& outColor) const {
190 outColor.r = ((inColor >> 16) & 0xff) / 255.0f;
191 outColor.g = ((inColor >> 8) & 0xff) / 255.0f;
192 outColor.b = ((inColor >> 0) & 0xff) / 255.0f;
193 outColor.a = ((inColor >> 24) & 0xff) / 255.0f;
194}
195
196void GradientCache::mixBytes(GradientColor& start, GradientColor& end, float amount,
197 uint8_t*& dst) const {
198 float oppAmount = 1.0f - amount;
199 const float alpha = start.a * oppAmount + end.a * amount;
200 const float a = alpha / 255.0f;
201
202 *dst++ = uint8_t(a * (start.r * oppAmount + end.r * amount));
203 *dst++ = uint8_t(a * (start.g * oppAmount + end.g * amount));
204 *dst++ = uint8_t(a * (start.b * oppAmount + end.b * amount));
205 *dst++ = uint8_t(alpha);
206}
207
208void GradientCache::mixFloats(GradientColor& start, GradientColor& end, float amount,
209 uint8_t*& dst) const {
210 float oppAmount = 1.0f - amount;
211 const float a = start.a * oppAmount + end.a * amount;
212
213 float* d = (float*) dst;
214 *d++ = a * (start.r * oppAmount + end.r * amount);
215 *d++ = a * (start.g * oppAmount + end.g * amount);
216 *d++ = a * (start.b * oppAmount + end.b * amount);
217 *d++ = a;
218
219 dst += 4 * sizeof(float);
220}
221
John Reck38e0c322015-11-10 12:19:17 -0800222void GradientCache::generateTexture(uint32_t* colors, float* positions,
223 const uint32_t width, const uint32_t height, Texture* texture) {
Romain Guyb4880042013-04-05 11:17:55 -0700224 const GLsizei rowBytes = width * bytesPerPixel();
John Reck38e0c322015-11-10 12:19:17 -0800225 uint8_t pixels[rowBytes * height];
Romain Guyb4880042013-04-05 11:17:55 -0700226
227 static ChannelSplitter gSplitters[] = {
228 &android::uirenderer::GradientCache::splitToBytes,
229 &android::uirenderer::GradientCache::splitToFloats,
230 };
231 ChannelSplitter split = gSplitters[mUseFloatTexture];
232
233 static ChannelMixer gMixers[] = {
234 &android::uirenderer::GradientCache::mixBytes,
235 &android::uirenderer::GradientCache::mixFloats,
236 };
237 ChannelMixer mix = gMixers[mUseFloatTexture];
238
239 GradientColor start;
240 (this->*split)(colors[0], start);
241
242 GradientColor end;
243 (this->*split)(colors[1], end);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700244
245 int currentPos = 1;
Romain Guyb4880042013-04-05 11:17:55 -0700246 float startPos = positions[0];
247 float distance = positions[1] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700248
Romain Guyb4880042013-04-05 11:17:55 -0700249 uint8_t* dst = pixels;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700250 for (uint32_t x = 0; x < width; x++) {
251 float pos = x / float(width - 1);
252 if (pos > positions[currentPos]) {
Romain Guyb4880042013-04-05 11:17:55 -0700253 start = end;
254 startPos = positions[currentPos];
Romain Guy42e1e0d2012-07-30 14:47:51 -0700255
256 currentPos++;
257
Romain Guyb4880042013-04-05 11:17:55 -0700258 (this->*split)(colors[currentPos], end);
259 distance = positions[currentPos] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700260 }
261
Romain Guyb4880042013-04-05 11:17:55 -0700262 float amount = (pos - startPos) / distance;
263 (this->*mix)(start, end, amount, dst);
Romain Guyc0ac1932010-07-19 18:43:02 -0700264 }
265
Romain Guyb4880042013-04-05 11:17:55 -0700266 memcpy(pixels + rowBytes, pixels, rowBytes);
Romain Guyc0ac1932010-07-19 18:43:02 -0700267
Romain Guyb4880042013-04-05 11:17:55 -0700268 if (mUseFloatTexture) {
269 // We have to use GL_RGBA16F because GL_RGBA32F does not support filtering
John Reck9372ac32016-01-19 11:46:52 -0800270 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700271 } else {
John Reck9372ac32016-01-19 11:46:52 -0800272 texture->upload(GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700273 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700274
Romain Guy39d252a2011-12-12 18:14:06 -0800275 texture->setFilter(GL_LINEAR);
276 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700277}
278
279}; // namespace uirenderer
280}; // namespace android