blob: 128e8b85669f850767c4a2c9a37d3baac339ce11 [file] [log] [blame]
Romain Guy9f5dab32012-09-04 12:55:44 -07001/*
2 * Copyright (C) 2012 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 Guya3dc55f2012-09-28 13:55:44 -070017#define LOG_TAG "OpenGLRenderer"
18
Romain Guy9f5dab32012-09-04 12:55:44 -070019#include <cutils/compiler.h>
20
Romain Guye3a9b242013-01-08 11:15:30 -080021#include <utils/JenkinsHash.h>
22
Romain Guy9f5dab32012-09-04 12:55:44 -070023#include <SkUtils.h>
24
25#include "Debug.h"
26#include "FontUtil.h"
27#include "Font.h"
28#include "FontRenderer.h"
29#include "Properties.h"
30
31namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Font
36///////////////////////////////////////////////////////////////////////////////
37
Romain Guye3a9b242013-01-08 11:15:30 -080038Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
39 mState(state), mDescription(desc) {
Romain Guy9f5dab32012-09-04 12:55:44 -070040}
41
Romain Guye3a9b242013-01-08 11:15:30 -080042Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
43 mFontId = SkTypeface::UniqueID(paint->getTypeface());
44 mFontSize = paint->getTextSize();
45 mFlags = 0;
46 if (paint->isFakeBoldText()) {
47 mFlags |= Font::kFakeBold;
48 }
49 mItalicStyle = paint->getTextSkewX();
50 mScaleX = paint->getTextScaleX();
51 mStyle = paint->getStyle();
52 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080053 mAntiAliasing = paint->isAntiAlias();
Romain Guye3a9b242013-01-08 11:15:30 -080054}
Romain Guy9f5dab32012-09-04 12:55:44 -070055
56Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070057 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070058
59 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
60 delete mCachedGlyphs.valueAt(i);
61 }
62}
63
Romain Guye3a9b242013-01-08 11:15:30 -080064hash_t Font::FontDescription::hash() const {
65 uint32_t hash = JenkinsHashMix(0, mFontId);
66 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
67 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
68 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
69 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
70 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
71 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080072 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guye3a9b242013-01-08 11:15:30 -080073 return JenkinsHashWhiten(hash);
74}
75
76int Font::FontDescription::compare(const Font::FontDescription& lhs,
77 const Font::FontDescription& rhs) {
78 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
79 if (deltaInt != 0) return deltaInt;
80
81 if (lhs.mFontSize < rhs.mFontSize) return -1;
82 if (lhs.mFontSize > rhs.mFontSize) return +1;
83
84 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
85 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
86
87 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
88 if (deltaInt != 0) return deltaInt;
89
90 if (lhs.mScaleX < rhs.mScaleX) return -1;
91 if (lhs.mScaleX > rhs.mScaleX) return +1;
92
93 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
94 if (deltaInt != 0) return deltaInt;
95
96 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
97 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
98
Romain Guyb969a0d2013-02-05 14:38:40 -080099 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
100 if (deltaInt != 0) return deltaInt;
101
Romain Guye3a9b242013-01-08 11:15:30 -0800102 return 0;
103}
104
Romain Guy80872462012-09-04 16:42:01 -0700105void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700106 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
107 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700108 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700109 cachedGlyph->mIsValid = false;
110 }
111 }
112}
113
114void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
115 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
116 int nPenX = x + glyph->mBitmapLeft;
117 int nPenY = y + glyph->mBitmapTop;
118
119 int width = (int) glyph->mBitmapWidth;
120 int height = (int) glyph->mBitmapHeight;
121
122 if (bounds->bottom > nPenY) {
123 bounds->bottom = nPenY;
124 }
125 if (bounds->left > nPenX) {
126 bounds->left = nPenX;
127 }
128 if (bounds->right < nPenX + width) {
129 bounds->right = nPenX + width;
130 }
131 if (bounds->top < nPenY + height) {
132 bounds->top = nPenY + height;
133 }
134}
135
136void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
137 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800138 float nPenX = x + glyph->mBitmapLeft;
139 float nPenY = y + (glyph->mBitmapTop + glyph->mBitmapHeight);
140
141 float width = (float) glyph->mBitmapWidth;
142 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700143
144 float u1 = glyph->mBitmapMinU;
145 float u2 = glyph->mBitmapMaxU;
146 float v1 = glyph->mBitmapMinV;
147 float v2 = glyph->mBitmapMaxV;
148
Romain Guy9f5dab32012-09-04 12:55:44 -0700149 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
150 nPenX + width, nPenY, u2, v2,
151 nPenX + width, nPenY - height, u2, v1,
152 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
153}
154
155void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
156 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
157 int nPenX = x + glyph->mBitmapLeft;
158 int nPenY = y + glyph->mBitmapTop;
159
160 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
161 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
162
Romain Guy80872462012-09-04 16:42:01 -0700163 CacheTexture* cacheTexture = glyph->mCacheTexture;
164 uint32_t cacheWidth = cacheTexture->getWidth();
165 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700166
167 uint32_t cacheX = 0, cacheY = 0;
168 int32_t bX = 0, bY = 0;
169 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
170 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
171#if DEBUG_FONT_RENDERER
172 if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) {
173 ALOGE("Skipping invalid index");
174 continue;
175 }
176#endif
177 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
178 bitmap[bY * bitmapW + bX] = tempCol;
179 }
180 }
181}
182
183void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
184 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
185 const float halfWidth = glyph->mBitmapWidth * 0.5f;
186 const float height = glyph->mBitmapHeight;
187
188 vOffset += glyph->mBitmapTop + height;
189
190 SkPoint destination[4];
Romain Guye3a9b242013-01-08 11:15:30 -0800191 measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
Romain Guy9f5dab32012-09-04 12:55:44 -0700192
193 // Move along the tangent and offset by the normal
194 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
195 -tangent->fY * halfWidth + tangent->fX * vOffset);
196 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
197 tangent->fY * halfWidth + tangent->fX * vOffset);
198 destination[2].set(destination[1].fX + tangent->fY * height,
199 destination[1].fY - tangent->fX * height);
200 destination[3].set(destination[0].fX + tangent->fY * height,
201 destination[0].fY - tangent->fX * height);
202
203 const float u1 = glyph->mBitmapMinU;
204 const float u2 = glyph->mBitmapMaxU;
205 const float v1 = glyph->mBitmapMinV;
206 const float v2 = glyph->mBitmapMaxV;
207
208 mState->appendRotatedMeshQuad(
209 position->fX + destination[0].fX,
210 position->fY + destination[0].fY, u1, v2,
211 position->fX + destination[1].fX,
212 position->fY + destination[1].fY, u2, v2,
213 position->fX + destination[2].fX,
214 position->fY + destination[2].fY, u2, v1,
215 position->fX + destination[3].fX,
216 position->fY + destination[3].fY, u1, v1,
217 glyph->mCacheTexture);
218}
219
220CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
221 CachedGlyphInfo* cachedGlyph = NULL;
222 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
223 if (index >= 0) {
224 cachedGlyph = mCachedGlyphs.valueAt(index);
225 } else {
226 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
227 }
228
229 // Is the glyph still in texture cache?
230 if (!cachedGlyph->mIsValid) {
Romain Guye3a9b242013-01-08 11:15:30 -0800231 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700232 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
233 }
234
235 return cachedGlyph;
236}
237
Romain Guy9f5dab32012-09-04 12:55:44 -0700238void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
239 int numGlyphs, int x, int y, const float* positions) {
240 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
241 0, 0, NULL, positions);
242}
243
244void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
245 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
246 if (numGlyphs == 0 || text == NULL || len == 0) {
247 return;
248 }
249
250 text += start;
251
252 int glyphsCount = 0;
253 SkFixed prevRsbDelta = 0;
254
255 float penX = 0.0f;
256
257 SkPoint position;
258 SkVector tangent;
259
260 SkPathMeasure measure(*path, false);
261 float pathLength = SkScalarToFloat(measure.getLength());
262
263 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
264 float textWidth = SkScalarToFloat(paint->measureText(text, len));
265 float pathOffset = pathLength;
266 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
267 textWidth *= 0.5f;
268 pathOffset *= 0.5f;
269 }
270 penX += pathOffset - textWidth;
271 }
272
273 while (glyphsCount < numGlyphs && penX < pathLength) {
274 glyph_t glyph = GET_GLYPH(text);
275
276 if (IS_END_OF_STRING(glyph)) {
277 break;
278 }
279
280 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
281 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
282 prevRsbDelta = cachedGlyph->mRsbDelta;
283
284 if (cachedGlyph->mIsValid) {
285 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
286 }
287
288 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
289
290 glyphsCount++;
291 }
292}
293
294void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
295 int numGlyphs, Rect *bounds, const float* positions) {
296 if (bounds == NULL) {
297 ALOGE("No return rectangle provided to measure text");
298 return;
299 }
300 bounds->set(1e6, -1e6, -1e6, 1e6);
301 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
302}
303
304void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
305
306 if (numGlyphs == 0 || text == NULL) {
307 return;
308 }
309 int glyphsCount = 0;
310
311 while (glyphsCount < numGlyphs) {
312 glyph_t glyph = GET_GLYPH(text);
313
314 // Reached the end of the string
315 if (IS_END_OF_STRING(glyph)) {
316 break;
317 }
318
319 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
320
321 glyphsCount++;
322 }
323}
324
325void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
326 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
327 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
328 if (numGlyphs == 0 || text == NULL || len == 0) {
329 return;
330 }
331
332 static RenderGlyph gRenderGlyph[] = {
333 &android::uirenderer::Font::drawCachedGlyph,
334 &android::uirenderer::Font::drawCachedGlyphBitmap,
335 &android::uirenderer::Font::measureCachedGlyph
336 };
337 RenderGlyph render = gRenderGlyph[mode];
338
339 text += start;
340 int glyphsCount = 0;
341
Romain Guye3a9b242013-01-08 11:15:30 -0800342 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700343
Romain Guye3a9b242013-01-08 11:15:30 -0800344 while (glyphsCount < numGlyphs) {
345 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700346
Romain Guye3a9b242013-01-08 11:15:30 -0800347 // Reached the end of the string
348 if (IS_END_OF_STRING(glyph)) {
349 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700350 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700351
Romain Guye3a9b242013-01-08 11:15:30 -0800352 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700353
Romain Guye3a9b242013-01-08 11:15:30 -0800354 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
355 if (cachedGlyph->mIsValid) {
356 int penX = x + positions[(glyphsCount << 1)];
357 int penY = y + positions[(glyphsCount << 1) + 1];
358
359 switch (align) {
360 case SkPaint::kRight_Align:
361 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX);
362 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY);
363 break;
364 case SkPaint::kCenter_Align:
365 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX >> 1);
366 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY >> 1);
367 default:
368 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700369 }
370
Romain Guye3a9b242013-01-08 11:15:30 -0800371 (*this.*render)(cachedGlyph, penX, penY,
372 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700373 }
Romain Guye3a9b242013-01-08 11:15:30 -0800374
375 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700376 }
377}
378
379void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
380 bool precaching) {
381 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
382 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
383 glyph->mBitmapLeft = skiaGlyph.fLeft;
384 glyph->mBitmapTop = skiaGlyph.fTop;
385 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
386 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
387
388 uint32_t startX = 0;
389 uint32_t startY = 0;
390
391 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800392 if (!skiaGlyph.fImage) {
393 paint->findImage(skiaGlyph, NULL);
394 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700395 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
396
397 if (!glyph->mIsValid) {
398 return;
399 }
400
401 uint32_t endX = startX + skiaGlyph.fWidth;
402 uint32_t endY = startY + skiaGlyph.fHeight;
403
404 glyph->mStartX = startX;
405 glyph->mStartY = startY;
406 glyph->mBitmapWidth = skiaGlyph.fWidth;
407 glyph->mBitmapHeight = skiaGlyph.fHeight;
408
Romain Guy80872462012-09-04 16:42:01 -0700409 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
410 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700411
412 glyph->mBitmapMinU = startX / (float) cacheWidth;
413 glyph->mBitmapMinV = startY / (float) cacheHeight;
414 glyph->mBitmapMaxU = endX / (float) cacheWidth;
415 glyph->mBitmapMaxV = endY / (float) cacheHeight;
416
Romain Guy9b1204b2012-09-04 15:22:57 -0700417 mState->setTextureDirty();
Romain Guy9f5dab32012-09-04 12:55:44 -0700418}
419
420CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
421 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
422 mCachedGlyphs.add(glyph, newGlyph);
423
Romain Guye3a9b242013-01-08 11:15:30 -0800424 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700425 newGlyph->mGlyphIndex = skiaGlyph.fID;
426 newGlyph->mIsValid = false;
427
428 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
429
430 return newGlyph;
431}
432
Romain Guye3a9b242013-01-08 11:15:30 -0800433Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
434 FontDescription description(paint, matrix);
435 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700436
Romain Guye3a9b242013-01-08 11:15:30 -0800437 if (font) {
438 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700439 }
440
Romain Guye3a9b242013-01-08 11:15:30 -0800441 Font* newFont = new Font(state, description);
442 state->mActiveFonts.put(description, newFont);
Romain Guy9f5dab32012-09-04 12:55:44 -0700443 return newFont;
444}
445
446}; // namespace uirenderer
447}; // namespace android