android中,与图形相关的上层java package包括:
android.graphics,
android.view,
android.widget
android.opengl
android.graphics, android.view和android.widget功能和其他类似的图形库如Qt/Gtk+差不多,分别提供基本的图形原语(如画点画线,设置图形上下文等),事件机制,以及开发图形用户界面的控件等。canvas 用于开发2D图形,而android.opengl用于开发3d graphics,在http://developer.android.com/guide/topics/graphics/opengl.html有详细介绍如何使用。
本文重点放在研究android.graphics之下的和图形硬件设备交互的层。
Canvas: the Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing). Via the Canvas, your drawing is actually performed upon an underlying
Bitmap, which is placed into the window
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.
frameworks/base/graphics/java/android/graphics/Canvas.java 提供基础的绘制图形原语。
frameworks/base/core/jni/android/graphics/Canvas.cpp 提供native的绘制图形原语。Canvas.java中的函数通过JNI调用Canvas.cpp中的函数。但Canvas.cpp中的函数并不是最终实现,仅仅是一个中间函数。
external/skia/src/core/SkCanvas.cpp提供最终函数实现。Canvas可以画在一个bitmap上,之后传给surface。
Java API 包括Surface.java 以及 SurfaceSession.java,由客户端应用使用;对应的c++代码创建SurfaceComposerClient。而 SurfaceComposerClient建立与surfaceflinger server的连接。
Surface的概念:
每个surface有一个front buffer和一个back buffer。每个window有一个对应的surface. window内容绘制在view的bitmap后传给surface。surface作为一个service提供给系统使用,由service manager(frameworks/base/services/java/com/android/server/SystemSever.java, frameworks/base/cmds/system_server/library/system_init.cpp)初始化。surface server的代码位于frameworks/base/libs/surfaceflinger下。
frameworks/base/libs/ui下提供:
EGLDisplaySurface.cpp 中函数真正和图形设备framebuffer打交道。函数mapFrameBuffer打开图形设备framebuffer(/dev/graphics/fb0或者/dev/fb0),然后定义framebuffer的frontbuffer和backbuffer。backbuffer可能位于framebuffer上,也可能是一块system memory(由malloc分配),然后函数swapBuffers将back buffer内容拷贝到front buffer中。如果copybit device实现的话,拷贝可以使用硬件加速。
hardware/libhardware实现了HAL(Hardware Abstraction Layer)层,copybit device是其中一个模块。
frameworks/base/libs/surfaceflinger中提供:
DisplayHardware/DisplayHardware.cpp调用EGLDisplaySurface.cpp中的函数,初始化egl library,创建EGLSurface。
阅读(3500) | 评论(0) | 转发(0) |