Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Camera預覽過程數據流淺析

Android Camera預覽過程數據流淺析

編輯:關於Android編程

硬件平台: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
    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++;
    }



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved