1. 程式人生 > >Android用surface直接顯示yuv資料(二)

Android用surface直接顯示yuv資料(二)

上一篇文章主要是參照AwesomePlayer直接用SoftwareRenderer類來顯示yuv,為了能用到這個類,不惜依賴了libstagefright、libstagefright_color_conversion等動態靜態庫,從而造成程式具有很高的耦合度,也不便於我們理解yuv資料直接顯示的深層次原因。

    於是我開始研究SoftwareRenderer的具體實現,我們來提取SoftwareRenderer的核心程式碼,自己來實現yuv的顯示。

    SoftwareRenderer就只有三個方法,一個建構函式,一個解構函式,還有一個負責顯示的render方法。構造方法裡有個很重要的地方native_window_set_buffers_geometry這裡是配置即將申請的圖形緩衝區的寬高和顏色空間,忽略了這個地方,畫面將用預設的值顯示,將造成顯示不正確。render函式裡最重要的三個地方,一個的dequeBuffer,一個是mapper,一個是queue_buffer。

  1. native_window_set_buffers_geometry;//設定寬高以及顏色空間yuv420
  2. native_window_dequeue_buffer_and_wait;//根據以上配置申請圖形緩衝區
  3. mapper.lock(buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//將申請到的圖形緩衝區跨程序對映到使用者空間
  4. memcpy(dst, data, dst_y_size + dst_c_size*2);//填充yuv資料到圖形緩衝區
  5. mNativeWindow->queueBuffer;//顯示

以上五步是surface顯示圖形必不可少的五步。

有了以上分析,我們直接上程式碼:(yuv資料下載地址點選開啟連結,放到sdcard)

main.cpp

  1. #include <cutils/memory.h>
  2. #include <unistd.h>
  3. #include <utils/Log.h>
  4. #include <binder/IPCThreadState.h>
  5. #include <binder/ProcessState.h>
  6. #include <binder/IServiceManager.h>
  7. #include <media/stagefright/foundation/ADebug.h>
  8. #include <gui/Surface.h>
  9. #include <gui/SurfaceComposerClient.h>
  10. #include <gui/ISurfaceComposer.h>
  11. #include <ui/DisplayInfo.h>
  12. #include <android/native_window.h>
  13. #include <system/window.h>
  14. #include <ui/GraphicBufferMapper.h>
  15. //ANativeWindow 就是surface,對應surface.cpp裡的code
  16. usingnamespace android;  
  17. //將x規整為y的倍數,也就是將x按y對齊
  18. staticint ALIGN(int x, int y) {  
  19.     // y must be a power of 2.
  20.     return (x + y - 1) & ~(y - 1);  
  21. }  
  22. void render(  
  23.         constvoid *data, size_t size, const sp<ANativeWindow> &nativeWindow,int width,int height) {  
  24.     sp<ANativeWindow> mNativeWindow = nativeWindow;  
  25.     int err;  
  26.     int mCropWidth = width;  
  27.     int mCropHeight = height;  
  28.     int halFormat = HAL_PIXEL_FORMAT_YV12;//顏色空間
  29.     int bufWidth = (mCropWidth + 1) & ~1;//按2對齊
  30.     int bufHeight = (mCropHeight + 1) & ~1;  
  31.     CHECK_EQ(0,  
  32.             native_window_set_usage(  
  33.             mNativeWindow.get(),  
  34.             GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN  
  35.             | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));  
  36.     CHECK_EQ(0,  
  37.             native_window_set_scaling_mode(  
  38.             mNativeWindow.get(),  
  39.             NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));  
  40.     // Width must be multiple of 32???
  41.     //很重要,配置寬高和和指定顏色空間yuv420
  42.     //如果這裡不配置好,下面deque_buffer只能去申請一個預設寬高的圖形緩衝區
  43.     CHECK_EQ(0, native_window_set_buffers_geometry(  
  44.                 mNativeWindow.get(),  
  45.                 bufWidth,  
  46.                 bufHeight,  
  47.                 halFormat));  
  48.     ANativeWindowBuffer *buf;//描述buffer
  49.     //申請一塊空閒的圖形緩衝區
  50.     if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),  
  51.             &buf)) != 0) {  
  52.         ALOGW("Surface::dequeueBuffer returned error %d", err);  
  53.         return;  
  54.     }  
  55.     GraphicBufferMapper &mapper = GraphicBufferMapper::get();  
  56.     Rect bounds(mCropWidth, mCropHeight);  
  57.     void *dst;  
  58.     CHECK_EQ(0, mapper.lock(//用來鎖定一個圖形緩衝區並將緩衝區對映到使用者程序
  59.                 buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//dst就指向圖形緩衝區首地址
  60.     if (true){  
  61.         size_t dst_y_size = buf->stride * buf->height;  
  62.         size_t dst_c_stride = ALIGN(buf->stride / 2, 16);//1行v/u的大小
  63.         size_t dst_c_size = dst_c_stride * buf->height / 2;//u/v的大小
  64.         memcpy(dst, data, dst_y_size + dst_c_size*2);//將yuv資料copy到圖形緩衝區
  65.     }  
  66.     CHECK_EQ(0, mapper.unlock(buf->handle));  
  67.     if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,  
  68.             -1)) != 0) {  
  69.         ALOGW("Surface::queueBuffer returned error %d", err);  
  70.     }  
  71.     buf = NULL;  
  72. }  
  73. bool getYV12Data(constchar *path,unsigned char * pYUVData,int size){  
  74.     FILE *fp = fopen(path,"rb");  
  75.     if(fp == NULL){  
  76.         printf("read %s fail !!!!!!!!!!!!!!!!!!!\n",path);  
  77.         returnfalse;  
  78.     }  
  79.     fread(pYUVData,size,1,fp);  
  80.     fclose(fp);  
  81.     returntrue;  
  82. }  
  83. int main(void){  
  84.     // set up the thread-pool
  85.     sp<ProcessState> proc(ProcessState::self());  
  86.     ProcessState::self()->startThreadPool();  
  87.     // create a client to surfaceflinger
  88.     sp<SurfaceComposerClient> client = new SurfaceComposerClient();  
  89.     sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(  
  90.             ISurfaceComposer::eDisplayIdMain));  
  91.     DisplayInfo dinfo;  
  92.     //獲取螢幕的寬高等資訊
  93.     status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);  
  94.     printf("w=%d,h=%d,xdpi=%f,ydpi=%f,fps=%f,ds=%f\n",   
  95.         dinfo.w, dinfo.h, dinfo.xdpi, dinfo.ydpi, dinfo.fps, dinfo.density);  
  96.     if (status)  
  97.         return -1;  
  98.     //建立surface
  99.     sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"),  
  100.             dinfo.w, dinfo.h, PIXEL_FORMAT_RGBA_8888, 0);  
  101. /*************************get yuv data from file;****************************************/
  102.     printf("[%s][%d]\n",__FILE__,__LINE__);  
  103.     int width,height;  
  104.     width = 320;  
  105.     height = 240;  
  106.     int size = width * height * 3/2;  
  107.     unsigned char *data = new unsigned char[size];  
  108.     constchar *path = "/mnt/sdcard/yuv_320_240.yuv";  
  109.     getYV12Data(path,data,size);//get yuv data from file;
  110. /*********************配置surface*******************************************************************/
  111.     SurfaceComposerClient::openGlobalTransaction();  
  112.     surfaceControl->setLayer(100000);//設定Z座標
  113.     surfaceControl->setPosition(100, 100);//以左上角為(0,0)設定顯示位置
  114.     surfaceControl->setSize(width, height);//設定視訊顯示大小
  115.     SurfaceComposerClient::closeGlobalTransaction();  
  116.     sp<Surface> surface = surfaceControl->getSurface();  
  117.     printf("[%s][%d]\n",__FILE__,__LINE__);  
  118. /**********************顯示yuv資料******************************************************************/
  119.     render(data,size,surface,width,height);  
  120.     printf("[%s][%d]\n",__FILE__,__LINE__);  
  121.     IPCThreadState::self()->joinThreadPool();//可以保證畫面一直顯示,否則瞬間消失
  122.     IPCThreadState::self()->stopProcess();  
  123.     return 0;  
  124. }  

Android.mk (這次依賴的庫少了很多)

  1. LOCAL_PATH:= $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:= \  
  4.     main.cpp  
  5. LOCAL_SHARED_LIBRARIES := \  
  6.     libcutils \  
  7.     libutils \  
  8.     libbinder \  
  9.     libui \  
  10.     libgui \  
  11.     libstagefright_foundation  
  12. LOCAL_MODULE:= MyShowYUV  
  13. LOCAL_MODULE_TAGS := tests  
  14. include $(BUILD_EXECUTABLE)  
轉載請註明出處http://blog.csdn.net/tung214/article/details/37651825