天天看點

Android用surface直接顯示yuv資料Android用surface直接顯示yuv資料

Android用surface直接顯示yuv資料

原文位址:

http://blog.csdn.net/tung214/article/details/37651825

上一篇文章主要是參照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;//設定寬高以及顔色空間yuv420  
native_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*);//填充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裡的code  
using namespace android;  

//将x規整為y的倍數,也就是将x按y對齊  
static int ALIGN(int x, int y) {  
    // y must be a power of 2.  
    return (x + y - ) & ~(y - );  
}  

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 + ) & ~;//按2對齊  
    int bufHeight = (mCropHeight + ) & ~;  

    CHECK_EQ(,  
            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(,  
            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(, native_window_set_buffers_geometry(  
                mNativeWindow.get(),  
                bufWidth,  
                bufHeight,  
                halFormat));  

    ANativeWindowBuffer *buf;//描述buffer  
    //申請一塊空閑的圖形緩沖區  
    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),  
            &buf)) != ) {  
        ALOGW("Surface::dequeueBuffer returned error %d", err);  
        return;  
    }  

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();  
    Rect bounds(mCropWidth, mCropHeight);  
    void *dst;  
    CHECK_EQ(, 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 / , );//1行v/u的大小  
        size_t dst_c_size = dst_c_stride * buf->height / ;//u/v的大小  
        memcpy(dst, data, dst_y_size + dst_c_size*);//将yuv資料copy到圖形緩沖區  
    }  

    CHECK_EQ(, mapper.unlock(buf->handle));  
    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,  
            -)) != ) {  
        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,,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 -;  
    //建立surface  
    sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"),  
            dinfo.w, dinfo.h, PIXEL_FORMAT_RGBA_8888, );  

/*************************get yuv data from file;****************************************/            
    printf("[%s][%d]\n",__FILE__,__LINE__);  
    int width,height;  
    width = ;  
    height = ;  
    int size = width * height * /;  
    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();//設定Z坐标  
    surfaceControl->setPosition(, );//以左上角為(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 ;  
}  
           

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

LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  

LOCAL_SRC_FILES:= \  
    main.cpp  

LOCAL_SHARED_LIBRARIES := \  
    libcutils \  
    libutils \  
    libbinder \  
    libui \  
    libgui \  
    libstagefright_foundation  

LOCAL_MODULE:= MyShowYUV  

LOCAL_MODULE_TAGS := tests  

include $(BUILD_EXECUTABLE)  
           

用Java建立UI操作surface來顯示yuv資料

下面用Java建立UI并聯合JNI層操作surface來直接顯示yuv資料(yv12),開發環境為Android 4.4。

package com.example.myyuvviewer;  

import java.io.File;  
import java.io.FileInputStream;  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Environment;  
import android.util.Log;  
import android.view.Surface;  
import android.view.SurfaceHolder;  
import android.view.SurfaceHolder.Callback;  
import android.view.SurfaceView;  

public class MainActivity extends Activity {  

