Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #include "hardware_composer.h" |
| 2 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 3 | #include <cutils/properties.h> |
| 4 | #include <cutils/sched_policy.h> |
| 5 | #include <fcntl.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 6 | #include <log/log.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 7 | #include <poll.h> |
| 8 | #include <sync/sync.h> |
| 9 | #include <sys/eventfd.h> |
| 10 | #include <sys/prctl.h> |
| 11 | #include <sys/resource.h> |
| 12 | #include <sys/system_properties.h> |
| 13 | #include <sys/timerfd.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 14 | #include <time.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #include <utils/Trace.h> |
| 17 | |
| 18 | #include <algorithm> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 19 | #include <chrono> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 22 | #include <sstream> |
| 23 | #include <string> |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 24 | #include <tuple> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 25 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 26 | #include <dvr/dvr_display_types.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 27 | #include <dvr/performance_client_api.h> |
| 28 | #include <private/dvr/clock_ns.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 29 | #include <private/dvr/ion_buffer.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 30 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 31 | using android::hardware::Return; |
| 32 | using android::hardware::Void; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 33 | using android::pdx::LocalHandle; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 34 | using android::pdx::rpc::EmptyVariant; |
| 35 | using android::pdx::rpc::IfAnyOf; |
| 36 | |
| 37 | using namespace std::chrono_literals; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace dvr { |
| 41 | |
| 42 | namespace { |
| 43 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 44 | const char kBacklightBrightnessSysFile[] = |
| 45 | "/sys/class/leds/lcd-backlight/brightness"; |
| 46 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 47 | const char kPrimaryDisplayWaitPPEventFile[] = "/sys/class/graphics/fb0/wait_pp"; |
| 48 | |
| 49 | const char kDvrPerformanceProperty[] = "sys.dvr.performance"; |
| 50 | |
Luke Song | 4b78832 | 2017-03-24 14:17:31 -0700 | [diff] [blame] | 51 | const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns"; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 52 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 53 | // 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. |
| 59 | inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) { |
| 60 | return (vsync_period_ns * 150) / 100; |
| 61 | } |
| 62 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 63 | // Attempts to set the scheduler class and partiton for the current thread. |
| 64 | // Returns true on success or false on failure. |
| 65 | bool 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 Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 86 | } // anonymous namespace |
| 87 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 88 | HardwareComposer::HardwareComposer() |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 89 | : initialized_(false), request_display_callback_(nullptr) {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 90 | |
| 91 | HardwareComposer::~HardwareComposer(void) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 92 | UpdatePostThreadState(PostThreadState::Quit, true); |
| 93 | if (post_thread_.joinable()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 94 | post_thread_.join(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 97 | bool HardwareComposer::Initialize( |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 98 | Hwc2::Composer* composer, RequestDisplayCallback request_display_callback) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 99 | if (initialized_) { |
| 100 | ALOGE("HardwareComposer::Initialize: already initialized."); |
| 101 | return false; |
| 102 | } |
| 103 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 104 | request_display_callback_ = request_display_callback; |
| 105 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 106 | HWC::Error error = HWC::Error::None; |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 107 | |
| 108 | Hwc2::Config config; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 109 | error = composer->getActiveConfig(HWC_DISPLAY_PRIMARY, &config); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 110 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 111 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 112 | ALOGE("HardwareComposer: Failed to get current display config : %d", |
| 113 | config); |
| 114 | return false; |
| 115 | } |
| 116 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 117 | error = GetDisplayMetrics(composer, HWC_DISPLAY_PRIMARY, config, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 118 | &native_display_metrics_); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 119 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 120 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 121 | ALOGE( |
| 122 | "HardwareComposer: Failed to get display attributes for current " |
| 123 | "configuration : %d", |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 124 | error.value); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
| 128 | ALOGI( |
| 129 | "HardwareComposer: primary display attributes: width=%d height=%d " |
| 130 | "vsync_period_ns=%d DPI=%dx%d", |
| 131 | native_display_metrics_.width, native_display_metrics_.height, |
| 132 | native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x, |
| 133 | native_display_metrics_.dpi.y); |
| 134 | |
| 135 | // Set the display metrics but never use rotation to avoid the long latency of |
| 136 | // rotation processing in hwc. |
| 137 | display_transform_ = HWC_TRANSFORM_NONE; |
| 138 | display_metrics_ = native_display_metrics_; |
| 139 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 140 | // Setup the display metrics used by all Layer instances. |
| 141 | Layer::SetDisplayMetrics(native_display_metrics_); |
| 142 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 143 | post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 144 | LOG_ALWAYS_FATAL_IF( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 145 | !post_thread_event_fd_, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 146 | "HardwareComposer: Failed to create interrupt event fd : %s", |
| 147 | strerror(errno)); |
| 148 | |
| 149 | post_thread_ = std::thread(&HardwareComposer::PostThread, this); |
| 150 | |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 151 | initialized_ = true; |
| 152 | |
| 153 | return initialized_; |
| 154 | } |
| 155 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 156 | void HardwareComposer::Enable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 157 | UpdatePostThreadState(PostThreadState::Suspended, false); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void HardwareComposer::Disable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 161 | UpdatePostThreadState(PostThreadState::Suspended, true); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 162 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 163 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 164 | // Update the post thread quiescent state based on idle and suspended inputs. |
| 165 | void 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 Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 198 | } |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 199 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 200 | void HardwareComposer::OnPostThreadResumed() { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 201 | composer_.reset(new Hwc2::Composer(false)); |
| 202 | composer_callback_ = new ComposerCallback; |
| 203 | composer_->registerCallback(composer_callback_); |
| 204 | Layer::SetComposer(composer_.get()); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 205 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 206 | EnableVsync(true); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 207 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 208 | // TODO(skiazyk): We need to do something about accessing this directly, |
| 209 | // supposedly there is a backlight service on the way. |
| 210 | // TODO(steventhomas): When we change the backlight setting, will surface |
| 211 | // flinger (or something else) set it back to its original value once we give |
| 212 | // control of the display back to surface flinger? |
| 213 | SetBacklightBrightness(255); |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 214 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 215 | // Trigger target-specific performance mode change. |
| 216 | property_set(kDvrPerformanceProperty, "performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 219 | void HardwareComposer::OnPostThreadPaused() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 220 | retire_fence_fds_.clear(); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 221 | layers_.clear(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 222 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 223 | if (composer_) { |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 224 | EnableVsync(false); |
| 225 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 226 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 227 | composer_callback_ = nullptr; |
| 228 | composer_.reset(nullptr); |
| 229 | Layer::SetComposer(nullptr); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 230 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 231 | // Trigger target-specific performance mode change. |
| 232 | property_set(kDvrPerformanceProperty, "idle"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 235 | HWC::Error HardwareComposer::Validate(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 236 | uint32_t num_types; |
| 237 | uint32_t num_requests; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 238 | HWC::Error error = |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 239 | composer_->validateDisplay(display, &num_types, &num_requests); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 240 | |
| 241 | if (error == HWC2_ERROR_HAS_CHANGES) { |
| 242 | // TODO(skiazyk): We might need to inspect the requested changes first, but |
| 243 | // so far it seems like we shouldn't ever hit a bad state. |
| 244 | // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_, |
| 245 | // display); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 246 | error = composer_->acceptDisplayChanges(display); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return error; |
| 250 | } |
| 251 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 252 | HWC::Error HardwareComposer::EnableVsync(bool enabled) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 253 | return composer_->setVsyncEnabled( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 254 | HWC_DISPLAY_PRIMARY, |
| 255 | (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE |
| 256 | : HWC2_VSYNC_DISABLE)); |
| 257 | } |
| 258 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 259 | HWC::Error HardwareComposer::Present(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 260 | int32_t present_fence; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 261 | HWC::Error error = composer_->presentDisplay(display, &present_fence); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 262 | |
| 263 | // According to the documentation, this fence is signaled at the time of |
| 264 | // vsync/DMA for physical displays. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 265 | if (error == HWC::Error::None) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 266 | ATRACE_INT("HardwareComposer: VsyncFence", present_fence); |
| 267 | retire_fence_fds_.emplace_back(present_fence); |
| 268 | } else { |
| 269 | ATRACE_INT("HardwareComposer: PresentResult", error); |
| 270 | } |
| 271 | |
| 272 | return error; |
| 273 | } |
| 274 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 275 | HWC::Error HardwareComposer::GetDisplayAttribute(Hwc2::Composer* composer, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 276 | hwc2_display_t display, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 277 | hwc2_config_t config, |
| 278 | hwc2_attribute_t attribute, |
| 279 | int32_t* out_value) const { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 280 | return composer->getDisplayAttribute( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 281 | display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value); |
| 282 | } |
| 283 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 284 | HWC::Error HardwareComposer::GetDisplayMetrics( |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 285 | Hwc2::Composer* composer, hwc2_display_t display, hwc2_config_t config, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 286 | HWCDisplayMetrics* out_metrics) const { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 287 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 288 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 289 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_WIDTH, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 290 | &out_metrics->width); |
| 291 | if (error != HWC::Error::None) { |
| 292 | ALOGE( |
| 293 | "HardwareComposer::GetDisplayMetrics: Failed to get display width: %s", |
| 294 | error.to_string().c_str()); |
| 295 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 298 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_HEIGHT, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 299 | &out_metrics->height); |
| 300 | if (error != HWC::Error::None) { |
| 301 | ALOGE( |
| 302 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 303 | error.to_string().c_str()); |
| 304 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 307 | error = GetDisplayAttribute(composer, display, config, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 308 | HWC2_ATTRIBUTE_VSYNC_PERIOD, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 309 | &out_metrics->vsync_period_ns); |
| 310 | if (error != HWC::Error::None) { |
| 311 | ALOGE( |
| 312 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 313 | error.to_string().c_str()); |
| 314 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 317 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_X, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 318 | &out_metrics->dpi.x); |
| 319 | if (error != HWC::Error::None) { |
| 320 | ALOGE( |
| 321 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI X: %s", |
| 322 | error.to_string().c_str()); |
| 323 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 326 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_Y, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 327 | &out_metrics->dpi.y); |
| 328 | if (error != HWC::Error::None) { |
| 329 | ALOGE( |
| 330 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI Y: %s", |
| 331 | error.to_string().c_str()); |
| 332 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 335 | return HWC::Error::None; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 338 | std::string HardwareComposer::Dump() { |
| 339 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 340 | std::ostringstream stream; |
| 341 | |
| 342 | stream << "Display metrics: " << display_metrics_.width << "x" |
| 343 | << display_metrics_.height << " " << (display_metrics_.dpi.x / 1000.0) |
| 344 | << "x" << (display_metrics_.dpi.y / 1000.0) << " dpi @ " |
| 345 | << (1000000000.0 / display_metrics_.vsync_period_ns) << " Hz" |
| 346 | << std::endl; |
| 347 | |
| 348 | stream << "Post thread resumed: " << post_thread_resumed_ << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 349 | stream << "Active layers: " << layers_.size() << std::endl; |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 350 | stream << std::endl; |
| 351 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 352 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 353 | stream << "Layer " << i << ":"; |
| 354 | stream << " type=" << layers_[i].GetCompositionType().to_string(); |
| 355 | stream << " surface_id=" << layers_[i].GetSurfaceId(); |
| 356 | stream << " buffer_id=" << layers_[i].GetBufferId(); |
| 357 | stream << std::endl; |
| 358 | } |
| 359 | stream << std::endl; |
| 360 | |
| 361 | if (post_thread_resumed_) { |
| 362 | stream << "Hardware Composer Debug Info:" << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 363 | stream << composer_->dumpDebugInfo(); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return stream.str(); |
| 367 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 368 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 369 | void HardwareComposer::PostLayers() { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 370 | ATRACE_NAME("HardwareComposer::PostLayers"); |
| 371 | |
| 372 | // Setup the hardware composer layers with current buffers. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 373 | for (auto& layer : layers_) { |
| 374 | layer.Prepare(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 377 | HWC::Error error = Validate(HWC_DISPLAY_PRIMARY); |
| 378 | if (error != HWC::Error::None) { |
| 379 | ALOGE("HardwareComposer::PostLayers: Validate failed: %s", |
| 380 | error.to_string().c_str()); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 384 | // Now that we have taken in a frame from the application, we have a chance |
| 385 | // to drop the frame before passing the frame along to HWC. |
| 386 | // If the display driver has become backed up, we detect it here and then |
| 387 | // react by skipping this frame to catch up latency. |
| 388 | while (!retire_fence_fds_.empty() && |
| 389 | (!retire_fence_fds_.front() || |
| 390 | sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) { |
| 391 | // There are only 2 fences in here, no performance problem to shift the |
| 392 | // array of ints. |
| 393 | retire_fence_fds_.erase(retire_fence_fds_.begin()); |
| 394 | } |
| 395 | |
| 396 | const bool is_frame_pending = IsFramePendingInDriver(); |
George Burgess IV | 353a6f6 | 2017-06-26 17:13:09 -0700 | [diff] [blame] | 397 | const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) > |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 398 | post_thread_config_.allowed_pending_fence_count; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 399 | |
| 400 | if (is_fence_pending || is_frame_pending) { |
| 401 | ATRACE_INT("frame_skip_count", ++frame_skip_count_); |
| 402 | |
| 403 | ALOGW_IF(is_frame_pending, "Warning: frame already queued, dropping frame"); |
| 404 | ALOGW_IF(is_fence_pending, |
| 405 | "Warning: dropping a frame to catch up with HWC (pending = %zd)", |
| 406 | retire_fence_fds_.size()); |
| 407 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 408 | for (auto& layer : layers_) { |
| 409 | layer.Drop(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 410 | } |
| 411 | return; |
| 412 | } else { |
| 413 | // Make the transition more obvious in systrace when the frame skip happens |
| 414 | // above. |
| 415 | ATRACE_INT("frame_skip_count", 0); |
| 416 | } |
| 417 | |
Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 418 | #if TRACE > 1 |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 419 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 420 | ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s", |
| 421 | i, layers_[i].GetBufferId(), |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 422 | layers_[i].GetCompositionType().to_string().c_str()); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 423 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 424 | #endif |
| 425 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 426 | error = Present(HWC_DISPLAY_PRIMARY); |
| 427 | if (error != HWC::Error::None) { |
| 428 | ALOGE("HardwareComposer::PostLayers: Present failed: %s", |
| 429 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | |
| 433 | std::vector<Hwc2::Layer> out_layers; |
| 434 | std::vector<int> out_fences; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 435 | error = composer_->getReleaseFences(HWC_DISPLAY_PRIMARY, &out_layers, |
| 436 | &out_fences); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 437 | ALOGE_IF(error != HWC::Error::None, |
| 438 | "HardwareComposer::PostLayers: Failed to get release fences: %s", |
| 439 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 440 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 441 | // Perform post-frame bookkeeping. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 442 | uint32_t num_elements = out_layers.size(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 443 | for (size_t i = 0; i < num_elements; ++i) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 444 | for (auto& layer : layers_) { |
| 445 | if (layer.GetLayerHandle() == out_layers[i]) { |
| 446 | layer.Finish(out_fences[i]); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 452 | void HardwareComposer::SetDisplaySurfaces( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 453 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) { |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 454 | ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd", |
| 455 | surfaces.size()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 456 | const bool display_idle = surfaces.size() == 0; |
| 457 | { |
| 458 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 459 | pending_surfaces_ = std::move(surfaces); |
| 460 | } |
| 461 | |
Steven Thomas | 2ddf567 | 2017-06-15 11:38:40 -0700 | [diff] [blame] | 462 | if (request_display_callback_) |
| 463 | request_display_callback_(!display_idle); |
| 464 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 465 | // Set idle state based on whether there are any surfaces to handle. |
| 466 | UpdatePostThreadState(PostThreadState::Idle, display_idle); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 467 | } |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 468 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 469 | int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key, |
| 470 | IonBuffer& ion_buffer) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 471 | if (key == DvrGlobalBuffers::kVsyncBuffer) { |
| 472 | vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>( |
| 473 | &ion_buffer, CPUUsageMode::WRITE_OFTEN); |
| 474 | |
| 475 | if (vsync_ring_->IsMapped() == false) { |
| 476 | return -EPERM; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 481 | return MapConfigBuffer(ion_buffer); |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 488 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 489 | ConfigBufferDeleted(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) { |
| 494 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 495 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 496 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 497 | if (ion_buffer.width() < DvrConfigRing::MemorySize()) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 498 | ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size."); |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
| 502 | void* buffer_base = 0; |
| 503 | int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(), |
| 504 | ion_buffer.height(), &buffer_base); |
| 505 | if (result != 0) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 506 | ALOGE( |
| 507 | "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config " |
| 508 | "buffer."); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 509 | return -EPERM; |
| 510 | } |
| 511 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 512 | shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width()); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 513 | ion_buffer.Unlock(); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | void HardwareComposer::ConfigBufferDeleted() { |
| 519 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 520 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void HardwareComposer::UpdateConfigBuffer() { |
| 524 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
| 525 | if (!shared_config_ring_.is_valid()) |
| 526 | return; |
| 527 | // Copy from latest record in shared_config_ring_ to local copy. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 528 | DvrConfig record; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 529 | if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) { |
| 530 | post_thread_config_ = record; |
| 531 | } |
| 532 | } |
| 533 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 534 | int HardwareComposer::PostThreadPollInterruptible( |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 535 | const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 536 | pollfd pfd[2] = { |
| 537 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 538 | .fd = event_fd.Get(), |
Steven Thomas | 66747c1 | 2017-03-22 18:45:31 -0700 | [diff] [blame] | 539 | .events = static_cast<short>(requested_events), |
| 540 | .revents = 0, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 541 | }, |
| 542 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 543 | .fd = post_thread_event_fd_.Get(), |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 544 | .events = POLLPRI | POLLIN, |
| 545 | .revents = 0, |
| 546 | }, |
| 547 | }; |
| 548 | int ret, error; |
| 549 | do { |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 550 | ret = poll(pfd, 2, timeout_ms); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 551 | error = errno; |
| 552 | ALOGW_IF(ret < 0, |
| 553 | "HardwareComposer::PostThreadPollInterruptible: Error during " |
| 554 | "poll(): %s (%d)", |
| 555 | strerror(error), error); |
| 556 | } while (ret < 0 && error == EINTR); |
| 557 | |
| 558 | if (ret < 0) { |
| 559 | return -error; |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 560 | } else if (ret == 0) { |
| 561 | return -ETIMEDOUT; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 562 | } else if (pfd[0].revents != 0) { |
| 563 | return 0; |
| 564 | } else if (pfd[1].revents != 0) { |
| 565 | ALOGI("VrHwcPost thread interrupted"); |
| 566 | return kPostThreadInterrupted; |
| 567 | } else { |
| 568 | return 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 569 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Reads the value of the display driver wait_pingpong state. Returns 0 or 1 |
| 573 | // (the value of the state) on success or a negative error otherwise. |
| 574 | // TODO(eieio): This is pretty driver specific, this should be moved to a |
| 575 | // separate class eventually. |
| 576 | int HardwareComposer::ReadWaitPPState() { |
| 577 | // Gracefully handle when the kernel does not support this feature. |
| 578 | if (!primary_display_wait_pp_fd_) |
| 579 | return 0; |
| 580 | |
| 581 | const int wait_pp_fd = primary_display_wait_pp_fd_.Get(); |
| 582 | int ret, error; |
| 583 | |
| 584 | ret = lseek(wait_pp_fd, 0, SEEK_SET); |
| 585 | if (ret < 0) { |
| 586 | error = errno; |
| 587 | ALOGE("HardwareComposer::ReadWaitPPState: Failed to seek wait_pp fd: %s", |
| 588 | strerror(error)); |
| 589 | return -error; |
| 590 | } |
| 591 | |
| 592 | char data = -1; |
| 593 | ret = read(wait_pp_fd, &data, sizeof(data)); |
| 594 | if (ret < 0) { |
| 595 | error = errno; |
| 596 | ALOGE("HardwareComposer::ReadWaitPPState: Failed to read wait_pp state: %s", |
| 597 | strerror(error)); |
| 598 | return -error; |
| 599 | } |
| 600 | |
| 601 | switch (data) { |
| 602 | case '0': |
| 603 | return 0; |
| 604 | case '1': |
| 605 | return 1; |
| 606 | default: |
| 607 | ALOGE( |
| 608 | "HardwareComposer::ReadWaitPPState: Unexpected value for wait_pp: %d", |
| 609 | data); |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | } |
| 613 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 614 | // Waits for the next vsync and returns the timestamp of the vsync event. If |
| 615 | // vsync already passed since the last call, returns the latest vsync timestamp |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 616 | // instead of blocking. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 617 | int HardwareComposer::WaitForVSync(int64_t* timestamp) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 618 | int error = PostThreadPollInterruptible(composer_callback_->GetVsyncEventFd(), |
| 619 | POLLIN, /*timeout_ms*/ 1000); |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 620 | if (error == kPostThreadInterrupted || error < 0) { |
| 621 | return error; |
| 622 | } else { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 623 | *timestamp = composer_callback_->GetVsyncTime(); |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 624 | return 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) { |
| 629 | const int timer_fd = vsync_sleep_timer_fd_.Get(); |
| 630 | const itimerspec wakeup_itimerspec = { |
| 631 | .it_interval = {.tv_sec = 0, .tv_nsec = 0}, |
| 632 | .it_value = NsToTimespec(wakeup_timestamp), |
| 633 | }; |
| 634 | int ret = |
| 635 | timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr); |
| 636 | int error = errno; |
| 637 | if (ret < 0) { |
| 638 | ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s", |
| 639 | strerror(error)); |
| 640 | return -error; |
| 641 | } |
| 642 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 643 | return PostThreadPollInterruptible( |
| 644 | vsync_sleep_timer_fd_, POLLIN, /*timeout_ms*/ -1); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void HardwareComposer::PostThread() { |
| 648 | // NOLINTNEXTLINE(runtime/int) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 649 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 650 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 651 | // Set the scheduler to SCHED_FIFO with high priority. If this fails here |
| 652 | // there may have been a startup timing issue between this thread and |
| 653 | // performanced. Try again later when this thread becomes active. |
| 654 | bool thread_policy_setup = |
| 655 | SetThreadPolicy("graphics:high", "/system/performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 656 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 657 | #if ENABLE_BACKLIGHT_BRIGHTNESS |
| 658 | // TODO(hendrikw): This isn't required at the moment. It's possible that there |
| 659 | // is another method to access this when needed. |
| 660 | // Open the backlight brightness control sysfs node. |
| 661 | backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR); |
| 662 | ALOGW_IF(!backlight_brightness_fd_, |
| 663 | "HardwareComposer: Failed to open backlight brightness control: %s", |
| 664 | strerror(errno)); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 665 | #endif // ENABLE_BACKLIGHT_BRIGHTNESS |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 666 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 667 | // Open the wait pingpong status node for the primary display. |
| 668 | // TODO(eieio): Move this into a platform-specific class. |
| 669 | primary_display_wait_pp_fd_ = |
| 670 | LocalHandle(kPrimaryDisplayWaitPPEventFile, O_RDONLY); |
| 671 | ALOGW_IF( |
| 672 | !primary_display_wait_pp_fd_, |
| 673 | "HardwareComposer: Failed to open wait_pp node for primary display: %s", |
| 674 | strerror(errno)); |
| 675 | |
| 676 | // Create a timerfd based on CLOCK_MONOTINIC. |
| 677 | vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0)); |
| 678 | LOG_ALWAYS_FATAL_IF( |
| 679 | !vsync_sleep_timer_fd_, |
| 680 | "HardwareComposer: Failed to create vsync sleep timerfd: %s", |
| 681 | strerror(errno)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 682 | |
| 683 | const int64_t ns_per_frame = display_metrics_.vsync_period_ns; |
| 684 | const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame); |
| 685 | |
| 686 | // TODO(jbates) Query vblank time from device, when such an API is available. |
| 687 | // This value (6.3%) was measured on A00 in low persistence mode. |
| 688 | int64_t vblank_ns = ns_per_frame * 63 / 1000; |
| 689 | int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2; |
| 690 | |
| 691 | // Check property for overriding right eye offset value. |
| 692 | right_eye_photon_offset_ns = |
| 693 | property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns); |
| 694 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 695 | bool was_running = false; |
| 696 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 697 | while (1) { |
| 698 | ATRACE_NAME("HardwareComposer::PostThread"); |
| 699 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 700 | // Check for updated config once per vsync. |
| 701 | UpdateConfigBuffer(); |
| 702 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 703 | while (post_thread_quiescent_) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 704 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 705 | ALOGI("HardwareComposer::PostThread: Entering quiescent state."); |
| 706 | |
Corey Tabaka | df0b916 | 2017-08-03 17:14:08 -0700 | [diff] [blame] | 707 | // Tear down resources if necessary. |
| 708 | if (was_running) |
| 709 | OnPostThreadPaused(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 710 | |
| 711 | was_running = false; |
| 712 | post_thread_resumed_ = false; |
| 713 | post_thread_ready_.notify_all(); |
| 714 | |
| 715 | if (post_thread_state_ & PostThreadState::Quit) { |
| 716 | ALOGI("HardwareComposer::PostThread: Quitting."); |
| 717 | return; |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 718 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 719 | |
| 720 | post_thread_wait_.wait(lock, [this] { return !post_thread_quiescent_; }); |
| 721 | |
| 722 | post_thread_resumed_ = true; |
| 723 | post_thread_ready_.notify_all(); |
| 724 | |
| 725 | ALOGI("HardwareComposer::PostThread: Exiting quiescent state."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | if (!was_running) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 729 | // Setup resources. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 730 | OnPostThreadResumed(); |
| 731 | was_running = true; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 732 | |
| 733 | // Try to setup the scheduler policy if it failed during startup. Only |
| 734 | // attempt to do this on transitions from inactive to active to avoid |
| 735 | // spamming the system with RPCs and log messages. |
| 736 | if (!thread_policy_setup) { |
| 737 | thread_policy_setup = |
| 738 | SetThreadPolicy("graphics:high", "/system/performance"); |
| 739 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | int64_t vsync_timestamp = 0; |
| 743 | { |
| 744 | std::array<char, 128> buf; |
| 745 | snprintf(buf.data(), buf.size(), "wait_vsync|vsync=%d|", |
| 746 | vsync_count_ + 1); |
| 747 | ATRACE_NAME(buf.data()); |
| 748 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 749 | const int error = WaitForVSync(&vsync_timestamp); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 750 | ALOGE_IF( |
| 751 | error < 0, |
| 752 | "HardwareComposer::PostThread: Failed to wait for vsync event: %s", |
| 753 | strerror(-error)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 754 | // Don't bother processing this frame if a pause was requested |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 755 | if (error == kPostThreadInterrupted) |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 756 | continue; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | ++vsync_count_; |
| 760 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 761 | const bool layer_config_changed = UpdateLayerConfig(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 762 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 763 | // Publish the vsync event. |
| 764 | if (vsync_ring_) { |
| 765 | DvrVsync vsync; |
| 766 | vsync.vsync_count = vsync_count_; |
| 767 | vsync.vsync_timestamp_ns = vsync_timestamp; |
| 768 | vsync.vsync_left_eye_offset_ns = photon_offset_ns; |
| 769 | vsync.vsync_right_eye_offset_ns = right_eye_photon_offset_ns; |
| 770 | vsync.vsync_period_ns = ns_per_frame; |
| 771 | |
| 772 | vsync_ring_->Publish(vsync); |
| 773 | } |
| 774 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 775 | // Signal all of the vsync clients. Because absolute time is used for the |
| 776 | // wakeup time below, this can take a little time if necessary. |
| 777 | if (vsync_callback_) |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 778 | vsync_callback_(HWC_DISPLAY_PRIMARY, vsync_timestamp, |
| 779 | /*frame_time_estimate*/ 0, vsync_count_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 780 | |
| 781 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 782 | // Sleep until shortly before vsync. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 783 | ATRACE_NAME("sleep"); |
| 784 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 785 | const int64_t display_time_est_ns = vsync_timestamp + ns_per_frame; |
| 786 | const int64_t now_ns = GetSystemClockNs(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 787 | const int64_t sleep_time_ns = display_time_est_ns - now_ns - |
| 788 | post_thread_config_.frame_post_offset_ns; |
| 789 | const int64_t wakeup_time_ns = |
| 790 | display_time_est_ns - post_thread_config_.frame_post_offset_ns; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 791 | |
| 792 | ATRACE_INT64("sleep_time_ns", sleep_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 793 | if (sleep_time_ns > 0) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 794 | int error = SleepUntil(wakeup_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 795 | ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s", |
| 796 | strerror(-error)); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 797 | if (error == kPostThreadInterrupted) { |
| 798 | if (layer_config_changed) { |
| 799 | // If the layer config changed we need to validateDisplay() even if |
| 800 | // we're going to drop the frame, to flush the Composer object's |
| 801 | // internal command buffer and apply our layer changes. |
| 802 | Validate(HWC_DISPLAY_PRIMARY); |
| 803 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 804 | continue; |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 805 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 809 | PostLayers(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 810 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 813 | // Checks for changes in the surface stack and updates the layer config to |
| 814 | // accomodate the new stack. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 815 | bool HardwareComposer::UpdateLayerConfig() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 816 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 817 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 818 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 819 | if (pending_surfaces_.empty()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 820 | return false; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 821 | |
| 822 | surfaces = std::move(pending_surfaces_); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 823 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 824 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 825 | ATRACE_NAME("UpdateLayerConfig_HwLayers"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 826 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 827 | // Sort the new direct surface list by z-order to determine the relative order |
| 828 | // of the surfaces. This relative order is used for the HWC z-order value to |
| 829 | // insulate VrFlinger and HWC z-order semantics from each other. |
| 830 | std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) { |
| 831 | return a->z_order() < b->z_order(); |
| 832 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 833 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 834 | // Prepare a new layer stack, pulling in layers from the previous |
| 835 | // layer stack that are still active and updating their attributes. |
| 836 | std::vector<Layer> layers; |
| 837 | size_t layer_index = 0; |
| 838 | for (const auto& surface : surfaces) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 839 | // The bottom layer is opaque, other layers blend. |
| 840 | HWC::BlendMode blending = |
| 841 | layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 842 | |
| 843 | // Try to find a layer for this surface in the set of active layers. |
| 844 | auto search = |
| 845 | std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id()); |
| 846 | const bool found = search != layers_.end() && |
| 847 | search->GetSurfaceId() == surface->surface_id(); |
| 848 | if (found) { |
| 849 | // Update the attributes of the layer that may have changed. |
| 850 | search->SetBlending(blending); |
| 851 | search->SetZOrder(layer_index); // Relative z-order. |
| 852 | |
| 853 | // Move the existing layer to the new layer set and remove the empty layer |
| 854 | // object from the current set. |
| 855 | layers.push_back(std::move(*search)); |
| 856 | layers_.erase(search); |
| 857 | } else { |
| 858 | // Insert a layer for the new surface. |
| 859 | layers.emplace_back(surface, blending, display_transform_, |
| 860 | HWC::Composition::Device, layer_index); |
| 861 | } |
| 862 | |
| 863 | ALOGI_IF( |
| 864 | TRACE, |
| 865 | "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d", |
| 866 | layer_index, layers[layer_index].GetSurfaceId()); |
| 867 | |
| 868 | layer_index++; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 871 | // Sort the new layer stack by ascending surface id. |
| 872 | std::sort(layers.begin(), layers.end()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 873 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 874 | // Replace the previous layer set with the new layer set. The destructor of |
| 875 | // the previous set will clean up the remaining Layers that are not moved to |
| 876 | // the new layer set. |
| 877 | layers_ = std::move(layers); |
| 878 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 879 | ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers", |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 880 | layers_.size()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 881 | return true; |
| 882 | } |
| 883 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 884 | void HardwareComposer::SetVSyncCallback(VSyncCallback callback) { |
| 885 | vsync_callback_ = callback; |
| 886 | } |
| 887 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 888 | void HardwareComposer::SetBacklightBrightness(int brightness) { |
| 889 | if (backlight_brightness_fd_) { |
| 890 | std::array<char, 32> text; |
| 891 | const int length = snprintf(text.data(), text.size(), "%d", brightness); |
| 892 | write(backlight_brightness_fd_.Get(), text.data(), length); |
| 893 | } |
| 894 | } |
| 895 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 896 | HardwareComposer::ComposerCallback::ComposerCallback() { |
| 897 | vsync_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); |
| 898 | LOG_ALWAYS_FATAL_IF( |
| 899 | !vsync_event_fd_, |
| 900 | "Failed to create vsync event fd : %s", |
| 901 | strerror(errno)); |
| 902 | } |
| 903 | |
| 904 | Return<void> HardwareComposer::ComposerCallback::onHotplug( |
| 905 | Hwc2::Display /*display*/, |
| 906 | IComposerCallback::Connection /*conn*/) { |
| 907 | return Void(); |
| 908 | } |
| 909 | |
| 910 | Return<void> HardwareComposer::ComposerCallback::onRefresh( |
| 911 | Hwc2::Display /*display*/) { |
| 912 | return hardware::Void(); |
| 913 | } |
| 914 | |
| 915 | Return<void> HardwareComposer::ComposerCallback::onVsync( |
| 916 | Hwc2::Display display, int64_t timestamp) { |
| 917 | if (display == HWC_DISPLAY_PRIMARY) { |
| 918 | std::lock_guard<std::mutex> lock(vsync_mutex_); |
| 919 | vsync_time_ = timestamp; |
| 920 | int error = eventfd_write(vsync_event_fd_.Get(), 1); |
| 921 | LOG_ALWAYS_FATAL_IF(error != 0, "Failed writing to vsync event fd"); |
| 922 | } |
| 923 | return Void(); |
| 924 | } |
| 925 | |
| 926 | const pdx::LocalHandle& |
| 927 | HardwareComposer::ComposerCallback::GetVsyncEventFd() const { |
| 928 | return vsync_event_fd_; |
| 929 | } |
| 930 | |
| 931 | int64_t HardwareComposer::ComposerCallback::GetVsyncTime() { |
| 932 | std::lock_guard<std::mutex> lock(vsync_mutex_); |
| 933 | eventfd_t event; |
| 934 | eventfd_read(vsync_event_fd_.Get(), &event); |
| 935 | LOG_ALWAYS_FATAL_IF(vsync_time_ < 0, |
| 936 | "Attempt to read vsync time before vsync event"); |
| 937 | int64_t return_val = vsync_time_; |
| 938 | vsync_time_ = -1; |
| 939 | return return_val; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 942 | Hwc2::Composer* Layer::composer_{nullptr}; |
| 943 | HWCDisplayMetrics Layer::display_metrics_{0, 0, {0, 0}, 0}; |
| 944 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 945 | void Layer::Reset() { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 946 | if (hardware_composer_layer_) { |
| 947 | composer_->destroyLayer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 948 | hardware_composer_layer_ = 0; |
| 949 | } |
| 950 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 951 | z_order_ = 0; |
| 952 | blending_ = HWC::BlendMode::None; |
| 953 | transform_ = HWC::Transform::None; |
| 954 | composition_type_ = HWC::Composition::Invalid; |
| 955 | target_composition_type_ = composition_type_; |
| 956 | source_ = EmptyVariant{}; |
| 957 | acquire_fence_.Close(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 958 | surface_rect_functions_applied_ = false; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 959 | pending_visibility_settings_ = true; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 960 | } |
| 961 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 962 | Layer::Layer(const std::shared_ptr<DirectDisplaySurface>& surface, |
| 963 | HWC::BlendMode blending, HWC::Transform transform, |
| 964 | HWC::Composition composition_type, size_t z_order) |
| 965 | : z_order_{z_order}, |
| 966 | blending_{blending}, |
| 967 | transform_{transform}, |
| 968 | target_composition_type_{composition_type}, |
| 969 | source_{SourceSurface{surface}} { |
| 970 | CommonLayerSetup(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 973 | Layer::Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending, |
| 974 | HWC::Transform transform, HWC::Composition composition_type, |
| 975 | size_t z_order) |
| 976 | : z_order_{z_order}, |
| 977 | blending_{blending}, |
| 978 | transform_{transform}, |
| 979 | target_composition_type_{composition_type}, |
| 980 | source_{SourceBuffer{buffer}} { |
| 981 | CommonLayerSetup(); |
| 982 | } |
| 983 | |
| 984 | Layer::~Layer() { Reset(); } |
| 985 | |
| 986 | Layer::Layer(Layer&& other) { *this = std::move(other); } |
| 987 | |
| 988 | Layer& Layer::operator=(Layer&& other) { |
| 989 | if (this != &other) { |
| 990 | Reset(); |
| 991 | using std::swap; |
| 992 | swap(hardware_composer_layer_, other.hardware_composer_layer_); |
| 993 | swap(z_order_, other.z_order_); |
| 994 | swap(blending_, other.blending_); |
| 995 | swap(transform_, other.transform_); |
| 996 | swap(composition_type_, other.composition_type_); |
| 997 | swap(target_composition_type_, other.target_composition_type_); |
| 998 | swap(source_, other.source_); |
| 999 | swap(acquire_fence_, other.acquire_fence_); |
| 1000 | swap(surface_rect_functions_applied_, |
| 1001 | other.surface_rect_functions_applied_); |
| 1002 | swap(pending_visibility_settings_, other.pending_visibility_settings_); |
| 1003 | } |
| 1004 | return *this; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1007 | void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) { |
| 1008 | if (source_.is<SourceBuffer>()) |
| 1009 | std::get<SourceBuffer>(source_) = {buffer}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1012 | void Layer::SetBlending(HWC::BlendMode blending) { |
| 1013 | if (blending_ != blending) { |
| 1014 | blending_ = blending; |
| 1015 | pending_visibility_settings_ = true; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void Layer::SetZOrder(size_t z_order) { |
| 1020 | if (z_order_ != z_order) { |
| 1021 | z_order_ = z_order; |
| 1022 | pending_visibility_settings_ = true; |
| 1023 | } |
| 1024 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1025 | |
| 1026 | IonBuffer* Layer::GetBuffer() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1027 | struct Visitor { |
| 1028 | IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); } |
| 1029 | IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); } |
| 1030 | IonBuffer* operator()(EmptyVariant) { return nullptr; } |
| 1031 | }; |
| 1032 | return source_.Visit(Visitor{}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1035 | void Layer::UpdateVisibilitySettings() { |
| 1036 | if (pending_visibility_settings_) { |
| 1037 | pending_visibility_settings_ = false; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1038 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1039 | HWC::Error error; |
| 1040 | hwc2_display_t display = HWC_DISPLAY_PRIMARY; |
| 1041 | |
| 1042 | error = composer_->setLayerBlendMode( |
| 1043 | display, hardware_composer_layer_, |
| 1044 | blending_.cast<Hwc2::IComposerClient::BlendMode>()); |
| 1045 | ALOGE_IF(error != HWC::Error::None, |
| 1046 | "Layer::UpdateLayerSettings: Error setting layer blend mode: %s", |
| 1047 | error.to_string().c_str()); |
| 1048 | |
| 1049 | error = |
| 1050 | composer_->setLayerZOrder(display, hardware_composer_layer_, z_order_); |
| 1051 | ALOGE_IF(error != HWC::Error::None, |
| 1052 | "Layer::UpdateLayerSettings: Error setting z_ order: %s", |
| 1053 | error.to_string().c_str()); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | void Layer::UpdateLayerSettings() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1058 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1059 | hwc2_display_t display = HWC_DISPLAY_PRIMARY; |
| 1060 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1061 | UpdateVisibilitySettings(); |
| 1062 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1063 | // TODO(eieio): Use surface attributes or some other mechanism to control |
| 1064 | // the layer display frame. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1065 | error = composer_->setLayerDisplayFrame( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1066 | display, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1067 | {0, 0, display_metrics_.width, display_metrics_.height}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1068 | ALOGE_IF(error != HWC::Error::None, |
| 1069 | "Layer::UpdateLayerSettings: Error setting layer display frame: %s", |
| 1070 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1071 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1072 | error = composer_->setLayerVisibleRegion( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1073 | display, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1074 | {{0, 0, display_metrics_.width, display_metrics_.height}}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1075 | ALOGE_IF(error != HWC::Error::None, |
| 1076 | "Layer::UpdateLayerSettings: Error setting layer visible region: %s", |
| 1077 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1078 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1079 | error = |
| 1080 | composer_->setLayerPlaneAlpha(display, hardware_composer_layer_, 1.0f); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1081 | ALOGE_IF(error != HWC::Error::None, |
| 1082 | "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s", |
| 1083 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1086 | void Layer::CommonLayerSetup() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1087 | HWC::Error error = |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1088 | composer_->createLayer(HWC_DISPLAY_PRIMARY, &hardware_composer_layer_); |
| 1089 | ALOGE_IF(error != HWC::Error::None, |
| 1090 | "Layer::CommonLayerSetup: Failed to create layer on primary " |
| 1091 | "display: %s", |
| 1092 | error.to_string().c_str()); |
| 1093 | UpdateLayerSettings(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | void Layer::Prepare() { |
| 1097 | int right, bottom; |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 1098 | sp<GraphicBuffer> handle; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1099 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1100 | // Acquire the next buffer according to the type of source. |
| 1101 | IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) { |
| 1102 | std::tie(right, bottom, handle, acquire_fence_) = source.Acquire(); |
| 1103 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1104 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1105 | // Update any visibility (blending, z-order) changes that occurred since |
| 1106 | // last prepare. |
| 1107 | UpdateVisibilitySettings(); |
| 1108 | |
| 1109 | // When a layer is first setup there may be some time before the first |
| 1110 | // buffer arrives. Setup the HWC layer as a solid color to stall for time |
| 1111 | // until the first buffer arrives. Once the first buffer arrives there will |
| 1112 | // always be a buffer for the frame even if it is old. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1113 | if (!handle.get()) { |
| 1114 | if (composition_type_ == HWC::Composition::Invalid) { |
| 1115 | composition_type_ = HWC::Composition::SolidColor; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1116 | composer_->setLayerCompositionType( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1117 | HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1118 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1119 | Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1120 | composer_->setLayerColor(HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1121 | layer_color); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1122 | } else { |
| 1123 | // The composition type is already set. Nothing else to do until a |
| 1124 | // buffer arrives. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1125 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1126 | } else { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1127 | if (composition_type_ != target_composition_type_) { |
| 1128 | composition_type_ = target_composition_type_; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1129 | composer_->setLayerCompositionType( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1130 | HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1131 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1132 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1133 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1134 | HWC::Error error{HWC::Error::None}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1135 | error = |
| 1136 | composer_->setLayerBuffer(HWC_DISPLAY_PRIMARY, hardware_composer_layer_, |
| 1137 | 0, handle, acquire_fence_.Get()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1138 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1139 | ALOGE_IF(error != HWC::Error::None, |
| 1140 | "Layer::Prepare: Error setting layer buffer: %s", |
| 1141 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1142 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1143 | if (!surface_rect_functions_applied_) { |
| 1144 | const float float_right = right; |
| 1145 | const float float_bottom = bottom; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1146 | error = composer_->setLayerSourceCrop(HWC_DISPLAY_PRIMARY, |
| 1147 | hardware_composer_layer_, |
| 1148 | {0, 0, float_right, float_bottom}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1149 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1150 | ALOGE_IF(error != HWC::Error::None, |
| 1151 | "Layer::Prepare: Error setting layer source crop: %s", |
| 1152 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1153 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1154 | surface_rect_functions_applied_ = true; |
| 1155 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | void Layer::Finish(int release_fence_fd) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1160 | IfAnyOf<SourceSurface, SourceBuffer>::Call( |
| 1161 | &source_, [release_fence_fd](auto& source) { |
| 1162 | source.Finish(LocalHandle(release_fence_fd)); |
| 1163 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1166 | void Layer::Drop() { acquire_fence_.Close(); } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1167 | |
| 1168 | } // namespace dvr |
| 1169 | } // namespace android |