blob: 0f6815d218db29f02762ce74404e3aa650e34b62 [file] [log] [blame]
Alex Sakhartchouk9b949fc2010-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
26#include <ft2build.h>
27#include FT_FREETYPE_H
28
29// ---------------------------------------------------------------------------
30namespace android {
31
32namespace renderscript {
33
Alex Sakhartchouk3bf3ea02010-10-01 15:20:41 -070034// 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 Sakhartchouk9b949fc2010-06-24 17:15:34 -070043class FontState;
44
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080045class Font : public ObjectBase {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070046public:
Alex Sakhartchouk10825a02010-10-05 11:33:27 -070047 enum RenderMode {
48 FRAMEBUFFER,
49 BITMAP,
50 MEASURE,
51 };
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070052
Alex Sakhartchouk10825a02010-10-05 11:33:27 -070053 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 Sakhartchouk9b949fc2010-06-24 17:15:34 -070067
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
76 static Font * create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi);
77
78protected:
79
80 friend class FontState;
81
Alex Sakhartchouk10825a02010-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,
87 RenderMode mode = FRAMEBUFFER, Rect *bounds = NULL,
88 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
89
Alex Sakhartchouk9b949fc2010-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
107 FT_UInt mGlyphIndex;
108 FT_Vector mAdvance;
109 // Values below contain a glyph's origin in the bitmap
110 FT_Int mBitmapLeft;
111 FT_Int mBitmapTop;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700112 };
113
114 String8 mFontName;
115 uint32_t mFontSize;
116 uint32_t mDpi;
117
118 Font(Context *rsc);
119 bool init(const char *name, uint32_t fontSize, uint32_t dpi);
120
121 FT_Face mFace;
122 bool mInitialized;
123 bool mHasKerning;
124
125 DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs;
Alex Sakhartchouk94bbccc2010-08-17 11:09:49 -0700126 CachedGlyphInfo* getCachedUTFChar(int32_t utfChar);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700127
128 CachedGlyphInfo *cacheGlyph(uint32_t glyph);
129 void updateGlyphCache(CachedGlyphInfo *glyph);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700130 void measureCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y, Rect *bounds);
131 void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y);
132 void drawCachedGlyph(CachedGlyphInfo *glyph, int32_t x, int32_t y,
133 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700134};
135
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800136class FontState {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700137public:
138 FontState();
139 ~FontState();
140
141 void init(Context *rsc);
142 void deinit(Context *rsc);
143
144 ObjectBaseRef<Font> mDefault;
145 ObjectBaseRef<Font> mLast;
146
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700147 void renderText(const char *text, uint32_t len, int32_t x, int32_t y,
148 uint32_t startIndex = 0, int numGlyphs = -1,
149 Font::RenderMode mode = Font::FRAMEBUFFER,
150 Font::Rect *bounds = NULL,
151 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
152
153 void measureText(const char *text, uint32_t len, Font::Rect *bounds);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700154
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700155 void setFontColor(float r, float g, float b, float a);
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700156 void getFontColor(float *r, float *g, float *b, float *a) const;
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700157
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700158protected:
159
160 friend class Font;
161
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800162 struct CacheTextureLine {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700163 uint32_t mMaxHeight;
164 uint32_t mMaxWidth;
165 uint32_t mCurrentRow;
166 uint32_t mCurrentCol;
Alex Sakhartchouk94bbccc2010-08-17 11:09:49 -0700167 bool mDirty;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700168
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800169 CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol)
170 : mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow),
171 mCurrentCol(currentCol), mDirty(false) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700172 }
173
174 bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800175 if ((uint32_t)bitmap->rows > mMaxHeight) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700176 return false;
177 }
178
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800179 if (mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) {
180 *retOriginX = mCurrentCol;
181 *retOriginY = mCurrentRow;
182 mCurrentCol += bitmap->width;
183 mDirty = true;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700184 return true;
185 }
186
187 return false;
188 }
189 };
190
191 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk94bbccc2010-08-17 11:09:49 -0700192 uint32_t getRemainingCacheCapacity();
193
194 void precacheLatin(Font *font);
195 String8 mLatinPrecache;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700196
197 Context *mRSC;
198
Alex Sakhartchouk3bf3ea02010-10-01 15:20:41 -0700199 struct {
200 float mFontColor[4];
201 float mGamma;
202 } mConstants;
203 bool mConstantsDirty;
204
205 float mBlackGamma;
206 float mWhiteGamma;
207
208 float mBlackThreshold;
209 float mWhiteThreshold;
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700210
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700211 // Free type library, we only need one copy
212 FT_Library mLibrary;
Alex Sakhartchouk071508d2010-06-30 12:49:27 -0700213 FT_Library getLib();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700214 Vector<Font*> mActiveFonts;
215
216 // Render state for the font
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700217 ObjectBaseRef<Allocation> mFontShaderFConstant;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700218 ObjectBaseRef<ProgramFragment> mFontShaderF;
219 ObjectBaseRef<Sampler> mFontSampler;
220 ObjectBaseRef<ProgramStore> mFontProgramStore;
221 void initRenderState();
222
223 // Texture to cache glyph bitmaps
224 ObjectBaseRef<Allocation> mTextTexture;
225 void initTextTexture();
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700226 const uint8_t* getTextTextureData() const {
227 return (uint8_t*)mTextTexture->getPtr();
228 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700229
230 bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
231 const Type* getCacheTextureType() {
232 return mTextTexture->getType();
233 }
234
235 void flushAllAndInvalidate();
236
237 // Pointer to vertex data to speed up frame to frame work
238 float *mTextMeshPtr;
239 uint32_t mCurrentQuadIndex;
240 uint32_t mMaxNumberOfQuads;
241
242 void initVertexArrayBuffers();
243 ObjectBaseRef<Allocation> mIndexBuffer;
244 ObjectBaseRef<Allocation> mVertexArray;
245
246
247 bool mInitialized;
248
249 void checkInit();
250
251 void issueDrawCommand();
252
253 void appendMeshQuad(float x1, float y1, float z1,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800254 float u1, float v1,
255 float x2, float y2, float z2,
256 float u2, float v2,
257 float x3, float y3, float z3,
258 float u3, float v3,
259 float x4, float y4, float z4,
260 float u4, float v4);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700261};
262
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700263}
264}
265
266#endif