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 | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 27 | // Base class GammaFontRenderer |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 30 | GammaFontRenderer* GammaFontRenderer::createRenderer() { |
| 31 | // Choose the best renderer |
| 32 | char property[PROPERTY_VALUE_MAX]; |
| 33 | if (property_get(PROPERTY_TEXT_GAMMA_SHADER, property, DEFAULT_TEXT_GAMMA_SHADER) > 0) { |
| 34 | if (!strcasecmp(property, "true")) { |
| 35 | return new ShaderGammaFontRenderer(); |
| 36 | } |
| 37 | } |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 38 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 39 | return new LookupGammaFontRenderer(); |
| 40 | } |
| 41 | |
| 42 | GammaFontRenderer::GammaFontRenderer() { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 43 | // Get the renderer properties |
| 44 | char property[PROPERTY_VALUE_MAX]; |
| 45 | |
| 46 | // Get the gamma |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 47 | mGamma = DEFAULT_TEXT_GAMMA; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 48 | if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 49 | INIT_LOGD(" Setting text gamma to %s", property); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 50 | mGamma = atof(property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 51 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 52 | INIT_LOGD(" Using default text gamma of %.2f", DEFAULT_TEXT_GAMMA); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Get the black gamma threshold |
| 56 | mBlackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD; |
| 57 | if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 58 | INIT_LOGD(" Setting text black gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 59 | mBlackThreshold = atoi(property); |
| 60 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 61 | INIT_LOGD(" Using default text black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 62 | DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD); |
| 63 | } |
| 64 | |
| 65 | // Get the white gamma threshold |
| 66 | mWhiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD; |
| 67 | if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 68 | INIT_LOGD(" Setting text white gamma threshold to %s", property); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 69 | mWhiteThreshold = atoi(property); |
| 70 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 71 | INIT_LOGD(" Using default white black gamma threshold of %d", |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 72 | DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD); |
| 73 | } |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 74 | } |
| 75 | |
| 76 | GammaFontRenderer::~GammaFontRenderer() { |
| 77 | } |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | // Shader-based renderer |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | ShaderGammaFontRenderer::ShaderGammaFontRenderer(): GammaFontRenderer() { |
| 84 | INIT_LOGD("Creating shader gamma font renderer"); |
| 85 | } |
| 86 | |
| 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | // Lookup-based renderer |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | LookupGammaFontRenderer::LookupGammaFontRenderer(): GammaFontRenderer() { |
| 92 | INIT_LOGD("Creating lookup gamma font renderer"); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 93 | |
| 94 | // Compute the gamma tables |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 95 | const float blackGamma = mGamma; |
| 96 | const float whiteGamma = 1.0f / mGamma; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 97 | |
| 98 | for (uint32_t i = 0; i <= 255; i++) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 99 | mGammaTable[i] = i; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 100 | |
| 101 | const float v = i / 255.0f; |
| 102 | const float black = pow(v, blackGamma); |
| 103 | const float white = pow(v, whiteGamma); |
| 104 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 105 | mGammaTable[256 + i] = uint8_t((float)::floor(black * 255.0f + 0.5f)); |
| 106 | mGammaTable[512 + i] = uint8_t((float)::floor(white * 255.0f + 0.5f)); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 109 | memset(mRenderers, 0, sizeof(FontRenderer*) * kGammaCount); |
| 110 | memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount); |
| 111 | } |
| 112 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 113 | LookupGammaFontRenderer::~LookupGammaFontRenderer() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 114 | for (int i = 0; i < kGammaCount; i++) { |
| 115 | delete mRenderers[i]; |
| 116 | } |
| 117 | } |
| 118 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 119 | void LookupGammaFontRenderer::clear() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 120 | for (int i = 0; i < kGammaCount; i++) { |
| 121 | delete mRenderers[i]; |
| 122 | mRenderers[i] = NULL; |
| 123 | } |
| 124 | } |
| 125 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 126 | void LookupGammaFontRenderer::flush() { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 127 | int count = 0; |
| 128 | int min = -1; |
| 129 | uint32_t minCount = UINT_MAX; |
| 130 | |
| 131 | for (int i = 0; i < kGammaCount; i++) { |
| 132 | if (mRenderers[i]) { |
| 133 | count++; |
| 134 | if (mRenderersUsageCount[i] < minCount) { |
| 135 | minCount = mRenderersUsageCount[i]; |
| 136 | min = i; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (count <= 1 || min < 0) return; |
| 142 | |
| 143 | delete mRenderers[min]; |
| 144 | mRenderers[min] = NULL; |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 145 | |
| 146 | // Also eliminate the caches for large glyphs, as they consume significant memory |
| 147 | for (int i = 0; i < kGammaCount; ++i) { |
| 148 | if (mRenderers[i]) { |
| 149 | mRenderers[i]->flushLargeCaches(); |
| 150 | } |
| 151 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 154 | FontRenderer* LookupGammaFontRenderer::getRenderer(Gamma gamma) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 155 | FontRenderer* renderer = mRenderers[gamma]; |
| 156 | if (!renderer) { |
| 157 | renderer = new FontRenderer(); |
| 158 | mRenderers[gamma] = renderer; |
| 159 | renderer->setGammaTable(&mGammaTable[gamma * 256]); |
| 160 | } |
| 161 | mRenderersUsageCount[gamma]++; |
| 162 | return renderer; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame^] | 165 | FontRenderer& LookupGammaFontRenderer::getFontRenderer(const SkPaint* paint) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 166 | if (paint->getShader() == NULL) { |
| 167 | uint32_t c = paint->getColor(); |
| 168 | const int r = (c >> 16) & 0xFF; |
| 169 | const int g = (c >> 8) & 0xFF; |
| 170 | const int b = (c ) & 0xFF; |
| 171 | const int luminance = (r * 2 + g * 5 + b) >> 3; |
| 172 | |
| 173 | if (luminance <= mBlackThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 174 | return *getRenderer(kGammaBlack); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 175 | } else if (luminance >= mWhiteThreshold) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 176 | return *getRenderer(kGammaWhite); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 179 | return *getRenderer(kGammaDefault); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | }; // namespace uirenderer |
| 183 | }; // namespace android |