Home | History | Annotate | Download | only in HAL3
      1 /* Copyright (c) 2012-2014, 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 
     41 #include <hardware/power.h>
     42 
     43 extern "C" {
     44 #include <mm_camera_interface.h>
     45 #include <mm_jpeg_interface.h>
     46 }
     47 
     48 using namespace android;
     49 
     50 namespace qcamera {
     51 
     52 #ifndef TRUE
     53 #define TRUE 1
     54 #endif
     55 
     56 #ifndef FALSE
     57 #define FALSE 0
     58 #endif
     59 
     60 /* Time related macros */
     61 typedef int64_t nsecs_t;
     62 #define NSEC_PER_SEC 1000000000LL
     63 #define NSEC_PER_USEC 1000
     64 #define NSEC_PER_33MSEC 33000000LL
     65 
     66 class QCamera3MetadataChannel;
     67 class QCamera3PicChannel;
     68 class QCamera3HeapMemory;
     69 class QCamera3Exif;
     70 
     71 typedef struct {
     72     camera3_stream_t *stream;
     73     stream_status_t status;
     74     QCamera3Channel *channel;
     75 } stream_info_t;
     76 
     77 class QCamera3HardwareInterface {
     78 public:
     79     /* static variable and functions accessed by camera service */
     80     static camera3_device_ops_t mCameraOps;
     81     static int initialize(const struct camera3_device *,
     82                 const camera3_callback_ops_t *callback_ops);
     83     static int configure_streams(const struct camera3_device *,
     84                 camera3_stream_configuration_t *stream_list);
     85     static int register_stream_buffers(const struct camera3_device *,
     86                 const camera3_stream_buffer_set_t *buffer_set);
     87     static const camera_metadata_t* construct_default_request_settings(
     88                                 const struct camera3_device *, int type);
     89     static int process_capture_request(const struct camera3_device *,
     90                                 camera3_capture_request_t *request);
     91 
     92     static void dump(const struct camera3_device *, int fd);
     93     static int flush(const struct camera3_device *);
     94     static int close_camera_device(struct hw_device_t* device);
     95 
     96 public:
     97     QCamera3HardwareInterface(int cameraId,
     98             const camera_module_callbacks_t *callbacks);
     99     virtual ~QCamera3HardwareInterface();
    100     int openCamera(struct hw_device_t **hw_device);
    101     int getMetadata(int type);
    102     camera_metadata_t* translateCapabilityToMetadata(int type);
    103 
    104     static int getCamInfo(int cameraId, struct camera_info *info);
    105     static int initCapabilities(int cameraId);
    106     static int initStaticMetadata(int cameraId);
    107     static void makeTable(cam_dimension_t* dimTable, uint8_t size, int32_t* sizeTable);
    108     static void makeFPSTable(cam_fps_range_t* fpsTable, uint8_t size,
    109                                           int32_t* fpsRangesTable);
    110     static void makeOverridesList(cam_scene_mode_overrides_t* overridesTable, uint8_t size,
    111                                    uint8_t* overridesList, uint8_t* supported_indexes, int camera_id);
    112     static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
    113     static void convertFromRegions(cam_area_t* roi, const camera_metadata_t *settings,
    114                                    uint32_t tag);
    115     static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
    116     static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
    117     static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
    118                                             void *userdata);
    119     static int32_t getScalarFormat(int32_t format);
    120     static int32_t getSensorSensitivity(int32_t iso_mode);
    121 
    122     static double computeNoiseModelEntryS(int32_t sensitivity);
    123     static double computeNoiseModelEntryO(int32_t sensitivity);
    124 
    125     static void captureResultCb(mm_camera_super_buf_t *metadata,
    126                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
    127                 void *userdata);
    128 
    129     int initialize(const camera3_callback_ops_t *callback_ops);
    130     int configureStreams(camera3_stream_configuration_t *stream_list);
    131     int registerStreamBuffers(const camera3_stream_buffer_set_t *buffer_set);
    132     int processCaptureRequest(camera3_capture_request_t *request);
    133     void dump(int fd);
    134     int flush();
    135 
    136     int setFrameParameters(camera3_capture_request_t *request,
    137             cam_stream_ID_t streamID);
    138     int setReprocParameters(camera3_capture_request_t *request);
    139     int translateToHalMetadata(const camera3_capture_request_t *request,
    140             metadata_buffer_t *parm);
    141     camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
    142                              metadata_buffer_t *metadata);
    143 
    144     camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata,
    145                             nsecs_t timestamp, int32_t request_id,
    146                             const CameraMetadata& jpegMetadata, uint8_t pipeline_depth);
    147     int getJpegSettings(const camera_metadata_t *settings);
    148     int initParameters();
    149     void deinitParameters();
    150     QCamera3ReprocessChannel *addOfflineReprocChannel(QCamera3Channel *pInputChannel, QCamera3PicChannel *picChHandle, metadata_buffer_t *metadata);
    151     bool needRotationReprocess();
    152     bool needReprocess();
    153     bool isWNREnabled();
    154     cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
    155 
    156     void captureResultCb(mm_camera_super_buf_t *metadata,
    157                 camera3_stream_buffer_t *buffer, uint32_t frame_number);
    158 
    159     typedef struct {
    160         uint32_t fwk_name;
    161         uint8_t hal_name;
    162     } QCameraMap;
    163 
    164 private:
    165 
    166     int openCamera();
    167     int closeCamera();
    168     int AddSetMetaEntryToBatch(metadata_buffer_t *p_table,
    169                                unsigned int paramType,
    170                                uint32_t paramLength,
    171                                void *paramValue);
    172     static int8_t lookupHalName(const QCameraMap arr[],
    173                       int len, unsigned int fwk_name);
    174     static int32_t lookupFwkName(const QCameraMap arr[],
    175                       int len, int hal_name);
    176 
    177     int validateCaptureRequest(camera3_capture_request_t *request);
    178     int validateStreamDimensions(camera3_stream_configuration_t *streamList);
    179     void deriveMinFrameDuration();
    180     int64_t getMinFrameDuration(const camera3_capture_request_t *request);
    181 
    182     void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf);
    183     void handleBufferWithLock(camera3_stream_buffer_t *buffer,
    184         uint32_t frame_number);
    185     void unblockRequestIfNecessary();
    186     void dumpMetadataToFile(tuning_params_t &meta,
    187                             uint32_t &dumpFrameCount,
    188                             int32_t enabled,
    189                             const char *type,
    190                             uint32_t frameNumber);
    191 
    192     void cleanAndSortStreamInfo();
    193     void extractJpegMetadata(CameraMetadata& jpegMetadata,
    194             const camera3_capture_request_t *request);
    195 public:
    196 
    197     bool needOnlineRotation();
    198     int getJpegQuality();
    199     int calcMaxJpegSize();
    200     QCamera3Exif *getExifData();
    201 public:
    202     static int kMaxInFlight;
    203 private:
    204     camera3_device_t   mCameraDevice;
    205     uint8_t            mCameraId;
    206     mm_camera_vtbl_t  *mCameraHandle;
    207     bool               mCameraOpened;
    208     bool               mCameraInitialized;
    209     camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
    210     int mBlobRequest;
    211 
    212     const camera3_callback_ops_t *mCallbackOps;
    213 
    214     camera3_stream_t *mInputStream;
    215     QCamera3MetadataChannel *mMetadataChannel;
    216     QCamera3PicChannel *mPictureChannel;
    217     QCamera3RawChannel *mRawChannel;
    218     QCamera3SupportChannel *mSupportChannel;
    219 
    220      //First request yet to be processed after configureStreams
    221     bool mFirstRequest;
    222     bool mRepeatingRequest;
    223     QCamera3HeapMemory *mParamHeap;
    224     metadata_buffer_t* mParameters;
    225     metadata_buffer_t* mPrevParameters;
    226     bool m_bWNROn;
    227 
    228     /* Data structure to store pending request */
    229     typedef struct {
    230         camera3_stream_t *stream;
    231         camera3_stream_buffer_t *buffer;
    232     } RequestedBufferInfo;
    233     typedef struct {
    234         uint32_t frame_number;
    235         uint32_t num_buffers;
    236         int32_t request_id;
    237         List<RequestedBufferInfo> buffers;
    238         int blob_request;
    239         nsecs_t timestamp;
    240         uint8_t bNotified;
    241         int input_buffer_present;
    242         CameraMetadata jpegMetadata;
    243         uint8_t pipeline_depth;
    244         uint32_t partial_result_cnt;
    245     } PendingRequestInfo;
    246     typedef struct {
    247         uint32_t frame_number;
    248         uint32_t stream_ID;
    249     } PendingFrameDropInfo;
    250 
    251     // Store the Pending buffers for Flushing
    252     typedef struct {
    253         // Frame number pertaining to the buffer
    254         uint32_t frame_number;
    255         camera3_stream_t *stream;
    256         // Buffer handle
    257         buffer_handle_t *buffer;
    258     } PendingBufferInfo;
    259 
    260     typedef struct {
    261         // Total number of buffer requests pending
    262         uint32_t num_buffers;
    263         // List of pending buffers
    264         List<PendingBufferInfo> mPendingBufferList;
    265     } PendingBuffersMap;
    266 
    267     typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
    268 
    269     List<PendingRequestInfo> mPendingRequestsList;
    270     List<PendingFrameDropInfo> mPendingFrameDropList;
    271     PendingBuffersMap mPendingBuffersMap;
    272     pthread_cond_t mRequestCond;
    273     int mPendingRequest;
    274     int32_t mCurrentRequestId;
    275     camera3_capture_result_t *mLoopBackResult;
    276     nsecs_t mLoopBackTimestamp;
    277 
    278     //mutex for serialized access to camera3_device_ops_t functions
    279     pthread_mutex_t mMutex;
    280 
    281     List<stream_info_t*> mStreamInfo;
    282 
    283     int64_t mMinProcessedFrameDuration;
    284     int64_t mMinJpegFrameDuration;
    285     int64_t mMinRawFrameDuration;
    286 
    287     power_module_t *m_pPowerModule;   // power module
    288 
    289 #ifdef HAS_MULTIMEDIA_HINTS
    290     bool mHdrHint;
    291 #endif
    292     uint32_t mMetaFrameCount;
    293     const camera_module_callbacks_t *mCallbacks;
    294 
    295     static const QCameraMap EFFECT_MODES_MAP[];
    296     static const QCameraMap WHITE_BALANCE_MODES_MAP[];
    297     static const QCameraMap SCENE_MODES_MAP[];
    298     static const QCameraMap FOCUS_MODES_MAP[];
    299     static const QCameraMap ANTIBANDING_MODES_MAP[];
    300     static const QCameraMap AE_FLASH_MODE_MAP[];
    301     static const QCameraMap FLASH_MODES_MAP[];
    302     static const QCameraMap FACEDETECT_MODES_MAP[];
    303     static const QCameraMap FOCUS_CALIBRATION_MAP[];
    304     static const QCameraMap TEST_PATTERN_MAP[];
    305     static const QCameraMap REFERENCE_ILLUMINANT_MAP[];
    306 
    307     static pthread_mutex_t mCameraSessionLock;
    308     static unsigned int mCameraSessionActive;
    309 };
    310 
    311 }; // namespace qcamera
    312 
    313 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
    314