Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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_RS_FONT_H |
| 18 | #define ANDROID_RS_FONT_H |
| 19 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 20 | #include "rsStream.h" |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 21 | #include <utils/Vector.h> |
| 22 | #include <utils/KeyedVector.h> |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 24 | struct FT_LibraryRec_; |
| 25 | struct FT_FaceRec_; |
| 26 | struct FT_Bitmap_; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 27 | |
| 28 | // --------------------------------------------------------------------------- |
| 29 | namespace android { |
| 30 | |
| 31 | namespace renderscript { |
| 32 | |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 33 | // Gamma (>= 1.0, <= 10.0) |
| 34 | #define PROPERTY_TEXT_GAMMA "ro.text_gamma" |
| 35 | #define PROPERTY_TEXT_BLACK_GAMMA_THRESHOLD "ro.text_gamma.black_threshold" |
| 36 | #define PROPERTY_TEXT_WHITE_GAMMA_THRESHOLD "ro.text_gamma.white_threshold" |
| 37 | |
| 38 | #define DEFAULT_TEXT_GAMMA 1.4f |
| 39 | #define DEFAULT_TEXT_BLACK_GAMMA_THRESHOLD 64 |
| 40 | #define DEFAULT_TEXT_WHITE_GAMMA_THRESHOLD 192 |
| 41 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 42 | class FontState; |
| 43 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | class Font : public ObjectBase { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 45 | public: |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 46 | enum RenderMode { |
| 47 | FRAMEBUFFER, |
| 48 | BITMAP, |
| 49 | MEASURE, |
| 50 | }; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 51 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 52 | struct Rect { |
| 53 | int32_t left; |
| 54 | int32_t top; |
| 55 | int32_t right; |
| 56 | int32_t bottom; |
| 57 | void set(int32_t l, int32_t r, int32_t t, int32_t b) { |
| 58 | left = l; |
| 59 | right = r; |
| 60 | top = t; |
| 61 | bottom = b; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | ~Font(); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 66 | |
| 67 | // Currently files do not get serialized, |
| 68 | // but we need to inherit from ObjectBase for ref tracking |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 69 | virtual void serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 70 | } |
| 71 | virtual RsA3DClassID getClassId() const { |
| 72 | return RS_A3D_CLASS_ID_UNKNOWN; |
| 73 | } |
| 74 | |
Alex Sakhartchouk | 5224a27 | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 75 | static Font * create(Context *rsc, const char *name, float fontSize, uint32_t dpi, |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 76 | const void *data = nullptr, uint32_t dataLen = 0); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 77 | |
| 78 | protected: |
| 79 | |
| 80 | friend class FontState; |
| 81 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 82 | // Pointer to the utf data, length of data, where to start, number of glyphs ot read |
| 83 | // (each glyph may be longer than a char because we are dealing with utf data) |
| 84 | // Last two variables are the initial pen position |
| 85 | void renderUTF(const char *text, uint32_t len, int32_t x, int32_t y, |
| 86 | uint32_t start, int32_t numGlyphs, |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 87 | RenderMode mode = FRAMEBUFFER, Rect *bounds = nullptr, |
| 88 | uint8_t *bitmap = nullptr, uint32_t bitmapW = 0, uint32_t bitmapH = 0); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 89 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 90 | void invalidateTextureCache(); |
| 91 | struct CachedGlyphInfo |
| 92 | { |
| 93 | // Has the cache been invalidated? |
| 94 | bool mIsValid; |
| 95 | // Location of the cached glyph in the bitmap |
| 96 | // in case we need to resize the texture |
| 97 | uint32_t mBitmapMinX; |
| 98 | uint32_t mBitmapMinY; |
| 99 | uint32_t mBitmapWidth; |
| 100 | uint32_t mBitmapHeight; |
| 101 | // Also cache texture coords for the quad |
| 102 | float mBitmapMinU; |
| 103 | float mBitmapMinV; |
| 104 | float mBitmapMaxU; |
| 105 | float mBitmapMaxV; |
| 106 | // Minimize how much we call freetype |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 107 | int32_t mGlyphIndex; |
| 108 | int32_t mAdvanceX; |
| 109 | int32_t mAdvanceY; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 110 | // Values below contain a glyph's origin in the bitmap |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 111 | int32_t mBitmapLeft; |
| 112 | int32_t mBitmapTop; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 115 | const char *mFontName; |
Alex Sakhartchouk | c17ace2 | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 116 | float mFontSize; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 117 | uint32_t mDpi; |
| 118 | |
| 119 | Font(Context *rsc); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 120 | bool init(const char *name, float fontSize, uint32_t dpi, const void *data = nullptr, uint32_t dataLen = 0); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 121 | |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 122 | virtual void preDestroy() const; |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 123 | FT_FaceRec_ *mFace; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 124 | bool mInitialized; |
| 125 | bool mHasKerning; |
| 126 | |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 127 | DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs; |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 128 | CachedGlyphInfo* getCachedUTFChar(int32_t utfChar); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 129 | |
| 130 | CachedGlyphInfo *cacheGlyph(uint32_t glyph); |
| 131 | void updateGlyphCache(CachedGlyphInfo *glyph); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 132 | void measureCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, Rect *bounds); |
| 133 | void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y); |
| 134 | void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, |
| 135 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | class FontState { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 139 | public: |
| 140 | FontState(); |
| 141 | ~FontState(); |
| 142 | |
| 143 | void init(Context *rsc); |
| 144 | void deinit(Context *rsc); |
| 145 | |
| 146 | ObjectBaseRef<Font> mDefault; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 147 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 148 | void renderText(const char *text, uint32_t len, int32_t x, int32_t y, |
| 149 | uint32_t startIndex = 0, int numGlyphs = -1, |
| 150 | Font::RenderMode mode = Font::FRAMEBUFFER, |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 151 | Font::Rect *bounds = nullptr, |
| 152 | uint8_t *bitmap = nullptr, uint32_t bitmapW = 0, uint32_t bitmapH = 0); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 153 | |
| 154 | void measureText(const char *text, uint32_t len, Font::Rect *bounds); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 155 | |
Alex Sakhartchouk | 9fc9f03 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 156 | void setFontColor(float r, float g, float b, float a); |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 157 | void getFontColor(float *r, float *g, float *b, float *a) const; |
Alex Sakhartchouk | 9fc9f03 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 158 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 159 | protected: |
| 160 | |
Alex Sakhartchouk | a74a8f6 | 2011-11-16 12:22:10 -0800 | [diff] [blame] | 161 | float mSurfaceWidth; |
| 162 | float mSurfaceHeight; |
| 163 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 164 | friend class Font; |
| 165 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 166 | struct CacheTextureLine { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 167 | uint32_t mMaxHeight; |
| 168 | uint32_t mMaxWidth; |
| 169 | uint32_t mCurrentRow; |
| 170 | uint32_t mCurrentCol; |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 171 | bool mDirty; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 172 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 173 | CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol) |
| 174 | : mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), |
| 175 | mCurrentCol(currentCol), mDirty(false) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 178 | bool fitBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 181 | Vector<CacheTextureLine*> mCacheLines; |
Alex Sakhartchouk | 01bcef6 | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 182 | uint32_t getRemainingCacheCapacity(); |
| 183 | |
| 184 | void precacheLatin(Font *font); |
Jason Sams | a7f5e04 | 2013-07-08 16:46:18 -0700 | [diff] [blame] | 185 | const char *mLatinPrecache; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 186 | |
| 187 | Context *mRSC; |
| 188 | |
Alex Sakhartchouk | c9fa305 | 2010-10-01 15:20:41 -0700 | [diff] [blame] | 189 | struct { |
| 190 | float mFontColor[4]; |
| 191 | float mGamma; |
| 192 | } mConstants; |
| 193 | bool mConstantsDirty; |
| 194 | |
| 195 | float mBlackGamma; |
| 196 | float mWhiteGamma; |
| 197 | |
| 198 | float mBlackThreshold; |
| 199 | float mWhiteThreshold; |
Alex Sakhartchouk | 9fc9f03 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 200 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 201 | // Free type library, we only need one copy |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 202 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 203 | FT_LibraryRec_ *mLibrary; |
| 204 | FT_LibraryRec_ *getLib(); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 205 | #endif //ANDROID_RS_SERIALIZE |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 206 | Vector<Font*> mActiveFonts; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 207 | |
| 208 | // Render state for the font |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 209 | ObjectBaseRef<Allocation> mFontShaderFConstant; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 210 | ObjectBaseRef<ProgramFragment> mFontShaderF; |
| 211 | ObjectBaseRef<Sampler> mFontSampler; |
| 212 | ObjectBaseRef<ProgramStore> mFontProgramStore; |
| 213 | void initRenderState(); |
| 214 | |
| 215 | // Texture to cache glyph bitmaps |
| 216 | ObjectBaseRef<Allocation> mTextTexture; |
Jason Sams | a6dd823 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 217 | uint8_t *mCacheBuffer; |
| 218 | uint32_t mCacheWidth; |
| 219 | uint32_t mCacheHeight; |
| 220 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 221 | void initTextTexture(); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 222 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 223 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 02000b3 | 2011-02-25 09:34:33 -0800 | [diff] [blame] | 224 | bool cacheBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 225 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 226 | const Type* getCacheTextureType() { |
| 227 | return mTextTexture->getType(); |
| 228 | } |
| 229 | |
| 230 | void flushAllAndInvalidate(); |
| 231 | |
| 232 | // Pointer to vertex data to speed up frame to frame work |
| 233 | float *mTextMeshPtr; |
| 234 | uint32_t mCurrentQuadIndex; |
| 235 | uint32_t mMaxNumberOfQuads; |
| 236 | |
| 237 | void initVertexArrayBuffers(); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 238 | ObjectBaseRef<Mesh> mMesh; |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 239 | |
| 240 | bool mInitialized; |
| 241 | |
| 242 | void checkInit(); |
| 243 | |
| 244 | void issueDrawCommand(); |
| 245 | |
| 246 | void appendMeshQuad(float x1, float y1, float z1, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 247 | float u1, float v1, |
| 248 | float x2, float y2, float z2, |
| 249 | float u2, float v2, |
| 250 | float x3, float y3, float z3, |
| 251 | float u3, float v3, |
| 252 | float x4, float y4, float z4, |
| 253 | float u4, float v4); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | #endif |