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