blob: 70f303b2085ff84531fe0ad2e1916a187797c155 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#include "hardware_composer.h"
2
Steven Thomasdfde8fa2018-04-19 16:00:58 -07003#include <binder/IServiceManager.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08004#include <cutils/properties.h>
5#include <cutils/sched_policy.h>
6#include <fcntl.h>
Corey Tabaka2251d822017-04-20 16:04:07 -07007#include <log/log.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08008#include <poll.h>
Corey Tabakab3732f02017-09-16 00:58:54 -07009#include <stdint.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080010#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>
Corey Tabakab3732f02017-09-16 00:58:54 -070016#include <sys/types.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070017#include <time.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080018#include <unistd.h>
19#include <utils/Trace.h>
20
21#include <algorithm>
Corey Tabaka2251d822017-04-20 16:04:07 -070022#include <chrono>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080023#include <functional>
24#include <map>
Corey Tabaka0b485c92017-05-19 12:02:58 -070025#include <sstream>
26#include <string>
John Bates954796e2017-05-11 11:00:31 -070027#include <tuple>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080028
Corey Tabaka2251d822017-04-20 16:04:07 -070029#include <dvr/dvr_display_types.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080030#include <dvr/performance_client_api.h>
31#include <private/dvr/clock_ns.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070032#include <private/dvr/ion_buffer.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080033
Steven Thomasb02664d2017-07-26 18:48:28 -070034using android::hardware::Return;
35using android::hardware::Void;
Corey Tabakab3732f02017-09-16 00:58:54 -070036using android::pdx::ErrorStatus;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080037using android::pdx::LocalHandle;
Corey Tabakab3732f02017-09-16 00:58:54 -070038using android::pdx::Status;
Corey Tabaka2251d822017-04-20 16:04:07 -070039using android::pdx::rpc::EmptyVariant;
40using android::pdx::rpc::IfAnyOf;
41
42using namespace std::chrono_literals;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080043
44namespace android {
45namespace dvr {
46
47namespace {
48
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080049const char kDvrPerformanceProperty[] = "sys.dvr.performance";
50
Luke Song4b788322017-03-24 14:17:31 -070051const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns";
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080052
Steven Thomasdfde8fa2018-04-19 16:00:58 -070053// Surface flinger uses "VSYNC-sf" and "VSYNC-app" for its version of these
54// events. Name ours similarly.
55const char kVsyncTraceEventName[] = "VSYNC-vrflinger";
56
Steven Thomasaf336272018-01-04 17:36:47 -080057// How long to wait after boot finishes before we turn the display off.
58constexpr int kBootFinishedDisplayOffTimeoutSec = 10;
59
Steven Thomasbfe46a02018-02-16 14:27:35 -080060constexpr int kDefaultDisplayWidth = 1920;
61constexpr int kDefaultDisplayHeight = 1080;
62constexpr int64_t kDefaultVsyncPeriodNs = 16666667;
63// Hardware composer reports dpi as dots per thousand inches (dpi * 1000).
64constexpr int kDefaultDpi = 400000;
65
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080066// Get time offset from a vsync to when the pose for that vsync should be
67// predicted out to. For example, if scanout gets halfway through the frame
68// at the halfway point between vsyncs, then this could be half the period.
69// With global shutter displays, this should be changed to the offset to when
70// illumination begins. Low persistence adds a frame of latency, so we predict
71// to the center of the next frame.
72inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
73 return (vsync_period_ns * 150) / 100;
74}
75
Corey Tabaka2251d822017-04-20 16:04:07 -070076// Attempts to set the scheduler class and partiton for the current thread.
77// Returns true on success or false on failure.
78bool SetThreadPolicy(const std::string& scheduler_class,
79 const std::string& partition) {
80 int error = dvrSetSchedulerClass(0, scheduler_class.c_str());
81 if (error < 0) {
82 ALOGE(
83 "SetThreadPolicy: Failed to set scheduler class \"%s\" for "
84 "thread_id=%d: %s",
85 scheduler_class.c_str(), gettid(), strerror(-error));
86 return false;
87 }
88 error = dvrSetCpuPartition(0, partition.c_str());
89 if (error < 0) {
90 ALOGE(
91 "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: "
92 "%s",
93 partition.c_str(), gettid(), strerror(-error));
94 return false;
95 }
96 return true;
97}
98
Corey Tabakab3732f02017-09-16 00:58:54 -070099// Utility to generate scoped tracers with arguments.
100// TODO(eieio): Move/merge this into utils/Trace.h?
101class TraceArgs {
102 public:
103 template <typename... Args>
Chih-Hung Hsieh79e7f1b2018-12-20 15:53:43 -0800104 explicit TraceArgs(const char* format, Args&&... args) {
Corey Tabakab3732f02017-09-16 00:58:54 -0700105 std::array<char, 1024> buffer;
106 snprintf(buffer.data(), buffer.size(), format, std::forward<Args>(args)...);
107 atrace_begin(ATRACE_TAG, buffer.data());
108 }
109
110 ~TraceArgs() { atrace_end(ATRACE_TAG); }
111
112 private:
113 TraceArgs(const TraceArgs&) = delete;
114 void operator=(const TraceArgs&) = delete;
115};
116
117// Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
118// defined in utils/Trace.h.
119#define TRACE_FORMAT(format, ...) \
120 TraceArgs PASTE(__tracer, __LINE__) { format, ##__VA_ARGS__ }
121
Steven Thomasbfe46a02018-02-16 14:27:35 -0800122// Returns "primary" or "external". Useful for writing more readable logs.
123const char* GetDisplayName(bool is_primary) {
124 return is_primary ? "primary" : "external";
125}
126
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800127} // anonymous namespace
128
Corey Tabaka2251d822017-04-20 16:04:07 -0700129HardwareComposer::HardwareComposer()
Steven Thomasb02664d2017-07-26 18:48:28 -0700130 : initialized_(false), request_display_callback_(nullptr) {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800131
132HardwareComposer::~HardwareComposer(void) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700133 UpdatePostThreadState(PostThreadState::Quit, true);
134 if (post_thread_.joinable())
Steven Thomas050b2c82017-03-06 11:45:16 -0800135 post_thread_.join();
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700136 composer_callback_->SetVsyncService(nullptr);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800137}
138
mamik913cc132019-09-13 14:58:43 -0700139void HardwareComposer::UpdateEdidData(Hwc2::Composer* composer,
140 hwc2_display_t hw_id) {
141 const auto error = composer->getDisplayIdentificationData(
142 hw_id, &display_port_, &display_identification_data_);
143 if (error != android::hardware::graphics::composer::V2_1::Error::NONE) {
144 if (error !=
145 android::hardware::graphics::composer::V2_1::Error::UNSUPPORTED) {
146 ALOGI("hardware_composer: identification data error\n");
147 } else {
148 ALOGI("hardware_composer: identification data unsupported\n");
149 }
150 }
151}
152
Steven Thomasb02664d2017-07-26 18:48:28 -0700153bool HardwareComposer::Initialize(
Steven Thomas6e8f7062017-11-22 14:15:29 -0800154 Hwc2::Composer* composer, hwc2_display_t primary_display_id,
155 RequestDisplayCallback request_display_callback) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800156 if (initialized_) {
157 ALOGE("HardwareComposer::Initialize: already initialized.");
158 return false;
159 }
160
Steven Thomasb02664d2017-07-26 18:48:28 -0700161 request_display_callback_ = request_display_callback;
162
Steven Thomasbfe46a02018-02-16 14:27:35 -0800163 primary_display_ = GetDisplayParams(composer, primary_display_id, true);
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700164
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700165 vsync_service_ = new VsyncService;
166 sp<IServiceManager> sm(defaultServiceManager());
167 auto result = sm->addService(String16(VsyncService::GetServiceName()),
168 vsync_service_, false);
169 LOG_ALWAYS_FATAL_IF(result != android::OK,
170 "addService(%s) failed", VsyncService::GetServiceName());
171
Corey Tabaka2251d822017-04-20 16:04:07 -0700172 post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
Steven Thomas050b2c82017-03-06 11:45:16 -0800173 LOG_ALWAYS_FATAL_IF(
Corey Tabaka2251d822017-04-20 16:04:07 -0700174 !post_thread_event_fd_,
Steven Thomas050b2c82017-03-06 11:45:16 -0800175 "HardwareComposer: Failed to create interrupt event fd : %s",
176 strerror(errno));
177
mamik913cc132019-09-13 14:58:43 -0700178 UpdateEdidData(composer, primary_display_id);
179
Steven Thomas050b2c82017-03-06 11:45:16 -0800180 post_thread_ = std::thread(&HardwareComposer::PostThread, this);
181
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800182 initialized_ = true;
183
184 return initialized_;
185}
186
Steven Thomas050b2c82017-03-06 11:45:16 -0800187void HardwareComposer::Enable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700188 UpdatePostThreadState(PostThreadState::Suspended, false);
Steven Thomas050b2c82017-03-06 11:45:16 -0800189}
190
191void HardwareComposer::Disable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700192 UpdatePostThreadState(PostThreadState::Suspended, true);
Steven Thomas7f8761e2018-01-18 18:49:59 -0800193
194 std::unique_lock<std::mutex> lock(post_thread_mutex_);
195 post_thread_ready_.wait(lock, [this] {
196 return !post_thread_resumed_;
197 });
Steven Thomas050b2c82017-03-06 11:45:16 -0800198}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800199
Steven Thomasaf336272018-01-04 17:36:47 -0800200void HardwareComposer::OnBootFinished() {
201 std::lock_guard<std::mutex> lock(post_thread_mutex_);
202 if (boot_finished_)
203 return;
204 boot_finished_ = true;
205 post_thread_wait_.notify_one();
Steven Thomasaf336272018-01-04 17:36:47 -0800206}
207
Corey Tabaka2251d822017-04-20 16:04:07 -0700208// Update the post thread quiescent state based on idle and suspended inputs.
209void HardwareComposer::UpdatePostThreadState(PostThreadStateType state,
210 bool suspend) {
211 std::unique_lock<std::mutex> lock(post_thread_mutex_);
212
213 // Update the votes in the state variable before evaluating the effective
214 // quiescent state. Any bits set in post_thread_state_ indicate that the post
215 // thread should be suspended.
216 if (suspend) {
217 post_thread_state_ |= state;
218 } else {
219 post_thread_state_ &= ~state;
220 }
221
222 const bool quit = post_thread_state_ & PostThreadState::Quit;
223 const bool effective_suspend = post_thread_state_ != PostThreadState::Active;
224 if (quit) {
225 post_thread_quiescent_ = true;
226 eventfd_write(post_thread_event_fd_.Get(), 1);
227 post_thread_wait_.notify_one();
228 } else if (effective_suspend && !post_thread_quiescent_) {
229 post_thread_quiescent_ = true;
230 eventfd_write(post_thread_event_fd_.Get(), 1);
231 } else if (!effective_suspend && post_thread_quiescent_) {
232 post_thread_quiescent_ = false;
233 eventfd_t value;
234 eventfd_read(post_thread_event_fd_.Get(), &value);
235 post_thread_wait_.notify_one();
236 }
Steven Thomas050b2c82017-03-06 11:45:16 -0800237}
Steven Thomas282a5ed2017-02-07 18:07:01 -0800238
Steven Thomasbfe46a02018-02-16 14:27:35 -0800239void HardwareComposer::CreateComposer() {
240 if (composer_)
241 return;
242 composer_.reset(new Hwc2::impl::Composer("default"));
243 composer_callback_ = new ComposerCallback;
244 composer_->registerCallback(composer_callback_);
245 LOG_ALWAYS_FATAL_IF(!composer_callback_->GotFirstHotplug(),
246 "Registered composer callback but didn't get hotplug for primary"
247 " display");
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700248 composer_callback_->SetVsyncService(vsync_service_);
Steven Thomasbfe46a02018-02-16 14:27:35 -0800249}
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700250
Steven Thomasbfe46a02018-02-16 14:27:35 -0800251void HardwareComposer::OnPostThreadResumed() {
252 ALOGI("OnPostThreadResumed");
253 EnableDisplay(*target_display_, true);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800254
Steven Thomas050b2c82017-03-06 11:45:16 -0800255 // Trigger target-specific performance mode change.
256 property_set(kDvrPerformanceProperty, "performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800257}
258
Steven Thomas050b2c82017-03-06 11:45:16 -0800259void HardwareComposer::OnPostThreadPaused() {
Steven Thomasbfe46a02018-02-16 14:27:35 -0800260 ALOGI("OnPostThreadPaused");
Corey Tabaka2251d822017-04-20 16:04:07 -0700261 retire_fence_fds_.clear();
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700262 layers_.clear();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800263
Steven Thomasbfe46a02018-02-16 14:27:35 -0800264 // Phones create a new composer client on resume and destroy it on pause.
Inseob Kim15791ea2020-07-31 19:29:32 +0900265 if (composer_callback_ != nullptr) {
266 composer_callback_->SetVsyncService(nullptr);
267 composer_callback_ = nullptr;
Corey Tabaka451256f2017-08-22 11:59:15 -0700268 }
Inseob Kim15791ea2020-07-31 19:29:32 +0900269 composer_.reset(nullptr);
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700270
Steven Thomas050b2c82017-03-06 11:45:16 -0800271 // Trigger target-specific performance mode change.
272 property_set(kDvrPerformanceProperty, "idle");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800273}
274
Steven Thomasaf336272018-01-04 17:36:47 -0800275bool HardwareComposer::PostThreadCondWait(std::unique_lock<std::mutex>& lock,
276 int timeout_sec,
277 const std::function<bool()>& pred) {
278 auto pred_with_quit = [&] {
279 return pred() || (post_thread_state_ & PostThreadState::Quit);
280 };
281 if (timeout_sec >= 0) {
282 post_thread_wait_.wait_for(lock, std::chrono::seconds(timeout_sec),
283 pred_with_quit);
284 } else {
285 post_thread_wait_.wait(lock, pred_with_quit);
286 }
287 if (post_thread_state_ & PostThreadState::Quit) {
288 ALOGI("HardwareComposer::PostThread: Quitting.");
289 return true;
290 }
291 return false;
292}
293
Corey Tabaka2251d822017-04-20 16:04:07 -0700294HWC::Error HardwareComposer::Validate(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800295 uint32_t num_types;
296 uint32_t num_requests;
Corey Tabaka2251d822017-04-20 16:04:07 -0700297 HWC::Error error =
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700298 composer_->validateDisplay(display, &num_types, &num_requests);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800299
300 if (error == HWC2_ERROR_HAS_CHANGES) {
Steven Thomasbfe46a02018-02-16 14:27:35 -0800301 ALOGE("Hardware composer has requested composition changes, "
302 "which we don't support.");
303 // Accept the changes anyway and see if we can get something on the screen.
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700304 error = composer_->acceptDisplayChanges(display);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800305 }
306
307 return error;
308}
309
Steven Thomasbfe46a02018-02-16 14:27:35 -0800310bool HardwareComposer::EnableVsync(const DisplayParams& display, bool enabled) {
311 HWC::Error error = composer_->setVsyncEnabled(display.id,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800312 (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
313 : HWC2_VSYNC_DISABLE));
Steven Thomasbfe46a02018-02-16 14:27:35 -0800314 if (error != HWC::Error::None) {
315 ALOGE("Error attempting to %s vsync on %s display: %s",
316 enabled ? "enable" : "disable", GetDisplayName(display.is_primary),
317 error.to_string().c_str());
318 }
319 return error == HWC::Error::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800320}
321
Steven Thomasbfe46a02018-02-16 14:27:35 -0800322bool HardwareComposer::SetPowerMode(const DisplayParams& display, bool active) {
323 ALOGI("Turning %s display %s", GetDisplayName(display.is_primary),
324 active ? "on" : "off");
Corey Tabaka451256f2017-08-22 11:59:15 -0700325 HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off;
Steven Thomasbfe46a02018-02-16 14:27:35 -0800326 HWC::Error error = composer_->setPowerMode(display.id,
Steven Thomas6e8f7062017-11-22 14:15:29 -0800327 power_mode.cast<Hwc2::IComposerClient::PowerMode>());
Steven Thomasbfe46a02018-02-16 14:27:35 -0800328 if (error != HWC::Error::None) {
329 ALOGE("Error attempting to turn %s display %s: %s",
330 GetDisplayName(display.is_primary), active ? "on" : "off",
331 error.to_string().c_str());
332 }
333 return error == HWC::Error::None;
334}
335
336bool HardwareComposer::EnableDisplay(const DisplayParams& display,
337 bool enabled) {
338 bool power_result;
339 bool vsync_result;
340 // When turning a display on, we set the power state then set vsync. When
341 // turning a display off we do it in the opposite order.
342 if (enabled) {
343 power_result = SetPowerMode(display, enabled);
344 vsync_result = EnableVsync(display, enabled);
345 } else {
346 vsync_result = EnableVsync(display, enabled);
347 power_result = SetPowerMode(display, enabled);
348 }
349 return power_result && vsync_result;
Corey Tabaka451256f2017-08-22 11:59:15 -0700350}
351
Corey Tabaka2251d822017-04-20 16:04:07 -0700352HWC::Error HardwareComposer::Present(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800353 int32_t present_fence;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700354 HWC::Error error = composer_->presentDisplay(display, &present_fence);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800355
356 // According to the documentation, this fence is signaled at the time of
357 // vsync/DMA for physical displays.
Corey Tabaka2251d822017-04-20 16:04:07 -0700358 if (error == HWC::Error::None) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800359 retire_fence_fds_.emplace_back(present_fence);
360 } else {
361 ATRACE_INT("HardwareComposer: PresentResult", error);
362 }
363
364 return error;
365}
366
Steven Thomasbfe46a02018-02-16 14:27:35 -0800367DisplayParams HardwareComposer::GetDisplayParams(
368 Hwc2::Composer* composer, hwc2_display_t display, bool is_primary) {
369 DisplayParams params;
370 params.id = display;
371 params.is_primary = is_primary;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800372
Steven Thomasbfe46a02018-02-16 14:27:35 -0800373 Hwc2::Config config;
374 HWC::Error error = composer->getActiveConfig(display, &config);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800375
Steven Thomasbfe46a02018-02-16 14:27:35 -0800376 if (error == HWC::Error::None) {
377 auto get_attr = [&](hwc2_attribute_t attr, const char* attr_name)
378 -> std::optional<int32_t> {
379 int32_t val;
380 HWC::Error error = composer->getDisplayAttribute(
381 display, config, (Hwc2::IComposerClient::Attribute)attr, &val);
382 if (error != HWC::Error::None) {
383 ALOGE("Failed to get %s display attr %s: %s",
384 GetDisplayName(is_primary), attr_name,
385 error.to_string().c_str());
386 return std::nullopt;
387 }
388 return val;
389 };
390
391 auto width = get_attr(HWC2_ATTRIBUTE_WIDTH, "width");
392 auto height = get_attr(HWC2_ATTRIBUTE_HEIGHT, "height");
393
394 if (width && height) {
395 params.width = *width;
396 params.height = *height;
397 } else {
398 ALOGI("Failed to get width and/or height for %s display. Using default"
399 " size %dx%d.", GetDisplayName(is_primary), kDefaultDisplayWidth,
400 kDefaultDisplayHeight);
401 params.width = kDefaultDisplayWidth;
402 params.height = kDefaultDisplayHeight;
403 }
404
405 auto vsync_period = get_attr(HWC2_ATTRIBUTE_VSYNC_PERIOD, "vsync period");
406 if (vsync_period) {
407 params.vsync_period_ns = *vsync_period;
408 } else {
409 ALOGI("Failed to get vsync period for %s display. Using default vsync"
410 " period %.2fms", GetDisplayName(is_primary),
411 static_cast<float>(kDefaultVsyncPeriodNs) / 1000000);
412 params.vsync_period_ns = kDefaultVsyncPeriodNs;
413 }
414
415 auto dpi_x = get_attr(HWC2_ATTRIBUTE_DPI_X, "DPI X");
416 auto dpi_y = get_attr(HWC2_ATTRIBUTE_DPI_Y, "DPI Y");
417 if (dpi_x && dpi_y) {
418 params.dpi.x = *dpi_x;
419 params.dpi.y = *dpi_y;
420 } else {
421 ALOGI("Failed to get dpi_x and/or dpi_y for %s display. Using default"
422 " dpi %d.", GetDisplayName(is_primary), kDefaultDpi);
423 params.dpi.x = kDefaultDpi;
424 params.dpi.y = kDefaultDpi;
425 }
426 } else {
427 ALOGE("HardwareComposer: Failed to get current %s display config: %d."
428 " Using default display values.",
429 GetDisplayName(is_primary), error.value);
430 params.width = kDefaultDisplayWidth;
431 params.height = kDefaultDisplayHeight;
432 params.dpi.x = kDefaultDpi;
433 params.dpi.y = kDefaultDpi;
434 params.vsync_period_ns = kDefaultVsyncPeriodNs;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800435 }
436
Steven Thomasbfe46a02018-02-16 14:27:35 -0800437 ALOGI(
438 "HardwareComposer: %s display attributes: width=%d height=%d "
439 "vsync_period_ns=%d DPI=%dx%d",
440 GetDisplayName(is_primary),
441 params.width,
442 params.height,
443 params.vsync_period_ns,
444 params.dpi.x,
445 params.dpi.y);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800446
Steven Thomasbfe46a02018-02-16 14:27:35 -0800447 return params;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800448}
449
Corey Tabaka0b485c92017-05-19 12:02:58 -0700450std::string HardwareComposer::Dump() {
451 std::unique_lock<std::mutex> lock(post_thread_mutex_);
452 std::ostringstream stream;
453
Steven Thomasbfe46a02018-02-16 14:27:35 -0800454 auto print_display_metrics = [&](const DisplayParams& params) {
455 stream << GetDisplayName(params.is_primary)
456 << " display metrics: " << params.width << "x"
457 << params.height << " " << (params.dpi.x / 1000.0)
458 << "x" << (params.dpi.y / 1000.0) << " dpi @ "
459 << (1000000000.0 / params.vsync_period_ns) << " Hz"
460 << std::endl;
461 };
462
463 print_display_metrics(primary_display_);
464 if (external_display_)
465 print_display_metrics(*external_display_);
Corey Tabaka0b485c92017-05-19 12:02:58 -0700466
467 stream << "Post thread resumed: " << post_thread_resumed_ << std::endl;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700468 stream << "Active layers: " << layers_.size() << std::endl;
Corey Tabaka0b485c92017-05-19 12:02:58 -0700469 stream << std::endl;
470
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700471 for (size_t i = 0; i < layers_.size(); i++) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700472 stream << "Layer " << i << ":";
473 stream << " type=" << layers_[i].GetCompositionType().to_string();
474 stream << " surface_id=" << layers_[i].GetSurfaceId();
475 stream << " buffer_id=" << layers_[i].GetBufferId();
476 stream << std::endl;
477 }
478 stream << std::endl;
479
480 if (post_thread_resumed_) {
481 stream << "Hardware Composer Debug Info:" << std::endl;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700482 stream << composer_->dumpDebugInfo();
Corey Tabaka0b485c92017-05-19 12:02:58 -0700483 }
484
485 return stream.str();
486}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800487
Steven Thomasbfe46a02018-02-16 14:27:35 -0800488void HardwareComposer::PostLayers(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800489 ATRACE_NAME("HardwareComposer::PostLayers");
490
491 // Setup the hardware composer layers with current buffers.
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700492 for (auto& layer : layers_) {
493 layer.Prepare();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800494 }
495
496 // Now that we have taken in a frame from the application, we have a chance
497 // to drop the frame before passing the frame along to HWC.
498 // If the display driver has become backed up, we detect it here and then
499 // react by skipping this frame to catch up latency.
500 while (!retire_fence_fds_.empty() &&
501 (!retire_fence_fds_.front() ||
502 sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
503 // There are only 2 fences in here, no performance problem to shift the
504 // array of ints.
505 retire_fence_fds_.erase(retire_fence_fds_.begin());
506 }
507
George Burgess IV353a6f62017-06-26 17:13:09 -0700508 const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) >
John Bates954796e2017-05-11 11:00:31 -0700509 post_thread_config_.allowed_pending_fence_count;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800510
Corey Tabakab3732f02017-09-16 00:58:54 -0700511 if (is_fence_pending) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800512 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
513
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800514 ALOGW_IF(is_fence_pending,
515 "Warning: dropping a frame to catch up with HWC (pending = %zd)",
516 retire_fence_fds_.size());
517
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700518 for (auto& layer : layers_) {
519 layer.Drop();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800520 }
521 return;
522 } else {
523 // Make the transition more obvious in systrace when the frame skip happens
524 // above.
525 ATRACE_INT("frame_skip_count", 0);
526 }
527
Corey Tabaka89bbefc2017-06-06 16:14:21 -0700528#if TRACE > 1
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700529 for (size_t i = 0; i < layers_.size(); i++) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700530 ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s",
531 i, layers_[i].GetBufferId(),
Corey Tabaka2251d822017-04-20 16:04:07 -0700532 layers_[i].GetCompositionType().to_string().c_str());
Corey Tabaka0b485c92017-05-19 12:02:58 -0700533 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800534#endif
535
Steven Thomasbfe46a02018-02-16 14:27:35 -0800536 HWC::Error error = Validate(display);
John Bates46684842017-12-13 15:26:50 -0800537 if (error != HWC::Error::None) {
Steven Thomasbfe46a02018-02-16 14:27:35 -0800538 ALOGE("HardwareComposer::PostLayers: Validate failed: %s display=%" PRIu64,
539 error.to_string().c_str(), display);
John Bates46684842017-12-13 15:26:50 -0800540 return;
541 }
542
Steven Thomasbfe46a02018-02-16 14:27:35 -0800543 error = Present(display);
Corey Tabaka2251d822017-04-20 16:04:07 -0700544 if (error != HWC::Error::None) {
545 ALOGE("HardwareComposer::PostLayers: Present failed: %s",
546 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800547 return;
548 }
549
550 std::vector<Hwc2::Layer> out_layers;
551 std::vector<int> out_fences;
Steven Thomasbfe46a02018-02-16 14:27:35 -0800552 error = composer_->getReleaseFences(display,
Steven Thomas6e8f7062017-11-22 14:15:29 -0800553 &out_layers, &out_fences);
Corey Tabaka2251d822017-04-20 16:04:07 -0700554 ALOGE_IF(error != HWC::Error::None,
555 "HardwareComposer::PostLayers: Failed to get release fences: %s",
556 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800557
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700558 // Perform post-frame bookkeeping.
Corey Tabaka2251d822017-04-20 16:04:07 -0700559 uint32_t num_elements = out_layers.size();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800560 for (size_t i = 0; i < num_elements; ++i) {
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700561 for (auto& layer : layers_) {
562 if (layer.GetLayerHandle() == out_layers[i]) {
563 layer.Finish(out_fences[i]);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800564 }
565 }
566 }
567}
568
Steven Thomas050b2c82017-03-06 11:45:16 -0800569void HardwareComposer::SetDisplaySurfaces(
Corey Tabaka2251d822017-04-20 16:04:07 -0700570 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) {
Jin Qian7480c062017-03-21 00:04:15 +0000571 ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
572 surfaces.size());
Corey Tabaka2251d822017-04-20 16:04:07 -0700573 const bool display_idle = surfaces.size() == 0;
574 {
575 std::unique_lock<std::mutex> lock(post_thread_mutex_);
Steven Thomasbfe46a02018-02-16 14:27:35 -0800576 surfaces_ = std::move(surfaces);
577 surfaces_changed_ = true;
Corey Tabaka2251d822017-04-20 16:04:07 -0700578 }
579
Inseob Kim15791ea2020-07-31 19:29:32 +0900580 if (request_display_callback_)
Steven Thomas2ddf5672017-06-15 11:38:40 -0700581 request_display_callback_(!display_idle);
582
Corey Tabaka2251d822017-04-20 16:04:07 -0700583 // Set idle state based on whether there are any surfaces to handle.
584 UpdatePostThreadState(PostThreadState::Idle, display_idle);
Steven Thomas050b2c82017-03-06 11:45:16 -0800585}
Jin Qian7480c062017-03-21 00:04:15 +0000586
John Bates954796e2017-05-11 11:00:31 -0700587int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key,
588 IonBuffer& ion_buffer) {
Okan Arikan822b7102017-05-08 13:31:34 -0700589 if (key == DvrGlobalBuffers::kVsyncBuffer) {
590 vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>(
591 &ion_buffer, CPUUsageMode::WRITE_OFTEN);
592
593 if (vsync_ring_->IsMapped() == false) {
594 return -EPERM;
595 }
596 }
597
598 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700599 return MapConfigBuffer(ion_buffer);
600 }
601
602 return 0;
603}
604
605void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) {
Okan Arikan822b7102017-05-08 13:31:34 -0700606 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700607 ConfigBufferDeleted();
608 }
609}
610
611int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) {
612 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700613 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700614
Okan Arikan6f468c62017-05-31 14:48:30 -0700615 if (ion_buffer.width() < DvrConfigRing::MemorySize()) {
John Bates954796e2017-05-11 11:00:31 -0700616 ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size.");
617 return -EINVAL;
618 }
619
620 void* buffer_base = 0;
621 int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(),
622 ion_buffer.height(), &buffer_base);
623 if (result != 0) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700624 ALOGE(
625 "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config "
626 "buffer.");
John Bates954796e2017-05-11 11:00:31 -0700627 return -EPERM;
628 }
629
Okan Arikan6f468c62017-05-31 14:48:30 -0700630 shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width());
John Bates954796e2017-05-11 11:00:31 -0700631 ion_buffer.Unlock();
632
633 return 0;
634}
635
636void HardwareComposer::ConfigBufferDeleted() {
637 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700638 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700639}
640
641void HardwareComposer::UpdateConfigBuffer() {
642 std::lock_guard<std::mutex> lock(shared_config_mutex_);
643 if (!shared_config_ring_.is_valid())
644 return;
645 // Copy from latest record in shared_config_ring_ to local copy.
Okan Arikan6f468c62017-05-31 14:48:30 -0700646 DvrConfig record;
John Bates954796e2017-05-11 11:00:31 -0700647 if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) {
John Batescc65c3c2017-09-28 14:43:19 -0700648 ALOGI("DvrConfig updated: sequence %u, post offset %d",
649 shared_config_ring_sequence_, record.frame_post_offset_ns);
650 ++shared_config_ring_sequence_;
John Bates954796e2017-05-11 11:00:31 -0700651 post_thread_config_ = record;
652 }
653}
654
Corey Tabaka2251d822017-04-20 16:04:07 -0700655int HardwareComposer::PostThreadPollInterruptible(
Steven Thomasb02664d2017-07-26 18:48:28 -0700656 const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800657 pollfd pfd[2] = {
658 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700659 .fd = event_fd.Get(),
Steven Thomas66747c12017-03-22 18:45:31 -0700660 .events = static_cast<short>(requested_events),
661 .revents = 0,
Steven Thomas050b2c82017-03-06 11:45:16 -0800662 },
663 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700664 .fd = post_thread_event_fd_.Get(),
Steven Thomas050b2c82017-03-06 11:45:16 -0800665 .events = POLLPRI | POLLIN,
666 .revents = 0,
667 },
668 };
669 int ret, error;
670 do {
Steven Thomasb02664d2017-07-26 18:48:28 -0700671 ret = poll(pfd, 2, timeout_ms);
Steven Thomas050b2c82017-03-06 11:45:16 -0800672 error = errno;
673 ALOGW_IF(ret < 0,
674 "HardwareComposer::PostThreadPollInterruptible: Error during "
675 "poll(): %s (%d)",
676 strerror(error), error);
677 } while (ret < 0 && error == EINTR);
678
679 if (ret < 0) {
680 return -error;
Steven Thomasb02664d2017-07-26 18:48:28 -0700681 } else if (ret == 0) {
682 return -ETIMEDOUT;
Steven Thomas050b2c82017-03-06 11:45:16 -0800683 } else if (pfd[0].revents != 0) {
684 return 0;
685 } else if (pfd[1].revents != 0) {
Corey Tabaka0d07cdd2017-09-28 11:15:50 -0700686 ALOGI("VrHwcPost thread interrupted: revents=%x", pfd[1].revents);
Steven Thomas050b2c82017-03-06 11:45:16 -0800687 return kPostThreadInterrupted;
688 } else {
689 return 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800690 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800691}
692
Steven Thomasbfe46a02018-02-16 14:27:35 -0800693// Sleep until the next predicted vsync, returning the predicted vsync
694// timestamp.
695Status<int64_t> HardwareComposer::WaitForPredictedVSync() {
696 const int64_t predicted_vsync_time = last_vsync_timestamp_ +
697 (target_display_->vsync_period_ns * vsync_prediction_interval_);
Corey Tabakab3732f02017-09-16 00:58:54 -0700698 const int error = SleepUntil(predicted_vsync_time);
699 if (error < 0) {
700 ALOGE("HardwareComposer::WaifForVSync:: Failed to sleep: %s",
701 strerror(-error));
Steven Thomasb02664d2017-07-26 18:48:28 -0700702 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800703 }
Corey Tabakab3732f02017-09-16 00:58:54 -0700704 return {predicted_vsync_time};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800705}
706
707int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
708 const int timer_fd = vsync_sleep_timer_fd_.Get();
709 const itimerspec wakeup_itimerspec = {
710 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
711 .it_value = NsToTimespec(wakeup_timestamp),
712 };
713 int ret =
714 timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
715 int error = errno;
716 if (ret < 0) {
717 ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
718 strerror(error));
719 return -error;
720 }
721
Corey Tabaka451256f2017-08-22 11:59:15 -0700722 return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN,
723 /*timeout_ms*/ -1);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800724}
725
726void HardwareComposer::PostThread() {
727 // NOLINTNEXTLINE(runtime/int)
Steven Thomas050b2c82017-03-06 11:45:16 -0800728 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800729
Corey Tabaka2251d822017-04-20 16:04:07 -0700730 // Set the scheduler to SCHED_FIFO with high priority. If this fails here
731 // there may have been a startup timing issue between this thread and
732 // performanced. Try again later when this thread becomes active.
733 bool thread_policy_setup =
734 SetThreadPolicy("graphics:high", "/system/performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800735
Steven Thomas050b2c82017-03-06 11:45:16 -0800736 // Create a timerfd based on CLOCK_MONOTINIC.
737 vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
738 LOG_ALWAYS_FATAL_IF(
739 !vsync_sleep_timer_fd_,
740 "HardwareComposer: Failed to create vsync sleep timerfd: %s",
741 strerror(errno));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800742
Steven Thomasbfe46a02018-02-16 14:27:35 -0800743 struct VsyncEyeOffsets { int64_t left_ns, right_ns; };
Steven Thomas050b2c82017-03-06 11:45:16 -0800744 bool was_running = false;
745
Steven Thomasbfe46a02018-02-16 14:27:35 -0800746 auto get_vsync_eye_offsets = [this]() -> VsyncEyeOffsets {
747 VsyncEyeOffsets offsets;
748 offsets.left_ns =
749 GetPosePredictionTimeOffset(target_display_->vsync_period_ns);
750
751 // TODO(jbates) Query vblank time from device, when such an API is
752 // available. This value (6.3%) was measured on A00 in low persistence mode.
753 int64_t vblank_ns = target_display_->vsync_period_ns * 63 / 1000;
754 offsets.right_ns = (target_display_->vsync_period_ns - vblank_ns) / 2;
755
756 // Check property for overriding right eye offset value.
757 offsets.right_ns =
758 property_get_int64(kRightEyeOffsetProperty, offsets.right_ns);
759
760 return offsets;
761 };
762
763 VsyncEyeOffsets vsync_eye_offsets = get_vsync_eye_offsets();
764
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800765 while (1) {
766 ATRACE_NAME("HardwareComposer::PostThread");
767
John Bates954796e2017-05-11 11:00:31 -0700768 // Check for updated config once per vsync.
769 UpdateConfigBuffer();
770
Corey Tabaka2251d822017-04-20 16:04:07 -0700771 while (post_thread_quiescent_) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800772 std::unique_lock<std::mutex> lock(post_thread_mutex_);
Corey Tabaka2251d822017-04-20 16:04:07 -0700773 ALOGI("HardwareComposer::PostThread: Entering quiescent state.");
774
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700775 if (was_running) {
776 vsync_trace_parity_ = false;
777 ATRACE_INT(kVsyncTraceEventName, 0);
778 }
779
Steven Thomasbfe46a02018-02-16 14:27:35 -0800780 // Tear down resources.
781 OnPostThreadPaused();
Corey Tabaka2251d822017-04-20 16:04:07 -0700782 was_running = false;
783 post_thread_resumed_ = false;
784 post_thread_ready_.notify_all();
785
Steven Thomasaf336272018-01-04 17:36:47 -0800786 if (PostThreadCondWait(lock, -1,
787 [this] { return !post_thread_quiescent_; })) {
788 // A true return value means we've been asked to quit.
Corey Tabaka2251d822017-04-20 16:04:07 -0700789 return;
Steven Thomas282a5ed2017-02-07 18:07:01 -0800790 }
Corey Tabaka2251d822017-04-20 16:04:07 -0700791
Corey Tabaka2251d822017-04-20 16:04:07 -0700792 post_thread_resumed_ = true;
793 post_thread_ready_.notify_all();
794
795 ALOGI("HardwareComposer::PostThread: Exiting quiescent state.");
Steven Thomas050b2c82017-03-06 11:45:16 -0800796 }
797
Steven Thomasbfe46a02018-02-16 14:27:35 -0800798 if (!composer_)
799 CreateComposer();
800
801 bool target_display_changed = UpdateTargetDisplay();
802 bool just_resumed_running = !was_running;
803 was_running = true;
804
805 if (target_display_changed)
806 vsync_eye_offsets = get_vsync_eye_offsets();
807
808 if (just_resumed_running) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800809 OnPostThreadResumed();
Corey Tabaka2251d822017-04-20 16:04:07 -0700810
811 // Try to setup the scheduler policy if it failed during startup. Only
812 // attempt to do this on transitions from inactive to active to avoid
813 // spamming the system with RPCs and log messages.
814 if (!thread_policy_setup) {
815 thread_policy_setup =
816 SetThreadPolicy("graphics:high", "/system/performance");
817 }
Steven Thomasbfe46a02018-02-16 14:27:35 -0800818 }
Corey Tabakab3732f02017-09-16 00:58:54 -0700819
Steven Thomasbfe46a02018-02-16 14:27:35 -0800820 if (target_display_changed || just_resumed_running) {
Corey Tabakab3732f02017-09-16 00:58:54 -0700821 // Initialize the last vsync timestamp with the current time. The
822 // predictor below uses this time + the vsync interval in absolute time
823 // units for the initial delay. Once the driver starts reporting vsync the
824 // predictor will sync up with the real vsync.
825 last_vsync_timestamp_ = GetSystemClockNs();
Steven Thomasbfe46a02018-02-16 14:27:35 -0800826 vsync_prediction_interval_ = 1;
827 retire_fence_fds_.clear();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800828 }
829
830 int64_t vsync_timestamp = 0;
831 {
Corey Tabakab3732f02017-09-16 00:58:54 -0700832 TRACE_FORMAT("wait_vsync|vsync=%u;last_timestamp=%" PRId64
833 ";prediction_interval=%d|",
834 vsync_count_ + 1, last_vsync_timestamp_,
835 vsync_prediction_interval_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800836
Steven Thomasbfe46a02018-02-16 14:27:35 -0800837 auto status = WaitForPredictedVSync();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800838 ALOGE_IF(
Corey Tabakab3732f02017-09-16 00:58:54 -0700839 !status,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800840 "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
Corey Tabakab3732f02017-09-16 00:58:54 -0700841 status.GetErrorMessage().c_str());
842
843 // If there was an error either sleeping was interrupted due to pausing or
844 // there was an error getting the latest timestamp.
845 if (!status)
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800846 continue;
Corey Tabakab3732f02017-09-16 00:58:54 -0700847
848 // Predicted vsync timestamp for this interval. This is stable because we
849 // use absolute time for the wakeup timer.
850 vsync_timestamp = status.get();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800851 }
852
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700853 vsync_trace_parity_ = !vsync_trace_parity_;
854 ATRACE_INT(kVsyncTraceEventName, vsync_trace_parity_ ? 1 : 0);
855
Corey Tabakab3732f02017-09-16 00:58:54 -0700856 // Advance the vsync counter only if the system is keeping up with hardware
857 // vsync to give clients an indication of the delays.
858 if (vsync_prediction_interval_ == 1)
859 ++vsync_count_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800860
Steven Thomasbfe46a02018-02-16 14:27:35 -0800861 UpdateLayerConfig();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800862
Okan Arikan822b7102017-05-08 13:31:34 -0700863 // Publish the vsync event.
864 if (vsync_ring_) {
865 DvrVsync vsync;
866 vsync.vsync_count = vsync_count_;
867 vsync.vsync_timestamp_ns = vsync_timestamp;
Steven Thomasbfe46a02018-02-16 14:27:35 -0800868 vsync.vsync_left_eye_offset_ns = vsync_eye_offsets.left_ns;
869 vsync.vsync_right_eye_offset_ns = vsync_eye_offsets.right_ns;
870 vsync.vsync_period_ns = target_display_->vsync_period_ns;
Okan Arikan822b7102017-05-08 13:31:34 -0700871
872 vsync_ring_->Publish(vsync);
873 }
874
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800875 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700876 // Sleep until shortly before vsync.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800877 ATRACE_NAME("sleep");
878
Steven Thomasbfe46a02018-02-16 14:27:35 -0800879 const int64_t display_time_est_ns =
880 vsync_timestamp + target_display_->vsync_period_ns;
Corey Tabaka2251d822017-04-20 16:04:07 -0700881 const int64_t now_ns = GetSystemClockNs();
John Bates954796e2017-05-11 11:00:31 -0700882 const int64_t sleep_time_ns = display_time_est_ns - now_ns -
883 post_thread_config_.frame_post_offset_ns;
884 const int64_t wakeup_time_ns =
885 display_time_est_ns - post_thread_config_.frame_post_offset_ns;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800886
887 ATRACE_INT64("sleep_time_ns", sleep_time_ns);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800888 if (sleep_time_ns > 0) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700889 int error = SleepUntil(wakeup_time_ns);
John Bates46684842017-12-13 15:26:50 -0800890 ALOGE_IF(error < 0 && error != kPostThreadInterrupted,
891 "HardwareComposer::PostThread: Failed to sleep: %s",
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800892 strerror(-error));
John Bates46684842017-12-13 15:26:50 -0800893 // If the sleep was interrupted (error == kPostThreadInterrupted),
894 // we still go through and present this frame because we may have set
895 // layers earlier and we want to flush the Composer's internal command
896 // buffer by continuing through to validate and present.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800897 }
898 }
899
Corey Tabakab3732f02017-09-16 00:58:54 -0700900 {
Steven Thomasbfe46a02018-02-16 14:27:35 -0800901 auto status = composer_callback_->GetVsyncTime(target_display_->id);
Corey Tabakab3732f02017-09-16 00:58:54 -0700902
903 // If we failed to read vsync there might be a problem with the driver.
904 // Since there's nothing we can do just behave as though we didn't get an
905 // updated vsync time and let the prediction continue.
906 const int64_t current_vsync_timestamp =
907 status ? status.get() : last_vsync_timestamp_;
908
909 const bool vsync_delayed =
910 last_vsync_timestamp_ == current_vsync_timestamp;
911 ATRACE_INT("vsync_delayed", vsync_delayed);
912
913 // If vsync was delayed advance the prediction interval and allow the
914 // fence logic in PostLayers() to skip the frame.
915 if (vsync_delayed) {
916 ALOGW(
917 "HardwareComposer::PostThread: VSYNC timestamp did not advance "
918 "since last frame: timestamp=%" PRId64 " prediction_interval=%d",
919 current_vsync_timestamp, vsync_prediction_interval_);
920 vsync_prediction_interval_++;
921 } else {
922 // We have an updated vsync timestamp, reset the prediction interval.
923 last_vsync_timestamp_ = current_vsync_timestamp;
924 vsync_prediction_interval_ = 1;
925 }
926 }
927
Steven Thomasbfe46a02018-02-16 14:27:35 -0800928 PostLayers(target_display_->id);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800929 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800930}
931
Steven Thomasbfe46a02018-02-16 14:27:35 -0800932bool HardwareComposer::UpdateTargetDisplay() {
933 bool target_display_changed = false;
934 auto displays = composer_callback_->GetDisplays();
935 if (displays.external_display_was_hotplugged) {
936 bool was_using_external_display = !target_display_->is_primary;
937 if (was_using_external_display) {
938 // The external display was hotplugged, so make sure to ignore any bad
939 // display errors as we destroy the layers.
940 for (auto& layer: layers_)
941 layer.IgnoreBadDisplayErrorsOnDestroy(true);
942 }
943
944 if (displays.external_display) {
945 // External display was connected
946 external_display_ = GetDisplayParams(composer_.get(),
947 *displays.external_display, /*is_primary*/ false);
948
mamika4f14de2019-08-30 07:27:14 -0700949 ALOGI("External display connected. Switching to external display.");
950 target_display_ = &(*external_display_);
951 target_display_changed = true;
Steven Thomasbfe46a02018-02-16 14:27:35 -0800952 } else {
953 // External display was disconnected
954 external_display_ = std::nullopt;
955 if (was_using_external_display) {
956 ALOGI("External display disconnected. Switching to primary display.");
957 target_display_ = &primary_display_;
958 target_display_changed = true;
959 }
960 }
961 }
962
963 if (target_display_changed) {
964 // If we're switching to the external display, turn the primary display off.
965 if (!target_display_->is_primary) {
966 EnableDisplay(primary_display_, false);
967 }
968 // If we're switching to the primary display, and the external display is
969 // still connected, turn the external display off.
970 else if (target_display_->is_primary && external_display_) {
971 EnableDisplay(*external_display_, false);
972 }
973
mamik913cc132019-09-13 14:58:43 -0700974 // Update the cached edid data for the current display.
975 UpdateEdidData(composer_.get(), target_display_->id);
976
Steven Thomasbfe46a02018-02-16 14:27:35 -0800977 // Turn the new target display on.
978 EnableDisplay(*target_display_, true);
979
980 // When we switch displays we need to recreate all the layers, so clear the
981 // current list, which will trigger layer recreation.
982 layers_.clear();
983 }
984
985 return target_display_changed;
986}
987
Corey Tabaka2251d822017-04-20 16:04:07 -0700988// Checks for changes in the surface stack and updates the layer config to
989// accomodate the new stack.
Steven Thomasbfe46a02018-02-16 14:27:35 -0800990void HardwareComposer::UpdateLayerConfig() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700991 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces;
Steven Thomas050b2c82017-03-06 11:45:16 -0800992 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700993 std::unique_lock<std::mutex> lock(post_thread_mutex_);
Corey Tabaka2251d822017-04-20 16:04:07 -0700994
Steven Thomasbfe46a02018-02-16 14:27:35 -0800995 if (!surfaces_changed_ && (!layers_.empty() || surfaces_.empty()))
996 return;
997
998 surfaces = surfaces_;
999 surfaces_changed_ = false;
Steven Thomas050b2c82017-03-06 11:45:16 -08001000 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001001
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001002 ATRACE_NAME("UpdateLayerConfig_HwLayers");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001003
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001004 // Sort the new direct surface list by z-order to determine the relative order
1005 // of the surfaces. This relative order is used for the HWC z-order value to
1006 // insulate VrFlinger and HWC z-order semantics from each other.
1007 std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) {
1008 return a->z_order() < b->z_order();
1009 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001010
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001011 // Prepare a new layer stack, pulling in layers from the previous
1012 // layer stack that are still active and updating their attributes.
1013 std::vector<Layer> layers;
1014 size_t layer_index = 0;
1015 for (const auto& surface : surfaces) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001016 // The bottom layer is opaque, other layers blend.
1017 HWC::BlendMode blending =
1018 layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001019
1020 // Try to find a layer for this surface in the set of active layers.
1021 auto search =
1022 std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id());
1023 const bool found = search != layers_.end() &&
1024 search->GetSurfaceId() == surface->surface_id();
1025 if (found) {
1026 // Update the attributes of the layer that may have changed.
1027 search->SetBlending(blending);
1028 search->SetZOrder(layer_index); // Relative z-order.
1029
1030 // Move the existing layer to the new layer set and remove the empty layer
1031 // object from the current set.
1032 layers.push_back(std::move(*search));
1033 layers_.erase(search);
1034 } else {
1035 // Insert a layer for the new surface.
Steven Thomasbfe46a02018-02-16 14:27:35 -08001036 layers.emplace_back(composer_.get(), *target_display_, surface, blending,
1037 HWC::Composition::Device, layer_index);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001038 }
1039
1040 ALOGI_IF(
1041 TRACE,
1042 "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d",
1043 layer_index, layers[layer_index].GetSurfaceId());
1044
1045 layer_index++;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001046 }
1047
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001048 // Sort the new layer stack by ascending surface id.
1049 std::sort(layers.begin(), layers.end());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001050
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001051 // Replace the previous layer set with the new layer set. The destructor of
1052 // the previous set will clean up the remaining Layers that are not moved to
1053 // the new layer set.
1054 layers_ = std::move(layers);
1055
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001056 ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001057 layers_.size());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001058}
1059
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001060std::vector<sp<IVsyncCallback>>::const_iterator
1061HardwareComposer::VsyncService::FindCallback(
1062 const sp<IVsyncCallback>& callback) const {
1063 sp<IBinder> binder = IInterface::asBinder(callback);
1064 return std::find_if(callbacks_.cbegin(), callbacks_.cend(),
1065 [&](const sp<IVsyncCallback>& callback) {
1066 return IInterface::asBinder(callback) == binder;
1067 });
1068}
1069
1070status_t HardwareComposer::VsyncService::registerCallback(
1071 const sp<IVsyncCallback> callback) {
1072 std::lock_guard<std::mutex> autolock(mutex_);
1073 if (FindCallback(callback) == callbacks_.cend()) {
1074 callbacks_.push_back(callback);
1075 }
Tianyu Jiang932fb4f2018-11-15 11:09:58 -08001076 return OK;
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001077}
1078
1079status_t HardwareComposer::VsyncService::unregisterCallback(
1080 const sp<IVsyncCallback> callback) {
1081 std::lock_guard<std::mutex> autolock(mutex_);
1082 auto iter = FindCallback(callback);
1083 if (iter != callbacks_.cend()) {
1084 callbacks_.erase(iter);
1085 }
Tianyu Jiang932fb4f2018-11-15 11:09:58 -08001086 return OK;
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001087}
1088
1089void HardwareComposer::VsyncService::OnVsync(int64_t vsync_timestamp) {
1090 ATRACE_NAME("VsyncService::OnVsync");
1091 std::lock_guard<std::mutex> autolock(mutex_);
1092 for (auto iter = callbacks_.begin(); iter != callbacks_.end();) {
1093 if ((*iter)->onVsync(vsync_timestamp) == android::DEAD_OBJECT) {
1094 iter = callbacks_.erase(iter);
1095 } else {
1096 ++iter;
1097 }
1098 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001099}
1100
Steven Thomasb02664d2017-07-26 18:48:28 -07001101Return<void> HardwareComposer::ComposerCallback::onHotplug(
Steven Thomas6e8f7062017-11-22 14:15:29 -08001102 Hwc2::Display display, IComposerCallback::Connection conn) {
Steven Thomasbfe46a02018-02-16 14:27:35 -08001103 std::lock_guard<std::mutex> lock(mutex_);
1104 ALOGI("onHotplug display=%" PRIu64 " conn=%d", display, conn);
1105
1106 bool is_primary = !got_first_hotplug_ || display == primary_display_.id;
1107
Steven Thomas6e8f7062017-11-22 14:15:29 -08001108 // Our first onHotplug callback is always for the primary display.
Steven Thomasbfe46a02018-02-16 14:27:35 -08001109 if (!got_first_hotplug_) {
Steven Thomas6e8f7062017-11-22 14:15:29 -08001110 LOG_ALWAYS_FATAL_IF(conn != IComposerCallback::Connection::CONNECTED,
1111 "Initial onHotplug callback should be primary display connected");
Steven Thomasbfe46a02018-02-16 14:27:35 -08001112 got_first_hotplug_ = true;
1113 } else if (is_primary) {
1114 ALOGE("Ignoring unexpected onHotplug() call for primary display");
1115 return Void();
1116 }
1117
1118 if (conn == IComposerCallback::Connection::CONNECTED) {
1119 if (!is_primary)
1120 external_display_ = DisplayInfo();
1121 DisplayInfo& display_info = is_primary ?
1122 primary_display_ : *external_display_;
1123 display_info.id = display;
Steven Thomas6e8f7062017-11-22 14:15:29 -08001124
Corey Tabakab3732f02017-09-16 00:58:54 -07001125 std::array<char, 1024> buffer;
1126 snprintf(buffer.data(), buffer.size(),
1127 "/sys/class/graphics/fb%" PRIu64 "/vsync_event", display);
1128 if (LocalHandle handle{buffer.data(), O_RDONLY}) {
1129 ALOGI(
1130 "HardwareComposer::ComposerCallback::onHotplug: Driver supports "
1131 "vsync_event node for display %" PRIu64,
1132 display);
Steven Thomasbfe46a02018-02-16 14:27:35 -08001133 display_info.driver_vsync_event_fd = std::move(handle);
Corey Tabakab3732f02017-09-16 00:58:54 -07001134 } else {
1135 ALOGI(
1136 "HardwareComposer::ComposerCallback::onHotplug: Driver does not "
1137 "support vsync_event node for display %" PRIu64,
1138 display);
1139 }
Steven Thomasbfe46a02018-02-16 14:27:35 -08001140 } else if (conn == IComposerCallback::Connection::DISCONNECTED) {
1141 external_display_ = std::nullopt;
Corey Tabakab3732f02017-09-16 00:58:54 -07001142 }
1143
Steven Thomasbfe46a02018-02-16 14:27:35 -08001144 if (!is_primary)
1145 external_display_was_hotplugged_ = true;
1146
Steven Thomasb02664d2017-07-26 18:48:28 -07001147 return Void();
1148}
1149
1150Return<void> HardwareComposer::ComposerCallback::onRefresh(
1151 Hwc2::Display /*display*/) {
1152 return hardware::Void();
1153}
1154
Corey Tabaka451256f2017-08-22 11:59:15 -07001155Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display,
1156 int64_t timestamp) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001157 TRACE_FORMAT("vsync_callback|display=%" PRIu64 ";timestamp=%" PRId64 "|",
1158 display, timestamp);
1159 std::lock_guard<std::mutex> lock(mutex_);
Steven Thomasbfe46a02018-02-16 14:27:35 -08001160 DisplayInfo* display_info = GetDisplayInfo(display);
1161 if (display_info) {
Steven Thomasbfe46a02018-02-16 14:27:35 -08001162 display_info->callback_vsync_timestamp = timestamp;
Steven Thomasb02664d2017-07-26 18:48:28 -07001163 }
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001164 if (primary_display_.id == display && vsync_service_ != nullptr) {
1165 vsync_service_->OnVsync(timestamp);
1166 }
Steven Thomasbfe46a02018-02-16 14:27:35 -08001167
Steven Thomasb02664d2017-07-26 18:48:28 -07001168 return Void();
1169}
1170
Ady Abraham7159f572019-10-11 11:10:18 -07001171Return<void> HardwareComposer::ComposerCallback::onVsync_2_4(
1172 Hwc2::Display /*display*/, int64_t /*timestamp*/,
1173 Hwc2::VsyncPeriodNanos /*vsyncPeriodNanos*/) {
1174 LOG_ALWAYS_FATAL("Unexpected onVsync_2_4 callback");
1175 return Void();
1176}
1177
1178Return<void> HardwareComposer::ComposerCallback::onVsyncPeriodTimingChanged(
1179 Hwc2::Display /*display*/,
1180 const Hwc2::VsyncPeriodChangeTimeline& /*updatedTimeline*/) {
1181 LOG_ALWAYS_FATAL("Unexpected onVsyncPeriodTimingChanged callback");
1182 return Void();
1183}
1184
Ady Abrahamb0433bc2020-01-08 17:31:06 -08001185Return<void> HardwareComposer::ComposerCallback::onSeamlessPossible(
1186 Hwc2::Display /*display*/) {
1187 LOG_ALWAYS_FATAL("Unexpected onSeamlessPossible callback");
1188 return Void();
1189}
1190
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001191void HardwareComposer::ComposerCallback::SetVsyncService(
1192 const sp<VsyncService>& vsync_service) {
1193 std::lock_guard<std::mutex> lock(mutex_);
1194 vsync_service_ = vsync_service;
1195}
1196
Steven Thomasbfe46a02018-02-16 14:27:35 -08001197HardwareComposer::ComposerCallback::Displays
1198HardwareComposer::ComposerCallback::GetDisplays() {
1199 std::lock_guard<std::mutex> lock(mutex_);
1200 Displays displays;
1201 displays.primary_display = primary_display_.id;
1202 if (external_display_)
1203 displays.external_display = external_display_->id;
1204 if (external_display_was_hotplugged_) {
1205 external_display_was_hotplugged_ = false;
1206 displays.external_display_was_hotplugged = true;
1207 }
1208 return displays;
1209}
1210
1211Status<int64_t> HardwareComposer::ComposerCallback::GetVsyncTime(
1212 hwc2_display_t display) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -07001213 std::lock_guard<std::mutex> autolock(mutex_);
Steven Thomasbfe46a02018-02-16 14:27:35 -08001214 DisplayInfo* display_info = GetDisplayInfo(display);
1215 if (!display_info) {
1216 ALOGW("Attempt to get vsync time for unknown display %" PRIu64, display);
1217 return ErrorStatus(EINVAL);
1218 }
1219
Corey Tabakab3732f02017-09-16 00:58:54 -07001220 // See if the driver supports direct vsync events.
Steven Thomasbfe46a02018-02-16 14:27:35 -08001221 LocalHandle& event_fd = display_info->driver_vsync_event_fd;
Corey Tabakab3732f02017-09-16 00:58:54 -07001222 if (!event_fd) {
1223 // Fall back to returning the last timestamp returned by the vsync
1224 // callback.
Steven Thomasbfe46a02018-02-16 14:27:35 -08001225 return display_info->callback_vsync_timestamp;
Corey Tabakab3732f02017-09-16 00:58:54 -07001226 }
1227
1228 // When the driver supports the vsync_event sysfs node we can use it to
1229 // determine the latest vsync timestamp, even if the HWC callback has been
1230 // delayed.
1231
1232 // The driver returns data in the form "VSYNC=<timestamp ns>".
1233 std::array<char, 32> data;
1234 data.fill('\0');
1235
1236 // Seek back to the beginning of the event file.
1237 int ret = lseek(event_fd.Get(), 0, SEEK_SET);
1238 if (ret < 0) {
1239 const int error = errno;
1240 ALOGE(
1241 "HardwareComposer::ComposerCallback::GetVsyncTime: Failed to seek "
1242 "vsync event fd: %s",
1243 strerror(error));
1244 return ErrorStatus(error);
1245 }
1246
1247 // Read the vsync event timestamp.
1248 ret = read(event_fd.Get(), data.data(), data.size());
1249 if (ret < 0) {
1250 const int error = errno;
1251 ALOGE_IF(error != EAGAIN,
1252 "HardwareComposer::ComposerCallback::GetVsyncTime: Error "
1253 "while reading timestamp: %s",
1254 strerror(error));
1255 return ErrorStatus(error);
1256 }
1257
1258 int64_t timestamp;
1259 ret = sscanf(data.data(), "VSYNC=%" PRIu64,
1260 reinterpret_cast<uint64_t*>(&timestamp));
1261 if (ret < 0) {
1262 const int error = errno;
1263 ALOGE(
1264 "HardwareComposer::ComposerCallback::GetVsyncTime: Error while "
1265 "parsing timestamp: %s",
1266 strerror(error));
1267 return ErrorStatus(error);
1268 }
1269
1270 return {timestamp};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001271}
1272
Steven Thomasbfe46a02018-02-16 14:27:35 -08001273HardwareComposer::ComposerCallback::DisplayInfo*
1274HardwareComposer::ComposerCallback::GetDisplayInfo(hwc2_display_t display) {
1275 if (display == primary_display_.id) {
1276 return &primary_display_;
1277 } else if (external_display_ && display == external_display_->id) {
1278 return &(*external_display_);
1279 }
1280 return nullptr;
1281}
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001282
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001283void Layer::Reset() {
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001284 if (hardware_composer_layer_) {
Steven Thomasbfe46a02018-02-16 14:27:35 -08001285 HWC::Error error =
1286 composer_->destroyLayer(display_params_.id, hardware_composer_layer_);
1287 if (error != HWC::Error::None &&
1288 (!ignore_bad_display_errors_on_destroy_ ||
1289 error != HWC::Error::BadDisplay)) {
1290 ALOGE("destroyLayer() failed for display %" PRIu64 ", layer %" PRIu64
1291 ". error: %s", display_params_.id, hardware_composer_layer_,
1292 error.to_string().c_str());
1293 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001294 hardware_composer_layer_ = 0;
1295 }
1296
Corey Tabaka2251d822017-04-20 16:04:07 -07001297 z_order_ = 0;
1298 blending_ = HWC::BlendMode::None;
Corey Tabaka2251d822017-04-20 16:04:07 -07001299 composition_type_ = HWC::Composition::Invalid;
1300 target_composition_type_ = composition_type_;
1301 source_ = EmptyVariant{};
1302 acquire_fence_.Close();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001303 surface_rect_functions_applied_ = false;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001304 pending_visibility_settings_ = true;
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001305 cached_buffer_map_.clear();
Steven Thomasbfe46a02018-02-16 14:27:35 -08001306 ignore_bad_display_errors_on_destroy_ = false;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001307}
1308
Steven Thomasbfe46a02018-02-16 14:27:35 -08001309Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
1310 const std::shared_ptr<DirectDisplaySurface>& surface,
1311 HWC::BlendMode blending, HWC::Composition composition_type,
1312 size_t z_order)
1313 : composer_(composer),
1314 display_params_(display_params),
1315 z_order_{z_order},
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001316 blending_{blending},
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001317 target_composition_type_{composition_type},
1318 source_{SourceSurface{surface}} {
1319 CommonLayerSetup();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001320}
1321
Steven Thomasbfe46a02018-02-16 14:27:35 -08001322Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
1323 const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
1324 HWC::Composition composition_type, size_t z_order)
1325 : composer_(composer),
1326 display_params_(display_params),
1327 z_order_{z_order},
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001328 blending_{blending},
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001329 target_composition_type_{composition_type},
1330 source_{SourceBuffer{buffer}} {
1331 CommonLayerSetup();
1332}
1333
1334Layer::~Layer() { Reset(); }
1335
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -07001336Layer::Layer(Layer&& other) noexcept { *this = std::move(other); }
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001337
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -07001338Layer& Layer::operator=(Layer&& other) noexcept {
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001339 if (this != &other) {
1340 Reset();
1341 using std::swap;
Steven Thomasbfe46a02018-02-16 14:27:35 -08001342 swap(composer_, other.composer_);
1343 swap(display_params_, other.display_params_);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001344 swap(hardware_composer_layer_, other.hardware_composer_layer_);
1345 swap(z_order_, other.z_order_);
1346 swap(blending_, other.blending_);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001347 swap(composition_type_, other.composition_type_);
1348 swap(target_composition_type_, other.target_composition_type_);
1349 swap(source_, other.source_);
1350 swap(acquire_fence_, other.acquire_fence_);
1351 swap(surface_rect_functions_applied_,
1352 other.surface_rect_functions_applied_);
1353 swap(pending_visibility_settings_, other.pending_visibility_settings_);
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001354 swap(cached_buffer_map_, other.cached_buffer_map_);
Steven Thomasbfe46a02018-02-16 14:27:35 -08001355 swap(ignore_bad_display_errors_on_destroy_,
1356 other.ignore_bad_display_errors_on_destroy_);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001357 }
1358 return *this;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001359}
1360
Corey Tabaka2251d822017-04-20 16:04:07 -07001361void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) {
1362 if (source_.is<SourceBuffer>())
1363 std::get<SourceBuffer>(source_) = {buffer};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001364}
1365
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001366void Layer::SetBlending(HWC::BlendMode blending) {
1367 if (blending_ != blending) {
1368 blending_ = blending;
1369 pending_visibility_settings_ = true;
1370 }
1371}
1372
1373void Layer::SetZOrder(size_t z_order) {
1374 if (z_order_ != z_order) {
1375 z_order_ = z_order;
1376 pending_visibility_settings_ = true;
1377 }
1378}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001379
1380IonBuffer* Layer::GetBuffer() {
Corey Tabaka2251d822017-04-20 16:04:07 -07001381 struct Visitor {
1382 IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); }
1383 IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); }
1384 IonBuffer* operator()(EmptyVariant) { return nullptr; }
1385 };
1386 return source_.Visit(Visitor{});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001387}
1388
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001389void Layer::UpdateVisibilitySettings() {
1390 if (pending_visibility_settings_) {
1391 pending_visibility_settings_ = false;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001392
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001393 HWC::Error error;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001394
1395 error = composer_->setLayerBlendMode(
Steven Thomasbfe46a02018-02-16 14:27:35 -08001396 display_params_.id, hardware_composer_layer_,
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001397 blending_.cast<Hwc2::IComposerClient::BlendMode>());
1398 ALOGE_IF(error != HWC::Error::None,
1399 "Layer::UpdateLayerSettings: Error setting layer blend mode: %s",
1400 error.to_string().c_str());
1401
Steven Thomasbfe46a02018-02-16 14:27:35 -08001402 error = composer_->setLayerZOrder(display_params_.id,
1403 hardware_composer_layer_, z_order_);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001404 ALOGE_IF(error != HWC::Error::None,
1405 "Layer::UpdateLayerSettings: Error setting z_ order: %s",
1406 error.to_string().c_str());
1407 }
1408}
1409
1410void Layer::UpdateLayerSettings() {
Corey Tabaka2251d822017-04-20 16:04:07 -07001411 HWC::Error error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001412
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001413 UpdateVisibilitySettings();
1414
Corey Tabaka2251d822017-04-20 16:04:07 -07001415 // TODO(eieio): Use surface attributes or some other mechanism to control
1416 // the layer display frame.
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001417 error = composer_->setLayerDisplayFrame(
Steven Thomasbfe46a02018-02-16 14:27:35 -08001418 display_params_.id, hardware_composer_layer_,
1419 {0, 0, display_params_.width, display_params_.height});
Corey Tabaka2251d822017-04-20 16:04:07 -07001420 ALOGE_IF(error != HWC::Error::None,
1421 "Layer::UpdateLayerSettings: Error setting layer display frame: %s",
1422 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001423
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001424 error = composer_->setLayerVisibleRegion(
Steven Thomasbfe46a02018-02-16 14:27:35 -08001425 display_params_.id, hardware_composer_layer_,
1426 {{0, 0, display_params_.width, display_params_.height}});
Corey Tabaka2251d822017-04-20 16:04:07 -07001427 ALOGE_IF(error != HWC::Error::None,
1428 "Layer::UpdateLayerSettings: Error setting layer visible region: %s",
1429 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001430
Steven Thomasbfe46a02018-02-16 14:27:35 -08001431 error = composer_->setLayerPlaneAlpha(display_params_.id,
1432 hardware_composer_layer_, 1.0f);
Corey Tabaka2251d822017-04-20 16:04:07 -07001433 ALOGE_IF(error != HWC::Error::None,
1434 "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s",
1435 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001436}
1437
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001438void Layer::CommonLayerSetup() {
Steven Thomasbfe46a02018-02-16 14:27:35 -08001439 HWC::Error error = composer_->createLayer(display_params_.id,
Steven Thomas6e8f7062017-11-22 14:15:29 -08001440 &hardware_composer_layer_);
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001441 ALOGE_IF(error != HWC::Error::None,
1442 "Layer::CommonLayerSetup: Failed to create layer on primary "
1443 "display: %s",
1444 error.to_string().c_str());
1445 UpdateLayerSettings();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001446}
1447
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001448bool Layer::CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id) {
1449 auto search = cached_buffer_map_.find(slot);
1450 if (search != cached_buffer_map_.end() && search->second == buffer_id)
1451 return true;
1452
1453 // Assign or update the buffer slot.
1454 if (buffer_id >= 0)
1455 cached_buffer_map_[slot] = buffer_id;
1456 return false;
1457}
1458
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001459void Layer::Prepare() {
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001460 int right, bottom, id;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -04001461 sp<GraphicBuffer> handle;
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001462 std::size_t slot;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001463
Corey Tabaka2251d822017-04-20 16:04:07 -07001464 // Acquire the next buffer according to the type of source.
1465 IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) {
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001466 std::tie(right, bottom, id, handle, acquire_fence_, slot) =
1467 source.Acquire();
Corey Tabaka2251d822017-04-20 16:04:07 -07001468 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001469
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001470 TRACE_FORMAT("Layer::Prepare|buffer_id=%d;slot=%zu|", id, slot);
1471
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001472 // Update any visibility (blending, z-order) changes that occurred since
1473 // last prepare.
1474 UpdateVisibilitySettings();
1475
1476 // When a layer is first setup there may be some time before the first
1477 // buffer arrives. Setup the HWC layer as a solid color to stall for time
1478 // until the first buffer arrives. Once the first buffer arrives there will
1479 // always be a buffer for the frame even if it is old.
Corey Tabaka2251d822017-04-20 16:04:07 -07001480 if (!handle.get()) {
1481 if (composition_type_ == HWC::Composition::Invalid) {
1482 composition_type_ = HWC::Composition::SolidColor;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001483 composer_->setLayerCompositionType(
Steven Thomasbfe46a02018-02-16 14:27:35 -08001484 display_params_.id, hardware_composer_layer_,
Corey Tabaka2251d822017-04-20 16:04:07 -07001485 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1486 Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0};
Steven Thomasbfe46a02018-02-16 14:27:35 -08001487 composer_->setLayerColor(display_params_.id, hardware_composer_layer_,
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001488 layer_color);
Corey Tabaka2251d822017-04-20 16:04:07 -07001489 } else {
1490 // The composition type is already set. Nothing else to do until a
1491 // buffer arrives.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001492 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001493 } else {
Corey Tabaka2251d822017-04-20 16:04:07 -07001494 if (composition_type_ != target_composition_type_) {
1495 composition_type_ = target_composition_type_;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001496 composer_->setLayerCompositionType(
Steven Thomasbfe46a02018-02-16 14:27:35 -08001497 display_params_.id, hardware_composer_layer_,
Corey Tabaka2251d822017-04-20 16:04:07 -07001498 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1499 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001500
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001501 // See if the HWC cache already has this buffer.
1502 const bool cached = CheckAndUpdateCachedBuffer(slot, id);
1503 if (cached)
1504 handle = nullptr;
1505
Corey Tabaka2251d822017-04-20 16:04:07 -07001506 HWC::Error error{HWC::Error::None};
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001507 error =
Steven Thomasbfe46a02018-02-16 14:27:35 -08001508 composer_->setLayerBuffer(display_params_.id, hardware_composer_layer_,
Corey Tabaka0d07cdd2017-09-28 11:15:50 -07001509 slot, handle, acquire_fence_.Get());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001510
Corey Tabaka2251d822017-04-20 16:04:07 -07001511 ALOGE_IF(error != HWC::Error::None,
1512 "Layer::Prepare: Error setting layer buffer: %s",
1513 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001514
Corey Tabaka2251d822017-04-20 16:04:07 -07001515 if (!surface_rect_functions_applied_) {
1516 const float float_right = right;
1517 const float float_bottom = bottom;
Steven Thomasbfe46a02018-02-16 14:27:35 -08001518 error = composer_->setLayerSourceCrop(display_params_.id,
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001519 hardware_composer_layer_,
1520 {0, 0, float_right, float_bottom});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001521
Corey Tabaka2251d822017-04-20 16:04:07 -07001522 ALOGE_IF(error != HWC::Error::None,
1523 "Layer::Prepare: Error setting layer source crop: %s",
1524 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001525
Corey Tabaka2251d822017-04-20 16:04:07 -07001526 surface_rect_functions_applied_ = true;
1527 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001528 }
1529}
1530
1531void Layer::Finish(int release_fence_fd) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001532 IfAnyOf<SourceSurface, SourceBuffer>::Call(
1533 &source_, [release_fence_fd](auto& source) {
1534 source.Finish(LocalHandle(release_fence_fd));
1535 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001536}
1537
Corey Tabaka2251d822017-04-20 16:04:07 -07001538void Layer::Drop() { acquire_fence_.Close(); }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001539
1540} // namespace dvr
1541} // namespace android