From 4f2cc5308d3f5772f235ff179c2591757e09fd02 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sun, 16 Jun 2024 11:35:44 +0800 Subject: [PATCH] Simplify bitmap reallocation on Android * java/org/gnu/emacs/EmacsView.java: Update outdated commentary. (handleDirtyBitmap): Don't copy contents of the previous bitmap to the new. (onLayout): Unconditionally expose upon layout changes. (cherry picked from commit 8d60b6bab8b95e4f3b350a8b093e8f60e3f83f69) --- java/org/gnu/emacs/EmacsView.java | 35 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/java/org/gnu/emacs/EmacsView.java b/java/org/gnu/emacs/EmacsView.java index 4a505b3c0dc..78d1ef785da 100644 --- a/java/org/gnu/emacs/EmacsView.java +++ b/java/org/gnu/emacs/EmacsView.java @@ -49,11 +49,13 @@ import java.util.Arrays; /* This is an Android view which has a back and front buffer. When swapBuffers is called, the back buffer is swapped to the front - buffer, and any damage is invalidated. frontBitmap and backBitmap - are modified and used both from the UI and the Emacs thread. As a - result, there is a lock held during all drawing operations. + buffer, and any damage is invalidated. A front buffer bitmap defined + in EmacsSurfaceView, and the write buffer in this file, are modified + and used both from the UI and the Emacs thread. As a result, there + is a lock held during all drawing operations. - It is also a ViewGroup, as it also lays out children. */ + It is also a ViewGroup, so that it may also manage the layout of its + children. */ public final class EmacsView extends ViewGroup implements ViewTreeObserver.OnGlobalLayoutListener @@ -204,19 +206,19 @@ public final class EmacsView extends ViewGroup rectangle ID. */ lastClipSerial = 0; - /* Copy over the contents of the old bitmap. */ - if (oldBitmap != null) - canvas.drawBitmap (oldBitmap, 0f, 0f, new Paint ()); - + /* Clear the bitmap reallocation flag. */ bitmapDirty = false; - /* Explicitly free the old bitmap's memory. */ - + /* Explicitly free the old bitmap's memory. The bitmap might + continue to be referenced by canvas or JNI objects returned by + getBitmap or getCanvas, but the underlying storage will not be + released until such references disappear. See + BitmapWrapper::freePixels in hwui/jni/Bitmap.cpp. */ if (oldBitmap != null) oldBitmap.recycle (); - /* Some Android versions still don't free the bitmap until the - next GC. */ + /* Some Android versions still refuse to release the bitmap until + the next GC. */ Runtime.getRuntime ().gc (); } @@ -367,13 +369,10 @@ public final class EmacsView extends ViewGroup if (changed) { + /* Expose the window upon a change in the view's size that + prompts the creation of a new bitmap. */ explicitlyDirtyBitmap (); - - /* Expose the window upon a change in the view's size. */ - - if (right - left > oldMeasuredWidth - || bottom - top > oldMeasuredHeight) - needExpose = true; + needExpose = true; /* This might return NULL if this view is not attached. */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) -- 2.39.2