【Android源码 栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了Android源码 -Android系统教程之GUI详解 - 企业软件开发的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
第一部分
Android GUI系统综述第二部分 pixelflinger和libui库第三部分 Surface系统 第五部分 Skia系统 第六部分 OpenGL系统架构 libpixelflinger SurfaceFlinger libuiSurfaceOverlayCameraEglWindowsformat Key / EventSkia图形引擎OpenGL3D引擎
android.graphics.Canvasandroid.view.SurfaceGraphic JNISurface JNIandroid.view.ViewGLSurfaceView javax.microedition.khronos.opengles各种GUI元素OpenGL JNIcom.google.
android.gles_jniFrameBuffer驱动C框架层JAVA框架Event输入驱动
Android的GUI系统由C语言的框架和JAVA语言的框架组成。
GUI系统的C语言部分包括 PixelFlinger libui框架库 SurfaceFlingerSurface的管理 Skia图形图像引擎 OpenGL 3D 引擎 各种JNI向JAVA提供接口 GUI系统JAVA语言的核心包括
android.graphics对应Skia底层库
android.view.Surface构建显示介面
android.view.View及其继承者 用于构建UI元素 OpenGL的功能类 javax.microedition.khronos.opengles 由com.google.
android.gles_jni实现 2.1 pixelflinger2.2 libui libpixelflingner.so是一个下层的工具性的类这个类对外的主要内容是GGLContext结构以及初始化和卸载的函数。
system/core/include/pixelflinger/ system/core/libpixelflinger/ssize_t gglInitGGLContext contextssize_t gglUninitGGLContext context libpixelflingner.so这个库对其他的库没有依赖也并不提供实际的功能类似一个用于管理工具的库。
libui是一个框架性质的集成库它不仅是显示的中枢也是整个GUI系统的中枢。
UI lib libui → libpixelflinger 这个的相关内容在以下的路径中 frameworks/base/include/ui/ framework/base/libs/ui/ libui包含了颜色格式用于实际显示的Egl窗口按键及事件处理Suface界面OverlayCamera等几个方面的接口。
format部分 这个部分本身定义颜色空间的枚举和数据结构它需要充用pixelflinger中的一些关于数据格式定义。
EglWindows部分 包含了EGL头文件构建的egl_native_window_t它依赖OpenGL的结构并给libEGL使用的。
EGLDisplaySurface操作了硬件的framebuffer的驱动。
这也是整个系统显示的基础。
Key/Event部分 这是
Android系统输入的基础其中定义按键的映射通过操作event事件设备来实现获取系统的输入的。
Surface Surface相关的头文件和实现为SurfaceFlinger定义接口和框架。
Overlay 定义视频输出的接口。
Camera 定义摄像头的框架和接口。
输出部分的硬件抽象donut之前 EGLDisplaySurface.cpp 调用标准的FrameBuffer驱动。
Android使用标准的framebuffer作为驱动程序
Android的本地框架中提供了系统和framebuffer驱动程序之间的适配层硬件抽象层。
Gralloc Module是Eclair版本之后显示部分的抽象层它是系统和Framebuffer设备的接口以硬件模块的形式存在。
头文件路径hardware/libhardware/include/hardware/gralloc.h Gralloc模块实现hardware/libhardware/modules/gralloc/ Gralloc被libui使用。
Gralloc.h中包含了Gralloc模块需要具有的接口。
typedef struct gralloc_module_t struct hw_module_t common int registerBufferstruct gralloc_module_t const module buffer_handle_t handle int unregisterBufferstruct gralloc_module_t const module buffer_handle_t handle int lockstruct gralloc_module_t const module buffer_handle_t handle int usage int l int t int w int h void vaddr int unlockstruct gralloc_module_t const module buffer_handle_t handle int performstruct gralloc_module_t const module int operation ... void reserved_proc7 gralloc_module_t enum GRALLOC_USAGE_SW_READ_NEVER 0x00000000 GRALLOC_USAGE_SW_READ_RARELY 0x00000002 GRALLOC_USAGE_SW_READ_OFTEN 0x00000003 GRALLOC_USAGE_SW_READ_MASK 0x0000000F GRALLOC_USAGE_SW_WRITE_NEVER 0x00000000 GRALLOC_USAGE_SW_WRITE_RARELY 0x00000020 GRALLOC_USAGE_SW_WRITE_OFTEN 0x00000030 GRALLOC_USAGE_SW_WRITE_MASK 0x000000F0 / buffer will be used as an OpenGL ES texture / GRALLOC_USAGE_HW_TEXTURE 0x00000100 / buffer will be used as an OpenGL ES render target / GRALLOC_USAGE_HW_RENDER 0x00000200 / buffer will be used by the 2D hardware blitter / GRALLOC_USAGE_HW_2D 0x00000C00 / buffer will be used with the framebuffer device / GRALLOC_USAGE_HW_FB 0x00001000 / mask for the software usage bit-mask / GRALLOC_USAGE_HW_MASK 0x00001F00 Gralloc模块是显示模块的实现。
其中 framebuffer.cpp用于基于framebuffer的显示实现gralloc是基于pmem的实现。
int gralloc_device_openconst hw_module_t module const char name hw_device_t device int status -EINVAL if strcmpname GRALLOC_HARDWARE_GPU0 gralloc_context_t dev dev gralloc_context_tmallocsizeofdev memsetdev 0 sizeofdev dev-device.common.tag HARDWARE_DEVICE_TAG dev-device.common.version 0 dev-device.common.module const_castmodule dev-device.common.close gralloc_close dev-device.alloc gralloc_alloc dev-device.free gralloc_free device dev-device.common status 0 else status fb_device_openmodule name device return status libui对gralloc模块实现了调用在ui/FramebufferNativeWindow.cpp中打开了gralloc设备。
FramebufferNativeWindow::FramebufferNativeWindow : BASE fbDev0 grDev0 mUpdateOnDemandfalse hw_module_t const module if hw_get_moduleGRALLOC_HARDWARE_MODULE_ID module 0 int stride int err err framebuffer_openmodule fbDev LOGE_IFerr couldnt open framebuffer HAL s strerror-err err gralloc_openmodule grDev LOGE_IFerr couldnt open gralloc HAL s strerror-err // ......
Android输入系统由Event 驱动程序libui中的EventHub和JAVA框架中的几个类组成。
Event的功能被集成在
android.view包的View类中在应用程序层调用主要通过View类及其继承者。
KeyEvent和MotionEvent的处理方法略有不同KeyEvent通过转化按键码得到MotionEvent通过转化RawEvent得到。
libuiEventHubJava FrameworkJava ApplicationNativeFramework/dev/input/KernelRawInputEventKeyInputQueue.kl KeyInputDeviceMotionEventRawInputEventKeyEventKeyEventWindowManagerServiceServiceAndrido.view.ViewonKeyDownonKeyUponTouchEventonTrackballEventtransferTouch / Mouse / KeyKeyLayoutMapKeyCharacterMap.kcm 输入部分的硬件抽象 EventHub.cpp 调用标准的Event设备驱动。
KeyCodeLabel.h
android/view/KeyEvent.java qwert.kl 键盘布局文件 Multi-Touch 是Eclair版本的新特性。
多点触摸的特性需要从硬件到软件系统的支持。
作为
Android的GUI系统最终的就是将消息从下传递到上面。
输入设备中增加了一个新的类型TOUCHSCREEN_MTEventHub.h 定义 EventHub获得信息只有交由JAVA层处理。
android.view.RawInputEvent增加多点数据表示InputDevice作处理并保持对非多点触摸的兼容性KeyInputQueue
android/server/KeyInputQueue进行多点数据的转化。
MotionEvent增加了对多点的支持最多支持同时有3个点触摸。
这是上层程序中用到的接口。
注意没有手势方面的解析需要应用程序根据多点信息自己实现解释。
Virtual Key 是Eclair版本的新特性。
Virtual Key的功能是利用触摸屏模拟按键发生的事件这样就可以利用触摸屏的边缘实现一些可以自定义的按键效果。
ABCDEFGH 从应用程序的角度触摸屏设备发送的是RawInputEvent原始输入事件而按键发送的是KeyEvent按键事件。
虚拟按键的作用是在某种情况下将RawInputEvent转换成KeyEvent。
按键文件/sys/board_properties/virtualkeys.devicename 代码路径 services/java/com/
android/server/InputDevice.java 通过readVirtualKeys进行消息的转化将RawInputEvent转换成KeyEvent。
virtualkeys.devicename是虚拟按键的适配文件需要放置在目标系统的以下目录中/sys/board_properties/ 文件的格式如下所示0x1:扫描码:X:Y:W:H:0x1: …… 3.1 Surface系统的结构3.2 SurfaceFlinger本地代码3.3 Surface的Java和Jni代码 Surface系统的结构 libui.so提供与Surface接口。
libsurfacefilnger.so提供实现。
Java框架层次主要调用Surface向UI提供接口。
Navtive本地调用部分主要使用ISurface。
registerBufferspostBufferunregisterBuffersISurfaceBnSurfaceIDsetLayersetPositionsetSizesetAlphagetIdentitydirtyRegionsetDirtyRegion-mClient-mSurfaceSurfacecreateSurfacedestroySurfaceISurfaceFlingerClientBnSurfaceFlingerClientcreateConnectionfreezeDisplayunfreezeDisplaysetOrientationbootFinishedrequestGPUrevokeGPUISurfaceComposerBnSurfaceComposercreateSurface-ISurfaceFlingerClientSurfaceComposerClient Surface系统的头文件路径为frameworks/base/include/ui/:ISurface.hISurfaceComposer.hISurfaceFlingerClient.hSurface.hSurfaceComposerClient.hSurface系统的源代码文件路径为frameworks/base/libs/surfaceflinger/:ISurfaceFlingerClient.cppSurfaceComposerClient.cppIsurfaceComposer.cppSurface.cppIsurface.cppSurfaceFlinger类继承了ISurfaceComposer是一个核心的实现。
class ISurfaceComposer : public IInterfacepublic: enum // keep in sync with Surface.java eHidden 0x00000004 eGPU 0x00000008 eHardware 0x00000010 eDestroyBackbuffer 0x00000020 eSecure 0x00000080 eNonPremultiplied 0x00000100 ePushBuffers 0x00000200 eFXSurfaceNormal 0x00000000 eFXSurfaceBlur 0x00010000 eFXSurfaceDim 0x00020000 eFXSurfaceMask 0x000F0000 enum ePositionChanged 0x00000001 / ... ... / enum eLayerHidden 0x01 / ... ... / enum eOrientationDefault 0 / ... ... / 代码的路径:frameworks/base/libs/surfaceflinger/class LayerBaseClient : public LayerBase class Surface : public BnSurface class Layer : public LayerBaseClientclass LayerBuffer : public LayerBaseClient class SurfaceBuffer : public LayerBaseClient::Surfaceclass LayerDim : public LayerBaseClientclass LayerBlur : public LayerBaseClient createSurfacedestroySurface-mId-mFlingerBClientcreateConnectionfreezeDisplayunfreezeDisplaysetOrientationbootFinishedrequestGPUrevokeGPUSurfaceFlingersetPositionsetLayersetSizesetAlphagetTypeIDgetTypeInfoLayerBasegetSurface-client-clientIndex-getIdentityLayerBaseClientgetSurfaceDataregisterBufferspostBufferunregisterBuffersLayerBaseClient::SurfaceLayerBuffergetSurfaceDataregisterBufferspostBufferunregisterBuffersLayerBuffer::SurfaceBufferLayerBlurgetLayerUsergetLayersgetSurfaceHeapManagerClientregisterBufferspostBufferunregisterBuffersISurfaceBnSurfaceIDsetLayersetPositionsetSizesetAlphagetIdentitydirtyRegionsetDirtyRegion-mClient-mSurfaceSurfacecreateSurfacedestroySurfaceISurfaceFlingerClientBnSurfaceFlingerClientcreateConnectionfreezeDisplayunfreezeDisplaysetOrientationbootFinishedrequestGPUrevokeGPUISurfaceComposerBnSurfaceComposercreateSurface-ISurfaceFlingerClientSurfaceComposerClientLayerDimLayerlibuiSurfaceFlinger For Create a Surface:SurfaceComposerClient::createSurface ISurface → Suface frameworks/base/libs/ui/SurfaceComposerClient.cpp→ IsurfaceFlingerClient:: createSurface→ Bclient::createSurface framework/base/libs/surfaceflinger/SurfaceFlinger.cpp→ SurfaceFlinger::createSurface framework/base/libs/surfaceflinger/SurfaceFlinger.cppLayerBaseClient layer 0sp surfaceHandle eFXSurfaceNormal → Layer or LayerBuffer eFXSurfaceBlur → LayerBlur eFXSurfaceDim → LayerDimsurfaceHandle layer-getSurfaceFor setSize:Surface::setSize→ SurfaceComposerClient::setSize SurfaceSurfaceComposerClientandroid/view/Surfaceandroid/view/SurfaceSessionsetLayersetPositionsetPositionandroid.view.Surfaceandroid.view.SurfaceSessiongetHolder-mSurface-mSurfaceHolderandroid.view.SurfaceViewgetSurfacelockCanvassetFixedSizesetFormatsetKeepScreenOnsetSizeFromLayoutsetTypeunlockCanvasAndPostandroid.view.SurfaceHolderandroid.view.SurfaceView::SurfaceHolderandroid.view.viewlibui.soJNIJAVA FrameworkJAVAApplicationz include const char const kSurfaceSessionClassPathName
android/view/SurfaceSessionconst char const kSurfaceClassPathName
android/view/Surfacestatic void nativeClassInitJNIEnv env jclass clazzJNI Code path:frameworks/base/core/jni/
android_view_Surface.cpp include const char const kSurfaceSessionClassPathName
android/view/SurfaceSessionconst char const kSurfaceClassPathName
android/view/Surfacestatic void nativeClassInitJNIEnv env jclass clazzJNI 代码的路径 :frameworks/base/core/jni/
android_view_Surface.cpp Java 代码的路径:frameworks/base/core/java/
android/view/3 个主要的类: Surface Surface.java SurfaceView SurfaceView.java SurfaceSession SurfaceSession.java 1 个接口 : SurfaceHolder SurfaceSession.java
android.view.Surface表示了一个可以绘制图形的界面它是实际调用底层的Suface接口来完成来控制的硬件载体。
关于
android.view.View类 View 类呈现了最基本的UI构造块。
一个视图占据屏幕上的一个方形区域并且负责绘制和时间处理。
View 是widgets的基类常用来创建交互式的用户图形界面GUI。
View中包含了一个Surface并处理onDrawCanvas事件。
4.1 Skia底层库4.2
Android图形系统的JNI接口4.3
Android的图形类 skia-opengl glue librarylibskiagl.soSGLSkia Graphic liblibsgl.soCore Cglibcorecg.solibjpglibgiflibpnglibft2Codec PluginSkiaCodecSkia
Android PortingAndroid basic libSkia APIandroid.view.SurfaceCanvasandroid.graphics.C框架层Java框架GraphicsJNIandroid.view.View Skia是Google一个底层的图形图像动画SVG文本等多方面的图形库它是
Android中图形系统的引擎。
Skia代码的路径 external/skia/ Skia主要包含三个库 Core Cglibcorecg.so GLSkia Graphic Libliblibsgl.so skia-opengl glue librarylibskiagl.so Skia主要包含三个库的代码路径 核心图形库libcorecg.sosrc/core/ Skia 图形库liblibsgl.sosrc/effects/src/images/src/ports/src/core/src/utils/ skia-opengl glue librarylibskiagl.sosrc/gl/
Android的图形系统和Skia底层库的联系比较紧密。
Android的图形系统的JNI提供了从Skia底层库到JAVA上层的支持。
Android的图形系统的JNI代码的路径 frameworks/base/core/jni/
android/graphic/
Android的图形系统对JAVA层提供了绘制基本图形的功能是GUI系统的基础。
Canvas.cpp是JNI中核心的接口为JAVA上层的
android.graphics.Canvas类提供了支持。
static SkCanvas initRasterJNIEnv env jobject SkBitmap bitmap return bitmap new SkCanvasbitmap : new SkCanvas static SkCanvas initGLJNIEnv env jobject return new SkGLCanvas
Android的图形类的包是
android.graphic它通过调用图形系统的JNI提供了对JAVA框架中图形系统的支持。
Android的图形系统的代码的路径 frameworks/graphic/java/
android/graphic/ Canvas.java实现图形系统中最为重要的一个类
android.graphics.Canvas。
Canvas类处理“draw” 的调用当绘制draw内容的时候需要4个基本的组件一个保持像素的A Bitmap一个处理绘制调用的Canvas写入bitmap 绘制的内容例如RectPathtext Bitmap和一个paint 用来描述颜色和样式。
5.1 OpenGL系统结构5.2 OpenGL的Native代码5.3 OpenGL的JAVA和JNI代码 libEGL libGLESv1_CM libagllibhglSoftware OpenGLHardware OpenGLcom.google.
android.gles_jnijavax.microedition.khronos.openglesJava FrameworkNativeFrameworkegl_native_window_tandroid.openglSurface系统OpenGL引擎OpenGL JNI OpenGL的本地代码:frameworks/base/opengl/libagl/frameworks/base/opengl/libs/OpenGL的本地头文件:frameworks/base/opengl/include/EGL/frameworks/base/opengl/include/GLES/OpenGL的库:libEGL.so EGL库libGLESv1_CM.so OpenGL ES库的封装libagl.so OpenGL 的软件实现库 测试代码的路径:frameworks/base/opengl/tests 使用Surface的代码: frameworks/base/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp 另外的一个本地的接口: frameworks/base/opengl/include/EGL/eglnatives.h其实现frameworks/base/libs/ui/EGLDisplaySurface.cppstruct egl_native_window_t
android_createDisplaySurfaceegl_native_window_t
android_createDisplaySurface
Android Eclair中版本之后libagl的库名称为libGLES_
android.so放置到目标系统的目录system/lib/egl。
新增OpenGL ES2的支持frameworks/base/opengl/libs/ELGS_CM库的名称为libGLESv2.so它与libGLESv1_CM.so是并列关系。