blob: 21e3c730cbec737c35fc70c83b02aecf2c82d4fe [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"
John Reck1bcacfd2017-11-03 10:12:19 -070021#include "DeviceInfo.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070022#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070023#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070024
John Reck6b507802015-11-03 10:09:59 -080025#include <cutils/properties.h>
26
Romain Guyc0ac1932010-07-19 18:43:02 -070027namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
Romain Guy42e1e0d2012-07-30 14:47:51 -070031// Functions
32///////////////////////////////////////////////////////////////////////////////
33
John Reck1bcacfd2017-11-03 10:12:19 -070034template <typename T>
Romain Guy42e1e0d2012-07-30 14:47:51 -070035static inline T min(T a, T b) {
36 return a < b ? a : b;
37}
38
39///////////////////////////////////////////////////////////////////////////////
Romain Guy059e12c2012-11-28 17:35:51 -080040// Cache entry
41///////////////////////////////////////////////////////////////////////////////
42
43hash_t GradientCacheEntry::hash() const {
44 uint32_t hash = JenkinsHashMix(0, count);
Romain Guy059e12c2012-11-28 17:35:51 -080045 for (uint32_t i = 0; i < count; i++) {
46 hash = JenkinsHashMix(hash, android::hash_type(colors[i]));
47 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
48 }
49 return JenkinsHashWhiten(hash);
50}
51
52int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
53 int deltaInt = int(lhs.count) - int(rhs.count);
54 if (deltaInt != 0) return deltaInt;
55
Chris Craik51d6a3d2014-12-22 17:16:56 -080056 deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t));
Romain Guy059e12c2012-11-28 17:35:51 -080057 if (deltaInt != 0) return deltaInt;
58
Chris Craik51d6a3d2014-12-22 17:16:56 -080059 return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float));
Romain Guy059e12c2012-11-28 17:35:51 -080060}
61
62///////////////////////////////////////////////////////////////////////////////
Romain Guyc0ac1932010-07-19 18:43:02 -070063// Constructors/destructor
64///////////////////////////////////////////////////////////////////////////////
65
John Reck8dc02f92017-07-17 09:55:02 -070066GradientCache::GradientCache(const Extensions& extensions)
Chris Craik117bdbc2015-02-05 10:12:38 -080067 : mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity)
68 , mSize(0)
John Reck8dc02f92017-07-17 09:55:02 -070069 , mMaxSize(MB(1))
Chris Craik117bdbc2015-02-05 10:12:38 -080070 , mUseFloatTexture(extensions.hasFloatTextures())
Romain Guy253f2c22016-09-28 17:34:42 -070071 , mHasNpot(extensions.hasNPot())
Romain Guyefb4b062017-02-27 11:00:04 -080072 , mHasLinearBlending(extensions.hasLinearBlending()) {
John Reck8dc02f92017-07-17 09:55:02 -070073 mMaxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guy8dcfd5e2012-07-20 11:36:03 -070074
Romain Guyfb8b7632010-08-23 21:05:08 -070075 mCache.setOnEntryRemovedListener(this);
Romain Guyc0ac1932010-07-19 18:43:02 -070076}
77
78GradientCache::~GradientCache() {
79 mCache.clear();
80}
81
82///////////////////////////////////////////////////////////////////////////////
83// Size management
84///////////////////////////////////////////////////////////////////////////////
85
86uint32_t GradientCache::getSize() {
87 return mSize;
88}
89
90uint32_t GradientCache::getMaxSize() {
91 return mMaxSize;
92}
93
Romain Guyc0ac1932010-07-19 18:43:02 -070094///////////////////////////////////////////////////////////////////////////////
95// Callbacks
96///////////////////////////////////////////////////////////////////////////////
97
Chris Craike63f7c622013-10-17 10:30:55 -070098void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) {
Romain Guy6203f6c2011-08-01 18:56:21 -070099 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800100 mSize -= texture->objectSize();
Romain Guybe1b1272013-06-06 14:02:54 -0700101 texture->deleteTexture();
Romain Guyc0ac1932010-07-19 18:43:02 -0700102 delete texture;
103 }
104}
105
106///////////////////////////////////////////////////////////////////////////////
107// Caching
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guy42e1e0d2012-07-30 14:47:51 -0700110Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700111 GradientCacheEntry gradient(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700112 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700113
Romain Guy6203f6c2011-08-01 18:56:21 -0700114 if (!texture) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700115 texture = addLinearGradient(gradient, colors, positions, count);
Romain Guyfe48f652010-11-11 15:36:56 -0800116 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700117
118 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800119}
120
121void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700122 mCache.clear();
123}
124
John Reck1bcacfd2017-11-03 10:12:19 -0700125void GradientCache::getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info) {
Romain Guy320d46b2012-08-08 16:05:42 -0700126 uint32_t width = 256 * (count - 1);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700127
Romain Guy95aeff82013-04-12 16:32:05 -0700128 // If the npot extension is not supported we cannot use non-clamp
129 // wrap modes. We therefore find the nearest largest power of 2
130 // unless width is already a power of 2
131 if (!mHasNpot && (width & (width - 1)) != 0) {
132 width = 1 << (32 - __builtin_clz(width));
Romain Guy320d46b2012-08-08 16:05:42 -0700133 }
134
135 bool hasAlpha = false;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700136 for (int i = 0; i < count; i++) {
137 if (((colors[i] >> 24) & 0xff) < 255) {
138 hasAlpha = true;
139 break;
140 }
141 }
142
143 info.width = min(width, uint32_t(mMaxTextureSize));
144 info.hasAlpha = hasAlpha;
145}
146
John Reck1bcacfd2017-11-03 10:12:19 -0700147Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient, uint32_t* colors,
148 float* positions, int count) {
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(),
John Reck1bcacfd2017-11-03 10:12:19 -0700160 "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(),
John Reck1bcacfd2017-11-03 10:12:19 -0700169 "size != texture->objectSize(), size %" PRIu32
170 ", objectSize %d"
171 " width = %" PRIu32 " bytesPerPixel() = %zu",
172 size, texture->objectSize(), info.width, bytesPerPixel());
Romain Guy6203f6c2011-08-01 18:56:21 -0700173 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700174
175 return texture;
176}
177
Romain Guyb4880042013-04-05 11:17:55 -0700178size_t GradientCache::bytesPerPixel() const {
179 // We use 4 channels (RGBA)
Romain Guy253f2c22016-09-28 17:34:42 -0700180 return 4 * (mUseFloatTexture ? /* fp16 */ 2 : sizeof(uint8_t));
181}
182
183size_t GradientCache::sourceBytesPerPixel() const {
184 // We use 4 channels (RGBA) and upload from floats (not half floats)
Romain Guyb4880042013-04-05 11:17:55 -0700185 return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
186}
187
John Reck1bcacfd2017-11-03 10:12:19 -0700188void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end, float amount,
189 uint8_t*& dst) const {
Romain Guyb4880042013-04-05 11:17:55 -0700190 float oppAmount = 1.0f - amount;
Romain Guya0ed6f02016-12-12 18:21:32 -0800191 float a = start.a * oppAmount + end.a * amount;
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400192 *dst++ = uint8_t(OECF(start.r * oppAmount + end.r * amount) * 255.0f);
193 *dst++ = uint8_t(OECF(start.g * oppAmount + end.g * amount) * 255.0f);
194 *dst++ = uint8_t(OECF(start.b * oppAmount + end.b * amount) * 255.0f);
Romain Guya0ed6f02016-12-12 18:21:32 -0800195 *dst++ = uint8_t(a * 255.0f);
Romain Guyb4880042013-04-05 11:17:55 -0700196}
197
John Reck1bcacfd2017-11-03 10:12:19 -0700198void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end, float amount,
199 uint8_t*& dst) const {
Romain Guyb4880042013-04-05 11:17:55 -0700200 float oppAmount = 1.0f - amount;
Romain Guya0ed6f02016-12-12 18:21:32 -0800201 float a = start.a * oppAmount + end.a * amount;
John Reck1bcacfd2017-11-03 10:12:19 -0700202 float* d = (float*)dst;
Romain Guyf9037da2016-10-12 18:29:06 -0700203#ifdef ANDROID_ENABLE_LINEAR_BLENDING
Romain Guy6183c972017-03-16 12:24:55 -0700204 // We want to stay linear
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400205 *d++ = (start.r * oppAmount + end.r * amount);
206 *d++ = (start.g * oppAmount + end.g * amount);
207 *d++ = (start.b * oppAmount + end.b * amount);
Romain Guy8762e332016-10-12 12:14:07 -0700208#else
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400209 *d++ = OECF(start.r * oppAmount + end.r * amount);
210 *d++ = OECF(start.g * oppAmount + end.g * amount);
211 *d++ = OECF(start.b * oppAmount + end.b * amount);
Romain Guy8762e332016-10-12 12:14:07 -0700212#endif
Romain Guya0ed6f02016-12-12 18:21:32 -0800213 *d++ = a;
Romain Guyb4880042013-04-05 11:17:55 -0700214 dst += 4 * sizeof(float);
215}
216
John Reck1bcacfd2017-11-03 10:12:19 -0700217void GradientCache::generateTexture(uint32_t* colors, float* positions, const uint32_t width,
218 const uint32_t height, Texture* texture) {
Romain Guy253f2c22016-09-28 17:34:42 -0700219 const GLsizei rowBytes = width * sourceBytesPerPixel();
John Reck38e0c322015-11-10 12:19:17 -0800220 uint8_t pixels[rowBytes * height];
Romain Guyb4880042013-04-05 11:17:55 -0700221
Romain Guyb4880042013-04-05 11:17:55 -0700222 static ChannelMixer gMixers[] = {
Romain Guya0ed6f02016-12-12 18:21:32 -0800223 // colors are stored gamma-encoded
224 &android::uirenderer::GradientCache::mixBytes,
225 // colors are stored in linear (linear blending on)
226 // or gamma-encoded (linear blending off)
227 &android::uirenderer::GradientCache::mixFloats,
Romain Guyb4880042013-04-05 11:17:55 -0700228 };
229 ChannelMixer mix = gMixers[mUseFloatTexture];
230
Romain Guy253f2c22016-09-28 17:34:42 -0700231 FloatColor start;
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400232 start.set(colors[0]);
Romain Guyb4880042013-04-05 11:17:55 -0700233
Romain Guy253f2c22016-09-28 17:34:42 -0700234 FloatColor end;
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400235 end.set(colors[1]);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700236
237 int currentPos = 1;
Romain Guyb4880042013-04-05 11:17:55 -0700238 float startPos = positions[0];
239 float distance = positions[1] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700240
Romain Guyb4880042013-04-05 11:17:55 -0700241 uint8_t* dst = pixels;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700242 for (uint32_t x = 0; x < width; x++) {
243 float pos = x / float(width - 1);
244 if (pos > positions[currentPos]) {
Romain Guyb4880042013-04-05 11:17:55 -0700245 start = end;
246 startPos = positions[currentPos];
Romain Guy42e1e0d2012-07-30 14:47:51 -0700247
248 currentPos++;
249
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400250 end.set(colors[currentPos]);
Romain Guyb4880042013-04-05 11:17:55 -0700251 distance = positions[currentPos] - startPos;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700252 }
253
Romain Guyb4880042013-04-05 11:17:55 -0700254 float amount = (pos - startPos) / distance;
255 (this->*mix)(start, end, amount, dst);
Romain Guyc0ac1932010-07-19 18:43:02 -0700256 }
257
Romain Guyb4880042013-04-05 11:17:55 -0700258 memcpy(pixels + rowBytes, pixels, rowBytes);
Romain Guyc0ac1932010-07-19 18:43:02 -0700259
Romain Guyb4880042013-04-05 11:17:55 -0700260 if (mUseFloatTexture) {
John Reck9372ac32016-01-19 11:46:52 -0800261 texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700262 } else {
Romain Guyefb4b062017-02-27 11:00:04 -0800263 GLint internalFormat = mHasLinearBlending ? GL_SRGB8_ALPHA8 : GL_RGBA;
Romain Guy253f2c22016-09-28 17:34:42 -0700264 texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Romain Guyb4880042013-04-05 11:17:55 -0700265 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700266
Romain Guy39d252a2011-12-12 18:14:06 -0800267 texture->setFilter(GL_LINEAR);
268 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700269}
270
John Reck1bcacfd2017-11-03 10:12:19 -0700271}; // namespace uirenderer
272}; // namespace android