blob: 4cd902e1804c6ff0a9d97a2ab1ebcaa558bd2022 [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.
48 */
49 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070050 int numGlyphs, int x, int y,
51 uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0);
Romain Guy51769a62010-07-23 00:28:00 -070052 /**
53 * Creates a new font associated with the specified font state.
54 */
Romain Guy694b5192010-07-21 21:33:20 -070055 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize);
56
57protected:
Romain Guy694b5192010-07-21 21:33:20 -070058 friend class FontRenderer;
59
Romain Guy694b5192010-07-21 21:33:20 -070060 struct CachedGlyphInfo {
61 // Has the cache been invalidated?
62 bool mIsValid;
63 // Location of the cached glyph in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070064 // in case we need to resize the texture or
65 // render to bitmap
66 uint32_t mStartX;
67 uint32_t mStartY;
Romain Guy694b5192010-07-21 21:33:20 -070068 uint32_t mBitmapWidth;
69 uint32_t mBitmapHeight;
70 // Also cache texture coords for the quad
71 float mBitmapMinU;
72 float mBitmapMinV;
73 float mBitmapMaxU;
74 float mBitmapMaxV;
75 // Minimize how much we call freetype
76 uint32_t mGlyphIndex;
77 uint32_t mAdvanceX;
78 uint32_t mAdvanceY;
79 // Values below contain a glyph's origin in the bitmap
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070080 int32_t mBitmapLeft;
81 int32_t mBitmapTop;
Romain Guy694b5192010-07-21 21:33:20 -070082 };
83
Romain Guy694b5192010-07-21 21:33:20 -070084 Font(FontRenderer* state, uint32_t fontId, float fontSize);
85
86 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs;
87
Romain Guybd0e6aa2010-07-22 18:50:12 -070088 void invalidateTextureCache();
89
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -070090 CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph);
Romain Guy694b5192010-07-21 21:33:20 -070091 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
92 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -070093 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
94 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH);
Romain Guybd0e6aa2010-07-22 18:50:12 -070095
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -070096 CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar);
97
Romain Guybd0e6aa2010-07-22 18:50:12 -070098 FontRenderer* mState;
99 uint32_t mFontId;
100 float mFontSize;
Romain Guy694b5192010-07-21 21:33:20 -0700101};
102
103class FontRenderer {
104public:
105 FontRenderer();
106 ~FontRenderer();
107
108 void init();
109 void deinit();
110
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700111 void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
Romain Guy51769a62010-07-23 00:28:00 -0700112 void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
113 uint32_t len, int numGlyphs, int x, int y);
Romain Guy694b5192010-07-21 21:33:20 -0700114
115 GLuint getTexture() {
116 checkInit();
117 return mTextureId;
118 }
119
120protected:
121 friend class Font;
122
123 struct CacheTextureLine {
124 uint16_t mMaxHeight;
125 uint16_t mMaxWidth;
126 uint32_t mCurrentRow;
127 uint32_t mCurrentCol;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700128 bool mDirty;
Romain Guy694b5192010-07-21 21:33:20 -0700129
Romain Guy51769a62010-07-23 00:28:00 -0700130 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow,
Romain Guy694b5192010-07-21 21:33:20 -0700131 uint32_t currentCol):
Romain Guy51769a62010-07-23 00:28:00 -0700132 mMaxHeight(maxHeight),
133 mMaxWidth(maxWidth),
134 mCurrentRow(currentRow),
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700135 mCurrentCol(currentCol),
136 mDirty(false) {
Romain Guy694b5192010-07-21 21:33:20 -0700137 }
138
139 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) {
140 if (glyph.fHeight > mMaxHeight) {
141 return false;
142 }
143
144 if (mCurrentCol + glyph.fWidth < mMaxWidth) {
145 *retOriginX = mCurrentCol;
146 *retOriginY = mCurrentRow;
147 mCurrentCol += glyph.fWidth;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700148 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -0700149 return true;
150 }
151
152 return false;
153 }
154 };
155
156 uint32_t getCacheWidth() const {
157 return mCacheWidth;
158 }
159
160 uint32_t getCacheHeight() const {
161 return mCacheHeight;
162 }
163
164 void initTextTexture();
Romain Guy694b5192010-07-21 21:33:20 -0700165 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
166
167 void flushAllAndInvalidate();
168 void initVertexArrayBuffers();
169
170 void checkInit();
171
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700172 String16 mLatinPrecache;
173 void precacheLatin(SkPaint* paint);
174
Romain Guy694b5192010-07-21 21:33:20 -0700175 void issueDrawCommand();
Romain Guy694b5192010-07-21 21:33:20 -0700176 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
177 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
178 float x4, float y4, float z4, float u4, float v4);
179
180 uint32_t mCacheWidth;
181 uint32_t mCacheHeight;
182
Romain Guy694b5192010-07-21 21:33:20 -0700183 Vector<CacheTextureLine*> mCacheLines;
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700184 uint32_t getRemainingCacheCapacity();
Romain Guy694b5192010-07-21 21:33:20 -0700185
Romain Guy09147fb2010-07-22 13:08:20 -0700186 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700187 Vector<Font*> mActiveFonts;
188
189 // Texture to cache glyph bitmaps
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700190 uint8_t* mTextTexture;
191 const uint8_t* getTextTextureData() const {
192 return mTextTexture;
193 }
Romain Guy694b5192010-07-21 21:33:20 -0700194 GLuint mTextureId;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700195 void checkTextureUpdate();
Romain Guy694b5192010-07-21 21:33:20 -0700196 bool mUploadTexture;
197
198 // Pointer to vertex data to speed up frame to frame work
199 float *mTextMeshPtr;
200 uint32_t mCurrentQuadIndex;
201 uint32_t mMaxNumberOfQuads;
202
203 uint32_t mIndexBufferID;
204
Romain Guy09147fb2010-07-22 13:08:20 -0700205 const Rect* mClip;
206
Romain Guy694b5192010-07-21 21:33:20 -0700207 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700208
209 void computeGaussianWeights(float* weights, int32_t radius);
210 void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
211 int32_t width, int32_t height);
212 void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
213 int32_t width, int32_t height);
214 void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700215};
216
217}; // namespace uirenderer
218}; // namespace android
219
220#endif // ANDROID_UI_FONT_RENDERER_H