Home | History | Annotate | Download | only in libcamera
      1 /*
      2 **
      3 ** Copyright 2008, The Android Open Source Project
      4 ** Copyright 2010, Samsung Electronics Co. LTD
      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 
     19 /*!
     20  * \file      ExynosCameraHWInterface.h
     21  * \brief     hearder file for Android Camera HAL
     22  * \author    thun.hwang(thun.hwang (at) samsung.com)
     23  * \date      2010/06/03
     24  *
     25  * <b>Revision History: </b>
     26  * - 2011/12/31 : thun.hwang(thun.hwang (at) samsung.com) \n
     27  *   Initial version
     28  *
     29  * - 2012/03/14 : sangwoo.park(sw5771.park (at) samsung.com) \n
     30  *   Change file, class name to ExynosXXX.
     31  *
     32  */
     33 
     34 #ifndef EXYNOS_CAMERA_HW_INTERFACE_H
     35 
     36 #include <utils/threads.h>
     37 #include <utils/RefBase.h>
     38 #include <binder/MemoryBase.h>
     39 #include <binder/MemoryHeapBase.h>
     40 #include <hardware/camera.h>
     41 #include <hardware/gralloc.h>
     42 #include <camera/Camera.h>
     43 #include <camera/CameraParameters.h>
     44 #include <media/hardware/MetadataBufferType.h>
     45 
     46 #include "gralloc_priv.h"
     47 
     48 #include "exynos_format.h"
     49 #include "csc.h"
     50 #include "ExynosCamera.h"
     51 
     52 #include <fcntl.h>
     53 #include <sys/mman.h>
     54 
     55 #define USE_EGL                         (1)
     56 
     57 #define  NUM_OF_PREVIEW_BUF             (8)
     58 #define  NUM_OF_VIDEO_BUF               (8)
     59 #define  NUM_OF_PICTURE_BUF             (6)
     60 #define  NUM_OF_WAITING_PUT_PICTURE_BUF (1)
     61 
     62 #define  NUM_OF_DETECTED_FACES          (32)
     63 
     64 namespace android {
     65 
     66 class ExynosCameraHWInterface : public virtual RefBase {
     67 public:
     68     ExynosCameraHWInterface(int cameraId, camera_device_t *dev);
     69     virtual             ~ExynosCameraHWInterface();
     70 
     71     virtual status_t    setPreviewWindow(preview_stream_ops *w);
     72     virtual void        setCallbacks(camera_notify_callback notify_cb,
     73                                      camera_data_callback data_cb,
     74                                      camera_data_timestamp_callback data_cb_timestamp,
     75                                      camera_request_memory get_memory,
     76                                      void *user);
     77 
     78     virtual void        enableMsgType(int32_t msgType);
     79     virtual void        disableMsgType(int32_t msgType);
     80     virtual bool        msgTypeEnabled(int32_t msgType);
     81 
     82     virtual status_t    startPreview();
     83     virtual void        stopPreview();
     84     virtual bool        previewEnabled();
     85 
     86     virtual status_t    storeMetaDataInBuffers(bool enable);
     87 
     88     virtual status_t    startRecording();
     89     virtual void        stopRecording();
     90     virtual bool        recordingEnabled();
     91     virtual void        releaseRecordingFrame(const void *opaque);
     92 
     93     virtual status_t    autoFocus();
     94     virtual status_t    cancelAutoFocus();
     95 
     96     virtual status_t    takePicture();
     97     virtual status_t    cancelPicture();
     98 
     99     virtual status_t    setParameters(const CameraParameters& params);
    100     virtual CameraParameters  getParameters() const;
    101     virtual status_t    sendCommand(int32_t command, int32_t arg1, int32_t arg2);
    102 
    103     virtual void        release();
    104 
    105     virtual status_t    dump(int fd) const;
    106 
    107     inline  int         getCameraId() const;
    108 
    109 private:
    110     class PreviewThread : public Thread {
    111         ExynosCameraHWInterface *mHardware;
    112     public:
    113         PreviewThread(ExynosCameraHWInterface *hw):
    114         Thread(false),
    115         mHardware(hw) { }
    116         virtual void onFirstRef() {
    117             //run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY);
    118             run("CameraPreviewThread", PRIORITY_DEFAULT);
    119         }
    120         virtual bool threadLoop() {
    121             mHardware->m_previewThreadFuncWrapper();
    122             return false;
    123         }
    124     };
    125 
    126     class VideoThread : public Thread {
    127         ExynosCameraHWInterface *mHardware;
    128     public:
    129         VideoThread(ExynosCameraHWInterface *hw):
    130         Thread(false),
    131         mHardware(hw) { }
    132         virtual void onFirstRef() {
    133             run("CameraVideoThread", PRIORITY_DEFAULT);
    134         }
    135         virtual bool threadLoop() {
    136             mHardware->m_videoThreadFuncWrapper();
    137             return false;
    138         }
    139     };
    140 
    141     class PictureThread : public Thread {
    142         ExynosCameraHWInterface *mHardware;
    143     public:
    144         PictureThread(ExynosCameraHWInterface *hw):
    145         Thread(false),
    146         mHardware(hw) { }
    147         virtual bool threadLoop() {
    148             mHardware->m_pictureThreadFunc();
    149             return false;
    150         }
    151     };
    152 
    153     class AutoFocusThread : public Thread {
    154         ExynosCameraHWInterface *mHardware;
    155     public:
    156         AutoFocusThread(ExynosCameraHWInterface *hw): Thread(false), mHardware(hw) { }
    157         virtual void onFirstRef() {
    158             run("CameraAutoFocusThread", PRIORITY_DEFAULT);
    159         }
    160         virtual bool threadLoop() {
    161             mHardware->m_autoFocusThreadFunc();
    162             return true;
    163         }
    164     };
    165 
    166 private:
    167     void        m_initDefaultParameters(int cameraId);
    168 
    169     bool        m_startPreviewInternal(void);
    170     void        m_stopPreviewInternal(void);
    171 
    172     bool        m_previewThreadFuncWrapper(void);
    173     bool        m_previewThreadFunc(void);
    174     bool        m_videoThreadFuncWrapper(void);
    175     bool        m_videoThreadFunc(void);
    176     bool        m_autoFocusThreadFunc(void);
    177 
    178     bool        m_startPictureInternal(void);
    179     bool        m_stopPictureInternal(void);
    180     bool        m_pictureThreadFunc(void);
    181 
    182     int         m_saveJpeg(unsigned char *real_jpeg, int jpeg_size);
    183     void        m_savePostView(const char *fname, uint8_t *buf,
    184                                uint32_t size);
    185     int         m_decodeInterleaveData(unsigned char *pInterleaveData,
    186                                        int interleaveDataSize,
    187                                        int yuvWidth,
    188                                        int yuvHeight,
    189                                        int *pJpegSize,
    190                                        void *pJpegData,
    191                                        void *pYuvData);
    192     bool        m_YUY2toNV21(void *srcBuf, void *dstBuf, uint32_t srcWidth, uint32_t srcHeight);
    193     bool        m_scaleDownYuv422(char *srcBuf, uint32_t srcWidth,
    194                                   uint32_t srcHight, char *dstBuf,
    195                                   uint32_t dstWidth, uint32_t dstHight);
    196 
    197     bool        m_checkVideoStartMarker(unsigned char *pBuf);
    198     bool        m_checkEOIMarker(unsigned char *pBuf);
    199     bool        m_findEOIMarkerInJPEG(unsigned char *pBuf,
    200                                       int dwBufSize, int *pnJPEGsize);
    201     bool        m_splitFrame(unsigned char *pFrame, int dwSize,
    202                              int dwJPEGLineLength, int dwVideoLineLength,
    203                              int dwVideoHeight, void *pJPEG,
    204                              int *pdwJPEGSize, void *pVideo,
    205                              int *pdwVideoSize);
    206     void        m_setSkipFrame(int frame);
    207     bool        m_isSupportedPreviewSize(const int width, const int height) const;
    208 
    209     void        m_getAlignedYUVSize(int colorFormat, int w, int h, ExynosBuffer *buf);
    210 
    211     bool        m_getResolutionList(String8 & string8Buf, char * strBuf, int w, int h);
    212 
    213     bool        m_getZoomRatioList(String8 & string8Buf, char * strBuf, int maxZoom, int start, int end);
    214 
    215     int         m_bracketsStr2Ints(char *str, int num, ExynosRect2 *rect2s, int *weights);
    216     bool        m_subBracketsStr2Ints(int num, char *str, int *arr);
    217     bool        m_getRatioSize(int  src_w,  int   src_h,
    218                                int  dst_w,  int   dst_h,
    219                                int *crop_x, int *crop_y,
    220                                int *crop_w, int *crop_h,
    221                                int zoom);
    222     int         m_calibratePosition(int w, int new_w, int x);
    223 #ifdef LOG_NDEBUG
    224     bool        m_fileDump(char *filename, void *srcBuf, uint32_t size);
    225 #endif
    226 
    227 private:
    228     sp<PreviewThread>   m_previewThread;
    229     sp<VideoThread>     m_videoThread;
    230     sp<AutoFocusThread> m_autoFocusThread;
    231     sp<PictureThread>   m_pictureThread;
    232 
    233     /* used by auto focus thread to block until it's told to run */
    234     mutable Mutex       m_focusLock;
    235     mutable Condition   m_focusCondition;
    236             bool        m_exitAutoFocusThread;
    237 
    238     /* used by preview thread to block until it's told to run */
    239     mutable Mutex       m_previewLock;
    240     mutable Condition   m_previewCondition;
    241     mutable Condition   m_previewStoppedCondition;
    242             bool        m_previewRunning;
    243             bool        m_exitPreviewThread;
    244             bool        m_previewStartDeferred;
    245 
    246     mutable Mutex       m_videoLock;
    247     mutable Condition   m_videoCondition;
    248     mutable Condition   m_videoStoppedCondition;
    249             bool        m_videoRunning;
    250             bool        m_videoStart;
    251             bool        m_exitVideoThread;
    252             bool        m_recordingHint;
    253 
    254     void               *m_grallocVirtAddr[NUM_OF_PREVIEW_BUF];
    255     int                 m_matchedGrallocIndex[NUM_OF_PREVIEW_BUF];
    256     ExynosBuffer        m_pictureBuf;
    257     ExynosBuffer        copy_previewBuf;
    258 
    259     struct ExynosBufferQueue {
    260         ExynosBuffer       buf;
    261         ExynosBufferQueue *next;
    262     };
    263 
    264     ExynosBufferQueue  *m_oldPictureBufQueueHead;
    265     ExynosBufferQueue   m_oldPictureBufQueue[NUM_OF_PICTURE_BUF];
    266     mutable Mutex       m_pictureLock;
    267     mutable Condition   m_pictureCondition;
    268             bool        m_pictureRunning;
    269             bool        m_captureInProgress;
    270             int         m_numOfAvaliblePictureBuf;
    271 
    272     ExynosRect          m_orgPreviewRect;
    273     ExynosRect          m_orgPictureRect;
    274     ExynosRect          m_orgVideoRect;
    275 
    276     void               *m_exynosPreviewCSC;
    277     void               *m_exynosPictureCSC;
    278     void               *m_exynosVideoCSC;
    279 
    280             preview_stream_ops *m_previewWindow;
    281 
    282     /* used to guard threading state */
    283     mutable Mutex       m_stateLock;
    284 
    285     CameraParameters    m_params;
    286     CameraParameters    m_internalParams;
    287 
    288     camera_memory_t    *m_previewHeap[NUM_OF_PREVIEW_BUF];
    289     buffer_handle_t    *m_previewBufHandle[NUM_OF_PREVIEW_BUF];
    290     int                 m_previewStride[NUM_OF_PREVIEW_BUF];
    291     bool                m_avaliblePreviewBufHandle[NUM_OF_PREVIEW_BUF];
    292     bool                m_flagGrallocLocked[NUM_OF_PREVIEW_BUF];
    293     int                 m_minUndequeuedBufs;
    294     int                 m_numOfAvailableVideoBuf;
    295     int                 m_cntVideoBuf;
    296 
    297     camera_memory_t    *m_videoHeap[NUM_OF_VIDEO_BUF];
    298     camera_memory_t    *m_resizedVideoHeap[NUM_OF_VIDEO_BUF];
    299     camera_memory_t    *m_pictureHeap[NUM_OF_PICTURE_BUF];
    300     int			m_pictureFds[NUM_OF_PICTURE_BUF][3];
    301     camera_memory_t    *m_rawHeap;
    302     int			m_ion_client;
    303 
    304     camera_frame_metadata_t  m_frameMetadata;
    305     camera_face_t            m_faces[NUM_OF_DETECTED_FACES];
    306     bool                     m_faceDetected;
    307 
    308     ExynosCamera       *m_secCamera;
    309 
    310     mutable Mutex       m_skipFrameLock;
    311             int         m_skipFrame;
    312 
    313     camera_notify_callback     m_notifyCb;
    314     camera_data_callback       m_dataCb;
    315     camera_data_timestamp_callback m_dataCbTimestamp;
    316     camera_request_memory      m_getMemoryCb;
    317     void                      *m_callbackCookie;
    318 
    319             int32_t     m_msgEnabled;
    320 
    321            Vector<Size> m_supportedPreviewSizes;
    322 
    323     camera_device_t *m_halDevice;
    324     static gralloc_module_t const* m_grallocHal;
    325 };
    326 
    327 }; // namespace android
    328 
    329 #endif
    330