Home | History | Annotate | Download | only in hardware
      1 /*
      2  * Copyright (C) 2013 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 #ifndef ANDROID_INCLUDE_CAMERA3_H
     18 #define ANDROID_INCLUDE_CAMERA3_H
     19 
     20 #include <system/camera_metadata.h>
     21 #include "camera_common.h"
     22 
     23 /**
     24  * Camera device HAL 3.1 [ CAMERA_DEVICE_API_VERSION_3_1 ]
     25  *
     26  * EXPERIMENTAL.
     27  *
     28  * Supports the android.hardware.Camera API.
     29  *
     30  * Camera devices that support this version of the HAL must return
     31  * CAMERA_DEVICE_API_VERSION_3_1 in camera_device_t.common.version and in
     32  * camera_info_t.device_version (from camera_module_t.get_camera_info).
     33  *
     34  * Camera modules that may contain version 3.1 devices must implement at least
     35  * version 2.0 of the camera module interface (as defined by
     36  * camera_module_t.common.module_api_version).
     37  *
     38  * See camera_common.h for more versioning details.
     39  *
     40  * Documentation index:
     41  *   S1. Version history
     42  *   S2. Startup and operation sequencing
     43  *   S3. Operational modes
     44  *   S4. 3A modes and state machines
     45  *   S5. Cropping
     46  *   S6. Error management
     47  */
     48 
     49 /**
     50  * S1. Version history:
     51  *
     52  * 1.0: Initial Android camera HAL (Android 4.0) [camera.h]:
     53  *
     54  *   - Converted from C++ CameraHardwareInterface abstraction layer.
     55  *
     56  *   - Supports android.hardware.Camera API.
     57  *
     58  * 2.0: Initial release of expanded-capability HAL (Android 4.2) [camera2.h]:
     59  *
     60  *   - Sufficient for implementing existing android.hardware.Camera API.
     61  *
     62  *   - Allows for ZSL queue in camera service layer
     63  *
     64  *   - Not tested for any new features such manual capture control, Bayer RAW
     65  *     capture, reprocessing of RAW data.
     66  *
     67  * 3.0: First revision of expanded-capability HAL:
     68  *
     69  *   - Major version change since the ABI is completely different. No change to
     70  *     the required hardware capabilities or operational model from 2.0.
     71  *
     72  *   - Reworked input request and stream queue interfaces: Framework calls into
     73  *     HAL with next request and stream buffers already dequeued. Sync framework
     74  *     support is included, necessary for efficient implementations.
     75  *
     76  *   - Moved triggers into requests, most notifications into results.
     77  *
     78  *   - Consolidated all callbacks into framework into one structure, and all
     79  *     setup methods into a single initialize() call.
     80  *
     81  *   - Made stream configuration into a single call to simplify stream
     82  *     management. Bidirectional streams replace STREAM_FROM_STREAM construct.
     83  *
     84  *   - Limited mode semantics for older/limited hardware devices.
     85  *
     86  * 3.1: Minor revision of expanded-capability HAL:
     87  *
     88  *   - configure_streams passes consumer usage flags to the HAL.
     89  *
     90  *   - flush call to drop all in-flight requests/buffers as fast as possible.
     91  */
     92 
     93 /**
     94  * S2. Startup and general expected operation sequence:
     95  *
     96  * 1. Framework calls camera_module_t->common.open(), which returns a
     97  *    hardware_device_t structure.
     98  *
     99  * 2. Framework inspects the hardware_device_t->version field, and instantiates
    100  *    the appropriate handler for that version of the camera hardware device. In
    101  *    case the version is CAMERA_DEVICE_API_VERSION_3_0, the device is cast to
    102  *    a camera3_device_t.
    103  *
    104  * 3. Framework calls camera3_device_t->ops->initialize() with the framework
    105  *    callback function pointers. This will only be called this one time after
    106  *    open(), before any other functions in the ops structure are called.
    107  *
    108  * 4. The framework calls camera3_device_t->ops->configure_streams() with a list
    109  *    of input/output streams to the HAL device.
    110  *
    111  * 5. The framework allocates gralloc buffers and calls
    112  *    camera3_device_t->ops->register_stream_buffers() for at least one of the
    113  *    output streams listed in configure_streams. The same stream is registered
    114  *    only once.
    115  *
    116  * 5. The framework requests default settings for some number of use cases with
    117  *    calls to camera3_device_t->ops->construct_default_request_settings(). This
    118  *    may occur any time after step 3.
    119  *
    120  * 7. The framework constructs and sends the first capture request to the HAL,
    121  *    with settings based on one of the sets of default settings, and with at
    122  *    least one output stream, which has been registered earlier by the
    123  *    framework. This is sent to the HAL with
    124  *    camera3_device_t->ops->process_capture_request(). The HAL must block the
    125  *    return of this call until it is ready for the next request to be sent.
    126  *
    127  * 8. The framework continues to submit requests, and possibly call
    128  *    register_stream_buffers() for not-yet-registered streams, and call
    129  *    construct_default_request_settings to get default settings buffers for
    130  *    other use cases.
    131  *
    132  * 9. When the capture of a request begins (sensor starts exposing for the
    133  *    capture), the HAL calls camera3_callback_ops_t->notify() with the SHUTTER
    134  *    event, including the frame number and the timestamp for start of exposure.
    135  *    This notify call must be made before the first call to
    136  *    process_capture_result() for that frame number.
    137  *
    138  * 10. After some pipeline delay, the HAL begins to return completed captures to
    139  *    the framework with camera3_callback_ops_t->process_capture_result(). These
    140  *    are returned in the same order as the requests were submitted. Multiple
    141  *    requests can be in flight at once, depending on the pipeline depth of the
    142  *    camera HAL device.
    143  *
    144  * 11. After some time, the framework may stop submitting new requests, wait for
    145  *    the existing captures to complete (all buffers filled, all results
    146  *    returned), and then call configure_streams() again. This resets the camera
    147  *    hardware and pipeline for a new set of input/output streams. Some streams
    148  *    may be reused from the previous configuration; if these streams' buffers
    149  *    had already been registered with the HAL, they will not be registered
    150  *    again. The framework then continues from step 7, if at least one
    151  *    registered output stream remains (otherwise, step 5 is required first).
    152  *
    153  * 12. Alternatively, the framework may call camera3_device_t->common->close()
    154  *    to end the camera session. This may be called at any time when no other
    155  *    calls from the framework are active, although the call may block until all
    156  *    in-flight captures have completed (all results returned, all buffers
    157  *    filled). After the close call returns, no more calls to the
    158  *    camera3_callback_ops_t functions are allowed from the HAL. Once the
    159  *    close() call is underway, the framework may not call any other HAL device
    160  *    functions.
    161  *
    162  * 13. In case of an error or other asynchronous event, the HAL must call
    163  *    camera3_callback_ops_t->notify() with the appropriate error/event
    164  *    message. After returning from a fatal device-wide error notification, the
    165  *    HAL should act as if close() had been called on it. However, the HAL must
    166  *    either cancel or complete all outstanding captures before calling
    167  *    notify(), so that once notify() is called with a fatal error, the
    168  *    framework will not receive further callbacks from the device. Methods
    169  *    besides close() should return -ENODEV or NULL after the notify() method
    170  *    returns from a fatal error message.
    171  */
    172 
    173 /**
    174  * S3. Operational modes:
    175  *
    176  * The camera 3 HAL device can implement one of two possible operational modes;
    177  * limited and full. Full support is expected from new higher-end
    178  * devices. Limited mode has hardware requirements roughly in line with those
    179  * for a camera HAL device v1 implementation, and is expected from older or
    180  * inexpensive devices. Full is a strict superset of limited, and they share the
    181  * same essential operational flow, as documented above.
    182  *
    183  * The HAL must indicate its level of support with the
    184  * android.info.supportedHardwareLevel static metadata entry, with 0 indicating
    185  * limited mode, and 1 indicating full mode support.
    186  *
    187  * Roughly speaking, limited-mode devices do not allow for application control
    188  * of capture settings (3A control only), high-rate capture of high-resolution
    189  * images, raw sensor readout, or support for YUV output streams above maximum
    190  * recording resolution (JPEG only for large images).
    191  *
    192  * ** Details of limited mode behavior:
    193  *
    194  * - Limited-mode devices do not need to implement accurate synchronization
    195  *   between capture request settings and the actual image data
    196  *   captured. Instead, changes to settings may take effect some time in the
    197  *   future, and possibly not for the same output frame for each settings
    198  *   entry. Rapid changes in settings may result in some settings never being
    199  *   used for a capture. However, captures that include high-resolution output
    200  *   buffers ( > 1080p ) have to use the settings as specified (but see below
    201  *   for processing rate).
    202  *
    203  * - Limited-mode devices do not need to support most of the
    204  *   settings/result/static info metadata. Full-mode devices must support all
    205  *   metadata fields listed in TODO. Specifically, only the following settings
    206  *   are expected to be consumed or produced by a limited-mode HAL device:
    207  *
    208  *   android.control.aeAntibandingMode (controls)
    209  *   android.control.aeExposureCompensation (controls)
    210  *   android.control.aeLock (controls)
    211  *   android.control.aeMode (controls)
    212  *       [OFF means ON_FLASH_TORCH - TODO]
    213  *   android.control.aeRegions (controls)
    214  *   android.control.aeTargetFpsRange (controls)
    215  *   android.control.afMode (controls)
    216  *       [OFF means infinity focus]
    217  *   android.control.afRegions (controls)
    218  *   android.control.awbLock (controls)
    219  *   android.control.awbMode (controls)
    220  *       [OFF not supported]
    221  *   android.control.awbRegions (controls)
    222  *   android.control.captureIntent (controls)
    223  *   android.control.effectMode (controls)
    224  *   android.control.mode (controls)
    225  *       [OFF not supported]
    226  *   android.control.sceneMode (controls)
    227  *   android.control.videoStabilizationMode (controls)
    228  *   android.control.aeAvailableAntibandingModes (static)
    229  *   android.control.aeAvailableModes (static)
    230  *   android.control.aeAvailableTargetFpsRanges (static)
    231  *   android.control.aeCompensationRange (static)
    232  *   android.control.aeCompensationStep (static)
    233  *   android.control.afAvailableModes (static)
    234  *   android.control.availableEffects (static)
    235  *   android.control.availableSceneModes (static)
    236  *   android.control.availableVideoStabilizationModes (static)
    237  *   android.control.awbAvailableModes (static)
    238  *   android.control.maxRegions (static)
    239  *   android.control.sceneModeOverrides (static)
    240  *   android.control.aeRegions (dynamic)
    241  *   android.control.aeState (dynamic)
    242  *   android.control.afMode (dynamic)
    243  *   android.control.afRegions (dynamic)
    244  *   android.control.afState (dynamic)
    245  *   android.control.awbMode (dynamic)
    246  *   android.control.awbRegions (dynamic)
    247  *   android.control.awbState (dynamic)
    248  *   android.control.mode (dynamic)
    249  *
    250  *   android.flash.info.available (static)
    251  *
    252  *   android.info.supportedHardwareLevel (static)
    253  *
    254  *   android.jpeg.gpsCoordinates (controls)
    255  *   android.jpeg.gpsProcessingMethod (controls)
    256  *   android.jpeg.gpsTimestamp (controls)
    257  *   android.jpeg.orientation (controls)
    258  *   android.jpeg.quality (controls)
    259  *   android.jpeg.thumbnailQuality (controls)
    260  *   android.jpeg.thumbnailSize (controls)
    261  *   android.jpeg.availableThumbnailSizes (static)
    262  *   android.jpeg.maxSize (static)
    263  *   android.jpeg.gpsCoordinates (dynamic)
    264  *   android.jpeg.gpsProcessingMethod (dynamic)
    265  *   android.jpeg.gpsTimestamp (dynamic)
    266  *   android.jpeg.orientation (dynamic)
    267  *   android.jpeg.quality (dynamic)
    268  *   android.jpeg.size (dynamic)
    269  *   android.jpeg.thumbnailQuality (dynamic)
    270  *   android.jpeg.thumbnailSize (dynamic)
    271  *
    272  *   android.lens.info.minimumFocusDistance (static)
    273  *
    274  *   android.request.id (controls)
    275  *   android.request.id (dynamic)
    276  *
    277  *   android.scaler.cropRegion (controls)
    278  *       [ignores (x,y), assumes center-zoom]
    279  *   android.scaler.availableFormats (static)
    280  *       [RAW not supported]
    281  *   android.scaler.availableJpegMinDurations (static)
    282  *   android.scaler.availableJpegSizes (static)
    283  *   android.scaler.availableMaxDigitalZoom (static)
    284  *   android.scaler.availableProcessedMinDurations (static)
    285  *   android.scaler.availableProcessedSizes (static)
    286  *       [full resolution not supported]
    287  *   android.scaler.maxDigitalZoom (static)
    288  *   android.scaler.cropRegion (dynamic)
    289  *
    290  *   android.sensor.orientation (static)
    291  *   android.sensor.timestamp (dynamic)
    292  *
    293  *   android.statistics.faceDetectMode (controls)
    294  *   android.statistics.info.availableFaceDetectModes (static)
    295  *   android.statistics.faceDetectMode (dynamic)
    296  *   android.statistics.faceIds (dynamic)
    297  *   android.statistics.faceLandmarks (dynamic)
    298  *   android.statistics.faceRectangles (dynamic)
    299  *   android.statistics.faceScores (dynamic)
    300  *
    301  * - Captures in limited mode that include high-resolution (> 1080p) output
    302  *   buffers may block in process_capture_request() until all the output buffers
    303  *   have been filled. A full-mode HAL device must process sequences of
    304  *   high-resolution requests at the rate indicated in the static metadata for
    305  *   that pixel format. The HAL must still call process_capture_result() to
    306  *   provide the output; the framework must simply be prepared for
    307  *   process_capture_request() to block until after process_capture_result() for
    308  *   that request completes for high-resolution captures for limited-mode
    309  *   devices.
    310  *
    311  */
    312 
    313 /**
    314  * S4. 3A modes and state machines:
    315  *
    316  * While the actual 3A algorithms are up to the HAL implementation, a high-level
    317  * state machine description is defined by the HAL interface, to allow the HAL
    318  * device and the framework to communicate about the current state of 3A, and to
    319  * trigger 3A events.
    320  *
    321  * When the device is opened, all the individual 3A states must be
    322  * STATE_INACTIVE. Stream configuration does not reset 3A. For example, locked
    323  * focus must be maintained across the configure() call.
    324  *
    325  * Triggering a 3A action involves simply setting the relevant trigger entry in
    326  * the settings for the next request to indicate start of trigger. For example,
    327  * the trigger for starting an autofocus scan is setting the entry
    328  * ANDROID_CONTROL_AF_TRIGGER to ANDROID_CONTROL_AF_TRIGGER_START for one
    329  * request, and cancelling an autofocus scan is triggered by setting
    330  * ANDROID_CONTROL_AF_TRIGGER to ANDROID_CONTRL_AF_TRIGGER_CANCEL. Otherwise,
    331  * the entry will not exist, or be set to ANDROID_CONTROL_AF_TRIGGER_IDLE. Each
    332  * request with a trigger entry set to a non-IDLE value will be treated as an
    333  * independent triggering event.
    334  *
    335  * At the top level, 3A is controlled by the ANDROID_CONTROL_MODE setting, which
    336  * selects between no 3A (ANDROID_CONTROL_MODE_OFF), normal AUTO mode
    337  * (ANDROID_CONTROL_MODE_AUTO), and using the scene mode setting
    338  * (ANDROID_CONTROL_USE_SCENE_MODE).
    339  *
    340  * - In OFF mode, each of the individual AE/AF/AWB modes are effectively OFF,
    341  *   and none of the capture controls may be overridden by the 3A routines.
    342  *
    343  * - In AUTO mode, Auto-focus, auto-exposure, and auto-whitebalance all run
    344  *   their own independent algorithms, and have their own mode, state, and
    345  *   trigger metadata entries, as listed in the next section.
    346  *
    347  * - In USE_SCENE_MODE, the value of the ANDROID_CONTROL_SCENE_MODE entry must
    348  *   be used to determine the behavior of 3A routines. In SCENE_MODEs other than
    349  *   FACE_PRIORITY, the HAL must override the values of
    350  *   ANDROId_CONTROL_AE/AWB/AF_MODE to be the mode it prefers for the selected
    351  *   SCENE_MODE. For example, the HAL may prefer SCENE_MODE_NIGHT to use
    352  *   CONTINUOUS_FOCUS AF mode. Any user selection of AE/AWB/AF_MODE when scene
    353  *   must be ignored for these scene modes.
    354  *
    355  * - For SCENE_MODE_FACE_PRIORITY, the AE/AWB/AF_MODE controls work as in
    356  *   ANDROID_CONTROL_MODE_AUTO, but the 3A routines must bias toward metering
    357  *   and focusing on any detected faces in the scene.
    358  *
    359  * S4.1. Auto-focus settings and result entries:
    360  *
    361  *  Main metadata entries:
    362  *
    363  *   ANDROID_CONTROL_AF_MODE: Control for selecting the current autofocus
    364  *      mode. Set by the framework in the request settings.
    365  *
    366  *     AF_MODE_OFF: AF is disabled; the framework/app directly controls lens
    367  *         position.
    368  *
    369  *     AF_MODE_AUTO: Single-sweep autofocus. No lens movement unless AF is
    370  *         triggered.
    371  *
    372  *     AF_MODE_MACRO: Single-sweep up-close autofocus. No lens movement unless
    373  *         AF is triggered.
    374  *
    375  *     AF_MODE_CONTINUOUS_VIDEO: Smooth continuous focusing, for recording
    376  *         video. Triggering immediately locks focus in current
    377  *         position. Canceling resumes cotinuous focusing.
    378  *
    379  *     AF_MODE_CONTINUOUS_PICTURE: Fast continuous focusing, for
    380  *        zero-shutter-lag still capture. Triggering locks focus once currently
    381  *        active sweep concludes. Canceling resumes continuous focusing.
    382  *
    383  *     AF_MODE_EDOF: Advanced extended depth of field focusing. There is no
    384  *        autofocus scan, so triggering one or canceling one has no effect.
    385  *        Images are focused automatically by the HAL.
    386  *
    387  *   ANDROID_CONTROL_AF_STATE: Dynamic metadata describing the current AF
    388  *       algorithm state, reported by the HAL in the result metadata.
    389  *
    390  *     AF_STATE_INACTIVE: No focusing has been done, or algorithm was
    391  *        reset. Lens is not moving. Always the state for MODE_OFF or MODE_EDOF.
    392  *        When the device is opened, it must start in this state.
    393  *
    394  *     AF_STATE_PASSIVE_SCAN: A continuous focus algorithm is currently scanning
    395  *        for good focus. The lens is moving.
    396  *
    397  *     AF_STATE_PASSIVE_FOCUSED: A continuous focus algorithm believes it is
    398  *        well focused. The lens is not moving. The HAL may spontaneously leave
    399  *        this state.
    400  *
    401  *     AF_STATE_PASSIVE_UNFOCUSED: A continuous focus algorithm believes it is
    402  *        not well focused. The lens is not moving. The HAL may spontaneously
    403  *        leave this state.
    404  *
    405  *     AF_STATE_ACTIVE_SCAN: A scan triggered by the user is underway.
    406  *
    407  *     AF_STATE_FOCUSED_LOCKED: The AF algorithm believes it is focused. The
    408  *        lens is not moving.
    409  *
    410  *     AF_STATE_NOT_FOCUSED_LOCKED: The AF algorithm has been unable to
    411  *        focus. The lens is not moving.
    412  *
    413  *   ANDROID_CONTROL_AF_TRIGGER: Control for starting an autofocus scan, the
    414  *       meaning of which is mode- and state- dependent. Set by the framework in
    415  *       the request settings.
    416  *
    417  *     AF_TRIGGER_IDLE: No current trigger.
    418  *
    419  *     AF_TRIGGER_START: Trigger start of AF scan. Effect is mode and state
    420  *         dependent.
    421  *
    422  *     AF_TRIGGER_CANCEL: Cancel current AF scan if any, and reset algorithm to
    423  *         default.
    424  *
    425  *  Additional metadata entries:
    426  *
    427  *   ANDROID_CONTROL_AF_REGIONS: Control for selecting the regions of the FOV
    428  *       that should be used to determine good focus. This applies to all AF
    429  *       modes that scan for focus. Set by the framework in the request
    430  *       settings.
    431  *
    432  * S4.2. Auto-exposure settings and result entries:
    433  *
    434  *  Main metadata entries:
    435  *
    436  *   ANDROID_CONTROL_AE_MODE: Control for selecting the current auto-exposure
    437  *       mode. Set by the framework in the request settings.
    438  *
    439  *     AE_MODE_OFF: Autoexposure is disabled; the user controls exposure, gain,
    440  *         frame duration, and flash.
    441  *
    442  *     AE_MODE_ON: Standard autoexposure, with flash control disabled. User may
    443  *         set flash to fire or to torch mode.
    444  *
    445  *     AE_MODE_ON_AUTO_FLASH: Standard autoexposure, with flash on at HAL's
    446  *         discretion for precapture and still capture. User control of flash
    447  *         disabled.
    448  *
    449  *     AE_MODE_ON_ALWAYS_FLASH: Standard autoexposure, with flash always fired
    450  *         for capture, and at HAL's discretion for precapture.. User control of
    451  *         flash disabled.
    452  *
    453  *     AE_MODE_ON_AUTO_FLASH_REDEYE: Standard autoexposure, with flash on at
    454  *         HAL's discretion for precapture and still capture. Use a flash burst
    455  *         at end of precapture sequence to reduce redeye in the final
    456  *         picture. User control of flash disabled.
    457  *
    458  *   ANDROID_CONTROL_AE_STATE: Dynamic metadata describing the current AE
    459  *       algorithm state, reported by the HAL in the result metadata.
    460  *
    461  *     AE_STATE_INACTIVE: Initial AE state after mode switch. When the device is
    462  *         opened, it must start in this state.
    463  *
    464  *     AE_STATE_SEARCHING: AE is not converged to a good value, and is adjusting
    465  *         exposure parameters.
    466  *
    467  *     AE_STATE_CONVERGED: AE has found good exposure values for the current
    468  *         scene, and the exposure parameters are not changing. HAL may
    469  *         spontaneously leave this state to search for better solution.
    470  *
    471  *     AE_STATE_LOCKED: AE has been locked with the AE_LOCK control. Exposure
    472  *         values are not changing.
    473  *
    474  *     AE_STATE_FLASH_REQUIRED: The HAL has converged exposure, but believes
    475  *         flash is required for a sufficiently bright picture. Used for
    476  *         determining if a zero-shutter-lag frame can be used.
    477  *
    478  *     AE_STATE_PRECAPTURE: The HAL is in the middle of a precapture
    479  *         sequence. Depending on AE mode, this mode may involve firing the
    480  *         flash for metering, or a burst of flash pulses for redeye reduction.
    481  *
    482  *   ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER: Control for starting a metering
    483  *       sequence before capturing a high-quality image. Set by the framework in
    484  *       the request settings.
    485  *
    486  *      PRECAPTURE_TRIGGER_IDLE: No current trigger.
    487  *
    488  *      PRECAPTURE_TRIGGER_START: Start a precapture sequence. The HAL should
    489  *         use the subsequent requests to measure good exposure/white balance
    490  *         for an upcoming high-resolution capture.
    491  *
    492  *  Additional metadata entries:
    493  *
    494  *   ANDROID_CONTROL_AE_LOCK: Control for locking AE controls to their current
    495  *       values
    496  *
    497  *   ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION: Control for adjusting AE
    498  *       algorithm target brightness point.
    499  *
    500  *   ANDROID_CONTROL_AE_TARGET_FPS_RANGE: Control for selecting the target frame
    501  *       rate range for the AE algorithm. The AE routine cannot change the frame
    502  *       rate to be outside these bounds.
    503  *
    504  *   ANDROID_CONTROL_AE_REGIONS: Control for selecting the regions of the FOV
    505  *       that should be used to determine good exposure levels. This applies to
    506  *       all AE modes besides OFF.
    507  *
    508  * S4.3. Auto-whitebalance settings and result entries:
    509  *
    510  *  Main metadata entries:
    511  *
    512  *   ANDROID_CONTROL_AWB_MODE: Control for selecting the current white-balance
    513  *       mode.
    514  *
    515  *     AWB_MODE_OFF: Auto-whitebalance is disabled. User controls color matrix.
    516  *
    517  *     AWB_MODE_AUTO: Automatic white balance is enabled; 3A controls color
    518  *        transform, possibly using more complex transforms than a simple
    519  *        matrix.
    520  *
    521  *     AWB_MODE_INCANDESCENT: Fixed white balance settings good for indoor
    522  *        incandescent (tungsten) lighting, roughly 2700K.
    523  *
    524  *     AWB_MODE_FLUORESCENT: Fixed white balance settings good for fluorescent
    525  *        lighting, roughly 5000K.
    526  *
    527  *     AWB_MODE_WARM_FLUORESCENT: Fixed white balance settings good for
    528  *        fluorescent lighting, roughly 3000K.
    529  *
    530  *     AWB_MODE_DAYLIGHT: Fixed white balance settings good for daylight,
    531  *        roughly 5500K.
    532  *
    533  *     AWB_MODE_CLOUDY_DAYLIGHT: Fixed white balance settings good for clouded
    534  *        daylight, roughly 6500K.
    535  *
    536  *     AWB_MODE_TWILIGHT: Fixed white balance settings good for
    537  *        near-sunset/sunrise, roughly 15000K.
    538  *
    539  *     AWB_MODE_SHADE: Fixed white balance settings good for areas indirectly
    540  *        lit by the sun, roughly 7500K.
    541  *
    542  *   ANDROID_CONTROL_AWB_STATE: Dynamic metadata describing the current AWB
    543  *       algorithm state, reported by the HAL in the result metadata.
    544  *
    545  *     AWB_STATE_INACTIVE: Initial AWB state after mode switch. When the device
    546  *         is opened, it must start in this state.
    547  *
    548  *     AWB_STATE_SEARCHING: AWB is not converged to a good value, and is
    549  *         changing color adjustment parameters.
    550  *
    551  *     AWB_STATE_CONVERGED: AWB has found good color adjustment values for the
    552  *         current scene, and the parameters are not changing. HAL may
    553  *         spontaneously leave this state to search for better solution.
    554  *
    555  *     AWB_STATE_LOCKED: AWB has been locked with the AWB_LOCK control. Color
    556  *         adjustment values are not changing.
    557  *
    558  *  Additional metadata entries:
    559  *
    560  *   ANDROID_CONTROL_AWB_LOCK: Control for locking AWB color adjustments to
    561  *       their current values.
    562  *
    563  *   ANDROID_CONTROL_AWB_REGIONS: Control for selecting the regions of the FOV
    564  *       that should be used to determine good color balance. This applies only
    565  *       to auto-WB mode.
    566  *
    567  * S4.4. General state machine transition notes
    568  *
    569  *   Switching between AF, AE, or AWB modes always resets the algorithm's state
    570  *   to INACTIVE.  Similarly, switching between CONTROL_MODE or
    571  *   CONTROL_SCENE_MODE if CONTROL_MODE == USE_SCENE_MODE resets all the
    572  *   algorithm states to INACTIVE.
    573  *
    574  *   The tables below are per-mode.
    575  *
    576  * S4.5. AF state machines
    577  *
    578  *                       when enabling AF or changing AF mode
    579  *| state              | trans. cause  | new state          | notes            |
    580  *+--------------------+---------------+--------------------+------------------+
    581  *| Any                | AF mode change| INACTIVE           |                  |
    582  *+--------------------+---------------+--------------------+------------------+
    583  *
    584  *                            mode = AF_MODE_OFF or AF_MODE_EDOF
    585  *| state              | trans. cause  | new state          | notes            |
    586  *+--------------------+---------------+--------------------+------------------+
    587  *| INACTIVE           |               | INACTIVE           | Never changes    |
    588  *+--------------------+---------------+--------------------+------------------+
    589  *
    590  *                            mode = AF_MODE_AUTO or AF_MODE_MACRO
    591  *| state              | trans. cause  | new state          | notes            |
    592  *+--------------------+---------------+--------------------+------------------+
    593  *| INACTIVE           | AF_TRIGGER    | ACTIVE_SCAN        | Start AF sweep   |
    594  *|                    |               |                    | Lens now moving  |
    595  *+--------------------+---------------+--------------------+------------------+
    596  *| ACTIVE_SCAN        | AF sweep done | FOCUSED_LOCKED     | If AF successful |
    597  *|                    |               |                    | Lens now locked  |
    598  *+--------------------+---------------+--------------------+------------------+
    599  *| ACTIVE_SCAN        | AF sweep done | NOT_FOCUSED_LOCKED | If AF successful |
    600  *|                    |               |                    | Lens now locked  |
    601  *+--------------------+---------------+--------------------+------------------+
    602  *| ACTIVE_SCAN        | AF_CANCEL     | INACTIVE           | Cancel/reset AF  |
    603  *|                    |               |                    | Lens now locked  |
    604  *+--------------------+---------------+--------------------+------------------+
    605  *| FOCUSED_LOCKED     | AF_CANCEL     | INACTIVE           | Cancel/reset AF  |
    606  *+--------------------+---------------+--------------------+------------------+
    607  *| FOCUSED_LOCKED     | AF_TRIGGER    | ACTIVE_SCAN        | Start new sweep  |
    608  *|                    |               |                    | Lens now moving  |
    609  *+--------------------+---------------+--------------------+------------------+
    610  *| NOT_FOCUSED_LOCKED | AF_CANCEL     | INACTIVE           | Cancel/reset AF  |
    611  *+--------------------+---------------+--------------------+------------------+
    612  *| NOT_FOCUSED_LOCKED | AF_TRIGGER    | ACTIVE_SCAN        | Start new sweep  |
    613  *|                    |               |                    | Lens now moving  |
    614  *+--------------------+---------------+--------------------+------------------+
    615  *| All states         | mode change   | INACTIVE           |                  |
    616  *+--------------------+---------------+--------------------+------------------+
    617  *
    618  *                            mode = AF_MODE_CONTINUOUS_VIDEO
    619  *| state              | trans. cause  | new state          | notes            |
    620  *+--------------------+---------------+--------------------+------------------+
    621  *| INACTIVE           | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    622  *|                    | new scan      |                    | Lens now moving  |
    623  *+--------------------+---------------+--------------------+------------------+
    624  *| INACTIVE           | AF_TRIGGER    | NOT_FOCUSED_LOCKED | AF state query   |
    625  *|                    |               |                    | Lens now locked  |
    626  *+--------------------+---------------+--------------------+------------------+
    627  *| PASSIVE_SCAN       | HAL completes | PASSIVE_FOCUSED    | End AF scan      |
    628  *|                    | current scan  |                    | Lens now locked  |
    629  *+--------------------+---------------+--------------------+------------------+
    630  *| PASSIVE_SCAN       | HAL fails     | PASSIVE_UNFOCUSED  | End AF scan      |
    631  *|                    | current scan  |                    | Lens now locked  |
    632  *+--------------------+---------------+--------------------+------------------+
    633  *| PASSIVE_SCAN       | AF_TRIGGER    | FOCUSED_LOCKED     | Immediate trans. |
    634  *|                    |               |                    | if focus is good |
    635  *|                    |               |                    | Lens now locked  |
    636  *+--------------------+---------------+--------------------+------------------+
    637  *| PASSIVE_SCAN       | AF_TRIGGER    | NOT_FOCUSED_LOCKED | Immediate trans. |
    638  *|                    |               |                    | if focus is bad  |
    639  *|                    |               |                    | Lens now locked  |
    640  *+--------------------+---------------+--------------------+------------------+
    641  *| PASSIVE_SCAN       | AF_CANCEL     | INACTIVE           | Reset lens       |
    642  *|                    |               |                    | position         |
    643  *|                    |               |                    | Lens now locked  |
    644  *+--------------------+---------------+--------------------+------------------+
    645  *| PASSIVE_FOCUSED    | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    646  *|                    | new scan      |                    | Lens now moving  |
    647  *+--------------------+---------------+--------------------+------------------+
    648  *| PASSIVE_UNFOCUSED  | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    649  *|                    | new scan      |                    | Lens now moving  |
    650  *+--------------------+---------------+--------------------+------------------+
    651  *| PASSIVE_FOCUSED    | AF_TRIGGER    | FOCUSED_LOCKED     | Immediate trans. |
    652  *|                    |               |                    | Lens now locked  |
    653  *+--------------------+---------------+--------------------+------------------+
    654  *| PASSIVE_UNFOCUSED  | AF_TRIGGER    | NOT_FOCUSED_LOCKED | Immediate trans. |
    655  *|                    |               |                    | Lens now locked  |
    656  *+--------------------+---------------+--------------------+------------------+
    657  *| FOCUSED_LOCKED     | AF_TRIGGER    | FOCUSED_LOCKED     | No effect        |
    658  *+--------------------+---------------+--------------------+------------------+
    659  *| FOCUSED_LOCKED     | AF_CANCEL     | INACTIVE           | Restart AF scan  |
    660  *+--------------------+---------------+--------------------+------------------+
    661  *| NOT_FOCUSED_LOCKED | AF_TRIGGER    | NOT_FOCUSED_LOCKED | No effect        |
    662  *+--------------------+---------------+--------------------+------------------+
    663  *| NOT_FOCUSED_LOCKED | AF_CANCEL     | INACTIVE           | Restart AF scan  |
    664  *+--------------------+---------------+--------------------+------------------+
    665  *
    666  *                            mode = AF_MODE_CONTINUOUS_PICTURE
    667  *| state              | trans. cause  | new state          | notes            |
    668  *+--------------------+---------------+--------------------+------------------+
    669  *| INACTIVE           | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    670  *|                    | new scan      |                    | Lens now moving  |
    671  *+--------------------+---------------+--------------------+------------------+
    672  *| INACTIVE           | AF_TRIGGER    | NOT_FOCUSED_LOCKED | AF state query   |
    673  *|                    |               |                    | Lens now locked  |
    674  *+--------------------+---------------+--------------------+------------------+
    675  *| PASSIVE_SCAN       | HAL completes | PASSIVE_FOCUSED    | End AF scan      |
    676  *|                    | current scan  |                    | Lens now locked  |
    677  *+--------------------+---------------+--------------------+------------------+
    678  *| PASSIVE_SCAN       | HAL fails     | PASSIVE_UNFOCUSED  | End AF scan      |
    679  *|                    | current scan  |                    | Lens now locked  |
    680  *+--------------------+---------------+--------------------+------------------+
    681  *| PASSIVE_SCAN       | AF_TRIGGER    | FOCUSED_LOCKED     | Eventual trans.  |
    682  *|                    |               |                    | once focus good  |
    683  *|                    |               |                    | Lens now locked  |
    684  *+--------------------+---------------+--------------------+------------------+
    685  *| PASSIVE_SCAN       | AF_TRIGGER    | NOT_FOCUSED_LOCKED | Eventual trans.  |
    686  *|                    |               |                    | if cannot focus  |
    687  *|                    |               |                    | Lens now locked  |
    688  *+--------------------+---------------+--------------------+------------------+
    689  *| PASSIVE_SCAN       | AF_CANCEL     | INACTIVE           | Reset lens       |
    690  *|                    |               |                    | position         |
    691  *|                    |               |                    | Lens now locked  |
    692  *+--------------------+---------------+--------------------+------------------+
    693  *| PASSIVE_FOCUSED    | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    694  *|                    | new scan      |                    | Lens now moving  |
    695  *+--------------------+---------------+--------------------+------------------+
    696  *| PASSIVE_UNFOCUSED  | HAL initiates | PASSIVE_SCAN       | Start AF scan    |
    697  *|                    | new scan      |                    | Lens now moving  |
    698  *+--------------------+---------------+--------------------+------------------+
    699  *| PASSIVE_FOCUSED    | AF_TRIGGER    | FOCUSED_LOCKED     | Immediate trans. |
    700  *|                    |               |                    | Lens now locked  |
    701  *+--------------------+---------------+--------------------+------------------+
    702  *| PASSIVE_UNFOCUSED  | AF_TRIGGER    | NOT_FOCUSED_LOCKED | Immediate trans. |
    703  *|                    |               |                    | Lens now locked  |
    704  *+--------------------+---------------+--------------------+------------------+
    705  *| FOCUSED_LOCKED     | AF_TRIGGER    | FOCUSED_LOCKED     | No effect        |
    706  *+--------------------+---------------+--------------------+------------------+
    707  *| FOCUSED_LOCKED     | AF_CANCEL     | INACTIVE           | Restart AF scan  |
    708  *+--------------------+---------------+--------------------+------------------+
    709  *| NOT_FOCUSED_LOCKED | AF_TRIGGER    | NOT_FOCUSED_LOCKED | No effect        |
    710  *+--------------------+---------------+--------------------+------------------+
    711  *| NOT_FOCUSED_LOCKED | AF_CANCEL     | INACTIVE           | Restart AF scan  |
    712  *+--------------------+---------------+--------------------+------------------+
    713  *
    714  * S4.6. AE and AWB state machines
    715  *
    716  *   The AE and AWB state machines are mostly identical. AE has additional
    717  *   FLASH_REQUIRED and PRECAPTURE states. So rows below that refer to those two
    718  *   states should be ignored for the AWB state machine.
    719  *
    720  *                  when enabling AE/AWB or changing AE/AWB mode
    721  *| state              | trans. cause  | new state          | notes            |
    722  *+--------------------+---------------+--------------------+------------------+
    723  *| Any                |  mode change  | INACTIVE           |                  |
    724  *+--------------------+---------------+--------------------+------------------+
    725  *
    726  *                            mode = AE_MODE_OFF / AWB mode not AUTO
    727  *| state              | trans. cause  | new state          | notes            |
    728  *+--------------------+---------------+--------------------+------------------+
    729  *| INACTIVE           |               | INACTIVE           | AE/AWB disabled  |
    730  *+--------------------+---------------+--------------------+------------------+
    731  *
    732  *                            mode = AE_MODE_ON_* / AWB_MODE_AUTO
    733  *| state              | trans. cause  | new state          | notes            |
    734  *+--------------------+---------------+--------------------+------------------+
    735  *| INACTIVE           | HAL initiates | SEARCHING          |                  |
    736  *|                    | AE/AWB scan   |                    |                  |
    737  *+--------------------+---------------+--------------------+------------------+
    738  *| INACTIVE           | AE/AWB_LOCK   | LOCKED             | values locked    |
    739  *|                    | on            |                    |                  |
    740  *+--------------------+---------------+--------------------+------------------+
    741  *| SEARCHING          | HAL finishes  | CONVERGED          | good values, not |
    742  *|                    | AE/AWB scan   |                    | changing         |
    743  *+--------------------+---------------+--------------------+------------------+
    744  *| SEARCHING          | HAL finishes  | FLASH_REQUIRED     | converged but too|
    745  *|                    | AE scan       |                    | dark w/o flash   |
    746  *+--------------------+---------------+--------------------+------------------+
    747  *| SEARCHING          | AE/AWB_LOCK   | LOCKED             | values locked    |
    748  *|                    | on            |                    |                  |
    749  *+--------------------+---------------+--------------------+------------------+
    750  *| CONVERGED          | HAL initiates | SEARCHING          | values locked    |
    751  *|                    | AE/AWB scan   |                    |                  |
    752  *+--------------------+---------------+--------------------+------------------+
    753  *| CONVERGED          | AE/AWB_LOCK   | LOCKED             | values locked    |
    754  *|                    | on            |                    |                  |
    755  *+--------------------+---------------+--------------------+------------------+
    756  *| FLASH_REQUIRED     | HAL initiates | SEARCHING          | values locked    |
    757  *|                    | AE/AWB scan   |                    |                  |
    758  *+--------------------+---------------+--------------------+------------------+
    759  *| FLASH_REQUIRED     | AE/AWB_LOCK   | LOCKED             | values locked    |
    760  *|                    | on            |                    |                  |
    761  *+--------------------+---------------+--------------------+------------------+
    762  *| LOCKED             | AE/AWB_LOCK   | SEARCHING          | values not good  |
    763  *|                    | off           |                    | after unlock     |
    764  *+--------------------+---------------+--------------------+------------------+
    765  *| LOCKED             | AE/AWB_LOCK   | CONVERGED          | values good      |
    766  *|                    | off           |                    | after unlock     |
    767  *+--------------------+---------------+--------------------+------------------+
    768  *| LOCKED             | AE_LOCK       | FLASH_REQUIRED     | exposure good,   |
    769  *|                    | off           |                    | but too dark     |
    770  *+--------------------+---------------+--------------------+------------------+
    771  *| All AE states      | PRECAPTURE_   | PRECAPTURE         | Start precapture |
    772  *|                    | START         |                    | sequence         |
    773  *+--------------------+---------------+--------------------+------------------+
    774  *| PRECAPTURE         | Sequence done.| CONVERGED          | Ready for high-  |
    775  *|                    | AE_LOCK off   |                    | quality capture  |
    776  *+--------------------+---------------+--------------------+------------------+
    777  *| PRECAPTURE         | Sequence done.| LOCKED             | Ready for high-  |
    778  *|                    | AE_LOCK on    |                    | quality capture  |
    779  *+--------------------+---------------+--------------------+------------------+
    780  *
    781  */
    782 
    783 /**
    784  * S5. Cropping:
    785  *
    786  * Cropping of the full pixel array (for digital zoom and other use cases where
    787  * a smaller FOV is desirable) is communicated through the
    788  * ANDROID_SCALER_CROP_REGION setting. This is a per-request setting, and can
    789  * change on a per-request basis, which is critical for implementing smooth
    790  * digital zoom.
    791  *
    792  * The region is defined as a rectangle (x, y, width, height), with (x, y)
    793  * describing the top-left corner of the rectangle. The rectangle is defined on
    794  * the coordinate system of the sensor active pixel array, with (0,0) being the
    795  * top-left pixel of the active pixel array. Therefore, the width and height
    796  * cannot be larger than the dimensions reported in the
    797  * ANDROID_SENSOR_ACTIVE_PIXEL_ARRAY static info field. The minimum allowed
    798  * width and height are reported by the HAL through the
    799  * ANDROID_SCALER_MAX_DIGITAL_ZOOM static info field, which describes the
    800  * maximum supported zoom factor. Therefore, the minimum crop region width and
    801  * height are:
    802  *
    803  * {width, height} =
    804  *    { floor(ANDROID_SENSOR_ACTIVE_PIXEL_ARRAY[0] /
    805  *        ANDROID_SCALER_MAX_DIGITAL_ZOOM),
    806  *      floor(ANDROID_SENSOR_ACTIVE_PIXEL_ARRAY[1] /
    807  *        ANDROID_SCALER_MAX_DIGITAL_ZOOM) }
    808  *
    809  * If the crop region needs to fulfill specific requirements (for example, it
    810  * needs to start on even coordinates, and its width/height needs to be even),
    811  * the HAL must do the necessary rounding and write out the final crop region
    812  * used in the output result metadata. Similarly, if the HAL implements video
    813  * stabilization, it must adjust the result crop region to describe the region
    814  * actually included in the output after video stabilization is applied. In
    815  * general, a camera-using application must be able to determine the field of
    816  * view it is receiving based on the crop region, the dimensions of the image
    817  * sensor, and the lens focal length.
    818  *
    819  * Since the crop region applies to all streams, which may have different aspect
    820  * ratios than the crop region, the exact sensor region used for each stream may
    821  * be smaller than the crop region. Specifically, each stream should maintain
    822  * square pixels and its aspect ratio by minimally further cropping the defined
    823  * crop region. If the stream's aspect ratio is wider than the crop region, the
    824  * stream should be further cropped vertically, and if the stream's aspect ratio
    825  * is narrower than the crop region, the stream should be further cropped
    826  * horizontally.
    827  *
    828  * In all cases, the stream crop must be centered within the full crop region,
    829  * and each stream is only either cropped horizontally or vertical relative to
    830  * the full crop region, never both.
    831  *
    832  * For example, if two streams are defined, a 640x480 stream (4:3 aspect), and a
    833  * 1280x720 stream (16:9 aspect), below demonstrates the expected output regions
    834  * for each stream for a few sample crop regions, on a hypothetical 3 MP (2000 x
    835  * 1500 pixel array) sensor.
    836  *
    837  * Crop region: (500, 375, 1000, 750) (4:3 aspect ratio)
    838  *
    839  *   640x480 stream crop: (500, 375, 1000, 750) (equal to crop region)
    840  *   1280x720 stream crop: (500, 469, 1000, 562) (marked with =)
    841  *
    842  * 0                   1000               2000
    843  * +---------+---------+---------+----------+
    844  * | Active pixel array                     |
    845  * |                                        |
    846  * |                                        |
    847  * +         +-------------------+          + 375
    848  * |         |                   |          |
    849  * |         O===================O          |
    850  * |         I 1280x720 stream   I          |
    851  * +         I                   I          + 750
    852  * |         I                   I          |
    853  * |         O===================O          |
    854  * |         |                   |          |
    855  * +         +-------------------+          + 1125
    856  * |          Crop region, 640x480 stream   |
    857  * |                                        |
    858  * |                                        |
    859  * +---------+---------+---------+----------+ 1500
    860  *
    861  * Crop region: (500, 375, 1333, 750) (16:9 aspect ratio)
    862  *
    863  *   640x480 stream crop: (666, 375, 1000, 750) (marked with =)
    864  *   1280x720 stream crop: (500, 375, 1333, 750) (equal to crop region)
    865  *
    866  * 0                   1000               2000
    867  * +---------+---------+---------+----------+
    868  * | Active pixel array                     |
    869  * |                                        |
    870  * |                                        |
    871  * +         +---O==================O---+   + 375
    872  * |         |   I 640x480 stream   I   |   |
    873  * |         |   I                  I   |   |
    874  * |         |   I                  I   |   |
    875  * +         |   I                  I   |   + 750
    876  * |         |   I                  I   |   |
    877  * |         |   I                  I   |   |
    878  * |         |   I                  I   |   |
    879  * +         +---O==================O---+   + 1125
    880  * |          Crop region, 1280x720 stream  |
    881  * |                                        |
    882  * |                                        |
    883  * +---------+---------+---------+----------+ 1500
    884  *
    885  * Crop region: (500, 375, 750, 750) (1:1 aspect ratio)
    886  *
    887  *   640x480 stream crop: (500, 469, 750, 562) (marked with =)
    888  *   1280x720 stream crop: (500, 543, 750, 414) (marged with #)
    889  *
    890  * 0                   1000               2000
    891  * +---------+---------+---------+----------+
    892  * | Active pixel array                     |
    893  * |                                        |
    894  * |                                        |
    895  * +         +--------------+               + 375
    896  * |         O==============O               |
    897  * |         ################               |
    898  * |         #              #               |
    899  * +         #              #               + 750
    900  * |         #              #               |
    901  * |         ################ 1280x720      |
    902  * |         O==============O 640x480       |
    903  * +         +--------------+               + 1125
    904  * |          Crop region                   |
    905  * |                                        |
    906  * |                                        |
    907  * +---------+---------+---------+----------+ 1500
    908  *
    909  * And a final example, a 1024x1024 square aspect ratio stream instead of the
    910  * 480p stream:
    911  *
    912  * Crop region: (500, 375, 1000, 750) (4:3 aspect ratio)
    913  *
    914  *   1024x1024 stream crop: (625, 375, 750, 750) (marked with #)
    915  *   1280x720 stream crop: (500, 469, 1000, 562) (marked with =)
    916  *
    917  * 0                   1000               2000
    918  * +---------+---------+---------+----------+
    919  * | Active pixel array                     |
    920  * |                                        |
    921  * |              1024x1024 stream          |
    922  * +         +--###############--+          + 375
    923  * |         |  #             #  |          |
    924  * |         O===================O          |
    925  * |         I 1280x720 stream   I          |
    926  * +         I                   I          + 750
    927  * |         I                   I          |
    928  * |         O===================O          |
    929  * |         |  #             #  |          |
    930  * +         +--###############--+          + 1125
    931  * |          Crop region                   |
    932  * |                                        |
    933  * |                                        |
    934  * +---------+---------+---------+----------+ 1500
    935  *
    936  */
    937 
    938 /**
    939  * S6. Error management:
    940  *
    941  * Camera HAL device ops functions that have a return value will all return
    942  * -ENODEV / NULL in case of a serious error. This means the device cannot
    943  * continue operation, and must be closed by the framework. Once this error is
    944  * returned by some method, or if notify() is called with ERROR_DEVICE, only
    945  * the close() method can be called successfully. All other methods will return
    946  * -ENODEV / NULL.
    947  *
    948  * If a device op is called in the wrong sequence, for example if the framework
    949  * calls configure_streams() is called before initialize(), the device must
    950  * return -ENOSYS from the call, and do nothing.
    951  *
    952  * Transient errors in image capture must be reported through notify() as follows:
    953  *
    954  * - The failure of an entire capture to occur must be reported by the HAL by
    955  *   calling notify() with ERROR_REQUEST. Individual errors for the result
    956  *   metadata or the output buffers must not be reported in this case.
    957  *
    958  * - If the metadata for a capture cannot be produced, but some image buffers
    959  *   were filled, the HAL must call notify() with ERROR_RESULT.
    960  *
    961  * - If an output image buffer could not be filled, but either the metadata was
    962  *   produced or some other buffers were filled, the HAL must call notify() with
    963  *   ERROR_BUFFER for each failed buffer.
    964  *
    965  * In each of these transient failure cases, the HAL must still call
    966  * process_capture_result, with valid output buffer_handle_t. If the result
    967  * metadata could not be produced, it should be NULL. If some buffers could not
    968  * be filled, their sync fences must be set to the error state.
    969  *
    970  * Invalid input arguments result in -EINVAL from the appropriate methods. In
    971  * that case, the framework must act as if that call had never been made.
    972  *
    973  */
    974 
    975 __BEGIN_DECLS
    976 
    977 struct camera3_device;
    978 
    979 /**********************************************************************
    980  *
    981  * Camera3 stream and stream buffer definitions.
    982  *
    983  * These structs and enums define the handles and contents of the input and
    984  * output streams connecting the HAL to various framework and application buffer
    985  * consumers. Each stream is backed by a gralloc buffer queue.
    986  *
    987  */
    988 
    989 /**
    990  * camera3_stream_type_t:
    991  *
    992  * The type of the camera stream, which defines whether the camera HAL device is
    993  * the producer or the consumer for that stream, and how the buffers of the
    994  * stream relate to the other streams.
    995  */
    996 typedef enum camera3_stream_type {
    997     /**
    998      * This stream is an output stream; the camera HAL device will be
    999      * responsible for filling buffers from this stream with newly captured or
   1000      * reprocessed image data.
   1001      */
   1002     CAMERA3_STREAM_OUTPUT = 0,
   1003 
   1004     /**
   1005      * This stream is an input stream; the camera HAL device will be responsible
   1006      * for reading buffers from this stream and sending them through the camera
   1007      * processing pipeline, as if the buffer was a newly captured image from the
   1008      * imager.
   1009      */
   1010     CAMERA3_STREAM_INPUT = 1,
   1011 
   1012     /**
   1013      * This stream can be used for input and output. Typically, the stream is
   1014      * used as an output stream, but occasionally one already-filled buffer may
   1015      * be sent back to the HAL device for reprocessing.
   1016      *
   1017      * This kind of stream is meant generally for zero-shutter-lag features,
   1018      * where copying the captured image from the output buffer to the
   1019      * reprocessing input buffer would be expensive. The stream will be used by
   1020      * the framework as follows:
   1021      *
   1022      * 1. The framework includes a buffer from this stream as output buffer in a
   1023      *    request as normal.
   1024      *
   1025      * 2. Once the HAL device returns a filled output buffer to the framework,
   1026      *    the framework may do one of two things with the filled buffer:
   1027      *
   1028      * 2. a. The framework uses the filled data, and returns the now-used buffer
   1029      *       to the stream queue for reuse. This behavior exactly matches the
   1030      *       OUTPUT type of stream.
   1031      *
   1032      * 2. b. The framework wants to reprocess the filled data, and uses the
   1033      *       buffer as an input buffer for a request. Once the HAL device has
   1034      *       used the reprocessing buffer, it then returns it to the
   1035      *       framework. The framework then returns the now-used buffer to the
   1036      *       stream queue for reuse.
   1037      *
   1038      * 3. The HAL device will be given the buffer again as an output buffer for
   1039      *    a request at some future point.
   1040      *
   1041      * Note that the HAL will always be reprocessing data it produced.
   1042      *
   1043      */
   1044     CAMERA3_STREAM_BIDIRECTIONAL = 2,
   1045 
   1046     /**
   1047      * Total number of framework-defined stream types
   1048      */
   1049     CAMERA3_NUM_STREAM_TYPES
   1050 
   1051 } camera3_stream_type_t;
   1052 
   1053 /**
   1054  * camera3_stream_t:
   1055  *
   1056  * A handle to a single camera input or output stream. A stream is defined by
   1057  * the framework by its buffer resolution and format, and additionally by the
   1058  * HAL with the gralloc usage flags and the maximum in-flight buffer count.
   1059  *
   1060  * The stream structures are owned by the framework, but pointers to a
   1061  * camera3_stream passed into the HAL by configure_streams() are valid until the
   1062  * end of the first subsequent configure_streams() call that _does not_ include
   1063  * that camera3_stream as an argument, or until the end of the close() call.
   1064  *
   1065  * All camera3_stream framework-controlled members are immutable once the
   1066  * camera3_stream is passed into configure_streams().  The HAL may only change
   1067  * the HAL-controlled parameters during a configure_streams() call, except for
   1068  * the contents of the private pointer.
   1069  *
   1070  * If a configure_streams() call returns a non-fatal error, all active streams
   1071  * remain valid as if configure_streams() had not been called.
   1072  *
   1073  * The endpoint of the stream is not visible to the camera HAL device.
   1074  * In DEVICE_API_VERSION_3_1, this was changed to share consumer usage flags
   1075  * on streams where the camera is a producer (OUTPUT and BIDIRECTIONAL stream
   1076  * types) see the usage field below.
   1077  */
   1078 typedef struct camera3_stream {
   1079 
   1080     /*****
   1081      * Set by framework before configure_streams()
   1082      */
   1083 
   1084     /**
   1085      * The type of the stream, one of the camera3_stream_type_t values.
   1086      */
   1087     int stream_type;
   1088 
   1089     /**
   1090      * The width in pixels of the buffers in this stream
   1091      */
   1092     uint32_t width;
   1093 
   1094     /**
   1095      * The height in pixels of the buffers in this stream
   1096      */
   1097     uint32_t height;
   1098 
   1099     /**
   1100      * The pixel format for the buffers in this stream. Format is a value from
   1101      * the HAL_PIXEL_FORMAT_* list in system/core/include/system/graphics.h, or
   1102      * from device-specific headers.
   1103      *
   1104      * If HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED is used, then the platform
   1105      * gralloc module will select a format based on the usage flags provided by
   1106      * the camera device and the other endpoint of the stream.
   1107      *
   1108      * The camera HAL device must inspect the buffers handed to it in the
   1109      * subsequent register_stream_buffers() call to obtain the
   1110      * implementation-specific format details, if necessary.
   1111      */
   1112     int format;
   1113 
   1114     /*****
   1115      * Set by HAL during configure_streams().
   1116      */
   1117 
   1118     /**
   1119      * The gralloc usage flags for this stream, as needed by the HAL. The usage
   1120      * flags are defined in gralloc.h (GRALLOC_USAGE_*), or in device-specific
   1121      * headers.
   1122      *
   1123      * For output streams, these are the HAL's producer usage flags. For input
   1124      * streams, these are the HAL's consumer usage flags. The usage flags from
   1125      * the producer and the consumer will be combined together and then passed
   1126      * to the platform gralloc HAL module for allocating the gralloc buffers for
   1127      * each stream.
   1128      *
   1129      * Version information:
   1130      *
   1131      * == CAMERA_DEVICE_API_VERSION_3_0:
   1132      *
   1133      *   No initial value guaranteed when passed via configure_streams().
   1134      *   HAL may not use this field as input, and must write over this field
   1135      *   with its usage flags.
   1136      *
   1137      * >= CAMERA_DEVICE_API_VERSION_3_1:
   1138      *
   1139      *   For stream_type OUTPUT and BIDIRECTIONAL, when passed via
   1140      *   configure_streams(), the initial value of this is the consumer's
   1141      *   usage flags.  The HAL may use these consumer flags to decide stream
   1142      *   configuration.
   1143      *   For stream_type INPUT, when passed via configure_streams(), the initial
   1144      *   value of this is 0.
   1145      *   For all streams passed via configure_streams(), the HAL must write
   1146      *   over this field with its usage flags.
   1147      */
   1148     uint32_t usage;
   1149 
   1150     /**
   1151      * The maximum number of buffers the HAL device may need to have dequeued at
   1152      * the same time. The HAL device may not have more buffers in-flight from
   1153      * this stream than this value.
   1154      */
   1155     uint32_t max_buffers;
   1156 
   1157     /**
   1158      * A handle to HAL-private information for the stream. Will not be inspected
   1159      * by the framework code.
   1160      */
   1161     void *priv;
   1162 
   1163 } camera3_stream_t;
   1164 
   1165 /**
   1166  * camera3_stream_configuration_t:
   1167  *
   1168  * A structure of stream definitions, used by configure_streams(). This
   1169  * structure defines all the output streams and the reprocessing input
   1170  * stream for the current camera use case.
   1171  */
   1172 typedef struct camera3_stream_configuration {
   1173     /**
   1174      * The total number of streams requested by the framework.  This includes
   1175      * both input and output streams. The number of streams will be at least 1,
   1176      * and there will be at least one output-capable stream.
   1177      */
   1178     uint32_t num_streams;
   1179 
   1180     /**
   1181      * An array of camera stream pointers, defining the input/output
   1182      * configuration for the camera HAL device.
   1183      *
   1184      * At most one input-capable stream may be defined (INPUT or BIDIRECTIONAL)
   1185      * in a single configuration.
   1186      *
   1187      * At least one output-capable stream must be defined (OUTPUT or
   1188      * BIDIRECTIONAL).
   1189      */
   1190     camera3_stream_t **streams;
   1191 
   1192 } camera3_stream_configuration_t;
   1193 
   1194 /**
   1195  * camera3_buffer_status_t:
   1196  *
   1197  * The current status of a single stream buffer.
   1198  */
   1199 typedef enum camera3_buffer_status {
   1200     /**
   1201      * The buffer is in a normal state, and can be used after waiting on its
   1202      * sync fence.
   1203      */
   1204     CAMERA3_BUFFER_STATUS_OK = 0,
   1205 
   1206     /**
   1207      * The buffer does not contain valid data, and the data in it should not be
   1208      * used. The sync fence must still be waited on before reusing the buffer.
   1209      */
   1210     CAMERA3_BUFFER_STATUS_ERROR = 1
   1211 
   1212 } camera3_buffer_status_t;
   1213 
   1214 /**
   1215  * camera3_stream_buffer_t:
   1216  *
   1217  * A single buffer from a camera3 stream. It includes a handle to its parent
   1218  * stream, the handle to the gralloc buffer itself, and sync fences
   1219  *
   1220  * The buffer does not specify whether it is to be used for input or output;
   1221  * that is determined by its parent stream type and how the buffer is passed to
   1222  * the HAL device.
   1223  */
   1224 typedef struct camera3_stream_buffer {
   1225     /**
   1226      * The handle of the stream this buffer is associated with
   1227      */
   1228     camera3_stream_t *stream;
   1229 
   1230     /**
   1231      * The native handle to the buffer
   1232      */
   1233     buffer_handle_t *buffer;
   1234 
   1235     /**
   1236      * Current state of the buffer, one of the camera3_buffer_status_t
   1237      * values. The framework will not pass buffers to the HAL that are in an
   1238      * error state. In case a buffer could not be filled by the HAL, it must
   1239      * have its status set to CAMERA3_BUFFER_STATUS_ERROR when returned to the
   1240      * framework with process_capture_result().
   1241      */
   1242     int status;
   1243 
   1244     /**
   1245      * The acquire sync fence for this buffer. The HAL must wait on this fence
   1246      * fd before attempting to read from or write to this buffer.
   1247      *
   1248      * The framework may be set to -1 to indicate that no waiting is necessary
   1249      * for this buffer.
   1250      *
   1251      * When the HAL returns an output buffer to the framework with
   1252      * process_capture_result(), the acquire_fence must be set to -1. If the HAL
   1253      * never waits on the acquire_fence due to an error in filling a buffer,
   1254      * when calling process_capture_result() the HAL must set the release_fence
   1255      * of the buffer to be the acquire_fence passed to it by the framework. This
   1256      * will allow the framework to wait on the fence before reusing the buffer.
   1257      *
   1258      * For input buffers, the HAL must not change the acquire_fence field during
   1259      * the process_capture_request() call.
   1260      */
   1261      int acquire_fence;
   1262 
   1263     /**
   1264      * The release sync fence for this buffer. The HAL must set this fence when
   1265      * returning buffers to the framework, or write -1 to indicate that no
   1266      * waiting is required for this buffer.
   1267      *
   1268      * For the input buffer, the release fence must be set by the
   1269      * process_capture_request() call. For the output buffers, the fences must
   1270      * be set in the output_buffers array passed to process_capture_result().
   1271      *
   1272      */
   1273     int release_fence;
   1274 
   1275 } camera3_stream_buffer_t;
   1276 
   1277 /**
   1278  * camera3_stream_buffer_set_t:
   1279  *
   1280  * The complete set of gralloc buffers for a stream. This structure is given to
   1281  * register_stream_buffers() to allow the camera HAL device to register/map/etc
   1282  * newly allocated stream buffers.
   1283  */
   1284 typedef struct camera3_stream_buffer_set {
   1285     /**
   1286      * The stream handle for the stream these buffers belong to
   1287      */
   1288     camera3_stream_t *stream;
   1289 
   1290     /**
   1291      * The number of buffers in this stream. It is guaranteed to be at least
   1292      * stream->max_buffers.
   1293      */
   1294     uint32_t num_buffers;
   1295 
   1296     /**
   1297      * The array of gralloc buffer handles for this stream. If the stream format
   1298      * is set to HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, the camera HAL device
   1299      * should inspect the passed-in buffers to determine any platform-private
   1300      * pixel format information.
   1301      */
   1302     buffer_handle_t **buffers;
   1303 
   1304 } camera3_stream_buffer_set_t;
   1305 
   1306 /**
   1307  * camera3_jpeg_blob:
   1308  *
   1309  * Transport header for compressed JPEG buffers in output streams.
   1310  *
   1311  * To capture JPEG images, a stream is created using the pixel format
   1312  * HAL_PIXEL_FORMAT_BLOB, and the static metadata field android.jpeg.maxSize is
   1313  * used as the buffer size. Since compressed JPEG images are of variable size,
   1314  * the HAL needs to include the final size of the compressed image using this
   1315  * structure inside the output stream buffer. The JPEG blob ID field must be set
   1316  * to CAMERA3_JPEG_BLOB_ID.
   1317  *
   1318  * Transport header should be at the end of the JPEG output stream buffer.  That
   1319  * means the jpeg_blob_id must start at byte[android.jpeg.maxSize -
   1320  * sizeof(camera3_jpeg_blob)].  Any HAL using this transport header must
   1321  * account for it in android.jpeg.maxSize.  The JPEG data itself starts at
   1322  * the beginning of the buffer and should be jpeg_size bytes long.
   1323  */
   1324 typedef struct camera3_jpeg_blob {
   1325     uint16_t jpeg_blob_id;
   1326     uint32_t jpeg_size;
   1327 } camera3_jpeg_blob_t;
   1328 
   1329 enum {
   1330     CAMERA3_JPEG_BLOB_ID = 0x00FF
   1331 };
   1332 
   1333 /**********************************************************************
   1334  *
   1335  * Message definitions for the HAL notify() callback.
   1336  *
   1337  * These definitions are used for the HAL notify callback, to signal
   1338  * asynchronous events from the HAL device to the Android framework.
   1339  *
   1340  */
   1341 
   1342 /**
   1343  * camera3_msg_type:
   1344  *
   1345  * Indicates the type of message sent, which specifies which member of the
   1346  * message union is valid.
   1347  *
   1348  */
   1349 typedef enum camera3_msg_type {
   1350     /**
   1351      * An error has occurred. camera3_notify_msg.message.error contains the
   1352      * error information.
   1353      */
   1354     CAMERA3_MSG_ERROR = 1,
   1355 
   1356     /**
   1357      * The exposure of a given request has
   1358      * begun. camera3_notify_msg.message.shutter contains the information
   1359      * the capture.
   1360      */
   1361     CAMERA3_MSG_SHUTTER = 2,
   1362 
   1363     /**
   1364      * Number of framework message types
   1365      */
   1366     CAMERA3_NUM_MESSAGES
   1367 
   1368 } camera3_msg_type_t;
   1369 
   1370 /**
   1371  * Defined error codes for CAMERA_MSG_ERROR
   1372  */
   1373 typedef enum camera3_error_msg_code {
   1374     /**
   1375      * A serious failure occured. No further frames or buffer streams will
   1376      * be produced by the device. Device should be treated as closed. The
   1377      * client must reopen the device to use it again. The frame_number field
   1378      * is unused.
   1379      */
   1380     CAMERA3_MSG_ERROR_DEVICE = 1,
   1381 
   1382     /**
   1383      * An error has occurred in processing a request. No output (metadata or
   1384      * buffers) will be produced for this request. The frame_number field
   1385      * specifies which request has been dropped. Subsequent requests are
   1386      * unaffected, and the device remains operational.
   1387      */
   1388     CAMERA3_MSG_ERROR_REQUEST = 2,
   1389 
   1390     /**
   1391      * An error has occurred in producing an output result metadata buffer
   1392      * for a request, but output stream buffers for it will still be
   1393      * available. Subsequent requests are unaffected, and the device remains
   1394      * operational.  The frame_number field specifies the request for which
   1395      * result metadata won't be available.
   1396      */
   1397     CAMERA3_MSG_ERROR_RESULT = 3,
   1398 
   1399     /**
   1400      * An error has occurred in placing an output buffer into a stream for a
   1401      * request. The frame metadata and other buffers may still be
   1402      * available. Subsequent requests are unaffected, and the device remains
   1403      * operational. The frame_number field specifies the request for which the
   1404      * buffer was dropped, and error_stream contains a pointer to the stream
   1405      * that dropped the frame.u
   1406      */
   1407     CAMERA3_MSG_ERROR_BUFFER = 4,
   1408 
   1409     /**
   1410      * Number of error types
   1411      */
   1412     CAMERA3_MSG_NUM_ERRORS
   1413 
   1414 } camera3_error_msg_code_t;
   1415 
   1416 /**
   1417  * camera3_error_msg_t:
   1418  *
   1419  * Message contents for CAMERA3_MSG_ERROR
   1420  */
   1421 typedef struct camera3_error_msg {
   1422     /**
   1423      * Frame number of the request the error applies to. 0 if the frame number
   1424      * isn't applicable to the error.
   1425      */
   1426     uint32_t frame_number;
   1427 
   1428     /**
   1429      * Pointer to the stream that had a failure. NULL if the stream isn't
   1430      * applicable to the error.
   1431      */
   1432     camera3_stream_t *error_stream;
   1433 
   1434     /**
   1435      * The code for this error; one of the CAMERA_MSG_ERROR enum values.
   1436      */
   1437     int error_code;
   1438 
   1439 } camera3_error_msg_t;
   1440 
   1441 /**
   1442  * camera3_shutter_msg_t:
   1443  *
   1444  * Message contents for CAMERA3_MSG_SHUTTER
   1445  */
   1446 typedef struct camera3_shutter_msg {
   1447     /**
   1448      * Frame number of the request that has begun exposure
   1449      */
   1450     uint32_t frame_number;
   1451 
   1452     /**
   1453      * Timestamp for the start of capture. This must match the capture result
   1454      * metadata's sensor exposure start timestamp.
   1455      */
   1456     uint64_t timestamp;
   1457 
   1458 } camera3_shutter_msg_t;
   1459 
   1460 /**
   1461  * camera3_notify_msg_t:
   1462  *
   1463  * The message structure sent to camera3_callback_ops_t.notify()
   1464  */
   1465 typedef struct camera3_notify_msg {
   1466 
   1467     /**
   1468      * The message type. One of camera3_notify_msg_type, or a private extension.
   1469      */
   1470     int type;
   1471 
   1472     union {
   1473         /**
   1474          * Error message contents. Valid if type is CAMERA3_MSG_ERROR
   1475          */
   1476         camera3_error_msg_t error;
   1477 
   1478         /**
   1479          * Shutter message contents. Valid if type is CAMERA3_MSG_SHUTTER
   1480          */
   1481         camera3_shutter_msg_t shutter;
   1482 
   1483         /**
   1484          * Generic message contents. Used to ensure a minimum size for custom
   1485          * message types.
   1486          */
   1487         uint8_t generic[32];
   1488     } message;
   1489 
   1490 } camera3_notify_msg_t;
   1491 
   1492 /**********************************************************************
   1493  *
   1494  * Capture request/result definitions for the HAL process_capture_request()
   1495  * method, and the process_capture_result() callback.
   1496  *
   1497  */
   1498 
   1499 /**
   1500  * camera3_request_template_t:
   1501  *
   1502  * Available template types for
   1503  * camera3_device_ops.construct_default_request_settings()
   1504  */
   1505 typedef enum camera3_request_template {
   1506     /**
   1507      * Standard camera preview operation with 3A on auto.
   1508      */
   1509     CAMERA3_TEMPLATE_PREVIEW = 1,
   1510 
   1511     /**
   1512      * Standard camera high-quality still capture with 3A and flash on auto.
   1513      */
   1514     CAMERA3_TEMPLATE_STILL_CAPTURE = 2,
   1515 
   1516     /**
   1517      * Standard video recording plus preview with 3A on auto, torch off.
   1518      */
   1519     CAMERA3_TEMPLATE_VIDEO_RECORD = 3,
   1520 
   1521     /**
   1522      * High-quality still capture while recording video. Application will
   1523      * include preview, video record, and full-resolution YUV or JPEG streams in
   1524      * request. Must not cause stuttering on video stream. 3A on auto.
   1525      */
   1526     CAMERA3_TEMPLATE_VIDEO_SNAPSHOT = 4,
   1527 
   1528     /**
   1529      * Zero-shutter-lag mode. Application will request preview and
   1530      * full-resolution data for each frame, and reprocess it to JPEG when a
   1531      * still image is requested by user. Settings should provide highest-quality
   1532      * full-resolution images without compromising preview frame rate. 3A on
   1533      * auto.
   1534      */
   1535     CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG = 5,
   1536 
   1537     /* Total number of templates */
   1538     CAMERA3_TEMPLATE_COUNT,
   1539 
   1540     /**
   1541      * First value for vendor-defined request templates
   1542      */
   1543     CAMERA3_VENDOR_TEMPLATE_START = 0x40000000
   1544 
   1545 } camera3_request_template_t;
   1546 
   1547 /**
   1548  * camera3_capture_request_t:
   1549  *
   1550  * A single request for image capture/buffer reprocessing, sent to the Camera
   1551  * HAL device by the framework in process_capture_request().
   1552  *
   1553  * The request contains the settings to be used for this capture, and the set of
   1554  * output buffers to write the resulting image data in. It may optionally
   1555  * contain an input buffer, in which case the request is for reprocessing that
   1556  * input buffer instead of capturing a new image with the camera sensor. The
   1557  * capture is identified by the frame_number.
   1558  *
   1559  * In response, the camera HAL device must send a camera3_capture_result
   1560  * structure asynchronously to the framework, using the process_capture_result()
   1561  * callback.
   1562  */
   1563 typedef struct camera3_capture_request {
   1564     /**
   1565      * The frame number is an incrementing integer set by the framework to
   1566      * uniquely identify this capture. It needs to be returned in the result
   1567      * call, and is also used to identify the request in asynchronous
   1568      * notifications sent to camera3_callback_ops_t.notify().
   1569      */
   1570     uint32_t frame_number;
   1571 
   1572     /**
   1573      * The settings buffer contains the capture and processing parameters for
   1574      * the request. As a special case, a NULL settings buffer indicates that the
   1575      * settings are identical to the most-recently submitted capture request. A
   1576      * NULL buffer cannot be used as the first submitted request after a
   1577      * configure_streams() call.
   1578      */
   1579     const camera_metadata_t *settings;
   1580 
   1581     /**
   1582      * The input stream buffer to use for this request, if any.
   1583      *
   1584      * If input_buffer is NULL, then the request is for a new capture from the
   1585      * imager. If input_buffer is valid, the request is for reprocessing the
   1586      * image contained in input_buffer.
   1587      *
   1588      * In the latter case, the HAL must set the release_fence of the
   1589      * input_buffer to a valid sync fence, or to -1 if the HAL does not support
   1590      * sync, before process_capture_request() returns.
   1591      *
   1592      * The HAL is required to wait on the acquire sync fence of the input buffer
   1593      * before accessing it.
   1594      *
   1595      * Any input buffer included here will have been registered with the HAL
   1596      * through register_stream_buffers() before its inclusion in a request.
   1597      */
   1598     camera3_stream_buffer_t *input_buffer;
   1599 
   1600     /**
   1601      * The number of output buffers for this capture request. Must be at least
   1602      * 1.
   1603      */
   1604     uint32_t num_output_buffers;
   1605 
   1606     /**
   1607      * An array of num_output_buffers stream buffers, to be filled with image
   1608      * data from this capture/reprocess. The HAL must wait on the acquire fences
   1609      * of each stream buffer before writing to them. All the buffers included
   1610      * here will have been registered with the HAL through
   1611      * register_stream_buffers() before their inclusion in a request.
   1612      *
   1613      * The HAL takes ownership of the actual buffer_handle_t entries in
   1614      * output_buffers; the framework does not access them until they are
   1615      * returned in a camera3_capture_result_t.
   1616      */
   1617     const camera3_stream_buffer_t *output_buffers;
   1618 
   1619 } camera3_capture_request_t;
   1620 
   1621 /**
   1622  * camera3_capture_result_t:
   1623  *
   1624  * The result of a single capture/reprocess by the camera HAL device. This is
   1625  * sent to the framework asynchronously with process_capture_result(), in
   1626  * response to a single capture request sent to the HAL with
   1627  * process_capture_request(). Multiple process_capture_result() calls may be
   1628  * performed by the HAL for each request. Each call, all with the same frame
   1629  * number, may contain some subset of the output buffers, and/or the result
   1630  * metadata. The metadata may only be provided once for a given frame number;
   1631  * all other calls must set the result metadata to NULL.
   1632  *
   1633  * The result structure contains the output metadata from this capture, and the
   1634  * set of output buffers that have been/will be filled for this capture. Each
   1635  * output buffer may come with a release sync fence that the framework will wait
   1636  * on before reading, in case the buffer has not yet been filled by the HAL.
   1637  *
   1638  */
   1639 typedef struct camera3_capture_result {
   1640     /**
   1641      * The frame number is an incrementing integer set by the framework in the
   1642      * submitted request to uniquely identify this capture. It is also used to
   1643      * identify the request in asynchronous notifications sent to
   1644      * camera3_callback_ops_t.notify().
   1645     */
   1646     uint32_t frame_number;
   1647 
   1648     /**
   1649      * The result metadata for this capture. This contains information about the
   1650      * final capture parameters, the state of the capture and post-processing
   1651      * hardware, the state of the 3A algorithms, if enabled, and the output of
   1652      * any enabled statistics units.
   1653      *
   1654      * Only one call to process_capture_result() with a given frame_number may
   1655      * include the result metadata. All other calls for the same frame_number
   1656      * must set this to NULL.
   1657      *
   1658      * If there was an error producing the result metadata, result must be an
   1659      * empty metadata buffer, and notify() must be called with ERROR_RESULT.
   1660      */
   1661     const camera_metadata_t *result;
   1662 
   1663     /**
   1664      * The number of output buffers returned in this result structure. Must be
   1665      * less than or equal to the matching capture request's count. If this is
   1666      * less than the buffer count in the capture request, at least one more call
   1667      * to process_capture_result with the same frame_number must be made, to
   1668      * return the remaining output buffers to the framework. This may only be
   1669      * zero if the structure includes valid result metadata.
   1670      */
   1671     uint32_t num_output_buffers;
   1672 
   1673     /**
   1674      * The handles for the output stream buffers for this capture. They may not
   1675      * yet be filled at the time the HAL calls process_capture_result(); the
   1676      * framework will wait on the release sync fences provided by the HAL before
   1677      * reading the buffers.
   1678      *
   1679      * The HAL must set the stream buffer's release sync fence to a valid sync
   1680      * fd, or to -1 if the buffer has already been filled.
   1681      *
   1682      * If the HAL encounters an error while processing the buffer, and the
   1683      * buffer is not filled, the buffer's status field must be set to
   1684      * CAMERA3_BUFFER_STATUS_ERROR. If the HAL did not wait on the acquire fence
   1685      * before encountering the error, the acquire fence should be copied into
   1686      * the release fence, to allow the framework to wait on the fence before
   1687      * reusing the buffer.
   1688      *
   1689      * The acquire fence must be set to -1 for all output buffers.  If
   1690      * num_output_buffers is zero, this may be NULL. In that case, at least one
   1691      * more process_capture_result call must be made by the HAL to provide the
   1692      * output buffers.
   1693      */
   1694      const camera3_stream_buffer_t *output_buffers;
   1695 
   1696 } camera3_capture_result_t;
   1697 
   1698 /**********************************************************************
   1699  *
   1700  * Callback methods for the HAL to call into the framework.
   1701  *
   1702  * These methods are used to return metadata and image buffers for a completed
   1703  * or failed captures, and to notify the framework of asynchronous events such
   1704  * as errors.
   1705  *
   1706  * The framework will not call back into the HAL from within these callbacks,
   1707  * and these calls will not block for extended periods.
   1708  *
   1709  */
   1710 typedef struct camera3_callback_ops {
   1711 
   1712     /**
   1713      * process_capture_result:
   1714      *
   1715      * Send results from a completed capture to the framework.
   1716      * process_capture_result() may be invoked multiple times by the HAL in
   1717      * response to a single capture request. This allows, for example, the
   1718      * metadata and low-resolution buffers to be returned in one call, and
   1719      * post-processed JPEG buffers in a later call, once it is available. Each
   1720      * call must include the frame number of the request it is returning
   1721      * metadata or buffers for.
   1722      *
   1723      * A component (buffer or metadata) of the complete result may only be
   1724      * included in one process_capture_result call. A buffer for each stream,
   1725      * and the result metadata, must be returned by the HAL for each request in
   1726      * one of the process_capture_result calls, even in case of errors producing
   1727      * some of the output. A call to process_capture_result() with neither
   1728      * output buffers or result metadata is not allowed.
   1729      *
   1730      * The order of returning metadata and buffers for a single result does not
   1731      * matter, but buffers for a given stream must be returned in FIFO order. So
   1732      * the buffer for request 5 for stream A must always be returned before the
   1733      * buffer for request 6 for stream A. This also applies to the result
   1734      * metadata; the metadata for request 5 must be returned before the metadata
   1735      * for request 6.
   1736      *
   1737      * However, different streams are independent of each other, so it is
   1738      * acceptable and expected that the buffer for request 5 for stream A may be
   1739      * returned after the buffer for request 6 for stream B is. And it is
   1740      * acceptable that the result metadata for request 6 for stream B is
   1741      * returned before the buffer for request 5 for stream A is.
   1742      *
   1743      * The HAL retains ownership of result structure, which only needs to be
   1744      * valid to access during this call. The framework will copy whatever it
   1745      * needs before this call returns.
   1746      *
   1747      * The output buffers do not need to be filled yet; the framework will wait
   1748      * on the stream buffer release sync fence before reading the buffer
   1749      * data. Therefore, this method should be called by the HAL as soon as
   1750      * possible, even if some or all of the output buffers are still in
   1751      * being filled. The HAL must include valid release sync fences into each
   1752      * output_buffers stream buffer entry, or -1 if that stream buffer is
   1753      * already filled.
   1754      *
   1755      * If the result buffer cannot be constructed for a request, the HAL should
   1756      * return an empty metadata buffer, but still provide the output buffers and
   1757      * their sync fences. In addition, notify() must be called with an
   1758      * ERROR_RESULT message.
   1759      *
   1760      * If an output buffer cannot be filled, its status field must be set to
   1761      * STATUS_ERROR. In addition, notify() must be called with a ERROR_BUFFER
   1762      * message.
   1763      *
   1764      * If the entire capture has failed, then this method still needs to be
   1765      * called to return the output buffers to the framework. All the buffer
   1766      * statuses should be STATUS_ERROR, and the result metadata should be an
   1767      * empty buffer. In addition, notify() must be called with a ERROR_REQUEST
   1768      * message. In this case, individual ERROR_RESULT/ERROR_BUFFER messages
   1769      * should not be sent.
   1770      *
   1771      */
   1772     void (*process_capture_result)(const struct camera3_callback_ops *,
   1773             const camera3_capture_result_t *result);
   1774 
   1775     /**
   1776      * notify:
   1777      *
   1778      * Asynchronous notification callback from the HAL, fired for various
   1779      * reasons. Only for information independent of frame capture, or that
   1780      * require specific timing. The ownership of the message structure remains
   1781      * with the HAL, and the msg only needs to be valid for the duration of this
   1782      * call.
   1783      *
   1784      * The notification for the start of exposure for a given request must be
   1785      * sent by the HAL before the first call to process_capture_result() for
   1786      * that request is made.
   1787      *
   1788      * Multiple threads may call notify() simultaneously.
   1789      */
   1790     void (*notify)(const struct camera3_callback_ops *,
   1791             const camera3_notify_msg_t *msg);
   1792 
   1793 } camera3_callback_ops_t;
   1794 
   1795 /**********************************************************************
   1796  *
   1797  * Camera device operations
   1798  *
   1799  */
   1800 typedef struct camera3_device_ops {
   1801 
   1802     /**
   1803      * initialize:
   1804      *
   1805      * One-time initialization to pass framework callback function pointers to
   1806      * the HAL. Will be called once after a successful open() call, before any
   1807      * other functions are called on the camera3_device_ops structure.
   1808      *
   1809      * Return values:
   1810      *
   1811      *  0:     On successful initialization
   1812      *
   1813      * -ENODEV: If initialization fails. Only close() can be called successfully
   1814      *          by the framework after this.
   1815      */
   1816     int (*initialize)(const struct camera3_device *,
   1817             const camera3_callback_ops_t *callback_ops);
   1818 
   1819     /**********************************************************************
   1820      * Stream management
   1821      */
   1822 
   1823     /**
   1824      * configure_streams:
   1825      *
   1826      * Reset the HAL camera device processing pipeline and set up new input and
   1827      * output streams. This call replaces any existing stream configuration with
   1828      * the streams defined in the stream_list. This method will be called at
   1829      * least once after initialize() before a request is submitted with
   1830      * process_capture_request().
   1831      *
   1832      * The stream_list must contain at least one output-capable stream, and may
   1833      * not contain more than one input-capable stream.
   1834      *
   1835      * The stream_list may contain streams that are also in the currently-active
   1836      * set of streams (from the previous call to configure_stream()). These
   1837      * streams will already have valid values for usage, max_buffers, and the
   1838      * private pointer. If such a stream has already had its buffers registered,
   1839      * register_stream_buffers() will not be called again for the stream, and
   1840      * buffers from the stream can be immediately included in input requests.
   1841      *
   1842      * If the HAL needs to change the stream configuration for an existing
   1843      * stream due to the new configuration, it may rewrite the values of usage
   1844      * and/or max_buffers during the configure call. The framework will detect
   1845      * such a change, and will then reallocate the stream buffers, and call
   1846      * register_stream_buffers() again before using buffers from that stream in
   1847      * a request.
   1848      *
   1849      * If a currently-active stream is not included in stream_list, the HAL may
   1850      * safely remove any references to that stream. It will not be reused in a
   1851      * later configure() call by the framework, and all the gralloc buffers for
   1852      * it will be freed after the configure_streams() call returns.
   1853      *
   1854      * The stream_list structure is owned by the framework, and may not be
   1855      * accessed once this call completes. The address of an individual
   1856      * camera3_stream_t structure will remain valid for access by the HAL until
   1857      * the end of the first configure_stream() call which no longer includes
   1858      * that camera3_stream_t in the stream_list argument. The HAL may not change
   1859      * values in the stream structure outside of the private pointer, except for
   1860      * the usage and max_buffers members during the configure_streams() call
   1861      * itself.
   1862      *
   1863      * If the stream is new, the usage, max_buffer, and private pointer fields
   1864      * of the stream structure will all be set to 0. The HAL device must set
   1865      * these fields before the configure_streams() call returns. These fields
   1866      * are then used by the framework and the platform gralloc module to
   1867      * allocate the gralloc buffers for each stream.
   1868      *
   1869      * Before such a new stream can have its buffers included in a capture
   1870      * request, the framework will call register_stream_buffers() with that
   1871      * stream. However, the framework is not required to register buffers for
   1872      * _all_ streams before submitting a request. This allows for quick startup
   1873      * of (for example) a preview stream, with allocation for other streams
   1874      * happening later or concurrently.
   1875      *
   1876      * Preconditions:
   1877      *
   1878      * The framework will only call this method when no captures are being
   1879      * processed. That is, all results have been returned to the framework, and
   1880      * all in-flight input and output buffers have been returned and their
   1881      * release sync fences have been signaled by the HAL. The framework will not
   1882      * submit new requests for capture while the configure_streams() call is
   1883      * underway.
   1884      *
   1885      * Postconditions:
   1886      *
   1887      * The HAL device must configure itself to provide maximum possible output
   1888      * frame rate given the sizes and formats of the output streams, as
   1889      * documented in the camera device's static metadata.
   1890      *
   1891      * Performance expectations:
   1892      *
   1893      * This call is expected to be heavyweight and possibly take several hundred
   1894      * milliseconds to complete, since it may require resetting and
   1895      * reconfiguring the image sensor and the camera processing pipeline.
   1896      * Nevertheless, the HAL device should attempt to minimize the
   1897      * reconfiguration delay to minimize the user-visible pauses during
   1898      * application operational mode changes (such as switching from still
   1899      * capture to video recording).
   1900      *
   1901      * Return values:
   1902      *
   1903      *  0:      On successful stream configuration
   1904      *
   1905      * -EINVAL: If the requested stream configuration is invalid. Some examples
   1906      *          of invalid stream configurations include:
   1907      *
   1908      *          - Including more than 1 input-capable stream (INPUT or
   1909      *            BIDIRECTIONAL)
   1910      *
   1911      *          - Not including any output-capable streams (OUTPUT or
   1912      *            BIDIRECTIONAL)
   1913      *
   1914      *          - Including streams with unsupported formats, or an unsupported
   1915      *            size for that format.
   1916      *
   1917      *          - Including too many output streams of a certain format.
   1918      *
   1919      *          Note that the framework submitting an invalid stream
   1920      *          configuration is not normal operation, since stream
   1921      *          configurations are checked before configure. An invalid
   1922      *          configuration means that a bug exists in the framework code, or
   1923      *          there is a mismatch between the HAL's static metadata and the
   1924      *          requirements on streams.
   1925      *
   1926      * -ENODEV: If there has been a fatal error and the device is no longer
   1927      *          operational. Only close() can be called successfully by the
   1928      *          framework after this error is returned.
   1929      */
   1930     int (*configure_streams)(const struct camera3_device *,
   1931             camera3_stream_configuration_t *stream_list);
   1932 
   1933     /**
   1934      * register_stream_buffers:
   1935      *
   1936      * Register buffers for a given stream with the HAL device. This method is
   1937      * called by the framework after a new stream is defined by
   1938      * configure_streams, and before buffers from that stream are included in a
   1939      * capture request. If the same stream is listed in a subsequent
   1940      * configure_streams() call, register_stream_buffers will _not_ be called
   1941      * again for that stream.
   1942      *
   1943      * The framework does not need to register buffers for all configured
   1944      * streams before it submits the first capture request. This allows quick
   1945      * startup for preview (or similar use cases) while other streams are still
   1946      * being allocated.
   1947      *
   1948      * This method is intended to allow the HAL device to map or otherwise
   1949      * prepare the buffers for later use. The buffers passed in will already be
   1950      * locked for use. At the end of the call, all the buffers must be ready to
   1951      * be returned to the stream.  The buffer_set argument is only valid for the
   1952      * duration of this call.
   1953      *
   1954      * If the stream format was set to HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
   1955      * the camera HAL should inspect the passed-in buffers here to determine any
   1956      * platform-private pixel format information.
   1957      *
   1958      * Return values:
   1959      *
   1960      *  0:      On successful registration of the new stream buffers
   1961      *
   1962      * -EINVAL: If the stream_buffer_set does not refer to a valid active
   1963      *          stream, or if the buffers array is invalid.
   1964      *
   1965      * -ENOMEM: If there was a failure in registering the buffers. The framework
   1966      *          must consider all the stream buffers to be unregistered, and can
   1967      *          try to register again later.
   1968      *
   1969      * -ENODEV: If there is a fatal error, and the device is no longer
   1970      *          operational. Only close() can be called successfully by the
   1971      *          framework after this error is returned.
   1972      */
   1973     int (*register_stream_buffers)(const struct camera3_device *,
   1974             const camera3_stream_buffer_set_t *buffer_set);
   1975 
   1976     /**********************************************************************
   1977      * Request creation and submission
   1978      */
   1979 
   1980     /**
   1981      * construct_default_request_settings:
   1982      *
   1983      * Create capture settings for standard camera use cases.
   1984      *
   1985      * The device must return a settings buffer that is configured to meet the
   1986      * requested use case, which must be one of the CAMERA3_TEMPLATE_*
   1987      * enums. All request control fields must be included.
   1988      *
   1989      * The HAL retains ownership of this structure, but the pointer to the
   1990      * structure must be valid until the device is closed. The framework and the
   1991      * HAL may not modify the buffer once it is returned by this call. The same
   1992      * buffer may be returned for subsequent calls for the same template, or for
   1993      * other templates.
   1994      *
   1995      * Return values:
   1996      *
   1997      *   Valid metadata: On successful creation of a default settings
   1998      *                   buffer.
   1999      *
   2000      *   NULL:           In case of a fatal error. After this is returned, only
   2001      *                   the close() method can be called successfully by the
   2002      *                   framework.
   2003      */
   2004     const camera_metadata_t* (*construct_default_request_settings)(
   2005             const struct camera3_device *,
   2006             int type);
   2007 
   2008     /**
   2009      * process_capture_request:
   2010      *
   2011      * Send a new capture request to the HAL. The HAL should not return from
   2012      * this call until it is ready to accept the next request to process. Only
   2013      * one call to process_capture_request() will be made at a time by the
   2014      * framework, and the calls will all be from the same thread. The next call
   2015      * to process_capture_request() will be made as soon as a new request and
   2016      * its associated buffers are available. In a normal preview scenario, this
   2017      * means the function will be called again by the framework almost
   2018      * instantly.
   2019      *
   2020      * The actual request processing is asynchronous, with the results of
   2021      * capture being returned by the HAL through the process_capture_result()
   2022      * call. This call requires the result metadata to be available, but output
   2023      * buffers may simply provide sync fences to wait on. Multiple requests are
   2024      * expected to be in flight at once, to maintain full output frame rate.
   2025      *
   2026      * The framework retains ownership of the request structure. It is only
   2027      * guaranteed to be valid during this call. The HAL device must make copies
   2028      * of the information it needs to retain for the capture processing. The HAL
   2029      * is responsible for waiting on and closing the buffers' fences and
   2030      * returning the buffer handles to the framework.
   2031      *
   2032      * The HAL must write the file descriptor for the input buffer's release
   2033      * sync fence into input_buffer->release_fence, if input_buffer is not
   2034      * NULL. If the HAL returns -1 for the input buffer release sync fence, the
   2035      * framework is free to immediately reuse the input buffer. Otherwise, the
   2036      * framework will wait on the sync fence before refilling and reusing the
   2037      * input buffer.
   2038      *
   2039      * Return values:
   2040      *
   2041      *  0:      On a successful start to processing the capture request
   2042      *
   2043      * -EINVAL: If the input is malformed (the settings are NULL when not
   2044      *          allowed, there are 0 output buffers, etc) and capture processing
   2045      *          cannot start. Failures during request processing should be
   2046      *          handled by calling camera3_callback_ops_t.notify(). In case of
   2047      *          this error, the framework will retain responsibility for the
   2048      *          stream buffers' fences and the buffer handles; the HAL should
   2049      *          not close the fences or return these buffers with
   2050      *          process_capture_result.
   2051      *
   2052      * -ENODEV: If the camera device has encountered a serious error. After this
   2053      *          error is returned, only the close() method can be successfully
   2054      *          called by the framework.
   2055      *
   2056      */
   2057     int (*process_capture_request)(const struct camera3_device *,
   2058             camera3_capture_request_t *request);
   2059 
   2060     /**********************************************************************
   2061      * Miscellaneous methods
   2062      */
   2063 
   2064     /**
   2065      * get_metadata_vendor_tag_ops:
   2066      *
   2067      * Get methods to query for vendor extension metadata tag information. The
   2068      * HAL should fill in all the vendor tag operation methods, or leave ops
   2069      * unchanged if no vendor tags are defined.
   2070      *
   2071      * The definition of vendor_tag_query_ops_t can be found in
   2072      * system/media/camera/include/system/camera_metadata.h.
   2073      *
   2074      */
   2075     void (*get_metadata_vendor_tag_ops)(const struct camera3_device*,
   2076             vendor_tag_query_ops_t* ops);
   2077 
   2078     /**
   2079      * dump:
   2080      *
   2081      * Print out debugging state for the camera device. This will be called by
   2082      * the framework when the camera service is asked for a debug dump, which
   2083      * happens when using the dumpsys tool, or when capturing a bugreport.
   2084      *
   2085      * The passed-in file descriptor can be used to write debugging text using
   2086      * dprintf() or write(). The text should be in ASCII encoding only.
   2087      */
   2088     void (*dump)(const struct camera3_device *, int fd);
   2089 
   2090     /**
   2091      * flush:
   2092      *
   2093      * Flush all currently in-process captures and all buffers in the pipeline
   2094      * on the given device. The framework will use this to dump all state as
   2095      * quickly as possible in order to prepare for a configure_streams() call.
   2096      *
   2097      * No buffers are required to be successfully returned, so every buffer
   2098      * held at the time of flush() (whether sucessfully filled or not) may be
   2099      * returned with CAMERA3_BUFFER_STATUS_ERROR. Note the HAL is still allowed
   2100      * to return valid (STATUS_OK) buffers during this call, provided they are
   2101      * succesfully filled.
   2102      *
   2103      * All requests currently in the HAL are expected to be returned as soon as
   2104      * possible.  Not-in-process requests should return errors immediately. Any
   2105      * interruptible hardware blocks should be stopped, and any uninterruptible
   2106      * blocks should be waited on.
   2107      *
   2108      * flush() should only return when there are no more outstanding buffers or
   2109      * requests left in the HAL.  The framework may call configure_streams (as
   2110      * the HAL state is now quiesced) or may issue new requests.
   2111      *
   2112      * A flush() call should only take 100ms or less. The maximum time it can
   2113      * take is 1 second.
   2114      *
   2115      * Version information:
   2116      *
   2117      *   only available if device version >= CAMERA_DEVICE_API_VERSION_3_1.
   2118      *
   2119      * Return values:
   2120      *
   2121      *  0:      On a successful flush of the camera HAL.
   2122      *
   2123      * -EINVAL: If the input is malformed (the device is not valid).
   2124      *
   2125      * -ENODEV: If the camera device has encountered a serious error. After this
   2126      *          error is returned, only the close() method can be successfully
   2127      *          called by the framework.
   2128      */
   2129     int (*flush)(const struct camera3_device *);
   2130 
   2131     /* reserved for future use */
   2132     void *reserved[8];
   2133 } camera3_device_ops_t;
   2134 
   2135 /**********************************************************************
   2136  *
   2137  * Camera device definition
   2138  *
   2139  */
   2140 typedef struct camera3_device {
   2141     /**
   2142      * common.version must equal CAMERA_DEVICE_API_VERSION_3_0 to identify this
   2143      * device as implementing version 3.0 of the camera device HAL.
   2144      */
   2145     hw_device_t common;
   2146     camera3_device_ops_t *ops;
   2147     void *priv;
   2148 } camera3_device_t;
   2149 
   2150 __END_DECLS
   2151 
   2152 #endif /* #ifdef ANDROID_INCLUDE_CAMERA3_H */
   2153