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> |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 9 | #include <sync/sync.h> |
| 10 | #include <sys/eventfd.h> |
| 11 | #include <sys/prctl.h> |
| 12 | #include <sys/resource.h> |
| 13 | #include <sys/system_properties.h> |
| 14 | #include <sys/timerfd.h> |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 15 | #include <sys/types.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 16 | #include <time.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <utils/Trace.h> |
| 19 | |
| 20 | #include <algorithm> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 21 | #include <chrono> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <map> |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 24 | #include <sstream> |
| 25 | #include <string> |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 26 | #include <tuple> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 27 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 28 | #include <dvr/dvr_display_types.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 29 | #include <dvr/performance_client_api.h> |
| 30 | #include <private/dvr/clock_ns.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 31 | #include <private/dvr/ion_buffer.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 32 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 33 | using android::hardware::Return; |
| 34 | using android::hardware::Void; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 35 | using android::pdx::ErrorStatus; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 36 | using android::pdx::LocalHandle; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 37 | using android::pdx::Status; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 38 | using android::pdx::rpc::EmptyVariant; |
| 39 | using android::pdx::rpc::IfAnyOf; |
| 40 | |
| 41 | using namespace std::chrono_literals; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | namespace dvr { |
| 45 | |
| 46 | namespace { |
| 47 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 48 | const char kBacklightBrightnessSysFile[] = |
| 49 | "/sys/class/leds/lcd-backlight/brightness"; |
| 50 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 51 | const char kDvrPerformanceProperty[] = "sys.dvr.performance"; |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 52 | const char kDvrStandaloneProperty[] = "ro.boot.vr"; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 53 | |
Luke Song | 4b78832 | 2017-03-24 14:17:31 -0700 | [diff] [blame] | 54 | const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns"; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 55 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 56 | // How long to wait after boot finishes before we turn the display off. |
| 57 | constexpr int kBootFinishedDisplayOffTimeoutSec = 10; |
| 58 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 59 | // Get time offset from a vsync to when the pose for that vsync should be |
| 60 | // predicted out to. For example, if scanout gets halfway through the frame |
| 61 | // at the halfway point between vsyncs, then this could be half the period. |
| 62 | // With global shutter displays, this should be changed to the offset to when |
| 63 | // illumination begins. Low persistence adds a frame of latency, so we predict |
| 64 | // to the center of the next frame. |
| 65 | inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) { |
| 66 | return (vsync_period_ns * 150) / 100; |
| 67 | } |
| 68 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 69 | // Attempts to set the scheduler class and partiton for the current thread. |
| 70 | // Returns true on success or false on failure. |
| 71 | bool SetThreadPolicy(const std::string& scheduler_class, |
| 72 | const std::string& partition) { |
| 73 | int error = dvrSetSchedulerClass(0, scheduler_class.c_str()); |
| 74 | if (error < 0) { |
| 75 | ALOGE( |
| 76 | "SetThreadPolicy: Failed to set scheduler class \"%s\" for " |
| 77 | "thread_id=%d: %s", |
| 78 | scheduler_class.c_str(), gettid(), strerror(-error)); |
| 79 | return false; |
| 80 | } |
| 81 | error = dvrSetCpuPartition(0, partition.c_str()); |
| 82 | if (error < 0) { |
| 83 | ALOGE( |
| 84 | "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: " |
| 85 | "%s", |
| 86 | partition.c_str(), gettid(), strerror(-error)); |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 92 | // Utility to generate scoped tracers with arguments. |
| 93 | // TODO(eieio): Move/merge this into utils/Trace.h? |
| 94 | class TraceArgs { |
| 95 | public: |
| 96 | template <typename... Args> |
| 97 | TraceArgs(const char* format, Args&&... args) { |
| 98 | std::array<char, 1024> buffer; |
| 99 | snprintf(buffer.data(), buffer.size(), format, std::forward<Args>(args)...); |
| 100 | atrace_begin(ATRACE_TAG, buffer.data()); |
| 101 | } |
| 102 | |
| 103 | ~TraceArgs() { atrace_end(ATRACE_TAG); } |
| 104 | |
| 105 | private: |
| 106 | TraceArgs(const TraceArgs&) = delete; |
| 107 | void operator=(const TraceArgs&) = delete; |
| 108 | }; |
| 109 | |
| 110 | // Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro |
| 111 | // defined in utils/Trace.h. |
| 112 | #define TRACE_FORMAT(format, ...) \ |
| 113 | TraceArgs PASTE(__tracer, __LINE__) { format, ##__VA_ARGS__ } |
| 114 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 115 | } // anonymous namespace |
| 116 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 117 | HardwareComposer::HardwareComposer() |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 118 | : initialized_(false), request_display_callback_(nullptr) {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 119 | |
| 120 | HardwareComposer::~HardwareComposer(void) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 121 | UpdatePostThreadState(PostThreadState::Quit, true); |
| 122 | if (post_thread_.joinable()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 123 | post_thread_.join(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 126 | bool HardwareComposer::Initialize( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 127 | Hwc2::Composer* composer, hwc2_display_t primary_display_id, |
| 128 | RequestDisplayCallback request_display_callback) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 129 | if (initialized_) { |
| 130 | ALOGE("HardwareComposer::Initialize: already initialized."); |
| 131 | return false; |
| 132 | } |
| 133 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 134 | is_standalone_device_ = property_get_bool(kDvrStandaloneProperty, false); |
| 135 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 136 | request_display_callback_ = request_display_callback; |
| 137 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 138 | HWC::Error error = HWC::Error::None; |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 139 | |
| 140 | Hwc2::Config config; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 141 | error = composer->getActiveConfig(primary_display_id, &config); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 142 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 143 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 144 | ALOGE("HardwareComposer: Failed to get current display config : %d", |
| 145 | config); |
| 146 | return false; |
| 147 | } |
| 148 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 149 | error = GetDisplayMetrics(composer, primary_display_id, config, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 150 | &native_display_metrics_); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 151 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 152 | if (error != HWC::Error::None) { |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 153 | ALOGE( |
| 154 | "HardwareComposer: Failed to get display attributes for current " |
| 155 | "configuration : %d", |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 156 | error.value); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | ALOGI( |
| 161 | "HardwareComposer: primary display attributes: width=%d height=%d " |
| 162 | "vsync_period_ns=%d DPI=%dx%d", |
| 163 | native_display_metrics_.width, native_display_metrics_.height, |
| 164 | native_display_metrics_.vsync_period_ns, native_display_metrics_.dpi.x, |
| 165 | native_display_metrics_.dpi.y); |
| 166 | |
| 167 | // Set the display metrics but never use rotation to avoid the long latency of |
| 168 | // rotation processing in hwc. |
| 169 | display_transform_ = HWC_TRANSFORM_NONE; |
| 170 | display_metrics_ = native_display_metrics_; |
| 171 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 172 | // Setup the display metrics used by all Layer instances. |
| 173 | Layer::SetDisplayMetrics(native_display_metrics_); |
| 174 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 175 | post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 176 | LOG_ALWAYS_FATAL_IF( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 177 | !post_thread_event_fd_, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 178 | "HardwareComposer: Failed to create interrupt event fd : %s", |
| 179 | strerror(errno)); |
| 180 | |
| 181 | post_thread_ = std::thread(&HardwareComposer::PostThread, this); |
| 182 | |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 183 | initialized_ = true; |
| 184 | |
| 185 | return initialized_; |
| 186 | } |
| 187 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 188 | void HardwareComposer::Enable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 189 | UpdatePostThreadState(PostThreadState::Suspended, false); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void HardwareComposer::Disable() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 193 | UpdatePostThreadState(PostThreadState::Suspended, true); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 194 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 195 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 196 | void HardwareComposer::OnBootFinished() { |
| 197 | std::lock_guard<std::mutex> lock(post_thread_mutex_); |
| 198 | if (boot_finished_) |
| 199 | return; |
| 200 | boot_finished_ = true; |
| 201 | post_thread_wait_.notify_one(); |
| 202 | if (is_standalone_device_) |
| 203 | request_display_callback_(true); |
| 204 | } |
| 205 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 206 | // Update the post thread quiescent state based on idle and suspended inputs. |
| 207 | void HardwareComposer::UpdatePostThreadState(PostThreadStateType state, |
| 208 | bool suspend) { |
| 209 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 210 | |
| 211 | // Update the votes in the state variable before evaluating the effective |
| 212 | // quiescent state. Any bits set in post_thread_state_ indicate that the post |
| 213 | // thread should be suspended. |
| 214 | if (suspend) { |
| 215 | post_thread_state_ |= state; |
| 216 | } else { |
| 217 | post_thread_state_ &= ~state; |
| 218 | } |
| 219 | |
| 220 | const bool quit = post_thread_state_ & PostThreadState::Quit; |
| 221 | const bool effective_suspend = post_thread_state_ != PostThreadState::Active; |
| 222 | if (quit) { |
| 223 | post_thread_quiescent_ = true; |
| 224 | eventfd_write(post_thread_event_fd_.Get(), 1); |
| 225 | post_thread_wait_.notify_one(); |
| 226 | } else if (effective_suspend && !post_thread_quiescent_) { |
| 227 | post_thread_quiescent_ = true; |
| 228 | eventfd_write(post_thread_event_fd_.Get(), 1); |
| 229 | } else if (!effective_suspend && post_thread_quiescent_) { |
| 230 | post_thread_quiescent_ = false; |
| 231 | eventfd_t value; |
| 232 | eventfd_read(post_thread_event_fd_.Get(), &value); |
| 233 | post_thread_wait_.notify_one(); |
| 234 | } |
| 235 | |
| 236 | // Wait until the post thread is in the requested state. |
| 237 | post_thread_ready_.wait(lock, [this, effective_suspend] { |
| 238 | return effective_suspend != post_thread_resumed_; |
| 239 | }); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 240 | } |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 241 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 242 | void HardwareComposer::OnPostThreadResumed() { |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 243 | // Phones create a new composer client on resume and destroy it on pause. |
| 244 | // Standalones only create the composer client once and then use SetPowerMode |
| 245 | // to control the screen on pause/resume. |
| 246 | if (!is_standalone_device_ || !composer_) { |
| 247 | composer_.reset(new Hwc2::Composer("default")); |
| 248 | composer_callback_ = new ComposerCallback; |
| 249 | composer_->registerCallback(composer_callback_); |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 250 | LOG_ALWAYS_FATAL_IF(!composer_callback_->HasDisplayId(), |
| 251 | "Registered composer callback but didn't get primary display"); |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 252 | Layer::SetComposer(composer_.get()); |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 253 | Layer::SetDisplayId(composer_callback_->GetDisplayId()); |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 254 | } else { |
| 255 | SetPowerMode(true); |
| 256 | } |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 257 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 258 | EnableVsync(true); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 259 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 260 | // TODO(skiazyk): We need to do something about accessing this directly, |
| 261 | // supposedly there is a backlight service on the way. |
| 262 | // TODO(steventhomas): When we change the backlight setting, will surface |
| 263 | // flinger (or something else) set it back to its original value once we give |
| 264 | // control of the display back to surface flinger? |
| 265 | SetBacklightBrightness(255); |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 266 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 267 | // Trigger target-specific performance mode change. |
| 268 | property_set(kDvrPerformanceProperty, "performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 271 | void HardwareComposer::OnPostThreadPaused() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 272 | retire_fence_fds_.clear(); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 273 | layers_.clear(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 274 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 275 | if (composer_) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 276 | EnableVsync(false); |
| 277 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 278 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 279 | if (!is_standalone_device_) { |
| 280 | composer_callback_ = nullptr; |
| 281 | composer_.reset(nullptr); |
| 282 | Layer::SetComposer(nullptr); |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 283 | Layer::SetDisplayId(0); |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 284 | } else { |
| 285 | SetPowerMode(false); |
| 286 | } |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 287 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 288 | // Trigger target-specific performance mode change. |
| 289 | property_set(kDvrPerformanceProperty, "idle"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 292 | bool HardwareComposer::PostThreadCondWait(std::unique_lock<std::mutex>& lock, |
| 293 | int timeout_sec, |
| 294 | const std::function<bool()>& pred) { |
| 295 | auto pred_with_quit = [&] { |
| 296 | return pred() || (post_thread_state_ & PostThreadState::Quit); |
| 297 | }; |
| 298 | if (timeout_sec >= 0) { |
| 299 | post_thread_wait_.wait_for(lock, std::chrono::seconds(timeout_sec), |
| 300 | pred_with_quit); |
| 301 | } else { |
| 302 | post_thread_wait_.wait(lock, pred_with_quit); |
| 303 | } |
| 304 | if (post_thread_state_ & PostThreadState::Quit) { |
| 305 | ALOGI("HardwareComposer::PostThread: Quitting."); |
| 306 | return true; |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 311 | HWC::Error HardwareComposer::Validate(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 312 | uint32_t num_types; |
| 313 | uint32_t num_requests; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 314 | HWC::Error error = |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 315 | composer_->validateDisplay(display, &num_types, &num_requests); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 316 | |
| 317 | if (error == HWC2_ERROR_HAS_CHANGES) { |
| 318 | // TODO(skiazyk): We might need to inspect the requested changes first, but |
| 319 | // so far it seems like we shouldn't ever hit a bad state. |
| 320 | // error = hwc2_funcs_.accept_display_changes_fn_(hardware_composer_device_, |
| 321 | // display); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 322 | error = composer_->acceptDisplayChanges(display); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return error; |
| 326 | } |
| 327 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 328 | HWC::Error HardwareComposer::EnableVsync(bool enabled) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 329 | return composer_->setVsyncEnabled( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 330 | composer_callback_->GetDisplayId(), |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 331 | (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE |
| 332 | : HWC2_VSYNC_DISABLE)); |
| 333 | } |
| 334 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 335 | HWC::Error HardwareComposer::SetPowerMode(bool active) { |
| 336 | HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off; |
| 337 | return composer_->setPowerMode( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 338 | composer_callback_->GetDisplayId(), |
| 339 | power_mode.cast<Hwc2::IComposerClient::PowerMode>()); |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 342 | HWC::Error HardwareComposer::Present(hwc2_display_t display) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 343 | int32_t present_fence; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 344 | HWC::Error error = composer_->presentDisplay(display, &present_fence); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 345 | |
| 346 | // According to the documentation, this fence is signaled at the time of |
| 347 | // vsync/DMA for physical displays. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 348 | if (error == HWC::Error::None) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 349 | ATRACE_INT("HardwareComposer: VsyncFence", present_fence); |
| 350 | retire_fence_fds_.emplace_back(present_fence); |
| 351 | } else { |
| 352 | ATRACE_INT("HardwareComposer: PresentResult", error); |
| 353 | } |
| 354 | |
| 355 | return error; |
| 356 | } |
| 357 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 358 | HWC::Error HardwareComposer::GetDisplayAttribute(Hwc2::Composer* composer, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 359 | hwc2_display_t display, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 360 | hwc2_config_t config, |
| 361 | hwc2_attribute_t attribute, |
| 362 | int32_t* out_value) const { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 363 | return composer->getDisplayAttribute( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 364 | display, config, (Hwc2::IComposerClient::Attribute)attribute, out_value); |
| 365 | } |
| 366 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 367 | HWC::Error HardwareComposer::GetDisplayMetrics( |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 368 | Hwc2::Composer* composer, hwc2_display_t display, hwc2_config_t config, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 369 | HWCDisplayMetrics* out_metrics) const { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 370 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 371 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 372 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_WIDTH, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 373 | &out_metrics->width); |
| 374 | if (error != HWC::Error::None) { |
| 375 | ALOGE( |
| 376 | "HardwareComposer::GetDisplayMetrics: Failed to get display width: %s", |
| 377 | error.to_string().c_str()); |
| 378 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 381 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_HEIGHT, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 382 | &out_metrics->height); |
| 383 | if (error != HWC::Error::None) { |
| 384 | ALOGE( |
| 385 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 386 | error.to_string().c_str()); |
| 387 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 390 | error = GetDisplayAttribute(composer, display, config, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 391 | HWC2_ATTRIBUTE_VSYNC_PERIOD, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 392 | &out_metrics->vsync_period_ns); |
| 393 | if (error != HWC::Error::None) { |
| 394 | ALOGE( |
| 395 | "HardwareComposer::GetDisplayMetrics: Failed to get display height: %s", |
| 396 | error.to_string().c_str()); |
| 397 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 400 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_X, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 401 | &out_metrics->dpi.x); |
| 402 | if (error != HWC::Error::None) { |
| 403 | ALOGE( |
| 404 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI X: %s", |
| 405 | error.to_string().c_str()); |
| 406 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 409 | error = GetDisplayAttribute(composer, display, config, HWC2_ATTRIBUTE_DPI_Y, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 410 | &out_metrics->dpi.y); |
| 411 | if (error != HWC::Error::None) { |
| 412 | ALOGE( |
| 413 | "HardwareComposer::GetDisplayMetrics: Failed to get display DPI Y: %s", |
| 414 | error.to_string().c_str()); |
| 415 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 418 | return HWC::Error::None; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 421 | std::string HardwareComposer::Dump() { |
| 422 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 423 | std::ostringstream stream; |
| 424 | |
| 425 | stream << "Display metrics: " << display_metrics_.width << "x" |
| 426 | << display_metrics_.height << " " << (display_metrics_.dpi.x / 1000.0) |
| 427 | << "x" << (display_metrics_.dpi.y / 1000.0) << " dpi @ " |
| 428 | << (1000000000.0 / display_metrics_.vsync_period_ns) << " Hz" |
| 429 | << std::endl; |
| 430 | |
| 431 | stream << "Post thread resumed: " << post_thread_resumed_ << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 432 | stream << "Active layers: " << layers_.size() << std::endl; |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 433 | stream << std::endl; |
| 434 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 435 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 436 | stream << "Layer " << i << ":"; |
| 437 | stream << " type=" << layers_[i].GetCompositionType().to_string(); |
| 438 | stream << " surface_id=" << layers_[i].GetSurfaceId(); |
| 439 | stream << " buffer_id=" << layers_[i].GetBufferId(); |
| 440 | stream << std::endl; |
| 441 | } |
| 442 | stream << std::endl; |
| 443 | |
| 444 | if (post_thread_resumed_) { |
| 445 | stream << "Hardware Composer Debug Info:" << std::endl; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 446 | stream << composer_->dumpDebugInfo(); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return stream.str(); |
| 450 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 451 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 452 | void HardwareComposer::PostLayers() { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 453 | ATRACE_NAME("HardwareComposer::PostLayers"); |
| 454 | |
| 455 | // Setup the hardware composer layers with current buffers. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 456 | for (auto& layer : layers_) { |
| 457 | layer.Prepare(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 460 | HWC::Error error = Validate(composer_callback_->GetDisplayId()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 461 | if (error != HWC::Error::None) { |
| 462 | ALOGE("HardwareComposer::PostLayers: Validate failed: %s", |
| 463 | error.to_string().c_str()); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 467 | // Now that we have taken in a frame from the application, we have a chance |
| 468 | // to drop the frame before passing the frame along to HWC. |
| 469 | // If the display driver has become backed up, we detect it here and then |
| 470 | // react by skipping this frame to catch up latency. |
| 471 | while (!retire_fence_fds_.empty() && |
| 472 | (!retire_fence_fds_.front() || |
| 473 | sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) { |
| 474 | // There are only 2 fences in here, no performance problem to shift the |
| 475 | // array of ints. |
| 476 | retire_fence_fds_.erase(retire_fence_fds_.begin()); |
| 477 | } |
| 478 | |
George Burgess IV | 353a6f6 | 2017-06-26 17:13:09 -0700 | [diff] [blame] | 479 | 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] | 480 | post_thread_config_.allowed_pending_fence_count; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 481 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 482 | if (is_fence_pending) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 483 | ATRACE_INT("frame_skip_count", ++frame_skip_count_); |
| 484 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 485 | ALOGW_IF(is_fence_pending, |
| 486 | "Warning: dropping a frame to catch up with HWC (pending = %zd)", |
| 487 | retire_fence_fds_.size()); |
| 488 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 489 | for (auto& layer : layers_) { |
| 490 | layer.Drop(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 491 | } |
| 492 | return; |
| 493 | } else { |
| 494 | // Make the transition more obvious in systrace when the frame skip happens |
| 495 | // above. |
| 496 | ATRACE_INT("frame_skip_count", 0); |
| 497 | } |
| 498 | |
Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 499 | #if TRACE > 1 |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 500 | for (size_t i = 0; i < layers_.size(); i++) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 501 | ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s", |
| 502 | i, layers_[i].GetBufferId(), |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 503 | layers_[i].GetCompositionType().to_string().c_str()); |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 504 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 505 | #endif |
| 506 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 507 | error = Present(composer_callback_->GetDisplayId()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 508 | if (error != HWC::Error::None) { |
| 509 | ALOGE("HardwareComposer::PostLayers: Present failed: %s", |
| 510 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 511 | return; |
| 512 | } |
| 513 | |
| 514 | std::vector<Hwc2::Layer> out_layers; |
| 515 | std::vector<int> out_fences; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 516 | error = composer_->getReleaseFences(composer_callback_->GetDisplayId(), |
| 517 | &out_layers, &out_fences); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 518 | ALOGE_IF(error != HWC::Error::None, |
| 519 | "HardwareComposer::PostLayers: Failed to get release fences: %s", |
| 520 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 521 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 522 | // Perform post-frame bookkeeping. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 523 | uint32_t num_elements = out_layers.size(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 524 | for (size_t i = 0; i < num_elements; ++i) { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 525 | for (auto& layer : layers_) { |
| 526 | if (layer.GetLayerHandle() == out_layers[i]) { |
| 527 | layer.Finish(out_fences[i]); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 533 | void HardwareComposer::SetDisplaySurfaces( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 534 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) { |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 535 | ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd", |
| 536 | surfaces.size()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 537 | const bool display_idle = surfaces.size() == 0; |
| 538 | { |
| 539 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 540 | pending_surfaces_ = std::move(surfaces); |
| 541 | } |
| 542 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 543 | if (request_display_callback_ && !is_standalone_device_) |
Steven Thomas | 2ddf567 | 2017-06-15 11:38:40 -0700 | [diff] [blame] | 544 | request_display_callback_(!display_idle); |
| 545 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 546 | // Set idle state based on whether there are any surfaces to handle. |
| 547 | UpdatePostThreadState(PostThreadState::Idle, display_idle); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 548 | } |
Jin Qian | 7480c06 | 2017-03-21 00:04:15 +0000 | [diff] [blame] | 549 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 550 | int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key, |
| 551 | IonBuffer& ion_buffer) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 552 | if (key == DvrGlobalBuffers::kVsyncBuffer) { |
| 553 | vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>( |
| 554 | &ion_buffer, CPUUsageMode::WRITE_OFTEN); |
| 555 | |
| 556 | if (vsync_ring_->IsMapped() == false) { |
| 557 | return -EPERM; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 562 | return MapConfigBuffer(ion_buffer); |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) { |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 569 | if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 570 | ConfigBufferDeleted(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) { |
| 575 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 576 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 577 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 578 | if (ion_buffer.width() < DvrConfigRing::MemorySize()) { |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 579 | ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size."); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
| 583 | void* buffer_base = 0; |
| 584 | int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(), |
| 585 | ion_buffer.height(), &buffer_base); |
| 586 | if (result != 0) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 587 | ALOGE( |
| 588 | "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config " |
| 589 | "buffer."); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 590 | return -EPERM; |
| 591 | } |
| 592 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 593 | shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width()); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 594 | ion_buffer.Unlock(); |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | void HardwareComposer::ConfigBufferDeleted() { |
| 600 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 601 | shared_config_ring_ = DvrConfigRing(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void HardwareComposer::UpdateConfigBuffer() { |
| 605 | std::lock_guard<std::mutex> lock(shared_config_mutex_); |
| 606 | if (!shared_config_ring_.is_valid()) |
| 607 | return; |
| 608 | // Copy from latest record in shared_config_ring_ to local copy. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 609 | DvrConfig record; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 610 | if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) { |
John Bates | cc65c3c | 2017-09-28 14:43:19 -0700 | [diff] [blame] | 611 | ALOGI("DvrConfig updated: sequence %u, post offset %d", |
| 612 | shared_config_ring_sequence_, record.frame_post_offset_ns); |
| 613 | ++shared_config_ring_sequence_; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 614 | post_thread_config_ = record; |
| 615 | } |
| 616 | } |
| 617 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 618 | int HardwareComposer::PostThreadPollInterruptible( |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 619 | const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 620 | pollfd pfd[2] = { |
| 621 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 622 | .fd = event_fd.Get(), |
Steven Thomas | 66747c1 | 2017-03-22 18:45:31 -0700 | [diff] [blame] | 623 | .events = static_cast<short>(requested_events), |
| 624 | .revents = 0, |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 625 | }, |
| 626 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 627 | .fd = post_thread_event_fd_.Get(), |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 628 | .events = POLLPRI | POLLIN, |
| 629 | .revents = 0, |
| 630 | }, |
| 631 | }; |
| 632 | int ret, error; |
| 633 | do { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 634 | ret = poll(pfd, 2, timeout_ms); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 635 | error = errno; |
| 636 | ALOGW_IF(ret < 0, |
| 637 | "HardwareComposer::PostThreadPollInterruptible: Error during " |
| 638 | "poll(): %s (%d)", |
| 639 | strerror(error), error); |
| 640 | } while (ret < 0 && error == EINTR); |
| 641 | |
| 642 | if (ret < 0) { |
| 643 | return -error; |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 644 | } else if (ret == 0) { |
| 645 | return -ETIMEDOUT; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 646 | } else if (pfd[0].revents != 0) { |
| 647 | return 0; |
| 648 | } else if (pfd[1].revents != 0) { |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 649 | ALOGI("VrHwcPost thread interrupted: revents=%x", pfd[1].revents); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 650 | return kPostThreadInterrupted; |
| 651 | } else { |
| 652 | return 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 653 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 656 | // Waits for the next vsync and returns the timestamp of the vsync event. If |
| 657 | // vsync already passed since the last call, returns the latest vsync timestamp |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 658 | // instead of blocking. |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 659 | Status<int64_t> HardwareComposer::WaitForVSync() { |
| 660 | const int64_t predicted_vsync_time = |
| 661 | last_vsync_timestamp_ + |
| 662 | display_metrics_.vsync_period_ns * vsync_prediction_interval_; |
| 663 | const int error = SleepUntil(predicted_vsync_time); |
| 664 | if (error < 0) { |
| 665 | ALOGE("HardwareComposer::WaifForVSync:: Failed to sleep: %s", |
| 666 | strerror(-error)); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 667 | return error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 668 | } |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 669 | return {predicted_vsync_time}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) { |
| 673 | const int timer_fd = vsync_sleep_timer_fd_.Get(); |
| 674 | const itimerspec wakeup_itimerspec = { |
| 675 | .it_interval = {.tv_sec = 0, .tv_nsec = 0}, |
| 676 | .it_value = NsToTimespec(wakeup_timestamp), |
| 677 | }; |
| 678 | int ret = |
| 679 | timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr); |
| 680 | int error = errno; |
| 681 | if (ret < 0) { |
| 682 | ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s", |
| 683 | strerror(error)); |
| 684 | return -error; |
| 685 | } |
| 686 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 687 | return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN, |
| 688 | /*timeout_ms*/ -1); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void HardwareComposer::PostThread() { |
| 692 | // NOLINTNEXTLINE(runtime/int) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 693 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 694 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 695 | // Set the scheduler to SCHED_FIFO with high priority. If this fails here |
| 696 | // there may have been a startup timing issue between this thread and |
| 697 | // performanced. Try again later when this thread becomes active. |
| 698 | bool thread_policy_setup = |
| 699 | SetThreadPolicy("graphics:high", "/system/performance"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 700 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 701 | #if ENABLE_BACKLIGHT_BRIGHTNESS |
| 702 | // TODO(hendrikw): This isn't required at the moment. It's possible that there |
| 703 | // is another method to access this when needed. |
| 704 | // Open the backlight brightness control sysfs node. |
| 705 | backlight_brightness_fd_ = LocalHandle(kBacklightBrightnessSysFile, O_RDWR); |
| 706 | ALOGW_IF(!backlight_brightness_fd_, |
| 707 | "HardwareComposer: Failed to open backlight brightness control: %s", |
| 708 | strerror(errno)); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 709 | #endif // ENABLE_BACKLIGHT_BRIGHTNESS |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 710 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 711 | // Create a timerfd based on CLOCK_MONOTINIC. |
| 712 | vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0)); |
| 713 | LOG_ALWAYS_FATAL_IF( |
| 714 | !vsync_sleep_timer_fd_, |
| 715 | "HardwareComposer: Failed to create vsync sleep timerfd: %s", |
| 716 | strerror(errno)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 717 | |
| 718 | const int64_t ns_per_frame = display_metrics_.vsync_period_ns; |
| 719 | const int64_t photon_offset_ns = GetPosePredictionTimeOffset(ns_per_frame); |
| 720 | |
| 721 | // TODO(jbates) Query vblank time from device, when such an API is available. |
| 722 | // This value (6.3%) was measured on A00 in low persistence mode. |
| 723 | int64_t vblank_ns = ns_per_frame * 63 / 1000; |
| 724 | int64_t right_eye_photon_offset_ns = (ns_per_frame - vblank_ns) / 2; |
| 725 | |
| 726 | // Check property for overriding right eye offset value. |
| 727 | right_eye_photon_offset_ns = |
| 728 | property_get_int64(kRightEyeOffsetProperty, right_eye_photon_offset_ns); |
| 729 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 730 | bool was_running = false; |
| 731 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 732 | if (is_standalone_device_) { |
| 733 | // First, wait until boot finishes. |
| 734 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 735 | if (PostThreadCondWait(lock, -1, [this] { return boot_finished_; })) { |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | // Then, wait until we're either leaving the quiescent state, or the boot |
| 740 | // finished display off timeout expires. |
| 741 | if (PostThreadCondWait(lock, kBootFinishedDisplayOffTimeoutSec, |
| 742 | [this] { return !post_thread_quiescent_; })) { |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | LOG_ALWAYS_FATAL_IF(post_thread_state_ & PostThreadState::Suspended, |
| 747 | "Vr flinger should own the display by now."); |
| 748 | post_thread_resumed_ = true; |
| 749 | post_thread_ready_.notify_all(); |
| 750 | OnPostThreadResumed(); |
| 751 | was_running = true; |
| 752 | } |
| 753 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 754 | while (1) { |
| 755 | ATRACE_NAME("HardwareComposer::PostThread"); |
| 756 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 757 | // Check for updated config once per vsync. |
| 758 | UpdateConfigBuffer(); |
| 759 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 760 | while (post_thread_quiescent_) { |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 761 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 762 | ALOGI("HardwareComposer::PostThread: Entering quiescent state."); |
| 763 | |
Corey Tabaka | df0b916 | 2017-08-03 17:14:08 -0700 | [diff] [blame] | 764 | // Tear down resources if necessary. |
| 765 | if (was_running) |
| 766 | OnPostThreadPaused(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 767 | |
| 768 | was_running = false; |
| 769 | post_thread_resumed_ = false; |
| 770 | post_thread_ready_.notify_all(); |
| 771 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 772 | if (PostThreadCondWait(lock, -1, |
| 773 | [this] { return !post_thread_quiescent_; })) { |
| 774 | // A true return value means we've been asked to quit. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 775 | return; |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 776 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 777 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 778 | post_thread_resumed_ = true; |
| 779 | post_thread_ready_.notify_all(); |
| 780 | |
| 781 | ALOGI("HardwareComposer::PostThread: Exiting quiescent state."); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | if (!was_running) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 785 | // Setup resources. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 786 | OnPostThreadResumed(); |
| 787 | was_running = true; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 788 | |
| 789 | // Try to setup the scheduler policy if it failed during startup. Only |
| 790 | // attempt to do this on transitions from inactive to active to avoid |
| 791 | // spamming the system with RPCs and log messages. |
| 792 | if (!thread_policy_setup) { |
| 793 | thread_policy_setup = |
| 794 | SetThreadPolicy("graphics:high", "/system/performance"); |
| 795 | } |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 796 | |
| 797 | // Initialize the last vsync timestamp with the current time. The |
| 798 | // predictor below uses this time + the vsync interval in absolute time |
| 799 | // units for the initial delay. Once the driver starts reporting vsync the |
| 800 | // predictor will sync up with the real vsync. |
| 801 | last_vsync_timestamp_ = GetSystemClockNs(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | int64_t vsync_timestamp = 0; |
| 805 | { |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 806 | TRACE_FORMAT("wait_vsync|vsync=%u;last_timestamp=%" PRId64 |
| 807 | ";prediction_interval=%d|", |
| 808 | vsync_count_ + 1, last_vsync_timestamp_, |
| 809 | vsync_prediction_interval_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 810 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 811 | auto status = WaitForVSync(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 812 | ALOGE_IF( |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 813 | !status, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 814 | "HardwareComposer::PostThread: Failed to wait for vsync event: %s", |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 815 | status.GetErrorMessage().c_str()); |
| 816 | |
| 817 | // If there was an error either sleeping was interrupted due to pausing or |
| 818 | // there was an error getting the latest timestamp. |
| 819 | if (!status) |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 820 | continue; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 821 | |
| 822 | // Predicted vsync timestamp for this interval. This is stable because we |
| 823 | // use absolute time for the wakeup timer. |
| 824 | vsync_timestamp = status.get(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 827 | // Advance the vsync counter only if the system is keeping up with hardware |
| 828 | // vsync to give clients an indication of the delays. |
| 829 | if (vsync_prediction_interval_ == 1) |
| 830 | ++vsync_count_; |
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 | const bool layer_config_changed = UpdateLayerConfig(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 833 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 834 | // Publish the vsync event. |
| 835 | if (vsync_ring_) { |
| 836 | DvrVsync vsync; |
| 837 | vsync.vsync_count = vsync_count_; |
| 838 | vsync.vsync_timestamp_ns = vsync_timestamp; |
| 839 | vsync.vsync_left_eye_offset_ns = photon_offset_ns; |
| 840 | vsync.vsync_right_eye_offset_ns = right_eye_photon_offset_ns; |
| 841 | vsync.vsync_period_ns = ns_per_frame; |
| 842 | |
| 843 | vsync_ring_->Publish(vsync); |
| 844 | } |
| 845 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 846 | // Signal all of the vsync clients. Because absolute time is used for the |
| 847 | // wakeup time below, this can take a little time if necessary. |
| 848 | if (vsync_callback_) |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 849 | vsync_callback_(vsync_timestamp, /*frame_time_estimate*/ 0, vsync_count_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 850 | |
| 851 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 852 | // Sleep until shortly before vsync. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 853 | ATRACE_NAME("sleep"); |
| 854 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 855 | const int64_t display_time_est_ns = vsync_timestamp + ns_per_frame; |
| 856 | const int64_t now_ns = GetSystemClockNs(); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 857 | const int64_t sleep_time_ns = display_time_est_ns - now_ns - |
| 858 | post_thread_config_.frame_post_offset_ns; |
| 859 | const int64_t wakeup_time_ns = |
| 860 | display_time_est_ns - post_thread_config_.frame_post_offset_ns; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 861 | |
| 862 | ATRACE_INT64("sleep_time_ns", sleep_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 863 | if (sleep_time_ns > 0) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 864 | int error = SleepUntil(wakeup_time_ns); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 865 | ALOGE_IF(error < 0, "HardwareComposer::PostThread: Failed to sleep: %s", |
| 866 | strerror(-error)); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 867 | if (error == kPostThreadInterrupted) { |
| 868 | if (layer_config_changed) { |
| 869 | // If the layer config changed we need to validateDisplay() even if |
| 870 | // we're going to drop the frame, to flush the Composer object's |
| 871 | // internal command buffer and apply our layer changes. |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 872 | Validate(composer_callback_->GetDisplayId()); |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 873 | } |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 874 | continue; |
Steven Thomas | 0af4b9f | 2017-04-26 14:34:01 -0700 | [diff] [blame] | 875 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 879 | { |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 880 | auto status = composer_callback_->GetVsyncTime(); |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 881 | if (!status) { |
| 882 | ALOGE("HardwareComposer::PostThread: Failed to get VSYNC time: %s", |
| 883 | status.GetErrorMessage().c_str()); |
| 884 | } |
| 885 | |
| 886 | // If we failed to read vsync there might be a problem with the driver. |
| 887 | // Since there's nothing we can do just behave as though we didn't get an |
| 888 | // updated vsync time and let the prediction continue. |
| 889 | const int64_t current_vsync_timestamp = |
| 890 | status ? status.get() : last_vsync_timestamp_; |
| 891 | |
| 892 | const bool vsync_delayed = |
| 893 | last_vsync_timestamp_ == current_vsync_timestamp; |
| 894 | ATRACE_INT("vsync_delayed", vsync_delayed); |
| 895 | |
| 896 | // If vsync was delayed advance the prediction interval and allow the |
| 897 | // fence logic in PostLayers() to skip the frame. |
| 898 | if (vsync_delayed) { |
| 899 | ALOGW( |
| 900 | "HardwareComposer::PostThread: VSYNC timestamp did not advance " |
| 901 | "since last frame: timestamp=%" PRId64 " prediction_interval=%d", |
| 902 | current_vsync_timestamp, vsync_prediction_interval_); |
| 903 | vsync_prediction_interval_++; |
| 904 | } else { |
| 905 | // We have an updated vsync timestamp, reset the prediction interval. |
| 906 | last_vsync_timestamp_ = current_vsync_timestamp; |
| 907 | vsync_prediction_interval_ = 1; |
| 908 | } |
| 909 | } |
| 910 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 911 | PostLayers(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 912 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 915 | // Checks for changes in the surface stack and updates the layer config to |
| 916 | // accomodate the new stack. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 917 | bool HardwareComposer::UpdateLayerConfig() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 918 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 919 | { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 920 | std::unique_lock<std::mutex> lock(post_thread_mutex_); |
| 921 | if (pending_surfaces_.empty()) |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 922 | return false; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 923 | |
| 924 | surfaces = std::move(pending_surfaces_); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 925 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 926 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 927 | ATRACE_NAME("UpdateLayerConfig_HwLayers"); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 928 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 929 | // Sort the new direct surface list by z-order to determine the relative order |
| 930 | // of the surfaces. This relative order is used for the HWC z-order value to |
| 931 | // insulate VrFlinger and HWC z-order semantics from each other. |
| 932 | std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) { |
| 933 | return a->z_order() < b->z_order(); |
| 934 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 935 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 936 | // Prepare a new layer stack, pulling in layers from the previous |
| 937 | // layer stack that are still active and updating their attributes. |
| 938 | std::vector<Layer> layers; |
| 939 | size_t layer_index = 0; |
| 940 | for (const auto& surface : surfaces) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 941 | // The bottom layer is opaque, other layers blend. |
| 942 | HWC::BlendMode blending = |
| 943 | layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 944 | |
| 945 | // Try to find a layer for this surface in the set of active layers. |
| 946 | auto search = |
| 947 | std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id()); |
| 948 | const bool found = search != layers_.end() && |
| 949 | search->GetSurfaceId() == surface->surface_id(); |
| 950 | if (found) { |
| 951 | // Update the attributes of the layer that may have changed. |
| 952 | search->SetBlending(blending); |
| 953 | search->SetZOrder(layer_index); // Relative z-order. |
| 954 | |
| 955 | // Move the existing layer to the new layer set and remove the empty layer |
| 956 | // object from the current set. |
| 957 | layers.push_back(std::move(*search)); |
| 958 | layers_.erase(search); |
| 959 | } else { |
| 960 | // Insert a layer for the new surface. |
| 961 | layers.emplace_back(surface, blending, display_transform_, |
| 962 | HWC::Composition::Device, layer_index); |
| 963 | } |
| 964 | |
| 965 | ALOGI_IF( |
| 966 | TRACE, |
| 967 | "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d", |
| 968 | layer_index, layers[layer_index].GetSurfaceId()); |
| 969 | |
| 970 | layer_index++; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 973 | // Sort the new layer stack by ascending surface id. |
| 974 | std::sort(layers.begin(), layers.end()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 975 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 976 | // Replace the previous layer set with the new layer set. The destructor of |
| 977 | // the previous set will clean up the remaining Layers that are not moved to |
| 978 | // the new layer set. |
| 979 | layers_ = std::move(layers); |
| 980 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 981 | ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers", |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 982 | layers_.size()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 983 | return true; |
| 984 | } |
| 985 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 986 | void HardwareComposer::SetVSyncCallback(VSyncCallback callback) { |
| 987 | vsync_callback_ = callback; |
| 988 | } |
| 989 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 990 | void HardwareComposer::SetBacklightBrightness(int brightness) { |
| 991 | if (backlight_brightness_fd_) { |
| 992 | std::array<char, 32> text; |
| 993 | const int length = snprintf(text.data(), text.size(), "%d", brightness); |
| 994 | write(backlight_brightness_fd_.Get(), text.data(), length); |
| 995 | } |
| 996 | } |
| 997 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 998 | Return<void> HardwareComposer::ComposerCallback::onHotplug( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 999 | Hwc2::Display display, IComposerCallback::Connection conn) { |
| 1000 | // Our first onHotplug callback is always for the primary display. |
| 1001 | // |
| 1002 | // Ignore any other hotplug callbacks since the primary display is never |
| 1003 | // disconnected and we don't care about other displays. |
| 1004 | if (!has_display_id_) { |
| 1005 | LOG_ALWAYS_FATAL_IF(conn != IComposerCallback::Connection::CONNECTED, |
| 1006 | "Initial onHotplug callback should be primary display connected"); |
| 1007 | has_display_id_ = true; |
| 1008 | display_id_ = display; |
| 1009 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 1010 | std::array<char, 1024> buffer; |
| 1011 | snprintf(buffer.data(), buffer.size(), |
| 1012 | "/sys/class/graphics/fb%" PRIu64 "/vsync_event", display); |
| 1013 | if (LocalHandle handle{buffer.data(), O_RDONLY}) { |
| 1014 | ALOGI( |
| 1015 | "HardwareComposer::ComposerCallback::onHotplug: Driver supports " |
| 1016 | "vsync_event node for display %" PRIu64, |
| 1017 | display); |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1018 | driver_vsync_event_fd_ = std::move(handle); |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 1019 | } else { |
| 1020 | ALOGI( |
| 1021 | "HardwareComposer::ComposerCallback::onHotplug: Driver does not " |
| 1022 | "support vsync_event node for display %" PRIu64, |
| 1023 | display); |
| 1024 | } |
| 1025 | } |
| 1026 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1027 | return Void(); |
| 1028 | } |
| 1029 | |
| 1030 | Return<void> HardwareComposer::ComposerCallback::onRefresh( |
| 1031 | Hwc2::Display /*display*/) { |
| 1032 | return hardware::Void(); |
| 1033 | } |
| 1034 | |
Corey Tabaka | 451256f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 1035 | Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display, |
| 1036 | int64_t timestamp) { |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1037 | // Ignore any onVsync callbacks for the non-primary display. |
| 1038 | if (has_display_id_ && display == display_id_) { |
| 1039 | TRACE_FORMAT("vsync_callback|display=%" PRIu64 ";timestamp=%" PRId64 "|", |
| 1040 | display, timestamp); |
| 1041 | callback_vsync_timestamp_ = timestamp; |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1042 | } |
| 1043 | return Void(); |
| 1044 | } |
| 1045 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1046 | Status<int64_t> HardwareComposer::ComposerCallback::GetVsyncTime() { |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 1047 | // See if the driver supports direct vsync events. |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1048 | LocalHandle& event_fd = driver_vsync_event_fd_; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 1049 | if (!event_fd) { |
| 1050 | // Fall back to returning the last timestamp returned by the vsync |
| 1051 | // callback. |
| 1052 | std::lock_guard<std::mutex> autolock(vsync_mutex_); |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1053 | return callback_vsync_timestamp_; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // When the driver supports the vsync_event sysfs node we can use it to |
| 1057 | // determine the latest vsync timestamp, even if the HWC callback has been |
| 1058 | // delayed. |
| 1059 | |
| 1060 | // The driver returns data in the form "VSYNC=<timestamp ns>". |
| 1061 | std::array<char, 32> data; |
| 1062 | data.fill('\0'); |
| 1063 | |
| 1064 | // Seek back to the beginning of the event file. |
| 1065 | int ret = lseek(event_fd.Get(), 0, SEEK_SET); |
| 1066 | if (ret < 0) { |
| 1067 | const int error = errno; |
| 1068 | ALOGE( |
| 1069 | "HardwareComposer::ComposerCallback::GetVsyncTime: Failed to seek " |
| 1070 | "vsync event fd: %s", |
| 1071 | strerror(error)); |
| 1072 | return ErrorStatus(error); |
| 1073 | } |
| 1074 | |
| 1075 | // Read the vsync event timestamp. |
| 1076 | ret = read(event_fd.Get(), data.data(), data.size()); |
| 1077 | if (ret < 0) { |
| 1078 | const int error = errno; |
| 1079 | ALOGE_IF(error != EAGAIN, |
| 1080 | "HardwareComposer::ComposerCallback::GetVsyncTime: Error " |
| 1081 | "while reading timestamp: %s", |
| 1082 | strerror(error)); |
| 1083 | return ErrorStatus(error); |
| 1084 | } |
| 1085 | |
| 1086 | int64_t timestamp; |
| 1087 | ret = sscanf(data.data(), "VSYNC=%" PRIu64, |
| 1088 | reinterpret_cast<uint64_t*>(×tamp)); |
| 1089 | if (ret < 0) { |
| 1090 | const int error = errno; |
| 1091 | ALOGE( |
| 1092 | "HardwareComposer::ComposerCallback::GetVsyncTime: Error while " |
| 1093 | "parsing timestamp: %s", |
| 1094 | strerror(error)); |
| 1095 | return ErrorStatus(error); |
| 1096 | } |
| 1097 | |
| 1098 | return {timestamp}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1101 | Hwc2::Composer* Layer::composer_{nullptr}; |
| 1102 | HWCDisplayMetrics Layer::display_metrics_{0, 0, {0, 0}, 0}; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1103 | hwc2_display_t Layer::display_id_{0}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1104 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1105 | void Layer::Reset() { |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1106 | if (hardware_composer_layer_) { |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1107 | composer_->destroyLayer(display_id_, hardware_composer_layer_); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1108 | hardware_composer_layer_ = 0; |
| 1109 | } |
| 1110 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1111 | z_order_ = 0; |
| 1112 | blending_ = HWC::BlendMode::None; |
| 1113 | transform_ = HWC::Transform::None; |
| 1114 | composition_type_ = HWC::Composition::Invalid; |
| 1115 | target_composition_type_ = composition_type_; |
| 1116 | source_ = EmptyVariant{}; |
| 1117 | acquire_fence_.Close(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1118 | surface_rect_functions_applied_ = false; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1119 | pending_visibility_settings_ = true; |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1120 | cached_buffer_map_.clear(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1123 | Layer::Layer(const std::shared_ptr<DirectDisplaySurface>& surface, |
| 1124 | HWC::BlendMode blending, HWC::Transform transform, |
| 1125 | HWC::Composition composition_type, size_t z_order) |
| 1126 | : z_order_{z_order}, |
| 1127 | blending_{blending}, |
| 1128 | transform_{transform}, |
| 1129 | target_composition_type_{composition_type}, |
| 1130 | source_{SourceSurface{surface}} { |
| 1131 | CommonLayerSetup(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1134 | Layer::Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending, |
| 1135 | HWC::Transform transform, HWC::Composition composition_type, |
| 1136 | size_t z_order) |
| 1137 | : z_order_{z_order}, |
| 1138 | blending_{blending}, |
| 1139 | transform_{transform}, |
| 1140 | target_composition_type_{composition_type}, |
| 1141 | source_{SourceBuffer{buffer}} { |
| 1142 | CommonLayerSetup(); |
| 1143 | } |
| 1144 | |
| 1145 | Layer::~Layer() { Reset(); } |
| 1146 | |
| 1147 | Layer::Layer(Layer&& other) { *this = std::move(other); } |
| 1148 | |
| 1149 | Layer& Layer::operator=(Layer&& other) { |
| 1150 | if (this != &other) { |
| 1151 | Reset(); |
| 1152 | using std::swap; |
| 1153 | swap(hardware_composer_layer_, other.hardware_composer_layer_); |
| 1154 | swap(z_order_, other.z_order_); |
| 1155 | swap(blending_, other.blending_); |
| 1156 | swap(transform_, other.transform_); |
| 1157 | swap(composition_type_, other.composition_type_); |
| 1158 | swap(target_composition_type_, other.target_composition_type_); |
| 1159 | swap(source_, other.source_); |
| 1160 | swap(acquire_fence_, other.acquire_fence_); |
| 1161 | swap(surface_rect_functions_applied_, |
| 1162 | other.surface_rect_functions_applied_); |
| 1163 | swap(pending_visibility_settings_, other.pending_visibility_settings_); |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1164 | swap(cached_buffer_map_, other.cached_buffer_map_); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1165 | } |
| 1166 | return *this; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1169 | void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) { |
| 1170 | if (source_.is<SourceBuffer>()) |
| 1171 | std::get<SourceBuffer>(source_) = {buffer}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1174 | void Layer::SetBlending(HWC::BlendMode blending) { |
| 1175 | if (blending_ != blending) { |
| 1176 | blending_ = blending; |
| 1177 | pending_visibility_settings_ = true; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | void Layer::SetZOrder(size_t z_order) { |
| 1182 | if (z_order_ != z_order) { |
| 1183 | z_order_ = z_order; |
| 1184 | pending_visibility_settings_ = true; |
| 1185 | } |
| 1186 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1187 | |
| 1188 | IonBuffer* Layer::GetBuffer() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1189 | struct Visitor { |
| 1190 | IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); } |
| 1191 | IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); } |
| 1192 | IonBuffer* operator()(EmptyVariant) { return nullptr; } |
| 1193 | }; |
| 1194 | return source_.Visit(Visitor{}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1197 | void Layer::UpdateVisibilitySettings() { |
| 1198 | if (pending_visibility_settings_) { |
| 1199 | pending_visibility_settings_ = false; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1200 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1201 | HWC::Error error; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1202 | |
| 1203 | error = composer_->setLayerBlendMode( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1204 | display_id_, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1205 | blending_.cast<Hwc2::IComposerClient::BlendMode>()); |
| 1206 | ALOGE_IF(error != HWC::Error::None, |
| 1207 | "Layer::UpdateLayerSettings: Error setting layer blend mode: %s", |
| 1208 | error.to_string().c_str()); |
| 1209 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1210 | error = composer_->setLayerZOrder(display_id_, hardware_composer_layer_, |
| 1211 | z_order_); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1212 | ALOGE_IF(error != HWC::Error::None, |
| 1213 | "Layer::UpdateLayerSettings: Error setting z_ order: %s", |
| 1214 | error.to_string().c_str()); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void Layer::UpdateLayerSettings() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1219 | HWC::Error error; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1220 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1221 | UpdateVisibilitySettings(); |
| 1222 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1223 | // TODO(eieio): Use surface attributes or some other mechanism to control |
| 1224 | // the layer display frame. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1225 | error = composer_->setLayerDisplayFrame( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1226 | display_id_, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1227 | {0, 0, display_metrics_.width, display_metrics_.height}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1228 | ALOGE_IF(error != HWC::Error::None, |
| 1229 | "Layer::UpdateLayerSettings: Error setting layer display frame: %s", |
| 1230 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1231 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1232 | error = composer_->setLayerVisibleRegion( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1233 | display_id_, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1234 | {{0, 0, display_metrics_.width, display_metrics_.height}}); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1235 | ALOGE_IF(error != HWC::Error::None, |
| 1236 | "Layer::UpdateLayerSettings: Error setting layer visible region: %s", |
| 1237 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1238 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1239 | error = composer_->setLayerPlaneAlpha(display_id_, hardware_composer_layer_, |
| 1240 | 1.0f); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1241 | ALOGE_IF(error != HWC::Error::None, |
| 1242 | "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s", |
| 1243 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1246 | void Layer::CommonLayerSetup() { |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1247 | HWC::Error error = composer_->createLayer(display_id_, |
| 1248 | &hardware_composer_layer_); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1249 | ALOGE_IF(error != HWC::Error::None, |
| 1250 | "Layer::CommonLayerSetup: Failed to create layer on primary " |
| 1251 | "display: %s", |
| 1252 | error.to_string().c_str()); |
| 1253 | UpdateLayerSettings(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1256 | bool Layer::CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id) { |
| 1257 | auto search = cached_buffer_map_.find(slot); |
| 1258 | if (search != cached_buffer_map_.end() && search->second == buffer_id) |
| 1259 | return true; |
| 1260 | |
| 1261 | // Assign or update the buffer slot. |
| 1262 | if (buffer_id >= 0) |
| 1263 | cached_buffer_map_[slot] = buffer_id; |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1267 | void Layer::Prepare() { |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1268 | int right, bottom, id; |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 1269 | sp<GraphicBuffer> handle; |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1270 | std::size_t slot; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1271 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1272 | // Acquire the next buffer according to the type of source. |
| 1273 | IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) { |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1274 | std::tie(right, bottom, id, handle, acquire_fence_, slot) = |
| 1275 | source.Acquire(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1276 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1277 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1278 | TRACE_FORMAT("Layer::Prepare|buffer_id=%d;slot=%zu|", id, slot); |
| 1279 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1280 | // Update any visibility (blending, z-order) changes that occurred since |
| 1281 | // last prepare. |
| 1282 | UpdateVisibilitySettings(); |
| 1283 | |
| 1284 | // When a layer is first setup there may be some time before the first |
| 1285 | // buffer arrives. Setup the HWC layer as a solid color to stall for time |
| 1286 | // until the first buffer arrives. Once the first buffer arrives there will |
| 1287 | // always be a buffer for the frame even if it is old. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1288 | if (!handle.get()) { |
| 1289 | if (composition_type_ == HWC::Composition::Invalid) { |
| 1290 | composition_type_ = HWC::Composition::SolidColor; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1291 | composer_->setLayerCompositionType( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1292 | display_id_, hardware_composer_layer_, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1293 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1294 | Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0}; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1295 | composer_->setLayerColor(display_id_, hardware_composer_layer_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1296 | layer_color); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1297 | } else { |
| 1298 | // The composition type is already set. Nothing else to do until a |
| 1299 | // buffer arrives. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1300 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1301 | } else { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1302 | if (composition_type_ != target_composition_type_) { |
| 1303 | composition_type_ = target_composition_type_; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1304 | composer_->setLayerCompositionType( |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1305 | display_id_, hardware_composer_layer_, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1306 | composition_type_.cast<Hwc2::IComposerClient::Composition>()); |
| 1307 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1308 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1309 | // See if the HWC cache already has this buffer. |
| 1310 | const bool cached = CheckAndUpdateCachedBuffer(slot, id); |
| 1311 | if (cached) |
| 1312 | handle = nullptr; |
| 1313 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1314 | HWC::Error error{HWC::Error::None}; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1315 | error = |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1316 | composer_->setLayerBuffer(display_id_, hardware_composer_layer_, |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 1317 | slot, handle, acquire_fence_.Get()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1318 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1319 | ALOGE_IF(error != HWC::Error::None, |
| 1320 | "Layer::Prepare: Error setting layer buffer: %s", |
| 1321 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1322 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1323 | if (!surface_rect_functions_applied_) { |
| 1324 | const float float_right = right; |
| 1325 | const float float_bottom = bottom; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 1326 | error = composer_->setLayerSourceCrop(display_id_, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 1327 | hardware_composer_layer_, |
| 1328 | {0, 0, float_right, float_bottom}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1329 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1330 | ALOGE_IF(error != HWC::Error::None, |
| 1331 | "Layer::Prepare: Error setting layer source crop: %s", |
| 1332 | error.to_string().c_str()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1333 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1334 | surface_rect_functions_applied_ = true; |
| 1335 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | void Layer::Finish(int release_fence_fd) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1340 | IfAnyOf<SourceSurface, SourceBuffer>::Call( |
| 1341 | &source_, [release_fence_fd](auto& source) { |
| 1342 | source.Finish(LocalHandle(release_fence_fd)); |
| 1343 | }); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 1346 | void Layer::Drop() { acquire_fence_.Close(); } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1347 | |
| 1348 | } // namespace dvr |
| 1349 | } // namespace android |