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