blob: 3f0371c99a474efd46421a2c7973115f18400349 [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>
Corey Tabakab3732f02017-09-16 00:58:54 -07008#include <stdint.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08009#include <sync/sync.h>
10#include <sys/eventfd.h>
11#include <sys/prctl.h>
12#include <sys/resource.h>
13#include <sys/system_properties.h>
14#include <sys/timerfd.h>
Corey Tabakab3732f02017-09-16 00:58:54 -070015#include <sys/types.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070016#include <time.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080017#include <unistd.h>
18#include <utils/Trace.h>
19
20#include <algorithm>
Corey Tabaka2251d822017-04-20 16:04:07 -070021#include <chrono>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080022#include <functional>
23#include <map>
Corey Tabaka0b485c92017-05-19 12:02:58 -070024#include <sstream>
25#include <string>
John Bates954796e2017-05-11 11:00:31 -070026#include <tuple>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080027
Corey Tabaka2251d822017-04-20 16:04:07 -070028#include <dvr/dvr_display_types.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080029#include <dvr/performance_client_api.h>
30#include <private/dvr/clock_ns.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070031#include <private/dvr/ion_buffer.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080032
Steven Thomasb02664d2017-07-26 18:48:28 -070033using android::hardware::Return;
34using android::hardware::Void;
Corey Tabakab3732f02017-09-16 00:58:54 -070035using android::pdx::ErrorStatus;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080036using android::pdx::LocalHandle;
Corey Tabakab3732f02017-09-16 00:58:54 -070037using android::pdx::Status;
Corey Tabaka2251d822017-04-20 16:04:07 -070038using android::pdx::rpc::EmptyVariant;
39using android::pdx::rpc::IfAnyOf;
40
41using namespace std::chrono_literals;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080042
43namespace android {
44namespace dvr {
45
46namespace {
47
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080048const char kBacklightBrightnessSysFile[] =
49 "/sys/class/leds/lcd-backlight/brightness";
50
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080051const char kDvrPerformanceProperty[] = "sys.dvr.performance";
Corey Tabaka451256f2017-08-22 11:59:15 -070052const char kDvrStandaloneProperty[] = "ro.boot.vr";
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080053
Luke Song4b788322017-03-24 14:17:31 -070054const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns";
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080055
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080056// Get time offset from a vsync to when the pose for that vsync should be
57// predicted out to. For example, if scanout gets halfway through the frame
58// at the halfway point between vsyncs, then this could be half the period.
59// With global shutter displays, this should be changed to the offset to when
60// illumination begins. Low persistence adds a frame of latency, so we predict
61// to the center of the next frame.
62inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
63 return (vsync_period_ns * 150) / 100;
64}
65
Corey Tabaka2251d822017-04-20 16:04:07 -070066// Attempts to set the scheduler class and partiton for the current thread.
67// Returns true on success or false on failure.
68bool SetThreadPolicy(const std::string& scheduler_class,
69 const std::string& partition) {
70 int error = dvrSetSchedulerClass(0, scheduler_class.c_str());
71 if (error < 0) {
72 ALOGE(
73 "SetThreadPolicy: Failed to set scheduler class \"%s\" for "
74 "thread_id=%d: %s",
75 scheduler_class.c_str(), gettid(), strerror(-error));
76 return false;
77 }
78 error = dvrSetCpuPartition(0, partition.c_str());
79 if (error < 0) {
80 ALOGE(
81 "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: "
82 "%s",
83 partition.c_str(), gettid(), strerror(-error));
84 return false;
85 }
86 return true;
87}
88
Corey Tabakab3732f02017-09-16 00:58:54 -070089// Utility to generate scoped tracers with arguments.
90// TODO(eieio): Move/merge this into utils/Trace.h?
91class TraceArgs {
92 public:
93 template <typename... Args>
94 TraceArgs(const char* format, Args&&... args) {
95 std::array<char, 1024> buffer;
96 snprintf(buffer.data(), buffer.size(), format, std::forward<Args>(args)...);
97 atrace_begin(ATRACE_TAG, buffer.data());
98 }
99
100 ~TraceArgs() { atrace_end(ATRACE_TAG); }
101
102 private:
103 TraceArgs(const TraceArgs&) = delete;
104 void operator=(const TraceArgs&) = delete;
105};
106
107// Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
108// defined in utils/Trace.h.
109#define TRACE_FORMAT(format, ...) \
110 TraceArgs PASTE(__tracer, __LINE__) { format, ##__VA_ARGS__ }
111
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800112} // anonymous namespace
113
Corey Tabaka2251d822017-04-20 16:04:07 -0700114HardwareComposer::HardwareComposer()
Steven Thomasb02664d2017-07-26 18:48:28 -0700115 : initialized_(false), request_display_callback_(nullptr) {}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800116
117HardwareComposer::~HardwareComposer(void) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700118 UpdatePostThreadState(PostThreadState::Quit, true);
119 if (post_thread_.joinable())
Steven Thomas050b2c82017-03-06 11:45:16 -0800120 post_thread_.join();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800121}
122
Steven Thomasb02664d2017-07-26 18:48:28 -0700123bool HardwareComposer::Initialize(
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700124 Hwc2::Composer* composer, RequestDisplayCallback request_display_callback) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800125 if (initialized_) {
126 ALOGE("HardwareComposer::Initialize: already initialized.");
127 return false;
128 }
129
Corey Tabaka451256f2017-08-22 11:59:15 -0700130 is_standalone_device_ = property_get_bool(kDvrStandaloneProperty, false);
131
Steven Thomasb02664d2017-07-26 18:48:28 -0700132 request_display_callback_ = request_display_callback;
133
Corey Tabaka2251d822017-04-20 16:04:07 -0700134 HWC::Error error = HWC::Error::None;
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800135
136 Hwc2::Config config;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700137 error = composer->getActiveConfig(HWC_DISPLAY_PRIMARY, &config);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800138
Corey Tabaka2251d822017-04-20 16:04:07 -0700139 if (error != HWC::Error::None) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800140 ALOGE("HardwareComposer: Failed to get current display config : %d",
141 config);
142 return false;
143 }
144
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700145 error = GetDisplayMetrics(composer, HWC_DISPLAY_PRIMARY, config,
Steven Thomasb02664d2017-07-26 18:48:28 -0700146 &native_display_metrics_);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800147
Corey Tabaka2251d822017-04-20 16:04:07 -0700148 if (error != HWC::Error::None) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800149 ALOGE(
150 "HardwareComposer: Failed to get display attributes for current "
151 "configuration : %d",
Corey Tabaka2251d822017-04-20 16:04:07 -0700152 error.value);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800153 return false;
154 }
155
156 ALOGI(
157 "HardwareComposer: primary display attributes: width=%d height=%d "
158 "vsync_period_ns=%d DPI=%dx%d",
159 native_display_metrics_.width, native_display_metrics_.height,
160 native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x,
161 native_display_metrics_.dpi.y);
162
163 // Set the display metrics but never use rotation to avoid the long latency of
164 // rotation processing in hwc.
165 display_transform_ = HWC_TRANSFORM_NONE;
166 display_metrics_ = native_display_metrics_;
167
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700168 // Setup the display metrics used by all Layer instances.
169 Layer::SetDisplayMetrics(native_display_metrics_);
170
Corey Tabaka2251d822017-04-20 16:04:07 -0700171 post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
Steven Thomas050b2c82017-03-06 11:45:16 -0800172 LOG_ALWAYS_FATAL_IF(
Corey Tabaka2251d822017-04-20 16:04:07 -0700173 !post_thread_event_fd_,
Steven Thomas050b2c82017-03-06 11:45:16 -0800174 "HardwareComposer: Failed to create interrupt event fd : %s",
175 strerror(errno));
176
177 post_thread_ = std::thread(&HardwareComposer::PostThread, this);
178
Stephen Kiazyk016e5e32017-02-21 17:09:22 -0800179 initialized_ = true;
180
181 return initialized_;
182}
183
Steven Thomas050b2c82017-03-06 11:45:16 -0800184void HardwareComposer::Enable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700185 UpdatePostThreadState(PostThreadState::Suspended, false);
Steven Thomas050b2c82017-03-06 11:45:16 -0800186}
187
188void HardwareComposer::Disable() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700189 UpdatePostThreadState(PostThreadState::Suspended, true);
Steven Thomas050b2c82017-03-06 11:45:16 -0800190}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800191
Corey Tabaka2251d822017-04-20 16:04:07 -0700192// Update the post thread quiescent state based on idle and suspended inputs.
193void HardwareComposer::UpdatePostThreadState(PostThreadStateType state,
194 bool suspend) {
195 std::unique_lock<std::mutex> lock(post_thread_mutex_);
196
197 // Update the votes in the state variable before evaluating the effective
198 // quiescent state. Any bits set in post_thread_state_ indicate that the post
199 // thread should be suspended.
200 if (suspend) {
201 post_thread_state_ |= state;
202 } else {
203 post_thread_state_ &= ~state;
204 }
205
206 const bool quit = post_thread_state_ & PostThreadState::Quit;
207 const bool effective_suspend = post_thread_state_ != PostThreadState::Active;
208 if (quit) {
209 post_thread_quiescent_ = true;
210 eventfd_write(post_thread_event_fd_.Get(), 1);
211 post_thread_wait_.notify_one();
212 } else if (effective_suspend && !post_thread_quiescent_) {
213 post_thread_quiescent_ = true;
214 eventfd_write(post_thread_event_fd_.Get(), 1);
215 } else if (!effective_suspend && post_thread_quiescent_) {
216 post_thread_quiescent_ = false;
217 eventfd_t value;
218 eventfd_read(post_thread_event_fd_.Get(), &value);
219 post_thread_wait_.notify_one();
220 }
221
222 // Wait until the post thread is in the requested state.
223 post_thread_ready_.wait(lock, [this, effective_suspend] {
224 return effective_suspend != post_thread_resumed_;
225 });
Steven Thomas050b2c82017-03-06 11:45:16 -0800226}
Steven Thomas282a5ed2017-02-07 18:07:01 -0800227
Steven Thomas050b2c82017-03-06 11:45:16 -0800228void HardwareComposer::OnPostThreadResumed() {
Corey Tabaka451256f2017-08-22 11:59:15 -0700229 // Phones create a new composer client on resume and destroy it on pause.
230 // Standalones only create the composer client once and then use SetPowerMode
231 // to control the screen on pause/resume.
232 if (!is_standalone_device_ || !composer_) {
233 composer_.reset(new Hwc2::Composer("default"));
234 composer_callback_ = new ComposerCallback;
235 composer_->registerCallback(composer_callback_);
236 Layer::SetComposer(composer_.get());
237 } else {
238 SetPowerMode(true);
239 }
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700240
Steven Thomas050b2c82017-03-06 11:45:16 -0800241 EnableVsync(true);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800242
Steven Thomas050b2c82017-03-06 11:45:16 -0800243 // TODO(skiazyk): We need to do something about accessing this directly,
244 // supposedly there is a backlight service on the way.
245 // TODO(steventhomas): When we change the backlight setting, will surface
246 // flinger (or something else) set it back to its original value once we give
247 // control of the display back to surface flinger?
248 SetBacklightBrightness(255);
Steven Thomas282a5ed2017-02-07 18:07:01 -0800249
Steven Thomas050b2c82017-03-06 11:45:16 -0800250 // Trigger target-specific performance mode change.
251 property_set(kDvrPerformanceProperty, "performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800252}
253
Steven Thomas050b2c82017-03-06 11:45:16 -0800254void HardwareComposer::OnPostThreadPaused() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700255 retire_fence_fds_.clear();
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700256 layers_.clear();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800257
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700258 if (composer_) {
Steven Thomasb02664d2017-07-26 18:48:28 -0700259 EnableVsync(false);
260 }
Steven Thomas050b2c82017-03-06 11:45:16 -0800261
Corey Tabaka451256f2017-08-22 11:59:15 -0700262 if (!is_standalone_device_) {
263 composer_callback_ = nullptr;
264 composer_.reset(nullptr);
265 Layer::SetComposer(nullptr);
266 } else {
267 SetPowerMode(false);
268 }
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700269
Steven Thomas050b2c82017-03-06 11:45:16 -0800270 // Trigger target-specific performance mode change.
271 property_set(kDvrPerformanceProperty, "idle");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800272}
273
Corey Tabaka2251d822017-04-20 16:04:07 -0700274HWC::Error HardwareComposer::Validate(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800275 uint32_t num_types;
276 uint32_t num_requests;
Corey Tabaka2251d822017-04-20 16:04:07 -0700277 HWC::Error error =
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700278 composer_->validateDisplay(display, &num_types, &num_requests);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800279
280 if (error == HWC2_ERROR_HAS_CHANGES) {
281 // TODO(skiazyk): We might need to inspect the requested changes first, but
282 // so far it seems like we shouldn't ever hit a bad state.
283 // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_,
284 // display);
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700285 error = composer_->acceptDisplayChanges(display);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800286 }
287
288 return error;
289}
290
Steven Thomasb02664d2017-07-26 18:48:28 -0700291HWC::Error HardwareComposer::EnableVsync(bool enabled) {
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700292 return composer_->setVsyncEnabled(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800293 HWC_DISPLAY_PRIMARY,
294 (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
295 : HWC2_VSYNC_DISABLE));
296}
297
Corey Tabaka451256f2017-08-22 11:59:15 -0700298HWC::Error HardwareComposer::SetPowerMode(bool active) {
299 HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off;
300 return composer_->setPowerMode(
301 HWC_DISPLAY_PRIMARY, power_mode.cast<Hwc2::IComposerClient::PowerMode>());
302}
303
Corey Tabaka2251d822017-04-20 16:04:07 -0700304HWC::Error HardwareComposer::Present(hwc2_display_t display) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800305 int32_t present_fence;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700306 HWC::Error error = composer_->presentDisplay(display, &present_fence);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800307
308 // According to the documentation, this fence is signaled at the time of
309 // vsync/DMA for physical displays.
Corey Tabaka2251d822017-04-20 16:04:07 -0700310 if (error == HWC::Error::None) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800311 ATRACE_INT("HardwareComposer: VsyncFence", present_fence);
312 retire_fence_fds_.emplace_back(present_fence);
313 } else {
314 ATRACE_INT("HardwareComposer: PresentResult", error);
315 }
316
317 return error;
318}
319
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700320HWC::Error HardwareComposer::GetDisplayAttribute(Hwc2::Composer* composer,
Steven Thomasb02664d2017-07-26 18:48:28 -0700321 hwc2_display_t display,
Corey Tabaka2251d822017-04-20 16:04:07 -0700322 hwc2_config_t config,
323 hwc2_attribute_t attribute,
324 int32_t* out_value) const {
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700325 return composer->getDisplayAttribute(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800326 display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value);
327}
328
Corey Tabaka2251d822017-04-20 16:04:07 -0700329HWC::Error HardwareComposer::GetDisplayMetrics(
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700330 Hwc2::Composer* composer, hwc2_display_t display, hwc2_config_t config,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800331 HWCDisplayMetrics* out_metrics) const {
Corey Tabaka2251d822017-04-20 16:04:07 -0700332 HWC::Error error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800333
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700334 error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_WIDTH,
Corey Tabaka2251d822017-04-20 16:04:07 -0700335 &out_metrics->width);
336 if (error != HWC::Error::None) {
337 ALOGE(
338 "HardwareComposer::GetDisplayMetrics: Failed to get display width: %s",
339 error.to_string().c_str());
340 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800341 }
342
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700343 error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_HEIGHT,
Corey Tabaka2251d822017-04-20 16:04:07 -0700344 &out_metrics->height);
345 if (error != HWC::Error::None) {
346 ALOGE(
347 "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s",
348 error.to_string().c_str());
349 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800350 }
351
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700352 error = GetDisplayAttribute(composer, display, config,
Steven Thomasb02664d2017-07-26 18:48:28 -0700353 HWC2_ATTRIBUTE_VSYNC_PERIOD,
Corey Tabaka2251d822017-04-20 16:04:07 -0700354 &out_metrics->vsync_period_ns);
355 if (error != HWC::Error::None) {
356 ALOGE(
357 "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s",
358 error.to_string().c_str());
359 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800360 }
361
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700362 error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_X,
Corey Tabaka2251d822017-04-20 16:04:07 -0700363 &out_metrics->dpi.x);
364 if (error != HWC::Error::None) {
365 ALOGE(
366 "HardwareComposer::GetDisplayMetrics: Failed to get display DPI X: %s",
367 error.to_string().c_str());
368 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800369 }
370
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700371 error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_Y,
Corey Tabaka2251d822017-04-20 16:04:07 -0700372 &out_metrics->dpi.y);
373 if (error != HWC::Error::None) {
374 ALOGE(
375 "HardwareComposer::GetDisplayMetrics: Failed to get display DPI Y: %s",
376 error.to_string().c_str());
377 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800378 }
379
Corey Tabaka2251d822017-04-20 16:04:07 -0700380 return HWC::Error::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800381}
382
Corey Tabaka0b485c92017-05-19 12:02:58 -0700383std::string HardwareComposer::Dump() {
384 std::unique_lock<std::mutex> lock(post_thread_mutex_);
385 std::ostringstream stream;
386
387 stream << "Display metrics: " << display_metrics_.width << "x"
388 << display_metrics_.height << " " << (display_metrics_.dpi.x / 1000.0)
389 << "x" << (display_metrics_.dpi.y / 1000.0) << " dpi @ "
390 << (1000000000.0 / display_metrics_.vsync_period_ns) << " Hz"
391 << std::endl;
392
393 stream << "Post thread resumed: " << post_thread_resumed_ << std::endl;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700394 stream << "Active layers: " << layers_.size() << std::endl;
Corey Tabaka0b485c92017-05-19 12:02:58 -0700395 stream << std::endl;
396
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700397 for (size_t i = 0; i < layers_.size(); i++) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700398 stream << "Layer " << i << ":";
399 stream << " type=" << layers_[i].GetCompositionType().to_string();
400 stream << " surface_id=" << layers_[i].GetSurfaceId();
401 stream << " buffer_id=" << layers_[i].GetBufferId();
402 stream << std::endl;
403 }
404 stream << std::endl;
405
406 if (post_thread_resumed_) {
407 stream << "Hardware Composer Debug Info:" << std::endl;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700408 stream << composer_->dumpDebugInfo();
Corey Tabaka0b485c92017-05-19 12:02:58 -0700409 }
410
411 return stream.str();
412}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800413
Corey Tabaka2251d822017-04-20 16:04:07 -0700414void HardwareComposer::PostLayers() {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800415 ATRACE_NAME("HardwareComposer::PostLayers");
416
417 // Setup the hardware composer layers with current buffers.
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700418 for (auto& layer : layers_) {
419 layer.Prepare();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800420 }
421
Corey Tabaka2251d822017-04-20 16:04:07 -0700422 HWC::Error error = Validate(HWC_DISPLAY_PRIMARY);
423 if (error != HWC::Error::None) {
424 ALOGE("HardwareComposer::PostLayers: Validate failed: %s",
425 error.to_string().c_str());
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700426 return;
427 }
428
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800429 // Now that we have taken in a frame from the application, we have a chance
430 // to drop the frame before passing the frame along to HWC.
431 // If the display driver has become backed up, we detect it here and then
432 // react by skipping this frame to catch up latency.
433 while (!retire_fence_fds_.empty() &&
434 (!retire_fence_fds_.front() ||
435 sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
436 // There are only 2 fences in here, no performance problem to shift the
437 // array of ints.
438 retire_fence_fds_.erase(retire_fence_fds_.begin());
439 }
440
George Burgess IV353a6f62017-06-26 17:13:09 -0700441 const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) >
John Bates954796e2017-05-11 11:00:31 -0700442 post_thread_config_.allowed_pending_fence_count;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800443
Corey Tabakab3732f02017-09-16 00:58:54 -0700444 if (is_fence_pending) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800445 ATRACE_INT("frame_skip_count", ++frame_skip_count_);
446
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800447 ALOGW_IF(is_fence_pending,
448 "Warning: dropping a frame to catch up with HWC (pending = %zd)",
449 retire_fence_fds_.size());
450
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700451 for (auto& layer : layers_) {
452 layer.Drop();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800453 }
454 return;
455 } else {
456 // Make the transition more obvious in systrace when the frame skip happens
457 // above.
458 ATRACE_INT("frame_skip_count", 0);
459 }
460
Corey Tabaka89bbefc2017-06-06 16:14:21 -0700461#if TRACE > 1
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700462 for (size_t i = 0; i < layers_.size(); i++) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700463 ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s",
464 i, layers_[i].GetBufferId(),
Corey Tabaka2251d822017-04-20 16:04:07 -0700465 layers_[i].GetCompositionType().to_string().c_str());
Corey Tabaka0b485c92017-05-19 12:02:58 -0700466 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800467#endif
468
Corey Tabaka2251d822017-04-20 16:04:07 -0700469 error = Present(HWC_DISPLAY_PRIMARY);
470 if (error != HWC::Error::None) {
471 ALOGE("HardwareComposer::PostLayers: Present failed: %s",
472 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800473 return;
474 }
475
476 std::vector<Hwc2::Layer> out_layers;
477 std::vector<int> out_fences;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700478 error = composer_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers,
479 &out_fences);
Corey Tabaka2251d822017-04-20 16:04:07 -0700480 ALOGE_IF(error != HWC::Error::None,
481 "HardwareComposer::PostLayers: Failed to get release fences: %s",
482 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800483
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700484 // Perform post-frame bookkeeping.
Corey Tabaka2251d822017-04-20 16:04:07 -0700485 uint32_t num_elements = out_layers.size();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800486 for (size_t i = 0; i < num_elements; ++i) {
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700487 for (auto& layer : layers_) {
488 if (layer.GetLayerHandle() == out_layers[i]) {
489 layer.Finish(out_fences[i]);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800490 }
491 }
492 }
493}
494
Steven Thomas050b2c82017-03-06 11:45:16 -0800495void HardwareComposer::SetDisplaySurfaces(
Corey Tabaka2251d822017-04-20 16:04:07 -0700496 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) {
Jin Qian7480c062017-03-21 00:04:15 +0000497 ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
498 surfaces.size());
Corey Tabaka2251d822017-04-20 16:04:07 -0700499 const bool display_idle = surfaces.size() == 0;
500 {
501 std::unique_lock<std::mutex> lock(post_thread_mutex_);
502 pending_surfaces_ = std::move(surfaces);
503 }
504
Corey Tabaka451256f2017-08-22 11:59:15 -0700505 if (request_display_callback_ && (!is_standalone_device_ || !composer_))
Steven Thomas2ddf5672017-06-15 11:38:40 -0700506 request_display_callback_(!display_idle);
507
Corey Tabaka2251d822017-04-20 16:04:07 -0700508 // Set idle state based on whether there are any surfaces to handle.
509 UpdatePostThreadState(PostThreadState::Idle, display_idle);
Steven Thomas050b2c82017-03-06 11:45:16 -0800510}
Jin Qian7480c062017-03-21 00:04:15 +0000511
John Bates954796e2017-05-11 11:00:31 -0700512int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key,
513 IonBuffer& ion_buffer) {
Okan Arikan822b7102017-05-08 13:31:34 -0700514 if (key == DvrGlobalBuffers::kVsyncBuffer) {
515 vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>(
516 &ion_buffer, CPUUsageMode::WRITE_OFTEN);
517
518 if (vsync_ring_->IsMapped() == false) {
519 return -EPERM;
520 }
521 }
522
523 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700524 return MapConfigBuffer(ion_buffer);
525 }
526
527 return 0;
528}
529
530void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) {
Okan Arikan822b7102017-05-08 13:31:34 -0700531 if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
John Bates954796e2017-05-11 11:00:31 -0700532 ConfigBufferDeleted();
533 }
534}
535
536int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) {
537 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700538 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700539
Okan Arikan6f468c62017-05-31 14:48:30 -0700540 if (ion_buffer.width() < DvrConfigRing::MemorySize()) {
John Bates954796e2017-05-11 11:00:31 -0700541 ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size.");
542 return -EINVAL;
543 }
544
545 void* buffer_base = 0;
546 int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(),
547 ion_buffer.height(), &buffer_base);
548 if (result != 0) {
Corey Tabaka0b485c92017-05-19 12:02:58 -0700549 ALOGE(
550 "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config "
551 "buffer.");
John Bates954796e2017-05-11 11:00:31 -0700552 return -EPERM;
553 }
554
Okan Arikan6f468c62017-05-31 14:48:30 -0700555 shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width());
John Bates954796e2017-05-11 11:00:31 -0700556 ion_buffer.Unlock();
557
558 return 0;
559}
560
561void HardwareComposer::ConfigBufferDeleted() {
562 std::lock_guard<std::mutex> lock(shared_config_mutex_);
Okan Arikan6f468c62017-05-31 14:48:30 -0700563 shared_config_ring_ = DvrConfigRing();
John Bates954796e2017-05-11 11:00:31 -0700564}
565
566void HardwareComposer::UpdateConfigBuffer() {
567 std::lock_guard<std::mutex> lock(shared_config_mutex_);
568 if (!shared_config_ring_.is_valid())
569 return;
570 // Copy from latest record in shared_config_ring_ to local copy.
Okan Arikan6f468c62017-05-31 14:48:30 -0700571 DvrConfig record;
John Bates954796e2017-05-11 11:00:31 -0700572 if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) {
John Batescc65c3c2017-09-28 14:43:19 -0700573 ALOGI("DvrConfig updated: sequence %u, post offset %d",
574 shared_config_ring_sequence_, record.frame_post_offset_ns);
575 ++shared_config_ring_sequence_;
John Bates954796e2017-05-11 11:00:31 -0700576 post_thread_config_ = record;
577 }
578}
579
Corey Tabaka2251d822017-04-20 16:04:07 -0700580int HardwareComposer::PostThreadPollInterruptible(
Steven Thomasb02664d2017-07-26 18:48:28 -0700581 const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800582 pollfd pfd[2] = {
583 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700584 .fd = event_fd.Get(),
Steven Thomas66747c12017-03-22 18:45:31 -0700585 .events = static_cast<short>(requested_events),
586 .revents = 0,
Steven Thomas050b2c82017-03-06 11:45:16 -0800587 },
588 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700589 .fd = post_thread_event_fd_.Get(),
Steven Thomas050b2c82017-03-06 11:45:16 -0800590 .events = POLLPRI | POLLIN,
591 .revents = 0,
592 },
593 };
594 int ret, error;
595 do {
Steven Thomasb02664d2017-07-26 18:48:28 -0700596 ret = poll(pfd, 2, timeout_ms);
Steven Thomas050b2c82017-03-06 11:45:16 -0800597 error = errno;
598 ALOGW_IF(ret < 0,
599 "HardwareComposer::PostThreadPollInterruptible: Error during "
600 "poll(): %s (%d)",
601 strerror(error), error);
602 } while (ret < 0 && error == EINTR);
603
604 if (ret < 0) {
605 return -error;
Steven Thomasb02664d2017-07-26 18:48:28 -0700606 } else if (ret == 0) {
607 return -ETIMEDOUT;
Steven Thomas050b2c82017-03-06 11:45:16 -0800608 } else if (pfd[0].revents != 0) {
609 return 0;
610 } else if (pfd[1].revents != 0) {
611 ALOGI("VrHwcPost thread interrupted");
612 return kPostThreadInterrupted;
613 } else {
614 return 0;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800615 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800616}
617
Corey Tabakab3732f02017-09-16 00:58:54 -0700618Status<int64_t> HardwareComposer::GetVSyncTime() {
619 auto status = composer_callback_->GetVsyncTime(HWC_DISPLAY_PRIMARY);
620 ALOGE_IF(!status,
621 "HardwareComposer::GetVSyncTime: Failed to get vsync timestamp: %s",
622 status.GetErrorMessage().c_str());
623 return status;
624}
625
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800626// Waits for the next vsync and returns the timestamp of the vsync event. If
627// vsync already passed since the last call, returns the latest vsync timestamp
Steven Thomasb02664d2017-07-26 18:48:28 -0700628// instead of blocking.
Corey Tabakab3732f02017-09-16 00:58:54 -0700629Status<int64_t> HardwareComposer::WaitForVSync() {
630 const int64_t predicted_vsync_time =
631 last_vsync_timestamp_ +
632 display_metrics_.vsync_period_ns * vsync_prediction_interval_;
633 const int error = SleepUntil(predicted_vsync_time);
634 if (error < 0) {
635 ALOGE("HardwareComposer::WaifForVSync:: Failed to sleep: %s",
636 strerror(-error));
Steven Thomasb02664d2017-07-26 18:48:28 -0700637 return error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800638 }
Corey Tabakab3732f02017-09-16 00:58:54 -0700639 return {predicted_vsync_time};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800640}
641
642int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
643 const int timer_fd = vsync_sleep_timer_fd_.Get();
644 const itimerspec wakeup_itimerspec = {
645 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
646 .it_value = NsToTimespec(wakeup_timestamp),
647 };
648 int ret =
649 timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
650 int error = errno;
651 if (ret < 0) {
652 ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
653 strerror(error));
654 return -error;
655 }
656
Corey Tabaka451256f2017-08-22 11:59:15 -0700657 return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN,
658 /*timeout_ms*/ -1);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800659}
660
661void HardwareComposer::PostThread() {
662 // NOLINTNEXTLINE(runtime/int)
Steven Thomas050b2c82017-03-06 11:45:16 -0800663 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800664
Corey Tabaka2251d822017-04-20 16:04:07 -0700665 // Set the scheduler to SCHED_FIFO with high priority. If this fails here
666 // there may have been a startup timing issue between this thread and
667 // performanced. Try again later when this thread becomes active.
668 bool thread_policy_setup =
669 SetThreadPolicy("graphics:high", "/system/performance");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800670
Steven Thomas050b2c82017-03-06 11:45:16 -0800671#if ENABLE_BACKLIGHT_BRIGHTNESS
672 // TODO(hendrikw): This isn't required at the moment. It's possible that there
673 // is another method to access this when needed.
674 // Open the backlight brightness control sysfs node.
675 backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR);
676 ALOGW_IF(!backlight_brightness_fd_,
677 "HardwareComposer: Failed to open backlight brightness control: %s",
678 strerror(errno));
Corey Tabaka2251d822017-04-20 16:04:07 -0700679#endif // ENABLE_BACKLIGHT_BRIGHTNESS
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800680
Steven Thomas050b2c82017-03-06 11:45:16 -0800681 // Create a timerfd based on CLOCK_MONOTINIC.
682 vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
683 LOG_ALWAYS_FATAL_IF(
684 !vsync_sleep_timer_fd_,
685 "HardwareComposer: Failed to create vsync sleep timerfd: %s",
686 strerror(errno));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800687
688 const int64_t ns_per_frame = display_metrics_.vsync_period_ns;
689 const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame);
690
691 // TODO(jbates) Query vblank time from device, when such an API is available.
692 // This value (6.3%) was measured on A00 in low persistence mode.
693 int64_t vblank_ns = ns_per_frame * 63 / 1000;
694 int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2;
695
696 // Check property for overriding right eye offset value.
697 right_eye_photon_offset_ns =
698 property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns);
699
Steven Thomas050b2c82017-03-06 11:45:16 -0800700 bool was_running = false;
701
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800702 while (1) {
703 ATRACE_NAME("HardwareComposer::PostThread");
704
John Bates954796e2017-05-11 11:00:31 -0700705 // Check for updated config once per vsync.
706 UpdateConfigBuffer();
707
Corey Tabaka2251d822017-04-20 16:04:07 -0700708 while (post_thread_quiescent_) {
Steven Thomas050b2c82017-03-06 11:45:16 -0800709 std::unique_lock<std::mutex> lock(post_thread_mutex_);
Corey Tabaka2251d822017-04-20 16:04:07 -0700710 ALOGI("HardwareComposer::PostThread: Entering quiescent state.");
711
Corey Tabakadf0b9162017-08-03 17:14:08 -0700712 // Tear down resources if necessary.
713 if (was_running)
714 OnPostThreadPaused();
Corey Tabaka2251d822017-04-20 16:04:07 -0700715
716 was_running = false;
717 post_thread_resumed_ = false;
718 post_thread_ready_.notify_all();
719
720 if (post_thread_state_ & PostThreadState::Quit) {
721 ALOGI("HardwareComposer::PostThread: Quitting.");
722 return;
Steven Thomas282a5ed2017-02-07 18:07:01 -0800723 }
Corey Tabaka2251d822017-04-20 16:04:07 -0700724
725 post_thread_wait_.wait(lock, [this] { return !post_thread_quiescent_; });
726
727 post_thread_resumed_ = true;
728 post_thread_ready_.notify_all();
729
730 ALOGI("HardwareComposer::PostThread: Exiting quiescent state.");
Steven Thomas050b2c82017-03-06 11:45:16 -0800731 }
732
733 if (!was_running) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700734 // Setup resources.
Steven Thomas050b2c82017-03-06 11:45:16 -0800735 OnPostThreadResumed();
736 was_running = true;
Corey Tabaka2251d822017-04-20 16:04:07 -0700737
738 // Try to setup the scheduler policy if it failed during startup. Only
739 // attempt to do this on transitions from inactive to active to avoid
740 // spamming the system with RPCs and log messages.
741 if (!thread_policy_setup) {
742 thread_policy_setup =
743 SetThreadPolicy("graphics:high", "/system/performance");
744 }
Corey Tabakab3732f02017-09-16 00:58:54 -0700745
746 // Initialize the last vsync timestamp with the current time. The
747 // predictor below uses this time + the vsync interval in absolute time
748 // units for the initial delay. Once the driver starts reporting vsync the
749 // predictor will sync up with the real vsync.
750 last_vsync_timestamp_ = GetSystemClockNs();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800751 }
752
753 int64_t vsync_timestamp = 0;
754 {
Corey Tabakab3732f02017-09-16 00:58:54 -0700755 TRACE_FORMAT("wait_vsync|vsync=%u;last_timestamp=%" PRId64
756 ";prediction_interval=%d|",
757 vsync_count_ + 1, last_vsync_timestamp_,
758 vsync_prediction_interval_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800759
Corey Tabakab3732f02017-09-16 00:58:54 -0700760 auto status = WaitForVSync();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800761 ALOGE_IF(
Corey Tabakab3732f02017-09-16 00:58:54 -0700762 !status,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800763 "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
Corey Tabakab3732f02017-09-16 00:58:54 -0700764 status.GetErrorMessage().c_str());
765
766 // If there was an error either sleeping was interrupted due to pausing or
767 // there was an error getting the latest timestamp.
768 if (!status)
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800769 continue;
Corey Tabakab3732f02017-09-16 00:58:54 -0700770
771 // Predicted vsync timestamp for this interval. This is stable because we
772 // use absolute time for the wakeup timer.
773 vsync_timestamp = status.get();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800774 }
775
Corey Tabakab3732f02017-09-16 00:58:54 -0700776 // Advance the vsync counter only if the system is keeping up with hardware
777 // vsync to give clients an indication of the delays.
778 if (vsync_prediction_interval_ == 1)
779 ++vsync_count_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800780
Corey Tabaka2251d822017-04-20 16:04:07 -0700781 const bool layer_config_changed = UpdateLayerConfig();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800782
Okan Arikan822b7102017-05-08 13:31:34 -0700783 // Publish the vsync event.
784 if (vsync_ring_) {
785 DvrVsync vsync;
786 vsync.vsync_count = vsync_count_;
787 vsync.vsync_timestamp_ns = vsync_timestamp;
788 vsync.vsync_left_eye_offset_ns = photon_offset_ns;
789 vsync.vsync_right_eye_offset_ns = right_eye_photon_offset_ns;
790 vsync.vsync_period_ns = ns_per_frame;
791
792 vsync_ring_->Publish(vsync);
793 }
794
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800795 // Signal all of the vsync clients. Because absolute time is used for the
796 // wakeup time below, this can take a little time if necessary.
797 if (vsync_callback_)
Corey Tabaka2251d822017-04-20 16:04:07 -0700798 vsync_callback_(HWC_DISPLAY_PRIMARY, vsync_timestamp,
799 /*frame_time_estimate*/ 0, vsync_count_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800800
801 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700802 // Sleep until shortly before vsync.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800803 ATRACE_NAME("sleep");
804
Corey Tabaka2251d822017-04-20 16:04:07 -0700805 const int64_t display_time_est_ns = vsync_timestamp + ns_per_frame;
806 const int64_t now_ns = GetSystemClockNs();
John Bates954796e2017-05-11 11:00:31 -0700807 const int64_t sleep_time_ns = display_time_est_ns - now_ns -
808 post_thread_config_.frame_post_offset_ns;
809 const int64_t wakeup_time_ns =
810 display_time_est_ns - post_thread_config_.frame_post_offset_ns;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800811
812 ATRACE_INT64("sleep_time_ns", sleep_time_ns);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800813 if (sleep_time_ns > 0) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700814 int error = SleepUntil(wakeup_time_ns);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800815 ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s",
816 strerror(-error));
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700817 if (error == kPostThreadInterrupted) {
818 if (layer_config_changed) {
819 // If the layer config changed we need to validateDisplay() even if
820 // we're going to drop the frame, to flush the Composer object's
821 // internal command buffer and apply our layer changes.
822 Validate(HWC_DISPLAY_PRIMARY);
823 }
Steven Thomas050b2c82017-03-06 11:45:16 -0800824 continue;
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700825 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800826 }
827 }
828
Corey Tabakab3732f02017-09-16 00:58:54 -0700829 {
830 auto status = GetVSyncTime();
831 if (!status) {
832 ALOGE("HardwareComposer::PostThread: Failed to get VSYNC time: %s",
833 status.GetErrorMessage().c_str());
834 }
835
836 // If we failed to read vsync there might be a problem with the driver.
837 // Since there's nothing we can do just behave as though we didn't get an
838 // updated vsync time and let the prediction continue.
839 const int64_t current_vsync_timestamp =
840 status ? status.get() : last_vsync_timestamp_;
841
842 const bool vsync_delayed =
843 last_vsync_timestamp_ == current_vsync_timestamp;
844 ATRACE_INT("vsync_delayed", vsync_delayed);
845
846 // If vsync was delayed advance the prediction interval and allow the
847 // fence logic in PostLayers() to skip the frame.
848 if (vsync_delayed) {
849 ALOGW(
850 "HardwareComposer::PostThread: VSYNC timestamp did not advance "
851 "since last frame: timestamp=%" PRId64 " prediction_interval=%d",
852 current_vsync_timestamp, vsync_prediction_interval_);
853 vsync_prediction_interval_++;
854 } else {
855 // We have an updated vsync timestamp, reset the prediction interval.
856 last_vsync_timestamp_ = current_vsync_timestamp;
857 vsync_prediction_interval_ = 1;
858 }
859 }
860
Corey Tabaka2251d822017-04-20 16:04:07 -0700861 PostLayers();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800862 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800863}
864
Corey Tabaka2251d822017-04-20 16:04:07 -0700865// Checks for changes in the surface stack and updates the layer config to
866// accomodate the new stack.
Steven Thomas050b2c82017-03-06 11:45:16 -0800867bool HardwareComposer::UpdateLayerConfig() {
Corey Tabaka2251d822017-04-20 16:04:07 -0700868 std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces;
Steven Thomas050b2c82017-03-06 11:45:16 -0800869 {
Corey Tabaka2251d822017-04-20 16:04:07 -0700870 std::unique_lock<std::mutex> lock(post_thread_mutex_);
871 if (pending_surfaces_.empty())
Steven Thomas050b2c82017-03-06 11:45:16 -0800872 return false;
Corey Tabaka2251d822017-04-20 16:04:07 -0700873
874 surfaces = std::move(pending_surfaces_);
Steven Thomas050b2c82017-03-06 11:45:16 -0800875 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800876
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800877 ATRACE_NAME("UpdateLayerConfig_HwLayers");
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800878
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700879 // Sort the new direct surface list by z-order to determine the relative order
880 // of the surfaces. This relative order is used for the HWC z-order value to
881 // insulate VrFlinger and HWC z-order semantics from each other.
882 std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) {
883 return a->z_order() < b->z_order();
884 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800885
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700886 // Prepare a new layer stack, pulling in layers from the previous
887 // layer stack that are still active and updating their attributes.
888 std::vector<Layer> layers;
889 size_t layer_index = 0;
890 for (const auto& surface : surfaces) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700891 // The bottom layer is opaque, other layers blend.
892 HWC::BlendMode blending =
893 layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage;
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700894
895 // Try to find a layer for this surface in the set of active layers.
896 auto search =
897 std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id());
898 const bool found = search != layers_.end() &&
899 search->GetSurfaceId() == surface->surface_id();
900 if (found) {
901 // Update the attributes of the layer that may have changed.
902 search->SetBlending(blending);
903 search->SetZOrder(layer_index); // Relative z-order.
904
905 // Move the existing layer to the new layer set and remove the empty layer
906 // object from the current set.
907 layers.push_back(std::move(*search));
908 layers_.erase(search);
909 } else {
910 // Insert a layer for the new surface.
911 layers.emplace_back(surface, blending, display_transform_,
912 HWC::Composition::Device, layer_index);
913 }
914
915 ALOGI_IF(
916 TRACE,
917 "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d",
918 layer_index, layers[layer_index].GetSurfaceId());
919
920 layer_index++;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800921 }
922
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700923 // Sort the new layer stack by ascending surface id.
924 std::sort(layers.begin(), layers.end());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800925
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700926 // Replace the previous layer set with the new layer set. The destructor of
927 // the previous set will clean up the remaining Layers that are not moved to
928 // the new layer set.
929 layers_ = std::move(layers);
930
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800931 ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
Corey Tabaka2c4aea32017-08-31 20:01:15 -0700932 layers_.size());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800933 return true;
934}
935
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800936void HardwareComposer::SetVSyncCallback(VSyncCallback callback) {
937 vsync_callback_ = callback;
938}
939
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800940void HardwareComposer::SetBacklightBrightness(int brightness) {
941 if (backlight_brightness_fd_) {
942 std::array<char, 32> text;
943 const int length = snprintf(text.data(), text.size(), "%d", brightness);
944 write(backlight_brightness_fd_.Get(), text.data(), length);
945 }
946}
947
Steven Thomasb02664d2017-07-26 18:48:28 -0700948Return<void> HardwareComposer::ComposerCallback::onHotplug(
Corey Tabakab3732f02017-09-16 00:58:54 -0700949 Hwc2::Display display, IComposerCallback::Connection /*conn*/) {
950 // See if the driver supports the vsync_event node in sysfs.
951 if (display < HWC_NUM_PHYSICAL_DISPLAY_TYPES &&
952 !displays_[display].driver_vsync_event_fd) {
953 std::array<char, 1024> buffer;
954 snprintf(buffer.data(), buffer.size(),
955 "/sys/class/graphics/fb%" PRIu64 "/vsync_event", display);
956 if (LocalHandle handle{buffer.data(), O_RDONLY}) {
957 ALOGI(
958 "HardwareComposer::ComposerCallback::onHotplug: Driver supports "
959 "vsync_event node for display %" PRIu64,
960 display);
961 displays_[display].driver_vsync_event_fd = std::move(handle);
962 } else {
963 ALOGI(
964 "HardwareComposer::ComposerCallback::onHotplug: Driver does not "
965 "support vsync_event node for display %" PRIu64,
966 display);
967 }
968 }
969
Steven Thomasb02664d2017-07-26 18:48:28 -0700970 return Void();
971}
972
973Return<void> HardwareComposer::ComposerCallback::onRefresh(
974 Hwc2::Display /*display*/) {
975 return hardware::Void();
976}
977
Corey Tabaka451256f2017-08-22 11:59:15 -0700978Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display,
979 int64_t timestamp) {
Corey Tabakab3732f02017-09-16 00:58:54 -0700980 TRACE_FORMAT("vsync_callback|display=%" PRIu64 ";timestamp=%" PRId64 "|",
981 display, timestamp);
982 if (display < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
983 displays_[display].callback_vsync_timestamp = timestamp;
984 } else {
985 ALOGW(
986 "HardwareComposer::ComposerCallback::onVsync: Received vsync on "
987 "non-physical display: display=%" PRId64,
988 display);
Steven Thomasb02664d2017-07-26 18:48:28 -0700989 }
990 return Void();
991}
992
Corey Tabakab3732f02017-09-16 00:58:54 -0700993Status<int64_t> HardwareComposer::ComposerCallback::GetVsyncTime(
994 Hwc2::Display display) {
995 if (display >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
996 ALOGE(
997 "HardwareComposer::ComposerCallback::GetVsyncTime: Invalid physical "
998 "display requested: display=%" PRIu64,
999 display);
1000 return ErrorStatus(EINVAL);
1001 }
Steven Thomasb02664d2017-07-26 18:48:28 -07001002
Corey Tabakab3732f02017-09-16 00:58:54 -07001003 // See if the driver supports direct vsync events.
1004 LocalHandle& event_fd = displays_[display].driver_vsync_event_fd;
1005 if (!event_fd) {
1006 // Fall back to returning the last timestamp returned by the vsync
1007 // callback.
1008 std::lock_guard<std::mutex> autolock(vsync_mutex_);
1009 return displays_[display].callback_vsync_timestamp;
1010 }
1011
1012 // When the driver supports the vsync_event sysfs node we can use it to
1013 // determine the latest vsync timestamp, even if the HWC callback has been
1014 // delayed.
1015
1016 // The driver returns data in the form "VSYNC=<timestamp ns>".
1017 std::array<char, 32> data;
1018 data.fill('\0');
1019
1020 // Seek back to the beginning of the event file.
1021 int ret = lseek(event_fd.Get(), 0, SEEK_SET);
1022 if (ret < 0) {
1023 const int error = errno;
1024 ALOGE(
1025 "HardwareComposer::ComposerCallback::GetVsyncTime: Failed to seek "
1026 "vsync event fd: %s",
1027 strerror(error));
1028 return ErrorStatus(error);
1029 }
1030
1031 // Read the vsync event timestamp.
1032 ret = read(event_fd.Get(), data.data(), data.size());
1033 if (ret < 0) {
1034 const int error = errno;
1035 ALOGE_IF(error != EAGAIN,
1036 "HardwareComposer::ComposerCallback::GetVsyncTime: Error "
1037 "while reading timestamp: %s",
1038 strerror(error));
1039 return ErrorStatus(error);
1040 }
1041
1042 int64_t timestamp;
1043 ret = sscanf(data.data(), "VSYNC=%" PRIu64,
1044 reinterpret_cast<uint64_t*>(&timestamp));
1045 if (ret < 0) {
1046 const int error = errno;
1047 ALOGE(
1048 "HardwareComposer::ComposerCallback::GetVsyncTime: Error while "
1049 "parsing timestamp: %s",
1050 strerror(error));
1051 return ErrorStatus(error);
1052 }
1053
1054 return {timestamp};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001055}
1056
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001057Hwc2::Composer* Layer::composer_{nullptr};
1058HWCDisplayMetrics Layer::display_metrics_{0, 0, {0, 0}, 0};
1059
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001060void Layer::Reset() {
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001061 if (hardware_composer_layer_) {
1062 composer_->destroyLayer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001063 hardware_composer_layer_ = 0;
1064 }
1065
Corey Tabaka2251d822017-04-20 16:04:07 -07001066 z_order_ = 0;
1067 blending_ = HWC::BlendMode::None;
1068 transform_ = HWC::Transform::None;
1069 composition_type_ = HWC::Composition::Invalid;
1070 target_composition_type_ = composition_type_;
1071 source_ = EmptyVariant{};
1072 acquire_fence_.Close();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001073 surface_rect_functions_applied_ = false;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001074 pending_visibility_settings_ = true;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001075}
1076
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001077Layer::Layer(const std::shared_ptr<DirectDisplaySurface>& surface,
1078 HWC::BlendMode blending, HWC::Transform transform,
1079 HWC::Composition composition_type, size_t z_order)
1080 : z_order_{z_order},
1081 blending_{blending},
1082 transform_{transform},
1083 target_composition_type_{composition_type},
1084 source_{SourceSurface{surface}} {
1085 CommonLayerSetup();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001086}
1087
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001088Layer::Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
1089 HWC::Transform transform, HWC::Composition composition_type,
1090 size_t z_order)
1091 : z_order_{z_order},
1092 blending_{blending},
1093 transform_{transform},
1094 target_composition_type_{composition_type},
1095 source_{SourceBuffer{buffer}} {
1096 CommonLayerSetup();
1097}
1098
1099Layer::~Layer() { Reset(); }
1100
1101Layer::Layer(Layer&& other) { *this = std::move(other); }
1102
1103Layer& Layer::operator=(Layer&& other) {
1104 if (this != &other) {
1105 Reset();
1106 using std::swap;
1107 swap(hardware_composer_layer_, other.hardware_composer_layer_);
1108 swap(z_order_, other.z_order_);
1109 swap(blending_, other.blending_);
1110 swap(transform_, other.transform_);
1111 swap(composition_type_, other.composition_type_);
1112 swap(target_composition_type_, other.target_composition_type_);
1113 swap(source_, other.source_);
1114 swap(acquire_fence_, other.acquire_fence_);
1115 swap(surface_rect_functions_applied_,
1116 other.surface_rect_functions_applied_);
1117 swap(pending_visibility_settings_, other.pending_visibility_settings_);
1118 }
1119 return *this;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001120}
1121
Corey Tabaka2251d822017-04-20 16:04:07 -07001122void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) {
1123 if (source_.is<SourceBuffer>())
1124 std::get<SourceBuffer>(source_) = {buffer};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001125}
1126
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001127void Layer::SetBlending(HWC::BlendMode blending) {
1128 if (blending_ != blending) {
1129 blending_ = blending;
1130 pending_visibility_settings_ = true;
1131 }
1132}
1133
1134void Layer::SetZOrder(size_t z_order) {
1135 if (z_order_ != z_order) {
1136 z_order_ = z_order;
1137 pending_visibility_settings_ = true;
1138 }
1139}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001140
1141IonBuffer* Layer::GetBuffer() {
Corey Tabaka2251d822017-04-20 16:04:07 -07001142 struct Visitor {
1143 IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); }
1144 IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); }
1145 IonBuffer* operator()(EmptyVariant) { return nullptr; }
1146 };
1147 return source_.Visit(Visitor{});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001148}
1149
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001150void Layer::UpdateVisibilitySettings() {
1151 if (pending_visibility_settings_) {
1152 pending_visibility_settings_ = false;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001153
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001154 HWC::Error error;
1155 hwc2_display_t display = HWC_DISPLAY_PRIMARY;
1156
1157 error = composer_->setLayerBlendMode(
1158 display, hardware_composer_layer_,
1159 blending_.cast<Hwc2::IComposerClient::BlendMode>());
1160 ALOGE_IF(error != HWC::Error::None,
1161 "Layer::UpdateLayerSettings: Error setting layer blend mode: %s",
1162 error.to_string().c_str());
1163
1164 error =
1165 composer_->setLayerZOrder(display, hardware_composer_layer_, z_order_);
1166 ALOGE_IF(error != HWC::Error::None,
1167 "Layer::UpdateLayerSettings: Error setting z_ order: %s",
1168 error.to_string().c_str());
1169 }
1170}
1171
1172void Layer::UpdateLayerSettings() {
Corey Tabaka2251d822017-04-20 16:04:07 -07001173 HWC::Error error;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001174 hwc2_display_t display = HWC_DISPLAY_PRIMARY;
1175
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001176 UpdateVisibilitySettings();
1177
Corey Tabaka2251d822017-04-20 16:04:07 -07001178 // TODO(eieio): Use surface attributes or some other mechanism to control
1179 // the layer display frame.
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001180 error = composer_->setLayerDisplayFrame(
Corey Tabaka2251d822017-04-20 16:04:07 -07001181 display, hardware_composer_layer_,
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001182 {0, 0, display_metrics_.width, display_metrics_.height});
Corey Tabaka2251d822017-04-20 16:04:07 -07001183 ALOGE_IF(error != HWC::Error::None,
1184 "Layer::UpdateLayerSettings: Error setting layer display frame: %s",
1185 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001186
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001187 error = composer_->setLayerVisibleRegion(
Corey Tabaka2251d822017-04-20 16:04:07 -07001188 display, hardware_composer_layer_,
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001189 {{0, 0, display_metrics_.width, display_metrics_.height}});
Corey Tabaka2251d822017-04-20 16:04:07 -07001190 ALOGE_IF(error != HWC::Error::None,
1191 "Layer::UpdateLayerSettings: Error setting layer visible region: %s",
1192 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001193
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001194 error =
1195 composer_->setLayerPlaneAlpha(display, hardware_composer_layer_, 1.0f);
Corey Tabaka2251d822017-04-20 16:04:07 -07001196 ALOGE_IF(error != HWC::Error::None,
1197 "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s",
1198 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001199}
1200
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001201void Layer::CommonLayerSetup() {
Corey Tabaka2251d822017-04-20 16:04:07 -07001202 HWC::Error error =
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001203 composer_->createLayer(HWC_DISPLAY_PRIMARY, &hardware_composer_layer_);
1204 ALOGE_IF(error != HWC::Error::None,
1205 "Layer::CommonLayerSetup: Failed to create layer on primary "
1206 "display: %s",
1207 error.to_string().c_str());
1208 UpdateLayerSettings();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001209}
1210
1211void Layer::Prepare() {
1212 int right, bottom;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -04001213 sp<GraphicBuffer> handle;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001214
Corey Tabaka2251d822017-04-20 16:04:07 -07001215 // Acquire the next buffer according to the type of source.
1216 IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) {
1217 std::tie(right, bottom, handle, acquire_fence_) = source.Acquire();
1218 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001219
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001220 // Update any visibility (blending, z-order) changes that occurred since
1221 // last prepare.
1222 UpdateVisibilitySettings();
1223
1224 // When a layer is first setup there may be some time before the first
1225 // buffer arrives. Setup the HWC layer as a solid color to stall for time
1226 // until the first buffer arrives. Once the first buffer arrives there will
1227 // always be a buffer for the frame even if it is old.
Corey Tabaka2251d822017-04-20 16:04:07 -07001228 if (!handle.get()) {
1229 if (composition_type_ == HWC::Composition::Invalid) {
1230 composition_type_ = HWC::Composition::SolidColor;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001231 composer_->setLayerCompositionType(
Corey Tabaka2251d822017-04-20 16:04:07 -07001232 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1233 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1234 Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0};
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001235 composer_->setLayerColor(HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1236 layer_color);
Corey Tabaka2251d822017-04-20 16:04:07 -07001237 } else {
1238 // The composition type is already set. Nothing else to do until a
1239 // buffer arrives.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001240 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001241 } else {
Corey Tabaka2251d822017-04-20 16:04:07 -07001242 if (composition_type_ != target_composition_type_) {
1243 composition_type_ = target_composition_type_;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001244 composer_->setLayerCompositionType(
Corey Tabaka2251d822017-04-20 16:04:07 -07001245 HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1246 composition_type_.cast<Hwc2::IComposerClient::Composition>());
1247 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001248
Corey Tabaka2251d822017-04-20 16:04:07 -07001249 HWC::Error error{HWC::Error::None};
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001250 error =
1251 composer_->setLayerBuffer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_,
1252 0, handle, acquire_fence_.Get());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001253
Corey Tabaka2251d822017-04-20 16:04:07 -07001254 ALOGE_IF(error != HWC::Error::None,
1255 "Layer::Prepare: Error setting layer buffer: %s",
1256 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001257
Corey Tabaka2251d822017-04-20 16:04:07 -07001258 if (!surface_rect_functions_applied_) {
1259 const float float_right = right;
1260 const float float_bottom = bottom;
Corey Tabaka2c4aea32017-08-31 20:01:15 -07001261 error = composer_->setLayerSourceCrop(HWC_DISPLAY_PRIMARY,
1262 hardware_composer_layer_,
1263 {0, 0, float_right, float_bottom});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001264
Corey Tabaka2251d822017-04-20 16:04:07 -07001265 ALOGE_IF(error != HWC::Error::None,
1266 "Layer::Prepare: Error setting layer source crop: %s",
1267 error.to_string().c_str());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001268
Corey Tabaka2251d822017-04-20 16:04:07 -07001269 surface_rect_functions_applied_ = true;
1270 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001271 }
1272}
1273
1274void Layer::Finish(int release_fence_fd) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001275 IfAnyOf<SourceSurface, SourceBuffer>::Call(
1276 &source_, [release_fence_fd](auto& source) {
1277 source.Finish(LocalHandle(release_fence_fd));
1278 });
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001279}
1280
Corey Tabaka2251d822017-04-20 16:04:07 -07001281void Layer::Drop() { acquire_fence_.Close(); }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001282
1283} // namespace dvr
1284} // namespace android