androidGUI Architecture as follow:
androidGUI系統由C語言的框架和JAVA語言的框架組成。
C語言的核心如下:
-
PixelFlinger
-
libui(框架庫)
-
SurfaceFlinger(Surface的管理)
-
Skia圖形圖像引擎
-
OpenGL3D引擎
-
各種JNI
java語言的核心如下:
-
android.graphic(對應SKia圖形庫)
-
android.view.Surface(構造各種介面)
-
android.view.View及其繼承類
-
OpenGL的功能類
Android圖形框架也可以分這麼分爲兩部分:用於應用程序圖形UI顯示的上層View體系和用於圖形渲染的底層圖形驅動。
View/GLSurfaceView體系就是用於構建ApplicationUI的圖形控件;而Skia與OpenGL| ES則是實際進行圖形渲染的圖形驅動,其中,Skia是2D圖形庫,而OpenGL|ES則是2D/3D圖形庫。Canvas可以看做是View與Skia之間的一個接口,其實它就是一套2D圖形API,供Application進行2D圖形繪製。
在Android中,圖形系統由2D圖形庫Skia和2D/3D圖形庫OpenGL|
ES來驅動。對於3D圖形庫OpenGL|
ES來說,從Android1.0開始,就已經支持OpenGLES
1.0和1.1,從Android2.2開始,實現了對OpenGLES
2.0的支持;從Android3.0開始,系統又新增了android.renderscript包及相關技術來支持3D圖形渲染。
與2D圖形相關的幾個包有:
android.graphics.drawable、android.view、android.view.animation、android.graphics、android.graphics.drawable.shapes。
與3D圖形相關的幾個包有:
android.opengl
javax.microedition.khronos.egl
javax.microedition.khronos.opengles。
不同的應用場景,需要用到的圖形技術是不同的,比如,如果您的應用程序僅僅需要繪製一些相對靜態的圖形及簡單動畫,可能2D圖形技術就能夠滿足您的需求;如果您的程序是一個交互式的遊戲動畫等,可能就得需要使用3D圖形技術了。
如下圖爲surface與SurfaceFlinger關係圖:
SurfaceFlinger
SurfaceFlinger在整個圖形系統中擔任server角色,它負責將各個surface根據Zorder合成(composer)起來。
Surface
Android圖形系統中一個重要的概念和線索是surface。View及其子類(如TextView,Button)要畫在surface上。每個surface創建一個Canvas對象(但 屬性時常改變),用來管理view在surface上的繪圖操作,如畫點畫線。每個canvas對象對應一個bitmap,存儲畫在surface上的內容。
每個Surface通常對應兩個buffer,一個frontbuffer,一個backbuffer。其中,backbuffer就是canvas繪圖時對應的bitmap(研究android_view_Surface.cpp::lockCanvas)。因此,繪畫總是在backbuffer上, 需要更新時,則將backbuffer和frontbuffer互換。
Thewindow is tied to a Surface and the ViewRoot asks the Surface for aCanvas that is then used by the Views to draw onto. After View drawits data to canvas, ViewRoot will callsurface.unlockCanvasAndPost(canvas) to schedulesurfaceFlinger::composeSurfaces() which do the actually display todisplay panel. SurfaceFlinger handles to transfers drawn data incanvas to surface front buffer or backbuffer.
Exceptfor SurfaceViews, different views within the same ViewRoot share thesame surface.
Layer
每個surface又對應一個layer,SurfaceFlinger負責將各個layer的frontbuffer合成(composite)繪製到屏幕上。
ALayer is something that can be composited by SurfaceFlinger (shouldhave been called LayerFlinger). There are several types of Layers ifyou look in the code, in particular the regular ones (Layer.cpp) ,they are backed by a Surface, and the LayerBuffer (very badly chosenname) which don't have a backing store, but receive one from theirclient. . Note that the GGLSurface type, should have been calledGGLBuffer.
Multiplelayers are just composited to the final buffer in their Z order.
與圖形相關的代碼主要位於下列目錄:
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