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