劉琳
【摘 要】Wayland是一個(gè)新興的開源顯示通信協(xié)議,用于替代X窗口系統(tǒng)。本文以Wayland的1.3.0版本作為基礎(chǔ),通過分析源代碼,從而分析Wayland協(xié)議的具體實(shí)現(xiàn)。
【關(guān)鍵詞】Wayland;Display;協(xié)議;Compositor
0 引言
Wayland是一款規(guī)定顯示合成器與其客戶端之間通信方式的協(xié)議,它最初由英特爾開源技術(shù)中心的雇員Kristian H?覬gsberg于2008年發(fā)起,用以取代X窗口系統(tǒng)。
在本文中,顯示合成器、合成器以及服務(wù)端都是指提供顯示合成、輸出服務(wù)的一方;客戶端是指提供顯示數(shù)據(jù)的一方。
1 模型
Wayland源代碼編譯以后,會(huì)生成分別用于客戶端和服務(wù)端使用的動(dòng)態(tài)鏈接庫libwayland-client和libwayland-server。
圖1 客戶端和服務(wù)端的關(guān)系
客戶端通過libwayland-client和服務(wù)端進(jìn)行通信,圖形合成器通過libwayland-server和客戶端進(jìn)行通信。
libwayland-client 和libwayland-server 內(nèi)部封裝了 Wayland 通信協(xié)議。
Wayland協(xié)議是基于面向?qū)ο笏枷攵O(shè)計(jì)的異步通信協(xié)議。
圖2 客戶端和服務(wù)端的關(guān)系
2 消息
客戶端和服務(wù)端的控制信息是通過 unix domain socket 進(jìn)行傳輸?shù)摹6鴪D形數(shù)據(jù)則通過其他方式傳輸,比如共享內(nèi)存技術(shù)。
socket 上的數(shù)據(jù)是以消息為單位進(jìn)行傳輸?shù)?。從客戶端發(fā)送給合成器消息叫做 request,從合成器發(fā)送給客戶端的消息有 event 和 error 兩種。
每一個(gè)消息可以理解為要求某一個(gè)對(duì)象進(jìn)行特定動(dòng)作,或者某一個(gè)對(duì)象主動(dòng)通知狀態(tài)發(fā)生變化。
圖3 消息
3 消息格式
消息由消息頭和消息體組成。并且整個(gè)消息必須4字節(jié)對(duì)齊。
消息頭的長(zhǎng)度固定為8個(gè)字節(jié)。前4個(gè)字節(jié)存放的是對(duì)象ID,緊接著的2個(gè)字節(jié)是操作碼,最后2個(gè)字節(jié)存放的是整個(gè)消息的長(zhǎng)度。
消息體長(zhǎng)度不固定,但都由4字節(jié)對(duì)齊的參數(shù)組成。不同的操作碼在Wayland協(xié)議里定義了不同的參數(shù)。
4 參數(shù)格式
消息體中的參數(shù),Wayland也定義了相關(guān)格式。
表1 參數(shù)格式
參數(shù)標(biāo)識(shí)是libwayland-client和libwayland-server中用于識(shí)別參數(shù)類型的標(biāo)識(shí)符號(hào)。libwayland-client和libwayland-server通過讀取協(xié)議數(shù)據(jù)常量部分的wl_message 中的signature 字符串,依次判斷字符串中的字符是某一個(gè)參數(shù)標(biāo)識(shí),從而知道消息體中的數(shù)據(jù)類型。
而文件描述符是特殊的參數(shù),通過socket傳送文件描述符的時(shí)候,需要將文件描述符通過socket的優(yōu)先數(shù)據(jù)區(qū)域才能發(fā)送。
5 數(shù)據(jù)緩沖區(qū)
無論是客戶端還是服務(wù)端,當(dāng)發(fā)送消息和接收消息的時(shí)候,都會(huì)首先將消息存放到數(shù)據(jù)緩沖區(qū)中進(jìn)行緩存,借以減少IO的次數(shù)。
數(shù)據(jù)緩沖區(qū)是一個(gè)環(huán)形緩沖,當(dāng)客戶端明確需要進(jìn)行發(fā)送或者buffer滿的時(shí)候,才會(huì)進(jìn)行發(fā)送動(dòng)作。
圖5 數(shù)據(jù)緩沖區(qū)
6 連接(connection)
連接用于描述物理通信層的信息,主要包括了輸入緩沖區(qū)和輸出緩沖區(qū)以及socket本身。
由于文件描述符的傳輸需要使用特殊的方法,需要為文件描述符建立專用的數(shù)據(jù)緩沖區(qū),因此每一個(gè)連接包含4個(gè)數(shù)據(jù)緩沖區(qū)。2個(gè)用于輸入,2個(gè)用于輸出。
圖6 connection
7 主要對(duì)象
由于Wayland 采用了面向?qū)ο蟮脑O(shè)計(jì)思想,客戶端和服務(wù)端的控制信息都是基于對(duì)象的。
有一些對(duì)象希望所有的客戶端都可以訪問它,并且會(huì)廣播這些對(duì)象,這樣的對(duì)象叫做global。
Wayland 環(huán)境中,有一些對(duì)象只有一個(gè)實(shí)體,而另外一些對(duì)象,是會(huì)頻繁創(chuàng)建和銷毀的。
下面是最主要的幾個(gè)對(duì)象:
表2 主要對(duì)象
8 接口(interface)
Wayland把對(duì)象提供的能力通過接口來進(jìn)行描述,接口的類型就是消息的類型:request、event和error。
在Wayland項(xiàng)目里面,接口是通過xml文件來描述的。
下面是wayland.xml文件中描述 protocol、request和event的一部分摘抄,每一個(gè)部分將其描述已經(jīng)參數(shù)的類型都描述得十分清楚。
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 代碼自動(dòng)生成
wayland-scanner是Wayland項(xiàng)目的一個(gè)小工具,用于將描述接口的xml文件翻譯成C語言的源文件和頭文件。
接口的使用分為客戶端和合成器端,因此生成了2個(gè)頭文件,一個(gè)用于客戶端,一個(gè)用于服務(wù)端。
而源文件主要包含了接口的C語言實(shí)現(xiàn),無論客戶端或者是合成器端,都是使用同一個(gè)。
Wayland已經(jīng)實(shí)現(xiàn)了:
客戶端、將C語言接口調(diào)用的數(shù)據(jù)做成消息并發(fā)送。
服務(wù)端、將接收到的消息轉(zhuǎn)化成C語言的函數(shù)調(diào)用。
因此對(duì)于開發(fā)者而言,只需要寫好接口描述xml文檔,而不用關(guān)注接口的C語言封裝。
圖7 代碼自動(dòng)生成
下面是使用wayland-scanner將wayland.xml解析完成后生成的C語言源代碼的部分摘抄,能夠和接口部分對(duì)應(yīng)起來。
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,可以將客戶端和合成器端的消息通信日志輸出到當(dāng)前終端。
在日志中能夠看到實(shí)時(shí)的通信交互情況,有利于分析通信協(xié)議。
下面是截取的一段通信日志, 符號(hào)"->"表示發(fā)送,沒有這個(gè)符號(hào)表示接收。@+數(shù)字表示對(duì)象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 結(jié)束語
Wayland協(xié)議以其高效簡(jiǎn)潔的設(shè)計(jì),贏得越來越多第三方開發(fā)人員以及軟件的支持。本文通過對(duì)Wayland協(xié)議進(jìn)行概要的介紹,使讀者可以更了解Wayland的具體實(shí)現(xiàn),從而添加更符合自己需求的自定義擴(kuò)展協(xié)議。相信Wayland項(xiàng)目將來會(huì)對(duì)開源圖形系統(tǒng)產(chǎn)生更加深遠(yuǎn)的影響。
【參考文獻(xiàn)】
[1]http://wayland.freedesktop.org/docs/html/[OL].
[2]http://en.wikipedia.org/wiki/Wayland_%28display_server_protocol%29[OL].
[責(zé)任編輯:楊玉潔]