鸿蒙移植指南GPU测试程序编译
但是我们还需要一些手段进行测试,以确认 GPU 能够正常工作。这里分享我用过的两个测试程序,glmark2 和一个简单的三角形绘制程序。
简单绘制三角形程序
该测试程序程序来自 AlgoIdeas 大神,文末附有 AlgoIdeas 的原文连接,大家可以进一步细读。
关键代码 AlgoIdeas 帖子里面已经提供下载,请大家去原帖下载,原版程序是基于 dayu200 开发板的,在此我讲解如何进行适配其他设备。 ①适配 OpenHarmony 窗口系统
OpenGL 渲染出来的数据并不能直接显示到屏幕上,需要适配对接 OpenHarmony 的窗口系统。
OpenHarmony 提供了 NativeWindow 的 API 接口用以创建窗口,我们调用 GPU 渲染之后的数据就可以采用这个 API 接口进行显示。
下载 native_window_wrapper.zip 解压放到: foundation/window/window_manager
目录下,因 OH 版本更新,部分配置需要修改并添加部件信息,修改后的 BUILD.gn 如下: # Copyright (c) Hisilicon Technologies Co., Ltd. 2021-2021. All rights reserved import("//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 = [ "//foundation/window/window_manager/wm:libwm", "//foundation/graphic/graphic_2d/frameworks/surface:surface", "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", ] # 添加OH部件配置 part_name = "window_manager" subsystem_name = "window" }
②修改 Makefile 适配 Mesa3d
原版例子是适配 rk3568 的闭源驱动的,我们需要修改适配 Mesa3d,以及适配 64 位系统。
下载 native_window_ohos.zip 解压放到 OH 项目根目录。
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
③编译为可执行程序,加入 OH 编译框架
进入 native_window_ohos 目录执行 make 命令: cd native_window_ohos make
复制生成好的 native_main 程序到 board 仓下的自定义目录,我的是 test 下的 native_window_ohos,然后配置 BUILD.gn,添加 native_main 为预编译程序。 ohos_prebuilt_executable("native_main") { source = "native_window_ohos/native_main" part_name = "qualcomm_products" install_images = [ "system" ] install_enable = true }
重新编译系统,刷机之后使用串口工具输入命令直接运行: native_main
如果一切正常,屏幕左上角会显示通过 OpenGL 接口调用 GPU 绘制的三角形,颜色会有异常这个无所谓,三角形能正确显示就证明 Mesa3d 已经正确配置。
使用 glmark2
glmark2 是一个经典的图形测试程序,lhl 大神 gitee 仓中有一份移植的代码,但是有部分没有实现,这里我描述一下如何把 glmark2 适配到 OpenHarmony。 ①适配 ohos
lhl/glmark2_2: https://gitee.com/honglianglin/glmark2_2
去 lhl 大神的仓库下载 glmark2 的源码,删除 ohos 文件夹,复制我们之前适配 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 " 其他保持不变②适配 OpenHarmony 本地窗口
glmark2 在 OpenHarmony 平台不能直接显示,需要对接 OpenHarmony 的 NativeWindow,需要预先完成章节 1.1 的适配,同时在 glmark2 中实现对窗口的调用。
修改 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 #include "log.h" bool OhosWrapperLinker::Init() { wrapperModule_ = dlopen(WRAPPER_LIB_NAME, RTLD_NOW | RTLD_NOLOAD); if (wrapperModule_ != nullptr) { Log::debug("Module "%s" already loaded ", WRAPPER_LIB_NAME); } else { Log::debug("Loading module %s ", WRAPPER_LIB_NAME); wrapperModule_ = dlopen(WRAPPER_LIB_NAME, RTLD_NOW); if (wrapperModule_ == nullptr) { Log::debug("Failed to load module: %s ", dlerror()); return false; } } using InitFunc = bool (*)(WrapperFunc *funcs); InitFunc func = reinterpret_cast(dlsym(wrapperModule_, WRAPPER_FUNC_GET)); if (func == nullptr) { Log::debug("Failed to lookup %s function: %s ", WRAPPER_FUNC_GET, dlerror()); dlclose(wrapperModule_); return false; } if (func(&wapperFuncs_)) { wrapper_ = wapperFuncs_.CreateWindowWrapper(); } else { Log::debug("can not get wrapper functions "); return false; } if (wrapper_ != nullptr) { Log::debug("wrapper init success "); 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 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 窗口系统。
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 位编译环境,编译过程中 src/gl-state-egl.cpp 会报错,这里也顺便贴出解决办法。 - native_display_ = reinterpret_cast(native_display); + native_display_ = static_cast((intptr_t)native_display);③编译为可执行程序,加入 OH 编译框架
在 glmark2 目录执行以下命令: python ohos/build_ohos64.py ~/ohos_beta5 sagit ~/ohos_beta5/glmark2_2/
复制 build-hos/install/bin 下生成好的 glmark2-es2-ohos 程序到 board 仓下的自定义目录,我的是 test 下的 glmark2,然后配置 BUILD.gn,添加 glmark2-es2-ohos 为预编译程序。 ohos_prebuilt_executable("glmark2") { source = "glmark2/glmark2-es2-ohos" part_name = "qualcomm_products" install_images = [ "system" ] install_enable = true }
重新编译系统,刷机,此时还不能直接运行 glamrk2 运行需要额外的模型资源跟纹理资源,复制 build-hos/install/share/glmark2 下的资源到 data 分区。
对于小米 6,我是进入 twrp,挂载 data 分区,使用 mtp 程序直接拷贝到机器内部。
对于树莓派 4,我是读卡器插入到 Linux 主机,然后挂载 tf 卡的 data 分区,再拷贝到 data 分区内。
最后在小米 6 对应的路径为 /data/media/glmark2/,其他开发板如果打通了 hdc,可通过 hdc 传输文件。
理论上也可以通过配置 gn 预置到某个目录,但是目前还没研究透,打包失败了,有知道的大神可以留言指导以下。
准备就绪后,使用串口执行以下命令: 默认分辨率运行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 接口的测试程序如何进行编译以及调用验证讲解完毕,希望能对大家有所帮助。