blob: 639ad6054a325ebe30ae85cb21e567b7c385a314 [file] [log] [blame]
Igor Murashkin9c595172014-05-12 13:56:20 -07001/*
2 * Copyright (C) 2014 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
17package android.hardware.camera2.params;
18
19import android.graphics.ImageFormat;
20import android.graphics.PixelFormat;
21import android.hardware.camera2.CameraCharacteristics;
22import android.hardware.camera2.CameraDevice;
Zhijun He3c1ff682015-06-23 09:21:43 -070023import android.hardware.camera2.CameraMetadata;
Igor Murashkin9c595172014-05-12 13:56:20 -070024import android.hardware.camera2.CaptureRequest;
25import android.hardware.camera2.utils.HashCodeHelpers;
Eino-Ville Talvalae3651202015-06-19 17:29:14 -070026import android.hardware.camera2.utils.SurfaceUtils;
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -080027import android.hardware.camera2.legacy.LegacyCameraDevice;
28import android.hardware.camera2.legacy.LegacyMetadataMapper;
Igor Murashkin9c595172014-05-12 13:56:20 -070029import android.view.Surface;
Yin-Chia Yeh12da1402014-07-15 10:37:31 -070030import android.util.Range;
Igor Murashkin9c595172014-05-12 13:56:20 -070031import android.util.Size;
Eino-Ville Talvala0819c752015-06-17 11:34:41 -070032import android.util.SparseIntArray;
Igor Murashkin9c595172014-05-12 13:56:20 -070033
34import java.util.Arrays;
35import java.util.HashMap;
36import java.util.Objects;
Yin-Chia Yehb0056642014-07-28 13:17:05 -070037import java.util.Set;
Igor Murashkin9c595172014-05-12 13:56:20 -070038
39import static com.android.internal.util.Preconditions.*;
40
41/**
42 * Immutable class to store the available stream
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -070043 * {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP configurations} to set up
44 * {@link android.view.Surface Surfaces} for creating a
45 * {@link android.hardware.camera2.CameraCaptureSession capture session} with
46 * {@link android.hardware.camera2.CameraDevice#createCaptureSession}.
Igor Murashkin9c595172014-05-12 13:56:20 -070047 * <!-- TODO: link to input stream configuration -->
48 *
49 * <p>This is the authoritative list for all <!-- input/ -->output formats (and sizes respectively
50 * for that format) that are supported by a camera device.</p>
51 *
52 * <p>This also contains the minimum frame durations and stall durations for each format/size
53 * combination that can be used to calculate effective frame rate when submitting multiple captures.
54 * </p>
55 *
56 * <p>An instance of this object is available from {@link CameraCharacteristics} using
57 * the {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP} key and the
58 * {@link CameraCharacteristics#get} method.</p>
59 *
60 * <pre><code>{@code
61 * CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
62 * StreamConfigurationMap configs = characteristics.get(
63 * CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
64 * }</code></pre>
65 *
66 * @see CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -070067 * @see CameraDevice#createCaptureSession
Igor Murashkin9c595172014-05-12 13:56:20 -070068 */
69public final class StreamConfigurationMap {
70
71 private static final String TAG = "StreamConfigurationMap";
Ruben Brunk3e4fed22014-06-18 17:08:42 -070072
73 /**
Igor Murashkin9c595172014-05-12 13:56:20 -070074 * Create a new {@link StreamConfigurationMap}.
75 *
76 * <p>The array parameters ownership is passed to this object after creation; do not
77 * write to them after this constructor is invoked.</p>
78 *
79 * @param configurations a non-{@code null} array of {@link StreamConfiguration}
80 * @param minFrameDurations a non-{@code null} array of {@link StreamConfigurationDuration}
81 * @param stallDurations a non-{@code null} array of {@link StreamConfigurationDuration}
Yin-Chia Yeh12da1402014-07-15 10:37:31 -070082 * @param highSpeedVideoConfigurations an array of {@link HighSpeedVideoConfiguration}, null if
83 * camera device does not support high speed video recording
Eino-Ville Talvala0819c752015-06-17 11:34:41 -070084 * @param listHighResolution a flag indicating whether the device supports BURST_CAPTURE
85 * and thus needs a separate list of slow high-resolution output sizes
Yin-Chia Yeh12da1402014-07-15 10:37:31 -070086 * @throws NullPointerException if any of the arguments except highSpeedVideoConfigurations
87 * were {@code null} or any subelements were {@code null}
Igor Murashkin9c595172014-05-12 13:56:20 -070088 *
89 * @hide
90 */
91 public StreamConfigurationMap(
92 StreamConfiguration[] configurations,
93 StreamConfigurationDuration[] minFrameDurations,
Yin-Chia Yeh12da1402014-07-15 10:37:31 -070094 StreamConfigurationDuration[] stallDurations,
Eino-Ville Talvala456432e2015-03-05 15:42:49 -080095 StreamConfiguration[] depthConfigurations,
96 StreamConfigurationDuration[] depthMinFrameDurations,
97 StreamConfigurationDuration[] depthStallDurations,
Chien-Yu Chen0a551f12015-04-03 17:57:35 -070098 HighSpeedVideoConfiguration[] highSpeedVideoConfigurations,
Eino-Ville Talvala0819c752015-06-17 11:34:41 -070099 ReprocessFormatsMap inputOutputFormatsMap,
100 boolean listHighResolution) {
Igor Murashkin9c595172014-05-12 13:56:20 -0700101 mConfigurations = checkArrayElementsNotNull(configurations, "configurations");
102 mMinFrameDurations = checkArrayElementsNotNull(minFrameDurations, "minFrameDurations");
103 mStallDurations = checkArrayElementsNotNull(stallDurations, "stallDurations");
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700104 mListHighResolution = listHighResolution;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800105
106 if (depthConfigurations == null) {
107 mDepthConfigurations = new StreamConfiguration[0];
108 mDepthMinFrameDurations = new StreamConfigurationDuration[0];
109 mDepthStallDurations = new StreamConfigurationDuration[0];
110 } else {
111 mDepthConfigurations = checkArrayElementsNotNull(depthConfigurations,
112 "depthConfigurations");
113 mDepthMinFrameDurations = checkArrayElementsNotNull(depthMinFrameDurations,
114 "depthMinFrameDurations");
115 mDepthStallDurations = checkArrayElementsNotNull(depthStallDurations,
116 "depthStallDurations");
117 }
118
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700119 if (highSpeedVideoConfigurations == null) {
120 mHighSpeedVideoConfigurations = new HighSpeedVideoConfiguration[0];
121 } else {
122 mHighSpeedVideoConfigurations = checkArrayElementsNotNull(
123 highSpeedVideoConfigurations, "highSpeedVideoConfigurations");
124 }
Igor Murashkin9c595172014-05-12 13:56:20 -0700125
126 // For each format, track how many sizes there are available to configure
127 for (StreamConfiguration config : configurations) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700128 int fmt = config.getFormat();
129 SparseIntArray map = null;
130 if (config.isOutput()) {
131 mAllOutputFormats.put(fmt, mAllOutputFormats.get(fmt) + 1);
132 long duration = 0;
133 if (mListHighResolution) {
134 for (StreamConfigurationDuration configurationDuration : mMinFrameDurations) {
135 if (configurationDuration.getFormat() == fmt &&
136 configurationDuration.getWidth() == config.getSize().getWidth() &&
137 configurationDuration.getHeight() == config.getSize().getHeight()) {
138 duration = configurationDuration.getDuration();
139 break;
140 }
141 }
142 }
143 map = duration <= DURATION_20FPS_NS ?
144 mOutputFormats : mHighResOutputFormats;
145 } else {
146 map = mInputFormats;
Igor Murashkin9c595172014-05-12 13:56:20 -0700147 }
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700148 map.put(fmt, map.get(fmt) + 1);
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800149 }
150
151 // For each depth format, track how many sizes there are available to configure
152 for (StreamConfiguration config : mDepthConfigurations) {
153 if (!config.isOutput()) {
154 // Ignoring input depth configs
155 continue;
156 }
157
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700158 mDepthOutputFormats.put(config.getFormat(),
159 mDepthOutputFormats.get(config.getFormat()) + 1);
Igor Murashkin9c595172014-05-12 13:56:20 -0700160 }
161
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700162 if (mOutputFormats.indexOfKey(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) < 0) {
Igor Murashkin9c595172014-05-12 13:56:20 -0700163 throw new AssertionError(
164 "At least one stream configuration for IMPLEMENTATION_DEFINED must exist");
165 }
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700166
167 // For each Size/FPS range, track how many FPS range/Size there are available
168 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
169 Size size = config.getSize();
170 Range<Integer> fpsRange = config.getFpsRange();
171 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size);
172 if (fpsRangeCount == null) {
173 fpsRangeCount = 0;
174 }
175 mHighSpeedVideoSizeMap.put(size, fpsRangeCount + 1);
176 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange);
177 if (sizeCount == null) {
178 sizeCount = 0;
179 }
180 mHighSpeedVideoFpsRangeMap.put(fpsRange, sizeCount + 1);
181 }
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700182
183 mInputOutputFormatsMap = inputOutputFormatsMap;
Igor Murashkin9c595172014-05-12 13:56:20 -0700184 }
185
186 /**
187 * Get the image {@code format} output formats in this stream configuration.
188 *
189 * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
190 * or in {@link PixelFormat} (and there is no possibility of collision).</p>
191 *
192 * <p>Formats listed in this array are guaranteed to return true if queried with
Eino-Ville Talvalaf3621f32014-08-26 14:53:39 -0700193 * {@link #isOutputSupportedFor(int)}.</p>
Igor Murashkin9c595172014-05-12 13:56:20 -0700194 *
195 * @return an array of integer format
196 *
197 * @see ImageFormat
198 * @see PixelFormat
199 */
200 public final int[] getOutputFormats() {
201 return getPublicFormats(/*output*/true);
202 }
203
204 /**
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700205 * Get the image {@code format} output formats for a reprocessing input format.
206 *
207 * <p>When submitting a {@link CaptureRequest} with an input Surface of a given format,
208 * the only allowed target outputs of the {@link CaptureRequest} are the ones with a format
209 * listed in the return value of this method. Including any other output Surface as a target
210 * will throw an IllegalArgumentException. If no output format is supported given the input
211 * format, an empty int[] will be returned.</p>
212 *
213 * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
214 * or in {@link PixelFormat} (and there is no possibility of collision).</p>
215 *
216 * <p>Formats listed in this array are guaranteed to return true if queried with
217 * {@link #isOutputSupportedFor(int)}.</p>
218 *
219 * @return an array of integer format
220 *
221 * @see ImageFormat
222 * @see PixelFormat
223 */
224 public final int[] getValidOutputFormatsForInput(int inputFormat) {
225 if (mInputOutputFormatsMap == null) {
226 return new int[0];
227 }
228 return mInputOutputFormatsMap.getOutputs(inputFormat);
229 }
230
231 /**
Igor Murashkin9c595172014-05-12 13:56:20 -0700232 * Get the image {@code format} input formats in this stream configuration.
233 *
234 * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
235 * or in {@link PixelFormat} (and there is no possibility of collision).</p>
236 *
237 * @return an array of integer format
238 *
239 * @see ImageFormat
240 * @see PixelFormat
Igor Murashkin9c595172014-05-12 13:56:20 -0700241 */
242 public final int[] getInputFormats() {
243 return getPublicFormats(/*output*/false);
244 }
245
246 /**
247 * Get the supported input sizes for this input format.
248 *
249 * <p>The format must have come from {@link #getInputFormats}; otherwise
250 * {@code null} is returned.</p>
251 *
252 * @param format a format from {@link #getInputFormats}
253 * @return a non-empty array of sizes, or {@code null} if the format was not available.
Igor Murashkin9c595172014-05-12 13:56:20 -0700254 */
255 public Size[] getInputSizes(final int format) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700256 return getPublicFormatSizes(format, /*output*/false, /*highRes*/false);
Igor Murashkin9c595172014-05-12 13:56:20 -0700257 }
258
259 /**
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700260 * Determine whether or not output surfaces with a particular user-defined format can be passed
261 * {@link CameraDevice#createCaptureSession createCaptureSession}.
Igor Murashkin9c595172014-05-12 13:56:20 -0700262 *
263 * <p>This method determines that the output {@code format} is supported by the camera device;
264 * each output {@code surface} target may or may not itself support that {@code format}.
265 * Refer to the class which provides the surface for additional documentation.</p>
266 *
267 * <p>Formats for which this returns {@code true} are guaranteed to exist in the result
268 * returned by {@link #getOutputSizes}.</p>
269 *
270 * @param format an image format from either {@link ImageFormat} or {@link PixelFormat}
271 * @return
272 * {@code true} iff using a {@code surface} with this {@code format} will be
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700273 * supported with {@link CameraDevice#createCaptureSession}
Igor Murashkin9c595172014-05-12 13:56:20 -0700274 *
275 * @throws IllegalArgumentException
276 * if the image format was not a defined named constant
277 * from either {@link ImageFormat} or {@link PixelFormat}
278 *
279 * @see ImageFormat
280 * @see PixelFormat
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700281 * @see CameraDevice#createCaptureSession
Igor Murashkin9c595172014-05-12 13:56:20 -0700282 */
283 public boolean isOutputSupportedFor(int format) {
284 checkArgumentFormat(format);
285
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800286 int internalFormat = imageFormatToInternal(format);
287 int dataspace = imageFormatToDataspace(format);
288 if (dataspace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700289 return mDepthOutputFormats.indexOfKey(internalFormat) >= 0;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800290 } else {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700291 return getFormatsMap(/*output*/true).indexOfKey(internalFormat) >= 0;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800292 }
Igor Murashkin9c595172014-05-12 13:56:20 -0700293 }
294
295 /**
296 * Determine whether or not output streams can be configured with a particular class
297 * as a consumer.
298 *
299 * <p>The following list is generally usable for outputs:
300 * <ul>
301 * <li>{@link android.media.ImageReader} -
302 * Recommended for image processing or streaming to external resources (such as a file or
303 * network)
304 * <li>{@link android.media.MediaRecorder} -
305 * Recommended for recording video (simple to use)
306 * <li>{@link android.media.MediaCodec} -
307 * Recommended for recording video (more complicated to use, with more flexibility)
308 * <li>{@link android.renderscript.Allocation} -
309 * Recommended for image processing with {@link android.renderscript RenderScript}
310 * <li>{@link android.view.SurfaceHolder} -
311 * Recommended for low-power camera preview with {@link android.view.SurfaceView}
312 * <li>{@link android.graphics.SurfaceTexture} -
313 * Recommended for OpenGL-accelerated preview processing or compositing with
314 * {@link android.view.TextureView}
315 * </ul>
316 * </p>
317 *
318 * <p>Generally speaking this means that creating a {@link Surface} from that class <i>may</i>
319 * provide a producer endpoint that is suitable to be used with
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700320 * {@link CameraDevice#createCaptureSession}.</p>
Igor Murashkin9c595172014-05-12 13:56:20 -0700321 *
322 * <p>Since not all of the above classes support output of all format and size combinations,
323 * the particular combination should be queried with {@link #isOutputSupportedFor(Surface)}.</p>
324 *
325 * @param klass a non-{@code null} {@link Class} object reference
326 * @return {@code true} if this class is supported as an output, {@code false} otherwise
327 *
328 * @throws NullPointerException if {@code klass} was {@code null}
329 *
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700330 * @see CameraDevice#createCaptureSession
Igor Murashkin9c595172014-05-12 13:56:20 -0700331 * @see #isOutputSupportedFor(Surface)
332 */
333 public static <T> boolean isOutputSupportedFor(Class<T> klass) {
334 checkNotNull(klass, "klass must not be null");
335
336 if (klass == android.media.ImageReader.class) {
337 return true;
338 } else if (klass == android.media.MediaRecorder.class) {
339 return true;
340 } else if (klass == android.media.MediaCodec.class) {
341 return true;
342 } else if (klass == android.renderscript.Allocation.class) {
343 return true;
344 } else if (klass == android.view.SurfaceHolder.class) {
345 return true;
346 } else if (klass == android.graphics.SurfaceTexture.class) {
347 return true;
348 }
349
350 return false;
351 }
352
353 /**
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700354 * Determine whether or not the {@code surface} in its current state is suitable to be included
355 * in a {@link CameraDevice#createCaptureSession capture session} as an output.
Igor Murashkin9c595172014-05-12 13:56:20 -0700356 *
357 * <p>Not all surfaces are usable with the {@link CameraDevice}, and not all configurations
358 * of that {@code surface} are compatible. Some classes that provide the {@code surface} are
359 * compatible with the {@link CameraDevice} in general
360 * (see {@link #isOutputSupportedFor(Class)}, but it is the caller's responsibility to put the
361 * {@code surface} into a state that will be compatible with the {@link CameraDevice}.</p>
362 *
363 * <p>Reasons for a {@code surface} being specifically incompatible might be:
364 * <ul>
365 * <li>Using a format that's not listed by {@link #getOutputFormats}
366 * <li>Using a format/size combination that's not listed by {@link #getOutputSizes}
367 * <li>The {@code surface} itself is not in a state where it can service a new producer.</p>
368 * </li>
369 * </ul>
370 *
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800371 * <p>Surfaces from flexible sources will return true even if the exact size of the Surface does
372 * not match a camera-supported size, as long as the format (or class) is supported and the
373 * camera device supports a size that is equal to or less than 1080p in that format. If such as
374 * Surface is used to create a capture session, it will have its size rounded to the nearest
375 * supported size, below or equal to 1080p. Flexible sources include SurfaceView, SurfaceTexture,
376 * and ImageReader.</p>
377 *
378 * <p>This is not an exhaustive list; see the particular class's documentation for further
Igor Murashkin9c595172014-05-12 13:56:20 -0700379 * possible reasons of incompatibility.</p>
380 *
381 * @param surface a non-{@code null} {@link Surface} object reference
382 * @return {@code true} if this is supported, {@code false} otherwise
383 *
384 * @throws NullPointerException if {@code surface} was {@code null}
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800385 * @throws IllegalArgumentException if the Surface endpoint is no longer valid
Igor Murashkin9c595172014-05-12 13:56:20 -0700386 *
Eino-Ville Talvalab67a3b32014-06-06 13:07:20 -0700387 * @see CameraDevice#createCaptureSession
Igor Murashkin9c595172014-05-12 13:56:20 -0700388 * @see #isOutputSupportedFor(Class)
389 */
390 public boolean isOutputSupportedFor(Surface surface) {
391 checkNotNull(surface, "surface must not be null");
392
Eino-Ville Talvalae3651202015-06-19 17:29:14 -0700393 Size surfaceSize = SurfaceUtils.getSurfaceSize(surface);
394 int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface);
395 int surfaceDataspace = SurfaceUtils.getSurfaceDataspace(surface);
Igor Murashkin9c595172014-05-12 13:56:20 -0700396
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800397 // See if consumer is flexible.
Eino-Ville Talvalae3651202015-06-19 17:29:14 -0700398 boolean isFlexible = SurfaceUtils.isFlexibleConsumer(surface);
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800399
400 // Override RGB formats to IMPLEMENTATION_DEFINED, b/9487482
401 if ((surfaceFormat >= LegacyMetadataMapper.HAL_PIXEL_FORMAT_RGBA_8888 &&
402 surfaceFormat <= LegacyMetadataMapper.HAL_PIXEL_FORMAT_BGRA_8888)) {
Eino-Ville Talvalae3651202015-06-19 17:29:14 -0700403 surfaceFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800404 }
405
Eino-Ville Talvalae3651202015-06-19 17:29:14 -0700406 StreamConfiguration[] configs =
407 surfaceDataspace != HAL_DATASPACE_DEPTH ? mConfigurations : mDepthConfigurations;
408 for (StreamConfiguration config : configs) {
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800409 if (config.getFormat() == surfaceFormat && config.isOutput()) {
Eino-Ville Talvalae3651202015-06-19 17:29:14 -0700410 // Matching format, either need exact size match, or a flexible consumer
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -0800411 // and a size no bigger than MAX_DIMEN_FOR_ROUNDING
412 if (config.getSize().equals(surfaceSize)) {
413 return true;
414 } else if (isFlexible &&
415 (config.getSize().getWidth() <= LegacyCameraDevice.MAX_DIMEN_FOR_ROUNDING)) {
416 return true;
417 }
418 }
419 }
420 return false;
Igor Murashkin9c595172014-05-12 13:56:20 -0700421 }
422
423 /**
424 * Get a list of sizes compatible with {@code klass} to use as an output.
425 *
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700426 * <p>Some of the supported classes may support additional formats beyond
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700427 * {@link ImageFormat#PRIVATE}; this function only returns
428 * sizes for {@link ImageFormat#PRIVATE}. For example, {@link android.media.ImageReader}
429 * supports {@link ImageFormat#YUV_420_888} and {@link ImageFormat#PRIVATE}, this method will
430 * only return the sizes for {@link ImageFormat#PRIVATE} for {@link android.media.ImageReader}
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700431 * class.</p>
Igor Murashkin9c595172014-05-12 13:56:20 -0700432 *
433 * <p>If a well-defined format such as {@code NV21} is required, use
434 * {@link #getOutputSizes(int)} instead.</p>
435 *
436 * <p>The {@code klass} should be a supported output, that querying
437 * {@code #isOutputSupportedFor(Class)} should return {@code true}.</p>
438 *
439 * @param klass
440 * a non-{@code null} {@link Class} object reference
441 * @return
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700442 * an array of supported sizes for {@link ImageFormat#PRIVATE} format,
443 * or {@code null} iff the {@code klass} is not a supported output.
444 *
Igor Murashkin9c595172014-05-12 13:56:20 -0700445 *
446 * @throws NullPointerException if {@code klass} was {@code null}
447 *
448 * @see #isOutputSupportedFor(Class)
449 */
450 public <T> Size[] getOutputSizes(Class<T> klass) {
451 if (isOutputSupportedFor(klass) == false) {
452 return null;
453 }
454
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800455 return getInternalFormatSizes(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700456 HAL_DATASPACE_UNKNOWN,/*output*/true, /*highRes*/false);
Igor Murashkin9c595172014-05-12 13:56:20 -0700457 }
458
459 /**
460 * Get a list of sizes compatible with the requested image {@code format}.
461 *
462 * <p>The {@code format} should be a supported format (one of the formats returned by
463 * {@link #getOutputFormats}).</p>
464 *
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700465 * As of API level 23, the {@link #getHighResolutionOutputSizes} method can be used on devices
466 * that support the
467 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE}
468 * capability to get a list of high-resolution output sizes that cannot operate at the preferred
469 * 20fps rate. This means that for some supported formats, this method will return an empty
470 * list, if all the supported resolutions operate at below 20fps. For devices that do not
471 * support the BURST_CAPTURE capability, all output resolutions are listed through this method.
472 *
Igor Murashkin9c595172014-05-12 13:56:20 -0700473 * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
474 * @return
475 * an array of supported sizes,
476 * or {@code null} if the {@code format} is not a supported output
477 *
478 * @see ImageFormat
479 * @see PixelFormat
480 * @see #getOutputFormats
481 */
482 public Size[] getOutputSizes(int format) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700483 return getPublicFormatSizes(format, /*output*/true, /*highRes*/ false);
Igor Murashkin9c595172014-05-12 13:56:20 -0700484 }
485
486 /**
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700487 * Get a list of supported high speed video recording sizes.
Zhijun He3c1ff682015-06-23 09:21:43 -0700488 * <p>
489 * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is
490 * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will
491 * list the supported high speed video size configurations. All the sizes listed will be a
492 * subset of the sizes reported by {@link #getOutputSizes} for processed non-stalling formats
493 * (typically {@link ImageFormat#PRIVATE} {@link ImageFormat#YUV_420_888}, etc.)
494 * </p>
495 * <p>
496 * To enable high speed video recording, application must create a constrained create high speed
497 * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit
498 * a CaptureRequest list created by {@link CameraDevice#createConstrainedHighSpeedRequestList}
499 * to this session. The application must select the video size from this method and
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700500 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from
Zhijun He3c1ff682015-06-23 09:21:43 -0700501 * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and
502 * generate the high speed request list. For example, if the application intends to do high
503 * speed recording, it can select the maximum size reported by this method to create high speed
504 * capture session. Note that for the use case of multiple output streams, application must
505 * select one unique size from this method to use (e.g., preview and recording streams must have
506 * the same size). Otherwise, the high speed session creation will fail. Once the size is
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700507 * selected, application can get the supported FPS ranges by
508 * {@link #getHighSpeedVideoFpsRangesFor}, and use these FPS ranges to setup the recording
Zhijun He3c1ff682015-06-23 09:21:43 -0700509 * request lists via {@link CameraDevice#createConstrainedHighSpeedRequestList}.
510 * </p>
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700511 *
Zhijun He3c1ff682015-06-23 09:21:43 -0700512 * @return an array of supported high speed video recording sizes
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700513 * @see #getHighSpeedVideoFpsRangesFor(Size)
Zhijun He3c1ff682015-06-23 09:21:43 -0700514 * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO
515 * @see CameraDevice#createConstrainedHighSpeedCaptureSession
516 * @see CameraDevice#createConstrainedHighSpeedRequestList
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700517 */
518 public Size[] getHighSpeedVideoSizes() {
Yin-Chia Yehb0056642014-07-28 13:17:05 -0700519 Set<Size> keySet = mHighSpeedVideoSizeMap.keySet();
520 return keySet.toArray(new Size[keySet.size()]);
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700521 }
522
523 /**
524 * Get the frame per second ranges (fpsMin, fpsMax) for input high speed video size.
Zhijun He3c1ff682015-06-23 09:21:43 -0700525 * <p>
526 * See {@link #getHighSpeedVideoFpsRanges} for how to enable high speed recording.
527 * </p>
528 * <p>
529 * The {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS ranges} reported in this method
530 * must not be used to setup capture requests that are submitted to unconstrained capture
531 * sessions, or it will result in {@link IllegalArgumentException IllegalArgumentExceptions}.
532 * </p>
533 * <p>
534 * See {@link #getHighSpeedVideoFpsRanges} for the characteristics of the returned FPS ranges.
535 * </p>
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700536 *
537 * @param size one of the sizes returned by {@link #getHighSpeedVideoSizes()}
Zhijun He3c1ff682015-06-23 09:21:43 -0700538 * @return an array of supported high speed video recording FPS ranges The upper bound of
539 * returned ranges is guaranteed to be greater than or equal to 120.
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700540 * @throws IllegalArgumentException if input size does not exist in the return value of
Zhijun He3c1ff682015-06-23 09:21:43 -0700541 * getHighSpeedVideoSizes
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700542 * @see #getHighSpeedVideoSizes()
Zhijun He3c1ff682015-06-23 09:21:43 -0700543 * @see #getHighSpeedVideoFpsRanges()
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700544 */
545 public Range<Integer>[] getHighSpeedVideoFpsRangesFor(Size size) {
546 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size);
547 if (fpsRangeCount == null || fpsRangeCount == 0) {
548 throw new IllegalArgumentException(String.format(
549 "Size %s does not support high speed video recording", size));
550 }
551
552 @SuppressWarnings("unchecked")
553 Range<Integer>[] fpsRanges = new Range[fpsRangeCount];
554 int i = 0;
555 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
556 if (size.equals(config.getSize())) {
557 fpsRanges[i++] = config.getFpsRange();
558 }
559 }
560 return fpsRanges;
561 }
562
563 /**
564 * Get a list of supported high speed video recording FPS ranges.
Zhijun He3c1ff682015-06-23 09:21:43 -0700565 * <p>
566 * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is
567 * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will
568 * list the supported high speed video FPS range configurations. Application can then use
569 * {@link #getHighSpeedVideoSizesFor} to query available sizes for one of returned FPS range.
570 * </p>
571 * <p>
572 * To enable high speed video recording, application must create a constrained create high speed
573 * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit
574 * a CaptureRequest list created by {@link CameraDevice#createConstrainedHighSpeedRequestList}
575 * to this session. The application must select the video size from this method and
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700576 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from
Zhijun He3c1ff682015-06-23 09:21:43 -0700577 * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and
578 * generate the high speed request list. For example, if the application intends to do high
579 * speed recording, it can select one FPS range reported by this method, query the video sizes
580 * corresponding to this FPS range by {@link #getHighSpeedVideoSizesFor} and use one of reported
581 * sizes to create a high speed capture session. Note that for the use case of multiple output
582 * streams, application must select one unique size from this method to use (e.g., preview and
583 * recording streams must have the same size). Otherwise, the high speed session creation will
584 * fail. Once the high speed capture session is created, the application can set the FPS range
585 * in the recording request lists via
586 * {@link CameraDevice#createConstrainedHighSpeedRequestList}.
587 * </p>
588 * <p>
589 * The FPS ranges reported by this method will have below characteristics:
590 * <li>The fpsMin and fpsMax will be a multiple 30fps.</li>
591 * <li>The fpsMin will be no less than 30fps, the fpsMax will be no less than 120fps.</li>
592 * <li>At least one range will be a fixed FPS range where fpsMin == fpsMax.</li>
593 * <li>For each fixed FPS range, there will be one corresponding variable FPS range [30,
594 * fps_max]. These kinds of FPS ranges are suitable for preview-only use cases where the
595 * application doesn't want the camera device always produce higher frame rate than the display
596 * refresh rate.</li>
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700597 * </p>
598 *
Zhijun He3c1ff682015-06-23 09:21:43 -0700599 * @return an array of supported high speed video recording FPS ranges The upper bound of
600 * returned ranges is guaranteed to be larger or equal to 120.
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700601 * @see #getHighSpeedVideoSizesFor
Zhijun He3c1ff682015-06-23 09:21:43 -0700602 * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO
603 * @see CameraDevice#createConstrainedHighSpeedCaptureSession
604 * @see CameraDevice#createConstrainedHighSpeedRequestList
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700605 */
606 @SuppressWarnings("unchecked")
607 public Range<Integer>[] getHighSpeedVideoFpsRanges() {
Yin-Chia Yehb0056642014-07-28 13:17:05 -0700608 Set<Range<Integer>> keySet = mHighSpeedVideoFpsRangeMap.keySet();
609 return keySet.toArray(new Range[keySet.size()]);
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700610 }
611
612 /**
Zhijun He3c1ff682015-06-23 09:21:43 -0700613 * Get the supported video sizes for an input high speed FPS range.
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700614 *
Zhijun He3c1ff682015-06-23 09:21:43 -0700615 * <p> See {@link #getHighSpeedVideoSizes} for how to enable high speed recording.</p>
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700616 *
617 * @param fpsRange one of the FPS range returned by {@link #getHighSpeedVideoFpsRanges()}
Zhijun He3c1ff682015-06-23 09:21:43 -0700618 * @return An array of video sizes to create high speed capture sessions for high speed streaming
619 * use cases.
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700620 *
621 * @throws IllegalArgumentException if input FPS range does not exist in the return value of
622 * getHighSpeedVideoFpsRanges
623 * @see #getHighSpeedVideoFpsRanges()
624 */
625 public Size[] getHighSpeedVideoSizesFor(Range<Integer> fpsRange) {
626 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange);
627 if (sizeCount == null || sizeCount == 0) {
628 throw new IllegalArgumentException(String.format(
629 "FpsRange %s does not support high speed video recording", fpsRange));
630 }
631
632 Size[] sizes = new Size[sizeCount];
633 int i = 0;
634 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
635 if (fpsRange.equals(config.getFpsRange())) {
636 sizes[i++] = config.getSize();
637 }
638 }
639 return sizes;
640 }
641
642 /**
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700643 * Get a list of supported high resolution sizes, which cannot operate at full BURST_CAPTURE
644 * rate.
645 *
646 * <p>This includes all output sizes that cannot meet the 20 fps frame rate requirements for the
647 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE}
648 * capability. This does not include the stall duration, so for example, a JPEG or RAW16 output
649 * resolution with a large stall duration but a minimum frame duration that's above 20 fps will
650 * still be listed in the regular {@link #getOutputSizes} list. All the sizes on this list are
651 * still guaranteed to operate at a rate of at least 10 fps, not including stall duration.</p>
652 *
653 * <p>For a device that does not support the BURST_CAPTURE capability, this list will be
654 * {@code null}, since resolutions in the {@link #getOutputSizes} list are already not
655 * guaranteed to meet &gt;= 20 fps rate requirements. For a device that does support the
656 * BURST_CAPTURE capability, this list may be empty, if all supported resolutions meet the 20
657 * fps requirement.</p>
658 *
659 * @return an array of supported slower high-resolution sizes, or {@code null} if the
660 * BURST_CAPTURE capability is not supported
661 */
662 public Size[] getHighResolutionOutputSizes(int format) {
663 if (!mListHighResolution) return null;
664
665 return getPublicFormatSizes(format, /*output*/true, /*highRes*/ true);
666 }
667
668 /**
Igor Murashkin9c595172014-05-12 13:56:20 -0700669 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration}
670 * for the format/size combination (in nanoseconds).
671 *
672 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p>
673 * <p>{@code size} should be one of the ones returned by
674 * {@link #getOutputSizes(int)}.</p>
675 *
676 * <p>This should correspond to the frame duration when only that stream is active, with all
677 * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}.
678 * </p>
679 *
680 * <p>When multiple streams are used in a request, the minimum frame duration will be
681 * {@code max(individual stream min durations)}.</p>
682 *
Eino-Ville Talvala08bc3b02014-07-09 10:07:36 -0700683 * <p>For devices that do not support manual sensor control
684 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}),
Eino-Ville Talvalaabd9d3c2014-07-28 13:01:52 -0700685 * this function may return 0.</p>
Eino-Ville Talvala08bc3b02014-07-09 10:07:36 -0700686 *
Igor Murashkin9c595172014-05-12 13:56:20 -0700687 * <!--
688 * TODO: uncomment after adding input stream support
689 * <p>The minimum frame duration of a stream (of a particular format, size) is the same
690 * regardless of whether the stream is input or output.</p>
691 * -->
692 *
693 * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
694 * @param size an output-compatible size
Ruben Brunk3e4fed22014-06-18 17:08:42 -0700695 * @return a minimum frame duration {@code >} 0 in nanoseconds, or
Eino-Ville Talvalaabd9d3c2014-07-28 13:01:52 -0700696 * 0 if the minimum frame duration is not available.
Igor Murashkin9c595172014-05-12 13:56:20 -0700697 *
698 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported
699 * @throws NullPointerException if {@code size} was {@code null}
700 *
701 * @see CaptureRequest#SENSOR_FRAME_DURATION
702 * @see #getOutputStallDuration(int, Size)
703 * @see ImageFormat
704 * @see PixelFormat
705 */
706 public long getOutputMinFrameDuration(int format, Size size) {
707 checkNotNull(size, "size must not be null");
708 checkArgumentFormatSupported(format, /*output*/true);
709
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800710 return getInternalFormatDuration(imageFormatToInternal(format),
711 imageFormatToDataspace(format),
712 size,
713 DURATION_MIN_FRAME);
Igor Murashkin9c595172014-05-12 13:56:20 -0700714 }
715
716 /**
717 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration}
718 * for the class/size combination (in nanoseconds).
719 *
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700720 * <p>This assumes a the {@code klass} is set up to use {@link ImageFormat#PRIVATE}.
Igor Murashkin9c595172014-05-12 13:56:20 -0700721 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p>
722 *
723 * <p>{@code klass} should be one of the ones which is supported by
724 * {@link #isOutputSupportedFor(Class)}.</p>
725 *
726 * <p>{@code size} should be one of the ones returned by
727 * {@link #getOutputSizes(int)}.</p>
728 *
729 * <p>This should correspond to the frame duration when only that stream is active, with all
730 * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}.
731 * </p>
732 *
733 * <p>When multiple streams are used in a request, the minimum frame duration will be
734 * {@code max(individual stream min durations)}.</p>
735 *
Eino-Ville Talvala08bc3b02014-07-09 10:07:36 -0700736 * <p>For devices that do not support manual sensor control
737 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}),
Eino-Ville Talvalaabd9d3c2014-07-28 13:01:52 -0700738 * this function may return 0.</p>
Eino-Ville Talvala08bc3b02014-07-09 10:07:36 -0700739 *
Igor Murashkin9c595172014-05-12 13:56:20 -0700740 * <!--
741 * TODO: uncomment after adding input stream support
742 * <p>The minimum frame duration of a stream (of a particular format, size) is the same
743 * regardless of whether the stream is input or output.</p>
744 * -->
745 *
746 * @param klass
747 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a
748 * non-empty array returned by {@link #getOutputSizes(Class)}
749 * @param size an output-compatible size
Ruben Brunk3e4fed22014-06-18 17:08:42 -0700750 * @return a minimum frame duration {@code >} 0 in nanoseconds, or
Eino-Ville Talvalaabd9d3c2014-07-28 13:01:52 -0700751 * 0 if the minimum frame duration is not available.
Igor Murashkin9c595172014-05-12 13:56:20 -0700752 *
753 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported
754 * @throws NullPointerException if {@code size} or {@code klass} was {@code null}
755 *
756 * @see CaptureRequest#SENSOR_FRAME_DURATION
757 * @see ImageFormat
758 * @see PixelFormat
759 */
760 public <T> long getOutputMinFrameDuration(final Class<T> klass, final Size size) {
761 if (!isOutputSupportedFor(klass)) {
762 throw new IllegalArgumentException("klass was not supported");
763 }
764
765 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800766 HAL_DATASPACE_UNKNOWN,
Igor Murashkin9c595172014-05-12 13:56:20 -0700767 size, DURATION_MIN_FRAME);
768 }
769
770 /**
771 * Get the stall duration for the format/size combination (in nanoseconds).
772 *
773 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p>
774 * <p>{@code size} should be one of the ones returned by
775 * {@link #getOutputSizes(int)}.</p>
776 *
777 * <p>
778 * A stall duration is how much extra time would get added to the normal minimum frame duration
779 * for a repeating request that has streams with non-zero stall.
780 *
781 * <p>For example, consider JPEG captures which have the following characteristics:
782 *
783 * <ul>
784 * <li>JPEG streams act like processed YUV streams in requests for which they are not included;
785 * in requests in which they are directly referenced, they act as JPEG streams.
786 * This is because supporting a JPEG stream requires the underlying YUV data to always be ready
787 * for use by a JPEG encoder, but the encoder will only be used (and impact frame duration) on
788 * requests that actually reference a JPEG stream.
789 * <li>The JPEG processor can run concurrently to the rest of the camera pipeline, but cannot
790 * process more than 1 capture at a time.
791 * </ul>
792 *
793 * <p>In other words, using a repeating YUV request would result in a steady frame rate
794 * (let's say it's 30 FPS). If a single JPEG request is submitted periodically,
795 * the frame rate will stay at 30 FPS (as long as we wait for the previous JPEG to return each
796 * time). If we try to submit a repeating YUV + JPEG request, then the frame rate will drop from
797 * 30 FPS.</p>
798 *
799 * <p>In general, submitting a new request with a non-0 stall time stream will <em>not</em> cause a
800 * frame rate drop unless there are still outstanding buffers for that stream from previous
801 * requests.</p>
802 *
803 * <p>Submitting a repeating request with streams (call this {@code S}) is the same as setting
804 * the minimum frame duration from the normal minimum frame duration corresponding to {@code S},
805 * added with the maximum stall duration for {@code S}.</p>
806 *
807 * <p>If interleaving requests with and without a stall duration, a request will stall by the
808 * maximum of the remaining times for each can-stall stream with outstanding buffers.</p>
809 *
810 * <p>This means that a stalling request will not have an exposure start until the stall has
811 * completed.</p>
812 *
813 * <p>This should correspond to the stall duration when only that stream is active, with all
814 * processing (typically in {@code android.*.mode}) set to {@code FAST} or {@code OFF}.
815 * Setting any of the processing modes to {@code HIGH_QUALITY} effectively results in an
816 * indeterminate stall duration for all streams in a request (the regular stall calculation
817 * rules are ignored).</p>
818 *
819 * <p>The following formats may always have a stall duration:
820 * <ul>
821 * <li>{@link ImageFormat#JPEG JPEG}
822 * <li>{@link ImageFormat#RAW_SENSOR RAW16}
823 * </ul>
824 * </p>
825 *
826 * <p>The following formats will never have a stall duration:
827 * <ul>
828 * <li>{@link ImageFormat#YUV_420_888 YUV_420_888}
829 * <li>{@link #isOutputSupportedFor(Class) Implementation-Defined}
830 * </ul></p>
831 *
832 * <p>
833 * All other formats may or may not have an allowed stall duration on a per-capability basis;
834 * refer to {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES
835 * android.request.availableCapabilities} for more details.</p>
836 * </p>
837 *
838 * <p>See {@link CaptureRequest#SENSOR_FRAME_DURATION android.sensor.frameDuration}
839 * for more information about calculating the max frame rate (absent stalls).</p>
840 *
841 * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
842 * @param size an output-compatible size
843 * @return a stall duration {@code >=} 0 in nanoseconds
844 *
845 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported
846 * @throws NullPointerException if {@code size} was {@code null}
847 *
848 * @see CaptureRequest#SENSOR_FRAME_DURATION
849 * @see ImageFormat
850 * @see PixelFormat
851 */
852 public long getOutputStallDuration(int format, Size size) {
853 checkArgumentFormatSupported(format, /*output*/true);
854
855 return getInternalFormatDuration(imageFormatToInternal(format),
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800856 imageFormatToDataspace(format),
857 size,
858 DURATION_STALL);
Igor Murashkin9c595172014-05-12 13:56:20 -0700859 }
860
861 /**
862 * Get the stall duration for the class/size combination (in nanoseconds).
863 *
Chien-Yu Chen0a551f12015-04-03 17:57:35 -0700864 * <p>This assumes a the {@code klass} is set up to use {@link ImageFormat#PRIVATE}.
Igor Murashkin9c595172014-05-12 13:56:20 -0700865 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p>
866 *
867 * <p>{@code klass} should be one of the ones with a non-empty array returned by
868 * {@link #getOutputSizes(Class)}.</p>
869 *
870 * <p>{@code size} should be one of the ones returned by
871 * {@link #getOutputSizes(Class)}.</p>
872 *
873 * <p>See {@link #getOutputStallDuration(int, Size)} for a definition of a
874 * <em>stall duration</em>.</p>
875 *
876 * @param klass
877 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a
878 * non-empty array returned by {@link #getOutputSizes(Class)}
879 * @param size an output-compatible size
880 * @return a minimum frame duration {@code >=} 0 in nanoseconds
881 *
882 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported
883 * @throws NullPointerException if {@code size} or {@code klass} was {@code null}
884 *
885 * @see CaptureRequest#SENSOR_FRAME_DURATION
886 * @see ImageFormat
887 * @see PixelFormat
888 */
889 public <T> long getOutputStallDuration(final Class<T> klass, final Size size) {
890 if (!isOutputSupportedFor(klass)) {
891 throw new IllegalArgumentException("klass was not supported");
892 }
893
894 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800895 HAL_DATASPACE_UNKNOWN, size, DURATION_STALL);
Igor Murashkin9c595172014-05-12 13:56:20 -0700896 }
897
898 /**
899 * Check if this {@link StreamConfigurationMap} is equal to another
900 * {@link StreamConfigurationMap}.
901 *
902 * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p>
903 *
904 * @return {@code true} if the objects were equal, {@code false} otherwise
905 */
906 @Override
907 public boolean equals(final Object obj) {
908 if (obj == null) {
909 return false;
910 }
911 if (this == obj) {
912 return true;
913 }
914 if (obj instanceof StreamConfigurationMap) {
915 final StreamConfigurationMap other = (StreamConfigurationMap) obj;
916 // XX: do we care about order?
917 return Arrays.equals(mConfigurations, other.mConfigurations) &&
918 Arrays.equals(mMinFrameDurations, other.mMinFrameDurations) &&
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700919 Arrays.equals(mStallDurations, other.mStallDurations) &&
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700920 Arrays.equals(mDepthConfigurations, other.mDepthConfigurations) &&
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700921 Arrays.equals(mHighSpeedVideoConfigurations,
922 other.mHighSpeedVideoConfigurations);
Igor Murashkin9c595172014-05-12 13:56:20 -0700923 }
924 return false;
925 }
926
927 /**
928 * {@inheritDoc}
929 */
930 @Override
931 public int hashCode() {
932 // XX: do we care about order?
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700933 return HashCodeHelpers.hashCodeGeneric(
Yin-Chia Yeh12da1402014-07-15 10:37:31 -0700934 mConfigurations, mMinFrameDurations,
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700935 mStallDurations,
936 mDepthConfigurations, mHighSpeedVideoConfigurations);
Igor Murashkin9c595172014-05-12 13:56:20 -0700937 }
938
939 // Check that the argument is supported by #getOutputFormats or #getInputFormats
940 private int checkArgumentFormatSupported(int format, boolean output) {
941 checkArgumentFormat(format);
942
Eino-Ville Talvala0819c752015-06-17 11:34:41 -0700943 int internalFormat = imageFormatToInternal(format);
944 int internalDataspace = imageFormatToDataspace(format);
945
946 if (output) {
947 if (internalDataspace == HAL_DATASPACE_DEPTH) {
948 if (mDepthOutputFormats.indexOfKey(internalFormat) >= 0) {
949 return format;
950 }
951 } else {
952 if (mAllOutputFormats.indexOfKey(internalFormat) >= 0) {
953 return format;
954 }
955 }
956 } else {
957 if (mInputFormats.indexOfKey(internalFormat) >= 0) {
Igor Murashkin9c595172014-05-12 13:56:20 -0700958 return format;
959 }
960 }
961
962 throw new IllegalArgumentException(String.format(
963 "format %x is not supported by this stream configuration map", format));
964 }
965
966 /**
967 * Ensures that the format is either user-defined or implementation defined.
968 *
969 * <p>If a format has a different internal representation than the public representation,
970 * passing in the public representation here will fail.</p>
971 *
972 * <p>For example if trying to use {@link ImageFormat#JPEG}:
973 * it has a different public representation than the internal representation
974 * {@code HAL_PIXEL_FORMAT_BLOB}, this check will fail.</p>
975 *
976 * <p>Any invalid/undefined formats will raise an exception.</p>
977 *
978 * @param format image format
979 * @return the format
980 *
981 * @throws IllegalArgumentException if the format was invalid
982 */
983 static int checkArgumentFormatInternal(int format) {
984 switch (format) {
985 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
986 case HAL_PIXEL_FORMAT_BLOB:
Eino-Ville Talvalad3b85f62014-08-05 15:02:31 -0700987 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Eino-Ville Talvala456432e2015-03-05 15:42:49 -0800988 case HAL_PIXEL_FORMAT_Y16:
Igor Murashkin9c595172014-05-12 13:56:20 -0700989 return format;
990 case ImageFormat.JPEG:
991 throw new IllegalArgumentException(
992 "ImageFormat.JPEG is an unknown internal format");
993 default:
994 return checkArgumentFormat(format);
995 }
996 }
997
998 /**
999 * Ensures that the format is publicly user-defined in either ImageFormat or PixelFormat.
1000 *
1001 * <p>If a format has a different public representation than the internal representation,
1002 * passing in the internal representation here will fail.</p>
1003 *
1004 * <p>For example if trying to use {@code HAL_PIXEL_FORMAT_BLOB}:
1005 * it has a different internal representation than the public representation
1006 * {@link ImageFormat#JPEG}, this check will fail.</p>
1007 *
1008 * <p>Any invalid/undefined formats will raise an exception, including implementation-defined.
1009 * </p>
1010 *
1011 * <p>Note that {@code @hide} and deprecated formats will not pass this check.</p>
1012 *
1013 * @param format image format
1014 * @return the format
1015 *
1016 * @throws IllegalArgumentException if the format was not user-defined
1017 */
1018 static int checkArgumentFormat(int format) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001019 if (!ImageFormat.isPublicFormat(format) && !PixelFormat.isPublicFormat(format)) {
1020 throw new IllegalArgumentException(String.format(
1021 "format 0x%x was not defined in either ImageFormat or PixelFormat", format));
1022 }
1023
1024 return format;
1025 }
1026
1027 /**
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001028 * Convert an internal format compatible with {@code graphics.h} into public-visible
1029 * {@code ImageFormat}. This assumes the dataspace of the format is not HAL_DATASPACE_DEPTH.
Igor Murashkin9c595172014-05-12 13:56:20 -07001030 *
1031 * <p>In particular these formats are converted:
1032 * <ul>
Chien-Yu Chen0a551f12015-04-03 17:57:35 -07001033 * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.JPEG</li>
Igor Murashkin9c595172014-05-12 13:56:20 -07001034 * </ul>
1035 * </p>
1036 *
Chien-Yu Chen0a551f12015-04-03 17:57:35 -07001037 * <p>Passing in a format which has no public equivalent will fail;
Igor Murashkin9c595172014-05-12 13:56:20 -07001038 * as will passing in a public format which has a different internal format equivalent.
1039 * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1040 *
1041 * <p>All other formats are returned as-is, no further invalid check is performed.</p>
1042 *
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001043 * <p>This function is the dual of {@link #imageFormatToInternal} for dataspaces other than
1044 * HAL_DATASPACE_DEPTH.</p>
Igor Murashkin9c595172014-05-12 13:56:20 -07001045 *
1046 * @param format image format from {@link ImageFormat} or {@link PixelFormat}
1047 * @return the converted image formats
1048 *
1049 * @throws IllegalArgumentException
1050 * if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or
1051 * {@link ImageFormat#JPEG}
1052 *
1053 * @see ImageFormat
1054 * @see PixelFormat
1055 * @see #checkArgumentFormat
1056 */
1057 static int imageFormatToPublic(int format) {
1058 switch (format) {
1059 case HAL_PIXEL_FORMAT_BLOB:
1060 return ImageFormat.JPEG;
1061 case ImageFormat.JPEG:
1062 throw new IllegalArgumentException(
1063 "ImageFormat.JPEG is an unknown internal format");
Igor Murashkin9c595172014-05-12 13:56:20 -07001064 default:
1065 return format;
1066 }
1067 }
1068
1069 /**
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001070 * Convert an internal format compatible with {@code graphics.h} into public-visible
1071 * {@code ImageFormat}. This assumes the dataspace of the format is HAL_DATASPACE_DEPTH.
1072 *
1073 * <p>In particular these formats are converted:
1074 * <ul>
1075 * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.DEPTH_POINT_CLOUD
1076 * <li>HAL_PIXEL_FORMAT_Y16 => ImageFormat.DEPTH16
1077 * </ul>
1078 * </p>
1079 *
1080 * <p>Passing in an implementation-defined format which has no public equivalent will fail;
1081 * as will passing in a public format which has a different internal format equivalent.
1082 * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1083 *
1084 * <p>All other formats are returned as-is, no further invalid check is performed.</p>
1085 *
1086 * <p>This function is the dual of {@link #imageFormatToInternal} for formats associated with
1087 * HAL_DATASPACE_DEPTH.</p>
1088 *
1089 * @param format image format from {@link ImageFormat} or {@link PixelFormat}
1090 * @return the converted image formats
1091 *
1092 * @throws IllegalArgumentException
1093 * if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or
1094 * {@link ImageFormat#JPEG}
1095 *
1096 * @see ImageFormat
1097 * @see PixelFormat
1098 * @see #checkArgumentFormat
1099 */
1100 static int depthFormatToPublic(int format) {
1101 switch (format) {
1102 case HAL_PIXEL_FORMAT_BLOB:
1103 return ImageFormat.DEPTH_POINT_CLOUD;
1104 case HAL_PIXEL_FORMAT_Y16:
1105 return ImageFormat.DEPTH16;
1106 case ImageFormat.JPEG:
1107 throw new IllegalArgumentException(
1108 "ImageFormat.JPEG is an unknown internal format");
1109 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1110 throw new IllegalArgumentException(
1111 "IMPLEMENTATION_DEFINED must not leak to public API");
1112 default:
1113 throw new IllegalArgumentException(
1114 "Unknown DATASPACE_DEPTH format " + format);
1115 }
1116 }
1117
1118 /**
Igor Murashkin9c595172014-05-12 13:56:20 -07001119 * Convert image formats from internal to public formats (in-place).
1120 *
1121 * @param formats an array of image formats
1122 * @return {@code formats}
1123 *
1124 * @see #imageFormatToPublic
1125 */
1126 static int[] imageFormatToPublic(int[] formats) {
1127 if (formats == null) {
1128 return null;
1129 }
1130
1131 for (int i = 0; i < formats.length; ++i) {
1132 formats[i] = imageFormatToPublic(formats[i]);
1133 }
1134
1135 return formats;
1136 }
1137
1138 /**
1139 * Convert a public format compatible with {@code ImageFormat} to an internal format
1140 * from {@code graphics.h}.
1141 *
1142 * <p>In particular these formats are converted:
1143 * <ul>
1144 * <li>ImageFormat.JPEG => HAL_PIXEL_FORMAT_BLOB
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001145 * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_PIXEL_FORMAT_BLOB
1146 * <li>ImageFormat.DEPTH16 => HAL_PIXEL_FORMAT_Y16
Igor Murashkin9c595172014-05-12 13:56:20 -07001147 * </ul>
1148 * </p>
1149 *
Chien-Yu Chen0a551f12015-04-03 17:57:35 -07001150 * <p>Passing in an internal format which has a different public format equivalent will fail.
Igor Murashkin9c595172014-05-12 13:56:20 -07001151 * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1152 *
1153 * <p>All other formats are returned as-is, no invalid check is performed.</p>
1154 *
1155 * <p>This function is the dual of {@link #imageFormatToPublic}.</p>
1156 *
1157 * @param format public image format from {@link ImageFormat} or {@link PixelFormat}
1158 * @return the converted image formats
1159 *
1160 * @see ImageFormat
1161 * @see PixelFormat
1162 *
1163 * @throws IllegalArgumentException
1164 * if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED}
1165 */
1166 static int imageFormatToInternal(int format) {
1167 switch (format) {
1168 case ImageFormat.JPEG:
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001169 case ImageFormat.DEPTH_POINT_CLOUD:
Igor Murashkin9c595172014-05-12 13:56:20 -07001170 return HAL_PIXEL_FORMAT_BLOB;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001171 case ImageFormat.DEPTH16:
1172 return HAL_PIXEL_FORMAT_Y16;
Igor Murashkin9c595172014-05-12 13:56:20 -07001173 default:
1174 return format;
1175 }
1176 }
1177
1178 /**
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001179 * Convert a public format compatible with {@code ImageFormat} to an internal dataspace
1180 * from {@code graphics.h}.
1181 *
1182 * <p>In particular these formats are converted:
1183 * <ul>
1184 * <li>ImageFormat.JPEG => HAL_DATASPACE_JFIF
1185 * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_DATASPACE_DEPTH
1186 * <li>ImageFormat.DEPTH16 => HAL_DATASPACE_DEPTH
1187 * <li>others => HAL_DATASPACE_UNKNOWN
1188 * </ul>
1189 * </p>
1190 *
1191 * <p>Passing in an implementation-defined format here will fail (it's not a public format);
1192 * as will passing in an internal format which has a different public format equivalent.
1193 * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1194 *
1195 * <p>All other formats are returned as-is, no invalid check is performed.</p>
1196 *
1197 * <p>This function is the dual of {@link #imageFormatToPublic}.</p>
1198 *
1199 * @param format public image format from {@link ImageFormat} or {@link PixelFormat}
1200 * @return the converted image formats
1201 *
1202 * @see ImageFormat
1203 * @see PixelFormat
1204 *
1205 * @throws IllegalArgumentException
1206 * if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED}
1207 */
1208 static int imageFormatToDataspace(int format) {
1209 switch (format) {
1210 case ImageFormat.JPEG:
1211 return HAL_DATASPACE_JFIF;
1212 case ImageFormat.DEPTH_POINT_CLOUD:
1213 case ImageFormat.DEPTH16:
1214 return HAL_DATASPACE_DEPTH;
1215 default:
1216 return HAL_DATASPACE_UNKNOWN;
1217 }
1218 }
1219
1220 /**
Igor Murashkin9c595172014-05-12 13:56:20 -07001221 * Convert image formats from public to internal formats (in-place).
1222 *
1223 * @param formats an array of image formats
1224 * @return {@code formats}
1225 *
1226 * @see #imageFormatToInternal
1227 *
1228 * @hide
1229 */
1230 public static int[] imageFormatToInternal(int[] formats) {
1231 if (formats == null) {
1232 return null;
1233 }
1234
1235 for (int i = 0; i < formats.length; ++i) {
1236 formats[i] = imageFormatToInternal(formats[i]);
1237 }
1238
1239 return formats;
1240 }
1241
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001242 private Size[] getPublicFormatSizes(int format, boolean output, boolean highRes) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001243 try {
1244 checkArgumentFormatSupported(format, output);
1245 } catch (IllegalArgumentException e) {
1246 return null;
1247 }
1248
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001249 int internalFormat = imageFormatToInternal(format);
1250 int dataspace = imageFormatToDataspace(format);
Igor Murashkin9c595172014-05-12 13:56:20 -07001251
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001252 return getInternalFormatSizes(internalFormat, dataspace, output, highRes);
Igor Murashkin9c595172014-05-12 13:56:20 -07001253 }
1254
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001255 private Size[] getInternalFormatSizes(int format, int dataspace,
1256 boolean output, boolean highRes) {
1257 SparseIntArray formatsMap =
1258 !output ? mInputFormats :
1259 dataspace == HAL_DATASPACE_DEPTH ? mDepthOutputFormats :
1260 highRes ? mHighResOutputFormats :
1261 mOutputFormats;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001262
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001263 int sizesCount = formatsMap.get(format);
1264 if ( ((!output || dataspace == HAL_DATASPACE_DEPTH) && sizesCount == 0) ||
1265 (output && dataspace != HAL_DATASPACE_DEPTH && mAllOutputFormats.get(format) == 0)) {
1266 // Only throw if this is really not supported at all
Igor Murashkin9c595172014-05-12 13:56:20 -07001267 throw new IllegalArgumentException("format not available");
1268 }
1269
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001270 Size[] sizes = new Size[sizesCount];
Igor Murashkin9c595172014-05-12 13:56:20 -07001271 int sizeIndex = 0;
1272
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001273 StreamConfiguration[] configurations =
1274 (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : mConfigurations;
1275
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001276 for (StreamConfiguration config : configurations) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001277 int fmt = config.getFormat();
1278 if (fmt == format && config.isOutput() == output) {
1279 if (output) {
1280 // Filter slow high-res output formats; include for
1281 // highRes, remove for !highRes
1282 long duration = 0;
1283 for (int i = 0; i < mMinFrameDurations.length; i++) {
1284 StreamConfigurationDuration d = mMinFrameDurations[i];
1285 if (d.getFormat() == fmt &&
1286 d.getWidth() == config.getSize().getWidth() &&
1287 d.getHeight() == config.getSize().getHeight()) {
1288 duration = d.getDuration();
1289 break;
1290 }
1291 }
1292 if (highRes != (duration > DURATION_20FPS_NS)) {
1293 continue;
1294 }
1295 }
Igor Murashkin9c595172014-05-12 13:56:20 -07001296 sizes[sizeIndex++] = config.getSize();
1297 }
1298 }
1299
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001300 if (sizeIndex != sizesCount) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001301 throw new AssertionError(
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001302 "Too few sizes (expected " + sizesCount + ", actual " + sizeIndex + ")");
Igor Murashkin9c595172014-05-12 13:56:20 -07001303 }
1304
1305 return sizes;
1306 }
1307
1308 /** Get the list of publically visible output formats; does not include IMPL_DEFINED */
1309 private int[] getPublicFormats(boolean output) {
1310 int[] formats = new int[getPublicFormatCount(output)];
1311
1312 int i = 0;
1313
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001314 SparseIntArray map = getFormatsMap(output);
1315 for (int j = 0; j < map.size(); j++) {
1316 int format = map.keyAt(j);
Chien-Yu Chen0a551f12015-04-03 17:57:35 -07001317 if (format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001318 formats[i++] = imageFormatToPublic(format);
Igor Murashkin9c595172014-05-12 13:56:20 -07001319 }
1320 }
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001321 if (output) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001322 for (int j = 0; j < mDepthOutputFormats.size(); j++) {
1323 formats[i++] = depthFormatToPublic(mDepthOutputFormats.keyAt(j));
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001324 }
1325 }
Igor Murashkin9c595172014-05-12 13:56:20 -07001326 if (formats.length != i) {
1327 throw new AssertionError("Too few formats " + i + ", expected " + formats.length);
1328 }
1329
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001330 return formats;
Igor Murashkin9c595172014-05-12 13:56:20 -07001331 }
1332
1333 /** Get the format -> size count map for either output or input formats */
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001334 private SparseIntArray getFormatsMap(boolean output) {
1335 return output ? mAllOutputFormats : mInputFormats;
Igor Murashkin9c595172014-05-12 13:56:20 -07001336 }
1337
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001338 private long getInternalFormatDuration(int format, int dataspace, Size size, int duration) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001339 // assume format is already checked, since its internal
1340
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001341 if (!isSupportedInternalConfiguration(format, dataspace, size)) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001342 throw new IllegalArgumentException("size was not supported");
1343 }
1344
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001345 StreamConfigurationDuration[] durations = getDurations(duration, dataspace);
Igor Murashkin9c595172014-05-12 13:56:20 -07001346
1347 for (StreamConfigurationDuration configurationDuration : durations) {
1348 if (configurationDuration.getFormat() == format &&
1349 configurationDuration.getWidth() == size.getWidth() &&
1350 configurationDuration.getHeight() == size.getHeight()) {
1351 return configurationDuration.getDuration();
1352 }
1353 }
Eino-Ville Talvalaabd9d3c2014-07-28 13:01:52 -07001354 // Default duration is '0' (unsupported/no extra stall)
1355 return 0;
Igor Murashkin9c595172014-05-12 13:56:20 -07001356 }
1357
1358 /**
1359 * Get the durations array for the kind of duration
1360 *
1361 * @see #DURATION_MIN_FRAME
1362 * @see #DURATION_STALL
1363 * */
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001364 private StreamConfigurationDuration[] getDurations(int duration, int dataspace) {
Igor Murashkin9c595172014-05-12 13:56:20 -07001365 switch (duration) {
1366 case DURATION_MIN_FRAME:
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001367 return (dataspace == HAL_DATASPACE_DEPTH) ?
1368 mDepthMinFrameDurations : mMinFrameDurations;
Igor Murashkin9c595172014-05-12 13:56:20 -07001369 case DURATION_STALL:
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001370 return (dataspace == HAL_DATASPACE_DEPTH) ?
1371 mDepthStallDurations : mStallDurations;
Igor Murashkin9c595172014-05-12 13:56:20 -07001372 default:
1373 throw new IllegalArgumentException("duration was invalid");
1374 }
1375 }
1376
Igor Murashkin9c595172014-05-12 13:56:20 -07001377 /** Count the number of publicly-visible output formats */
1378 private int getPublicFormatCount(boolean output) {
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001379 SparseIntArray formatsMap = getFormatsMap(output);
Igor Murashkin9c595172014-05-12 13:56:20 -07001380 int size = formatsMap.size();
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001381 if (formatsMap.indexOfKey(HAL_PIXEL_FORMAT_RAW_OPAQUE) >= 0) {
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -08001382 size -= 1;
1383 }
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001384 if (output) {
1385 size += mDepthOutputFormats.size();
1386 }
Eino-Ville Talvalafa0b9a02015-01-20 12:30:59 -08001387
Igor Murashkin9c595172014-05-12 13:56:20 -07001388 return size;
1389 }
1390
1391 private static <T> boolean arrayContains(T[] array, T element) {
1392 if (array == null) {
1393 return false;
1394 }
1395
1396 for (T el : array) {
1397 if (Objects.equals(el, element)) {
1398 return true;
1399 }
1400 }
1401
1402 return false;
1403 }
1404
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001405 private boolean isSupportedInternalConfiguration(int format, int dataspace,
1406 Size size) {
1407 StreamConfiguration[] configurations =
1408 (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : mConfigurations;
1409
1410 for (int i = 0; i < configurations.length; i++) {
1411 if (configurations[i].getFormat() == format &&
1412 configurations[i].getSize().equals(size)) {
1413 return true;
1414 }
1415 }
1416
1417 return false;
1418 }
1419
Chien-Yu Chen310f3812015-05-20 10:16:59 -07001420 /**
1421 * Return this {@link StreamConfigurationMap} as a string representation.
1422 *
1423 * <p>{@code "StreamConfigurationMap(Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d,
1424 * stall:%d], ... [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]), Inputs([w:%d, h:%d,
1425 * format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)]), ValidOutputFormatsForInput(
1426 * [in:%d, out:%d, ... %d], ... [in:%d, out:%d, ... %d]), HighSpeedVideoConfigurations(
1427 * [w:%d, h:%d, min_fps:%d, max_fps:%d], ... [w:%d, h:%d, min_fps:%d, max_fps:%d]))"}.</p>
1428 *
1429 * <p>{@code Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d], ...
1430 * [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d])}, where
1431 * {@code [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]} represents an output
1432 * configuration's width, height, format, minimal frame duration in nanoseconds, and stall
1433 * duration in nanoseconds.</p>
1434 *
1435 * <p>{@code Inputs([w:%d, h:%d, format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)])}, where
1436 * {@code [w:%d, h:%d, format:%s(%d)]} represents an input configuration's width, height, and
1437 * format.</p>
1438 *
1439 * <p>{@code ValidOutputFormatsForInput([in:%s(%d), out:%s(%d), ... %s(%d)],
1440 * ... [in:%s(%d), out:%s(%d), ... %s(%d)])}, where {@code [in:%s(%d), out:%s(%d), ... %s(%d)]}
1441 * represents an input fomat and its valid output formats.</p>
1442 *
1443 * <p>{@code HighSpeedVideoConfigurations([w:%d, h:%d, min_fps:%d, max_fps:%d],
1444 * ... [w:%d, h:%d, min_fps:%d, max_fps:%d])}, where
1445 * {@code [w:%d, h:%d, min_fps:%d, max_fps:%d]} represents a high speed video output
1446 * configuration's width, height, minimal frame rate, and maximal frame rate.</p>
1447 *
1448 * @return string representation of {@link StreamConfigurationMap}
1449 */
1450 @Override
1451 public String toString() {
1452 StringBuilder sb = new StringBuilder("StreamConfiguration(");
1453 appendOutputsString(sb);
1454 sb.append(", ");
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001455 appendHighResOutputsString(sb);
1456 sb.append(", ");
Chien-Yu Chen310f3812015-05-20 10:16:59 -07001457 appendInputsString(sb);
1458 sb.append(", ");
1459 appendValidOutputFormatsForInputString(sb);
1460 sb.append(", ");
1461 appendHighSpeedVideoConfigurationsString(sb);
1462 sb.append(")");
1463
1464 return sb.toString();
1465 }
1466
1467 private void appendOutputsString(StringBuilder sb) {
1468 sb.append("Outputs(");
1469 int[] formats = getOutputFormats();
1470 for (int format : formats) {
1471 Size[] sizes = getOutputSizes(format);
1472 for (Size size : sizes) {
1473 long minFrameDuration = getOutputMinFrameDuration(format, size);
1474 long stallDuration = getOutputStallDuration(format, size);
1475 sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " +
1476 "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format),
1477 format, minFrameDuration, stallDuration));
1478 }
1479 }
1480 // Remove the pending ", "
1481 if (sb.charAt(sb.length() - 1) == ' ') {
1482 sb.delete(sb.length() - 2, sb.length());
1483 }
1484 sb.append(")");
1485 }
1486
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001487 private void appendHighResOutputsString(StringBuilder sb) {
1488 sb.append("HighResolutionOutputs(");
1489 int[] formats = getOutputFormats();
1490 for (int format : formats) {
1491 Size[] sizes = getHighResolutionOutputSizes(format);
1492 if (sizes == null) continue;
1493 for (Size size : sizes) {
1494 long minFrameDuration = getOutputMinFrameDuration(format, size);
1495 long stallDuration = getOutputStallDuration(format, size);
1496 sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " +
1497 "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format),
1498 format, minFrameDuration, stallDuration));
1499 }
1500 }
1501 // Remove the pending ", "
1502 if (sb.charAt(sb.length() - 1) == ' ') {
1503 sb.delete(sb.length() - 2, sb.length());
1504 }
1505 sb.append(")");
1506 }
1507
Chien-Yu Chen310f3812015-05-20 10:16:59 -07001508 private void appendInputsString(StringBuilder sb) {
1509 sb.append("Inputs(");
1510 int[] formats = getInputFormats();
1511 for (int format : formats) {
1512 Size[] sizes = getInputSizes(format);
1513 for (Size size : sizes) {
1514 sb.append(String.format("[w:%d, h:%d, format:%s(%d)], ", size.getWidth(),
1515 size.getHeight(), formatToString(format), format));
1516 }
1517 }
1518 // Remove the pending ", "
1519 if (sb.charAt(sb.length() - 1) == ' ') {
1520 sb.delete(sb.length() - 2, sb.length());
1521 }
1522 sb.append(")");
1523 }
1524
1525 private void appendValidOutputFormatsForInputString(StringBuilder sb) {
1526 sb.append("ValidOutputFormatsForInput(");
1527 int[] inputFormats = getInputFormats();
1528 for (int inputFormat : inputFormats) {
1529 sb.append(String.format("[in:%s(%d), out:", formatToString(inputFormat), inputFormat));
1530 int[] outputFormats = getValidOutputFormatsForInput(inputFormat);
1531 for (int i = 0; i < outputFormats.length; i++) {
1532 sb.append(String.format("%s(%d)", formatToString(outputFormats[i]),
1533 outputFormats[i]));
1534 if (i < outputFormats.length - 1) {
1535 sb.append(", ");
1536 }
1537 }
1538 sb.append("], ");
1539 }
1540 // Remove the pending ", "
1541 if (sb.charAt(sb.length() - 1) == ' ') {
1542 sb.delete(sb.length() - 2, sb.length());
1543 }
1544 sb.append(")");
1545 }
1546
1547 private void appendHighSpeedVideoConfigurationsString(StringBuilder sb) {
1548 sb.append("HighSpeedVideoConfigurations(");
1549 Size[] sizes = getHighSpeedVideoSizes();
1550 for (Size size : sizes) {
1551 Range<Integer>[] ranges = getHighSpeedVideoFpsRangesFor(size);
1552 for (Range<Integer> range : ranges) {
1553 sb.append(String.format("[w:%d, h:%d, min_fps:%d, max_fps:%d], ", size.getWidth(),
1554 size.getHeight(), range.getLower(), range.getUpper()));
1555 }
1556 }
1557 // Remove the pending ", "
1558 if (sb.charAt(sb.length() - 1) == ' ') {
1559 sb.delete(sb.length() - 2, sb.length());
1560 }
1561 sb.append(")");
1562 }
1563
1564 private String formatToString(int format) {
1565 switch (format) {
1566 case ImageFormat.YV12:
1567 return "YV12";
1568 case ImageFormat.YUV_420_888:
1569 return "YUV_420_888";
1570 case ImageFormat.NV21:
1571 return "NV21";
1572 case ImageFormat.NV16:
1573 return "NV16";
1574 case PixelFormat.RGB_565:
1575 return "RGB_565";
1576 case PixelFormat.RGBA_8888:
1577 return "RGBA_8888";
1578 case PixelFormat.RGBX_8888:
1579 return "RGBX_8888";
1580 case PixelFormat.RGB_888:
1581 return "RGB_888";
1582 case ImageFormat.JPEG:
1583 return "JPEG";
1584 case ImageFormat.YUY2:
1585 return "YUY2";
1586 case ImageFormat.Y8:
1587 return "Y8";
1588 case ImageFormat.Y16:
1589 return "Y16";
1590 case ImageFormat.RAW_SENSOR:
1591 return "RAW_SENSOR";
1592 case ImageFormat.RAW10:
1593 return "RAW10";
1594 case ImageFormat.DEPTH16:
1595 return "DEPTH16";
1596 case ImageFormat.DEPTH_POINT_CLOUD:
1597 return "DEPTH_POINT_CLOUD";
1598 case ImageFormat.PRIVATE:
1599 return "PRIVATE";
1600 default:
1601 return "UNKNOWN";
1602 }
1603 }
1604
Igor Murashkin9c595172014-05-12 13:56:20 -07001605 // from system/core/include/system/graphics.h
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001606 private static final int HAL_PIXEL_FORMAT_RAW16 = 0x20;
Igor Murashkin9c595172014-05-12 13:56:20 -07001607 private static final int HAL_PIXEL_FORMAT_BLOB = 0x21;
1608 private static final int HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22;
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001609 private static final int HAL_PIXEL_FORMAT_YCbCr_420_888 = 0x23;
Igor Murashkin9c595172014-05-12 13:56:20 -07001610 private static final int HAL_PIXEL_FORMAT_RAW_OPAQUE = 0x24;
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001611 private static final int HAL_PIXEL_FORMAT_RAW10 = 0x25;
1612 private static final int HAL_PIXEL_FORMAT_RAW12 = 0x26;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001613 private static final int HAL_PIXEL_FORMAT_Y16 = 0x20363159;
1614
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001615
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001616 private static final int HAL_DATASPACE_UNKNOWN = 0x0;
1617 private static final int HAL_DATASPACE_JFIF = 0x101;
1618 private static final int HAL_DATASPACE_DEPTH = 0x1000;
Igor Murashkin9c595172014-05-12 13:56:20 -07001619
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001620 private static final long DURATION_20FPS_NS = 50000000L;
Igor Murashkin9c595172014-05-12 13:56:20 -07001621 /**
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001622 * @see #getDurations(int, int)
Igor Murashkin9c595172014-05-12 13:56:20 -07001623 */
1624 private static final int DURATION_MIN_FRAME = 0;
1625 private static final int DURATION_STALL = 1;
1626
1627 private final StreamConfiguration[] mConfigurations;
1628 private final StreamConfigurationDuration[] mMinFrameDurations;
1629 private final StreamConfigurationDuration[] mStallDurations;
Eino-Ville Talvala456432e2015-03-05 15:42:49 -08001630
1631 private final StreamConfiguration[] mDepthConfigurations;
1632 private final StreamConfigurationDuration[] mDepthMinFrameDurations;
1633 private final StreamConfigurationDuration[] mDepthStallDurations;
1634
Yin-Chia Yeh12da1402014-07-15 10:37:31 -07001635 private final HighSpeedVideoConfiguration[] mHighSpeedVideoConfigurations;
Chien-Yu Chen0a551f12015-04-03 17:57:35 -07001636 private final ReprocessFormatsMap mInputOutputFormatsMap;
Igor Murashkin9c595172014-05-12 13:56:20 -07001637
Eino-Ville Talvala0819c752015-06-17 11:34:41 -07001638 private final boolean mListHighResolution;
1639
1640 /** internal format -> num output sizes mapping, not including slow high-res sizes, for
1641 * non-depth dataspaces */
1642 private final SparseIntArray mOutputFormats = new SparseIntArray();
1643 /** internal format -> num output sizes mapping for slow high-res sizes, for non-depth
1644 * dataspaces */
1645 private final SparseIntArray mHighResOutputFormats = new SparseIntArray();
1646 /** internal format -> num output sizes mapping for all non-depth dataspaces */
1647 private final SparseIntArray mAllOutputFormats = new SparseIntArray();
1648 /** internal format -> num input sizes mapping, for input reprocessing formats */
1649 private final SparseIntArray mInputFormats = new SparseIntArray();
1650 /** internal format -> num depth output sizes mapping, for HAL_DATASPACE_DEPTH */
1651 private final SparseIntArray mDepthOutputFormats = new SparseIntArray();
Yin-Chia Yeh12da1402014-07-15 10:37:31 -07001652 /** High speed video Size -> FPS range count mapping*/
1653 private final HashMap</*HighSpeedVideoSize*/Size, /*Count*/Integer> mHighSpeedVideoSizeMap =
1654 new HashMap<Size, Integer>();
1655 /** High speed video FPS range -> Size count mapping*/
1656 private final HashMap</*HighSpeedVideoFpsRange*/Range<Integer>, /*Count*/Integer>
1657 mHighSpeedVideoFpsRangeMap = new HashMap<Range<Integer>, Integer>();
Igor Murashkin9c595172014-05-12 13:56:20 -07001658
1659}