編輯:關於 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)
Android 中搭建 FTP 伺服器的入門指南
先簡單介紹一下 FTP:FTP(File Transfer Protocol)即檔案傳輸協定。它的作用是讓使用者連上一台運行著 FTP 伺服器程式的遠端電腦,瀏覽遠端有哪
Android 屬性動畫完全解析:ObjectAnimator、ValueAnimator 與 AnimatorSet
屬性動畫——res/animator 所謂屬性動畫,顧名思義就是通過動畫的方式去改變對象的屬性。開始之前,我們先了解幾個關鍵概念: · Duration:動畫的持續時間,
百度地圖 POI 範圍搜尋實作:關鍵詞、建議查詢與詳情頁
你大概也有過這樣的經驗:揣著一張金融卡在街頭找 ATM。在這個場景裡,ATM 就是我們的「興趣點」(Point of Interest)——我們想知道附近有哪些 ATM,
HTC One X9 一鍵 root 教程:卡刷 SuperSU 包全流程
HTC One X9 的 root 怎麼做?這裡採用的是比較傳統也相對穩定的卡刷方式:透過第三方 Recovery 刷入 zip 格式的 root 包。這個方法的成功率通