Android5.1.1Camera 系统架构源码分析(2)---->Camera的startPreview和setPreviewCallback

链接:点击打开链接

一、命令流程


1.APP

packages/apps/Camera/src/com/android/camera/PhotoModule.java

[java]  view plain  copy
  1. private void startPreview() {  
  2.   mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);  
  3.   mCameraDevice.startPreviewAsync();  
  4. }  

packages/apps/Camera/src/com/android/camera/CameraManager.java

[java]  view plain  copy
  1. public void setPreviewDisplayAsync(final SurfaceHolder surfaceHolder) {  
  2.   mCameraHandler.obtainMessage(SET_PREVIEW_DISPLAY_ASYNC, surfaceHolder).sendToTarget();  
  3. }  
  4. public void startPreviewAsync() {  
  5.   mCameraHandler.sendEmptyMessage(START_PREVIEW_ASYNC);  
  6. }  
  7. public void handleMessage(final Message msg) {  
  8.   switch (msg.what) {  
  9.     case SET_PREVIEW_DISPLAY_ASYNC:  
  10.       try {  
  11.         mCamera.setPreviewDisplay((SurfaceHolder) msg.obj);  
  12.       } catch(IOException e) {  
  13.           throw new RuntimeException(e);  
  14.       }  
  15.       return;  // no need to call mSig.open()  
  16.     case START_PREVIEW_ASYNC:  
  17.       mCamera.startPreview();  
  18.       return;  // no need to call mSig.open()  
  19.   }  
  20. }  

2.frameworks

frameworks/base/core/java/android/hardware/Camera.java

[java]  view plain  copy
  1. public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {  
  2.   setPreviewDisplay(holder.getSurface());  
  3. }  
  4. public native final void startPreview();  

frameworks/base/core/jni/android_hardware_Camera.cpp

[cpp]  view plain  copy
  1. static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface){  
  2.   sp<Camera> camera = get_native_camera(env, thiz, NULL);  
  3.   if (camera->setPreviewDisplay(surface) != NO_ERROR) {  
  4.     jniThrowException(env, "java/io/IOException""setPreviewDisplay failed");  
  5.   }  
  6. }  
  7. static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz){  
  8.   sp<Camera> camera = get_native_camera(env, thiz, NULL);  
  9.   if (camera->startPreview() != NO_ERROR) {  
  10.     jniThrowRuntimeException(env, "startPreview failed");  
  11.     return;  
  12.   }  
  13. }  

frameworks/av/camera/Camera.cpp

[cpp]  view plain  copy
  1. status_t Camera::setPreviewDisplay(const sp<Surface>& surface){  
  2.   sp <ICamera> c = mCamera;  
  3.   return c->setPreviewDisplay(surface);  
  4. }  
  5. status_t Camera::startPreview(){  
  6.   sp <ICamera> c = mCamera;  
  7.   return c->startPreview();  
  8. }  

Binder调用

frameworks/av/services/camera/libcameraservice/CameraClient.cpp

[cpp]  view plain  copy
  1. status_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) {  
  2.   return setPreviewWindow(binder, window);  
  3. }  
  4. status_t CameraClient::startPreview() {  
  5.   return startCameraMode(CAMERA_PREVIEW_MODE);  
  6. }  
  7. status_t CameraClient::startCameraMode(camera_mode mode) {  
  8.   switch(mode) {  
  9.     case CAMERA_PREVIEW_MODE:  
  10.       return startPreviewMode();  
  11.   }  
  12. }  
  13. status_t CameraClient::startPreviewMode() {  
  14.   mHardware->setPreviewWindow(mPreviewWindow);  
  15.   result = mHardware->startPreview();  
  16. }  

frameworks/av/services/camera/libcameraservice/CameraHardwareInterface.h

[cpp]  view plain  copy
  1. status_t setPreviewWindow(const sp<ANativeWindow>& buf){  
  2.   mPreviewWindow = buf;  
  3.   /* 
  4.   #define anw(n) __to_anw(((struct camera_preview_window *)n)->user) 
  5.   static ANativeWindow *__to_anw(void *user){ 
  6.     CameraHardwareInterface *__this = 
  7.                 reinterpret_cast<CameraHardwareInterface *>(user); 
  8.     return __this->mPreviewWindow.get(); 
  9.   } 
  10.   */  
  11.   return mDevice->ops->set_preview_window(mDevice,  
  12.                     buf.get() ? &mHalPreviewWindow.nw : 0);  
  13.   /* 
  14.   void initHalPreviewWindow(){ 
  15.     mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer; 
  16.     mHalPreviewWindow.nw.lock_buffer = __lock_buffer; 
  17.     mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer; 
  18.     mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer; 
  19.     mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count; 
  20.     mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry; 
  21.     mHalPreviewWindow.nw.set_crop = __set_crop; 
  22.     mHalPreviewWindow.nw.set_timestamp = __set_timestamp; 
  23.     mHalPreviewWindow.nw.set_usage = __set_usage; 
  24.     mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval; 
  25.  
  26.  
  27.     mHalPreviewWindow.nw.get_min_undequeued_buffer_count = 
  28.                 __get_min_undequeued_buffer_count; 
  29.   } 
  30.   */  
  31. }  
  32. status_t startPreview(){  
  33.   return mDevice->ops->start_preview(mDevice);  
  34. }  

3.HAL

hardware/amlogic/camera/CameraHal_Module.cpp

[cpp]  view plain  copy
  1. int camera_set_preview_window(struct camera_device * device,  
  2.         struct preview_stream_ops *window){  
  3.   rv = gCameraHals[aml_dev->cameraid]->setPreviewWindow(window);  
  4. }  
  5. int camera_start_preview(struct camera_device * device){  
  6.   rv = gCameraHals[aml_dev->cameraid]->startPreview();  
  7. }  

