android 4.0.3android 系统支持分屏BTm...

Android 4.0.3发布 关注系统优化和漏洞修复
来源:搜狐IT
  【搜狐IT消息】北京时间12月17日消息,据国外媒体报道,在电信运营商Verizon面向Galaxy Nexus用户发布Android 4.0.2一天后,Google正式发布Android 4.0.3,Google称之为Android 4.0 (Ice Cream Sandwich)平台增发版。
  新版Android包括了面向平板和手机平台的系统优化和漏洞修复,以及面向开发者的少量新API。
  新API包括联系人程序支持服务社交流API,日历程序支持服务改进,新的相机性能(应用可检查和管理视频稳定性,必要时可利用QVGA分辨率文档),已经提供了易用性(文本转语音引擎采用新的状态和错误报告)。
  另外,新版Android还对“图形,数据库,拼写检查,蓝牙等”进行了细微的改进。
  Google表示,Android 4.0.3将成为手机合作商的基本版Ice Cream Sandwich,预计未来数周同时面向手机和平板发布。(Edward)
(责任编辑:樵夫)
11-12-14·
11-11-11·
11-11-03·
11-10-19·
11-09-22·
11-09-20·
11-09-05·
11-09-01·
11-07-11·
11-05-09·
网友点击排行
网友评论排行
高清影视剧
4寸iPhone SE发布 售价3288元起中国首发
移动新发现
近期热点关注下次自动登录
现在的位置:
& 综合 & 正文
Android4.0.3 显示系统深入理解
网上已经有很多兄弟对Android的显示系统做了深入解剖,很是佩服。可最近小弟在研究Android4.0时发现出入比较大,也许是Android4.0的修改比较多吧!因为小弟没有看Android4.0以前的代码。
面对这么复杂一个Android显示系统,如何入手呢? 根据以前的经验,不管它有多么复杂,其功能不就是以下三步曲吗?
  1)显示系统的创建及初始化
