| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 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 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 12 | #include <binder/IServiceManager.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 13 | #include <binder/ProcessState.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 14 | #include <cutils/properties.h> | 
|  | 15 | #include <cutils/sched_policy.h> | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 16 | #include <log/log.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 17 | #include <private/dvr/display_client.h> | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 18 | #include <sys/prctl.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 19 | #include <sys/resource.h> | 
|  | 20 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 21 | #include <functional> | 
|  | 22 |  | 
|  | 23 | #include "DisplayHardware/ComposerHal.h" | 
|  | 24 | #include "display_manager_service.h" | 
|  | 25 | #include "display_service.h" | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 26 | #include "vsync_service.h" | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 | namespace dvr { | 
|  | 30 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 31 | std::unique_ptr<VrFlinger> VrFlinger::Create( | 
|  | 32 | Hwc2::Composer* hidl, RequestDisplayCallback request_display_callback) { | 
|  | 33 | std::unique_ptr<VrFlinger> vr_flinger(new VrFlinger); | 
|  | 34 | if (vr_flinger->Init(hidl, request_display_callback)) | 
|  | 35 | return vr_flinger; | 
|  | 36 | else | 
|  | 37 | return nullptr; | 
|  | 38 | } | 
|  | 39 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 40 | VrFlinger::VrFlinger() {} | 
|  | 41 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 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 |  | 
|  | 58 | bool VrFlinger::Init(Hwc2::Composer* hidl, | 
|  | 59 | RequestDisplayCallback request_display_callback) { | 
|  | 60 | if (!hidl || !request_display_callback) | 
|  | 61 | return false; | 
| Hendrik Wagenaar | ae2d58b | 2017-02-06 08:48:24 -0800 | [diff] [blame] | 62 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 63 | std::shared_ptr<android::pdx::Service> service; | 
|  | 64 |  | 
|  | 65 | ALOGI("Starting up VrFlinger..."); | 
|  | 66 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 67 | // We need to be able to create endpoints with full perms. | 
|  | 68 | umask(0000); | 
|  | 69 |  | 
|  | 70 | android::ProcessState::self()->startThreadPool(); | 
|  | 71 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 72 | request_display_callback_ = request_display_callback; | 
|  | 73 |  | 
| Alex Vakulenko | 5a244ed | 2017-06-09 16:29:04 -0700 | [diff] [blame] | 74 | dispatcher_ = android::pdx::ServiceDispatcher::Create(); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 75 | CHECK_ERROR(!dispatcher_, error, "Failed to create service dispatcher."); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 76 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 77 | display_service_ = | 
|  | 78 | android::dvr::DisplayService::Create(hidl, request_display_callback); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 79 | CHECK_ERROR(!display_service_, error, "Failed to create display service."); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 80 | dispatcher_->AddService(display_service_); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 81 |  | 
|  | 82 | service = android::dvr::DisplayManagerService::Create(display_service_); | 
|  | 83 | CHECK_ERROR(!service, error, "Failed to create display manager service."); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 84 | dispatcher_->AddService(service); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 85 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 86 | service = android::dvr::VSyncService::Create(); | 
|  | 87 | CHECK_ERROR(!service, error, "Failed to create vsync service."); | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 88 | dispatcher_->AddService(service); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | display_service_->SetVSyncCallback( | 
|  | 91 | std::bind(&android::dvr::VSyncService::VSyncEvent, | 
|  | 92 | std::static_pointer_cast<android::dvr::VSyncService>(service), | 
|  | 93 | std::placeholders::_1, std::placeholders::_2, | 
|  | 94 | std::placeholders::_3, std::placeholders::_4)); | 
|  | 95 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 96 | dispatcher_thread_ = std::thread([this]() { | 
|  | 97 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrDispatch"), 0, 0, 0); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 98 | ALOGI("Entering message loop."); | 
|  | 99 |  | 
| Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 100 | setpriority(PRIO_PROCESS, 0, android::PRIORITY_URGENT_DISPLAY); | 
|  | 101 | set_sched_policy(0, SP_FOREGROUND); | 
|  | 102 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 103 | int ret = dispatcher_->EnterDispatchLoop(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 104 | if (ret < 0) { | 
|  | 105 | ALOGE("Dispatch loop exited because: %s\n", strerror(-ret)); | 
|  | 106 | } | 
|  | 107 | }); | 
|  | 108 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 109 | return true; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 110 |  | 
|  | 111 | error: | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 112 | return false; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 115 | void VrFlinger::OnBootFinished() { | 
|  | 116 | sp<IVrManager> vr_manager = interface_cast<IVrManager>( | 
|  | 117 | defaultServiceManager()->checkService(String16("vrmanager"))); | 
|  | 118 | if (vr_manager.get()) { | 
|  | 119 | persistent_vr_state_callback_ = | 
|  | 120 | new PersistentVrStateCallback(request_display_callback_); | 
|  | 121 | vr_manager->registerPersistentVrStateListener( | 
|  | 122 | persistent_vr_state_callback_); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 123 | } else { | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 124 | ALOGE("Unable to register vr flinger for persistent vr mode changes"); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 128 | void VrFlinger::GrantDisplayOwnership() { | 
|  | 129 | display_service_->GrantDisplayOwnership(); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | void VrFlinger::SeizeDisplayOwnership() { | 
|  | 133 | display_service_->SeizeDisplayOwnership(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Karthik Ravi Shankar | 171155a | 2017-06-28 15:40:24 -0700 | [diff] [blame] | 136 | std::string VrFlinger::Dump() { | 
|  | 137 | // TODO(karthikrs): Add more state information here. | 
|  | 138 | return display_service_->DumpState(0/*unused*/); | 
|  | 139 | } | 
|  | 140 |  | 
| Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 141 | void VrFlinger::PersistentVrStateCallback::onPersistentVrStateChanged( | 
|  | 142 | bool enabled) { | 
|  | 143 | ALOGV("Notified persistent vr mode is %s", enabled ? "on" : "off"); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 144 | // TODO(eieio): Determine the correct signal to request display control. | 
|  | 145 | // Persistent VR mode is not enough. | 
|  | 146 | // request_display_callback_(enabled); | 
| Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 147 | } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 148 | }  // namespace dvr | 
|  | 149 | }  // namespace android |