鸿蒙移植指南GPU测试程序编译
但是我们还需要一些手段进行测试,以确认GPU能够正常工作。这里分享我用过的两个测试程序,glmark2和一个简单的三角形绘制程序。
简单绘制三角形程序
该测试程序程序来自AlgoIdeas大神,文末附有AlgoIdeas的原文连接,大家可以进一步细读。
关键代码AlgoIdeas帖子里面已经提供下载,请大家去原帖下载,原版程序是基于dayu200开发板的,在此我讲解如何进行适配其他设备。适配OpenHarmony窗口系统
OpenGL渲染出来的数据并不能直接显示到屏幕上,需要适配对接OpenHarmony的窗口系统。
OpenHarmony提供了NativeWindow的API接口用以创建窗口,我们调用GPU渲染之后的数据就可以采用这个API接口进行显示。
下载nativewindowwrapper。zip解压放到:foundationwindowwindowmanager
目录下,因OH版本更新,部分配置需要修改并添加部件信息,修改后的BUILD。gn如下:Copyright(c)HisiliconTechnologiesCo。,Ltd。20212021。Allrightsreservedimport(buildohos。gni)ohossharedlibrary(nativewindowwrapper){sources〔nativewindowwrapper。cpp〕includedirs〔。〕cflags〔Wall,Werror,Wnocastqual,Wnopointerarith,Wnounusedparameter,Wnounusedvariable,Wnodeleteincomplete,〕deps〔foundationwindowwindowmanagerwm:libwm,foundationgraphicgraphic2dframeworkssurface:surface,foundationgraphicgraphic2drosenmodulesrenderserviceclient:librenderserviceclient,〕添加OH部件配置partnamewindowmanagersubsystemnamewindow}
修改Makefile适配Mesa3d
原版例子是适配rk3568的闭源驱动的,我们需要修改适配Mesa3d,以及适配64位系统。
下载nativewindowohos。zip解压放到OH项目根目录。
Makefile修改适配32位程序,rpi4举例:修改OHOSROOT为自己的目录OHOSROOThomealgoideasopenharmonymaster改为OHOSROOThomediemitOpenHarmony修改三处sysrootsysroot(OHOSROOT)outa311dobjthirdpartymusl改为sysroot(OHOSROOT)outrpi4objthirdpartymusl修改lib链接CLIBSL(OHOSROOT)devicesocrockchiphardwaregpuliblmalibifrostg52g2p0ohosCLIBSL(OHOSROOT)outrk3568packagesphonesystemliblhiloglsurface。zlutils。z改为CLIBSL(OHOSROOT)devicesocbroadcombcm2711standardhardwaregpuliblgalliumdriCLIBSL(OHOSROOT)outrpi4packagesphonesystemliblhiloglsurface。zlutils。z
Makefile修改适配64位程序,小米6举例:修改OHOSROOT为自己的目录OHOSROOThomealgoideasopenharmonymaster改为OHOSROOThomediemitOpenHarmony修改三处sysrootsysroot(OHOSROOT)outa311dobjthirdpartymusl改为sysroot(OHOSROOT)outsagitobjthirdpartymusl修改lib链接CLIBSL(OHOSROOT)devicesocrockchiphardwaregpuliblmalibifrostg52g2p0ohosCLIBSL(OHOSROOT)outrk3568packagesphonesystemliblhiloglsurface。zlutils。z改为CLIBSL(OHOSROOT)devicesocqualcommmsm8998hardwaregpulib64lgalliumdriCLIBSL(OHOSROOT)outsagitpackagesphonesystemlib64lhiloglsurface。zlutils。z修改CFLAGS:删除marcharmv7amfloatabisoftfpmtunegenericarmv7amfpuneonmthumb修改targetarmlinuxohosmusl改为targetaarch64linuxohosmusl
编译为可执行程序,加入OH编译框架
进入nativewindowohos目录执行make命令:cdnativewindowohosmake
复制生成好的nativemain程序到board仓下的自定义目录,我的是test下的nativewindowohos,然后配置BUILD。gn,添加nativemain为预编译程序。ohosprebuiltexecutable(nativemain){sourcenativewindowohosnativemainpartnamequalcommproductsinstallimages〔system〕installenabletrue}
重新编译系统,刷机之后使用串口工具输入命令直接运行:nativemain
如果一切正常,屏幕左上角会显示通过OpenGL接口调用GPU绘制的三角形,颜色会有异常这个无所谓,三角形能正确显示就证明Mesa3d已经正确配置。
使用glmark2
glmark2是一个经典的图形测试程序,lhl大神gitee仓中有一份移植的代码,但是有部分没有实现,这里我描述一下如何把glmark2适配到OpenHarmony。适配ohos
lhlglmark22:https:gitee。comhonglianglinglmark22
去lhl大神的仓库下载glmark2的源码,删除ohos文件夹,复制我们之前适配mesa3d修改好的ohos文件夹,然后修改编译的python脚本。
ohosbuildohos64。py:修改runbuildcmdDplatformsohosDeglnativeplatformohosDdridriversDgalliumdriversfreedrenoDvulkandriversDgbmenabledDeglenabledDcpprttifalseDglxdisabledDtoolsDdrisearchpathvendorlib64chipsetsdk修改后runbuildcmdDflavorsohosglesv2其他保持不变适配OpenHarmony本地窗口
glmark2在OpenHarmony平台不能直接显示,需要对接OpenHarmony的NativeWindow,需要预先完成章节1。1的适配,同时在glmark2中实现对窗口的调用。
修改glmark2程序中的,添加ohoswrapperlinker。cpp。
srcmeson。build:nativeohoslibstaticlibrary(nativeohos,nativestateohos。cpp,ohoswrapperlinker。cpp,dependencies:〔libmatrixheadersdep〕,)nativeohosdepdeclaredependency(
srcohoswrapperlinker。cpp:includeohoswrapperlinker。hincludedlfcn。hincludelog。hboolOhosWrapperLinker::Init(){wrapperModuledlopen(WRAPPERLIBNAME,RTLDNOWRTLDNOLOAD);if(wrapperModule!nullptr){Log::debug(Modulesalreadyloaded,WRAPPERLIBNAME);}else{Log::debug(Loadingmodules,WRAPPERLIBNAME);wrapperModuledlopen(WRAPPERLIBNAME,RTLDNOW);if(wrapperModulenullptr){Log::debug(Failedtoloadmodule:s,dlerror());returnfalse;}}usingInitFuncbool()(WrapperFuncfuncs);InitFuncfuncreinterpretcastInitFunc(dlsym(wrapperModule,WRAPPERFUNCGET));if(funcnullptr){Log::debug(Failedtolookupsfunction:s,WRAPPERFUNCGET,dlerror());dlclose(wrapperModule);returnfalse;}if(func(wapperFuncs)){wrapperwapperFuncs。CreateWindowWrapper();}else{Log::debug(cannotgetwrapperfunctions);returnfalse;}if(wrapper!nullptr){Log::debug(wrapperinitsuccess);returntrue;}returnfalse;}boolOhosWrapperLinker::CreateWindow(uint32tw,uint32th){returnwapperFuncs。CreateWindow(wrapper,w,h);}voidOhosWrapperLinker::GetWindow(){returnwapperFuncs。GetNativeWindow(wrapper);}voidOhosWrapperLinker::SetVisibility(boolvisible){wapperFuncs。SetVisibility(wrapper,visible);}
srcohoswrapperlinker。h:includecstdintexternC{typedefstruct{void(CreateWindowWrapper)();bool(CreateWindow)(voidwrapper,uint32tw,uint32th);void(GetNativeWindow)(voidwrapper);void(SetVisibility)(voidwrapper,boolvisible);void(DestroyWindowWrapper)(voidwrapper);}WrapperFunc;}classOhosWrapperLinker{public:boolInit();boolCreateWindow(uint32tw,uint32th);voidGetWindow();voidSetVisibility(boolvisible);private:staticconstexprconstcharWRAPPERLIBNAMElibnativewindowwrapper。z。so;staticconstexprconstcharWRAPPERFUNCGETGetWrapperFunc;WrapperFuncwapperFuncs;voidwrappernullptr;voidwrapperModulenullptr;};
修改glmark2,对接OpenHarmony窗口系统。
srcnativestateohos。h:ifndefGLMARK2NATIVESTATEOHOSHdefineGLMARK2NATIVESTATEOHOSHincludenativestate。hincludeohoswrapperlinker。hclassNativeStateOhos:publicNativeState{27,628,9classNativeStateOhos:publicNativeStateFlipsthedisplayvoidflip();private:OhosWrapperLinkerwrapper;WindowPropertiesproperties;};endifGLMARK2NATIVESTATEOHOSH
srcnativestateohos。cpp:includenativestateohos。hincludelog。hInitializesthenativedisplayboolNativeStateOhos::initdisplay(){Log::debug(ss:d,FUNCTION,FILE,LINE);returnwrapper。Init();}GetsthenativedisplayvoidNativeStateOhos::display(){Log::debug(ss:d,FUNCTION,FILE,LINE);returnnullptr;}Creates(orrecreates)thenativewindowboolNativeStateOhos::createwindow(WindowPropertiesconstproperties){propertiesproperties;returnwrapper。CreateWindow(properties。width,properties。height);}Getsthenativewindowanditsproperties。Thedimensionsmaybedifferentthantheonesrequested。voidNativeStateOhos::window(WindowPropertiesproperties){propertiesproperties;returnwrapper。GetWindow();}SetsthevisibilityofthenativewindowvoidNativeStateOhos::visible(boolv){wrapper。SetVisibility(v);return;}WhethertheuserhasrequestedanexitboolNativeStateOhos::shouldquit(){returnfalse;}FlipsthedisplayvoidNativeStateOhos::flip(){return;}
小米6适配的是arm64位编译环境,编译过程中srcglstateegl。cpp会报错,这里也顺便贴出解决办法。nativedisplayreinterpretcastEGLNativeDisplayType(nativedisplay);nativedisplaystaticcastEGLNativeDisplayType((intptrt)nativedisplay);编译为可执行程序,加入OH编译框架
在glmark2目录执行以下命令:pythonohosbuildohos64。pyohosbeta5sagitohosbeta5glmark22
复制buildhosinstallbin下生成好的glmark2es2ohos程序到board仓下的自定义目录,我的是test下的glmark2,然后配置BUILD。gn,添加glmark2es2ohos为预编译程序。ohosprebuiltexecutable(glmark2){sourceglmark2glmark2es2ohospartnamequalcommproductsinstallimages〔system〕installenabletrue}
重新编译系统,刷机,此时还不能直接运行glamrk2运行需要额外的模型资源跟纹理资源,复制buildhosinstallshareglmark2下的资源到data分区。
对于小米6,我是进入twrp,挂载data分区,使用mtp程序直接拷贝到机器内部。
对于树莓派4,我是读卡器插入到Linux主机,然后挂载tf卡的data分区,再拷贝到data分区内。
最后在小米6对应的路径为datamediaglmark2,其他开发板如果打通了hdc,可通过hdc传输文件。
理论上也可以通过配置gn预置到某个目录,但是目前还没研究透,打包失败了,有知道的大神可以留言指导以下。
准备就绪后,使用串口执行以下命令:默认分辨率运行800600glmark2es2ohosdatapathdatamediaglmark2指定分辨率10801920glmark2es2ohoss10801920datapathdatamediaglmark2
顺利的话可以在屏幕上看到glmark2的渲染窗口。
至此OpenGL接口的测试程序如何进行编译以及调用验证讲解完毕,希望能对大家有所帮助。