当前位置: 代码迷 >> Android >> Android Camera预览历程数据流浅析
  详细解决方案

Android Camera预览历程数据流浅析

热度:6   发布时间:2016-04-28 05:47:34.0
Android Camera预览过程数据流浅析
硬件平台:Atmel SAMA5D3 SoC + OV2640 Camera Sensor
Android版本:4.2.2

mediaserver进程是Camera Service的容器进程,它会动态加载Camera HAL和Gralloc HAL。
视频数据帧首先必须从Camera驱动程序到达Camera硬件抽象层。
在Camera硬件抽象层,视频数据帧被从video capture buffer拷贝到gralloc buffer。

surfaceflinger进程作为显示服务器会动态加载HWComposer HAL和Gralloc HAL。
在HWComposer硬件抽象层,会把数据帧从gralloc buffer拷贝到video output buffer。

经过上述过程,Camera Sensor采集的图像最终通过LCDC HEO显示在显示屏上。


图中红色实线为视频数据帧流向,不带箭头的红线连接的两端为同一块内存。

涉及三块内存,分别如下:
video capture buffer /dev/video1
gralloc buffer       匿名共享内存 mediaserver进程和surfacelinger进程都可以访问这块内存 
video output buffer /dev/video0

进行了两次数据拷贝操作,如下:
media server进程
Camera HAL    video capture buffer -> gralloc buffer

surfaceflinger进程
HWCompser HAL   gralloc buffer -> video output buffer


video capture buffer的分配与内存映射
https://github.com/Android4SAM/platform_hardware_atmel/blob/android4sam_v4.0/camera/CameraHardwareSam.cpp
https://github.com/Android4SAM/platform_hardware_atmel/blob/android4sam_v4.0/camera/V4L2Camera.cpp

申请video capture buffer

CameraHardwareSam::startPreviewInternal -> V4L2Camera::startPreview -> isi_v4l2_reqbufs

    ret = ioctl(fp, VIDIOC_REQBUFS, &req);

内存映射
CameraHardwareSam::startPreviewInternal

    mPreviewHeap = mGetMemoryCb((int)mV4L2Camera->getCameraFd(),                                aligned_buffer_size,                                kBufferCount,                                0); 

获得gralloc buffer以及视频数据帧从video capture buffer到gralloc buffer的拷贝
https://github.com/Android4SAM/platform_hardware_atmel/blob/android4sam_v4.0/camera/CameraHardwareSam.cpp


CameraHardwareSam::previewThread
    if (mPreviewWindow && mGrallocHal) {        buffer_handle_t *buf_handle;        int stride;        if (0 != mPreviewWindow->dequeue_buffer(mPreviewWindow, &buf_handle, &stride)) {            ALOGE("Could not dequeue gralloc buffer!\n");            goto callbacks;        }        void *vaddr;        if (!mGrallocHal->lock(mGrallocHal,                               *buf_handle,                               GRALLOC_USAGE_SW_WRITE_OFTEN,                               0, 0, width, height, &vaddr)) {            char *frame = ((char *)mPreviewHeap->data) + offset;            // the code below assumes YUV, not RGB            {                int h;                char *src = frame;                char *ptr = (char *)vaddr;                memcpy(ptr, src, frame_size);                //YUY2toYV12(frame, vaddr, width, height);            }            mGrallocHal->unlock(mGrallocHal, *buf_handle);        }        else            ALOGE("%s: could not obtain gralloc buffer", __func__);        if (0 != mPreviewWindow->enqueue_buffer(mPreviewWindow, buf_handle)) {            ALOGE("Could not enqueue gralloc buffer!\n");            goto callbacks;        }    }

video output buffer的分配与内存映射
https://github.com/Android4SAM/platform_hardware_atmel/blob/android4sam_v4.0/hwcomposer/hwcomposer.cpp
https://github.com/Android4SAM/platform_hardware_atmel/blob/android4sam_v4.0/hwcomposer/v4l2_utils.cpp


申请video ouput buffer
hwc_prepare -> assign_heo_overlay_window -> v4l2_overlay_req_buf
    ret = ioctl(win->fd, VIDIOC_REQBUFS, &reqbuf);

内存映射
hwc_prepare -> assign_heo_overlay_window -> v4l2_overlay_map_buf
    *len = buf.length;    *start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED,            fd, buf.m.offset);

视频数据帧从gralloc buffer到video output buffer的拷贝

hwc_set ->  copy_heo_src_content

    for (unsigned int i = 0; i < cur_layer->visibleRegionScreen.numRects; i++) {        uint8_t *cur_dst_addr = dst_addr;        uint8_t *cur_src_addr = src_addr;        for (int j = 0; j < h ; j++) {            memcpy(cur_dst_addr, cur_src_addr, cpy_size);            cur_dst_addr = &cur_dst_addr[cpy_size];            cur_src_addr = &cur_src_addr[(cur_layer->displayFrame.right - cur_layer->displayFrame.left) * (prev_handle->uiBpp / 8)];        }        cur_rect++;    }



  相关解决方案