QCamera3HWI.h revision 9443c6d3cd31da9b89221a4261debfeddafe3b00
1/* Copyright (c) 2012-2013, 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
41extern "C" {
42#include <mm_camera_interface.h>
43#include <mm_jpeg_interface.h>
44}
45
46using namespace android;
47
48namespace qcamera {
49
50#ifndef TRUE
51#define TRUE 1
52#endif
53
54#ifndef FALSE
55#define FALSE 0
56#endif
57
58/* Time related macros */
59typedef int64_t nsecs_t;
60#define NSEC_PER_SEC 1000000000LL
61#define NSEC_PER_USEC 1000
62#define NSEC_PER_33MSEC 33000000LL
63
64class QCamera3MetadataChannel;
65class QCamera3PicChannel;
66class QCamera3HeapMemory;
67class QCamera3Exif;
68
69class QCamera3HardwareInterface {
70public:
71    /* static variable and functions accessed by camera service */
72    static camera3_device_ops_t mCameraOps;
73    static int initialize(const struct camera3_device *,
74                const camera3_callback_ops_t *callback_ops);
75    static int configure_streams(const struct camera3_device *,
76                camera3_stream_configuration_t *stream_list);
77    static int register_stream_buffers(const struct camera3_device *,
78                const camera3_stream_buffer_set_t *buffer_set);
79    static const camera_metadata_t* construct_default_request_settings(
80                                const struct camera3_device *, int type);
81    static int process_capture_request(const struct camera3_device *,
82                                camera3_capture_request_t *request);
83    static void get_metadata_vendor_tag_ops(const struct camera3_device *,
84                                               vendor_tag_query_ops_t* ops);
85    static void dump(const struct camera3_device *, int fd);
86    static int close_camera_device(struct hw_device_t* device);
87public:
88    QCamera3HardwareInterface(int cameraId);
89    virtual ~QCamera3HardwareInterface();
90    int openCamera(struct hw_device_t **hw_device);
91    int getMetadata(int type);
92    camera_metadata_t* translateCapabilityToMetadata(int type);
93
94    static int getCamInfo(int cameraId, struct camera_info *info);
95    static int initCapabilities(int cameraId);
96    static int initStaticMetadata(int cameraId);
97    static void makeTable(cam_dimension_t* dimTable, uint8_t size, int32_t* sizeTable);
98    static void makeFPSTable(cam_fps_range_t* fpsTable, uint8_t size,
99                                          int32_t* fpsRangesTable);
100    static void makeOverridesList(cam_scene_mode_overrides_t* overridesTable, uint8_t size,
101                                   uint8_t* overridesList, uint8_t* supported_indexes, int camera_id);
102    static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
103    static void convertFromRegions(cam_area_t* roi, const camera_metadata_t *settings,
104                                   uint32_t tag);
105    static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
106    static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
107    static int32_t getScalarFormat(int32_t format);
108    static int32_t getSensorSensitivity(int32_t iso_mode);
109    static void captureResultCb(mm_camera_super_buf_t *metadata,
110                camera3_stream_buffer_t *buffer, uint32_t frame_number,
111                void *userdata);
112
113    int initialize(const camera3_callback_ops_t *callback_ops);
114    int configureStreams(camera3_stream_configuration_t *stream_list);
115    int registerStreamBuffers(const camera3_stream_buffer_set_t *buffer_set);
116    int processCaptureRequest(camera3_capture_request_t *request);
117    void getMetadataVendorTagOps(vendor_tag_query_ops_t* ops);
118    void dump(int fd);
119
120    int setFrameParameters(int frame_id, const camera_metadata_t *settings);
121    int translateMetadataToParameters(const camera_metadata_t *settings);
122    camera_metadata_t* translateCbMetadataToResultMetadata(metadata_buffer_t *metadata,
123                            nsecs_t timestamp, int32_t request_id);
124    int getJpegSettings(const camera_metadata_t *settings);
125    int initParameters();
126    void deinitParameters();
127
128    void captureResultCb(mm_camera_super_buf_t *metadata,
129                camera3_stream_buffer_t *buffer, uint32_t frame_number);
130
131    typedef struct {
132        uint8_t fwk_name;
133        uint8_t hal_name;
134    } QCameraMap;
135
136private:
137
138    int openCamera();
139    int closeCamera();
140    int AddSetParmEntryToBatch(parm_buffer_t *p_table,
141                               cam_intf_parm_type_t paramType,
142                               uint32_t paramLength,
143                               void *paramValue);
144    static int8_t lookupHalName(const QCameraMap arr[],
145                      int len, int fwk_name);
146    static int8_t lookupFwkName(const QCameraMap arr[],
147                      int len, int hal_name);
148
149    int validateCaptureRequest(camera3_capture_request_t *request);
150
151public:
152
153    bool needOnlineRotation();
154    void getThumbnailSize(cam_dimension_t &dim);
155    int getJpegQuality();
156    int getJpegRotation();
157    int calcMaxJpegSize();
158    QCamera3Exif *getExifData();
159
160private:
161    camera3_device_t   mCameraDevice;
162    uint8_t            mCameraId;
163    mm_camera_vtbl_t  *mCameraHandle;
164    bool               mCameraOpened;
165    bool               mCameraInitialized;
166    camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
167
168    const camera3_callback_ops_t *mCallbackOps;
169
170    camera3_stream_t *mInputStream;
171    QCamera3MetadataChannel *mMetadataChannel;
172
173     //First request yet to be processed after configureStreams
174    bool mFirstRequest;
175    QCamera3HeapMemory *mParamHeap;
176    parm_buffer_t* mParameters;
177
178    /* Data structure to store pending request */
179    typedef struct {
180        camera3_stream_t *stream;
181        camera3_stream_buffer_t *buffer;
182    } RequestedBufferInfo;
183    typedef struct {
184        uint32_t frame_number;
185        uint32_t num_buffers;
186        int32_t request_id;
187        List<RequestedBufferInfo> buffers;
188    } PendingRequestInfo;
189    typedef KeyedVector<camera3_stream_t *, uint32_t> PendingBuffersMap;
190
191    List<PendingRequestInfo> mPendingRequestsList;
192    PendingBuffersMap mPendingBuffersMap;
193    pthread_mutex_t mRequestLock;
194    pthread_cond_t mRequestCond;
195    int mPendingRequest;
196    int32_t mCurrentRequestId;
197
198    //mutex for serialized access to camera3_device_ops_t functions
199    pthread_mutex_t mMutex;
200
201    //mutex to protect the critial section for processCaptureResult
202    pthread_mutex_t mCaptureResultLock;
203
204    jpeg_settings_t* mJpegSettings;
205    metadata_response_t mMetadataResponse;
206    List<stream_info_t*> mStreamInfo;
207
208    static const QCameraMap EFFECT_MODES_MAP[];
209    static const QCameraMap WHITE_BALANCE_MODES_MAP[];
210    static const QCameraMap SCENE_MODES_MAP[];
211    static const QCameraMap FOCUS_MODES_MAP[];
212    static const QCameraMap ANTIBANDING_MODES_MAP[];
213    static const QCameraMap AE_FLASH_MODE_MAP[];
214    static const QCameraMap FLASH_MODES_MAP[];
215
216    static pthread_mutex_t mCameraSessionLock;
217    static unsigned int mCameraSessionActive;
218};
219
220}; // namespace qcamera
221
222#endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
223