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

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

  1. LOCAL_PATH:= $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3.   
  4. LOCAL_SRC_FILES:= \  
  5.     main.cpp  
  6.       
  7. LOCAL_SHARED_LIBRARIES := \  
  8.     libcutils \  
  9.     libutils \  
  10.     libbinder \  
  11.     libui \  
  12.     libgui \  
  13.     libstagefright_foundation  
  14.       
  15. LOCAL_MODULE:= MyShowYUV  
  16.   
  17. LOCAL_MODULE_TAGS := tests  
  18.   
  19. include $(BUILD_EXECUTABLE)  
轉載請註明出處http://blog.csdn.net/tung214/article/details/37651825
發佈了16 篇原創文章 · 獲贊 20 · 訪問量 94萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章