public class

SurfaceProcessorWithExecutor

extends java.lang.Object

implements SurfaceProcessorInternal

 java.lang.Object

↳androidx.camera.core.processing.SurfaceProcessorWithExecutor

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

A wrapper of a pair of SurfaceProcessor and java.util.concurrent.Executor.

Wraps the external SurfaceProcessor and java.util.concurrent.Executor provided by the app. It makes sure that CameraX always invoke the SurfaceProcessor on the correct java.util.concurrent.Executor.

Summary

Constructors
publicSurfaceProcessorWithExecutor(CameraEffect cameraEffect)

Methods
public java.util.concurrent.ExecutorgetExecutor()

public SurfaceProcessorgetProcessor()

public voidonInputSurface(SurfaceRequest request)

public voidonOutputSurface(SurfaceOutput surfaceOutput)

public voidrelease()

public <any>snapshot(int jpegQuality, int rotationDegrees)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SurfaceProcessorWithExecutor(CameraEffect cameraEffect)

Methods

public SurfaceProcessor getProcessor()

public java.util.concurrent.Executor getExecutor()

public void onInputSurface(SurfaceRequest request)

public void onOutputSurface(SurfaceOutput surfaceOutput)

public <any> snapshot(int jpegQuality, int rotationDegrees)

public void release()

Source

/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.core.processing;

import static androidx.camera.core.impl.utils.futures.Futures.immediateFailedFuture;

import static java.util.Objects.requireNonNull;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.camera.core.CameraEffect;
import androidx.camera.core.Logger;
import androidx.camera.core.ProcessingException;
import androidx.camera.core.SurfaceOutput;
import androidx.camera.core.SurfaceProcessor;
import androidx.camera.core.SurfaceRequest;
import androidx.core.util.Consumer;

import com.google.common.util.concurrent.ListenableFuture;

import java.util.concurrent.Executor;

/**
 * A wrapper of a pair of {@link SurfaceProcessor} and {@link Executor}.
 *
 * <p> Wraps the external {@link SurfaceProcessor} and {@link Executor} provided by the app. It
 * makes sure that CameraX always invoke the {@link SurfaceProcessor} on the correct
 * {@link Executor}.
 */
public class SurfaceProcessorWithExecutor implements SurfaceProcessorInternal {

    private static final String TAG = "SurfaceProcessor";

    @NonNull
    private final SurfaceProcessor mSurfaceProcessor;
    @NonNull
    private final Executor mExecutor;
    @NonNull
    private final Consumer<Throwable> mErrorListener;

    public SurfaceProcessorWithExecutor(@NonNull CameraEffect cameraEffect) {
        mSurfaceProcessor = requireNonNull(cameraEffect.getSurfaceProcessor());
        mExecutor = cameraEffect.getExecutor();
        mErrorListener = cameraEffect.getErrorListener();
    }

    @NonNull
    @VisibleForTesting
    public SurfaceProcessor getProcessor() {
        return mSurfaceProcessor;
    }

    @NonNull
    @VisibleForTesting
    public Executor getExecutor() {
        return mExecutor;
    }

    @Override
    public void onInputSurface(@NonNull SurfaceRequest request) {
        mExecutor.execute(() -> {
            try {
                mSurfaceProcessor.onInputSurface(request);
            } catch (ProcessingException e) {
                Logger.e(TAG, "Failed to setup SurfaceProcessor input.", e);
                mErrorListener.accept(e);
            }
        });
    }

    @Override
    public void onOutputSurface(@NonNull SurfaceOutput surfaceOutput) {
        mExecutor.execute(() -> {
            try {
                mSurfaceProcessor.onOutputSurface(surfaceOutput);
            } catch (ProcessingException e) {
                Logger.e(TAG, "Failed to setup SurfaceProcessor output.", e);
                mErrorListener.accept(e);
            }
        });
    }

    @NonNull
    @Override
    public ListenableFuture<Void> snapshot(
            @IntRange(from = 0, to = 100) int jpegQuality,
            @IntRange(from = 0, to = 359) int rotationDegrees) {
        return immediateFailedFuture(
                new Exception("Snapshot not supported by external SurfaceProcessor"));
    }

    @Override
    public void release() {
        // No-op. External SurfaceProcessor should not be released by CameraX.
    }
}