GammaFontRenderer.cpp revision b45c0c9774bd19a9dbe77d149abae4e124b08bf6
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 "GammaFontRenderer.h"
20#include "Properties.h"
21
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Constructors/destructor
27///////////////////////////////////////////////////////////////////////////////
28
29GammaFontRenderer::GammaFontRenderer() {
30    LOGD("Creating gamma font renderer");
31
32    // Get the renderer properties
33    char property[PROPERTY_VALUE_MAX];
34
35    // Get the gamma
36    float gamma = DEFAULT_TEXT_GAMMA;
37    if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) {
38        LOGD("  Setting text gamma to %s", property);
39        gamma = atof(property);
40    } else {
41        LOGD("  Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA);
42    }
43
44    // Get the black gamma threshold
45    mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
46    if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) {
47        LOGD("  Setting text black gamma threshold to %s", property);
48        mBlackThreshold = atoi(property);
49    } else {
50        LOGD("  Using default text black gamma threshold of %d",
51                DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD);
52    }
53
54    // Get the white gamma threshold
55    mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
56    if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) {
57        LOGD("  Setting text white gamma threshold to %s", property);
58        mWhiteThreshold = atoi(property);
59    } else {
60        LOGD("  Using default white black gamma threshold of %d",
61                DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD);
62    }
63
64    // Compute the gamma tables
65    const float blackGamma = gamma;
66    const float whiteGamma = 1.0f / gamma;
67
68    for (uint32_t i = 0; i <= 255; i++) {
69        mDefault[i] = i;
70
71        const float v = i / 255.0f;
72        const float black = pow(v, blackGamma);
73        const float white = pow(v, whiteGamma);
74
75        mBlackGamma[i] = uint8_t((float)::floor(black * 255.0f + 0.5f));
76        mWhiteGamma[i] = uint8_t((float)::floor(white * 255.0f + 0.5f));
77    }
78
79    // Configure the font renderers
80    mDefaultRenderer.setGammaTable(&mDefault[0]);
81    mBlackGammaRenderer.setGammaTable(&mBlackGamma[0]);
82    mWhiteGammaRenderer.setGammaTable(&mWhiteGamma[0]);
83}
84
85FontRenderer& GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
86    if (paint->getShader() == NULL) {
87        uint32_t c = paint->getColor();
88        const int r = (c >> 16) & 0xFF;
89        const int g = (c >>  8) & 0xFF;
90        const int b = (c      ) & 0xFF;
91        const int luminance = (r * 2 + g * 5 + b) >> 3;
92
93        if (luminance <= mBlackThreshold) {
94            return mBlackGammaRenderer;
95        } else if (luminance >= mWhiteThreshold) {
96            return mWhiteGammaRenderer;
97        }
98    }
99    return mDefaultRenderer;
100}
101
102}; // namespace uirenderer
103}; // namespace android
104