
??想了解更多關(guān)于開源的內(nèi)容,請?jiān)L問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
上一篇文章講解了編譯開源gpu驅(qū)動(dòng),并把gpu驅(qū)動(dòng)添加到編譯框架中,此時(shí)理論上gpu已經(jīng)可以調(diào)用,但是我們需要一些手段進(jìn)行測試,以確認(rèn)gpu能夠正常工作。這里分享我用過的兩個(gè)測試程序,glmark2和一個(gè)簡單的三角形繪制程序。
移植GPU過程中受到開源社區(qū)里各位大神的指導(dǎo),特別是AlgoIdeas和lhl,特此鳴謝。
1、簡單繪制三角形程序
該測試程序程序來自AlgoIdeas大神,文末附有AlgoIdeas的原文連接,大家可以進(jìn)一步細(xì)讀,關(guān)鍵代碼AlgoIdeas帖子里面已經(jīng)提供下載,請大家去原帖下載,原版程序是基于dayu200開發(fā)板的,在此我講解如何進(jìn)行適配其他設(shè)備。
(1)適配OpenHarmony窗口系統(tǒng)
OpenGL渲染出來的數(shù)據(jù)并不能直接顯示到屏幕上,需要適配對接OpenHarmony的窗口系統(tǒng),OpenHarmony提供了NativeWindow的API接口用以創(chuàng)建窗口,我們調(diào)用GPU渲染之后的數(shù)據(jù)就可以采用這個(gè)API接口進(jìn)行顯示。
下載native_window_wrapper.zip解壓放到。
【foundation/window/window_manager】目錄下,因OH版本更新,部分配置需要修改并添加部件信息,修改后的BUILD.gn如下:
# Copyright (c) Hisilicon Technologies Co., Ltd. 2021-2021. All rights reserved
import("http://build/ohos.gni")
ohos_shared_library("native_window_wrapper") {
sources = ["native_window_wrapper.cpp"]
include_dirs = [
"."
]
cflags = [
"-Wall",
"-Werror",
"-Wno-cast-qual",
"-Wno-pointer-arith",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-delete-incomplete",
]
deps = [
"http://foundation/window/window_manager/wm:libwm",
"http://foundation/graphic/graphic_2d/frameworks/surface:surface",
"http://foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client",
]
# 添加OH部件配置
part_name = "window_manager"
subsystem_name = "window"
}
(2)修改Makefile適配Mesa3d
原版例子是適配rk3568的閉源驅(qū)動(dòng)的,我們需要修改適配Mesa3d,以及適配64位系統(tǒng)。
下載native_window_ohos.zip解壓放到OH項(xiàng)目根目錄。
Makefile修改適配32位程序,rpi4舉例。
#修改OHOS_ROOT為自己的目錄
OHOS_ROOT = /home/algoideas/openharmony/master
改為
OHOS_ROOT = /home/diemit/OpenHarmony
#修改三處--sysroot
--sysroot=$(OHOS_ROOT)/out/a311d/obj/third_party/musl
改為
--sysroot=$(OHOS_ROOT)/out/rpi4/obj/third_party/musl
#修改lib鏈接
CLIBS += -L$(OHOS_ROOT)/device/soc/rockchip/hardware/gpu/lib -lmali-bifrost-g52-g2p0-ohos
CLIBS += -L$(OHOS_ROOT)/out/rk3568/packages/phone/system/lib -lhilog -lsurface.z -lutils.z
改為
CLIBS += -L$(OHOS_ROOT)/device/soc/broadcom/bcm2711/standard/hardware/gpu/lib -lgallium_dri
CLIBS += -L$(OHOS_ROOT)/out/rpi4/packages/phone/system/lib -lhilog -lsurface.z -lutils.z
Makefile修改適配64位程序,小米6舉例。
#修改OHOS_ROOT為自己的目錄
OHOS_ROOT = /home/algoideas/openharmony/master
改為
OHOS_ROOT = /home/diemit/OpenHarmony
#修改三處--sysroot
--sysroot=$(OHOS_ROOT)/out/a311d/obj/third_party/musl
改為
--sysroot=$(OHOS_ROOT)/out/sagit/obj/third_party/musl
#修改lib鏈接
CLIBS += -L$(OHOS_ROOT)/device/soc/rockchip/hardware/gpu/lib -lmali-bifrost-g52-g2p0-ohos
CLIBS += -L$(OHOS_ROOT)/out/rk3568/packages/phone/system/lib -lhilog -lsurface.z -lutils.z
改為
CLIBS += -L$(OHOS_ROOT)/device/soc/qualcomm/msm8998/hardware/gpu/lib64 -lgallium_dri
CLIBS += -L$(OHOS_ROOT)/out/sagit/packages/phone/system/lib64 -lhilog -lsurface.z -lutils.z
#修改
CFLAGS :=
#刪除
-march=armv7-a \
-mfloat-abi=softfp \
-mtune=generic-armv7-a \
-mfpu=neon \
-mthumb \
#修改
--target=arm-linux-ohosmusl \
改為
--target=aarch64-linux-ohosmusl \
(3)編譯為可執(zhí)行程序,加入OH編譯框架
進(jìn)入native_window_ohos目錄執(zhí)行make命令。
cd native_window_ohos
make
復(fù)制生成好的native_main程序到board倉下的自定義目錄,我的是test下的native_window_ohos,然后配置BUILD.gn,添加native_main為預(yù)編譯程序。
ohos_prebuilt_executable("native_main") {
source = "native_window_ohos/native_main"
part_name = "qualcomm_products"
install_images = [ "system" ]
install_enable = true
}
重新編譯系統(tǒng),刷機(jī)之后使用串口工具輸入命令直接運(yùn)行
如果一切正常,屏幕左上角會(huì)顯示通過OpenGL接口調(diào)用GPU繪制的三角形,顏色會(huì)有異常這個(gè)無所謂,三角形能正確顯示就證明Mesa3d已經(jīng)正確配置。

