OpenHarmony 圖形子系統(二)weston compositor分析
??https://harmonyos.51cto.com??
通過上一節,我們熟悉了基于 Linux DRM的基礎顯示平臺,以及wayland 相關的幾個基礎概念。這節我們將對搭建在其上的 weston compositor 進行深入分析。
Weston 是基于Wayland 協議的 compositor 的參考實現。其它的實現比如 GNOME 和KDE 也默認提供了基于Wayland display server 協議建立的全功能桌面環境。OpenHarmony 標準系統目前采用的是weston 的實現。
了解weston compositor 有利于我們對OpenHarmony 圖形子系統的移植適配及啟動問題進行調試。
Weston 結構分析
下圖是 OpenHarmony-3.0-LTS 版本的圖形子系統 compositor server端的結構圖。

compositor 上端通過 wayland 協議與client 進行通訊。
server 端除了 weston外,還加載了窗口管理服務(wmserver)模塊和 vsync 模塊。另外加載了一個 ivi-shell 模塊,這個我們后面在分析client 端 WindowManager 時再說。
weston 下端依賴幾個display hdi 層相關的庫:
- libdisplay_gfx 實現圖形的硬件加速接口。
- libdisplay_gralloc:負責顯示模塊內存的管理,包括內存的申請和釋放、內存映射等操作。
drm backend 中 renderer模塊通過 use_pixman 選項選擇使用 pixman renderer 還是 egl。 egl 是 rendering API(如 OpenGL,OpenGL ES) 與底層原生平臺窗口系統之間的接口。
pixman-render 中又通過 use_tde 變量來選擇是否使用 tde 硬件加速模塊。 TDE(Two Dimensional Engine)是海思的2D圖形加速引擎。Rockchip 對應的叫 RGA (Raster Graphic Acceleration) 二維光柵圖形加速單元,用來加速了二維圖形操作。例如點/線繪制、圖像縮放、旋轉、位圖、圖像合成等。
目前 3.0-LTS 若是其它非海思平臺,若檢測不到tde 模塊,則會默認使用 pixman 來進行軟件渲染。
關于Wayland
要知道 wayland 協議是被設計成”異步的面向對象“(asynchronous object-oriented protocol)的協議。面向對象(Object-oriented)表示 compositor 所提供的服務是以一系列貯存在同一個compositor 中的對象的方式呈現。
各個對象實現了一個接口(interface),接口有名字、若干的方法(request)及系列相關的events。接口協議可以在xml 文件中描述,編譯時有腳本可將其自動生成C 代碼(wayland_standard/wayland_scanner_wrapper.py)。
客戶端可以給對象發送請求,如果對象的接口支持這個請求的話。
compositor 中有一些wayland 的核心接口(core interfaces) 是必須要具備的,定義在 wayland_standard/protocol/wayland.xml中。此外特定的compositor 可以實現它們自己的接口作為擴展協議。每個接口協議都有版本號,以保證版本的兼容性。
知道上面的前置知識后,我們就可以開始分析weston 的代碼了。
weston 啟動流程偽代碼
weston 啟動流程比較長,我們只挑出我們感興趣的主干部分。整理一下流程,有助于后續調試的時候迅速回憶起看過的代碼。
wet_main(args)
weston_display_create() //創建 display 對象
load_configuration(&config) //根據啟動參數,加載配置文件 weston.ini 中的配置
weston_compositor_create() //創建 compositor 實例
load_backend() //根據啟動參數-b,顯式加載后端顯示接口 drm-backend.so
WL_EXPORT weston_backend_init() //顯示后端drm-bakcend.so 初始化入口
drm_backend_create()
if use_pixman:
init_pixman() //根據啟動參數 use_pixman, 在renderer pixman 或者 egl 二選一
pixman_render_init()
tde_renderer_alloc_hook()
tde_render_gfx_init()
dlopen(”libdisplay_gfx.so”)
GrallocInitialize()
→ peripheral/display “libdisplay_gralloc.so”
else:
init_egl()
VsyncModuleStart() //依賴圖形子系統中的 libvsync_module.so
InitSA() //注冊ID為VSYNC_MANAGER_ID的 Vsync Manager 服務
RegisterSystemAbility(VSYNC_MANAGER_ID)
VsyncMainThread()
load_modules() //加載weston.ini 里配置的 modules 項,3.0-LTS版本里加載了 libivi-controller.z.so,libwmserver.z.so 。 后面介紹 wmserver 窗口管理器模塊。
wl_display_run() //進入事件等待及常規任務循環
while(run)
wl_display_flush_clients()
wl_event_loop_dispatch()
然后來梳理一下我們最關心的 surface 提交, 然后重繪(repaint)及輸出流程。
surface 接口綁定及 surface commit 流程
這里就會涉及到一些接口實現的綁定。偽代碼中用 (->) 箭頭表示我們所關注的其中一個接口方法的實現。方法調用是當client 端發送對應的 wl_xxx 請求事件時被調用。
weston_compositor_create()
compositor_bind() //創建 compositor 時綁定 compositor_interface 接口實現
struct wl_compositor_interface compositor_interface //compositor 接口實現
→compositor_create_surface //創建surface 時綁定 surface 接口實現
struct wl_surface_interface surface_interface //surface 接口實現
→ surface_commit() //在 client 端調用 wl_surface_commit() 提交至此接口
weston_surface_commit_state()
pixman_render_attach() //若是新加入的surface 則會進行renderer attach
weston_surface_schedule_repaint(surface)//標記 output 中 該surface 需要被 repaint
repaint 流程
當有surface 被標記成需要 repaint 時,repaint timer handler 會對這些surface 進行重繪后輸出顯示。
wl_event_loop //wayland 事件循環
output_repaint_timer_handler
backend→repaint_begin() //開始調用后端 repaint 接口
weston_output_repaint()
→drm_output_repaint()
drm_output_render() //渲染
if use_pixman:
drm_output_render_pixman()
→pixman_renderer_repaint_output()
repaint_surfaces()
draw_view()
repaint_region()
else:
drm_output_render_gl()
drm_repaint_flush() //合成重繪后的畫面刷新輸出
drm_pending_state_apply() //kms
復制先更新這些,偽代碼部分可以對照著源碼多梳理幾遍。后面將編寫一個簡單的client,熟悉其核心接口對象(core interfaces)。然后以其為基礎分析圖形子系統中如何對其進行封裝適配,增加WM 窗口管理,Vsync 幀同步,內存管理等模塊。
??https://harmonyos.51cto.com??