哪我的分析就从显示系统的创建及初始化开始吧!由于小弟对Java没有什么研究兴趣,所有重点就分析Native部分。当然Native的入口就在android_view_Surface.cpp中,此文件主要包含以下两部分给Java层调用:
1)gSurfaceSessionMethods: 操作SurfaceSession的方法
2)gSurfaceMethods:操作Surface的方法
2. android_view_Surface.cpp
2.1 SurfaceSession操作方法
static JNINativeMethod gSurfaceSessionMethods[] = {
(void*)SurfaceSession_init },
{"destroy",
(void*)SurfaceSession_destroy },
(void*)SurfaceSession_kill },
2.1.1 SurfaceSession_init
其功能如下:
1)创建SurfaceComposerClient对象
2)调用SurfaceComposerClient::onFirstRef方法
现在已经进入到SurfaceComposerClient的地盘,根据其名字含义,它应该是一个进行Surface合成的客户端,通过它发命令给SurfaceFlinger来进行需要的操作。其初始化流程如下图所示:
2.1.2 SurfaceComposerClient.cpp中的宝贝
为了方便后面的理解,先看看SurfaceComposerClient中有些什么宝贝来完成这个任务。在其中定义了如下几个类:
2.1.2.1 ComposerService(获取SurfaceFlinger服务)
一看到名字为Service,应该是用于从SurfaceFlinger中获取Service以建立连接关系&它是一个单实例,一个进程有且只有一个实例对象&,然后供后面进行相关的操作。其构造函数代码如下:
class ComposerService : public Singleton&ComposerService&
sp&ISurfaceComposer& mComposerS
sp&IMemoryHeap& mServerCblkM
surface_flinger_cblk_t volatile* mServerC
ComposerService();
friend class Singleton&ComposerService&;
static sp&ISurfaceComposer& getComposerService();
static surface_flinger_cblk_t const volatile * getControlBlock();
ComposerService::ComposerService()
: Singleton&ComposerService&() {
const String16 name("SurfaceFlinger");
while (getService(name, &mComposerService) != NO_ERROR) {
usleep(250000);
mServerCblkMemory = mComposerService-&getCblk();
mServerCblk = static_cast&surface_flinger_cblk_t volatile *&(
mServerCblkMemory-&getBase());
由此可见,ComposerService主要是获取SurfaceFlinger服务、获取在SurfaceFlinger::readyToRun中创建的共享内存块及其基地址。在Client中,谁要想与SurfaceFlinger通信,需要通过接口getComposerService来获取此BpSurfaceComposer。
此ComposerService是在调用ComposerService::getInstance时进行有且只有一个的实例化,因为前面讲过,它是一个单实例。
2.1.2.2 Composer
它也是一个单实例,管理并发送每个layer的ComposerState。其定义如下:
struct ComposerState {
sp&ISurfaceComposerClient&
layer_state_
write(Parcel& output) const;
read(const Parcel& input);
class Composer : public Singleton&Composer&
friend class Singleton&Composer&;
mutable Mutex
SortedVector&ComposerState& mS
Composer() : Singleton&Composer&(),
mOrientation(ISurfaceComposer::eOrientationUnchanged) { }
void closeGlobalTransactionImpl();
layer_state_t* getLayerStateLocked(
const sp&SurfaceComposerClient&& client, SurfaceID id);
status_t setPosition(const sp&SurfaceComposerClient&& client, SurfaceID id,
float x, float y);
status_t setSize(const sp&SurfaceComposerClient&& client, SurfaceID id,
uint32_t w, uint32_t h);
status_t setLayer(const sp&SurfaceComposerClient&& client, SurfaceID id,
int32_t z);
status_t setFlags(const sp&SurfaceComposerClient&& client, SurfaceID id,
uint32_t flags, uint32_t mask);
status_t setTransparentRegionHint(
const sp&SurfaceComposerClient&& client, SurfaceID id,
const Region& transparentRegion);
status_t setAlpha(const sp&SurfaceComposerClient&& client, SurfaceID id,
float alpha);
status_t setMatrix(const sp&SurfaceComposerClient&& client, SurfaceID id,
float dsdx, float dtdx, float dsdy, float dtdy);
status_t setFreezeTint(
const sp&SurfaceComposerClient&& client, SurfaceID id,
uint32_t tint);
status_t setOrientation(int orientation);
static void closeGlobalTransaction() {
Composer::getInstance().closeGlobalTransactionImpl();
把上面的comments看完就明白了,Composer管理每个SurfaceComposerClient中的每一个Surface的状态,并记录在ComposerState的layer_state_t中,然后调用者可以调用其closeGlobalTransaction方法把这些mStates发送给SurfaceFlinger处理(处理函数为:SurfaceFlinger::setTransactionState)。
谁来调用它的方法设置层的属性及发送mStates呢? -----***是由SurfaceComposerClient来调用。
SurfaceComposerClient
前面介绍的两个类一个用于获取SurfaceFlinger服务;一个用于记录每个Layer的状态,且可按要求把这些CoposerState发送给SurfaceFlinger。这个类是不是来使用前面两个类提供的服务呢? --***是肯定的。其定义及详细注释如下:
#define NUM_DISPLAY_MAX 4
//最多支持四个显示屏
struct display_cblk_t
reserved[2];
struct surface_flinger_cblk_t
reserved[3];
display_cblk_t
displays[NUM_DISPLAY_MAX];
class SurfaceComposerClient : public RefBase
friend class C
SurfaceComposerClient();
~SurfaceComposerClient();
sp&SurfaceControl& createSurface(
const String8& name,
DisplayID display,
uint32_t w,
uint32_t h,
PixelFormat format,
uint32_t flags = 0
static void openGlobalTransaction();
static void closeGlobalTransaction();
static status_t freezeDisplay(DisplayID dpy, uint32_t flags = 0);
static status_t unfreezeDisplay(DisplayID dpy, uint32_t flags = 0);
static int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
static ssize_t getNumberOfDisplays();
static status_t getDisplayInfo(DisplayID dpy, DisplayInfo* info);
static ssize_t getDisplayWidth(DisplayID dpy);
static ssize_t getDisplayHeight(DisplayID dpy);
static ssize_t getDisplayOrientation(DisplayID dpy);
status_t linkToComposerDeath(const sp&IBinder::DeathRecipient&& recipient,
void* cookie = NULL, uint32_t flags = 0);
hide(SurfaceID id);
show(SurfaceID id, int32_t layer = -1);
freeze(SurfaceID id);
unfreeze(SurfaceID id);
setFlags(SurfaceID id, uint32_t flags, uint32_t mask);
setTransparentRegionHint(SurfaceID id, const Region& transparent);
setLayer(SurfaceID id, int32_t layer);
setAlpha(SurfaceID id, float alpha=1.0f);
setFreezeTint(SurfaceID id, uint32_t tint);
setMatrix(SurfaceID id, float dsdx, float dtdx, float dsdy, float dtdy);
setPosition(SurfaceID id, float x, float y);
setSize(SurfaceID id, uint32_t w, uint32_t h);
destroySurface(SurfaceID sid);
virtual void onFirstRef();
Composer& getComposer();
sp&ISurfaceComposerClient&
其功能列表如下:
1)获取BpSurfaceComposerClient(即mClient),在onFirstRef中实现
2)通过BpSurfaceComposerClient(即mClient)创建和销毁Surface
3)通过Composer来记录Surface和显示屏状态变化,及在Composer中通过BpSurfaceComposer把状态变化发给SurfaceFlinger处理
至此,SurfaceComposerClient功能已经分析清楚。可是从这三个类中,我们已经看到三个Bp(BpSurfaceComposer,BpSurfaceComposerClient和BpSurface)及三个对应的接口。下面总结一下,每个接口的功能,在客户端由谁使用,在服务器端谁来实现。
2.1.2.4 Surface相关接口总结
2.2 Surface操作
其相关接口如下:
static JNINativeMethod gSurfaceMethods[] = {
{"nativeClassInit",
(void*)nativeClassInit },
"(Landroid/view/SurfaceSILjava/lang/SIIIII)V",
(void*)Surface_init },
"(Landroid/os/P)V",
(void*)Surface_initParcel },
{"initFromSurfaceTexture", "(Landroid/graphics/SurfaceT)V", (void*)Surface_initFromSurfaceTexture },
{"getIdentity",
(void*)Surface_getIdentity },
{"destroy",
(void*)Surface_destroy },
{"release",
(void*)Surface_release },
{"copyFrom",
"(Landroid/view/S)V",
(void*)Surface_copyFrom },
{"isValid",
(void*)Surface_isValid },
{"lockCanvasNative",
"(Landroid/graphics/R)Landroid/graphics/C",
(void*)Surface_lockCanvas },
{"unlockCanvasAndPost", "(Landroid/graphics/C)V", (void*)Surface_unlockCanvasAndPost },
{"unlockCanvas",
"(Landroid/graphics/C)V", (void*)Surface_unlockCanvas },
{"openTransaction",
(void*)Surface_openTransaction },
{"closeTransaction",
(void*)Surface_closeTransaction },
{"setOrientation",
"(III)V", (void*)Surface_setOrientation },
{"freezeDisplay",
"(I)V", (void*)Surface_freezeDisplay },
{"unfreezeDisplay",
"(I)V", (void*)Surface_unfreezeDisplay },
{"screenshot",
"(II)Landroid/graphics/B", (void*)Surface_screenshotAll },
{"screenshot",
"(IIII)Landroid/graphics/B", (void*)Surface_screenshot },
{"setLayer",
"(I)V", (void*)Surface_setLayer },
{"setPosition",
"(FF)V",(void*)Surface_setPosition },
{"setSize",
"(II)V",(void*)Surface_setSize },
(void*)Surface_hide },
(void*)Surface_show },
{"freeze",
(void*)Surface_freeze },
{"unfreeze",
(void*)Surface_unfreeze },
{"setFlags",
"(II)V",(void*)Surface_setFlags },
{"setTransparentRegionHint","(Landroid/graphics/R)V", (void*)Surface_setTransparentRegion },
{"setAlpha",
"(F)V", (void*)Surface_setAlpha },
{"setMatrix",
"(FFFF)V",
(void*)Surface_setMatrix },
{"setFreezeTint",
(void*)Surface_setFreezeTint },
{"readFromParcel",
"(Landroid/os/P)V", (void*)Surface_readFromParcel },
{"writeToParcel",
"(Landroid/os/PI)V", (void*)Surface_writeToParcel },
2.2.1 Surface_init调用流程
在SurfaceFlinger端创建BSurface,在客户端返回SurfaceControl,同时在SurfaceControl中拥有了BpSurface用于与BSurface交互。
2.2.1.1 调用流程分析
BpSurfaceComposerClient-&createSurface返回BpSurface。且通过参数返回ISurfaceComposerClient::surface_data_t,其定义如下:
其中token在SurfaceComposerClient的函数参数中,对应于SurfaceID。即在客户端,它就是SurfaceID。
token: 加入到Client::mLayers中的序号,在Client中单调递增,初始值为:1,一个Layer创建一个BSurface
identity: LayerBaseClient中的mIdentity,在所有的Layer中单调递增,初始值为:1
struct surface_data_t {
status_t readFromParcel(const Parcel& parcel);
status_t writeToParcel(Parcel* parcel) const;
2.2.1.2 创建真正的Surface
在Layer::createSurface中创建真正的BSurface,在SurfaceFlinger::createSurface中调用layer-&getSurface时创建的。此BSurface定义如下:
sp&ISurface& Layer::createSurface()
class BSurface : public BnSurface, public LayerCleaner {
wp&const Layer& mO
virtual sp&ISurfaceTexture& getSurfaceTexture() const {
sp&ISurfaceTexture&
sp&const Layer& that( mOwner.promote() );
if (that != NULL) {
res = that-&mSurfaceT
BSurface(const sp&SurfaceFlinger&& flinger,
const sp&Layer&& layer)
: LayerCleaner(flinger, layer), mOwner(layer) { }
sp&ISurface& sur(new BSurface(mFlinger, this));
在此BSurface中实现了ISurface的接口getSurfaceTexture,在此接口中返回Layer::mSurfaceTexture(类型为:SurfaceTextureLayer,它才是真正操作内存的东东),此成员在Layer::onFirstRef中创建,SurfaceTextureLayer是SurfaceTexture的派生类,代码如下:
void Layer::onFirstRef()
LayerBaseClient::onFirstRef();
struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
FrameQueuedListener(Layer* layer) : mLayer(layer) { }
wp&Layer& mL
virtual void onFrameAvailable() {
sp&Layer& that(mLayer.promote());
if (that != 0) {
that-&onFrameQueued();
mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
mSurfaceTexture-&setFrameAvailableListener(new FrameQueuedListener(this));
mSurfaceTexture-&setSynchronousMode(true);
mSurfaceTexture-&setBufferCountServer(2);
2.2.1.3 不得不说的SurfaceControl
本来Surface_init调用SurfaceComposerClient::createSurface创建一个Surface,可却返回了一个SurfaceControl,下面看看SurfaceCotrol到底做了些什么,以及如何做的?
相关数据结构如下图所示:
SurfaceControl定义如下:
class SurfaceControl : public RefBase
setLayer(int32_t layer);
setPosition(int32_t x, int32_t y);
setSize(uint32_t w, uint32_t h);
show(int32_t layer = -1);
unfreeze();
setFlags(uint32_t flags, uint32_t mask);
setTransparentRegionHint(const Region& transparent);
setAlpha(float alpha=1.0f);
setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
setFreezeTint(uint32_t tint);
static status_t writeSurfaceToParcel(
const sp&SurfaceControl&& control, Parcel* parcel);
sp&Surface& getSurface() const;
SurfaceControl(
const sp&SurfaceComposerClient&& client,
const sp&ISurface&& surface,
const ISurfaceComposerClient::surface_data_t& data);
~SurfaceControl();
void destroy();
sp&SurfaceComposerClient&
sp&ISurface&
mutable Mutex
mutable sp&Surface&
从其定义中可以看出,在getSurface中将有新花样,其它操作函数都是直接以mToken作为SurfaceID,直接调用SurfaceComposerClient中对应方法。 经过这样一分析,SurfaceControl也没什么神秘的了。但它的getSurface到有点神秘。
2.2.2 getSurface流程
getSurface在客户端返回Surface(派生于SurfaceTextureClient),并在Surface的mSurfaceTexture域中保存了BpSurfaceTexture。
前面Surface初始化之后,就可以getSurface了。getSurface流程如下图所示:
有了Surface,且在Surface中又有了BpSurfaceTexture,下一步就操作GraphicBuffer了。
3. 画图流程
对于画图流程,可以从ViewRootImpl(ViewRootImpl.java)的draw函数看起,在画图之间,它要调用java层的surface.lockCanvas,画完图之后调用surface.unlockCanvasAndPost来提交显示。
surface.lockCanvas-&
lockCanvasNative(Java)-&
(C++)Surface_lockCanvas&android_view_Surface.cpp&
surface.unlockCanvasAndPost(Java)-&
(C++)Surface_unlockCanvasAndPost&android_view_Surface.cpp&
本章主要分析这两个函数到底做了些什么&
3.1 Surface_lockCanvas
Android图形系统中一个重要的概念是surface。View及其子类(如TextView, Button)要画在surface上。每个surface创建一个Canvas对象(但属性时常改变),用来管理view在surface上的绘图操作,如画点画线。每个canvas对象对应一个bitmap,存储画在surface上的内容。
3.1.1 相关数据结构定义
3.1.1.1 ANativeWindow_Buffer
typedef struct ANativeWindow_Buffer {
uint32_t reserved[6];
} ANativeWindow_B
3.1.1.2 SurfaceInfo
struct SurfaceInfo {
reserved[2];
3.1.1.3 二者对应关系
SurfaceInfo*
ANativeWindow_Buffer outB
other-&w = uint32_t(outBuffer.width);
other-&h = uint32_t(outBuffer.height);
other-&s = uint32_t(outBuffer.stride);
other-&usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
other-&format = uint32_t(outBuffer.format);
other-&bits = outBuffer.
3.1.1.4 GraphicBuffer
在分析下面的流程时, 不得不对GraphicBuffer进行深入了解,特别是其Flattenable interface,这是实现画图buffer的关键。其相关定义如下:
typedef struct native_handle
int data[0];
} native_handle_t;
typedef const native_handle_t* buffer_handle_t;
class GraphicBuffer
: public EGLNativeBase&
ANativeWindowBuffer,
GraphicBuffer,
LightRefBase&GraphicBuffer& &, public Flattenable
size_t getFlattenedSize() const;
size_t getFdCount() const;
status_t flatten(void* buffer, size_t size,
int fds[], size_t count) const;
status_t unflatten(void const* buffer, size_t size,
int fds[], size_t count);
buffer_handle_
3.1.1.5 Flattenable interface

参考资料

 

随机推荐