Fix bug #5332081 TextLayoutCache needs to be able to have more cache hits

- makes TextLayoutCache not carring about start/count. Basically he will cache the result for
the full string and gives back the "chunk" corresponding to start/count
- changed the TextLayoutCacheValue API to take start/count parameters
- add Harfbuzz LogClusters in TextLayoutCacheValue as it is needed for extracting the start/count "chunk"

Change-Id: I4b38a4442428606de9a093303bbbe98181e1f89c
diff --git a/core/jni/android/graphics/Canvas.cpp b/core/jni/android/graphics/Canvas.cpp
index 9313d0a..02c5bb3 100644
--- a/core/jni/android/graphics/Canvas.cpp
+++ b/core/jni/android/graphics/Canvas.cpp
@@ -758,21 +758,7 @@
             jfloat x, jfloat y, int flags, SkPaint* paint) {
 
         jint count = end - start;
-        sp<TextLayoutCacheValue> value;
-#if USE_TEXT_LAYOUT_CACHE
-        value = TextLayoutCache::getInstance().getValue(paint, textArray, start, count,
-                        end, flags);
-        if (value == NULL) {
-            LOGE("Cannot get TextLayoutCache value");
-            return ;
-        }
-#else
-        value = new TextLayoutCacheValue();
-        value->computeValues(paint, textArray, start, count, end, flags);
-#endif
-
-        doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(),
-            x, y, flags, paint);
+        drawTextWithGlyphs(canvas, textArray + start, 0, count, count, x, y, flags, paint);
     }
 
     static void drawTextWithGlyphs(SkCanvas* canvas, const jchar* textArray,
@@ -781,19 +767,20 @@
 
         sp<TextLayoutCacheValue> value;
 #if USE_TEXT_LAYOUT_CACHE
-        value = TextLayoutCache::getInstance().getValue(paint, textArray, start, count,
-                        contextCount, flags);
+        value = TextLayoutCache::getInstance().getValue(paint, textArray, contextCount, flags);
         if (value == NULL) {
             LOGE("Cannot get TextLayoutCache value");
             return ;
         }
 #else
         value = new TextLayoutCacheValue();
-        value->computeValues(paint, textArray, start, count, contextCount, flags);
+        value->computeValues(paint, textArray, contextCount, flags);
 #endif
-
-        doDrawGlyphs(canvas, value->getGlyphs(), 0, value->getGlyphsCount(),
-                x, y, flags, paint);
+        size_t startIndex = 0;
+        size_t glyphsCount = 0;
+        value->getGlyphsIndexAndCount(start, count, &startIndex, &glyphsCount);
+        const jchar* glyphs = value->getGlyphs(startIndex, glyphsCount);
+        doDrawGlyphs(canvas, glyphs, 0, glyphsCount, x, y, flags, paint);
     }
 
     static void doDrawGlyphs(SkCanvas* canvas, const jchar* glyphArray, int index, int count,
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 7d222f6..3daf7d0 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -352,7 +352,7 @@
         jfloat result = 0;
 #if RTL_USE_HARFBUZZ
         TextLayout::getTextRunAdvances(paint, textArray, index, count, textLength,
-                paint->getFlags(), NULL /* dont need all advances */, result);
+                paint->getFlags(), NULL /* dont need all advances */, &result);
 #else
         // we double count, since measureText wants a byteLength
         SkScalar width = paint->measureText(textArray + index, count << 1);
@@ -382,7 +382,7 @@
 
 #if RTL_USE_HARFBUZZ
         TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
-                paint->getFlags(), NULL /* dont need all advances */, width);
+                paint->getFlags(), NULL /* dont need all advances */, &width);
 #else
 
         width = SkScalarToFloat(paint->measureText(textArray + start, count << 1));
@@ -406,7 +406,7 @@
 
 #if RTL_USE_HARFBUZZ
         TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength,
-                paint->getFlags(), NULL /* dont need all advances */, width);
+                paint->getFlags(), NULL /* dont need all advances */, &width);
 #else
         width = SkScalarToFloat(paint->measureText(textArray, textLength << 1));
 #endif
