Skip to content

Commit 39d18f5

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Code cleaning: centralize use of #if USE_TEXT_LAYOUT_CACHE"
2 parents 11001c3 + a731b08 commit 39d18f5

File tree

6 files changed

+174
-206
lines changed

6 files changed

+174
-206
lines changed

core/jni/android/graphics/Canvas.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -757,20 +757,11 @@ class SkCanvasGlue {
757757
int start, int count, int contextCount,
758758
jfloat x, jfloat y, int flags, SkPaint* paint) {
759759

760-
sp<TextLayoutCacheValue> value;
761-
#if USE_TEXT_LAYOUT_CACHE
762-
value = TextLayoutCache::getInstance().getValue(paint, textArray, start, count,
763-
contextCount, flags);
760+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
761+
textArray, start, count, contextCount, flags);
764762
if (value == NULL) {
765-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
766-
String8(textArray + start, count).string());
767-
return ;
763+
return;
768764
}
769-
#else
770-
value = new TextLayoutCacheValue(contextCount);
771-
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
772-
reinterpret_cast<const UChar*>(textArray), start, count, contextCount, flags);
773-
#endif
774765
doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(), x, y, flags, paint);
775766
}
776767

core/jni/android/graphics/Paint.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,10 @@ class SkPaintGlue {
465465

466466
jchar* glyphsArray = env->GetCharArrayElements(glyphs, NULL);
467467

468-
TextLayoutCacheValue value(contextCount);
469-
TextLayoutEngine::getInstance().computeValues(&value, paint, text, start, count,
470-
contextCount, flags);
471-
const jchar* shapedGlyphs = value.getGlyphs();
472-
size_t glyphsCount = value.getGlyphsCount();
468+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
469+
text, start, count, contextCount, flags);
470+
const jchar* shapedGlyphs = value->getGlyphs();
471+
size_t glyphsCount = value->getGlyphsCount();
473472
memcpy(glyphsArray, shapedGlyphs, sizeof(jchar) * glyphsCount);
474473

475474
env->ReleaseCharArrayElements(glyphs, glyphsArray, JNI_ABORT);
@@ -677,20 +676,11 @@ class SkPaintGlue {
677676
static int breakText(JNIEnv* env, SkPaint& paint, const jchar text[],
678677
int count, float maxWidth, jfloatArray jmeasured,
679678
SkPaint::TextBufferDirection tbd) {
680-
sp<TextLayoutCacheValue> value;
681-
#if USE_TEXT_LAYOUT_CACHE
682-
value = TextLayoutCache::getInstance().getValue(&paint, text, 0, count,
683-
count, paint.getFlags());
679+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
680+
text, 0, count, count, paint.getFlags());
684681
if (value == NULL) {
685-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
686-
String8(text, count).string());
682+
return 0;
687683
}
688-
#else
689-
value = new TextLayoutCacheValue(count);
690-
TextLayoutEngine::getInstance().computeValues(value.get(), &paint,
691-
reinterpret_cast<const UChar*>(text), 0, count, count, paint.getFlags());
692-
#endif
693-
694684
SkScalar measured;
695685
size_t bytes = paint.breakText(value->getGlyphs(), value->getGlyphsCount() << 1,
696686
SkFloatToScalar(maxWidth), &measured, tbd);
@@ -756,19 +746,11 @@ class SkPaintGlue {
756746
SkRect r;
757747
SkIRect ir;
758748

759-
sp<TextLayoutCacheValue> value;
760-
#if USE_TEXT_LAYOUT_CACHE
761-
value = TextLayoutCache::getInstance().getValue(&paint, text, 0, count,
762-
count, paint.getFlags());
749+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
750+
text, 0, count, count, paint.getFlags());
763751
if (value == NULL) {
764-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
765-
String8(text, count).string());
752+
return;
766753
}
767-
#else
768-
value = new TextLayoutCacheValue(count);
769-
TextLayoutEngine::getInstance().computeValues(value.get(), &paint,
770-
reinterpret_cast<const UChar*>(text), 0, count, count, paint.getFlags());
771-
#endif
772754
paint.measureText(value->getGlyphs(), value->getGlyphsCount() << 1, &r);
773755
r.roundOut(&ir);
774756
GraphicsJNI::irect_to_jrect(ir, env, bounds);

