xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
18*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
19*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wconversion"
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <sys/resource.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <sched.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <android/frameworks/displayservice/1.0/IDisplayService.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/graphics/allocator/3.0/IAllocator.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <common/FlagManager.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <configstore/Utils.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <displayservice/DisplayService.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <errno.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <hidl/LegacySupport.h>
37*38e8c45fSAndroid Build Coastguard Worker #include <processgroup/sched_policy.h>
38*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlinger.h"
39*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlingerFactory.h"
40*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlingerProperties.h"
41*38e8c45fSAndroid Build Coastguard Worker 
42*38e8c45fSAndroid Build Coastguard Worker using namespace android;
43*38e8c45fSAndroid Build Coastguard Worker 
startGraphicsAllocatorService()44*38e8c45fSAndroid Build Coastguard Worker static status_t startGraphicsAllocatorService() {
45*38e8c45fSAndroid Build Coastguard Worker     using android::hardware::configstore::getBool;
46*38e8c45fSAndroid Build Coastguard Worker     using android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
47*38e8c45fSAndroid Build Coastguard Worker     if (!android::sysprop::start_graphics_allocator_service(false)) {
48*38e8c45fSAndroid Build Coastguard Worker         return OK;
49*38e8c45fSAndroid Build Coastguard Worker     }
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker     status_t result = hardware::registerPassthroughServiceImplementation<
52*38e8c45fSAndroid Build Coastguard Worker             android::hardware::graphics::allocator::V3_0::IAllocator>();
53*38e8c45fSAndroid Build Coastguard Worker     if (result == OK) {
54*38e8c45fSAndroid Build Coastguard Worker         return OK;
55*38e8c45fSAndroid Build Coastguard Worker     }
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker     result = hardware::registerPassthroughServiceImplementation<
58*38e8c45fSAndroid Build Coastguard Worker             android::hardware::graphics::allocator::V2_0::IAllocator>();
59*38e8c45fSAndroid Build Coastguard Worker     if (result != OK) {
60*38e8c45fSAndroid Build Coastguard Worker         ALOGE("could not start graphics allocator service");
61*38e8c45fSAndroid Build Coastguard Worker         return result;
62*38e8c45fSAndroid Build Coastguard Worker     }
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker     return OK;
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker 
startDisplayService()67*38e8c45fSAndroid Build Coastguard Worker static void startDisplayService() {
68*38e8c45fSAndroid Build Coastguard Worker     using android::frameworks::displayservice::V1_0::implementation::DisplayService;
69*38e8c45fSAndroid Build Coastguard Worker     using android::frameworks::displayservice::V1_0::IDisplayService;
70*38e8c45fSAndroid Build Coastguard Worker 
71*38e8c45fSAndroid Build Coastguard Worker     sp<IDisplayService> displayservice = sp<DisplayService>::make();
72*38e8c45fSAndroid Build Coastguard Worker     status_t err = displayservice->registerAsService();
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker     // b/141930622
75*38e8c45fSAndroid Build Coastguard Worker     if (err != OK) {
76*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Did not register (deprecated) IDisplayService service.");
77*38e8c45fSAndroid Build Coastguard Worker     }
78*38e8c45fSAndroid Build Coastguard Worker }
79*38e8c45fSAndroid Build Coastguard Worker 
main(int,char **)80*38e8c45fSAndroid Build Coastguard Worker int main(int, char**) {
81*38e8c45fSAndroid Build Coastguard Worker     signal(SIGPIPE, SIG_IGN);
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     hardware::configureRpcThreadpool(1 /* maxThreads */,
84*38e8c45fSAndroid Build Coastguard Worker             false /* callerWillJoin */);
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     startGraphicsAllocatorService();
87*38e8c45fSAndroid Build Coastguard Worker 
88*38e8c45fSAndroid Build Coastguard Worker     // When SF is launched in its own process, limit the number of
89*38e8c45fSAndroid Build Coastguard Worker     // binder threads to 4.
90*38e8c45fSAndroid Build Coastguard Worker     ProcessState::self()->setThreadPoolMaxThreadCount(4);
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     // Set uclamp.min setting on all threads, maybe an overkill but we want
93*38e8c45fSAndroid Build Coastguard Worker     // to cover important threads like RenderEngine.
94*38e8c45fSAndroid Build Coastguard Worker     if (SurfaceFlinger::setSchedAttr(true) != NO_ERROR) {
95*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Failed to set uclamp.min during boot: %s", strerror(errno));
96*38e8c45fSAndroid Build Coastguard Worker     }
97*38e8c45fSAndroid Build Coastguard Worker 
98*38e8c45fSAndroid Build Coastguard Worker     // The binder threadpool we start will inherit sched policy and priority
99*38e8c45fSAndroid Build Coastguard Worker     // of (this) creating thread. We want the binder thread pool to have
100*38e8c45fSAndroid Build Coastguard Worker     // SCHED_FIFO policy and priority 1 (lowest RT priority)
101*38e8c45fSAndroid Build Coastguard Worker     // Once the pool is created we reset this thread's priority back to
102*38e8c45fSAndroid Build Coastguard Worker     // original.
103*38e8c45fSAndroid Build Coastguard Worker     int newPriority = 0;
104*38e8c45fSAndroid Build Coastguard Worker     int origPolicy = sched_getscheduler(0);
105*38e8c45fSAndroid Build Coastguard Worker     struct sched_param origSchedParam;
106*38e8c45fSAndroid Build Coastguard Worker 
107*38e8c45fSAndroid Build Coastguard Worker     int errorInPriorityModification = sched_getparam(0, &origSchedParam);
108*38e8c45fSAndroid Build Coastguard Worker     if (errorInPriorityModification == 0) {
109*38e8c45fSAndroid Build Coastguard Worker         int policy = SCHED_FIFO;
110*38e8c45fSAndroid Build Coastguard Worker         newPriority = sched_get_priority_min(policy);
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker         struct sched_param param;
113*38e8c45fSAndroid Build Coastguard Worker         param.sched_priority = newPriority;
114*38e8c45fSAndroid Build Coastguard Worker 
115*38e8c45fSAndroid Build Coastguard Worker         errorInPriorityModification = sched_setscheduler(0, policy, &param);
116*38e8c45fSAndroid Build Coastguard Worker     }
117*38e8c45fSAndroid Build Coastguard Worker 
118*38e8c45fSAndroid Build Coastguard Worker     // start the thread pool
119*38e8c45fSAndroid Build Coastguard Worker     sp<ProcessState> ps(ProcessState::self());
120*38e8c45fSAndroid Build Coastguard Worker     ps->startThreadPool();
121*38e8c45fSAndroid Build Coastguard Worker 
122*38e8c45fSAndroid Build Coastguard Worker     // Reset current thread's policy and priority
123*38e8c45fSAndroid Build Coastguard Worker     if (errorInPriorityModification == 0) {
124*38e8c45fSAndroid Build Coastguard Worker         errorInPriorityModification = sched_setscheduler(0, origPolicy, &origSchedParam);
125*38e8c45fSAndroid Build Coastguard Worker     } else {
126*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Failed to set SurfaceFlinger binder threadpool priority to SCHED_FIFO");
127*38e8c45fSAndroid Build Coastguard Worker     }
128*38e8c45fSAndroid Build Coastguard Worker 
129*38e8c45fSAndroid Build Coastguard Worker     // instantiate surfaceflinger
130*38e8c45fSAndroid Build Coastguard Worker     sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker     // Set the minimum policy of surfaceflinger node to be SCHED_FIFO.
133*38e8c45fSAndroid Build Coastguard Worker     // So any thread with policy/priority lower than {SCHED_FIFO, 1}, will run
134*38e8c45fSAndroid Build Coastguard Worker     // at least with SCHED_FIFO policy and priority 1.
135*38e8c45fSAndroid Build Coastguard Worker     if (errorInPriorityModification == 0) {
136*38e8c45fSAndroid Build Coastguard Worker         flinger->setMinSchedulerPolicy(SCHED_FIFO, newPriority);
137*38e8c45fSAndroid Build Coastguard Worker     }
138*38e8c45fSAndroid Build Coastguard Worker 
139*38e8c45fSAndroid Build Coastguard Worker     setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
140*38e8c45fSAndroid Build Coastguard Worker 
141*38e8c45fSAndroid Build Coastguard Worker     set_sched_policy(0, SP_FOREGROUND);
142*38e8c45fSAndroid Build Coastguard Worker 
143*38e8c45fSAndroid Build Coastguard Worker     // initialize before clients can connect
144*38e8c45fSAndroid Build Coastguard Worker     flinger->init();
145*38e8c45fSAndroid Build Coastguard Worker 
146*38e8c45fSAndroid Build Coastguard Worker     // publish surface flinger
147*38e8c45fSAndroid Build Coastguard Worker     sp<IServiceManager> sm(defaultServiceManager());
148*38e8c45fSAndroid Build Coastguard Worker     sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
149*38e8c45fSAndroid Build Coastguard Worker                    IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
150*38e8c45fSAndroid Build Coastguard Worker 
151*38e8c45fSAndroid Build Coastguard Worker     // publish gui::ISurfaceComposer, the new AIDL interface
152*38e8c45fSAndroid Build Coastguard Worker     sp<SurfaceComposerAIDL> composerAIDL = sp<SurfaceComposerAIDL>::make(flinger);
153*38e8c45fSAndroid Build Coastguard Worker     if (FlagManager::getInstance().misc1()) {
154*38e8c45fSAndroid Build Coastguard Worker         composerAIDL->setMinSchedulerPolicy(SCHED_FIFO, newPriority);
155*38e8c45fSAndroid Build Coastguard Worker     }
156*38e8c45fSAndroid Build Coastguard Worker     sm->addService(String16("SurfaceFlingerAIDL"), composerAIDL, false,
157*38e8c45fSAndroid Build Coastguard Worker                    IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker     startDisplayService(); // dependency on SF getting registered above
160*38e8c45fSAndroid Build Coastguard Worker 
161*38e8c45fSAndroid Build Coastguard Worker     if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) {
162*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Failed to set SCHED_FIFO during boot: %s", strerror(errno));
163*38e8c45fSAndroid Build Coastguard Worker     }
164*38e8c45fSAndroid Build Coastguard Worker 
165*38e8c45fSAndroid Build Coastguard Worker     // run surface flinger in this thread
166*38e8c45fSAndroid Build Coastguard Worker     flinger->run();
167*38e8c45fSAndroid Build Coastguard Worker 
168*38e8c45fSAndroid Build Coastguard Worker     return 0;
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker 
171*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
172*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop // ignored "-Wconversion"
173