1 /*
2 ** Copyright 2008, Google Inc.
3 ** Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_HARDWARE_QCAMERA_STREAM_H
19 #define ANDROID_HARDWARE_QCAMERA_STREAM_H
20 
21 
22 #include <utils/threads.h>
23 
24 #include <binder/MemoryBase.h>
25 #include <binder/MemoryHeapBase.h>
26 #include <utils/threads.h>
27 
28 
29 #include "QCameraHWI.h"
30 extern "C" {
31 #include <mm_camera_interface.h>
32 #include <mm_jpeg_interface.h>
33 
34 #define OPAQUE_BUFFER_COUNT 5
35 #define STREAM_TABLE_SIZE 8
36 
37 #define MM_CAMERA_CH_PREVIEW_MASK    (0x01 << MM_CAMERA_CH_PREVIEW)
38 #define MM_CAMERA_CH_VIDEO_MASK      (0x01 << MM_CAMERA_CH_VIDEO)
39 #define MM_CAMERA_CH_SNAPSHOT_MASK   (0x01 << MM_CAMERA_CH_SNAPSHOT)
40 
41 } /* extern C*/
42 
43 namespace android {
44 
45 class QCameraHardwareInterface;
46 
47 class StreamQueue {
48 private:
49     Mutex mQueueLock;
50     Condition mQueueWait;
51     bool mInitialized;
52 
53     //Vector<struct msm_frame *> mContainer;
54     Vector<void *> mContainer;
55 public:
56     StreamQueue();
57     virtual ~StreamQueue();
58     bool enqueue(void *element);
59     void flush();
60     void* dequeue();
61     void init();
62     void deinit();
63     bool isInitialized();
64 bool isEmpty();
65 };
66 
67 typedef struct {
68      int                     buffer_count;
69      buffer_handle_t         buffer_handle[MM_CAMERA_MAX_NUM_FRAMES];
70      struct private_handle_t *private_buffer_handle[MM_CAMERA_MAX_NUM_FRAMES];
71      int                     stride[MM_CAMERA_MAX_NUM_FRAMES];
72      uint32_t                addr_offset[MM_CAMERA_MAX_NUM_FRAMES];
73      uint8_t                 local_flag[MM_CAMERA_MAX_NUM_FRAMES];
74      int                     main_ion_fd[MM_CAMERA_MAX_NUM_FRAMES];
75      struct ion_fd_data      ion_info_fd[MM_CAMERA_MAX_NUM_FRAMES];
76 } QCameraStreamMemory_t;
77 
78 class QCameraStream {
79 
80 public:
81     bool mInit;
82     bool mActive;
83 
84     uint32_t mCameraHandle;
85     uint32_t mChannelId;
86     uint32_t mMmStreamId;
87     uint32_t mWidth;
88     uint32_t mHeight;
89     cam_format_t mFormat;
90     uint8_t mNumBuffers;
91     mm_camera_frame_len_offset mFrameOffsetInfo;
92     mm_camera_vtbl_t *p_mm_ops;
93     mm_camera_img_mode mExtImgMode;
94 
95     uint8_t mPendingCount;
96 
97     pthread_mutex_t mFrameDeliveredMutex;
98     pthread_cond_t mFrameDeliveredCond;
99 
100     virtual status_t    init();
101     virtual status_t    start();
102     virtual void        stop();
103     virtual void        release();
104 
105     status_t setFormat();
106     status_t setMode(int enable);
107 
getStreamId()108     int getStreamId() {return mStreamId;}
getMaxBuffers()109     int getMaxBuffers() {return mMaxBuffers;}
110 
111     static QCameraStream *getStreamAtId(int id);
112     static QCameraStream *getStreamAtMmId(uint32_t mm_id);
113     static void streamOffAll();
114 
115     virtual void setHALCameraControl(QCameraHardwareInterface* ctrl);
116 
prepareStream()117     virtual int prepareStream() {return NO_ERROR;}
118     void onNewRequest();
119     void dataCallback(mm_camera_super_buf_t *bufs);
120     int32_t streamOn();
121     int32_t streamOff(bool aSync);
122     virtual status_t initStream(mm_camera_img_mode imgmode, cam_format_t format);
123     virtual status_t deinitStream();
initBuffers()124     virtual status_t initBuffers(){return NO_ERROR;}
getRawHeap()125     virtual sp<IMemoryHeap> getRawHeap() const {return NULL;}
getLastQueuedFrame(void)126     virtual void *getLastQueuedFrame(void){return NULL;}
processPreviewFrame(mm_camera_super_buf_t * frame)127     virtual status_t processPreviewFrame(mm_camera_super_buf_t *frame){return NO_ERROR;}
128 
129     /* Set the ANativeWindow */
setPreviewWindow(const camera2_stream_ops_t * window)130     virtual int setPreviewWindow(const camera2_stream_ops_t* window) {return NO_ERROR;}
notifyWDenoiseEvent(cam_ctrl_status_t status,void * cookie)131     virtual void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie) {;}
resetSnapshotCounters(void)132     virtual void resetSnapshotCounters(void ){};
initHdrInfoForSnapshot(bool HDR_on,int number_frames,int * exp)133     virtual void initHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp){};
notifyHdrEvent(cam_ctrl_status_t status,void * cookie)134     virtual void notifyHdrEvent(cam_ctrl_status_t status, void * cookie){};
receiveRawPicture(mm_camera_super_buf_t * recvd_frame)135     virtual status_t receiveRawPicture(mm_camera_super_buf_t* recvd_frame){return NO_ERROR;};
136 
137     QCameraStream(uint32_t CameraHandle,
138                         uint32_t ChannelId,
139                         uint32_t Width,
140                         uint32_t Height,
141                         mm_camera_vtbl_t *mm_ops,
142                         camera_mode_t mode);
143     virtual ~QCameraStream();
144 public:
145     QCameraHardwareInterface*  mHalCamCtrl;
146     mm_camera_rect_t mCrop;
147 
148     camera_mode_t myMode;
149 
150     mutable Mutex mLock;
151 protected:
152 
153     uint32_t mStreamId;
154     int mMaxBuffers;
155     int allocateStreamId();
156     int deallocateStreamId(int id);
157 
158 private:
159     static QCameraStream *mStreamTable[STREAM_TABLE_SIZE];
160 
161     StreamQueue mBusyQueue;
162     StreamQueue mFreeQueue;
163 
164     QCameraStream();
165 public:
166 //     friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
167 };
168 
169 class QCameraStream_preview : public QCameraStream {
170 public:
171     status_t    init();
172     status_t    start() ;
173     void        stop()  ;
174     void        release() ;
175 
176     static QCameraStream* createInstance(uint32_t CameraHandle,
177                         uint32_t ChannelId,
178                         uint32_t Width,
179                         uint32_t Height,
180                         int requestedFormat,
181                         mm_camera_vtbl_t *mm_ops,
182                         camera_mode_t mode);
183     static void deleteInstance(QCameraStream *p);
184 
185     virtual ~QCameraStream_preview();
186     void *getLastQueuedFrame(void);
187     status_t initBuffers();
188     void deinitBuffers();
189 
190     virtual int prepareStream();
191     virtual status_t processPreviewFrame(mm_camera_super_buf_t *frame);
192 
193     int setPreviewWindow(const camera2_stream_ops_t* window);
194     int registerStreamBuffers(int num_buffers, buffer_handle_t *buffers);
195     friend class QCameraHardwareInterface;
196 
197 private:
198     QCameraStream_preview();
199     QCameraStream_preview(uint32_t CameraHandle,
200                         uint32_t ChannelId,
201                         uint32_t Width,
202                         uint32_t Height,
203                         int requestedFormat,
204                         mm_camera_vtbl_t *mm_ops,
205                         camera_mode_t mode);
206 
207     void                     dumpFrameToFile(mm_camera_buf_def_t* newFrame);
208 
209 private:
210     bool                     mFirstFrameRcvd;
211     QCameraStreamMemory_t mPreviewMemory;
212     Mutex         mPreviewMemoryLock;
213 
214     int8_t                   my_id;
215     mm_camera_op_mode_type_t op_mode;
216     mm_camera_buf_def_t      *mLastQueuedFrame;
217     mm_camera_buf_def_t      *mDisplayBuf;
218     Mutex                   mDisplayLock;
219     const camera2_stream_ops_t   *mPreviewWindow;
220     mm_camera_super_buf_t mNotifyBuffer[16];
221     int8_t                  mNumFDRcvd;
222     int                     mVFEOutputs;
223     int                     mHFRFrameCnt;
224     int                     mHFRFrameSkip;
225 };
226 
227 class QCameraStream_SnapshotMain : public QCameraStream {
228 public:
229     status_t    init();
230     status_t    start();
231     void        stop();
232     void        release();
233     status_t    initMainBuffers();
234     void        releaseMainBuffers();
235     void        initHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp);
236     void        notifyHdrEvent(cam_ctrl_status_t status, void * cookie);
237     static void            deleteInstance(QCameraStream *p);
238     status_t receiveRawPicture(mm_camera_super_buf_t* recvd_frame);
239     mm_camera_buf_def_t mSnapshotStreamBuf[MM_CAMERA_MAX_NUM_FRAMES];
240     static QCameraStream* createInstance(uint32_t CameraHandle,
241                         uint32_t ChannelId,
242                         uint32_t Width,
243                         uint32_t Height,
244                         uint32_t Format,
245                         uint8_t NumBuffers,
246                         mm_camera_vtbl_t *mm_ops,
247                         mm_camera_img_mode imgmode,
248                         camera_mode_t mode);
249     QCameraStream_SnapshotMain(uint32_t CameraHandle,
250                         uint32_t ChannelId,
251                         uint32_t Width,
252                         uint32_t Height,
253                         uint32_t Format,
254                         uint8_t NumBuffers,
255                         mm_camera_vtbl_t *mm_ops,
256                         mm_camera_img_mode imgmode,
257                         camera_mode_t mode);
258     ~QCameraStream_SnapshotMain();
259 
260 private:
261     status_t doHdrProcessing();
262     status_t encodeData(mm_camera_super_buf_t* recvd_frame);
263     /*Member variables*/
264     mm_jpeg_ops_t mJpegHandle;
265     uint32_t mJpegClientHandle;
266     int mSnapshotState;
267     StreamQueue mSnapshotQueue;
268 };
269 
270 class QCameraStream_SnapshotThumbnail : public QCameraStream {
271 public:
272     status_t    init();
273     status_t    start();
274     void        stop();
275     void        release();
276     status_t    initThumbnailBuffers();
277     static QCameraStream* createInstance(uint32_t CameraHandle,
278                         uint32_t ChannelId,
279                         uint32_t Width,
280                         uint32_t Height,
281                         uint32_t Format,
282                         uint8_t NumBuffers,
283                         mm_camera_vtbl_t *mm_ops,
284                         mm_camera_img_mode imgmode,
285                         camera_mode_t mode);
286     QCameraStream_SnapshotThumbnail(uint32_t CameraHandle,
287                         uint32_t ChannelId,
288                         uint32_t Width,
289                         uint32_t Height,
290                         uint32_t Format,
291                         uint8_t NumBuffers,
292                         mm_camera_vtbl_t *mm_ops,
293                         mm_camera_img_mode imgmode,
294                         camera_mode_t mode);
295     ~QCameraStream_SnapshotThumbnail();
296     static void            deleteInstance(QCameraStream *p);
297     mm_camera_buf_def_t mPostviewStreamBuf[MM_CAMERA_MAX_NUM_FRAMES];
298 };
299 
300 }; // namespace android
301 
302 #endif
303