劉琳
【摘 要】Wayland是一個新興的開源顯示通信協議,用于替代X窗口系統。本文以Wayland的1.3.0版本作為基礎,通過分析源代碼,從而分析Wayland協議的具體實現。
【關鍵詞】Wayland;Display;協議;Compositor
0 引言
Wayland是一款規定顯示合成器與其客戶端之間通信方式的協議,它最初由英特爾開源技術中心的雇員Kristian H?覬gsberg于2008年發起,用以取代X窗口系統。
在本文中,顯示合成器、合成器以及服務端都是指提供顯示合成、輸出服務的一方;客戶端是指提供顯示數據的一方。
1 模型
Wayland源代碼編譯以后,會生成分別用于客戶端和服務端使用的動態鏈接庫libwayland-client和libwayland-server。
圖1 客戶端和服務端的關系
客戶端通過libwayland-client和服務端進行通信,圖形合成器通過libwayland-server和客戶端進行通信。
libwayland-client 和libwayland-server 內部封裝了 Wayland 通信協議。
Wayland協議是基于面向對象思想而設計的異步通信協議。
圖2 客戶端和服務端的關系
2 消息
客戶端和服務端的控制信息是通過 unix domain socket 進行傳輸的。而圖形數據則通過其他方式傳輸,比如共享內存技術。
socket 上的數據是以消息為單位進行傳輸的。從客戶端發送給合成器消息叫做 request,從合成器發送給客戶端的消息有 event 和 error 兩種。
每一個消息可以理解為要求某一個對象進行特定動作,或者某一個對象主動通知狀態發生變化。
圖3 消息
3 消息格式
消息由消息頭和消息體組成。并且整個消息必須4字節對齊。
消息頭的長度固定為8個字節。前4個字節存放的是對象ID,緊接著的2個字節是操作碼,最后2個字節存放的是整個消息的長度。
消息體長度不固定,但都由4字節對齊的參數組成。不同的操作碼在Wayland協議里定義了不同的參數。
4 參數格式
消息體中的參數,Wayland也定義了相關格式。
表1 參數格式
參數標識是libwayland-client和libwayland-server中用于識別參數類型的標識符號。libwayland-client和libwayland-server通過讀取協議數據常量部分的wl_message 中的signature 字符串,依次判斷字符串中的字符是某一個參數標識,從而知道消息體中的數據類型。
而文件描述符是特殊的參數,通過socket傳送文件描述符的時候,需要將文件描述符通過socket的優先數據區域才能發送。
5 數據緩沖區
無論是客戶端還是服務端,當發送消息和接收消息的時候,都會首先將消息存放到數據緩沖區中進行緩存,借以減少IO的次數。
數據緩沖區是一個環形緩沖,當客戶端明確需要進行發送或者buffer滿的時候,才會進行發送動作。
圖5 數據緩沖區
6 連接(connection)
連接用于描述物理通信層的信息,主要包括了輸入緩沖區和輸出緩沖區以及socket本身。
由于文件描述符的傳輸需要使用特殊的方法,需要為文件描述符建立專用的數據緩沖區,因此每一個連接包含4個數據緩沖區。2個用于輸入,2個用于輸出。
圖6 connection
7 主要對象
由于Wayland 采用了面向對象的設計思想,客戶端和服務端的控制信息都是基于對象的。
有一些對象希望所有的客戶端都可以訪問它,并且會廣播這些對象,這樣的對象叫做global。
Wayland 環境中,有一些對象只有一個實體,而另外一些對象,是會頻繁創建和銷毀的。
下面是最主要的幾個對象:
表2 主要對象
8 接口(interface)
Wayland把對象提供的能力通過接口來進行描述,接口的類型就是消息的類型:request、event和error。
在Wayland項目里面,接口是通過xml文件來描述的。
下面是wayland.xml文件中描述 protocol、request和event的一部分摘抄,每一個部分將其描述已經參數的類型都描述得十分清楚。
The core global object. ?This is a special singleton object.
It is used for internal Wayland protocol features.
The sync request asks the server to emit the 'done' event
on the returned wl_callback object. ?Since requests are
handled in-order and events are delivered in-order, this can
used as a barrier to ensure all previous requests and the
resulting events have been handled.
The object returned by this request will be destroyed by the
compositor after the callback is fired and as such the client
must not attempt to use it after that point.
The error event is sent out when a fatal (non-recoverable)
error has occurred. ?The object_id argument is the object
where the error occurred, most often in response to a request
to that object. ?The code identifies the error and is defined
by the object interface. ?As such, each interface defines its
own set of error codes. ?The message is an brief description
of the error, for (debugging) convenience.
9 代碼自動生成
wayland-scanner是Wayland項目的一個小工具,用于將描述接口的xml文件翻譯成C語言的源文件和頭文件。
接口的使用分為客戶端和合成器端,因此生成了2個頭文件,一個用于客戶端,一個用于服務端。
而源文件主要包含了接口的C語言實現,無論客戶端或者是合成器端,都是使用同一個。
Wayland已經實現了:
客戶端、將C語言接口調用的數據做成消息并發送。
服務端、將接收到的消息轉化成C語言的函數調用。
因此對于開發者而言,只需要寫好接口描述xml文檔,而不用關注接口的C語言封裝。
圖7 代碼自動生成
下面是使用wayland-scanner將wayland.xml解析完成后生成的C語言源代碼的部分摘抄,能夠和接口部分對應起來。
static const struct wl_message wl_display_requests[] = {
{ "sync", "n", types + 8 },
{ "get_registry", "n", types + 9 },
};
static const struct wl_message wl_display_events[] = {
{ "error", "ous", types + 0 },
{ "delete_id", "u", types + 0 },
};
WL_EXPORT const struct wl_interface wl_display_interface = {
"wl_display", 1,
2, wl_display_requests,
2, wl_display_events,
};
static inline struct wl_callback *
wl_display_sync(struct wl_display *wl_display)
{
struct wl_proxy *callback;
callback = wl_proxy_create((struct wl_proxy *) wl_display,
&wl_callback_interface);
if (!callback)
return NULL;
wl_proxy_marshal((struct wl_proxy *) wl_display,
WL_DISPLAY_SYNC, callback);
return (struct wl_callback *) callback;
}
struct wl_display_interface {
/**
* sync - asynchronous roundtrip
* @callback: (none)
*
*The sync request asks the server to emit the 'done' event on
* the returned wl_callback object. Since requests are handled
* in-order and events are delivered in-order, this can used as a
* barrier to ensure all previous requests and the
* resulting events ?have been handled.
*
* The object returned by this request will be destroyed by the
* compositor after the callback is fired and as such the client
* must not attempt to use it after that point.
*/
void (*sync)(struct wl_client *client,
struct wl_resource *resource,
uint32_t callback);
/**
* get_registry - get global registry object
* @callback: (none)
*
* This request creates a registry object that allows the client
* to list and bind the global objects available from the
* compositor.
*/
void (*get_registry)(struct wl_client *client,
struct wl_resource *resource,
uint32_t callback);
};
10 通信日志
客戶端或者合成器端通過 export WAYLAND_DEBUG=1,可以將客戶端和合成器端的消息通信日志輸出到當前終端。
在日志中能夠看到實時的通信交互情況,有利于分析通信協議。
下面是截取的一段通信日志, 符號"->"表示發送,沒有這個符號表示接收。@+數字表示對象ID。
-> wl_display@1.get_registry(new id wl_registry@2)
-> wl_display@1.sync(new id wl_callback@3)
wl_display@1.delete_id(3)
wl_registry@2.global(1, "wl_display", 1)
wl_registry@2.global(2, "wl_compositor", 3)
-> wl_registry@2.bind(2, "wl_compositor", 1, new id [unknown]@4)
wl_registry@2.global(3, "wl_subcompositor", 1)
wl_registry@2.global(4, "wl_scaler", 2)
wl_registry@2.global(5, "wl_text_input_manager", 1)
wl_registry@2.global(6, "wl_data_device_manager", 1)
wl_registry@2.global(7, "wl_shm", 1)
-> wl_registry@2.bind(7, "wl_shm", 1, new id [unknown]@5)
wl_registry@2.global(8, "wl_drm", 2)
wl_registry@2.global(9, "wl_seat", 3)
wl_registry@2.global(10, "wl_input_method", 1)
wl_registry@2.global(11, "wl_output", 2)
11 結束語
Wayland協議以其高效簡潔的設計,贏得越來越多第三方開發人員以及軟件的支持。本文通過對Wayland協議進行概要的介紹,使讀者可以更了解Wayland的具體實現,從而添加更符合自己需求的自定義擴展協議。相信Wayland項目將來會對開源圖形系統產生更加深遠的影響。
【參考文獻】
[1]http://wayland.freedesktop.org/docs/html/[OL].
[2]http://en.wikipedia.org/wiki/Wayland_%28display_server_protocol%29[OL].
[責任編輯:楊玉潔]