Android 圖形系統剖析

來源:互聯網
上載者:User

本文為轉貼。From :  http://www.linuxgraphics.cn/android/graphics_system.html

 

Android 圖形系統剖析作者: 本文根據參考資料1補充整理而成日期: 2009-06-04本文試圖講述Android圖形系統的底層實現。Android圖形系統底層實現非常複雜,文檔較少,沒有使用比較流行的圖形組建如X window, Cairo等。
架構

Android 中的圖形系統採用 Client/Server 架構。Server (即SurfaceFlinger) 主要由 C++ 代碼編寫而成。Client 端代碼分為兩部分,一部分是由 Java 提供 的供應用程式使用的 API,令一部分則是用 C++ 寫成的底層實現。概要介 紹了 Android 圖形系統的架構以及使用的主要組件。

SurfaceFlinger

SurfaceFlinger 在整個圖形系統中擔任 server 角色,它負責將各個 surface 根據 Z order 合成 (composer) 起來。

Surface

Android圖形系統中一個重要的概念和線索是surface。View及其子類(如 TextView, Button)要畫在surface上。每個surface建立一個Canvas對象 (但 屬性時常改變),用來管理view在surface上的繪圖操作,如畫點畫線。每個 canvas對象對應一個bitmap,儲存畫在surface上的內容。

每個Surface通常對應兩個buffer,一個front buffer, 一個back buffer。其中, back buffer就是canvas繪圖時對應的bitmap (研究 android_view_Surface.cpp::lockCanvas)。因此,繪畫總是在back buffer上, 需要更新時,則將back buffer和front buffer互換。

The window is tied to a Surface and the ViewRoot asks the Surface for a Canvas that is then used by the Views to draw onto. After View draw its data to canvas, ViewRoot will call surface.unlockCanvasAndPost(canvas) to schedule surfaceFlinger::composeSurfaces() which do the actually display to display panel. SurfaceFlinger handles to transfers drawn data in canvas to surface front buffer or backbuffer.

Except for SurfaceViews, different views within the same ViewRoot share the same surface.

Layer

每個surface又對應一個layer, SurfaceFlinger負責將各個layer的front buffer合成(composite)繪製到螢幕上。

A Layer is something that can be composited by SurfaceFlinger (should have been called LayerFlinger). There are several types of Layers if you look in the code, in particular the regular ones (Layer.cpp) , they are backed by a Surface, and the LayerBuffer (very badly chosen name) which don't have a backing store, but receive one from their client. . Note that the GGLSurface type, should have been called GGLBuffer.

Multiple layers are just composited to the final buffer in their Z order.

建立一個 Surface 的過程

Android 建立一個 Surface 的過程如下面的順序圖表所示:

研究一個surface如何建立的關鍵路徑如下:

1. frameworks/base/core/java/android/view/Surface.java — Surface::Surface ()。

2. frameworks/base/core/jni/android_view_Surface.cpp — Surface_init ()。

在這個函數中SurfaceComposerClient 對象被建立。

3. frameworks/base/libs/ui/SurfaceComposerClient.cpp — SurfaceComposerClient::SurfaceComposerClient ().

這個函數非常重要,在這裡建立了client和server之間的橋樑。通過函數_get_surface_manager()獲得了一個指向server的IBinder 對象(具有ISurfaceComposer介面),之後通過這個IBinder就可以跨進程訪問Server的功能。接著調用ISurfaceComposer::createConnection()建立並返回了一個ISurfaceFlingerClient的IBinder。

4. frameworks/base/libs/ui/SurfaceComposerClient.cpp — SurfaceComposerClient::createSurface().

這個函數中,利用前面獲得的ISurfaceFlingerClient的IBinder,調用其createSurface介面。

5.frameworks/base/libs/surfaceflinger/SurfaceFlinger.cpp — BClient::createSurface ()。

BClient由ISurfaceFlingerClient派生而來。

6.frameworks/base/libs/surfaceflinger/SurfaceFlinger.cpp — SurfaceFlinger:: createSurface()。

這個函數為Surface建立一個對應的Layer。

上述關鍵路徑中,1,2,3,4運行於client進程中,而5,6運行與server進程中。server作為一個service提供給client訪問。

與圖形相關的程式碼分析

與圖形相關的代碼主要位於下列目錄:

1. frameworks/base/graphics/java/android/graphics

