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> |
| 27 | |
| 28 | #include <GLES2/gl2.h> |
| 29 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 30 | #include "Rect.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 31 | #include "Properties.h" |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 32 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
| 36 | class FontRenderer; |
| 37 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Represents a font, defined by a Skia font id and a font size. A font is used |
| 40 | * to generate glyphs and cache them in the FontState. |
| 41 | */ |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 42 | class Font { |
| 43 | public: |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 44 | enum Style { |
| 45 | kFakeBold |
| 46 | }; |
| 47 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 48 | ~Font(); |
| 49 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Renders the specified string of text. |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 52 | * If bitmap is specified, it will be used as the render target |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 53 | */ |
| 54 | void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 55 | int numGlyphs, int x, int y, |
| 56 | uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Creates a new font associated with the specified font state. |
| 59 | */ |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 60 | static Font* create(FontRenderer* state, uint32_t fontId, float fontSize, |
| 61 | int flags, uint32_t italicStyle); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 62 | |
| 63 | protected: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 64 | friend class FontRenderer; |
| 65 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 66 | enum RenderMode { |
| 67 | FRAMEBUFFER, |
| 68 | BITMAP, |
| 69 | MEASURE, |
| 70 | }; |
| 71 | |
| 72 | void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 73 | int numGlyphs, int x, int y, RenderMode mode, |
| 74 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, |
| 75 | Rect *bounds); |
| 76 | |
| 77 | void measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
| 78 | int numGlyphs, Rect *bounds); |
| 79 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 80 | struct CachedGlyphInfo { |
| 81 | // Has the cache been invalidated? |
| 82 | bool mIsValid; |
| 83 | // Location of the cached glyph in the bitmap |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 84 | // in case we need to resize the texture or |
| 85 | // render to bitmap |
| 86 | uint32_t mStartX; |
| 87 | uint32_t mStartY; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 88 | uint32_t mBitmapWidth; |
| 89 | uint32_t mBitmapHeight; |
| 90 | // Also cache texture coords for the quad |
| 91 | float mBitmapMinU; |
| 92 | float mBitmapMinV; |
| 93 | float mBitmapMaxU; |
| 94 | float mBitmapMaxV; |
| 95 | // Minimize how much we call freetype |
| 96 | uint32_t mGlyphIndex; |
| 97 | uint32_t mAdvanceX; |
| 98 | uint32_t mAdvanceY; |
| 99 | // Values below contain a glyph's origin in the bitmap |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 100 | int32_t mBitmapLeft; |
| 101 | int32_t mBitmapTop; |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 102 | // Auto-kerning |
| 103 | SkFixed mLsbDelta; |
| 104 | SkFixed mRsbDelta; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 107 | Font(FontRenderer* state, uint32_t fontId, float fontSize, int flags, uint32_t italicStyle); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 108 | |
| 109 | DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; |
| 110 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 111 | void invalidateTextureCache(); |
| 112 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 113 | CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 114 | void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 115 | void measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 116 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 117 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y, |
| 118 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH); |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 119 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 120 | CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar); |
| 121 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 122 | FontRenderer* mState; |
| 123 | uint32_t mFontId; |
| 124 | float mFontSize; |
Romain Guy | 325a0f9 | 2011-01-05 15:26:55 -0800 | [diff] [blame] | 125 | int mFlags; |
Romain Guy | 2577db1 | 2011-01-18 13:02:38 -0800 | [diff] [blame] | 126 | uint32_t mItalicStyle; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | class FontRenderer { |
| 130 | public: |
| 131 | FontRenderer(); |
| 132 | ~FontRenderer(); |
| 133 | |
| 134 | void init(); |
| 135 | void deinit(); |
| 136 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 137 | void setGammaTable(const uint8_t* gammaTable) { |
| 138 | mGammaTable = gammaTable; |
| 139 | } |
| 140 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 141 | void setFont(SkPaint* paint, uint32_t fontId, float fontSize); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 142 | bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 143 | uint32_t len, int numGlyphs, int x, int y, Rect* bounds); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 144 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 145 | struct DropShadow { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 146 | DropShadow() { }; |
| 147 | |
| 148 | DropShadow(const DropShadow& dropShadow): |
| 149 | width(dropShadow.width), height(dropShadow.height), |
| 150 | image(dropShadow.image), penX(dropShadow.penX), |
| 151 | penY(dropShadow.penY) { |
| 152 | } |
| 153 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 154 | uint32_t width; |
| 155 | uint32_t height; |
| 156 | uint8_t* image; |
| 157 | int32_t penX; |
| 158 | int32_t penY; |
| 159 | }; |
| 160 | |
| 161 | // After renderDropShadow returns, the called owns the memory in DropShadow.image |
| 162 | // and is responsible for releasing it when it's done with it |
| 163 | DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 164 | uint32_t len, int numGlyphs, uint32_t radius); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 165 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 166 | GLuint getTexture(bool linearFiltering = false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 167 | checkInit(); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 168 | if (linearFiltering != mLinearFiltering) { |
| 169 | mLinearFiltering = linearFiltering; |
| 170 | const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST; |
| 171 | |
| 172 | glBindTexture(GL_TEXTURE_2D, mTextureId); |
| 173 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); |
| 174 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); |
| 175 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 176 | return mTextureId; |
| 177 | } |
| 178 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 179 | uint32_t getCacheWidth() const { |
| 180 | return mCacheWidth; |
| 181 | } |
| 182 | |
| 183 | uint32_t getCacheHeight() const { |
| 184 | return mCacheHeight; |
| 185 | } |
| 186 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 187 | protected: |
| 188 | friend class Font; |
| 189 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 190 | const uint8_t* mGammaTable; |
| 191 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 192 | struct CacheTextureLine { |
| 193 | uint16_t mMaxHeight; |
| 194 | uint16_t mMaxWidth; |
| 195 | uint32_t mCurrentRow; |
| 196 | uint32_t mCurrentCol; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 197 | bool mDirty; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 198 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 199 | CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 200 | uint32_t currentCol): |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 201 | mMaxHeight(maxHeight), |
| 202 | mMaxWidth(maxWidth), |
| 203 | mCurrentRow(currentRow), |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 204 | mCurrentCol(currentCol), |
| 205 | mDirty(false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 209 | if (glyph.fHeight + 2 > mMaxHeight) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
Romain Guy | 7975fb6 | 2010-10-01 16:36:14 -0700 | [diff] [blame] | 213 | if (mCurrentCol + glyph.fWidth + 2 < mMaxWidth) { |
| 214 | *retOriginX = mCurrentCol + 1; |
| 215 | *retOriginY = mCurrentRow + 1; |
| 216 | mCurrentCol += glyph.fWidth + 2; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 217 | mDirty = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return false; |
| 222 | } |
| 223 | }; |
| 224 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 225 | void initTextTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 226 | bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); |
| 227 | |
| 228 | void flushAllAndInvalidate(); |
| 229 | void initVertexArrayBuffers(); |
| 230 | |
| 231 | void checkInit(); |
| 232 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 233 | String16 mLatinPrecache; |
| 234 | void precacheLatin(SkPaint* paint); |
| 235 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 236 | void issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 237 | void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, |
| 238 | float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, |
| 239 | float x4, float y4, float z4, float u4, float v4); |
| 240 | |
| 241 | uint32_t mCacheWidth; |
| 242 | uint32_t mCacheHeight; |
| 243 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 244 | Vector<CacheTextureLine*> mCacheLines; |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 245 | uint32_t getRemainingCacheCapacity(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 246 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 247 | Font* mCurrentFont; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 248 | Vector<Font*> mActiveFonts; |
| 249 | |
| 250 | // Texture to cache glyph bitmaps |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 251 | uint8_t* mTextTexture; |
| 252 | const uint8_t* getTextTextureData() const { |
| 253 | return mTextTexture; |
| 254 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 255 | GLuint mTextureId; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 256 | void checkTextureUpdate(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 257 | bool mUploadTexture; |
| 258 | |
| 259 | // Pointer to vertex data to speed up frame to frame work |
| 260 | float *mTextMeshPtr; |
| 261 | uint32_t mCurrentQuadIndex; |
| 262 | uint32_t mMaxNumberOfQuads; |
| 263 | |
| 264 | uint32_t mIndexBufferID; |
| 265 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 266 | const Rect* mClip; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 267 | Rect* mBounds; |
| 268 | bool mDrawn; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 269 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 270 | bool mInitialized; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 271 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 272 | bool mLinearFiltering; |
| 273 | |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 274 | void computeGaussianWeights(float* weights, int32_t radius); |
| 275 | 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] | 276 | int32_t width, int32_t height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 277 | 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] | 278 | int32_t width, int32_t height); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 279 | 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] | 280 | }; |
| 281 | |
| 282 | }; // namespace uirenderer |
| 283 | }; // namespace android |
| 284 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 285 | #endif // ANDROID_HWUI_FONT_RENDERER_H |