1 #include <dvr/vr_flinger.h>
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <poll.h>
6 #include <signal.h>
7 #include <string.h>
8 #include <time.h>
9 #include <unistd.h>
10 #include <memory>
11
12 #include <binder/IServiceManager.h>
13 #include <binder/ProcessState.h>
14 #include <cutils/properties.h>
15 #include <log/log.h>
16 #include <private/dvr/display_client.h>
17 #include <processgroup/sched_policy.h>
18 #include <sys/prctl.h>
19 #include <sys/resource.h>
20
21 #include <functional>
22
23 #include "DisplayHardware/ComposerHal.h"
24 #include "display_manager_service.h"
25 #include "display_service.h"
26
27 namespace android {
28 namespace dvr {
29
Create(Hwc2::Composer * hidl,hwc2_display_t primary_display_id,RequestDisplayCallback request_display_callback)30 std::unique_ptr<VrFlinger> VrFlinger::Create(
31 Hwc2::Composer* hidl, hwc2_display_t primary_display_id,
32 RequestDisplayCallback request_display_callback) {
33 std::unique_ptr<VrFlinger> vr_flinger(new VrFlinger);
34 if (vr_flinger->Init(hidl, primary_display_id, request_display_callback))
35 return vr_flinger;
36 else
37 return nullptr;
38 }
39
VrFlinger()40 VrFlinger::VrFlinger() {}
41
~VrFlinger()42 VrFlinger::~VrFlinger() {
43 if (persistent_vr_state_callback_.get()) {
44 sp<IVrManager> vr_manager = interface_cast<IVrManager>(
45 defaultServiceManager()->checkService(String16("vrmanager")));
46 if (vr_manager.get()) {
47 vr_manager->unregisterPersistentVrStateListener(
48 persistent_vr_state_callback_);
49 }
50 }
51
52 if (dispatcher_)
53 dispatcher_->SetCanceled(true);
54 if (dispatcher_thread_.joinable())
55 dispatcher_thread_.join();
56 }
57
Init(Hwc2::Composer * hidl,hwc2_display_t primary_display_id,RequestDisplayCallback request_display_callback)58 bool VrFlinger::Init(Hwc2::Composer* hidl,
59 hwc2_display_t primary_display_id,
60 RequestDisplayCallback request_display_callback) {
61 if (!hidl || !request_display_callback)
62 return false;
63
64 std::shared_ptr<android::pdx::Service> service;
65
66 ALOGI("Starting up VrFlinger...");
67
68 // We need to be able to create endpoints with full perms.
69 umask(0000);
70
71 android::ProcessState::self()->startThreadPool();
72
73 request_display_callback_ = request_display_callback;
74
75 dispatcher_ = android::pdx::ServiceDispatcher::Create();
76 CHECK_ERROR(!dispatcher_, error, "Failed to create service dispatcher.");
77
78 display_service_ = android::dvr::DisplayService::Create(
79 hidl, primary_display_id, request_display_callback);
80 CHECK_ERROR(!display_service_, error, "Failed to create display service.");
81 dispatcher_->AddService(display_service_);
82
83 service = android::dvr::DisplayManagerService::Create(display_service_);
84 CHECK_ERROR(!service, error, "Failed to create display manager service.");
85 dispatcher_->AddService(service);
86
87 dispatcher_thread_ = std::thread([this]() {
88 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrDispatch"), 0, 0, 0);
89 ALOGI("Entering message loop.");
90
91 setpriority(PRIO_PROCESS, 0, android::PRIORITY_URGENT_DISPLAY);
92 set_sched_policy(0, SP_FOREGROUND);
93
94 int ret = dispatcher_->EnterDispatchLoop();
95 if (ret < 0) {
96 ALOGE("Dispatch loop exited because: %s\n", strerror(-ret));
97 }
98 });
99
100 return true;
101
102 error:
103 return false;
104 }
105
OnBootFinished()106 void VrFlinger::OnBootFinished() {
107 display_service_->OnBootFinished();
108 sp<IVrManager> vr_manager = interface_cast<IVrManager>(
109 defaultServiceManager()->checkService(String16("vrmanager")));
110 if (vr_manager.get()) {
111 persistent_vr_state_callback_ =
112 new PersistentVrStateCallback(request_display_callback_);
113 vr_manager->registerPersistentVrStateListener(
114 persistent_vr_state_callback_);
115 } else {
116 ALOGE("Unable to register vr flinger for persistent vr mode changes");
117 }
118 }
119
GrantDisplayOwnership()120 void VrFlinger::GrantDisplayOwnership() {
121 display_service_->GrantDisplayOwnership();
122 }
123
SeizeDisplayOwnership()124 void VrFlinger::SeizeDisplayOwnership() {
125 display_service_->SeizeDisplayOwnership();
126 }
127
Dump()128 std::string VrFlinger::Dump() {
129 // TODO(karthikrs): Add more state information here.
130 return display_service_->DumpState(0/*unused*/);
131 }
132
onPersistentVrStateChanged(bool enabled)133 void VrFlinger::PersistentVrStateCallback::onPersistentVrStateChanged(
134 bool enabled) {
135 ALOGV("Notified persistent vr mode is %s", enabled ? "on" : "off");
136 // TODO(eieio): Determine the correct signal to request display control.
137 // Persistent VR mode is not enough.
138 // request_display_callback_(enabled);
139 }
140 } // namespace dvr
141 } // namespace android
142