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 17 package android.hardware.camera2.params; 18 19 import static com.android.internal.util.Preconditions.checkArrayElementsNotNull; 20 21 import android.graphics.ImageFormat; 22 import android.graphics.PixelFormat; 23 import android.hardware.camera2.CameraCharacteristics; 24 import android.hardware.camera2.CameraDevice; 25 import android.hardware.camera2.CameraMetadata; 26 import android.hardware.camera2.CaptureRequest; 27 import android.hardware.camera2.utils.HashCodeHelpers; 28 import android.hardware.camera2.utils.SurfaceUtils; 29 import android.util.Range; 30 import android.util.Size; 31 import android.util.SparseIntArray; 32 import android.view.Surface; 33 34 import java.util.Arrays; 35 import java.util.HashMap; 36 import java.util.Objects; 37 import java.util.Set; 38 39 /** 40 * Immutable class to store the available stream 41 * {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP configurations} to set up 42 * {@link android.view.Surface Surfaces} for creating a 43 * {@link android.hardware.camera2.CameraCaptureSession capture session} with 44 * {@link android.hardware.camera2.CameraDevice#createCaptureSession}. 45 * <!-- TODO: link to input stream configuration --> 46 * 47 * <p>This is the authoritative list for all <!-- input/ -->output formats (and sizes respectively 48 * for that format) that are supported by a camera device.</p> 49 * 50 * <p>This also contains the minimum frame durations and stall durations for each format/size 51 * combination that can be used to calculate effective frame rate when submitting multiple captures. 52 * </p> 53 * 54 * <p>An instance of this object is available from {@link CameraCharacteristics} using 55 * the {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP} key and the 56 * {@link CameraCharacteristics#get} method.</p> 57 * 58 * <pre><code>{@code 59 * CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); 60 * StreamConfigurationMap configs = characteristics.get( 61 * CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 62 * }</code></pre> 63 * 64 * @see CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP 65 * @see CameraDevice#createCaptureSession 66 */ 67 public final class StreamConfigurationMap { 68 69 private static final String TAG = "StreamConfigurationMap"; 70 71 private static final int MAX_DIMEN_FOR_ROUNDING = 1920; // maximum allowed width for rounding 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 depthConfigurations a non-{@code null} array of depth {@link StreamConfiguration} 83 * @param depthMinFrameDurations a non-{@code null} array of depth 84 * {@link StreamConfigurationDuration} 85 * @param depthStallDurations a non-{@code null} array of depth 86 * {@link StreamConfigurationDuration} 87 * @param dynamicDepthConfigurations a non-{@code null} array of dynamic depth 88 * {@link StreamConfiguration} 89 * @param dynamicDepthMinFrameDurations a non-{@code null} array of dynamic depth 90 * {@link StreamConfigurationDuration} 91 * @param dynamicDepthStallDurations a non-{@code null} array of dynamic depth 92 * {@link StreamConfigurationDuration} 93 * @param heicConfigurations a non-{@code null} array of heic {@link StreamConfiguration} 94 * @param heicMinFrameDurations a non-{@code null} array of heic 95 * {@link StreamConfigurationDuration} 96 * @param heicStallDurations a non-{@code null} array of heic 97 * {@link StreamConfigurationDuration} 98 * @param highSpeedVideoConfigurations an array of {@link HighSpeedVideoConfiguration}, null if 99 * camera device does not support high speed video recording 100 * @param listHighResolution a flag indicating whether the device supports BURST_CAPTURE 101 * and thus needs a separate list of slow high-resolution output sizes 102 * @throws NullPointerException if any of the arguments except highSpeedVideoConfigurations 103 * were {@code null} or any subelements were {@code null} 104 * 105 * @hide 106 */ StreamConfigurationMap( StreamConfiguration[] configurations, StreamConfigurationDuration[] minFrameDurations, StreamConfigurationDuration[] stallDurations, StreamConfiguration[] depthConfigurations, StreamConfigurationDuration[] depthMinFrameDurations, StreamConfigurationDuration[] depthStallDurations, StreamConfiguration[] dynamicDepthConfigurations, StreamConfigurationDuration[] dynamicDepthMinFrameDurations, StreamConfigurationDuration[] dynamicDepthStallDurations, StreamConfiguration[] heicConfigurations, StreamConfigurationDuration[] heicMinFrameDurations, StreamConfigurationDuration[] heicStallDurations, HighSpeedVideoConfiguration[] highSpeedVideoConfigurations, ReprocessFormatsMap inputOutputFormatsMap, boolean listHighResolution)107 public StreamConfigurationMap( 108 StreamConfiguration[] configurations, 109 StreamConfigurationDuration[] minFrameDurations, 110 StreamConfigurationDuration[] stallDurations, 111 StreamConfiguration[] depthConfigurations, 112 StreamConfigurationDuration[] depthMinFrameDurations, 113 StreamConfigurationDuration[] depthStallDurations, 114 StreamConfiguration[] dynamicDepthConfigurations, 115 StreamConfigurationDuration[] dynamicDepthMinFrameDurations, 116 StreamConfigurationDuration[] dynamicDepthStallDurations, 117 StreamConfiguration[] heicConfigurations, 118 StreamConfigurationDuration[] heicMinFrameDurations, 119 StreamConfigurationDuration[] heicStallDurations, 120 HighSpeedVideoConfiguration[] highSpeedVideoConfigurations, 121 ReprocessFormatsMap inputOutputFormatsMap, 122 boolean listHighResolution) { 123 this(configurations, minFrameDurations, stallDurations, 124 depthConfigurations, depthMinFrameDurations, depthStallDurations, 125 dynamicDepthConfigurations, dynamicDepthMinFrameDurations, 126 dynamicDepthStallDurations, 127 heicConfigurations, heicMinFrameDurations, heicStallDurations, 128 highSpeedVideoConfigurations, inputOutputFormatsMap, listHighResolution, 129 /*enforceImplementationDefined*/ true); 130 } 131 132 /** 133 * Create a new {@link StreamConfigurationMap}. 134 * 135 * <p>The array parameters ownership is passed to this object after creation; do not 136 * write to them after this constructor is invoked.</p> 137 * 138 * @param configurations a non-{@code null} array of {@link StreamConfiguration} 139 * @param minFrameDurations a non-{@code null} array of {@link StreamConfigurationDuration} 140 * @param stallDurations a non-{@code null} array of {@link StreamConfigurationDuration} 141 * @param depthConfigurations a non-{@code null} array of depth {@link StreamConfiguration} 142 * @param depthMinFrameDurations a non-{@code null} array of depth 143 * {@link StreamConfigurationDuration} 144 * @param depthStallDurations a non-{@code null} array of depth 145 * {@link StreamConfigurationDuration} 146 * @param dynamicDepthConfigurations a non-{@code null} array of dynamic depth 147 * {@link StreamConfiguration} 148 * @param dynamicDepthMinFrameDurations a non-{@code null} array of dynamic depth 149 * {@link StreamConfigurationDuration} 150 * @param dynamicDepthStallDurations a non-{@code null} array of dynamic depth 151 * {@link StreamConfigurationDuration} 152 * @param heicConfigurations a non-{@code null} array of heic {@link StreamConfiguration} 153 * @param heicMinFrameDurations a non-{@code null} array of heic 154 * {@link StreamConfigurationDuration} 155 * @param heicStallDurations a non-{@code null} array of heic 156 * {@link StreamConfigurationDuration} 157 * @param highSpeedVideoConfigurations an array of {@link HighSpeedVideoConfiguration}, null if 158 * camera device does not support high speed video recording 159 * @param listHighResolution a flag indicating whether the device supports BURST_CAPTURE 160 * and thus needs a separate list of slow high-resolution output sizes 161 * @param enforceImplementationDefined a flag indicating whether 162 * IMPLEMENTATION_DEFINED format configuration must be present 163 * @throws NullPointerException if any of the arguments except highSpeedVideoConfigurations 164 * were {@code null} or any subelements were {@code null} 165 * 166 * @hide 167 */ StreamConfigurationMap( StreamConfiguration[] configurations, StreamConfigurationDuration[] minFrameDurations, StreamConfigurationDuration[] stallDurations, StreamConfiguration[] depthConfigurations, StreamConfigurationDuration[] depthMinFrameDurations, StreamConfigurationDuration[] depthStallDurations, StreamConfiguration[] dynamicDepthConfigurations, StreamConfigurationDuration[] dynamicDepthMinFrameDurations, StreamConfigurationDuration[] dynamicDepthStallDurations, StreamConfiguration[] heicConfigurations, StreamConfigurationDuration[] heicMinFrameDurations, StreamConfigurationDuration[] heicStallDurations, HighSpeedVideoConfiguration[] highSpeedVideoConfigurations, ReprocessFormatsMap inputOutputFormatsMap, boolean listHighResolution, boolean enforceImplementationDefined)168 public StreamConfigurationMap( 169 StreamConfiguration[] configurations, 170 StreamConfigurationDuration[] minFrameDurations, 171 StreamConfigurationDuration[] stallDurations, 172 StreamConfiguration[] depthConfigurations, 173 StreamConfigurationDuration[] depthMinFrameDurations, 174 StreamConfigurationDuration[] depthStallDurations, 175 StreamConfiguration[] dynamicDepthConfigurations, 176 StreamConfigurationDuration[] dynamicDepthMinFrameDurations, 177 StreamConfigurationDuration[] dynamicDepthStallDurations, 178 StreamConfiguration[] heicConfigurations, 179 StreamConfigurationDuration[] heicMinFrameDurations, 180 StreamConfigurationDuration[] heicStallDurations, 181 HighSpeedVideoConfiguration[] highSpeedVideoConfigurations, 182 ReprocessFormatsMap inputOutputFormatsMap, 183 boolean listHighResolution, 184 boolean enforceImplementationDefined) { 185 186 if (configurations == null && 187 depthConfigurations == null && 188 heicConfigurations == null) { 189 throw new NullPointerException("At least one of color/depth/heic configurations " + 190 "must not be null"); 191 } 192 193 if (configurations == null) { 194 // If no color configurations exist, ensure depth ones do 195 mConfigurations = new StreamConfiguration[0]; 196 mMinFrameDurations = new StreamConfigurationDuration[0]; 197 mStallDurations = new StreamConfigurationDuration[0]; 198 } else { 199 mConfigurations = checkArrayElementsNotNull(configurations, "configurations"); 200 mMinFrameDurations = checkArrayElementsNotNull(minFrameDurations, "minFrameDurations"); 201 mStallDurations = checkArrayElementsNotNull(stallDurations, "stallDurations"); 202 } 203 204 mListHighResolution = listHighResolution; 205 206 if (depthConfigurations == null) { 207 mDepthConfigurations = new StreamConfiguration[0]; 208 mDepthMinFrameDurations = new StreamConfigurationDuration[0]; 209 mDepthStallDurations = new StreamConfigurationDuration[0]; 210 } else { 211 mDepthConfigurations = checkArrayElementsNotNull(depthConfigurations, 212 "depthConfigurations"); 213 mDepthMinFrameDurations = checkArrayElementsNotNull(depthMinFrameDurations, 214 "depthMinFrameDurations"); 215 mDepthStallDurations = checkArrayElementsNotNull(depthStallDurations, 216 "depthStallDurations"); 217 } 218 219 if (dynamicDepthConfigurations == null) { 220 mDynamicDepthConfigurations = new StreamConfiguration[0]; 221 mDynamicDepthMinFrameDurations = new StreamConfigurationDuration[0]; 222 mDynamicDepthStallDurations = new StreamConfigurationDuration[0]; 223 } else { 224 mDynamicDepthConfigurations = checkArrayElementsNotNull(dynamicDepthConfigurations, 225 "dynamicDepthConfigurations"); 226 mDynamicDepthMinFrameDurations = checkArrayElementsNotNull( 227 dynamicDepthMinFrameDurations, "dynamicDepthMinFrameDurations"); 228 mDynamicDepthStallDurations = checkArrayElementsNotNull(dynamicDepthStallDurations, 229 "dynamicDepthStallDurations"); 230 } 231 232 if (heicConfigurations == null) { 233 mHeicConfigurations = new StreamConfiguration[0]; 234 mHeicMinFrameDurations = new StreamConfigurationDuration[0]; 235 mHeicStallDurations = new StreamConfigurationDuration[0]; 236 } else { 237 mHeicConfigurations = checkArrayElementsNotNull(heicConfigurations, 238 "heicConfigurations"); 239 mHeicMinFrameDurations = checkArrayElementsNotNull(heicMinFrameDurations, 240 "heicMinFrameDurations"); 241 mHeicStallDurations = checkArrayElementsNotNull(heicStallDurations, 242 "heicStallDurations"); 243 } 244 245 if (highSpeedVideoConfigurations == null) { 246 mHighSpeedVideoConfigurations = new HighSpeedVideoConfiguration[0]; 247 } else { 248 mHighSpeedVideoConfigurations = checkArrayElementsNotNull( 249 highSpeedVideoConfigurations, "highSpeedVideoConfigurations"); 250 } 251 252 // For each format, track how many sizes there are available to configure 253 for (StreamConfiguration config : mConfigurations) { 254 int fmt = config.getFormat(); 255 SparseIntArray map = null; 256 if (config.isOutput()) { 257 mAllOutputFormats.put(fmt, mAllOutputFormats.get(fmt) + 1); 258 long duration = 0; 259 if (mListHighResolution) { 260 for (StreamConfigurationDuration configurationDuration : mMinFrameDurations) { 261 if (configurationDuration.getFormat() == fmt && 262 configurationDuration.getWidth() == config.getSize().getWidth() && 263 configurationDuration.getHeight() == config.getSize().getHeight()) { 264 duration = configurationDuration.getDuration(); 265 break; 266 } 267 } 268 } 269 map = duration <= DURATION_20FPS_NS ? 270 mOutputFormats : mHighResOutputFormats; 271 } else { 272 map = mInputFormats; 273 } 274 map.put(fmt, map.get(fmt) + 1); 275 } 276 277 // For each depth format, track how many sizes there are available to configure 278 for (StreamConfiguration config : mDepthConfigurations) { 279 if (!config.isOutput()) { 280 // Ignoring input depth configs 281 continue; 282 } 283 284 mDepthOutputFormats.put(config.getFormat(), 285 mDepthOutputFormats.get(config.getFormat()) + 1); 286 } 287 for (StreamConfiguration config : mDynamicDepthConfigurations) { 288 if (!config.isOutput()) { 289 // Ignoring input configs 290 continue; 291 } 292 293 mDynamicDepthOutputFormats.put(config.getFormat(), 294 mDynamicDepthOutputFormats.get(config.getFormat()) + 1); 295 } 296 297 // For each heic format, track how many sizes there are available to configure 298 for (StreamConfiguration config : mHeicConfigurations) { 299 if (!config.isOutput()) { 300 // Ignoring input depth configs 301 continue; 302 } 303 304 mHeicOutputFormats.put(config.getFormat(), 305 mHeicOutputFormats.get(config.getFormat()) + 1); 306 } 307 308 if (configurations != null && enforceImplementationDefined && 309 mOutputFormats.indexOfKey(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) < 0) { 310 throw new AssertionError( 311 "At least one stream configuration for IMPLEMENTATION_DEFINED must exist"); 312 } 313 314 // For each Size/FPS range, track how many FPS range/Size there are available 315 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 316 Size size = config.getSize(); 317 Range<Integer> fpsRange = config.getFpsRange(); 318 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size); 319 if (fpsRangeCount == null) { 320 fpsRangeCount = 0; 321 } 322 mHighSpeedVideoSizeMap.put(size, fpsRangeCount + 1); 323 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange); 324 if (sizeCount == null) { 325 sizeCount = 0; 326 } 327 mHighSpeedVideoFpsRangeMap.put(fpsRange, sizeCount + 1); 328 } 329 330 mInputOutputFormatsMap = inputOutputFormatsMap; 331 } 332 333 /** 334 * Get the image {@code format} output formats in this stream configuration. 335 * 336 * <p>All image formats returned by this function will be defined in either {@link ImageFormat} 337 * or in {@link PixelFormat} (and there is no possibility of collision).</p> 338 * 339 * <p>Formats listed in this array are guaranteed to return true if queried with 340 * {@link #isOutputSupportedFor(int)}.</p> 341 * 342 * @return an array of integer format 343 * 344 * @see ImageFormat 345 * @see PixelFormat 346 */ getOutputFormats()347 public int[] getOutputFormats() { 348 return getPublicFormats(/*output*/true); 349 } 350 351 /** 352 * Get the image {@code format} output formats for a reprocessing input format. 353 * 354 * <p>When submitting a {@link CaptureRequest} with an input Surface of a given format, 355 * the only allowed target outputs of the {@link CaptureRequest} are the ones with a format 356 * listed in the return value of this method. Including any other output Surface as a target 357 * will throw an IllegalArgumentException. If no output format is supported given the input 358 * format, an empty int[] will be returned.</p> 359 * 360 * <p>All image formats returned by this function will be defined in either {@link ImageFormat} 361 * or in {@link PixelFormat} (and there is no possibility of collision).</p> 362 * 363 * <p>Formats listed in this array are guaranteed to return true if queried with 364 * {@link #isOutputSupportedFor(int)}.</p> 365 * 366 * @return an array of integer format 367 * 368 * @see ImageFormat 369 * @see PixelFormat 370 */ getValidOutputFormatsForInput(int inputFormat)371 public int[] getValidOutputFormatsForInput(int inputFormat) { 372 if (mInputOutputFormatsMap == null) { 373 return new int[0]; 374 } 375 376 int[] outputs = mInputOutputFormatsMap.getOutputs(inputFormat); 377 if (mHeicOutputFormats.size() > 0) { 378 // All reprocessing formats map contain JPEG. 379 int[] outputsWithHeic = Arrays.copyOf(outputs, outputs.length+1); 380 outputsWithHeic[outputs.length] = ImageFormat.HEIC; 381 return outputsWithHeic; 382 } else { 383 return outputs; 384 } 385 } 386 387 /** 388 * Get the image {@code format} input formats in this stream configuration. 389 * 390 * <p>All image formats returned by this function will be defined in either {@link ImageFormat} 391 * or in {@link PixelFormat} (and there is no possibility of collision).</p> 392 * 393 * @return an array of integer format 394 * 395 * @see ImageFormat 396 * @see PixelFormat 397 */ getInputFormats()398 public int[] getInputFormats() { 399 return getPublicFormats(/*output*/false); 400 } 401 402 /** 403 * Get the supported input sizes for this input format. 404 * 405 * <p>The format must have come from {@link #getInputFormats}; otherwise 406 * {@code null} is returned.</p> 407 * 408 * @param format a format from {@link #getInputFormats} 409 * @return a non-empty array of sizes, or {@code null} if the format was not available. 410 */ getInputSizes(final int format)411 public Size[] getInputSizes(final int format) { 412 return getPublicFormatSizes(format, /*output*/false, /*highRes*/false); 413 } 414 415 /** 416 * Determine whether or not output surfaces with a particular user-defined format can be passed 417 * {@link CameraDevice#createCaptureSession createCaptureSession}. 418 * 419 * <p>This method determines that the output {@code format} is supported by the camera device; 420 * each output {@code surface} target may or may not itself support that {@code format}. 421 * Refer to the class which provides the surface for additional documentation.</p> 422 * 423 * <p>Formats for which this returns {@code true} are guaranteed to exist in the result 424 * returned by {@link #getOutputSizes}.</p> 425 * 426 * @param format an image format from either {@link ImageFormat} or {@link PixelFormat} 427 * @return 428 * {@code true} iff using a {@code surface} with this {@code format} will be 429 * supported with {@link CameraDevice#createCaptureSession} 430 * 431 * @throws IllegalArgumentException 432 * if the image format was not a defined named constant 433 * from either {@link ImageFormat} or {@link PixelFormat} 434 * 435 * @see ImageFormat 436 * @see PixelFormat 437 * @see CameraDevice#createCaptureSession 438 */ isOutputSupportedFor(int format)439 public boolean isOutputSupportedFor(int format) { 440 checkArgumentFormat(format); 441 442 int internalFormat = imageFormatToInternal(format); 443 int dataspace = imageFormatToDataspace(format); 444 if (dataspace == HAL_DATASPACE_DEPTH) { 445 return mDepthOutputFormats.indexOfKey(internalFormat) >= 0; 446 } else if (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) { 447 return mDynamicDepthOutputFormats.indexOfKey(internalFormat) >= 0; 448 } else if (dataspace == HAL_DATASPACE_HEIF) { 449 return mHeicOutputFormats.indexOfKey(internalFormat) >= 0; 450 } else { 451 return getFormatsMap(/*output*/true).indexOfKey(internalFormat) >= 0; 452 } 453 } 454 455 /** 456 * Determine whether or not output streams can be configured with a particular class 457 * as a consumer. 458 * 459 * <p>The following list is generally usable for outputs: 460 * <ul> 461 * <li>{@link android.media.ImageReader} - 462 * Recommended for image processing or streaming to external resources (such as a file or 463 * network) 464 * <li>{@link android.media.MediaRecorder} - 465 * Recommended for recording video (simple to use) 466 * <li>{@link android.media.MediaCodec} - 467 * Recommended for recording video (more complicated to use, with more flexibility) 468 * <li>{@link android.renderscript.Allocation} - 469 * Recommended for image processing with {@link android.renderscript RenderScript} 470 * <li>{@link android.view.SurfaceHolder} - 471 * Recommended for low-power camera preview with {@link android.view.SurfaceView} 472 * <li>{@link android.graphics.SurfaceTexture} - 473 * Recommended for OpenGL-accelerated preview processing or compositing with 474 * {@link android.view.TextureView} 475 * </ul> 476 * </p> 477 * 478 * <p>Generally speaking this means that creating a {@link Surface} from that class <i>may</i> 479 * provide a producer endpoint that is suitable to be used with 480 * {@link CameraDevice#createCaptureSession}.</p> 481 * 482 * <p>Since not all of the above classes support output of all format and size combinations, 483 * the particular combination should be queried with {@link #isOutputSupportedFor(Surface)}.</p> 484 * 485 * @param klass a non-{@code null} {@link Class} object reference 486 * @return {@code true} if this class is supported as an output, {@code false} otherwise 487 * 488 * @throws NullPointerException if {@code klass} was {@code null} 489 * 490 * @see CameraDevice#createCaptureSession 491 * @see #isOutputSupportedFor(Surface) 492 */ isOutputSupportedFor(Class<T> klass)493 public static <T> boolean isOutputSupportedFor(Class<T> klass) { 494 Objects.requireNonNull(klass, "klass must not be null"); 495 496 if (klass == android.media.ImageReader.class) { 497 return true; 498 } else if (klass == android.media.MediaRecorder.class) { 499 return true; 500 } else if (klass == android.media.MediaCodec.class) { 501 return true; 502 } else if (klass == android.renderscript.Allocation.class) { 503 return true; 504 } else if (klass == android.view.SurfaceHolder.class) { 505 return true; 506 } else if (klass == android.graphics.SurfaceTexture.class) { 507 return true; 508 } 509 510 return false; 511 } 512 513 /** 514 * Determine whether or not the {@code surface} in its current state is suitable to be included 515 * in a {@link CameraDevice#createCaptureSession capture session} as an output. 516 * 517 * <p>Not all surfaces are usable with the {@link CameraDevice}, and not all configurations 518 * of that {@code surface} are compatible. Some classes that provide the {@code surface} are 519 * compatible with the {@link CameraDevice} in general 520 * (see {@link #isOutputSupportedFor(Class)}, but it is the caller's responsibility to put the 521 * {@code surface} into a state that will be compatible with the {@link CameraDevice}.</p> 522 * 523 * <p>Reasons for a {@code surface} being specifically incompatible might be: 524 * <ul> 525 * <li>Using a format that's not listed by {@link #getOutputFormats} 526 * <li>Using a format/size combination that's not listed by {@link #getOutputSizes} 527 * <li>The {@code surface} itself is not in a state where it can service a new producer.</p> 528 * </li> 529 * </ul> 530 * 531 * <p>Surfaces from flexible sources will return true even if the exact size of the Surface does 532 * not match a camera-supported size, as long as the format (or class) is supported and the 533 * camera device supports a size that is equal to or less than 1080p in that format. If such as 534 * Surface is used to create a capture session, it will have its size rounded to the nearest 535 * supported size, below or equal to 1080p. Flexible sources include SurfaceView, SurfaceTexture, 536 * and ImageReader.</p> 537 * 538 * <p>This is not an exhaustive list; see the particular class's documentation for further 539 * possible reasons of incompatibility.</p> 540 * 541 * @param surface a non-{@code null} {@link Surface} object reference 542 * @return {@code true} if this is supported, {@code false} otherwise 543 * 544 * @throws NullPointerException if {@code surface} was {@code null} 545 * @throws IllegalArgumentException if the Surface endpoint is no longer valid 546 * 547 * @see CameraDevice#createCaptureSession 548 * @see #isOutputSupportedFor(Class) 549 */ isOutputSupportedFor(Surface surface)550 public boolean isOutputSupportedFor(Surface surface) { 551 Objects.requireNonNull(surface, "surface must not be null"); 552 553 Size surfaceSize = SurfaceUtils.getSurfaceSize(surface); 554 int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface); 555 int surfaceDataspace = SurfaceUtils.getSurfaceDataspace(surface); 556 557 // See if consumer is flexible. 558 boolean isFlexible = SurfaceUtils.isFlexibleConsumer(surface); 559 560 StreamConfiguration[] configs = 561 surfaceDataspace == HAL_DATASPACE_DEPTH ? mDepthConfigurations : 562 surfaceDataspace == HAL_DATASPACE_DYNAMIC_DEPTH ? mDynamicDepthConfigurations : 563 surfaceDataspace == HAL_DATASPACE_HEIF ? mHeicConfigurations : 564 mConfigurations; 565 for (StreamConfiguration config : configs) { 566 if (config.getFormat() == surfaceFormat && config.isOutput()) { 567 // Matching format, either need exact size match, or a flexible consumer 568 // and a size no bigger than MAX_DIMEN_FOR_ROUNDING 569 if (config.getSize().equals(surfaceSize)) { 570 return true; 571 } else if (isFlexible && 572 (config.getSize().getWidth() <= MAX_DIMEN_FOR_ROUNDING)) { 573 return true; 574 } 575 } 576 } 577 return false; 578 } 579 580 /** 581 * Determine whether or not the particular stream configuration is suitable to be included 582 * in a {@link CameraDevice#createCaptureSession capture session} as an output. 583 * 584 * @param size stream configuration size 585 * @param format stream configuration format 586 * @return {@code true} if this is supported, {@code false} otherwise 587 * 588 * @see CameraDevice#createCaptureSession 589 * @see #isOutputSupportedFor(Class) 590 * @hide 591 */ isOutputSupportedFor(Size size, int format)592 public boolean isOutputSupportedFor(Size size, int format) { 593 int internalFormat = imageFormatToInternal(format); 594 int dataspace = imageFormatToDataspace(format); 595 596 StreamConfiguration[] configs = 597 dataspace == HAL_DATASPACE_DEPTH ? mDepthConfigurations : 598 dataspace == HAL_DATASPACE_DYNAMIC_DEPTH ? mDynamicDepthConfigurations : 599 dataspace == HAL_DATASPACE_HEIF ? mHeicConfigurations : 600 mConfigurations; 601 for (StreamConfiguration config : configs) { 602 if ((config.getFormat() == internalFormat) && config.isOutput() && 603 config.getSize().equals(size)) { 604 return true; 605 } 606 } 607 608 return false; 609 } 610 611 /** 612 * Get a list of sizes compatible with {@code klass} to use as an output. 613 * 614 * <p>Some of the supported classes may support additional formats beyond 615 * {@link ImageFormat#PRIVATE}; this function only returns 616 * sizes for {@link ImageFormat#PRIVATE}. For example, {@link android.media.ImageReader} 617 * supports {@link ImageFormat#YUV_420_888} and {@link ImageFormat#PRIVATE}, this method will 618 * only return the sizes for {@link ImageFormat#PRIVATE} for {@link android.media.ImageReader} 619 * class.</p> 620 * 621 * <p>If a well-defined format such as {@code NV21} is required, use 622 * {@link #getOutputSizes(int)} instead.</p> 623 * 624 * <p>The {@code klass} should be a supported output, that querying 625 * {@code #isOutputSupportedFor(Class)} should return {@code true}.</p> 626 * 627 * @param klass 628 * a non-{@code null} {@link Class} object reference 629 * @return 630 * an array of supported sizes for {@link ImageFormat#PRIVATE} format, 631 * or {@code null} iff the {@code klass} is not a supported output. 632 * 633 * 634 * @throws NullPointerException if {@code klass} was {@code null} 635 * 636 * @see #isOutputSupportedFor(Class) 637 */ getOutputSizes(Class<T> klass)638 public <T> Size[] getOutputSizes(Class<T> klass) { 639 if (isOutputSupportedFor(klass) == false) { 640 return null; 641 } 642 643 return getInternalFormatSizes(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 644 HAL_DATASPACE_UNKNOWN,/*output*/true, /*highRes*/false); 645 } 646 647 /** 648 * Get a list of sizes compatible with the requested image {@code format}. 649 * 650 * <p>The {@code format} should be a supported format (one of the formats returned by 651 * {@link #getOutputFormats}).</p> 652 * 653 * As of API level 23, the {@link #getHighResolutionOutputSizes} method can be used on devices 654 * that support the 655 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE} 656 * capability to get a list of high-resolution output sizes that cannot operate at the preferred 657 * 20fps rate. This means that for some supported formats, this method will return an empty 658 * list, if all the supported resolutions operate at below 20fps. For devices that do not 659 * support the BURST_CAPTURE capability, all output resolutions are listed through this method. 660 * 661 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 662 * @return 663 * an array of supported sizes, 664 * or {@code null} if the {@code format} is not a supported output 665 * 666 * @see ImageFormat 667 * @see PixelFormat 668 * @see #getOutputFormats 669 */ getOutputSizes(int format)670 public Size[] getOutputSizes(int format) { 671 return getPublicFormatSizes(format, /*output*/true, /*highRes*/ false); 672 } 673 674 /** 675 * Get a list of supported high speed video recording sizes. 676 * <p> 677 * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is 678 * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will 679 * list the supported high speed video size configurations. All the sizes listed will be a 680 * subset of the sizes reported by {@link #getOutputSizes} for processed non-stalling formats 681 * (typically {@link ImageFormat#PRIVATE} {@link ImageFormat#YUV_420_888}, etc.) 682 * </p> 683 * <p> 684 * To enable high speed video recording, application must create a constrained create high speed 685 * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit 686 * a CaptureRequest list created by 687 * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList} 688 * to this session. The application must select the video size from this method and 689 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from 690 * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and 691 * generate the high speed request list. For example, if the application intends to do high 692 * speed recording, it can select the maximum size reported by this method to create high speed 693 * capture session. Note that for the use case of multiple output streams, application must 694 * select one unique size from this method to use (e.g., preview and recording streams must have 695 * the same size). Otherwise, the high speed session creation will fail. Once the size is 696 * selected, application can get the supported FPS ranges by 697 * {@link #getHighSpeedVideoFpsRangesFor}, and use these FPS ranges to setup the recording 698 * request lists via 699 * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. 700 * </p> 701 * 702 * @return an array of supported high speed video recording sizes 703 * @see #getHighSpeedVideoFpsRangesFor(Size) 704 * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO 705 * @see CameraDevice#createConstrainedHighSpeedCaptureSession 706 * @see android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList 707 */ getHighSpeedVideoSizes()708 public Size[] getHighSpeedVideoSizes() { 709 Set<Size> keySet = mHighSpeedVideoSizeMap.keySet(); 710 return keySet.toArray(new Size[keySet.size()]); 711 } 712 713 /** 714 * Get the frame per second ranges (fpsMin, fpsMax) for input high speed video size. 715 * <p> 716 * See {@link #getHighSpeedVideoFpsRanges} for how to enable high speed recording. 717 * </p> 718 * <p> 719 * The {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS ranges} reported in this method 720 * must not be used to setup capture requests that are submitted to unconstrained capture 721 * sessions, or it will result in {@link IllegalArgumentException IllegalArgumentExceptions}. 722 * </p> 723 * <p> 724 * See {@link #getHighSpeedVideoFpsRanges} for the characteristics of the returned FPS ranges. 725 * </p> 726 * 727 * @param size one of the sizes returned by {@link #getHighSpeedVideoSizes()} 728 * @return an array of supported high speed video recording FPS ranges The upper bound of 729 * returned ranges is guaranteed to be greater than or equal to 120. 730 * @throws IllegalArgumentException if input size does not exist in the return value of 731 * getHighSpeedVideoSizes 732 * @see #getHighSpeedVideoSizes() 733 * @see #getHighSpeedVideoFpsRanges() 734 */ getHighSpeedVideoFpsRangesFor(Size size)735 public Range<Integer>[] getHighSpeedVideoFpsRangesFor(Size size) { 736 Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size); 737 if (fpsRangeCount == null || fpsRangeCount == 0) { 738 throw new IllegalArgumentException(String.format( 739 "Size %s does not support high speed video recording", size)); 740 } 741 742 @SuppressWarnings("unchecked") 743 Range<Integer>[] fpsRanges = new Range[fpsRangeCount]; 744 int i = 0; 745 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 746 if (size.equals(config.getSize())) { 747 fpsRanges[i++] = config.getFpsRange(); 748 } 749 } 750 return fpsRanges; 751 } 752 753 /** 754 * Get a list of supported high speed video recording FPS ranges. 755 * <p> 756 * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is 757 * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will 758 * list the supported high speed video FPS range configurations. Application can then use 759 * {@link #getHighSpeedVideoSizesFor} to query available sizes for one of returned FPS range. 760 * </p> 761 * <p> 762 * To enable high speed video recording, application must create a constrained create high speed 763 * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit 764 * a CaptureRequest list created by 765 * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList} 766 * to this session. The application must select the video size from this method and 767 * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from 768 * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and 769 * generate the high speed request list. For example, if the application intends to do high 770 * speed recording, it can select one FPS range reported by this method, query the video sizes 771 * corresponding to this FPS range by {@link #getHighSpeedVideoSizesFor} and use one of reported 772 * sizes to create a high speed capture session. Note that for the use case of multiple output 773 * streams, application must select one unique size from this method to use (e.g., preview and 774 * recording streams must have the same size). Otherwise, the high speed session creation will 775 * fail. Once the high speed capture session is created, the application can set the FPS range 776 * in the recording request lists via 777 * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. 778 * </p> 779 * <p> 780 * The FPS ranges reported by this method will have below characteristics: 781 * <li>The fpsMin and fpsMax will be a multiple 30fps.</li> 782 * <li>The fpsMin will be no less than 30fps, the fpsMax will be no less than 120fps.</li> 783 * <li>At least one range will be a fixed FPS range where fpsMin == fpsMax.</li> 784 * <li>For each fixed FPS range, there will be one corresponding variable FPS range 785 * [30, fps_max] or [60, fps_max]. These kinds of FPS ranges are suitable for preview-only 786 * use cases where the application doesn't want the camera device always produce higher frame 787 * rate than the display refresh rate. Both 30fps and 60fps preview rate will not be 788 * supported for the same recording rate.</li> 789 * </p> 790 * 791 * @return an array of supported high speed video recording FPS ranges The upper bound of 792 * returned ranges is guaranteed to be larger or equal to 120. 793 * @see #getHighSpeedVideoSizesFor 794 * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO 795 * @see CameraDevice#createConstrainedHighSpeedCaptureSession 796 * @see android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList 797 */ 798 @SuppressWarnings("unchecked") getHighSpeedVideoFpsRanges()799 public Range<Integer>[] getHighSpeedVideoFpsRanges() { 800 Set<Range<Integer>> keySet = mHighSpeedVideoFpsRangeMap.keySet(); 801 return keySet.toArray(new Range[keySet.size()]); 802 } 803 804 /** 805 * Get the supported video sizes for an input high speed FPS range. 806 * 807 * <p> See {@link #getHighSpeedVideoSizes} for how to enable high speed recording.</p> 808 * 809 * @param fpsRange one of the FPS range returned by {@link #getHighSpeedVideoFpsRanges()} 810 * @return An array of video sizes to create high speed capture sessions for high speed streaming 811 * use cases. 812 * 813 * @throws IllegalArgumentException if input FPS range does not exist in the return value of 814 * getHighSpeedVideoFpsRanges 815 * @see #getHighSpeedVideoFpsRanges() 816 */ getHighSpeedVideoSizesFor(Range<Integer> fpsRange)817 public Size[] getHighSpeedVideoSizesFor(Range<Integer> fpsRange) { 818 Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange); 819 if (sizeCount == null || sizeCount == 0) { 820 throw new IllegalArgumentException(String.format( 821 "FpsRange %s does not support high speed video recording", fpsRange)); 822 } 823 824 Size[] sizes = new Size[sizeCount]; 825 int i = 0; 826 for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) { 827 if (fpsRange.equals(config.getFpsRange())) { 828 sizes[i++] = config.getSize(); 829 } 830 } 831 return sizes; 832 } 833 834 /** 835 * Get a list of supported high resolution sizes, which cannot operate at full BURST_CAPTURE 836 * rate. 837 * 838 * <p>This includes all output sizes that cannot meet the 20 fps frame rate requirements for the 839 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE} 840 * capability. This does not include the stall duration, so for example, a JPEG or RAW16 output 841 * resolution with a large stall duration but a minimum frame duration that's above 20 fps will 842 * still be listed in the regular {@link #getOutputSizes} list. All the sizes on this list that 843 * are less than 24 megapixels are still guaranteed to operate at a rate of at least 10 fps, 844 * not including stall duration. Sizes on this list that are at least 24 megapixels are allowed 845 * to operate at less than 10 fps.</p> 846 * 847 * <p>For a device that does not support the BURST_CAPTURE capability, this list will be 848 * {@code null}, since resolutions in the {@link #getOutputSizes} list are already not 849 * guaranteed to meet >= 20 fps rate requirements. For a device that does support the 850 * BURST_CAPTURE capability, this list may be empty, if all supported resolutions meet the 20 851 * fps requirement.</p> 852 * 853 * @return an array of supported slower high-resolution sizes, or {@code null} if the 854 * BURST_CAPTURE capability is not supported 855 */ getHighResolutionOutputSizes(int format)856 public Size[] getHighResolutionOutputSizes(int format) { 857 if (!mListHighResolution) return null; 858 859 return getPublicFormatSizes(format, /*output*/true, /*highRes*/ true); 860 } 861 862 /** 863 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration} 864 * for the format/size combination (in nanoseconds). 865 * 866 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p> 867 * <p>{@code size} should be one of the ones returned by 868 * {@link #getOutputSizes(int)}.</p> 869 * 870 * <p>This corresponds to the minimum frame duration (maximum frame rate) possible when only 871 * that stream is configured in a session, with all processing (typically in 872 * {@code android.*.mode}) set to either {@code OFF} or {@code FAST}. </p> 873 * 874 * <p>When multiple streams are used in a session, the minimum frame duration will be 875 * {@code max(individual stream min durations)}. See {@link #getOutputStallDuration} for 876 * details of timing for formats that may cause frame rate slowdown when they are targeted by a 877 * capture request.</p> 878 * 879 * <p>For devices that do not support manual sensor control 880 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}), 881 * this function may return 0.</p> 882 * 883 * <p>The minimum frame duration of a stream (of a particular format, size) is the same 884 * regardless of whether the stream is input or output.</p> 885 * 886 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 887 * @param size an output-compatible size 888 * @return a minimum frame duration {@code >} 0 in nanoseconds, or 889 * 0 if the minimum frame duration is not available. 890 * 891 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported 892 * @throws NullPointerException if {@code size} was {@code null} 893 * 894 * @see CaptureRequest#SENSOR_FRAME_DURATION 895 * @see #getOutputStallDuration(int, Size) 896 * @see ImageFormat 897 * @see PixelFormat 898 */ getOutputMinFrameDuration(int format, Size size)899 public long getOutputMinFrameDuration(int format, Size size) { 900 Objects.requireNonNull(size, "size must not be null"); 901 checkArgumentFormatSupported(format, /*output*/true); 902 903 return getInternalFormatDuration(imageFormatToInternal(format), 904 imageFormatToDataspace(format), 905 size, 906 DURATION_MIN_FRAME); 907 } 908 909 /** 910 * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration} 911 * for the class/size combination (in nanoseconds). 912 * 913 * <p>This assumes that the {@code klass} is set up to use {@link ImageFormat#PRIVATE}. 914 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p> 915 * 916 * <p>{@code klass} should be one of the ones which is supported by 917 * {@link #isOutputSupportedFor(Class)}.</p> 918 * 919 * <p>{@code size} should be one of the ones returned by 920 * {@link #getOutputSizes(int)}.</p> 921 * 922 * <p>This corresponds to the minimum frame duration (maximum frame rate) possible when only 923 * that stream is configured in a session, with all processing (typically in 924 * {@code android.*.mode}) set to either {@code OFF} or {@code FAST}. </p> 925 * 926 * <p>When multiple streams are used in a session, the minimum frame duration will be 927 * {@code max(individual stream min durations)}. See {@link #getOutputStallDuration} for 928 * details of timing for formats that may cause frame rate slowdown when they are targeted by a 929 * capture request.</p> 930 * 931 * <p>For devices that do not support manual sensor control 932 * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}), 933 * this function may return 0.</p> 934 * 935 * <p>The minimum frame duration of a stream (of a particular format, size) is the same 936 * regardless of whether the stream is input or output.</p> 937 * 938 * @param klass 939 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a 940 * non-empty array returned by {@link #getOutputSizes(Class)} 941 * @param size an output-compatible size 942 * @return a minimum frame duration {@code >} 0 in nanoseconds, or 943 * 0 if the minimum frame duration is not available. 944 * 945 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported 946 * @throws NullPointerException if {@code size} or {@code klass} was {@code null} 947 * 948 * @see CaptureRequest#SENSOR_FRAME_DURATION 949 * @see ImageFormat 950 * @see PixelFormat 951 */ getOutputMinFrameDuration(final Class<T> klass, final Size size)952 public <T> long getOutputMinFrameDuration(final Class<T> klass, final Size size) { 953 if (!isOutputSupportedFor(klass)) { 954 throw new IllegalArgumentException("klass was not supported"); 955 } 956 957 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 958 HAL_DATASPACE_UNKNOWN, 959 size, DURATION_MIN_FRAME); 960 } 961 962 /** 963 * Get the stall duration for the format/size combination (in nanoseconds). 964 * 965 * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p> 966 * <p>{@code size} should be one of the ones returned by 967 * {@link #getOutputSizes(int)}.</p> 968 * 969 * <p> 970 * A stall duration is how much extra time would get added to the normal minimum frame duration 971 * for a repeating request that has streams with non-zero stall. 972 * 973 * <p>For example, consider JPEG captures which have the following characteristics: 974 * 975 * <ul> 976 * <li>JPEG streams act like processed YUV streams in requests for which they are not included; 977 * in requests in which they are directly referenced, they act as JPEG streams. 978 * This is because supporting a JPEG stream requires the underlying YUV data to always be ready 979 * for use by a JPEG encoder, but the encoder will only be used (and impact frame duration) on 980 * requests that actually reference a JPEG stream. 981 * <li>The JPEG processor can run concurrently to the rest of the camera pipeline, but cannot 982 * process more than 1 capture at a time. 983 * </ul> 984 * 985 * <p>In other words, using a repeating YUV request would result in a steady frame rate 986 * (let's say it's 30 FPS). If a single JPEG request is submitted periodically, 987 * the frame rate will stay at 30 FPS (as long as we wait for the previous JPEG to return each 988 * time). If we try to submit a repeating YUV + JPEG request, then the frame rate will drop from 989 * 30 FPS.</p> 990 * 991 * <p>In general, submitting a new request with a non-0 stall time stream will <em>not</em> cause a 992 * frame rate drop unless there are still outstanding buffers for that stream from previous 993 * requests.</p> 994 * 995 * <p>Submitting a repeating request with streams (call this {@code S}) is the same as setting 996 * the minimum frame duration from the normal minimum frame duration corresponding to {@code S}, 997 * added with the maximum stall duration for {@code S}.</p> 998 * 999 * <p>If interleaving requests with and without a stall duration, a request will stall by the 1000 * maximum of the remaining times for each can-stall stream with outstanding buffers.</p> 1001 * 1002 * <p>This means that a stalling request will not have an exposure start until the stall has 1003 * completed.</p> 1004 * 1005 * <p>This should correspond to the stall duration when only that stream is active, with all 1006 * processing (typically in {@code android.*.mode}) set to {@code FAST} or {@code OFF}. 1007 * Setting any of the processing modes to {@code HIGH_QUALITY} effectively results in an 1008 * indeterminate stall duration for all streams in a request (the regular stall calculation 1009 * rules are ignored).</p> 1010 * 1011 * <p>The following formats may always have a stall duration: 1012 * <ul> 1013 * <li>{@link ImageFormat#JPEG JPEG} 1014 * <li>{@link ImageFormat#RAW_SENSOR RAW16} 1015 * <li>{@link ImageFormat#RAW_PRIVATE RAW_PRIVATE} 1016 * </ul> 1017 * </p> 1018 * 1019 * <p>The following formats will never have a stall duration: 1020 * <ul> 1021 * <li>{@link ImageFormat#YUV_420_888 YUV_420_888} 1022 * <li>{@link ImageFormat#PRIVATE PRIVATE} 1023 * </ul></p> 1024 * 1025 * <p> 1026 * All other formats may or may not have an allowed stall duration on a per-capability basis; 1027 * refer to {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES 1028 * android.request.availableCapabilities} for more details.</p> 1029 * </p> 1030 * 1031 * <p>See {@link CaptureRequest#SENSOR_FRAME_DURATION android.sensor.frameDuration} 1032 * for more information about calculating the max frame rate (absent stalls).</p> 1033 * 1034 * @param format an image format from {@link ImageFormat} or {@link PixelFormat} 1035 * @param size an output-compatible size 1036 * @return a stall duration {@code >=} 0 in nanoseconds 1037 * 1038 * @throws IllegalArgumentException if {@code format} or {@code size} was not supported 1039 * @throws NullPointerException if {@code size} was {@code null} 1040 * 1041 * @see CaptureRequest#SENSOR_FRAME_DURATION 1042 * @see ImageFormat 1043 * @see PixelFormat 1044 */ getOutputStallDuration(int format, Size size)1045 public long getOutputStallDuration(int format, Size size) { 1046 checkArgumentFormatSupported(format, /*output*/true); 1047 1048 return getInternalFormatDuration(imageFormatToInternal(format), 1049 imageFormatToDataspace(format), 1050 size, 1051 DURATION_STALL); 1052 } 1053 1054 /** 1055 * Get the stall duration for the class/size combination (in nanoseconds). 1056 * 1057 * <p>This assumes that the {@code klass} is set up to use {@link ImageFormat#PRIVATE}. 1058 * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p> 1059 * 1060 * <p>{@code klass} should be one of the ones with a non-empty array returned by 1061 * {@link #getOutputSizes(Class)}.</p> 1062 * 1063 * <p>{@code size} should be one of the ones returned by 1064 * {@link #getOutputSizes(Class)}.</p> 1065 * 1066 * <p>See {@link #getOutputStallDuration(int, Size)} for a definition of a 1067 * <em>stall duration</em>.</p> 1068 * 1069 * @param klass 1070 * a class which is supported by {@link #isOutputSupportedFor(Class)} and has a 1071 * non-empty array returned by {@link #getOutputSizes(Class)} 1072 * @param size an output-compatible size 1073 * @return a minimum frame duration {@code >=} 0 in nanoseconds 1074 * 1075 * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported 1076 * @throws NullPointerException if {@code size} or {@code klass} was {@code null} 1077 * 1078 * @see CaptureRequest#SENSOR_FRAME_DURATION 1079 * @see ImageFormat 1080 * @see PixelFormat 1081 */ getOutputStallDuration(final Class<T> klass, final Size size)1082 public <T> long getOutputStallDuration(final Class<T> klass, final Size size) { 1083 if (!isOutputSupportedFor(klass)) { 1084 throw new IllegalArgumentException("klass was not supported"); 1085 } 1086 1087 return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 1088 HAL_DATASPACE_UNKNOWN, size, DURATION_STALL); 1089 } 1090 1091 /** 1092 * Check if this {@link StreamConfigurationMap} is equal to another 1093 * {@link StreamConfigurationMap}. 1094 * 1095 * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p> 1096 * 1097 * @return {@code true} if the objects were equal, {@code false} otherwise 1098 */ 1099 @Override equals(final Object obj)1100 public boolean equals(final Object obj) { 1101 if (obj == null) { 1102 return false; 1103 } 1104 if (this == obj) { 1105 return true; 1106 } 1107 if (obj instanceof StreamConfigurationMap) { 1108 final StreamConfigurationMap other = (StreamConfigurationMap) obj; 1109 // XX: do we care about order? 1110 return Arrays.equals(mConfigurations, other.mConfigurations) && 1111 Arrays.equals(mMinFrameDurations, other.mMinFrameDurations) && 1112 Arrays.equals(mStallDurations, other.mStallDurations) && 1113 Arrays.equals(mDepthConfigurations, other.mDepthConfigurations) && 1114 Arrays.equals(mDepthMinFrameDurations, other.mDepthMinFrameDurations) && 1115 Arrays.equals(mDepthStallDurations, other.mDepthStallDurations) && 1116 Arrays.equals(mDynamicDepthConfigurations, other.mDynamicDepthConfigurations) && 1117 Arrays.equals(mDynamicDepthMinFrameDurations, 1118 other.mDynamicDepthMinFrameDurations) && 1119 Arrays.equals(mDynamicDepthStallDurations, other.mDynamicDepthStallDurations) && 1120 Arrays.equals(mHeicConfigurations, other.mHeicConfigurations) && 1121 Arrays.equals(mHeicMinFrameDurations, other.mHeicMinFrameDurations) && 1122 Arrays.equals(mHeicStallDurations, other.mHeicStallDurations) && 1123 Arrays.equals(mHighSpeedVideoConfigurations, 1124 other.mHighSpeedVideoConfigurations); 1125 } 1126 return false; 1127 } 1128 1129 /** 1130 * {@inheritDoc} 1131 */ 1132 @Override hashCode()1133 public int hashCode() { 1134 // XX: do we care about order? 1135 return HashCodeHelpers.hashCodeGeneric( 1136 mConfigurations, mMinFrameDurations, mStallDurations, 1137 mDepthConfigurations, mDepthMinFrameDurations, mDepthStallDurations, 1138 mDynamicDepthConfigurations, mDynamicDepthMinFrameDurations, 1139 mDynamicDepthStallDurations, mHeicConfigurations, 1140 mHeicMinFrameDurations, mHeicStallDurations, 1141 mHighSpeedVideoConfigurations); 1142 } 1143 1144 // Check that the argument is supported by #getOutputFormats or #getInputFormats checkArgumentFormatSupported(int format, boolean output)1145 private int checkArgumentFormatSupported(int format, boolean output) { 1146 checkArgumentFormat(format); 1147 1148 int internalFormat = imageFormatToInternal(format); 1149 int internalDataspace = imageFormatToDataspace(format); 1150 1151 if (output) { 1152 if (internalDataspace == HAL_DATASPACE_DEPTH) { 1153 if (mDepthOutputFormats.indexOfKey(internalFormat) >= 0) { 1154 return format; 1155 } 1156 } else if (internalDataspace == HAL_DATASPACE_DYNAMIC_DEPTH) { 1157 if (mDynamicDepthOutputFormats.indexOfKey(internalFormat) >= 0) { 1158 return format; 1159 } 1160 } else if (internalDataspace == HAL_DATASPACE_HEIF) { 1161 if (mHeicOutputFormats.indexOfKey(internalFormat) >= 0) { 1162 return format; 1163 } 1164 } else { 1165 if (mAllOutputFormats.indexOfKey(internalFormat) >= 0) { 1166 return format; 1167 } 1168 } 1169 } else { 1170 if (mInputFormats.indexOfKey(internalFormat) >= 0) { 1171 return format; 1172 } 1173 } 1174 1175 throw new IllegalArgumentException(String.format( 1176 "format %x is not supported by this stream configuration map", format)); 1177 } 1178 1179 /** 1180 * Ensures that the format is either user-defined or implementation defined. 1181 * 1182 * <p>If a format has a different internal representation than the public representation, 1183 * passing in the public representation here will fail.</p> 1184 * 1185 * <p>For example if trying to use {@link ImageFormat#JPEG}: 1186 * it has a different public representation than the internal representation 1187 * {@code HAL_PIXEL_FORMAT_BLOB}, this check will fail.</p> 1188 * 1189 * <p>Any invalid/undefined formats will raise an exception.</p> 1190 * 1191 * @param format image format 1192 * @return the format 1193 * 1194 * @throws IllegalArgumentException if the format was invalid 1195 */ checkArgumentFormatInternal(int format)1196 static int checkArgumentFormatInternal(int format) { 1197 switch (format) { 1198 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 1199 case HAL_PIXEL_FORMAT_BLOB: 1200 case HAL_PIXEL_FORMAT_RAW_OPAQUE: 1201 case HAL_PIXEL_FORMAT_Y16: 1202 return format; 1203 case ImageFormat.JPEG: 1204 case ImageFormat.HEIC: 1205 throw new IllegalArgumentException( 1206 "An unknown internal format: " + format); 1207 default: 1208 return checkArgumentFormat(format); 1209 } 1210 } 1211 1212 /** 1213 * Ensures that the format is publicly user-defined in either ImageFormat or PixelFormat. 1214 * 1215 * <p>If a format has a different public representation than the internal representation, 1216 * passing in the internal representation here will fail.</p> 1217 * 1218 * <p>For example if trying to use {@code HAL_PIXEL_FORMAT_BLOB}: 1219 * it has a different internal representation than the public representation 1220 * {@link ImageFormat#JPEG}, this check will fail.</p> 1221 * 1222 * <p>Any invalid/undefined formats will raise an exception, including implementation-defined. 1223 * </p> 1224 * 1225 * <p>Note that {@code @hide} and deprecated formats will not pass this check.</p> 1226 * 1227 * @param format image format 1228 * @return the format 1229 * 1230 * @throws IllegalArgumentException if the format was not user-defined 1231 */ checkArgumentFormat(int format)1232 static int checkArgumentFormat(int format) { 1233 if (!ImageFormat.isPublicFormat(format) && !PixelFormat.isPublicFormat(format)) { 1234 throw new IllegalArgumentException(String.format( 1235 "format 0x%x was not defined in either ImageFormat or PixelFormat", format)); 1236 } 1237 1238 return format; 1239 } 1240 1241 /** 1242 * Convert an internal format compatible with {@code graphics.h} into public-visible 1243 * {@code ImageFormat}. This assumes the dataspace of the format is not HAL_DATASPACE_DEPTH. 1244 * 1245 * <p>In particular these formats are converted: 1246 * <ul> 1247 * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.JPEG</li> 1248 * </ul> 1249 * </p> 1250 * 1251 * <p>Passing in a format which has no public equivalent will fail; 1252 * as will passing in a public format which has a different internal format equivalent. 1253 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 1254 * 1255 * <p>All other formats are returned as-is, no further invalid check is performed.</p> 1256 * 1257 * <p>This function is the dual of {@link #imageFormatToInternal} for dataspaces other than 1258 * HAL_DATASPACE_DEPTH.</p> 1259 * 1260 * @param format image format from {@link ImageFormat} or {@link PixelFormat} 1261 * @return the converted image formats 1262 * 1263 * @throws IllegalArgumentException 1264 * if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or 1265 * {@link ImageFormat#JPEG} 1266 * 1267 * @see ImageFormat 1268 * @see PixelFormat 1269 * @see #checkArgumentFormat 1270 * @hide 1271 */ imageFormatToPublic(int format)1272 public static int imageFormatToPublic(int format) { 1273 switch (format) { 1274 case HAL_PIXEL_FORMAT_BLOB: 1275 return ImageFormat.JPEG; 1276 case ImageFormat.JPEG: 1277 throw new IllegalArgumentException( 1278 "ImageFormat.JPEG is an unknown internal format"); 1279 default: 1280 return format; 1281 } 1282 } 1283 1284 /** 1285 * Convert an internal format compatible with {@code graphics.h} into public-visible 1286 * {@code ImageFormat}. This assumes the dataspace of the format is HAL_DATASPACE_DEPTH. 1287 * 1288 * <p>In particular these formats are converted: 1289 * <ul> 1290 * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.DEPTH_POINT_CLOUD 1291 * <li>HAL_PIXEL_FORMAT_Y16 => ImageFormat.DEPTH16 1292 * </ul> 1293 * </p> 1294 * 1295 * <p>Passing in an implementation-defined format which has no public equivalent will fail; 1296 * as will passing in a public format which has a different internal format equivalent. 1297 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 1298 * 1299 * <p>All other formats are returned as-is, no further invalid check is performed.</p> 1300 * 1301 * <p>This function is the dual of {@link #imageFormatToInternal} for formats associated with 1302 * HAL_DATASPACE_DEPTH.</p> 1303 * 1304 * @param format image format from {@link ImageFormat} or {@link PixelFormat} 1305 * @return the converted image formats 1306 * 1307 * @throws IllegalArgumentException 1308 * if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or 1309 * {@link ImageFormat#JPEG} 1310 * 1311 * @see ImageFormat 1312 * @see PixelFormat 1313 * @see #checkArgumentFormat 1314 * @hide 1315 */ depthFormatToPublic(int format)1316 public static int depthFormatToPublic(int format) { 1317 switch (format) { 1318 case HAL_PIXEL_FORMAT_BLOB: 1319 return ImageFormat.DEPTH_POINT_CLOUD; 1320 case HAL_PIXEL_FORMAT_Y16: 1321 return ImageFormat.DEPTH16; 1322 case HAL_PIXEL_FORMAT_RAW16: 1323 return ImageFormat.RAW_DEPTH; 1324 case HAL_PIXEL_FORMAT_RAW10: 1325 return ImageFormat.RAW_DEPTH10; 1326 case ImageFormat.JPEG: 1327 throw new IllegalArgumentException( 1328 "ImageFormat.JPEG is an unknown internal format"); 1329 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 1330 throw new IllegalArgumentException( 1331 "IMPLEMENTATION_DEFINED must not leak to public API"); 1332 default: 1333 throw new IllegalArgumentException( 1334 "Unknown DATASPACE_DEPTH format " + format); 1335 } 1336 } 1337 1338 /** 1339 * Convert image formats from internal to public formats (in-place). 1340 * 1341 * @param formats an array of image formats 1342 * @return {@code formats} 1343 * 1344 * @see #imageFormatToPublic 1345 */ imageFormatToPublic(int[] formats)1346 static int[] imageFormatToPublic(int[] formats) { 1347 if (formats == null) { 1348 return null; 1349 } 1350 1351 for (int i = 0; i < formats.length; ++i) { 1352 formats[i] = imageFormatToPublic(formats[i]); 1353 } 1354 1355 return formats; 1356 } 1357 1358 /** 1359 * Convert a public format compatible with {@code ImageFormat} to an internal format 1360 * from {@code graphics.h}. 1361 * 1362 * <p>In particular these formats are converted: 1363 * <ul> 1364 * <li>ImageFormat.JPEG => HAL_PIXEL_FORMAT_BLOB 1365 * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_PIXEL_FORMAT_BLOB 1366 * <li>ImageFormat.DEPTH_JPEG => HAL_PIXEL_FORMAT_BLOB 1367 * <li>ImageFormat.HEIC => HAL_PIXEL_FORMAT_BLOB 1368 * <li>ImageFormat.DEPTH16 => HAL_PIXEL_FORMAT_Y16 1369 * </ul> 1370 * </p> 1371 * 1372 * <p>Passing in an internal format which has a different public format equivalent will fail. 1373 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 1374 * 1375 * <p>All other formats are returned as-is, no invalid check is performed.</p> 1376 * 1377 * <p>This function is the dual of {@link #imageFormatToPublic}.</p> 1378 * 1379 * @param format public image format from {@link ImageFormat} or {@link PixelFormat} 1380 * @return the converted image formats 1381 * 1382 * @see ImageFormat 1383 * @see PixelFormat 1384 * 1385 * @throws IllegalArgumentException 1386 * if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} 1387 */ imageFormatToInternal(int format)1388 static int imageFormatToInternal(int format) { 1389 switch (format) { 1390 case ImageFormat.JPEG: 1391 case ImageFormat.DEPTH_POINT_CLOUD: 1392 case ImageFormat.DEPTH_JPEG: 1393 case ImageFormat.HEIC: 1394 return HAL_PIXEL_FORMAT_BLOB; 1395 case ImageFormat.DEPTH16: 1396 return HAL_PIXEL_FORMAT_Y16; 1397 case ImageFormat.RAW_DEPTH: 1398 return HAL_PIXEL_FORMAT_RAW16; 1399 case ImageFormat.RAW_DEPTH10: 1400 return HAL_PIXEL_FORMAT_RAW10; 1401 default: 1402 return format; 1403 } 1404 } 1405 1406 /** 1407 * Convert a public format compatible with {@code ImageFormat} to an internal dataspace 1408 * from {@code graphics.h}. 1409 * 1410 * <p>In particular these formats are converted: 1411 * <ul> 1412 * <li>ImageFormat.JPEG => HAL_DATASPACE_V0_JFIF 1413 * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_DATASPACE_DEPTH 1414 * <li>ImageFormat.DEPTH16 => HAL_DATASPACE_DEPTH 1415 * <li>ImageFormat.DEPTH_JPEG => HAL_DATASPACE_DYNAMIC_DEPTH 1416 * <li>ImageFormat.HEIC => HAL_DATASPACE_HEIF 1417 * <li>others => HAL_DATASPACE_UNKNOWN 1418 * </ul> 1419 * </p> 1420 * 1421 * <p>Passing in an implementation-defined format here will fail (it's not a public format); 1422 * as will passing in an internal format which has a different public format equivalent. 1423 * See {@link #checkArgumentFormat} for more details about a legal public format.</p> 1424 * 1425 * <p>All other formats are returned as-is, no invalid check is performed.</p> 1426 * 1427 * <p>This function is the dual of {@link #imageFormatToPublic}.</p> 1428 * 1429 * @param format public image format from {@link ImageFormat} or {@link PixelFormat} 1430 * @return the converted image formats 1431 * 1432 * @see ImageFormat 1433 * @see PixelFormat 1434 * 1435 * @throws IllegalArgumentException 1436 * if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} 1437 */ imageFormatToDataspace(int format)1438 static int imageFormatToDataspace(int format) { 1439 switch (format) { 1440 case ImageFormat.JPEG: 1441 return HAL_DATASPACE_V0_JFIF; 1442 case ImageFormat.DEPTH_POINT_CLOUD: 1443 case ImageFormat.DEPTH16: 1444 case ImageFormat.RAW_DEPTH: 1445 case ImageFormat.RAW_DEPTH10: 1446 return HAL_DATASPACE_DEPTH; 1447 case ImageFormat.DEPTH_JPEG: 1448 return HAL_DATASPACE_DYNAMIC_DEPTH; 1449 case ImageFormat.HEIC: 1450 return HAL_DATASPACE_HEIF; 1451 default: 1452 return HAL_DATASPACE_UNKNOWN; 1453 } 1454 } 1455 1456 /** 1457 * Convert image formats from public to internal formats (in-place). 1458 * 1459 * @param formats an array of image formats 1460 * @return {@code formats} 1461 * 1462 * @see #imageFormatToInternal 1463 * 1464 * @hide 1465 */ imageFormatToInternal(int[] formats)1466 public static int[] imageFormatToInternal(int[] formats) { 1467 if (formats == null) { 1468 return null; 1469 } 1470 1471 for (int i = 0; i < formats.length; ++i) { 1472 formats[i] = imageFormatToInternal(formats[i]); 1473 } 1474 1475 return formats; 1476 } 1477 getPublicFormatSizes(int format, boolean output, boolean highRes)1478 private Size[] getPublicFormatSizes(int format, boolean output, boolean highRes) { 1479 try { 1480 checkArgumentFormatSupported(format, output); 1481 } catch (IllegalArgumentException e) { 1482 return null; 1483 } 1484 1485 int internalFormat = imageFormatToInternal(format); 1486 int dataspace = imageFormatToDataspace(format); 1487 1488 return getInternalFormatSizes(internalFormat, dataspace, output, highRes); 1489 } 1490 getInternalFormatSizes(int format, int dataspace, boolean output, boolean highRes)1491 private Size[] getInternalFormatSizes(int format, int dataspace, 1492 boolean output, boolean highRes) { 1493 // All depth formats are non-high-res. 1494 if (dataspace == HAL_DATASPACE_DEPTH && highRes) { 1495 return new Size[0]; 1496 } 1497 1498 SparseIntArray formatsMap = 1499 !output ? mInputFormats : 1500 dataspace == HAL_DATASPACE_DEPTH ? mDepthOutputFormats : 1501 dataspace == HAL_DATASPACE_DYNAMIC_DEPTH ? mDynamicDepthOutputFormats : 1502 dataspace == HAL_DATASPACE_HEIF ? mHeicOutputFormats : 1503 highRes ? mHighResOutputFormats : 1504 mOutputFormats; 1505 1506 int sizesCount = formatsMap.get(format); 1507 if ( ((!output || (dataspace == HAL_DATASPACE_DEPTH || 1508 dataspace == HAL_DATASPACE_DYNAMIC_DEPTH || 1509 dataspace == HAL_DATASPACE_HEIF)) && sizesCount == 0) || 1510 (output && (dataspace != HAL_DATASPACE_DEPTH && 1511 dataspace != HAL_DATASPACE_DYNAMIC_DEPTH && 1512 dataspace != HAL_DATASPACE_HEIF) && 1513 mAllOutputFormats.get(format) == 0)) { 1514 return null; 1515 } 1516 1517 Size[] sizes = new Size[sizesCount]; 1518 int sizeIndex = 0; 1519 1520 StreamConfiguration[] configurations = 1521 (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : 1522 (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) ? mDynamicDepthConfigurations : 1523 (dataspace == HAL_DATASPACE_HEIF) ? mHeicConfigurations : 1524 mConfigurations; 1525 StreamConfigurationDuration[] minFrameDurations = 1526 (dataspace == HAL_DATASPACE_DEPTH) ? mDepthMinFrameDurations : 1527 (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) ? mDynamicDepthMinFrameDurations : 1528 (dataspace == HAL_DATASPACE_HEIF) ? mHeicMinFrameDurations : 1529 mMinFrameDurations; 1530 1531 for (StreamConfiguration config : configurations) { 1532 int fmt = config.getFormat(); 1533 if (fmt == format && config.isOutput() == output) { 1534 if (output && mListHighResolution) { 1535 // Filter slow high-res output formats; include for 1536 // highRes, remove for !highRes 1537 long duration = 0; 1538 for (int i = 0; i < minFrameDurations.length; i++) { 1539 StreamConfigurationDuration d = minFrameDurations[i]; 1540 if (d.getFormat() == fmt && 1541 d.getWidth() == config.getSize().getWidth() && 1542 d.getHeight() == config.getSize().getHeight()) { 1543 duration = d.getDuration(); 1544 break; 1545 } 1546 } 1547 if (dataspace != HAL_DATASPACE_DEPTH && 1548 highRes != (duration > DURATION_20FPS_NS)) { 1549 continue; 1550 } 1551 } 1552 sizes[sizeIndex++] = config.getSize(); 1553 } 1554 } 1555 1556 // Dynamic depth streams can have both fast and also high res modes. 1557 if ((sizeIndex != sizesCount) && (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH || 1558 dataspace == HAL_DATASPACE_HEIF)) { 1559 1560 if (sizeIndex > sizesCount) { 1561 throw new AssertionError( 1562 "Too many dynamic depth sizes (expected " + sizesCount + ", actual " + 1563 sizeIndex + ")"); 1564 } 1565 1566 if (sizeIndex <= 0) { 1567 sizes = new Size[0]; 1568 } else { 1569 sizes = Arrays.copyOf(sizes, sizeIndex); 1570 } 1571 } else if (sizeIndex != sizesCount) { 1572 throw new AssertionError( 1573 "Too few sizes (expected " + sizesCount + ", actual " + sizeIndex + ")"); 1574 } 1575 1576 return sizes; 1577 } 1578 1579 /** Get the list of publicly visible output formats */ getPublicFormats(boolean output)1580 private int[] getPublicFormats(boolean output) { 1581 int[] formats = new int[getPublicFormatCount(output)]; 1582 1583 int i = 0; 1584 1585 SparseIntArray map = getFormatsMap(output); 1586 for (int j = 0; j < map.size(); j++) { 1587 int format = map.keyAt(j); 1588 formats[i++] = imageFormatToPublic(format); 1589 } 1590 if (output) { 1591 for (int j = 0; j < mDepthOutputFormats.size(); j++) { 1592 formats[i++] = depthFormatToPublic(mDepthOutputFormats.keyAt(j)); 1593 } 1594 if (mDynamicDepthOutputFormats.size() > 0) { 1595 // Only one publicly dynamic depth format is available. 1596 formats[i++] = ImageFormat.DEPTH_JPEG; 1597 } 1598 if (mHeicOutputFormats.size() > 0) { 1599 formats[i++] = ImageFormat.HEIC; 1600 } 1601 } 1602 if (formats.length != i) { 1603 throw new AssertionError("Too few formats " + i + ", expected " + formats.length); 1604 } 1605 1606 return formats; 1607 } 1608 1609 /** Get the format -> size count map for either output or input formats */ getFormatsMap(boolean output)1610 private SparseIntArray getFormatsMap(boolean output) { 1611 return output ? mAllOutputFormats : mInputFormats; 1612 } 1613 getInternalFormatDuration(int format, int dataspace, Size size, int duration)1614 private long getInternalFormatDuration(int format, int dataspace, Size size, int duration) { 1615 // assume format is already checked, since its internal 1616 1617 if (!isSupportedInternalConfiguration(format, dataspace, size)) { 1618 throw new IllegalArgumentException("size was not supported"); 1619 } 1620 1621 StreamConfigurationDuration[] durations = getDurations(duration, dataspace); 1622 1623 for (StreamConfigurationDuration configurationDuration : durations) { 1624 if (configurationDuration.getFormat() == format && 1625 configurationDuration.getWidth() == size.getWidth() && 1626 configurationDuration.getHeight() == size.getHeight()) { 1627 return configurationDuration.getDuration(); 1628 } 1629 } 1630 // Default duration is '0' (unsupported/no extra stall) 1631 return 0; 1632 } 1633 1634 /** 1635 * Get the durations array for the kind of duration 1636 * 1637 * @see #DURATION_MIN_FRAME 1638 * @see #DURATION_STALL 1639 * */ getDurations(int duration, int dataspace)1640 private StreamConfigurationDuration[] getDurations(int duration, int dataspace) { 1641 switch (duration) { 1642 case DURATION_MIN_FRAME: 1643 return (dataspace == HAL_DATASPACE_DEPTH) ? mDepthMinFrameDurations : 1644 (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) ? 1645 mDynamicDepthMinFrameDurations : 1646 (dataspace == HAL_DATASPACE_HEIF) ? mHeicMinFrameDurations : 1647 mMinFrameDurations; 1648 1649 case DURATION_STALL: 1650 return (dataspace == HAL_DATASPACE_DEPTH) ? mDepthStallDurations : 1651 (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) ? mDynamicDepthStallDurations : 1652 (dataspace == HAL_DATASPACE_HEIF) ? mHeicStallDurations : 1653 mStallDurations; 1654 default: 1655 throw new IllegalArgumentException("duration was invalid"); 1656 } 1657 } 1658 1659 /** Count the number of publicly-visible output formats */ getPublicFormatCount(boolean output)1660 private int getPublicFormatCount(boolean output) { 1661 SparseIntArray formatsMap = getFormatsMap(output); 1662 int size = formatsMap.size(); 1663 if (output) { 1664 size += mDepthOutputFormats.size(); 1665 size += mDynamicDepthOutputFormats.size(); 1666 size += mHeicOutputFormats.size(); 1667 } 1668 1669 return size; 1670 } 1671 arrayContains(T[] array, T element)1672 private static <T> boolean arrayContains(T[] array, T element) { 1673 if (array == null) { 1674 return false; 1675 } 1676 1677 for (T el : array) { 1678 if (Objects.equals(el, element)) { 1679 return true; 1680 } 1681 } 1682 1683 return false; 1684 } 1685 isSupportedInternalConfiguration(int format, int dataspace, Size size)1686 private boolean isSupportedInternalConfiguration(int format, int dataspace, Size size) { 1687 StreamConfiguration[] configurations = 1688 (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : 1689 (dataspace == HAL_DATASPACE_DYNAMIC_DEPTH) ? mDynamicDepthConfigurations : 1690 (dataspace == HAL_DATASPACE_HEIF) ? mHeicConfigurations : 1691 mConfigurations; 1692 1693 for (int i = 0; i < configurations.length; i++) { 1694 if (configurations[i].getFormat() == format && 1695 configurations[i].getSize().equals(size)) { 1696 return true; 1697 } 1698 } 1699 1700 return false; 1701 } 1702 1703 /** 1704 * Return this {@link StreamConfigurationMap} as a string representation. 1705 * 1706 * <p>{@code "StreamConfigurationMap(Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d, 1707 * stall:%d], ... [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]), Inputs([w:%d, h:%d, 1708 * format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)]), ValidOutputFormatsForInput( 1709 * [in:%d, out:%d, ... %d], ... [in:%d, out:%d, ... %d]), HighSpeedVideoConfigurations( 1710 * [w:%d, h:%d, min_fps:%d, max_fps:%d], ... [w:%d, h:%d, min_fps:%d, max_fps:%d]))"}.</p> 1711 * 1712 * <p>{@code Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d], ... 1713 * [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d])}, where 1714 * {@code [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]} represents an output 1715 * configuration's width, height, format, minimal frame duration in nanoseconds, and stall 1716 * duration in nanoseconds.</p> 1717 * 1718 * <p>{@code Inputs([w:%d, h:%d, format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)])}, where 1719 * {@code [w:%d, h:%d, format:%s(%d)]} represents an input configuration's width, height, and 1720 * format.</p> 1721 * 1722 * <p>{@code ValidOutputFormatsForInput([in:%s(%d), out:%s(%d), ... %s(%d)], 1723 * ... [in:%s(%d), out:%s(%d), ... %s(%d)])}, where {@code [in:%s(%d), out:%s(%d), ... %s(%d)]} 1724 * represents an input fomat and its valid output formats.</p> 1725 * 1726 * <p>{@code HighSpeedVideoConfigurations([w:%d, h:%d, min_fps:%d, max_fps:%d], 1727 * ... [w:%d, h:%d, min_fps:%d, max_fps:%d])}, where 1728 * {@code [w:%d, h:%d, min_fps:%d, max_fps:%d]} represents a high speed video output 1729 * configuration's width, height, minimal frame rate, and maximal frame rate.</p> 1730 * 1731 * @return string representation of {@link StreamConfigurationMap} 1732 */ 1733 @Override toString()1734 public String toString() { 1735 StringBuilder sb = new StringBuilder("StreamConfiguration("); 1736 appendOutputsString(sb); 1737 sb.append(", "); 1738 appendHighResOutputsString(sb); 1739 sb.append(", "); 1740 appendInputsString(sb); 1741 sb.append(", "); 1742 appendValidOutputFormatsForInputString(sb); 1743 sb.append(", "); 1744 appendHighSpeedVideoConfigurationsString(sb); 1745 sb.append(")"); 1746 1747 return sb.toString(); 1748 } 1749 1750 /** 1751 * Size comparison method used by size comparators. 1752 * 1753 * @hide 1754 */ compareSizes(int widthA, int heightA, int widthB, int heightB)1755 public static int compareSizes(int widthA, int heightA, int widthB, int heightB) { 1756 long left = widthA * (long) heightA; 1757 long right = widthB * (long) heightB; 1758 if (left == right) { 1759 left = widthA; 1760 right = widthB; 1761 } 1762 return (left < right) ? -1 : (left > right ? 1 : 0); 1763 } 1764 appendOutputsString(StringBuilder sb)1765 private void appendOutputsString(StringBuilder sb) { 1766 sb.append("Outputs("); 1767 int[] formats = getOutputFormats(); 1768 for (int format : formats) { 1769 Size[] sizes = getOutputSizes(format); 1770 for (Size size : sizes) { 1771 long minFrameDuration = getOutputMinFrameDuration(format, size); 1772 long stallDuration = getOutputStallDuration(format, size); 1773 sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " + 1774 "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format), 1775 format, minFrameDuration, stallDuration)); 1776 } 1777 } 1778 // Remove the pending ", " 1779 if (sb.charAt(sb.length() - 1) == ' ') { 1780 sb.delete(sb.length() - 2, sb.length()); 1781 } 1782 sb.append(")"); 1783 } 1784 appendHighResOutputsString(StringBuilder sb)1785 private void appendHighResOutputsString(StringBuilder sb) { 1786 sb.append("HighResolutionOutputs("); 1787 int[] formats = getOutputFormats(); 1788 for (int format : formats) { 1789 Size[] sizes = getHighResolutionOutputSizes(format); 1790 if (sizes == null) continue; 1791 for (Size size : sizes) { 1792 long minFrameDuration = getOutputMinFrameDuration(format, size); 1793 long stallDuration = getOutputStallDuration(format, size); 1794 sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " + 1795 "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format), 1796 format, minFrameDuration, stallDuration)); 1797 } 1798 } 1799 // Remove the pending ", " 1800 if (sb.charAt(sb.length() - 1) == ' ') { 1801 sb.delete(sb.length() - 2, sb.length()); 1802 } 1803 sb.append(")"); 1804 } 1805 appendInputsString(StringBuilder sb)1806 private void appendInputsString(StringBuilder sb) { 1807 sb.append("Inputs("); 1808 int[] formats = getInputFormats(); 1809 for (int format : formats) { 1810 Size[] sizes = getInputSizes(format); 1811 for (Size size : sizes) { 1812 sb.append(String.format("[w:%d, h:%d, format:%s(%d)], ", size.getWidth(), 1813 size.getHeight(), formatToString(format), format)); 1814 } 1815 } 1816 // Remove the pending ", " 1817 if (sb.charAt(sb.length() - 1) == ' ') { 1818 sb.delete(sb.length() - 2, sb.length()); 1819 } 1820 sb.append(")"); 1821 } 1822 appendValidOutputFormatsForInputString(StringBuilder sb)1823 private void appendValidOutputFormatsForInputString(StringBuilder sb) { 1824 sb.append("ValidOutputFormatsForInput("); 1825 int[] inputFormats = getInputFormats(); 1826 for (int inputFormat : inputFormats) { 1827 sb.append(String.format("[in:%s(%d), out:", formatToString(inputFormat), inputFormat)); 1828 int[] outputFormats = getValidOutputFormatsForInput(inputFormat); 1829 for (int i = 0; i < outputFormats.length; i++) { 1830 sb.append(String.format("%s(%d)", formatToString(outputFormats[i]), 1831 outputFormats[i])); 1832 if (i < outputFormats.length - 1) { 1833 sb.append(", "); 1834 } 1835 } 1836 sb.append("], "); 1837 } 1838 // Remove the pending ", " 1839 if (sb.charAt(sb.length() - 1) == ' ') { 1840 sb.delete(sb.length() - 2, sb.length()); 1841 } 1842 sb.append(")"); 1843 } 1844 appendHighSpeedVideoConfigurationsString(StringBuilder sb)1845 private void appendHighSpeedVideoConfigurationsString(StringBuilder sb) { 1846 sb.append("HighSpeedVideoConfigurations("); 1847 Size[] sizes = getHighSpeedVideoSizes(); 1848 for (Size size : sizes) { 1849 Range<Integer>[] ranges = getHighSpeedVideoFpsRangesFor(size); 1850 for (Range<Integer> range : ranges) { 1851 sb.append(String.format("[w:%d, h:%d, min_fps:%d, max_fps:%d], ", size.getWidth(), 1852 size.getHeight(), range.getLower(), range.getUpper())); 1853 } 1854 } 1855 // Remove the pending ", " 1856 if (sb.charAt(sb.length() - 1) == ' ') { 1857 sb.delete(sb.length() - 2, sb.length()); 1858 } 1859 sb.append(")"); 1860 } 1861 1862 /** 1863 * @hide 1864 */ formatToString(int format)1865 public static String formatToString(int format) { 1866 switch (format) { 1867 case ImageFormat.YV12: 1868 return "YV12"; 1869 case ImageFormat.YUV_420_888: 1870 return "YUV_420_888"; 1871 case ImageFormat.NV21: 1872 return "NV21"; 1873 case ImageFormat.NV16: 1874 return "NV16"; 1875 case PixelFormat.RGB_565: 1876 return "RGB_565"; 1877 case PixelFormat.RGBA_8888: 1878 return "RGBA_8888"; 1879 case PixelFormat.RGBX_8888: 1880 return "RGBX_8888"; 1881 case PixelFormat.RGB_888: 1882 return "RGB_888"; 1883 case ImageFormat.JPEG: 1884 return "JPEG"; 1885 case ImageFormat.YUY2: 1886 return "YUY2"; 1887 case ImageFormat.Y8: 1888 return "Y8"; 1889 case ImageFormat.Y16: 1890 return "Y16"; 1891 case ImageFormat.RAW_SENSOR: 1892 return "RAW_SENSOR"; 1893 case ImageFormat.RAW_PRIVATE: 1894 return "RAW_PRIVATE"; 1895 case ImageFormat.RAW10: 1896 return "RAW10"; 1897 case ImageFormat.DEPTH16: 1898 return "DEPTH16"; 1899 case ImageFormat.DEPTH_POINT_CLOUD: 1900 return "DEPTH_POINT_CLOUD"; 1901 case ImageFormat.DEPTH_JPEG: 1902 return "DEPTH_JPEG"; 1903 case ImageFormat.RAW_DEPTH: 1904 return "RAW_DEPTH"; 1905 case ImageFormat.RAW_DEPTH10: 1906 return "RAW_DEPTH10"; 1907 case ImageFormat.PRIVATE: 1908 return "PRIVATE"; 1909 case ImageFormat.HEIC: 1910 return "HEIC"; 1911 default: 1912 return "UNKNOWN"; 1913 } 1914 } 1915 1916 // from system/core/include/system/graphics.h 1917 private static final int HAL_PIXEL_FORMAT_RAW16 = 0x20; 1918 /** @hide */ 1919 public static final int HAL_PIXEL_FORMAT_BLOB = 0x21; 1920 private static final int HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22; 1921 private static final int HAL_PIXEL_FORMAT_YCbCr_420_888 = 0x23; 1922 private static final int HAL_PIXEL_FORMAT_RAW_OPAQUE = 0x24; 1923 private static final int HAL_PIXEL_FORMAT_RAW10 = 0x25; 1924 private static final int HAL_PIXEL_FORMAT_RAW12 = 0x26; 1925 private static final int HAL_PIXEL_FORMAT_Y16 = 0x20363159; 1926 1927 1928 private static final int HAL_DATASPACE_STANDARD_SHIFT = 16; 1929 private static final int HAL_DATASPACE_TRANSFER_SHIFT = 22; 1930 private static final int HAL_DATASPACE_RANGE_SHIFT = 27; 1931 1932 private static final int HAL_DATASPACE_UNKNOWN = 0x0; 1933 /** @hide */ 1934 public static final int HAL_DATASPACE_V0_JFIF = 1935 (2 << HAL_DATASPACE_STANDARD_SHIFT) | 1936 (3 << HAL_DATASPACE_TRANSFER_SHIFT) | 1937 (1 << HAL_DATASPACE_RANGE_SHIFT); 1938 1939 /** 1940 * @hide 1941 */ 1942 public static final int HAL_DATASPACE_DEPTH = 0x1000; 1943 /** 1944 * @hide 1945 */ 1946 public static final int HAL_DATASPACE_DYNAMIC_DEPTH = 0x1002; 1947 /** 1948 * @hide 1949 */ 1950 public static final int HAL_DATASPACE_HEIF = 0x1003; 1951 private static final long DURATION_20FPS_NS = 50000000L; 1952 /** 1953 * @see #getDurations(int, int) 1954 */ 1955 private static final int DURATION_MIN_FRAME = 0; 1956 private static final int DURATION_STALL = 1; 1957 1958 private final StreamConfiguration[] mConfigurations; 1959 private final StreamConfigurationDuration[] mMinFrameDurations; 1960 private final StreamConfigurationDuration[] mStallDurations; 1961 1962 private final StreamConfiguration[] mDepthConfigurations; 1963 private final StreamConfigurationDuration[] mDepthMinFrameDurations; 1964 private final StreamConfigurationDuration[] mDepthStallDurations; 1965 1966 private final StreamConfiguration[] mDynamicDepthConfigurations; 1967 private final StreamConfigurationDuration[] mDynamicDepthMinFrameDurations; 1968 private final StreamConfigurationDuration[] mDynamicDepthStallDurations; 1969 1970 private final StreamConfiguration[] mHeicConfigurations; 1971 private final StreamConfigurationDuration[] mHeicMinFrameDurations; 1972 private final StreamConfigurationDuration[] mHeicStallDurations; 1973 1974 private final HighSpeedVideoConfiguration[] mHighSpeedVideoConfigurations; 1975 private final ReprocessFormatsMap mInputOutputFormatsMap; 1976 1977 private final boolean mListHighResolution; 1978 1979 /** internal format -> num output sizes mapping, not including slow high-res sizes, for 1980 * non-depth dataspaces */ 1981 private final SparseIntArray mOutputFormats = new SparseIntArray(); 1982 /** internal format -> num output sizes mapping for slow high-res sizes, for non-depth 1983 * dataspaces */ 1984 private final SparseIntArray mHighResOutputFormats = new SparseIntArray(); 1985 /** internal format -> num output sizes mapping for all non-depth dataspaces */ 1986 private final SparseIntArray mAllOutputFormats = new SparseIntArray(); 1987 /** internal format -> num input sizes mapping, for input reprocessing formats */ 1988 private final SparseIntArray mInputFormats = new SparseIntArray(); 1989 /** internal format -> num depth output sizes mapping, for HAL_DATASPACE_DEPTH */ 1990 private final SparseIntArray mDepthOutputFormats = new SparseIntArray(); 1991 /** internal format -> num dynamic depth output sizes mapping, for HAL_DATASPACE_DYNAMIC_DEPTH */ 1992 private final SparseIntArray mDynamicDepthOutputFormats = new SparseIntArray(); 1993 /** internal format -> num heic output sizes mapping, for HAL_DATASPACE_HEIF */ 1994 private final SparseIntArray mHeicOutputFormats = new SparseIntArray(); 1995 1996 /** High speed video Size -> FPS range count mapping*/ 1997 private final HashMap</*HighSpeedVideoSize*/Size, /*Count*/Integer> mHighSpeedVideoSizeMap = 1998 new HashMap<Size, Integer>(); 1999 /** High speed video FPS range -> Size count mapping*/ 2000 private final HashMap</*HighSpeedVideoFpsRange*/Range<Integer>, /*Count*/Integer> 2001 mHighSpeedVideoFpsRangeMap = new HashMap<Range<Integer>, Integer>(); 2002 2003 } 2004