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喎�"/kf/ware/vc/" target="_blank" class="keylink">vcGxhdGZvcm1faGFyZHdhcmVfYXRtZWwvYmxvYi9hbmRyb2lkNHNhbV92NC4wL2NhbWVyYS9WNEwyQ2FtZXJhLmNwcAo8YnI+CgoKyerH63ZpZGVvIGNhcHR1cmUgYnVmZmVyCjxicj4KCkNhbWVyYUhhcmR3YXJlU2FtPHN0cm9uZz46Ojwvc3Ryb25nPnN0YXJ0UHJldmlld0ludGVybmFsCiAtPiBWNEwyQ2FtZXJhPHN0cm9uZz46Ojwvc3Ryb25nPnN0YXJ0UHJldmlldwogLT4gaXNpX3Y0bDJfcmVxYnVmcwo8YnI+Cgo8cHJlIGNsYXNzPQ=="brush:java;"> ret = ioctl(fp, VIDIOC_REQBUFS, &req); 
内存映射 CameraHardwareSam :: startPreviewInternal 
?
1
2
3
4
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
?
1
ret = ioctl(win->fd, VIDIOC_REQBUFS, &reqbuf);

内存映射  hwc_prepare ->  assign_heo_overlay_window ->  v4l2_overlay_map_buf
?
1
2
3
*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 
?
1
2
3
4
5
6
7
8
9
10
11
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++;
}

你可能感兴趣的:(多媒体)