1 #include "hardware_composer.h"
2
3 #include <binder/IServiceManager.h>
4 #include <cutils/properties.h>
5 #include <cutils/sched_policy.h>
6 #include <fcntl.h>
7 #include <log/log.h>
8 #include <poll.h>
9 #include <stdint.h>
10 #include <sync/sync.h>
11 #include <sys/eventfd.h>
12 #include <sys/prctl.h>
13 #include <sys/resource.h>
14 #include <sys/system_properties.h>
15 #include <sys/timerfd.h>
16 #include <sys/types.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include <utils/Trace.h>
20
21 #include <algorithm>
22 #include <chrono>
23 #include <functional>
24 #include <map>
25 #include <sstream>
26 #include <string>
27 #include <tuple>
28
29 #include <dvr/dvr_display_types.h>
30 #include <dvr/performance_client_api.h>
31 #include <private/dvr/clock_ns.h>
32 #include <private/dvr/ion_buffer.h>
33
34 using android::hardware::Return;
35 using android::hardware::Void;
36 using android::pdx::ErrorStatus;
37 using android::pdx::LocalHandle;
38 using android::pdx::Status;
39 using android::pdx::rpc::EmptyVariant;
40 using android::pdx::rpc::IfAnyOf;
41
42 using namespace std::chrono_literals;
43
44 namespace android {
45 namespace dvr {
46
47 namespace {
48
49 const char kDvrPerformanceProperty[] = "sys.dvr.performance";
50
51 const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns";
52
53 // Surface flinger uses "VSYNC-sf" and "VSYNC-app" for its version of these
54 // events. Name ours similarly.
55 const char kVsyncTraceEventName[] = "VSYNC-vrflinger";
56
57 // How long to wait after boot finishes before we turn the display off.
58 constexpr int kBootFinishedDisplayOffTimeoutSec = 10;
59
60 constexpr int kDefaultDisplayWidth = 1920;
61 constexpr int kDefaultDisplayHeight = 1080;
62 constexpr int64_t kDefaultVsyncPeriodNs = 16666667;
63 // Hardware composer reports dpi as dots per thousand inches (dpi * 1000).
64 constexpr int kDefaultDpi = 400000;
65
66 // Get time offset from a vsync to when the pose for that vsync should be
67 // predicted out to. For example, if scanout gets halfway through the frame
68 // at the halfway point between vsyncs, then this could be half the period.
69 // With global shutter displays, this should be changed to the offset to when
70 // illumination begins. Low persistence adds a frame of latency, so we predict
71 // to the center of the next frame.
GetPosePredictionTimeOffset(int64_t vsync_period_ns)72 inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
73 return (vsync_period_ns * 150) / 100;
74 }
75
76 // Attempts to set the scheduler class and partiton for the current thread.
77 // Returns true on success or false on failure.
SetThreadPolicy(const std::string & scheduler_class,const std::string & partition)78 bool SetThreadPolicy(const std::string& scheduler_class,
79 const std::string& partition) {
80 int error = dvrSetSchedulerClass(0, scheduler_class.c_str());
81 if (error < 0) {
82 ALOGE(
83 "SetThreadPolicy: Failed to set scheduler class \"%s\" for "
84 "thread_id=%d: %s",
85 scheduler_class.c_str(), gettid(), strerror(-error));
86 return false;
87 }
88 error = dvrSetCpuPartition(0, partition.c_str());
89 if (error < 0) {
90 ALOGE(
91 "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: "
92 "%s",
93 partition.c_str(), gettid(), strerror(-error));
94 return false;
95 }
96 return true;
97 }
98
99 // Utility to generate scoped tracers with arguments.
100 // TODO(eieio): Move/merge this into utils/Trace.h?
101 class TraceArgs {
102 public:
103 template <typename... Args>
TraceArgs(const char * format,Args &&...args)104 explicit TraceArgs(const char* format, Args&&... args) {
105 std::array<char, 1024> buffer;
106 snprintf(buffer.data(), buffer.size(), format, std::forward<Args>(args)...);
107 atrace_begin(ATRACE_TAG, buffer.data());
108 }
109
~TraceArgs()110 ~TraceArgs() { atrace_end(ATRACE_TAG); }
111
112 private:
113 TraceArgs(const TraceArgs&) = delete;
114 void operator=(const TraceArgs&) = delete;
115 };
116
117 // Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
118 // defined in utils/Trace.h.
119 #define TRACE_FORMAT(format, ...) \
120 TraceArgs PASTE(__tracer, __LINE__) { format, ##__VA_ARGS__ }
121
122 // Returns "primary" or "external". Useful for writing more readable logs.
GetDisplayName(bool is_primary)123 const char* GetDisplayName(bool is_primary) {
124 return is_primary ? "primary" : "external";
125 }
126
127 } // anonymous namespace
128
HardwareComposer()129 HardwareComposer::HardwareComposer()
130 : initialized_(false), request_display_callback_(nullptr) {}
131
~HardwareComposer(void)132 HardwareComposer::~HardwareComposer(void) {
133 UpdatePostThreadState(PostThreadState::Quit, true);
134 if (post_thread_.joinable())
135 post_thread_.join();
136 composer_callback_->SetVsyncService(nullptr);
137 }
138
UpdateEdidData(Hwc2::Composer * composer,hwc2_display_t hw_id)139 void HardwareComposer::UpdateEdidData(Hwc2::Composer* composer,
140 hwc2_display_t hw_id) {
141 const auto error = composer->getDisplayIdentificationData(
142 hw_id, &display_port_, &display_identification_data_);
143 if (error != android::hardware::graphics::composer::V2_1::Error::NONE) {
144 if (error !=
145 android::hardware::graphics::composer::V2_1::Error::UNSUPPORTED) {
146 ALOGI("hardware_composer: identification data error\n");
147 } else {
148 ALOGI("hardware_composer: identification data unsupported\n");
149 }
150 }
151 }
152
Initialize(Hwc2::Composer * composer,hwc2_display_t primary_display_id,RequestDisplayCallback request_display_callback)153 bool HardwareComposer::Initialize(
154 Hwc2::Composer* composer, hwc2_display_t primary_display_id,
155 RequestDisplayCallback request_display_callback) {
156 if (initialized_) {
157 ALOGE("HardwareComposer::Initialize: already initialized.");
158 return false;
159 }
160
161 request_display_callback_ = request_display_callback;
162
163 primary_display_ = GetDisplayParams(composer, primary_display_id, true);
164
165 vsync_service_ = new VsyncService;
166 sp<IServiceManager> sm(defaultServiceManager());
167 auto result = sm->addService(String16(VsyncService::GetServiceName()),
168 vsync_service_, false);
169 LOG_ALWAYS_FATAL_IF(result != android::OK,
170 "addService(%s) failed", VsyncService::GetServiceName());
171
172 post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
173 LOG_ALWAYS_FATAL_IF(
174 !post_thread_event_fd_,
175 "HardwareComposer: Failed to create interrupt event fd : %s",
176 strerror(errno));
177
178 UpdateEdidData(composer, primary_display_id);
179
180 post_thread_ = std::thread(&HardwareComposer::PostThread, this);
181
182 initialized_ = true;
183
184 return initialized_;
185 }
186
Enable()187 void HardwareComposer::Enable() {
188 UpdatePostThreadState(PostThreadState::Suspended, false);
189 }
190
Disable()191 void HardwareComposer::Disable() {
192 UpdatePostThreadState(PostThreadState::Suspended, true);
193
194 std::unique_lock<std::mutex> lock(post_thread_mutex_);
195 post_thread_ready_.wait(lock, [this] {
196 return !post_thread_resumed_;
197 });
198 }
199
OnBootFinished()200 void HardwareComposer::OnBootFinished() {
201 std::lock_guard<std::mutex> lock(post_thread_mutex_);
202 if (boot_finished_)
203 return;
204 boot_finished_ = true;
205 post_thread_wait_.notify_one();
206 }
207
208 // Update the post thread quiescent state based on idle and suspended inputs.
UpdatePostThreadState(PostThreadStateType state,bool suspend)209 void HardwareComposer::UpdatePostThreadState(PostThreadStateType state,
210 bool suspend) {
211 std::unique_lock<std::mutex> lock(post_thread_mutex_);
212
213 // Update the votes in the state variable before evaluating the effective
214 // quiescent state. Any bits set in post_thread_state_ indicate that the post
215 // thread should be suspended.
216 if (suspend) {
217 post_thread_state_ |= state;
218 } else {
219 post_thread_state_ &= ~state;
220 }
221
222 const bool quit = post_thread_state_ & PostThreadState::Quit;
223 const bool effective_suspend = post_thread_state_ != PostThreadState::Active;
224 if (quit) {
225 post_thread_quiescent_ = true;
226 eventfd_write(post_thread_event_fd_.Get(), 1);
227 post_thread_wait_.notify_one();
228 } else if (effective_suspend && !post_thread_quiescent_) {
229 post_thread_quiescent_ = true;
230 eventfd_write(post_thread_event_fd_.Get(), 1);
231 } else if (!effective_suspend && post_thread_quiescent_) {
232 post_thread_quiescent_ = false;
233 eventfd_t value;
234 eventfd_read(post_thread_event_fd_.Get(), &value);
235 post_thread_wait_.notify_one();
236 }
237 }
238
CreateComposer()239 void HardwareComposer::CreateComposer() {
240 if (composer_)
241 return;
242 composer_.reset(new Hwc2::impl::Composer("default"));
243 composer_callback_ = new ComposerCallback;
244 composer_->registerCallback(composer_callback_);
245 LOG_ALWAYS_FATAL_IF(!composer_callback_->GotFirstHotplug(),
246 "Registered composer callback but didn't get hotplug for primary"
247 " display");
248 composer_callback_->SetVsyncService(vsync_service_);
249 }
250
OnPostThreadResumed()251 void HardwareComposer::OnPostThreadResumed() {
252 ALOGI("OnPostThreadResumed");
253 EnableDisplay(*target_display_, true);
254
255 // Trigger target-specific performance mode change.
256 property_set(kDvrPerformanceProperty, "performance");
257 }
258
OnPostThreadPaused()259 void HardwareComposer::OnPostThreadPaused() {
260 ALOGI("OnPostThreadPaused");
261 retire_fence_fds_.clear();
262 layers_.clear();
263
264 // Phones create a new composer client on resume and destroy it on pause.
265 if (composer_callback_ != nullptr) {
266 composer_callback_->SetVsyncService(nullptr);
267 composer_callback_ = nullptr;
268 }
269 composer_.reset(nullptr);
270
271 // Trigger target-specific performance mode change.
272 property_set(kDvrPerformanceProperty, "idle");
273 }
274
PostThreadCondWait(std::unique_lock<std::mutex> & lock,int timeout_sec,const std::function<bool ()> & pred)275 bool HardwareComposer::PostThreadCondWait(std::unique_lock<std::mutex>& lock,
276 int timeout_sec,
277 const std::function<bool()>& pred) {
278 auto pred_with_quit = [&] {
279 return pred() || (post_thread_state_ & PostThreadState::Quit);
280 };
281 if (timeout_sec >= 0) {
282 post_thread_wait_.wait_for(lock, std::chrono::seconds(timeout_sec),
283 pred_with_quit);
284 } else {
285 post_thread_wait_.wait(lock, pred_with_quit);
286 }
287 if (post_thread_state_ & PostThreadState::Quit) {
288 ALOGI("HardwareComposer::PostThread: Quitting.");
289 return true;
290 }
291 return false;
292 }
293
Validate(hwc2_display_t display)294 HWC::Error HardwareComposer::Validate(hwc2_display_t display) {
295 uint32_t num_types;
296 uint32_t num_requests;
297 HWC::Error error =
298 composer_->validateDisplay(display, &num_types, &num_requests);
299
300 if (error == HWC2_ERROR_HAS_CHANGES) {
301 ALOGE("Hardware composer has requested composition changes, "
302 "which we don't support.");
303 // Accept the changes anyway and see if we can get something on the screen.
304 error = composer_->acceptDisplayChanges(display);
305 }
306
307 return error;
308 }
309
EnableVsync(const DisplayParams & display,bool enabled)310 bool HardwareComposer::EnableVsync(const DisplayParams& display, bool enabled) {
311 HWC::Error error = composer_->setVsyncEnabled(display.id,
312 (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
313 : HWC2_VSYNC_DISABLE));
314 if (error != HWC::Error::None) {
315 ALOGE("Error attempting to %s vsync on %s display: %s",
316 enabled ? "enable" : "disable", GetDisplayName(display.is_primary),
317 error.to_string().c_str());
318 }
319 return error == HWC::Error::None;
320 }
321
SetPowerMode(const DisplayParams & display,bool active)322 bool HardwareComposer::SetPowerMode(const DisplayParams& display, bool active) {
323 ALOGI("Turning %s display %s", GetDisplayName(display.is_primary),
324 active ? "on" : "off");
325 HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off;
326 HWC::Error error = composer_->setPowerMode(display.id,
327 power_mode.cast<Hwc2::IComposerClient::PowerMode>());
328 if (error != HWC::Error::None) {
329 ALOGE("Error attempting to turn %s display %s: %s",
330 GetDisplayName(display.is_primary), active ? "on" : "off",
331 error.to_string().c_str());
332 }
333 return error == HWC::Error::None;
334 }
335
EnableDisplay(const DisplayParams & display,bool enabled)336 bool HardwareComposer::EnableDisplay(const DisplayParams& display,
337 bool enabled) {
338 bool power_result;
339 bool vsync_result;
340 // When turning a display on, we set the power state then set vsync. When
341 // turning a display off we do it in the opposite order.
342 if (enabled) {
343 power_result = SetPowerMode(display, enabled);
344 vsync_result = EnableVsync(display, enabled);
345 } else {
346 vsync_result = EnableVsync(display, enabled);
347 power_result = SetPowerMode(display, enabled);
348 }
349 return power_result && vsync_result;
350 }
351
Present(hwc2_display_t display)352 HWC::Error HardwareComposer::Present(hwc2_display_t display) {
353 int32_t present_fence;
354 HWC::Error error = composer_->presentDisplay(display, &present_fence);
355
356 // According to the documentation, this fence is signaled at the time of
357 // vsync/DMA for physical displays.
358 if (error == HWC::Error::None) {
359 retire_fence_fds_.emplace_back(present_fence);
360 } else {
361 ATRACE_INT("HardwareComposer: PresentResult", error);
362 }
363
364 return error;
365 }
366
GetDisplayParams(Hwc2::Composer * composer,hwc2_display_t display,bool is_primary)367 DisplayParams HardwareComposer::GetDisplayParams(
368 Hwc2::Composer* composer, hwc2_display_t display, bool is_primary) {
369 DisplayParams params;
370 params.id = display;
371 params.is_primary = is_primary;
372
373 Hwc2::Config config;
374 HWC::Error error = composer->getActiveConfig(display, &config);
375
376 if (error == HWC::Error::None) {
377 auto get_attr = [&](hwc2_attribute_t attr, const char* attr_name)
378 -> std::optional<int32_t> {
379 int32_t val;
380 HWC::Error error = composer->getDisplayAttribute(
381 display, config, (Hwc2::IComposerClient::Attribute)attr, &val);
382 if (error != HWC::Error::None) {
383 ALOGE("Failed to get %s display attr %s: %s",
384 GetDisplayName(is_primary), attr_name,
385 error.to_string().c_str());
386 return std::nullopt;
387 }
388 return val;
389 };
390
391 auto width = get_attr(HWC2_ATTRIBUTE_WIDTH, "width");
392 auto height = get_attr(HWC2_ATTRIBUTE_HEIGHT, "height");
393
394 if (width && height) {
395 params.width = *width;
396 params.height = *height;
397 } else {
398 ALOGI("Failed to get width and/or height for %s display. Using default"
399 " size %dx%d.", GetDisplayName(is_primary), kDefaultDisplayWidth,
400 kDefaultDisplayHeight);
401 params.width = kDefaultDisplayWidth;
402 params.height = kDefaultDisplayHeight;
403 }
404
405 auto vsync_period = get_attr(HWC2_ATTRIBUTE_VSYNC_PERIOD, "vsync period");
406 if (vsync_period) {
407 params.vsync_period_ns = *vsync_period;
408 } else {
409 ALOGI("Failed to get vsync period for %s display. Using default vsync"
410 " period %.2fms", GetDisplayName(is_primary),
411 static_cast<float>(kDefaultVsyncPeriodNs) / 1000000);
412 params.vsync_period_ns = kDefaultVsyncPeriodNs;
413 }
414
415 auto dpi_x = get_attr(HWC2_ATTRIBUTE_DPI_X, "DPI X");
416 auto dpi_y = get_attr(HWC2_ATTRIBUTE_DPI_Y, "DPI Y");
417 if (dpi_x && dpi_y) {
418 params.dpi.x = *dpi_x;
419 params.dpi.y = *dpi_y;
420 } else {
421 ALOGI("Failed to get dpi_x and/or dpi_y for %s display. Using default"
422 " dpi %d.", GetDisplayName(is_primary), kDefaultDpi);
423 params.dpi.x = kDefaultDpi;
424 params.dpi.y = kDefaultDpi;
425 }
426 } else {
427 ALOGE("HardwareComposer: Failed to get current %s display config: %d."
428 " Using default display values.",
429 GetDisplayName(is_primary), error.value);
430 params.width = kDefaultDisplayWidth;
431 params.height = kDefaultDisplayHeight;
432 params.dpi.x = kDefaultDpi;
433 params.dpi.y = kDefaultDpi;
434 params.vsync_period_ns = kDefaultVsyncPeriodNs;
435 }
436
437 ALOGI(
438 "HardwareComposer: %s display attributes: width=%d height=%d "
439 "vsync_period_ns=%d DPI=%dx%d",
440 GetDisplayName(is_primary),
441 params.width,
442 params.height,
443 params.vsync_period_ns,
444 params.dpi.x,
445 params.dpi.y);
446
447 return params;
448 }
449
Dump()450 std::string HardwareComposer::Dump() {
451 std::unique_lock<std::mutex> lock(post_thread_mutex_);
452 std::ostringstream stream;
453
454 auto print_display_metrics = [&](const DisplayParams& params) {
455 stream << GetDisplayName(params.is_primary)
456 << " display metrics: " << params.width << "x"
457 << params.height << " " << (params.dpi.x / 1000.0)
458 << "x" << (params.dpi.y / 1000.0) << " dpi @ "
459 << (1000000000.0 / params.vsync_period_ns) << " Hz"
460 << std::endl;
461 };
462
463 print_display_metrics(primary_display_);
464 if (external_display_)
465 print_display_metrics(*external_display_);
466
467 stream << "Post thread resumed: " << post_thread_resumed_ << std::endl;
468 stream << "Active layers: " << layers_.size() << std::endl;
469 stream << std::endl;
470
471 for (size_t i = 0; i < layers_.size(); i++) {
472 stream << "Layer " << i << ":";
473 stream << " type=" << layers_[i].GetCompositionType().to_string();
474 stream << " surface_id=" << layers_[i].GetSurfaceId();
475 stream << " buffer_id=" << layers_[i].GetBufferId();
476 stream << std::endl;
477 }
478 stream << std::endl;
479
480 if (post_thread_resumed_) {
481 stream << "Hardware Composer Debug Info:" << std::endl;
482 stream << composer_->dumpDebugInfo();
483 }
484
485 return stream.str();
486 }
487
PostLayers(hwc2_display_t display)488 void HardwareComposer::PostLayers(hwc2_display_t display) {
489 ATRACE_NAME("HardwareComposer::PostLayers");
490
491 // Setup the hardware composer layers with current buffers.
492 for (auto& layer : layers_) {
493 layer.Prepare();
494 }
495
496 // Now that we have taken in a frame from the application, we have a chance
497 // to drop the frame before passing the frame along to HWC.
498 // If the display driver has become backed up, we detect it here and then
499 // react by skipping this frame to catch up latency.
500 while (!retire_fence_fds_.empty() &&
501 (!retire_fence_fds_.front() ||
502 sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
503 // There are only 2 fences in here, no performance problem to shift the
504 // array of ints.
505 retire_fence_fds_.erase(retire_fence_fds_.begin());
506 }
507
508 const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) >
509 post_thread_config_.allowed_pending_fence_count;
510
511 if (is_fence_pending) {
512 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
513
514 ALOGW_IF(is_fence_pending,
515 "Warning: dropping a frame to catch up with HWC (pending = %zd)",
516 retire_fence_fds_.size());
517
518 for (auto& layer : layers_) {
519 layer.Drop();
520 }
521 return;
522 } else {
523 // Make the transition more obvious in systrace when the frame skip happens
524 // above.
525 ATRACE_INT("frame_skip_count", 0);
526 }
527
528 #if TRACE > 1
529 for (size_t i = 0; i < layers_.size(); i++) {
530 ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s",
531 i, layers_[i].GetBufferId(),
532 layers_[i].GetCompositionType().to_string().c_str());
533 }
534 #endif
535
536 HWC::Error error = Validate(display);
537 if (error != HWC::Error::None) {
538 ALOGE("HardwareComposer::PostLayers: Validate failed: %s display=%" PRIu64,
539 error.to_string().c_str(), display);
540 return;
541 }
542
543 error = Present(display);
544 if (error != HWC::Error::None) {
545 ALOGE("HardwareComposer::PostLayers: Present failed: %s",
546 error.to_string().c_str());
547 return;
548 }
549
550 std::vector<Hwc2::Layer> out_layers;
551 std::vector<int> out_fences;
552 error = composer_->getReleaseFences(display,
553 &out_layers, &out_fences);
554 ALOGE_IF(error != HWC::Error::None,
555 "HardwareComposer::PostLayers: Failed to get release fences: %s",
556 error.to_string().c_str());
557
558 // Perform post-frame bookkeeping.
559 uint32_t num_elements = out_layers.size();
560 for (size_t i = 0; i < num_elements; ++i) {
561 for (auto& layer : layers_) {
562 if (layer.GetLayerHandle() == out_layers[i]) {
563 layer.Finish(out_fences[i]);
564 }
565 }
566 }
567 }
568
SetDisplaySurfaces(std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces)569 void HardwareComposer::SetDisplaySurfaces(
570 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) {
571 ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
572 surfaces.size());
573 const bool display_idle = surfaces.size() == 0;
574 {
575 std::unique_lock<std::mutex> lock(post_thread_mutex_);
576 surfaces_ = std::move(surfaces);
577 surfaces_changed_ = true;
578 }
579
580 if (request_display_callback_)
581 request_display_callback_(!display_idle);
582
583 // Set idle state based on whether there are any surfaces to handle.
584 UpdatePostThreadState(PostThreadState::Idle, display_idle);
585 }
586
OnNewGlobalBuffer(DvrGlobalBufferKey key,IonBuffer & ion_buffer)587 int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key,
588 IonBuffer& ion_buffer) {
589 if (key == DvrGlobalBuffers::kVsyncBuffer) {
590 vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>(
591 &ion_buffer, CPUUsageMode::WRITE_OFTEN);
592
593 if (vsync_ring_->IsMapped() == false) {
594 return -EPERM;
595 }
596 }
597
598 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
599 return MapConfigBuffer(ion_buffer);
600 }
601
602 return 0;
603 }
604
OnDeletedGlobalBuffer(DvrGlobalBufferKey key)605 void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) {
606 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
607 ConfigBufferDeleted();
608 }
609 }
610
MapConfigBuffer(IonBuffer & ion_buffer)611 int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) {
612 std::lock_guard<std::mutex> lock(shared_config_mutex_);
613 shared_config_ring_ = DvrConfigRing();
614
615 if (ion_buffer.width() < DvrConfigRing::MemorySize()) {
616 ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size.");
617 return -EINVAL;
618 }
619
620 void* buffer_base = 0;
621 int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(),
622 ion_buffer.height(), &buffer_base);
623 if (result != 0) {
624 ALOGE(
625 "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config "
626 "buffer.");
627 return -EPERM;
628 }
629
630 shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width());
631 ion_buffer.Unlock();
632
633 return 0;
634 }
635
ConfigBufferDeleted()636 void HardwareComposer::ConfigBufferDeleted() {
637 std::lock_guard<std::mutex> lock(shared_config_mutex_);
638 shared_config_ring_ = DvrConfigRing();
639 }
640
UpdateConfigBuffer()641 void HardwareComposer::UpdateConfigBuffer() {
642 std::lock_guard<std::mutex> lock(shared_config_mutex_);
643 if (!shared_config_ring_.is_valid())
644 return;
645 // Copy from latest record in shared_config_ring_ to local copy.
646 DvrConfig record;
647 if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) {
648 ALOGI("DvrConfig updated: sequence %u, post offset %d",
649 shared_config_ring_sequence_, record.frame_post_offset_ns);
650 ++shared_config_ring_sequence_;
651 post_thread_config_ = record;
652 }
653 }
654
PostThreadPollInterruptible(const pdx::LocalHandle & event_fd,int requested_events,int timeout_ms)655 int HardwareComposer::PostThreadPollInterruptible(
656 const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) {
657 pollfd pfd[2] = {
658 {
659 .fd = event_fd.Get(),
660 .events = static_cast<short>(requested_events),
661 .revents = 0,
662 },
663 {
664 .fd = post_thread_event_fd_.Get(),
665 .events = POLLPRI | POLLIN,
666 .revents = 0,
667 },
668 };
669 int ret, error;
670 do {
671 ret = poll(pfd, 2, timeout_ms);
672 error = errno;
673 ALOGW_IF(ret < 0,
674 "HardwareComposer::PostThreadPollInterruptible: Error during "
675 "poll(): %s (%d)",
676 strerror(error), error);
677 } while (ret < 0 && error == EINTR);
678
679 if (ret < 0) {
680 return -error;
681 } else if (ret == 0) {
682 return -ETIMEDOUT;
683 } else if (pfd[0].revents != 0) {
684 return 0;
685 } else if (pfd[1].revents != 0) {
686 ALOGI("VrHwcPost thread interrupted: revents=%x", pfd[1].revents);
687 return kPostThreadInterrupted;
688 } else {
689 return 0;
690 }
691 }
692
693 // Sleep until the next predicted vsync, returning the predicted vsync
694 // timestamp.
WaitForPredictedVSync()695 Status<int64_t> HardwareComposer::WaitForPredictedVSync() {
696 const int64_t predicted_vsync_time = last_vsync_timestamp_ +
697 (target_display_->vsync_period_ns * vsync_prediction_interval_);
698 const int error = SleepUntil(predicted_vsync_time);
699 if (error < 0) {
700 ALOGE("HardwareComposer::WaifForVSync:: Failed to sleep: %s",
701 strerror(-error));
702 return error;
703 }
704 return {predicted_vsync_time};
705 }
706
SleepUntil(int64_t wakeup_timestamp)707 int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
708 const int timer_fd = vsync_sleep_timer_fd_.Get();
709 const itimerspec wakeup_itimerspec = {
710 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
711 .it_value = NsToTimespec(wakeup_timestamp),
712 };
713 int ret =
714 timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
715 int error = errno;
716 if (ret < 0) {
717 ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
718 strerror(error));
719 return -error;
720 }
721
722 return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN,
723 /*timeout_ms*/ -1);
724 }
725
PostThread()726 void HardwareComposer::PostThread() {
727 // NOLINTNEXTLINE(runtime/int)
728 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0);
729
730 // Set the scheduler to SCHED_FIFO with high priority. If this fails here
731 // there may have been a startup timing issue between this thread and
732 // performanced. Try again later when this thread becomes active.
733 bool thread_policy_setup =
734 SetThreadPolicy("graphics:high", "/system/performance");
735
736 // Create a timerfd based on CLOCK_MONOTINIC.
737 vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
738 LOG_ALWAYS_FATAL_IF(
739 !vsync_sleep_timer_fd_,
740 "HardwareComposer: Failed to create vsync sleep timerfd: %s",
741 strerror(errno));
742
743 struct VsyncEyeOffsets { int64_t left_ns, right_ns; };
744 bool was_running = false;
745
746 auto get_vsync_eye_offsets = [this]() -> VsyncEyeOffsets {
747 VsyncEyeOffsets offsets;
748 offsets.left_ns =
749 GetPosePredictionTimeOffset(target_display_->vsync_period_ns);
750
751 // TODO(jbates) Query vblank time from device, when such an API is
752 // available. This value (6.3%) was measured on A00 in low persistence mode.
753 int64_t vblank_ns = target_display_->vsync_period_ns * 63 / 1000;
754 offsets.right_ns = (target_display_->vsync_period_ns - vblank_ns) / 2;
755
756 // Check property for overriding right eye offset value.
757 offsets.right_ns =
758 property_get_int64(kRightEyeOffsetProperty, offsets.right_ns);
759
760 return offsets;
761 };
762
763 VsyncEyeOffsets vsync_eye_offsets = get_vsync_eye_offsets();
764
765 while (1) {
766 ATRACE_NAME("HardwareComposer::PostThread");
767
768 // Check for updated config once per vsync.
769 UpdateConfigBuffer();
770
771 while (post_thread_quiescent_) {
772 std::unique_lock<std::mutex> lock(post_thread_mutex_);
773 ALOGI("HardwareComposer::PostThread: Entering quiescent state.");
774
775 if (was_running) {
776 vsync_trace_parity_ = false;
777 ATRACE_INT(kVsyncTraceEventName, 0);
778 }
779
780 // Tear down resources.
781 OnPostThreadPaused();
782 was_running = false;
783 post_thread_resumed_ = false;
784 post_thread_ready_.notify_all();
785
786 if (PostThreadCondWait(lock, -1,
787 [this] { return !post_thread_quiescent_; })) {
788 // A true return value means we've been asked to quit.
789 return;
790 }
791
792 post_thread_resumed_ = true;
793 post_thread_ready_.notify_all();
794
795 ALOGI("HardwareComposer::PostThread: Exiting quiescent state.");
796 }
797
798 if (!composer_)
799 CreateComposer();
800
801 bool target_display_changed = UpdateTargetDisplay();
802 bool just_resumed_running = !was_running;
803 was_running = true;
804
805 if (target_display_changed)
806 vsync_eye_offsets = get_vsync_eye_offsets();
807
808 if (just_resumed_running) {
809 OnPostThreadResumed();
810
811 // Try to setup the scheduler policy if it failed during startup. Only
812 // attempt to do this on transitions from inactive to active to avoid
813 // spamming the system with RPCs and log messages.
814 if (!thread_policy_setup) {
815 thread_policy_setup =
816 SetThreadPolicy("graphics:high", "/system/performance");
817 }
818 }
819
820 if (target_display_changed || just_resumed_running) {
821 // Initialize the last vsync timestamp with the current time. The
822 // predictor below uses this time + the vsync interval in absolute time
823 // units for the initial delay. Once the driver starts reporting vsync the
824 // predictor will sync up with the real vsync.
825 last_vsync_timestamp_ = GetSystemClockNs();
826 vsync_prediction_interval_ = 1;
827 retire_fence_fds_.clear();
828 }
829
830 int64_t vsync_timestamp = 0;
831 {
832 TRACE_FORMAT("wait_vsync|vsync=%u;last_timestamp=%" PRId64
833 ";prediction_interval=%d|",
834 vsync_count_ + 1, last_vsync_timestamp_,
835 vsync_prediction_interval_);
836
837 auto status = WaitForPredictedVSync();
838 ALOGE_IF(
839 !status,
840 "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
841 status.GetErrorMessage().c_str());
842
843 // If there was an error either sleeping was interrupted due to pausing or
844 // there was an error getting the latest timestamp.
845 if (!status)
846 continue;
847
848 // Predicted vsync timestamp for this interval. This is stable because we
849 // use absolute time for the wakeup timer.
850 vsync_timestamp = status.get();
851 }
852
853 vsync_trace_parity_ = !vsync_trace_parity_;
854 ATRACE_INT(kVsyncTraceEventName, vsync_trace_parity_ ? 1 : 0);
855
856 // Advance the vsync counter only if the system is keeping up with hardware
857 // vsync to give clients an indication of the delays.
858 if (vsync_prediction_interval_ == 1)
859 ++vsync_count_;
860
861 UpdateLayerConfig();
862
863 // Publish the vsync event.
864 if (vsync_ring_) {
865 DvrVsync vsync;
866 vsync.vsync_count = vsync_count_;
867 vsync.vsync_timestamp_ns = vsync_timestamp;
868 vsync.vsync_left_eye_offset_ns = vsync_eye_offsets.left_ns;
869 vsync.vsync_right_eye_offset_ns = vsync_eye_offsets.right_ns;
870 vsync.vsync_period_ns = target_display_->vsync_period_ns;
871
872 vsync_ring_->Publish(vsync);
873 }
874
875 {
876 // Sleep until shortly before vsync.
877 ATRACE_NAME("sleep");
878
879 const int64_t display_time_est_ns =
880 vsync_timestamp + target_display_->vsync_period_ns;
881 const int64_t now_ns = GetSystemClockNs();
882 const int64_t sleep_time_ns = display_time_est_ns - now_ns -
883 post_thread_config_.frame_post_offset_ns;
884 const int64_t wakeup_time_ns =
885 display_time_est_ns - post_thread_config_.frame_post_offset_ns;
886
887 ATRACE_INT64("sleep_time_ns", sleep_time_ns);
888 if (sleep_time_ns > 0) {
889 int error = SleepUntil(wakeup_time_ns);
890 ALOGE_IF(error < 0 && error != kPostThreadInterrupted,
891 "HardwareComposer::PostThread: Failed to sleep: %s",
892 strerror(-error));
893 // If the sleep was interrupted (error == kPostThreadInterrupted),
894 // we still go through and present this frame because we may have set
895 // layers earlier and we want to flush the Composer's internal command
896 // buffer by continuing through to validate and present.
897 }
898 }
899
900 {
901 auto status = composer_callback_->GetVsyncTime(target_display_->id);
902
903 // If we failed to read vsync there might be a problem with the driver.
904 // Since there's nothing we can do just behave as though we didn't get an
905 // updated vsync time and let the prediction continue.
906 const int64_t current_vsync_timestamp =
907 status ? status.get() : last_vsync_timestamp_;
908
909 const bool vsync_delayed =
910 last_vsync_timestamp_ == current_vsync_timestamp;
911 ATRACE_INT("vsync_delayed", vsync_delayed);
912
913 // If vsync was delayed advance the prediction interval and allow the
914 // fence logic in PostLayers() to skip the frame.
915 if (vsync_delayed) {
916 ALOGW(
917 "HardwareComposer::PostThread: VSYNC timestamp did not advance "
918 "since last frame: timestamp=%" PRId64 " prediction_interval=%d",
919 current_vsync_timestamp, vsync_prediction_interval_);
920 vsync_prediction_interval_++;
921 } else {
922 // We have an updated vsync timestamp, reset the prediction interval.
923 last_vsync_timestamp_ = current_vsync_timestamp;
924 vsync_prediction_interval_ = 1;
925 }
926 }
927
928 PostLayers(target_display_->id);
929 }
930 }
931
UpdateTargetDisplay()932 bool HardwareComposer::UpdateTargetDisplay() {
933 bool target_display_changed = false;
934 auto displays = composer_callback_->GetDisplays();
935 if (displays.external_display_was_hotplugged) {
936 bool was_using_external_display = !target_display_->is_primary;
937 if (was_using_external_display) {
938 // The external display was hotplugged, so make sure to ignore any bad
939 // display errors as we destroy the layers.
940 for (auto& layer: layers_)
941 layer.IgnoreBadDisplayErrorsOnDestroy(true);
942 }
943
944 if (displays.external_display) {
945 // External display was connected
946 external_display_ = GetDisplayParams(composer_.get(),
947 *displays.external_display, /*is_primary*/ false);
948
949 ALOGI("External display connected. Switching to external display.");
950 target_display_ = &(*external_display_);
951 target_display_changed = true;
952 } else {
953 // External display was disconnected
954 external_display_ = std::nullopt;
955 if (was_using_external_display) {
956 ALOGI("External display disconnected. Switching to primary display.");
957 target_display_ = &primary_display_;
958 target_display_changed = true;
959 }
960 }
961 }
962
963 if (target_display_changed) {
964 // If we're switching to the external display, turn the primary display off.
965 if (!target_display_->is_primary) {
966 EnableDisplay(primary_display_, false);
967 }
968 // If we're switching to the primary display, and the external display is
969 // still connected, turn the external display off.
970 else if (target_display_->is_primary && external_display_) {
971 EnableDisplay(*external_display_, false);
972 }
973
974 // Update the cached edid data for the current display.
975 UpdateEdidData(composer_.get(), target_display_->id);
976
977 // Turn the new target display on.
978 EnableDisplay(*target_display_, true);
979
980 // When we switch displays we need to recreate all the layers, so clear the
981 // current list, which will trigger layer recreation.
982 layers_.clear();
983 }
984
985 return target_display_changed;
986 }
987
988 // Checks for changes in the surface stack and updates the layer config to
989 // accomodate the new stack.
UpdateLayerConfig()990 void HardwareComposer::UpdateLayerConfig() {
991 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces;
992 {
993 std::unique_lock<std::mutex> lock(post_thread_mutex_);
994
995 if (!surfaces_changed_ && (!layers_.empty() || surfaces_.empty()))
996 return;
997
998 surfaces = surfaces_;
999 surfaces_changed_ = false;
1000 }
1001
1002 ATRACE_NAME("UpdateLayerConfig_HwLayers");
1003
1004 // Sort the new direct surface list by z-order to determine the relative order
1005 // of the surfaces. This relative order is used for the HWC z-order value to
1006 // insulate VrFlinger and HWC z-order semantics from each other.
1007 std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) {
1008 return a->z_order() < b->z_order();
1009 });
1010
1011 // Prepare a new layer stack, pulling in layers from the previous
1012 // layer stack that are still active and updating their attributes.
1013 std::vector<Layer> layers;
1014 size_t layer_index = 0;
1015 for (const auto& surface : surfaces) {
1016 // The bottom layer is opaque, other layers blend.
1017 HWC::BlendMode blending =
1018 layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage;
1019
1020 // Try to find a layer for this surface in the set of active layers.
1021 auto search =
1022 std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id());
1023 const bool found = search != layers_.end() &&
1024 search->GetSurfaceId() == surface->surface_id();
1025 if (found) {
1026 // Update the attributes of the layer that may have changed.
1027 search->SetBlending(blending);
1028 search->SetZOrder(layer_index); // Relative z-order.
1029
1030 // Move the existing layer to the new layer set and remove the empty layer
1031 // object from the current set.
1032 layers.push_back(std::move(*search));
1033 layers_.erase(search);
1034 } else {
1035 // Insert a layer for the new surface.
1036 layers.emplace_back(composer_.get(), *target_display_, surface, blending,
1037 HWC::Composition::Device, layer_index);
1038 }
1039
1040 ALOGI_IF(
1041 TRACE,
1042 "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d",
1043 layer_index, layers[layer_index].GetSurfaceId());
1044
1045 layer_index++;
1046 }
1047
1048 // Sort the new layer stack by ascending surface id.
1049 std::sort(layers.begin(), layers.end());
1050
1051 // Replace the previous layer set with the new layer set. The destructor of
1052 // the previous set will clean up the remaining Layers that are not moved to
1053 // the new layer set.
1054 layers_ = std::move(layers);
1055
1056 ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
1057 layers_.size());
1058 }
1059
1060 std::vector<sp<IVsyncCallback>>::const_iterator
FindCallback(const sp<IVsyncCallback> & callback) const1061 HardwareComposer::VsyncService::FindCallback(
1062 const sp<IVsyncCallback>& callback) const {
1063 sp<IBinder> binder = IInterface::asBinder(callback);
1064 return std::find_if(callbacks_.cbegin(), callbacks_.cend(),
1065 [&](const sp<IVsyncCallback>& callback) {
1066 return IInterface::asBinder(callback) == binder;
1067 });
1068 }
1069
registerCallback(const sp<IVsyncCallback> callback)1070 status_t HardwareComposer::VsyncService::registerCallback(
1071 const sp<IVsyncCallback> callback) {
1072 std::lock_guard<std::mutex> autolock(mutex_);
1073 if (FindCallback(callback) == callbacks_.cend()) {
1074 callbacks_.push_back(callback);
1075 }
1076 return OK;
1077 }
1078
unregisterCallback(const sp<IVsyncCallback> callback)1079 status_t HardwareComposer::VsyncService::unregisterCallback(
1080 const sp<IVsyncCallback> callback) {
1081 std::lock_guard<std::mutex> autolock(mutex_);
1082 auto iter = FindCallback(callback);
1083 if (iter != callbacks_.cend()) {
1084 callbacks_.erase(iter);
1085 }
1086 return OK;
1087 }
1088
OnVsync(int64_t vsync_timestamp)1089 void HardwareComposer::VsyncService::OnVsync(int64_t vsync_timestamp) {
1090 ATRACE_NAME("VsyncService::OnVsync");
1091 std::lock_guard<std::mutex> autolock(mutex_);
1092 for (auto iter = callbacks_.begin(); iter != callbacks_.end();) {
1093 if ((*iter)->onVsync(vsync_timestamp) == android::DEAD_OBJECT) {
1094 iter = callbacks_.erase(iter);
1095 } else {
1096 ++iter;
1097 }
1098 }
1099 }
1100
onHotplug(Hwc2::Display display,IComposerCallback::Connection conn)1101 Return<void> HardwareComposer::ComposerCallback::onHotplug(
1102 Hwc2::Display display, IComposerCallback::Connection conn) {
1103 std::lock_guard<std::mutex> lock(mutex_);
1104 ALOGI("onHotplug display=%" PRIu64 " conn=%d", display, conn);
1105
1106 bool is_primary = !got_first_hotplug_ || display == primary_display_.id;
1107
1108 // Our first onHotplug callback is always for the primary display.
1109 if (!got_first_hotplug_) {
1110 LOG_ALWAYS_FATAL_IF(conn != IComposerCallback::Connection::CONNECTED,
1111 "Initial onHotplug callback should be primary display connected");
1112 got_first_hotplug_ = true;
1113 } else if (is_primary) {
1114 ALOGE("Ignoring unexpected onHotplug() call for primary display");
1115 return Void();
1116 }
1117
1118 if (conn == IComposerCallback::Connection::CONNECTED) {
1119 if (!is_primary)
1120 external_display_ = DisplayInfo();
1121 DisplayInfo& display_info = is_primary ?
1122 primary_display_ : *external_display_;
1123 display_info.id = display;
1124
1125 std::array<char, 1024> buffer;
1126 snprintf(buffer.data(), buffer.size(),
1127 "/sys/class/graphics/fb%" PRIu64 "/vsync_event", display);
1128 if (LocalHandle handle{buffer.data(), O_RDONLY}) {
1129 ALOGI(
1130 "HardwareComposer::ComposerCallback::onHotplug: Driver supports "
1131 "vsync_event node for display %" PRIu64,
1132 display);
1133 display_info.driver_vsync_event_fd = std::move(handle);
1134 } else {
1135 ALOGI(
1136 "HardwareComposer::ComposerCallback::onHotplug: Driver does not "
1137 "support vsync_event node for display %" PRIu64,
1138 display);
1139 }
1140 } else if (conn == IComposerCallback::Connection::DISCONNECTED) {
1141 external_display_ = std::nullopt;
1142 }
1143
1144 if (!is_primary)
1145 external_display_was_hotplugged_ = true;
1146
1147 return Void();
1148 }
1149
onRefresh(Hwc2::Display)1150 Return<void> HardwareComposer::ComposerCallback::onRefresh(
1151 Hwc2::Display /*display*/) {
1152 return hardware::Void();
1153 }
1154
onVsync(Hwc2::Display display,int64_t timestamp)1155 Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display,
1156 int64_t timestamp) {
1157 TRACE_FORMAT("vsync_callback|display=%" PRIu64 ";timestamp=%" PRId64 "|",
1158 display, timestamp);
1159 std::lock_guard<std::mutex> lock(mutex_);
1160 DisplayInfo* display_info = GetDisplayInfo(display);
1161 if (display_info) {
1162 display_info->callback_vsync_timestamp = timestamp;
1163 }
1164 if (primary_display_.id == display && vsync_service_ != nullptr) {
1165 vsync_service_->OnVsync(timestamp);
1166 }
1167
1168 return Void();
1169 }
1170
onVsync_2_4(Hwc2::Display,int64_t,Hwc2::VsyncPeriodNanos)1171 Return<void> HardwareComposer::ComposerCallback::onVsync_2_4(
1172 Hwc2::Display /*display*/, int64_t /*timestamp*/,
1173 Hwc2::VsyncPeriodNanos /*vsyncPeriodNanos*/) {
1174 LOG_ALWAYS_FATAL("Unexpected onVsync_2_4 callback");
1175 return Void();
1176 }
1177
onVsyncPeriodTimingChanged(Hwc2::Display,const Hwc2::VsyncPeriodChangeTimeline &)1178 Return<void> HardwareComposer::ComposerCallback::onVsyncPeriodTimingChanged(
1179 Hwc2::Display /*display*/,
1180 const Hwc2::VsyncPeriodChangeTimeline& /*updatedTimeline*/) {
1181 LOG_ALWAYS_FATAL("Unexpected onVsyncPeriodTimingChanged callback");
1182 return Void();
1183 }
1184
onSeamlessPossible(Hwc2::Display)1185 Return<void> HardwareComposer::ComposerCallback::onSeamlessPossible(
1186 Hwc2::Display /*display*/) {
1187 LOG_ALWAYS_FATAL("Unexpected onSeamlessPossible callback");
1188 return Void();
1189 }
1190
SetVsyncService(const sp<VsyncService> & vsync_service)1191 void HardwareComposer::ComposerCallback::SetVsyncService(
1192 const sp<VsyncService>& vsync_service) {
1193 std::lock_guard<std::mutex> lock(mutex_);
1194 vsync_service_ = vsync_service;
1195 }
1196
1197 HardwareComposer::ComposerCallback::Displays
GetDisplays()1198 HardwareComposer::ComposerCallback::GetDisplays() {
1199 std::lock_guard<std::mutex> lock(mutex_);
1200 Displays displays;
1201 displays.primary_display = primary_display_.id;
1202 if (external_display_)
1203 displays.external_display = external_display_->id;
1204 if (external_display_was_hotplugged_) {
1205 external_display_was_hotplugged_ = false;
1206 displays.external_display_was_hotplugged = true;
1207 }
1208 return displays;
1209 }
1210
GetVsyncTime(hwc2_display_t display)1211 Status<int64_t> HardwareComposer::ComposerCallback::GetVsyncTime(
1212 hwc2_display_t display) {
1213 std::lock_guard<std::mutex> autolock(mutex_);
1214 DisplayInfo* display_info = GetDisplayInfo(display);
1215 if (!display_info) {
1216 ALOGW("Attempt to get vsync time for unknown display %" PRIu64, display);
1217 return ErrorStatus(EINVAL);
1218 }
1219
1220 // See if the driver supports direct vsync events.
1221 LocalHandle& event_fd = display_info->driver_vsync_event_fd;
1222 if (!event_fd) {
1223 // Fall back to returning the last timestamp returned by the vsync
1224 // callback.
1225 return display_info->callback_vsync_timestamp;
1226 }
1227
1228 // When the driver supports the vsync_event sysfs node we can use it to
1229 // determine the latest vsync timestamp, even if the HWC callback has been
1230 // delayed.
1231
1232 // The driver returns data in the form "VSYNC=<timestamp ns>".
1233 std::array<char, 32> data;
1234 data.fill('\0');
1235
1236 // Seek back to the beginning of the event file.
1237 int ret = lseek(event_fd.Get(), 0, SEEK_SET);
1238 if (ret < 0) {
1239 const int error = errno;
1240 ALOGE(
1241 "HardwareComposer::ComposerCallback::GetVsyncTime: Failed to seek "
1242 "vsync event fd: %s",
1243 strerror(error));
1244 return ErrorStatus(error);
1245 }
1246
1247 // Read the vsync event timestamp.
1248 ret = read(event_fd.Get(), data.data(), data.size());
1249 if (ret < 0) {
1250 const int error = errno;
1251 ALOGE_IF(error != EAGAIN,
1252 "HardwareComposer::ComposerCallback::GetVsyncTime: Error "
1253 "while reading timestamp: %s",
1254 strerror(error));
1255 return ErrorStatus(error);
1256 }
1257
1258 int64_t timestamp;
1259 ret = sscanf(data.data(), "VSYNC=%" PRIu64,
1260 reinterpret_cast<uint64_t*>(×tamp));
1261 if (ret < 0) {
1262 const int error = errno;
1263 ALOGE(
1264 "HardwareComposer::ComposerCallback::GetVsyncTime: Error while "
1265 "parsing timestamp: %s",
1266 strerror(error));
1267 return ErrorStatus(error);
1268 }
1269
1270 return {timestamp};
1271 }
1272
1273 HardwareComposer::ComposerCallback::DisplayInfo*
GetDisplayInfo(hwc2_display_t display)1274 HardwareComposer::ComposerCallback::GetDisplayInfo(hwc2_display_t display) {
1275 if (display == primary_display_.id) {
1276 return &primary_display_;
1277 } else if (external_display_ && display == external_display_->id) {
1278 return &(*external_display_);
1279 }
1280 return nullptr;
1281 }
1282
Reset()1283 void Layer::Reset() {
1284 if (hardware_composer_layer_) {
1285 HWC::Error error =
1286 composer_->destroyLayer(display_params_.id, hardware_composer_layer_);
1287 if (error != HWC::Error::None &&
1288 (!ignore_bad_display_errors_on_destroy_ ||
1289 error != HWC::Error::BadDisplay)) {
1290 ALOGE("destroyLayer() failed for display %" PRIu64 ", layer %" PRIu64
1291 ". error: %s", display_params_.id, hardware_composer_layer_,
1292 error.to_string().c_str());
1293 }
1294 hardware_composer_layer_ = 0;
1295 }
1296
1297 z_order_ = 0;
1298 blending_ = HWC::BlendMode::None;
1299 composition_type_ = HWC::Composition::Invalid;
1300 target_composition_type_ = composition_type_;
1301 source_ = EmptyVariant{};
1302 acquire_fence_.Close();
1303 surface_rect_functions_applied_ = false;
1304 pending_visibility_settings_ = true;
1305 cached_buffer_map_.clear();
1306 ignore_bad_display_errors_on_destroy_ = false;
1307 }
1308
Layer(Hwc2::Composer * composer,const DisplayParams & display_params,const std::shared_ptr<DirectDisplaySurface> & surface,HWC::BlendMode blending,HWC::Composition composition_type,size_t z_order)1309 Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
1310 const std::shared_ptr<DirectDisplaySurface>& surface,
1311 HWC::BlendMode blending, HWC::Composition composition_type,
1312 size_t z_order)
1313 : composer_(composer),
1314 display_params_(display_params),
1315 z_order_{z_order},
1316 blending_{blending},
1317 target_composition_type_{composition_type},
1318 source_{SourceSurface{surface}} {
1319 CommonLayerSetup();
1320 }
1321
Layer(Hwc2::Composer * composer,const DisplayParams & display_params,const std::shared_ptr<IonBuffer> & buffer,HWC::BlendMode blending,HWC::Composition composition_type,size_t z_order)1322 Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
1323 const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
1324 HWC::Composition composition_type, size_t z_order)
1325 : composer_(composer),
1326 display_params_(display_params),
1327 z_order_{z_order},
1328 blending_{blending},
1329 target_composition_type_{composition_type},
1330 source_{SourceBuffer{buffer}} {
1331 CommonLayerSetup();
1332 }
1333
~Layer()1334 Layer::~Layer() { Reset(); }
1335
Layer(Layer && other)1336 Layer::Layer(Layer&& other) noexcept { *this = std::move(other); }
1337
operator =(Layer && other)1338 Layer& Layer::operator=(Layer&& other) noexcept {
1339 if (this != &other) {
1340 Reset();
1341 using std::swap;
1342 swap(composer_, other.composer_);
1343 swap(display_params_, other.display_params_);
1344 swap(hardware_composer_layer_, other.hardware_composer_layer_);
1345 swap(z_order_, other.z_order_);
1346 swap(blending_, other.blending_);
1347 swap(composition_type_, other.composition_type_);
1348 swap(target_composition_type_, other.target_composition_type_);
1349 swap(source_, other.source_);
1350 swap(acquire_fence_, other.acquire_fence_);
1351 swap(surface_rect_functions_applied_,
1352 other.surface_rect_functions_applied_);
1353 swap(pending_visibility_settings_, other.pending_visibility_settings_);
1354 swap(cached_buffer_map_, other.cached_buffer_map_);
1355 swap(ignore_bad_display_errors_on_destroy_,
1356 other.ignore_bad_display_errors_on_destroy_);
1357 }
1358 return *this;
1359 }
1360
UpdateBuffer(const std::shared_ptr<IonBuffer> & buffer)1361 void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) {
1362 if (source_.is<SourceBuffer>())
1363 std::get<SourceBuffer>(source_) = {buffer};
1364 }
1365
SetBlending(HWC::BlendMode blending)1366 void Layer::SetBlending(HWC::BlendMode blending) {
1367 if (blending_ != blending) {
1368 blending_ = blending;
1369 pending_visibility_settings_ = true;
1370 }
1371 }
1372
SetZOrder(size_t z_order)1373 void Layer::SetZOrder(size_t z_order) {
1374 if (z_order_ != z_order) {
1375 z_order_ = z_order;
1376 pending_visibility_settings_ = true;
1377 }
1378 }
1379
GetBuffer()1380 IonBuffer* Layer::GetBuffer() {
1381 struct Visitor {
1382 IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); }
1383 IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); }
1384 IonBuffer* operator()(EmptyVariant) { return nullptr; }
1385 };
1386 return source_.Visit(Visitor{});
1387 }
1388
UpdateVisibilitySettings()1389 void Layer::UpdateVisibilitySettings() {
1390 if (pending_visibility_settings_) {
1391 pending_visibility_settings_ = false;
1392
1393 HWC::Error error;
1394
1395 error = composer_->setLayerBlendMode(
1396 display_params_.id, hardware_composer_layer_,
1397 blending_.cast<Hwc2::IComposerClient::BlendMode>());
1398 ALOGE_IF(error != HWC::Error::None,
1399 "Layer::UpdateLayerSettings: Error setting layer blend mode: %s",
1400 error.to_string().c_str());
1401
1402 error = composer_->setLayerZOrder(display_params_.id,
1403 hardware_composer_layer_, z_order_);
1404 ALOGE_IF(error != HWC::Error::None,
1405 "Layer::UpdateLayerSettings: Error setting z_ order: %s",
1406 error.to_string().c_str());
1407 }
1408 }
1409
UpdateLayerSettings()1410 void Layer::UpdateLayerSettings() {
1411 HWC::Error error;
1412
1413 UpdateVisibilitySettings();
1414
1415 // TODO(eieio): Use surface attributes or some other mechanism to control
1416 // the layer display frame.
1417 error = composer_->setLayerDisplayFrame(
1418 display_params_.id, hardware_composer_layer_,
1419 {0, 0, display_params_.width, display_params_.height});
1420 ALOGE_IF(error != HWC::Error::None,
1421 "Layer::UpdateLayerSettings: Error setting layer display frame: %s",
1422 error.to_string().c_str());
1423
1424 error = composer_->setLayerVisibleRegion(
1425 display_params_.id, hardware_composer_layer_,
1426 {{0, 0, display_params_.width, display_params_.height}});
1427 ALOGE_IF(error != HWC::Error::None,
1428 "Layer::UpdateLayerSettings: Error setting layer visible region: %s",
1429 error.to_string().c_str());
1430
1431 error = composer_->setLayerPlaneAlpha(display_params_.id,
1432 hardware_composer_layer_, 1.0f);
1433 ALOGE_IF(error != HWC::Error::None,
1434 "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s",
1435 error.to_string().c_str());
1436 }
1437
CommonLayerSetup()1438 void Layer::CommonLayerSetup() {
1439 HWC::Error error = composer_->createLayer(display_params_.id,
1440 &hardware_composer_layer_);
1441 ALOGE_IF(error != HWC::Error::None,
1442 "Layer::CommonLayerSetup: Failed to create layer on primary "
1443 "display: %s",
1444 error.to_string().c_str());
1445 UpdateLayerSettings();
1446 }
1447
CheckAndUpdateCachedBuffer(std::size_t slot,int buffer_id)1448 bool Layer::CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id) {
1449 auto search = cached_buffer_map_.find(slot);
1450 if (search != cached_buffer_map_.end() && search->second == buffer_id)
1451 return true;
1452
1453 // Assign or update the buffer slot.
1454 if (buffer_id >= 0)
1455 cached_buffer_map_[slot] = buffer_id;
1456 return false;
1457 }
1458
Prepare()1459 void Layer::Prepare() {
1460 int right, bottom, id;
1461 sp<GraphicBuffer> handle;
1462 std::size_t slot;
1463
1464 // Acquire the next buffer according to the type of source.
1465 IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) {
1466 std::tie(right, bottom, id, handle, acquire_fence_, slot) =
1467 source.Acquire();
1468 });
1469
1470 TRACE_FORMAT("Layer::Prepare|buffer_id=%d;slot=%zu|", id, slot);
1471
1472 // Update any visibility (blending, z-order) changes that occurred since
1473 // last prepare.
1474 UpdateVisibilitySettings();
1475
1476 // When a layer is first setup there may be some time before the first
1477 // buffer arrives. Setup the HWC layer as a solid color to stall for time
1478 // until the first buffer arrives. Once the first buffer arrives there will
1479 // always be a buffer for the frame even if it is old.
1480 if (!handle.get()) {
1481 if (composition_type_ == HWC::Composition::Invalid) {
1482 composition_type_ = HWC::Composition::SolidColor;
1483 composer_->setLayerCompositionType(
1484 display_params_.id, hardware_composer_layer_,
1485 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1486 Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0};
1487 composer_->setLayerColor(display_params_.id, hardware_composer_layer_,
1488 layer_color);
1489 } else {
1490 // The composition type is already set. Nothing else to do until a
1491 // buffer arrives.
1492 }
1493 } else {
1494 if (composition_type_ != target_composition_type_) {
1495 composition_type_ = target_composition_type_;
1496 composer_->setLayerCompositionType(
1497 display_params_.id, hardware_composer_layer_,
1498 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1499 }
1500
1501 // See if the HWC cache already has this buffer.
1502 const bool cached = CheckAndUpdateCachedBuffer(slot, id);
1503 if (cached)
1504 handle = nullptr;
1505
1506 HWC::Error error{HWC::Error::None};
1507 error =
1508 composer_->setLayerBuffer(display_params_.id, hardware_composer_layer_,
1509 slot, handle, acquire_fence_.Get());
1510
1511 ALOGE_IF(error != HWC::Error::None,
1512 "Layer::Prepare: Error setting layer buffer: %s",
1513 error.to_string().c_str());
1514
1515 if (!surface_rect_functions_applied_) {
1516 const float float_right = right;
1517 const float float_bottom = bottom;
1518 error = composer_->setLayerSourceCrop(display_params_.id,
1519 hardware_composer_layer_,
1520 {0, 0, float_right, float_bottom});
1521
1522 ALOGE_IF(error != HWC::Error::None,
1523 "Layer::Prepare: Error setting layer source crop: %s",
1524 error.to_string().c_str());
1525
1526 surface_rect_functions_applied_ = true;
1527 }
1528 }
1529 }
1530
Finish(int release_fence_fd)1531 void Layer::Finish(int release_fence_fd) {
1532 IfAnyOf<SourceSurface, SourceBuffer>::Call(
1533 &source_, [release_fence_fd](auto& source) {
1534 source.Finish(LocalHandle(release_fence_fd));
1535 });
1536 }
1537
Drop()1538 void Layer::Drop() { acquire_fence_.Close(); }
1539
1540 } // namespace dvr
1541 } // namespace android
1542