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 | |
| 20 | #include "RenderScript.h" |
| 21 | #include "rsStream.h" |
| 22 | #include <utils/String8.h> |
| 23 | #include <utils/Vector.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | |
| 26 | #include <ft2build.h> |
| 27 | #include FT_FREETYPE_H |
| 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 | |
| 45 | class Font : public ObjectBase |
| 46 | { |
| 47 | public: |
| 48 | ~Font(); |
| 49 | |
| 50 | // Pointer to the utf data, length of data, where to start, number of glyphs ot read |
| 51 | // (each glyph may be longer than a char because we are dealing with utf data) |
| 52 | // Last two variables are the initial pen position |
| 53 | void renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyphs, int x, int y); |
| 54 | |
| 55 | // Currently files do not get serialized, |
| 56 | // but we need to inherit from ObjectBase for ref tracking |
| 57 | virtual void serialize(OStream *stream) const { |
| 58 | } |
| 59 | virtual RsA3DClassID getClassId() const { |
| 60 | return RS_A3D_CLASS_ID_UNKNOWN; |
| 61 | } |
| 62 | |
| 63 | static Font * create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi); |
| 64 | |
| 65 | protected: |
| 66 | |
| 67 | friend class FontState; |
| 68 | |
| 69 | void invalidateTextureCache(); |
| 70 | struct CachedGlyphInfo |
| 71 | { |
| 72 | // Has the cache been invalidated? |
| 73 | bool mIsValid; |
| 74 | // Location of the cached glyph in the bitmap |
| 75 | // in case we need to resize the texture |
| 76 | uint32_t mBitmapMinX; |
| 77 | uint32_t mBitmapMinY; |
| 78 | uint32_t mBitmapWidth; |
| 79 | uint32_t mBitmapHeight; |
| 80 | // Also cache texture coords for the quad |
| 81 | float mBitmapMinU; |
| 82 | float mBitmapMinV; |
| 83 | float mBitmapMaxU; |
| 84 | float mBitmapMaxV; |
| 85 | // Minimize how much we call freetype |
| 86 | FT_UInt mGlyphIndex; |
| 87 | FT_Vector mAdvance; |
| 88 | // Values below contain a glyph's origin in the bitmap |
| 89 | FT_Int mBitmapLeft; |
| 90 | FT_Int mBitmapTop; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | String8 mFontName; |
| 94 | uint32_t mFontSize; |
| 95 | uint32_t mDpi; |
| 96 | |
| 97 | Font(Context *rsc); |
| 98 | bool init(const char *name, uint32_t fontSize, uint32_t dpi); |
| 99 | |
| 100 | FT_Face mFace; |
| 101 | bool mInitialized; |
| 102 | bool mHasKerning; |
| 103 | |
| 104 | DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs; |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 105 | CachedGlyphInfo* getCachedUTFChar(int32_t utfChar); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 106 | |
| 107 | CachedGlyphInfo *cacheGlyph(uint32_t glyph); |
| 108 | void updateGlyphCache(CachedGlyphInfo *glyph); |
| 109 | void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); |
| 110 | }; |
| 111 | |
| 112 | class FontState |
| 113 | { |
| 114 | public: |
| 115 | FontState(); |
| 116 | ~FontState(); |
| 117 | |
| 118 | void init(Context *rsc); |
| 119 | void deinit(Context *rsc); |
| 120 | |
| 121 | ObjectBaseRef<Font> mDefault; |
| 122 | ObjectBaseRef<Font> mLast; |
| 123 | |
| 124 | void renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y); |
| 125 | void renderText(const char *text, int x, int y); |
| 126 | void renderText(Allocation *alloc, int x, int y); |
| 127 | void renderText(Allocation *alloc, uint32_t start, int len, int x, int y); |
| 128 | |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 129 | void setFontColor(float r, float g, float b, float a); |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 130 | void getFontColor(float *r, float *g, float *b, float *a) const; |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 131 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 132 | protected: |
| 133 | |
| 134 | friend class Font; |
| 135 | |
| 136 | struct CacheTextureLine |
| 137 | { |
| 138 | uint32_t mMaxHeight; |
| 139 | uint32_t mMaxWidth; |
| 140 | uint32_t mCurrentRow; |
| 141 | uint32_t mCurrentCol; |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 142 | bool mDirty; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 143 | |
| 144 | CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol) : |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 145 | mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), mCurrentCol(currentCol), |
| 146 | mDirty(false) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { |
| 150 | if((uint32_t)bitmap->rows > mMaxHeight) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | if(mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) { |
| 155 | *retOriginX = mCurrentCol; |
| 156 | *retOriginY = mCurrentRow; |
| 157 | mCurrentCol += bitmap->width; |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 158 | mDirty = true; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | Vector<CacheTextureLine*> mCacheLines; |
Alex Sakhartchouk | 94bbccc | 2010-08-17 11:09:49 -0700 | [diff] [blame] | 167 | uint32_t getRemainingCacheCapacity(); |
| 168 | |
| 169 | void precacheLatin(Font *font); |
| 170 | String8 mLatinPrecache; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 171 | |
| 172 | Context *mRSC; |
| 173 | |
Alex Sakhartchouk | 3bf3ea0 | 2010-10-01 15:20:41 -0700 | [diff] [blame^] | 174 | struct { |
| 175 | float mFontColor[4]; |
| 176 | float mGamma; |
| 177 | } mConstants; |
| 178 | bool mConstantsDirty; |
| 179 | |
| 180 | float mBlackGamma; |
| 181 | float mWhiteGamma; |
| 182 | |
| 183 | float mBlackThreshold; |
| 184 | float mWhiteThreshold; |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 185 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 186 | // Free type library, we only need one copy |
| 187 | FT_Library mLibrary; |
Alex Sakhartchouk | 071508d | 2010-06-30 12:49:27 -0700 | [diff] [blame] | 188 | FT_Library getLib(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 189 | Vector<Font*> mActiveFonts; |
| 190 | |
| 191 | // Render state for the font |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 192 | ObjectBaseRef<Allocation> mFontShaderFConstant; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 193 | ObjectBaseRef<ProgramFragment> mFontShaderF; |
| 194 | ObjectBaseRef<Sampler> mFontSampler; |
| 195 | ObjectBaseRef<ProgramStore> mFontProgramStore; |
| 196 | void initRenderState(); |
| 197 | |
| 198 | // Texture to cache glyph bitmaps |
| 199 | ObjectBaseRef<Allocation> mTextTexture; |
| 200 | void initTextTexture(); |
| 201 | |
| 202 | bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY); |
| 203 | const Type* getCacheTextureType() { |
| 204 | return mTextTexture->getType(); |
| 205 | } |
| 206 | |
| 207 | void flushAllAndInvalidate(); |
| 208 | |
| 209 | // Pointer to vertex data to speed up frame to frame work |
| 210 | float *mTextMeshPtr; |
| 211 | uint32_t mCurrentQuadIndex; |
| 212 | uint32_t mMaxNumberOfQuads; |
| 213 | |
| 214 | void initVertexArrayBuffers(); |
| 215 | ObjectBaseRef<Allocation> mIndexBuffer; |
| 216 | ObjectBaseRef<Allocation> mVertexArray; |
| 217 | |
| 218 | |
| 219 | bool mInitialized; |
| 220 | |
| 221 | void checkInit(); |
| 222 | |
| 223 | void issueDrawCommand(); |
| 224 | |
| 225 | void appendMeshQuad(float x1, float y1, float z1, |
| 226 | float u1, float v1, |
| 227 | float x2, float y2, float z2, |
| 228 | float u2, float v2, |
| 229 | float x3, float y3, float z3, |
| 230 | float u3, float v3, |
| 231 | float x4, float y4, float z4, |
| 232 | float u4, float v4); |
| 233 | |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | #endif |