hardware_composer.cpp revision 282a5ed48f64e4010f97ca4077e3bd4b54ba3268
1#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      post_thread_state_(PostThreadState::kPaused),
111      terminate_post_thread_event_fd_(-1),
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  Suspend();
128}
129
130bool HardwareComposer::Resume() {
131  std::lock_guard<std::mutex> post_thread_lock(post_thread_state_mutex_);
132  if (post_thread_state_ == PostThreadState::kRunning) {
133    return false;
134  }
135
136  std::lock_guard<std::mutex> layer_lock(layer_mutex_);
137
138  int32_t ret = HWC2_ERROR_NONE;
139
140  static const uint32_t attributes[] = {
141      HWC_DISPLAY_WIDTH, HWC_DISPLAY_HEIGHT, HWC_DISPLAY_VSYNC_PERIOD,
142      HWC_DISPLAY_DPI_X, HWC_DISPLAY_DPI_Y,  HWC_DISPLAY_NO_ATTRIBUTE,
143  };
144
145  std::vector<Hwc2::Config> configs;
146  ret = (int32_t)hwc2_hidl_->getDisplayConfigs(HWC_DISPLAY_PRIMARY, &configs);
147
148  if (ret != HWC2_ERROR_NONE) {
149    ALOGE("HardwareComposer: Failed to get display configs");
150    return false;
151  }
152
153  uint32_t num_configs = configs.size();
154
155  for (size_t i = 0; i < num_configs; i++) {
156    ALOGI("HardwareComposer: cfg[%zd/%zd] = 0x%08x", i, num_configs,
157          configs[i]);
158
159    ret = GetDisplayMetrics(HWC_DISPLAY_PRIMARY, configs[i],
160                            &native_display_metrics_);
161
162    if (ret != HWC2_ERROR_NONE) {
163      ALOGE("HardwareComposer: Failed to get display attributes %d", ret);
164      continue;
165    } else {
166      ret =
167          (int32_t)hwc2_hidl_->setActiveConfig(HWC_DISPLAY_PRIMARY, configs[i]);
168
169      if (ret != HWC2_ERROR_NONE) {
170        ALOGE("HardwareComposer: Failed to set display configuration; ret=%d",
171              ret);
172        continue;
173      }
174
175      break;
176    }
177  }
178
179  if (ret != HWC2_ERROR_NONE) {
180    ALOGE("HardwareComposer: Could not set a valid display configuration.");
181    return false;
182  }
183
184  // Set the display metrics but never use rotation to avoid the long latency of
185  // rotation processing in hwc.
186  display_transform_ = HWC_TRANSFORM_NONE;
187  display_metrics_ = native_display_metrics_;
188
189  ALOGI(
190      "HardwareComposer: primary display attributes: width=%d height=%d "
191      "vsync_period_ns=%d DPI=%dx%d",
192      native_display_metrics_.width, native_display_metrics_.height,
193      native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x,
194      native_display_metrics_.dpi.y);
195
196  // Always turn off vsync when we start.
197  EnableVsync(false);
198
199  constexpr int format = HAL_PIXEL_FORMAT_RGBA_8888;
200  constexpr int usage =
201      GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER;
202
203  framebuffer_target_ = std::make_shared<IonBuffer>(
204      native_display_metrics_.width, native_display_metrics_.height, format,
205      usage);
206
207  // Associate each Layer instance with a hardware composer layer.
208  for (auto layer : layers_) {
209    layer->Initialize(hwc2_hidl_.get(), &native_display_metrics_);
210  }
211
212#if ENABLE_BACKLIGHT_BRIGHTNESS
213  // TODO(hendrikw): This isn't required at the moment. It's possible that there
214  //                 is another method to access this when needed.
215  // Open the backlight brightness control sysfs node.
216  backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR);
217  ALOGW_IF(!backlight_brightness_fd_,
218           "HardwareComposer: Failed to open backlight brightness control: %s",
219           strerror(errno));
220#endif // ENABLE_BACKLIGHT_BRIGHTNESS
221
222  // Open the vsync event node for the primary display.
223  // TODO(eieio): Move this into a platform-specific class.
224  primary_display_vsync_event_fd_ =
225      LocalHandle(kPrimaryDisplayVSyncEventFile, O_RDONLY);
226  ALOGE_IF(!primary_display_vsync_event_fd_,
227           "HardwareComposer: Failed to open vsync event node for primary "
228           "display: %s",
229           strerror(errno));
230
231  // Open the wait pingpong status node for the primary display.
232  // TODO(eieio): Move this into a platform-specific class.
233  primary_display_wait_pp_fd_ =
234      LocalHandle(kPrimaryDisplayWaitPPEventFile, O_RDONLY);
235  ALOGE_IF(
236      !primary_display_wait_pp_fd_,
237      "HardwareComposer: Failed to open wait_pp node for primary display: %s",
238      strerror(errno));
239
240  // Create a timerfd based on CLOCK_MONOTINIC.
241  vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
242  LOG_ALWAYS_FATAL_IF(
243      !vsync_sleep_timer_fd_,
244      "HardwareComposer: Failed to create vsync sleep timerfd: %s",
245      strerror(errno));
246
247  // Connect to pose service.
248  pose_client_ = dvrPoseCreate();
249  ALOGE_IF(!pose_client_, "HardwareComposer: Failed to create pose client");
250
251  terminate_post_thread_event_fd_.Reset(
252      eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
253  LOG_ALWAYS_FATAL_IF(
254      !terminate_post_thread_event_fd_,
255      "HardwareComposer: Failed to create terminate PostThread event fd : %s",
256      strerror(errno));
257
258  post_thread_state_ = PostThreadState::kRunning;
259  post_thread_state_cond_var_.notify_all();
260
261  // If get_id() is the default thread::id object, it has not been created yet
262  if (post_thread_.get_id() == std::thread::id()) {
263    post_thread_ = std::thread(&HardwareComposer::PostThread, this);
264  } else {
265    UpdateDisplayState();
266  }
267
268  return true;
269}
270
271bool HardwareComposer::Suspend() {
272  std::unique_lock<std::mutex> post_thread_lock(post_thread_state_mutex_);
273  if (post_thread_state_ == PostThreadState::kPaused) {
274    return false;
275  }
276
277  post_thread_state_ = PostThreadState::kPauseRequested;
278
279  int error = eventfd_write(terminate_post_thread_event_fd_.Get(), 1);
280  ALOGE_IF(error,
281           "HardwareComposer::Suspend: could not write post "
282           "thread termination event fd : %d",
283           error);
284
285  post_thread_state_cond_var_.wait(
286      post_thread_lock,
287      [this] { return post_thread_state_ == PostThreadState::kPaused; });
288  terminate_post_thread_event_fd_.Close();
289
290  // Wait for any pending layer operations to finish
291  std::lock_guard<std::mutex> layer_lock(layer_mutex_);
292
293  EnableVsync(false);
294  SetPowerMode(HWC_DISPLAY_PRIMARY, HWC2_POWER_MODE_OFF);
295
296  backlight_brightness_fd_.Close();
297  primary_display_vsync_event_fd_.Close();
298  primary_display_wait_pp_fd_.Close();
299  vsync_sleep_timer_fd_.Close();
300  retire_fence_fds_.clear();
301  gpu_layer_ = nullptr;
302
303  // We have to destroy the layers before we close the hwc device
304  for (size_t i = 0; i < kMaxHardwareLayers; ++i) {
305    layers_[i]->Reset();
306  }
307
308  active_layer_count_ = 0;
309
310  framebuffer_target_.reset();
311
312  //hwc2_hidl_.reset();
313
314  if (pose_client_)
315    dvrPoseDestroy(pose_client_);
316
317  return true;
318}
319
320DisplayMetrics HardwareComposer::GetHmdDisplayMetrics() const {
321  vec2i screen_size(display_metrics_.width, display_metrics_.height);
322  DisplayOrientation orientation =
323      (display_metrics_.width > display_metrics_.height
324           ? DisplayOrientation::kLandscape
325           : DisplayOrientation::kPortrait);
326  float dpi_x = static_cast<float>(display_metrics_.dpi.x) / 1000.0f;
327  float dpi_y = static_cast<float>(display_metrics_.dpi.y) / 1000.0f;
328  float meters_per_pixel_x = kMetersPerInch / dpi_x;
329  float meters_per_pixel_y = kMetersPerInch / dpi_y;
330  vec2 meters_per_pixel(meters_per_pixel_x, meters_per_pixel_y);
331  double frame_duration_s =
332      static_cast<double>(display_metrics_.vsync_period_ns) / 1000000000.0;
333  // TODO(hendrikw): Hard coding to 3mm.  The Pixel is actually 4mm, but it
334  //                 seems that their tray to lens distance is wrong too, which
335  //                 offsets this, at least for the pixel.
336  float border_size = 0.003f;
337  return DisplayMetrics(screen_size, meters_per_pixel, border_size,
338                        static_cast<float>(frame_duration_s), orientation);
339}
340
341int32_t HardwareComposer::Validate(hwc2_display_t display) {
342  uint32_t num_types;
343  uint32_t num_requests;
344  int32_t error =
345      (int32_t)hwc2_hidl_->validateDisplay(display, &num_types, &num_requests);
346
347  if (error == HWC2_ERROR_HAS_CHANGES) {
348    // TODO(skiazyk): We might need to inspect the requested changes first, but
349    // so far it seems like we shouldn't ever hit a bad state.
350    // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_,
351    //                                               display);
352    error = (int32_t)hwc2_hidl_->acceptDisplayChanges(display);
353  }
354
355  return error;
356}
357
358int32_t HardwareComposer::EnableVsync(bool enabled) {
359  return (int32_t)hwc2_hidl_->setVsyncEnabled(
360      HWC_DISPLAY_PRIMARY,
361      (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
362                                             : HWC2_VSYNC_DISABLE));
363}
364
365int32_t HardwareComposer::Present(hwc2_display_t display) {
366  int32_t present_fence;
367  int32_t error = (int32_t)hwc2_hidl_->presentDisplay(display, &present_fence);
368
369  // According to the documentation, this fence is signaled at the time of
370  // vsync/DMA for physical displays.
371  if (error == HWC2_ERROR_NONE) {
372    ATRACE_INT("HardwareComposer: VsyncFence", present_fence);
373    retire_fence_fds_.emplace_back(present_fence);
374  } else {
375    ATRACE_INT("HardwareComposer: PresentResult", error);
376  }
377
378  return error;
379}
380
381int32_t HardwareComposer::SetPowerMode(hwc2_display_t display,
382                                       hwc2_power_mode_t mode) {
383  if (mode == HWC2_POWER_MODE_OFF) {
384    EnableVsync(false);
385  }
386
387  display_on_ = mode != HWC2_POWER_MODE_OFF;
388
389  return (int32_t)hwc2_hidl_->setPowerMode(
390      display, (Hwc2::IComposerClient::PowerMode)mode);
391}
392
393int32_t HardwareComposer::GetDisplayAttribute(hwc2_display_t display,
394                                              hwc2_config_t config,
395                                              hwc2_attribute_t attribute,
396                                              int32_t* out_value) const {
397  return (int32_t)hwc2_hidl_->getDisplayAttribute(
398      display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value);
399}
400
401int32_t HardwareComposer::GetDisplayMetrics(
402    hwc2_display_t display, hwc2_config_t config,
403    HWCDisplayMetrics* out_metrics) const {
404  int32_t ret = HWC2_ERROR_NONE;
405
406  ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_WIDTH,
407                            &out_metrics->width);
408  if (ret != HWC2_ERROR_NONE) {
409    ALOGE("HardwareComposer: Failed to get display width");
410    return ret;
411  }
412
413  ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_HEIGHT,
414                            &out_metrics->height);
415  if (ret != HWC2_ERROR_NONE) {
416    ALOGE("HardwareComposer: Failed to get display height");
417    return ret;
418  }
419
420  ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_VSYNC_PERIOD,
421                            &out_metrics->vsync_period_ns);
422  if (ret != HWC2_ERROR_NONE) {
423    ALOGE("HardwareComposer: Failed to get display height");
424    return ret;
425  }
426
427  ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_DPI_X,
428                            &out_metrics->dpi.x);
429  if (ret != HWC2_ERROR_NONE) {
430    ALOGE("HardwareComposer: Failed to get display DPI X");
431    return ret;
432  }
433
434  ret = GetDisplayAttribute(display, config, HWC2_ATTRIBUTE_DPI_Y,
435                            &out_metrics->dpi.y);
436  if (ret != HWC2_ERROR_NONE) {
437    ALOGE("HardwareComposer: Failed to get display DPI Y");
438    return ret;
439  }
440
441  return HWC2_ERROR_NONE;
442}
443
444void HardwareComposer::Dump(char* buffer, uint32_t* out_size) {
445  std::string debug_str = hwc2_hidl_->dumpDebugInfo();
446  ALOGI("%s", debug_str.c_str());
447
448  if (buffer == nullptr) {
449    *out_size = debug_str.size();
450  } else {
451    std::copy(debug_str.begin(), debug_str.begin() + *out_size, buffer);
452  }
453}
454
455// TODO(skiazyk): Figure out what to do with `is_geometry_changed`. There does
456// not seem to be any equivalent in the HWC2 API, but that doesn't mean its not
457// there.
458void HardwareComposer::PostLayers(bool /*is_geometry_changed*/) {
459  ATRACE_NAME("HardwareComposer::PostLayers");
460
461  // Setup the hardware composer layers with current buffers.
462  for (size_t i = 0; i < active_layer_count_; i++) {
463    layers_[i]->Prepare();
464  }
465
466  // Now that we have taken in a frame from the application, we have a chance
467  // to drop the frame before passing the frame along to HWC.
468  // If the display driver has become backed up, we detect it here and then
469  // react by skipping this frame to catch up latency.
470  while (!retire_fence_fds_.empty() &&
471         (!retire_fence_fds_.front() ||
472          sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
473    // There are only 2 fences in here, no performance problem to shift the
474    // array of ints.
475    retire_fence_fds_.erase(retire_fence_fds_.begin());
476  }
477
478  const bool is_frame_pending = IsFramePendingInDriver();
479  const bool is_fence_pending =
480      retire_fence_fds_.size() > kAllowedPendingFenceCount;
481
482  if (is_fence_pending || is_frame_pending) {
483    ATRACE_INT("frame_skip_count", ++frame_skip_count_);
484
485    ALOGW_IF(is_frame_pending, "Warning: frame already queued, dropping frame");
486    ALOGW_IF(is_fence_pending,
487             "Warning: dropping a frame to catch up with HWC (pending = %zd)",
488             retire_fence_fds_.size());
489
490    for (size_t i = 0; i < active_layer_count_; i++) {
491      layers_[i]->Drop();
492    }
493    return;
494  } else {
495    // Make the transition more obvious in systrace when the frame skip happens
496    // above.
497    ATRACE_INT("frame_skip_count", 0);
498  }
499
500#if TRACE
501  for (size_t i = 0; i < active_layer_count_; i++)
502    ALOGI("HardwareComposer::PostLayers: dl[%zu] ctype=0x%08x", i,
503          layers_[i]->GetCompositionType());
504#endif
505
506  int32_t ret = HWC2_ERROR_NONE;
507
508  std::vector<Hwc2::IComposerClient::Rect> full_region(1);
509  full_region[0].left = 0;
510  full_region[0].top = 0;
511  full_region[0].right = framebuffer_target_->width();
512  full_region[0].bottom = framebuffer_target_->height();
513
514  ALOGE_IF(ret, "Error setting client target : %d", ret);
515
516  ret = Validate(HWC_DISPLAY_PRIMARY);
517  if (ret) {
518    ALOGE("HardwareComposer::Validate failed; ret=%d", ret);
519    return;
520  }
521
522  ret = Present(HWC_DISPLAY_PRIMARY);
523  if (ret) {
524    ALOGE("HardwareComposer::Present failed; ret=%d", ret);
525    return;
526  }
527
528  std::vector<Hwc2::Layer> out_layers;
529  std::vector<int> out_fences;
530  ret = (int32_t)hwc2_hidl_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers,
531                                              &out_fences);
532  uint32_t num_elements = out_layers.size();
533
534  ALOGE_IF(ret, "HardwareComposer: GetReleaseFences failed; ret=%d", ret);
535
536  // Perform post-frame bookkeeping. Unused layers are a no-op.
537  for (size_t i = 0; i < num_elements; ++i) {
538    for (size_t j = 0; j < active_layer_count_; ++j) {
539      if (layers_[j]->GetLayerHandle() == out_layers[i]) {
540        layers_[j]->Finish(out_fences[i]);
541      }
542    }
543  }
544}
545
546// TODO(skiazyk): This is a work-around for the fact that we currently do not
547// handle the case when new surfaces are introduced when displayd is not
548// in an active state. A proper-solution will require re-structuring
549// displayd a little, but hopefully this is sufficient for now.
550// For example, could this be handled in |UpdateLayerSettings| instead?
551void HardwareComposer::UpdateDisplayState() {
552  const bool has_display_surfaces = display_surfaces_.size() > 0;
553
554  if (has_display_surfaces) {
555    int32_t ret = SetPowerMode(HWC_DISPLAY_PRIMARY, HWC2_POWER_MODE_ON);
556
557    ALOGE_IF(ret, "HardwareComposer: Could not set power mode; ret=%d", ret);
558
559    EnableVsync(true);
560  }
561  // TODO(skiazyk): We need to do something about accessing this directly,
562  // supposedly there is a backlight service on the way.
563  SetBacklightBrightness(255);
564
565  if (!display_on_ && has_display_surfaces) {
566    const int error = ReadVSyncTimestamp(&last_vsync_timestamp_);
567    ALOGE_IF(error < 0,
568             "HardwareComposer::SetDisplaySurfaces: Failed to read vsync "
569             "timestamp: %s",
570             strerror(-error));
571  }
572
573  // Trigger target-specific performance mode change.
574  property_set(kDvrPerformanceProperty, display_on_ ? "performance" : "idle");
575}
576
577int HardwareComposer::SetDisplaySurfaces(
578    std::vector<std::shared_ptr<DisplaySurface>> surfaces) {
579  // The double lock is necessary because we access both the display surfaces
580  // and post_thread_state_.
581  std::lock_guard<std::mutex> post_thread_state_lock(post_thread_state_mutex_);
582  std::lock_guard<std::mutex> layer_lock(layer_mutex_);
583
584  ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
585        surfaces.size());
586
587  // Figure out whether we need to update hardware layers. If this surface
588  // change does not add or remove hardware layers we can avoid display hiccups
589  // by gracefully updating only the GPU compositor layers.
590  // hardware_layers_need_update_ is reset to false by the Post thread.
591  int old_gpu_layer_count = 0;
592  int new_gpu_layer_count = 0;
593  // Look for new hardware layers and count new GPU layers.
594  for (const auto& surface : surfaces) {
595    if (!(surface->flags() &
596          DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION))
597      ++new_gpu_layer_count;
598    else if (std::find(display_surfaces_.begin(), display_surfaces_.end(),
599                       surface) == display_surfaces_.end())
600      // This is a new hardware layer, we need to update.
601      hardware_layers_need_update_ = true;
602  }
603  // Look for deleted hardware layers or compositor layers.
604  for (const auto& surface : display_surfaces_) {
605    if (!(surface->flags() &
606          DVR_DISPLAY_SURFACE_FLAGS_DISABLE_SYSTEM_DISTORTION))
607      ++old_gpu_layer_count;
608    else if (std::find(surfaces.begin(), surfaces.end(), surface) ==
609             surfaces.end())
610      // This is a deleted hardware layer, we need to update.
611      hardware_layers_need_update_ = true;
612  }
613  // Check for compositor hardware layer transition.
614  if ((!old_gpu_layer_count && new_gpu_layer_count) ||
615      (old_gpu_layer_count && !new_gpu_layer_count))
616    hardware_layers_need_update_ = true;
617
618  display_surfaces_ = std::move(surfaces);
619  display_surfaces_updated_ = true;
620
621  // Set the chosen layer order for all surfaces.
622  for (size_t i = 0; i < display_surfaces_.size(); ++i) {
623    display_surfaces_[i]->SetLayerOrder(static_cast<int>(i));
624  }
625
626  // TODO(skiazyk): fix this so that it is handled seamlessly with dormant/non-
627  // dormant state.
628  if (post_thread_state_ == PostThreadState::kRunning) {
629    UpdateDisplayState();
630  }
631
632  return 0;
633}
634
635// Reads the value of the display driver wait_pingpong state. Returns 0 or 1
636// (the value of the state) on success or a negative error otherwise.
637// TODO(eieio): This is pretty driver specific, this should be moved to a
638// separate class eventually.
639int HardwareComposer::ReadWaitPPState() {
640  // Gracefully handle when the kernel does not support this feature.
641  if (!primary_display_wait_pp_fd_)
642    return 0;
643
644  const int wait_pp_fd = primary_display_wait_pp_fd_.Get();
645  int ret, error;
646
647  ret = lseek(wait_pp_fd, 0, SEEK_SET);
648  if (ret < 0) {
649    error = errno;
650    ALOGE("HardwareComposer::ReadWaitPPState: Failed to seek wait_pp fd: %s",
651          strerror(error));
652    return -error;
653  }
654
655  char data = -1;
656  ret = read(wait_pp_fd, &data, sizeof(data));
657  if (ret < 0) {
658    error = errno;
659    ALOGE("HardwareComposer::ReadWaitPPState: Failed to read wait_pp state: %s",
660          strerror(error));
661    return -error;
662  }
663
664  switch (data) {
665    case '0':
666      return 0;
667    case '1':
668      return 1;
669    default:
670      ALOGE(
671          "HardwareComposer::ReadWaitPPState: Unexpected value for wait_pp: %d",
672          data);
673      return -EINVAL;
674  }
675}
676
677// Reads the timestamp of the last vsync from the display driver.
678// TODO(eieio): This is pretty driver specific, this should be moved to a
679// separate class eventually.
680int HardwareComposer::ReadVSyncTimestamp(int64_t* timestamp) {
681  const int event_fd = primary_display_vsync_event_fd_.Get();
682  int ret, error;
683
684  // The driver returns data in the form "VSYNC=<timestamp ns>".
685  std::array<char, 32> data;
686  data.fill('\0');
687
688  // Seek back to the beginning of the event file.
689  ret = lseek(event_fd, 0, SEEK_SET);
690  if (ret < 0) {
691    error = errno;
692    ALOGE(
693        "HardwareComposer::ReadVSyncTimestamp: Failed to seek vsync event fd: "
694        "%s",
695        strerror(error));
696    return -error;
697  }
698
699  // Read the vsync event timestamp.
700  ret = read(event_fd, data.data(), data.size());
701  if (ret < 0) {
702    error = errno;
703    ALOGE_IF(
704        error != EAGAIN,
705        "HardwareComposer::ReadVSyncTimestamp: Error while reading timestamp: "
706        "%s",
707        strerror(error));
708    return -error;
709  }
710
711  ret = sscanf(data.data(), "VSYNC=%" PRIu64,
712               reinterpret_cast<uint64_t*>(timestamp));
713  if (ret < 0) {
714    error = errno;
715    ALOGE(
716        "HardwareComposer::ReadVSyncTimestamp: Error while parsing timestamp: "
717        "%s",
718        strerror(error));
719    return -error;
720  }
721
722  return 0;
723}
724
725// Blocks until the next vsync event is signaled by the display driver.
726// TODO(eieio): This is pretty driver specific, this should be moved to a
727// separate class eventually.
728int HardwareComposer::BlockUntilVSync(/*out*/ bool* suspend_requested) {
729  *suspend_requested = false;
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  if (ret >= 0 && pfd[1].revents != 0)
755    *suspend_requested = true;
756  return ret < 0 ? -error : 0;
757}
758
759// Waits for the next vsync and returns the timestamp of the vsync event. If
760// vsync already passed since the last call, returns the latest vsync timestamp
761// instead of blocking. This method updates the last_vsync_timeout_ in the
762// process.
763//
764// TODO(eieio): This is pretty driver specific, this should be moved to a
765// separate class eventually.
766int HardwareComposer::WaitForVSync(int64_t* timestamp) {
767  int error;
768
769  // Get the current timestamp and decide what to do.
770  while (true) {
771    int64_t current_vsync_timestamp;
772    error = ReadVSyncTimestamp(&current_vsync_timestamp);
773    if (error < 0 && error != -EAGAIN)
774      return error;
775
776    if (error == -EAGAIN) {
777      // Vsync was turned off, wait for the next vsync event.
778      bool suspend_requested = false;
779      error = BlockUntilVSync(&suspend_requested);
780      if (error < 0 || suspend_requested)
781        return error;
782
783      // Try again to get the timestamp for this new vsync interval.
784      continue;
785    }
786
787    // Check that we advanced to a later vsync interval.
788    if (TimestampGT(current_vsync_timestamp, last_vsync_timestamp_)) {
789      *timestamp = last_vsync_timestamp_ = current_vsync_timestamp;
790      return 0;
791    }
792
793    // See how close we are to the next expected vsync. If we're within 1ms,
794    // sleep for 1ms and try again.
795    const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
796    const int64_t threshold_ns = 1000000;
797
798    const int64_t next_vsync_est = last_vsync_timestamp_ + ns_per_frame;
799    const int64_t distance_to_vsync_est = next_vsync_est - GetSystemClockNs();
800
801    if (distance_to_vsync_est > threshold_ns) {
802      // Wait for vsync event notification.
803      bool suspend_requested = false;
804      error = BlockUntilVSync(&suspend_requested);
805      if (error < 0 || suspend_requested)
806        return error;
807    } else {
808      // Sleep for a short time before retrying.
809      std::this_thread::sleep_for(std::chrono::milliseconds(1));
810    }
811  }
812}
813
814int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
815  const int timer_fd = vsync_sleep_timer_fd_.Get();
816  const itimerspec wakeup_itimerspec = {
817      .it_interval = {.tv_sec = 0, .tv_nsec = 0},
818      .it_value = NsToTimespec(wakeup_timestamp),
819  };
820  int ret =
821      timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
822  int error = errno;
823  if (ret < 0) {
824    ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
825          strerror(error));
826    return -error;
827  }
828
829  // Wait for the timer by reading the expiration count.
830  uint64_t expiration_count;
831  ret = read(timer_fd, &expiration_count, sizeof(expiration_count));
832  if (ret < 0) {
833    ALOGE("HardwareComposer::SleepUntil: Failed to wait for timerfd: %s",
834          strerror(error));
835    return -error;
836  }
837
838  return 0;
839}
840
841void HardwareComposer::PostThread() {
842  // NOLINTNEXTLINE(runtime/int)
843  prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("PostThread"), 0, 0, 0);
844
845  // Set the scheduler to SCHED_FIFO with high priority.
846  int error = dvrSetSchedulerClass(0, "graphics:high");
847  LOG_ALWAYS_FATAL_IF(
848      error < 0,
849      "HardwareComposer::PostThread: Failed to set scheduler class: %s",
850      strerror(-error));
851  error = dvrSetCpuPartition(0, "/system/performance");
852  LOG_ALWAYS_FATAL_IF(
853      error < 0,
854      "HardwareComposer::PostThread: Failed to set cpu partition: %s",
855      strerror(-error));
856
857  // Force the layers to be setup at least once.
858  display_surfaces_updated_ = true;
859
860  // Initialize the GPU compositor.
861  LOG_ALWAYS_FATAL_IF(!compositor_.Initialize(GetHmdDisplayMetrics()),
862                      "Failed to initialize the compositor");
863
864  const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
865  const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame);
866
867  // TODO(jbates) Query vblank time from device, when such an API is available.
868  // This value (6.3%) was measured on A00 in low persistence mode.
869  int64_t vblank_ns = ns_per_frame * 63 / 1000;
870  int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2;
871
872  // Check property for overriding right eye offset value.
873  right_eye_photon_offset_ns =
874      property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns);
875
876  // The list of surfaces the compositor should attempt to render. This is set
877  // at the start of each frame.
878  std::vector<std::shared_ptr<DisplaySurface>> compositor_surfaces;
879  compositor_surfaces.reserve(2);
880
881  // Our history of frame times. This is used to get a better estimate of how
882  // long the next frame will take, to set a schedule for EDS.
883  FrameTimeHistory frame_time_history;
884
885  // The backlog is used to allow us to start rendering the next frame before
886  // the previous frame has finished, and still get an accurate measurement of
887  // frame duration.
888  std::vector<FrameTimeMeasurementRecord> frame_time_backlog;
889  constexpr int kFrameTimeBacklogMax = 2;
890  frame_time_backlog.reserve(kFrameTimeBacklogMax);
891
892  // Storage for retrieving fence info.
893  FenceInfoBuffer fence_info_buffer;
894
895  while (1) {
896    ATRACE_NAME("HardwareComposer::PostThread");
897
898    {
899      std::unique_lock<std::mutex> post_thread_lock(post_thread_state_mutex_);
900      if (post_thread_state_ == PostThreadState::kPauseRequested) {
901        ALOGI("HardwareComposer::PostThread: Post thread pause requested.");
902        post_thread_state_ = PostThreadState::kPaused;
903        post_thread_state_cond_var_.notify_all();
904        post_thread_state_cond_var_.wait(
905            post_thread_lock,
906            [this] { return post_thread_state_ == PostThreadState::kRunning; });
907        // The layers will need to be updated since they were deleted previously
908        display_surfaces_updated_ = true;
909        hardware_layers_need_update_ = true;
910      }
911    }
912
913    int64_t vsync_timestamp = 0;
914    {
915      std::array<char, 128> buf;
916      snprintf(buf.data(), buf.size(), "wait_vsync|vsync=%d|",
917               vsync_count_ + 1);
918      ATRACE_NAME(buf.data());
919
920      error = WaitForVSync(&vsync_timestamp);
921      ALOGE_IF(
922          error < 0,
923          "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
924          strerror(-error));
925
926      // Don't bother processing this frame if a pause was requested
927      std::lock_guard<std::mutex> post_thread_lock(post_thread_state_mutex_);
928      if (post_thread_state_ == PostThreadState::kPauseRequested) {
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> layer_lock(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
1577