Romain Guy | 694b519 | 2010-07-21 21:33:20 -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 | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 19 | #include <SkUtils.h> |
| 20 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 22 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 25 | #include "Debug.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 26 | #include "FontRenderer.h" |
| 27 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 32 | // Defines |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | #define DEFAULT_TEXT_CACHE_WIDTH 1024 |
| 36 | #define DEFAULT_TEXT_CACHE_HEIGHT 256 |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 39 | // Font |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 42 | Font::Font(FontRenderer* state, uint32_t fontId, float fontSize, |
| 43 | int flags, uint32_t italicStyle) : |
| 44 | mState(state), mFontId(fontId), mFontSize(fontSize), |
| 45 | mFlags(flags), mItalicStyle(italicStyle) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | Font::~Font() { |
| 50 | for (uint32_t ct = 0; ct < mState->mActiveFonts.size(); ct++) { |
| 51 | if (mState->mActiveFonts[ct] == this) { |
| 52 | mState->mActiveFonts.removeAt(ct); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 58 | CachedGlyphInfo* glyph = mCachedGlyphs.valueAt(i); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 59 | delete glyph; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Font::invalidateTextureCache() { |
| 64 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
| 65 | mCachedGlyphs.valueAt(i)->mIsValid = false; |
| 66 | } |
| 67 | } |
| 68 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 69 | void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds) { |
| 70 | int nPenX = x + glyph->mBitmapLeft; |
| 71 | int nPenY = y + glyph->mBitmapTop; |
| 72 | |
| 73 | int width = (int) glyph->mBitmapWidth; |
| 74 | int height = (int) glyph->mBitmapHeight; |
| 75 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 76 | if (bounds->bottom > nPenY) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 77 | bounds->bottom = nPenY; |
| 78 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 79 | if (bounds->left > nPenX) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 80 | bounds->left = nPenX; |
| 81 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 82 | if (bounds->right < nPenX + width) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 83 | bounds->right = nPenX + width; |
| 84 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 85 | if (bounds->top < nPenY + height) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 86 | bounds->top = nPenY + height; |
| 87 | } |
| 88 | } |
| 89 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 90 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 91 | int nPenX = x + glyph->mBitmapLeft; |
| 92 | int nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight; |
| 93 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 94 | float u1 = glyph->mBitmapMinU; |
| 95 | float u2 = glyph->mBitmapMaxU; |
| 96 | float v1 = glyph->mBitmapMinV; |
| 97 | float v2 = glyph->mBitmapMaxV; |
| 98 | |
| 99 | int width = (int) glyph->mBitmapWidth; |
| 100 | int height = (int) glyph->mBitmapHeight; |
| 101 | |
| 102 | mState->appendMeshQuad(nPenX, nPenY, 0, u1, v2, |
| 103 | nPenX + width, nPenY, 0, u2, v2, |
| 104 | nPenX + width, nPenY - height, 0, u2, v1, |
| 105 | nPenX, nPenY - height, 0, u1, v1); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 108 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, |
| 109 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 110 | int nPenX = x + glyph->mBitmapLeft; |
| 111 | int nPenY = y + glyph->mBitmapTop; |
| 112 | |
| 113 | uint32_t endX = glyph->mStartX + glyph->mBitmapWidth; |
| 114 | uint32_t endY = glyph->mStartY + glyph->mBitmapHeight; |
| 115 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 116 | uint32_t cacheWidth = mState->getCacheWidth(); |
| 117 | const uint8_t* cacheBuffer = mState->getTextTextureData(); |
| 118 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 119 | uint32_t cacheX = 0, cacheY = 0; |
| 120 | int32_t bX = 0, bY = 0; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 121 | for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) { |
| 122 | for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 123 | if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 124 | LOGE("Skipping invalid index"); |
| 125 | continue; |
| 126 | } |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 127 | uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; |
| 128 | bitmap[bY * bitmapW + bX] = tempCol; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 134 | Font::CachedGlyphInfo* Font::getCachedUTFChar(SkPaint* paint, int32_t utfChar) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 135 | CachedGlyphInfo* cachedGlyph = NULL; |
| 136 | ssize_t index = mCachedGlyphs.indexOfKey(utfChar); |
| 137 | if (index >= 0) { |
| 138 | cachedGlyph = mCachedGlyphs.valueAt(index); |
| 139 | } else { |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 140 | cachedGlyph = cacheGlyph(paint, utfChar); |
| 141 | } |
| 142 | |
| 143 | // Is the glyph still in texture cache? |
| 144 | if (!cachedGlyph->mIsValid) { |
| 145 | const SkGlyph& skiaGlyph = paint->getUnicharMetrics(utfChar); |
| 146 | updateGlyphCache(paint, skiaGlyph, cachedGlyph); |
| 147 | } |
| 148 | |
| 149 | return cachedGlyph; |
| 150 | } |
| 151 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 152 | void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 153 | int numGlyphs, int x, int y, uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
| 154 | if (bitmap != NULL && bitmapW > 0 && bitmapH > 0) { |
| 155 | renderUTF(paint, text, start, len, numGlyphs, x, y, BITMAP, bitmap, |
| 156 | bitmapW, bitmapH, NULL); |
| 157 | } else { |
| 158 | renderUTF(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL, 0, 0, NULL); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | void Font::measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 164 | int numGlyphs, Rect *bounds) { |
| 165 | if (bounds == NULL) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 166 | LOGE("No return rectangle provided to measure text"); |
| 167 | return; |
| 168 | } |
| 169 | bounds->set(1e6, -1e6, -1e6, 1e6); |
| 170 | renderUTF(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds); |
| 171 | } |
| 172 | |
Romain Guy | 58ef7fb | 2010-09-13 12:52:37 -0700 | [diff] [blame] | 173 | #define SkAutoKern_AdjustF(prev, next) (((next) - (prev) + 32) >> 6 << 16) |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 174 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 175 | void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 176 | int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, |
| 177 | uint32_t bitmapW, uint32_t bitmapH,Rect *bounds) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 178 | if (numGlyphs == 0 || text == NULL || len == 0) { |
| 179 | return; |
| 180 | } |
| 181 | |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 182 | SkFixed penX = SkIntToFixed(x); |
| 183 | int penY = y; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 184 | int glyphsLeft = 1; |
| 185 | if (numGlyphs > 0) { |
| 186 | glyphsLeft = numGlyphs; |
| 187 | } |
| 188 | |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 189 | SkFixed prevRsbDelta = 0; |
| 190 | penX += SK_Fixed1 / 2; |
| 191 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 192 | text += start; |
| 193 | |
| 194 | while (glyphsLeft > 0) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 195 | int32_t utfChar = SkUTF16_NextUnichar((const uint16_t**) &text); |
| 196 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 197 | // Reached the end of the string |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 198 | if (utfChar < 0) { |
| 199 | break; |
| 200 | } |
| 201 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 202 | CachedGlyphInfo* cachedGlyph = getCachedUTFChar(paint, utfChar); |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 203 | penX += SkAutoKern_AdjustF(prevRsbDelta, cachedGlyph->mLsbDelta); |
| 204 | prevRsbDelta = cachedGlyph->mRsbDelta; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 205 | |
| 206 | // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage |
| 207 | if (cachedGlyph->mIsValid) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 208 | switch(mode) { |
| 209 | case FRAMEBUFFER: |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 210 | drawCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 211 | break; |
| 212 | case BITMAP: |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 213 | drawCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY, bitmap, bitmapW, bitmapH); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 214 | break; |
| 215 | case MEASURE: |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 216 | measureCachedGlyph(cachedGlyph, SkFixedFloor(penX), penY, bounds); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 217 | break; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 218 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 221 | penX += cachedGlyph->mAdvanceX; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 222 | |
| 223 | // If we were given a specific number of glyphs, decrement |
| 224 | if (numGlyphs > 0) { |
| 225 | glyphsLeft--; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 230 | void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 231 | glyph->mAdvanceX = skiaGlyph.fAdvanceX; |
| 232 | glyph->mAdvanceY = skiaGlyph.fAdvanceY; |
| 233 | glyph->mBitmapLeft = skiaGlyph.fLeft; |
| 234 | glyph->mBitmapTop = skiaGlyph.fTop; |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 235 | glyph->mLsbDelta = skiaGlyph.fLsbDelta; |
| 236 | glyph->mRsbDelta = skiaGlyph.fRsbDelta; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 237 | |
| 238 | uint32_t startX = 0; |
| 239 | uint32_t startY = 0; |
| 240 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 241 | // Get the bitmap for the glyph |
| 242 | paint->findImage(skiaGlyph); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 243 | glyph->mIsValid = mState->cacheBitmap(skiaGlyph, &startX, &startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 244 | |
| 245 | if (!glyph->mIsValid) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | uint32_t endX = startX + skiaGlyph.fWidth; |
| 250 | uint32_t endY = startY + skiaGlyph.fHeight; |
| 251 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 252 | glyph->mStartX = startX; |
| 253 | glyph->mStartY = startY; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 254 | glyph->mBitmapWidth = skiaGlyph.fWidth; |
| 255 | glyph->mBitmapHeight = skiaGlyph.fHeight; |
| 256 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 257 | uint32_t cacheWidth = mState->getCacheWidth(); |
| 258 | uint32_t cacheHeight = mState->getCacheHeight(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 259 | |
| 260 | glyph->mBitmapMinU = (float) startX / (float) cacheWidth; |
| 261 | glyph->mBitmapMinV = (float) startY / (float) cacheHeight; |
| 262 | glyph->mBitmapMaxU = (float) endX / (float) cacheWidth; |
| 263 | glyph->mBitmapMaxV = (float) endY / (float) cacheHeight; |
| 264 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 265 | mState->mUploadTexture = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 268 | Font::CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, int32_t glyph) { |
| 269 | CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 270 | mCachedGlyphs.add(glyph, newGlyph); |
| 271 | |
| 272 | const SkGlyph& skiaGlyph = paint->getUnicharMetrics(glyph); |
| 273 | newGlyph->mGlyphIndex = skiaGlyph.fID; |
| 274 | newGlyph->mIsValid = false; |
| 275 | |
| 276 | updateGlyphCache(paint, skiaGlyph, newGlyph); |
| 277 | |
| 278 | return newGlyph; |
| 279 | } |
| 280 | |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 281 | Font* Font::create(FontRenderer* state, uint32_t fontId, float fontSize, |
| 282 | int flags, uint32_t italicStyle) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 283 | Vector<Font*> &activeFonts = state->mActiveFonts; |
| 284 | |
| 285 | for (uint32_t i = 0; i < activeFonts.size(); i++) { |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 286 | Font* font = activeFonts[i]; |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 287 | if (font->mFontId == fontId && font->mFontSize == fontSize && |
| 288 | font->mFlags == flags && font->mItalicStyle == italicStyle) { |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 289 | return font; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 293 | Font* newFont = new Font(state, fontId, fontSize, flags, italicStyle); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 294 | activeFonts.push(newFont); |
| 295 | return newFont; |
| 296 | } |
| 297 | |
| 298 | /////////////////////////////////////////////////////////////////////////////// |
| 299 | // FontRenderer |
| 300 | /////////////////////////////////////////////////////////////////////////////// |
| 301 | |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 302 | static bool sLogFontRendererCreate = true; |
| 303 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 304 | FontRenderer::FontRenderer() { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 305 | if (sLogFontRendererCreate) { |
| 306 | INIT_LOGD("Creating FontRenderer"); |
| 307 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 308 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 309 | mGammaTable = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 310 | mInitialized = false; |
| 311 | mMaxNumberOfQuads = 1024; |
| 312 | mCurrentQuadIndex = 0; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 313 | mTextureId = 0; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 314 | |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 315 | mTextMeshPtr = NULL; |
| 316 | mTextTexture = NULL; |
| 317 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 318 | mIndexBufferID = 0; |
Alex Sakhartchouk | 894df17 | 2011-02-17 16:45:37 -0800 | [diff] [blame] | 319 | mPositionAttrSlot = -1; |
| 320 | mTexcoordAttrSlot = -1; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 321 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 322 | mCacheWidth = DEFAULT_TEXT_CACHE_WIDTH; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 323 | mCacheHeight = DEFAULT_TEXT_CACHE_HEIGHT; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 324 | |
| 325 | char property[PROPERTY_VALUE_MAX]; |
| 326 | if (property_get(PROPERTY_TEXT_CACHE_WIDTH, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 327 | if (sLogFontRendererCreate) { |
| 328 | INIT_LOGD(" Setting text cache width to %s pixels", property); |
| 329 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 330 | mCacheWidth = atoi(property); |
| 331 | } else { |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 332 | if (sLogFontRendererCreate) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 333 | INIT_LOGD(" Using default text cache width of %i pixels", mCacheWidth); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 334 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (property_get(PROPERTY_TEXT_CACHE_HEIGHT, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 338 | if (sLogFontRendererCreate) { |
| 339 | INIT_LOGD(" Setting text cache width to %s pixels", property); |
| 340 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 341 | mCacheHeight = atoi(property); |
| 342 | } else { |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 343 | if (sLogFontRendererCreate) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 344 | INIT_LOGD(" Using default text cache height of %i pixels", mCacheHeight); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 345 | } |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 346 | } |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 347 | |
| 348 | sLogFontRendererCreate = false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | FontRenderer::~FontRenderer() { |
| 352 | for (uint32_t i = 0; i < mCacheLines.size(); i++) { |
| 353 | delete mCacheLines[i]; |
| 354 | } |
| 355 | mCacheLines.clear(); |
| 356 | |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 357 | if (mInitialized) { |
| 358 | delete[] mTextMeshPtr; |
| 359 | delete[] mTextTexture; |
| 360 | } |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 361 | |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 362 | if (mTextureId) { |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 363 | glDeleteTextures(1, &mTextureId); |
| 364 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 365 | |
| 366 | Vector<Font*> fontsToDereference = mActiveFonts; |
| 367 | for (uint32_t i = 0; i < fontsToDereference.size(); i++) { |
| 368 | delete fontsToDereference[i]; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void FontRenderer::flushAllAndInvalidate() { |
| 373 | if (mCurrentQuadIndex != 0) { |
| 374 | issueDrawCommand(); |
| 375 | mCurrentQuadIndex = 0; |
| 376 | } |
| 377 | for (uint32_t i = 0; i < mActiveFonts.size(); i++) { |
| 378 | mActiveFonts[i]->invalidateTextureCache(); |
| 379 | } |
| 380 | for (uint32_t i = 0; i < mCacheLines.size(); i++) { |
| 381 | mCacheLines[i]->mCurrentCol = 0; |
| 382 | } |
| 383 | } |
| 384 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 385 | bool FontRenderer::cacheBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 386 | // If the glyph is too tall, don't cache it |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 387 | if (glyph.fHeight > mCacheLines[mCacheLines.size() - 1]->mMaxHeight) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 388 | LOGE("Font size to large to fit in cache. width, height = %i, %i", |
| 389 | (int) glyph.fWidth, (int) glyph.fHeight); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | // Now copy the bitmap into the cache texture |
| 394 | uint32_t startX = 0; |
| 395 | uint32_t startY = 0; |
| 396 | |
| 397 | bool bitmapFit = false; |
| 398 | for (uint32_t i = 0; i < mCacheLines.size(); i++) { |
| 399 | bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY); |
| 400 | if (bitmapFit) { |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // If the new glyph didn't fit, flush the state so far and invalidate everything |
| 406 | if (!bitmapFit) { |
| 407 | flushAllAndInvalidate(); |
| 408 | |
| 409 | // Try to fit it again |
| 410 | for (uint32_t i = 0; i < mCacheLines.size(); i++) { |
| 411 | bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY); |
| 412 | if (bitmapFit) { |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // if we still don't fit, something is wrong and we shouldn't draw |
| 418 | if (!bitmapFit) { |
| 419 | LOGE("Bitmap doesn't fit in cache. width, height = %i, %i", |
| 420 | (int) glyph.fWidth, (int) glyph.fHeight); |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | *retOriginX = startX; |
| 426 | *retOriginY = startY; |
| 427 | |
| 428 | uint32_t endX = startX + glyph.fWidth; |
| 429 | uint32_t endY = startY + glyph.fHeight; |
| 430 | |
| 431 | uint32_t cacheWidth = mCacheWidth; |
| 432 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 433 | uint8_t* cacheBuffer = mTextTexture; |
| 434 | uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 435 | unsigned int stride = glyph.rowBytes(); |
| 436 | |
| 437 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
| 438 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) { |
| 439 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 440 | uint8_t tempCol = bitmapBuffer[bY * stride + bX]; |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 441 | cacheBuffer[cacheY * cacheWidth + cacheX] = mGammaTable[tempCol]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | void FontRenderer::initTextTexture() { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 449 | mTextTexture = new uint8_t[mCacheWidth * mCacheHeight]; |
Romain Guy | 1de1083 | 2010-10-03 14:37:09 -0700 | [diff] [blame] | 450 | memset(mTextTexture, 0, mCacheWidth * mCacheHeight * sizeof(uint8_t)); |
| 451 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 452 | mUploadTexture = false; |
| 453 | |
| 454 | glGenTextures(1, &mTextureId); |
| 455 | glBindTexture(GL_TEXTURE_2D, mTextureId); |
| 456 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 457 | // Initialize texture dimentions |
| 458 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, mCacheWidth, mCacheHeight, 0, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 459 | GL_ALPHA, GL_UNSIGNED_BYTE, 0); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 460 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 461 | mLinearFiltering = false; |
| 462 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 463 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 464 | |
| 465 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 466 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 467 | |
| 468 | // Split up our cache texture into lines of certain widths |
| 469 | int nextLine = 0; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 470 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 18, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 471 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 472 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 26, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 473 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 474 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 26, nextLine, 0)); |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 475 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 476 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 34, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 477 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 478 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 34, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 479 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 480 | mCacheLines.push(new CacheTextureLine(mCacheWidth, 42, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 481 | nextLine += mCacheLines.top()->mMaxHeight; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 482 | mCacheLines.push(new CacheTextureLine(mCacheWidth, mCacheHeight - nextLine, nextLine, 0)); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // Avoid having to reallocate memory and render quad by quad |
| 486 | void FontRenderer::initVertexArrayBuffers() { |
| 487 | uint32_t numIndicies = mMaxNumberOfQuads * 6; |
| 488 | uint32_t indexBufferSizeBytes = numIndicies * sizeof(uint16_t); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 489 | uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 490 | |
| 491 | // Four verts, two triangles , six indices per quad |
| 492 | for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) { |
| 493 | int i6 = i * 6; |
| 494 | int i4 = i * 4; |
| 495 | |
| 496 | indexBufferData[i6 + 0] = i4 + 0; |
| 497 | indexBufferData[i6 + 1] = i4 + 1; |
| 498 | indexBufferData[i6 + 2] = i4 + 2; |
| 499 | |
| 500 | indexBufferData[i6 + 3] = i4 + 0; |
| 501 | indexBufferData[i6 + 4] = i4 + 2; |
| 502 | indexBufferData[i6 + 5] = i4 + 3; |
| 503 | } |
| 504 | |
| 505 | glGenBuffers(1, &mIndexBufferID); |
Romain Guy | 5d79441 | 2010-10-13 16:36:22 -0700 | [diff] [blame] | 506 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferID); |
| 507 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_STATIC_DRAW); |
| 508 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 509 | |
| 510 | free(indexBufferData); |
| 511 | |
| 512 | uint32_t coordSize = 3; |
| 513 | uint32_t uvSize = 2; |
| 514 | uint32_t vertsPerQuad = 4; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 515 | uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize; |
| 516 | mTextMeshPtr = new float[vertexBufferSize]; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // We don't want to allocate anything unless we actually draw text |
| 520 | void FontRenderer::checkInit() { |
| 521 | if (mInitialized) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | initTextTexture(); |
| 526 | initVertexArrayBuffers(); |
| 527 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 528 | // We store a string with letters in a rough frequency of occurrence |
| 529 | mLatinPrecache = String16("eisarntolcdugpmhbyfvkwzxjq "); |
| 530 | mLatinPrecache += String16("EISARNTOLCDUGPMHBYFVKWZXJQ"); |
| 531 | mLatinPrecache += String16(",.?!()-+@;:`'"); |
| 532 | mLatinPrecache += String16("0123456789"); |
| 533 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 534 | mInitialized = true; |
| 535 | } |
| 536 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 537 | void FontRenderer::checkTextureUpdate() { |
| 538 | if (!mUploadTexture) { |
| 539 | return; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 542 | glBindTexture(GL_TEXTURE_2D, mTextureId); |
| 543 | |
| 544 | // Iterate over all the cache lines and see which ones need to be updated |
| 545 | for (uint32_t i = 0; i < mCacheLines.size(); i++) { |
| 546 | CacheTextureLine* cl = mCacheLines[i]; |
| 547 | if(cl->mDirty) { |
| 548 | uint32_t xOffset = 0; |
| 549 | uint32_t yOffset = cl->mCurrentRow; |
| 550 | uint32_t width = mCacheWidth; |
| 551 | uint32_t height = cl->mMaxHeight; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 552 | void* textureData = mTextTexture + yOffset*width; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 553 | |
| 554 | glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 555 | GL_ALPHA, GL_UNSIGNED_BYTE, textureData); |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 556 | |
| 557 | cl->mDirty = false; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | mUploadTexture = false; |
| 562 | } |
| 563 | |
| 564 | void FontRenderer::issueDrawCommand() { |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 565 | checkTextureUpdate(); |
| 566 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 567 | float* vtx = mTextMeshPtr; |
| 568 | float* tex = vtx + 3; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 569 | |
Alex Sakhartchouk | 894df17 | 2011-02-17 16:45:37 -0800 | [diff] [blame] | 570 | glVertexAttribPointer(mPositionAttrSlot, 3, GL_FLOAT, false, 20, vtx); |
| 571 | glVertexAttribPointer(mTexcoordAttrSlot, 2, GL_FLOAT, false, 20, tex); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 572 | |
| 573 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferID); |
| 574 | glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, NULL); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 575 | |
| 576 | mDrawn = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void FontRenderer::appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, |
| 580 | float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, |
| 581 | float x4, float y4, float z4, float u4, float v4) { |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 582 | if (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom) { |
| 583 | return; |
| 584 | } |
| 585 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 586 | const uint32_t vertsPerQuad = 4; |
| 587 | const uint32_t floatsPerVert = 5; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 588 | float* currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 589 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 590 | (*currentPos++) = x1; |
| 591 | (*currentPos++) = y1; |
| 592 | (*currentPos++) = z1; |
| 593 | (*currentPos++) = u1; |
| 594 | (*currentPos++) = v1; |
| 595 | |
| 596 | (*currentPos++) = x2; |
| 597 | (*currentPos++) = y2; |
| 598 | (*currentPos++) = z2; |
| 599 | (*currentPos++) = u2; |
| 600 | (*currentPos++) = v2; |
| 601 | |
| 602 | (*currentPos++) = x3; |
| 603 | (*currentPos++) = y3; |
| 604 | (*currentPos++) = z3; |
| 605 | (*currentPos++) = u3; |
| 606 | (*currentPos++) = v3; |
| 607 | |
| 608 | (*currentPos++) = x4; |
| 609 | (*currentPos++) = y4; |
| 610 | (*currentPos++) = z4; |
| 611 | (*currentPos++) = u4; |
| 612 | (*currentPos++) = v4; |
| 613 | |
| 614 | mCurrentQuadIndex++; |
| 615 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 616 | if (mBounds) { |
| 617 | mBounds->left = fmin(mBounds->left, x1); |
| 618 | mBounds->top = fmin(mBounds->top, y3); |
| 619 | mBounds->right = fmax(mBounds->right, x3); |
| 620 | mBounds->bottom = fmax(mBounds->bottom, y1); |
| 621 | } |
| 622 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 623 | if (mCurrentQuadIndex == mMaxNumberOfQuads) { |
| 624 | issueDrawCommand(); |
| 625 | mCurrentQuadIndex = 0; |
| 626 | } |
| 627 | } |
| 628 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 629 | uint32_t FontRenderer::getRemainingCacheCapacity() { |
| 630 | uint32_t remainingCapacity = 0; |
| 631 | float totalPixels = 0; |
| 632 | for(uint32_t i = 0; i < mCacheLines.size(); i ++) { |
| 633 | remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol); |
| 634 | totalPixels += mCacheLines[i]->mMaxWidth; |
| 635 | } |
| 636 | remainingCapacity = (remainingCapacity * 100) / totalPixels; |
| 637 | return remainingCapacity; |
| 638 | } |
| 639 | |
| 640 | void FontRenderer::precacheLatin(SkPaint* paint) { |
| 641 | // Remaining capacity is measured in % |
| 642 | uint32_t remainingCapacity = getRemainingCacheCapacity(); |
| 643 | uint32_t precacheIdx = 0; |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 644 | while (remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) { |
| 645 | mCurrentFont->getCachedUTFChar(paint, (int32_t) mLatinPrecache[precacheIdx]); |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 646 | remainingCapacity = getRemainingCacheCapacity(); |
| 647 | precacheIdx ++; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void FontRenderer::setFont(SkPaint* paint, uint32_t fontId, float fontSize) { |
| 652 | uint32_t currentNumFonts = mActiveFonts.size(); |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 653 | int flags = 0; |
| 654 | if (paint->isFakeBoldText()) { |
| 655 | flags |= Font::kFakeBold; |
| 656 | } |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 657 | |
| 658 | const float skewX = paint->getTextSkewX(); |
| 659 | uint32_t italicStyle = *(uint32_t*) &skewX; |
| 660 | mCurrentFont = Font::create(this, fontId, fontSize, flags, italicStyle); |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 661 | |
| 662 | const float maxPrecacheFontSize = 40.0f; |
| 663 | bool isNewFont = currentNumFonts != mActiveFonts.size(); |
| 664 | |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 665 | if (isNewFont && fontSize <= maxPrecacheFontSize) { |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 666 | precacheLatin(paint); |
| 667 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 668 | } |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 669 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 670 | FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 671 | uint32_t startIndex, uint32_t len, int numGlyphs, uint32_t radius) { |
| 672 | checkInit(); |
| 673 | |
| 674 | if (!mCurrentFont) { |
| 675 | DropShadow image; |
| 676 | image.width = 0; |
| 677 | image.height = 0; |
| 678 | image.image = NULL; |
| 679 | image.penX = 0; |
| 680 | image.penY = 0; |
| 681 | return image; |
| 682 | } |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 683 | |
| 684 | Rect bounds; |
| 685 | mCurrentFont->measureUTF(paint, text, startIndex, len, numGlyphs, &bounds); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 686 | uint32_t paddedWidth = (uint32_t) (bounds.right - bounds.left) + 2 * radius; |
| 687 | uint32_t paddedHeight = (uint32_t) (bounds.top - bounds.bottom) + 2 * radius; |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 688 | uint8_t* dataBuffer = new uint8_t[paddedWidth * paddedHeight]; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 689 | for (uint32_t i = 0; i < paddedWidth * paddedHeight; i++) { |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 690 | dataBuffer[i] = 0; |
| 691 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 692 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 693 | int penX = radius - bounds.left; |
| 694 | int penY = radius - bounds.bottom; |
| 695 | |
| 696 | mCurrentFont->renderUTF(paint, text, startIndex, len, numGlyphs, penX, penY, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 697 | dataBuffer, paddedWidth, paddedHeight); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 698 | blurImage(dataBuffer, paddedWidth, paddedHeight, radius); |
| 699 | |
| 700 | DropShadow image; |
| 701 | image.width = paddedWidth; |
| 702 | image.height = paddedHeight; |
| 703 | image.image = dataBuffer; |
| 704 | image.penX = penX; |
| 705 | image.penY = penY; |
| 706 | return image; |
| 707 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 708 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 709 | bool FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text, |
| 710 | uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, Rect* bounds) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 711 | checkInit(); |
| 712 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 713 | if (!mCurrentFont) { |
| 714 | LOGE("No font set"); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 715 | return false; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Alex Sakhartchouk | 894df17 | 2011-02-17 16:45:37 -0800 | [diff] [blame] | 718 | if (mPositionAttrSlot < 0 || mTexcoordAttrSlot < 0) { |
| 719 | LOGE("Font renderer unable to draw, attribute slots undefined"); |
| 720 | return false; |
| 721 | } |
| 722 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 723 | mDrawn = false; |
| 724 | mBounds = bounds; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 725 | mClip = clip; |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 726 | mCurrentFont->renderUTF(paint, text, startIndex, len, numGlyphs, x, y); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 727 | mBounds = NULL; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 728 | |
| 729 | if (mCurrentQuadIndex != 0) { |
| 730 | issueDrawCommand(); |
| 731 | mCurrentQuadIndex = 0; |
| 732 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 733 | |
| 734 | return mDrawn; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 737 | void FontRenderer::computeGaussianWeights(float* weights, int32_t radius) { |
| 738 | // Compute gaussian weights for the blur |
| 739 | // e is the euler's number |
| 740 | float e = 2.718281828459045f; |
| 741 | float pi = 3.1415926535897932f; |
| 742 | // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) |
| 743 | // x is of the form [-radius .. 0 .. radius] |
| 744 | // and sigma varies with radius. |
| 745 | // Based on some experimental radius values and sigma's |
| 746 | // we approximately fit sigma = f(radius) as |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 747 | // sigma = radius * 0.3 + 0.6 |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 748 | // The larger the radius gets, the more our gaussian blur |
| 749 | // will resemble a box blur since with large sigma |
| 750 | // the gaussian curve begins to lose its shape |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 751 | float sigma = 0.3f * (float) radius + 0.6f; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 752 | |
| 753 | // Now compute the coefficints |
| 754 | // We will store some redundant values to save some math during |
| 755 | // the blur calculations |
| 756 | // precompute some values |
| 757 | float coeff1 = 1.0f / (sqrt( 2.0f * pi ) * sigma); |
| 758 | float coeff2 = - 1.0f / (2.0f * sigma * sigma); |
| 759 | |
| 760 | float normalizeFactor = 0.0f; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 761 | for (int32_t r = -radius; r <= radius; r ++) { |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 762 | float floatR = (float) r; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 763 | weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2); |
| 764 | normalizeFactor += weights[r + radius]; |
| 765 | } |
| 766 | |
| 767 | //Now we need to normalize the weights because all our coefficients need to add up to one |
| 768 | normalizeFactor = 1.0f / normalizeFactor; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 769 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 770 | weights[r + radius] *= normalizeFactor; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void FontRenderer::horizontalBlur(float* weights, int32_t radius, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 775 | const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 776 | float blurredPixel = 0.0f; |
| 777 | float currentPixel = 0.0f; |
| 778 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 779 | for (int32_t y = 0; y < height; y ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 780 | |
| 781 | const uint8_t* input = source + y * width; |
| 782 | uint8_t* output = dest + y * width; |
| 783 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 784 | for (int32_t x = 0; x < width; x ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 785 | blurredPixel = 0.0f; |
| 786 | const float* gPtr = weights; |
| 787 | // Optimization for non-border pixels |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 788 | if (x > radius && x < (width - radius)) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 789 | const uint8_t *i = input + (x - radius); |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 790 | for (int r = -radius; r <= radius; r ++) { |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 791 | currentPixel = (float) (*i); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 792 | blurredPixel += currentPixel * gPtr[0]; |
| 793 | gPtr++; |
| 794 | i++; |
| 795 | } |
| 796 | } else { |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 797 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 798 | // Stepping left and right away from the pixel |
| 799 | int validW = x + r; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 800 | if (validW < 0) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 801 | validW = 0; |
| 802 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 803 | if (validW > width - 1) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 804 | validW = width - 1; |
| 805 | } |
| 806 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 807 | currentPixel = (float) input[validW]; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 808 | blurredPixel += currentPixel * gPtr[0]; |
| 809 | gPtr++; |
| 810 | } |
| 811 | } |
| 812 | *output = (uint8_t)blurredPixel; |
| 813 | output ++; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void FontRenderer::verticalBlur(float* weights, int32_t radius, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 819 | const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 820 | float blurredPixel = 0.0f; |
| 821 | float currentPixel = 0.0f; |
| 822 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 823 | for (int32_t y = 0; y < height; y ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 824 | |
| 825 | uint8_t* output = dest + y * width; |
| 826 | |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 827 | for (int32_t x = 0; x < width; x ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 828 | blurredPixel = 0.0f; |
| 829 | const float* gPtr = weights; |
| 830 | const uint8_t* input = source + x; |
| 831 | // Optimization for non-border pixels |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 832 | if (y > radius && y < (height - radius)) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 833 | const uint8_t *i = input + ((y - radius) * width); |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 834 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 835 | currentPixel = (float)(*i); |
| 836 | blurredPixel += currentPixel * gPtr[0]; |
| 837 | gPtr++; |
| 838 | i += width; |
| 839 | } |
| 840 | } else { |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 841 | for (int32_t r = -radius; r <= radius; r ++) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 842 | int validH = y + r; |
| 843 | // Clamp to zero and width |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 844 | if (validH < 0) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 845 | validH = 0; |
| 846 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 847 | if (validH > height - 1) { |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 848 | validH = height - 1; |
| 849 | } |
| 850 | |
| 851 | const uint8_t *i = input + validH * width; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 852 | currentPixel = (float) (*i); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 853 | blurredPixel += currentPixel * gPtr[0]; |
| 854 | gPtr++; |
| 855 | } |
| 856 | } |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 857 | *output = (uint8_t) blurredPixel; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 858 | output ++; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | |
| 864 | void FontRenderer::blurImage(uint8_t *image, int32_t width, int32_t height, int32_t radius) { |
| 865 | float *gaussian = new float[2 * radius + 1]; |
| 866 | computeGaussianWeights(gaussian, radius); |
| 867 | uint8_t* scratch = new uint8_t[width * height]; |
| 868 | horizontalBlur(gaussian, radius, image, scratch, width, height); |
| 869 | verticalBlur(gaussian, radius, scratch, image, width, height); |
| 870 | delete[] gaussian; |
| 871 | delete[] scratch; |
| 872 | } |
| 873 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 874 | }; // namespace uirenderer |
| 875 | }; // namespace android |