Skip to content

Commit 9f443fb

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Fix bug #5274332 TextLayoutCache is having multiple instances"
2 parents aacbf91 + 163268b commit 9f443fb

File tree

6 files changed

+26
-32
lines changed

6 files changed

+26
-32
lines changed

core/jni/android/graphics/Canvas.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ class SkCanvasGlue {
760760
jint count = end - start;
761761
sp<TextLayoutCacheValue> value;
762762
#if USE_TEXT_LAYOUT_CACHE
763-
value = gTextLayoutCache.getValue(paint, textArray, start, count, end, flags);
763+
value = TextLayoutCache::getInstance().getValue(paint, textArray, start, count,
764+
end, flags);
764765
if (value == NULL) {
765766
LOGE("Cannot get TextLayoutCache value");
766767
return ;
@@ -780,7 +781,8 @@ class SkCanvasGlue {
780781

781782
sp<TextLayoutCacheValue> value;
782783
#if USE_TEXT_LAYOUT_CACHE
783-
value = gTextLayoutCache.getValue(paint, textArray, start, count, contextCount, flags);
784+
value = TextLayoutCache::getInstance().getValue(paint, textArray, start, count,
785+
contextCount, flags);
784786
if (value == NULL) {
785787
LOGE("Cannot get TextLayoutCache value");
786788
return ;

core/jni/android/graphics/TextLayout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint sta
257257
sp<TextLayoutCacheValue> value;
258258
#if USE_TEXT_LAYOUT_CACHE
259259
// Return advances from the cache. Compute them if needed
260-
value = gTextLayoutCache.getValue(
260+
value = TextLayoutCache::getInstance().getValue(
261261
paint, chars, start, count, contextCount, dirFlags);
262262
#else
263263
value = new TextLayoutCacheValue();

core/jni/android/graphics/TextLayout.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ namespace android {
4141
*/
4242
#define USE_TEXT_LAYOUT_CACHE 1
4343

44-
45-
#if USE_TEXT_LAYOUT_CACHE
46-
static TextLayoutCache gTextLayoutCache;
47-
#endif
48-
4944
enum {
5045
kBidi_LTR = 0,
5146
kBidi_RTL = 1,

core/jni/android/graphics/TextLayoutCache.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#define LOG_TAG "TextLayoutCache"
18+
1719
#include "TextLayoutCache.h"
1820
#include "TextLayout.h"
1921

@@ -23,20 +25,19 @@ extern "C" {
2325

2426
namespace android {
2527

28+
//--------------------------------------------------------------------------------------------------
29+
#if USE_TEXT_LAYOUT_CACHE
30+
ANDROID_SINGLETON_STATIC_INSTANCE(TextLayoutCache);
31+
#endif
32+
//--------------------------------------------------------------------------------------------------
33+
2634
TextLayoutCache::TextLayoutCache() :
2735
mCache(GenerationCache<TextLayoutCacheKey, sp<TextLayoutCacheValue> >::kUnlimitedCapacity),
2836
mSize(0), mMaxSize(MB(DEFAULT_TEXT_LAYOUT_CACHE_SIZE_IN_MB)),
2937
mCacheHitCount(0), mNanosecondsSaved(0) {
3038
init();
3139
}
3240

33-
TextLayoutCache::TextLayoutCache(uint32_t max):
34-
mCache(GenerationCache<TextLayoutCacheKey, sp<TextLayoutCacheValue> >::kUnlimitedCapacity),
35-
mSize(0), mMaxSize(max),
36-
mCacheHitCount(0), mNanosecondsSaved(0) {
37-
init();
38-
}
39-
4041
TextLayoutCache::~TextLayoutCache() {
4142
mCache.clear();
4243
}
@@ -46,25 +47,21 @@ void TextLayoutCache::init() {
4647

4748
mDebugLevel = readRtlDebugLevel();
4849
mDebugEnabled = mDebugLevel & kRtlDebugCaches;
49-
LOGD("Using TextLayoutCache debug level: %d - Debug Enabled: %d", mDebugLevel, mDebugEnabled);
50+
LOGD("Using debug level: %d - Debug Enabled: %d", mDebugLevel, mDebugEnabled);
5051

5152
mCacheStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
52-
if (mDebugEnabled) {
53-
LOGD("TextLayoutCache start time: %lld", mCacheStartTime);
54-
}
55-
mInitialized = true;
5653

5754
if (mDebugEnabled) {
55+
LOGD("Start time: %lld", mCacheStartTime);
5856
#if RTL_USE_HARFBUZZ
59-
LOGD("TextLayoutCache is using HARFBUZZ");
57+
LOGD("Using HARFBUZZ");
6058
#else
61-
LOGD("TextLayoutCache is using ICU");
59+
LOGD("Using ICU");
6260
#endif
61+
LOGD("Initialization is done");
6362
}
6463

65-
if (mDebugEnabled) {
66-
LOGD("TextLayoutCache initialization is done");
67-
}
64+
mInitialized = true;
6865
}
6966

7067
/*
@@ -147,8 +144,7 @@ sp<TextLayoutCacheValue> TextLayoutCache::getValue(SkPaint* paint,
147144
// Cleanup to make some room if needed
148145
if (mSize + size > mMaxSize) {
149146
if (mDebugEnabled) {
150-
LOGD("TextLayoutCache: need to clean some entries "
151-
"for making some room for a new entry");
147+
LOGD("Need to clean some entries for making some room for a new entry");
152148
}
153149
while (mSize + size > mMaxSize) {
154150
// This will call the callback
@@ -213,7 +209,7 @@ void TextLayoutCache::dumpCacheStats() {
213209
float remainingPercent = 100 * ((mMaxSize - mSize) / ((float)mMaxSize));
214210
float timeRunningInSec = (systemTime(SYSTEM_TIME_MONOTONIC) - mCacheStartTime) / 1000000000;
215211
LOGD("------------------------------------------------");
216-
LOGD("TextLayoutCache stats");
212+
LOGD("Cache stats");
217213
LOGD("------------------------------------------------");
218214
LOGD("pid : %d", getpid());
219215
LOGD("running : %.0f seconds", timeRunningInSec);

core/jni/android/graphics/TextLayoutCache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <utils/GenerationCache.h>
2626
#include <utils/Compare.h>
2727
#include <utils/RefBase.h>
28+
#include <utils/Singleton.h>
2829

2930
#include <SkPaint.h>
3031
#include <SkTemplates.h>
@@ -187,11 +188,11 @@ class TextLayoutCacheValue : public RefBase {
187188
/**
188189
* Cache of text layout information.
189190
*/
190-
class TextLayoutCache : public OnEntryRemoved<TextLayoutCacheKey, sp<TextLayoutCacheValue> >
191+
class TextLayoutCache : public OnEntryRemoved<TextLayoutCacheKey, sp<TextLayoutCacheValue> >,
192+
public Singleton<TextLayoutCache>
191193
{
192194
public:
193195
TextLayoutCache();
194-
TextLayoutCache(uint32_t maxByteSize);
195196

196197
virtual ~TextLayoutCache();
197198

core/jni/android_view_GLES20Canvas.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ static void renderText(OpenGLRenderer* renderer, const jchar* text, int count,
477477
#if RTL_USE_HARFBUZZ
478478
sp<TextLayoutCacheValue> value;
479479
#if USE_TEXT_LAYOUT_CACHE
480-
value = gTextLayoutCache.getValue(paint, text, 0, count, count, flags);
480+
value = TextLayoutCache::getInstance().getValue(paint, text, 0, count, count, flags);
481481
if (value == NULL) {
482482
LOGE("Cannot get TextLayoutCache value");
483483
return ;
@@ -507,7 +507,7 @@ static void renderTextRun(OpenGLRenderer* renderer, const jchar* text,
507507
#if RTL_USE_HARFBUZZ
508508
sp<TextLayoutCacheValue> value;
509509
#if USE_TEXT_LAYOUT_CACHE
510-
value = gTextLayoutCache.getValue(paint, text, start, count, contextCount, flags);
510+
value = TextLayoutCache::getInstance().getValue(paint, text, start, count, contextCount, flags);
511511
if (value == NULL) {
512512
LOGE("Cannot get TextLayoutCache value");
513513
return ;

0 commit comments

Comments
 (0)