一、什么是SurfaceFlinger
SurfaceFlinger 是 Android 系统的核心系统服务之一,负责合成和显示所有图形界面内容。它作为系统级图形合成引擎,接收来自各个应用和系统组件的图形缓冲区(如 SurfaceView、TextureView、系统UI等),通过 GPU 硬件加速 将这些独立的图形层合成为最终的屏幕显示图像,并精确控制帧率同步(VSync)、窗口层级管理、动画渲染和多屏显示等关键功能,确保整个系统的流畅、无撕裂的视觉体验。
二、SurfaceFlinger服务启动和初始化
当系统内核启动第一个用户空间进程init进程后,init进程解析init.rc文件来启动一些必要的系统服务,其中包含:
...
on boot
...
class_start core
...
class_start core表示启动所有类别属于core的服务,那我们在看下surfaceflinger.rc文件:
service surfaceflinger /system/bin/surfaceflinger
class core animation
user system
group graphics drmrpc readproc
capabilities SYS_NICE
onrestart restart --only-if-running zygote
task_profiles HighPerformance
我们可以看到 class core animation,说明surfaceflinger属于系统核心组同时也属于动画服务组,所以这里我们就可以知道init进程会在系统启动的时候启动core组成员之一的surfaceflinger服务。
那么我们来看下SurfaceFlinger进程对应的类的入口在哪里,怎么看呢,我们可以找到AOSP中SurfaceFlinger模块,找到模块中的frameworks/native/services/surfaceflinger/Android.bp这个文件:
...
filegroup {
// 看命名可以知道是surfaceflinger二进制文件对应的源码文件
name: "surfaceflinger_binary_sources",
srcs: [
":libsurfaceflinger_sources",
// 入口在这个文件中
"main_surfaceflinger.cpp",
],
}
...
SurfaceFlinger被编译生成的二进制文件对应的源码上面说的很清楚了,所以我们知道进程的启动入口就是在类main_surfaceflinger.cpp文件中的main()函数:

int main() {
// 对于SIGPIPE信号,选择忽略(SIG_ING)
// SIGPIPE信号可能是连接surfaceflinger的APP异常退出的时候系统发出此信号
// 这里选择忽略,避免surfaflinger服务被影响kill掉
signal(SIGPIPE, SIG_IGN);
//
hardware::configureRpcThreadpool(1 /* maxThreads */,
false /* callerWillJoin */);
// 核心流程1:
startGraphicsAllocatorService();
// When SF is launched in its own process, limit the number of
// binder threads to 4.
ProcessState::self()->setThreadPoolMaxThreadCount(4);
// Set uclamp.min setting on all threads, maybe an overkill but we want
// to cover important threads like RenderEngine.
SurfaceFlinger::setSchedAttr(true, __func__);
// The binder threadpool we start will inherit sched policy and priority
// of (this) creating thread. We want the binder thread pool to have
// SCHED_FIFO policy and priority 1 (lowest RT priority)
// Once the pool is created we reset this thread's priority back to
// original.
int newPriority = 0;
int origPolicy = sched_getscheduler(0);
struct sched_param origSchedParam;
int errorInPriorityModification = sched_getparam(0, &origSchedParam);
if (errorInPriorityModification == 0) {
int policy = SCHED_FIFO;
newPriority = sched_get_priority_min(policy);
struct sched_param param;
param.sched_priority = newPriority;
errorInPriorityModification = sched_setscheduler(0, policy, ¶m);
}
// start the thread pool
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
// Reset current thread's policy and priority
if (errorInPriorityModification == 0) {
errorInPriorityModification = sched_setscheduler(0, origPolicy, &origSchedParam);
} else {
ALOGE("Failed to set SurfaceFlinger binder threadpool priority to SCHED_FIFO");
}
// 核心流程2:
// instantiate surfaceflinger
sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();
// Set the minimum policy of surfaceflinger node to be SCHED_FIFO.
// So any thread with policy/priority lower than {SCHED_FIFO, 1}, will run
// at least with SCHED_FIFO policy and priority 1.
if (errorInPriorityModification == 0 &&
!FlagManager::getInstance().disable_sched_fifo_sf_binder()) {
flinger->setMinSchedulerPolicy(SCHED_FIFO, newPriority);
}
setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
set_sched_policy(0, SP_FOREGROUND);
// 核心流程3:
// initialize before clients can connect
flinger->init();
// 核心流程4:
// publish surface flinger
sp<IServiceManager> sm(defaultServiceManager());
sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
// publish

最低0.47元/天 解锁文章
1390

被折叠的 条评论
为什么被折叠?



