Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2009 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include "rsFont.h" |
| 21 | #include "rsProgramFragment.h" |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 23 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 24 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 25 | #include <ft2build.h> |
| 26 | #include FT_FREETYPE_H |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 27 | #include FT_BITMAP_H |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 28 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 29 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 30 | using namespace android; |
| 31 | using namespace android::renderscript; |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 34 | mInitialized = false; |
| 35 | mHasKerning = false; |
Alex Sakhartchouk | 3659d94 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 36 | mFace = NULL; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 39 | bool Font::init(const char *name, float fontSize, uint32_t dpi, const void *data, uint32_t dataLen) { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 40 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 41 | if (mInitialized) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 42 | ALOGE("Reinitialization of fonts not supported"); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 46 | FT_Error error = 0; |
| 47 | if (data != NULL && dataLen > 0) { |
| 48 | error = FT_New_Memory_Face(mRSC->mStateFont.getLib(), (const FT_Byte*)data, dataLen, 0, &mFace); |
| 49 | } else { |
| 50 | error = FT_New_Face(mRSC->mStateFont.getLib(), name, 0, &mFace); |
| 51 | } |
| 52 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 53 | if (error) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 54 | ALOGE("Unable to initialize font %s", name); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | mFontName = name; |
| 59 | mFontSize = fontSize; |
| 60 | mDpi = dpi; |
| 61 | |
Alex Sakhartchouk | c17ace2 | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 62 | error = FT_Set_Char_Size(mFace, (FT_F26Dot6)(fontSize * 64.0f), 0, dpi, 0); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 63 | if (error) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 64 | ALOGE("Unable to set font size on %s", name); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
| 68 | mHasKerning = FT_HAS_KERNING(mFace); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 69 | |
| 70 | mInitialized = true; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 71 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 75 | void Font::preDestroy() const { |
| 76 | for (uint32_t ct = 0; ct < mRSC->mStateFont.mActiveFonts.size(); ct++) { |
| 77 | if (mRSC->mStateFont.mActiveFonts[ct] == this) { |
| 78 | mRSC->mStateFont.mActiveFonts.removeAt(ct); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 84 | void Font::invalidateTextureCache() { |
| 85 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 86 | mCachedGlyphs.valueAt(i)->mIsValid = false; |
| 87 | } |
| 88 | } |
| 89 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 90 | void Font::drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 91 | FontState *state = &mRSC->mStateFont; |
| 92 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 93 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 94 | int32_t nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 95 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 96 | float u1 = glyph->mBitmapMinU; |
| 97 | float u2 = glyph->mBitmapMaxU; |
| 98 | float v1 = glyph->mBitmapMinV; |
| 99 | float v2 = glyph->mBitmapMaxV; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 100 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 101 | int32_t width = (int32_t) glyph->mBitmapWidth; |
| 102 | int32_t height = (int32_t) glyph->mBitmapHeight; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 103 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 104 | state->appendMeshQuad(nPenX, nPenY, 0, u1, v2, |
| 105 | nPenX + width, nPenY, 0, u2, v2, |
| 106 | nPenX + width, nPenY - height, 0, u2, v1, |
| 107 | nPenX, nPenY - height, 0, u1, v1); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 110 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int32_t x, int32_t y, |
| 111 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
| 112 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 113 | int32_t nPenY = y + glyph->mBitmapTop; |
| 114 | |
| 115 | uint32_t endX = glyph->mBitmapMinX + glyph->mBitmapWidth; |
| 116 | uint32_t endY = glyph->mBitmapMinY + glyph->mBitmapHeight; |
| 117 | |
| 118 | FontState *state = &mRSC->mStateFont; |
| 119 | uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); |
| 120 | const uint8_t* cacheBuffer = state->getTextTextureData(); |
| 121 | |
| 122 | uint32_t cacheX = 0, cacheY = 0; |
| 123 | int32_t bX = 0, bY = 0; |
| 124 | for (cacheX = glyph->mBitmapMinX, bX = nPenX; cacheX < endX; cacheX++, bX++) { |
| 125 | for (cacheY = glyph->mBitmapMinY, bY = nPenY; cacheY < endY; cacheY++, bY++) { |
| 126 | if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 127 | ALOGE("Skipping invalid index"); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; |
| 131 | bitmap[bY * bitmapW + bX] = tempCol; |
| 132 | } |
| 133 | } |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, Rect *bounds) { |
| 137 | int32_t nPenX = x + glyph->mBitmapLeft; |
| 138 | int32_t nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight; |
| 139 | |
| 140 | int32_t width = (int32_t) glyph->mBitmapWidth; |
| 141 | int32_t height = (int32_t) glyph->mBitmapHeight; |
| 142 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 143 | // 0, 0 is top left, so bottom is a positive number |
| 144 | if (bounds->bottom < nPenY) { |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 145 | bounds->bottom = nPenY; |
| 146 | } |
| 147 | if (bounds->left > nPenX) { |
| 148 | bounds->left = nPenX; |
| 149 | } |
| 150 | if (bounds->right < nPenX + width) { |
| 151 | bounds->right = nPenX + width; |
| 152 | } |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 153 | if (bounds->top > nPenY - height) { |
| 154 | bounds->top = nPenY - height; |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void Font::renderUTF(const char *text, uint32_t len, int32_t x, int32_t y, |
| 159 | uint32_t start, int32_t numGlyphs, |
| 160 | RenderMode mode, Rect *bounds, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 161 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
| 162 | if (!mInitialized || numGlyphs == 0 || text == NULL || len == 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 166 | if (mode == Font::MEASURE) { |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 167 | if (bounds == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 168 | ALOGE("No return rectangle provided to measure text"); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | // Reset min and max of the bounding box to something large |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 172 | bounds->set(1e6, -1e6, 1e6, -1e6); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int32_t penX = x, penY = y; |
| 176 | int32_t glyphsLeft = 1; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 177 | if (numGlyphs > 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 178 | glyphsLeft = numGlyphs; |
| 179 | } |
| 180 | |
| 181 | size_t index = start; |
| 182 | size_t nextIndex = 0; |
| 183 | |
| 184 | while (glyphsLeft > 0) { |
| 185 | |
Kenny Root | c9c38dd | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 186 | int32_t utfChar = utf32_from_utf8_at(text, len, index, &nextIndex); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 187 | |
| 188 | // Reached the end of the string or encountered |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 189 | if (utfChar < 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | |
| 193 | // Move to the next character in the array |
| 194 | index = nextIndex; |
| 195 | |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 196 | CachedGlyphInfo *cachedGlyph = getCachedUTFChar(utfChar); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 197 | |
| 198 | // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 199 | if (cachedGlyph->mIsValid) { |
| 200 | switch (mode) { |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 201 | case FRAMEBUFFER: |
| 202 | drawCachedGlyph(cachedGlyph, penX, penY); |
| 203 | break; |
| 204 | case BITMAP: |
| 205 | drawCachedGlyph(cachedGlyph, penX, penY, bitmap, bitmapW, bitmapH); |
| 206 | break; |
| 207 | case MEASURE: |
| 208 | measureCachedGlyph(cachedGlyph, penX, penY, bounds); |
| 209 | break; |
| 210 | } |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 213 | penX += (cachedGlyph->mAdvanceX >> 6); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 214 | |
| 215 | // If we were given a specific number of glyphs, decrement |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 216 | if (numGlyphs > 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 217 | glyphsLeft --; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 222 | Font::CachedGlyphInfo* Font::getCachedUTFChar(int32_t utfChar) { |
| 223 | |
| 224 | CachedGlyphInfo *cachedGlyph = mCachedGlyphs.valueFor((uint32_t)utfChar); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 225 | if (cachedGlyph == NULL) { |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 226 | cachedGlyph = cacheGlyph((uint32_t)utfChar); |
| 227 | } |
| 228 | // Is the glyph still in texture cache? |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 229 | if (!cachedGlyph->mIsValid) { |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 230 | updateGlyphCache(cachedGlyph); |
| 231 | } |
| 232 | |
| 233 | return cachedGlyph; |
| 234 | } |
| 235 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 236 | void Font::updateGlyphCache(CachedGlyphInfo *glyph) { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 237 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 238 | FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 239 | if (error) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 240 | ALOGE("Couldn't load glyph."); |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 241 | return; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 244 | glyph->mAdvanceX = mFace->glyph->advance.x; |
| 245 | glyph->mAdvanceY = mFace->glyph->advance.y; |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 246 | glyph->mBitmapLeft = mFace->glyph->bitmap_left; |
| 247 | glyph->mBitmapTop = mFace->glyph->bitmap_top; |
| 248 | |
| 249 | FT_Bitmap *bitmap = &mFace->glyph->bitmap; |
| 250 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 251 | // Now copy the bitmap into the cache texture |
| 252 | uint32_t startX = 0; |
| 253 | uint32_t startY = 0; |
| 254 | |
| 255 | // Let the font state figure out where to put the bitmap |
| 256 | FontState *state = &mRSC->mStateFont; |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 257 | glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 258 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 259 | if (!glyph->mIsValid) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 263 | uint32_t endX = startX + bitmap->width; |
| 264 | uint32_t endY = startY + bitmap->rows; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 265 | |
| 266 | glyph->mBitmapMinX = startX; |
| 267 | glyph->mBitmapMinY = startY; |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 268 | glyph->mBitmapWidth = bitmap->width; |
| 269 | glyph->mBitmapHeight = bitmap->rows; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 270 | |
| 271 | uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); |
| 272 | uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); |
| 273 | |
| 274 | glyph->mBitmapMinU = (float)startX / (float)cacheWidth; |
| 275 | glyph->mBitmapMinV = (float)startY / (float)cacheHeight; |
| 276 | glyph->mBitmapMaxU = (float)endX / (float)cacheWidth; |
| 277 | glyph->mBitmapMaxV = (float)endY / (float)cacheHeight; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 278 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 281 | Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 282 | CachedGlyphInfo *newGlyph = new CachedGlyphInfo(); |
| 283 | mCachedGlyphs.add(glyph, newGlyph); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 284 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 285 | newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); |
| 286 | newGlyph->mIsValid = false; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 287 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 288 | updateGlyphCache(newGlyph); |
| 289 | |
| 290 | return newGlyph; |
| 291 | } |
| 292 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 293 | Font * Font::create(Context *rsc, const char *name, float fontSize, uint32_t dpi, |
| 294 | const void *data, uint32_t dataLen) { |
Alex Sakhartchouk | 35b9644 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 295 | rsc->mStateFont.checkInit(); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 296 | Vector<Font*> &activeFonts = rsc->mStateFont.mActiveFonts; |
| 297 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 298 | for (uint32_t i = 0; i < activeFonts.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 299 | Font *ithFont = activeFonts[i]; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 300 | if (ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 301 | return ithFont; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | Font *newFont = new Font(rsc); |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 306 | bool isInitialized = newFont->init(name, fontSize, dpi, data, dataLen); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 307 | if (isInitialized) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 308 | activeFonts.push(newFont); |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 309 | rsc->mStateFont.precacheLatin(newFont); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 310 | return newFont; |
| 311 | } |
| 312 | |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 313 | ObjectBase::checkDelete(newFont); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 314 | return NULL; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 317 | Font::~Font() { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 318 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 319 | if (mFace) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 320 | FT_Done_Face(mFace); |
| 321 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 322 | #endif |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 323 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 324 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 325 | CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 326 | delete glyph; |
| 327 | } |
| 328 | } |
| 329 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 330 | FontState::FontState() { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 331 | mInitialized = false; |
| 332 | mMaxNumberOfQuads = 1024; |
| 333 | mCurrentQuadIndex = 0; |
| 334 | mRSC = NULL; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 335 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 3659d94 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 336 | mLibrary = NULL; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 337 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 338 | |
| 339 | // Get the renderer properties |
| 340 | char property[PROPERTY_VALUE_MAX]; |
| 341 | |
| 342 | // Get the gamma |
| 343 | float gamma = DEFAULT_TEXT_GAMMA; |
| 344 | if (property_get(PROPERTY_TEXT_GAMMA, property, NULL) > 0) { |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 345 | gamma = atof(property); |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Get the black gamma threshold |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 349 | int32_t blackThreshold = DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD; |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 350 | if (property_get(PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD, property, NULL) > 0) { |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 351 | blackThreshold = atoi(property); |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 352 | } |
| 353 | mBlackThreshold = (float)(blackThreshold) / 255.0f; |
| 354 | |
| 355 | // Get the white gamma threshold |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 356 | int32_t whiteThreshold = DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD; |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 357 | if (property_get(PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD, property, NULL) > 0) { |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 358 | whiteThreshold = atoi(property); |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 359 | } |
| 360 | mWhiteThreshold = (float)(whiteThreshold) / 255.0f; |
| 361 | |
| 362 | // Compute the gamma tables |
| 363 | mBlackGamma = gamma; |
| 364 | mWhiteGamma = 1.0f / gamma; |
Alex Sakhartchouk | 4f230b3 | 2010-10-12 14:15:17 -0700 | [diff] [blame] | 365 | |
| 366 | setFontColor(0.1f, 0.1f, 0.1f, 1.0f); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 369 | FontState::~FontState() { |
| 370 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 371 | delete mCacheLines[i]; |
| 372 | } |
| 373 | |
| 374 | rsAssert(!mActiveFonts.size()); |
| 375 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 376 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 377 | FT_Library FontState::getLib() { |
| 378 | if (!mLibrary) { |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 379 | FT_Error error = FT_Init_FreeType(&mLibrary); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 380 | if (error) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 381 | ALOGE("Unable to initialize freetype"); |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 382 | return NULL; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
Alex Sakhartchouk | 3659d94 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 385 | |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 386 | return mLibrary; |
| 387 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 388 | #endif //ANDROID_RS_SERIALIZE |
| 389 | |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 390 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 391 | void FontState::init(Context *rsc) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 392 | mRSC = rsc; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 395 | void FontState::flushAllAndInvalidate() { |
| 396 | if (mCurrentQuadIndex != 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 397 | issueDrawCommand(); |
| 398 | mCurrentQuadIndex = 0; |
| 399 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 400 | for (uint32_t i = 0; i < mActiveFonts.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 401 | mActiveFonts[i]->invalidateTextureCache(); |
| 402 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 403 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 404 | mCacheLines[i]->mCurrentCol = 0; |
| 405 | } |
| 406 | } |
| 407 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 408 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 409 | bool FontState::cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 410 | // If the glyph is too tall, don't cache it |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 411 | if ((uint32_t)bitmap->rows > mCacheLines[mCacheLines.size()-1]->mMaxHeight) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 412 | ALOGE("Font size to large to fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
| 416 | // Now copy the bitmap into the cache texture |
| 417 | uint32_t startX = 0; |
| 418 | uint32_t startY = 0; |
| 419 | |
| 420 | bool bitmapFit = false; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 421 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 422 | bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 423 | if (bitmapFit) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // If the new glyph didn't fit, flush the state so far and invalidate everything |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 429 | if (!bitmapFit) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 430 | flushAllAndInvalidate(); |
| 431 | |
| 432 | // Try to fit it again |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 433 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 434 | bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 435 | if (bitmapFit) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // if we still don't fit, something is wrong and we shouldn't draw |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 441 | if (!bitmapFit) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 442 | ALOGE("Bitmap doesn't fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | *retOriginX = startX; |
| 448 | *retOriginY = startY; |
| 449 | |
| 450 | uint32_t endX = startX + bitmap->width; |
| 451 | uint32_t endY = startY + bitmap->rows; |
| 452 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 453 | uint32_t cacheWidth = getCacheTextureType()->getDimX(); |
| 454 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 455 | uint8_t *cacheBuffer = (uint8_t*)mTextTexture->getPtr(); |
| 456 | uint8_t *bitmapBuffer = bitmap->buffer; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 457 | |
| 458 | uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 459 | for (cacheX = startX, bX = 0; cacheX < endX; cacheX ++, bX ++) { |
| 460 | for (cacheY = startY, bY = 0; cacheY < endY; cacheY ++, bY ++) { |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 461 | uint8_t tempCol = bitmapBuffer[bY * bitmap->width + bX]; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 462 | cacheBuffer[cacheY*cacheWidth + cacheX] = tempCol; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // This will dirty the texture and the shader so next time |
| 467 | // we draw it will upload the data |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 468 | |
| 469 | mTextTexture->sendDirty(mRSC); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 470 | mFontShaderF->bindTexture(mRSC, 0, mTextTexture.get()); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 471 | |
| 472 | // Some debug code |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 473 | /*for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 474 | ALOGE("Cache Line: H: %u Empty Space: %f", |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 475 | mCacheLines[i]->mMaxHeight, |
| 476 | (1.0f - (float)mCacheLines[i]->mCurrentCol/(float)mCacheLines[i]->mMaxWidth)*100.0f); |
| 477 | |
| 478 | }*/ |
| 479 | |
| 480 | return true; |
| 481 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 482 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 483 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 484 | void FontState::initRenderState() { |
Alex Sakhartchouk | 7ffcaf2 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 485 | String8 shaderString("varying vec2 varTex0;\n"); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 486 | shaderString.append("void main() {\n"); |
| 487 | shaderString.append(" lowp vec4 col = UNI_Color;\n"); |
| 488 | shaderString.append(" col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n"); |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 489 | shaderString.append(" col.a = pow(col.a, UNI_Gamma);\n"); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 490 | shaderString.append(" gl_FragColor = col;\n"); |
| 491 | shaderString.append("}\n"); |
| 492 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 493 | ObjectBaseRef<const Element> colorElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
| 494 | ObjectBaseRef<const Element> gammaElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); |
| 495 | Element::Builder builder; |
| 496 | builder.add(colorElem.get(), "Color", 1); |
| 497 | builder.add(gammaElem.get(), "Gamma", 1); |
| 498 | ObjectBaseRef<const Element> constInput = builder.create(mRSC); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 499 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 500 | ObjectBaseRef<Type> inputType = Type::getTypeRef(mRSC, constInput.get(), 1, 0, 0, false, false); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 501 | |
| 502 | uint32_t tmp[4]; |
| 503 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 504 | tmp[1] = (uint32_t)inputType.get(); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 505 | tmp[2] = RS_PROGRAM_PARAM_TEXTURE_TYPE; |
| 506 | tmp[3] = RS_TEXTURE_2D; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 507 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 508 | mFontShaderFConstant.set(Allocation::createAllocation(mRSC, inputType.get(), |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 509 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS)); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 510 | ProgramFragment *pf = new ProgramFragment(mRSC, shaderString.string(), |
| 511 | shaderString.length(), tmp, 4); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 512 | mFontShaderF.set(pf); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 513 | mFontShaderF->bindAllocation(mRSC, mFontShaderFConstant.get(), 0); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 514 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 515 | mFontSampler.set(Sampler::getSampler(mRSC, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, |
| 516 | RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP).get()); |
| 517 | mFontShaderF->bindSampler(mRSC, 0, mFontSampler.get()); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 518 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 519 | mFontProgramStore.set(ProgramStore::getProgramStore(mRSC, true, true, true, true, |
| 520 | false, false, |
| 521 | RS_BLEND_SRC_SRC_ALPHA, |
| 522 | RS_BLEND_DST_ONE_MINUS_SRC_ALPHA, |
| 523 | RS_DEPTH_FUNC_ALWAYS).get()); |
Jason Sams | 8feea4e | 2011-03-18 15:03:25 -0700 | [diff] [blame] | 524 | mFontProgramStore->init(); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 527 | void FontState::initTextTexture() { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 528 | ObjectBaseRef<const Element> alphaElem = Element::createRef(mRSC, RS_TYPE_UNSIGNED_8, RS_KIND_PIXEL_A, true, 1); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 529 | |
| 530 | // We will allocate a texture to initially hold 32 character bitmaps |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 531 | ObjectBaseRef<Type> texType = Type::getTypeRef(mRSC, alphaElem.get(), 1024, 256, 0, false, false); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 532 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 533 | Allocation *cacheAlloc = Allocation::createAllocation(mRSC, texType.get(), |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 534 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 535 | mTextTexture.set(cacheAlloc); |
Jason Sams | b7e83bd | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 536 | mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 537 | |
| 538 | // Split up our cache texture into lines of certain widths |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 539 | int32_t nextLine = 0; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 540 | mCacheLines.push(new CacheTextureLine(16, texType->getDimX(), nextLine, 0)); |
| 541 | nextLine += mCacheLines.top()->mMaxHeight; |
| 542 | mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0)); |
| 543 | nextLine += mCacheLines.top()->mMaxHeight; |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 544 | mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0)); |
| 545 | nextLine += mCacheLines.top()->mMaxHeight; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 546 | mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); |
| 547 | nextLine += mCacheLines.top()->mMaxHeight; |
| 548 | mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); |
| 549 | nextLine += mCacheLines.top()->mMaxHeight; |
| 550 | mCacheLines.push(new CacheTextureLine(40, texType->getDimX(), nextLine, 0)); |
| 551 | nextLine += mCacheLines.top()->mMaxHeight; |
| 552 | mCacheLines.push(new CacheTextureLine(texType->getDimY() - nextLine, texType->getDimX(), nextLine, 0)); |
| 553 | } |
| 554 | |
| 555 | // Avoid having to reallocate memory and render quad by quad |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 556 | void FontState::initVertexArrayBuffers() { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 557 | // Now lets write index data |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 558 | ObjectBaseRef<const Element> indexElem = Element::createRef(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 559 | uint32_t numIndicies = mMaxNumberOfQuads * 6; |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 560 | ObjectBaseRef<Type> indexType = Type::getTypeRef(mRSC, indexElem.get(), numIndicies, 0, 0, false, false); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 561 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 562 | Allocation *indexAlloc = Allocation::createAllocation(mRSC, indexType.get(), |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 563 | RS_ALLOCATION_USAGE_SCRIPT | |
| 564 | RS_ALLOCATION_USAGE_GRAPHICS_VERTEX); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 565 | uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr(); |
| 566 | |
| 567 | // Four verts, two triangles , six indices per quad |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 568 | for (uint32_t i = 0; i < mMaxNumberOfQuads; i ++) { |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 569 | int32_t i6 = i * 6; |
| 570 | int32_t i4 = i * 4; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 571 | |
| 572 | indexPtr[i6 + 0] = i4 + 0; |
| 573 | indexPtr[i6 + 1] = i4 + 1; |
| 574 | indexPtr[i6 + 2] = i4 + 2; |
| 575 | |
| 576 | indexPtr[i6 + 3] = i4 + 0; |
| 577 | indexPtr[i6 + 4] = i4 + 2; |
| 578 | indexPtr[i6 + 5] = i4 + 3; |
| 579 | } |
| 580 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 581 | indexAlloc->sendDirty(mRSC); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 582 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 583 | ObjectBaseRef<const Element> posElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3); |
| 584 | ObjectBaseRef<const Element> texElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 585 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 586 | Element::Builder builder; |
| 587 | builder.add(posElem.get(), "position", 1); |
| 588 | builder.add(texElem.get(), "texture0", 1); |
| 589 | ObjectBaseRef<const Element> vertexDataElem = builder.create(mRSC); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 590 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 591 | ObjectBaseRef<Type> vertexDataType = Type::getTypeRef(mRSC, vertexDataElem.get(), |
| 592 | mMaxNumberOfQuads * 4, |
| 593 | 0, 0, false, false); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 594 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 595 | Allocation *vertexAlloc = Allocation::createAllocation(mRSC, vertexDataType.get(), |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 596 | RS_ALLOCATION_USAGE_SCRIPT); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 597 | mTextMeshPtr = (float*)vertexAlloc->getPtr(); |
| 598 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 599 | mMesh.set(new Mesh(mRSC, 1, 1)); |
| 600 | mMesh->setVertexBuffer(vertexAlloc, 0); |
| 601 | mMesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, 0); |
| 602 | mMesh->init(); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // We don't want to allocate anything unless we actually draw text |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 606 | void FontState::checkInit() { |
| 607 | if (mInitialized) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | |
| 611 | initTextTexture(); |
| 612 | initRenderState(); |
| 613 | |
| 614 | initVertexArrayBuffers(); |
| 615 | |
Alex Sakhartchouk | 35b9644 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 616 | // We store a string with letters in a rough frequency of occurrence |
| 617 | mLatinPrecache = String8(" eisarntolcdugpmhbyfvkwzxjq"); |
| 618 | mLatinPrecache += String8("EISARNTOLCDUGPMHBYFVKWZXJQ"); |
| 619 | mLatinPrecache += String8(",.?!()-+@;:`'"); |
| 620 | mLatinPrecache += String8("0123456789"); |
| 621 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 622 | mInitialized = true; |
| 623 | } |
| 624 | |
| 625 | void FontState::issueDrawCommand() { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 626 | Context::PushState ps(mRSC); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 627 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 628 | mRSC->setProgramVertex(mRSC->getDefaultProgramVertex()); |
| 629 | mRSC->setProgramRaster(mRSC->getDefaultProgramRaster()); |
| 630 | mRSC->setProgramFragment(mFontShaderF.get()); |
| 631 | mRSC->setProgramStore(mFontProgramStore.get()); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 632 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 633 | if (mConstantsDirty) { |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 634 | mFontShaderFConstant->data(mRSC, 0, 0, 1, &mConstants, sizeof(mConstants)); |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 635 | mConstantsDirty = false; |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 638 | if (!mRSC->setupCheck()) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 642 | mMesh->renderPrimitiveRange(mRSC, 0, 0, mCurrentQuadIndex * 6); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void FontState::appendMeshQuad(float x1, float y1, float z1, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 646 | float u1, float v1, |
| 647 | float x2, float y2, float z2, |
| 648 | float u2, float v2, |
| 649 | float x3, float y3, float z3, |
| 650 | float u3, float v3, |
| 651 | float x4, float y4, float z4, |
| 652 | float u4, float v4) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 653 | const uint32_t vertsPerQuad = 4; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 654 | const uint32_t floatsPerVert = 6; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 655 | float *currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; |
| 656 | |
Alex Sakhartchouk | a74a8f6 | 2011-11-16 12:22:10 -0800 | [diff] [blame] | 657 | if (x1 > mSurfaceWidth || y1 < 0.0f || x2 < 0 || y4 > mSurfaceHeight) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
| 661 | /*LOGE("V0 x: %f y: %f z: %f", x1, y1, z1); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 662 | ALOGE("V1 x: %f y: %f z: %f", x2, y2, z2); |
| 663 | ALOGE("V2 x: %f y: %f z: %f", x3, y3, z3); |
| 664 | ALOGE("V3 x: %f y: %f z: %f", x4, y4, z4);*/ |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 665 | |
| 666 | (*currentPos++) = x1; |
| 667 | (*currentPos++) = y1; |
| 668 | (*currentPos++) = z1; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 669 | (*currentPos++) = 0; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 670 | (*currentPos++) = u1; |
| 671 | (*currentPos++) = v1; |
| 672 | |
| 673 | (*currentPos++) = x2; |
| 674 | (*currentPos++) = y2; |
| 675 | (*currentPos++) = z2; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 676 | (*currentPos++) = 0; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 677 | (*currentPos++) = u2; |
| 678 | (*currentPos++) = v2; |
| 679 | |
| 680 | (*currentPos++) = x3; |
| 681 | (*currentPos++) = y3; |
| 682 | (*currentPos++) = z3; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 683 | (*currentPos++) = 0; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 684 | (*currentPos++) = u3; |
| 685 | (*currentPos++) = v3; |
| 686 | |
| 687 | (*currentPos++) = x4; |
| 688 | (*currentPos++) = y4; |
| 689 | (*currentPos++) = z4; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 690 | (*currentPos++) = 0; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 691 | (*currentPos++) = u4; |
| 692 | (*currentPos++) = v4; |
| 693 | |
| 694 | mCurrentQuadIndex ++; |
| 695 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 696 | if (mCurrentQuadIndex == mMaxNumberOfQuads) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 697 | issueDrawCommand(); |
| 698 | mCurrentQuadIndex = 0; |
| 699 | } |
| 700 | } |
| 701 | |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 702 | uint32_t FontState::getRemainingCacheCapacity() { |
| 703 | uint32_t remainingCapacity = 0; |
Alex Sakhartchouk | 35b9644 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 704 | uint32_t totalPixels = 0; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 705 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 706 | remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol); |
| 707 | totalPixels += mCacheLines[i]->mMaxWidth; |
| 708 | } |
| 709 | remainingCapacity = (remainingCapacity * 100) / totalPixels; |
| 710 | return remainingCapacity; |
| 711 | } |
| 712 | |
| 713 | void FontState::precacheLatin(Font *font) { |
| 714 | // Remaining capacity is measured in % |
| 715 | uint32_t remainingCapacity = getRemainingCacheCapacity(); |
| 716 | uint32_t precacheIdx = 0; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 717 | while (remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) { |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 718 | font->getCachedUTFChar((int32_t)mLatinPrecache[precacheIdx]); |
| 719 | remainingCapacity = getRemainingCacheCapacity(); |
| 720 | precacheIdx ++; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 725 | void FontState::renderText(const char *text, uint32_t len, int32_t x, int32_t y, |
| 726 | uint32_t startIndex, int32_t numGlyphs, |
| 727 | Font::RenderMode mode, |
| 728 | Font::Rect *bounds, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 729 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 730 | checkInit(); |
| 731 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 732 | // Render code here |
| 733 | Font *currentFont = mRSC->getFont(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 734 | if (!currentFont) { |
| 735 | if (!mDefault.get()) { |
Christian Robertson | 1f5f9a3 | 2011-08-09 15:24:25 -0700 | [diff] [blame] | 736 | String8 fontsDir("/fonts/Roboto-Regular.ttf"); |
Alex Sakhartchouk | c17ace2 | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 737 | String8 fullPath(getenv("ANDROID_ROOT")); |
| 738 | fullPath += fontsDir; |
| 739 | |
Alex Sakhartchouk | 7b3e9bd | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 740 | mDefault.set(Font::create(mRSC, fullPath.string(), 8, mRSC->getDPI())); |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 741 | } |
| 742 | currentFont = mDefault.get(); |
| 743 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 744 | if (!currentFont) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 745 | ALOGE("Unable to initialize any fonts"); |
Alex Sakhartchouk | 3659d94 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
Alex Sakhartchouk | a74a8f6 | 2011-11-16 12:22:10 -0800 | [diff] [blame] | 749 | // Cull things that are off the screen |
| 750 | mSurfaceWidth = (float)mRSC->getCurrentSurfaceWidth(); |
| 751 | mSurfaceHeight = (float)mRSC->getCurrentSurfaceHeight(); |
| 752 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 753 | currentFont->renderUTF(text, len, x, y, startIndex, numGlyphs, |
| 754 | mode, bounds, bitmap, bitmapW, bitmapH); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 755 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 756 | if (mCurrentQuadIndex != 0) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 757 | issueDrawCommand(); |
| 758 | mCurrentQuadIndex = 0; |
| 759 | } |
| 760 | } |
| 761 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 762 | void FontState::measureText(const char *text, uint32_t len, Font::Rect *bounds) { |
| 763 | renderText(text, len, 0, 0, 0, -1, Font::MEASURE, bounds); |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 764 | bounds->bottom = - bounds->bottom; |
| 765 | bounds->top = - bounds->top; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Alex Sakhartchouk | 9fc9f03 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 768 | void FontState::setFontColor(float r, float g, float b, float a) { |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 769 | mConstants.mFontColor[0] = r; |
| 770 | mConstants.mFontColor[1] = g; |
| 771 | mConstants.mFontColor[2] = b; |
| 772 | mConstants.mFontColor[3] = a; |
| 773 | |
| 774 | mConstants.mGamma = 1.0f; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 775 | const float luminance = (r * 2.0f + g * 5.0f + b) / 8.0f; |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 776 | if (luminance <= mBlackThreshold) { |
| 777 | mConstants.mGamma = mBlackGamma; |
| 778 | } else if (luminance >= mWhiteThreshold) { |
| 779 | mConstants.mGamma = mWhiteGamma; |
| 780 | } |
Alex Sakhartchouk | 4f230b3 | 2010-10-12 14:15:17 -0700 | [diff] [blame] | 781 | |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 782 | mConstantsDirty = true; |
Alex Sakhartchouk | 9fc9f03 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 785 | void FontState::getFontColor(float *r, float *g, float *b, float *a) const { |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 786 | *r = mConstants.mFontColor[0]; |
| 787 | *g = mConstants.mFontColor[1]; |
| 788 | *b = mConstants.mFontColor[2]; |
| 789 | *a = mConstants.mFontColor[3]; |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 792 | void FontState::deinit(Context *rsc) { |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 793 | mInitialized = false; |
| 794 | |
Stephen Hines | 01b7d29 | 2010-09-28 15:45:45 -0700 | [diff] [blame] | 795 | mFontShaderFConstant.clear(); |
| 796 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 797 | mMesh.clear(); |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 798 | |
| 799 | mFontShaderF.clear(); |
| 800 | mFontSampler.clear(); |
| 801 | mFontProgramStore.clear(); |
| 802 | |
| 803 | mTextTexture.clear(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 804 | for (uint32_t i = 0; i < mCacheLines.size(); i ++) { |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 805 | delete mCacheLines[i]; |
| 806 | } |
| 807 | mCacheLines.clear(); |
| 808 | |
| 809 | mDefault.clear(); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 810 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 811 | if (mLibrary) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 812 | FT_Done_FreeType( mLibrary ); |
Alex Sakhartchouk | 3659d94 | 2010-06-30 16:53:43 -0700 | [diff] [blame] | 813 | mLibrary = NULL; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 814 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 815 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 818 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 819 | bool FontState::CacheTextureLine::fitBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { |
| 820 | if ((uint32_t)bitmap->rows > mMaxHeight) { |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | if (mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) { |
| 825 | *retOriginX = mCurrentCol; |
| 826 | *retOriginY = mCurrentRow; |
| 827 | mCurrentCol += bitmap->width; |
| 828 | mDirty = true; |
| 829 | return true; |
| 830 | } |
| 831 | |
| 832 | return false; |
| 833 | } |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 834 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 835 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 836 | namespace android { |
| 837 | namespace renderscript { |
| 838 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 839 | RsFont rsi_FontCreateFromFile(Context *rsc, |
| 840 | char const *name, size_t name_length, |
| 841 | float fontSize, uint32_t dpi) { |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 842 | Font *newFont = Font::create(rsc, name, fontSize, dpi); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 843 | if (newFont) { |
Alex Sakhartchouk | a1ccecd | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 844 | newFont->incUserRef(); |
| 845 | } |
| 846 | return newFont; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 849 | RsFont rsi_FontCreateFromMemory(Context *rsc, |
| 850 | char const *name, size_t name_length, |
| 851 | float fontSize, uint32_t dpi, |
| 852 | const void *data, size_t data_length) { |
| 853 | Font *newFont = Font::create(rsc, name, fontSize, dpi, data, data_length); |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 854 | if (newFont) { |
| 855 | newFont->incUserRef(); |
| 856 | } |
| 857 | return newFont; |
| 858 | } |
| 859 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 860 | } // renderscript |
| 861 | } // android |