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 #ifndef ANDROID_HARDWARE_CAMERA_HARDWARE_SEC_H
     20 #define ANDROID_HARDWARE_CAMERA_HARDWARE_SEC_H
     21 
     22 #include "SecCamera.h"
     23 #include <utils/threads.h>
     24 #include <camera/CameraHardwareInterface.h>
     25 #include <binder/MemoryBase.h>
     26 #include <binder/MemoryHeapBase.h>
     27 #include <utils/threads.h>
     28 
     29 namespace android {
     30 class CameraHardwareSec : public CameraHardwareInterface {
     31 public:
     32     virtual sp<IMemoryHeap> getPreviewHeap() const;
     33     virtual sp<IMemoryHeap> getRawHeap() const;
     34 
     35     virtual void        setCallbacks(notify_callback notify_cb,
     36                                      data_callback data_cb,
     37                                      data_callback_timestamp data_cb_timestamp,
     38                                      void *user);
     39 
     40     virtual void        enableMsgType(int32_t msgType);
     41     virtual void        disableMsgType(int32_t msgType);
     42     virtual bool        msgTypeEnabled(int32_t msgType);
     43 
     44     virtual status_t    startPreview();
     45 #if defined(BOARD_USES_OVERLAY)
     46     virtual bool        useOverlay();
     47     virtual status_t    setOverlay(const sp<Overlay> &overlay);
     48 #endif
     49     virtual void        stopPreview();
     50     virtual bool        previewEnabled();
     51 
     52     virtual status_t    startRecording();
     53     virtual void        stopRecording();
     54     virtual bool        recordingEnabled();
     55     virtual void        releaseRecordingFrame(const sp<IMemory> &mem);
     56 
     57     virtual status_t    autoFocus();
     58     virtual status_t    cancelAutoFocus();
     59     virtual status_t    takePicture();
     60     virtual status_t    cancelPicture();
     61     virtual status_t    dump(int fd, const Vector<String16> &args) const;
     62     virtual status_t    setParameters(const CameraParameters& params);
     63     virtual CameraParameters  getParameters() const;
     64     virtual status_t    sendCommand(int32_t command, int32_t arg1,
     65                                     int32_t arg2);
     66     virtual void        release();
     67 
     68     static    sp<CameraHardwareInterface> createInstance(int cameraId);
     69 
     70 private:
     71                         CameraHardwareSec(int cameraId);
     72     virtual             ~CameraHardwareSec();
     73 
     74     static wp<CameraHardwareInterface> singleton;
     75 
     76     static  const int   kBufferCount = MAX_BUFFERS;
     77     static  const int   kBufferCountForRecord = MAX_BUFFERS;
     78 
     79     class PreviewThread : public Thread {
     80         CameraHardwareSec *mHardware;
     81     public:
     82         PreviewThread(CameraHardwareSec *hw):
     83 #ifdef SINGLE_PROCESS
     84         // In single process mode this thread needs to be a java thread,
     85         // since we won't be calling through the binder.
     86         Thread(true),
     87 #else
     88         Thread(false),
     89 #endif
     90         mHardware(hw) { }
     91         virtual bool threadLoop() {
     92             int ret = mHardware->previewThread();
     93             // loop until we need to quit
     94             if(ret == NO_ERROR)
     95                 return true;
     96             else
     97                 return false;
     98         }
     99     };
    100 
    101     class PictureThread : public Thread {
    102         CameraHardwareSec *mHardware;
    103     public:
    104         PictureThread(CameraHardwareSec *hw):
    105         Thread(false),
    106         mHardware(hw) { }
    107         virtual bool threadLoop() {
    108             mHardware->pictureThread();
    109             return false;
    110         }
    111     };
    112 
    113     class AutoFocusThread : public Thread {
    114         CameraHardwareSec *mHardware;
    115     public:
    116         AutoFocusThread(CameraHardwareSec *hw): Thread(false), mHardware(hw) { }
    117         virtual void onFirstRef() {
    118             run("CameraAutoFocusThread", PRIORITY_DEFAULT);
    119         }
    120         virtual bool threadLoop() {
    121             mHardware->autoFocusThread();
    122             return true;
    123         }
    124     };
    125 
    126             void        initDefaultParameters(int cameraId);
    127             void        initHeapLocked();
    128 
    129     sp<PreviewThread>   mPreviewThread;
    130             int         previewThread();
    131             bool        mPreviewRunning;
    132 
    133     sp<AutoFocusThread> mAutoFocusThread;
    134             int         autoFocusThread();
    135 
    136     sp<PictureThread>   mPictureThread;
    137             int         pictureThread();
    138             bool        mCaptureInProgress;
    139 
    140             int         save_jpeg(unsigned char *real_jpeg, int jpeg_size);
    141             void        save_postview(const char *fname, uint8_t *buf,
    142                                         uint32_t size);
    143             int         decodeInterleaveData(unsigned char *pInterleaveData,
    144                                                 int interleaveDataSize,
    145                                                 int yuvWidth,
    146                                                 int yuvHeight,
    147                                                 int *pJpegSize,
    148                                                 void *pJpegData,
    149                                                 void *pYuvData);
    150             bool        YUY2toNV21(void *srcBuf, void *dstBuf, uint32_t srcWidth, uint32_t srcHeight);
    151             bool        scaleDownYuv422(char *srcBuf, uint32_t srcWidth,
    152                                         uint32_t srcHight, char *dstBuf,
    153                                         uint32_t dstWidth, uint32_t dstHight);
    154 
    155             bool        CheckVideoStartMarker(unsigned char *pBuf);
    156             bool        CheckEOIMarker(unsigned char *pBuf);
    157             bool        FindEOIMarkerInJPEG(unsigned char *pBuf,
    158                                             int dwBufSize, int *pnJPEGsize);
    159             bool        SplitFrame(unsigned char *pFrame, int dwSize,
    160                                    int dwJPEGLineLength, int dwVideoLineLength,
    161                                    int dwVideoHeight, void *pJPEG,
    162                                    int *pdwJPEGSize, void *pVideo,
    163                                    int *pdwVideoSize);
    164             void        setSkipFrame(int frame);
    165     /* used by auto focus thread to block until it's told to run */
    166     mutable Mutex       mFocusLock;
    167     mutable Condition   mCondition;
    168             bool        mExitAutoFocusThread;
    169 
    170     /* used to guard threading state */
    171     mutable Mutex       mStateLock;
    172 
    173     CameraParameters    mParameters;
    174     CameraParameters    mInternalParameters;
    175 
    176     sp<MemoryHeapBase>  mPreviewHeap;
    177     sp<MemoryHeapBase>  mRawHeap;
    178     sp<MemoryHeapBase>  mRecordHeap;
    179     sp<MemoryHeapBase>  mJpegHeap;
    180     sp<MemoryBase>      mBuffers[kBufferCount];
    181     sp<MemoryBase>      mRecordBuffers[kBufferCountForRecord];
    182 
    183             SecCamera   *mSecCamera;
    184             int         mPreviewFrameSize;
    185             int         mRawFrameSize;
    186             int         mPreviewFrameRateMicrosec;
    187             const __u8  *mCameraSensorName;
    188 
    189     mutable Mutex       mSkipFrameLock;
    190             int         mSkipFrame;
    191 
    192 #if defined(BOARD_USES_OVERLAY)
    193             sp<Overlay> mOverlay;
    194             bool        mUseOverlay;
    195             int         mOverlayBufferIdx;
    196 #endif
    197 
    198     notify_callback     mNotifyCb;
    199     data_callback       mDataCb;
    200     data_callback_timestamp mDataCbTimestamp;
    201             void        *mCallbackCookie;
    202 
    203             int32_t     mMsgEnabled;
    204 
    205             // only used from PreviewThread
    206             int         mCurrentPreviewFrame;
    207             int         mCurrentRecordFrame;
    208 
    209             bool        mRecordRunning;
    210 #ifdef JPEG_FROM_SENSOR
    211             int         mPostViewWidth;
    212             int         mPostViewHeight;
    213             int         mPostViewSize;
    214 #endif
    215 
    216     struct timeval      mTimeStart;
    217     struct timeval      mTimeStop;
    218 
    219 };
    220 
    221 }; // namespace android
    222 
    223 #endif
    224