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 <cutils/sched_policy.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/IPCThreadState.h>
27 #include <binder/ProcessState.h>
28 #include <binder/IServiceManager.h>
29 #include <displayservice/DisplayService.h>
30 #include <hidl/LegacySupport.h>
31 #include <configstore/Utils.h>
32 #include "GpuService.h"
33 #include "SurfaceFlinger.h"
34
35 using namespace android;
36
startGraphicsAllocatorService()37 static status_t startGraphicsAllocatorService() {
38 using android::hardware::graphics::allocator::V2_0::IAllocator;
39
40 status_t result =
41 hardware::registerPassthroughServiceImplementation<IAllocator>();
42 if (result != OK) {
43 ALOGE("could not start graphics allocator service");
44 return result;
45 }
46
47 return OK;
48 }
49
startHidlServices()50 static status_t startHidlServices() {
51 using android::frameworks::displayservice::V1_0::implementation::DisplayService;
52 using android::frameworks::displayservice::V1_0::IDisplayService;
53 using android::hardware::configstore::getBool;
54 using android::hardware::configstore::getBool;
55 using android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
56 hardware::configureRpcThreadpool(1 /* maxThreads */,
57 false /* callerWillJoin */);
58
59 status_t err;
60
61 if (getBool<ISurfaceFlingerConfigs,
62 &ISurfaceFlingerConfigs::startGraphicsAllocatorService>(false)) {
63 err = startGraphicsAllocatorService();
64 if (err != OK) {
65 return err;
66 }
67 }
68
69 sp<IDisplayService> displayservice = new DisplayService();
70 err = displayservice->registerAsService();
71
72 if (err != OK) {
73 ALOGE("Could not register IDisplayService service.");
74 }
75
76 return err;
77 }
78
main(int,char **)79 int main(int, char**) {
80 startHidlServices();
81
82 signal(SIGPIPE, SIG_IGN);
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 = new SurfaceFlinger();
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
110 // publish GpuService
111 sp<GpuService> gpuservice = new GpuService();
112 sm->addService(String16(GpuService::SERVICE_NAME), gpuservice, false);
113
114 struct sched_param param = {0};
115 param.sched_priority = 2;
116 if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) {
117 ALOGE("Couldn't set SCHED_FIFO");
118 }
119
120 // run surface flinger in this thread
121 flinger->run();
122
123 return 0;
124 }
125