1 page.title=HAL subsystem 2 @jd:body 3 4 <!-- 5 Copyright 2013 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 --> 19 <div id="qv-wrapper"> 20 <div id="qv"> 21 <h2>In this document</h2> 22 <ol id="auto-toc"> 23 </ol> 24 </div> 25 </div> 26 27 <h2 id="requests">Requests</h2> 28 <p> The app framework issues requests for captured results to the camera subsystem. 29 One request corresponds to one set of results. A request encapsulates all 30 configuration information about the capturing and processing of those results. 31 This includes things such as resolution and pixel format; manual sensor, lens, 32 and flash control; 3A operating modes; RAW to YUV processing control; and 33 statistics generation. This allows for much more control over the results' 34 output and processing. Multiple requests can be in flight at once, and 35 submitting requests is non-blocking. And the requests are always processed in 36 the order they are received.<br/> 37 <img src="images/camera_model.png" alt="Camera request model"/> 38 <br/> 39 <strong>Figure 3.</strong> Camera model</p> 40 <h2 id="hal-subsystem">The HAL and camera subsystem</h2> 41 <p> The camera subsystem includes the implementations for components in the camera 42 pipeline such as the 3A algorithm and processing controls. The camera HAL 43 provides interfaces for you to implement your versions of these components. To 44 maintain cross-platform compatibility between multiple device manufacturers and 45 Image Signal Processor (ISP, or camera sensor) vendors, the camera pipeline 46 model is virtual and does not directly correspond to any real ISP. However, it 47 is similar enough to real processing pipelines so that you can map it to your 48 hardware efficiently. In addition, it is abstract enough to allow for multiple 49 different algorithms and orders of operation without compromising either 50 quality, efficiency, or cross-device compatibility.<br/> 51 The camera pipeline also supports triggers that the app framework can initiate 52 to turn on things such as auto-focus. It also sends notifications back to the 53 app framework, notifying apps of events such as an auto-focus lock or errors.<br/> 54 <img src="images/camera_hal.png" alt="Camera hardware abstraction layer"/> 55 <br/> 56 <strong>Figure 4.</strong> Camera pipeline<br/> 57 Please note, some image processing blocks shown in the diagram above are not 58 well-defined in the initial release.<br/> 59 The camera pipeline makes the following assumptions:</p> 60 <ul> 61 <li>RAW Bayer output undergoes no processing inside the ISP.</li> 62 <li>Statistics are generated based off the raw sensor data.</li> 63 <li>The various processing blocks that convert raw sensor data to YUV are in an 64 arbitrary order.</li> 65 <li>While multiple scale and crop units are shown, all scaler units share the 66 output region controls (digital zoom). However, each unit may have a different 67 output resolution and pixel format.</li> 68 </ul> 69 <p><strong>Summary of API use</strong><br/> 70 This is a brief summary of the steps for using the Android camera API. See the 71 Startup and expected operation sequence section for a detailed breakdown of 72 these steps, including API calls.</p> 73 <ol> 74 <li>Listen for and enumerate camera devices.</li> 75 <li>Open device and connect listeners.</li> 76 <li>Configure outputs for target use case (such as still capture, recording, 77 etc.).</li> 78 <li>Create request(s) for target use case.</li> 79 <li>Capture/repeat requests and bursts.</li> 80 <li>Receive result metadata and image data.</li> 81 <li>When switching use cases, return to step 3.</li> 82 </ol> 83 <p><strong>HAL operation summary</strong></p> 84 <ul> 85 <li>Asynchronous requests for captures come from the framework.</li> 86 <li>HAL device must process requests in order. And for each request, produce 87 output result metadata, and one or more output image buffers.</li> 88 <li>First-in, first-out for requests and results, and for streams referenced by 89 subsequent requests. </li> 90 <li>Timestamps must be identical for all outputs from a given request, so that the 91 framework can match them together if needed. </li> 92 <li>All capture configuration and state (except for the 3A routines) is 93 encapsulated in the requests and results.</li> 94 </ul> 95 <p><img src="images/camera-hal-overview.png" alt="Camera HAL overview"/> 96 <br/> 97 <strong>Figure 5.</strong> Camera HAL overview</p> 98 <h2 id="startup">Startup and expected operation sequence</h2> 99 <p>This section contains a detailed explanation of the steps expected when using 100 the camera API. Please see <a href="https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/camera3.h">platform/hardware/libhardware/include/hardware/camera3.h</a> for definitions of these structures and methods.</p> 101 <ol> 102 <li>Framework calls camera_module_t->common.open(), which returns a 103 hardware_device_t structure.</li> 104 <li>Framework inspects the hardware_device_t->version field, and instantiates the 105 appropriate handler for that version of the camera hardware device. In case 106 the version is CAMERA_DEVICE_API_VERSION_3_0, the device is cast to a 107 camera3_device_t.</li> 108 <li>Framework calls camera3_device_t->ops->initialize() with the framework 109 callback function pointers. This will only be called this one time after 110 open(), before any other functions in the ops structure are called.</li> 111 <li>The framework calls camera3_device_t->ops->configure_streams() with a list of 112 input/output streams to the HAL device.</li> 113 <li>The framework allocates gralloc buffers and calls 114 camera3_device_t->ops->register_stream_buffers() for at least one of the 115 output streams listed in configure_streams. The same stream is registered 116 only once.</li> 117 <li>The framework requests default settings for some number of use cases with 118 calls to camera3_device_t->ops->construct_default_request_settings(). This 119 may occur any time after step 3.</li> 120 <li>The framework constructs and sends the first capture request to the HAL with 121 settings based on one of the sets of default settings, and with at least one 122 output stream that has been registered earlier by the framework. This is sent 123 to the HAL with camera3_device_t->ops->process_capture_request(). The HAL 124 must block the return of this call until it is ready for the next request to 125 be sent.</li> 126 <li>The framework continues to submit requests, and possibly call 127 register_stream_buffers() for not-yet-registered streams, and call 128 construct_default_request_settings to get default settings buffers for other 129 use cases.</li> 130 <li>When the capture of a request begins (sensor starts exposing for the 131 capture), the HAL calls camera3_callback_ops_t->notify() with the SHUTTER 132 event, including the frame number and the timestamp for start of exposure. 133 This notify call must be made before the first call to 134 process_capture_result() for that frame number.</li> 135 <li>After some pipeline delay, the HAL begins to return completed captures to 136 the framework with camera3_callback_ops_t->process_capture_result(). These 137 are returned in the same order as the requests were submitted. Multiple 138 requests can be in flight at once, depending on the pipeline depth of the 139 camera HAL device.</li> 140 <li>After some time, the framework may stop submitting new requests, wait for 141 the existing captures to complete (all buffers filled, all results 142 returned), and then call configure_streams() again. This resets the camera 143 hardware and pipeline for a new set of input/output streams. Some streams 144 may be reused from the previous configuration; if these streams' buffers had 145 already been registered with the HAL, they will not be registered again. The 146 framework then continues from step 7, if at least one registered output 147 stream remains. (Otherwise, step 5 is required first.)</li> 148 <li>Alternatively, the framework may call camera3_device_t->common->close() to 149 end the camera session. This may be called at any time when no other calls 150 from the framework are active, although the call may block until all 151 in-flight captures have completed (all results returned, all buffers 152 filled). After the close call returns, no more calls to the 153 camera3_callback_ops_t functions are allowed from the HAL. Once the close() 154 call is underway, the framework may not call any other HAL device functions.</li> 155 <li>In case of an error or other asynchronous event, the HAL must call 156 camera3_callback_ops_t->notify() with the appropriate error/event message. 157 After returning from a fatal device-wide error notification, the HAL should 158 act as if close() had been called on it. However, the HAL must either cancel 159 or complete all outstanding captures before calling notify(), so that once 160 notify() is called with a fatal error, the framework will not receive 161 further callbacks from the device. Methods besides close() should return 162 -ENODEV or NULL after the notify() method returns from a fatal error 163 message.</li> 164 </ol> 165 <p><img src="images/camera-ops-flow.png" width="600" height="434" alt="Camera operations flow" /> 166 </p> 167 <p><strong>Figure 6.</strong> Camera operational flow</p> 168 <h2 id="ops-modes">Operational modes</h2> 169 <p>The camera 3 HAL device can implement one of two possible operational modes: 170 limited and full. Full support is expected from new higher-end devices. Limited 171 mode has hardware requirements roughly in line with those for a camera HAL 172 device v1 implementation, and is expected from older or inexpensive devices. 173 Full is a strict superset of limited, and they share the same essential 174 operational flow, as documented above.</p> 175 <p>The HAL must indicate its level of support with the 176 android.info.supportedHardwareLevel static metadata entry, with 0 indicating 177 limited mode, and 1 indicating full mode support.</p> 178 <p>Roughly speaking, limited-mode devices do not allow for application control of 179 capture settings (3A control only), high-rate capture of high-resolution images, 180 raw sensor readout, or support for YUV output streams above maximum recording 181 resolution (JPEG only for large images).<br/> 182 Here are the details of limited-mode behavior:</p> 183 <ul> 184 <li>Limited-mode devices do not need to implement accurate synchronization between 185 capture request settings and the actual image data captured. Instead, changes 186 to settings may take effect some time in the future, and possibly not for the 187 same output frame for each settings entry. Rapid changes in settings may 188 result in some settings never being used for a capture. However, captures that 189 include high-resolution output buffers ( > 1080p ) have to use the settings as 190 specified (but see below for processing rate).</li> 191 <li>Captures in limited mode that include high-resolution (> 1080p) output buffers 192 may block in process_capture_request() until all the output buffers have been 193 filled. A full-mode HAL device must process sequences of high-resolution 194 requests at the rate indicated in the static metadata for that pixel format. 195 The HAL must still call process_capture_result() to provide the output; the 196 framework must simply be prepared for process_capture_request() to block until 197 after process_capture_result() for that request completes for high-resolution 198 captures for limited-mode devices.</li> 199 <li>Limited-mode devices do not need to support most of the settings/result/static 200 info metadata. Only the following settings are expected to be consumed or 201 produced by a limited-mode HAL device: 202 <ul> 203 <li>android.control.aeAntibandingMode (controls)</li> 204 <li>android.control.aeExposureCompensation (controls)</li> 205 <li>android.control.aeLock (controls)</li> 206 <li>android.control.aeMode (controls)</li> 207 <li>[OFF means ON_FLASH_TORCH]</li> 208 <li>android.control.aeRegions (controls)</li> 209 <li>android.control.aeTargetFpsRange (controls)</li> 210 <li>android.control.afMode (controls)</li> 211 <li>[OFF means infinity focus]</li> 212 <li>android.control.afRegions (controls)</li> 213 <li>android.control.awbLock (controls)</li> 214 <li>android.control.awbMode (controls)</li> 215 <li>[OFF not supported]</li> 216 <li>android.control.awbRegions (controls)</li> 217 <li>android.control.captureIntent (controls)</li> 218 <li>android.control.effectMode (controls)</li> 219 <li>android.control.mode (controls)</li> 220 <li>[OFF not supported]</li> 221 <li>android.control.sceneMode (controls)</li> 222 <li>android.control.videoStabilizationMode (controls)</li> 223 <li>android.control.aeAvailableAntibandingModes (static)</li> 224 <li>android.control.aeAvailableModes (static)</li> 225 <li>android.control.aeAvailableTargetFpsRanges (static)</li> 226 <li>android.control.aeCompensationRange (static)</li> 227 <li>android.control.aeCompensationStep (static)</li> 228 <li>android.control.afAvailableModes (static)</li> 229 <li>android.control.availableEffects (static)</li> 230 <li>android.control.availableSceneModes (static)</li> 231 <li>android.control.availableVideoStabilizationModes (static)</li> 232 <li>android.control.awbAvailableModes (static)</li> 233 <li>android.control.maxRegions (static)</li> 234 <li>android.control.sceneModeOverrides (static)</li> 235 <li>android.control.aeRegions (dynamic)</li> 236 <li>android.control.aeState (dynamic)</li> 237 <li>android.control.afMode (dynamic)</li> 238 <li>android.control.afRegions (dynamic)</li> 239 <li>android.control.afState (dynamic)</li> 240 <li>android.control.awbMode (dynamic)</li> 241 <li>android.control.awbRegions (dynamic)</li> 242 <li>android.control.awbState (dynamic)</li> 243 <li>android.control.mode (dynamic)</li> 244 <li>android.flash.info.available (static)</li> 245 <li>android.info.supportedHardwareLevel (static)</li> 246 <li>android.jpeg.gpsCoordinates (controls)</li> 247 <li>android.jpeg.gpsProcessingMethod (controls)</li> 248 <li>android.jpeg.gpsTimestamp (controls)</li> 249 <li>android.jpeg.orientation (controls)</li> 250 <li>android.jpeg.quality (controls)</li> 251 <li>android.jpeg.thumbnailQuality (controls)</li> 252 <li>android.jpeg.thumbnailSize (controls)</li> 253 <li>android.jpeg.availableThumbnailSizes (static)</li> 254 <li>android.jpeg.maxSize (static)</li> 255 <li>android.jpeg.gpsCoordinates (dynamic)</li> 256 <li>android.jpeg.gpsProcessingMethod (dynamic)</li> 257 <li>android.jpeg.gpsTimestamp (dynamic)</li> 258 <li>android.jpeg.orientation (dynamic)</li> 259 <li>android.jpeg.quality (dynamic)</li> 260 <li>android.jpeg.size (dynamic)</li> 261 <li>android.jpeg.thumbnailQuality (dynamic)</li> 262 <li>android.jpeg.thumbnailSize (dynamic)</li> 263 <li>android.lens.info.minimumFocusDistance (static)</li> 264 <li>android.request.id (controls)</li> 265 <li>android.request.id (dynamic)</li> 266 <li>android.scaler.cropRegion (controls)</li> 267 <li>[ignores (x,y), assumes center-zoom]</li> 268 <li>android.scaler.availableFormats (static)</li> 269 <li>[RAW not supported]</li> 270 <li>android.scaler.availableJpegMinDurations (static)</li> 271 <li>android.scaler.availableJpegSizes (static)</li> 272 <li>android.scaler.availableMaxDigitalZoom (static)</li> 273 <li>android.scaler.availableProcessedMinDurations (static)</li> 274 <li>android.scaler.availableProcessedSizes (static)</li> 275 <li>[full resolution not supported]</li> 276 <li>android.scaler.maxDigitalZoom (static)</li> 277 <li>android.scaler.cropRegion (dynamic)</li> 278 <li>android.sensor.orientation (static)</li> 279 <li>android.sensor.timestamp (dynamic)</li> 280 <li>android.statistics.faceDetectMode (controls)</li> 281 <li>android.statistics.info.availableFaceDetectModes (static)</li> 282 <li>android.statistics.faceDetectMode (dynamic)</li> 283 <li>android.statistics.faceIds (dynamic)</li> 284 <li>android.statistics.faceLandmarks (dynamic)</li> 285 <li>android.statistics.faceRectangles (dynamic)</li> 286 <li>android.statistics.faceScores (dynamic)</li> 287 </ul> 288 </li> 289 </ul> 290 <h2 id="interaction">Interaction between the application capture request, 3A 291 control, and the processing pipeline</h2> 292 <p>Depending on the settings in the 3A control block, the camera pipeline ignores 293 some of the parameters in the application's capture request and uses the values 294 provided by the 3A control routines instead. For example, when auto-exposure is 295 active, the exposure time, frame duration, and sensitivity parameters of the 296 sensor are controlled by the platform 3A algorithm, and any app-specified values 297 are ignored. The values chosen for the frame by the 3A routines must be reported 298 in the output metadata. The following table describes the different modes of the 299 3A control block and the properties that are controlled by these modes. See 300 the <a href="https://android.googlesource.com/platform/system/media/+/master/camera/docs/docs.html">platform/system/media/camera/docs/docs.html</a> file for definitions of these properties.</p> 301 <table> 302 <tr> 303 <th>Parameter</th> 304 <th>State</th> 305 <th>Properties controlled</th> 306 </tr> 307 <tr> 308 <td>android.control.aeMode</td> 309 <td>OFF</td> 310 <td>None</td> 311 </tr> 312 <tr> 313 <td></td> 314 <td>ON</td> 315 <td>android.sensor.exposureTime 316 android.sensor.frameDuration 317 android.sensor.sensitivity 318 android.lens.aperture (if supported) 319 android.lens.filterDensity (if supported)</td> 320 </tr> 321 <tr> 322 <td></td> 323 <td>ON_AUTO_FLASH</td> 324 <td>Everything is ON, plus android.flash.firingPower, android.flash.firingTime, and android.flash.mode</td> 325 </tr> 326 <tr> 327 <td></td> 328 <td>ON_ALWAYS_FLASH</td> 329 <td>Same as ON_AUTO_FLASH</td> 330 </tr> 331 <tr> 332 <td></td> 333 <td>ON_AUTO_FLASH_RED_EYE</td> 334 <td>Same as ON_AUTO_FLASH</td> 335 </tr> 336 <tr> 337 <td>android.control.awbMode</td> 338 <td>OFF</td> 339 <td>None</td> 340 </tr> 341 <tr> 342 <td></td> 343 <td>WHITE_BALANCE_*</td> 344 <td>android.colorCorrection.transform. Platform-specific adjustments if android.colorCorrection.mode is FAST or HIGH_QUALITY.</td> 345 </tr> 346 <tr> 347 <td>android.control.afMode</td> 348 <td>OFF</td> 349 <td>None</td> 350 </tr> 351 <tr> 352 <td></td> 353 <td>FOCUS_MODE_*</td> 354 <td>android.lens.focusDistance</td> 355 </tr> 356 <tr> 357 <td>android.control.videoStabilization</td> 358 <td>OFF</td> 359 <td>None</td> 360 </tr> 361 <tr> 362 <td></td> 363 <td>ON</td> 364 <td>Can adjust android.scaler.cropRegion to implement video stabilization</td> 365 </tr> 366 <tr> 367 <td>android.control.mode</td> 368 <td>OFF</td> 369 <td>AE, AWB, and AF are disabled</td> 370 </tr> 371 <tr> 372 <td></td> 373 <td>AUTO</td> 374 <td>Individual AE, AWB, and AF settings are used</td> 375 </tr> 376 <tr> 377 <td></td> 378 <td>SCENE_MODE_*</td> 379 <td>Can override all parameters listed above. Individual 3A controls are disabled.</td> 380 </tr> 381 </table> 382 <p>The controls exposed for the 3A algorithm mostly map 1:1 to the old API's 383 parameters (such as exposure compensation, scene mode, or white balance mode).<br/> 384 The controls in the Image Processing block in Figure 2</a> all 385 operate on a similar principle, and generally each block has three modes:</p> 386 <ul> 387 <li>OFF: This processing block is disabled. The demosaic, color correction, and 388 tone curve adjustment blocks cannot be disabled.</li> 389 <li>FAST: In this mode, the processing block may not slow down the output frame 390 rate compared to OFF mode, but should otherwise produce the best-quality 391 output it can given that restriction. Typically, this would be used for 392 preview or video recording modes, or burst capture for still images. On some 393 devices, this may be equivalent to OFF mode (no processing can be done without 394 slowing down the frame rate), and on some devices, this may be equivalent to 395 HIGH_QUALITY mode (best quality still does not slow down frame rate).</li> 396 <li>HIGHQUALITY: In this mode, the processing block should produce the best 397 quality result possible, slowing down the output frame rate as needed. 398 Typically, this would be used for high-quality still capture. Some blocks 399 include a manual control which can be optionally selected instead of FAST or 400 HIGHQUALITY. For example, the color correction block supports a color 401 transform matrix, while the tone curve adjustment supports an arbitrary global 402 tone mapping curve.</li> 403 </ul> 404 <p>The maximum frame rate that can be supported by a camera subsystem is a function 405 of many factors:</p> 406 <ul> 407 <li>Requested resolutions of output image streams</li> 408 <li>Availability of binning / skipping modes on the imager</li> 409 <li>The bandwidth of the imager interface</li> 410 <li>The bandwidth of the various ISP processing blocks</li> 411 </ul> 412 <p>Since these factors can vary greatly between different ISPs and sensors, the 413 camera HAL interface tries to abstract the bandwidth restrictions into as simple 414 model as possible. The model presented has the following characteristics:</p> 415 <ul> 416 <li>The image sensor is always configured to output the smallest resolution 417 possible given the application's requested output stream sizes. The smallest 418 resolution is defined as being at least as large as the largest requested 419 output stream size.</li> 420 <li>Since any request may use any or all the currently configured output streams, 421 the sensor and ISP must be configured to support scaling a single capture to 422 all the streams at the same time. </li> 423 <li>JPEG streams act like processed YUV streams for requests for which they are 424 not included; in requests in which they are directly referenced, they act as 425 JPEG streams.</li> 426 <li>The JPEG processor can run concurrently to the rest of the camera pipeline but 427 cannot process more than one capture at a time.</li> 428 </ul> 429