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