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 | |
| 67 | setpriority(PRIO_PROCESS, 0, android::PRIORITY_URGENT_DISPLAY); |
| 68 | set_sched_policy(0, SP_FOREGROUND); |
| 69 | |
| 70 | // We need to be able to create endpoints with full perms. |
| 71 | umask(0000); |
| 72 | |
| 73 | android::ProcessState::self()->startThreadPool(); |
| 74 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 75 | request_display_callback_ = request_display_callback; |
| 76 | |
Alex Vakulenko | 5a244ed | 2017-06-09 16:29:04 -0700 | [diff] [blame^] | 77 | dispatcher_ = android::pdx::ServiceDispatcher::Create(); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 78 | CHECK_ERROR(!dispatcher_, error, "Failed to create service dispatcher."); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 79 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 80 | display_service_ = |
| 81 | android::dvr::DisplayService::Create(hidl, request_display_callback); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 82 | CHECK_ERROR(!display_service_, error, "Failed to create display service."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 83 | dispatcher_->AddService(display_service_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 84 | |
| 85 | service = android::dvr::DisplayManagerService::Create(display_service_); |
| 86 | CHECK_ERROR(!service, error, "Failed to create display manager service."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 87 | dispatcher_->AddService(service); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 88 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 89 | service = android::dvr::VSyncService::Create(); |
| 90 | CHECK_ERROR(!service, error, "Failed to create vsync service."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 91 | dispatcher_->AddService(service); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 92 | |
| 93 | display_service_->SetVSyncCallback( |
| 94 | std::bind(&android::dvr::VSyncService::VSyncEvent, |
| 95 | std::static_pointer_cast<android::dvr::VSyncService>(service), |
| 96 | std::placeholders::_1, std::placeholders::_2, |
| 97 | std::placeholders::_3, std::placeholders::_4)); |
| 98 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 99 | dispatcher_thread_ = std::thread([this]() { |
| 100 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrDispatch"), 0, 0, 0); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 101 | ALOGI("Entering message loop."); |
| 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 | |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 136 | void VrFlinger::OnHardwareComposerRefresh() { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 137 | display_service_->OnHardwareComposerRefresh(); |
| 138 | } |
| 139 | |
| 140 | void VrFlinger::PersistentVrStateCallback::onPersistentVrStateChanged( |
| 141 | bool enabled) { |
| 142 | ALOGV("Notified persistent vr mode is %s", enabled ? "on" : "off"); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 143 | // TODO(eieio): Determine the correct signal to request display control. |
| 144 | // Persistent VR mode is not enough. |
| 145 | // request_display_callback_(enabled); |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 148 | } // namespace dvr |
| 149 | } // namespace android |