A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
perf(terminal): Paint state reset + color cache + reuse coord buf + drop dead AutoFill, unsync sixel, row scratch
ExTvbea29ff74f75
diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalBuffer.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalBuffer.javaindex a284996..9a5e828 100644--- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalBuffer.java+++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalBuffer.java@@ -708,7 +708,11 @@ public final class TerminalBuffer {mTerminalSixel = null;}- public synchronized boolean sixelReadData(int codePoint, int repeat) {+ // Podroid: sixel parsing is single-threaded (TerminalEmulator's parse loop+ // runs on the TerminalSession reader thread), so `synchronized` here only+ // adds monitor-acquisition cost on the hot path of image-heavy frames+ // (chafa, sixel-rendered tools). Drop it.+ public boolean sixelReadData(int codePoint, int repeat) {// If an error occurred during processing (like OOM), then remaining sixel command is// completely read, but is ignored.if (mTerminalSixel != null) {@@ -732,7 +736,8 @@ public final class TerminalBuffer {return true;}- public synchronized void sixelSetColor(int color) {+ // Podroid: single-threaded by the same reasoning as sixelReadData(); drop `synchronized`.+ public void sixelSetColor(int color) {if (mTerminalSixel != null)mTerminalSixel.setColor(color);}diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalRow.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalRow.javaindex 14de528..9bd2edf 100644--- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalRow.java+++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalRow.java@@ -52,6 +52,11 @@ public final class TerminalRow {/** If this row has a {@link TerminalBitmap}. Used for performance only. */public boolean mHasTerminalBitmap;+ /** Podroid: scratch buffer reused by {@link #copyInterval} for the in-place+ * case (this == line). Avoids allocating a fresh char[] every row-shift —+ * hot path during full-screen scrolls. Grown on demand. */+ private char[] mCopyScratch;+/** Construct a blank row (containing only whitespace, ' ') with a specified style. */public TerminalRow(int columns, long style) {mColumns = columns;@@ -66,7 +71,18 @@ public final class TerminalRow {final int x1 = line.findStartOfColumn(sourceX1);final int x2 = line.findStartOfColumn(sourceX2);boolean startingFromSecondHalfOfWideChar = (sourceX1 > 0 && line.wideDisplayCharacterStartingAt(sourceX1 - 1));- final char[] sourceChars = (this == line) ? Arrays.copyOf(line.mText, line.mText.length) : line.mText;+ // Podroid: in-place copy (this == line) needs an isolated source view because+ // setChar() below mutates mText. Reuse a per-row scratch buffer instead of+ // Arrays.copyOf'ing fresh each call — full-screen scrolls hit this rapidly.+ final char[] sourceChars;+ if (this == line) {+ int len = line.mText.length;+ if (mCopyScratch == null || mCopyScratch.length < len) mCopyScratch = new char[len];+ System.arraycopy(line.mText, 0, mCopyScratch, 0, len);+ sourceChars = mCopyScratch;+ } else {+ sourceChars = line.mText;+ }int latestNonCombiningWidth = 0;for (int i = x1; i < x2; i++) {char sourceChar = sourceChars[i];diff --git a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.javaindex f499d1c..238d8de 100644--- a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java+++ b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java@@ -42,6 +42,11 @@ public final class TerminalRenderer {// time was straight-up GC pressure with no benefit since we just mutate it.private final RectF mBitmapDestRect = new RectF();+ // Podroid: cache last paint color so repeated runs with the same color skip+ // the (relatively expensive) setColor() call. Reset at the start of each+ // render() so cross-frame state doesn't leak after a resize.+ private int mLastPaintColor = 0;+public TerminalRenderer(int textSize, Typeface typeface) {mTextSize = textSize;mTypeface = typeface;@@ -85,6 +90,17 @@ public final class TerminalRenderer {if (reverseVideo)canvas.drawColor(palette[TextStyle.COLOR_INDEX_FOREGROUND], PorterDuff.Mode.SRC);+ // Podroid: reset Paint attribute state at the start of every frame so+ // a previous frame's bold/underline/italic/strike-thru can't leak into+ // the first run of this frame (drawTextRun only sets these per-run+ // when the style demands it). Also reset mLastPaintColor so the+ // color cache starts fresh after cell-grid resizes.+ mTextPaint.setFakeBoldText(false);+ mTextPaint.setUnderlineText(false);+ mTextPaint.setStrikeThruText(false);+ mTextPaint.setTextSkewX(0f);+ mLastPaintColor = 0;+float heightOffset = mFontLineSpacingAndAscent;for (int row = topRow; row < endRow; row++) {heightOffset += mFontLineSpacing;@@ -236,12 +252,12 @@ public final class TerminalRenderer {if (backColor != palette[TextStyle.COLOR_INDEX_BACKGROUND]) {// Only draw non-default background.- mTextPaint.setColor(backColor);+ if (backColor != mLastPaintColor) { mTextPaint.setColor(backColor); mLastPaintColor = backColor; }canvas.drawRect(left, y - mFontLineSpacingAndAscent + mFontAscent, right, y, mTextPaint);}if (cursor != 0) {- mTextPaint.setColor(cursor);+ if (cursor != mLastPaintColor) { mTextPaint.setColor(cursor); mLastPaintColor = cursor; }float cursorHeight = mFontLineSpacingAndAscent - mFontAscent;if (cursorStyle == TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE) cursorHeight /= 4.f;else if (cursorStyle == TerminalEmulator.TERMINAL_CURSOR_STYLE_BAR) right -= (((right - left) * 3) / 4.f);@@ -261,11 +277,15 @@ public final class TerminalRenderer {foreColor = 0xFF000000 + (red << 16) + (green << 8) + blue;}+ // Podroid: always set all four attributes per run — they MUST be+ // reset to defaults when the run doesn't ask for them, otherwise+ // the previous run's bold/underline/italic/strike-thru leaks into+ // this run's drawTextRun() call.mTextPaint.setFakeBoldText(bold);mTextPaint.setUnderlineText(underline);mTextPaint.setTextSkewX(italic ? -0.35f : 0.f);mTextPaint.setStrikeThruText(strikeThrough);- mTextPaint.setColor(foreColor);+ if (foreColor != mLastPaintColor) { mTextPaint.setColor(foreColor); mLastPaintColor = foreColor; }// The text alignment is the default Paint.Align.LEFT.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.javaindex cfd6f5b..df5cb2b 100644--- a/terminal-view/src/main/java/com/termux/view/TerminalView.java+++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java@@ -33,7 +33,6 @@ import android.view.inputmethod.EditorInfo;import android.view.inputmethod.InputConnection;import android.widget.Scroller;-import androidx.annotation.Nullable;import androidx.annotation.RequiresApi;import com.termux.terminal.KeyHandler;@@ -163,6 +162,12 @@ public final class TerminalView extends View {/** Keep track of the time when a touch event leading to sending mouse scroll events started. */private long mMouseStartDownTime = -1;+ /** Podroid: reusable scratch buffer for {@link #getColumnAndRow(MotionEvent, boolean)}.+ * Mouse events fire dozens of times per second during drag — allocating a fresh+ * int[2] each call was needless GC pressure. Callers read & forward the values+ * immediately, so reuse is safe. */+ private final int[] mTempCoords = new int[2];+final Scroller mScroller;/** What was left in from scrolling movement. */@@ -666,7 +671,11 @@ public final class TerminalView extends View {if (relativeToScroll) {row += mTopRow;}- return new int[] { column, row };+ // Podroid: returns the shared mTempCoords; callers read and forward+ // the values immediately, so the next call overwriting them is fine.+ mTempCoords[0] = column;+ mTempCoords[1] = row;+ return mTempCoords;}/** Send a single mouse event code to the terminal. */@@ -1207,24 +1216,12 @@ public final class TerminalView extends View {return mAutoFillType;}- @RequiresApi(api = Build.VERSION_CODES.O)- @Override- public String[] getAutofillHints() {- return mAutoFillHints;- }-@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic AutofillValue getAutofillValue() {return AutofillValue.forText("");}- @RequiresApi(api = Build.VERSION_CODES.O)- @Override- public int getImportantForAutofill() {- return mAutoFillImportance;- }-@RequiresApi(api = Build.VERSION_CODES.O)private synchronized void resetAutoFill() {// Restore none type so that AutoFill UI isn't shown anymore.@@ -1246,30 +1243,6 @@ public final class TerminalView extends View {}}- public boolean isAutoFillEnabled() {- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return false;-- try {- AutofillManager autofillManager = getAutoFillManagerService();- return autofillManager != null && autofillManager.isEnabled();- } catch (Exception e) {- mClient.logStackTraceWithMessage(LOG_TAG, "Failed to check if Autofill is enabled", e);- return false;- }- }-- public synchronized void requestAutoFillUsername() {- requestAutoFill(- Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? new String[]{View.AUTOFILL_HINT_USERNAME} :- null);- }-- public synchronized void requestAutoFillPassword() {- requestAutoFill(- Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? new String[]{View.AUTOFILL_HINT_PASSWORD} :- null);- }-public synchronized void requestAutoFill(String[] autoFillHints) {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;if (autoFillHints == null || autoFillHints.length < 1) return;@@ -1527,12 +1500,6 @@ public final class TerminalView extends View {return null;}- /** Get the selected text stored before "MORE" button was pressed on the context menu. */- @Nullable- public String getStoredSelectedText() {- return mTextSelectionCursorController != null ? mTextSelectionCursorController.getStoredSelectedText() : null;- }-/** Unset the selected text stored before "MORE" button was pressed on the context menu. */public void unsetStoredSelectedText() {if (mTextSelectionCursorController != null) mTextSelectionCursorController.unsetStoredSelectedText();