Android(安卓)display架构分析(七-1)
转 http://hi.baidu.com/leowenj/blog/item/7abbe33a309367ff3b87ce6f.html
Android display架构分析(七-1)
Surfaceflinger process 流程分析
根据前面的介绍, surfaceflinger 作为一个 server process ,上层的应用程序(作为 client )通过 Binder方 式与其进行通信。 Surfaceflinger 作为一个 thread ,这里把它分为 3 个部分,如下:
1、 Thread 本身处理部分,包括初始化以及 thread loop 。
2、 Binder 部分,负责接收上层应用的各个设置和命令,并反馈状态标志给上层。
3、 与底层的交互,负责调用底层接口( HAL )。
结构图如下:
注释:
a、 Binder 接收到应用程序的命令(如创建 surface 、设置参数等),传递给 flinger 。
b、 Flinger 完成对应命令后将相关结果状态反馈给上层。
c、 在处理上层命令过程中,根据需要设置 event (主要和显示有关),通知 Thread Loop 进行处理。
d、 Flinger 根据上层命令通知底层进行处理(主要是设置一些参数, Layer 、 position 等)
e、 Thread Loop 中进行 surface 的合成并通知底层进行显示( Post buffer )。
f、 DisplayHardware 层根据 flinger 命令调用 HAL 进行 HW 的操作。
下面来具体分析一些 SurfaceFlinger 中重要的处理函数 以及 surface 、 Layer 的属性
1 )、 readToRun
SurfaceFlinger thread 的初始化函数,主要任务是分配内存和设置底层接口 (EGL&HAL) 。
status_t SurfaceFlinger::readyToRun()
{
…
…
mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY);// 为 IPC 分配共享内存
…
mSurfaceHeapManager = new SurfaceHeapManager(this, 8 << 20);// 为 flinger 分配 heap ,大小为 8M ,存放具体的显示数据
{
// initialize the main display
GraphicPlane& plane(graphicPlane(dpy));
DisplayHardware* const hw = new DisplayHardware(this, dpy);
plane.setDisplayHardware(hw);// 保存显示接口
}
// 获取显示相关参数
const GraphicPlane& plane(graphicPlane(dpy));
const DisplayHardware& hw = plane.displayHardware();
const uint32_t w = hw.getWidth();
const uint32_t h = hw.getHeight();
const uint32_t f = hw.getFormat();
…
…
// Initialize OpenGL|ES
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
…
…
}
2 )、 ThreadLoop
Surfaceflinger 的 loop 函数,主要是等待其他接口发送的 event ,进行显示数据的合成以及显示。
bool SurfaceFlinger::threadLoop()
{
waitForEvent();// 等待其他接口的 signal event
…
…
// post surfaces (if needed)
handlePageFlip();// 处理翻页机制
const DisplayHardware& hw(graphicPlane(0).displayHardware());
if (LIKELY(hw.canDraw()))
{
// repaint the framebuffer (if needed)
handleRepaint();// 合并所有 layer 并填充到 buffer 中去
…
…
postFramebuffer();// 互换 front buffer 和 back buffer ,调用 EGL 接口进行显示
}
…
…
}
3 )、 createSurface
提供给应用程序的主要接口,该接口可以创建一个 surface ,底层会根据参数创建 layer 以及分配内存, surface 相关参数会反馈给上层
sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
ISurfaceFlingerClient::surface_data_t* params,
DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
uint32_t flags)
{
…
…
int32_t id = c->generateId(pid);
if (uint32_t(id) >= NUM_LAYERS_MAX) //NUM_LAYERS_MAX=31
{
LOGE("createSurface() failed, generateId = %d", id);
return
}
…
layer = createNormalSurfaceLocked(c, d, id, w, h, format, flags);// 创建 layer ,根据参数(宽高格式)分配内存(共 2 个 buffer : front/back buffer )
if (layer)
{
setTransactionFlags(eTransactionNeeded);
surfaceHandle = layer->getSurface();// 创建 surface
if (surfaceHandle != 0)
surfaceHandle->getSurfaceData(params);// 创建的 surface 参数反馈给应用层
}
}
待续。。。
更多相关文章
- 实例教程五:采用SharedPreferences保存用户偏好设置参数
- Android单个进程内存分配
- Linux下Android(安卓)ADB驱动安装详解
- Android(安卓)学习笔记 Contacts ContentResolver query、add、u
- Android倒计时实现
- Android之动态改变控件大小
- Android的Audio系统详解
- Android(安卓)后台服务
- Android(安卓)使用 AIDL 实现进程间通信,使用基本类型作为输入和