blob: e8362dcd45d2ea1e50f09d1decae617259ef11f7 [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///////////////////////////////////////////////////////////////////////////////
27// Constructors/destructor
28///////////////////////////////////////////////////////////////////////////////
29
30GammaFontRenderer::GammaFontRenderer() {
Romain Guyc9855a52011-01-21 21:14:15 -080031 INIT_LOGD("Creating gamma font renderer");
Romain Guyb45c0c92010-08-26 20:35:23 -070032
33 // Get the renderer properties
34 char property[PROPERTY_VALUE_MAX];
35
36 // Get the gamma
37 float gamma = DEFAULT_TEXT_GAMMA;
38 if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080039 INIT_LOGD(" Setting text gamma to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070040 gamma = atof(property);
41 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080042 INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA);
Romain Guyb45c0c92010-08-26 20:35:23 -070043 }
44
45 // Get the black gamma threshold
46 mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD;
47 if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080048 INIT_LOGD(" Setting text black gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070049 mBlackThreshold = atoi(property);
50 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080051 INIT_LOGD(" Using default text black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070052 DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD);
53 }
54
55 // Get the white gamma threshold
56 mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD;
57 if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080058 INIT_LOGD(" Setting text white gamma threshold to %s", property);
Romain Guyb45c0c92010-08-26 20:35:23 -070059 mWhiteThreshold = atoi(property);
60 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080061 INIT_LOGD(" Using default white black gamma threshold of %d",
Romain Guyb45c0c92010-08-26 20:35:23 -070062 DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD);
63 }
64
65 // Compute the gamma tables
66 const float blackGamma = gamma;
67 const float whiteGamma = 1.0f / gamma;
68
69 for (uint32_t i = 0; i <= 255; i++) {
70 mDefault[i] = i;
71
72 const float v = i / 255.0f;
73 const float black = pow(v, blackGamma);
74 const float white = pow(v, whiteGamma);
75
76 mBlackGamma[i] = uint8_t((float)::floor(black * 255.0f + 0.5f));
77 mWhiteGamma[i] = uint8_t((float)::floor(white * 255.0f + 0.5f));
78 }
79
80 // Configure the font renderers
81 mDefaultRenderer.setGammaTable(&mDefault[0]);
82 mBlackGammaRenderer.setGammaTable(&mBlackGamma[0]);
83 mWhiteGammaRenderer.setGammaTable(&mWhiteGamma[0]);
84}
85
86FontRenderer& GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
87 if (paint->getShader() == NULL) {
88 uint32_t c = paint->getColor();
89 const int r = (c >> 16) & 0xFF;
90 const int g = (c >> 8) & 0xFF;
91 const int b = (c ) & 0xFF;
92 const int luminance = (r * 2 + g * 5 + b) >> 3;
93
94 if (luminance <= mBlackThreshold) {
95 return mBlackGammaRenderer;
96 } else if (luminance >= mWhiteThreshold) {
97 return mWhiteGammaRenderer;
98 }
99 }
100 return mDefaultRenderer;
101}
102
103}; // namespace uirenderer
104}; // namespace android