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: PreviewThread(CameraHardwareSec * hw)82 PreviewThread(CameraHardwareSec *hw): 83 Thread(false), 84 mHardware(hw) { } onFirstRef()85 virtual void onFirstRef() { 86 run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY); 87 } threadLoop()88 virtual bool threadLoop() { 89 mHardware->previewThreadWrapper(); 90 return false; 91 } 92 }; 93 94 class PictureThread : public Thread { 95 CameraHardwareSec *mHardware; 96 public: PictureThread(CameraHardwareSec * hw)97 PictureThread(CameraHardwareSec *hw): 98 Thread(false), 99 mHardware(hw) { } threadLoop()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: AutoFocusThread(CameraHardwareSec * hw)110 AutoFocusThread(CameraHardwareSec *hw): Thread(false), mHardware(hw) { } onFirstRef()111 virtual void onFirstRef() { 112 run("CameraAutoFocusThread", PRIORITY_DEFAULT); 113 } threadLoop()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 mExitPreviewThread; 172 173 /* used to guard threading state */ 174 mutable Mutex mStateLock; 175 176 CameraParameters mParameters; 177 CameraParameters mInternalParameters; 178 179 sp<MemoryHeapBase> mPreviewHeap; 180 sp<MemoryHeapBase> mRawHeap; 181 sp<MemoryHeapBase> mRecordHeap; 182 sp<MemoryHeapBase> mJpegHeap; 183 sp<MemoryBase> mBuffers[kBufferCount]; 184 sp<MemoryBase> mRecordBuffers[kBufferCountForRecord]; 185 186 SecCamera *mSecCamera; 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 bool mRecordRunning; 206 mutable Mutex mRecordLock; 207 int mPostViewWidth; 208 int mPostViewHeight; 209 int mPostViewSize; 210 211 Vector<Size> mSupportedPreviewSizes; 212 }; 213 214 }; // namespace android 215 216 #endif 217