Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_FONT_RENDERER_H |
| 18 | #define ANDROID_HWUI_FONT_RENDERER_H |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/String8.h> |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 21 | #include <utils/String16.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 22 | #include <utils/Vector.h> |
| 23 | #include <utils/KeyedVector.h> |
| 24 | |
| 25 | #include <SkScalerContext.h> |
| 26 | #include <SkPaint.h> |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 27 | #include <SkPathMeasure.h> |
| 28 | #include <SkPoint.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 29 | |
| 30 | #include <GLES2/gl2.h> |
| 31 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 32 | #include "Rect.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 33 | #include "Properties.h" |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 34 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | // Defines |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | #if RENDER_TEXT_AS_GLYPHS |
| 43 | typedef uint16_t glyph_t; |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 44 | #define TO_GLYPH(g) g |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 45 | #define GET_METRICS(paint, glyph) paint->getGlyphMetrics(glyph) |
| 46 | #define GET_GLYPH(text) nextGlyph((const uint16_t**) &text) |
| 47 | #define IS_END_OF_STRING(glyph) false |
| 48 | #else |
| 49 | typedef SkUnichar glyph_t; |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 50 | #define TO_GLYPH(g) ((SkUnichar) g) |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 51 | #define GET_METRICS(paint, glyph) paint->getUnicharMetrics(glyph) |
| 52 | #define GET_GLYPH(text) SkUTF16_NextUnichar((const uint16_t**) &text) |
| 53 | #define IS_END_OF_STRING(glyph) glyph < 0 |
| 54 | #endif |
| 55 | |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 56 | #define TEXTURE_BORDER_SIZE 1 |
| 57 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | // Declarations |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 62 | class FontRenderer; |
| 63 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 64 | class CacheTexture { |
| 65 | public: |
Romain Guy | 0aa87bb | 2012-07-20 11:14:32 -0700 | [diff] [blame] | 66 | CacheTexture(uint16_t width, uint16_t height) : |
| 67 | mTexture(NULL), mTextureId(0), mWidth(width), mHeight(height), |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 68 | mLinearFiltering(false) { } |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 69 | ~CacheTexture() { |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 70 | if (mTexture) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 71 | delete[] mTexture; |
| 72 | } |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 73 | if (mTextureId) { |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 74 | glDeleteTextures(1, &mTextureId); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | uint8_t* mTexture; |
| 79 | GLuint mTextureId; |
| 80 | uint16_t mWidth; |
| 81 | uint16_t mHeight; |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 82 | bool mLinearFiltering; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 85 | /** |
| 86 | * CacheBlock is a noce in a linked list of current free space areas in a CacheTextureLine. |
| 87 | * Using CacheBlocks enables us to pack the cache line from top to bottom as well as left to right. |
| 88 | * When we add a glyph to the cache, we see if it fits within one of the existing columns that |
| 89 | * have already been started (this is the case if the glyph fits vertically as well as |
| 90 | * horizontally, and if its width is sufficiently close to the column width to avoid |
| 91 | * sub-optimal packing of small glyphs into wide columns). If there is no column in which the |
| 92 | * glyph fits, we check the final node, which is the remaining space in the cache line, creating |
| 93 | * a new column as appropriate. |
| 94 | * |
| 95 | * As columns fill up, we remove their CacheBlock from the list to avoid having to check |
| 96 | * small blocks in the future. |
| 97 | */ |
| 98 | struct CacheBlock { |
| 99 | uint16_t mX; |
| 100 | uint16_t mY; |
| 101 | uint16_t mWidth; |
| 102 | uint16_t mHeight; |
| 103 | CacheBlock* mNext; |
| 104 | CacheBlock* mPrev; |
| 105 | |
| 106 | CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool empty = false): |
| 107 | mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | static CacheBlock* insertBlock(CacheBlock* head, CacheBlock *newBlock); |
| 112 | |
| 113 | static CacheBlock* removeBlock(CacheBlock* head, CacheBlock *blockToRemove); |
| 114 | |
| 115 | void output() { |
| 116 | CacheBlock *currBlock = this; |
| 117 | while (currBlock) { |
| 118 | ALOGD("Block: this, x, y, w, h = %p, %d, %d, %d, %d", |
| 119 | currBlock, currBlock->mX, currBlock->mY, currBlock->mWidth, currBlock->mHeight); |
| 120 | currBlock = currBlock->mNext; |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 125 | class CacheTextureLine { |
| 126 | public: |
| 127 | CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 128 | CacheTexture* cacheTexture): |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 129 | mMaxHeight(maxHeight), |
| 130 | mMaxWidth(maxWidth), |
| 131 | mCurrentRow(currentRow), |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 132 | mDirty(false), |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 133 | mNumGlyphs(0), |
Romain Guy | 9d9758a | 2012-05-14 15:19:58 -0700 | [diff] [blame] | 134 | mCacheTexture(cacheTexture) { |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 135 | mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 136 | maxWidth - TEXTURE_BORDER_SIZE, maxHeight - TEXTURE_BORDER_SIZE, true); |
| 137 | } |
| 138 | |
| 139 | ~CacheTextureLine() { |
| 140 | reset(); |
| 141 | } |
| 142 | |
| 143 | void reset() { |
| 144 | // Delete existing cache blocks |
| 145 | while (mCacheBlocks != NULL) { |
| 146 | CacheBlock* tmpBlock = mCacheBlocks; |
| 147 | mCacheBlocks = mCacheBlocks->mNext; |
| 148 | delete tmpBlock; |
| 149 | } |
| 150 | mNumGlyphs = 0; |
| 151 | } |
| 152 | |
| 153 | void init() { |
| 154 | // reset, then create a new remainder space to start again |
| 155 | reset(); |
| 156 | mCacheBlocks = new CacheBlock(TEXTURE_BORDER_SIZE, TEXTURE_BORDER_SIZE, |
| 157 | mMaxWidth - TEXTURE_BORDER_SIZE, mMaxHeight - TEXTURE_BORDER_SIZE, true); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); |
| 161 | |
| 162 | uint16_t mMaxHeight; |
| 163 | uint16_t mMaxWidth; |
| 164 | uint32_t mCurrentRow; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 165 | bool mDirty; |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 166 | uint16_t mNumGlyphs; |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 167 | CacheTexture* mCacheTexture; |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 168 | CacheBlock* mCacheBlocks; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | struct CachedGlyphInfo { |
| 172 | // Has the cache been invalidated? |
| 173 | bool mIsValid; |
| 174 | // Location of the cached glyph in the bitmap |
| 175 | // in case we need to resize the texture or |
| 176 | // render to bitmap |
| 177 | uint32_t mStartX; |
| 178 | uint32_t mStartY; |
| 179 | uint32_t mBitmapWidth; |
| 180 | uint32_t mBitmapHeight; |
| 181 | // Also cache texture coords for the quad |
| 182 | float mBitmapMinU; |
| 183 | float mBitmapMinV; |
| 184 | float mBitmapMaxU; |
| 185 | float mBitmapMaxV; |
| 186 | // Minimize how much we call freetype |
| 187 | uint32_t mGlyphIndex; |
| 188 | uint32_t mAdvanceX; |
| 189 | uint32_t mAdvanceY; |
| 190 | // Values below contain a glyph's origin in the bitmap |
| 191 | int32_t mBitmapLeft; |
| 192 | int32_t mBitmapTop; |
| 193 | // Auto-kerning |
| 194 | SkFixed mLsbDelta; |
| 195 | SkFixed mRsbDelta; |
| 196 | CacheTextureLine* mCachedTextureLine; |
| 197 | }; |
| 198 | |
| 199 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 200 | /////////////////////////////////////////////////////////////////////////////// |
| 201 | // Font |
| 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Represents a font, defined by a Skia font id and a font size. A font is used |
| 206 | * to generate glyphs and cache them in the FontState. |
| 207 | */ |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 208 | class Font { |
| 209 | public: |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 210 | enum Style { |
Romain Guy | c7b25be | 2011-03-23 14:59:20 -0700 | [diff] [blame] | 211 | kFakeBold = 1 |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 214 | ~Font(); |
| 215 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Renders the specified string of text. |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 218 | * If bitmap is specified, it will be used as the render target |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 219 | */ |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 220 | void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 221 | int numGlyphs, int x, int y, uint8_t *bitmap = NULL, |
| 222 | uint32_t bitmapW = 0, uint32_t bitmapH = 0); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 223 | |
| 224 | void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 225 | int numGlyphs, int x, int y, const float* positions); |
| 226 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 227 | void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 228 | int numGlyphs, SkPath* path, float hOffset, float vOffset); |
| 229 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Creates a new font associated with the specified font state. |
| 232 | */ |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 233 | static Font* create(FontRenderer* state, uint32_t fontId, float fontSize, |
Romain Guy | bd496bc | 2011-08-02 17:32:41 -0700 | [diff] [blame] | 234 | int flags, uint32_t italicStyle, uint32_t scaleX, SkPaint::Style style, |
| 235 | uint32_t strokeWidth); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 236 | |
| 237 | protected: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 238 | friend class FontRenderer; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 239 | typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*, |
| 240 | uint32_t, uint32_t, Rect*, const float*); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 241 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 242 | enum RenderMode { |
| 243 | FRAMEBUFFER, |
| 244 | BITMAP, |
| 245 | MEASURE, |
| 246 | }; |
| 247 | |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 248 | void precache(SkPaint* paint, const char* text, int numGlyphs); |
| 249 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 250 | void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 251 | int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 252 | uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 253 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 254 | void measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 255 | int numGlyphs, Rect *bounds, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 256 | |
Chet Haase | 8668f8a | 2011-03-02 13:51:36 -0800 | [diff] [blame] | 257 | Font(FontRenderer* state, uint32_t fontId, float fontSize, int flags, uint32_t italicStyle, |
Romain Guy | bd496bc | 2011-08-02 17:32:41 -0700 | [diff] [blame] | 258 | uint32_t scaleX, SkPaint::Style style, uint32_t strokeWidth); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 259 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 260 | // Cache of glyphs |
| 261 | DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 262 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 263 | void invalidateTextureCache(CacheTextureLine *cacheLine = NULL); |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 264 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 265 | CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 266 | void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph); |
| 267 | |
| 268 | void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y, |
| 269 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, |
| 270 | Rect* bounds, const float* pos); |
| 271 | void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, |
| 272 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, |
| 273 | Rect* bounds, const float* pos); |
| 274 | void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, |
| 275 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, |
| 276 | Rect* bounds, const float* pos); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 277 | void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, |
| 278 | SkPathMeasure& measure, SkPoint* position, SkVector* tangent); |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 279 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 280 | CachedGlyphInfo* getCachedGlyph(SkPaint* paint, glyph_t textUnit); |
| 281 | |
| 282 | static glyph_t nextGlyph(const uint16_t** srcPtr) { |
| 283 | const uint16_t* src = *srcPtr; |
| 284 | glyph_t g = *src++; |
| 285 | *srcPtr = src; |
| 286 | return g; |
| 287 | } |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 288 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 289 | FontRenderer* mState; |
| 290 | uint32_t mFontId; |
| 291 | float mFontSize; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 292 | int mFlags; |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 293 | uint32_t mItalicStyle; |
Chet Haase | 8668f8a | 2011-03-02 13:51:36 -0800 | [diff] [blame] | 294 | uint32_t mScaleX; |
Romain Guy | bd496bc | 2011-08-02 17:32:41 -0700 | [diff] [blame] | 295 | SkPaint::Style mStyle; |
| 296 | uint32_t mStrokeWidth; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 299 | /////////////////////////////////////////////////////////////////////////////// |
| 300 | // Renderer |
| 301 | /////////////////////////////////////////////////////////////////////////////// |
| 302 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 303 | class FontRenderer { |
| 304 | public: |
| 305 | FontRenderer(); |
| 306 | ~FontRenderer(); |
| 307 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 308 | void flushLargeCaches(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 309 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 310 | void setGammaTable(const uint8_t* gammaTable) { |
| 311 | mGammaTable = gammaTable; |
| 312 | } |
| 313 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 314 | void setFont(SkPaint* paint, uint32_t fontId, float fontSize); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 315 | |
| 316 | void precache(SkPaint* paint, const char* text, int numGlyphs); |
| 317 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 318 | // bounds is an out parameter |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 319 | bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 320 | uint32_t len, int numGlyphs, int x, int y, Rect* bounds); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 321 | // bounds is an out parameter |
| 322 | bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 323 | uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 324 | // bounds is an out parameter |
| 325 | bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 326 | uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 327 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 328 | struct DropShadow { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 329 | DropShadow() { }; |
| 330 | |
| 331 | DropShadow(const DropShadow& dropShadow): |
| 332 | width(dropShadow.width), height(dropShadow.height), |
| 333 | image(dropShadow.image), penX(dropShadow.penX), |
| 334 | penY(dropShadow.penY) { |
| 335 | } |
| 336 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 337 | uint32_t width; |
| 338 | uint32_t height; |
| 339 | uint8_t* image; |
| 340 | int32_t penX; |
| 341 | int32_t penY; |
| 342 | }; |
| 343 | |
| 344 | // After renderDropShadow returns, the called owns the memory in DropShadow.image |
| 345 | // and is responsible for releasing it when it's done with it |
| 346 | DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 347 | uint32_t len, int numGlyphs, uint32_t radius, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 348 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 349 | GLuint getTexture(bool linearFiltering = false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 350 | checkInit(); |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 351 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 352 | if (linearFiltering != mCurrentCacheTexture->mLinearFiltering) { |
| 353 | mCurrentCacheTexture->mLinearFiltering = linearFiltering; |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 354 | mLinearFiltering = linearFiltering; |
| 355 | const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST; |
| 356 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 357 | glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->mTextureId); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 358 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); |
| 359 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); |
| 360 | } |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 361 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 362 | return mCurrentCacheTexture->mTextureId; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 365 | uint32_t getCacheSize() const { |
| 366 | uint32_t size = 0; |
| 367 | if (mCacheTextureSmall != NULL && mCacheTextureSmall->mTexture != NULL) { |
| 368 | size += mCacheTextureSmall->mWidth * mCacheTextureSmall->mHeight; |
| 369 | } |
| 370 | if (mCacheTexture128 != NULL && mCacheTexture128->mTexture != NULL) { |
| 371 | size += mCacheTexture128->mWidth * mCacheTexture128->mHeight; |
| 372 | } |
| 373 | if (mCacheTexture256 != NULL && mCacheTexture256->mTexture != NULL) { |
| 374 | size += mCacheTexture256->mWidth * mCacheTexture256->mHeight; |
| 375 | } |
| 376 | if (mCacheTexture512 != NULL && mCacheTexture512->mTexture != NULL) { |
| 377 | size += mCacheTexture512->mWidth * mCacheTexture512->mHeight; |
| 378 | } |
| 379 | return size; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 382 | protected: |
| 383 | friend class Font; |
| 384 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 385 | const uint8_t* mGammaTable; |
| 386 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 387 | void allocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 388 | void deallocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 389 | void initTextTexture(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 390 | CacheTexture* createCacheTexture(int width, int height, bool allocate); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 391 | void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
| 392 | uint32_t *retOriginX, uint32_t *retOriginY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 393 | |
| 394 | void flushAllAndInvalidate(); |
| 395 | void initVertexArrayBuffers(); |
| 396 | |
| 397 | void checkInit(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 398 | void initRender(const Rect* clip, Rect* bounds); |
| 399 | void finishRender(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 400 | |
| 401 | void issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 402 | void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 403 | float x2, float y2, float u2, float v2, |
| 404 | float x3, float y3, float u3, float v3, |
| 405 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 406 | void appendMeshQuad(float x1, float y1, float u1, float v1, |
| 407 | float x2, float y2, float u2, float v2, |
| 408 | float x3, float y3, float u3, float v3, |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 409 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 410 | void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 411 | float x2, float y2, float u2, float v2, |
| 412 | float x3, float y3, float u3, float v3, |
| 413 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 414 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 415 | uint32_t mSmallCacheWidth; |
| 416 | uint32_t mSmallCacheHeight; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 417 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 418 | Vector<CacheTextureLine*> mCacheLines; |
| 419 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 420 | Font* mCurrentFont; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 421 | Vector<Font*> mActiveFonts; |
| 422 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 423 | CacheTexture* mCurrentCacheTexture; |
| 424 | CacheTexture* mLastCacheTexture; |
| 425 | CacheTexture* mCacheTextureSmall; |
| 426 | CacheTexture* mCacheTexture128; |
| 427 | CacheTexture* mCacheTexture256; |
| 428 | CacheTexture* mCacheTexture512; |
| 429 | |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 430 | void checkTextureUpdate(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 431 | bool mUploadTexture; |
| 432 | |
| 433 | // Pointer to vertex data to speed up frame to frame work |
| 434 | float *mTextMeshPtr; |
| 435 | uint32_t mCurrentQuadIndex; |
| 436 | uint32_t mMaxNumberOfQuads; |
| 437 | |
| 438 | uint32_t mIndexBufferID; |
| 439 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 440 | const Rect* mClip; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 441 | Rect* mBounds; |
| 442 | bool mDrawn; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 443 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 444 | bool mInitialized; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 445 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 446 | bool mLinearFiltering; |
| 447 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 448 | void computeGaussianWeights(float* weights, int32_t radius); |
| 449 | void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 450 | int32_t width, int32_t height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 451 | void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 452 | int32_t width, int32_t height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 453 | void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | }; // namespace uirenderer |
| 457 | }; // namespace android |
| 458 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 459 | #endif // ANDROID_HWUI_FONT_RENDERER_H |