camera/camera-testing/src/main/java/androidx/camera/testing/SurfaceTextureProvider.java
/**
     * Creates a {@link Preview.SurfaceProvider} that is backed by a {@link SurfaceTexture}.
     *
     * <p>This method also creates a backing OpenGL thread that will automatically drain frames
     * from the SurfaceTexture as they become available.
     */
Creates a  Preview.SurfaceProvider that is backed by a  SurfaceTexture.
This method also creates a backing OpenGL thread that will automatically drain frames
from the SurfaceTexture as they become available.
Creates a Preview . SurfaceProvider that is backed by a SurfaceTexture . This method also creates a backing OpenGL thread that will automatically drain frames from the SurfaceTexture as they become available .
@NonNull
    public static Preview.SurfaceProvider createAutoDrainingSurfaceTextureProvider() {
        return (surfaceRequest) -> {
            HandlerThread handlerThread = new HandlerThread(String.format("CameraX"
                    + "-AutoDrainThread-%x", surfaceRequest.hashCode()));
            handlerThread.start();
            Handler handler = HandlerCompat.createAsync(handlerThread.getLooper());
            Executor glContextExecutor = CameraXExecutors.newHandlerExecutor(handler);
            glContextExecutor.execute(() -> {
                EGLContextParams contextParams = createDummyEGLContext();
                EGL14.eglMakeCurrent(contextParams.display, contextParams.outputSurface,
                        contextParams.outputSurface, contextParams.context);
                int[] textureIds = new int[1];
                GLES20.glGenTextures(1, textureIds, 0);
                SurfaceTexture surfaceTexture = new SurfaceTexture(textureIds[0]);
                surfaceTexture.setDefaultBufferSize(surfaceRequest.getResolution().getWidth(),
                        surfaceRequest.getResolution().getHeight());
                surfaceTexture.setOnFrameAvailableListener(
                        SurfaceTexture::updateTexImage, handler);
                Surface surface = new Surface(surfaceTexture);
                surfaceRequest.provideSurface(surface,
                        glContextExecutor,
                        (surfaceResponse) -> {
                            surface.release();
                            surfaceTexture.release();
                            GLES20.glDeleteTextures(1, textureIds, 0);
                            terminateEGLContext(contextParams);
                            handlerThread.quitSafely();
                        });
            });
        };
    }
@ NonNull public static Preview . SurfaceProvider createAutoDrainingSurfaceTextureProvider ( ) { return ( surfaceRequest ) -> { HandlerThread handlerThread = new HandlerThread ( String . format ( "CameraX" + "-AutoDrainThread-%x" , surfaceRequest . hashCode ( ) ) ) ; handlerThread . start ( ) ; Handler handler = HandlerCompat . createAsync ( handlerThread . getLooper ( ) ) ; Executor glContextExecutor = CameraXExecutors . newHandlerExecutor ( handler ) ; glContextExecutor . execute ( ( ) -> { EGLContextParams contextParams = createDummyEGLContext ( ) ; EGL14 . eglMakeCurrent ( contextParams . display , contextParams . outputSurface , contextParams . outputSurface , contextParams . context ) ; int [ ] textureIds = new int [ 1 ] ; GLES20 . glGenTextures ( 1 , textureIds , 0 ) ; SurfaceTexture surfaceTexture = new SurfaceTexture ( textureIds [ 0 ] ) ; surfaceTexture . setDefaultBufferSize ( surfaceRequest . getResolution ( ) . getWidth ( ) , surfaceRequest . getResolution ( ) . getHeight ( ) ) ; surfaceTexture . setOnFrameAvailableListener ( SurfaceTexture :: updateTexImage , handler ) ; Surface surface = new Surface ( surfaceTexture ) ; surfaceRequest . provideSurface ( surface , glContextExecutor , ( surfaceResponse ) -> { surface . release ( ) ; surfaceTexture . release ( ) ; GLES20 . glDeleteTextures ( 1 , textureIds , 0 ) ; terminateEGLContext ( contextParams ) ; handlerThread . quitSafely ( ) ; } ) ; } ) ; } ; }
Creates a {@link Preview.SurfaceProvider} that is backed by a {@link SurfaceTexture}.
Creates a { @link Preview . SurfaceProvider } that is backed by a { @link SurfaceTexture } .