blob: fe532abca29c1b0d84e7d1bad891753f42da80f5 [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
17#define LOG_TAG "OpenGLRenderer"
18
Romain Guyc9855a52011-01-21 21:14:15 -080019#include "Debug.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070020#include "GammaFontRenderer.h"
21#include "Properties.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
Romain Guy41210632012-07-16 17:04:24 -070027// Utils
28///////////////////////////////////////////////////////////////////////////////
29
30static int luminance(const SkPaint* paint) {
31 uint32_t c = paint->getColor();
32 const int r = (c >> 16) & 0xFF;
33 const int g = (c >> 8) & 0xFF;
34 const int b = (c ) & 0xFF;
35 return (r * 2 + g * 5 + b) >> 3;
36}
37
38///////////////////////////////////////////////////////////////////////////////
Romain Guyb1d0a4e2012-07-13 18:25:35 -070039// Base class GammaFontRenderer
Romain Guyb45c0c92010-08-26 20:35:23 -070040///////////////////////////////////////////////////////////////////////////////
41
Romain Guyb1d0a4e2012-07-13 18:25:35 -070042GammaFontRenderer* GammaFontRenderer::createRenderer() {
43 // Choose the best renderer
44 char property[PROPERTY_VALUE_MAX];
45 if (property_get(PROPERTY_TEXT_GAMMA_SHADER, property, DEFAULT_TEXT_GAMMA_SHADER) > 0) {
Romain Guy6e25e382012-07-18 15:50:29 -070046 if (!strcasecmp(property, "shader")) {
47 return new ShaderGammaFontRenderer(false);
48 } else if (!strcasecmp(property, "shader3")) {
49 return new ShaderGammaFontRenderer(true);
50 } else if (!strcasecmp(property, "lookup")) {
51 return new LookupGammaFontRenderer();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070052 }
53 }
Romain Guyb45c0c92010-08-26 20:35:23 -070054
Romain Guy6e25e382012-07-18 15:50:29 -070055 return new Lookup3GammaFontRenderer();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070056}
57
58GammaFontRenderer::GammaFontRenderer() {
Romain Guyb45c0c92010-08-26 20:35:23 -070059 // Get the renderer properties
60 char property[PROPERTY_VALUE_MAX];
61
62 // Get the gamma
Romain Guyb1d0a4e2012-07-13 18:25:35 -070063 mGamma = DEFAULT_TEXT_GAMMA;
Romain Guyb45c0c92010-08-26 20:35:23 -070064 if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080065 INIT_LOGD(" Setting text gamma to %s", property);
Romain Guyb1d0a4e2012-07-13 18:25:35 -070066 mGamma = atof(property);
Romain Guyb45c0c92010-08-26 20:35:23 -070067 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080068 INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA);
Romain Guyb45c0c92010-08-26 20:35:23 -070069 }
70
71 // Get the black gamma threshold
72 mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
73 if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080074 INIT_LOGD(" Setting text black gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070075 mBlackThreshold = atoi(property);
76 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080077 INIT_LOGD(" Using default text black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070078 DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD);
79 }
80
81 // Get the white gamma threshold
82 mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
83 if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080084 INIT_LOGD(" Setting text white gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070085 mWhiteThreshold = atoi(property);
86 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080087 INIT_LOGD(" Using default white black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070088 DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD);
89 }
Romain Guyb1d0a4e2012-07-13 18:25:35 -070090}
91
92GammaFontRenderer::~GammaFontRenderer() {
93}
94
95///////////////////////////////////////////////////////////////////////////////
96// Shader-based renderer
97///////////////////////////////////////////////////////////////////////////////
98
Romain Guy6e25e382012-07-18 15:50:29 -070099ShaderGammaFontRenderer::ShaderGammaFontRenderer(bool multiGamma): GammaFontRenderer() {
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700100 INIT_LOGD("Creating shader gamma font renderer");
Romain Guy41210632012-07-16 17:04:24 -0700101 mRenderer = NULL;
Romain Guy6e25e382012-07-18 15:50:29 -0700102 mMultiGamma = multiGamma;
Romain Guy41210632012-07-16 17:04:24 -0700103}
104
105void ShaderGammaFontRenderer::describe(ProgramDescription& description,
106 const SkPaint* paint) const {
107 if (paint->getShader() == NULL) {
Romain Guy6e25e382012-07-18 15:50:29 -0700108 if (mMultiGamma) {
109 const int l = luminance(paint);
Romain Guy41210632012-07-16 17:04:24 -0700110
Romain Guy6e25e382012-07-18 15:50:29 -0700111 if (l <= mBlackThreshold) {
112 description.hasGammaCorrection = true;
113 description.gamma = mGamma;
114 } else if (l >= mWhiteThreshold) {
115 description.hasGammaCorrection = true;
116 description.gamma = 1.0f / mGamma;
117 }
118 } else {
Romain Guy41210632012-07-16 17:04:24 -0700119 description.hasGammaCorrection = true;
120 description.gamma = 1.0f / mGamma;
121 }
122 }
123}
124
125void ShaderGammaFontRenderer::setupProgram(ProgramDescription& description,
126 Program* program) const {
127 if (description.hasGammaCorrection) {
128 glUniform1f(program->getUniform("gamma"), description.gamma);
129 }
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700130}
131
132///////////////////////////////////////////////////////////////////////////////
133// Lookup-based renderer
134///////////////////////////////////////////////////////////////////////////////
135
136LookupGammaFontRenderer::LookupGammaFontRenderer(): GammaFontRenderer() {
137 INIT_LOGD("Creating lookup gamma font renderer");
Romain Guyb45c0c92010-08-26 20:35:23 -0700138
139 // Compute the gamma tables
Romain Guy6e25e382012-07-18 15:50:29 -0700140 const float gamma = 1.0f / mGamma;
141
142 for (uint32_t i = 0; i <= 255; i++) {
143 mGammaTable[i] = uint8_t((float)::floor(pow(i / 255.0f, gamma) * 255.0f + 0.5f));
144 }
145
146 mRenderer = NULL;
147}
148
149///////////////////////////////////////////////////////////////////////////////
150// Lookup-based renderer, using 3 different correction tables
151///////////////////////////////////////////////////////////////////////////////
152
153Lookup3GammaFontRenderer::Lookup3GammaFontRenderer(): GammaFontRenderer() {
154 INIT_LOGD("Creating lookup3 gamma font renderer");
155
156 // Compute the gamma tables
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700157 const float blackGamma = mGamma;
158 const float whiteGamma = 1.0f / mGamma;
Romain Guyb45c0c92010-08-26 20:35:23 -0700159
160 for (uint32_t i = 0; i <= 255; i++) {
Romain Guyb45c0c92010-08-26 20:35:23 -0700161 const float v = i / 255.0f;
162 const float black = pow(v, blackGamma);
163 const float white = pow(v, whiteGamma);
164
Romain Guy6e25e382012-07-18 15:50:29 -0700165 mGammaTable[i] = i;
Romain Guyeca0ca22011-11-04 15:12:29 -0700166 mGammaTable[256 + i] = uint8_t((float)::floor(black * 255.0f + 0.5f));
167 mGammaTable[512 + i] = uint8_t((float)::floor(white * 255.0f + 0.5f));
Romain Guyb45c0c92010-08-26 20:35:23 -0700168 }
169
Romain Guyeca0ca22011-11-04 15:12:29 -0700170 memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount);
171 memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount);
172}
173
Romain Guy6e25e382012-07-18 15:50:29 -0700174Lookup3GammaFontRenderer::~Lookup3GammaFontRenderer() {
Romain Guyeca0ca22011-11-04 15:12:29 -0700175 for (int i = 0; i < kGammaCount; i++) {
176 delete mRenderers[i];
177 }
178}
179
Romain Guy6e25e382012-07-18 15:50:29 -0700180void Lookup3GammaFontRenderer::clear() {
Romain Guyeca0ca22011-11-04 15:12:29 -0700181 for (int i = 0; i < kGammaCount; i++) {
182 delete mRenderers[i];
183 mRenderers[i] = NULL;
184 }
185}
186
Romain Guy6e25e382012-07-18 15:50:29 -0700187void Lookup3GammaFontRenderer::flush() {
Romain Guyeca0ca22011-11-04 15:12:29 -0700188 int count = 0;
189 int min = -1;
190 uint32_t minCount = UINT_MAX;
191
192 for (int i = 0; i < kGammaCount; i++) {
193 if (mRenderers[i]) {
194 count++;
195 if (mRenderersUsageCount[i] < minCount) {
196 minCount = mRenderersUsageCount[i];
197 min = i;
198 }
199 }
200 }
201
202 if (count <= 1 || min < 0) return;
203
204 delete mRenderers[min];
205 mRenderers[min] = NULL;
Chet Haase9a824562011-12-16 15:44:59 -0800206
207 // Also eliminate the caches for large glyphs, as they consume significant memory
208 for (int i = 0; i < kGammaCount; ++i) {
209 if (mRenderers[i]) {
210 mRenderers[i]->flushLargeCaches();
211 }
212 }
Romain Guyeca0ca22011-11-04 15:12:29 -0700213}
214
Romain Guy6e25e382012-07-18 15:50:29 -0700215FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) {
Romain Guyeca0ca22011-11-04 15:12:29 -0700216 FontRenderer* renderer = mRenderers[gamma];
217 if (!renderer) {
218 renderer = new FontRenderer();
219 mRenderers[gamma] = renderer;
220 renderer->setGammaTable(&mGammaTable[gamma * 256]);
221 }
222 mRenderersUsageCount[gamma]++;
223 return renderer;
Romain Guyb45c0c92010-08-26 20:35:23 -0700224}
225
Romain Guy6e25e382012-07-18 15:50:29 -0700226FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
Romain Guyb45c0c92010-08-26 20:35:23 -0700227 if (paint->getShader() == NULL) {
Romain Guy41210632012-07-16 17:04:24 -0700228 const int l = luminance(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -0700229
Romain Guy41210632012-07-16 17:04:24 -0700230 if (l <= mBlackThreshold) {
Romain Guyeca0ca22011-11-04 15:12:29 -0700231 return *getRenderer(kGammaBlack);
Romain Guy41210632012-07-16 17:04:24 -0700232 } else if (l >= mWhiteThreshold) {
Romain Guyeca0ca22011-11-04 15:12:29 -0700233 return *getRenderer(kGammaWhite);
Romain Guyb45c0c92010-08-26 20:35:23 -0700234 }
235 }
Romain Guyeca0ca22011-11-04 15:12:29 -0700236 return *getRenderer(kGammaDefault);
Romain Guyb45c0c92010-08-26 20:35:23 -0700237}
238
239}; // namespace uirenderer
240}; // namespace android