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> |
| 21 | #include <utils/Vector.h> |
| 22 | #include <utils/KeyedVector.h> |
| 23 | |
| 24 | #include <SkScalerContext.h> |
| 25 | #include <SkPaint.h> |
| 26 | |
| 27 | #include <GLES2/gl2.h> |
| 28 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 29 | #include "Rect.h" |
| 30 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | class FontRenderer; |
| 35 | |
| 36 | class Font { |
| 37 | public: |
| 38 | ~Font(); |
| 39 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 40 | void renderUTF(SkPaint* paint, const char *text, uint32_t len, uint32_t start, |
| 41 | int numGlyphs, int x, int y); |
| 42 | |
| 43 | static Font* create(FontRenderer* state, uint32_t fontId, float fontSize); |
| 44 | |
| 45 | protected: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 46 | friend class FontRenderer; |
| 47 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 48 | struct CachedGlyphInfo { |
| 49 | // Has the cache been invalidated? |
| 50 | bool mIsValid; |
| 51 | // Location of the cached glyph in the bitmap |
| 52 | // in case we need to resize the texture |
| 53 | uint32_t mBitmapWidth; |
| 54 | uint32_t mBitmapHeight; |
| 55 | // Also cache texture coords for the quad |
| 56 | float mBitmapMinU; |
| 57 | float mBitmapMinV; |
| 58 | float mBitmapMaxU; |
| 59 | float mBitmapMaxV; |
| 60 | // Minimize how much we call freetype |
| 61 | uint32_t mGlyphIndex; |
| 62 | uint32_t mAdvanceX; |
| 63 | uint32_t mAdvanceY; |
| 64 | // Values below contain a glyph's origin in the bitmap |
| 65 | uint32_t mBitmapLeft; |
| 66 | uint32_t mBitmapTop; |
| 67 | }; |
| 68 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 69 | Font(FontRenderer* state, uint32_t fontId, float fontSize); |
| 70 | |
| 71 | DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; |
| 72 | |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame^] | 73 | void invalidateTextureCache(); |
| 74 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 75 | CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph); |
| 76 | void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); |
| 77 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame^] | 78 | |
| 79 | FontRenderer* mState; |
| 80 | uint32_t mFontId; |
| 81 | float mFontSize; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | class FontRenderer { |
| 85 | public: |
| 86 | FontRenderer(); |
| 87 | ~FontRenderer(); |
| 88 | |
| 89 | void init(); |
| 90 | void deinit(); |
| 91 | |
| 92 | void setFont(uint32_t fontId, float fontSize); |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 93 | void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t len, |
| 94 | uint32_t startIndex, int numGlyphs, int x, int y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 95 | |
| 96 | GLuint getTexture() { |
| 97 | checkInit(); |
| 98 | return mTextureId; |
| 99 | } |
| 100 | |
| 101 | protected: |
| 102 | friend class Font; |
| 103 | |
| 104 | struct CacheTextureLine { |
| 105 | uint16_t mMaxHeight; |
| 106 | uint16_t mMaxWidth; |
| 107 | uint32_t mCurrentRow; |
| 108 | uint32_t mCurrentCol; |
| 109 | |
| 110 | CacheTextureLine(uint16_t maxHeight, uint16_t maxWidth, uint32_t currentRow, |
| 111 | uint32_t currentCol): |
| 112 | mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), |
| 113 | mCurrentCol(currentCol) { |
| 114 | } |
| 115 | |
| 116 | bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { |
| 117 | if (glyph.fHeight > mMaxHeight) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (mCurrentCol + glyph.fWidth < mMaxWidth) { |
| 122 | *retOriginX = mCurrentCol; |
| 123 | *retOriginY = mCurrentRow; |
| 124 | mCurrentCol += glyph.fWidth; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | uint32_t getCacheWidth() const { |
| 133 | return mCacheWidth; |
| 134 | } |
| 135 | |
| 136 | uint32_t getCacheHeight() const { |
| 137 | return mCacheHeight; |
| 138 | } |
| 139 | |
| 140 | void initTextTexture(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 141 | bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); |
| 142 | |
| 143 | void flushAllAndInvalidate(); |
| 144 | void initVertexArrayBuffers(); |
| 145 | |
| 146 | void checkInit(); |
| 147 | |
| 148 | void issueDrawCommand(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 149 | void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, |
| 150 | float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, |
| 151 | float x4, float y4, float z4, float u4, float v4); |
| 152 | |
| 153 | uint32_t mCacheWidth; |
| 154 | uint32_t mCacheHeight; |
| 155 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 156 | Vector<CacheTextureLine*> mCacheLines; |
| 157 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 158 | Font* mCurrentFont; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 159 | Vector<Font*> mActiveFonts; |
| 160 | |
| 161 | // Texture to cache glyph bitmaps |
| 162 | unsigned char* mTextTexture; |
| 163 | GLuint mTextureId; |
| 164 | bool mUploadTexture; |
| 165 | |
| 166 | // Pointer to vertex data to speed up frame to frame work |
| 167 | float *mTextMeshPtr; |
| 168 | uint32_t mCurrentQuadIndex; |
| 169 | uint32_t mMaxNumberOfQuads; |
| 170 | |
| 171 | uint32_t mIndexBufferID; |
| 172 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 173 | const Rect* mClip; |
| 174 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 175 | bool mInitialized; |
| 176 | }; |
| 177 | |
| 178 | }; // namespace uirenderer |
| 179 | }; // namespace android |
| 180 | |
| 181 | #endif // ANDROID_UI_FONT_RENDERER_H |