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
35using namespace android;
36
37static status_t startGraphicsAllocatorService() {
38    using android::hardware::configstore::getBool;
39    using android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
40    if (!getBool<ISurfaceFlingerConfigs,
41            &ISurfaceFlingerConfigs::startGraphicsAllocatorService>(false)) {
42        return OK;
43    }
44
45    using android::hardware::graphics::allocator::V2_0::IAllocator;
46
47    status_t result =
48        hardware::registerPassthroughServiceImplementation<IAllocator>();
49    if (result != OK) {
50        ALOGE("could not start graphics allocator service");
51        return result;
52    }
53
54    return OK;
55}
56
57static status_t startDisplayService() {
58    using android::frameworks::displayservice::V1_0::implementation::DisplayService;
59    using android::frameworks::displayservice::V1_0::IDisplayService;
60
61    sp<IDisplayService> displayservice = new DisplayService();
62    status_t err = displayservice->registerAsService();
63
64    if (err != OK) {
65        ALOGE("Could not register IDisplayService service.");
66    }
67
68    return err;
69}
70
71int main(int, char**) {
72    signal(SIGPIPE, SIG_IGN);
73
74    hardware::configureRpcThreadpool(1 /* maxThreads */,
75            false /* callerWillJoin */);
76
77    startGraphicsAllocatorService();
78
79    // When SF is launched in its own process, limit the number of
80    // binder threads to 4.
81    ProcessState::self()->setThreadPoolMaxThreadCount(4);
82
83    // start the thread pool
84    sp<ProcessState> ps(ProcessState::self());
85    ps->startThreadPool();
86
87    // instantiate surfaceflinger
88    sp<SurfaceFlinger> flinger = new SurfaceFlinger();
89
90    setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
91
92    set_sched_policy(0, SP_FOREGROUND);
93
94    // Put most SurfaceFlinger threads in the system-background cpuset
95    // Keeps us from unnecessarily using big cores
96    // Do this after the binder thread pool init
97    if (cpusets_enabled()) set_cpuset_policy(0, SP_SYSTEM);
98
99    // initialize before clients can connect
100    flinger->init();
101
102    // publish surface flinger
103    sp<IServiceManager> sm(defaultServiceManager());
104    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
105                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
106
107    // publish GpuService
108    sp<GpuService> gpuservice = new GpuService();
109    sm->addService(String16(GpuService::SERVICE_NAME), gpuservice, false);
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