Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 17 | #define LOG_TAG "hwc-drm-two" |
| 18 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include "DrmHwcTwo.h" |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 20 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 21 | #include <cinttypes> |
| 22 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 23 | #include "backend/Backend.h" |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 24 | #include "utils/log.h" |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 25 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 28 | DrmHwcTwo::DrmHwcTwo() : resource_manager_(this){}; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 29 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 30 | /* Must be called after every display attach/detach cycle */ |
| 31 | void DrmHwcTwo::FinalizeDisplayBinding() { |
| 32 | if (displays_.count(kPrimaryDisplay) == 0) { |
| 33 | /* Create/update new headless display if no other displays exists |
| 34 | * or reattach different display to make it primary |
| 35 | */ |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 36 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 37 | if (display_handles_.empty()) { |
| 38 | /* Enable headless mode */ |
| 39 | ALOGI("No pipelines available. Creating null-display for headless mode"); |
| 40 | displays_[kPrimaryDisplay] = std::make_unique< |
| 41 | HwcDisplay>(nullptr, kPrimaryDisplay, HWC2::DisplayType::Physical, |
| 42 | this); |
| 43 | } else { |
| 44 | auto *pipe = display_handles_.begin()->first; |
| 45 | ALOGI("Primary display was disconnected, reattaching '%s' as new primary", |
| 46 | pipe->connector->Get()->GetName().c_str()); |
| 47 | UnbindDisplay(pipe); |
| 48 | BindDisplay(pipe); |
| 49 | if (displays_.count(kPrimaryDisplay) == 0) { |
| 50 | ALOGE("FIXME!!! Still no primary display after reattaching..."); |
| 51 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 55 | // Finally, send hotplug events to the client |
| 56 | for (auto &dhe : deferred_hotplug_events_) { |
| 57 | SendHotplugEventToClient(dhe.first, dhe.second); |
| 58 | } |
| 59 | deferred_hotplug_events_.clear(); |
| 60 | } |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame] | 61 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 62 | bool DrmHwcTwo::BindDisplay(DrmDisplayPipeline *pipeline) { |
| 63 | if (display_handles_.count(pipeline) != 0) { |
| 64 | ALOGE("%s, pipeline is already used by another display, FIXME!!!: %p", |
| 65 | __func__, pipeline); |
| 66 | return false; |
| 67 | } |
Roman Stratiienko | 1e053b4 | 2021-10-25 22:54:20 +0300 | [diff] [blame] | 68 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 69 | uint32_t disp_handle = kPrimaryDisplay; |
| 70 | |
| 71 | if (displays_.count(kPrimaryDisplay) != 0 && |
| 72 | !displays_[kPrimaryDisplay]->IsInHeadlessMode()) { |
| 73 | disp_handle = ++last_display_handle_; |
| 74 | } |
| 75 | |
| 76 | auto disp = std::make_unique<HwcDisplay>(pipeline, disp_handle, |
| 77 | HWC2::DisplayType::Physical, this); |
| 78 | |
| 79 | if (disp_handle == kPrimaryDisplay) { |
| 80 | displays_.erase(disp_handle); |
| 81 | } |
| 82 | |
| 83 | ALOGI("Attaching pipeline '%s' to the display #%d%s", |
| 84 | pipeline->connector->Get()->GetName().c_str(), (int)disp_handle, |
| 85 | disp_handle == kPrimaryDisplay ? " (Primary)" : ""); |
| 86 | |
| 87 | displays_[disp_handle] = std::move(disp); |
| 88 | display_handles_[pipeline] = disp_handle; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool DrmHwcTwo::UnbindDisplay(DrmDisplayPipeline *pipeline) { |
| 94 | if (display_handles_.count(pipeline) == 0) { |
| 95 | ALOGE("%s, can't find the display, pipeline: %p", __func__, pipeline); |
| 96 | return false; |
| 97 | } |
| 98 | auto handle = display_handles_[pipeline]; |
| 99 | |
| 100 | ALOGI("Detaching the pipeline '%s' from the display #%i%s", |
| 101 | pipeline->connector->Get()->GetName().c_str(), (int)handle, |
| 102 | handle == kPrimaryDisplay ? " (Primary)" : ""); |
| 103 | |
| 104 | display_handles_.erase(pipeline); |
| 105 | if (displays_.count(handle) == 0) { |
| 106 | ALOGE("%s, can't find the display, handle: %" PRIu64, __func__, handle); |
| 107 | return false; |
| 108 | } |
| 109 | displays_.erase(handle); |
| 110 | return true; |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Roman Stratiienko | 7f576ec | 2021-12-22 17:34:18 +0200 | [diff] [blame] | 113 | HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t /*width*/, |
| 114 | uint32_t /*height*/, |
| 115 | int32_t * /*format*/, |
| 116 | hwc2_display_t * /*display*/) { |
| 117 | // TODO(nobody): Implement virtual display |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 118 | return HWC2::Error::Unsupported; |
| 119 | } |
| 120 | |
Roman Stratiienko | 7f576ec | 2021-12-22 17:34:18 +0200 | [diff] [blame] | 121 | HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t /*display*/) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 122 | // TODO(nobody): Implement virtual display |
Roman Stratiienko | 7f576ec | 2021-12-22 17:34:18 +0200 | [diff] [blame] | 123 | return HWC2::Error::Unsupported; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 126 | void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) { |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 127 | if (outBuffer != nullptr) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 128 | auto copied_bytes = mDumpString.copy(outBuffer, *outSize); |
| 129 | *outSize = static_cast<uint32_t>(copied_bytes); |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | std::stringstream output; |
| 134 | |
| 135 | output << "-- drm_hwcomposer --\n\n"; |
| 136 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 137 | for (auto &disp : displays_) |
| 138 | output << disp.second->Dump(); |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 139 | |
| 140 | mDumpString = output.str(); |
| 141 | *outSize = static_cast<uint32_t>(mDumpString.size()); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 145 | // TODO(nobody): Implement virtual display |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 150 | hwc2_callback_data_t data, |
| 151 | hwc2_function_pointer_t function) { |
Roman Stratiienko | 2370109 | 2020-09-26 02:08:41 +0300 | [diff] [blame] | 152 | switch (static_cast<HWC2::Callback>(descriptor)) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 153 | case HWC2::Callback::Hotplug: { |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame] | 154 | hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data); |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 155 | if (function != nullptr) { |
| 156 | resource_manager_.Init(); |
| 157 | } else { |
| 158 | resource_manager_.DeInit(); |
| 159 | /* Headless display may still be here, remove it */ |
| 160 | displays_.erase(kPrimaryDisplay); |
| 161 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 162 | break; |
| 163 | } |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 164 | case HWC2::Callback::Refresh: { |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame] | 165 | refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 166 | break; |
| 167 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 168 | case HWC2::Callback::Vsync: { |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame] | 169 | vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 170 | break; |
| 171 | } |
Roman Stratiienko | 11ef8c5 | 2021-09-29 13:01:39 +0300 | [diff] [blame] | 172 | #if PLATFORM_SDK_VERSION > 29 |
| 173 | case HWC2::Callback::Vsync_2_4: { |
| 174 | vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data); |
| 175 | break; |
| 176 | } |
| 177 | #endif |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | return HWC2::Error::None; |
| 182 | } |
| 183 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 184 | void DrmHwcTwo::SendHotplugEventToClient(hwc2_display_t displayid, |
| 185 | bool connected) { |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame] | 186 | auto &mutex = GetResMan().GetMainLock(); |
| 187 | if (mutex.try_lock()) { |
| 188 | ALOGE("FIXME!!!: Main mutex must be locked in %s", __func__); |
| 189 | mutex.unlock(); |
| 190 | return; |
| 191 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 192 | |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame] | 193 | auto hc = hotplug_callback_; |
| 194 | if (hc.first != nullptr && hc.second != nullptr) { |
| 195 | /* For some reason CLIENT will call HWC2 API in hotplug callback handler, |
| 196 | * which will cause deadlock . Unlock main mutex to prevent this. |
| 197 | */ |
| 198 | mutex.unlock(); |
| 199 | hc.first(hc.second, displayid, |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame^] | 200 | connected == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED |
| 201 | : HWC2_CONNECTION_DISCONNECTED); |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame] | 202 | mutex.lock(); |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame] | 203 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 204 | } |
| 205 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 206 | } // namespace android |