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