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