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

Commit 26297cac authored by Hongwei Wang's avatar Hongwei Wang
Browse files

Cleanup Bitmap and buffer after setting up leash

- Recycle the Bitmap being used to draw the icon overlay for PiP right
  after setting the leash. This avoids Bitmap being leaked, eg. not
  recycled.
- Close also the hardware buffer to remove the reference to underlying
  buffer at the same time.

Flag: EXEMPT bugfix
Bug: 393584443
Test: manually, make sure the Bitmap is recycled and \
      no function regression
Change-Id: I46ec091432b235cb63ad7d40d73b39f3c1889470
parent 779e4c97
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.hardware.HardwareBuffer;
import android.util.TypedValue;
import android.view.SurfaceControl;
import android.window.TaskSnapshot;
@@ -225,12 +226,17 @@ public abstract class PipContentOverlay {

        @Override
        public void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash) {
            final HardwareBuffer buffer = mBitmap.getHardwareBuffer();
            tx.show(mLeash);
            tx.setLayer(mLeash, Integer.MAX_VALUE);
            tx.setBuffer(mLeash, mBitmap.getHardwareBuffer());
            tx.setBuffer(mLeash, buffer);
            tx.setAlpha(mLeash, 0f);
            tx.reparent(mLeash, parentLeash);
            tx.apply();
            // Cleanup the bitmap and buffer after setting up the leash
            mBitmap.recycle();
            mBitmap = null;
            buffer.close();
        }

        @Override
@@ -253,14 +259,6 @@ public abstract class PipContentOverlay {
                    .setAlpha(mLeash, fraction < 0.5f ? 0 : (fraction - 0.5f) * 2);
        }

        @Override
        public void detach(SurfaceControl.Transaction tx) {
            super.detach(tx);
            if (mBitmap != null && !mBitmap.isRecycled()) {
                mBitmap.recycle();
            }
        }

        private void prepareAppIconOverlay(Drawable appIcon) {
            final Canvas canvas = new Canvas();
            canvas.setBitmap(mBitmap);
@@ -282,7 +280,9 @@ public abstract class PipContentOverlay {
                    mOverlayHalfSize + mAppIconSizePx / 2);
            appIcon.setBounds(appIconBounds);
            appIcon.draw(canvas);
            Bitmap oldBitmap = mBitmap;
            mBitmap = mBitmap.copy(Bitmap.Config.HARDWARE, false /* mutable */);
            oldBitmap.recycle();
        }
    }
}