Screen tearing 是什嗎?從字面意思看,就是螢幕撕裂,讓人聯想到顯示器的硬體問題,其實不然,它是一種常見的顯示問題,就是我們常說的鋸齒、狗牙之類。看看下面這張圖片就明白了,圖上#1和#2處都產生了撕裂。這是從維基百科上摘過來的,一幅圖片被撕裂成了3段,看起來是從某段視頻中截取的。
這種現象通常是由於顯卡驅動沒裝造成的,在Linux案頭環境中非常常見,特別是在拖動視窗、調整視窗大小、播放視頻時經常發生。維基上對此現象解釋得非常清楚,並且還給出瞭解決方案。
Screen tearing is a visual artifact in video display where a display device shows information from two or more frames in a single screen draw.
The artifact occurs when the video feed to the device isn't in sync with the display's refresh. This can be due to non-matching refresh rates—in which case the tear line moves as the phase difference
changes (with speed proportional to difference of frame rates). It can also occur simply from lack of sync between two equal frame rates, in which case the tear line is at a fixed location that corresponds to the phase difference. During video motion, screen
tearing creates a torn look as edges of objects (such as a wall or a tree) fail to line up.——Wikipedia
如上所述,Screen tearing是因為顯卡與顯視器的重新整理頻率不同步造成顯示器同時將多個幀顯示在了同一畫面中了。解決方案很顯然,就是調整顯卡和顯示器的重新整理頻率,使其同步即可。現在的顯卡大都可以自動調整重新整理頻率,與顯示器保持一持,這可以通過開啟VSync選項來啟用。
但是在XFCE下,開啟顯卡VSync後,Screen tearing並沒有好轉。經過多次Google,發現問題出在了XFCE預設視窗管理器XFWM上,它沒有採用顯卡的加速功能,而是簡單地採用了CPU直接渲染,難怪開啟顯卡的VSync不能解決問題!找到了原因,也就有瞭解決方案,那就是換個可以使用顯卡加速的視窗管理器,Google發現只有Emerald和Mutter有這功能,但Mutter只能整合在Gnome3中使用,因此選擇Emerald。
安裝Emerald具體的方法不同發行版不同,就不多講了,需要協助的可以查看該發行版的軟體包管理文檔。裝上Emerald後,必須在Compiz(Emerald是它的一個組成部分)的CCSM中設定好Window Decoration才能讓通過命令emerald --replace來讓Emerald管理視窗。這樣,在CCSM中選上VSync選項就把Screen Tearing問題消滅了。當然,前提是顯卡驅動已經裝好,並且支援OpenGL才行。
我用的是Gentoo,具體的步驟如下:
$ emerge emerald compiz ccsm
# 在ccsm中設定VSync和Window Decoration
$ emerald --replace &
#選擇或DIY視窗修飾主題
$ emerald-theme-manager
我只在XFCE下測試了,實際上本方法可以作為一個通用的解決方案,在其它案頭環境應該也是可用的。