1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <sys/resource.h>
18 
19 #include <sched.h>
20 
21 #include <android/frameworks/displayservice/1.0/IDisplayService.h>
22 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
23 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
24 #include <android/hardware/graphics/allocator/3.0/IAllocator.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/ProcessState.h>
28 #include <configstore/Utils.h>
29 #include <displayservice/DisplayService.h>
30 #include <hidl/LegacySupport.h>
31 #include <processgroup/sched_policy.h>
32 #include "SurfaceFlinger.h"
33 #include "SurfaceFlingerFactory.h"
34 #include "SurfaceFlingerProperties.h"
35 
36 using namespace android;
37 
startGraphicsAllocatorService()38 static status_t startGraphicsAllocatorService() {
39     using android::hardware::configstore::getBool;
40     using android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
41     if (!android::sysprop::start_graphics_allocator_service(false)) {
42         return OK;
43     }
44 
45     status_t result = hardware::registerPassthroughServiceImplementation<
46             android::hardware::graphics::allocator::V3_0::IAllocator>();
47     if (result == OK) {
48         return OK;
49     }
50 
51     result = hardware::registerPassthroughServiceImplementation<
52             android::hardware::graphics::allocator::V2_0::IAllocator>();
53     if (result != OK) {
54         ALOGE("could not start graphics allocator service");
55         return result;
56     }
57 
58     return OK;
59 }
60 
startDisplayService()61 static status_t startDisplayService() {
62     using android::frameworks::displayservice::V1_0::implementation::DisplayService;
63     using android::frameworks::displayservice::V1_0::IDisplayService;
64 
65     sp<IDisplayService> displayservice = new DisplayService();
66     status_t err = displayservice->registerAsService();
67 
68     if (err != OK) {
69         ALOGE("Could not register IDisplayService service.");
70     }
71 
72     return err;
73 }
74 
main(int,char **)75 int main(int, char**) {
76     signal(SIGPIPE, SIG_IGN);
77 
78     hardware::configureRpcThreadpool(1 /* maxThreads */,
79             false /* callerWillJoin */);
80 
81     startGraphicsAllocatorService();
82 
83     // When SF is launched in its own process, limit the number of
84     // binder threads to 4.
85     ProcessState::self()->setThreadPoolMaxThreadCount(4);
86 
87     // start the thread pool
88     sp<ProcessState> ps(ProcessState::self());
89     ps->startThreadPool();
90 
91     // instantiate surfaceflinger
92     sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();
93 
94     setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
95 
96     set_sched_policy(0, SP_FOREGROUND);
97 
98     // Put most SurfaceFlinger threads in the system-background cpuset
99     // Keeps us from unnecessarily using big cores
100     // Do this after the binder thread pool init
101     if (cpusets_enabled()) set_cpuset_policy(0, SP_SYSTEM);
102 
103     // initialize before clients can connect
104     flinger->init();
105 
106     // publish surface flinger
107     sp<IServiceManager> sm(defaultServiceManager());
108     sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
109                    IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
110 
111     startDisplayService(); // dependency on SF getting registered above
112 
113     struct sched_param param = {0};
114     param.sched_priority = 2;
115     if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
116         ALOGE("Couldn't set SCHED_FIFO");
117     }
118 
119     // run surface flinger in this thread
120     flinger->run();
121 
122     return 0;
123 }
124