@@ -435,10 +435,8 @@
         jfloat* widthsArray = autoWidths.ptr();
 
 #if RTL_USE_HARFBUZZ
-        jfloat totalAdvance;
-
         TextLayout::getTextRunAdvances(paint, text, 0, count, count,
-                paint->getFlags(), widthsArray, totalAdvance);
+                paint->getFlags(), widthsArray, NULL /* dont need totalAdvance */);
 #else
         SkScalar* scalarArray = (SkScalar*)widthsArray;
 
@@ -533,7 +531,7 @@
         jfloat totalAdvance = 0;
 
         TextLayout::getTextRunAdvances(paint, text, start, count, contextCount, flags,
-                                       advancesArray, totalAdvance);
+                                       advancesArray, &totalAdvance);
 
         if (advances != NULL) {
             env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
@@ -604,10 +602,9 @@
             jint count, jint flags, jint offset, jint opt) {
 #if RTL_USE_HARFBUZZ
         jfloat scalarArray[count];
-        jfloat totalAdvance = 0;
 
         TextLayout::getTextRunAdvances(paint, text, start, count, count, flags,
-                scalarArray, totalAdvance);
+                scalarArray, NULL /* dont need totalAdvance */);
 #else
         SkScalar scalarArray[count];
         jchar buffer[count];
diff --git a/core/jni/android/graphics/TextLayout.cpp b/core/jni/android/graphics/TextLayout.cpp
index a2d6efb..ae17aed 100644
--- a/core/jni/android/graphics/TextLayout.cpp
+++ b/core/jni/android/graphics/TextLayout.cpp
@@ -253,21 +253,22 @@
 
 void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
                                     jint count, jint contextCount, jint dirFlags,
-                                    jfloat* resultAdvances, jfloat& resultTotalAdvance) {
+                                    jfloat* resultAdvances, jfloat* resultTotalAdvance) {
     sp<TextLayoutCacheValue> value;
 #if USE_TEXT_LAYOUT_CACHE
     // Return advances from the cache. Compute them if needed
-    value = TextLayoutCache::getInstance().getValue(
-            paint, chars, start, count, contextCount, dirFlags);
+    value = TextLayoutCache::getInstance().getValue(paint, chars, contextCount, dirFlags);
 #else
     value = new TextLayoutCacheValue();
-    value->computeValues(paint, chars, start, count, contextCount, dirFlags);
+    value->computeValues(paint, chars, contextCount, dirFlags);
 #endif
     if (value != NULL) {
         if (resultAdvances != NULL) {
-            memcpy(resultAdvances, value->getAdvances(), value->getAdvancesCount() * sizeof(jfloat));
+            value->getAdvances(start, count, resultAdvances);
         }
-        resultTotalAdvance = value->getTotalAdvance();
+        if (resultTotalAdvance) {
+            *resultTotalAdvance = value->getTotalAdvance(start, count);
+        }
     }
 }
 
diff --git a/core/jni/android/graphics/TextLayout.h b/core/jni/android/graphics/TextLayout.h
index 9d8913c..9df3829 100644
--- a/core/jni/android/graphics/TextLayout.h
+++ b/core/jni/android/graphics/TextLayout.h
@@ -71,7 +71,7 @@
 
     static void getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
                                    jint count, jint contextCount, jint dirFlags,
-                                   jfloat* resultAdvances, jfloat& resultTotalAdvance);
+                                   jfloat* resultAdvances, jfloat* resultTotalAdvance);
 
     static void getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint start,
                                    jint count, jint contextCount, jint dirFlags,
diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp
index e4eeec8..c045c07 100644
--- a/core/jni/android/graphics/TextLayoutCache.cpp
+++ b/core/jni/android/graphics/TextLayoutCache.cpp
@@ -106,7 +106,7 @@
  * Caching
  */
 sp<TextLayoutCacheValue> TextLayoutCache::getValue(SkPaint* paint,
-            const jchar* text, jint start, jint count, jint contextCount, jint dirFlags) {
+            const jchar* text, jint count, jint dirFlags) {
     AutoMutex _l(mLock);
     nsecs_t startTime = 0;
     if (mDebugEnabled) {
@@ -114,7 +114,7 @@
     }
 
     // Create the key
-    TextLayoutCacheKey key(paint, text, start, count, contextCount, dirFlags);
+    TextLayoutCacheKey key(paint, text, count, dirFlags);
 
     // Get value from cache if possible
     sp<TextLayoutCacheValue> value = mCache.get(key);
@@ -128,7 +128,7 @@
         value = new TextLayoutCacheValue();
 
         // Compute advances and store them
-        value->computeValues(paint, text, start, count, contextCount, dirFlags);
+        value->computeValues(paint, text, count, dirFlags);
 
         nsecs_t endTime = systemTime(SYSTEM_TIME_MONOTONIC);
 
@@ -157,20 +157,20 @@
                 // Update timing information for statistics
                 value->setElapsedTime(endTime - startTime);
 
-                LOGD("CACHE MISS: Added entry with start=%d, count=%d, "
-                        "contextCount=%d, entry size %d bytes, remaining space %d bytes"
+                LOGD("CACHE MISS: Added entry with "
+                        "count=%d, entry size %d bytes, remaining space %d bytes"
                         " - Compute time in nanos: %d - Text='%s' ",
-                        start, count, contextCount, size, mMaxSize - mSize, value->getElapsedTime(),
-                        String8(text, contextCount).string());
+                        count, size, mMaxSize - mSize, value->getElapsedTime(),
+                        String8(text, count).string());
             }
         } else {
             if (mDebugEnabled) {
                 LOGD("CACHE MISS: Calculated but not storing entry because it is too big "
-                        "with start=%d, count=%d, contextCount=%d, "
+                        "with count=%d, "
                         "entry size %d bytes, remaining space %d bytes"
                         " - Compute time in nanos: %lld - Text='%s'",
-                        start, count, contextCount, size, mMaxSize - mSize, endTime,
-                        String8(text, contextCount).string());
+                        count, size, mMaxSize - mSize, endTime,
+                        String8(text, count).string());
             }
             value.clear();
         }
@@ -184,12 +184,12 @@
             if (value->getElapsedTime() > 0) {
                 float deltaPercent = 100 * ((value->getElapsedTime() - elapsedTimeThruCacheGet)
                         / ((float)value->getElapsedTime()));
-                LOGD("CACHE HIT #%d with start=%d, count=%d, contextCount=%d "
+                LOGD("CACHE HIT #%d with count=%d "
                         "- Compute time in nanos: %d - "
                         "Cache get time in nanos: %lld - Gain in percent: %2.2f - Text='%s' ",
-                        mCacheHitCount, start, count, contextCount,
+                        mCacheHitCount, count,
                         value->getElapsedTime(), elapsedTimeThruCacheGet, deltaPercent,
-                        String8(text, contextCount).string());
+                        String8(text, count).string());
             }
             if (mCacheHitCount % DEFAULT_DUMP_STATS_CACHE_HIT_INTERVAL == 0) {
                 dumpCacheStats();
@@ -218,15 +218,14 @@
 /**
  * TextLayoutCacheKey
  */
-TextLayoutCacheKey::TextLayoutCacheKey(): text(NULL), start(0), count(0), contextCount(0),
+TextLayoutCacheKey::TextLayoutCacheKey(): text(NULL), count(0),
         dirFlags(0), typeface(NULL), textSize(0), textSkewX(0), textScaleX(0), flags(0),
         hinting(SkPaint::kNo_Hinting)  {
 }
 
 TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint,
-        const UChar* text, size_t start, size_t count,
-        size_t contextCount, int dirFlags) :
-            text(text), start(start), count(count), contextCount(contextCount),
+        const UChar* text, size_t count, int dirFlags) :
+            text(text), count(count),
             dirFlags(dirFlags) {
     typeface = paint->getTypeface();
     textSize = paint->getTextSize();
@@ -239,9 +238,7 @@
 TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) :
         text(NULL),
         textCopy(other.textCopy),
-        start(other.start),
         count(other.count),
