Commit 89be24c6 authored by Mihai Popa's avatar Mihai Popa Committed by android-build-team Robot

Optimise the hit test algorithm

Layout#getOffsetForHorizontal was running in O(n^2) time, where n is the
length of the current line. The method is used when a touch event
happens on a text line, to compute the cursor offset (and the character)
where it happened. Although this is not an issue in common usecases,
where the number of characters on a line is relatively small, this can
be very inefficient as a consequence of Unicode containing 0-width
(invisible) characters. Specifically, there are characters defining the
text direction (LTR or RTL), which cause our algorithm to touch the
worst case quadratic runtime. For example, a person is able to send a
message containing a few visible characters, and also a lot of these
direction changing invisible ones. When the receiver touches the message
(causing the Layout#getOffsetForHorizontal method to be called), the
receiver's application would become not responsive.

This CL optimizes the method to run in O(n) worst case. This is achieved
by computing the measurements of all line prefixes at first, which can
be done in a single pass. Then, all the prefix measurement queries will
be answered in O(1), rather than O(n) as it was happening before.

Bug: 79215201
Test: manual testing
Change-Id: Ib66ef392c19c937718e7101f6d48fac3abe51ad0
Merged-In: Ib66ef392c19c937718e7101f6d48fac3abe51ad0
(cherry picked from commit 69b589b2)
parent 3c2c834a
This diff is collapsed.
......@@ -368,6 +368,98 @@ class TextLine {
return h;
}
/**
* @see #measure(int, boolean, FontMetricsInt)
* @return The measure results for all possible offsets
*/
float[] measureAllOffsets(boolean[] trailing, FontMetricsInt fmi) {
float[] measurement = new float[mLen + 1];
int[] target = new int[mLen + 1];
for (int offset = 0; offset < target.length; ++offset) {
target[offset] = trailing[offset] ? offset - 1 : offset;
}
if (target[0] < 0) {
measurement[0] = 0;
}
float h = 0;
if (!mHasTabs) {
if (mDirections == Layout.DIRS_ALL_LEFT_TO_RIGHT) {
for (int offset = 0; offset <= mLen; ++offset) {
measurement[offset] = measureRun(0, offset, mLen, false, fmi);
}
return measurement;
}
if (mDirections == Layout.DIRS_ALL_RIGHT_TO_LEFT) {
for (int offset = 0; offset <= mLen; ++offset) {
measurement[offset] = measureRun(0, offset, mLen, true, fmi);
}
return measurement;
}
}
char[] chars = mChars;
int[] runs = mDirections.mDirections;
for (int i = 0; i < runs.length; i += 2) {
int runStart = runs[i];
int runLimit = runStart + (runs[i + 1] & Layout.RUN_LENGTH_MASK);
if (runLimit > mLen) {
runLimit = mLen;
}
boolean runIsRtl = (runs[i + 1] & Layout.RUN_RTL_FLAG) != 0;
int segstart = runStart;
for (int j = mHasTabs ? runStart : runLimit; j <= runLimit; ++j) {
int codept = 0;
if (mHasTabs && j < runLimit) {
codept = chars[j];
if (codept >= 0xD800 && codept < 0xDC00 && j + 1 < runLimit) {
codept = Character.codePointAt(chars, j);
if (codept > 0xFFFF) {
++j;
continue;
}
}
}
if (j == runLimit || codept == '\t') {
float oldh = h;
boolean advance = (mDir == Layout.DIR_RIGHT_TO_LEFT) == runIsRtl;
float w = measureRun(segstart, j, j, runIsRtl, fmi);
h += advance ? w : -w;
float baseh = advance ? oldh : h;
FontMetricsInt crtfmi = advance ? fmi : null;
for (int offset = segstart; offset <= j && offset <= mLen; ++offset) {
if (target[offset] >= segstart && target[offset] < j) {
measurement[offset] =
baseh + measureRun(segstart, offset, j, runIsRtl, crtfmi);
}
}
if (codept == '\t') {
if (target[j] == j) {
measurement[j] = h;
}
h = mDir * nextTab(h * mDir);
if (target[j + 1] == j) {
measurement[j + 1] = h;
}
}
segstart = j + 1;
}
}
}
if (target[mLen] == mLen) {
measurement[mLen] = h;
}
return measurement;
}
/**
* Draws a unidirectional (but possibly multi-styled) run of text.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment