1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "OpenGLRenderer"
#include <SkUtils.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include "FontRenderer.h"
namespace android {
namespace uirenderer {
///////////////////////////////////////////////////////////////////////////////
// Defines
///////////////////////////////////////////////////////////////////////////////
#define DEFAULT_TEXT_CACHE_WIDTH 1024
#define DEFAULT_TEXT_CACHE_HEIGHT 256
///////////////////////////////////////////////////////////////////////////////
// Font
///////////////////////////////////////////////////////////////////////////////
Font::Font(FontRenderer* state, uint32_t fontId, float fontSize) :
mState(state), mFontId(fontId), mFontSize(fontSize) {
}
Font::~Font() {
for (uint32_t ct = 0; ct < mState->mActiveFonts.size(); ct++) {
if (mState->mActiveFonts[ct] == this) {
mState->mActiveFonts.removeAt(ct);
break;
}
}
for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
CachedGlyphInfo* glyph = mCachedGlyphs.valueAt(i);
delete glyph;
}
}
void Font::invalidateTextureCache() {
for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
mCachedGlyphs.valueAt(i)->mIsValid = false;
}
}
void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y) {
int nPenX = x + glyph->mBitmapLeft;
int nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
float u1 = glyph->mBitmapMinU;
float u2 = glyph->mBitmapMaxU;
float v1 = glyph->mBitmapMinV;
float v2 = glyph->mBitmapMaxV;
int width = (int) glyph->mBitmapWidth;
int height = (int) glyph->mBitmapHeight;
mState->appendMeshQuad(nPenX, nPenY, 0, u1, v2,
nPenX + width, nPenY, 0, u2, v2,
nPenX + width, nPenY - height, 0, u2, v1,
nPenX, nPenY - height, 0, u1, v1);
}
Font::CachedGlyphInfo* Font::getCachedUTFChar(SkPaint* paint, int32_t utfChar) {
CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(utfChar);
if (cachedGlyph == NULL) {
cachedGlyph = cacheGlyph(paint, utfChar);
}
// Is the glyph still in texture cache?
if (!cachedGlyph->mIsValid) {
const SkGlyph& skiaGlyph = paint->getUnicharMetrics(utfChar);
updateGlyphCache(paint, skiaGlyph, cachedGlyph);
}
return cachedGlyph;
}
void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
int numGlyphs, int x, int y) {
if (numGlyphs == 0 || text == NULL || len == 0) {
return;
}
int penX = x, penY = y;
int glyphsLeft = 1;
if (numGlyphs > 0) {
glyphsLeft = numGlyphs;
}
text += start;
while (glyphsLeft > 0) {
int32_t utfChar = SkUTF16_NextUnichar((const uint16_t**) &text);
// Reached the end of the string or encountered
if (utfChar < 0) {
break;
}
CachedGlyphInfo* cachedGlyph = getCachedUTFChar(paint, utfChar);
// If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
if (cachedGlyph->mIsValid) {
drawCachedGlyph(cachedGlyph, penX, penY);
}
penX += SkFixedFloor(cachedGlyph->mAdvanceX);
// If we were given a specific number of glyphs, decrement
if (numGlyphs > 0) {
glyphsLeft--;
}
}
}
void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph) {
glyph->mAdvanceX = skiaGlyph.fAdvanceX;
glyph->mAdvanceY = skiaGlyph.fAdvanceY;
glyph->mBitmapLeft = skiaGlyph.fLeft;
glyph->mBitmapTop = skiaGlyph.fTop;
uint32_t startX = 0;
uint32_t startY = 0;
// Get the bitmap for the glyph
paint->findImage(skiaGlyph);
glyph->mIsValid = mState->cacheBitmap(skiaGlyph, &startX, &startY);
if (!glyph->mIsValid) {
return;
}
uint32_t endX = startX + skiaGlyph.fWidth;
uint32_t endY = startY + skiaGlyph.fHeight;
glyph->mBitmapWidth = skiaGlyph.fWidth;
glyph->mBitmapHeight = skiaGlyph.fHeight;
uint32_t cacheWidth = mState->getCacheWidth();
uint32_t cacheHeight = mState->getCacheHeight();
glyph->mBitmapMinU = (float) startX / (float) cacheWidth;
glyph->mBitmapMinV = (float) startY / (float) cacheHeight;
glyph->mBitmapMaxU = (float) endX / (float) cacheWidth;
glyph->mBitmapMaxV = (float) endY / (float) cacheHeight;
mState->mUploadTexture = true;
}
Font::CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, int32_t glyph) {
CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
mCachedGlyphs.add(glyph, newGlyph);
const SkGlyph& skiaGlyph = paint->getUnicharMetrics(glyph);
newGlyph->mGlyphIndex = skiaGlyph.fID;
newGlyph->mIsValid = false;
updateGlyphCache(paint, skiaGlyph, newGlyph);
return newGlyph;
}
Font* Font::create(FontRenderer* state, uint32_t fontId, float fontSize) {
Vector<Font*> &activeFonts = state->mActiveFonts;
for (uint32_t i = 0; i < activeFonts.size(); i++) {
Font* font = activeFonts[i];
if (font->mFontId == fontId && font->mFontSize == fontSize) {
return font;
}
}
Font* newFont = new Font(state, fontId, fontSize);
activeFonts.push(newFont);
return newFont;
}
///////////////////////////////////////////////////////////////////////////////
// FontRenderer
///////////////////////////////////////////////////////////////////////////////
FontRenderer::FontRenderer() {
LOGD("Creating FontRenderer");
mInitialized = false;
mMaxNumberOfQuads = 1024;
mCurrentQuadIndex = 0;
mTextureId = 0;
mIndexBufferID = 0;
mCacheWidth = DEFAULT_TEXT_CACHE_WIDTH;
mCacheHeight = DEFAULT_TEXT_CACHE_HEIGHT;
char property[PROPERTY_VALUE_MAX];
if (property_get(PROPERTY_TEXT_CACHE_WIDTH, property, NULL) > 0) {
LOGD(" Setting text cache width to %s pixels", property);
mCacheWidth = atoi(property);
} else {
LOGD(" Using default text cache width of %i pixels", mCacheWidth);
}
if (property_get(PROPERTY_TEXT_CACHE_HEIGHT, property, NULL) > 0) {
LOGD(" Setting text cache width to %s pixels", property);
mCacheHeight = atoi(property);
} else {
LOGD(" Using default text cache height of %i pixels", mCacheHeight);
}
}
FontRenderer::~FontRenderer() {
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
delete mCacheLines[i];
}
mCacheLines.clear();
delete mTextMeshPtr;
delete mTextTexture;
if(mTextureId) {
glDeleteTextures(1, &mTextureId);
}
Vector<Font*> fontsToDereference = mActiveFonts;
for (uint32_t i = 0; i < fontsToDereference.size(); i++) {
delete fontsToDereference[i];
}
}
void FontRenderer::flushAllAndInvalidate() {
if (mCurrentQuadIndex != 0) {
issueDrawCommand();
mCurrentQuadIndex = 0;
}
for (uint32_t i = 0; i < mActiveFonts.size(); i++) {
mActiveFonts[i]->invalidateTextureCache();
}
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
mCacheLines[i]->mCurrentCol = 0;
}
}
bool FontRenderer::cacheBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) {
// If the glyph is too tall, don't cache it
if (glyph.fWidth > mCacheLines[mCacheLines.size() - 1]->mMaxHeight) {
LOGE("Font size to large to fit in cache. width, height = %i, %i",
(int) glyph.fWidth, (int) glyph.fHeight);
return false;
}
// Now copy the bitmap into the cache texture
uint32_t startX = 0;
uint32_t startY = 0;
bool bitmapFit = false;
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY);
if (bitmapFit) {
break;
}
}
// If the new glyph didn't fit, flush the state so far and invalidate everything
if (!bitmapFit) {
flushAllAndInvalidate();
// Try to fit it again
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY);
if (bitmapFit) {
break;
}
}
// if we still don't fit, something is wrong and we shouldn't draw
if (!bitmapFit) {
LOGE("Bitmap doesn't fit in cache. width, height = %i, %i",
(int) glyph.fWidth, (int) glyph.fHeight);
return false;
}
}
*retOriginX = startX;
*retOriginY = startY;
uint32_t endX = startX + glyph.fWidth;
uint32_t endY = startY + glyph.fHeight;
uint32_t cacheWidth = mCacheWidth;
unsigned char* cacheBuffer = mTextTexture;
unsigned char* bitmapBuffer = (unsigned char*) glyph.fImage;
unsigned int stride = glyph.rowBytes();
uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0;
for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) {
for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY++) {
unsigned char tempCol = bitmapBuffer[bY * stride + bX];
cacheBuffer[cacheY * cacheWidth + cacheX] = tempCol;
}
}
return true;
}
void FontRenderer::initTextTexture() {
mTextTexture = new unsigned char[mCacheWidth * mCacheHeight];
mUploadTexture = false;
glGenTextures(1, &mTextureId);
glBindTexture(GL_TEXTURE_2D, mTextureId);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Initialize texture dimentions
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, mCacheWidth, mCacheHeight, 0,
GL_ALPHA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Split up our cache texture into lines of certain widths
int nextLine = 0;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 16, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, 40, nextLine, 0));
nextLine += mCacheLines.top()->mMaxHeight;
mCacheLines.push(new CacheTextureLine(mCacheWidth, mCacheHeight - nextLine, nextLine, 0));
}
// Avoid having to reallocate memory and render quad by quad
void FontRenderer::initVertexArrayBuffers() {
uint32_t numIndicies = mMaxNumberOfQuads * 6;
uint32_t indexBufferSizeBytes = numIndicies * sizeof(uint16_t);
uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes);
// Four verts, two triangles , six indices per quad
for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) {
int i6 = i * 6;
int i4 = i * 4;
indexBufferData[i6 + 0] = i4 + 0;
indexBufferData[i6 + 1] = i4 + 1;
indexBufferData[i6 + 2] = i4 + 2;
indexBufferData[i6 + 3] = i4 + 0;
indexBufferData[i6 + 4] = i4 + 2;
indexBufferData[i6 + 5] = i4 + 3;
}
glGenBuffers(1, &mIndexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, mIndexBufferID);
glBufferData(GL_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
free(indexBufferData);
uint32_t coordSize = 3;
uint32_t uvSize = 2;
uint32_t vertsPerQuad = 4;
uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize;
mTextMeshPtr = new float[vertexBufferSize];
}
// We don't want to allocate anything unless we actually draw text
void FontRenderer::checkInit() {
if (mInitialized) {
return;
}
initTextTexture();
initVertexArrayBuffers();
// We store a string with letters in a rough frequency of occurrence
mLatinPrecache = String16("eisarntolcdugpmhbyfvkwzxjq ");
mLatinPrecache += String16("EISARNTOLCDUGPMHBYFVKWZXJQ");
mLatinPrecache += String16(",.?!()-+@;:`'");
mLatinPrecache += String16("0123456789");
mInitialized = true;
}
void FontRenderer::checkTextureUpdate() {
if (!mUploadTexture) {
return;
}
glBindTexture(GL_TEXTURE_2D, mTextureId);
// Iterate over all the cache lines and see which ones need to be updated
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
CacheTextureLine* cl = mCacheLines[i];
if(cl->mDirty) {
uint32_t xOffset = 0;
uint32_t yOffset = cl->mCurrentRow;
uint32_t width = mCacheWidth;
uint32_t height = cl->mMaxHeight;
void* textureData = mTextTexture + yOffset*width;
glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height,
GL_ALPHA, GL_UNSIGNED_BYTE, textureData);
cl->mDirty = false;
}
}
mUploadTexture = false;
}
void FontRenderer::issueDrawCommand() {
checkTextureUpdate();
float* vtx = mTextMeshPtr;
float* tex = vtx + 3;
// position is slot 0
uint32_t slot = 0;
glVertexAttribPointer(slot, 3, GL_FLOAT, false, 20, vtx);
// texture0 is slot 1
slot = 1;
glVertexAttribPointer(slot, 2, GL_FLOAT, false, 20, tex);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferID);
glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, NULL);
}
void FontRenderer::appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2,
float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
float x4, float y4, float z4, float u4, float v4) {
if (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom) {
return;
}
const uint32_t vertsPerQuad = 4;
const uint32_t floatsPerVert = 5;
float* currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert;
(*currentPos++) = x1;
(*currentPos++) = y1;
(*currentPos++) = z1;
(*currentPos++) = u1;
(*currentPos++) = v1;
(*currentPos++) = x2;
(*currentPos++) = y2;
(*currentPos++) = z2;
(*currentPos++) = u2;
(*currentPos++) = v2;
(*currentPos++) = x3;
(*currentPos++) = y3;
(*currentPos++) = z3;
(*currentPos++) = u3;
(*currentPos++) = v3;
(*currentPos++) = x4;
(*currentPos++) = y4;
(*currentPos++) = z4;
(*currentPos++) = u4;
(*currentPos++) = v4;
mCurrentQuadIndex++;
if (mCurrentQuadIndex == mMaxNumberOfQuads) {
issueDrawCommand();
mCurrentQuadIndex = 0;
}
}
uint32_t FontRenderer::getRemainingCacheCapacity() {
uint32_t remainingCapacity = 0;
float totalPixels = 0;
for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol);
totalPixels += mCacheLines[i]->mMaxWidth;
}
remainingCapacity = (remainingCapacity * 100) / totalPixels;
return remainingCapacity;
}
void FontRenderer::precacheLatin(SkPaint* paint) {
// Remaining capacity is measured in %
uint32_t remainingCapacity = getRemainingCacheCapacity();
uint32_t precacheIdx = 0;
while(remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) {
mCurrentFont->getCachedUTFChar(paint, (int32_t)mLatinPrecache[precacheIdx]);
remainingCapacity = getRemainingCacheCapacity();
precacheIdx ++;
}
}
void FontRenderer::setFont(SkPaint* paint, uint32_t fontId, float fontSize) {
uint32_t currentNumFonts = mActiveFonts.size();
mCurrentFont = Font::create(this, fontId, fontSize);
const float maxPrecacheFontSize = 40.0f;
bool isNewFont = currentNumFonts != mActiveFonts.size();
if(isNewFont && fontSize <= maxPrecacheFontSize ){
precacheLatin(paint);
}
}
void FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text,
uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y) {
checkInit();
if (!mCurrentFont) {
LOGE("No font set");
return;
}
mClip = clip;
mCurrentFont->renderUTF(paint, text, startIndex, len, numGlyphs, x, y);
if (mCurrentQuadIndex != 0) {
issueDrawCommand();
mCurrentQuadIndex = 0;
}
}
}; // namespace uirenderer
}; // namespace android
|