Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 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 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 20 | #include "GammaFontRenderer.h" |
| 21 | #include "Properties.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 27 | // Utils |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | static 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 Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 39 | // Base class GammaFontRenderer |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 42 | GammaFontRenderer* GammaFontRenderer::createRenderer() { |
| 43 | // Choose the best renderer |
| 44 | char property[PROPERTY_VALUE_MAX]; |
Romain Guy | dfab50d | 2012-07-18 17:06:37 -0700 | [diff] [blame] | 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")) { |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 49 | return new ShaderGammaFontRenderer(false); |
| 50 | } else if (!strcasecmp(property, "shader3")) { |
| 51 | return new ShaderGammaFontRenderer(true); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 54 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 55 | return new Lookup3GammaFontRenderer(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | GammaFontRenderer::GammaFontRenderer() { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 59 | // Get the renderer properties |
| 60 | char property[PROPERTY_VALUE_MAX]; |
| 61 | |
| 62 | // Get the gamma |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 63 | mGamma = DEFAULT_TEXT_GAMMA; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 64 | if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 65 | INIT_LOGD(" Setting text gamma to %s", property); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 66 | mGamma = atof(property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 67 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 68 | INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 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) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 74 | INIT_LOGD(" Setting text black gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 75 | mBlackThreshold = atoi(property); |
| 76 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 77 | INIT_LOGD(" Using default text black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 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) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 84 | INIT_LOGD(" Setting text white gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 85 | mWhiteThreshold = atoi(property); |
| 86 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 87 | INIT_LOGD(" Using default white black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 88 | DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD); |
| 89 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | GammaFontRenderer::~GammaFontRenderer() { |
| 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | // Shader-based renderer |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 99 | ShaderGammaFontRenderer::ShaderGammaFontRenderer(bool multiGamma): GammaFontRenderer() { |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 100 | INIT_LOGD("Creating shader gamma font renderer"); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 101 | mRenderer = NULL; |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 102 | mMultiGamma = multiGamma; |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void ShaderGammaFontRenderer::describe(ProgramDescription& description, |
| 106 | const SkPaint* paint) const { |
| 107 | if (paint->getShader() == NULL) { |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 108 | if (mMultiGamma) { |
| 109 | const int l = luminance(paint); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 110 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 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 { |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 119 | description.hasGammaCorrection = true; |
| 120 | description.gamma = 1.0f / mGamma; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void ShaderGammaFontRenderer::setupProgram(ProgramDescription& description, |
| 126 | Program* program) const { |
| 127 | if (description.hasGammaCorrection) { |
| 128 | glUniform1f(program->getUniform("gamma"), description.gamma); |
| 129 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | // Lookup-based renderer |
| 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | LookupGammaFontRenderer::LookupGammaFontRenderer(): GammaFontRenderer() { |
| 137 | INIT_LOGD("Creating lookup gamma font renderer"); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 138 | |
| 139 | // Compute the gamma tables |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 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 | |
| 153 | Lookup3GammaFontRenderer::Lookup3GammaFontRenderer(): GammaFontRenderer() { |
| 154 | INIT_LOGD("Creating lookup3 gamma font renderer"); |
| 155 | |
| 156 | // Compute the gamma tables |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 157 | const float blackGamma = mGamma; |
| 158 | const float whiteGamma = 1.0f / mGamma; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 159 | |
| 160 | for (uint32_t i = 0; i <= 255; i++) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 161 | const float v = i / 255.0f; |
| 162 | const float black = pow(v, blackGamma); |
| 163 | const float white = pow(v, whiteGamma); |
| 164 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 165 | mGammaTable[i] = i; |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 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)); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 170 | memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount); |
| 171 | memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount); |
| 172 | } |
| 173 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 174 | Lookup3GammaFontRenderer::~Lookup3GammaFontRenderer() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 175 | for (int i = 0; i < kGammaCount; i++) { |
| 176 | delete mRenderers[i]; |
| 177 | } |
| 178 | } |
| 179 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 180 | void Lookup3GammaFontRenderer::clear() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 181 | for (int i = 0; i < kGammaCount; i++) { |
| 182 | delete mRenderers[i]; |
| 183 | mRenderers[i] = NULL; |
| 184 | } |
| 185 | } |
| 186 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 187 | void Lookup3GammaFontRenderer::flush() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 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; |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 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 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 215 | FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 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; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 226 | FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 227 | if (paint->getShader() == NULL) { |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 228 | const int l = luminance(paint); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 229 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 230 | if (l <= mBlackThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 231 | return *getRenderer(kGammaBlack); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 232 | } else if (l >= mWhiteThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 233 | return *getRenderer(kGammaWhite); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 236 | return *getRenderer(kGammaDefault); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | }; // namespace uirenderer |
| 240 | }; // namespace android |