1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.hardware.camera2.params;
17 
18 import android.annotation.FlaggedApi;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SuppressLint;
22 import android.graphics.ColorSpace;
23 import android.hardware.camera2.CameraCharacteristics;
24 import android.hardware.camera2.CameraExtensionCharacteristics.Extension;
25 import android.hardware.camera2.CameraExtensionSession;
26 import android.media.ImageReader;
27 
28 import com.android.internal.camera.flags.Flags;
29 
30 import java.util.List;
31 import java.util.concurrent.Executor;
32 
33 /**
34  * A class that aggregates all supported arguments for
35  * {@link CameraExtensionSession} initialization.
36  */
37 public final class ExtensionSessionConfiguration {
38     private static final String TAG = "ExtensionSessionConfiguration";
39 
40     private int mExtensionType;
41     private List<OutputConfiguration> mOutputs;
42     private OutputConfiguration mPostviewOutput = null;
43     private Executor mExecutor = null;
44     private CameraExtensionSession.StateCallback mCallback = null;
45     private int mColorSpace;
46 
47     /**
48      * Create a new ExtensionSessionConfiguration
49      *
50      * @param extension to be used for processing
51      * @param outputs   a list of output configurations for the capture session
52      * @param executor  the executor which will be used for invoking the callbacks
53      * @param listener  callbacks to be invoked when the state of the
54      *                  CameraExtensionSession changes
55      */
ExtensionSessionConfiguration(@xtension int extension, @NonNull List<OutputConfiguration> outputs, @NonNull Executor executor, @NonNull CameraExtensionSession.StateCallback listener)56     public ExtensionSessionConfiguration(@Extension int extension,
57             @NonNull List<OutputConfiguration> outputs, @NonNull Executor executor,
58             @NonNull CameraExtensionSession.StateCallback listener) {
59         mExtensionType = extension;
60         mOutputs = outputs;
61         mExecutor = executor;
62         mCallback = listener;
63     }
64 
65     /**
66      * Retrieve the extension type.
67      *
68      * @return the extension type.
69      */
70     public @Extension
getExtension()71     int getExtension() {
72         return mExtensionType;
73     }
74 
75     /**
76      * Set the postview for still capture output configuration.
77      *
78      * @param postviewOutput output configuration for postview
79      * @see android.hardware.camera2.CameraExtensionCharacteristics#isPostviewAvailable
80      */
81     public
setPostviewOutputConfiguration(@ullable OutputConfiguration postviewOutput)82     void setPostviewOutputConfiguration(@Nullable OutputConfiguration postviewOutput) {
83         mPostviewOutput = postviewOutput;
84     }
85 
86     /**
87      * Get the postview for still capture output configuration.
88      *
89      * @return output configuration for postview
90      * @see android.hardware.camera2.CameraExtensionCharacteristics#isPostviewAvailable
91      */
92     public @Nullable // Postview output is optional
getPostviewOutputConfiguration()93     OutputConfiguration getPostviewOutputConfiguration() {
94         return mPostviewOutput;
95     }
96 
97     /**
98      * Retrieve the {@link OutputConfiguration} list for the capture
99      * session.
100      *
101      * @return A list of output configurations for the capture session.
102      */
103     public @NonNull
getOutputConfigurations()104     List<OutputConfiguration> getOutputConfigurations() {
105         return mOutputs;
106     }
107 
108     /**
109      * Retrieve the CameraCaptureSession.StateCallback
110      * listener.
111      *
112      * @return A state callback interface implementation.
113      */
114     public @NonNull
getStateCallback()115     CameraExtensionSession.StateCallback getStateCallback() {
116         return mCallback;
117     }
118 
119     /**
120      * Retrieve the Executor for the CameraExtensionSession instance.
121      *
122      * @return The Executor on which the callback will be invoked.
123      */
124     public @NonNull
getExecutor()125     Executor getExecutor() {
126         return mExecutor;
127     }
128 
129     /**
130      * Set a specific device-supported color space.
131      *
132      * <p>Clients can choose from any profile advertised as supported in
133      * {@link CameraCharacteristics#REQUEST_AVAILABLE_COLOR_SPACE_PROFILES}
134      * queried using {@link ColorSpaceProfiles#getSupportedColorSpaces}.
135      * When set, the colorSpace will override the default color spaces of the output targets,
136      * or the color space implied by the dataSpace passed into an {@link ImageReader}'s
137      * constructor.</p>
138      */
139     @FlaggedApi(Flags.FLAG_EXTENSION_10_BIT)
setColorSpace(@onNull ColorSpace.Named colorSpace)140     public void setColorSpace(@NonNull ColorSpace.Named colorSpace) {
141         mColorSpace = colorSpace.ordinal();
142         for (OutputConfiguration outputConfiguration : mOutputs) {
143             outputConfiguration.setColorSpace(colorSpace);
144         }
145         if (mPostviewOutput != null) {
146             mPostviewOutput.setColorSpace(colorSpace);
147         }
148     }
149 
150     /**
151      * Clear the color space, such that the default color space will be used.
152      */
153     @FlaggedApi(Flags.FLAG_EXTENSION_10_BIT)
clearColorSpace()154     public void clearColorSpace() {
155         mColorSpace = ColorSpaceProfiles.UNSPECIFIED;
156         for (OutputConfiguration outputConfiguration : mOutputs) {
157             outputConfiguration.clearColorSpace();
158         }
159         if (mPostviewOutput != null) {
160             mPostviewOutput.clearColorSpace();
161         }
162     }
163 
164     /**
165      * Return the current color space.
166      *
167      * @return the currently set color space, or null
168      *         if not set
169      */
170     @FlaggedApi(Flags.FLAG_EXTENSION_10_BIT)
171     @SuppressLint("MethodNameUnits")
getColorSpace()172     public @Nullable ColorSpace getColorSpace() {
173         if (mColorSpace != ColorSpaceProfiles.UNSPECIFIED) {
174             return ColorSpace.get(ColorSpace.Named.values()[mColorSpace]);
175         } else {
176             return null;
177         }
178     }
179 }
180