blob: 46f332e54ee10bfee3112b1d1319d9bdd9671d5a [file] [log] [blame]
Romain Guy694b5192010-07-21 21:33:20 -07001/*
2 * Copyright (C) 2010 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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_FONT_RENDERER_H
18#define ANDROID_HWUI_FONT_RENDERER_H
Romain Guy694b5192010-07-21 21:33:20 -070019
20#include <utils/String8.h>
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -070021#include <utils/String16.h>
Romain Guy694b5192010-07-21 21:33:20 -070022#include <utils/Vector.h>
23#include <utils/KeyedVector.h>
24
25#include <SkScalerContext.h>
26#include <SkPaint.h>
27
28#include <GLES2/gl2.h>
29
Romain Guy09147fb2010-07-22 13:08:20 -070030#include "Rect.h"
Romain Guy51769a62010-07-23 00:28:00 -070031#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070032
Romain Guy694b5192010-07-21 21:33:20 -070033namespace android {
34namespace uirenderer {
35
36class FontRenderer;
37
Romain Guy51769a62010-07-23 00:28:00 -070038/**
39 * Represents a font, defined by a Skia font id and a font size. A font is used
40 * to generate glyphs and cache them in the FontState.
41 */
Romain Guy694b5192010-07-21 21:33:20 -070042class Font {
43public:
Romain Guy325a0f92011-01-05 15:26:55 -080044 enum Style {
45 kFakeBold
46 };
47
Romain Guy694b5192010-07-21 21:33:20 -070048 ~Font();
49
Romain Guy51769a62010-07-23 00:28:00 -070050 /**
51 * Renders the specified string of text.
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070052 * If bitmap is specified, it will be used as the render target
Romain Guy51769a62010-07-23 00:28:00 -070053 */
54 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070055 int numGlyphs, int x, int y,
56 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
Romain Guy51769a62010-07-23 00:28:00 -070057 /**
58 * Creates a new font associated with the specified font state.
59 */
Romain Guy2577db12011-01-18 13:02:38 -080060 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize,
61 int flags, uint32_t italicStyle);
Romain Guy694b5192010-07-21 21:33:20 -070062
63protected:
Romain Guy694b5192010-07-21 21:33:20 -070064 friend class FontRenderer;
65
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070066 enum RenderMode {
67 FRAMEBUFFER,
68 BITMAP,
69 MEASURE,
70 };
71
72 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
73 int numGlyphs, int x, int y, RenderMode mode,
74 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
75 Rect *bounds);
76
77 void measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
78 int numGlyphs, Rect *bounds);
79
Romain Guy694b5192010-07-21 21:33:20 -070080 struct CachedGlyphInfo {
81 // Has the cache been invalidated?
82 bool mIsValid;
83 // Location of the cached glyph in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070084 // in case we need to resize the texture or
85 // render to bitmap
86 uint32_t mStartX;
87 uint32_t mStartY;
Romain Guy694b5192010-07-21 21:33:20 -070088 uint32_t mBitmapWidth;
89 uint32_t mBitmapHeight;
90 // Also cache texture coords for the quad
91 float mBitmapMinU;
92 float mBitmapMinV;
93 float mBitmapMaxU;
94 float mBitmapMaxV;
95 // Minimize how much we call freetype
96 uint32_t mGlyphIndex;
97 uint32_t mAdvanceX;
98 uint32_t mAdvanceY;
99 // Values below contain a glyph's origin in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700100 int32_t mBitmapLeft;
101 int32_t mBitmapTop;
Romain Guy2bffd262010-09-12 17:40:02 -0700102 // Auto-kerning
103 SkFixed mLsbDelta;
104 SkFixed mRsbDelta;
Romain Guy694b5192010-07-21 21:33:20 -0700105 };
106
Romain Guy2577db12011-01-18 13:02:38 -0800107 Font(FontRenderer* state, uint32_t fontId, float fontSize, int flags, uint32_t italicStyle);
Romain Guy694b5192010-07-21 21:33:20 -0700108
109 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs;
110
Romain Guybd0e6aa2010-07-22 18:50:12 -0700111 void invalidateTextureCache();
112
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700113 CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph);
Romain Guy694b5192010-07-21 21:33:20 -0700114 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700115 void measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds);
Romain Guy694b5192010-07-21 21:33:20 -0700116 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700117 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
118 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH);
Romain Guybd0e6aa2010-07-22 18:50:12 -0700119
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700120 CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar);
121
Romain Guybd0e6aa2010-07-22 18:50:12 -0700122 FontRenderer* mState;
123 uint32_t mFontId;
124 float mFontSize;
Romain Guy325a0f92011-01-05 15:26:55 -0800125 int mFlags;
Romain Guy2577db12011-01-18 13:02:38 -0800126 uint32_t mItalicStyle;
Romain Guy694b5192010-07-21 21:33:20 -0700127};
128
129class FontRenderer {
130public:
131 FontRenderer();
132 ~FontRenderer();
133
134 void init();
135 void deinit();
136
Romain Guyb45c0c92010-08-26 20:35:23 -0700137 void setGammaTable(const uint8_t* gammaTable) {
138 mGammaTable = gammaTable;
139 }
140
Alex Sakhartchouk894df172011-02-17 16:45:37 -0800141 void setAttributeBindingSlots(int positionSlot, int texCoordSlot) {
142 mPositionAttrSlot = positionSlot;
143 mTexcoordAttrSlot = texCoordSlot;
144 }
145
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700146 void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
Romain Guy5b3b3522010-10-27 18:57:51 -0700147 bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
148 uint32_t len, int numGlyphs, int x, int y, Rect* bounds);
Romain Guy694b5192010-07-21 21:33:20 -0700149
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700150 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -0700151 DropShadow() { };
152
153 DropShadow(const DropShadow& dropShadow):
154 width(dropShadow.width), height(dropShadow.height),
155 image(dropShadow.image), penX(dropShadow.penX),
156 penY(dropShadow.penY) {
157 }
158
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700159 uint32_t width;
160 uint32_t height;
161 uint8_t* image;
162 int32_t penX;
163 int32_t penY;
164 };
165
166 // After renderDropShadow returns, the called owns the memory in DropShadow.image
167 // and is responsible for releasing it when it's done with it
168 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Romain Guy1e45aae2010-08-13 19:39:53 -0700169 uint32_t len, int numGlyphs, uint32_t radius);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700170
Romain Guye8cb9c142010-10-04 14:14:11 -0700171 GLuint getTexture(bool linearFiltering = false) {
Romain Guy694b5192010-07-21 21:33:20 -0700172 checkInit();
Romain Guye8cb9c142010-10-04 14:14:11 -0700173 if (linearFiltering != mLinearFiltering) {
174 mLinearFiltering = linearFiltering;
175 const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST;
176
177 glBindTexture(GL_TEXTURE_2D, mTextureId);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
180 }
Romain Guy694b5192010-07-21 21:33:20 -0700181 return mTextureId;
182 }
183
Romain Guyc15008e2010-11-10 11:59:15 -0800184 uint32_t getCacheWidth() const {
185 return mCacheWidth;
186 }
187
188 uint32_t getCacheHeight() const {
189 return mCacheHeight;
190 }
191
Romain Guy694b5192010-07-21 21:33:20 -0700192protected:
193 friend class Font;
194
Romain Guyb45c0c92010-08-26 20:35:23 -0700195 const uint8_t* mGammaTable;
196
Romain Guy694b5192010-07-21 21:33:20 -0700197 struct CacheTextureLine {
198 uint16_t mMaxHeight;
199 uint16_t mMaxWidth;
200 uint32_t mCurrentRow;
201 uint32_t mCurrentCol;
Romain Guy1e45aae2010-08-13 19:39:53 -0700202 bool mDirty;
Romain Guy694b5192010-07-21 21:33:20 -0700203
Romain Guy51769a62010-07-23 00:28:00 -0700204 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow,
Romain Guy694b5192010-07-21 21:33:20 -0700205 uint32_t currentCol):
Romain Guy51769a62010-07-23 00:28:00 -0700206 mMaxHeight(maxHeight),
207 mMaxWidth(maxWidth),
208 mCurrentRow(currentRow),
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700209 mCurrentCol(currentCol),
210 mDirty(false) {
Romain Guy694b5192010-07-21 21:33:20 -0700211 }
212
213 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) {
Romain Guy7975fb62010-10-01 16:36:14 -0700214 if (glyph.fHeight + 2 > mMaxHeight) {
Romain Guy694b5192010-07-21 21:33:20 -0700215 return false;
216 }
217
Romain Guy7975fb62010-10-01 16:36:14 -0700218 if (mCurrentCol + glyph.fWidth + 2 < mMaxWidth) {
219 *retOriginX = mCurrentCol + 1;
220 *retOriginY = mCurrentRow + 1;
221 mCurrentCol += glyph.fWidth + 2;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700222 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -0700223 return true;
224 }
225
226 return false;
227 }
228 };
229
Romain Guy694b5192010-07-21 21:33:20 -0700230 void initTextTexture();
Romain Guy694b5192010-07-21 21:33:20 -0700231 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
232
233 void flushAllAndInvalidate();
234 void initVertexArrayBuffers();
235
236 void checkInit();
237
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700238 String16 mLatinPrecache;
239 void precacheLatin(SkPaint* paint);
240
Romain Guy694b5192010-07-21 21:33:20 -0700241 void issueDrawCommand();
Romain Guy694b5192010-07-21 21:33:20 -0700242 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
243 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
244 float x4, float y4, float z4, float u4, float v4);
245
246 uint32_t mCacheWidth;
247 uint32_t mCacheHeight;
248
Romain Guy694b5192010-07-21 21:33:20 -0700249 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700250 uint32_t getRemainingCacheCapacity();
Romain Guy694b5192010-07-21 21:33:20 -0700251
Romain Guy09147fb2010-07-22 13:08:20 -0700252 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700253 Vector<Font*> mActiveFonts;
254
255 // Texture to cache glyph bitmaps
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700256 uint8_t* mTextTexture;
257 const uint8_t* getTextTextureData() const {
258 return mTextTexture;
259 }
Romain Guy694b5192010-07-21 21:33:20 -0700260 GLuint mTextureId;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700261 void checkTextureUpdate();
Romain Guy694b5192010-07-21 21:33:20 -0700262 bool mUploadTexture;
263
264 // Pointer to vertex data to speed up frame to frame work
265 float *mTextMeshPtr;
266 uint32_t mCurrentQuadIndex;
267 uint32_t mMaxNumberOfQuads;
268
269 uint32_t mIndexBufferID;
270
Alex Sakhartchouk894df172011-02-17 16:45:37 -0800271 int32_t mPositionAttrSlot;
272 int32_t mTexcoordAttrSlot;
273
Romain Guy09147fb2010-07-22 13:08:20 -0700274 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700275 Rect* mBounds;
276 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700277
Romain Guy694b5192010-07-21 21:33:20 -0700278 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700279
Romain Guye8cb9c142010-10-04 14:14:11 -0700280 bool mLinearFiltering;
281
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700282 void computeGaussianWeights(float* weights, int32_t radius);
283 void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700284 int32_t width, int32_t height);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700285 void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700286 int32_t width, int32_t height);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700287 void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700288};
289
290}; // namespace uirenderer
291}; // namespace android
292
Romain Guy5b3b3522010-10-27 18:57:51 -0700293#endif // ANDROID_HWUI_FONT_RENDERER_H