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