blob: a03ea92fbf08b7907e89a8fbd33c644589e4b739 [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
17#ifndef ANDROID_UI_FONT_RENDERER_H
18#define ANDROID_UI_FONT_RENDERER_H
19
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:
44 ~Font();
45
Romain Guy51769a62010-07-23 00:28:00 -070046 /**
47 * Renders the specified string of text.
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070048 * If bitmap is specified, it will be used as the render target
Romain Guy51769a62010-07-23 00:28:00 -070049 */
50 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070051 int numGlyphs, int x, int y,
52 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
Romain Guy51769a62010-07-23 00:28:00 -070053 /**
54 * Creates a new font associated with the specified font state.
55 */
Romain Guy694b5192010-07-21 21:33:20 -070056 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize);
57
58protected:
Romain Guy694b5192010-07-21 21:33:20 -070059 friend class FontRenderer;
60
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070061 enum RenderMode {
62 FRAMEBUFFER,
63 BITMAP,
64 MEASURE,
65 };
66
67 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
68 int numGlyphs, int x, int y, RenderMode mode,
69 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
70 Rect *bounds);
71
72 void measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
73 int numGlyphs, Rect *bounds);
74
Romain Guy694b5192010-07-21 21:33:20 -070075 struct CachedGlyphInfo {
76 // Has the cache been invalidated?
77 bool mIsValid;
78 // Location of the cached glyph in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070079 // in case we need to resize the texture or
80 // render to bitmap
81 uint32_t mStartX;
82 uint32_t mStartY;
Romain Guy694b5192010-07-21 21:33:20 -070083 uint32_t mBitmapWidth;
84 uint32_t mBitmapHeight;
85 // Also cache texture coords for the quad
86 float mBitmapMinU;
87 float mBitmapMinV;
88 float mBitmapMaxU;
89 float mBitmapMaxV;
90 // Minimize how much we call freetype
91 uint32_t mGlyphIndex;
92 uint32_t mAdvanceX;
93 uint32_t mAdvanceY;
94 // Values below contain a glyph's origin in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070095 int32_t mBitmapLeft;
96 int32_t mBitmapTop;
Romain Guy694b5192010-07-21 21:33:20 -070097 };
98
Romain Guy694b5192010-07-21 21:33:20 -070099 Font(FontRenderer* state, uint32_t fontId, float fontSize);
100
101 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs;
102
Romain Guybd0e6aa2010-07-22 18:50:12 -0700103 void invalidateTextureCache();
104
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700105 CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph);
Romain Guy694b5192010-07-21 21:33:20 -0700106 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700107 void measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds);
Romain Guy694b5192010-07-21 21:33:20 -0700108 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700109 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
110 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH);
Romain Guybd0e6aa2010-07-22 18:50:12 -0700111
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700112 CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar);
113
Romain Guybd0e6aa2010-07-22 18:50:12 -0700114 FontRenderer* mState;
115 uint32_t mFontId;
116 float mFontSize;
Romain Guy694b5192010-07-21 21:33:20 -0700117};
118
119class FontRenderer {
120public:
121 FontRenderer();
122 ~FontRenderer();
123
124 void init();
125 void deinit();
126
Romain Guyb45c0c92010-08-26 20:35:23 -0700127 void setGammaTable(const uint8_t* gammaTable) {
128 mGammaTable = gammaTable;
129 }
130
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700131 void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
Romain Guy51769a62010-07-23 00:28:00 -0700132 void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
133 uint32_t len, int numGlyphs, int x, int y);
Romain Guy694b5192010-07-21 21:33:20 -0700134
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700135 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -0700136 DropShadow() { };
137
138 DropShadow(const DropShadow& dropShadow):
139 width(dropShadow.width), height(dropShadow.height),
140 image(dropShadow.image), penX(dropShadow.penX),
141 penY(dropShadow.penY) {
142 }
143
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700144 uint32_t width;
145 uint32_t height;
146 uint8_t* image;
147 int32_t penX;
148 int32_t penY;
149 };
150
151 // After renderDropShadow returns, the called owns the memory in DropShadow.image
152 // and is responsible for releasing it when it's done with it
153 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Romain Guy1e45aae2010-08-13 19:39:53 -0700154 uint32_t len, int numGlyphs, uint32_t radius);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700155
Romain Guy694b5192010-07-21 21:33:20 -0700156 GLuint getTexture() {
157 checkInit();
158 return mTextureId;
159 }
160
161protected:
162 friend class Font;
163
Romain Guyb45c0c92010-08-26 20:35:23 -0700164 const uint8_t* mGammaTable;
165
Romain Guy694b5192010-07-21 21:33:20 -0700166 struct CacheTextureLine {
167 uint16_t mMaxHeight;
168 uint16_t mMaxWidth;
169 uint32_t mCurrentRow;
170 uint32_t mCurrentCol;
Romain Guy1e45aae2010-08-13 19:39:53 -0700171 bool mDirty;
Romain Guy694b5192010-07-21 21:33:20 -0700172
Romain Guy51769a62010-07-23 00:28:00 -0700173 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow,
Romain Guy694b5192010-07-21 21:33:20 -0700174 uint32_t currentCol):
Romain Guy51769a62010-07-23 00:28:00 -0700175 mMaxHeight(maxHeight),
176 mMaxWidth(maxWidth),
177 mCurrentRow(currentRow),
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700178 mCurrentCol(currentCol),
179 mDirty(false) {
Romain Guy694b5192010-07-21 21:33:20 -0700180 }
181
182 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) {
183 if (glyph.fHeight > mMaxHeight) {
184 return false;
185 }
186
187 if (mCurrentCol + glyph.fWidth < mMaxWidth) {
188 *retOriginX = mCurrentCol;
189 *retOriginY = mCurrentRow;
190 mCurrentCol += glyph.fWidth;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700191 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -0700192 return true;
193 }
194
195 return false;
196 }
197 };
198
199 uint32_t getCacheWidth() const {
200 return mCacheWidth;
201 }
202
203 uint32_t getCacheHeight() const {
204 return mCacheHeight;
205 }
206
207 void initTextTexture();
Romain Guy694b5192010-07-21 21:33:20 -0700208 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
209
210 void flushAllAndInvalidate();
211 void initVertexArrayBuffers();
212
213 void checkInit();
214
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700215 String16 mLatinPrecache;
216 void precacheLatin(SkPaint* paint);
217
Romain Guy694b5192010-07-21 21:33:20 -0700218 void issueDrawCommand();
Romain Guy694b5192010-07-21 21:33:20 -0700219 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
220 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
221 float x4, float y4, float z4, float u4, float v4);
222
223 uint32_t mCacheWidth;
224 uint32_t mCacheHeight;
225
Romain Guy694b5192010-07-21 21:33:20 -0700226 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700227 uint32_t getRemainingCacheCapacity();
Romain Guy694b5192010-07-21 21:33:20 -0700228
Romain Guy09147fb2010-07-22 13:08:20 -0700229 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700230 Vector<Font*> mActiveFonts;
231
232 // Texture to cache glyph bitmaps
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700233 uint8_t* mTextTexture;
234 const uint8_t* getTextTextureData() const {
235 return mTextTexture;
236 }
Romain Guy694b5192010-07-21 21:33:20 -0700237 GLuint mTextureId;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700238 void checkTextureUpdate();
Romain Guy694b5192010-07-21 21:33:20 -0700239 bool mUploadTexture;
240
241 // Pointer to vertex data to speed up frame to frame work
242 float *mTextMeshPtr;
243 uint32_t mCurrentQuadIndex;
244 uint32_t mMaxNumberOfQuads;
245
246 uint32_t mIndexBufferID;
247
Romain Guy09147fb2010-07-22 13:08:20 -0700248 const Rect* mClip;
249
Romain Guy694b5192010-07-21 21:33:20 -0700250 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700251
252 void computeGaussianWeights(float* weights, int32_t radius);
253 void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700254 int32_t width, int32_t height);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700255 void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700256 int32_t width, int32_t height);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700257 void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700258};
259
260}; // namespace uirenderer
261}; // namespace android
262
263#endif // ANDROID_UI_FONT_RENDERER_H