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 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 17 | #include "Debug.h" |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 18 | #include "GammaFontRenderer.h" |
| 19 | #include "Properties.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 25 | // Utils |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | static int luminance(const SkPaint* paint) { |
| 29 | uint32_t c = paint->getColor(); |
| 30 | const int r = (c >> 16) & 0xFF; |
| 31 | const int g = (c >> 8) & 0xFF; |
| 32 | const int b = (c ) & 0xFF; |
| 33 | return (r * 2 + g * 5 + b) >> 3; |
| 34 | } |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 37 | // Base class GammaFontRenderer |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 40 | GammaFontRenderer* GammaFontRenderer::createRenderer() { |
| 41 | // Choose the best renderer |
| 42 | char property[PROPERTY_VALUE_MAX]; |
Romain Guy | dfab50d | 2012-07-18 17:06:37 -0700 | [diff] [blame] | 43 | if (property_get(PROPERTY_TEXT_GAMMA_METHOD, property, DEFAULT_TEXT_GAMMA_METHOD) > 0) { |
| 44 | if (!strcasecmp(property, "lookup")) { |
| 45 | return new LookupGammaFontRenderer(); |
| 46 | } else if (!strcasecmp(property, "shader")) { |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 47 | return new ShaderGammaFontRenderer(false); |
| 48 | } else if (!strcasecmp(property, "shader3")) { |
| 49 | return new ShaderGammaFontRenderer(true); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 52 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 53 | return new Lookup3GammaFontRenderer(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | GammaFontRenderer::GammaFontRenderer() { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 57 | // Get the renderer properties |
| 58 | char property[PROPERTY_VALUE_MAX]; |
| 59 | |
| 60 | // Get the gamma |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 61 | mGamma = DEFAULT_TEXT_GAMMA; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 62 | if (property_get(PROPERTY_TEXT_GAMMA, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 63 | INIT_LOGD(" Setting text gamma to %s", property); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 64 | mGamma = atof(property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 65 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 66 | INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Get the black gamma threshold |
| 70 | mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 71 | if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 72 | INIT_LOGD(" Setting text black gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 73 | mBlackThreshold = atoi(property); |
| 74 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 75 | INIT_LOGD(" Using default text black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 76 | DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD); |
| 77 | } |
| 78 | |
| 79 | // Get the white gamma threshold |
| 80 | mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 81 | if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 82 | INIT_LOGD(" Setting text white gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 83 | mWhiteThreshold = atoi(property); |
| 84 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 85 | INIT_LOGD(" Using default white black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 86 | DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD); |
| 87 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | GammaFontRenderer::~GammaFontRenderer() { |
| 91 | } |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | // Shader-based renderer |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 97 | ShaderGammaFontRenderer::ShaderGammaFontRenderer(bool multiGamma) |
| 98 | : GammaFontRenderer() { |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 99 | INIT_LOGD("Creating shader gamma font renderer"); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 100 | mRenderer = nullptr; |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 101 | mMultiGamma = multiGamma; |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ShaderGammaFontRenderer::describe(ProgramDescription& description, |
| 105 | const SkPaint* paint) const { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 106 | if (paint->getShader() == nullptr) { |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 107 | if (mMultiGamma) { |
| 108 | const int l = luminance(paint); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 109 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 110 | if (l <= mBlackThreshold) { |
| 111 | description.hasGammaCorrection = true; |
| 112 | description.gamma = mGamma; |
| 113 | } else if (l >= mWhiteThreshold) { |
| 114 | description.hasGammaCorrection = true; |
| 115 | description.gamma = 1.0f / mGamma; |
| 116 | } |
| 117 | } else { |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 118 | description.hasGammaCorrection = true; |
| 119 | description.gamma = 1.0f / mGamma; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void ShaderGammaFontRenderer::setupProgram(ProgramDescription& description, |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 125 | Program& program) const { |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 126 | if (description.hasGammaCorrection) { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 127 | glUniform1f(program.getUniform("gamma"), description.gamma); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 128 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 131 | void ShaderGammaFontRenderer::endPrecaching() { |
| 132 | if (mRenderer) { |
| 133 | mRenderer->endPrecaching(); |
| 134 | } |
| 135 | } |
| 136 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 137 | /////////////////////////////////////////////////////////////////////////////// |
| 138 | // Lookup-based renderer |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 141 | LookupGammaFontRenderer::LookupGammaFontRenderer() |
| 142 | : GammaFontRenderer() { |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 143 | INIT_LOGD("Creating lookup gamma font renderer"); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 144 | |
| 145 | // Compute the gamma tables |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 146 | const float gamma = 1.0f / mGamma; |
| 147 | |
| 148 | for (uint32_t i = 0; i <= 255; i++) { |
| 149 | mGammaTable[i] = uint8_t((float)::floor(pow(i / 255.0f, gamma) * 255.0f + 0.5f)); |
| 150 | } |
| 151 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 152 | mRenderer = nullptr; |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 155 | void LookupGammaFontRenderer::endPrecaching() { |
| 156 | if (mRenderer) { |
| 157 | mRenderer->endPrecaching(); |
| 158 | } |
| 159 | } |
| 160 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | // Lookup-based renderer, using 3 different correction tables |
| 163 | /////////////////////////////////////////////////////////////////////////////// |
| 164 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 165 | Lookup3GammaFontRenderer::Lookup3GammaFontRenderer() |
| 166 | : GammaFontRenderer() { |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 167 | INIT_LOGD("Creating lookup3 gamma font renderer"); |
| 168 | |
| 169 | // Compute the gamma tables |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 170 | const float blackGamma = mGamma; |
| 171 | const float whiteGamma = 1.0f / mGamma; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 172 | |
| 173 | for (uint32_t i = 0; i <= 255; i++) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 174 | const float v = i / 255.0f; |
| 175 | const float black = pow(v, blackGamma); |
| 176 | const float white = pow(v, whiteGamma); |
| 177 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 178 | mGammaTable[i] = i; |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 179 | mGammaTable[256 + i] = uint8_t((float)::floor(black * 255.0f + 0.5f)); |
| 180 | mGammaTable[512 + i] = uint8_t((float)::floor(white * 255.0f + 0.5f)); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 183 | memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount); |
| 184 | memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount); |
| 185 | } |
| 186 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 187 | void Lookup3GammaFontRenderer::endPrecaching() { |
| 188 | for (int i = 0; i < kGammaCount; i++) { |
| 189 | if (mRenderers[i]) { |
| 190 | mRenderers[i]->endPrecaching(); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 195 | void Lookup3GammaFontRenderer::clear() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 196 | for (int i = 0; i < kGammaCount; i++) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 197 | mRenderers[i].release(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 201 | void Lookup3GammaFontRenderer::flush() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 202 | int count = 0; |
| 203 | int min = -1; |
| 204 | uint32_t minCount = UINT_MAX; |
| 205 | |
| 206 | for (int i = 0; i < kGammaCount; i++) { |
| 207 | if (mRenderers[i]) { |
| 208 | count++; |
| 209 | if (mRenderersUsageCount[i] < minCount) { |
| 210 | minCount = mRenderersUsageCount[i]; |
| 211 | min = i; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (count <= 1 || min < 0) return; |
| 217 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 218 | mRenderers[min].release(); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 219 | |
| 220 | // Also eliminate the caches for large glyphs, as they consume significant memory |
| 221 | for (int i = 0; i < kGammaCount; ++i) { |
| 222 | if (mRenderers[i]) { |
| 223 | mRenderers[i]->flushLargeCaches(); |
| 224 | } |
| 225 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 228 | FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 229 | if (!mRenderers[gamma]) { |
| 230 | mRenderers[gamma].reset(new FontRenderer()); |
| 231 | mRenderers[gamma]->setGammaTable(&mGammaTable[gamma * 256]); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 232 | } |
| 233 | mRenderersUsageCount[gamma]++; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 234 | return mRenderers[gamma].get(); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Romain Guy | 6e25e38 | 2012-07-18 15:50:29 -0700 | [diff] [blame] | 237 | FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 238 | if (paint->getShader() == nullptr) { |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 239 | const int l = luminance(paint); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 240 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 241 | if (l <= mBlackThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 242 | return *getRenderer(kGammaBlack); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 243 | } else if (l >= mWhiteThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 244 | return *getRenderer(kGammaWhite); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 247 | return *getRenderer(kGammaDefault); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | }; // namespace uirenderer |
| 251 | }; // namespace android |