blob: cc082091bec335f8c0575f73707b752223465038 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#include "hardware_composer.h"
2
3#include <log/log.h>
4#include <cutils/properties.h>
5#include <cutils/sched_policy.h>
6#include <fcntl.h>
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>
14#include <unistd.h>
15#include <utils/Trace.h>
16
17#include <algorithm>
18#include <functional>
19#include <map>
20
21#include <dvr/performance_client_api.h>
22#include <private/dvr/clock_ns.h>
23#include <private/dvr/display_types.h>
24#include <private/dvr/pose_client_internal.h>
25#include <private/dvr/sync_util.h>
26
27#include "debug_hud_data.h"
28#include "screenshot_service.h"
29
30using android::pdx::LocalHandle;
31
32namespace android {
33namespace dvr {
34
35namespace {
36
37// If the number of pending fences goes over this count at the point when we
38// are about to submit a new frame to HWC, we will drop the frame. This should
39// be a signal that the display driver has begun queuing frames. Note that with
40// smart displays (with RAM), the fence is signaled earlier than the next vsync,
41// at the point when the DMA to the display completes. Currently we use a smart
42// display and the EDS timing coincides with zero pending fences, so this is 0.
43constexpr int kAllowedPendingFenceCount = 0;
44
45// If we think we're going to miss vsync by more than this amount, skip the
46// frame.
47constexpr int64_t kFrameSkipThresholdNs = 4000000; // 4ms
48
49// Counter PostLayers() deficiency by requiring apps to produce a frame at least
50// 2.5ms before vsync. See b/28881672.
51constexpr int64_t kFrameTimeEstimateMin = 2500000; // 2.5ms
52
53constexpr size_t kDefaultDisplayConfigCount = 32;
54
55constexpr float kMetersPerInch = 0.0254f;
56
57const char kBacklightBrightnessSysFile[] =
58 "/sys/class/leds/lcd-backlight/brightness";
59
60const char kPrimaryDisplayVSyncEventFile[] =
61 "/sys/class/graphics/fb0/vsync_event";
62
63const char kPrimaryDisplayWaitPPEventFile[] = "/sys/class/graphics/fb0/wait_pp";
64
65const char kDvrPerformanceProperty[] = "sys.dvr.performance";
66
67const char kRightEyeOffsetProperty[] = "dreamos.right_eye_offset_ns";
68
69// Returns our best guess for the time the compositor will spend rendering the
70// next frame.
71int64_t GuessFrameTime(int compositor_visible_layer_count) {
72 // The cost of asynchronous EDS and lens warp is currently measured at 2.5ms
73 // for one layer and 7ms for two layers, but guess a higher frame time to
74 // account for CPU overhead. This guess is only used before we've measured the
75 // actual time to render a frame for the current compositor configuration.
76 switch (compositor_visible_layer_count) {
77 case 0:
78 return 500000; // .5ms
79 case 1:
80 return 5000000; // 5ms
81 default:
82 return 10500000; // 10.5ms
83 }
84}
85
86// Get time offset from a vsync to when the pose for that vsync should be
87// predicted out to. For example, if scanout gets halfway through the frame
88// at the halfway point between vsyncs, then this could be half the period.
89// With global shutter displays, this should be changed to the offset to when
90// illumination begins. Low persistence adds a frame of latency, so we predict
91// to the center of the next frame.
92inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
93 return (vsync_period_ns * 150) / 100;
94}
95
96} // anonymous namespace
97
98HardwareComposer::HardwareComposer()
99 : HardwareComposer(nullptr) {
100}
101
102HardwareComposer::HardwareComposer(Hwc2::Composer* hwc2_hidl)
103 : hwc2_hidl_(hwc2_hidl),
104 display_transform_(HWC_TRANSFORM_NONE),
105 display_surfaces_updated_(false),
106 hardware_layers_need_update_(false),
107 display_on_(false),
108 active_layer_count_(0),
109 gpu_layer_(nullptr),
110 terminate_post_thread_event_fd_(-1),
111 pause_post_thread_(true),
112 backlight_brightness_fd_(-1),
113 primary_display_vsync_event_fd_(-1),
114 primary_display_wait_pp_fd_(-1),
115 vsync_sleep_timer_fd_(-1),
116 last_vsync_timestamp_(0),
117 vsync_count_(0),
118 frame_skip_count_(0),
119 pose_client_(nullptr) {
120 std::transform(layer_storage_.begin(), layer_storage_.end(), layers_.begin(),
121 [](auto& layer) { return &layer; });
122
123 callbacks_ = new ComposerCallback;
124}
125
126HardwareComposer::~HardwareComposer(void) {
127 if (!IsSuspended()) {
128 Suspend();
129 }
130}
131
132bool HardwareComposer::Resume() {
133 std::lock_guard<std::mutex> autolock(layer_mutex_);
134
135 if (!IsSuspended()) {
136 ALOGE("HardwareComposer::Resume: HardwareComposer is already running.");
137 return false;
138 }
139
140 int32_t ret = HWC2_ERROR_NONE;
141
142 static const uint32_t attributes[] = {
143 HWC_DISPLAY_WIDTH, HWC_DISPLAY_HEIGHT, HWC_DISPLAY_VSYNC_PERIOD,
144 HWC_DISPLAY_DPI_X, HWC_DISPLAY_DPI_Y, HWC_DISPLAY_NO_ATTRIBUTE,
145 };
146
147 std::vector<Hwc2::Config> configs;
148 ret = (int32_t)hwc2_hidl_->getDisplayConfigs(HWC_DISPLAY_PRIMARY, &configs);
149
150 if (ret != HWC2_ERROR_NONE) {
151 ALOGE("HardwareComposer: Failed to get display configs");
152 return false;
153 }
154
155 uint32_t num_configs = configs.size();
156
157 for (size_t i = 0; i < num_configs; i++) {
158 ALOGI("HardwareComposer: cfg[%zd/%zd] = 0x%08x", i, num_configs,
159 configs[i]);
160
161 ret = GetDisplayMetrics(HWC_DISPLAY_PRIMARY, configs[i],
162 &native_display_metrics_);
163
164 if (ret != HWC2_ERROR_NONE) {
165 ALOGE("HardwareComposer: Failed to get display attributes %d", ret);
166 continue;
167 } else {
168 ret =
169 (int32_t)hwc2_hidl_->setActiveConfig(HWC_DISPLAY_PRIMARY, configs[i]);
170
171 if (ret != HWC2_ERROR_NONE) {
172 ALOGE("HardwareComposer: Failed to set display configuration; ret=%d",
173 ret);
174 continue;
175 }
176
177 break;
178 }
179 }
180
181 if (ret != HWC2_ERROR_NONE) {
182 ALOGE("HardwareComposer: Could not set a valid display configuration.");
183 return false;
184 }
185
186 // Set the display metrics but never use rotation to avoid the long latency of
187 // rotation processing in hwc.
188 display_transform_ = HWC_TRANSFORM_NONE;
189 display_metrics_ = native_display_metrics_;
190
191 ALOGI(
192 "HardwareComposer: primary display attributes: width=%d height=%d "
193 "vsync_period_ns=%d DPI=%dx%d",
194 native_display_metrics_.width, native_display_metrics_.height,
195 native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x,
196 native_display_metrics_.dpi.y);
197
198 // Always turn off vsync when we start.
199 EnableVsync(false);
200
201 constexpr int format = HAL_PIXEL_FORMAT_RGBA_8888;
202 constexpr int usage =
203 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER;
204
205 framebuffer_target_ = std::make_shared<IonBuffer>(
206 native_display_metrics_.width, native_display_metrics_.height, format,
207 usage);
208
209 // Associate each Layer instance with a hardware composer layer.
210 for (auto layer : layers_) {
211 layer->Initialize(hwc2_hidl_.get(), &native_display_metrics_);
212 }
213
Hendrik Wagenaarea572422017-02-02 10:00:48 -0800214#if ENABLE_BACKLIGHT_BRIGHTNESS
215 // TODO(hendrikw): This isn't required at the moment. It's possible that there
216 // is another method to access this when needed.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800217 // Open the backlight brightness control sysfs node.
218 backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR);
219 ALOGW_IF(!backlight_brightness_fd_,
220 "HardwareComposer: Failed to open backlight brightness control: %s",
221 strerror(errno));
Hendrik Wagenaarea572422017-02-02 10:00:48 -0800222#endif // ENABLE_BACKLIGHT_BRIGHTNESS
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800223
224 // Open the vsync event node for the primary display.
225 // TODO(eieio): Move this into a platform-specific class.
226 primary_display_vsync_event_fd_ =
227 LocalHandle(kPrimaryDisplayVSyncEventFile, O_RDONLY);
228 ALOGE_IF(!primary_display_vsync_event_fd_,
229 "HardwareComposer: Failed to open vsync event node for primary "
230 "display: %s",
231 strerror(errno));
232
233 // Open the wait pingpong status node for the primary display.
234 // TODO(eieio): Move this into a platform-specific class.
235 primary_display_wait_pp_fd_ =
236 LocalHandle(kPrimaryDisplayWaitPPEventFile, O_RDONLY);
237 ALOGE_IF(
238 !primary_display_wait_pp_fd_,
239 "HardwareComposer: Failed to open wait_pp node for primary display: %s",
240 strerror(errno));
241
242 // Create a timerfd based on CLOCK_MONOTINIC.
243 vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
244 LOG_ALWAYS_FATAL_IF(
245 !vsync_sleep_timer_fd_,
246 "HardwareComposer: Failed to create vsync sleep timerfd: %s",
247 strerror(errno));
248
249 // Connect to pose service.
250 pose_client_ = dvrPoseCreate();
251 ALOGE_IF(!pose_client_, "HardwareComposer: Failed to create pose client");
252
253 // Variables used to control the post thread state
254 pause_post_thread_ = false;
255 terminate_post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
256
257 LOG_ALWAYS_FATAL_IF(
258 !terminate_post_thread_event_fd_,
259 "HardwareComposer: Failed to create terminate PostThread event fd : %s",
260 strerror(errno));
261
262 // If get_id() is the default thread::id object, it has not been created yet
263 if (post_thread_.get_id() == std::thread::id()) {
264 post_thread_ = std::thread(&HardwareComposer::PostThread, this);
265 } else {
266 UpdateDisplayState();
267 thread_pause_semaphore_.notify_one();
268 }
269
270 return true;
271}
272
273bool HardwareComposer::Suspend() {
274 // Wait for any pending layer operations to finish
275 std::unique_lock<std::mutex> layer_lock(layer_mutex_);
276
277 if (IsSuspended()) {
278 ALOGE("HardwareComposer::Suspend: HardwareComposer is already suspended.");
279 return false;
280 }
281
282 PausePostThread();
283
284 EnableVsync(false);
285 SetPowerMode(HWC_DISPLAY_PRIMARY, HWC2_POWER_MODE_OFF);
286
287 backlight_brightness_fd_.Close();
288 primary_display_vsync_event_fd_.Close();
289 primary_display_wait_pp_fd_.Close();
290 vsync_sleep_timer_fd_.Close();
291 retire_fence_fds_.clear();
292 gpu_layer_ = nullptr;
293
294 // We have to destroy the layers before we close the hwc device
295 for (size_t i = 0; i < kMaxHardwareLayers; ++i) {
296 layers_[i]->Reset();
297 }
298
299 active_layer_count_ = 0;
300
301 framebuffer_target_.reset();
302
303 //hwc2_hidl_.reset();
304
305 if (pose_client_)
306 dvrPoseDestroy(pose_client_);
307
308 return true;
309}
310
311void HardwareComposer::PausePostThread() {
312 pause_post_thread_ = true;
313
314 int error = eventfd_write(terminate_post_thread_event_fd_.Get(), 1);
315 ALOGE_IF(error,
316 "HardwareComposer::PausePostThread: could not write post "
317 "thread termination event fd : %d",
318 error);
319
320 std::unique_lock<std::mutex> wait_for_thread(thread_pause_mutex_);
321 terminate_post_thread_event_fd_.Close();
322}
323
324DisplayMetrics HardwareComposer::GetHmdDisplayMetrics() const {
325 vec2i screen_size(display_metrics_.width, display_metrics_.height);
326 DisplayOrientation orientation =
327 (display_metrics_.width > display_metrics_.height
328 ? DisplayOrientation::kLandscape
329 : DisplayOrientation::kPortrait);
330 float dpi_x = static_cast<float>(display_metrics_.dpi.x) / 1000.0f;
331 float dpi_y = static_cast<float>(display_metrics_.dpi.y) / 1000.0f;
332 float meters_per_pixel_x = kMetersPerInch / dpi_x;
333 float meters_per_pixel_y = kMetersPerInch / dpi_y;
334 vec2 meters_per_pixel(meters_per_pixel_x, meters_per_pixel_y);
335 double frame_duration_s =
336 static_cast<double>(display_metrics_.vsync_period_ns) / 1000000000.0;
337 // TODO(hendrikw): Hard coding to 3mm. The Pixel is actually 4mm, but it
338 // seems that their tray to lens distance is wrong too, which
339 // offsets this, at least for the pixel.
340 float border_size = 0.003f;
341 return DisplayMetrics(screen_size, meters_per_pixel, border_size,
342 static_cast<float>(frame_duration_s), orientation);
343}
344
345int32_t HardwareComposer::Validate(hwc2_display_t display) {
346 uint32_t num_types;
347 uint32_t num_requests;
348 int32_t error =
349 (int32_t)hwc2_hidl_->validateDisplay(display, &num_types, &num_requests);
350
351 if (error == HWC2_ERROR_HAS_CHANGES) {
352 // TODO(skiazyk): We might need to inspect the requested changes first, but
353 // so far it seems like we shouldn't ever hit a bad state.
354 // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_,
355 // display);
356 error = (int32_t)hwc2_hidl_->acceptDisplayChanges(display);
357 }
358
359 return error;
360}
361
362int32_t HardwareComposer::EnableVsync(bool enabled) {
363 return (int32_t)hwc2_hidl_->setVsyncEnabled(
364 HWC_DISPLAY_PRIMARY,
365 (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
366 : HWC2_VSYNC_DISABLE));
367}
368
369int32_t HardwareComposer::Present(hwc2_display_t display) {
370 int32_t present_fence;
371 int32_t error = (int32_t)hwc2_hidl_->presentDisplay(display, &present_fence);
372
373 // According to the documentation, this fence is signaled at the time of
374 // vsync/DMA for physical displays.
375 if (error == HWC2_ERROR_NONE) {
376 ATRACE_INT("HardwareComposer: VsyncFence", present_fence);
377 retire_fence_fds_.emplace_back(present_fence);
378 } else {
379 ATRACE_INT("HardwareComposer: PresentResult", error);
380 }
381
382 return error;
383}
384
385int32_t HardwareComposer::SetPowerMode(hwc2_display_t display,
386 hwc2_power_mode_t mode) {
387 if (mode == HWC2_POWER_MODE_OFF) {
388 EnableVsync(false);
389 }
390
391 display_on_ = mode != HWC2_POWER_MODE_OFF;
392
393 return (int32_t)hwc2_hidl_->setPowerMode(
394 display, (Hwc2::IComposerClient::PowerMode)mode);
395}
396
397int32_t HardwareComposer::GetDisplayAttribute(hwc2_display_t display,
398 hwc2_config_t config,
399 hwc2_attribute_t attribute,
400 int32_t* out_value) const {
401 return (int32_t)hwc2_hidl_->getDisplayAttribute(
402 display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value);
403}
404
405int32_t HardwareComposer::GetDisplayMetrics(
406 hwc2_display_t display, hwc2_config_t config,
407 HWCDisplayMetrics* out_metrics) const {
408 int32_t ret = HWC2_ERROR_NONE;
409
410 ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_WIDTH,
411 &out_metrics->width);
412 if (ret != HWC2_ERROR_NONE) {
413 ALOGE("HardwareComposer: Failed to get display width");
414 return ret;
415 }
416
417 ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_HEIGHT,
418 &out_metrics->height);
419 if (ret != HWC2_ERROR_NONE) {
420 ALOGE("HardwareComposer: Failed to get display height");
421 return ret;
422 }
423
424 ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_VSYNC_PERIOD,
425 &out_metrics->vsync_period_ns);
426 if (ret != HWC2_ERROR_NONE) {
427 ALOGE("HardwareComposer: Failed to get display height");
428 return ret;
429 }
430
431 ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_DPI_X,
432 &out_metrics->dpi.x);
433 if (ret != HWC2_ERROR_NONE) {
434 ALOGE("HardwareComposer: Failed to get display DPI X");
435 return ret;
436 }
437
438 ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_DPI_Y,
439 &out_metrics->dpi.y);
440 if (ret != HWC2_ERROR_NONE) {
441 ALOGE("HardwareComposer: Failed to get display DPI Y");
442 return ret;
443 }
444
445 return HWC2_ERROR_NONE;
446}
447
448void HardwareComposer::Dump(char* buffer, uint32_t* out_size) {
449 std::string debug_str = hwc2_hidl_->dumpDebugInfo();
450 ALOGI("%s", debug_str.c_str());
451
452 if (buffer == nullptr) {
453 *out_size = debug_str.size();
454 } else {
455 std::copy(debug_str.begin(), debug_str.begin() + *out_size, buffer);
456 }
457}
458
459// TODO(skiazyk): Figure out what to do with `is_geometry_changed`. There does
460// not seem to be any equivalent in the HWC2 API, but that doesn't mean its not
461// there.
462void HardwareComposer::PostLayers(bool /*is_geometry_changed*/) {
463 ATRACE_NAME("HardwareComposer::PostLayers");
464
465 // Setup the hardware composer layers with current buffers.
466 for (size_t i = 0; i < active_layer_count_; i++) {
467 layers_[i]->Prepare();
468 }
469
470 // Now that we have taken in a frame from the application, we have a chance
471 // to drop the frame before passing the frame along to HWC.
472 // If the display driver has become backed up, we detect it here and then
473 // react by skipping this frame to catch up latency.
474 while (!retire_fence_fds_.empty() &&
475 (!retire_fence_fds_.front() ||
476 sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
477 // There are only 2 fences in here, no performance problem to shift the
478 // array of ints.
479 retire_fence_fds_.erase(retire_fence_fds_.begin());
480 }
481
482 const bool is_frame_pending = IsFramePendingInDriver();
483 const bool is_fence_pending =
484 retire_fence_fds_.size() > kAllowedPendingFenceCount;
485
486 if (is_fence_pending || is_frame_pending) {
487 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
488
489 ALOGW_IF(is_frame_pending, "Warning: frame already queued, dropping frame");
490 ALOGW_IF(is_fence_pending,
491 "Warning: dropping a frame to catch up with HWC (pending = %zd)",
492 retire_fence_fds_.size());
493
494 for (size_t i = 0; i < active_layer_count_; i++) {
495 layers_[i]->Drop();
496 }
497 return;
498 } else {
499 // Make the transition more obvious in systrace when the frame skip happens
500 // above.
501 ATRACE_INT("frame_skip_count", 0);
502 }
503
504#if TRACE
505 for (size_t i = 0; i < active_layer_count_; i++)
506 ALOGI("HardwareComposer::PostLayers: dl[%zu] ctype=0x%08x", i,
507 layers_[i]->GetCompositionType());
508#endif
509
510 int32_t ret = HWC2_ERROR_NONE;
511
512 std::vector<Hwc2::IComposerClient::Rect> full_region(1);
513 full_region[0].left = 0;
514 full_region[0].top = 0;
515 full_region[0].right = framebuffer_target_->width();
516 full_region[0].bottom = framebuffer_target_->height();
517
518 ALOGE_IF(ret, "Error setting client target : %d", ret);
519
520 ret = Validate(HWC_DISPLAY_PRIMARY);
521 if (ret) {
522 ALOGE("HardwareComposer::Validate failed; ret=%d", ret);
523 return;
524 }
525
526 ret = Present(HWC_DISPLAY_PRIMARY);
527 if (ret) {
528 ALOGE("HardwareComposer::Present failed; ret=%d", ret);
529 return;
530 }
531
532 std::vector<Hwc2::Layer> out_layers;
533 std::vector<int> out_fences;
534 ret = (int32_t)hwc2_hidl_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers,
535 &out_fences);
536 uint32_t num_elements = out_layers.size();
537
538 ALOGE_IF(ret, "HardwareComposer: GetReleaseFences failed; ret=%d", ret);
539
540 // Perform post-frame bookkeeping. Unused layers are a no-op.
541 for (size_t i = 0; i < num_elements; ++i) {
542 for (size_t j = 0; j < active_layer_count_; ++j) {
543 if (layers_[j]->GetLayerHandle() == out_layers[i]) {
544 layers_[j]->Finish(out_fences[i]);
545 }
546 }
547 }
548}
549
550// TODO(skiazyk): This is a work-around for the fact that we currently do not
551// handle the case when new surfaces are introduced when displayd is not
552// in an active state. A proper-solution will require re-structuring
553// displayd a little, but hopefully this is sufficient for now.
554// For example, could this be handled in |UpdateLayerSettings| instead?
555void HardwareComposer::UpdateDisplayState() {
556 const bool has_display_surfaces = display_surfaces_.size() > 0;
557
558 if (has_display_surfaces) {
559 int32_t ret = SetPowerMode(HWC_DISPLAY_PRIMARY, HWC2_POWER_MODE_ON);
560
561 ALOGE_IF(ret, "HardwareComposer: Could not set power mode; ret=%d", ret);
562
563 EnableVsync(true);
564 }
565 // TODO(skiazyk): We need to do something about accessing this directly,
566 // supposedly there is a backlight service on the way.
567 SetBacklightBrightness(255);
568
569 if (!display_on_ && has_display_surfaces) {
570 const int error = ReadVSyncTimestamp(&last_vsync_timestamp_);
571 ALOGE_IF(error < 0,
572 "HardwareComposer::SetDisplaySurfaces: Failed to read vsync "
573 "timestamp: %s",
574 strerror(-error));
575 }
576
577 // Trigger target-specific performance mode change.
578 property_set(kDvrPerformanceProperty, display_on_ ? "performance" : "idle");
579}
580
581int HardwareComposer::SetDisplaySurfaces(
582 std::vector<std::shared_ptr<DisplaySurface>> surfaces) {
583 std::lock_guard<std::mutex> autolock(layer_mutex_);
584
585 ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
586 surfaces.size());
587
588 // Figure out whether we need to update hardware layers. If this surface
589 // change does not add or remove hardware layers we can avoid display hiccups
590 // by gracefully updating only the GPU compositor layers.
591 // hardware_layers_need_update_ is reset to false by the Post thread.
592 int old_gpu_layer_count = 0;
593 int new_gpu_layer_count = 0;
594 // Look for new hardware layers and count new GPU layers.
595 for (const auto& surface : surfaces) {
596 if (!(surface->flags() &
597 DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION))
598 ++new_gpu_layer_count;
599 else if (std::find(display_surfaces_.begin(), display_surfaces_.end(),
600 surface) == display_surfaces_.end())
601 // This is a new hardware layer, we need to update.
602 hardware_layers_need_update_ = true;
603 }
604 // Look for deleted hardware layers or compositor layers.
605 for (const auto& surface : display_surfaces_) {
606 if (!(surface->flags() &
607 DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION))
608 ++old_gpu_layer_count;
609 else if (std::find(surfaces.begin(), surfaces.end(), surface) ==
610 surfaces.end())
611 // This is a deleted hardware layer, we need to update.
612 hardware_layers_need_update_ = true;
613 }
614 // Check for compositor hardware layer transition.
615 if ((!old_gpu_layer_count && new_gpu_layer_count) ||
616 (old_gpu_layer_count && !new_gpu_layer_count))
617 hardware_layers_need_update_ = true;
618
619 display_surfaces_ = std::move(surfaces);
620 display_surfaces_updated_ = true;
621
622 // Set the chosen layer order for all surfaces.
623 for (size_t i = 0; i < display_surfaces_.size(); ++i) {
624 display_surfaces_[i]->SetLayerOrder(static_cast<int>(i));
625 }
626
627 // TODO(skiazyk): fix this so that it is handled seamlessly with dormant/non-
628 // dormant state.
629 if (!IsSuspended()) {
630 UpdateDisplayState();
631 }
632
633 return 0;
634}
635
636// Reads the value of the display driver wait_pingpong state. Returns 0 or 1
637// (the value of the state) on success or a negative error otherwise.
638// TODO(eieio): This is pretty driver specific, this should be moved to a
639// separate class eventually.
640int HardwareComposer::ReadWaitPPState() {
641 // Gracefully handle when the kernel does not support this feature.
642 if (!primary_display_wait_pp_fd_)
643 return 0;
644
645 const int wait_pp_fd = primary_display_wait_pp_fd_.Get();
646 int ret, error;
647
648 ret = lseek(wait_pp_fd, 0, SEEK_SET);
649 if (ret < 0) {
650 error = errno;
651 ALOGE("HardwareComposer::ReadWaitPPState: Failed to seek wait_pp fd: %s",
652 strerror(error));
653 return -error;
654 }
655
656 char data = -1;
657 ret = read(wait_pp_fd, &data, sizeof(data));
658 if (ret < 0) {
659 error = errno;
660 ALOGE("HardwareComposer::ReadWaitPPState: Failed to read wait_pp state: %s",
661 strerror(error));
662 return -error;
663 }
664
665 switch (data) {
666 case '0':
667 return 0;
668 case '1':
669 return 1;
670 default:
671 ALOGE(
672 "HardwareComposer::ReadWaitPPState: Unexpected value for wait_pp: %d",
673 data);
674 return -EINVAL;
675 }
676}
677
678// Reads the timestamp of the last vsync from the display driver.
679// TODO(eieio): This is pretty driver specific, this should be moved to a
680// separate class eventually.
681int HardwareComposer::ReadVSyncTimestamp(int64_t* timestamp) {
682 const int event_fd = primary_display_vsync_event_fd_.Get();
683 int ret, error;
684
685 // The driver returns data in the form "VSYNC=<timestamp ns>".
686 std::array<char, 32> data;
687 data.fill('\0');
688
689 // Seek back to the beginning of the event file.
690 ret = lseek(event_fd, 0, SEEK_SET);
691 if (ret < 0) {
692 error = errno;
693 ALOGE(
694 "HardwareComposer::ReadVSyncTimestamp: Failed to seek vsync event fd: "
695 "%s",
696 strerror(error));
697 return -error;
698 }
699
700 // Read the vsync event timestamp.
701 ret = read(event_fd, data.data(), data.size());
702 if (ret < 0) {
703 error = errno;
704 ALOGE_IF(
705 error != EAGAIN,
706 "HardwareComposer::ReadVSyncTimestamp: Error while reading timestamp: "
707 "%s",
708 strerror(error));
709 return -error;
710 }
711
712 ret = sscanf(data.data(), "VSYNC=%" PRIu64,
713 reinterpret_cast<uint64_t*>(timestamp));
714 if (ret < 0) {
715 error = errno;
716 ALOGE(
717 "HardwareComposer::ReadVSyncTimestamp: Error while parsing timestamp: "
718 "%s",
719 strerror(error));
720 return -error;
721 }
722
723 return 0;
724}
725
726// Blocks until the next vsync event is signaled by the display driver.
727// TODO(eieio): This is pretty driver specific, this should be moved to a
728// separate class eventually.
729int HardwareComposer::BlockUntilVSync() {
730 const int event_fd = primary_display_vsync_event_fd_.Get();
731 pollfd pfd[2] = {
732 {
733 .fd = event_fd, .events = POLLPRI, .revents = 0,
734 },
735 // This extra event fd is to ensure that we can break out of this loop to
736 // pause the thread even when vsync is disabled, and thus no events on the
737 // vsync fd are being generated.
738 {
739 .fd = terminate_post_thread_event_fd_.Get(),
740 .events = POLLPRI | POLLIN,
741 .revents = 0,
742 },
743 };
744 int ret, error;
745 do {
746 ret = poll(pfd, 2, -1);
747 error = errno;
748 ALOGW_IF(ret < 0,
749 "HardwareComposer::BlockUntilVSync: Error while waiting for vsync "
750 "event: %s (%d)",
751 strerror(error), error);
752 } while (ret < 0 && error == EINTR);
753
754 return ret < 0 ? -error : 0;
755}
756
757// Waits for the next vsync and returns the timestamp of the vsync event. If
758// vsync already passed since the last call, returns the latest vsync timestamp
759// instead of blocking. This method updates the last_vsync_timeout_ in the
760// process.
761//
762// TODO(eieio): This is pretty driver specific, this should be moved to a
763// separate class eventually.
764int HardwareComposer::WaitForVSync(int64_t* timestamp) {
765 int error;
766
767 // Get the current timestamp and decide what to do.
768 while (true) {
769 int64_t current_vsync_timestamp;
770 error = ReadVSyncTimestamp(&current_vsync_timestamp);
771 if (error < 0 && error != -EAGAIN)
772 return error;
773
774 if (error == -EAGAIN) {
775 // Vsync was turned off, wait for the next vsync event.
776 error = BlockUntilVSync();
777 if (error < 0)
778 return error;
779
780 // If a request to pause the post thread was given, exit immediately
781 if (IsSuspended()) {
782 return 0;
783 }
784
785 // Try again to get the timestamp for this new vsync interval.
786 continue;
787 }
788
789 // Check that we advanced to a later vsync interval.
790 if (TimestampGT(current_vsync_timestamp, last_vsync_timestamp_)) {
791 *timestamp = last_vsync_timestamp_ = current_vsync_timestamp;
792 return 0;
793 }
794
795 // See how close we are to the next expected vsync. If we're within 1ms,
796 // sleep for 1ms and try again.
797 const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
798 const int64_t threshold_ns = 1000000;
799
800 const int64_t next_vsync_est = last_vsync_timestamp_ + ns_per_frame;
801 const int64_t distance_to_vsync_est = next_vsync_est - GetSystemClockNs();
802
803 if (distance_to_vsync_est > threshold_ns) {
804 // Wait for vsync event notification.
805 error = BlockUntilVSync();
806 if (error < 0)
807 return error;
808
809 // Again, exit immediately if the thread was requested to pause
810 if (IsSuspended()) {
811 return 0;
812 }
813 } else {
814 // Sleep for a short time before retrying.
815 std::this_thread::sleep_for(std::chrono::milliseconds(1));
816 }
817 }
818}
819
820int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
821 const int timer_fd = vsync_sleep_timer_fd_.Get();
822 const itimerspec wakeup_itimerspec = {
823 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
824 .it_value = NsToTimespec(wakeup_timestamp),
825 };
826 int ret =
827 timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
828 int error = errno;
829 if (ret < 0) {
830 ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
831 strerror(error));
832 return -error;
833 }
834
835 // Wait for the timer by reading the expiration count.
836 uint64_t expiration_count;
837 ret = read(timer_fd, &expiration_count, sizeof(expiration_count));
838 if (ret < 0) {
839 ALOGE("HardwareComposer::SleepUntil: Failed to wait for timerfd: %s",
840 strerror(error));
841 return -error;
842 }
843
844 return 0;
845}
846
847void HardwareComposer::PostThread() {
848 // NOLINTNEXTLINE(runtime/int)
849 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("PostThread"), 0, 0, 0);
850
851 std::unique_lock<std::mutex> thread_lock(thread_pause_mutex_);
852
853 // Set the scheduler to SCHED_FIFO with high priority.
854 int error = dvrSetSchedulerClass(0, "graphics:high");
855 LOG_ALWAYS_FATAL_IF(
856 error < 0,
857 "HardwareComposer::PostThread: Failed to set scheduler class: %s",
858 strerror(-error));
859 error = dvrSetCpuPartition(0, "/system/performance");
860 LOG_ALWAYS_FATAL_IF(
861 error < 0,
862 "HardwareComposer::PostThread: Failed to set cpu partition: %s",
863 strerror(-error));
864
865 // Force the layers to be setup at least once.
866 display_surfaces_updated_ = true;
867
868 // Initialize the GPU compositor.
869 LOG_ALWAYS_FATAL_IF(!compositor_.Initialize(GetHmdDisplayMetrics()),
870 "Failed to initialize the compositor");
871
872 const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
873 const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame);
874
875 // TODO(jbates) Query vblank time from device, when such an API is available.
876 // This value (6.3%) was measured on A00 in low persistence mode.
877 int64_t vblank_ns = ns_per_frame * 63 / 1000;
878 int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2;
879
880 // Check property for overriding right eye offset value.
881 right_eye_photon_offset_ns =
882 property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns);
883
884 // The list of surfaces the compositor should attempt to render. This is set
885 // at the start of each frame.
886 std::vector<std::shared_ptr<DisplaySurface>> compositor_surfaces;
887 compositor_surfaces.reserve(2);
888
889 // Our history of frame times. This is used to get a better estimate of how
890 // long the next frame will take, to set a schedule for EDS.
891 FrameTimeHistory frame_time_history;
892
893 // The backlog is used to allow us to start rendering the next frame before
894 // the previous frame has finished, and still get an accurate measurement of
895 // frame duration.
896 std::vector<FrameTimeMeasurementRecord> frame_time_backlog;
897 constexpr int kFrameTimeBacklogMax = 2;
898 frame_time_backlog.reserve(kFrameTimeBacklogMax);
899
900 // Storage for retrieving fence info.
901 FenceInfoBuffer fence_info_buffer;
902
903 while (1) {
904 ATRACE_NAME("HardwareComposer::PostThread");
905
906 while (IsSuspended()) {
907 ALOGI("HardwareComposer::PostThread: Post thread pause requested.");
908 thread_pause_semaphore_.wait(thread_lock);
909 // The layers will need to be updated since they were deleted previously
910 display_surfaces_updated_ = true;
911 hardware_layers_need_update_ = true;
912 }
913
914 int64_t vsync_timestamp = 0;
915 {
916 std::array<char, 128> buf;
917 snprintf(buf.data(), buf.size(), "wait_vsync|vsync=%d|",
918 vsync_count_ + 1);
919 ATRACE_NAME(buf.data());
920
921 error = WaitForVSync(&vsync_timestamp);
922 ALOGE_IF(
923 error < 0,
924 "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
925 strerror(-error));
926
927 // Don't bother processing this frame if a pause was requested
928 if (IsSuspended()) {
929 continue;
930 }
931 }
932
933 ++vsync_count_;
934
935 static double last_print_time = -1;
936 double current_time = GetSystemClockSec();
937 if (last_print_time < 0 || current_time - last_print_time > 3) {
938 last_print_time = current_time;
939 }
940
941 if (pose_client_) {
942 // Signal the pose service with vsync info.
943 // Display timestamp is in the middle of scanout.
944 privateDvrPoseNotifyVsync(pose_client_, vsync_count_,
945 vsync_timestamp + photon_offset_ns,
946 ns_per_frame, right_eye_photon_offset_ns);
947 }
948
949 bool layer_config_changed = UpdateLayerConfig(&compositor_surfaces);
950
951 if (layer_config_changed) {
952 frame_time_history.ResetWithSeed(
953 GuessFrameTime(compositor_surfaces.size()));
954 frame_time_backlog.clear();
955 } else {
956 UpdateFrameTimeHistory(&frame_time_backlog, kFrameTimeBacklogMax,
957 &fence_info_buffer, &frame_time_history);
958 }
959
960 // Get our current best estimate at how long the next frame will take to
961 // render, based on how long previous frames took to render. Use this
962 // estimate to decide when to wake up for EDS.
963 int64_t frame_time_estimate =
964 frame_time_history.GetSampleCount() == 0
965 ? GuessFrameTime(compositor_surfaces.size())
966 : frame_time_history.GetAverage();
967 frame_time_estimate = std::max(frame_time_estimate, kFrameTimeEstimateMin);
968 DebugHudData::data.hwc_latency = frame_time_estimate;
969
970 // Signal all of the vsync clients. Because absolute time is used for the
971 // wakeup time below, this can take a little time if necessary.
972 if (vsync_callback_)
973 vsync_callback_(HWC_DISPLAY_PRIMARY, vsync_timestamp, frame_time_estimate,
974 vsync_count_);
975
976 {
977 // Sleep until async EDS wakeup time.
978 ATRACE_NAME("sleep");
979
980 int64_t display_time_est = vsync_timestamp + ns_per_frame;
981 int64_t now = GetSystemClockNs();
982 int64_t frame_finish_time_est = now + frame_time_estimate;
983 int64_t sleep_time_ns = display_time_est - now - frame_time_estimate;
984
985 ATRACE_INT64("sleep_time_ns", sleep_time_ns);
986 if (frame_finish_time_est - display_time_est >= kFrameSkipThresholdNs) {
987 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
988 ALOGE(
989 "HardwareComposer::PostThread: Missed frame schedule, drop "
990 "frame. Expected frame miss: %.1fms",
991 static_cast<double>(frame_finish_time_est - display_time_est) /
992 1000000);
993
994 // There are several reasons we might skip a frame, but one possibility
995 // is we mispredicted the frame time. Clear out the frame time history.
996 frame_time_history.ResetWithSeed(
997 GuessFrameTime(compositor_surfaces.size()));
998 frame_time_backlog.clear();
999 DebugHudData::data.hwc_frame_stats.SkipFrame();
1000
1001 continue;
1002 } else {
1003 // Make the transition more obvious in systrace when the frame skip
1004 // happens above.
1005 ATRACE_INT("frame_skip_count", 0);
1006 }
1007
1008 if (sleep_time_ns > 0) {
1009 error = SleepUntil(display_time_est - frame_time_estimate);
1010 ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s",
1011 strerror(-error));
1012 }
1013 }
1014
1015 DebugHudData::data.hwc_frame_stats.AddFrame();
1016
1017 int64_t frame_start_time = GetSystemClockNs();
1018
1019 // Setup the output buffer for the compositor. This needs to happen before
1020 // you draw with the compositor.
1021 if (gpu_layer_ != nullptr) {
1022 gpu_layer_->UpdateDirectBuffer(compositor_.GetBuffer());
1023 }
1024
1025 // Call PostLayers now before performing the GL code for the compositor to
1026 // avoid missing the deadline that can cause the lower-level hwc to get
1027 // permanently backed up.
1028 PostLayers(layer_config_changed);
1029
1030 PostCompositorBuffers(compositor_surfaces);
1031
1032 if (gpu_layer_ != nullptr) {
1033 // Note, with scanline racing, this draw is timed along with the post
1034 // layers to finish just in time.
1035 LocalHandle frame_fence_fd;
1036 compositor_.DrawFrame(vsync_count_ + 1, &frame_fence_fd);
1037 if (frame_fence_fd) {
1038 LOG_ALWAYS_FATAL_IF(frame_time_backlog.size() >= kFrameTimeBacklogMax,
1039 "Frame time backlog exceeds capacity");
1040 frame_time_backlog.push_back(
1041 {frame_start_time, std::move(frame_fence_fd)});
1042 }
1043 } else if (!layer_config_changed) {
1044 frame_time_history.AddSample(GetSystemClockNs() - frame_start_time);
1045 }
1046
1047 HandlePendingScreenshots();
1048 }
1049
1050 // TODO(skiazyk): Currently the compositor is not fully releasing its EGL
1051 // context, which seems to prevent the thread from exiting properly.
1052 // This shouldn't be too hard to address, I just don't have time right now.
1053 compositor_.Shutdown();
1054}
1055
1056bool HardwareComposer::UpdateLayerConfig(
1057 std::vector<std::shared_ptr<DisplaySurface>>* compositor_surfaces) {
1058 std::lock_guard<std::mutex> autolock(layer_mutex_);
1059
1060 if (!display_surfaces_updated_)
1061 return false;
1062
1063 display_surfaces_updated_ = false;
1064 DebugHudData::data.ResetLayers();
1065
1066 // Update compositor layers.
1067 {
1068 ATRACE_NAME("UpdateLayerConfig_GpuLayers");
1069 compositor_.UpdateSurfaces(display_surfaces_);
1070 compositor_surfaces->clear();
1071 for (size_t i = 0; i < display_surfaces_.size(); ++i) {
1072 const auto& surface = display_surfaces_[i];
1073 if (!(surface->flags() &
1074 DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION)) {
1075 compositor_surfaces->push_back(surface);
1076 }
1077 }
1078 }
1079
1080 if (!hardware_layers_need_update_)
1081 return true;
1082
1083 // Update hardware layers.
1084
1085 ATRACE_NAME("UpdateLayerConfig_HwLayers");
1086 hardware_layers_need_update_ = false;
1087
1088 // Update the display layers in a non-destructive fashion.
1089
1090 // Create a map from surface id to hardware layer
1091 std::map<int, Layer*> display_surface_layers;
1092
1093 for (size_t i = 0; i < active_layer_count_; ++i) {
1094 auto layer = layers_[i];
1095 int surface_id = layer->GetSurfaceId();
1096
1097 auto found =
1098 std::find_if(display_surfaces_.begin(), display_surfaces_.end(),
1099 [surface_id](const auto& surface) {
1100 return surface->surface_id() == surface_id;
1101 });
1102
1103 if (found != display_surfaces_.end()) {
1104 display_surface_layers[surface_id] = layer;
1105 }
1106 }
1107
1108 bool has_gpu_layer = std::any_of(
1109 display_surfaces_.begin(), display_surfaces_.end(),
1110 [](const auto& surface) {
1111 return !(surface->flags() &
1112 DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION);
1113 });
1114
1115 if (!has_gpu_layer) {
1116 gpu_layer_ = nullptr;
1117 }
1118
1119 auto is_layer_active = [&display_surface_layers, has_gpu_layer](auto layer) {
1120 int surface_id = layer->GetSurfaceId();
1121 if (surface_id >= 0) {
1122 return display_surface_layers.count(surface_id) > 0;
1123 } else {
1124 return has_gpu_layer;
1125 }
1126 };
1127
1128 // Compress the in-use layers to the top of the list
1129 auto part = std::partition(
1130 layers_.begin(), layers_.begin() + active_layer_count_, is_layer_active);
1131
1132 size_t new_active_layer_count = part - layers_.begin();
1133
1134 // Clear any unused layers
1135 for (size_t i = new_active_layer_count; i < active_layer_count_; ++i) {
1136 layers_[i]->Reset();
1137 }
1138
1139 active_layer_count_ = new_active_layer_count;
1140
1141 bool gpu_layer_applied = false;
1142
1143 // Create/update all of the hardware layers
1144 for (size_t i = 0; i < display_surfaces_.size(); ++i) {
1145 const auto& surface = display_surfaces_[i];
1146 bool is_hw_surface =
1147 surface->flags() & DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION;
1148 hwc2_blend_mode_t blending =
1149 i == 0 ? HWC2_BLEND_MODE_NONE : HWC2_BLEND_MODE_COVERAGE;
1150
1151 DebugHudData::data.SetLayerInfo(
1152 i, surface->width(), surface->height(),
1153 !!(surface->flags() & DVR_DISPLAY_SURFACE_FLAGS_GEOMETRY_SEPARATE_2));
1154
1155 if (!is_hw_surface && gpu_layer_applied) {
1156 continue;
1157 }
1158
1159 Layer* target_layer;
1160 bool existing_layer = false;
1161
1162 if (is_hw_surface) {
1163 auto it = display_surface_layers.find(surface->surface_id());
1164
1165 if (it != display_surface_layers.end()) {
1166 target_layer = it->second;
1167 existing_layer = true;
1168 }
1169 } else if (gpu_layer_ != nullptr) {
1170 target_layer = gpu_layer_;
1171 existing_layer = true;
1172 }
1173
1174 if (!existing_layer) {
1175 if (active_layer_count_ >= kMaxHardwareLayers) {
1176 ALOGI("HardwareComposer: More than %d hardware layers requested.",
1177 kMaxHardwareLayers);
1178 break;
1179 } else {
1180 target_layer = layers_[active_layer_count_];
1181 ++active_layer_count_;
1182 }
1183
1184 ALOGD_IF(TRACE,
1185 "HardwareComposer::UpdateLayerConfig: (new) surface_id=%d -> "
1186 "layer=%zd",
1187 surface->surface_id(), i);
1188
1189 if (is_hw_surface) {
1190 target_layer->Setup(surface, blending, display_transform_,
1191 HWC2_COMPOSITION_DEVICE, i);
1192 } else {
1193 gpu_layer_ = target_layer;
1194 target_layer->Setup(compositor_.GetBuffer(), blending,
1195 display_transform_, HWC2_COMPOSITION_DEVICE, i);
1196 }
1197 } else {
1198 ALOGD_IF(TRACE,
1199 "HardwareComposer::UpdateLayerConfig: (retained) surface_id=%d "
1200 "-> layer=%zd",
1201 surface->surface_id(), i);
1202
1203 target_layer->SetBlending(blending);
1204 target_layer->SetZOrderIndex(i);
1205 target_layer->UpdateLayerSettings();
1206 }
1207
1208 gpu_layer_applied = !is_hw_surface;
1209 }
1210
1211 ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
1212 active_layer_count_);
1213
1214 return true;
1215}
1216
1217void HardwareComposer::PostCompositorBuffers(
1218 const std::vector<std::shared_ptr<DisplaySurface>>& compositor_surfaces) {
1219 ATRACE_NAME("PostCompositorBuffers");
1220 for (const auto& surface : compositor_surfaces) {
1221 compositor_.PostBuffer(surface);
1222 }
1223}
1224
1225void HardwareComposer::UpdateFrameTimeHistory(
1226 std::vector<FrameTimeMeasurementRecord>* backlog, int backlog_max,
1227 FenceInfoBuffer* fence_info_buffer, FrameTimeHistory* history) {
1228 while (!backlog->empty()) {
1229 const auto& frame_time_record = backlog->front();
1230 int64_t end_time = 0;
1231 bool frame_finished = CheckFrameFinished(frame_time_record.fence.Get(),
1232 fence_info_buffer, &end_time);
1233 if (frame_finished) {
1234 int64_t frame_duration = end_time - frame_time_record.start_time;
1235 history->AddSample(frame_duration);
1236 // Our backlog is tiny (2 elements), so erasing from the front is ok
1237 backlog->erase(backlog->begin());
1238 } else {
1239 break;
1240 }
1241 }
1242
1243 if (backlog->size() == static_cast<size_t>(backlog_max)) {
1244 // Yikes, something must've gone wrong if our oldest frame hasn't finished
1245 // yet. Give up on waiting for it.
1246 const auto& stale_frame_time_record = backlog->front();
1247 int64_t frame_duration =
1248 GetSystemClockNs() - stale_frame_time_record.start_time;
1249 backlog->erase(backlog->begin());
1250 history->AddSample(frame_duration);
1251 ALOGW("Frame didn't finish after %.1fms",
1252 static_cast<double>(frame_duration) / 1000000);
1253 }
1254}
1255
1256bool HardwareComposer::CheckFrameFinished(int frame_fence_fd,
1257 FenceInfoBuffer* fence_info_buffer,
1258 int64_t* timestamp) {
1259 int result = -1;
1260 int sync_result = sync_wait(frame_fence_fd, 0);
1261 if (sync_result == 0) {
1262 result =
1263 GetFenceSignaledTimestamp(frame_fence_fd, fence_info_buffer, timestamp);
1264 if (result < 0) {
1265 ALOGE("Failed getting signaled timestamp from fence");
1266 }
1267 } else if (errno != ETIME) {
1268 ALOGE("sync_wait on frame fence failed");
1269 }
1270 return result >= 0;
1271}
1272
1273void HardwareComposer::HandlePendingScreenshots() {
1274 // Take a screenshot of the requested layer, if available.
1275 // TODO(eieio): Look into using virtual displays to composite the layer stack
1276 // into a single output buffer that can be returned to the screenshot clients.
1277 if (active_layer_count_ > 0) {
1278 if (auto screenshot_service = ScreenshotService::GetInstance()) {
1279 if (screenshot_service->IsScreenshotRequestPending()) {
1280 ATRACE_NAME("screenshot");
1281 screenshot_service->TakeIfNeeded(layers_, compositor_);
1282 }
1283 } else {
1284 ALOGW(
1285 "HardwareComposer::HandlePendingScreenshots: Failed to get "
1286 "screenshot service!");
1287 }
1288 }
1289}
1290
1291void HardwareComposer::SetVSyncCallback(VSyncCallback callback) {
1292 vsync_callback_ = callback;
1293}
1294
1295void HardwareComposer::HwcRefresh(hwc2_callback_data_t /*data*/,
1296 hwc2_display_t /*display*/) {
1297 // TODO(eieio): implement invalidate callbacks.
1298}
1299
1300void HardwareComposer::HwcVSync(hwc2_callback_data_t /*data*/,
1301 hwc2_display_t /*display*/,
1302 int64_t /*timestamp*/) {
1303 ATRACE_NAME(__PRETTY_FUNCTION__);
1304 // Intentionally empty. HWC may require a callback to be set to enable vsync
1305 // signals. We bypass this callback thread by monitoring the vsync event
1306 // directly, but signals still need to be enabled.
1307}
1308
1309void HardwareComposer::HwcHotplug(hwc2_callback_data_t /*callbackData*/,
1310 hwc2_display_t /*display*/,
1311 hwc2_connection_t /*connected*/) {
1312 // TODO(eieio): implement display hotplug callbacks.
1313}
1314
1315void HardwareComposer::SetBacklightBrightness(int brightness) {
1316 if (backlight_brightness_fd_) {
1317 std::array<char, 32> text;
1318 const int length = snprintf(text.data(), text.size(), "%d", brightness);
1319 write(backlight_brightness_fd_.Get(), text.data(), length);
1320 }
1321}
1322
1323Layer::Layer()
1324 : hwc2_hidl_(nullptr),
1325 surface_index_(-1),
1326 hardware_composer_layer_(0),
1327 display_metrics_(nullptr),
1328 blending_(HWC2_BLEND_MODE_NONE),
1329 transform_(HWC_TRANSFORM_NONE),
1330 composition_type_(HWC2_COMPOSITION_DEVICE),
1331 surface_rect_functions_applied_(false) {}
1332
1333void Layer::Initialize(Hwc2::Composer* hwc2_hidl, HWCDisplayMetrics* metrics) {
1334 hwc2_hidl_ = hwc2_hidl;
1335 display_metrics_ = metrics;
1336}
1337
1338void Layer::Reset() {
1339 const int ret = acquired_buffer_.Release(std::move(release_fence_));
1340 ALOGE_IF(ret < 0, "Layer::Reset: failed to release buffer: %s",
1341 strerror(-ret));
1342
1343 if (hwc2_hidl_ != nullptr && hardware_composer_layer_) {
1344 hwc2_hidl_->destroyLayer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_);
1345 hardware_composer_layer_ = 0;
1346 }
1347
1348 surface_index_ = static_cast<size_t>(-1);
1349 blending_ = HWC2_BLEND_MODE_NONE;
1350 transform_ = HWC_TRANSFORM_NONE;
1351 composition_type_ = HWC2_COMPOSITION_DEVICE;
1352 direct_buffer_ = nullptr;
1353 surface_ = nullptr;
1354 acquire_fence_fd_.Close();
1355 surface_rect_functions_applied_ = false;
1356}
1357
1358void Layer::Setup(const std::shared_ptr<DisplaySurface>& surface,
1359 hwc2_blend_mode_t blending, hwc_transform_t transform,
1360 hwc2_composition_t composition_type, size_t index) {
1361 Reset();
1362 surface_index_ = index;
1363 surface_ = surface;
1364 blending_ = blending;
1365 transform_ = transform;
1366 composition_type_ = composition_type;
1367 CommonLayerSetup();
1368}
1369
1370void Layer::Setup(const std::shared_ptr<IonBuffer>& buffer,
1371 hwc2_blend_mode_t blending, hwc_transform_t transform,
1372 hwc2_composition_t composition_type, size_t z_order) {
1373 Reset();
1374 surface_index_ = z_order;
1375 direct_buffer_ = buffer;
1376 blending_ = blending;
1377 transform_ = transform;
1378 composition_type_ = composition_type;
1379 CommonLayerSetup();
1380}
1381
1382void Layer::UpdateDirectBuffer(const std::shared_ptr<IonBuffer>& buffer) {
1383 direct_buffer_ = buffer;
1384}
1385
1386void Layer::SetBlending(hwc2_blend_mode_t blending) { blending_ = blending; }
1387
1388void Layer::SetZOrderIndex(int z_index) { surface_index_ = z_index; }
1389
1390IonBuffer* Layer::GetBuffer() {
1391 if (direct_buffer_)
1392 return direct_buffer_.get();
1393 else if (acquired_buffer_.IsAvailable())
1394 return acquired_buffer_.buffer()->buffer();
1395 else
1396 return nullptr;
1397}
1398
1399void Layer::UpdateLayerSettings() {
1400 if (!IsLayerSetup()) {
1401 ALOGE("HardwareComposer: Trying to update layers data on an unused layer.");
1402 return;
1403 }
1404
1405 int32_t ret = HWC2_ERROR_NONE;
1406
1407 hwc2_display_t display = HWC_DISPLAY_PRIMARY;
1408
1409 ret = (int32_t)hwc2_hidl_->setLayerCompositionType(
1410 display, hardware_composer_layer_,
1411 (Hwc2::IComposerClient::Composition)composition_type_);
1412 ALOGE_IF(ret, "HardwareComposer: Error setting layer composition type : %d",
1413 ret);
1414 // ret = (int32_t) hwc2_hidl_->setLayerTransform(display,
1415 // hardware_composer_layer_,
1416 // (Hwc2::IComposerClient::Transform)
1417 // transform_);
1418 // ALOGE_IF(ret, "HardwareComposer: Error setting layer transform : %d", ret);
1419
1420 // ret = hwc2_funcs_->set_layer_blend_mode_fn_(
1421 // hardware_composer_device_, display, hardware_composer_layer_,
1422 // blending_);
1423 ret = (int32_t)hwc2_hidl_->setLayerBlendMode(
1424 display, hardware_composer_layer_,
1425 (Hwc2::IComposerClient::BlendMode)blending_);
1426 ALOGE_IF(ret, "HardwareComposer: Error setting layer blend mode : %d", ret);
1427
1428 Hwc2::IComposerClient::Rect display_frame;
1429 display_frame.left = 0;
1430 display_frame.top = 0;
1431 display_frame.right = display_metrics_->width;
1432 display_frame.bottom = display_metrics_->height;
1433 ret = (int32_t)hwc2_hidl_->setLayerDisplayFrame(
1434 display, hardware_composer_layer_, display_frame);
1435 ALOGE_IF(ret, "HardwareComposer: Error setting layer display frame : %d",
1436 ret);
1437
1438 std::vector<Hwc2::IComposerClient::Rect> visible_region(1);
1439 visible_region[0] = display_frame;
1440 ret = (int32_t)hwc2_hidl_->setLayerVisibleRegion(
1441 display, hardware_composer_layer_, visible_region);
1442 ALOGE_IF(ret, "HardwareComposer: Error setting layer visible region : %d",
1443 ret);
1444
1445 ret = (int32_t)hwc2_hidl_->setLayerPlaneAlpha(display,
1446 hardware_composer_layer_, 1.0f);
1447 ALOGE_IF(ret, "HardwareComposer: Error setting layer plane alpha : %d", ret);
1448
1449 ret = (int32_t)hwc2_hidl_->setLayerZOrder(display, hardware_composer_layer_,
1450 surface_index_);
1451 ALOGE_IF(ret, "HardwareComposer: Error, setting z order index : %d", ret);
1452}
1453
1454void Layer::CommonLayerSetup() {
1455 int32_t ret = (int32_t)hwc2_hidl_->createLayer(HWC_DISPLAY_PRIMARY,
1456 &hardware_composer_layer_);
1457
1458 ALOGE_IF(ret,
1459 "HardwareComposer: Failed to create layer on primary display : %d",
1460 ret);
1461
1462 UpdateLayerSettings();
1463}
1464
1465void Layer::Prepare() {
1466 int right, bottom;
1467 buffer_handle_t handle;
1468
1469 if (surface_) {
1470 // Only update the acquired buffer when one is either available or this is
1471 // the first time through.
1472 if (surface_->IsBufferAvailable()) {
1473 // If we previously set this to a solid color layer to stall for time,
1474 // revert it to a device layer.
1475 if (acquired_buffer_.IsEmpty() &&
1476 composition_type_ != HWC2_COMPOSITION_DEVICE) {
1477 composition_type_ = HWC2_COMPOSITION_DEVICE;
1478 hwc2_hidl_->setLayerCompositionType(
1479 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1480 (Hwc2::IComposerClient::Composition)HWC2_COMPOSITION_DEVICE);
1481 }
1482
1483 DebugHudData::data.AddLayerFrame(surface_index_);
1484 acquired_buffer_.Release(std::move(release_fence_));
1485 acquired_buffer_ = surface_->AcquireCurrentBuffer();
1486
1487 // Basic latency stopgap for when the application misses a frame:
1488 // If the application recovers on the 2nd or 3rd (etc) frame after
1489 // missing, this code will skip a frame to catch up by checking if
1490 // the next frame is also available.
1491 if (surface_->IsBufferAvailable()) {
1492 DebugHudData::data.SkipLayerFrame(surface_index_);
1493 ATRACE_NAME("DropToCatchUp");
1494 ATRACE_ASYNC_END("BufferPost", acquired_buffer_.buffer()->id());
1495 acquired_buffer_ = surface_->AcquireCurrentBuffer();
1496 }
1497 ATRACE_ASYNC_END("BufferPost", acquired_buffer_.buffer()->id());
1498 } else if (acquired_buffer_.IsEmpty()) {
1499 // While we are waiting for a buffer, set this to be an empty layer
1500 if (composition_type_ != HWC2_COMPOSITION_SOLID_COLOR) {
1501 composition_type_ = HWC2_COMPOSITION_SOLID_COLOR;
1502 hwc2_hidl_->setLayerCompositionType(
1503 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1504 (Hwc2::IComposerClient::Composition)HWC2_COMPOSITION_SOLID_COLOR);
1505
1506 Hwc2::IComposerClient::Color layer_color = {
1507 0, 0, 0, 0,
1508 };
1509 hwc2_hidl_->setLayerColor(HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1510 layer_color);
1511 }
1512 return;
1513 }
1514 right = acquired_buffer_.buffer()->width();
1515 bottom = acquired_buffer_.buffer()->height();
1516 handle = acquired_buffer_.buffer()->native_handle();
1517 acquire_fence_fd_.Reset(acquired_buffer_.ClaimAcquireFence().Release());
1518 } else {
1519 right = direct_buffer_->width();
1520 bottom = direct_buffer_->height();
1521 handle = direct_buffer_->handle();
1522 acquire_fence_fd_.Close();
1523 }
1524
1525 int32_t ret = HWC2_ERROR_NONE;
1526
1527 if (composition_type_ == HWC2_COMPOSITION_DEVICE) {
1528 ret = (int32_t)hwc2_hidl_->setLayerBuffer(HWC_DISPLAY_PRIMARY,
1529 hardware_composer_layer_, handle,
1530 acquire_fence_fd_.Get());
1531
1532 ALOGE_IF(ret, "HardwareComposer: Error setting layer buffer : %d", ret);
1533 }
1534
1535 if (!surface_rect_functions_applied_) {
1536 Hwc2::IComposerClient::FRect crop_rect = {
1537 0, 0, static_cast<float>(right), static_cast<float>(bottom),
1538 };
1539 hwc2_hidl_->setLayerSourceCrop(HWC_DISPLAY_PRIMARY,
1540 hardware_composer_layer_, crop_rect);
1541
1542 ALOGE_IF(ret, "HardwareComposer: Error setting layer source crop : %d",
1543 ret);
1544
1545// TODO(skiazyk): why is this ifdef'd out. Is if a driver-specific issue where
1546// it must/cannot be called?
1547#ifdef QCOM_BSP
1548 hwc_rect_t damage_rect = {
1549 0, 0, right, bottom,
1550 };
1551 hwc_region_t damage = {
1552 1, &damage_rect,
1553 };
1554 // ret = hwc2_funcs_->set_layer_surface_damage(
1555 // hardware_composer_device_, HWC_DISPLAY_PRIMARY,
1556 // hardware_composer_layer_, damage);
1557 // uses a std::vector as the listing
1558 // hwc2_hidl_->setLayerSurfaceDamage(HWC_DISPLAY_PRIMARY,
1559 // hardware_composer_layer_, vector here);
1560
1561 ALOGE_IF(ret, "HardwareComposer: Error settings layer surface damage : %d",
1562 ret);
1563#endif
1564
1565 surface_rect_functions_applied_ = true;
1566 }
1567}
1568
1569void Layer::Finish(int release_fence_fd) {
1570 release_fence_.Reset(release_fence_fd);
1571}
1572
1573void Layer::Drop() { acquire_fence_fd_.Close(); }
1574
1575} // namespace dvr
1576} // namespace android