StreamConfigurationMap.java revision 12da140082323d9aa048b3e928505a0a2adfdda7
1/* 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; 23import android.hardware.camera2.CaptureRequest; 24import android.hardware.camera2.utils.HashCodeHelpers; 25import android.view.Surface; 26import android.util.Log; 27import android.util.Range; 28import android.util.Size; 29 30import java.util.Arrays; 31import java.util.HashMap; 32import java.util.Objects; 33 34import static com.android.internal.util.Preconditions.*; 35 36/** 37 * Immutable class to store the available stream 38 * {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP configurations} to set up 39 * {@link android.view.Surface Surfaces} for creating a 40 * {@link android.hardware.camera2.CameraCaptureSession capture session} with 41 * {@link android.hardware.camera2.CameraDevice#createCaptureSession}. 42 * <!-- TODO: link to input stream configuration --> 43 * 44 * <p>This is the authoritative list for all <!-- input/ -->output formats (and sizes respectively 45 * for that format) that are supported by a camera device.</p> 46 * 47 * <p>This also contains the minimum frame durations and stall durations for each format/size 48 * combination that can be used to calculate effective frame rate when submitting multiple captures. 49 * </p> 50 * 51 * <p>An instance of this object is available from {@link CameraCharacteristics} using 52 * the {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP} key and the 53 * {@link CameraCharacteristics#get} method.</p> 54 * 55 * <pre><code>{@code 56 * CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); 57 * StreamConfigurationMap configs = characteristics.get( 58 * CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 59 * }</code></pre> 60 * 61 * @see CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP 62 * @see CameraDevice#createCaptureSession 63 */ 64public final class StreamConfigurationMap { 65 66 private static final String TAG = "StreamConfigurationMap"; 67 68 /** 69 * Indicates that a minimum frame duration is not available for a particular configuration. 70 */ 71 public static final long NO_MIN_FRAME_DURATION = 0; 72 73 /** 74 * 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} 82 * @param highSpeedVideoConfigurations an array of {@link HighSpeedVideoConfiguration}, null if 83 * camera device does not support high speed video recording 84 * 85 * @throws NullPointerException if any of the arguments except highSpeedVideoConfigurations 86 * were {@code null} or any subelements were {@code null} 87 * 88 * @hide 89 */ 90 public StreamConfigurationMap( 91 StreamConfiguration[] configurations, 92 StreamConfigurationDuration[] minFrameDurations, 93 StreamConfigurationDuration[] stallDurations, 94 HighSpeedVideoConfiguration[] highSpeedVideoConfigurations) { 95 96 mConfigurations = checkArrayElementsNotNull(configurations, "configurations"); 97 mMinFrameDurations = checkArrayElementsNotNull(minFrameDurations, "minFrameDurations"); 98 mStallDurations = checkArrayElementsNotNull(stallDurations, "stallDurations"); 99 if (highSpeedVideoConfigurations == null) { 100 mHighSpeedVideoConfigurations = new HighSpeedVideoConfiguration[0]; 101 } else { 102 mHighSpeedVideoConfigurations = checkArrayElementsNotNull( 103 highSpeedVideoConfigurations, "highSpeedVideoConfigurations"); 104 } 105 106 // For each format, track how many sizes there are available to configure 107 for (StreamConfiguration config : configurations) { 108 HashMap<Integer, Integer> map = config.isOutput() ? mOutputFormats : mInputFormats; 109 110 Integer count = map.get(config.getFormat()); 111 112 if (count == null) { 113 count = 0; 114 } 115 count = count + 1; 116 117 map.put(config.getFormat(), count); 118 } 119 120 if (!mOutputFormats.containsKey(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) { 121 throw new AssertionError( 122 "At least one stream configuration for IMPLEMENTATION_DEFINED must exist"); 123 } 124 125 // For each Size/FPS range, track how many FPS range/Size there are available 126 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 127 Size size = config.getSize(); 128 Range<Integer> fpsRange = config.getFpsRange(); 129 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size); 130 if (fpsRangeCount == null) { 131 fpsRangeCount = 0; 132 } 133 mHighSpeedVideoSizeMap.put(size, fpsRangeCount + 1); 134 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange); 135 if (sizeCount == null) { 136 sizeCount = 0; 137 } 138 mHighSpeedVideoFpsRangeMap.put(fpsRange, sizeCount + 1); 139 } 140 } 141 142 /** 143 * Get the image {@code format} output formats in this stream configuration. 144 * 145 * <p>All image formats returned by this function will be defined in either {@link ImageFormat} 146 * or in {@link PixelFormat} (and there is no possibility of collision).</p> 147 * 148 * <p>Formats listed in this array are guaranteed to return true if queried with 149 * {@link #isOutputSupportedFor(int).</p> 150 * 151 * @return an array of integer format 152 * 153 * @see ImageFormat 154 * @see PixelFormat 155 */ 156 public final int[] getOutputFormats() { 157 return getPublicFormats(/*output*/true); 158 } 159 160 /** 161 * Get the image {@code format} input formats in this stream configuration. 162 * 163 * <p>All image formats returned by this function will be defined in either {@link ImageFormat} 164 * or in {@link PixelFormat} (and there is no possibility of collision).</p> 165 * 166 * @return an array of integer format 167 * 168 * @see ImageFormat 169 * @see PixelFormat 170 * 171 * @hide 172 */ 173 public final int[] getInputFormats() { 174 return getPublicFormats(/*output*/false); 175 } 176 177 /** 178 * Get the supported input sizes for this input format. 179 * 180 * <p>The format must have come from {@link #getInputFormats}; otherwise 181 * {@code null} is returned.</p> 182 * 183 * @param format a format from {@link #getInputFormats} 184 * @return a non-empty array of sizes, or {@code null} if the format was not available. 185 * 186 * @hide 187 */ 188 public Size[] getInputSizes(final int format) { 189 return getPublicFormatSizes(format, /*output*/false); 190 } 191 192 /** 193 * Determine whether or not output surfaces with a particular user-defined format can be passed 194 * {@link CameraDevice#createCaptureSession createCaptureSession}. 195 * 196 * <p>This method determines that the output {@code format} is supported by the camera device; 197 * each output {@code surface} target may or may not itself support that {@code format}. 198 * Refer to the class which provides the surface for additional documentation.</p> 199 * 200 * <p>Formats for which this returns {@code true} are guaranteed to exist in the result 201 * returned by {@link #getOutputSizes}.</p> 202 * 203 * @param format an image format from either {@link ImageFormat} or {@link PixelFormat} 204 * @return 205 * {@code true} iff using a {@code surface} with this {@code format} will be 206 * supported with {@link CameraDevice#createCaptureSession} 207 * 208 * @throws IllegalArgumentException 209 * if the image format was not a defined named constant 210 * from either {@link ImageFormat} or {@link PixelFormat} 211 * 212 * @see ImageFormat 213 * @see PixelFormat 214 * @see CameraDevice#createCaptureSession 215 */ 216 public boolean isOutputSupportedFor(int format) { 217 checkArgumentFormat(format); 218 219 format = imageFormatToInternal(format); 220 return getFormatsMap(/*output*/true).containsKey(format); 221 } 222 223 /** 224 * Determine whether or not output streams can be configured with a particular class 225 * as a consumer. 226 * 227 * <p>The following list is generally usable for outputs: 228 * <ul> 229 * <li>{@link android.media.ImageReader} - 230 * Recommended for image processing or streaming to external resources (such as a file or 231 * network) 232 * <li>{@link android.media.MediaRecorder} - 233 * Recommended for recording video (simple to use) 234 * <li>{@link android.media.MediaCodec} - 235 * Recommended for recording video (more complicated to use, with more flexibility) 236 * <li>{@link android.renderscript.Allocation} - 237 * Recommended for image processing with {@link android.renderscript RenderScript} 238 * <li>{@link android.view.SurfaceHolder} - 239 * Recommended for low-power camera preview with {@link android.view.SurfaceView} 240 * <li>{@link android.graphics.SurfaceTexture} - 241 * Recommended for OpenGL-accelerated preview processing or compositing with 242 * {@link android.view.TextureView} 243 * </ul> 244 * </p> 245 * 246 * <p>Generally speaking this means that creating a {@link Surface} from that class <i>may</i> 247 * provide a producer endpoint that is suitable to be used with 248 * {@link CameraDevice#createCaptureSession}.</p> 249 * 250 * <p>Since not all of the above classes support output of all format and size combinations, 251 * the particular combination should be queried with {@link #isOutputSupportedFor(Surface)}.</p> 252 * 253 * @param klass a non-{@code null} {@link Class} object reference 254 * @return {@code true} if this class is supported as an output, {@code false} otherwise 255 * 256 * @throws NullPointerException if {@code klass} was {@code null} 257 * 258 * @see CameraDevice#createCaptureSession 259 * @see #isOutputSupportedFor(Surface) 260 */ 261 public static <T> boolean isOutputSupportedFor(Class<T> klass) { 262 checkNotNull(klass, "klass must not be null"); 263 264 if (klass == android.media.ImageReader.class) { 265 return true; 266 } else if (klass == android.media.MediaRecorder.class) { 267 return true; 268 } else if (klass == android.media.MediaCodec.class) { 269 return true; 270 } else if (klass == android.renderscript.Allocation.class) { 271 return true; 272 } else if (klass == android.view.SurfaceHolder.class) { 273 return true; 274 } else if (klass == android.graphics.SurfaceTexture.class) { 275 return true; 276 } 277 278 return false; 279 } 280 281 /** 282 * Determine whether or not the {@code surface} in its current state is suitable to be included 283 * in a {@link CameraDevice#createCaptureSession capture session} as an output. 284 * 285 * <p>Not all surfaces are usable with the {@link CameraDevice}, and not all configurations 286 * of that {@code surface} are compatible. Some classes that provide the {@code surface} are 287 * compatible with the {@link CameraDevice} in general 288 * (see {@link #isOutputSupportedFor(Class)}, but it is the caller's responsibility to put the 289 * {@code surface} into a state that will be compatible with the {@link CameraDevice}.</p> 290 * 291 * <p>Reasons for a {@code surface} being specifically incompatible might be: 292 * <ul> 293 * <li>Using a format that's not listed by {@link #getOutputFormats} 294 * <li>Using a format/size combination that's not listed by {@link #getOutputSizes} 295 * <li>The {@code surface} itself is not in a state where it can service a new producer.</p> 296 * </li> 297 * </ul> 298 * 299 * This is not an exhaustive list; see the particular class's documentation for further 300 * possible reasons of incompatibility.</p> 301 * 302 * @param surface a non-{@code null} {@link Surface} object reference 303 * @return {@code true} if this is supported, {@code false} otherwise 304 * 305 * @throws NullPointerException if {@code surface} was {@code null} 306 * 307 * @see CameraDevice#createCaptureSession 308 * @see #isOutputSupportedFor(Class) 309 */ 310 public boolean isOutputSupportedFor(Surface surface) { 311 checkNotNull(surface, "surface must not be null"); 312 313 throw new UnsupportedOperationException("Not implemented yet"); 314 315 // TODO: JNI function that checks the Surface's IGraphicBufferProducer state 316 } 317 318 /** 319 * Get a list of sizes compatible with {@code klass} to use as an output. 320 * 321 * <p>Since some of the supported classes may support additional formats beyond 322 * an opaque/implementation-defined (under-the-hood) format; this function only returns 323 * sizes for the implementation-defined format.</p> 324 * 325 * <p>Some classes such as {@link android.media.ImageReader} may only support user-defined 326 * formats; in particular {@link #isOutputSupportedFor(Class)} will return {@code true} for 327 * that class and this method will return an empty array (but not {@code null}).</p> 328 * 329 * <p>If a well-defined format such as {@code NV21} is required, use 330 * {@link #getOutputSizes(int)} instead.</p> 331 * 332 * <p>The {@code klass} should be a supported output, that querying 333 * {@code #isOutputSupportedFor(Class)} should return {@code true}.</p> 334 * 335 * @param klass 336 * a non-{@code null} {@link Class} object reference 337 * @return 338 * an array of supported sizes for implementation-defined formats, 339 * or {@code null} iff the {@code klass} is not a supported output 340 * 341 * @throws NullPointerException if {@code klass} was {@code null} 342 * 343 * @see #isOutputSupportedFor(Class) 344 */ 345 public <T> Size[] getOutputSizes(Class<T> klass) { 346 if (isOutputSupportedFor(klass) == false) { 347 return null; 348 } 349 350 return getInternalFormatSizes(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, /*output*/true); 351 } 352 353 /** 354 * Get a list of sizes compatible with the requested image {@code format}. 355 * 356 * <p>The {@code format} should be a supported format (one of the formats returned by 357 * {@link #getOutputFormats}).</p> 358 * 359 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 360 * @return 361 * an array of supported sizes, 362 * or {@code null} if the {@code format} is not a supported output 363 * 364 * @see ImageFormat 365 * @see PixelFormat 366 * @see #getOutputFormats 367 */ 368 public Size[] getOutputSizes(int format) { 369 return getPublicFormatSizes(format, /*output*/true); 370 } 371 372 /** 373 * Get a list of supported high speed video recording sizes. 374 * 375 * <p> When HIGH_SPEED_VIDEO is supported in 376 * {@link CameraCharacteristics#CONTROL_AVAILABLE_SCENE_MODES available scene modes}, this 377 * method will list the supported high speed video size configurations. All the sizes listed 378 * will be a subset of the sizes reported by {@link #getOutputSizes} for processed non-stalling 379 * formats (typically ImageFormat#YUV_420_888, ImageFormat#NV21, ImageFormat#YV12)</p> 380 * 381 * <p> To enable high speed video recording, application must set 382 * {@link CaptureRequest#CONTROL_SCENE_MODE} to 383 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} in capture 384 * requests and select the video size from this method and 385 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from 386 * {@link #getHighSpeedVideoFpsRangesFor} to configure the recording and preview streams and 387 * setup the recording requests. For example, if the application intends to do high speed 388 * recording, it can select the maximum size reported by this method to configure output 389 * streams. Note that for the use case of multiple output streams, application must select one 390 * unique size from this method to use. Otherwise a request error might occur. Once the size is 391 * selected, application can get the supported FPS ranges by 392 * {@link #getHighSpeedVideoFpsRangesFor}, and use these FPS ranges to setup the recording 393 * requests.</p> 394 * 395 * @return 396 * an array of supported high speed video recording sizes 397 * 398 * @see #getHighSpeedVideoFpsRangesFor(Size) 399 */ 400 public Size[] getHighSpeedVideoSizes() { 401 return (Size[]) mHighSpeedVideoSizeMap.keySet().toArray(); 402 } 403 404 /** 405 * Get the frame per second ranges (fpsMin, fpsMax) for input high speed video size. 406 * 407 * <p> See {@link #getHighSpeedVideoSizes} for how to enable high speed recording.</p> 408 * 409 * <p> For normal video recording use case, where some application will NOT set 410 * {@link CaptureRequest#CONTROL_SCENE_MODE} to 411 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} in capture 412 * requests, the {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS ranges} reported in 413 * this method must not be used to setup capture requests, or it will cause request error.</p> 414 * 415 * @param size one of the sizes returned by {@link #getHighSpeedVideoSizes()} 416 * @return 417 * An array of FPS range to use with 418 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE TARGET_FPS_RANGE} when using 419 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} scene 420 * mode. 421 * The upper bound of returned ranges is guaranteed to be larger or equal to 60. 422 * 423 * @throws IllegalArgumentException if input size does not exist in the return value of 424 * getHighSpeedVideoSizes 425 * @see #getHighSpeedVideoSizes() 426 */ 427 public Range<Integer>[] getHighSpeedVideoFpsRangesFor(Size size) { 428 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size); 429 if (fpsRangeCount == null || fpsRangeCount == 0) { 430 throw new IllegalArgumentException(String.format( 431 "Size %s does not support high speed video recording", size)); 432 } 433 434 @SuppressWarnings("unchecked") 435 Range<Integer>[] fpsRanges = new Range[fpsRangeCount]; 436 int i = 0; 437 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 438 if (size.equals(config.getSize())) { 439 fpsRanges[i++] = config.getFpsRange(); 440 } 441 } 442 return fpsRanges; 443 } 444 445 /** 446 * Get a list of supported high speed video recording FPS ranges. 447 * 448 * <p> When HIGH_SPEED_VIDEO is supported in 449 * {@link CameraCharacteristics#CONTROL_AVAILABLE_SCENE_MODES available scene modes}, this 450 * method will list the supported high speed video FPS range configurations. Application can 451 * then use {@link #getHighSpeedVideoSizesFor} to query available sizes for one of returned 452 * FPS range.</p> 453 * 454 * <p> To enable high speed video recording, application must set 455 * {@link CaptureRequest#CONTROL_SCENE_MODE} to 456 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} in capture 457 * requests and select the video size from {@link #getHighSpeedVideoSizesFor} and 458 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from 459 * this method to configure the recording and preview streams and setup the recording requests. 460 * For example, if the application intends to do high speed recording, it can select one FPS 461 * range reported by this method, query the video sizes corresponding to this FPS range by 462 * {@link #getHighSpeedVideoSizesFor} and select one of reported sizes to configure output 463 * streams. Note that for the use case of multiple output streams, application must select one 464 * unique size from {@link #getHighSpeedVideoSizesFor}, and use it for all output streams. 465 * Otherwise a request error might occur when attempting to enable 466 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO}. 467 * Once the stream is configured, application can set the FPS range in the recording requests. 468 * </p> 469 * 470 * @return 471 * an array of supported high speed video recording FPS ranges 472 * The upper bound of returned ranges is guaranteed to be larger or equal to 60. 473 * 474 * @see #getHighSpeedVideoSizesFor 475 */ 476 @SuppressWarnings("unchecked") 477 public Range<Integer>[] getHighSpeedVideoFpsRanges() { 478 return (Range<Integer>[]) mHighSpeedVideoFpsRangeMap.keySet().toArray(); 479 } 480 481 /** 482 * Get the supported video sizes for input FPS range. 483 * 484 * <p> See {@link #getHighSpeedVideoFpsRanges} for how to enable high speed recording.</p> 485 * 486 * <p> For normal video recording use case, where the application will NOT set 487 * {@link CaptureRequest#CONTROL_SCENE_MODE} to 488 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} in capture 489 * requests, the {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS ranges} reported in 490 * this method must not be used to setup capture requests, or it will cause request error.</p> 491 * 492 * @param fpsRange one of the FPS range returned by {@link #getHighSpeedVideoFpsRanges()} 493 * @return 494 * An array of video sizes to configure output stream when using 495 * {@link CaptureRequest#CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO HIGH_SPEED_VIDEO} scene 496 * mode. 497 * 498 * @throws IllegalArgumentException if input FPS range does not exist in the return value of 499 * getHighSpeedVideoFpsRanges 500 * @see #getHighSpeedVideoFpsRanges() 501 */ 502 public Size[] getHighSpeedVideoSizesFor(Range<Integer> fpsRange) { 503 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange); 504 if (sizeCount == null || sizeCount == 0) { 505 throw new IllegalArgumentException(String.format( 506 "FpsRange %s does not support high speed video recording", fpsRange)); 507 } 508 509 Size[] sizes = new Size[sizeCount]; 510 int i = 0; 511 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 512 if (fpsRange.equals(config.getFpsRange())) { 513 sizes[i++] = config.getSize(); 514 } 515 } 516 return sizes; 517 } 518 519 /** 520 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration} 521 * for the format/size combination (in nanoseconds). 522 * 523 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p> 524 * <p>{@code size} should be one of the ones returned by 525 * {@link #getOutputSizes(int)}.</p> 526 * 527 * <p>This should correspond to the frame duration when only that stream is active, with all 528 * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}. 529 * </p> 530 * 531 * <p>When multiple streams are used in a request, the minimum frame duration will be 532 * {@code max(individual stream min durations)}.</p> 533 * 534 * <p>For devices that do not support manual sensor control 535 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}), 536 * this function may return {@link #NO_MIN_FRAME_DURATION}.</p> 537 * 538 * <!-- 539 * TODO: uncomment after adding input stream support 540 * <p>The minimum frame duration of a stream (of a particular format, size) is the same 541 * regardless of whether the stream is input or output.</p> 542 * --> 543 * 544 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 545 * @param size an output-compatible size 546 * @return a minimum frame duration {@code >} 0 in nanoseconds, or 547 * {@link #NO_MIN_FRAME_DURATION} if the minimum frame duration is not available. 548 * 549 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported 550 * @throws NullPointerException if {@code size} was {@code null} 551 * 552 * @see CaptureRequest#SENSOR_FRAME_DURATION 553 * @see #getOutputStallDuration(int, Size) 554 * @see ImageFormat 555 * @see PixelFormat 556 */ 557 public long getOutputMinFrameDuration(int format, Size size) { 558 checkNotNull(size, "size must not be null"); 559 checkArgumentFormatSupported(format, /*output*/true); 560 561 return getInternalFormatDuration(imageFormatToInternal(format), size, DURATION_MIN_FRAME); 562 } 563 564 /** 565 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration} 566 * for the class/size combination (in nanoseconds). 567 * 568 * <p>This assumes a the {@code klass} is set up to use an implementation-defined format. 569 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p> 570 * 571 * <p>{@code klass} should be one of the ones which is supported by 572 * {@link #isOutputSupportedFor(Class)}.</p> 573 * 574 * <p>{@code size} should be one of the ones returned by 575 * {@link #getOutputSizes(int)}.</p> 576 * 577 * <p>This should correspond to the frame duration when only that stream is active, with all 578 * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}. 579 * </p> 580 * 581 * <p>When multiple streams are used in a request, the minimum frame duration will be 582 * {@code max(individual stream min durations)}.</p> 583 * 584 * <p>For devices that do not support manual sensor control 585 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}), 586 * this function may return {@link #NO_MIN_FRAME_DURATION}.</p> 587 * 588 * <!-- 589 * TODO: uncomment after adding input stream support 590 * <p>The minimum frame duration of a stream (of a particular format, size) is the same 591 * regardless of whether the stream is input or output.</p> 592 * --> 593 * 594 * @param klass 595 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a 596 * non-empty array returned by {@link #getOutputSizes(Class)} 597 * @param size an output-compatible size 598 * @return a minimum frame duration {@code >} 0 in nanoseconds, or 599 * {@link #NO_MIN_FRAME_DURATION} if the minimum frame duration is not available. 600 * 601 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported 602 * @throws NullPointerException if {@code size} or {@code klass} was {@code null} 603 * 604 * @see CaptureRequest#SENSOR_FRAME_DURATION 605 * @see ImageFormat 606 * @see PixelFormat 607 */ 608 public <T> long getOutputMinFrameDuration(final Class<T> klass, final Size size) { 609 if (!isOutputSupportedFor(klass)) { 610 throw new IllegalArgumentException("klass was not supported"); 611 } 612 613 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 614 size, DURATION_MIN_FRAME); 615 } 616 617 /** 618 * Get the stall duration for the format/size combination (in nanoseconds). 619 * 620 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p> 621 * <p>{@code size} should be one of the ones returned by 622 * {@link #getOutputSizes(int)}.</p> 623 * 624 * <p> 625 * A stall duration is how much extra time would get added to the normal minimum frame duration 626 * for a repeating request that has streams with non-zero stall. 627 * 628 * <p>For example, consider JPEG captures which have the following characteristics: 629 * 630 * <ul> 631 * <li>JPEG streams act like processed YUV streams in requests for which they are not included; 632 * in requests in which they are directly referenced, they act as JPEG streams. 633 * This is because supporting a JPEG stream requires the underlying YUV data to always be ready 634 * for use by a JPEG encoder, but the encoder will only be used (and impact frame duration) on 635 * requests that actually reference a JPEG stream. 636 * <li>The JPEG processor can run concurrently to the rest of the camera pipeline, but cannot 637 * process more than 1 capture at a time. 638 * </ul> 639 * 640 * <p>In other words, using a repeating YUV request would result in a steady frame rate 641 * (let's say it's 30 FPS). If a single JPEG request is submitted periodically, 642 * the frame rate will stay at 30 FPS (as long as we wait for the previous JPEG to return each 643 * time). If we try to submit a repeating YUV + JPEG request, then the frame rate will drop from 644 * 30 FPS.</p> 645 * 646 * <p>In general, submitting a new request with a non-0 stall time stream will <em>not</em> cause a 647 * frame rate drop unless there are still outstanding buffers for that stream from previous 648 * requests.</p> 649 * 650 * <p>Submitting a repeating request with streams (call this {@code S}) is the same as setting 651 * the minimum frame duration from the normal minimum frame duration corresponding to {@code S}, 652 * added with the maximum stall duration for {@code S}.</p> 653 * 654 * <p>If interleaving requests with and without a stall duration, a request will stall by the 655 * maximum of the remaining times for each can-stall stream with outstanding buffers.</p> 656 * 657 * <p>This means that a stalling request will not have an exposure start until the stall has 658 * completed.</p> 659 * 660 * <p>This should correspond to the stall duration when only that stream is active, with all 661 * processing (typically in {@code android.*.mode}) set to {@code FAST} or {@code OFF}. 662 * Setting any of the processing modes to {@code HIGH_QUALITY} effectively results in an 663 * indeterminate stall duration for all streams in a request (the regular stall calculation 664 * rules are ignored).</p> 665 * 666 * <p>The following formats may always have a stall duration: 667 * <ul> 668 * <li>{@link ImageFormat#JPEG JPEG} 669 * <li>{@link ImageFormat#RAW_SENSOR RAW16} 670 * </ul> 671 * </p> 672 * 673 * <p>The following formats will never have a stall duration: 674 * <ul> 675 * <li>{@link ImageFormat#YUV_420_888 YUV_420_888} 676 * <li>{@link #isOutputSupportedFor(Class) Implementation-Defined} 677 * </ul></p> 678 * 679 * <p> 680 * All other formats may or may not have an allowed stall duration on a per-capability basis; 681 * refer to {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES 682 * android.request.availableCapabilities} for more details.</p> 683 * </p> 684 * 685 * <p>See {@link CaptureRequest#SENSOR_FRAME_DURATION android.sensor.frameDuration} 686 * for more information about calculating the max frame rate (absent stalls).</p> 687 * 688 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 689 * @param size an output-compatible size 690 * @return a stall duration {@code >=} 0 in nanoseconds 691 * 692 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported 693 * @throws NullPointerException if {@code size} was {@code null} 694 * 695 * @see CaptureRequest#SENSOR_FRAME_DURATION 696 * @see ImageFormat 697 * @see PixelFormat 698 */ 699 public long getOutputStallDuration(int format, Size size) { 700 checkArgumentFormatSupported(format, /*output*/true); 701 702 return getInternalFormatDuration(imageFormatToInternal(format), 703 size, DURATION_STALL); 704 } 705 706 /** 707 * Get the stall duration for the class/size combination (in nanoseconds). 708 * 709 * <p>This assumes a the {@code klass} is set up to use an implementation-defined format. 710 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p> 711 * 712 * <p>{@code klass} should be one of the ones with a non-empty array returned by 713 * {@link #getOutputSizes(Class)}.</p> 714 * 715 * <p>{@code size} should be one of the ones returned by 716 * {@link #getOutputSizes(Class)}.</p> 717 * 718 * <p>See {@link #getOutputStallDuration(int, Size)} for a definition of a 719 * <em>stall duration</em>.</p> 720 * 721 * @param klass 722 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a 723 * non-empty array returned by {@link #getOutputSizes(Class)} 724 * @param size an output-compatible size 725 * @return a minimum frame duration {@code >=} 0 in nanoseconds 726 * 727 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported 728 * @throws NullPointerException if {@code size} or {@code klass} was {@code null} 729 * 730 * @see CaptureRequest#SENSOR_FRAME_DURATION 731 * @see ImageFormat 732 * @see PixelFormat 733 */ 734 public <T> long getOutputStallDuration(final Class<T> klass, final Size size) { 735 if (!isOutputSupportedFor(klass)) { 736 throw new IllegalArgumentException("klass was not supported"); 737 } 738 739 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 740 size, DURATION_STALL); 741 } 742 743 /** 744 * Check if this {@link StreamConfigurationMap} is equal to another 745 * {@link StreamConfigurationMap}. 746 * 747 * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p> 748 * 749 * @return {@code true} if the objects were equal, {@code false} otherwise 750 */ 751 @Override 752 public boolean equals(final Object obj) { 753 if (obj == null) { 754 return false; 755 } 756 if (this == obj) { 757 return true; 758 } 759 if (obj instanceof StreamConfigurationMap) { 760 final StreamConfigurationMap other = (StreamConfigurationMap) obj; 761 // XX: do we care about order? 762 return Arrays.equals(mConfigurations, other.mConfigurations) && 763 Arrays.equals(mMinFrameDurations, other.mMinFrameDurations) && 764 Arrays.equals(mStallDurations, other.mStallDurations) && 765 Arrays.equals(mHighSpeedVideoConfigurations, 766 other.mHighSpeedVideoConfigurations); 767 } 768 return false; 769 } 770 771 /** 772 * {@inheritDoc} 773 */ 774 @Override 775 public int hashCode() { 776 // XX: do we care about order? 777 return HashCodeHelpers.hashCode( 778 mConfigurations, mMinFrameDurations, 779 mStallDurations, mHighSpeedVideoConfigurations); 780 } 781 782 // Check that the argument is supported by #getOutputFormats or #getInputFormats 783 private int checkArgumentFormatSupported(int format, boolean output) { 784 checkArgumentFormat(format); 785 786 int[] formats = output ? getOutputFormats() : getInputFormats(); 787 for (int i = 0; i < formats.length; ++i) { 788 if (format == formats[i]) { 789 return format; 790 } 791 } 792 793 throw new IllegalArgumentException(String.format( 794 "format %x is not supported by this stream configuration map", format)); 795 } 796 797 /** 798 * Ensures that the format is either user-defined or implementation defined. 799 * 800 * <p>If a format has a different internal representation than the public representation, 801 * passing in the public representation here will fail.</p> 802 * 803 * <p>For example if trying to use {@link ImageFormat#JPEG}: 804 * it has a different public representation than the internal representation 805 * {@code HAL_PIXEL_FORMAT_BLOB}, this check will fail.</p> 806 * 807 * <p>Any invalid/undefined formats will raise an exception.</p> 808 * 809 * @param format image format 810 * @return the format 811 * 812 * @throws IllegalArgumentException if the format was invalid 813 */ 814 static int checkArgumentFormatInternal(int format) { 815 switch (format) { 816 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 817 case HAL_PIXEL_FORMAT_BLOB: 818 return format; 819 case ImageFormat.JPEG: 820 throw new IllegalArgumentException( 821 "ImageFormat.JPEG is an unknown internal format"); 822 default: 823 return checkArgumentFormat(format); 824 } 825 } 826 827 /** 828 * Ensures that the format is publicly user-defined in either ImageFormat or PixelFormat. 829 * 830 * <p>If a format has a different public representation than the internal representation, 831 * passing in the internal representation here will fail.</p> 832 * 833 * <p>For example if trying to use {@code HAL_PIXEL_FORMAT_BLOB}: 834 * it has a different internal representation than the public representation 835 * {@link ImageFormat#JPEG}, this check will fail.</p> 836 * 837 * <p>Any invalid/undefined formats will raise an exception, including implementation-defined. 838 * </p> 839 * 840 * <p>Note that {@code @hide} and deprecated formats will not pass this check.</p> 841 * 842 * @param format image format 843 * @return the format 844 * 845 * @throws IllegalArgumentException if the format was not user-defined 846 */ 847 static int checkArgumentFormat(int format) { 848 // TODO: remove this hack , CTS shouldn't have been using internal constants 849 if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) { 850 Log.w(TAG, "RAW_OPAQUE is not yet a published format; allowing it anyway"); 851 return format; 852 } 853 854 if (!ImageFormat.isPublicFormat(format) && !PixelFormat.isPublicFormat(format)) { 855 throw new IllegalArgumentException(String.format( 856 "format 0x%x was not defined in either ImageFormat or PixelFormat", format)); 857 } 858 859 return format; 860 } 861 862 /** 863 * Convert a public-visible {@code ImageFormat} into an internal format 864 * compatible with {@code graphics.h}. 865 * 866 * <p>In particular these formats are converted: 867 * <ul> 868 * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.JPEG 869 * </ul> 870 * </p> 871 * 872 * <p>Passing in an implementation-defined format which has no public equivalent will fail; 873 * as will passing in a public format which has a different internal format equivalent. 874 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 875 * 876 * <p>All other formats are returned as-is, no further invalid check is performed.</p> 877 * 878 * <p>This function is the dual of {@link #imageFormatToInternal}.</p> 879 * 880 * @param format image format from {@link ImageFormat} or {@link PixelFormat} 881 * @return the converted image formats 882 * 883 * @throws IllegalArgumentException 884 * if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or 885 * {@link ImageFormat#JPEG} 886 * 887 * @see ImageFormat 888 * @see PixelFormat 889 * @see #checkArgumentFormat 890 */ 891 static int imageFormatToPublic(int format) { 892 switch (format) { 893 case HAL_PIXEL_FORMAT_BLOB: 894 return ImageFormat.JPEG; 895 case ImageFormat.JPEG: 896 throw new IllegalArgumentException( 897 "ImageFormat.JPEG is an unknown internal format"); 898 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 899 throw new IllegalArgumentException( 900 "IMPLEMENTATION_DEFINED must not leak to public API"); 901 default: 902 return format; 903 } 904 } 905 906 /** 907 * Convert image formats from internal to public formats (in-place). 908 * 909 * @param formats an array of image formats 910 * @return {@code formats} 911 * 912 * @see #imageFormatToPublic 913 */ 914 static int[] imageFormatToPublic(int[] formats) { 915 if (formats == null) { 916 return null; 917 } 918 919 for (int i = 0; i < formats.length; ++i) { 920 formats[i] = imageFormatToPublic(formats[i]); 921 } 922 923 return formats; 924 } 925 926 /** 927 * Convert a public format compatible with {@code ImageFormat} to an internal format 928 * from {@code graphics.h}. 929 * 930 * <p>In particular these formats are converted: 931 * <ul> 932 * <li>ImageFormat.JPEG => HAL_PIXEL_FORMAT_BLOB 933 * </ul> 934 * </p> 935 * 936 * <p>Passing in an implementation-defined format here will fail (it's not a public format); 937 * as will passing in an internal format which has a different public format equivalent. 938 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 939 * 940 * <p>All other formats are returned as-is, no invalid check is performed.</p> 941 * 942 * <p>This function is the dual of {@link #imageFormatToPublic}.</p> 943 * 944 * @param format public image format from {@link ImageFormat} or {@link PixelFormat} 945 * @return the converted image formats 946 * 947 * @see ImageFormat 948 * @see PixelFormat 949 * 950 * @throws IllegalArgumentException 951 * if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} 952 */ 953 static int imageFormatToInternal(int format) { 954 switch (format) { 955 case ImageFormat.JPEG: 956 return HAL_PIXEL_FORMAT_BLOB; 957 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 958 throw new IllegalArgumentException( 959 "IMPLEMENTATION_DEFINED is not allowed via public API"); 960 default: 961 return format; 962 } 963 } 964 965 /** 966 * Convert image formats from public to internal formats (in-place). 967 * 968 * @param formats an array of image formats 969 * @return {@code formats} 970 * 971 * @see #imageFormatToInternal 972 * 973 * @hide 974 */ 975 public static int[] imageFormatToInternal(int[] formats) { 976 if (formats == null) { 977 return null; 978 } 979 980 for (int i = 0; i < formats.length; ++i) { 981 formats[i] = imageFormatToInternal(formats[i]); 982 } 983 984 return formats; 985 } 986 987 private Size[] getPublicFormatSizes(int format, boolean output) { 988 try { 989 checkArgumentFormatSupported(format, output); 990 } catch (IllegalArgumentException e) { 991 return null; 992 } 993 994 format = imageFormatToInternal(format); 995 996 return getInternalFormatSizes(format, output); 997 } 998 999 private Size[] getInternalFormatSizes(int format, boolean output) { 1000 HashMap<Integer, Integer> formatsMap = getFormatsMap(output); 1001 1002 Integer sizesCount = formatsMap.get(format); 1003 if (sizesCount == null) { 1004 throw new IllegalArgumentException("format not available"); 1005 } 1006 1007 int len = sizesCount; 1008 Size[] sizes = new Size[len]; 1009 int sizeIndex = 0; 1010 1011 for (StreamConfiguration config : mConfigurations) { 1012 if (config.getFormat() == format && config.isOutput() == output) { 1013 sizes[sizeIndex++] = config.getSize(); 1014 } 1015 } 1016 1017 if (sizeIndex != len) { 1018 throw new AssertionError( 1019 "Too few sizes (expected " + len + ", actual " + sizeIndex + ")"); 1020 } 1021 1022 return sizes; 1023 } 1024 1025 /** Get the list of publically visible output formats; does not include IMPL_DEFINED */ 1026 private int[] getPublicFormats(boolean output) { 1027 int[] formats = new int[getPublicFormatCount(output)]; 1028 1029 int i = 0; 1030 1031 for (int format : getFormatsMap(output).keySet()) { 1032 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 1033 formats[i++] = format; 1034 } 1035 } 1036 1037 if (formats.length != i) { 1038 throw new AssertionError("Too few formats " + i + ", expected " + formats.length); 1039 } 1040 1041 return imageFormatToPublic(formats); 1042 } 1043 1044 /** Get the format -> size count map for either output or input formats */ 1045 private HashMap<Integer, Integer> getFormatsMap(boolean output) { 1046 return output ? mOutputFormats : mInputFormats; 1047 } 1048 1049 private long getInternalFormatDuration(int format, Size size, int duration) { 1050 // assume format is already checked, since its internal 1051 1052 if (!arrayContains(getInternalFormatSizes(format, /*output*/true), size)) { 1053 throw new IllegalArgumentException("size was not supported"); 1054 } 1055 1056 StreamConfigurationDuration[] durations = getDurations(duration); 1057 1058 for (StreamConfigurationDuration configurationDuration : durations) { 1059 if (configurationDuration.getFormat() == format && 1060 configurationDuration.getWidth() == size.getWidth() && 1061 configurationDuration.getHeight() == size.getHeight()) { 1062 return configurationDuration.getDuration(); 1063 } 1064 } 1065 1066 return getDurationDefault(duration); 1067 } 1068 1069 /** 1070 * Get the durations array for the kind of duration 1071 * 1072 * @see #DURATION_MIN_FRAME 1073 * @see #DURATION_STALL 1074 * */ 1075 private StreamConfigurationDuration[] getDurations(int duration) { 1076 switch (duration) { 1077 case DURATION_MIN_FRAME: 1078 return mMinFrameDurations; 1079 case DURATION_STALL: 1080 return mStallDurations; 1081 default: 1082 throw new IllegalArgumentException("duration was invalid"); 1083 } 1084 } 1085 1086 private long getDurationDefault(int duration) { 1087 switch (duration) { 1088 case DURATION_MIN_FRAME: 1089 return NO_MIN_FRAME_DURATION; 1090 case DURATION_STALL: 1091 return 0L; // OK. A lack of a stall duration implies a 0 stall duration 1092 default: 1093 throw new IllegalArgumentException("duration was invalid"); 1094 } 1095 } 1096 1097 /** Count the number of publicly-visible output formats */ 1098 private int getPublicFormatCount(boolean output) { 1099 HashMap<Integer, Integer> formatsMap = getFormatsMap(output); 1100 1101 int size = formatsMap.size(); 1102 if (formatsMap.containsKey(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) { 1103 size -= 1; 1104 } 1105 return size; 1106 } 1107 1108 private static <T> boolean arrayContains(T[] array, T element) { 1109 if (array == null) { 1110 return false; 1111 } 1112 1113 for (T el : array) { 1114 if (Objects.equals(el, element)) { 1115 return true; 1116 } 1117 } 1118 1119 return false; 1120 } 1121 1122 // from system/core/include/system/graphics.h 1123 private static final int HAL_PIXEL_FORMAT_BLOB = 0x21; 1124 private static final int HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22; 1125 private static final int HAL_PIXEL_FORMAT_RAW_OPAQUE = 0x24; 1126 1127 /** 1128 * @see #getDurations(int) 1129 * @see #getDurationDefault(int) 1130 */ 1131 private static final int DURATION_MIN_FRAME = 0; 1132 private static final int DURATION_STALL = 1; 1133 1134 private final StreamConfiguration[] mConfigurations; 1135 private final StreamConfigurationDuration[] mMinFrameDurations; 1136 private final StreamConfigurationDuration[] mStallDurations; 1137 private final HighSpeedVideoConfiguration[] mHighSpeedVideoConfigurations; 1138 1139 /** ImageFormat -> num output sizes mapping */ 1140 private final HashMap</*ImageFormat*/Integer, /*Count*/Integer> mOutputFormats = 1141 new HashMap<Integer, Integer>(); 1142 /** ImageFormat -> num input sizes mapping */ 1143 private final HashMap</*ImageFormat*/Integer, /*Count*/Integer> mInputFormats = 1144 new HashMap<Integer, Integer>(); 1145 /** High speed video Size -> FPS range count mapping*/ 1146 private final HashMap</*HighSpeedVideoSize*/Size, /*Count*/Integer> mHighSpeedVideoSizeMap = 1147 new HashMap<Size, Integer>(); 1148 /** High speed video FPS range -> Size count mapping*/ 1149 private final HashMap</*HighSpeedVideoFpsRange*/Range<Integer>, /*Count*/Integer> 1150 mHighSpeedVideoFpsRangeMap = new HashMap<Range<Integer>, Integer>(); 1151 1152} 1153