Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 使用 Surface 直接顯示 YUV 資料(二):擺脫高耦合依賴

編輯:關於 Android 程式設計

上一篇主要是參照 AwesomePlayer,直接用 SoftwareRenderer 類別來顯示 YUV。但為了使用這個類別,得依賴 libstagefright、libstagefright_color_conversion 等動態與靜態庫,導致程式耦合度過高,也不利於理解 YUV 直接顯示背後的原理。

於是我開始研究 SoftwareRenderer 的實作,試著提取其核心程式碼,自己來完成 YUV 的顯示。

SoftwareRenderer 只有三個方法:建構子、解構子,以及負責顯示的 render 方法。建構子裡最關鍵的是 native_window_set_buffers_geometry,它用來配置即將申請的圖形緩衝區的寬高與色彩空間;若忽略這一步,畫面會用預設值顯示而導致不正確。render 函式裡最重要的則是 dequeue、lock、queue 三步。

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*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 <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/ISurfaceComposer.h>
#include <system/window.h>
#include <ui/GraphicBufferMapper.h>

using namespace android;

// 將 x 規整為 y 的倍數
static int ALIGN(int x, int y) {
    return (x + y - 1) & ~(y - 1);
}

void render(const void *data, size_t size, const sp<Surface> &nativeWindow, int width, int height) {
    sp<Surface> 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;

    // 配置寬高與色彩空間 YUV420,若不安置好,dequeue 只能申請預設寬高的緩衝區
    CHECK_EQ(0, native_window_set_buffers_geometry(
            mNativeWindow.get(), bufWidth, bufHeight, halFormat));

    ANativeWindowBuffer *buf;
    if ((err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &buf)) != 0) {
        ALOGW("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));

    size_t dst_y_size = buf->stride * buf->height;
    size_t dst_c_stride = ALIGN(buf->stride / 2, 16);
    size_t dst_c_size = dst_c_stride * buf->height / 2;
    memcpy(dst, data, dst_y_size + dst_c_size * 2); // 將 YUV 資料 copy 進緩衝區

    CHECK_EQ(0, mapper.unlock(buf->handle));
    mNativeWindow->queueBuffer(mNativeWindow.get(), buf, -1);
    buf = NULL;
}

main 函式中,先取得螢幕資訊,建立 Surface 並設定圖層位置與大小,最後呼叫 render 顯示,並透過 joinThreadPool 讓畫面持續顯示而不會瞬間消失。

這次 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)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved