blob: 0f17340861fa5124efd2a0695165f902ae4b4f5e [file] [log] [blame]
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001/*
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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070020#include "rsStream.h"
Yang Nib8353c52015-02-14 18:00:59 -080021#include <utils/Vector.h>
22#include <utils/KeyedVector.h>
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070023
Alex Sakhartchouk02000b32011-02-25 09:34:33 -080024struct FT_LibraryRec_;
25struct FT_FaceRec_;
26struct FT_Bitmap_;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070027
28// ---------------------------------------------------------------------------
29namespace android {
30
31namespace renderscript {
32
Alex Sakhartchoukc9fa3052010-10-01 15:20:41 -070033// 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070042class FontState;
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044class Font : public ObjectBase {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070045public:
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070046 enum RenderMode {
47 FRAMEBUFFER,
48 BITMAP,
49 MEASURE,
50 };
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070051
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070052 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070066
67 // Currently files do not get serialized,
68 // but we need to inherit from ObjectBase for ref tracking
Jason Samse3150cf2012-07-24 18:10:20 -070069 virtual void serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070070 }
71 virtual RsA3DClassID getClassId() const {
72 return RS_A3D_CLASS_ID_UNKNOWN;
73 }
74
Alex Sakhartchouk5224a272011-01-07 11:12:08 -080075 static Font * create(Context *rsc, const char *name, float fontSize, uint32_t dpi,
Chris Wailes44bef6f2014-08-12 13:51:10 -070076 const void *data = nullptr, uint32_t dataLen = 0);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070077
78protected:
79
80 friend class FontState;
81
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070082 // 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 Wailes44bef6f2014-08-12 13:51:10 -070087 RenderMode mode = FRAMEBUFFER, Rect *bounds = nullptr,
88 uint8_t *bitmap = nullptr, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070089
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070090 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 Sakhartchouk02000b32011-02-25 09:34:33 -0800107 int32_t mGlyphIndex;
108 int32_t mAdvanceX;
109 int32_t mAdvanceY;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700110 // Values below contain a glyph's origin in the bitmap
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800111 int32_t mBitmapLeft;
112 int32_t mBitmapTop;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700113 };
114
Jason Sams48ecf6a2013-07-09 15:35:29 -0700115 const char *mFontName;
Alex Sakhartchoukc17ace22010-12-17 11:41:08 -0800116 float mFontSize;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700117 uint32_t mDpi;
118
119 Font(Context *rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700120 bool init(const char *name, float fontSize, uint32_t dpi, const void *data = nullptr, uint32_t dataLen = 0);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700121
Jason Sams2e8665d2011-01-27 00:14:13 -0800122 virtual void preDestroy() const;
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800123 FT_FaceRec_ *mFace;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700124 bool mInitialized;
125 bool mHasKerning;
126
Yang Nib8353c52015-02-14 18:00:59 -0800127 DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700128 CachedGlyphInfo* getCachedUTFChar(int32_t utfChar);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700129
130 CachedGlyphInfo *cacheGlyph(uint32_t glyph);
131 void updateGlyphCache(CachedGlyphInfo *glyph);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700132 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700136};
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138class FontState {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700139public:
140 FontState();
141 ~FontState();
142
143 void init(Context *rsc);
144 void deinit(Context *rsc);
145
146 ObjectBaseRef<Font> mDefault;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700147
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700148 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 Wailes44bef6f2014-08-12 13:51:10 -0700151 Font::Rect *bounds = nullptr,
152 uint8_t *bitmap = nullptr, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700153
154 void measureText(const char *text, uint32_t len, Font::Rect *bounds);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700155
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700156 void setFontColor(float r, float g, float b, float a);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700157 void getFontColor(float *r, float *g, float *b, float *a) const;
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700158
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700159protected:
160
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800161 float mSurfaceWidth;
162 float mSurfaceHeight;
163
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700164 friend class Font;
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166 struct CacheTextureLine {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700167 uint32_t mMaxHeight;
168 uint32_t mMaxWidth;
169 uint32_t mCurrentRow;
170 uint32_t mCurrentCol;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700171 bool mDirty;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700172
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800173 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700176 }
177
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800178 bool fitBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700179 };
180
Yang Nib8353c52015-02-14 18:00:59 -0800181 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700182 uint32_t getRemainingCacheCapacity();
183
184 void precacheLatin(Font *font);
Jason Samsa7f5e042013-07-08 16:46:18 -0700185 const char *mLatinPrecache;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700186
187 Context *mRSC;
188
Alex Sakhartchoukc9fa3052010-10-01 15:20:41 -0700189 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 Sakhartchouk9fc9f032010-08-04 14:45:48 -0700200
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700201 // Free type library, we only need one copy
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700202#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800203 FT_LibraryRec_ *mLibrary;
204 FT_LibraryRec_ *getLib();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700205#endif //ANDROID_RS_SERIALIZE
Yang Nib8353c52015-02-14 18:00:59 -0800206 Vector<Font*> mActiveFonts;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700207
208 // Render state for the font
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700209 ObjectBaseRef<Allocation> mFontShaderFConstant;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700210 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 Samsa6dd8232012-07-25 19:33:43 -0700217 uint8_t *mCacheBuffer;
218 uint32_t mCacheWidth;
219 uint32_t mCacheHeight;
220
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700221 void initTextTexture();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700222
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700223#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800224 bool cacheBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700225#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700226 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 Sakhartchouka04e30d2011-04-29 16:49:08 -0700238 ObjectBaseRef<Mesh> mMesh;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700239
240 bool mInitialized;
241
242 void checkInit();
243
244 void issueDrawCommand();
245
246 void appendMeshQuad(float x1, float y1, float z1,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800247 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700254};
255
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700256}
257}
258
259#endif