PageRenderTime 81ms CodeModel.GetById 16ms app.highlight 60ms RepoModel.GetById 1ms app.codeStats 1ms

/libcamera/QualcommCameraHardware.h

http://github.com/CyanogenMod/android_device_zte_blade
C Header | 639 lines | 510 code | 88 blank | 41 comment | 6 complexity | 70514aa6bb66706f8211a95d84b3e6d5 MD5 | raw file
  1/*
  2** Copyright 2008, Google Inc.
  3** Copyright (c) 2009, Code Aurora Forum. All rights reserved.
  4** Copyright (c) 2010, Ricardo Cerqueira
  5**
  6** Licensed under the Apache License, Version 2.0 (the "License");
  7** you may not use this file except in compliance with the License.
  8** You may obtain a copy of the License at
  9**
 10**     http://www.apache.org/licenses/LICENSE-2.0
 11**
 12** Unless required by applicable law or agreed to in writing, software
 13** distributed under the License is distributed on an "AS IS" BASIS,
 14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15** See the License for the specific language governing permissions and
 16** limitations under the License.
 17**
 18** NOTICE - (RC)
 19**
 20** All alterations done to this file to add support for the Z71 terminal
 21** are intended for use with CyanogenMod. This includes all the support
 22** for ov5642, and the reverse engineered bits like ioctls and EXIF 
 23** referred to below as "Values originally from proprietary headers")
 24** Please do not change the EXIF header without asking me first.
 25*/
 26
 27#ifndef ANDROID_HARDWARE_QUALCOMM_CAMERA_HARDWARE_H
 28#define ANDROID_HARDWARE_QUALCOMM_CAMERA_HARDWARE_H
 29
 30#include <camera/CameraHardwareInterface.h>
 31#include <binder/MemoryBase.h>
 32#include <binder/MemoryHeapBase.h>
 33#include <stdint.h>
 34#include <ui/Overlay.h>
 35
 36extern "C" {
 37#include <linux/android_pmem.h>
 38#include <msm_camera.h>
 39}
 40
 41struct str_map {
 42    const char *const desc;
 43    int val;
 44};
 45
 46typedef enum {
 47    TARGET_MSM7625,
 48    TARGET_MSM7627,
 49    TARGET_QSD8250,
 50    TARGET_MSM7630,
 51    TARGET_MAX
 52}targetType;
 53
 54struct target_map {
 55    const char *targetStr;
 56    targetType targetEnum;
 57};
 58
 59struct board_property{
 60    targetType target;
 61    unsigned int previewSizeMask;
 62};
 63
 64/* Values originally in proprietary headers */
 65
 66#define MSM_CAMERA_CONTROL "/dev/msm_camera/control0"
 67
 68#define TRUE 1
 69#define FALSE 0
 70
 71#define CAMERA_MIN_CONTRAST 0
 72#define CAMERA_MAX_CONTRAST 4
 73#define CAMERA_MIN_SHARPNESS 0
 74#define CAMERA_MIN_EXPOSURE_COMPENSATION -2
 75#define CAMERA_MAX_SHARPNESS 4
 76#define CAMERA_MIN_SATURATION 0
 77#define CAMERA_MAX_SATURATION 4
 78#define CAMERA_MAX_EXPOSURE_COMPENSATION 2
 79#define CAMERA_DEF_SHARPNESS 2
 80#define CAMERA_DEF_CONTRAST 2
 81#define CAMERA_DEF_SATURATION 2
 82#define CAMERA_DEF_EXPOSURE_COMPENSATION "0"
 83#define CAMERA_EXPOSURE_COMPENSATION_STEP 1
 84
 85#define CEILING16(x) (x&0xfffffff0)
 86#define PAD_TO_WORD(x) ((x&1) ? x+1 : x)
 87
 88#define JPEG_EVENT_DONE 0
 89#define CAM_CTRL_SUCCESS 1
 90
 91#define MINIMUM_FPS 10
 92#define DEFAULT_FPS 15
 93#define MAXIMUM_FPS 25
 94
 95typedef struct {
 96	unsigned int in1_w;
 97	unsigned int in1_h;
 98	unsigned int out1_w;
 99	unsigned int out1_h;
100	unsigned int in2_w;
101	unsigned int in2_h;
102	unsigned int out2_w;
103	unsigned int out2_h;
104	uint8_t update_flag; 
105} common_crop_t;
106
107typedef uint8_t cam_ctrl_type;
108
109typedef struct {
110	unsigned short video_width;
111	unsigned short video_height;
112	unsigned short picture_width;
113	unsigned short picture_height;
114	unsigned short display_width;
115	unsigned short display_height;
116	unsigned short orig_picture_dx;
117	unsigned short orig_picture_dy;
118	unsigned short ui_thumbnail_height;
119	unsigned short ui_thumbnail_width;
120	unsigned short thumbnail_width;
121	unsigned short thumbnail_height;
122	unsigned short raw_picture_height;
123	unsigned short raw_picture_width;
124	unsigned short filler7;
125	unsigned short filler8;
126} cam_ctrl_dimension_t;
127
128typedef struct {
129	uint32_t timestamp;  /* seconds since 1/6/1980          */
130	double   latitude;   /* degrees, WGS ellipsoid */
131	double   longitude;  /* degrees                */
132	int16_t  altitude;   /* meters                          */
133} camera_position_type;
134typedef uint8_t jpeg_event_t;
135
136typedef enum {
137	CAMERA_WB_MIN_MINUS_1,
138	CAMERA_WB_AUTO = 1,  /* This list must match aeecamera.h */
139	CAMERA_WB_CUSTOM,
140	CAMERA_WB_INCANDESCENT,
141	CAMERA_WB_FLUORESCENT,
142	CAMERA_WB_DAYLIGHT,
143	CAMERA_WB_CLOUDY_DAYLIGHT,
144	CAMERA_WB_TWILIGHT,
145	CAMERA_WB_SHADE,
146	CAMERA_WB_MAX_PLUS_1
147} camera_wb_type;
148
149typedef enum {
150    CAMERA_ANTIBANDING_OFF,
151    CAMERA_ANTIBANDING_60HZ,
152    CAMERA_ANTIBANDING_50HZ,
153    CAMERA_ANTIBANDING_AUTO,
154    CAMERA_MAX_ANTIBANDING,
155} camera_antibanding_type;
156
157typedef enum {
158	AF_MODE_NORMAL,
159	AF_MODE_MACRO,
160	AF_MODE_AUTO,
161} isp3a_af_mode_t;
162
163enum {
164	CAMERA_AEC_FRAME_AVERAGE,
165	CAMERA_AEC_CENTER_WEIGHTED,
166	CAMERA_AEC_SPOT_METERING,
167};
168
169enum {
170	LED_MODE_OFF,
171	LED_MODE_AUTO,
172	LED_MODE_ON,
173};
174
175typedef enum {
176	CAMERA_ISO_AUTO,
177	CAMERA_ISO_DEBLUR,
178	CAMERA_ISO_100,
179	CAMERA_ISO_200,
180	CAMERA_ISO_400,
181	CAMERA_ISO_800,
182	CAMERA_ISO_1600,
183} camera_iso_mode_type;
184
185struct fifo_queue {
186	int num_of_frames;
187	int front;
188	struct fifo_node *node;
189	pthread_mutex_t mut;
190	pthread_cond_t wait;
191};
192
193struct fifo_node {
194	struct msm_frame *f;
195	struct fifo_node *next;
196};
197
198void enqueue(struct fifo_queue *queue, struct fifo_node *node) {
199	struct fifo_node *cur_node=queue->node;
200	int i;
201	LOGE("enqueue:%p(%d)\n", node, queue->num_of_frames);
202	node->next=NULL;
203	if(queue->num_of_frames==0) {
204		queue->num_of_frames++;
205		queue->front=!!queue->num_of_frames;
206		queue->node=node;
207		return;
208	}
209	queue->num_of_frames++;
210	queue->front=!!queue->num_of_frames;
211	for(i=0;i<(queue->num_of_frames-2);++i) {
212		cur_node=cur_node->next;
213		assert(!!cur_node);
214	}
215	cur_node->next=node;
216}
217
218struct fifo_node *dequeue(struct fifo_queue *queue) {
219	if(queue->num_of_frames==0)
220		return NULL;
221	struct fifo_node *node=queue->node;
222	LOGE("dequeue:%p(%d)\n", node, queue->num_of_frames);
223	queue->num_of_frames--;
224	queue->front=!!queue->num_of_frames;
225	queue->node=queue->node->next;
226	return node;
227}
228
229
230enum camera_ops {
231    CAMERA_SET_PARM_ENCODE_ROTATION,
232    CAMERA_SET_PARM_DIMENSION,
233    CAMERA_SET_PARM_ZOOM,
234    CAMERA_SET_PARM_SENSOR_POSITION,
235    CAMERA_SET_PARM_SHARPNESS,
236    CAMERA_SET_PARM_LUMA_ADAPTATION,
237    CAMERA_SET_PARM_CONTRAST,
238    CAMERA_SET_PARM_EXPOSURE_COMPENSATION,
239    CAMERA_SET_PARM_BRIGHTNESS,
240    CAMERA_SET_PARM_FOCUS_RECT,
241    CAMERA_SET_PARM_HUE,
242    CAMERA_SET_PARM_SATURATION,
243    CAMERA_SET_PARM_EXPOSURE,
244    CAMERA_SET_PARM_AUTO_FOCUS,
245    CAMERA_SET_PARM_WB,
246    CAMERA_SET_PARM_EFFECT,
247    CAMERA_SET_PARM_FPS,
248    CAMERA_SET_PARM_FLASH,
249    CAMERA_SET_PARM_NIGHTSHOT_MODE,
250    CAMERA_SET_PARM_REFLECT,
251    CAMERA_SET_PARM_PREVIEW_MODE,
252    CAMERA_SET_PARM_ANTIBANDING,
253    CAMERA_SET_PARM_RED_EYE_REDUCTION,
254    CAMERA_SET_PARM_FOCUS_STEP,
255    CAMERA_SET_PARM_EXPOSURE_METERING,
256    CAMERA_SET_PARM_AUTO_EXPOSURE_MODE,
257    CAMERA_SET_PARM_ISO,
258    CAMERA_SET_PARM_BESTSHOT_MODE,
259    CAMERA_SET_PARM_PREVIEW_FPS,
260    CAMERA_SET_PARM_AF_MODE,
261    CAMERA_SET_PARM_HISTOGRAM,
262    CAMERA_SET_PARM_FLASH_STATE,
263    CAMERA_SET_PARM_FRAME_TIMESTAMP,
264    CAMERA_SET_PARM_STROBE_FLASH,
265    CAMERA_SET_PARM_FPS_LIST,
266    CAMERA_SET_PARM_HJR,
267    CAMERA_SET_PARM_ROLLOFF=37,
268    CAMERA_STOP_PREVIEW=38,
269    CAMERA_START_PREVIEW,
270    CAMERA_START_SNAPSHOT,
271    CAMERA_START_VIDEO,
272    CAMERA_STOP_SNAPSHOT=42,
273    CAMERA_EXIT=43,
274    CAMERA_STOP_VIDEO,
275    CAMERA_START_RECORDING,
276    CAMERA_STOP_RECORDING,
277    CAMERA_GET_PARM_MAXZOOM,
278    CAMERA_START_RAW_SNAPSHOT,
279    CAMERA_SET_PARM_LED_MODE,
280    CAMERA_GET_PARM_AF_SHARPNESS,
281    CAMERA_SET_MOTION_ISO,
282    CAMERA_AUTO_FOCUS_CANCEL,
283    CAMERA_GET_PARM_FOCUS_STEP,
284    CAMERA_ENABLE_AFD,
285    CAMERA_PREPARE_SNAPSHOT,
286    CAMERA_SET_FPS_MODE,
287    CAMERA_SET_PARM_SCENE_MODE,
288};
289
290typedef enum {
291	CAMERA_RSP_CB_SUCCESS,
292	CAMERA_EXIT_CB_DONE,
293	CAMERA_EXIT_CB_FAILED,
294	CAMERA_EXIT_CB_DSP_IDLE,
295	CAMERA_EXIT_CB_DSP_ABORT,
296	CAMERA_EXIT_CB_ABORT,
297	CAMERA_EXIT_CB_ERROR,
298	CAMERA_EVT_CB_FRAME,
299	CAMERA_EVT_CB_PICTURE,
300	CAMERA_STATUS_CB,
301	CAMERA_EXIT_CB_FILE_SIZE_EXCEEDED,
302	CAMERA_EXIT_CB_BUFFER,
303	CAMERA_EVT_CB_SNAPSHOT_DONE,
304	CAMERA_CB_MAX,
305} camera_cb_type;
306
307struct cam_frame_start_parms {
308	unsigned int unknown;
309	struct msm_frame frame;
310	struct msm_frame video_frame;
311};
312
313typedef unsigned int exif_tag_id_t;
314
315#define EXIF_RATIONAL 5
316#define EXIF_ASCII 2
317#define EXIF_BYTE 1
318
319typedef struct {
320	int val;
321	int otherval;
322} rat_t;
323
324
325typedef union {
326        char * _ascii; /* At byte 16 relative to exif_tag_entry_t */
327        rat_t * _rats;
328        rat_t  _rat;
329        uint8_t _byte;
330} exif_tag_data_t;
331
332/* The entire exif_tag_entry_t struct must be 24 bytes in length */
333typedef unsigned int exif_tag_type_t;
334typedef struct {
335	exif_tag_type_t type;
336	uint32_t copy;
337	uint32_t count;
338	exif_tag_data_t data;
339} exif_tag_entry_t;
340
341typedef struct {
342	exif_tag_id_t tag_id;
343	exif_tag_entry_t tag_entry;
344} exif_tags_info_t;
345
346/* EXIF tag IDs */
347#define EXIFTAGID_GPS_LATITUDE 0x20002
348#define EXIFTAGID_GPS_LATITUDE_REF 0x10001
349#define EXIFTAGID_GPS_LONGITUDE 0x40004
350#define EXIFTAGID_GPS_LONGITUDE_REF 0x30003
351#define EXIFTAGID_GPS_ALTITUDE 0x60006
352#define EXIFTAGID_GPS_ALTITUDE_REF 0x50005
353#define EXIFTAGID_EXIF_CAMERA_MAKER 0x21010F
354#define EXIFTAGID_EXIF_CAMERA_MODEL 0x220110
355#define EXIFTAGID_EXIF_DATE_TIME_ORIGINAL 0x3A9003
356#define EXIFTAGID_EXIF_DATE_TIME 0x3B9004
357/* End of values originally in proprietary headers */
358
359namespace android {
360
361class QualcommCameraHardware : public CameraHardwareInterface {
362public:
363
364    virtual sp<IMemoryHeap> getPreviewHeap() const;
365    virtual sp<IMemoryHeap> getRawHeap() const;
366
367    virtual void setCallbacks(notify_callback notify_cb,
368                              data_callback data_cb,
369                              data_callback_timestamp data_cb_timestamp,
370                              void* user);
371    virtual void enableMsgType(int32_t msgType);
372    virtual void disableMsgType(int32_t msgType);
373    virtual bool msgTypeEnabled(int32_t msgType);
374
375    virtual status_t dump(int fd, const Vector<String16>& args) const;
376    virtual status_t startPreview();
377    virtual void stopPreview();
378    virtual bool previewEnabled();
379    virtual status_t startRecording();
380    virtual void stopRecording();
381    virtual bool recordingEnabled();
382    virtual void releaseRecordingFrame(const sp<IMemory>& mem);
383    virtual status_t autoFocus();
384    virtual status_t cancelAutoFocus();
385    virtual status_t takePicture();
386    virtual status_t cancelPicture();
387    virtual status_t setParameters(const CameraParameters& params);
388    virtual CameraParameters getParameters() const;
389    virtual status_t sendCommand(int32_t command, int32_t arg1, int32_t arg2);
390    virtual status_t getBufferInfo( sp<IMemory>& Frame, size_t *alignedSize);
391
392    virtual void release();
393    virtual bool useOverlay();
394    virtual status_t setOverlay(const sp<Overlay> &overlay);
395
396    static sp<CameraHardwareInterface> createInstance();
397    static sp<QualcommCameraHardware> getInstance();
398
399    void receivePreviewFrame(struct msm_frame *frame);
400    void receiveRecordingFrame(struct msm_frame *frame);
401    void receiveJpegPicture(void);
402    void jpeg_set_location();
403    void receiveJpegPictureFragment(uint8_t *buf, uint32_t size);
404    void notifyShutter(common_crop_t *crop);
405    void receive_camframetimeout();
406
407private:
408    QualcommCameraHardware();
409    virtual ~QualcommCameraHardware();
410    status_t startPreviewInternal();
411    void stopPreviewInternal();
412    friend void *auto_focus_thread(void *user);
413    void runAutoFocus();
414    status_t cancelAutoFocusInternal();
415    bool native_set_dimension (int camfd);
416    bool native_jpeg_encode (void);
417    bool native_set_parm(cam_ctrl_type type, uint16_t length, void *value);
418    bool native_zoom_image(int fd, int srcOffset, int dstOffset, common_crop_t *crop);
419
420    static wp<QualcommCameraHardware> singleton;
421
422    /* These constants reflect the number of buffers that libmmcamera requires
423       for preview and raw, and need to be updated when libmmcamera
424       changes.
425    */
426    static const int kPreviewBufferCount = NUM_PREVIEW_BUFFERS;
427    static const int kRawBufferCount = 1;
428    static const int kJpegBufferCount = 1;
429
430    int jpegPadding;
431
432    CameraParameters mParameters;
433    unsigned int frame_size;
434    bool mCameraRunning;
435    Mutex mCameraRunningLock;
436    bool mPreviewInitialized;
437
438    // This class represents a heap which maintains several contiguous
439    // buffers.  The heap may be backed by pmem (when pmem_pool contains
440    // the name of a /dev/pmem* file), or by ashmem (when pmem_pool == NULL).
441
442    struct MemPool : public RefBase {
443        MemPool(int buffer_size, int num_buffers,
444                int frame_size,
445                const char *name);
446
447        virtual ~MemPool() = 0;
448
449        void completeInitialization();
450        bool initialized() const {
451            return mHeap != NULL && mHeap->base() != MAP_FAILED;
452        }
453
454        virtual status_t dump(int fd, const Vector<String16>& args) const;
455
456        int mBufferSize;
457        int mAlignedBufferSize;
458        int mNumBuffers;
459        int mFrameSize;
460        sp<MemoryHeapBase> mHeap;
461        sp<MemoryBase> *mBuffers;
462
463        const char *mName;
464    };
465
466    struct AshmemPool : public MemPool {
467        AshmemPool(int buffer_size, int num_buffers,
468                   int frame_size,
469                   const char *name);
470    };
471
472    struct PmemPool : public MemPool {
473        PmemPool(const char *pmem_pool,
474                 int control_camera_fd, int flags, int pmem_type,
475                 int buffer_size, int num_buffers,
476                 int frame_size,
477                 const char *name);
478        virtual ~PmemPool();
479        int mFd;
480        int mPmemType;
481        int mCameraControlFd;
482        uint32_t mAlignedSize;
483        struct pmem_region mSize;
484    };
485
486    sp<PmemPool> mPreviewHeap;
487    sp<PmemPool> mRecordHeap;
488    sp<PmemPool> mThumbnailHeap;
489    sp<PmemPool> mRawHeap;
490    sp<PmemPool> mDisplayHeap;
491    sp<AshmemPool> mJpegHeap;
492    sp<PmemPool> mRawSnapShotPmemHeap;
493    sp<PmemPool> mPostViewHeap;
494
495
496    bool startCamera();
497    bool initPreview();
498    bool initRecord();
499    void deinitPreview();
500    bool initRaw(bool initJpegHeap);
501    bool initRawSnapshot();
502    void deinitRaw();
503    void deinitRawSnapshot();
504
505    bool mFrameThreadRunning;
506    Mutex mFrameThreadWaitLock;
507    Condition mFrameThreadWait;
508    friend void *frame_thread(void *user);
509    void runFrameThread(void *data);
510
511    //720p recording video thread
512    bool mVideoThreadExit;
513    bool mVideoThreadRunning;
514    Mutex mVideoThreadWaitLock;
515    Condition mVideoThreadWait;
516    friend void *video_thread(void *user);
517    void runVideoThread(void *data);
518
519
520    bool mShutterPending;
521    Mutex mShutterLock;
522
523    bool mSnapshotThreadRunning;
524    Mutex mSnapshotThreadWaitLock;
525    Condition mSnapshotThreadWait;
526    friend void *snapshot_thread(void *user);
527    void runSnapshotThread(void *data);
528    Mutex mRawPictureHeapLock;
529    bool mJpegThreadRunning;
530    Mutex mJpegThreadWaitLock;
531    Condition mJpegThreadWait;
532    bool mInSnapshotMode;
533    Mutex mInSnapshotModeWaitLock;
534    Condition mInSnapshotModeWait;
535
536    void debugShowPreviewFPS() const;
537    void debugShowVideoFPS() const;
538
539    int mSnapshotFormat;
540    void filterPictureSizes();
541    void filterPreviewSizes();
542    void storeTargetType();
543
544    void initDefaultParameters();
545    void findSensorType();
546
547    status_t setPreviewSize(const CameraParameters& params);
548    status_t setPreviewFrameRate(const CameraParameters& params);
549    status_t setPictureSize(const CameraParameters& params);
550    status_t setJpegQuality(const CameraParameters& params);
551    status_t setAntibanding(const CameraParameters& params);
552    status_t setEffect(const CameraParameters& params);
553    status_t setAutoExposure(const CameraParameters& params);
554    status_t setWhiteBalance(const CameraParameters& params);
555    status_t setFlash(const CameraParameters& params);
556    status_t setGpsLocation(const CameraParameters& params);
557    status_t setRotation(const CameraParameters& params);
558    status_t setZoom(const CameraParameters& params);
559    status_t setFocusMode(const CameraParameters& params);
560    status_t setBrightness(const CameraParameters& params);
561    status_t setExposureCompensation(const CameraParameters& params);
562    status_t setOrientation(const CameraParameters& params);
563    status_t setLensshadeValue(const CameraParameters& params);
564    status_t setISOValue(const CameraParameters& params);
565    status_t setPictureFormat(const CameraParameters& params);
566    status_t setSharpness(const CameraParameters& params);
567    status_t setContrast(const CameraParameters& params);
568    status_t setSaturation(const CameraParameters& params);
569    void setGpsParameters();
570    void storePreviewFrameForPostview();
571    bool isValidDimension(int w, int h);
572
573    Mutex mLock;
574    Mutex mCamframeTimeoutLock;
575    bool camframe_timeout_flag;
576    bool mReleasedRecordingFrame;
577
578    void receiveRawPicture(void);
579    void receiveRawSnapshot(void);
580
581    Mutex mCallbackLock;
582    Mutex mOverlayLock;
583	Mutex mRecordLock;
584	Mutex mRecordFrameLock;
585	Condition mRecordWait;
586    Condition mStateWait;
587
588    /* mJpegSize keeps track of the size of the accumulated JPEG.  We clear it
589       when we are about to take a picture, so at any time it contains either
590       zero, or the size of the last JPEG picture taken.
591    */
592    uint32_t mJpegSize;
593    unsigned int        mPreviewFrameSize;
594    unsigned int        mRecordFrameSize;
595    int                 mRawSize;
596    int                 mJpegMaxSize;
597
598#if DLOPEN_LIBMMCAMERA
599    void *libmmcamera;
600#endif
601
602    int mCameraControlFd;
603    struct msm_camsensor_info mSensorInfo;
604    cam_ctrl_dimension_t mDimension;
605    bool mAutoFocusThreadRunning;
606    Mutex mAutoFocusThreadLock;
607    int mAutoFocusFd;
608
609    Mutex mAfLock;
610
611    pthread_t mFrameThread;
612    pthread_t mVideoThread;
613    pthread_t mSnapshotThread;
614
615    common_crop_t mCrop;
616
617    bool mInitialized;
618
619    int mBrightness;
620    int mHJR;
621    struct msm_frame frames[kPreviewBufferCount];
622    struct msm_frame *recordframes;
623    bool mInPreviewCallback;
624    bool mUseOverlay;
625    sp<Overlay>  mOverlay;
626
627    int32_t mMsgEnabled;    // camera msg to be handled
628    notify_callback mNotifyCallback;
629    data_callback mDataCallback;
630    data_callback_timestamp mDataCallbackTimestamp;
631    void *mCallbackCookie;  // same for all callbacks
632    int mDebugFps;
633    int kPreviewBufferCountActual;
634    int previewWidth, previewHeight;
635};
636
637}; // namespace android
638
639#endif