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

來源:互聯網
上載者:User

標籤:softwarerenderer   android4.4   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。

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

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

有了以上分析,我們直接上代碼:(yuv資料點擊開啟連結,放到sdcard)

main.cpp

#include <cutils/memory.h>#include <unistd.h>#include <utils/Log.h>#include <binder/IPCThreadState.h>#include <binder/ProcessState.h>#include <binder/IServiceManager.h>#include <media/stagefright/foundation/ADebug.h>#include <gui/Surface.h>#include <gui/SurfaceComposerClient.h>#include <gui/ISurfaceComposer.h>#include <ui/DisplayInfo.h>#include <android/native_window.h>#include <system/window.h>#include <ui/GraphicBufferMapper.h>//ANativeWindow 就是surface,對應surface.cpp裡的codeusing namespace android;//將x規整為y的倍數,也就是將x按y對齊static int ALIGN(int x, int y) {    // y must be a power of 2.    return (x + y - 1) & ~(y - 1);}void render(        const void *data, size_t size, const sp<ANativeWindow> &nativeWindow,int width,int height) {    sp<ANativeWindow> mNativeWindow = nativeWindow;    int err;int mCropWidth = width;int mCropHeight = height;int halFormat = HAL_PIXEL_FORMAT_YV12;//色彩空間    int bufWidth = (mCropWidth + 1) & ~1;//按2對齊    int bufHeight = (mCropHeight + 1) & ~1;CHECK_EQ(0,            native_window_set_usage(            mNativeWindow.get(),            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN            | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));    CHECK_EQ(0,            native_window_set_scaling_mode(            mNativeWindow.get(),            NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));    // Width must be multiple of 32???//很重要,配置寬高和和指定色彩空間yuv420//如果這裡不配置好,下面deque_buffer只能去申請一個預設寬高的圖形緩衝區    CHECK_EQ(0, native_window_set_buffers_geometry(                mNativeWindow.get(),                bufWidth,                bufHeight,                halFormat));ANativeWindowBuffer *buf;//描述buffer//申請一塊閒置圖形緩衝區    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),            &buf)) != 0) {        ALOGW("Surface::dequeueBuffer returned error %d", err);        return;    }    GraphicBufferMapper &mapper = GraphicBufferMapper::get();    Rect bounds(mCropWidth, mCropHeight);    void *dst;    CHECK_EQ(0, mapper.lock(//用來鎖定一個圖形緩衝區並將緩衝區映射到使用者進程                buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst));//dst就指向圖形緩衝區首地址    if (true){        size_t dst_y_size = buf->stride * buf->height;        size_t dst_c_stride = ALIGN(buf->stride / 2, 16);//1行v/u的大小        size_t dst_c_size = dst_c_stride * buf->height / 2;//u/v的大小                memcpy(dst, data, dst_y_size + dst_c_size*2);//將yuv資料copy到圖形緩衝區    }    CHECK_EQ(0, mapper.unlock(buf->handle));    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,            -1)) != 0) {        ALOGW("Surface::queueBuffer returned error %d", err);    }    buf = NULL;}bool getYV12Data(const char *path,unsigned char * pYUVData,int size){FILE *fp = fopen(path,"rb");if(fp == NULL){printf("read %s fail !!!!!!!!!!!!!!!!!!!\n",path);return false;}fread(pYUVData,size,1,fp);fclose(fp);return true;}int main(void){// set up the thread-pool    sp<ProcessState> proc(ProcessState::self());    ProcessState::self()->startThreadPool();// create a client to surfaceflinger    sp<SurfaceComposerClient> client = new SurfaceComposerClient();sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(            ISurfaceComposer::eDisplayIdMain));DisplayInfo dinfo;//擷取螢幕的寬高等資訊    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);printf("w=%d,h=%d,xdpi=%f,ydpi=%f,fps=%f,ds=%f\n",         dinfo.w, dinfo.h, dinfo.xdpi, dinfo.ydpi, dinfo.fps, dinfo.density);    if (status)        return -1;//建立surface    sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"),            dinfo.w, dinfo.h, PIXEL_FORMAT_RGBA_8888, 0);/*************************get yuv data from file;****************************************/printf("[%s][%d]\n",__FILE__,__LINE__);int width,height;width = 320;height = 240;int size = width * height * 3/2;unsigned char *data = new unsigned char[size];const char *path = "/mnt/sdcard/yuv_320_240.yuv";getYV12Data(path,data,size);//get yuv data from file;/*********************配置surface*******************************************************************/    SurfaceComposerClient::openGlobalTransaction();    surfaceControl->setLayer(100000);//設定Z座標surfaceControl->setPosition(100, 100);//以左上方為(0,0)設定顯示位置surfaceControl->setSize(width, height);//設定視頻顯示大小    SurfaceComposerClient::closeGlobalTransaction();sp<Surface> surface = surfaceControl->getSurface();printf("[%s][%d]\n",__FILE__,__LINE__);/**********************顯示yuv資料******************************************************************/render(data,size,surface,width,height);printf("[%s][%d]\n",__FILE__,__LINE__);IPCThreadState::self()->joinThreadPool();//可以保證畫面一直顯示,否則瞬間消失    IPCThreadState::self()->stopProcess();return 0;}

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

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= main.cppLOCAL_SHARED_LIBRARIES := libcutils libutils libbinder     libui     libgui libstagefright_foundationLOCAL_MODULE:= MyShowYUVLOCAL_MODULE_TAGS := testsinclude $(BUILD_EXECUTABLE)
轉載請註明出處http://blog.csdn.net/tung214/article/details/37651825

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.