blob: 0bcd83a1a0506876a0d149c469d0d290169c903c [file] [log] [blame]
Romain Guyb45c0c92010-08-26 20:35:23 -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 Guyc9855a52011-01-21 21:14:15 -080017#include "Debug.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070018#include "GammaFontRenderer.h"
19#include "Properties.h"
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
Romain Guy41210632012-07-16 17:04:24 -070025// Utils
26///////////////////////////////////////////////////////////////////////////////
27
28static int luminance(const SkPaint* paint) {
29 uint32_t c = paint->getColor();
30 const int r = (c >> 16) & 0xFF;
31 const int g = (c >> 8) & 0xFF;
32 const int b = (c ) & 0xFF;
33 return (r * 2 + g * 5 + b) >> 3;
34}
35
36///////////////////////////////////////////////////////////////////////////////
Romain Guyb1d0a4e2012-07-13 18:25:35 -070037// Base class GammaFontRenderer
Romain Guyb45c0c92010-08-26 20:35:23 -070038///////////////////////////////////////////////////////////////////////////////
39
Romain Guyb1d0a4e2012-07-13 18:25:35 -070040GammaFontRenderer* GammaFontRenderer::createRenderer() {
41 // Choose the best renderer
42 char property[PROPERTY_VALUE_MAX];
Romain Guydfab50d2012-07-18 17:06:37 -070043 if (property_get(PROPERTY_TEXT_GAMMA_METHOD, property, DEFAULT_TEXT_GAMMA_METHOD) > 0) {
44 if (!strcasecmp(property, "lookup")) {
45 return new LookupGammaFontRenderer();
46 } else if (!strcasecmp(property, "shader")) {
Romain Guy6e25e382012-07-18 15:50:29 -070047 return new ShaderGammaFontRenderer(false);
48 } else if (!strcasecmp(property, "shader3")) {
49 return new ShaderGammaFontRenderer(true);
Romain Guyb1d0a4e2012-07-13 18:25:35 -070050 }
51 }
Romain Guyb45c0c92010-08-26 20:35:23 -070052
Romain Guy6e25e382012-07-18 15:50:29 -070053 return new Lookup3GammaFontRenderer();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070054}
55
56GammaFontRenderer::GammaFontRenderer() {
Romain Guyb45c0c92010-08-26 20:35:23 -070057 // Get the renderer properties
58 char property[PROPERTY_VALUE_MAX];
59
60 // Get the gamma
Romain Guyb1d0a4e2012-07-13 18:25:35 -070061 mGamma = DEFAULT_TEXT_GAMMA;
Chris Craikd41c4d82015-01-05 15:51:13 -080062 if (property_get(PROPERTY_TEXT_GAMMA, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080063 INIT_LOGD(" Setting text gamma to %s", property);
Romain Guyb1d0a4e2012-07-13 18:25:35 -070064 mGamma = atof(property);
Romain Guyb45c0c92010-08-26 20:35:23 -070065 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080066 INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA);
Romain Guyb45c0c92010-08-26 20:35:23 -070067 }
68
69 // Get the black gamma threshold
70 mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
Chris Craikd41c4d82015-01-05 15:51:13 -080071 if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080072 INIT_LOGD(" Setting text black gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070073 mBlackThreshold = atoi(property);
74 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080075 INIT_LOGD(" Using default text black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070076 DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD);
77 }
78
79 // Get the white gamma threshold
80 mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
Chris Craikd41c4d82015-01-05 15:51:13 -080081 if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080082 INIT_LOGD(" Setting text white gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070083 mWhiteThreshold = atoi(property);
84 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080085 INIT_LOGD(" Using default white black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070086 DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD);
87 }
Romain Guyb1d0a4e2012-07-13 18:25:35 -070088}
89
90GammaFontRenderer::~GammaFontRenderer() {
91}
92
93///////////////////////////////////////////////////////////////////////////////
94// Shader-based renderer
95///////////////////////////////////////////////////////////////////////////////
96
Chris Craik6c15ffa2015-02-02 13:50:55 -080097ShaderGammaFontRenderer::ShaderGammaFontRenderer(bool multiGamma)
98 : GammaFontRenderer() {
Romain Guyb1d0a4e2012-07-13 18:25:35 -070099 INIT_LOGD("Creating shader gamma font renderer");
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 mRenderer = nullptr;
Romain Guy6e25e382012-07-18 15:50:29 -0700101 mMultiGamma = multiGamma;
Romain Guy41210632012-07-16 17:04:24 -0700102}
103
104void ShaderGammaFontRenderer::describe(ProgramDescription& description,
105 const SkPaint* paint) const {
Chris Craikd41c4d82015-01-05 15:51:13 -0800106 if (paint->getShader() == nullptr) {
Romain Guy6e25e382012-07-18 15:50:29 -0700107 if (mMultiGamma) {
108 const int l = luminance(paint);
Romain Guy41210632012-07-16 17:04:24 -0700109
Romain Guy6e25e382012-07-18 15:50:29 -0700110 if (l <= mBlackThreshold) {
111 description.hasGammaCorrection = true;
112 description.gamma = mGamma;
113 } else if (l >= mWhiteThreshold) {
114 description.hasGammaCorrection = true;
115 description.gamma = 1.0f / mGamma;
116 }
117 } else {
Romain Guy41210632012-07-16 17:04:24 -0700118 description.hasGammaCorrection = true;
119 description.gamma = 1.0f / mGamma;
120 }
121 }
122}
123
124void ShaderGammaFontRenderer::setupProgram(ProgramDescription& description,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800125 Program& program) const {
Romain Guy41210632012-07-16 17:04:24 -0700126 if (description.hasGammaCorrection) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800127 glUniform1f(program.getUniform("gamma"), description.gamma);
Romain Guy41210632012-07-16 17:04:24 -0700128 }
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700129}
130
Romain Guycf51a412013-04-08 19:40:31 -0700131void ShaderGammaFontRenderer::endPrecaching() {
132 if (mRenderer) {
133 mRenderer->endPrecaching();
134 }
135}
136
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700137///////////////////////////////////////////////////////////////////////////////
138// Lookup-based renderer
139///////////////////////////////////////////////////////////////////////////////
140
Chris Craik6c15ffa2015-02-02 13:50:55 -0800141LookupGammaFontRenderer::LookupGammaFontRenderer()
142 : GammaFontRenderer() {
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700143 INIT_LOGD("Creating lookup gamma font renderer");
Romain Guyb45c0c92010-08-26 20:35:23 -0700144
145 // Compute the gamma tables
Romain Guy6e25e382012-07-18 15:50:29 -0700146 const float gamma = 1.0f / mGamma;
147
148 for (uint32_t i = 0; i <= 255; i++) {
149 mGammaTable[i] = uint8_t((float)::floor(pow(i / 255.0f, gamma) * 255.0f + 0.5f));
150 }
151
Chris Craikd41c4d82015-01-05 15:51:13 -0800152 mRenderer = nullptr;
Romain Guy6e25e382012-07-18 15:50:29 -0700153}
154
Romain Guycf51a412013-04-08 19:40:31 -0700155void LookupGammaFontRenderer::endPrecaching() {
156 if (mRenderer) {
157 mRenderer->endPrecaching();
158 }
159}
160
Romain Guy6e25e382012-07-18 15:50:29 -0700161///////////////////////////////////////////////////////////////////////////////
162// Lookup-based renderer, using 3 different correction tables
163///////////////////////////////////////////////////////////////////////////////
164
Chris Craik6c15ffa2015-02-02 13:50:55 -0800165Lookup3GammaFontRenderer::Lookup3GammaFontRenderer()
166 : GammaFontRenderer() {
Romain Guy6e25e382012-07-18 15:50:29 -0700167 INIT_LOGD("Creating lookup3 gamma font renderer");
168
169 // Compute the gamma tables
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700170 const float blackGamma = mGamma;
171 const float whiteGamma = 1.0f / mGamma;
Romain Guyb45c0c92010-08-26 20:35:23 -0700172
173 for (uint32_t i = 0; i <= 255; i++) {
Romain Guyb45c0c92010-08-26 20:35:23 -0700174 const float v = i / 255.0f;
175 const float black = pow(v, blackGamma);
176 const float white = pow(v, whiteGamma);
177
Romain Guy6e25e382012-07-18 15:50:29 -0700178 mGammaTable[i] = i;
Romain Guyeca0ca22011-11-04 15:12:29 -0700179 mGammaTable[256 + i] = uint8_t((float)::floor(black * 255.0f + 0.5f));
180 mGammaTable[512 + i] = uint8_t((float)::floor(white * 255.0f + 0.5f));
Romain Guyb45c0c92010-08-26 20:35:23 -0700181 }
182
Romain Guyeca0ca22011-11-04 15:12:29 -0700183 memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount);
184 memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount);
185}
186
Romain Guycf51a412013-04-08 19:40:31 -0700187void Lookup3GammaFontRenderer::endPrecaching() {
188 for (int i = 0; i < kGammaCount; i++) {
189 if (mRenderers[i]) {
190 mRenderers[i]->endPrecaching();
191 }
192 }
193}
194
Romain Guy6e25e382012-07-18 15:50:29 -0700195void Lookup3GammaFontRenderer::clear() {
Romain Guyeca0ca22011-11-04 15:12:29 -0700196 for (int i = 0; i < kGammaCount; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800197 mRenderers[i].release();
Romain Guyeca0ca22011-11-04 15:12:29 -0700198 }
199}
200
Romain Guy6e25e382012-07-18 15:50:29 -0700201void Lookup3GammaFontRenderer::flush() {
Romain Guyeca0ca22011-11-04 15:12:29 -0700202 int count = 0;
203 int min = -1;
204 uint32_t minCount = UINT_MAX;
205
206 for (int i = 0; i < kGammaCount; i++) {
207 if (mRenderers[i]) {
208 count++;
209 if (mRenderersUsageCount[i] < minCount) {
210 minCount = mRenderersUsageCount[i];
211 min = i;
212 }
213 }
214 }
215
216 if (count <= 1 || min < 0) return;
217
Chris Craik51d6a3d2014-12-22 17:16:56 -0800218 mRenderers[min].release();
Chet Haase9a824562011-12-16 15:44:59 -0800219
220 // Also eliminate the caches for large glyphs, as they consume significant memory
221 for (int i = 0; i < kGammaCount; ++i) {
222 if (mRenderers[i]) {
223 mRenderers[i]->flushLargeCaches();
224 }
225 }
Romain Guyeca0ca22011-11-04 15:12:29 -0700226}
227
Romain Guy6e25e382012-07-18 15:50:29 -0700228FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800229 if (!mRenderers[gamma]) {
230 mRenderers[gamma].reset(new FontRenderer());
231 mRenderers[gamma]->setGammaTable(&mGammaTable[gamma * 256]);
Romain Guyeca0ca22011-11-04 15:12:29 -0700232 }
233 mRenderersUsageCount[gamma]++;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800234 return mRenderers[gamma].get();
Romain Guyb45c0c92010-08-26 20:35:23 -0700235}
236
Romain Guy6e25e382012-07-18 15:50:29 -0700237FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800238 if (paint->getShader() == nullptr) {
Romain Guy41210632012-07-16 17:04:24 -0700239 const int l = luminance(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -0700240
Romain Guy41210632012-07-16 17:04:24 -0700241 if (l <= mBlackThreshold) {
Romain Guyeca0ca22011-11-04 15:12:29 -0700242 return *getRenderer(kGammaBlack);
Romain Guy41210632012-07-16 17:04:24 -0700243 } else if (l >= mWhiteThreshold) {
Romain Guyeca0ca22011-11-04 15:12:29 -0700244 return *getRenderer(kGammaWhite);
Romain Guyb45c0c92010-08-26 20:35:23 -0700245 }
246 }
Romain Guyeca0ca22011-11-04 15:12:29 -0700247 return *getRenderer(kGammaDefault);
Romain Guyb45c0c92010-08-26 20:35:23 -0700248}
249
250}; // namespace uirenderer
251}; // namespace android