Home | History | Annotate | Download | only in libcamera
      1 /*
      2 ** Copyright 2008, The Android Open-Source Project
      3 **
      4 ** Licensed under the Apache License, Version 2.0 (the "License");
      5 ** you may not use this file except in compliance with the License.
      6 ** You may obtain a copy of the License at
      7 **
      8 **     http://www.apache.org/licenses/LICENSE-2.0
      9 **
     10 ** Unless required by applicable law or agreed to in writing, software
     11 ** distributed under the License is distributed on an "AS IS" BASIS,
     12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 ** See the License for the specific language governing permissions and
     14 ** limitations under the License.
     15 */
     16 
     17 #ifndef ANDROID_HARDWARE_QUALCOMM_CAMERA_HARDWARE_H
     18 #define ANDROID_HARDWARE_QUALCOMM_CAMERA_HARDWARE_H
     19 
     20 #include <camera/CameraHardwareInterface.h>
     21 #include <binder/MemoryBase.h>
     22 #include <binder/MemoryHeapBase.h>
     23 
     24 extern "C" {
     25     #include <linux/android_pmem.h>
     26 }
     27 
     28 namespace android {
     29 
     30 class QualcommCameraHardware : public CameraHardwareInterface {
     31 public:
     32 
     33     virtual sp<IMemoryHeap> getPreviewHeap() const;
     34     virtual sp<IMemoryHeap> getRawHeap() const;
     35 
     36     virtual status_t    dump(int fd, const Vector<String16>& args) const;
     37     virtual status_t    startPreview(preview_callback cb, void* user);
     38     virtual void        stopPreview();
     39     virtual bool        previewEnabled();
     40     virtual status_t    startRecording(recording_callback cb, void* user);
     41     virtual void        stopRecording();
     42     virtual bool        recordingEnabled();
     43     virtual void        releaseRecordingFrame(const sp<IMemory>& mem);
     44     virtual status_t    autoFocus(autofocus_callback, void *user);
     45     virtual status_t    takePicture(shutter_callback,
     46                                     raw_callback,
     47                                     jpeg_callback,
     48                                     void* user);
     49     virtual status_t    cancelPicture(bool cancel_shutter,
     50                                       bool cancel_raw, bool cancel_jpeg);
     51     virtual status_t    setParameters(const CameraParameters& params);
     52     virtual CameraParameters  getParameters() const;
     53 
     54     virtual void release();
     55 
     56     static sp<CameraHardwareInterface> createInstance();
     57     static sp<QualcommCameraHardware> getInstance();
     58 
     59     void* get_preview_mem(uint32_t size, uint32_t *phy_addr, uint32_t index);
     60     void* get_raw_mem(uint32_t size, uint32_t *phy_addr, uint32_t index);
     61     void free_preview_mem(uint32_t *phy_addr, uint32_t size, uint32_t index);
     62     void free_raw_mem(uint32_t *phy_addr, uint32_t size, uint32_t index);
     63 
     64 private:
     65 
     66     QualcommCameraHardware();
     67     virtual ~QualcommCameraHardware();
     68     status_t startPreviewInternal(preview_callback pcb, void *puser,
     69                                   recording_callback rcb, void *ruser);
     70     void stopPreviewInternal();
     71 
     72     static wp<QualcommCameraHardware> singleton;
     73 
     74     /* These constants reflect the number of buffers that libqcamera requires
     75        for preview and raw, and need to be updated when libqcamera
     76        changes.
     77     */
     78     static const int kPreviewBufferCount = 4;
     79     static const int kRawBufferCount = 1;
     80     static const int kJpegBufferCount = 1;
     81     static const int kRawFrameHeaderSize = 0x48;
     82 
     83     //TODO: put the picture dimensions in the CameraParameters object;
     84     CameraParameters mParameters;
     85     int mPreviewHeight;
     86     int mPreviewWidth;
     87     int mRawHeight;
     88     int mRawWidth;
     89 
     90     void receivePreviewFrame(camera_frame_type *frame);
     91 
     92     static void stop_camera_cb(camera_cb_type cb,
     93             const void *client_data,
     94             camera_func_type func,
     95             int32_t parm4);
     96 
     97     static void camera_cb(camera_cb_type cb,
     98             const void *client_data,
     99             camera_func_type func,
    100             int32_t parm4);
    101 
    102     // This class represents a heap which maintains several contiguous
    103     // buffers.  The heap may be backed by pmem (when pmem_pool contains
    104     // the name of a /dev/pmem* file), or by ashmem (when pmem_pool == NULL).
    105 
    106     struct MemPool : public RefBase {
    107         MemPool(int buffer_size, int num_buffers,
    108                 int frame_size,
    109                 int frame_offset,
    110                 const char *name);
    111 
    112         virtual ~MemPool() = 0;
    113 
    114         void completeInitialization();
    115         bool initialized() const {
    116             return mHeap != NULL && mHeap->base() != MAP_FAILED;
    117         }
    118 
    119         virtual status_t dump(int fd, const Vector<String16>& args) const;
    120 
    121         int mBufferSize;
    122         int mNumBuffers;
    123         int mFrameSize;
    124         int mFrameOffset;
    125         sp<MemoryHeapBase> mHeap;
    126         sp<MemoryBase> *mBuffers;
    127 
    128         const char *mName;
    129     };
    130 
    131     struct AshmemPool : public MemPool {
    132         AshmemPool(int buffer_size, int num_buffers,
    133                    int frame_size,
    134                    int frame_offset,
    135                    const char *name);
    136     };
    137 
    138     struct PmemPool : public MemPool {
    139         PmemPool(const char *pmem_pool,
    140                 int buffer_size, int num_buffers,
    141                 int frame_size,
    142                 int frame_offset,
    143                 const char *name);
    144         virtual ~PmemPool() { }
    145         int mFd;
    146         uint32_t mAlignedSize;
    147         struct pmem_region mSize;
    148     };
    149 
    150     struct PreviewPmemPool : public PmemPool {
    151         virtual ~PreviewPmemPool();
    152         PreviewPmemPool(int buffer_size, int num_buffers,
    153                         int frame_size,
    154                         int frame_offset,
    155                         const char *name);
    156     };
    157 
    158     struct RawPmemPool : public PmemPool {
    159         virtual ~RawPmemPool();
    160         RawPmemPool(const char *pmem_pool,
    161                     int buffer_size, int num_buffers,
    162                     int frame_size,
    163                     int frame_offset,
    164                     const char *name);
    165     };
    166 
    167     sp<PreviewPmemPool> mPreviewHeap;
    168     sp<RawPmemPool> mRawHeap;
    169     sp<AshmemPool> mJpegHeap;
    170 
    171     void startCameraIfNecessary();
    172     bool initPreview();
    173     void deinitPreview();
    174     bool initRaw(bool initJpegHeap);
    175 
    176     void initDefaultParameters();
    177     void initCameraParameters();
    178     void setCameraDimensions();
    179 
    180     // The states described by qualcomm_camera_state are very similar to the
    181     // CAMERA_FUNC_xxx notifications reported by libqcamera.  The differences
    182     // are that they reflect not only the response from libqcamera, but also
    183     // the requests made by the clients of this object.  For example,
    184     // QCS_PREVIEW_REQUESTED is a state that we enter when we call
    185     // QualcommCameraHardware::startPreview(), and stay in until libqcamera
    186     // confirms that it has received the start-preview command (but not
    187     // actually initiated preview yet).
    188     //
    189     // NOTE: keep those values small; they are used internally as indices
    190     //       into a array of strings.
    191     // NOTE: if you add to this enumeration, make sure you update
    192     //       getCameraStateStr().
    193 
    194     enum qualcomm_camera_state {
    195         QCS_INIT,
    196         QCS_IDLE,
    197         QCS_ERROR,
    198         QCS_PREVIEW_IN_PROGRESS,
    199         QCS_WAITING_RAW,
    200         QCS_WAITING_JPEG,
    201         /* internal states */
    202         QCS_INTERNAL_PREVIEW_STOPPING,
    203         QCS_INTERNAL_PREVIEW_REQUESTED,
    204         QCS_INTERNAL_RAW_REQUESTED,
    205         QCS_INTERNAL_STOPPING,
    206     };
    207 
    208     volatile qualcomm_camera_state mCameraState;
    209     static const char* const getCameraStateStr(qualcomm_camera_state s);
    210     qualcomm_camera_state change_state(qualcomm_camera_state new_state,
    211                                        bool lock = true);
    212 
    213     void notifyShutter();
    214     void receiveJpegPictureFragment(JPEGENC_CBrtnType *encInfo);
    215 
    216     void receivePostLpmRawPicture(camera_frame_type *frame);
    217     void receiveRawPicture(camera_frame_type *frame);
    218     void receiveJpegPicture(void);
    219 
    220     Mutex mLock; // API lock -- all public methods
    221     Mutex mCallbackLock;
    222     Mutex mStateLock;
    223     Condition mStateWait;
    224 
    225     /* mJpegSize keeps track of the size of the accumulated JPEG.  We clear it
    226        when we are about to take a picture, so at any time it contains either
    227        zero, or the size of the last JPEG picture taken.
    228     */
    229     uint32_t mJpegSize;
    230     camera_handle_type camera_handle;
    231     camera_encode_properties_type encode_properties;
    232     camera_position_type pt;
    233 
    234     shutter_callback    mShutterCallback;
    235     raw_callback        mRawPictureCallback;
    236     jpeg_callback       mJpegPictureCallback;
    237     void                *mPictureCallbackCookie;
    238 
    239     autofocus_callback  mAutoFocusCallback;
    240     void                *mAutoFocusCallbackCookie;
    241 
    242     preview_callback    mPreviewCallback;
    243     void                *mPreviewCallbackCookie;
    244     recording_callback  mRecordingCallback;
    245     void                *mRecordingCallbackCookie;
    246     bool setCallbacks(preview_callback pcb, void *pu,
    247                       recording_callback rcb, void *ru);
    248 
    249     int                 mPreviewFrameSize;
    250     int                 mRawSize;
    251     int                 mJpegMaxSize;
    252 
    253     // hack to prevent black frame on first preview
    254     int                 mPreviewCount;
    255 
    256 #if DLOPEN_LIBQCAMERA == 1
    257     void *libqcamera;
    258 #endif
    259 };
    260 
    261 }; // namespace android
    262 
    263 #endif
    264 
    265 
    266