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