    final private String TAG = "MyYUVViewer";  
    final private String FILE_NAME = "yuv_320_240.yuv";  
    private int width = ;  
    private int height = ;  
    private int size = width * height * /;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        nativeTest();  
        SurfaceView surfaceview = (SurfaceView) findViewById(R.id.surfaceView);  
        SurfaceHolder holder = surfaceview.getHolder();  
        holder.addCallback(new Callback(){  

            @Override  
            public void surfaceCreated(SurfaceHolder holder) {  
                // TODO Auto-generated method stub  
                Log.d(TAG,"surfaceCreated");  
                byte[]yuvArray = new byte[size];  
                readYUVFile(yuvArray, FILE_NAME);  
                nativeSetVideoSurface(holder.getSurface());  
                nativeShowYUV(yuvArray,width,height);  
            }  

            @Override  
            public void surfaceChanged(SurfaceHolder holder, int format,  
                    int width, int height) {  
                // TODO Auto-generated method stub  

            }  

            @Override  
            public void surfaceDestroyed(SurfaceHolder holder) {  
                // TODO Auto-generated method stub  

            }});  
    }  

    private boolean readYUVFile(byte[] yuvArray,String filename){  
        try {  
            // 如果手機插入了SD卡,而且應用程式具有通路SD的權限  
            if (Environment.getExternalStorageState().equals(  
                    Environment.MEDIA_MOUNTED)) {  
                // 擷取SD卡對應的存儲目錄  
                File sdCardDir = Environment.getExternalStorageDirectory();  
                // 擷取指定檔案對應的輸入流  
                FileInputStream fis = new FileInputStream(  
                        sdCardDir.getCanonicalPath() +"/" + filename);  
                fis.read(yuvArray, , size);  
                fis.close();  
                return true;  
            } else {  
                return false;  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
    }  
    private native void nativeTest();  
    private native boolean nativeSetVideoSurface(Surface surface);  
    private native void nativeShowYUV(byte[] yuvArray,int width,int height);  
    static {  
        System.loadLibrary("showYUV");  
    }  
}  
           

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:orientation="vertical" >    

    <SurfaceView    
        android:id="@+id/surfaceView"    
        android:layout_width="fill_parent"    
        android:layout_height="360dp" />   

</LinearLayout>  
           

JNI層,showYUV.cpp(libshowyuv.so)采用動态注冊JNI函數的方法.

#include <jni.h>  
#include <android_runtime/AndroidRuntime.h>  
#include <android_runtime/android_view_Surface.h>  
#include <gui/Surface.h>  
#include <assert.h>  
#include <utils/Log.h>  
#include <JNIHelp.h>  
#include <media/stagefright/foundation/ADebug.h>  
#include <ui/GraphicBufferMapper.h>  
#include <cutils/properties.h>  
using namespace android;  

static sp<Surface> surface;  

static int ALIGN(int x, int y) {  
    // y must be a power of 2.  
    return (x + y - ) & ~(y - );  
}  

static void render(  
        const void *data, size_t size, const sp<ANativeWindow> &nativeWindow,int width,int height) {  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    sp<ANativeWindow> mNativeWindow = nativeWindow;  
    int err;  
    int mCropWidth = width;  
    int mCropHeight = height;  

    int halFormat = HAL_PIXEL_FORMAT_YV12;//顔色空間  
    int bufWidth = (mCropWidth + ) & ~;//按2對齊  
    int bufHeight = (mCropHeight + ) & ~;  

    CHECK_EQ(,  
            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(,  
            native_window_set_scaling_mode(  
            mNativeWindow.get(),  
            NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));  

    // Width must be multiple of 32???  
    //很重要,配置寬高和和指定顔色空間yuv420  
    //如果這裡不配置好,下面deque_buffer隻能去申請一個預設寬高的圖形緩沖區  
    CHECK_EQ(, native_window_set_buffers_geometry(  
                mNativeWindow.get(),  
                bufWidth,  
                bufHeight,  
                halFormat));  


    ANativeWindowBuffer *buf;//描述buffer  
    //申請一塊空閑的圖形緩沖區  
    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(),  
            &buf)) != ) {  
        ALOGW("Surface::dequeueBuffer returned error %d", err);  
        return;  
    }  

    GraphicBufferMapper &mapper = GraphicBufferMapper::get();  

    Rect bounds(mCropWidth, mCropHeight);  

    void *dst;  
    CHECK_EQ(, 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 / , );//1行v/u的大小  
        size_t dst_c_size = dst_c_stride * buf->height / ;//u/v的大小  

        memcpy(dst, data, dst_y_size + dst_c_size*);//将yuv資料copy到圖形緩沖區  
    }  

    CHECK_EQ(, mapper.unlock(buf->handle));  

    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf,  
            -)) != ) {  
        ALOGW("Surface::queueBuffer returned error %d", err);  
    }  
    buf = NULL;  
}  

static void nativeTest(){  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
}  

static jboolean  
nativeSetVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface){  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    surface = android_view_Surface_getSurface(env, jsurface);  
    if(android::Surface::isValid(surface)){  
        ALOGE("surface is valid ");  
    }else {  
        ALOGE("surface is invalid ");  
        return false;  
    }  
    ALOGE("[%s][%d]\n",__FILE__,__LINE__);  
    return true;  
}  
static void  
nativeShowYUV(JNIEnv *env, jobject thiz,jbyteArray yuvData,jint width,jint height){  
    ALOGE("width = %d,height = %d",width,height);  
    jint len = env->GetArrayLength(yuvData);  
    ALOGE("len = %d",len);  
    jbyte *byteBuf = env->GetByteArrayElements(yuvData, );  
    render(byteBuf,len,surface,width,height);  
}  
static JNINativeMethod gMethods[] = {  
    {"nativeTest",                  "()V",                              (void *)nativeTest},  
    {"nativeSetVideoSurface",       "(Landroid/view/Surface;)Z",        (void *)nativeSetVideoSurface},  
    {"nativeShowYUV",               "([BII)V",                          (void *)nativeShowYUV},  
};  

static const char* const kClassPathName = "com/example/myyuvviewer/MainActivity";  

// This function only registers the native methods  
static int register_com_example_myyuvviewer(JNIEnv *env)  
{  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    return AndroidRuntime::registerNativeMethods(env,  
                kClassPathName, gMethods, NELEM(gMethods));  
}  

jint JNI_OnLoad(JavaVM* vm, void* reserved)  
{  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
    JNIEnv* env = NULL;  
    jint result = -;  

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
        ALOGE("ERROR: GetEnv failed\n");  
        goto bail;  
    }  
    assert(env != NULL);  
    ALOGE("[%s]%d",__FILE__,__LINE__);  
   if (register_com_example_myyuvviewer(env) < ) {  
        ALOGE("ERROR: MediaPlayer native registration failed\n");  
        goto bail;  
    }  

    /* success -- return valid version number */  
    result = JNI_VERSION_1_4;  

bail:  
    return result;  
}  
           

Android.mk

LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  

LOCAL_SRC_FILES:= \  
    showYUV.cpp  

LOCAL_SHARED_LIBRARIES := \  
    libcutils \  
    libutils \  
    libbinder \  
    libui \  
    libgui \  
    libandroid_runtime \  
    libstagefright_foundation  

LOCAL_MODULE:= libshowYUV  

LOCAL_MODULE_TAGS := tests  

include $(BUILD_SHARED_LIBRARY)  
           

生成的so檔案複制到Java項目裡 與src并列的libs/armeabi目錄下,沒有就手動建立目錄,

這樣Eclipse會自動把so庫打包進apk。