Home | History | Annotate | Download | only in HAL3
      1 /* Copyright (c) 2012-2015, The Linux Foundataion. All rights reserved.
      2 *
      3 * Redistribution and use in source and binary forms, with or without
      4 * modification, are permitted provided that the following conditions are
      5 * met:
      6 *     * Redistributions of source code must retain the above copyright
      7 *       notice, this list of conditions and the following disclaimer.
      8 *     * Redistributions in binary form must reproduce the above
      9 *       copyright notice, this list of conditions and the following
     10 *       disclaimer in the documentation and/or other materials provided
     11 *       with the distribution.
     12 *     * Neither the name of The Linux Foundation nor the names of its
     13 *       contributors may be used to endorse or promote products derived
     14 *       from this software without specific prior written permission.
     15 *
     16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 *
     28 */
     29 
     30 #ifndef __QCAMERA3HARDWAREINTERFACE_H__
     31 #define __QCAMERA3HARDWAREINTERFACE_H__
     32 
     33 #include <pthread.h>
     34 #include <utils/List.h>
     35 #include <utils/KeyedVector.h>
     36 #include <hardware/camera3.h>
     37 #include <camera/CameraMetadata.h>
     38 #include "QCamera3HALHeader.h"
     39 #include "QCamera3Channel.h"
     40 #include "QCamera3CropRegionMapper.h"
     41 #include "QCameraPerf.h"
     42 
     43 extern "C" {
     44 #include <mm_camera_interface.h>
     45 #include <mm_jpeg_interface.h>
     46 }
     47 #ifdef CDBG
     48 #undef CDBG
     49 #endif //#ifdef CDBG
     50 #define CDBG(fmt, args...) ALOGD_IF(gCamHal3LogLevel >= 2, fmt, ##args)
     51 
     52 #ifdef CDBG_HIGH
     53 #undef CDBG_HIGH
     54 #endif //#ifdef CDBG_HIGH
     55 #define CDBG_HIGH(fmt, args...) ALOGD_IF(gCamHal3LogLevel >= 1, fmt, ##args)
     56 
     57 #ifdef CDBG_FATAL_IF
     58 #undef CDBG_FATAL_IF
     59 #endif //#ifdef CDBG_FATAL_IF
     60 #define CDBG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
     61 
     62 using namespace android;
     63 
     64 namespace qcamera {
     65 
     66 #ifndef TRUE
     67 #define TRUE 1
     68 #endif
     69 
     70 #ifndef FALSE
     71 #define FALSE 0
     72 #endif
     73 
     74 /* Time related macros */
     75 typedef int64_t nsecs_t;
     76 #define NSEC_PER_SEC 1000000000LLU
     77 #define NSEC_PER_USEC 1000LLU
     78 #define NSEC_PER_33MSEC 33000000LLU
     79 
     80 typedef enum {
     81     SET_ENABLE,
     82     SET_CONTROLENABLE,
     83     SET_RELOAD_CHROMATIX,
     84     SET_STATUS,
     85 } optype_t;
     86 
     87 #define MODULE_ALL 0
     88 
     89 extern volatile uint32_t gCamHal3LogLevel;
     90 
     91 class QCamera3MetadataChannel;
     92 class QCamera3PicChannel;
     93 class QCamera3HeapMemory;
     94 class QCamera3Exif;
     95 
     96 typedef struct {
     97     camera3_stream_t *stream;
     98     camera3_stream_buffer_set_t buffer_set;
     99     stream_status_t status;
    100     int registered;
    101     QCamera3ProcessingChannel *channel;
    102 } stream_info_t;
    103 
    104 class QCamera3HardwareInterface {
    105 public:
    106     /* static variable and functions accessed by camera service */
    107     static camera3_device_ops_t mCameraOps;
    108     static int initialize(const struct camera3_device *,
    109                 const camera3_callback_ops_t *callback_ops);
    110     static int configure_streams(const struct camera3_device *,
    111                 camera3_stream_configuration_t *stream_list);
    112     static const camera_metadata_t* construct_default_request_settings(
    113                                 const struct camera3_device *, int type);
    114     static int process_capture_request(const struct camera3_device *,
    115                                 camera3_capture_request_t *request);
    116 
    117     static void dump(const struct camera3_device *, int fd);
    118     static int flush(const struct camera3_device *);
    119     static int close_camera_device(struct hw_device_t* device);
    120 
    121 public:
    122     QCamera3HardwareInterface(uint32_t cameraId,
    123             const camera_module_callbacks_t *callbacks);
    124     virtual ~QCamera3HardwareInterface();
    125     static void camEvtHandle(uint32_t camera_handle, mm_camera_event_t *evt,
    126                                           void *user_data);
    127     int openCamera(struct hw_device_t **hw_device);
    128     camera_metadata_t* translateCapabilityToMetadata(int type);
    129 
    130     static int getCamInfo(uint32_t cameraId, struct camera_info *info);
    131     static int initCapabilities(uint32_t cameraId);
    132     static int initStaticMetadata(uint32_t cameraId);
    133     static void makeTable(cam_dimension_t *dimTable, size_t size,
    134             size_t max_size, int32_t *sizeTable);
    135     static void makeFPSTable(cam_fps_range_t *fpsTable, size_t size,
    136             size_t max_size, int32_t *fpsRangesTable);
    137     static void makeOverridesList(cam_scene_mode_overrides_t *overridesTable,
    138             size_t size, size_t max_size, uint8_t *overridesList,
    139             uint8_t *supported_indexes, uint32_t camera_id);
    140     static size_t filterJpegSizes(int32_t *jpegSizes, int32_t *processedSizes,
    141             size_t processedSizesCnt, size_t maxCount, cam_rect_t active_array_size,
    142             uint8_t downscale_factor);
    143     static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
    144     static void convertFromRegions(cam_area_t &roi, const camera_metadata_t *settings,
    145                                    uint32_t tag);
    146     static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
    147     static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
    148     static int32_t getScalarFormat(int32_t format);
    149     static int32_t getSensorSensitivity(int32_t iso_mode);
    150 
    151     double computeNoiseModelEntryS(int32_t sensitivity);
    152     double computeNoiseModelEntryO(int32_t sensitivity);
    153 
    154     static void captureResultCb(mm_camera_super_buf_t *metadata,
    155                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
    156                 bool isInputBuffer, void *userdata);
    157 
    158     int initialize(const camera3_callback_ops_t *callback_ops);
    159     int configureStreams(camera3_stream_configuration_t *stream_list);
    160     int configureStreamsPerfLocked(camera3_stream_configuration_t *stream_list);
    161     int processCaptureRequest(camera3_capture_request_t *request);
    162     void dump(int fd);
    163     int flush();
    164 
    165     int setFrameParameters(camera3_capture_request_t *request,
    166             cam_stream_ID_t streamID, int blob_request, uint32_t snapshotStreamId);
    167     int32_t setReprocParameters(camera3_capture_request_t *request,
    168             metadata_buffer_t *reprocParam, uint32_t snapshotStreamId);
    169     int translateToHalMetadata(const camera3_capture_request_t *request,
    170             metadata_buffer_t *parm, uint32_t snapshotStreamId);
    171     camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
    172                              metadata_buffer_t *metadata);
    173     camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata,
    174                             nsecs_t timestamp, int32_t request_id,
    175                             const CameraMetadata& jpegMetadata, uint8_t pipeline_depth,
    176                             uint8_t capture_intent, uint8_t hybrid_ae_enable,
    177                             bool pprocDone, bool dynamic_blklvl,
    178                             bool firstMetadataInBatch);
    179     camera_metadata_t* saveRequestSettings(const CameraMetadata& jpegMetadata,
    180                             camera3_capture_request_t *request);
    181     int initParameters();
    182     void deinitParameters();
    183     QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config,
    184             QCamera3ProcessingChannel *inputChHandle);
    185     bool needRotationReprocess();
    186     bool needReprocess(uint32_t postprocess_mask);
    187     bool needJpegRotation();
    188     cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
    189     cam_denoise_process_type_t getTemporalDenoiseProcessPlate();
    190 
    191     void captureResultCb(mm_camera_super_buf_t *metadata,
    192                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
    193                 bool isInputBuffer);
    194     cam_dimension_t calcMaxJpegDim();
    195     bool needOnlineRotation();
    196     uint32_t getJpegQuality();
    197     QCamera3Exif *getExifData();
    198     mm_jpeg_exif_params_t get3AExifParams();
    199     uint8_t getMobicatMask();
    200     static void getFlashInfo(const int cameraId,
    201             bool& hasFlash,
    202             char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]);
    203     const char *getEepromVersionInfo();
    204     const uint32_t *getLdafCalib();
    205     void get3AVersion(cam_q3a_version_t &swVersion);
    206 
    207     template <typename fwkType, typename halType> struct QCameraMap {
    208         fwkType fwk_name;
    209         halType hal_name;
    210     };
    211 
    212     typedef struct {
    213         const char *const desc;
    214         cam_cds_mode_type_t val;
    215     } QCameraPropMap;
    216 
    217     bool getBlackLevelRegion(int (&opticalBlackRegions)[4]);
    218     void sendDynamicBlackLevel(float blacklevel[4], uint32_t frame_number);
    219     void sendDynamicBlackLevelWithLock(float blacklevel[4], uint32_t frame_number);
    220 
    221 private:
    222 
    223     int openCamera();
    224     int closeCamera();
    225     static size_t calcMaxJpegSize(uint32_t camera_id);
    226     cam_dimension_t getMaxRawSize(uint32_t camera_id);
    227     static void addStreamConfig(Vector<int32_t> &available_stream_configs,
    228             int32_t scalar_format, const cam_dimension_t &dim,
    229             int32_t config_type);
    230 
    231     int validateCaptureRequest(camera3_capture_request_t *request);
    232     int validateStreamDimensions(camera3_stream_configuration_t *streamList);
    233     int validateStreamRotations(camera3_stream_configuration_t *streamList);
    234     void deriveMinFrameDuration();
    235     int32_t handlePendingReprocResults(uint32_t frame_number);
    236     int64_t getMinFrameDuration(const camera3_capture_request_t *request);
    237     void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf,
    238             bool free_and_bufdone_meta_buf,
    239             bool firstMetadataInBatch);
    240     void handleBatchMetadata(mm_camera_super_buf_t *metadata_buf,
    241             bool free_and_bufdone_meta_buf);
    242     void handleBufferWithLock(camera3_stream_buffer_t *buffer,
    243             uint32_t frame_number);
    244     void handleInputBufferWithLock(uint32_t frame_number);
    245     void unblockRequestIfNecessary();
    246     void dumpMetadataToFile(tuning_params_t &meta, uint32_t &dumpFrameCount,
    247             bool enabled, const char *type, uint32_t frameNumber);
    248     static void getLogLevel();
    249 
    250     void cleanAndSortStreamInfo();
    251     void extractJpegMetadata(CameraMetadata& jpegMetadata,
    252             const camera3_capture_request_t *request);
    253 
    254     bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList,
    255             cam_stream_size_info_t stream_config_info);
    256     int32_t setMobicat();
    257 
    258     int32_t setHalFpsRange(const CameraMetadata &settings,
    259             metadata_buffer_t *hal_metadata);
    260     int32_t extractSceneMode(const CameraMetadata &frame_settings, uint8_t metaMode,
    261             metadata_buffer_t *hal_metadata);
    262     int32_t numOfSizesOnEncoder(const camera3_stream_configuration_t *streamList,
    263             const cam_dimension_t &maxViewfinderSize);
    264 
    265     void enablePowerHint();
    266     void disablePowerHint();
    267     int32_t getSensorOutputSize(cam_dimension_t &sensor_dim);
    268     int32_t dynamicUpdateMetaStreamInfo();
    269     int32_t startAllChannels();
    270     int32_t stopAllChannels();
    271     int32_t notifyErrorForPendingRequests();
    272     int32_t getReprocessibleOutputStreamId(uint32_t &id);
    273 
    274     bool isOnEncoder(const cam_dimension_t max_viewfinder_size,
    275             uint32_t width, uint32_t height);
    276     void hdrPlusPerfLock(mm_camera_super_buf_t *metadata_buf);
    277 
    278     int32_t setBundleInfo();
    279 
    280     camera3_device_t   mCameraDevice;
    281     uint32_t           mCameraId;
    282     mm_camera_vtbl_t  *mCameraHandle;
    283     bool               mCameraOpened;
    284     bool               mCameraInitialized;
    285     camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
    286     const camera3_callback_ops_t *mCallbackOps;
    287 
    288     QCamera3MetadataChannel *mMetadataChannel;
    289     QCamera3PicChannel *mPictureChannel;
    290     QCamera3RawChannel *mRawChannel;
    291     QCamera3SupportChannel *mSupportChannel;
    292     QCamera3SupportChannel *mAnalysisChannel;
    293     QCamera3RawDumpChannel *mRawDumpChannel;
    294     QCamera3RegularChannel *mDummyBatchChannel;
    295     QCameraPerfLock m_perfLock;
    296 
    297     uint32_t mChannelHandle;
    298 
    299     void saveExifParams(metadata_buffer_t *metadata);
    300     mm_jpeg_exif_params_t mExifParams;
    301 
    302      //First request yet to be processed after configureStreams
    303     bool mFirstRequest;
    304     bool mFirstConfiguration;
    305     bool mFlush;
    306     bool mEnableRawDump;
    307     QCamera3HeapMemory *mParamHeap;
    308     metadata_buffer_t* mParameters;
    309     metadata_buffer_t* mPrevParameters;
    310     CameraMetadata mCurJpegMeta;
    311     bool m_bIsVideo;
    312     bool m_bIs4KVideo;
    313     bool m_bEisSupportedSize;
    314     bool m_bEisEnable;
    315     typedef struct {
    316         cam_dimension_t dim;
    317         int format;
    318         uint32_t usage;
    319     } InputStreamInfo;
    320 
    321     InputStreamInfo mInputStreamInfo;
    322     uint8_t m_MobicatMask;
    323     uint8_t mSupportedFaceDetectMode;
    324     uint8_t m_bTnrEnabled;
    325     uint8_t m_bTnrPreview;
    326     uint8_t m_bTnrVideo;
    327     cam_cds_mode_type_t m_CdsPreference;
    328     /* Data structure to store pending request */
    329     typedef struct {
    330         camera3_stream_t *stream;
    331         camera3_stream_buffer_t *buffer;
    332         // metadata needs to be consumed by the corresponding stream
    333         // in order to generate the buffer.
    334         bool need_metadata;
    335     } RequestedBufferInfo;
    336     typedef struct {
    337         uint32_t frame_number;
    338         uint32_t num_buffers;
    339         int32_t request_id;
    340         List<RequestedBufferInfo> buffers;
    341         int blob_request;
    342         uint8_t bUrgentReceived;
    343         nsecs_t timestamp;
    344         camera3_stream_buffer_t *input_buffer;
    345         const camera_metadata_t *settings;
    346         CameraMetadata jpegMetadata;
    347         uint8_t pipeline_depth;
    348         uint32_t partial_result_cnt;
    349         uint8_t capture_intent;
    350         bool shutter_notified;
    351         uint8_t hybrid_ae_enable;
    352         bool need_dynamic_blklvl;
    353         bool pending_extra_result;
    354     } PendingRequestInfo;
    355     typedef struct {
    356         uint32_t frame_number;
    357         uint32_t stream_ID;
    358     } PendingFrameDropInfo;
    359 
    360     // Store the Pending buffers for Flushing
    361     typedef struct {
    362         // Frame number pertaining to the buffer
    363         uint32_t frame_number;
    364         camera3_stream_t *stream;
    365         // Buffer handle
    366         buffer_handle_t *buffer;
    367 
    368     } PendingBufferInfo;
    369 
    370     typedef struct {
    371         // Total number of buffer requests pending
    372         uint32_t num_buffers;
    373         // List of pending buffers
    374         List<PendingBufferInfo> mPendingBufferList;
    375         // Last frame number requested
    376         uint32_t last_frame_number;
    377     } PendingBuffersMap;
    378 
    379     typedef struct {
    380         camera3_notify_msg_t notify_msg;
    381         camera3_stream_buffer_t buffer;
    382         uint32_t frame_number;
    383     } PendingReprocessResult;
    384 
    385     typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
    386     typedef List<QCamera3HardwareInterface::PendingRequestInfo>::iterator
    387             pendingRequestIterator;
    388     typedef List<QCamera3HardwareInterface::RequestedBufferInfo>::iterator
    389             pendingBufferIterator;
    390 
    391     List<PendingReprocessResult> mPendingReprocessResultList;
    392     List<PendingRequestInfo> mPendingRequestsList;
    393     List<PendingFrameDropInfo> mPendingFrameDropList;
    394     /* Use last frame number of the batch as key and first frame number of the
    395      * batch as value for that key */
    396     KeyedVector<uint32_t, uint32_t> mPendingBatchMap;
    397 
    398     PendingBuffersMap mPendingBuffersMap;
    399     pthread_cond_t mRequestCond;
    400     uint32_t mPendingLiveRequest;
    401     bool mWokenUpByDaemon;
    402     int32_t mCurrentRequestId;
    403     cam_stream_size_info_t mStreamConfigInfo;
    404 
    405     //mutex for serialized access to camera3_device_ops_t functions
    406     pthread_mutex_t mMutex;
    407 
    408     List<stream_info_t*> mStreamInfo;
    409 
    410     int64_t mMinProcessedFrameDuration;
    411     int64_t mMinJpegFrameDuration;
    412     int64_t mMinRawFrameDuration;
    413 
    414     uint32_t mMetaFrameCount;
    415     bool    mUpdateDebugLevel;
    416     const camera_module_callbacks_t *mCallbacks;
    417 
    418     uint8_t mCaptureIntent;
    419     uint8_t mHybridAeEnable;
    420     metadata_buffer_t mReprocMeta; //scratch meta buffer
    421     /* 0: Not batch, non-zero: Number of image buffers in a batch */
    422     uint8_t mBatchSize;
    423     // Used only in batch mode
    424     uint8_t mToBeQueuedVidBufs;
    425     // Fixed video fps
    426     float mHFRVideoFps;
    427     uint8_t mOpMode;
    428     uint32_t mFirstFrameNumberInBatch;
    429     camera3_stream_t mDummyBatchStream;
    430     bool mNeedSensorRestart;
    431 
    432     /* sensor output size with current stream configuration */
    433     QCamera3CropRegionMapper mCropRegionMapper;
    434 
    435     /* Ldaf calibration data */
    436     bool mLdafCalibExist;
    437     uint32_t mLdafCalib[2];
    438     bool mPowerHintEnabled;
    439     int32_t mLastCustIntentFrmNum;
    440     CameraMetadata  mCachedMetadata;
    441 
    442     static const QCameraMap<camera_metadata_enum_android_control_effect_mode_t,
    443             cam_effect_mode_type> EFFECT_MODES_MAP[];
    444     static const QCameraMap<camera_metadata_enum_android_control_awb_mode_t,
    445             cam_wb_mode_type> WHITE_BALANCE_MODES_MAP[];
    446     static const QCameraMap<camera_metadata_enum_android_control_scene_mode_t,
    447             cam_scene_mode_type> SCENE_MODES_MAP[];
    448     static const QCameraMap<camera_metadata_enum_android_control_af_mode_t,
    449             cam_focus_mode_type> FOCUS_MODES_MAP[];
    450     static const QCameraMap<camera_metadata_enum_android_color_correction_aberration_mode_t,
    451             cam_aberration_mode_t> COLOR_ABERRATION_MAP[];
    452     static const QCameraMap<camera_metadata_enum_android_control_ae_antibanding_mode_t,
    453             cam_antibanding_mode_type> ANTIBANDING_MODES_MAP[];
    454     static const QCameraMap<camera_metadata_enum_android_lens_state_t,
    455             cam_af_lens_state_t> LENS_STATE_MAP[];
    456     static const QCameraMap<camera_metadata_enum_android_control_ae_mode_t,
    457             cam_flash_mode_t> AE_FLASH_MODE_MAP[];
    458     static const QCameraMap<camera_metadata_enum_android_flash_mode_t,
    459             cam_flash_mode_t> FLASH_MODES_MAP[];
    460     static const QCameraMap<camera_metadata_enum_android_statistics_face_detect_mode_t,
    461             cam_face_detect_mode_t> FACEDETECT_MODES_MAP[];
    462     static const QCameraMap<camera_metadata_enum_android_lens_info_focus_distance_calibration_t,
    463             cam_focus_calibration_t> FOCUS_CALIBRATION_MAP[];
    464     static const QCameraMap<camera_metadata_enum_android_sensor_test_pattern_mode_t,
    465             cam_test_pattern_mode_t> TEST_PATTERN_MAP[];
    466     static const QCameraMap<camera_metadata_enum_android_sensor_reference_illuminant1_t,
    467             cam_illuminat_t> REFERENCE_ILLUMINANT_MAP[];
    468     static const QCameraMap<int32_t,
    469             cam_hfr_mode_t> HFR_MODE_MAP[];
    470 
    471     static const QCameraPropMap CDS_MAP[];
    472 
    473     pendingRequestIterator erasePendingRequest(pendingRequestIterator i);
    474 };
    475 
    476 }; // namespace qcamera
    477 
    478 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
    479