Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #include "hardware_composer.h" |
| 2 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 3 | #include <cutils/properties.h> |
| 4 | #include <cutils/sched_policy.h> |
| 5 | #include <fcntl.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 6 | #include <log/log.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 7 | #include <poll.h> |
| 8 | #include <sync/sync.h> |
| 9 | #include <sys/eventfd.h> |
| 10 | #include <sys/prctl.h> |
| 11 | #include <sys/resource.h> |
| 12 | #include <sys/system_properties.h> |
| 13 | #include <sys/timerfd.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 14 | #include <time.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #include <utils/Trace.h> |
| 17 | |
| 18 | #include <algorithm> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 19 | #include <chrono> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 22 | #include <sstream> |
| 23 | #include <string> |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 24 | #include <tuple> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 25 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 26 | #include <dvr/dvr_display_types.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 27 | #include <dvr/performance_client_api.h> |
| 28 | #include <private/dvr/clock_ns.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 29 | #include <private/dvr/ion_buffer.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 30 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 31 | using android::hardware::Return; |
| 32 | using android::hardware::Void; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 33 | using android::pdx::LocalHandle; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 34 | using android::pdx::rpc::EmptyVariant; |
| 35 | using android::pdx::rpc::IfAnyOf; |
| 36 | |
| 37 | using namespace std::chrono_literals; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace dvr { |
| 41 | |
| 42 | namespace { |
| 43 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 44 | const char kBacklightBrightnessSysFile[] = |
| 45 | "/sys/class/leds/lcd-backlight/brightness"; |
| 46 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 47 | const char kDvrPerformanceProperty[] = "sys.dvr.performance"; |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 48 | const char kDvrStandaloneProperty[] = "ro.boot.vr"; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 49 | |
Luke Song | 4b78832 | 2017-03-24 14:17:31 -0700 | [diff] [blame] | 50 | const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns"; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 51 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 52 | // Get time offset from a vsync to when the pose for that vsync should be |
| 53 | // predicted out to. For example, if scanout gets halfway through the frame |
| 54 | // at the halfway point between vsyncs, then this could be half the period. |
| 55 | // With global shutter displays, this should be changed to the offset to when |
| 56 | // illumination begins. Low persistence adds a frame of latency, so we predict |
| 57 | // to the center of the next frame. |
| 58 | inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) { |
| 59 | return (vsync_period_ns * 150) / 100; |
| 60 | } |
| 61 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 62 | // Attempts to set the scheduler class and partiton for the current thread. |
| 63 | // Returns true on success or false on failure. |
| 64 | bool SetThreadPolicy(const std::string& scheduler_class, |
| 65 | const std::string& partition) { |
| 66 | int error = dvrSetSchedulerClass(0, scheduler_class.c_str()); |
| 67 | if (error < 0) { |
| 68 | ALOGE( |
| 69 | "SetThreadPolicy: Failed to set scheduler class \"%s\" for " |
| 70 | "thread_id=%d: %s", |
| 71 | scheduler_class.c_str(), gettid(), strerror(-error)); |
| 72 | return false; |
| 73 | } |
| 74 | error = dvrSetCpuPartition(0, partition.c_str()); |
| 75 | if (error < 0) { |
| 76 | ALOGE( |
| 77 | "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: " |
| 78 | "%s", |
| 79 | partition.c_str(), gettid(), strerror(-error)); |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 85 | } // anonymous namespace |
| 86 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 87 | HardwareComposer::HardwareComposer() |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 88 | : initialized_(false), request_display_callback_(nullptr) {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 89 | |
| 90 | HardwareComposer::~HardwareComposer(void) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 91 | UpdatePostThreadState(PostThreadState::Quit, true); |
| 92 | if (post_thread_.joinable()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 93 | post_thread_.join(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 96 | bool HardwareComposer::Initialize( |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 97 | Hwc2::Composer* composer, RequestDisplayCallback request_display_callback) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 98 | if (initialized_) { |
| 99 | ALOGE("HardwareComposer::Initialize: already initialized."); |
| 100 | return false; |
| 101 | } |
| 102 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 103 | is_standalone_device_ = property_get_bool(kDvrStandaloneProperty, false); |
| 104 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 105 | request_display_callback_ = request_display_callback; |
| 106 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 107 | HWC::Error error = HWC::Error::None; |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 108 | |
| 109 | Hwc2::Config config; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 110 | error = composer->getActiveConfig(HWC_DISPLAY_PRIMARY, &config); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 111 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 112 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 113 | ALOGE("HardwareComposer: Failed to get current display config : %d", |
| 114 | config); |
| 115 | return false; |
| 116 | } |
| 117 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 118 | error = GetDisplayMetrics(composer, HWC_DISPLAY_PRIMARY, config, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 119 | &native_display_metrics_); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 120 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 121 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 122 | ALOGE( |
| 123 | "HardwareComposer: Failed to get display attributes for current " |
| 124 | "configuration : %d", |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 125 | error.value); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | ALOGI( |
| 130 | "HardwareComposer: primary display attributes: width=%d height=%d " |
| 131 | "vsync_period_ns=%d DPI=%dx%d", |
| 132 | native_display_metrics_.width, native_display_metrics_.height, |
| 133 | native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x, |
| 134 | native_display_metrics_.dpi.y); |
| 135 | |
| 136 | // Set the display metrics but never use rotation to avoid the long latency of |
| 137 | // rotation processing in hwc. |
| 138 | display_transform_ = HWC_TRANSFORM_NONE; |
| 139 | display_metrics_ = native_display_metrics_; |
| 140 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 141 | // Setup the display metrics used by all Layer instances. |
| 142 | Layer::SetDisplayMetrics(native_display_metrics_); |
| 143 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 144 | post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 145 | LOG_ALWAYS_FATAL_IF( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 146 | !post_thread_event_fd_, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 147 | "HardwareComposer: Failed to create interrupt event fd : %s", |
| 148 | strerror(errno)); |
| 149 | |
| 150 | post_thread_ = std::thread(&HardwareComposer::PostThread, this); |
| 151 | |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 152 | initialized_ = true; |
| 153 | |
| 154 | return initialized_; |
| 155 | } |
| 156 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 157 | void HardwareComposer::Enable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 158 | UpdatePostThreadState(PostThreadState::Suspended, false); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void HardwareComposer::Disable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 162 | UpdatePostThreadState(PostThreadState::Suspended, true); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 163 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 164 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 165 | // Update the post thread quiescent state based on idle and suspended inputs. |
| 166 | void HardwareComposer::UpdatePostThreadState(PostThreadStateType state, |
| 167 | bool suspend) { |
| 168 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 169 | |
| 170 | // Update the votes in the state variable before evaluating the effective |
| 171 | // quiescent state. Any bits set in post_thread_state_ indicate that the post |
| 172 | // thread should be suspended. |
| 173 | if (suspend) { |
| 174 | post_thread_state_ |= state; |
| 175 | } else { |
| 176 | post_thread_state_ &= ~state; |
| 177 | } |
| 178 | |
| 179 | const bool quit = post_thread_state_ & PostThreadState::Quit; |
| 180 | const bool effective_suspend = post_thread_state_ != PostThreadState::Active; |
| 181 | if (quit) { |
| 182 | post_thread_quiescent_ = true; |
| 183 | eventfd_write(post_thread_event_fd_.Get(), 1); |
| 184 | post_thread_wait_.notify_one(); |
| 185 | } else if (effective_suspend && !post_thread_quiescent_) { |
| 186 | post_thread_quiescent_ = true; |
| 187 | eventfd_write(post_thread_event_fd_.Get(), 1); |
| 188 | } else if (!effective_suspend && post_thread_quiescent_) { |
| 189 | post_thread_quiescent_ = false; |
| 190 | eventfd_t value; |
| 191 | eventfd_read(post_thread_event_fd_.Get(), &value); |
| 192 | post_thread_wait_.notify_one(); |
| 193 | } |
| 194 | |
| 195 | // Wait until the post thread is in the requested state. |
| 196 | post_thread_ready_.wait(lock, [this, effective_suspend] { |
| 197 | return effective_suspend != post_thread_resumed_; |
| 198 | }); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 199 | } |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 200 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 201 | void HardwareComposer::OnPostThreadResumed() { |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 202 | // Phones create a new composer client on resume and destroy it on pause. |
| 203 | // Standalones only create the composer client once and then use SetPowerMode |
| 204 | // to control the screen on pause/resume. |
| 205 | if (!is_standalone_device_ || !composer_) { |
| 206 | composer_.reset(new Hwc2::Composer("default")); |
| 207 | composer_callback_ = new ComposerCallback; |
| 208 | composer_->registerCallback(composer_callback_); |
| 209 | Layer::SetComposer(composer_.get()); |
| 210 | } else { |
| 211 | SetPowerMode(true); |
| 212 | } |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 213 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 214 | EnableVsync(true); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 215 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 216 | // TODO(skiazyk): We need to do something about accessing this directly, |
| 217 | // supposedly there is a backlight service on the way. |
| 218 | // TODO(steventhomas): When we change the backlight setting, will surface |
| 219 | // flinger (or something else) set it back to its original value once we give |
| 220 | // control of the display back to surface flinger? |
| 221 | SetBacklightBrightness(255); |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 222 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 223 | // Trigger target-specific performance mode change. |
| 224 | property_set(kDvrPerformanceProperty, "performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 227 | void HardwareComposer::OnPostThreadPaused() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 228 | retire_fence_fds_.clear(); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 229 | layers_.clear(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 230 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 231 | if (composer_) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 232 | EnableVsync(false); |
| 233 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 234 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 235 | if (!is_standalone_device_) { |
| 236 | composer_callback_ = nullptr; |
| 237 | composer_.reset(nullptr); |
| 238 | Layer::SetComposer(nullptr); |
| 239 | } else { |
| 240 | SetPowerMode(false); |
| 241 | } |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 242 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 243 | // Trigger target-specific performance mode change. |
| 244 | property_set(kDvrPerformanceProperty, "idle"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 247 | HWC::Error HardwareComposer::Validate(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 248 | uint32_t num_types; |
| 249 | uint32_t num_requests; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 250 | HWC::Error error = |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 251 | composer_->validateDisplay(display, &num_types, &num_requests); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 252 | |
| 253 | if (error == HWC2_ERROR_HAS_CHANGES) { |
| 254 | // TODO(skiazyk): We might need to inspect the requested changes first, but |
| 255 | // so far it seems like we shouldn't ever hit a bad state. |
| 256 | // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_, |
| 257 | // display); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 258 | error = composer_->acceptDisplayChanges(display); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return error; |
| 262 | } |
| 263 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 264 | HWC::Error HardwareComposer::EnableVsync(bool enabled) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 265 | return composer_->setVsyncEnabled( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 266 | HWC_DISPLAY_PRIMARY, |
| 267 | (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE |
| 268 | : HWC2_VSYNC_DISABLE)); |
| 269 | } |
| 270 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 271 | HWC::Error HardwareComposer::SetPowerMode(bool active) { |
| 272 | HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off; |
| 273 | return composer_->setPowerMode( |
| 274 | HWC_DISPLAY_PRIMARY, power_mode.cast<Hwc2::IComposerClient::PowerMode>()); |
| 275 | } |
| 276 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 277 | HWC::Error HardwareComposer::Present(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 278 | int32_t present_fence; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 279 | HWC::Error error = composer_->presentDisplay(display, &present_fence); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 280 | |
| 281 | // According to the documentation, this fence is signaled at the time of |
| 282 | // vsync/DMA for physical displays. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 283 | if (error == HWC::Error::None) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 284 | ATRACE_INT("HardwareComposer: VsyncFence", present_fence); |
| 285 | retire_fence_fds_.emplace_back(present_fence); |
| 286 | } else { |
| 287 | ATRACE_INT("HardwareComposer: PresentResult", error); |
| 288 | } |
| 289 | |
| 290 | return error; |
| 291 | } |
| 292 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 293 | HWC::Error HardwareComposer::GetDisplayAttribute(Hwc2::Composer* composer, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 294 | hwc2_display_t display, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 295 | hwc2_config_t config, |
| 296 | hwc2_attribute_t attribute, |
| 297 | int32_t* out_value) const { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 298 | return composer->getDisplayAttribute( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 299 | display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value); |
| 300 | } |
| 301 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 302 | HWC::Error HardwareComposer::GetDisplayMetrics( |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 303 | Hwc2::Composer* composer, hwc2_display_t display, hwc2_config_t config, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 304 | HWCDisplayMetrics* out_metrics) const { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 305 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 306 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 307 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_WIDTH, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 308 | &out_metrics->width); |
| 309 | if (error != HWC::Error::None) { |
| 310 | ALOGE( |
| 311 | "HardwareComposer::GetDisplayMetrics: Failed to get display width: %s", |
| 312 | error.to_string().c_str()); |
| 313 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 316 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_HEIGHT, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 317 | &out_metrics->height); |
| 318 | if (error != HWC::Error::None) { |
| 319 | ALOGE( |
| 320 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 321 | error.to_string().c_str()); |
| 322 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 325 | error = GetDisplayAttribute(composer, display, config, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 326 | HWC2_ATTRIBUTE_VSYNC_PERIOD, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 327 | &out_metrics->vsync_period_ns); |
| 328 | if (error != HWC::Error::None) { |
| 329 | ALOGE( |
| 330 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 331 | error.to_string().c_str()); |
| 332 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 335 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_X, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 336 | &out_metrics->dpi.x); |
| 337 | if (error != HWC::Error::None) { |
| 338 | ALOGE( |
| 339 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI X: %s", |
| 340 | error.to_string().c_str()); |
| 341 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 344 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_Y, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 345 | &out_metrics->dpi.y); |
| 346 | if (error != HWC::Error::None) { |
| 347 | ALOGE( |
| 348 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI Y: %s", |
| 349 | error.to_string().c_str()); |
| 350 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 353 | return HWC::Error::None; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 356 | std::string HardwareComposer::Dump() { |
| 357 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 358 | std::ostringstream stream; |
| 359 | |
| 360 | stream << "Display metrics: " << display_metrics_.width << "x" |
| 361 | << display_metrics_.height << " " << (display_metrics_.dpi.x / 1000.0) |
| 362 | << "x" << (display_metrics_.dpi.y / 1000.0) << " dpi @ " |
| 363 | << (1000000000.0 / display_metrics_.vsync_period_ns) << " Hz" |
| 364 | << std::endl; |
| 365 | |
| 366 | stream << "Post thread resumed: " << post_thread_resumed_ << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 367 | stream << "Active layers: " << layers_.size() << std::endl; |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 368 | stream << std::endl; |
| 369 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 370 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 371 | stream << "Layer " << i << ":"; |
| 372 | stream << " type=" << layers_[i].GetCompositionType().to_string(); |
| 373 | stream << " surface_id=" << layers_[i].GetSurfaceId(); |
| 374 | stream << " buffer_id=" << layers_[i].GetBufferId(); |
| 375 | stream << std::endl; |
| 376 | } |
| 377 | stream << std::endl; |
| 378 | |
| 379 | if (post_thread_resumed_) { |
| 380 | stream << "Hardware Composer Debug Info:" << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 381 | stream << composer_->dumpDebugInfo(); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | return stream.str(); |
| 385 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 386 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 387 | void HardwareComposer::PostLayers() { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 388 | ATRACE_NAME("HardwareComposer::PostLayers"); |
| 389 | |
| 390 | // Setup the hardware composer layers with current buffers. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 391 | for (auto& layer : layers_) { |
| 392 | layer.Prepare(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 395 | HWC::Error error = Validate(HWC_DISPLAY_PRIMARY); |
| 396 | if (error != HWC::Error::None) { |
| 397 | ALOGE("HardwareComposer::PostLayers: Validate failed: %s", |
| 398 | error.to_string().c_str()); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 402 | // Now that we have taken in a frame from the application, we have a chance |
| 403 | // to drop the frame before passing the frame along to HWC. |
| 404 | // If the display driver has become backed up, we detect it here and then |
| 405 | // react by skipping this frame to catch up latency. |
| 406 | while (!retire_fence_fds_.empty() && |
| 407 | (!retire_fence_fds_.front() || |
| 408 | sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) { |
| 409 | // There are only 2 fences in here, no performance problem to shift the |
| 410 | // array of ints. |
| 411 | retire_fence_fds_.erase(retire_fence_fds_.begin()); |
| 412 | } |
| 413 | |
| 414 | const bool is_frame_pending = IsFramePendingInDriver(); |
George Burgess IV | 353a6f6 | 2017-06-26 17:13:09 -0700 | [diff] [blame] | 415 | const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) > |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 416 | post_thread_config_.allowed_pending_fence_count; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 417 | |
| 418 | if (is_fence_pending || is_frame_pending) { |
| 419 | ATRACE_INT("frame_skip_count", ++frame_skip_count_); |
| 420 | |
| 421 | ALOGW_IF(is_frame_pending, "Warning: frame already queued, dropping frame"); |
| 422 | ALOGW_IF(is_fence_pending, |
| 423 | "Warning: dropping a frame to catch up with HWC (pending = %zd)", |
| 424 | retire_fence_fds_.size()); |
| 425 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 426 | for (auto& layer : layers_) { |
| 427 | layer.Drop(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 428 | } |
| 429 | return; |
| 430 | } else { |
| 431 | // Make the transition more obvious in systrace when the frame skip happens |
| 432 | // above. |
| 433 | ATRACE_INT("frame_skip_count", 0); |
| 434 | } |
| 435 | |
Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 436 | #if TRACE > 1 |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 437 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 438 | ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s", |
| 439 | i, layers_[i].GetBufferId(), |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 440 | layers_[i].GetCompositionType().to_string().c_str()); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 441 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 442 | #endif |
| 443 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 444 | error = Present(HWC_DISPLAY_PRIMARY); |
| 445 | if (error != HWC::Error::None) { |
| 446 | ALOGE("HardwareComposer::PostLayers: Present failed: %s", |
| 447 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
| 451 | std::vector<Hwc2::Layer> out_layers; |
| 452 | std::vector<int> out_fences; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 453 | error = composer_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers, |
| 454 | &out_fences); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 455 | ALOGE_IF(error != HWC::Error::None, |
| 456 | "HardwareComposer::PostLayers: Failed to get release fences: %s", |
| 457 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 458 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 459 | // Perform post-frame bookkeeping. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 460 | uint32_t num_elements = out_layers.size(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 461 | for (size_t i = 0; i < num_elements; ++i) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 462 | for (auto& layer : layers_) { |
| 463 | if (layer.GetLayerHandle() == out_layers[i]) { |
| 464 | layer.Finish(out_fences[i]); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 470 | void HardwareComposer::SetDisplaySurfaces( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 471 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) { |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 472 | ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd", |
| 473 | surfaces.size()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 474 | const bool display_idle = surfaces.size() == 0; |
| 475 | { |
| 476 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 477 | pending_surfaces_ = std::move(surfaces); |
| 478 | } |
| 479 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 480 | if (request_display_callback_ && (!is_standalone_device_ || !composer_)) |
Steven Thomas | 2ddf567 | 2017-06-15 11:38:40 -0700 | [diff] [blame] | 481 | request_display_callback_(!display_idle); |
| 482 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 483 | // Set idle state based on whether there are any surfaces to handle. |
| 484 | UpdatePostThreadState(PostThreadState::Idle, display_idle); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 485 | } |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 486 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 487 | int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key, |
| 488 | IonBuffer& ion_buffer) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 489 | if (key == DvrGlobalBuffers::kVsyncBuffer) { |
| 490 | vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>( |
| 491 | &ion_buffer, CPUUsageMode::WRITE_OFTEN); |
| 492 | |
| 493 | if (vsync_ring_->IsMapped() == false) { |
| 494 | return -EPERM; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 499 | return MapConfigBuffer(ion_buffer); |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 506 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 507 | ConfigBufferDeleted(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) { |
| 512 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 513 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 514 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 515 | if (ion_buffer.width() < DvrConfigRing::MemorySize()) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 516 | ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size."); |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
| 520 | void* buffer_base = 0; |
| 521 | int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(), |
| 522 | ion_buffer.height(), &buffer_base); |
| 523 | if (result != 0) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 524 | ALOGE( |
| 525 | "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config " |
| 526 | "buffer."); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 527 | return -EPERM; |
| 528 | } |
| 529 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 530 | shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width()); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 531 | ion_buffer.Unlock(); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | void HardwareComposer::ConfigBufferDeleted() { |
| 537 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 538 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | void HardwareComposer::UpdateConfigBuffer() { |
| 542 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
| 543 | if (!shared_config_ring_.is_valid()) |
| 544 | return; |
| 545 | // Copy from latest record in shared_config_ring_ to local copy. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 546 | DvrConfig record; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 547 | if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) { |
| 548 | post_thread_config_ = record; |
| 549 | } |
| 550 | } |
| 551 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 552 | int HardwareComposer::PostThreadPollInterruptible( |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 553 | const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 554 | pollfd pfd[2] = { |
| 555 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 556 | .fd = event_fd.Get(), |
Steven Thomas | 66747c1 | 2017-03-22 18:45:31 -0700 | [diff] [blame] | 557 | .events = static_cast<short>(requested_events), |
| 558 | .revents = 0, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 559 | }, |
| 560 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 561 | .fd = post_thread_event_fd_.Get(), |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 562 | .events = POLLPRI | POLLIN, |
| 563 | .revents = 0, |
| 564 | }, |
| 565 | }; |
| 566 | int ret, error; |
| 567 | do { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 568 | ret = poll(pfd, 2, timeout_ms); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 569 | error = errno; |
| 570 | ALOGW_IF(ret < 0, |
| 571 | "HardwareComposer::PostThreadPollInterruptible: Error during " |
| 572 | "poll(): %s (%d)", |
| 573 | strerror(error), error); |
| 574 | } while (ret < 0 && error == EINTR); |
| 575 | |
| 576 | if (ret < 0) { |
| 577 | return -error; |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 578 | } else if (ret == 0) { |
| 579 | return -ETIMEDOUT; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 580 | } else if (pfd[0].revents != 0) { |
| 581 | return 0; |
| 582 | } else if (pfd[1].revents != 0) { |
| 583 | ALOGI("VrHwcPost thread interrupted"); |
| 584 | return kPostThreadInterrupted; |
| 585 | } else { |
| 586 | return 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 587 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 590 | // Waits for the next vsync and returns the timestamp of the vsync event. If |
| 591 | // vsync already passed since the last call, returns the latest vsync timestamp |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 592 | // instead of blocking. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 593 | int HardwareComposer::WaitForVSync(int64_t* timestamp) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 594 | int error = PostThreadPollInterruptible(composer_callback_->GetVsyncEventFd(), |
| 595 | POLLIN, /*timeout_ms*/ 1000); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 596 | if (error == kPostThreadInterrupted || error < 0) { |
| 597 | return error; |
| 598 | } else { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 599 | *timestamp = composer_callback_->GetVsyncTime(); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 600 | return 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) { |
| 605 | const int timer_fd = vsync_sleep_timer_fd_.Get(); |
| 606 | const itimerspec wakeup_itimerspec = { |
| 607 | .it_interval = {.tv_sec = 0, .tv_nsec = 0}, |
| 608 | .it_value = NsToTimespec(wakeup_timestamp), |
| 609 | }; |
| 610 | int ret = |
| 611 | timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr); |
| 612 | int error = errno; |
| 613 | if (ret < 0) { |
| 614 | ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s", |
| 615 | strerror(error)); |
| 616 | return -error; |
| 617 | } |
| 618 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 619 | return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN, |
| 620 | /*timeout_ms*/ -1); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void HardwareComposer::PostThread() { |
| 624 | // NOLINTNEXTLINE(runtime/int) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 625 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 626 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 627 | // Set the scheduler to SCHED_FIFO with high priority. If this fails here |
| 628 | // there may have been a startup timing issue between this thread and |
| 629 | // performanced. Try again later when this thread becomes active. |
| 630 | bool thread_policy_setup = |
| 631 | SetThreadPolicy("graphics:high", "/system/performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 632 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 633 | #if ENABLE_BACKLIGHT_BRIGHTNESS |
| 634 | // TODO(hendrikw): This isn't required at the moment. It's possible that there |
| 635 | // is another method to access this when needed. |
| 636 | // Open the backlight brightness control sysfs node. |
| 637 | backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR); |
| 638 | ALOGW_IF(!backlight_brightness_fd_, |
| 639 | "HardwareComposer: Failed to open backlight brightness control: %s", |
| 640 | strerror(errno)); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 641 | #endif // ENABLE_BACKLIGHT_BRIGHTNESS |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 642 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 643 | // Create a timerfd based on CLOCK_MONOTINIC. |
| 644 | vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0)); |
| 645 | LOG_ALWAYS_FATAL_IF( |
| 646 | !vsync_sleep_timer_fd_, |
| 647 | "HardwareComposer: Failed to create vsync sleep timerfd: %s", |
| 648 | strerror(errno)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 649 | |
| 650 | const int64_t ns_per_frame = display_metrics_.vsync_period_ns; |
| 651 | const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame); |
| 652 | |
| 653 | // TODO(jbates) Query vblank time from device, when such an API is available. |
| 654 | // This value (6.3%) was measured on A00 in low persistence mode. |
| 655 | int64_t vblank_ns = ns_per_frame * 63 / 1000; |
| 656 | int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2; |
| 657 | |
| 658 | // Check property for overriding right eye offset value. |
| 659 | right_eye_photon_offset_ns = |
| 660 | property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns); |
| 661 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 662 | bool was_running = false; |
| 663 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 664 | while (1) { |
| 665 | ATRACE_NAME("HardwareComposer::PostThread"); |
| 666 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 667 | // Check for updated config once per vsync. |
| 668 | UpdateConfigBuffer(); |
| 669 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 670 | while (post_thread_quiescent_) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 671 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 672 | ALOGI("HardwareComposer::PostThread: Entering quiescent state."); |
| 673 | |
Corey Tabaka | df0b916 | 2017-08-03 17:14:08 -0700 | [diff] [blame] | 674 | // Tear down resources if necessary. |
| 675 | if (was_running) |
| 676 | OnPostThreadPaused(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 677 | |
| 678 | was_running = false; |
| 679 | post_thread_resumed_ = false; |
| 680 | post_thread_ready_.notify_all(); |
| 681 | |
| 682 | if (post_thread_state_ & PostThreadState::Quit) { |
| 683 | ALOGI("HardwareComposer::PostThread: Quitting."); |
| 684 | return; |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 685 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 686 | |
| 687 | post_thread_wait_.wait(lock, [this] { return !post_thread_quiescent_; }); |
| 688 | |
| 689 | post_thread_resumed_ = true; |
| 690 | post_thread_ready_.notify_all(); |
| 691 | |
| 692 | ALOGI("HardwareComposer::PostThread: Exiting quiescent state."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | if (!was_running) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 696 | // Setup resources. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 697 | OnPostThreadResumed(); |
| 698 | was_running = true; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 699 | |
| 700 | // Try to setup the scheduler policy if it failed during startup. Only |
| 701 | // attempt to do this on transitions from inactive to active to avoid |
| 702 | // spamming the system with RPCs and log messages. |
| 703 | if (!thread_policy_setup) { |
| 704 | thread_policy_setup = |
| 705 | SetThreadPolicy("graphics:high", "/system/performance"); |
| 706 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | int64_t vsync_timestamp = 0; |
| 710 | { |
| 711 | std::array<char, 128> buf; |
| 712 | snprintf(buf.data(), buf.size(), "wait_vsync|vsync=%d|", |
| 713 | vsync_count_ + 1); |
| 714 | ATRACE_NAME(buf.data()); |
| 715 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 716 | const int error = WaitForVSync(&vsync_timestamp); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 717 | ALOGE_IF( |
| 718 | error < 0, |
| 719 | "HardwareComposer::PostThread: Failed to wait for vsync event: %s", |
| 720 | strerror(-error)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 721 | // Don't bother processing this frame if a pause was requested |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 722 | if (error == kPostThreadInterrupted) |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 723 | continue; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | ++vsync_count_; |
| 727 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 728 | const bool layer_config_changed = UpdateLayerConfig(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 729 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 730 | // Publish the vsync event. |
| 731 | if (vsync_ring_) { |
| 732 | DvrVsync vsync; |
| 733 | vsync.vsync_count = vsync_count_; |
| 734 | vsync.vsync_timestamp_ns = vsync_timestamp; |
| 735 | vsync.vsync_left_eye_offset_ns = photon_offset_ns; |
| 736 | vsync.vsync_right_eye_offset_ns = right_eye_photon_offset_ns; |
| 737 | vsync.vsync_period_ns = ns_per_frame; |
| 738 | |
| 739 | vsync_ring_->Publish(vsync); |
| 740 | } |
| 741 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 742 | // Signal all of the vsync clients. Because absolute time is used for the |
| 743 | // wakeup time below, this can take a little time if necessary. |
| 744 | if (vsync_callback_) |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 745 | vsync_callback_(HWC_DISPLAY_PRIMARY, vsync_timestamp, |
| 746 | /*frame_time_estimate*/ 0, vsync_count_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 747 | |
| 748 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 749 | // Sleep until shortly before vsync. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 750 | ATRACE_NAME("sleep"); |
| 751 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 752 | const int64_t display_time_est_ns = vsync_timestamp + ns_per_frame; |
| 753 | const int64_t now_ns = GetSystemClockNs(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 754 | const int64_t sleep_time_ns = display_time_est_ns - now_ns - |
| 755 | post_thread_config_.frame_post_offset_ns; |
| 756 | const int64_t wakeup_time_ns = |
| 757 | display_time_est_ns - post_thread_config_.frame_post_offset_ns; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 758 | |
| 759 | ATRACE_INT64("sleep_time_ns", sleep_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 760 | if (sleep_time_ns > 0) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 761 | int error = SleepUntil(wakeup_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 762 | ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s", |
| 763 | strerror(-error)); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 764 | if (error == kPostThreadInterrupted) { |
| 765 | if (layer_config_changed) { |
| 766 | // If the layer config changed we need to validateDisplay() even if |
| 767 | // we're going to drop the frame, to flush the Composer object's |
| 768 | // internal command buffer and apply our layer changes. |
| 769 | Validate(HWC_DISPLAY_PRIMARY); |
| 770 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 771 | continue; |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 772 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 776 | PostLayers(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 777 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 780 | // Checks for changes in the surface stack and updates the layer config to |
| 781 | // accomodate the new stack. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 782 | bool HardwareComposer::UpdateLayerConfig() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 783 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 784 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 785 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 786 | if (pending_surfaces_.empty()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 787 | return false; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 788 | |
| 789 | surfaces = std::move(pending_surfaces_); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 790 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 791 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 792 | ATRACE_NAME("UpdateLayerConfig_HwLayers"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 793 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 794 | // Sort the new direct surface list by z-order to determine the relative order |
| 795 | // of the surfaces. This relative order is used for the HWC z-order value to |
| 796 | // insulate VrFlinger and HWC z-order semantics from each other. |
| 797 | std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) { |
| 798 | return a->z_order() < b->z_order(); |
| 799 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 800 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 801 | // Prepare a new layer stack, pulling in layers from the previous |
| 802 | // layer stack that are still active and updating their attributes. |
| 803 | std::vector<Layer> layers; |
| 804 | size_t layer_index = 0; |
| 805 | for (const auto& surface : surfaces) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 806 | // The bottom layer is opaque, other layers blend. |
| 807 | HWC::BlendMode blending = |
| 808 | layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 809 | |
| 810 | // Try to find a layer for this surface in the set of active layers. |
| 811 | auto search = |
| 812 | std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id()); |
| 813 | const bool found = search != layers_.end() && |
| 814 | search->GetSurfaceId() == surface->surface_id(); |
| 815 | if (found) { |
| 816 | // Update the attributes of the layer that may have changed. |
| 817 | search->SetBlending(blending); |
| 818 | search->SetZOrder(layer_index); // Relative z-order. |
| 819 | |
| 820 | // Move the existing layer to the new layer set and remove the empty layer |
| 821 | // object from the current set. |
| 822 | layers.push_back(std::move(*search)); |
| 823 | layers_.erase(search); |
| 824 | } else { |
| 825 | // Insert a layer for the new surface. |
| 826 | layers.emplace_back(surface, blending, display_transform_, |
| 827 | HWC::Composition::Device, layer_index); |
| 828 | } |
| 829 | |
| 830 | ALOGI_IF( |
| 831 | TRACE, |
| 832 | "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d", |
| 833 | layer_index, layers[layer_index].GetSurfaceId()); |
| 834 | |
| 835 | layer_index++; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 838 | // Sort the new layer stack by ascending surface id. |
| 839 | std::sort(layers.begin(), layers.end()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 840 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 841 | // Replace the previous layer set with the new layer set. The destructor of |
| 842 | // the previous set will clean up the remaining Layers that are not moved to |
| 843 | // the new layer set. |
| 844 | layers_ = std::move(layers); |
| 845 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 846 | ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers", |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 847 | layers_.size()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 848 | return true; |
| 849 | } |
| 850 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 851 | void HardwareComposer::SetVSyncCallback(VSyncCallback callback) { |
| 852 | vsync_callback_ = callback; |
| 853 | } |
| 854 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 855 | void HardwareComposer::SetBacklightBrightness(int brightness) { |
| 856 | if (backlight_brightness_fd_) { |
| 857 | std::array<char, 32> text; |
| 858 | const int length = snprintf(text.data(), text.size(), "%d", brightness); |
| 859 | write(backlight_brightness_fd_.Get(), text.data(), length); |
| 860 | } |
| 861 | } |
| 862 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 863 | HardwareComposer::ComposerCallback::ComposerCallback() { |
| 864 | vsync_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 865 | LOG_ALWAYS_FATAL_IF(!vsync_event_fd_, "Failed to create vsync event fd : %s", |
| 866 | strerror(errno)); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | Return<void> HardwareComposer::ComposerCallback::onHotplug( |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 870 | Hwc2::Display /*display*/, IComposerCallback::Connection /*conn*/) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 871 | return Void(); |
| 872 | } |
| 873 | |
| 874 | Return<void> HardwareComposer::ComposerCallback::onRefresh( |
| 875 | Hwc2::Display /*display*/) { |
| 876 | return hardware::Void(); |
| 877 | } |
| 878 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 879 | Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display, |
| 880 | int64_t timestamp) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 881 | if (display == HWC_DISPLAY_PRIMARY) { |
| 882 | std::lock_guard<std::mutex> lock(vsync_mutex_); |
| 883 | vsync_time_ = timestamp; |
| 884 | int error = eventfd_write(vsync_event_fd_.Get(), 1); |
| 885 | LOG_ALWAYS_FATAL_IF(error != 0, "Failed writing to vsync event fd"); |
| 886 | } |
| 887 | return Void(); |
| 888 | } |
| 889 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame^] | 890 | const pdx::LocalHandle& HardwareComposer::ComposerCallback::GetVsyncEventFd() |
| 891 | const { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 892 | return vsync_event_fd_; |
| 893 | } |
| 894 | |
| 895 | int64_t HardwareComposer::ComposerCallback::GetVsyncTime() { |
| 896 | std::lock_guard<std::mutex> lock(vsync_mutex_); |
| 897 | eventfd_t event; |
| 898 | eventfd_read(vsync_event_fd_.Get(), &event); |
| 899 | LOG_ALWAYS_FATAL_IF(vsync_time_ < 0, |
| 900 | "Attempt to read vsync time before vsync event"); |
| 901 | int64_t return_val = vsync_time_; |
| 902 | vsync_time_ = -1; |
| 903 | return return_val; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 906 | Hwc2::Composer* Layer::composer_{nullptr}; |
| 907 | HWCDisplayMetrics Layer::display_metrics_{0, 0, {0, 0}, 0}; |
| 908 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 909 | void Layer::Reset() { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 910 | if (hardware_composer_layer_) { |
| 911 | composer_->destroyLayer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 912 | hardware_composer_layer_ = 0; |
| 913 | } |
| 914 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 915 | z_order_ = 0; |
| 916 | blending_ = HWC::BlendMode::None; |
| 917 | transform_ = HWC::Transform::None; |
| 918 | composition_type_ = HWC::Composition::Invalid; |
| 919 | target_composition_type_ = composition_type_; |
| 920 | source_ = EmptyVariant{}; |
| 921 | acquire_fence_.Close(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 922 | surface_rect_functions_applied_ = false; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 923 | pending_visibility_settings_ = true; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 926 | Layer::Layer(const std::shared_ptr<DirectDisplaySurface>& surface, |
| 927 | HWC::BlendMode blending, HWC::Transform transform, |
| 928 | HWC::Composition composition_type, size_t z_order) |
| 929 | : z_order_{z_order}, |
| 930 | blending_{blending}, |
| 931 | transform_{transform}, |
| 932 | target_composition_type_{composition_type}, |
| 933 | source_{SourceSurface{surface}} { |
| 934 | CommonLayerSetup(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 937 | Layer::Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending, |
| 938 | HWC::Transform transform, HWC::Composition composition_type, |
| 939 | size_t z_order) |
| 940 | : z_order_{z_order}, |
| 941 | blending_{blending}, |
| 942 | transform_{transform}, |
| 943 | target_composition_type_{composition_type}, |
| 944 | source_{SourceBuffer{buffer}} { |
| 945 | CommonLayerSetup(); |
| 946 | } |
| 947 | |
| 948 | Layer::~Layer() { Reset(); } |
| 949 | |
| 950 | Layer::Layer(Layer&& other) { *this = std::move(other); } |
| 951 | |
| 952 | Layer& Layer::operator=(Layer&& other) { |
| 953 | if (this != &other) { |
| 954 | Reset(); |
| 955 | using std::swap; |
| 956 | swap(hardware_composer_layer_, other.hardware_composer_layer_); |
| 957 | swap(z_order_, other.z_order_); |
| 958 | swap(blending_, other.blending_); |
| 959 | swap(transform_, other.transform_); |
| 960 | swap(composition_type_, other.composition_type_); |
| 961 | swap(target_composition_type_, other.target_composition_type_); |
| 962 | swap(source_, other.source_); |
| 963 | swap(acquire_fence_, other.acquire_fence_); |
| 964 | swap(surface_rect_functions_applied_, |
| 965 | other.surface_rect_functions_applied_); |
| 966 | swap(pending_visibility_settings_, other.pending_visibility_settings_); |
| 967 | } |
| 968 | return *this; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 971 | void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) { |
| 972 | if (source_.is<SourceBuffer>()) |
| 973 | std::get<SourceBuffer>(source_) = {buffer}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 976 | void Layer::SetBlending(HWC::BlendMode blending) { |
| 977 | if (blending_ != blending) { |
| 978 | blending_ = blending; |
| 979 | pending_visibility_settings_ = true; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | void Layer::SetZOrder(size_t z_order) { |
| 984 | if (z_order_ != z_order) { |
| 985 | z_order_ = z_order; |
| 986 | pending_visibility_settings_ = true; |
| 987 | } |
| 988 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 989 | |
| 990 | IonBuffer* Layer::GetBuffer() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 991 | struct Visitor { |
| 992 | IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); } |
| 993 | IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); } |
| 994 | IonBuffer* operator()(EmptyVariant) { return nullptr; } |
| 995 | }; |
| 996 | return source_.Visit(Visitor{}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 999 | void Layer::UpdateVisibilitySettings() { |
| 1000 | if (pending_visibility_settings_) { |
| 1001 | pending_visibility_settings_ = false; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1002 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1003 | HWC::Error error; |
| 1004 | hwc2_display_t display = HWC_DISPLAY_PRIMARY; |
| 1005 | |
| 1006 | error = composer_->setLayerBlendMode( |
| 1007 | display, hardware_composer_layer_, |
| 1008 | blending_.cast<Hwc2::IComposerClient::BlendMode>()); |
| 1009 | ALOGE_IF(error != HWC::Error::None, |
| 1010 | "Layer::UpdateLayerSettings: Error setting layer blend mode: %s", |
| 1011 | error.to_string().c_str()); |
| 1012 | |
| 1013 | error = |
| 1014 | composer_->setLayerZOrder(display, hardware_composer_layer_, z_order_); |
| 1015 | ALOGE_IF(error != HWC::Error::None, |
| 1016 | "Layer::UpdateLayerSettings: Error setting z_ order: %s", |
| 1017 | error.to_string().c_str()); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | void Layer::UpdateLayerSettings() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1022 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1023 | hwc2_display_t display = HWC_DISPLAY_PRIMARY; |
| 1024 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1025 | UpdateVisibilitySettings(); |
| 1026 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1027 | // TODO(eieio): Use surface attributes or some other mechanism to control |
| 1028 | // the layer display frame. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1029 | error = composer_->setLayerDisplayFrame( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1030 | display, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1031 | {0, 0, display_metrics_.width, display_metrics_.height}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1032 | ALOGE_IF(error != HWC::Error::None, |
| 1033 | "Layer::UpdateLayerSettings: Error setting layer display frame: %s", |
| 1034 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1035 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1036 | error = composer_->setLayerVisibleRegion( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1037 | display, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1038 | {{0, 0, display_metrics_.width, display_metrics_.height}}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1039 | ALOGE_IF(error != HWC::Error::None, |
| 1040 | "Layer::UpdateLayerSettings: Error setting layer visible region: %s", |
| 1041 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1042 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1043 | error = |
| 1044 | composer_->setLayerPlaneAlpha(display, hardware_composer_layer_, 1.0f); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1045 | ALOGE_IF(error != HWC::Error::None, |
| 1046 | "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s", |
| 1047 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1050 | void Layer::CommonLayerSetup() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1051 | HWC::Error error = |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1052 | composer_->createLayer(HWC_DISPLAY_PRIMARY, &hardware_composer_layer_); |
| 1053 | ALOGE_IF(error != HWC::Error::None, |
| 1054 | "Layer::CommonLayerSetup: Failed to create layer on primary " |
| 1055 | "display: %s", |
| 1056 | error.to_string().c_str()); |
| 1057 | UpdateLayerSettings(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void Layer::Prepare() { |
| 1061 | int right, bottom; |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 1062 | sp<GraphicBuffer> handle; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1063 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1064 | // Acquire the next buffer according to the type of source. |
| 1065 | IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) { |
| 1066 | std::tie(right, bottom, handle, acquire_fence_) = source.Acquire(); |
| 1067 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1068 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1069 | // Update any visibility (blending, z-order) changes that occurred since |
| 1070 | // last prepare. |
| 1071 | UpdateVisibilitySettings(); |
| 1072 | |
| 1073 | // When a layer is first setup there may be some time before the first |
| 1074 | // buffer arrives. Setup the HWC layer as a solid color to stall for time |
| 1075 | // until the first buffer arrives. Once the first buffer arrives there will |
| 1076 | // always be a buffer for the frame even if it is old. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1077 | if (!handle.get()) { |
| 1078 | if (composition_type_ == HWC::Composition::Invalid) { |
| 1079 | composition_type_ = HWC::Composition::SolidColor; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1080 | composer_->setLayerCompositionType( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1081 | HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1082 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1083 | Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1084 | composer_->setLayerColor(HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1085 | layer_color); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1086 | } else { |
| 1087 | // The composition type is already set. Nothing else to do until a |
| 1088 | // buffer arrives. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1089 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1090 | } else { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1091 | if (composition_type_ != target_composition_type_) { |
| 1092 | composition_type_ = target_composition_type_; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1093 | composer_->setLayerCompositionType( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1094 | HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1095 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1096 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1097 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1098 | HWC::Error error{HWC::Error::None}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1099 | error = |
| 1100 | composer_->setLayerBuffer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1101 | 0, handle, acquire_fence_.Get()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1102 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1103 | ALOGE_IF(error != HWC::Error::None, |
| 1104 | "Layer::Prepare: Error setting layer buffer: %s", |
| 1105 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1106 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1107 | if (!surface_rect_functions_applied_) { |
| 1108 | const float float_right = right; |
| 1109 | const float float_bottom = bottom; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1110 | error = composer_->setLayerSourceCrop(HWC_DISPLAY_PRIMARY, |
| 1111 | hardware_composer_layer_, |
| 1112 | {0, 0, float_right, float_bottom}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1113 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1114 | ALOGE_IF(error != HWC::Error::None, |
| 1115 | "Layer::Prepare: Error setting layer source crop: %s", |
| 1116 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1117 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1118 | surface_rect_functions_applied_ = true; |
| 1119 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | void Layer::Finish(int release_fence_fd) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1124 | IfAnyOf<SourceSurface, SourceBuffer>::Call( |
| 1125 | &source_, [release_fence_fd](auto& source) { |
| 1126 | source.Finish(LocalHandle(release_fence_fd)); |
| 1127 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1130 | void Layer::Drop() { acquire_fence_.Close(); } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1131 | |
| 1132 | } // namespace dvr |
| 1133 | } // namespace android |