-        contextCount(other.contextCount),
         dirFlags(other.dirFlags),
         typeface(other.typeface),
         textSize(other.textSize),
@@ -256,18 +253,15 @@
 
 bool TextLayoutCacheKey::operator<(const TextLayoutCacheKey& rhs) const {
     LTE_INT(count) {
-        LTE_INT(contextCount) {
-            LTE_INT(start) {
-                LTE_INT(typeface) {
-                    LTE_FLOAT(textSize) {
-                        LTE_FLOAT(textSkewX) {
-                            LTE_FLOAT(textScaleX) {
-                                LTE_INT(flags) {
-                                    LTE_INT(hinting) {
-                                        LTE_INT(dirFlags) {
-                                            return strncmp16(getText(), rhs.getText(), contextCount) < 0;
-                                        }
-                                    }
+        LTE_INT(typeface) {
+            LTE_FLOAT(textSize) {
+                LTE_FLOAT(textSkewX) {
+                    LTE_FLOAT(textScaleX) {
+                        LTE_INT(flags) {
+                            LTE_INT(hinting) {
+                                LTE_INT(dirFlags) {
+                                    return memcmp(getText(), rhs.getText(),
+                                            count * sizeof(UChar)) < 0;
                                 }
                             }
                         }
@@ -280,12 +274,12 @@
 }
 
 void TextLayoutCacheKey::internalTextCopy() {
-    textCopy.setTo(text, contextCount);
+    textCopy.setTo(text, count);
     text = NULL;
 }
 
 size_t TextLayoutCacheKey::getSize() {
-    return sizeof(TextLayoutCacheKey) + sizeof(UChar) * contextCount;
+    return sizeof(TextLayoutCacheKey) + sizeof(UChar) * count;
 }
 
 /**
@@ -303,23 +297,23 @@
     return mElapsedTime;
 }
 
-void TextLayoutCacheValue::computeValues(SkPaint* paint, const UChar* chars, size_t start,
-        size_t count, size_t contextCount, int dirFlags) {
-    // Give a hint for advances and glyphs vectors size
-    mAdvances.setCapacity(count);
-    mGlyphs.setCapacity(count);
-    computeValuesWithHarfbuzz(paint, chars, start, count, contextCount, dirFlags,
-            &mAdvances, &mTotalAdvance, &mGlyphs);
+void TextLayoutCacheValue::computeValues(SkPaint* paint, const UChar* chars,
+        size_t contextCount, int dirFlags) {
+    // Give a hint for advances, glyphs and log clusters vectors size
+    mAdvances.setCapacity(contextCount);
+    mGlyphs.setCapacity(contextCount);
+    mLogClusters.setCapacity(contextCount);
+
+    computeValuesWithHarfbuzz(paint, chars, contextCount, dirFlags,
+            &mAdvances, &mTotalAdvance, &mGlyphs, &mLogClusters);
 #if DEBUG_ADVANCES
-    LOGD("Advances - count=%d - countextCount=%d - totalAdvance=%f - "
-            "adv[0]=%f adv[1]=%f adv[2]=%f adv[3]=%f", count, contextCount, mTotalAdvance,
-            mAdvances[0], mAdvances[1], mAdvances[2], mAdvances[3]);
+    LOGD("Advances - countextCount=%d - totalAdvance=%f", contextCount, mTotalAdvance);
 #endif
 }
 
 size_t TextLayoutCacheValue::getSize() {
     return sizeof(TextLayoutCacheValue) + sizeof(jfloat) * mAdvances.capacity() +
-            sizeof(jchar) * mGlyphs.capacity();
+            sizeof(jchar) * mGlyphs.capacity() + sizeof(unsigned short) * mLogClusters.capacity();
 }
 
 void TextLayoutCacheValue::setupShaperItem(HB_ShaperItem* shaperItem, HB_FontRec* font,
@@ -391,9 +385,9 @@
 }
 
 void TextLayoutCacheValue::computeValuesWithHarfbuzz(SkPaint* paint, const UChar* chars,
-        size_t start, size_t count, size_t contextCount, int dirFlags,
+        size_t contextCount, int dirFlags,
         Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
-        Vector<jchar>* const outGlyphs) {
+        Vector<jchar>* const outGlyphs, Vector<unsigned short>* const outLogClusters) {
 
         UBiDiLevel bidiReq = 0;
         bool forceLTR = false;
@@ -413,8 +407,8 @@
                     LOGD("computeValuesWithHarfbuzz -- forcing run with LTR=%d RTL=%d",
                             forceLTR, forceRTL);
 #endif
-            computeRunValuesWithHarfbuzz(paint, chars, start, count, contextCount, forceRTL,
-                    outAdvances, outTotalAdvance, outGlyphs);
+            computeRunValuesWithHarfbuzz(paint, chars, 0, contextCount, contextCount, forceRTL,
+                    outAdvances, outTotalAdvance, outGlyphs, outLogClusters);
         } else {
             UBiDi* bidi = ubidi_open();
             if (bidi) {
@@ -433,35 +427,16 @@
                         bool isRTL = (paraDir == 1);
 #if DEBUG_GLYPHS
                         LOGD("computeValuesWithHarfbuzz -- processing SINGLE run "
-                                "-- run-start=%d run-len=%d isRTL=%d", start, count, isRTL);
+                                "-- run-start=%d run-len=%d isRTL=%d", 0, contextCount, isRTL);
 #endif
-                        computeRunValuesWithHarfbuzz(paint, chars, start, count, contextCount,
-                                isRTL, outAdvances, outTotalAdvance, outGlyphs);
+                        computeRunValuesWithHarfbuzz(paint, chars, 0, contextCount, contextCount,
+                                isRTL, outAdvances, outTotalAdvance, outGlyphs, outLogClusters);
                     } else {
-                        int32_t end = start + count;
                         for (size_t i = 0; i < rc; ++i) {
                             int32_t startRun;
                             int32_t lengthRun;
                             UBiDiDirection runDir = ubidi_getVisualRun(bidi, i, &startRun, &lengthRun);
 
-                            if (startRun >= end) {
-                              break;
-                            }
-
-                            int32_t endRun = startRun + lengthRun;
-                            if (endRun <= start) {
-                              continue;
-                            }
-
-                            if (startRun < start) {
-                              startRun = start;
-                            }
-                            if (endRun > end) {
-                              endRun = end;
-                            }
-
-                            lengthRun = endRun - startRun;
-
                             bool isRTL = (runDir == UBIDI_RTL);
                             jfloat runTotalAdvance = 0;
 #if DEBUG_GLYPHS
@@ -471,7 +446,7 @@
                             computeRunValuesWithHarfbuzz(paint, chars, startRun,
                                     lengthRun, contextCount, isRTL,
                                     outAdvances, &runTotalAdvance,
-                                    outGlyphs);
+                                    outGlyphs, outLogClusters);
 
                             *outTotalAdvance += runTotalAdvance;
                         }
@@ -483,10 +458,10 @@
                 bool isRTL = (bidiReq = 1) || (bidiReq = UBIDI_DEFAULT_RTL);
 #if DEBUG_GLYPHS
                 LOGD("computeValuesWithHarfbuzz -- cannot run BiDi, considering a SINGLE Run "
-                        "-- run-start=%d run-len=%d isRTL=%d", start, count, isRTL);
+                        "-- run-start=%d run-len=%d isRTL=%d", 0, contextCount, isRTL);
 #endif
-                computeRunValuesWithHarfbuzz(paint, chars, start, count, contextCount, isRTL,
-                        outAdvances, outTotalAdvance, outGlyphs);
+                computeRunValuesWithHarfbuzz(paint, chars, 0, contextCount, contextCount, isRTL,
+                        outAdvances, outTotalAdvance, outGlyphs, outLogClusters);
             }
         }
 #if DEBUG_GLYPHS
@@ -506,7 +481,7 @@
 void TextLayoutCacheValue::computeRunValuesWithHarfbuzz(SkPaint* paint, const UChar* chars,
         size_t start, size_t count, size_t contextCount, bool isRTL,
         Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
-        Vector<jchar>* const outGlyphs) {
+        Vector<jchar>* const outGlyphs, Vector<unsigned short>* const outLogClusters) {
 
     HB_ShaperItem shaperItem;
     HB_FontRec font;
@@ -557,15 +532,15 @@
 #if DEBUG_ADVANCES
     for (size_t i = 0; i < count; i++) {
         LOGD("hb-adv[%d] = %f - log_clusters = %d - total = %f", i,
-                outAdvances[i], shaperItem.log_clusters[i], totalAdvance);
+                (*outAdvances)[i], shaperItem.log_clusters[i], totalAdvance);
     }
 #endif
 
     // Get Glyphs and reverse them in place if RTL
     if (outGlyphs) {
-        size_t count = shaperItem.num_glyphs;
-        for (size_t i = 0; i < count; i++) {
-            jchar glyph = (jchar) shaperItem.glyphs[(!isRTL) ? i : count - 1 - i];
+        size_t countGlyphs = shaperItem.num_glyphs;
+        for (size_t i = 0; i < countGlyphs; i++) {
+            jchar glyph = (jchar) shaperItem.glyphs[(!isRTL) ? i : countGlyphs - 1 - i];
 #if DEBUG_GLYPHS
             LOGD("HARFBUZZ  -- glyph[%d]=%d", i, glyph);
 #endif
@@ -573,6 +548,20 @@
         }
     }
 
+    // Get LogClusters
+    if (outLogClusters) {
+        size_t countLogClusters = outLogClusters->size();
+        size_t countGlyphs = shaperItem.num_glyphs;
+        for (size_t i = 0; i < countGlyphs; i++) {
+            // As there may be successive runs, we need to shift the log clusters
+            unsigned short logCluster = shaperItem.log_clusters[i] + countLogClusters;
+#if DEBUG_GLYPHS
+            LOGD("HARFBUZZ  -- logCluster[%d] relative=%d - absolute=%d", i, shaperItem.log_clusters[i], logCluster);
+#endif
+            outLogClusters->add(logCluster);
+        }
+    }
+
     // Cleaning
     deleteGlyphArrays(&shaperItem);
     HB_FreeFace(shaperItem.face);
@@ -603,5 +592,67 @@
     memset(shaperItem->offsets, 0, size * sizeof(shaperItem->offsets[0]));
 }
 
+void TextLayoutCacheValue::getAdvances(size_t start, size_t count, jfloat* outAdvances) const {
+    memcpy(outAdvances, mAdvances.array() + start, count * sizeof(jfloat));
+#if DEBUG_ADVANCES
+    LOGD("getAdvances - start=%d count=%d", start, count);
+    for (size_t i = 0; i < count; i++) {
+        LOGD("  adv[%d] = %f", i, outAdvances[i]);
+    }
+#endif
+}
+
+jfloat TextLayoutCacheValue::getTotalAdvance(size_t start, size_t count) const {
+    jfloat outTotalAdvance = 0;
+    for (size_t i = start; i < start + count; i++) {
+        outTotalAdvance += mAdvances[i];
+    }
+#if DEBUG_ADVANCES
+    LOGD("getTotalAdvance - start=%d count=%d - total=%f", start, count, outTotalAdvance);
+#endif
+     return outTotalAdvance;
+}
+
+void TextLayoutCacheValue::getGlyphsIndexAndCount(size_t start, size_t count, size_t* outStartIndex,
+        size_t* outGlyphsCount) const {
+    *outStartIndex = 0;
+    if (count == 0) {
+        *outGlyphsCount = 0;
+        return;
+    }
+    size_t endIndex = 0;
+    for(size_t i = 0; i < mGlyphs.size(); i++) {
+        if (mLogClusters[i] <= start) {
+            *outStartIndex = i;
+            endIndex = i;
+            continue;
+        }
+        if (mLogClusters[i] <= start + count) {
+            endIndex = i;
+        }
+    }
+    *outGlyphsCount = endIndex - *outStartIndex + 1;
+#if DEBUG_GLYPHS
+    LOGD("getGlyphsIndexes - start=%d count=%d - startIndex=%d count=%d", start, count,
+            *outStartIndex, *outGlyphsCount);
+    for(size_t i = 0; i < mGlyphs.size(); i++) {
+        LOGD("getGlyphs - all - glyph[%d] = %d", i, mGlyphs[i]);
+    }
+    for(size_t i = 0; i < mGlyphs.size(); i++) {
+        LOGD("getGlyphs - all - logcl[%d] = %d", i, mLogClusters[i]);
+    }
+#endif
+}
+
+const jchar* TextLayoutCacheValue::getGlyphs(size_t startIndex, size_t count) {
+    const jchar* glyphs = mGlyphs.array() + startIndex;
+#if DEBUG_GLYPHS
+    LOGD("getGlyphs - with startIndex = %d  count = %d", startIndex, count);
+    for (size_t i = 0; i < count; i++) {
+        LOGD("getGlyphs - result - glyph[%d] = %d", i, glyphs[i]);
+    }
+#endif
+    return glyphs;
+}
 
 } // namespace android
diff --git a/core/jni/android/graphics/TextLayoutCache.h b/core/jni/android/graphics/TextLayoutCache.h
index 1f08bda..35dd6fd 100644
--- a/core/jni/android/graphics/TextLayoutCache.h
+++ b/core/jni/android/graphics/TextLayoutCache.h
@@ -68,9 +68,7 @@
 public:
     TextLayoutCacheKey();
 
-    TextLayoutCacheKey(const SkPaint* paint,
-            const UChar* text, size_t start, size_t count,
-            size_t contextCount, int dirFlags);
+    TextLayoutCacheKey(const SkPaint* paint, const UChar* text, size_t count, int dirFlags);
 
     TextLayoutCacheKey(const TextLayoutCacheKey& other);
 
@@ -90,9 +88,7 @@
 private:
     const UChar* text; // if text is NULL, use textCopy
     String16 textCopy;
-    size_t start;
     size_t count;
-    size_t contextCount;
     int dirFlags;
     SkTypeface* typeface;
     SkScalar textSize;
@@ -116,14 +112,13 @@
     void setElapsedTime(uint32_t time);
     uint32_t getElapsedTime();
 
-    void computeValues(SkPaint* paint, const UChar* chars, size_t start, size_t count,
-            size_t contextCount, int dirFlags);
+    void computeValues(SkPaint* paint, const UChar* chars, size_t contextCount, int dirFlags);
 
-    inline const jfloat* getAdvances() const { return mAdvances.array(); }
-    inline size_t getAdvancesCount() const { return mAdvances.size(); }
-    inline jfloat getTotalAdvance() const { return mTotalAdvance; }
-    inline const jchar* getGlyphs() const { return mGlyphs.array(); }
-    inline size_t getGlyphsCount() const { return mGlyphs.size(); }
+    void    getAdvances(size_t start, size_t count, jfloat* outAdvances) const;
+    jfloat  getTotalAdvance(size_t start, size_t count) const;
+    void    getGlyphsIndexAndCount(size_t start, size_t count, size_t* outStartIndex,
+                size_t* outGlyphsCount) const;
+    const jchar*  getGlyphs(size_t startIndex, size_t count);
 
     /**
      * Get the size of the Cache entry
@@ -138,14 +133,10 @@
             SkPaint* paint, const UChar* chars, size_t start, size_t count, size_t contextCount,
             bool isRTL);
 
-    static void computeValuesWithHarfbuzz(SkPaint* paint, const UChar* chars, size_t start,
-            size_t count, size_t contextCount, int dirFlags,
+    static void computeValuesWithHarfbuzz(SkPaint* paint, const UChar* chars,
+            size_t contextCount, int dirFlags,
             Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
-            Vector<jchar>* const outGlyphs);
-
-    static void computeAdvancesWithICU(SkPaint* paint, const UChar* chars, size_t start,
-            size_t count, size_t contextCount, int dirFlags,
-            jfloat* outAdvances, jfloat* outTotalAdvance);
+            Vector<jchar>* const outGlyphs, Vector<unsigned short>* const outLogClusters);
 
 private:
     /**
@@ -164,6 +155,11 @@
     Vector<jchar> mGlyphs;
 
     /**
+     * Harfbuzz Log Clusters
+     */
+    Vector<unsigned short> mLogClusters;
+
+    /**
      * Time for computing the values (in milliseconds)
      */
     uint32_t mElapsedTime;
@@ -175,7 +171,7 @@
     static void computeRunValuesWithHarfbuzz(SkPaint* paint, const UChar* chars, size_t start,
             size_t count, size_t contextCount, bool isRTL,
             Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
-            Vector<jchar>* const outGlyphs);
+            Vector<jchar>* const outGlyphs, Vector<unsigned short>* const outLogClusters);
 }; // TextLayoutCacheValue
 
 /**
@@ -199,8 +195,8 @@
      */
     void operator()(TextLayoutCacheKey& text, sp<TextLayoutCacheValue>& desc);
 
-    sp<TextLayoutCacheValue> getValue(SkPaint* paint,
-            const jchar* text, jint start, jint count, jint contextCount, jint dirFlags);
+    sp<TextLayoutCacheValue> getValue(SkPaint* paint, const jchar* text, jint count,
+            jint dirFlags);
 
     /**
      * Clear the cache
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index 395e417..e542a47 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -477,19 +477,21 @@
 #if RTL_USE_HARFBUZZ
     sp<TextLayoutCacheValue> value;
 #if USE_TEXT_LAYOUT_CACHE
-    value = TextLayoutCache::getInstance().getValue(paint, text, 0, count, count, flags);
+    value = TextLayoutCache::getInstance().getValue(paint, text, count, flags);
     if (value == NULL) {
         LOGE("Cannot get TextLayoutCache value");
         return ;
     }
 #else
     value = new TextLayoutCacheValue();
-    value->computeValues(paint, text, 0, count, count, flags);
+    value->computeValues(paint, text, count, flags);
 #endif
-    const jchar* glyphArray = value->getGlyphs();
-    int glyphCount = value->getGlyphsCount();
-    int bytesCount = glyphCount * sizeof(jchar);
-    renderer->drawText((const char*) glyphArray, bytesCount, glyphCount, x, y, paint);
+    size_t startIndex = 0;
+    size_t glyphsCount = 0;
+    value->getGlyphsIndexAndCount(0, count, &startIndex, &glyphsCount);
+    const jchar* glyphs = value->getGlyphs(startIndex, glyphsCount);
+    int bytesCount = glyphsCount * sizeof(jchar);
+    renderer->drawText((const char*) glyphs, bytesCount, glyphsCount, x, y, paint);
 #else
     const jchar *workText;
     jchar* buffer = NULL;
@@ -507,19 +509,21 @@
 #if RTL_USE_HARFBUZZ
     sp<TextLayoutCacheValue> value;
 #if USE_TEXT_LAYOUT_CACHE
-    value = TextLayoutCache::getInstance().getValue(paint, text, start, count, contextCount, flags);
+    value = TextLayoutCache::getInstance().getValue(paint, text, contextCount, flags);
     if (value == NULL) {
         LOGE("Cannot get TextLayoutCache value");
         return ;
     }
 #else
     value = new TextLayoutCacheValue();
-    value->computeValues(paint, text, start, count, contextCount, flags);
+    value->computeValues(paint, text, contextCount, flags);
 #endif
-    const jchar* glyphArray = value->getGlyphs();
-    int glyphCount = value->getGlyphsCount();
-    int bytesCount = glyphCount * sizeof(jchar);
-    renderer->drawText((const char*) glyphArray, bytesCount, glyphCount, x, y, paint);
+    size_t startIndex = 0;
+    size_t glyphsCount = 0;
+    value->getGlyphsIndexAndCount(start, count, &startIndex, &glyphsCount);
+    const jchar* glyphs = value->getGlyphs(startIndex, glyphsCount);
+    int bytesCount = glyphsCount * sizeof(jchar);
+    renderer->drawText((const char*) glyphs, bytesCount, glyphsCount, x, y, paint);
 #else
     uint8_t rtl = flags & 0x1;
     if (rtl) {