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。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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 (这次依赖的库少了很多)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值