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