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