android display 笔记(六)SurfaceFlinger初始化

Surfaceflinger初始化

源码:android 14
在/frameworks/native/services/surfaceflinger/路径下可以看到 与surfaceflinger 很多相关的文件
如:
SurfaceFlinger.cpp
SurfaceFlinger.h
Layer.cpp
Layer.h
main_surfaceflinger.cpp等

Surfaceflinger的初始化就是在 main_surfaceflinger 中完成的。

/frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp
int main(int, char**) {
80      signal(SIGPIPE, SIG_IGN);
81  
82      hardware::configureRpcThreadpool(1 /* maxThreads */,
83              false /* callerWillJoin */);
84  
85      startGraphicsAllocatorService();
86  
87      // When SF is launched in its own process, limit the number of
88      // binder threads to 4.
89      ProcessState::self()->setThreadPoolMaxThreadCount(4);
90  
91      // Set uclamp.min setting on all threads, maybe an overkill but we want
92      // to cover important threads like RenderEngine.
93      if (SurfaceFlinger::setSchedAttr(true) != NO_ERROR) {
94          ALOGW("Failed to set uclamp.min during boot: %s", strerror(errno));
95      }
96  
97      // The binder threadpool we start will inherit sched policy and priority
98      // of (this) creating thread. We want the binder thread pool to have
99      // SCHED_FIFO policy and priority 1 (lowest RT priority)
100      // Once the pool is created we reset this thread's priority back to
101      // original.
102      int newPriority = 0;
103      int origPolicy = sched_getscheduler(0);
104      struct sched_param origSchedParam;
105  
106      int errorInPriorityModification = sched_getparam(0, &origSchedParam);
107      if (errorInPriorityModification == 0) {
108          int policy = SCHED_FIFO;
109          newPriority = sched_get_priority_min(policy);
110  
111          struct sched_param param;
112          param.sched_priority = newPriority;
113  
114          errorInPriorityModification = sched_setscheduler(0, policy, &param);
115      }
116  
117      // start the thread pool
118      sp<ProcessState> ps(ProcessState::self());
119      ps->startThreadPool();
120  
121      // Reset current thread's policy and priority
122      if (errorInPriorityModification == 0) {
123          errorInPriorityModification = sched_setscheduler(0, origPolicy, &origSchedParam);
124      } else {
125          ALOGE("Failed to set SurfaceFlinger binder threadpool priority to SCHED_FIFO");
126      }
127  
128      // instantiate surfaceflinger
129      sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();
130  
131      // Set the minimum policy of surfaceflinger node to be SCHED_FIFO.
132      // So any thread with policy/priority lower than {SCHED_FIFO, 1}, will run
133      // at least with SCHED_FIFO policy and priority 1.
134      if (errorInPriorityModification == 0) {
135          flinger->setMinSchedulerPolicy(SCHED_FIFO, newPriority);
136      }
137  
138      setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
139  
140      set_sched_policy(0, SP_FOREGROUND);
141  
142      // initialize before clients can connect
143      flinger->init();
144  
145      // publish surface flinger
146      sp<IServiceManager> sm(defaultServiceManager());
147      sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
148                     IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
149  
150      // publish gui::ISurfaceComposer, the new AIDL interface
151      sp<SurfaceComposerAIDL> composerAIDL = sp<SurfaceComposerAIDL>::make(flinger);
152      sm->addService(String16("SurfaceFlingerAIDL"), composerAIDL, false,
153                     IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
154  
155      startDisplayService(); // dependency on SF getting registered above
156  
157      if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) {
158          ALOGW("Failed to set SCHED_FIFO during boot: %s", strerror(errno));
159      }
160  
161      // run surface flinger in this thread
162      flinger->run();
163  
164      return 0;
165  }

118 sp ps(ProcessState::self());
119 ps->startThreadPool();
创建新线程并加入到线程池

sp flinger = surfaceflinger::createSurfaceFlinger();
flinger->init();
在客户端可以连接时进行初始化处理

47 sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
148 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
addService 将SurfaceFlinger 对象添加到服务管理进程。

flinger->run();
开始运行

客户端进程与SurfaceFlinger交互

什么是客户进程,指的是使用SurfaceFlinger服务的进程,通常是通过SurfaceComposerClient与SurfaceFlinger进程交互。
SurfaceComposerClient是native 目录下面
这里面代码很多,暂时先看建立通信连接的代码

/frameworks/native/libs/gui/SurfaceComposerClient.cpp
2349  void SurfaceComposerClient::onFirstRef() {
2350      sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2351      if (sf != nullptr && mStatus == NO_INIT) {
2352          sp<ISurfaceComposerClient> conn;
2353          binder::Status status = sf->createConnection(&conn);
2354          if (status.isOk() && conn != nullptr) {
2355              mClient = conn;
2356              mStatus = NO_ERROR;
2357          }
2358      }
2359  }

onFirstRef():该方法是SurfaceComposerClient对象第一次引用时会调用该方法。
getComposerService():向服务管理进程查询得到ISurfaceComposer对象,客户进程可以通过该对象向Surfaceflinger发送请求
createConnection():创建客户对象ISurfaceComposerClient并保存到mClient。
ISurfaceComposerClient通过mClient与SurfaceFlinger建立通信连接。

在SurfaceFlinger中收到createConnection后,完成以下处理,会对initCheck进行判断,如果无报错,则将client赋给*outClient,并返回binder Status ok 状态,否则为null

 /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
 binder::Status SurfaceComposerAIDL::createConnection(sp<gui::ISurfaceComposerClient>* outClient) {
8397      const sp<Client> client = sp<Client>::make(mFlinger);
8398      if (client->initCheck() == NO_ERROR) {
8399          *outClient = client;
8400          return binder::Status::ok();
8401      } else {
8402          *outClient = nullptr;
8403          return binderStatusFromStatusT(BAD_VALUE);
8404      }
8405  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那天的烟花雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值