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 | |
| 17 | #ifndef ANDROID_UI_FONT_RENDERER_H |
| 18 | #define ANDROID_UI_FONT_RENDERER_H |
| 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: |
| 44 | ~Font(); |
| 45 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Renders the specified string of text. |
| 48 | */ |
| 49 | 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^] | 50 | int numGlyphs, int x, int y, |
| 51 | uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Creates a new font associated with the specified font state. |
| 54 | */ |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 55 | static Font* create(FontRenderer* state, uint32_t fontId, float fontSize); |
| 56 | |
| 57 | protected: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 58 | friend class FontRenderer; |
| 59 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 60 | struct CachedGlyphInfo { |
| 61 | // Has the cache been invalidated? |
| 62 | bool mIsValid; |
| 63 | // Location of the cached glyph in the bitmap |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame^] | 64 | // in case we need to resize the texture or |
| 65 | // render to bitmap |
| 66 | uint32_t mStartX; |
| 67 | uint32_t mStartY; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 68 | uint32_t mBitmapWidth; |
| 69 | uint32_t mBitmapHeight; |
| 70 | // Also cache texture coords for the quad |
| 71 | float mBitmapMinU; |
| 72 | float mBitmapMinV; |
| 73 | float mBitmapMaxU; |
| 74 | float mBitmapMaxV; |
| 75 | // Minimize how much we call freetype |
| 76 | uint32_t mGlyphIndex; |
| 77 | uint32_t mAdvanceX; |
| 78 | uint32_t mAdvanceY; |
| 79 | // Values below contain a glyph's origin in the bitmap |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame^] | 80 | int32_t mBitmapLeft; |
| 81 | int32_t mBitmapTop; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 84 | Font(FontRenderer* state, uint32_t fontId, float fontSize); |
| 85 | |
| 86 | DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; |
| 87 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 88 | void invalidateTextureCache(); |
| 89 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 90 | CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 91 | void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); |
| 92 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame^] | 93 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y, |
| 94 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH); |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 95 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 96 | CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar); |
| 97 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 98 | FontRenderer* mState; |
| 99 | uint32_t mFontId; |
| 100 | float mFontSize; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class FontRenderer { |
| 104 | public: |
| 105 | FontRenderer(); |
| 106 | ~FontRenderer(); |
| 107 | |
| 108 | void init(); |
| 109 | void deinit(); |
| 110 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 111 | void setFont(SkPaint* paint, uint32_t fontId, float fontSize); |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 112 | void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 113 | uint32_t len, int numGlyphs, int x, int y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 114 | |
| 115 | GLuint getTexture() { |
| 116 | checkInit(); |
| 117 | return mTextureId; |
| 118 | } |
| 119 | |
| 120 | protected: |
| 121 | friend class Font; |
| 122 | |
| 123 | struct CacheTextureLine { |
| 124 | uint16_t mMaxHeight; |
| 125 | uint16_t mMaxWidth; |
| 126 | uint32_t mCurrentRow; |
| 127 | uint32_t mCurrentCol; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 128 | bool mDirty; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 129 | |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 130 | CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 131 | uint32_t currentCol): |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 132 | mMaxHeight(maxHeight), |
| 133 | mMaxWidth(maxWidth), |
| 134 | mCurrentRow(currentRow), |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 135 | mCurrentCol(currentCol), |
| 136 | mDirty(false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { |
| 140 | if (glyph.fHeight > mMaxHeight) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | if (mCurrentCol + glyph.fWidth < mMaxWidth) { |
| 145 | *retOriginX = mCurrentCol; |
| 146 | *retOriginY = mCurrentRow; |
| 147 | mCurrentCol += glyph.fWidth; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 148 | mDirty = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | uint32_t getCacheWidth() const { |
| 157 | return mCacheWidth; |
| 158 | } |
| 159 | |
| 160 | uint32_t getCacheHeight() const { |
| 161 | return mCacheHeight; |
| 162 | } |
| 163 | |
| 164 | void initTextTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 165 | bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); |
| 166 | |
| 167 | void flushAllAndInvalidate(); |
| 168 | void initVertexArrayBuffers(); |
| 169 | |
| 170 | void checkInit(); |
| 171 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 172 | String16 mLatinPrecache; |
| 173 | void precacheLatin(SkPaint* paint); |
| 174 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 175 | void issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 176 | void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, |
| 177 | float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, |
| 178 | float x4, float y4, float z4, float u4, float v4); |
| 179 | |
| 180 | uint32_t mCacheWidth; |
| 181 | uint32_t mCacheHeight; |
| 182 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 183 | Vector<CacheTextureLine*> mCacheLines; |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 184 | uint32_t getRemainingCacheCapacity(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 185 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 186 | Font* mCurrentFont; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 187 | Vector<Font*> mActiveFonts; |
| 188 | |
| 189 | // Texture to cache glyph bitmaps |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame^] | 190 | uint8_t* mTextTexture; |
| 191 | const uint8_t* getTextTextureData() const { |
| 192 | return mTextTexture; |
| 193 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 194 | GLuint mTextureId; |
Alex Sakhartchouk | 9b9902d | 2010-07-23 14:45:49 -0700 | [diff] [blame] | 195 | void checkTextureUpdate(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 196 | bool mUploadTexture; |
| 197 | |
| 198 | // Pointer to vertex data to speed up frame to frame work |
| 199 | float *mTextMeshPtr; |
| 200 | uint32_t mCurrentQuadIndex; |
| 201 | uint32_t mMaxNumberOfQuads; |
| 202 | |
| 203 | uint32_t mIndexBufferID; |
| 204 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 205 | const Rect* mClip; |
| 206 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 207 | bool mInitialized; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame^] | 208 | |
| 209 | void computeGaussianWeights(float* weights, int32_t radius); |
| 210 | void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
| 211 | int32_t width, int32_t height); |
| 212 | void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
| 213 | int32_t width, int32_t height); |
| 214 | 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] | 215 | }; |
| 216 | |
| 217 | }; // namespace uirenderer |
| 218 | }; // namespace android |
| 219 | |
| 220 | #endif // ANDROID_UI_FONT_RENDERER_H |