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