2.frameworks/base/core/java/android/view

3.frameworks/base/core/java/android/widget

4.frameworks/base/opengl/

5.frameworks/base/libs/ui

6.frameworks/base/libs/surfaceflinger

7.frameworks/base/core/jni/android/graphics

8.frameworks/base/core/jni/android/opengl

9.frameworks/base/core/jni/android/android_view_*.cpp

10.external/skia

android.graphics, android.view和android.widget

1.frameworks/base/graphics/java/android/graphics

2.frameworks/base/core/java/android/view

3.frameworks/base/core/java/android/widget

android.graphics, android.view和android.widget功能和其他類似的圖形庫如 Qt/Gtk+差不多,分別提供基本的圖形原語(如畫點畫線,設定圖形上下文等), 事件機制,以及開發圖形化使用者介面的控制項等。canvas 用於開發2D圖形, Surface 代表一個可供圖形系統繪製的surface。可在其上繪製2D活3D圖形。

frameworks/base/opengl/

這個目錄包含opengel的介面以及軟體實現。在

http://developer.android.com/guide/topics/graphics/opengl.html

有詳細介紹如何使用android.opengl開發3d graphics。

external/skia

skia與cairo功能相當,封裝底 層的圖形硬體,為上面的圖形庫提供最基礎的操 作圖形硬體的原語。關於 skia 可參考《淺談 Google Skia 圖形處理引擎》2和 《Android 的圖形引擎 Skia》3兩篇文章。

frameworks/base/libs/ui 和 frameworks/base/libs/surfaceflinger

ISurface 定義了基礎的Surface介面,供圖形系統用戶端 (應用)和server端 (即surfaceflinger)互動。

BpSurface是ISurface的衍生類別,提供介面供server 調用用戶端功能;

BnSurface是ISurface的另一個衍生類別,提供介面供用戶端調用server功能。當 server 收到來自用戶端 (通過BnSurace)的調用請求後,如registerBuffers, postBuffer等,BnSurface::onTransact被觸發。

Surface (LayerBaseClient的私人類)是BnSurface的衍生類別。

SurfaceBuffer (SurfaceBuffer的私人類)是Surface的衍生類別。

ISurfaceComposer 定義了基礎的介面,供用戶端和server端互動。

BpSurfaceComposer是一個衍生類別,提供介面供server調用用戶端功能;

BnSurfaceComposer是另一衍生類別,提供介面供用戶端調用server功能。類 SurfaceFlinger 由BnSurfaceComposer派生而來。

SurfaceComposerClient直接供用戶端使用,調用ISurface (BnSurface)和 ISurfaceComposer (BnSurfaceComposer)以及 ISurfaceFlingerClient 介面,與server互動。

BClient 派生自ISurfaceFlingerClient (BnSurfaceFlingerClient),調用server的createSurface,真正建立一個surface。每個surface對應一個layer.

egl_native_window_t 定義了一個本地window類 。這個類提供了對本地window 的所有描述以及用於egl (opengl 與本地圖形系統的介面)操作本地windwo的所 有方法。

EGLNativeSurface是egl_native_window_t的一個衍生類別。

EGLDisplaySurface是EGLNativeSurface的衍生類別。 EGLDisplaySurface 是一個 非常重要的類,在這個類裡,真正開啟framebuffer裝置(/dev/graphics/fb0 或 者/dev/fb0),並將這個裝置封裝成EGLDisplaySurface的形式供server使用。函 數mapFrameBuffer開啟framebuffer, 建立兩個緩衝區,(一個是on screen front 緩衝區, 另一個back buffer, 可能位於offscreen framebuffer,也可能 位於系統記憶體)。 函數swapBuffers將back buffer內容拷貝到front buffer中。

DisplayHardware 類中初始化了egl系統,並為本地視窗對象EGLDisplaySurface 建立了對應的EGLSurface 對象。surfaceflinger 使用DisplayHardware去和本地視窗打交道。

jni

7.frameworks/base/core/jni/android/graphics

8.frameworks/base/core/jni/android/opengl

9.frameworks/base/core/jni/android/android_view_*.cpp

這些目錄下的代碼在Java層的graphics 組件和native (c++)組件之間銜接,將java層的功能調用轉換到對應的本地調用。

hardware/libhardware實現了HAL(Hardware Abstraction Layer)層,copybit device是其中一個模組。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.