| /* |
| * Copyright 2019 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.extensions.impl; |
| |
| import android.content.Context; |
| import android.graphics.ImageFormat; |
| import android.hardware.camera2.CameraCharacteristics; |
| import android.hardware.camera2.CaptureRequest; |
| import android.hardware.camera2.params.StreamConfigurationMap; |
| import android.os.Build; |
| import android.util.Pair; |
| import android.util.Size; |
| |
| import androidx.annotation.NonNull; |
| import androidx.annotation.Nullable; |
| import androidx.annotation.RequiresApi; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| /** |
| * Implementation for beauty preview use case. |
| * |
| * <p>This class should be implemented by OEM and deployed to the target devices. 3P developers |
| * don't need to implement this, unless this is used for related testing usage. |
| * |
| * @since 1.0 |
| */ |
| @RequiresApi(21) // TODO(b/200306659): Remove and replace with annotation on package-info.java |
| public final class BeautyPreviewExtenderImpl implements PreviewExtenderImpl { |
| private static final int DEFAULT_STAGE_ID = 0; |
| private static final int SESSION_STAGE_ID = 101; |
| private static final int EFFECT = CaptureRequest.CONTROL_EFFECT_MODE_NEGATIVE; |
| |
| private CameraCharacteristics mCameraCharacteristics; |
| |
| public BeautyPreviewExtenderImpl() { |
| } |
| |
| @Override |
| public void init(@NonNull String cameraId, |
| @NonNull CameraCharacteristics cameraCharacteristics) { |
| mCameraCharacteristics = cameraCharacteristics; |
| } |
| |
| @Override |
| public boolean isExtensionAvailable(@NonNull String cameraId, |
| @Nullable CameraCharacteristics cameraCharacteristics) { |
| // Return false to skip tests since old devices do not support extensions. |
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { |
| return false; |
| } |
| |
| if (cameraCharacteristics == null) { |
| return false; |
| } |
| |
| return CameraCharacteristicAvailability.isEffectAvailable(cameraCharacteristics, EFFECT); |
| } |
| |
| @NonNull |
| @Override |
| public CaptureStageImpl getCaptureStage() { |
| // Set the necessary CaptureRequest parameters via CaptureStage, here we use some |
| // placeholder set of CaptureRequest.Key values |
| SettableCaptureStage captureStage = new SettableCaptureStage(DEFAULT_STAGE_ID); |
| captureStage.addCaptureRequestParameters(CaptureRequest.CONTROL_EFFECT_MODE, EFFECT); |
| |
| return captureStage; |
| } |
| |
| @Override |
| @NonNull |
| public ProcessorType getProcessorType() { |
| return ProcessorType.PROCESSOR_TYPE_REQUEST_UPDATE_ONLY; |
| } |
| |
| @Nullable |
| @Override |
| public ProcessorImpl getProcessor() { |
| return RequestUpdateProcessorImpls.noUpdateProcessor(); |
| } |
| |
| @SuppressWarnings("ConstantConditions") // Super method is nullable. |
| @Nullable |
| @Override |
| public List<Pair<Integer, Size[]>> getSupportedResolutions() { |
| List<Pair<Integer, Size[]>> formatResolutionsPairList = new ArrayList<>(); |
| |
| StreamConfigurationMap map = |
| mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); |
| |
| if (map != null) { |
| // The sample implementation only retrieves originally supported resolutions from |
| // CameraCharacteristics for PRIVATE format to return. |
| Size[] outputSizes = map.getOutputSizes(ImageFormat.PRIVATE); |
| |
| if (outputSizes != null) { |
| formatResolutionsPairList.add(Pair.create(ImageFormat.PRIVATE, outputSizes)); |
| } |
| } |
| |
| return formatResolutionsPairList; |
| } |
| |
| @Override |
| public void onInit(@NonNull String cameraId, |
| @NonNull CameraCharacteristics cameraCharacteristics, |
| @NonNull Context context) { |
| |
| } |
| |
| @Override |
| public void onDeInit() { |
| |
| } |
| |
| @Nullable |
| @Override |
| public CaptureStageImpl onPresetSession() { |
| // The CaptureRequest parameters will be set via SessionConfiguration#setSessionParameters |
| // (CaptureRequest) which only supported from API level 28. |
| if (Build.VERSION.SDK_INT < 28) { |
| return null; |
| } |
| |
| // Set the necessary CaptureRequest parameters via CaptureStage, here we use some |
| // placeholder set of CaptureRequest.Key values |
| SettableCaptureStage captureStage = new SettableCaptureStage(SESSION_STAGE_ID); |
| captureStage.addCaptureRequestParameters(CaptureRequest.CONTROL_EFFECT_MODE, EFFECT); |
| |
| return captureStage; |
| } |
| |
| @SuppressWarnings("ConstantConditions") // Super method is nullable. |
| @Nullable |
| @Override |
| public CaptureStageImpl onEnableSession() { |
| // Set the necessary CaptureRequest parameters via CaptureStage, here we use some |
| // placeholder set of CaptureRequest.Key values |
| SettableCaptureStage captureStage = new SettableCaptureStage(SESSION_STAGE_ID); |
| captureStage.addCaptureRequestParameters(CaptureRequest.CONTROL_EFFECT_MODE, EFFECT); |
| |
| return captureStage; |
| } |
| |
| @SuppressWarnings("ConstantConditions") // Super method is nullable. |
| @Nullable |
| @Override |
| public CaptureStageImpl onDisableSession() { |
| // Set the necessary CaptureRequest parameters via CaptureStage, here we use some |
| // placeholder set of CaptureRequest.Key values |
| SettableCaptureStage captureStage = new SettableCaptureStage(SESSION_STAGE_ID); |
| captureStage.addCaptureRequestParameters(CaptureRequest.CONTROL_EFFECT_MODE, EFFECT); |
| |
| return captureStage; |
| } |
| } |