Desktop Window Manager, DWM

原文出處來自 wiki,我將它稍微翻譯一下,醬子比較容易理解。


http://en.wikipedia.org/wiki/Desktop_Window_Manager


Architecture


DWM 工作時,每個 ap 會有自己的 buffer 用來 rendering。DWM 再將它們組合處理成最終使用者看到的 image。而在之前版本的 OS(包含沒開 aero 的 Win 7 and Vista),ap 是通通寫到一個 main buffer 上去。


DWM 工作方式與在 Vista or 7,和 graphic driver 使用 WDDM 1.0 or 1.1 有所不同。在 Win 7 上,使用 WDDM 1.1 的 DWM 只會將 program buffer 寫到 video RAM,就算它是 GDI program。這是因為 win 7 有支援 GDI 的硬體加速。


DWM 既然能夠存取所有 ap 的 graphics,故它可以允許對 ap 之間做一些 visual effect,例如透明這種。DWM 透過 DirectX 9 使用 GPU 來做 compositing 跟 rendering 這種動作(這是在 Vista 上,到了 Win 7 改為 Direct3D 10.1),讓 CPU 不用處理從 ap 的 off-buffer render 到 display 的動作。然而要注意的是,每個 ap 還是要寫到自己的 off buffer 中,這一段 DWM 不處理。換句話說,如果它是一個 CPU-bounded 的動作,那麼還是會吃 CPU resource。像 GDI 技術將 UI 以 bitmap 的方式 redirect 到 buffer? 中。而像 WPF 這種支援 DWM 的,就會直接在 buffer? 中產生 DWM 使用的資料結構。buffer 中的 window content 最後會用轉為 DirectX texture。


Desktop 本身是一個 Direct3D surface,每個 window 是一個 mesh,被轉為 2D 的 rectangle。而 texture (UI chrome) 會 map 到這一個 rectangle。Window transition 的實作是使用 Vista 上的 shader 這隻程式對 mesh 做 transformation。


每個 ap 負責 render 到自己的 off buffer 中,因為它們是持續 update,Live thumbnail preview 就利用這個特性,能夠抓到 updated 的 window thumbnail 而不僅僅的靜態的 rendering。DWM 提供 API 讓 ap 能夠抓到這些 thumbnail,並且可以抓到任意 size 的 thumbnail。Windows Flip 跟 Windows Flip 3D 功能便是透過這組 API 抓到 thumbnail 的 bitmap (2D 的),並利用 shader 來轉換成 mesh 並且在 3D plane 中旋轉。


DWM 使用 MIL (Media Integration Layer),它是與 WPF 共用的一個 unmanaged compositor。桌面上與其上面跑的 window,都會被表示成 composition tree,而每一個 composition node 便是一個 window。這個 composition tree 被 MIL render 出來。由於每個 window 與最後丟到螢幕的 image 都有關係,因此每一個 pixel 的 color 可以由一個以上的 window 來決定。這用來實作像 per-pixel transparency 這種效果。DWM 本身有內建 Pixel shader 2.0,每個 pixel 都會參考到在上 window 與鄰近 window 的 pixel color 做平均計算,來得到最後的 pixel color。


MIL 會 cache 住這個 composition tree,因此修復跟 refresh screen 的動作就是由 DWM 跟 MIL 來處理,ap 不做這件事。Background data 已經存在於 composition tree 與 (ap 的) off-screen buffer 中,因此就可以直接用來 render this background,而不需要 background ap 藉由收到 WM_PAINT 訊息來畫。


至於全螢幕的 ap,DWM 不做 window compositing,因此效能不會降低。


Redirection


而不支援 DWM 的 rendering 方式,仍然需要 redirect 至 DWM buffer 去。GDI 或者 DirectX 都可以用來做 rendering。DWM 有提供此兩者 redirection 的技術。


以 GDI 來說,當它的 window 或 window 某一部份被看見時,ap 即會被通知 repaint。在無 DWM 的情況下,這 rendering 會直接去修改 video memory 中的 UI buffer。有 DWM 的情況下,它會在 system memory 中產生跟 window 同大小的 buffer。GDI 的呼叫被 redirect 到寫入此 system memory 中的 buffer。而在 video memory 中也會產生一個 buffer,它用來表示一個 DirectX surface,並被當作 Window mesh 的 texture。System memory 中的 buffer 會再被轉換成 DirectX surface,這是因為 GDI 並不能直接輸出 DirectX pixel format,因此才會需要 GDI 先寫入 system memory buffer,然後再轉換為 video memroy 中的 buffer (DirectX surface)。Compositor 會來讀取 video memory 中的 DirectX surface 並 composite 至 desktop。由 GDI->system memory buffer 跟 system memory buffer->DirectX surface 這兩段沒有硬體加速。當使用 GDI 的 window 被最小化之後,因為 GDI 的特性它不會再 update,因此 DWM 在此情況下是使用它最小化之前的最後 bitmap。


至於使用 DirectX 的 3D 程式,DirectX 使用 WDDM 來跟 DWM share surface。DWM 接著直接使用這個 surface 並且 map 它到 window mesh 上。WPF 的 DirectX 程式也使用這個 share surface。應用程式可以同時使用 GDI or DirectX 來處理不同的 child window(只要不同時使用一個 window),然而這樣的話,GDI 與 DirectX rendering 的順序就不能保證。意即不能保證 GDI->system memory buffer->video memory DirectX surface 這一段已經完成。換句話說,有可能 render 出來的結果沒有包含 GDI 的 window。為了這個原因,當這種程式跑起來時,DWM 會被暫時關閉。


---


之後又看了 wiki 關於 GDI 的文章,裡頭提到 Vista 後的 video card driver 就不對 GDI 提供硬體加速,而 Win 7 中只對 GDI 的 blitting operation 做加速,其餘無。另外 GDI+ 是完全透過軟體。


這邊跟同事討論,有一個不曉得對不對的初步結論。GDI 程式由於在 XP 上有 video card driver 提供的硬體加速,而在 Vista/7 上沒有,故在不開 DWM 的前提下,GDI 程式在 XP 上應該效能會比在 Vista/7 上好?


如果在 Vista/7 上有 DWM 的情況下,因為 GDI 的 render path 是 redirect 到 DWM,由它來 render  window content,因此開了 DWM 的情況下,GDI 程式的效能比從前 XP 還是好。


留言

熱門文章