blob: f9a5dcd98765a385aeab8767abec93a8e6104e99 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#include "hardware_composer.h"
2
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08003#include <cutils/properties.h>
4#include <cutils/sched_policy.h>
5#include <fcntl.h>
Corey Tabaka2251d822017-04-20 16:04:07 -07006#include <log/log.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08007#include <poll.h>
8#include <sync/sync.h>
9#include <sys/eventfd.h>
10#include <sys/prctl.h>
11#include <sys/resource.h>
12#include <sys/system_properties.h>
13#include <sys/timerfd.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070014#include <time.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080015#include <unistd.h>
16#include <utils/Trace.h>
17
18#include <algorithm>
Corey Tabaka2251d822017-04-20 16:04:07 -070019#include <chrono>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080020#include <functional>
21#include <map>
Corey Tabaka0b485c92017-05-19 12:02:58 -070022#include <sstream>
23#include <string>
John Bates954796e2017-05-11 11:00:31 -070024#include <tuple>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080025
Corey Tabaka2251d822017-04-20 16:04:07 -070026#include <dvr/dvr_display_types.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080027#include <dvr/performance_client_api.h>
28#include <private/dvr/clock_ns.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070029#include <private/dvr/ion_buffer.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080030
Steven Thomas94e35b92017-07-26 18:48:28 -070031using android::hardware::Return;
32using android::hardware::Void;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080033using android::pdx::LocalHandle;
Corey Tabaka2251d822017-04-20 16:04:07 -070034using android::pdx::rpc::EmptyVariant;
35using android::pdx::rpc::IfAnyOf;
36
37using namespace std::chrono_literals;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080038
39namespace android {
40namespace dvr {
41
42namespace {
43
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080044const char kBacklightBrightnessSysFile[] =
45 "/sys/class/leds/lcd-backlight/brightness";
46
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080047const char kPrimaryDisplayWaitPPEventFile[] = "/sys/class/graphics/fb0/wait_pp";
48
49const 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
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080053// Get time offset from a vsync to when the pose for that vsync should be
54// predicted out to. For example, if scanout gets halfway through the frame
55// at the halfway point between vsyncs, then this could be half the period.
56// With global shutter displays, this should be changed to the offset to when
57// illumination begins. Low persistence adds a frame of latency, so we predict
58// to the center of the next frame.
59inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
60 return (vsync_period_ns * 150) / 100;
61}
62
Corey Tabaka2251d822017-04-20 16:04:07 -070063// Attempts to set the scheduler class and partiton for the current thread.
64// Returns true on success or false on failure.
65bool SetThreadPolicy(const std::string& scheduler_class,
66 const std::string& partition) {
67 int error = dvrSetSchedulerClass(0, scheduler_class.c_str());
68 if (error < 0) {
69 ALOGE(
70 "SetThreadPolicy: Failed to set scheduler class \"%s\" for "
71 "thread_id=%d: %s",
72 scheduler_class.c_str(), gettid(), strerror(-error));
73 return false;
74 }
75 error = dvrSetCpuPartition(0, partition.c_str());
76 if (error < 0) {
77 ALOGE(
78 "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: "
79 "%s",
80 partition.c_str(), gettid(), strerror(-error));
81 return false;
82 }
83 return true;
84}
85
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080086} // anonymous namespace
87
Corey Tabaka2251d822017-04-20 16:04:07 -070088// HardwareComposer static data;
89constexpr size_t HardwareComposer::kMaxHardwareLayers;
90
91HardwareComposer::HardwareComposer()
Steven Thomas94e35b92017-07-26 18:48:28 -070092 : initialized_(false), request_display_callback_(nullptr) {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080093
94HardwareComposer::~HardwareComposer(void) {
Corey Tabaka2251d822017-04-20 16:04:07 -070095 UpdatePostThreadState(PostThreadState::Quit, true);
96 if (post_thread_.joinable())
Steven Thomas050b2c82017-03-06 11:45:16 -080097 post_thread_.join();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080098}
99
Steven Thomas94e35b92017-07-26 18:48:28 -0700100bool HardwareComposer::Initialize(
101 Hwc2::Composer* hidl, RequestDisplayCallback request_display_callback) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800102 if (initialized_) {
103 ALOGE("HardwareComposer::Initialize: already initialized.");
104 return false;
105 }
106
Steven Thomas94e35b92017-07-26 18:48:28 -0700107 request_display_callback_ = request_display_callback;
108
Corey Tabaka2251d822017-04-20 16:04:07 -0700109 HWC::Error error = HWC::Error::None;
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800110
111 Hwc2::Config config;
Steven Thomas94e35b92017-07-26 18:48:28 -0700112 error = hidl->getActiveConfig(HWC_DISPLAY_PRIMARY, &config);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800113
Corey Tabaka2251d822017-04-20 16:04:07 -0700114 if (error != HWC::Error::None) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800115 ALOGE("HardwareComposer: Failed to get current display config : %d",
116 config);
117 return false;
118 }
119
Steven Thomas94e35b92017-07-26 18:48:28 -0700120 error = GetDisplayMetrics(hidl, HWC_DISPLAY_PRIMARY, config,
121 &native_display_metrics_);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800122
Corey Tabaka2251d822017-04-20 16:04:07 -0700123 if (error != HWC::Error::None) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800124 ALOGE(
125 "HardwareComposer: Failed to get display attributes for current "
126 "configuration : %d",
Corey Tabaka2251d822017-04-20 16:04:07 -0700127 error.value);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800128 return false;
129 }
130
131 ALOGI(
132 "HardwareComposer: primary display attributes: width=%d height=%d "
133 "vsync_period_ns=%d DPI=%dx%d",
134 native_display_metrics_.width, native_display_metrics_.height,
135 native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x,
136 native_display_metrics_.dpi.y);
137
138 // Set the display metrics but never use rotation to avoid the long latency of
139 // rotation processing in hwc.
140 display_transform_ = HWC_TRANSFORM_NONE;
141 display_metrics_ = native_display_metrics_;
142
Corey Tabaka2251d822017-04-20 16:04:07 -0700143 post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
Steven Thomas050b2c82017-03-06 11:45:16 -0800144 LOG_ALWAYS_FATAL_IF(
Corey Tabaka2251d822017-04-20 16:04:07 -0700145 !post_thread_event_fd_,
Steven Thomas050b2c82017-03-06 11:45:16 -0800146 "HardwareComposer: Failed to create interrupt event fd : %s",
147 strerror(errno));
148
149 post_thread_ = std::thread(&HardwareComposer::PostThread, this);
150
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800151 initialized_ = true;
152
153 return initialized_;
154}
155
Steven Thomas050b2c82017-03-06 11:45:16 -0800156void HardwareComposer::Enable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700157 UpdatePostThreadState(PostThreadState::Suspended, false);
Steven Thomas050b2c82017-03-06 11:45:16 -0800158}
159
160void HardwareComposer::Disable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700161 UpdatePostThreadState(PostThreadState::Suspended, true);
Steven Thomas050b2c82017-03-06 11:45:16 -0800162}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800163
Corey Tabaka2251d822017-04-20 16:04:07 -0700164// Update the post thread quiescent state based on idle and suspended inputs.
165void HardwareComposer::UpdatePostThreadState(PostThreadStateType state,
166 bool suspend) {
167 std::unique_lock<std::mutex> lock(post_thread_mutex_);
168
169 // Update the votes in the state variable before evaluating the effective
170 // quiescent state. Any bits set in post_thread_state_ indicate that the post
171 // thread should be suspended.
172 if (suspend) {
173 post_thread_state_ |= state;
174 } else {
175 post_thread_state_ &= ~state;
176 }
177
178 const bool quit = post_thread_state_ & PostThreadState::Quit;
179 const bool effective_suspend = post_thread_state_ != PostThreadState::Active;
180 if (quit) {
181 post_thread_quiescent_ = true;
182 eventfd_write(post_thread_event_fd_.Get(), 1);
183 post_thread_wait_.notify_one();
184 } else if (effective_suspend && !post_thread_quiescent_) {
185 post_thread_quiescent_ = true;
186 eventfd_write(post_thread_event_fd_.Get(), 1);
187 } else if (!effective_suspend && post_thread_quiescent_) {
188 post_thread_quiescent_ = false;
189 eventfd_t value;
190 eventfd_read(post_thread_event_fd_.Get(), &value);
191 post_thread_wait_.notify_one();
192 }
193
194 // Wait until the post thread is in the requested state.
195 post_thread_ready_.wait(lock, [this, effective_suspend] {
196 return effective_suspend != post_thread_resumed_;
197 });
Steven Thomas050b2c82017-03-06 11:45:16 -0800198}
Steven Thomas282a5ed2017-02-07 18:07:01 -0800199
Steven Thomas050b2c82017-03-06 11:45:16 -0800200void HardwareComposer::OnPostThreadResumed() {
Steven Thomas94e35b92017-07-26 18:48:28 -0700201 hidl_.reset(new Hwc2::Composer("default"));
202 hidl_callback_ = new ComposerCallback;
203 hidl_->registerCallback(hidl_callback_);
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700204
Steven Thomas050b2c82017-03-06 11:45:16 -0800205 EnableVsync(true);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800206
Steven Thomas050b2c82017-03-06 11:45:16 -0800207 // TODO(skiazyk): We need to do something about accessing this directly,
208 // supposedly there is a backlight service on the way.
209 // TODO(steventhomas): When we change the backlight setting, will surface
210 // flinger (or something else) set it back to its original value once we give
211 // control of the display back to surface flinger?
212 SetBacklightBrightness(255);
Steven Thomas282a5ed2017-02-07 18:07:01 -0800213
Steven Thomas050b2c82017-03-06 11:45:16 -0800214 // Trigger target-specific performance mode change.
215 property_set(kDvrPerformanceProperty, "performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800216}
217
Steven Thomas050b2c82017-03-06 11:45:16 -0800218void HardwareComposer::OnPostThreadPaused() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700219 retire_fence_fds_.clear();
Steven Thomas050b2c82017-03-06 11:45:16 -0800220 display_surfaces_.clear();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800221
Corey Tabaka2251d822017-04-20 16:04:07 -0700222 for (size_t i = 0; i < kMaxHardwareLayers; ++i) {
223 layers_[i].Reset();
224 }
225 active_layer_count_ = 0;
Steven Thomas050b2c82017-03-06 11:45:16 -0800226
Steven Thomas94e35b92017-07-26 18:48:28 -0700227 if (hidl_) {
228 EnableVsync(false);
229 }
Steven Thomas050b2c82017-03-06 11:45:16 -0800230
Steven Thomas94e35b92017-07-26 18:48:28 -0700231 hidl_callback_ = nullptr;
232 hidl_.reset(nullptr);
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700233
Steven Thomas050b2c82017-03-06 11:45:16 -0800234 // Trigger target-specific performance mode change.
235 property_set(kDvrPerformanceProperty, "idle");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800236}
237
Corey Tabaka2251d822017-04-20 16:04:07 -0700238HWC::Error HardwareComposer::Validate(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800239 uint32_t num_types;
240 uint32_t num_requests;
Corey Tabaka2251d822017-04-20 16:04:07 -0700241 HWC::Error error =
Steven Thomas94e35b92017-07-26 18:48:28 -0700242 hidl_->validateDisplay(display, &num_types, &num_requests);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800243
244 if (error == HWC2_ERROR_HAS_CHANGES) {
245 // TODO(skiazyk): We might need to inspect the requested changes first, but
246 // so far it seems like we shouldn't ever hit a bad state.
247 // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_,
248 // display);
Steven Thomas94e35b92017-07-26 18:48:28 -0700249 error = hidl_->acceptDisplayChanges(display);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800250 }
251
252 return error;
253}
254
Steven Thomas94e35b92017-07-26 18:48:28 -0700255HWC::Error HardwareComposer::EnableVsync(bool enabled) {
256 return hidl_->setVsyncEnabled(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800257 HWC_DISPLAY_PRIMARY,
258 (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
259 : HWC2_VSYNC_DISABLE));
260}
261
Corey Tabaka2251d822017-04-20 16:04:07 -0700262HWC::Error HardwareComposer::Present(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800263 int32_t present_fence;
Steven Thomas94e35b92017-07-26 18:48:28 -0700264 HWC::Error error = hidl_->presentDisplay(display, &present_fence);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800265
266 // According to the documentation, this fence is signaled at the time of
267 // vsync/DMA for physical displays.
Corey Tabaka2251d822017-04-20 16:04:07 -0700268 if (error == HWC::Error::None) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800269 ATRACE_INT("HardwareComposer: VsyncFence", present_fence);
270 retire_fence_fds_.emplace_back(present_fence);
271 } else {
272 ATRACE_INT("HardwareComposer: PresentResult", error);
273 }
274
275 return error;
276}
277
Steven Thomas94e35b92017-07-26 18:48:28 -0700278HWC::Error HardwareComposer::GetDisplayAttribute(Hwc2::Composer* hidl,
279 hwc2_display_t display,
Corey Tabaka2251d822017-04-20 16:04:07 -0700280 hwc2_config_t config,
281 hwc2_attribute_t attribute,
282 int32_t* out_value) const {
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 return hidl->getDisplayAttribute(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800284 display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value);
285}
286
Corey Tabaka2251d822017-04-20 16:04:07 -0700287HWC::Error HardwareComposer::GetDisplayMetrics(
Steven Thomas94e35b92017-07-26 18:48:28 -0700288 Hwc2::Composer* hidl, hwc2_display_t display, hwc2_config_t config,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800289 HWCDisplayMetrics* out_metrics) const {
Corey Tabaka2251d822017-04-20 16:04:07 -0700290 HWC::Error error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800291
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 error = GetDisplayAttribute(hidl, display, config, HWC2_ATTRIBUTE_WIDTH,
Corey Tabaka2251d822017-04-20 16:04:07 -0700293 &out_metrics->width);
294 if (error != HWC::Error::None) {
295 ALOGE(
296 "HardwareComposer::GetDisplayMetrics: Failed to get display width: %s",
297 error.to_string().c_str());
298 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800299 }
300
Steven Thomas94e35b92017-07-26 18:48:28 -0700301 error = GetDisplayAttribute(hidl, display, config, HWC2_ATTRIBUTE_HEIGHT,
Corey Tabaka2251d822017-04-20 16:04:07 -0700302 &out_metrics->height);
303 if (error != HWC::Error::None) {
304 ALOGE(
305 "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s",
306 error.to_string().c_str());
307 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800308 }
309
Steven Thomas94e35b92017-07-26 18:48:28 -0700310 error = GetDisplayAttribute(hidl, display, config,
311 HWC2_ATTRIBUTE_VSYNC_PERIOD,
Corey Tabaka2251d822017-04-20 16:04:07 -0700312 &out_metrics->vsync_period_ns);
313 if (error != HWC::Error::None) {
314 ALOGE(
315 "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s",
316 error.to_string().c_str());
317 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800318 }
319
Steven Thomas94e35b92017-07-26 18:48:28 -0700320 error = GetDisplayAttribute(hidl, display, config, HWC2_ATTRIBUTE_DPI_X,
Corey Tabaka2251d822017-04-20 16:04:07 -0700321 &out_metrics->dpi.x);
322 if (error != HWC::Error::None) {
323 ALOGE(
324 "HardwareComposer::GetDisplayMetrics: Failed to get display DPI X: %s",
325 error.to_string().c_str());
326 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800327 }
328
Steven Thomas94e35b92017-07-26 18:48:28 -0700329 error = GetDisplayAttribute(hidl, display, config, HWC2_ATTRIBUTE_DPI_Y,
Corey Tabaka2251d822017-04-20 16:04:07 -0700330 &out_metrics->dpi.y);
331 if (error != HWC::Error::None) {
332 ALOGE(
333 "HardwareComposer::GetDisplayMetrics: Failed to get display DPI Y: %s",
334 error.to_string().c_str());
335 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800336 }
337
Corey Tabaka2251d822017-04-20 16:04:07 -0700338 return HWC::Error::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800339}
340
Corey Tabaka0b485c92017-05-19 12:02:58 -0700341std::string HardwareComposer::Dump() {
342 std::unique_lock<std::mutex> lock(post_thread_mutex_);
343 std::ostringstream stream;
344
345 stream << "Display metrics: " << display_metrics_.width << "x"
346 << display_metrics_.height << " " << (display_metrics_.dpi.x / 1000.0)
347 << "x" << (display_metrics_.dpi.y / 1000.0) << " dpi @ "
348 << (1000000000.0 / display_metrics_.vsync_period_ns) << " Hz"
349 << std::endl;
350
351 stream << "Post thread resumed: " << post_thread_resumed_ << std::endl;
352 stream << "Active layers: " << active_layer_count_ << std::endl;
353 stream << std::endl;
354
355 for (size_t i = 0; i < active_layer_count_; i++) {
356 stream << "Layer " << i << ":";
357 stream << " type=" << layers_[i].GetCompositionType().to_string();
358 stream << " surface_id=" << layers_[i].GetSurfaceId();
359 stream << " buffer_id=" << layers_[i].GetBufferId();
360 stream << std::endl;
361 }
362 stream << std::endl;
363
364 if (post_thread_resumed_) {
365 stream << "Hardware Composer Debug Info:" << std::endl;
Steven Thomas94e35b92017-07-26 18:48:28 -0700366 stream << hidl_->dumpDebugInfo();
Corey Tabaka0b485c92017-05-19 12:02:58 -0700367 }
368
369 return stream.str();
370}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800371
Corey Tabaka2251d822017-04-20 16:04:07 -0700372void HardwareComposer::PostLayers() {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800373 ATRACE_NAME("HardwareComposer::PostLayers");
374
375 // Setup the hardware composer layers with current buffers.
376 for (size_t i = 0; i < active_layer_count_; i++) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700377 layers_[i].Prepare();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800378 }
379
Corey Tabaka2251d822017-04-20 16:04:07 -0700380 HWC::Error error = Validate(HWC_DISPLAY_PRIMARY);
381 if (error != HWC::Error::None) {
382 ALOGE("HardwareComposer::PostLayers: Validate failed: %s",
383 error.to_string().c_str());
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700384 return;
385 }
386
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800387 // Now that we have taken in a frame from the application, we have a chance
388 // to drop the frame before passing the frame along to HWC.
389 // If the display driver has become backed up, we detect it here and then
390 // react by skipping this frame to catch up latency.
391 while (!retire_fence_fds_.empty() &&
392 (!retire_fence_fds_.front() ||
393 sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
394 // There are only 2 fences in here, no performance problem to shift the
395 // array of ints.
396 retire_fence_fds_.erase(retire_fence_fds_.begin());
397 }
398
399 const bool is_frame_pending = IsFramePendingInDriver();
George Burgess IV353a6f62017-06-26 17:13:09 -0700400 const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) >
John Bates954796e2017-05-11 11:00:31 -0700401 post_thread_config_.allowed_pending_fence_count;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800402
403 if (is_fence_pending || is_frame_pending) {
404 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
405
406 ALOGW_IF(is_frame_pending, "Warning: frame already queued, dropping frame");
407 ALOGW_IF(is_fence_pending,
408 "Warning: dropping a frame to catch up with HWC (pending = %zd)",
409 retire_fence_fds_.size());
410
411 for (size_t i = 0; i < active_layer_count_; i++) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700412 layers_[i].Drop();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800413 }
414 return;
415 } else {
416 // Make the transition more obvious in systrace when the frame skip happens
417 // above.
418 ATRACE_INT("frame_skip_count", 0);
419 }
420
Corey Tabaka89bbefc2017-06-06 16:14:21 -0700421#if TRACE > 1
Corey Tabaka0b485c92017-05-19 12:02:58 -0700422 for (size_t i = 0; i < active_layer_count_; i++) {
423 ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s",
424 i, layers_[i].GetBufferId(),
Corey Tabaka2251d822017-04-20 16:04:07 -0700425 layers_[i].GetCompositionType().to_string().c_str());
Corey Tabaka0b485c92017-05-19 12:02:58 -0700426 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800427#endif
428
Corey Tabaka2251d822017-04-20 16:04:07 -0700429 error = Present(HWC_DISPLAY_PRIMARY);
430 if (error != HWC::Error::None) {
431 ALOGE("HardwareComposer::PostLayers: Present failed: %s",
432 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800433 return;
434 }
435
436 std::vector<Hwc2::Layer> out_layers;
437 std::vector<int> out_fences;
Steven Thomas94e35b92017-07-26 18:48:28 -0700438 error = hidl_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers,
439 &out_fences);
Corey Tabaka2251d822017-04-20 16:04:07 -0700440 ALOGE_IF(error != HWC::Error::None,
441 "HardwareComposer::PostLayers: Failed to get release fences: %s",
442 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800443
444 // Perform post-frame bookkeeping. Unused layers are a no-op.
Corey Tabaka2251d822017-04-20 16:04:07 -0700445 uint32_t num_elements = out_layers.size();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800446 for (size_t i = 0; i < num_elements; ++i) {
447 for (size_t j = 0; j < active_layer_count_; ++j) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700448 if (layers_[j].GetLayerHandle() == out_layers[i]) {
449 layers_[j].Finish(out_fences[i]);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800450 }
451 }
452 }
453}
454
Steven Thomas050b2c82017-03-06 11:45:16 -0800455void HardwareComposer::SetDisplaySurfaces(
Corey Tabaka2251d822017-04-20 16:04:07 -0700456 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) {
Jin Qian7480c062017-03-21 00:04:15 +0000457 ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
458 surfaces.size());
Corey Tabaka2251d822017-04-20 16:04:07 -0700459 const bool display_idle = surfaces.size() == 0;
460 {
461 std::unique_lock<std::mutex> lock(post_thread_mutex_);
462 pending_surfaces_ = std::move(surfaces);
463 }
464
Steven Thomas2ddf5672017-06-15 11:38:40 -0700465 if (request_display_callback_)
466 request_display_callback_(!display_idle);
467
Corey Tabaka2251d822017-04-20 16:04:07 -0700468 // Set idle state based on whether there are any surfaces to handle.
469 UpdatePostThreadState(PostThreadState::Idle, display_idle);
Steven Thomas050b2c82017-03-06 11:45:16 -0800470}
Jin Qian7480c062017-03-21 00:04:15 +0000471
John Bates954796e2017-05-11 11:00:31 -0700472int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key,
473 IonBuffer& ion_buffer) {
Okan Arikan822b7102017-05-08 13:31:34 -0700474 if (key == DvrGlobalBuffers::kVsyncBuffer) {
475 vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>(
476 &ion_buffer, CPUUsageMode::WRITE_OFTEN);
477
478 if (vsync_ring_->IsMapped() == false) {
479 return -EPERM;
480 }
481 }
482
483 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700484 return MapConfigBuffer(ion_buffer);
485 }
486
487 return 0;
488}
489
490void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) {
Okan Arikan822b7102017-05-08 13:31:34 -0700491 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700492 ConfigBufferDeleted();
493 }
494}
495
496int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) {
497 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700498 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700499
Okan Arikan6f468c62017-05-31 14:48:30 -0700500 if (ion_buffer.width() < DvrConfigRing::MemorySize()) {
John Bates954796e2017-05-11 11:00:31 -0700501 ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size.");
502 return -EINVAL;
503 }
504
505 void* buffer_base = 0;
506 int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(),
507 ion_buffer.height(), &buffer_base);
508 if (result != 0) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700509 ALOGE(
510 "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config "
511 "buffer.");
John Bates954796e2017-05-11 11:00:31 -0700512 return -EPERM;
513 }
514
Okan Arikan6f468c62017-05-31 14:48:30 -0700515 shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width());
John Bates954796e2017-05-11 11:00:31 -0700516 ion_buffer.Unlock();
517
518 return 0;
519}
520
521void HardwareComposer::ConfigBufferDeleted() {
522 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700523 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700524}
525
526void HardwareComposer::UpdateConfigBuffer() {
527 std::lock_guard<std::mutex> lock(shared_config_mutex_);
528 if (!shared_config_ring_.is_valid())
529 return;
530 // Copy from latest record in shared_config_ring_ to local copy.
Okan Arikan6f468c62017-05-31 14:48:30 -0700531 DvrConfig record;
John Bates954796e2017-05-11 11:00:31 -0700532 if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) {
533 post_thread_config_ = record;
534 }
535}
536
Corey Tabaka2251d822017-04-20 16:04:07 -0700537int HardwareComposer::PostThreadPollInterruptible(
Steven Thomas94e35b92017-07-26 18:48:28 -0700538 const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800539 pollfd pfd[2] = {
540 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700541 .fd = event_fd.Get(),
Steven Thomas66747c12017-03-22 18:45:31 -0700542 .events = static_cast<short>(requested_events),
543 .revents = 0,
Steven Thomas050b2c82017-03-06 11:45:16 -0800544 },
545 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700546 .fd = post_thread_event_fd_.Get(),
Steven Thomas050b2c82017-03-06 11:45:16 -0800547 .events = POLLPRI | POLLIN,
548 .revents = 0,
549 },
550 };
551 int ret, error;
552 do {
Steven Thomas94e35b92017-07-26 18:48:28 -0700553 ret = poll(pfd, 2, timeout_ms);
Steven Thomas050b2c82017-03-06 11:45:16 -0800554 error = errno;
555 ALOGW_IF(ret < 0,
556 "HardwareComposer::PostThreadPollInterruptible: Error during "
557 "poll(): %s (%d)",
558 strerror(error), error);
559 } while (ret < 0 && error == EINTR);
560
561 if (ret < 0) {
562 return -error;
Steven Thomas94e35b92017-07-26 18:48:28 -0700563 } else if (ret == 0) {
564 return -ETIMEDOUT;
Steven Thomas050b2c82017-03-06 11:45:16 -0800565 } else if (pfd[0].revents != 0) {
566 return 0;
567 } else if (pfd[1].revents != 0) {
568 ALOGI("VrHwcPost thread interrupted");
569 return kPostThreadInterrupted;
570 } else {
571 return 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800572 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800573}
574
575// Reads the value of the display driver wait_pingpong state. Returns 0 or 1
576// (the value of the state) on success or a negative error otherwise.
577// TODO(eieio): This is pretty driver specific, this should be moved to a
578// separate class eventually.
579int HardwareComposer::ReadWaitPPState() {
580 // Gracefully handle when the kernel does not support this feature.
581 if (!primary_display_wait_pp_fd_)
582 return 0;
583
584 const int wait_pp_fd = primary_display_wait_pp_fd_.Get();
585 int ret, error;
586
587 ret = lseek(wait_pp_fd, 0, SEEK_SET);
588 if (ret < 0) {
589 error = errno;
590 ALOGE("HardwareComposer::ReadWaitPPState: Failed to seek wait_pp fd: %s",
591 strerror(error));
592 return -error;
593 }
594
595 char data = -1;
596 ret = read(wait_pp_fd, &data, sizeof(data));
597 if (ret < 0) {
598 error = errno;
599 ALOGE("HardwareComposer::ReadWaitPPState: Failed to read wait_pp state: %s",
600 strerror(error));
601 return -error;
602 }
603
604 switch (data) {
605 case '0':
606 return 0;
607 case '1':
608 return 1;
609 default:
610 ALOGE(
611 "HardwareComposer::ReadWaitPPState: Unexpected value for wait_pp: %d",
612 data);
613 return -EINVAL;
614 }
615}
616
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800617// Waits for the next vsync and returns the timestamp of the vsync event. If
618// vsync already passed since the last call, returns the latest vsync timestamp
Steven Thomas94e35b92017-07-26 18:48:28 -0700619// instead of blocking.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800620int HardwareComposer::WaitForVSync(int64_t* timestamp) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700621 int error = PostThreadPollInterruptible(
622 hidl_callback_->GetVsyncEventFd(), POLLIN, /*timeout_ms*/ 1000);
623 if (error == kPostThreadInterrupted || error < 0) {
624 return error;
625 } else {
626 *timestamp = hidl_callback_->GetVsyncTime();
627 return 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800628 }
629}
630
631int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
632 const int timer_fd = vsync_sleep_timer_fd_.Get();
633 const itimerspec wakeup_itimerspec = {
634 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
635 .it_value = NsToTimespec(wakeup_timestamp),
636 };
637 int ret =
638 timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
639 int error = errno;
640 if (ret < 0) {
641 ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
642 strerror(error));
643 return -error;
644 }
645
Steven Thomas94e35b92017-07-26 18:48:28 -0700646 return PostThreadPollInterruptible(
647 vsync_sleep_timer_fd_, POLLIN, /*timeout_ms*/ -1);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800648}
649
650void HardwareComposer::PostThread() {
651 // NOLINTNEXTLINE(runtime/int)
Steven Thomas050b2c82017-03-06 11:45:16 -0800652 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800653
Corey Tabaka2251d822017-04-20 16:04:07 -0700654 // Set the scheduler to SCHED_FIFO with high priority. If this fails here
655 // there may have been a startup timing issue between this thread and
656 // performanced. Try again later when this thread becomes active.
657 bool thread_policy_setup =
658 SetThreadPolicy("graphics:high", "/system/performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800659
Steven Thomas050b2c82017-03-06 11:45:16 -0800660#if ENABLE_BACKLIGHT_BRIGHTNESS
661 // TODO(hendrikw): This isn't required at the moment. It's possible that there
662 // is another method to access this when needed.
663 // Open the backlight brightness control sysfs node.
664 backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR);
665 ALOGW_IF(!backlight_brightness_fd_,
666 "HardwareComposer: Failed to open backlight brightness control: %s",
667 strerror(errno));
Corey Tabaka2251d822017-04-20 16:04:07 -0700668#endif // ENABLE_BACKLIGHT_BRIGHTNESS
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800669
Steven Thomas050b2c82017-03-06 11:45:16 -0800670 // Open the wait pingpong status node for the primary display.
671 // TODO(eieio): Move this into a platform-specific class.
672 primary_display_wait_pp_fd_ =
673 LocalHandle(kPrimaryDisplayWaitPPEventFile, O_RDONLY);
674 ALOGW_IF(
675 !primary_display_wait_pp_fd_,
676 "HardwareComposer: Failed to open wait_pp node for primary display: %s",
677 strerror(errno));
678
679 // Create a timerfd based on CLOCK_MONOTINIC.
680 vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
681 LOG_ALWAYS_FATAL_IF(
682 !vsync_sleep_timer_fd_,
683 "HardwareComposer: Failed to create vsync sleep timerfd: %s",
684 strerror(errno));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800685
686 const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
687 const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame);
688
689 // TODO(jbates) Query vblank time from device, when such an API is available.
690 // This value (6.3%) was measured on A00 in low persistence mode.
691 int64_t vblank_ns = ns_per_frame * 63 / 1000;
692 int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2;
693
694 // Check property for overriding right eye offset value.
695 right_eye_photon_offset_ns =
696 property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns);
697
Steven Thomas050b2c82017-03-06 11:45:16 -0800698 bool was_running = false;
699
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800700 while (1) {
701 ATRACE_NAME("HardwareComposer::PostThread");
702
John Bates954796e2017-05-11 11:00:31 -0700703 // Check for updated config once per vsync.
704 UpdateConfigBuffer();
705
Corey Tabaka2251d822017-04-20 16:04:07 -0700706 while (post_thread_quiescent_) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800707 std::unique_lock<std::mutex> lock(post_thread_mutex_);
Corey Tabaka2251d822017-04-20 16:04:07 -0700708 ALOGI("HardwareComposer::PostThread: Entering quiescent state.");
709
Corey Tabakadf0b9162017-08-03 17:14:08 -0700710 // Tear down resources if necessary.
711 if (was_running)
712 OnPostThreadPaused();
Corey Tabaka2251d822017-04-20 16:04:07 -0700713
714 was_running = false;
715 post_thread_resumed_ = false;
716 post_thread_ready_.notify_all();
717
718 if (post_thread_state_ & PostThreadState::Quit) {
719 ALOGI("HardwareComposer::PostThread: Quitting.");
720 return;
Steven Thomas282a5ed2017-02-07 18:07:01 -0800721 }
Corey Tabaka2251d822017-04-20 16:04:07 -0700722
723 post_thread_wait_.wait(lock, [this] { return !post_thread_quiescent_; });
724
725 post_thread_resumed_ = true;
726 post_thread_ready_.notify_all();
727
728 ALOGI("HardwareComposer::PostThread: Exiting quiescent state.");
Steven Thomas050b2c82017-03-06 11:45:16 -0800729 }
730
731 if (!was_running) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700732 // Setup resources.
Steven Thomas050b2c82017-03-06 11:45:16 -0800733 OnPostThreadResumed();
734 was_running = true;
Corey Tabaka2251d822017-04-20 16:04:07 -0700735
736 // Try to setup the scheduler policy if it failed during startup. Only
737 // attempt to do this on transitions from inactive to active to avoid
738 // spamming the system with RPCs and log messages.
739 if (!thread_policy_setup) {
740 thread_policy_setup =
741 SetThreadPolicy("graphics:high", "/system/performance");
742 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800743 }
744
745 int64_t vsync_timestamp = 0;
746 {
747 std::array<char, 128> buf;
748 snprintf(buf.data(), buf.size(), "wait_vsync|vsync=%d|",
749 vsync_count_ + 1);
750 ATRACE_NAME(buf.data());
751
Corey Tabaka2251d822017-04-20 16:04:07 -0700752 const int error = WaitForVSync(&vsync_timestamp);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800753 ALOGE_IF(
754 error < 0,
755 "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
756 strerror(-error));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800757 // Don't bother processing this frame if a pause was requested
Steven Thomas050b2c82017-03-06 11:45:16 -0800758 if (error == kPostThreadInterrupted)
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800759 continue;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800760 }
761
762 ++vsync_count_;
763
Corey Tabaka2251d822017-04-20 16:04:07 -0700764 const bool layer_config_changed = UpdateLayerConfig();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800765
Okan Arikan822b7102017-05-08 13:31:34 -0700766 // Publish the vsync event.
767 if (vsync_ring_) {
768 DvrVsync vsync;
769 vsync.vsync_count = vsync_count_;
770 vsync.vsync_timestamp_ns = vsync_timestamp;
771 vsync.vsync_left_eye_offset_ns = photon_offset_ns;
772 vsync.vsync_right_eye_offset_ns = right_eye_photon_offset_ns;
773 vsync.vsync_period_ns = ns_per_frame;
774
775 vsync_ring_->Publish(vsync);
776 }
777
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800778 // Signal all of the vsync clients. Because absolute time is used for the
779 // wakeup time below, this can take a little time if necessary.
780 if (vsync_callback_)
Corey Tabaka2251d822017-04-20 16:04:07 -0700781 vsync_callback_(HWC_DISPLAY_PRIMARY, vsync_timestamp,
782 /*frame_time_estimate*/ 0, vsync_count_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800783
784 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700785 // Sleep until shortly before vsync.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800786 ATRACE_NAME("sleep");
787
Corey Tabaka2251d822017-04-20 16:04:07 -0700788 const int64_t display_time_est_ns = vsync_timestamp + ns_per_frame;
789 const int64_t now_ns = GetSystemClockNs();
John Bates954796e2017-05-11 11:00:31 -0700790 const int64_t sleep_time_ns = display_time_est_ns - now_ns -
791 post_thread_config_.frame_post_offset_ns;
792 const int64_t wakeup_time_ns =
793 display_time_est_ns - post_thread_config_.frame_post_offset_ns;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800794
795 ATRACE_INT64("sleep_time_ns", sleep_time_ns);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800796 if (sleep_time_ns > 0) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700797 int error = SleepUntil(wakeup_time_ns);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800798 ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s",
799 strerror(-error));
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700800 if (error == kPostThreadInterrupted) {
801 if (layer_config_changed) {
802 // If the layer config changed we need to validateDisplay() even if
803 // we're going to drop the frame, to flush the Composer object's
804 // internal command buffer and apply our layer changes.
805 Validate(HWC_DISPLAY_PRIMARY);
806 }
Steven Thomas050b2c82017-03-06 11:45:16 -0800807 continue;
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700808 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800809 }
810 }
811
Corey Tabaka2251d822017-04-20 16:04:07 -0700812 PostLayers();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800813 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800814}
815
Corey Tabaka2251d822017-04-20 16:04:07 -0700816// Checks for changes in the surface stack and updates the layer config to
817// accomodate the new stack.
Steven Thomas050b2c82017-03-06 11:45:16 -0800818bool HardwareComposer::UpdateLayerConfig() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700819 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces;
Steven Thomas050b2c82017-03-06 11:45:16 -0800820 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700821 std::unique_lock<std::mutex> lock(post_thread_mutex_);
822 if (pending_surfaces_.empty())
Steven Thomas050b2c82017-03-06 11:45:16 -0800823 return false;
Corey Tabaka2251d822017-04-20 16:04:07 -0700824
825 surfaces = std::move(pending_surfaces_);
Steven Thomas050b2c82017-03-06 11:45:16 -0800826 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800827
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800828 ATRACE_NAME("UpdateLayerConfig_HwLayers");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800829
Corey Tabaka2251d822017-04-20 16:04:07 -0700830 display_surfaces_.clear();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800831
Corey Tabaka2251d822017-04-20 16:04:07 -0700832 Layer* target_layer;
833 size_t layer_index;
834 for (layer_index = 0;
835 layer_index < std::min(surfaces.size(), kMaxHardwareLayers);
836 layer_index++) {
837 // The bottom layer is opaque, other layers blend.
838 HWC::BlendMode blending =
839 layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage;
Steven Thomas94e35b92017-07-26 18:48:28 -0700840 layers_[layer_index].Setup(surfaces[layer_index], native_display_metrics_,
841 hidl_.get(), blending,
Corey Tabaka2251d822017-04-20 16:04:07 -0700842 display_transform_, HWC::Composition::Device,
843 layer_index);
844 display_surfaces_.push_back(surfaces[layer_index]);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800845 }
846
Corey Tabaka2251d822017-04-20 16:04:07 -0700847 // Clear unused layers.
848 for (size_t i = layer_index; i < kMaxHardwareLayers; i++)
849 layers_[i].Reset();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800850
Corey Tabaka2251d822017-04-20 16:04:07 -0700851 active_layer_count_ = layer_index;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800852 ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
853 active_layer_count_);
854
Corey Tabaka2251d822017-04-20 16:04:07 -0700855 // Any surfaces left over could not be assigned a hardware layer and will
856 // not be displayed.
857 ALOGW_IF(surfaces.size() != display_surfaces_.size(),
858 "HardwareComposer::UpdateLayerConfig: More surfaces than layers: "
859 "pending_surfaces=%zu display_surfaces=%zu",
860 surfaces.size(), display_surfaces_.size());
861
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800862 return true;
863}
864
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800865void HardwareComposer::SetVSyncCallback(VSyncCallback callback) {
866 vsync_callback_ = callback;
867}
868
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800869void HardwareComposer::SetBacklightBrightness(int brightness) {
870 if (backlight_brightness_fd_) {
871 std::array<char, 32> text;
872 const int length = snprintf(text.data(), text.size(), "%d", brightness);
873 write(backlight_brightness_fd_.Get(), text.data(), length);
874 }
875}
876
Steven Thomas94e35b92017-07-26 18:48:28 -0700877HardwareComposer::ComposerCallback::ComposerCallback() {
878 vsync_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
879 LOG_ALWAYS_FATAL_IF(
880 !vsync_event_fd_,
881 "Failed to create vsync event fd : %s",
882 strerror(errno));
883}
884
885Return<void> HardwareComposer::ComposerCallback::onHotplug(
886 Hwc2::Display /*display*/,
887 IComposerCallback::Connection /*conn*/) {
888 return Void();
889}
890
891Return<void> HardwareComposer::ComposerCallback::onRefresh(
892 Hwc2::Display /*display*/) {
893 return hardware::Void();
894}
895
896Return<void> HardwareComposer::ComposerCallback::onVsync(
897 Hwc2::Display display, int64_t timestamp) {
898 if (display == HWC_DISPLAY_PRIMARY) {
899 std::lock_guard<std::mutex> lock(vsync_mutex_);
900 vsync_time_ = timestamp;
901 int error = eventfd_write(vsync_event_fd_.Get(), 1);
902 LOG_ALWAYS_FATAL_IF(error != 0, "Failed writing to vsync event fd");
903 }
904 return Void();
905}
906
907const pdx::LocalHandle&
908HardwareComposer::ComposerCallback::GetVsyncEventFd() const {
909 return vsync_event_fd_;
910}
911
912int64_t HardwareComposer::ComposerCallback::GetVsyncTime() {
913 std::lock_guard<std::mutex> lock(vsync_mutex_);
914 eventfd_t event;
915 eventfd_read(vsync_event_fd_.Get(), &event);
916 LOG_ALWAYS_FATAL_IF(vsync_time_ < 0,
917 "Attempt to read vsync time before vsync event");
918 int64_t return_val = vsync_time_;
919 vsync_time_ = -1;
920 return return_val;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800921}
922
923void Layer::Reset() {
Steven Thomas94e35b92017-07-26 18:48:28 -0700924 if (hidl_ != nullptr && hardware_composer_layer_) {
925 hidl_->destroyLayer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800926 hardware_composer_layer_ = 0;
927 }
928
Steven Thomas94e35b92017-07-26 18:48:28 -0700929 hidl_ = nullptr;
Corey Tabaka2251d822017-04-20 16:04:07 -0700930 z_order_ = 0;
931 blending_ = HWC::BlendMode::None;
932 transform_ = HWC::Transform::None;
933 composition_type_ = HWC::Composition::Invalid;
934 target_composition_type_ = composition_type_;
935 source_ = EmptyVariant{};
936 acquire_fence_.Close();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800937 surface_rect_functions_applied_ = false;
938}
939
Corey Tabaka2251d822017-04-20 16:04:07 -0700940void Layer::Setup(const std::shared_ptr<DirectDisplaySurface>& surface,
Steven Thomas94e35b92017-07-26 18:48:28 -0700941 const HWCDisplayMetrics& display_metrics,
942 Hwc2::Composer* hidl, HWC::BlendMode blending,
943 HWC::Transform transform, HWC::Composition composition_type,
944 size_t z_order) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800945 Reset();
Steven Thomas94e35b92017-07-26 18:48:28 -0700946 hidl_ = hidl;
Corey Tabaka2251d822017-04-20 16:04:07 -0700947 z_order_ = z_order;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800948 blending_ = blending;
949 transform_ = transform;
Corey Tabaka2251d822017-04-20 16:04:07 -0700950 composition_type_ = HWC::Composition::Invalid;
951 target_composition_type_ = composition_type;
952 source_ = SourceSurface{surface};
Steven Thomas94e35b92017-07-26 18:48:28 -0700953 CommonLayerSetup(display_metrics);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800954}
955
956void Layer::Setup(const std::shared_ptr<IonBuffer>& buffer,
Steven Thomas94e35b92017-07-26 18:48:28 -0700957 const HWCDisplayMetrics& display_metrics,
958 Hwc2::Composer* hidl, HWC::BlendMode blending,
959 HWC::Transform transform, HWC::Composition composition_type,
960 size_t z_order) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800961 Reset();
Steven Thomas94e35b92017-07-26 18:48:28 -0700962 hidl_ = hidl;
Corey Tabaka2251d822017-04-20 16:04:07 -0700963 z_order_ = z_order;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800964 blending_ = blending;
965 transform_ = transform;
Corey Tabaka2251d822017-04-20 16:04:07 -0700966 composition_type_ = HWC::Composition::Invalid;
967 target_composition_type_ = composition_type;
968 source_ = SourceBuffer{buffer};
Steven Thomas94e35b92017-07-26 18:48:28 -0700969 CommonLayerSetup(display_metrics);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800970}
971
Corey Tabaka2251d822017-04-20 16:04:07 -0700972void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) {
973 if (source_.is<SourceBuffer>())
974 std::get<SourceBuffer>(source_) = {buffer};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800975}
976
Corey Tabaka2251d822017-04-20 16:04:07 -0700977void Layer::SetBlending(HWC::BlendMode blending) { blending_ = blending; }
978void Layer::SetZOrder(size_t z_order) { z_order_ = z_order; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800979
980IonBuffer* Layer::GetBuffer() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700981 struct Visitor {
982 IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); }
983 IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); }
984 IonBuffer* operator()(EmptyVariant) { return nullptr; }
985 };
986 return source_.Visit(Visitor{});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800987}
988
Steven Thomas94e35b92017-07-26 18:48:28 -0700989void Layer::UpdateLayerSettings(const HWCDisplayMetrics& display_metrics) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800990 if (!IsLayerSetup()) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700991 ALOGE(
992 "HardwareComposer::Layer::UpdateLayerSettings: Attempt to update "
993 "unused Layer!");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800994 return;
995 }
996
Corey Tabaka2251d822017-04-20 16:04:07 -0700997 HWC::Error error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800998 hwc2_display_t display = HWC_DISPLAY_PRIMARY;
999
Steven Thomas94e35b92017-07-26 18:48:28 -07001000 error = hidl_->setLayerCompositionType(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001001 display, hardware_composer_layer_,
Corey Tabaka2251d822017-04-20 16:04:07 -07001002 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1003 ALOGE_IF(
1004 error != HWC::Error::None,
1005 "Layer::UpdateLayerSettings: Error setting layer composition type: %s",
1006 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001007
Steven Thomas94e35b92017-07-26 18:48:28 -07001008 error = hidl_->setLayerBlendMode(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001009 display, hardware_composer_layer_,
Corey Tabaka2251d822017-04-20 16:04:07 -07001010 blending_.cast<Hwc2::IComposerClient::BlendMode>());
1011 ALOGE_IF(error != HWC::Error::None,
1012 "Layer::UpdateLayerSettings: Error setting layer blend mode: %s",
1013 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001014
Corey Tabaka2251d822017-04-20 16:04:07 -07001015 // TODO(eieio): Use surface attributes or some other mechanism to control
1016 // the layer display frame.
Steven Thomas94e35b92017-07-26 18:48:28 -07001017 error = hidl_->setLayerDisplayFrame(
Corey Tabaka2251d822017-04-20 16:04:07 -07001018 display, hardware_composer_layer_,
Steven Thomas94e35b92017-07-26 18:48:28 -07001019 {0, 0, display_metrics.width, display_metrics.height});
Corey Tabaka2251d822017-04-20 16:04:07 -07001020 ALOGE_IF(error != HWC::Error::None,
1021 "Layer::UpdateLayerSettings: Error setting layer display frame: %s",
1022 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001023
Steven Thomas94e35b92017-07-26 18:48:28 -07001024 error = hidl_->setLayerVisibleRegion(
Corey Tabaka2251d822017-04-20 16:04:07 -07001025 display, hardware_composer_layer_,
Steven Thomas94e35b92017-07-26 18:48:28 -07001026 {{0, 0, display_metrics.width, display_metrics.height}});
Corey Tabaka2251d822017-04-20 16:04:07 -07001027 ALOGE_IF(error != HWC::Error::None,
1028 "Layer::UpdateLayerSettings: Error setting layer visible region: %s",
1029 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001030
Steven Thomas94e35b92017-07-26 18:48:28 -07001031 error = hidl_->setLayerPlaneAlpha(display, hardware_composer_layer_, 1.0f);
Corey Tabaka2251d822017-04-20 16:04:07 -07001032 ALOGE_IF(error != HWC::Error::None,
1033 "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s",
1034 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001035
Steven Thomas94e35b92017-07-26 18:48:28 -07001036 error = hidl_->setLayerZOrder(display, hardware_composer_layer_, z_order_);
Corey Tabaka2251d822017-04-20 16:04:07 -07001037 ALOGE_IF(error != HWC::Error::None,
1038 "Layer::UpdateLayerSettings: Error setting z_ order: %s",
1039 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001040}
1041
Steven Thomas94e35b92017-07-26 18:48:28 -07001042void Layer::CommonLayerSetup(const HWCDisplayMetrics& display_metrics) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001043 HWC::Error error =
Steven Thomas94e35b92017-07-26 18:48:28 -07001044 hidl_->createLayer(HWC_DISPLAY_PRIMARY, &hardware_composer_layer_);
Corey Tabaka2251d822017-04-20 16:04:07 -07001045 ALOGE_IF(
1046 error != HWC::Error::None,
1047 "Layer::CommonLayerSetup: Failed to create layer on primary display: %s",
1048 error.to_string().c_str());
Steven Thomas94e35b92017-07-26 18:48:28 -07001049 UpdateLayerSettings(display_metrics);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001050}
1051
1052void Layer::Prepare() {
1053 int right, bottom;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -04001054 sp<GraphicBuffer> handle;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001055
Corey Tabaka2251d822017-04-20 16:04:07 -07001056 // Acquire the next buffer according to the type of source.
1057 IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) {
1058 std::tie(right, bottom, handle, acquire_fence_) = source.Acquire();
1059 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001060
Corey Tabaka2251d822017-04-20 16:04:07 -07001061 // When a layer is first setup there may be some time before the first buffer
1062 // arrives. Setup the HWC layer as a solid color to stall for time until the
1063 // first buffer arrives. Once the first buffer arrives there will always be a
1064 // buffer for the frame even if it is old.
1065 if (!handle.get()) {
1066 if (composition_type_ == HWC::Composition::Invalid) {
1067 composition_type_ = HWC::Composition::SolidColor;
Steven Thomas94e35b92017-07-26 18:48:28 -07001068 hidl_->setLayerCompositionType(
Corey Tabaka2251d822017-04-20 16:04:07 -07001069 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1070 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1071 Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0};
Steven Thomas94e35b92017-07-26 18:48:28 -07001072 hidl_->setLayerColor(HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1073 layer_color);
Corey Tabaka2251d822017-04-20 16:04:07 -07001074 } else {
1075 // The composition type is already set. Nothing else to do until a
1076 // buffer arrives.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001077 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001078 } else {
Corey Tabaka2251d822017-04-20 16:04:07 -07001079 if (composition_type_ != target_composition_type_) {
1080 composition_type_ = target_composition_type_;
Steven Thomas94e35b92017-07-26 18:48:28 -07001081 hidl_->setLayerCompositionType(
Corey Tabaka2251d822017-04-20 16:04:07 -07001082 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1083 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1084 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001085
Corey Tabaka2251d822017-04-20 16:04:07 -07001086 HWC::Error error{HWC::Error::None};
Steven Thomas94e35b92017-07-26 18:48:28 -07001087 error = hidl_->setLayerBuffer(HWC_DISPLAY_PRIMARY,
1088 hardware_composer_layer_, 0, handle,
1089 acquire_fence_.Get());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001090
Corey Tabaka2251d822017-04-20 16:04:07 -07001091 ALOGE_IF(error != HWC::Error::None,
1092 "Layer::Prepare: Error setting layer buffer: %s",
1093 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001094
Corey Tabaka2251d822017-04-20 16:04:07 -07001095 if (!surface_rect_functions_applied_) {
1096 const float float_right = right;
1097 const float float_bottom = bottom;
Steven Thomas94e35b92017-07-26 18:48:28 -07001098 error = hidl_->setLayerSourceCrop(HWC_DISPLAY_PRIMARY,
1099 hardware_composer_layer_,
1100 {0, 0, float_right, float_bottom});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001101
Corey Tabaka2251d822017-04-20 16:04:07 -07001102 ALOGE_IF(error != HWC::Error::None,
1103 "Layer::Prepare: Error setting layer source crop: %s",
1104 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001105
Corey Tabaka2251d822017-04-20 16:04:07 -07001106 surface_rect_functions_applied_ = true;
1107 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001108 }
1109}
1110
1111void Layer::Finish(int release_fence_fd) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001112 IfAnyOf<SourceSurface, SourceBuffer>::Call(
1113 &source_, [release_fence_fd](auto& source) {
1114 source.Finish(LocalHandle(release_fence_fd));
1115 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001116}
1117
Corey Tabaka2251d822017-04-20 16:04:07 -07001118void Layer::Drop() { acquire_fence_.Close(); }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001119
1120} // namespace dvr
1121} // namespace android