core/jni/android/graphics/TextLayout.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,9 @@ bool TextLayout::needsLayout(const jchar* text, jint len, jint bidiFlags) {
5353
// a path representing the text that would have been drawn.
5454
void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len,
5555
jint bidiFlags, jfloat x, jfloat y, SkPath *path) {
56-
sp<TextLayoutCacheValue> value;
57-
#if USE_TEXT_LAYOUT_CACHE
58-
// Return advances from the cache. Compute them if needed
59-
value = TextLayoutCache::getInstance().getValue(paint, text, 0, len,
60-
len, bidiFlags);
61-
#else
62-
value = new TextLayoutCacheValue(len);
63-
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
64-
reinterpret_cast<const UChar*>(text), 0, len, len, bidiFlags);
65-
#endif
56+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
57+
text, 0, len, len, bidiFlags);
6658
if (value == NULL) {
67-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
68-
String8(text, len).string());
6959
return ;
7060
}
7161
SkScalar x_ = SkFloatToScalar(x);
@@ -77,19 +67,9 @@ void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len,
7767
void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
7868
jint count, jint contextCount, jint dirFlags,
7969
jfloat* resultAdvances, jfloat* resultTotalAdvance) {
80-
sp<TextLayoutCacheValue> value;
81-
#if USE_TEXT_LAYOUT_CACHE
82-
// Return advances from the cache. Compute them if needed
83-
value = TextLayoutCache::getInstance().getValue(paint, chars, start, count,
84-
contextCount, dirFlags);
85-
#else
86-
value = new TextLayoutCacheValue(contextCount);
87-
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
88-
reinterpret_cast<const UChar*>(chars), start, count, contextCount, dirFlags);
89-
#endif
70+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
71+
chars, start, count, contextCount, dirFlags);
9072
if (value == NULL) {
91-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
92-
String8(chars + start, count).string());
9373
return ;
9474
}
9575
if (resultAdvances) {
@@ -126,20 +106,12 @@ void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count,
126106
return;
127107
}
128108

129-
sp<TextLayoutCacheValue> value;
130-
#if USE_TEXT_LAYOUT_CACHE
131-
value = TextLayoutCache::getInstance().getValue(paint, text, 0, count,
132-
count, bidiFlags);
133-
#else
134-
value = new TextLayoutCacheValue(count);
135-
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
136-
reinterpret_cast<const UChar*>(text), 0, count, count, bidiFlags);
137-
#endif
109+
sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
110+
text, 0, count, count, bidiFlags);
138111
if (value == NULL) {
139-
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
140-
String8(text, count).string());
141-
return ;
112+
return;
142113
}
114+
143115
// Beware: this needs Glyph encoding (already done on the Paint constructor)
144116
canvas->drawTextOnPathHV(value->getGlyphs(), value->getGlyphsCount() * 2, *path, h_, v_, *paint);
145117
}

