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

Commit bb4aa207 authored by Biswarup Pal's avatar Biswarup Pal Committed by Android (Google) Code Review
Browse files

Merge "Use TextureView instead of SurfaceView in MirrorView" into main

parents c7749aeb 74e23332
Loading
Loading
Loading
Loading
+32 −24
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Looper;
@@ -29,8 +30,7 @@ import android.util.AttributeSet;
import android.view.InputDevice;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.SurfaceView;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout;
@@ -137,9 +137,9 @@ public class MirrorView extends FrameLayout {
    }
    }


    private void init(@NonNull Context context) {
    private void init(@NonNull Context context) {
        SurfaceView surfaceView = new SurfaceView(context);
        TextureView textureView = new TextureView(context);
        mOverlay = new Overlay(context);
        mOverlay = new Overlay(context);
        addView(surfaceView,
        addView(textureView,
                new ViewGroup.LayoutParams(
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(mOverlay,
        addView(mOverlay,
@@ -147,8 +147,7 @@ public class MirrorView extends FrameLayout {
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        mMirrorHelper = new MirrorHelper();
        mMirrorHelper = new MirrorHelper();
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        textureView.setSurfaceTextureListener(mMirrorHelper);
        surfaceHolder.addCallback(mMirrorHelper);


        super.setOnTouchListener((v, event) -> {
        super.setOnTouchListener((v, event) -> {
            boolean handled = mOnTouchListener != null && mOnTouchListener.onTouch(v, event);
            boolean handled = mOnTouchListener != null && mOnTouchListener.onTouch(v, event);
@@ -157,7 +156,7 @@ public class MirrorView extends FrameLayout {
        });
        });
    }
    }


    private static final class MirrorHelper implements SurfaceHolder.Callback {
    private static final class MirrorHelper implements TextureView.SurfaceTextureListener {
        private final HandlerThread mHandlerThread;
        private final HandlerThread mHandlerThread;


        // The following members are always written on the main thread, and always read from the
        // The following members are always written on the main thread, and always read from the
@@ -177,6 +176,32 @@ public class MirrorView extends FrameLayout {
            mHandlerThread.start();
            mHandlerThread.start();
        }
        }


        @Override
        public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int width,
                int height) {
            mSurface = new Surface(surfaceTexture);
            mWidth = width;
            mHeight = height;
            createOrResizeMirrorIfPossible();
        }

        @Override
        public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int width,
                int height) {
            mWidth = width;
            mHeight = height;
            createOrResizeMirrorIfPossible();
        }

        @Override
        public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
            destroyMirror();
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {}

        void setComputerControlSession(@Nullable ComputerControlSession computerControlSession) {
        void setComputerControlSession(@Nullable ComputerControlSession computerControlSession) {
            if (mComputerControlSession != null) {
            if (mComputerControlSession != null) {
                destroyMirror();
                destroyMirror();
@@ -207,23 +232,6 @@ public class MirrorView extends FrameLayout {
            return true;
            return true;
        }
        }


        @Override
        public void surfaceCreated(@NonNull SurfaceHolder holder) {}

        @Override
        public void surfaceChanged(
                @NonNull SurfaceHolder holder, int format, int width, int height) {
            mSurface = holder.getSurface();
            mWidth = width;
            mHeight = height;
            createOrResizeMirrorIfPossible();
        }

        @Override
        public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
            destroyMirror();
        }

        private void createOrResizeMirrorIfPossible() {
        private void createOrResizeMirrorIfPossible() {
            mHandlerThread.getThreadExecutor().execute(() -> {
            mHandlerThread.getThreadExecutor().execute(() -> {
                if (mComputerControlSession == null || mSurface == null) {
                if (mComputerControlSession == null || mSurface == null) {