1/*
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
19#include "Debug.h"
20#include "GammaFontRenderer.h"
21#include "Properties.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// 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///////////////////////////////////////////////////////////////////////////////
39// Base class GammaFontRenderer
40///////////////////////////////////////////////////////////////////////////////
41
42GammaFontRenderer* GammaFontRenderer::createRenderer() {
43    // Choose the best renderer
44    char property[PROPERTY_VALUE_MAX];
45    if (property_get(PROPERTY_TEXT_GAMMA_METHOD, property, DEFAULT_TEXT_GAMMA_METHOD) > 0) {
46        if (!strcasecmp(property, "lookup")) {
47            return new LookupGammaFontRenderer();
48        } else if (!strcasecmp(property, "shader")) {
49            return new ShaderGammaFontRenderer(false);
50        } else if (!strcasecmp(property, "shader3")) {
51            return new ShaderGammaFontRenderer(true);
52        }
53    }
54
55    return new Lookup3GammaFontRenderer();
56}
57
58GammaFontRenderer::GammaFontRenderer() {
59    // Get the renderer properties
60    char property[PROPERTY_VALUE_MAX];
61
62    // Get the gamma
63    mGamma = DEFAULT_TEXT_GAMMA;
64    if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) {
65        INIT_LOGD("  Setting text gamma to %s", property);
66        mGamma = atof(property);
67    } else {
68        INIT_LOGD("  Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA);
69    }
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) {
74        INIT_LOGD("  Setting text black gamma threshold to %s", property);
75        mBlackThreshold = atoi(property);
76    } else {
77        INIT_LOGD("  Using default text black gamma threshold of %d",
78                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) {
84        INIT_LOGD("  Setting text white gamma threshold to %s", property);
85        mWhiteThreshold = atoi(property);
86    } else {
87        INIT_LOGD("  Using default white black gamma threshold of %d",
88                DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD);
89    }
90}
91
92GammaFontRenderer::~GammaFontRenderer() {
93}
94
95///////////////////////////////////////////////////////////////////////////////
96// Shader-based renderer
97///////////////////////////////////////////////////////////////////////////////
98
99ShaderGammaFontRenderer::ShaderGammaFontRenderer(bool multiGamma): GammaFontRenderer() {
100    INIT_LOGD("Creating shader gamma font renderer");
101    mRenderer = NULL;
102    mMultiGamma = multiGamma;
103}
104
105void ShaderGammaFontRenderer::describe(ProgramDescription& description,
106        const SkPaint* paint) const {
107    if (paint->getShader() == NULL) {
108        if (mMultiGamma) {
109            const int l = luminance(paint);
110
111            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 {
119            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    }
130}
131
132///////////////////////////////////////////////////////////////////////////////
133// Lookup-based renderer
134///////////////////////////////////////////////////////////////////////////////
135
136LookupGammaFontRenderer::LookupGammaFontRenderer(): GammaFontRenderer() {
137    INIT_LOGD("Creating lookup gamma font renderer");
138
139    // Compute the gamma tables
140    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
157    const float blackGamma = mGamma;
158    const float whiteGamma = 1.0f / mGamma;
159
160    for (uint32_t i = 0; i <= 255; i++) {
161        const float v = i / 255.0f;
162        const float black = pow(v, blackGamma);
163        const float white = pow(v, whiteGamma);
164
165        mGammaTable[i] = i;
166        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));
168    }
169
170    memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount);
171    memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount);
172}
173
174Lookup3GammaFontRenderer::~Lookup3GammaFontRenderer() {
175    for (int i = 0; i < kGammaCount; i++) {
176        delete mRenderers[i];
177    }
178}
179
180void Lookup3GammaFontRenderer::clear() {
181    for (int i = 0; i < kGammaCount; i++) {
182        delete mRenderers[i];
183        mRenderers[i] = NULL;
184    }
185}
186
187void Lookup3GammaFontRenderer::flush() {
188    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;
206
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    }
213}
214
215FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) {
216    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;
224}
225
226FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
227    if (paint->getShader() == NULL) {
228        const int l = luminance(paint);
229
230        if (l <= mBlackThreshold) {
231            return *getRenderer(kGammaBlack);
232        } else if (l >= mWhiteThreshold) {
233            return *getRenderer(kGammaWhite);
234        }
235    }
236    return *getRenderer(kGammaDefault);
237}
238
239}; // namespace uirenderer
240}; // namespace android
241