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

Commit 285797a4 authored by Mihai Popa's avatar Mihai Popa Committed by Android (Google) Code Review
Browse files

Merge "[Magnifier-38] Avoid deadlock causing ANR" into pi-dev

parents 8ec34162 5d983d2b
Loading
Loading
Loading
Loading
+22 −12
Original line number Original line Diff line number Diff line
@@ -393,6 +393,12 @@ public final class Magnifier {
        private int mWindowPositionY;
        private int mWindowPositionY;
        private boolean mPendingWindowPositionUpdate;
        private boolean mPendingWindowPositionUpdate;


        // The lock used to synchronize the UI and render threads when a #destroy
        // is performed on the UI thread and a frame callback on the render thread.
        // When both mLock and mDestroyLock need to be held at the same time,
        // mDestroyLock should be acquired before mLock in order to avoid deadlocks.
        private final Object mDestroyLock = new Object();

        InternalPopupWindow(final Context context, final Display display,
        InternalPopupWindow(final Context context, final Display display,
                final Surface parentSurface,
                final Surface parentSurface,
                final int width, final int height, final float elevation, final float cornerRadius,
                final int width, final int height, final float elevation, final float cornerRadius,
@@ -517,9 +523,11 @@ public final class Magnifier {
         * Destroys this instance.
         * Destroys this instance.
         */
         */
        public void destroy() {
        public void destroy() {
            synchronized (mDestroyLock) {
                mSurface.destroy();
            }
            synchronized (mLock) {
            synchronized (mLock) {
                mRenderer.destroy();
                mRenderer.destroy();
                mSurface.destroy();
                mSurfaceControl.destroy();
                mSurfaceControl.destroy();
                mSurfaceSession.kill();
                mSurfaceSession.kill();
                mBitmapRenderNode.destroy();
                mBitmapRenderNode.destroy();
@@ -567,10 +575,11 @@ public final class Magnifier {
                    final int pendingY = mWindowPositionY;
                    final int pendingY = mWindowPositionY;


                    callback = frame -> {
                    callback = frame -> {
                        synchronized (mLock) {
                        synchronized (mDestroyLock) {
                            if (!mSurface.isValid()) {
                            if (!mSurface.isValid()) {
                                return;
                                return;
                            }
                            }
                            synchronized (mLock) {
                                mRenderer.setLightCenter(mDisplay, pendingX, pendingY);
                                mRenderer.setLightCenter(mDisplay, pendingX, pendingY);
                                // Show or move the window at the content draw frame.
                                // Show or move the window at the content draw frame.
                                SurfaceControl.openTransaction();
                                SurfaceControl.openTransaction();
@@ -583,6 +592,7 @@ public final class Magnifier {
                                }
                                }
                                SurfaceControl.closeTransaction();
                                SurfaceControl.closeTransaction();
                            }
                            }
                        }
                    };
                    };
                } else {
                } else {
                    callback = null;
                    callback = null;