core/jni/android/graphics/TextLayoutCache.cpp

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,13 @@ namespace android {
3535
#define TYPEFACE_BENGALI "/system/fonts/Lohit-Bengali.ttf"
3636
#define TYPEFACE_THAI "/system/fonts/DroidSansThai.ttf"
3737

38-
#if USE_TEXT_LAYOUT_CACHE
39-
40-
ANDROID_SINGLETON_STATIC_INSTANCE(TextLayoutCache);
41-
42-
#endif
43-
44-
ANDROID_SINGLETON_STATIC_INSTANCE(TextLayoutEngine);
38+
ANDROID_SINGLETON_STATIC_INSTANCE(TextLayoutEngine);
4539

4640
//--------------------------------------------------------------------------------------------------
4741

48-
TextLayoutCache::TextLayoutCache() :
49-
mCache(GenerationCache<TextLayoutCacheKey, sp<TextLayoutCacheValue> >::kUnlimitedCapacity),
42+
TextLayoutCache::TextLayoutCache(TextLayoutShaper* shaper) :
43+
mShaper(shaper),
44+
mCache(GenerationCache<TextLayoutCacheKey, sp<TextLayoutValue> >::kUnlimitedCapacity),
5045
mSize(0), mMaxSize(MB(DEFAULT_TEXT_LAYOUT_CACHE_SIZE_IN_MB)),
5146
mCacheHitCount(0), mNanosecondsSaved(0) {
5247
init();
@@ -75,7 +70,7 @@ void TextLayoutCache::init() {
7570
/**
7671
* Callbacks
7772
*/
78-
void TextLayoutCache::operator()(TextLayoutCacheKey& text, sp<TextLayoutCacheValue>& desc) {
73+
void TextLayoutCache::operator()(TextLayoutCacheKey& text, sp<TextLayoutValue>& desc) {
7974
size_t totalSizeToDelete = text.getSize() + desc->getSize();
8075
mSize -= totalSizeToDelete;
8176
if (mDebugEnabled) {
@@ -93,7 +88,7 @@ void TextLayoutCache::clear() {
9388
/*
9489
* Caching
9590
*/
96-
sp<TextLayoutCacheValue> TextLayoutCache::getValue(const SkPaint* paint,
91+
sp<TextLayoutValue> TextLayoutCache::getValue(const SkPaint* paint,
9792
const jchar* text, jint start, jint count, jint contextCount, jint dirFlags) {
9893
AutoMutex _l(mLock);
9994
nsecs_t startTime = 0;
@@ -105,18 +100,18 @@ sp<TextLayoutCacheValue> TextLayoutCache::getValue(const SkPaint* paint,
105100
TextLayoutCacheKey key(paint, text, start, count, contextCount, dirFlags);
106101

107102
// Get value from cache if possible
108-
sp<TextLayoutCacheValue> value = mCache.get(key);
103+
sp<TextLayoutValue> value = mCache.get(key);
109104

110105
// Value not found for the key, we need to add a new value in the cache
111106
if (value == NULL) {
112107
if (mDebugEnabled) {
113108
startTime = systemTime(SYSTEM_TIME_MONOTONIC);
114109
}
115110

116-
value = new TextLayoutCacheValue(contextCount);
111+
value = new TextLayoutValue(contextCount);
117112

118113
// Compute advances and store them
119-
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
114+
mShaper->computeValues(value.get(), paint,
120115
reinterpret_cast<const UChar*>(text), start, count,
121116
size_t(contextCount), int(dirFlags));
122117

@@ -312,31 +307,33 @@ size_t TextLayoutCacheKey::getSize() const {
312307
/**
313308
* TextLayoutCacheValue
314309
*/
315-
TextLayoutCacheValue::TextLayoutCacheValue(size_t contextCount) :
310+
TextLayoutValue::TextLayoutValue(size_t contextCount) :
316311
mTotalAdvance(0), mElapsedTime(0) {
317312
// Give a hint for advances and glyphs vectors size
318313
mAdvances.setCapacity(contextCount);
319314
mGlyphs.setCapacity(contextCount);
320315
}
321316

322-
size_t TextLayoutCacheValue::getSize() const {
323-
return sizeof(TextLayoutCacheValue) + sizeof(jfloat) * mAdvances.capacity() +
317+
size_t TextLayoutValue::getSize() const {
318+
return sizeof(TextLayoutValue) + sizeof(jfloat) * mAdvances.capacity() +
324319
sizeof(jchar) * mGlyphs.capacity();
325320
}
326321

327-
void TextLayoutCacheValue::setElapsedTime(uint32_t time) {
322+
void TextLayoutValue::setElapsedTime(uint32_t time) {
328323
mElapsedTime = time;
329324
}
330325

331-
uint32_t TextLayoutCacheValue::getElapsedTime() {
326+
uint32_t TextLayoutValue::getElapsedTime() {
332327
return mElapsedTime;
333328
}
334329

335-
TextLayoutEngine::TextLayoutEngine() : mShaperItemGlyphArraySize(0) {
330+
TextLayoutShaper::TextLayoutShaper() : mShaperItemGlyphArraySize(0) {
336331
mDefaultTypeface = SkFontHost::CreateTypeface(NULL, NULL, NULL, 0, SkTypeface::kNormal);
337332
mArabicTypeface = NULL;
338333
mHebrewRegularTypeface = NULL;
339334
mHebrewBoldTypeface = NULL;
335+
mBengaliTypeface = NULL;
336+
mThaiTypeface = NULL;
340337

341338
mFontRec.klass = &harfbuzzSkiaClass;
342339
mFontRec.userData = 0;
@@ -355,12 +352,17 @@ TextLayoutEngine::TextLayoutEngine() : mShaperItemGlyphArraySize(0) {
355352
mShaperItem.font->userData = &mShapingPaint;
356353
}
357354

358-
TextLayoutEngine::~TextLayoutEngine() {
359-
// FIXME should free fonts and caches but since this class is a singleton,
360-
// we don't bother at the moment
355+
TextLayoutShaper::~TextLayoutShaper() {
356+
SkSafeUnref(mDefaultTypeface);
357+
SkSafeUnref(mArabicTypeface);
358+
SkSafeUnref(mHebrewRegularTypeface);
359+
SkSafeUnref(mHebrewBoldTypeface);
360+
SkSafeUnref(mBengaliTypeface);
361+
SkSafeUnref(mThaiTypeface);
362+
deleteShaperItemGlyphArrays();
361363
}
362364

363-
void TextLayoutEngine::computeValues(TextLayoutCacheValue* value, const SkPaint* paint, const UChar* chars,
365+
void TextLayoutShaper::computeValues(TextLayoutValue* value, const SkPaint* paint, const UChar* chars,
364366
size_t start, size_t count, size_t contextCount, int dirFlags) {
365367

366368
computeValues(paint, chars, start, count, contextCount, dirFlags,
@@ -371,7 +373,7 @@ void TextLayoutEngine::computeValues(TextLayoutCacheValue* value, const SkPaint*
371373
#endif
372374
}
373375

374-
void TextLayoutEngine::computeValues(const SkPaint* paint, const UChar* chars,
376+
void TextLayoutShaper::computeValues(const SkPaint* paint, const UChar* chars,
375377
size_t start, size_t count, size_t contextCount, int dirFlags,
376378
Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
377379
Vector<jchar>* const outGlyphs) {
@@ -513,7 +515,7 @@ static void logGlyphs(HB_ShaperItem shaperItem) {
513515
}
514516
}
515517

516-
void TextLayoutEngine::computeRunValues(const SkPaint* paint, const UChar* chars,
518+
void TextLayoutShaper::computeRunValues(const SkPaint* paint, const UChar* chars,
517519
size_t count, bool isRTL,
518520
Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
519521
Vector<jchar>* const outGlyphs) {
@@ -719,7 +721,7 @@ void TextLayoutEngine::computeRunValues(const SkPaint* paint, const UChar* chars
719721
}
720722

721723

722-
size_t TextLayoutEngine::shapeFontRun(const SkPaint* paint, bool isRTL) {
724+
size_t TextLayoutShaper::shapeFontRun(const SkPaint* paint, bool isRTL) {
723725
// Reset kerning
724726
mShaperItem.kerning_applied = false;
725727

@@ -833,14 +835,14 @@ size_t TextLayoutEngine::shapeFontRun(const SkPaint* paint, bool isRTL) {
833835
return baseGlyphCount;
834836
}
835837

836-
void TextLayoutEngine::ensureShaperItemGlyphArrays(size_t size) {
838+
void TextLayoutShaper::ensureShaperItemGlyphArrays(size_t size) {
837839
if (size > mShaperItemGlyphArraySize) {
838840
deleteShaperItemGlyphArrays();
839841
createShaperItemGlyphArrays(size);
840842
}
841843
}
842844

843-
void TextLayoutEngine::createShaperItemGlyphArrays(size_t size) {
845+
void TextLayoutShaper::createShaperItemGlyphArrays(size_t size) {
844846
#if DEBUG_GLYPHS
845847
ALOGD("Creating Glyph Arrays with size = %d", size);
846848
#endif
@@ -858,15 +860,15 @@ void TextLayoutEngine::createShaperItemGlyphArrays(size_t size) {
858860
mShaperItem.log_clusters = new unsigned short[size];
859861
}
860862

861-
void TextLayoutEngine::deleteShaperItemGlyphArrays() {
863+
void TextLayoutShaper::deleteShaperItemGlyphArrays() {
862864
delete[] mShaperItem.glyphs;
863865
delete[] mShaperItem.attributes;
864866
delete[] mShaperItem.advances;
865867
delete[] mShaperItem.offsets;
866868
delete[] mShaperItem.log_clusters;
867869
}
868870

869-
SkTypeface* TextLayoutEngine::getCachedTypeface(SkTypeface** typeface, const char path[]) {
871+
SkTypeface* TextLayoutShaper::getCachedTypeface(SkTypeface** typeface, const char path[]) {
870872
if (!*typeface) {
871873
*typeface = SkTypeface::CreateFromFile(path);
872874
// CreateFromFile(path) can return NULL if the path is non existing
@@ -884,7 +886,7 @@ SkTypeface* TextLayoutEngine::getCachedTypeface(SkTypeface** typeface, const cha
884886
return *typeface;
885887
}
886888

887-
HB_Face TextLayoutEngine::getCachedHBFace(SkTypeface* typeface) {
889+
HB_Face TextLayoutShaper::getCachedHBFace(SkTypeface* typeface) {
888890
SkFontID fontId = typeface->uniqueID();
889891
ssize_t index = mCachedHBFaces.indexOfKey(fontId);
890892
if (index >= 0) {
@@ -900,4 +902,36 @@ HB_Face TextLayoutEngine::getCachedHBFace(SkTypeface* typeface) {
900902
return face;
901903
}
902904

905+
TextLayoutEngine::TextLayoutEngine() {
906+
mShaper = new TextLayoutShaper();
907+
#if USE_TEXT_LAYOUT_CACHE
908+
mTextLayoutCache = new TextLayoutCache(mShaper);
909+
#else
910+
mTextLayoutCache = NULL;
911+
#endif
912+
}
913+
914+
TextLayoutEngine::~TextLayoutEngine() {
915+
delete mTextLayoutCache;
916+
delete mShaper;
917+
}
918+
919+
sp<TextLayoutValue> TextLayoutEngine::getValue(const SkPaint* paint, const jchar* text,
920+
jint start, jint count, jint contextCount, jint dirFlags) {
921+
sp<TextLayoutValue> value;
922+
#if USE_TEXT_LAYOUT_CACHE
923+
value = mTextLayoutCache->getValue(paint, text, start, count,
924+
contextCount, dirFlags);
925+
if (value == NULL) {
926+
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
927+
String8(text + start, count).string());
928+
}
929+
#else
930+
value = new TextLayoutValue(count);
931+
mShaper->computeValues(value.get(), paint,
932+
reinterpret_cast<const UChar*>(text), start, count, contextCount, dirFlags);
933+
#endif
934+
return value;
935+
}
936+
903937
} // namespace android

0 commit comments

Comments
 (0)