codeworkx | 62f02ba | 2012-05-20 12:00:36 +0200 | [diff] [blame] | 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 | |
| 21 | #include "SecCamera.h" |
| 22 | #include <utils/threads.h> |
| 23 | #include <utils/RefBase.h> |
| 24 | #include <binder/MemoryBase.h> |
| 25 | #include <binder/MemoryHeapBase.h> |
| 26 | #include <hardware/camera.h> |
| 27 | #include <hardware/gralloc.h> |
| 28 | #include <camera/CameraParameters.h> |
| 29 | #include <binder/MemoryHeapBaseIon.h> |
| 30 | #include <sec_utils_v4l2.h> |
| 31 | |
| 32 | #include "gralloc_priv.h" |
| 33 | |
| 34 | #define BUFFER_COUNT_FOR_GRALLOC (MAX_BUFFERS + 4) |
| 35 | #define BUFFER_COUNT_FOR_ARRAY (MAX_BUFFERS) |
| 36 | |
| 37 | namespace android { |
| 38 | class CameraHardwareSec : public virtual RefBase { |
| 39 | public: |
| 40 | virtual void setCallbacks(camera_notify_callback notify_cb, |
| 41 | camera_data_callback data_cb, |
| 42 | camera_data_timestamp_callback data_cb_timestamp, |
| 43 | camera_request_memory get_memory, |
| 44 | void *user); |
| 45 | |
| 46 | virtual void enableMsgType(int32_t msgType); |
| 47 | virtual void disableMsgType(int32_t msgType); |
| 48 | virtual bool msgTypeEnabled(int32_t msgType); |
| 49 | |
| 50 | virtual status_t startPreview(); |
| 51 | virtual void stopPreview(); |
| 52 | virtual bool previewEnabled(); |
| 53 | |
| 54 | virtual status_t startRecording(); |
| 55 | virtual void stopRecording(); |
| 56 | virtual bool recordingEnabled(); |
| 57 | virtual void releaseRecordingFrame(const void *opaque); |
| 58 | |
| 59 | virtual status_t autoFocus(); |
| 60 | virtual status_t cancelAutoFocus(); |
| 61 | virtual status_t takePicture(); |
| 62 | virtual status_t cancelPicture(); |
| 63 | virtual status_t dump(int fd) const; |
| 64 | virtual status_t setParameters(const CameraParameters& params); |
| 65 | virtual CameraParameters getParameters() const; |
| 66 | virtual status_t sendCommand(int32_t command, int32_t arg1, int32_t arg2); |
| 67 | virtual status_t setPreviewWindow(preview_stream_ops *w); |
| 68 | virtual status_t storeMetaDataInBuffers(bool enable); |
| 69 | virtual void release(); |
| 70 | |
| 71 | inline int getCameraId() const; |
| 72 | |
| 73 | CameraHardwareSec(int cameraId, camera_device_t *dev); |
| 74 | virtual ~CameraHardwareSec(); |
| 75 | private: |
| 76 | status_t startPreviewInternal(); |
| 77 | void stopPreviewInternal(); |
| 78 | |
| 79 | class PreviewThread : public Thread { |
| 80 | CameraHardwareSec *mHardware; |
| 81 | public: |
| 82 | PreviewThread(CameraHardwareSec *hw): |
| 83 | Thread(false), |
| 84 | mHardware(hw) { } |
| 85 | virtual void onFirstRef() { |
| 86 | run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY); |
| 87 | } |
| 88 | virtual bool threadLoop() { |
| 89 | mHardware->previewThreadWrapper(); |
| 90 | return false; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class PictureThread : public Thread { |
| 95 | CameraHardwareSec *mHardware; |
| 96 | public: |
| 97 | PictureThread(CameraHardwareSec *hw): |
| 98 | Thread(false), |
| 99 | mHardware(hw) { } |
| 100 | virtual bool threadLoop() { |
| 101 | mHardware->pictureThread(); |
| 102 | mHardware->mSecCamera->endSnapshot(); |
| 103 | return false; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | class AutoFocusThread : public Thread { |
| 108 | CameraHardwareSec *mHardware; |
| 109 | public: |
| 110 | AutoFocusThread(CameraHardwareSec *hw): Thread(false), mHardware(hw) { } |
| 111 | virtual void onFirstRef() { |
| 112 | run("CameraAutoFocusThread", PRIORITY_DEFAULT); |
| 113 | } |
| 114 | virtual bool threadLoop() { |
| 115 | mHardware->autoFocusThread(); |
| 116 | return true; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | void initDefaultParameters(int cameraId); |
| 121 | void initHeapLocked(); |
| 122 | |
| 123 | sp<PreviewThread> mPreviewThread; |
| 124 | int previewThread(); |
| 125 | int previewThreadWrapper(); |
| 126 | |
| 127 | sp<AutoFocusThread> mAutoFocusThread; |
| 128 | int autoFocusThread(); |
| 129 | |
| 130 | sp<PictureThread> mPictureThread; |
| 131 | int pictureThread(); |
| 132 | bool mCaptureInProgress; |
| 133 | |
| 134 | int save_jpeg(unsigned char *real_jpeg, int jpeg_size); |
| 135 | void save_postview(const char *fname, uint8_t *buf, |
| 136 | uint32_t size); |
| 137 | int decodeInterleaveData(unsigned char *pInterleaveData, |
| 138 | int interleaveDataSize, |
| 139 | int yuvWidth, |
| 140 | int yuvHeight, |
| 141 | int *pJpegSize, |
| 142 | void *pJpegData, |
| 143 | void *pYuvData); |
| 144 | bool YUY2toNV21(void *srcBuf, void *dstBuf, uint32_t srcWidth, uint32_t srcHeight); |
| 145 | bool scaleDownYuv422(char *srcBuf, uint32_t srcWidth, |
| 146 | uint32_t srcHight, char *dstBuf, |
| 147 | uint32_t dstWidth, uint32_t dstHight); |
| 148 | |
| 149 | bool CheckVideoStartMarker(unsigned char *pBuf); |
| 150 | bool CheckEOIMarker(unsigned char *pBuf); |
| 151 | bool FindEOIMarkerInJPEG(unsigned char *pBuf, |
| 152 | int dwBufSize, int *pnJPEGsize); |
| 153 | bool SplitFrame(unsigned char *pFrame, int dwSize, |
| 154 | int dwJPEGLineLength, int dwVideoLineLength, |
| 155 | int dwVideoHeight, void *pJPEG, |
| 156 | int *pdwJPEGSize, void *pVideo, |
| 157 | int *pdwVideoSize); |
| 158 | void setSkipFrame(int frame); |
| 159 | bool isSupportedPreviewSize(const int width, |
| 160 | const int height) const; |
| 161 | /* used by auto focus thread to block until it's told to run */ |
| 162 | mutable Mutex mFocusLock; |
| 163 | mutable Condition mFocusCondition; |
| 164 | bool mExitAutoFocusThread; |
| 165 | |
| 166 | /* used by preview thread to block until it's told to run */ |
| 167 | mutable Mutex mPreviewLock; |
| 168 | mutable Condition mPreviewCondition; |
| 169 | mutable Condition mPreviewStoppedCondition; |
| 170 | bool mPreviewRunning; |
| 171 | bool mPreviewStartDeferred; |
| 172 | bool mExitPreviewThread; |
| 173 | |
| 174 | preview_stream_ops *mPreviewWindow; |
| 175 | |
| 176 | /* used to guard threading state */ |
| 177 | mutable Mutex mStateLock; |
| 178 | |
| 179 | CameraParameters mParameters; |
| 180 | CameraParameters mInternalParameters; |
| 181 | |
| 182 | int mFrameSizeDelta; |
| 183 | camera_memory_t *mPreviewHeap; |
| 184 | camera_memory_t *mRawHeap; |
| 185 | camera_memory_t *mRecordHeap[BUFFER_COUNT_FOR_ARRAY]; |
| 186 | |
| 187 | buffer_handle_t *mBufferHandle[BUFFER_COUNT_FOR_ARRAY]; |
| 188 | int mStride[BUFFER_COUNT_FOR_ARRAY]; |
| 189 | |
| 190 | |
| 191 | SecCamera *mSecCamera; |
| 192 | const __u8 *mCameraSensorName; |
| 193 | |
| 194 | mutable Mutex mSkipFrameLock; |
| 195 | int mSkipFrame; |
| 196 | |
| 197 | camera_notify_callback mNotifyCb; |
| 198 | camera_data_callback mDataCb; |
| 199 | camera_data_timestamp_callback mDataCbTimestamp; |
| 200 | camera_request_memory mGetMemoryCb; |
| 201 | void *mCallbackCookie; |
| 202 | |
| 203 | int32_t mMsgEnabled; |
| 204 | |
| 205 | bool mRecordRunning; |
| 206 | mutable Mutex mRecordLock; |
| 207 | int mPostViewWidth; |
| 208 | int mPostViewHeight; |
| 209 | int mPostViewSize; |
| 210 | |
| 211 | Vector<Size> mSupportedPreviewSizes; |
| 212 | |
| 213 | camera_device_t *mHalDevice; |
| 214 | static gralloc_module_t const* mGrallocHal; |
| 215 | }; |
| 216 | |
| 217 | }; // namespace android |
| 218 | |
| 219 | #endif |