2、使用glmark2
glmark2是一個(gè)經(jīng)典的圖形測試程序,lhl大神gitee倉中有一份移植的代碼,但是有部分沒有實(shí)現(xiàn),這里我描述一下如何把glmark2適配到OpenHarmony。
(1)適配ohos
??lhl/glmark2_2 (gitee.com)??
去lhl大神的倉庫下載glmark2的源碼,刪除ohos文件夾,復(fù)制我們之前適配mesa3d修改好的ohos文件夾,然后修改編譯的python腳本。
ohos/build_ohos64.py:
修改
run_build_cmd += '-Dplatforms=ohos -Degl-native-platform=ohos -Ddri-drivers= -Dgallium-drivers=freedreno \
-Dvulkan-drivers= -Dgbm=enabled -Degl=enabled -Dcpp_rtti=false -Dglx=disabled -Dtools= -Ddri-search-path=/vendor/lib64/chipsetsdk '
修改后
run_build_cmd += '-Dflavors=ohos-glesv2 '
其他保持不變
(2)適配OpenHarmony本地窗口
glmark2在OpenHarmony平臺不能直接顯示,需要對接OpenHarmony的NativeWindow,需要預(yù)先完成章節(jié)1.1的適配,同時(shí)在glmark2中實(shí)現(xiàn)對窗口的調(diào)用。
修改glmark2程序中的,添加ohos_wrapper_linker.cpp
src/meson.build:
native_ohos_lib = static_library(
'native-ohos',
'native-state-ohos.cpp',
+ 'ohos_wrapper_linker.cpp',
dependencies: [libmatrix_headers_dep],
)
native_ohos_dep = declare_dependency(
src/ohos_wrapper_linker.cpp:
#include "ohos_wrapper_linker.h"
#include <dlfcn.h>
#include "log.h"
bool OhosWrapperLinker::Init()
{
wrapperModule_ = dlopen(WRAPPER_LIB_NAME, RTLD_NOW | RTLD_NOLOAD);
if (wrapperModule_ != nullptr) {
Log::debug("Module '%s' already loaded \n", WRAPPER_LIB_NAME);
} else {
Log::debug("Loading module %s\n", WRAPPER_LIB_NAME);
wrapperModule_ = dlopen(WRAPPER_LIB_NAME, RTLD_NOW);
if (wrapperModule_ == nullptr) {
Log::debug("Failed to load module: %s \n", dlerror());
return false;
}
}
using InitFunc = bool (*)(WrapperFunc *funcs);
InitFunc func = reinterpret_cast<InitFunc>(dlsym(wrapperModule_, WRAPPER_FUNC_GET));
if (func == nullptr) {
Log::debug("Failed to lookup %s function: %s\n", WRAPPER_FUNC_GET, dlerror());
dlclose(wrapperModule_);
return false;
}
if (func(&wapperFuncs_)) {
wrapper_ = wapperFuncs_.CreateWindowWrapper();
} else {
Log::debug("can not get wrapper functions \n");
return false;
}
if (wrapper_ != nullptr) {
Log::debug("wrapper init success\n");
return true;
}
return false;
}
bool OhosWrapperLinker::CreateWindow(uint32_t w, uint32_t h)
{
return wapperFuncs_.CreateWindow(wrapper_, w, h);
}
void* OhosWrapperLinker::GetWindow()
{
return wapperFuncs_.GetNativeWindow(wrapper_);
}
void OhosWrapperLinker::SetVisibility(bool visible)
{
wapperFuncs_.SetVisibility(wrapper_, visible);
}
src/ohos_wrapper_linker.h:
#include <cstdint>
extern "C" {
typedef struct {
void* (*CreateWindowWrapper)();
bool (*CreateWindow)(void* wrapper, uint32_t w, uint32_t h);
void* (*GetNativeWindow)(void* wrapper);
void (*SetVisibility)(void* wrapper, bool visible);
void (*DestroyWindowWrapper)(void* wrapper);
} WrapperFunc;
}
class OhosWrapperLinker
{
public:
bool Init();
bool CreateWindow(uint32_t w, uint32_t h);
void *GetWindow();
void SetVisibility(bool visible);
private:
static constexpr const char *WRAPPER_LIB_NAME = "libnative_window_wrapper.z.so";
static constexpr const char *WRAPPER_FUNC_GET = "GetWrapperFunc";
WrapperFunc wapperFuncs_;
void* wrapper_ = nullptr;
void *wrapperModule_ = nullptr;
};
修改glmark2,對接OpenHarmony窗口系統(tǒng)。
src/native-state-ohos.h:
#ifndef GLMARK2_NATIVE_STATE_OHOS_H_
#define GLMARK2_NATIVE_STATE_OHOS_H_
#include "native-state.h"
+#include "ohos_wrapper_linker.h"
class NativeStateOhos : public NativeState
{
@@ -27,6 +28,9 @@ class NativeStateOhos : public NativeState
/* Flips the display */
void flip();
+ private:
+ OhosWrapperLinker wrapper;
+ WindowProperties properties_;
};
#endif // GLMARK2_NATIVE_STATE_OHOS_H_
src/native-state-ohos.cpp:
#include "native-state-ohos.h"
#include "log.h"
/* Initializes the native display */
bool NativeStateOhos::init_display()
{
//Log::debug("%s@%s:%d", __FUNCTION__, __FILE__, __LINE__);
return wrapper.Init();
}
/* Gets the native display */
void *NativeStateOhos::display()
{
//Log::debug("%s@%s:%d", __FUNCTION__, __FILE__, __LINE__);
return nullptr;
}
/* Creates (or recreates) the native window */
bool NativeStateOhos::create_window(WindowProperties const &properties)
{
properties_ = properties;
return wrapper.CreateWindow(properties.width, properties.height);
}
/*
* Gets the native window and its properties.
* The dimensions may be different than the ones requested.
*/
void *NativeStateOhos::window(WindowProperties &properties)
{
properties = properties_;
return wrapper.GetWindow();
}
/* Sets the visibility of the native window */
void NativeStateOhos::visible(bool v)
{
wrapper.SetVisibility(v);
return;
}
/* Whether the user has requested an exit */
bool NativeStateOhos::should_quit()
{
return false;
}
/* Flips the display */
void NativeStateOhos::flip()
{
return;
}
小米6適配的是arm64位編譯環(huán)境,編譯過程中src/gl-state-egl.cpp會(huì)報(bào)錯(cuò),這里也順便貼出解決辦法。
- native_display_ = reinterpret_cast<EGLNativeDisplayType>(native_display);
+ native_display_ = static_cast<EGLNativeDisplayType>((intptr_t)native_display);
(3)編譯為可執(zhí)行程序,加入OH編譯框架
在glmark2目錄執(zhí)行以下命令:
python ohos/build_ohos64.py ~/ohos_beta5 sagit ~/ohos_beta5/glmark2_2/
復(fù)制build-hos/install/bin下生成好的glmark2-es2-ohos程序到board倉下的自定義目錄,我的是test下的glmark2,然后配置BUILD.gn,添加glmark2-es2-ohos為預(yù)編譯程序。
ohos_prebuilt_executable("glmark2") {
source = "glmark2/glmark2-es2-ohos"
part_name = "qualcomm_products"
install_images = [ "system" ]
install_enable = true
}
重新編譯系統(tǒng),刷機(jī),此時(shí)還不能直接運(yùn)行g(shù)lamrk2運(yùn)行需要額外的模型資源跟紋理資源,復(fù)制build-hos/install/share/glmark2下的資源到data分區(qū),對于小米6,我是進(jìn)入twrp,掛載data分區(qū),使用mtp程序直接拷貝到機(jī)器內(nèi)部,對于樹莓派4,我是讀卡器插入到Linux主機(jī),然后掛載tf卡的data分區(qū),再拷貝到data分區(qū)內(nèi),最后在小米6對應(yīng)的路徑為/data/media/glmark2/,其他開發(fā)板如果打通了hdc可通過hdc傳輸文件,理論上也可以通過配置gn預(yù)置到某個(gè)目錄,但是目前還沒研究透,打包失敗了,有知道的大神可以留言指導(dǎo)以下。
準(zhǔn)備就緒后,使用串口執(zhí)行以下命令:
默認(rèn)分辨率運(yùn)行800*600
glmark2-es2-ohos --data-path /data/media/glmark2/
指定分辨率1080*1920
glmark2-es2-ohos -s 1080*1920 --data-path /data/media/glmark2/
順利的話可以在屏幕上看到glmark2的渲染窗口。

至此OpenGL接口的測試程序如何進(jìn)行編譯以及調(diào)用驗(yàn)證講解完畢,希望能對大家有所幫助,下篇我會(huì)分享一下樹莓派4與小米6在適配gpu時(shí)遇到的問題以及解決的過程。
參考資料
??#DAYU200體驗(yàn)官#在開發(fā)板上,使用OpenGL相關(guān)API繪制基本圖形流程-開源基礎(chǔ)軟件社區(qū)-51CTO.COM??
??想了解更多關(guān)于開源的內(nèi)容,請?jiān)L問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??