hardware/amlogic/camera/CameraHal.cpp

[cpp]  view plain  copy
  1. status_t CameraHal::setPreviewWindow(struct preview_stream_ops *window){  
  2.   mDisplayAdapter = new ANativeWindowDisplayAdapter();  
  3.   mDisplayAdapter->setFrameProvider(mCameraAdapter); //将frameCallbackRelay设置为BaseCameraAdapter的回调函数  
  4.   mDisplayAdapter->setErrorHandler(mAppCallbackNotifier.get()); //设置BaseCameraAdapter错误处理函数  
  5.   ret  = mDisplayAdapter->setPreviewWindow(window);//设置post的Surface  
  6.   /* 
  7.   hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp 
  8.   int ANativeWindowDisplayAdapter::setPreviewWindow(preview_stream_ops_t* window){ 
  9.     mANativeWindow = window; 
  10.   } 
  11.   */  
  12. }  
  13. status_t CameraHal::startPreview(){  
  14.   //hardware/amlogic/camera/inc/V4LCameraAdapter/V4LCameraAdapter.h  
  15.   //#define DEFAULT_PREVIEW_PIXEL_FORMAT        V4L2_PIX_FMT_NV21  
  16.   ret = allocPreviewBufs(mPreviewWidth, mPreviewHeight, mParameters.getPreviewFormat(), required_buffer_count, max_queueble_buffers);  
  17.   /* 
  18.   status_t CameraHal::allocPreviewBufs(int width, int height, const char* previewFormat, 
  19.                                         unsigned int buffercount, unsigned int &max_queueable){ 
  20.     mPreviewBufs = (int32_t *) mDisplayAdapter->allocateBuffer(width, height, 
  21.                                                                     previewFormat, 
  22.                                                                     mPreviewLength, 
  23.                                                                     buffercount); 
  24.   } 
  25.   hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp 
  26.   void* ANativeWindowDisplayAdapter::allocateBuffer(int width, int height, const char* format, int &bytes, int numBufs){ 
  27.     err = mANativeWindow->set_buffers_geometry( 
  28.             mANativeWindow, 
  29.             width, 
  30.             height, 
  31.             mNativeWindowPixelFormat); //NV21 
  32.   } 
  33.   frameworks/av/services/camera/libcameraservice/CameraHardwareInterface.h 
  34.   static int __set_buffers_geometry(struct preview_stream_ops* w, 
  35.                        int width, int height, int format){ 
  36.     ANativeWindow *a = anw(w); 
  37.     //#define anw(n) __to_anw(((struct camera_preview_window *)n)->user) 
  38.     //__this->mPreviewWindow.get(); 
  39.     return native_window_set_buffers_geometry(a, 
  40.                            width, height, format); 
  41.   } 
  42.   */  
  43. }  

二、数据流程


hardware/amlogic/camera/V4LCameraAdapter/V4LCameraAdapter.cpp

[cpp]  view plain  copy
  1. int V4LCameraAdapter::previewThread(){  
  2.   yuyv422_to_nv21(src,dest,width,height);  
  3.   frame.mFrameMask |= CameraFrame::PREVIEW_FRAME_SYNC;  
  4.   frame.mPixelFmt = mPixelFormat;  
  5.   ret = sendFrameToSubscribers(&frame);  
  6. }  

hardware/amlogic/camera/BaseCameraAdapter.cpp

[cpp]  view plain  copy
  1. status_t BaseCameraAdapter::sendFrameToSubscribers(CameraFrame *frame){  
  2.   ret = __sendFrameToSubscribers(frame, &mFrameSubscribers, CameraFrame::PREVIEW_FRAME_SYNC);  
  3. }  
  4. status_t BaseCameraAdapter::__sendFrameToSubscribers(CameraFrame* frame,  
  5.                                                      KeyedVector<int, frame_callback> *subscribers,  
  6.                                                      CameraFrame::FrameType frameType){  
  7.   callback = (frame_callback) subscribers->valueAt(k);  
  8.   callback(frame);  
  9. }  

hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp

[cpp]  view plain  copy
  1. void ANativeWindowDisplayAdapter::frameCallbackRelay(CameraFrame* caFrame){  
  2.   ANativeWindowDisplayAdapter *da = (ANativeWindowDisplayAdapter*) caFrame->mCookie;  
  3.   da->frameCallback(caFrame);  
  4. }  
  5. void ANativeWindowDisplayAdapter::frameCallback(CameraFrame* caFrame){  
  6.   PostFrame(df);  
  7. }  
  8. status_t ANativeWindowDisplayAdapter::PostFrame(ANativeWindowDisplayAdapter::DisplayFrame &dispFrame){  
  9.   mANativeWindow->set_crop(mANativeWindow, xOff/bytesPerPixel, yOff,  
  10.                                      (xOff/bytesPerPixel)+mPreviewWidth, yOff+mPreviewHeight);  
  11.   ret = mANativeWindow->enqueue_buffer(mANativeWindow, mBufferHandleMap[i]);  
  12. }  

frameworks/av/services/camera/libcameraservice/CameraHardwareInterface.h

[cpp]  view plain  copy
  1. static int __set_crop(struct preview_stream_ops *w,  
  2.                        int left, int top, int right, int bottom){  
  3.   ANativeWindow *a = anw(w);  
  4.   return native_window_set_crop(a, &crop);  
  5. }  
  6. static int __enqueue_buffer(struct preview_stream_ops* w,  
  7.                        buffer_handle_t* buffer){  
  8.   ANativeWindow *a = anw(w);  
  9.   return a->queueBuffer(a,  
  10.                    container_of(buffer, ANativeWindowBuffer, handle), -1);  
  11. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值