blob: b0e1430e228b8ab0d849a0320cc3f8fe82467f3d [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
20#include "RenderScript.h"
21#include "rsStream.h"
22#include <utils/String8.h>
23#include <utils/Vector.h>
24#include <utils/KeyedVector.h>
25
Alex Sakhartchouk02000b32011-02-25 09:34:33 -080026struct FT_LibraryRec_;
27struct FT_FaceRec_;
28struct FT_Bitmap_;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070029
30// ---------------------------------------------------------------------------
31namespace android {
32
33namespace renderscript {
34
Alex Sakhartchoukc9fa3052010-10-01 15:20:41 -070035// 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070044class FontState;
45
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080046class Font : public ObjectBase {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070047public:
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070048 enum RenderMode {
49 FRAMEBUFFER,
50 BITMAP,
51 MEASURE,
52 };
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070053
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070054 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070068
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 Sakhartchouk5224a272011-01-07 11:12:08 -080077 static Font * create(Context *rsc, const char *name, float fontSize, uint32_t dpi,
78 const void *data = NULL, uint32_t dataLen = 0);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070079
80protected:
81
82 friend class FontState;
83
Alex Sakhartchouk09c67352010-10-05 11:33:27 -070084 // 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -070092 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 Sakhartchouk02000b32011-02-25 09:34:33 -0800109 int32_t mGlyphIndex;
110 int32_t mAdvanceX;
111 int32_t mAdvanceY;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700112 // Values below contain a glyph's origin in the bitmap
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800113 int32_t mBitmapLeft;
114 int32_t mBitmapTop;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700115 };
116
117 String8 mFontName;
Alex Sakhartchoukc17ace22010-12-17 11:41:08 -0800118 float mFontSize;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700119 uint32_t mDpi;
120
121 Font(Context *rsc);
Alex Sakhartchouk5224a272011-01-07 11:12:08 -0800122 bool init(const char *name, float fontSize, uint32_t dpi, const void *data = NULL, uint32_t dataLen = 0);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700123
Jason Sams2e8665d2011-01-27 00:14:13 -0800124 virtual void preDestroy() const;
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800125 FT_FaceRec_ *mFace;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700126 bool mInitialized;
127 bool mHasKerning;
128
129 DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700130 CachedGlyphInfo* getCachedUTFChar(int32_t utfChar);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700131
132 CachedGlyphInfo *cacheGlyph(uint32_t glyph);
133 void updateGlyphCache(CachedGlyphInfo *glyph);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700134 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 Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700138};
139
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800140class FontState {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700141public:
142 FontState();
143 ~FontState();
144
145 void init(Context *rsc);
146 void deinit(Context *rsc);
147
148 ObjectBaseRef<Font> mDefault;
149 ObjectBaseRef<Font> mLast;
150
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700151 void renderText(const char *text, uint32_t len, int32_t x, int32_t y,
152 uint32_t startIndex = 0, int numGlyphs = -1,
153 Font::RenderMode mode = Font::FRAMEBUFFER,
154 Font::Rect *bounds = NULL,
155 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
156
157 void measureText(const char *text, uint32_t len, Font::Rect *bounds);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700158
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700159 void setFontColor(float r, float g, float b, float a);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700160 void getFontColor(float *r, float *g, float *b, float *a) const;
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700161
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700162protected:
163
164 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
181 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk01bcef62010-08-17 11:09:49 -0700182 uint32_t getRemainingCacheCapacity();
183
184 void precacheLatin(Font *font);
185 String8 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
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700206 Vector<Font*> mActiveFonts;
207
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;
217 void initTextTexture();
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700218 const uint8_t* getTextTextureData() const {
219 return (uint8_t*)mTextTexture->getPtr();
220 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700221
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700222#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk02000b32011-02-25 09:34:33 -0800223 bool cacheBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700224#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700225 const Type* getCacheTextureType() {
226 return mTextTexture->getType();
227 }
228
229 void flushAllAndInvalidate();
230
231 // Pointer to vertex data to speed up frame to frame work
232 float *mTextMeshPtr;
233 uint32_t mCurrentQuadIndex;
234 uint32_t mMaxNumberOfQuads;
235
236 void initVertexArrayBuffers();
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700237 ObjectBaseRef<Mesh> mMesh;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700238
239 bool mInitialized;
240
241 void checkInit();
242
243 void issueDrawCommand();
244
245 void appendMeshQuad(float x1, float y1, float z1,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800246 float u1, float v1,
247 float x2, float y2, float z2,
248 float u2, float v2,
249 float x3, float y3, float z3,
250 float u3, float v3,
251 float x4, float y4, float z4,
252 float u4, float v4);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700253};
254
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700255}
256}
257
258#endif