Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dd1a55b authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Remove nGetWidths and pass the jfloat[] as nComputeLineBreaks argument.

nGetWidths was always called just before nComputeLineBreaks. It would be
an improvement to pass the result array as the argument of
nComputeLineBreaks.

Bug: 65024629
Test: bit FrameworksCoreTests:android.text.StaticLayoutLineBreakingTest
Test: bit CtsTextTestCase:*
Test: Manually checked no regression in TextViewOnMeasurePerfTest
Test: Manually checked no regression in StaticLayoutPerfTest result
Change-Id: I566be66558a22b3a3ff79d502bebbbfec75dd065
parent 34e83d28
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -433,7 +433,6 @@ public class StaticLayout extends Layout {
         *    + addStyleRun (a text run, to be measured in native code)
         *    + addReplacementRun (a replacement run, width is given)
         *
         * After measurement, nGetWidths() is valid if the widths are needed (eg for ellipsis).
         * Run nComputeLineBreaks() to obtain line breaks for the paragraph.
         *
         * After all paragraphs, call finish() to release expensive buffers.
@@ -866,10 +865,9 @@ public class StaticLayout extends Layout {
                spanEndCacheCount++;
            }

            nGetWidths(b.mNativePtr, widths);
            int breakCount = nComputeLineBreaks(b.mNativePtr, lineBreaks, lineBreaks.breaks,
                    lineBreaks.widths, lineBreaks.ascents, lineBreaks.descents, lineBreaks.flags,
                    lineBreaks.breaks.length);
                    lineBreaks.breaks.length, widths);

            final int[] breaks = lineBreaks.breaks;
            final float[] lineWidths = lineBreaks.widths;
@@ -1551,16 +1549,17 @@ public class StaticLayout extends Layout {
            @FloatRange(from = 0.0f) float width, @Nullable String languageTags,
            @Nullable long[] hyphenators);

    private static native void nGetWidths(long nativePtr, float[] widths);

    // populates LineBreaks and returns the number of breaks found
    //
    // the arrays inside the LineBreaks objects are passed in as well
    // to reduce the number of JNI calls in the common case where the
    // arrays do not have to be resized
    // The individual character widths will be returned in charWidths. The length of charWidths must
    // be at least the length of the text.
    private static native int nComputeLineBreaks(long nativePtr, LineBreaks recycle,
            int[] recycleBreaks, float[] recycleWidths, float[] recycleAscents,
            float[] recycleDescents, int[] recycleFlags, int recycleLength);
            float[] recycleDescents, int[] recycleFlags, int recycleLength,
            float[] charWidths);

    private int mLineCount;
    private int mTopPadding, mBottomPadding;
+4 −8
Original line number Diff line number Diff line
@@ -166,7 +166,7 @@ static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr,
                               jobject recycle, jintArray recycleBreaks,
                               jfloatArray recycleWidths, jfloatArray recycleAscents,
                               jfloatArray recycleDescents, jintArray recycleFlags,
                               jint recycleLength) {
                               jint recycleLength, jfloatArray charWidths) {
    minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);

    size_t nBreaks = b->computeBreaks();
@@ -175,6 +175,8 @@ static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr,
            recycleFlags, recycleLength, nBreaks, b->getBreaks(), b->getWidths(), b->getAscents(),
            b->getDescents(), b->getFlags());

    env->SetFloatArrayRegion(charWidths, 0, b->size(), b->charWidths());

    b->finish();

    return static_cast<jint>(nBreaks);
@@ -256,11 +258,6 @@ static void nAddReplacementRun(JNIEnv* env, jclass, jlong nativePtr,
    b->addReplacement(start, end, width, langTagsString.get(), makeHyphenators(env, hyphenators));
}

static void nGetWidths(JNIEnv* env, jclass, jlong nativePtr, jfloatArray widths) {
    minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
    env->SetFloatArrayRegion(widths, 0, b->size(), b->charWidths());
}

static const JNINativeMethod gMethods[] = {
    // TODO performance: many of these are candidates for fast jni, awaiting guidance
    {"nNewBuilder", "()J", (void*) nNewBuilder},
@@ -269,8 +266,7 @@ static const JNINativeMethod gMethods[] = {
    {"nSetupParagraph", "(J[CIFIF[IIIIZ[I[I[II)V", (void*) nSetupParagraph},
    {"nAddStyleRun", "(JJIIZLjava/lang/String;[J)V", (void*) nAddStyleRun},
    {"nAddReplacementRun", "(JIIFLjava/lang/String;[J)V", (void*) nAddReplacementRun},
    {"nGetWidths", "(J[F)V", (void*) nGetWidths},
    {"nComputeLineBreaks", "(JLandroid/text/StaticLayout$LineBreaks;[I[F[F[F[II)I",
    {"nComputeLineBreaks", "(JLandroid/text/StaticLayout$LineBreaks;[I[F[F[F[II[F)I",
        (void*) nComputeLineBreaks}
};