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