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 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | #define LOG_TAG "hwc-drm-two" |
| 19 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 20 | #include "drmhwctwo.h" |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 21 | #include "drmdisplaycomposition.h" |
| 22 | #include "drmhwcomposer.h" |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 23 | #include "platform.h" |
| 24 | #include "vsyncworker.h" |
| 25 | |
| 26 | #include <inttypes.h> |
| 27 | #include <string> |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 28 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | #include <hardware/hardware.h> |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 31 | #include <hardware/hwcomposer2.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 32 | #include <log/log.h> |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 36 | class DrmVsyncCallback : public VsyncCallback { |
| 37 | public: |
| 38 | DrmVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook) |
| 39 | : data_(data), hook_(hook) { |
| 40 | } |
| 41 | |
| 42 | void Callback(int display, int64_t timestamp) { |
| 43 | auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_); |
| 44 | hook(data_, display, timestamp); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | hwc2_callback_data_t data_; |
| 49 | hwc2_function_pointer_t hook_; |
| 50 | }; |
| 51 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 52 | DrmHwcTwo::DrmHwcTwo() { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 53 | common.tag = HARDWARE_DEVICE_TAG; |
| 54 | common.version = HWC_DEVICE_API_VERSION_2_0; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 55 | common.close = HookDevClose; |
| 56 | getCapabilities = HookDevGetCapabilities; |
| 57 | getFunction = HookDevGetFunction; |
| 58 | } |
| 59 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 60 | HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ, |
| 61 | HWC2::DisplayType type) { |
| 62 | DrmDevice *drm = resource_manager_.GetDrmDevice(displ); |
| 63 | std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 64 | if (!drm || !importer) { |
| 65 | ALOGE("Failed to get a valid drmresource and importer"); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 66 | return HWC2::Error::NoResources; |
| 67 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 68 | displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ), |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 69 | std::forward_as_tuple(&resource_manager_, drm, importer, |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 70 | displ, type)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 71 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 72 | DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 73 | if (!crtc) { |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 74 | ALOGE("Failed to get crtc for display %d", static_cast<int>(displ)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 75 | return HWC2::Error::BadDisplay; |
| 76 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 77 | std::vector<DrmPlane *> display_planes; |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 78 | for (auto &plane : drm->planes()) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 79 | if (plane->GetCrtcSupported(*crtc)) |
| 80 | display_planes.push_back(plane.get()); |
| 81 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 82 | displays_.at(displ).Init(&display_planes); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 83 | return HWC2::Error::None; |
| 84 | } |
| 85 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 86 | HWC2::Error DrmHwcTwo::Init() { |
| 87 | int rv = resource_manager_.Init(); |
| 88 | if (rv) { |
| 89 | ALOGE("Can't initialize the resource manager %d", rv); |
| 90 | return HWC2::Error::NoResources; |
| 91 | } |
| 92 | |
| 93 | HWC2::Error ret = HWC2::Error::None; |
| 94 | for (int i = 0; i < resource_manager_.getDisplayCount(); i++) { |
| 95 | ret = CreateDisplay(i, HWC2::DisplayType::Physical); |
| 96 | if (ret != HWC2::Error::None) { |
| 97 | ALOGE("Failed to create display %d with error %d", i, ret); |
| 98 | return ret; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | auto &drmDevices = resource_manager_.getDrmDevices(); |
| 103 | for (auto &device : drmDevices) { |
| 104 | device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get())); |
| 105 | } |
| 106 | return ret; |
| 107 | } |
| 108 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 109 | template <typename... Args> |
| 110 | static inline HWC2::Error unsupported(char const *func, Args... /*args*/) { |
| 111 | ALOGV("Unsupported function: %s", func); |
| 112 | return HWC2::Error::Unsupported; |
| 113 | } |
| 114 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 115 | static inline void supported(char const *func) { |
| 116 | ALOGV("Supported function: %s", func); |
| 117 | } |
| 118 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 119 | HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height, |
| 120 | int32_t *format, |
| 121 | hwc2_display_t *display) { |
| 122 | // TODO: Implement virtual display |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 123 | return unsupported(__func__, width, height, format, display); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 127 | // TODO: Implement virtual display |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 128 | return unsupported(__func__, display); |
| 129 | } |
| 130 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 131 | std::string DrmHwcTwo::HwcDisplay::DumpDelta( |
| 132 | DrmHwcTwo::HwcDisplay::Stats delta) { |
| 133 | if (delta.total_pixops_ == 0) |
| 134 | return "No stats yet"; |
| 135 | double Ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_); |
| 136 | |
| 137 | return (std::stringstream() |
| 138 | << " Total frames count: " << delta.total_frames_ << "\n" |
| 139 | << " Failed to test commit frames: " << delta.failed_kms_validate_ |
| 140 | << "\n" |
| 141 | << " Failed to commit frames: " << delta.failed_kms_present_ << "\n" |
| 142 | << ((delta.failed_kms_present_ > 0) |
| 143 | ? " !!! Internal failure, FIX it please\n" |
| 144 | : "") |
| 145 | << " Pixel operations (free units)" |
| 146 | << " : [TOTAL: " << delta.total_pixops_ |
| 147 | << " / GPU: " << delta.gpu_pixops_ << "]\n" |
| 148 | << " Composition efficiency: " << Ratio) |
| 149 | .str(); |
| 150 | } |
| 151 | |
| 152 | std::string DrmHwcTwo::HwcDisplay::Dump() { |
| 153 | auto out = (std::stringstream() |
| 154 | << "- Display on: " << connector_->name() << "\n" |
| 155 | << "Statistics since system boot:\n" |
| 156 | << DumpDelta(total_stats_) << "\n\n" |
| 157 | << "Statistics since last dumpsys request:\n" |
| 158 | << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n") |
| 159 | .str(); |
| 160 | |
| 161 | memcpy(&prev_stats_, &total_stats_, sizeof(Stats)); |
| 162 | return out; |
| 163 | } |
| 164 | |
| 165 | void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) { |
| 166 | supported(__func__); |
| 167 | |
| 168 | if (outBuffer != nullptr) { |
| 169 | auto copiedBytes = mDumpString.copy(outBuffer, *outSize); |
| 170 | *outSize = static_cast<uint32_t>(copiedBytes); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | std::stringstream output; |
| 175 | |
| 176 | output << "-- drm_hwcomposer --\n\n"; |
| 177 | |
| 178 | for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_) |
| 179 | output << dp.second.Dump(); |
| 180 | |
| 181 | mDumpString = output.str(); |
| 182 | *outSize = static_cast<uint32_t>(mDumpString.size()); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 186 | // TODO: Implement virtual display |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 187 | unsupported(__func__); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 192 | hwc2_callback_data_t data, |
| 193 | hwc2_function_pointer_t function) { |
| 194 | supported(__func__); |
| 195 | auto callback = static_cast<HWC2::Callback>(descriptor); |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 196 | |
| 197 | if (!function) { |
| 198 | callbacks_.erase(callback); |
| 199 | return HWC2::Error::None; |
| 200 | } |
| 201 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 202 | callbacks_.emplace(callback, HwcCallback(data, function)); |
| 203 | |
| 204 | switch (callback) { |
| 205 | case HWC2::Callback::Hotplug: { |
| 206 | auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(function); |
| 207 | hotplug(data, HWC_DISPLAY_PRIMARY, |
| 208 | static_cast<int32_t>(HWC2::Connection::Connected)); |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 209 | auto &drmDevices = resource_manager_.getDrmDevices(); |
| 210 | for (auto &device : drmDevices) |
| 211 | HandleInitialHotplugState(device.get()); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | case HWC2::Callback::Vsync: { |
| 215 | for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d : |
| 216 | displays_) |
| 217 | d.second.RegisterVsyncCallback(data, function); |
| 218 | break; |
| 219 | } |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | return HWC2::Error::None; |
| 224 | } |
| 225 | |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 226 | DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager, |
| 227 | DrmDevice *drm, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 228 | std::shared_ptr<Importer> importer, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 229 | hwc2_display_t handle, HWC2::DisplayType type) |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 230 | : resource_manager_(resource_manager), |
| 231 | drm_(drm), |
| 232 | importer_(importer), |
| 233 | handle_(handle), |
| 234 | type_(type) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 235 | supported(__func__); |
| 236 | } |
| 237 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 238 | void DrmHwcTwo::HwcDisplay::ClearDisplay() { |
| 239 | compositor_.ClearDisplay(); |
| 240 | } |
| 241 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 242 | HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) { |
| 243 | supported(__func__); |
| 244 | planner_ = Planner::CreateInstance(drm_); |
| 245 | if (!planner_) { |
| 246 | ALOGE("Failed to create planner instance for composition"); |
| 247 | return HWC2::Error::NoResources; |
| 248 | } |
| 249 | |
| 250 | int display = static_cast<int>(handle_); |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 251 | int ret = compositor_.Init(resource_manager_, display); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 252 | if (ret) { |
| 253 | ALOGE("Failed display compositor init for display %d (%d)", display, ret); |
| 254 | return HWC2::Error::NoResources; |
| 255 | } |
| 256 | |
| 257 | // Split up the given display planes into primary and overlay to properly |
| 258 | // interface with the composition |
| 259 | char use_overlay_planes_prop[PROPERTY_VALUE_MAX]; |
| 260 | property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1"); |
| 261 | bool use_overlay_planes = atoi(use_overlay_planes_prop); |
| 262 | for (auto &plane : *planes) { |
| 263 | if (plane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 264 | primary_planes_.push_back(plane); |
| 265 | else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY) |
| 266 | overlay_planes_.push_back(plane); |
| 267 | } |
| 268 | |
| 269 | crtc_ = drm_->GetCrtcForDisplay(display); |
| 270 | if (!crtc_) { |
| 271 | ALOGE("Failed to get crtc for display %d", display); |
| 272 | return HWC2::Error::BadDisplay; |
| 273 | } |
| 274 | |
| 275 | connector_ = drm_->GetConnectorForDisplay(display); |
| 276 | if (!connector_) { |
| 277 | ALOGE("Failed to get connector for display %d", display); |
| 278 | return HWC2::Error::BadDisplay; |
| 279 | } |
| 280 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 281 | ret = vsync_worker_.Init(drm_, display); |
| 282 | if (ret) { |
| 283 | ALOGE("Failed to create event worker for d=%d %d\n", display, ret); |
| 284 | return HWC2::Error::BadDisplay; |
| 285 | } |
| 286 | |
| 287 | return ChosePreferredConfig(); |
| 288 | } |
| 289 | |
| 290 | HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 291 | // Fetch the number of modes from the display |
| 292 | uint32_t num_configs; |
| 293 | HWC2::Error err = GetDisplayConfigs(&num_configs, NULL); |
| 294 | if (err != HWC2::Error::None || !num_configs) |
| 295 | return err; |
| 296 | |
Andrii Chepurnyi | 1b1e35e | 2019-02-19 21:38:13 +0200 | [diff] [blame] | 297 | return SetActiveConfig(connector_->get_preferred_mode_id()); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | HWC2::Error DrmHwcTwo::HwcDisplay::RegisterVsyncCallback( |
| 301 | hwc2_callback_data_t data, hwc2_function_pointer_t func) { |
| 302 | supported(__func__); |
| 303 | auto callback = std::make_shared<DrmVsyncCallback>(data, func); |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 304 | vsync_worker_.RegisterCallback(std::move(callback)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 305 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 309 | supported(__func__); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 310 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) |
| 311 | l.second.accept_type_change(); |
| 312 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 316 | supported(__func__); |
| 317 | layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer()); |
| 318 | *layer = static_cast<hwc2_layer_t>(layer_idx_); |
| 319 | ++layer_idx_; |
| 320 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 324 | supported(__func__); |
Vincent Donnefort | 9abec03 | 2019-10-09 15:43:43 +0100 | [diff] [blame] | 325 | if (!get_layer(layer)) |
| 326 | return HWC2::Error::BadLayer; |
| 327 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 328 | layers_.erase(layer); |
| 329 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 333 | supported(__func__); |
| 334 | DrmMode const &mode = connector_->active_mode(); |
| 335 | if (mode.id() == 0) |
| 336 | return HWC2::Error::BadConfig; |
| 337 | |
| 338 | *config = mode.id(); |
| 339 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes( |
| 343 | uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 344 | supported(__func__); |
| 345 | uint32_t num_changes = 0; |
| 346 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) { |
| 347 | if (l.second.type_changed()) { |
| 348 | if (layers && num_changes < *num_elements) |
| 349 | layers[num_changes] = l.first; |
| 350 | if (types && num_changes < *num_elements) |
| 351 | types[num_changes] = static_cast<int32_t>(l.second.validated_type()); |
| 352 | ++num_changes; |
| 353 | } |
| 354 | } |
| 355 | if (!layers && !types) |
| 356 | *num_elements = num_changes; |
| 357 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 361 | uint32_t height, |
| 362 | int32_t /*format*/, |
| 363 | int32_t dataspace) { |
| 364 | supported(__func__); |
| 365 | std::pair<uint32_t, uint32_t> min = drm_->min_resolution(); |
| 366 | std::pair<uint32_t, uint32_t> max = drm_->max_resolution(); |
| 367 | |
| 368 | if (width < min.first || height < min.second) |
| 369 | return HWC2::Error::Unsupported; |
| 370 | |
| 371 | if (width > max.first || height > max.second) |
| 372 | return HWC2::Error::Unsupported; |
| 373 | |
| 374 | if (dataspace != HAL_DATASPACE_UNKNOWN && |
| 375 | dataspace != HAL_DATASPACE_STANDARD_UNSPECIFIED) |
| 376 | return HWC2::Error::Unsupported; |
| 377 | |
| 378 | // TODO: Validate format can be handled by either GL or planes |
| 379 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 383 | int32_t *modes) { |
| 384 | supported(__func__); |
Kalyan Kondapally | da5839c | 2016-11-10 10:59:50 -0800 | [diff] [blame] | 385 | if (!modes) |
| 386 | *num_modes = 1; |
| 387 | |
| 388 | if (modes) |
| 389 | *modes = HAL_COLOR_MODE_NATIVE; |
| 390 | |
| 391 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 395 | int32_t attribute_in, |
| 396 | int32_t *value) { |
| 397 | supported(__func__); |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 398 | auto mode = std::find_if(connector_->modes().begin(), |
| 399 | connector_->modes().end(), |
| 400 | [config](DrmMode const &m) { |
| 401 | return m.id() == config; |
| 402 | }); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 403 | if (mode == connector_->modes().end()) { |
| 404 | ALOGE("Could not find active mode for %d", config); |
| 405 | return HWC2::Error::BadConfig; |
| 406 | } |
| 407 | |
| 408 | static const int32_t kUmPerInch = 25400; |
| 409 | uint32_t mm_width = connector_->mm_width(); |
| 410 | uint32_t mm_height = connector_->mm_height(); |
| 411 | auto attribute = static_cast<HWC2::Attribute>(attribute_in); |
| 412 | switch (attribute) { |
| 413 | case HWC2::Attribute::Width: |
| 414 | *value = mode->h_display(); |
| 415 | break; |
| 416 | case HWC2::Attribute::Height: |
| 417 | *value = mode->v_display(); |
| 418 | break; |
| 419 | case HWC2::Attribute::VsyncPeriod: |
| 420 | // in nanoseconds |
| 421 | *value = 1000 * 1000 * 1000 / mode->v_refresh(); |
| 422 | break; |
| 423 | case HWC2::Attribute::DpiX: |
| 424 | // Dots per 1000 inches |
| 425 | *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1; |
| 426 | break; |
| 427 | case HWC2::Attribute::DpiY: |
| 428 | // Dots per 1000 inches |
| 429 | *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1; |
| 430 | break; |
| 431 | default: |
| 432 | *value = -1; |
| 433 | return HWC2::Error::BadConfig; |
| 434 | } |
| 435 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs, |
| 439 | hwc2_config_t *configs) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 440 | supported(__func__); |
| 441 | // Since this callback is normally invoked twice (once to get the count, and |
| 442 | // once to populate configs), we don't really want to read the edid |
| 443 | // redundantly. Instead, only update the modes on the first invocation. While |
| 444 | // it's possible this will result in stale modes, it'll all come out in the |
| 445 | // wash when we try to set the active config later. |
| 446 | if (!configs) { |
| 447 | int ret = connector_->UpdateModes(); |
| 448 | if (ret) { |
| 449 | ALOGE("Failed to update display modes %d", ret); |
| 450 | return HWC2::Error::BadDisplay; |
| 451 | } |
| 452 | } |
| 453 | |
Neil Armstrong | b67d049 | 2019-06-20 09:00:21 +0000 | [diff] [blame] | 454 | // Since the upper layers only look at vactive/hactive/refresh, height and |
| 455 | // width, it doesn't differentiate interlaced from progressive and other |
| 456 | // similar modes. Depending on the order of modes we return to SF, it could |
| 457 | // end up choosing a suboptimal configuration and dropping the preferred |
| 458 | // mode. To workaround this, don't offer interlaced modes to SF if there is |
| 459 | // at least one non-interlaced alternative and only offer a single WxH@R |
| 460 | // mode with at least the prefered mode from in DrmConnector::UpdateModes() |
| 461 | |
| 462 | // TODO: Remove the following block of code until AOSP handles all modes |
| 463 | std::vector<DrmMode> sel_modes; |
| 464 | |
| 465 | // Add the preferred mode first to be sure it's not dropped |
| 466 | auto mode = std::find_if(connector_->modes().begin(), |
| 467 | connector_->modes().end(), [&](DrmMode const &m) { |
| 468 | return m.id() == |
| 469 | connector_->get_preferred_mode_id(); |
| 470 | }); |
| 471 | if (mode != connector_->modes().end()) |
| 472 | sel_modes.push_back(*mode); |
| 473 | |
| 474 | // Add the active mode if different from preferred mode |
| 475 | if (connector_->active_mode().id() != connector_->get_preferred_mode_id()) |
| 476 | sel_modes.push_back(connector_->active_mode()); |
| 477 | |
| 478 | // Cycle over the modes and filter out "similar" modes, keeping only the |
| 479 | // first ones in the order given by DRM (from CEA ids and timings order) |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 480 | for (const DrmMode &mode : connector_->modes()) { |
Neil Armstrong | b67d049 | 2019-06-20 09:00:21 +0000 | [diff] [blame] | 481 | // TODO: Remove this when 3D Attributes are in AOSP |
| 482 | if (mode.flags() & DRM_MODE_FLAG_3D_MASK) |
| 483 | continue; |
| 484 | |
Neil Armstrong | 4c027a7 | 2019-06-04 14:48:02 +0000 | [diff] [blame] | 485 | // TODO: Remove this when the Interlaced attribute is in AOSP |
| 486 | if (mode.flags() & DRM_MODE_FLAG_INTERLACE) { |
| 487 | auto m = std::find_if(connector_->modes().begin(), |
| 488 | connector_->modes().end(), |
| 489 | [&mode](DrmMode const &m) { |
| 490 | return !(m.flags() & DRM_MODE_FLAG_INTERLACE) && |
| 491 | m.h_display() == mode.h_display() && |
| 492 | m.v_display() == mode.v_display(); |
| 493 | }); |
Neil Armstrong | b67d049 | 2019-06-20 09:00:21 +0000 | [diff] [blame] | 494 | if (m == connector_->modes().end()) |
| 495 | sel_modes.push_back(mode); |
| 496 | |
| 497 | continue; |
Neil Armstrong | 4c027a7 | 2019-06-04 14:48:02 +0000 | [diff] [blame] | 498 | } |
Neil Armstrong | b67d049 | 2019-06-20 09:00:21 +0000 | [diff] [blame] | 499 | |
| 500 | // Search for a similar WxH@R mode in the filtered list and drop it if |
| 501 | // another mode with the same WxH@R has already been selected |
| 502 | // TODO: Remove this when AOSP handles duplicates modes |
| 503 | auto m = std::find_if(sel_modes.begin(), sel_modes.end(), |
| 504 | [&mode](DrmMode const &m) { |
| 505 | return m.h_display() == mode.h_display() && |
| 506 | m.v_display() == mode.v_display() && |
| 507 | m.v_refresh() == mode.v_refresh(); |
| 508 | }); |
| 509 | if (m == sel_modes.end()) |
| 510 | sel_modes.push_back(mode); |
| 511 | } |
| 512 | |
| 513 | auto num_modes = static_cast<uint32_t>(sel_modes.size()); |
| 514 | if (!configs) { |
| 515 | *num_configs = num_modes; |
| 516 | return HWC2::Error::None; |
| 517 | } |
| 518 | |
| 519 | uint32_t idx = 0; |
| 520 | for (const DrmMode &mode : sel_modes) { |
| 521 | if (idx >= *num_configs) |
| 522 | break; |
| 523 | configs[idx++] = mode.id(); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 524 | } |
| 525 | *num_configs = idx; |
| 526 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 530 | supported(__func__); |
| 531 | std::ostringstream stream; |
| 532 | stream << "display-" << connector_->id(); |
| 533 | std::string string = stream.str(); |
| 534 | size_t length = string.length(); |
| 535 | if (!name) { |
| 536 | *size = length; |
| 537 | return HWC2::Error::None; |
| 538 | } |
| 539 | |
| 540 | *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size); |
| 541 | strncpy(name, string.c_str(), *size); |
| 542 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 543 | } |
| 544 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 545 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests, |
| 546 | uint32_t *num_elements, |
| 547 | hwc2_layer_t *layers, |
| 548 | int32_t *layer_requests) { |
| 549 | supported(__func__); |
| 550 | // TODO: I think virtual display should request |
| 551 | // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here |
| 552 | unsupported(__func__, display_requests, num_elements, layers, layer_requests); |
| 553 | *num_elements = 0; |
| 554 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 558 | supported(__func__); |
| 559 | *type = static_cast<int32_t>(type_); |
| 560 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 564 | supported(__func__); |
| 565 | *support = 0; |
| 566 | return HWC2::Error::None; |
| 567 | } |
| 568 | |
| 569 | HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities( |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 570 | uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/, |
| 571 | float * /*max_average_luminance*/, float * /*min_luminance*/) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 572 | supported(__func__); |
| 573 | *num_types = 0; |
| 574 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 578 | hwc2_layer_t *layers, |
| 579 | int32_t *fences) { |
| 580 | supported(__func__); |
| 581 | uint32_t num_layers = 0; |
| 582 | |
| 583 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) { |
| 584 | ++num_layers; |
| 585 | if (layers == NULL || fences == NULL) { |
| 586 | continue; |
| 587 | } else if (num_layers > *num_elements) { |
| 588 | ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements); |
| 589 | return HWC2::Error::None; |
| 590 | } |
| 591 | |
| 592 | layers[num_layers - 1] = l.first; |
| 593 | fences[num_layers - 1] = l.second.take_release_fence(); |
| 594 | } |
| 595 | *num_elements = num_layers; |
| 596 | return HWC2::Error::None; |
| 597 | } |
| 598 | |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 599 | void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 600 | if (fd < 0) |
| 601 | return; |
| 602 | |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 603 | if (present_fence_.get() >= 0) { |
| 604 | int old_fence = present_fence_.get(); |
| 605 | present_fence_.Set(sync_merge("dc_present", old_fence, fd)); |
| 606 | close(fd); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 607 | } else { |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 608 | present_fence_.Set(fd); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 609 | } |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 610 | } |
| 611 | |
Roman Stratiienko | afb3689 | 2019-11-08 17:16:11 +0200 | [diff] [blame] | 612 | bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType( |
| 613 | HWC2::Composition comp_type) { |
| 614 | return comp_type == HWC2::Composition::Device || |
| 615 | comp_type == HWC2::Composition::Cursor; |
| 616 | } |
| 617 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 618 | HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 619 | std::vector<DrmCompositionDisplayLayersMap> layers_map; |
| 620 | layers_map.emplace_back(); |
| 621 | DrmCompositionDisplayLayersMap &map = layers_map.back(); |
| 622 | |
| 623 | map.display = static_cast<int>(handle_); |
| 624 | map.geometry_changed = true; // TODO: Fix this |
| 625 | |
| 626 | // order the layers by z-order |
| 627 | bool use_client_layer = false; |
Alexandru Gheorghe | 1542d29 | 2018-06-13 16:46:36 +0100 | [diff] [blame] | 628 | uint32_t client_z_order = UINT32_MAX; |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 629 | std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map; |
| 630 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) { |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 631 | switch (l.second.validated_type()) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 632 | case HWC2::Composition::Device: |
| 633 | z_map.emplace(std::make_pair(l.second.z_order(), &l.second)); |
| 634 | break; |
| 635 | case HWC2::Composition::Client: |
Alexandru Gheorghe | 1542d29 | 2018-06-13 16:46:36 +0100 | [diff] [blame] | 636 | // Place it at the z_order of the lowest client layer |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 637 | use_client_layer = true; |
Alexandru Gheorghe | 1542d29 | 2018-06-13 16:46:36 +0100 | [diff] [blame] | 638 | client_z_order = std::min(client_z_order, l.second.z_order()); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 639 | break; |
| 640 | default: |
| 641 | continue; |
| 642 | } |
| 643 | } |
| 644 | if (use_client_layer) |
| 645 | z_map.emplace(std::make_pair(client_z_order, &client_layer_)); |
| 646 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 647 | if (z_map.empty()) |
| 648 | return HWC2::Error::BadLayer; |
| 649 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 650 | // now that they're ordered by z, add them to the composition |
| 651 | for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) { |
| 652 | DrmHwcLayer layer; |
| 653 | l.second->PopulateDrmLayer(&layer); |
Andrii Chepurnyi | dc1278c | 2018-03-20 19:41:18 +0200 | [diff] [blame] | 654 | int ret = layer.ImportBuffer(importer_.get()); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 655 | if (ret) { |
| 656 | ALOGE("Failed to import layer, ret=%d", ret); |
| 657 | return HWC2::Error::NoResources; |
| 658 | } |
| 659 | map.layers.emplace_back(std::move(layer)); |
| 660 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 661 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 662 | std::unique_ptr<DrmDisplayComposition> composition = compositor_ |
| 663 | .CreateComposition(); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 664 | composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_); |
| 665 | |
| 666 | // TODO: Don't always assume geometry changed |
| 667 | int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true); |
| 668 | if (ret) { |
| 669 | ALOGE("Failed to set layers in the composition ret=%d", ret); |
| 670 | return HWC2::Error::BadLayer; |
| 671 | } |
| 672 | |
| 673 | std::vector<DrmPlane *> primary_planes(primary_planes_); |
| 674 | std::vector<DrmPlane *> overlay_planes(overlay_planes_); |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 675 | ret = composition->Plan(&primary_planes, &overlay_planes); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 676 | if (ret) { |
| 677 | ALOGE("Failed to plan the composition ret=%d", ret); |
| 678 | return HWC2::Error::BadConfig; |
| 679 | } |
| 680 | |
| 681 | // Disable the planes we're not using |
| 682 | for (auto i = primary_planes.begin(); i != primary_planes.end();) { |
| 683 | composition->AddPlaneDisable(*i); |
| 684 | i = primary_planes.erase(i); |
| 685 | } |
| 686 | for (auto i = overlay_planes.begin(); i != overlay_planes.end();) { |
| 687 | composition->AddPlaneDisable(*i); |
| 688 | i = overlay_planes.erase(i); |
| 689 | } |
| 690 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 691 | if (test) { |
| 692 | ret = compositor_.TestComposition(composition.get()); |
| 693 | } else { |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 694 | ret = compositor_.ApplyComposition(std::move(composition)); |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 695 | AddFenceToPresentFence(compositor_.TakeOutFence()); |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 696 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 697 | if (ret) { |
John Stultz | 78c9f6c | 2018-05-24 16:43:35 -0700 | [diff] [blame] | 698 | if (!test) |
| 699 | ALOGE("Failed to apply the frame composition ret=%d", ret); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 700 | return HWC2::Error::BadParameter; |
| 701 | } |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 702 | return HWC2::Error::None; |
| 703 | } |
| 704 | |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 705 | HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) { |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 706 | supported(__func__); |
| 707 | HWC2::Error ret; |
| 708 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 709 | ++total_stats_.total_frames_; |
| 710 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 711 | ret = CreateComposition(false); |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 712 | if (ret != HWC2::Error::None) |
| 713 | ++total_stats_.failed_kms_present_; |
| 714 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 715 | if (ret == HWC2::Error::BadLayer) { |
| 716 | // Can we really have no client or device layers? |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 717 | *present_fence = -1; |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 718 | return HWC2::Error::None; |
| 719 | } |
| 720 | if (ret != HWC2::Error::None) |
| 721 | return ret; |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 722 | |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 723 | *present_fence = present_fence_.Release(); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 724 | |
| 725 | ++frame_no_; |
| 726 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 730 | supported(__func__); |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 731 | auto mode = std::find_if(connector_->modes().begin(), |
| 732 | connector_->modes().end(), |
| 733 | [config](DrmMode const &m) { |
| 734 | return m.id() == config; |
| 735 | }); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 736 | if (mode == connector_->modes().end()) { |
| 737 | ALOGE("Could not find active mode for %d", config); |
| 738 | return HWC2::Error::BadConfig; |
| 739 | } |
| 740 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 741 | std::unique_ptr<DrmDisplayComposition> composition = compositor_ |
| 742 | .CreateComposition(); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 743 | composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_); |
| 744 | int ret = composition->SetDisplayMode(*mode); |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 745 | ret = compositor_.ApplyComposition(std::move(composition)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 746 | if (ret) { |
| 747 | ALOGE("Failed to queue dpms composition on %d", ret); |
| 748 | return HWC2::Error::BadConfig; |
| 749 | } |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 750 | |
| 751 | connector_->set_active_mode(*mode); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 752 | |
| 753 | // Setup the client layer's dimensions |
| 754 | hwc_rect_t display_frame = {.left = 0, |
| 755 | .top = 0, |
| 756 | .right = static_cast<int>(mode->h_display()), |
| 757 | .bottom = static_cast<int>(mode->v_display())}; |
| 758 | client_layer_.SetLayerDisplayFrame(display_frame); |
| 759 | hwc_frect_t source_crop = {.left = 0.0f, |
| 760 | .top = 0.0f, |
| 761 | .right = mode->h_display() + 0.0f, |
| 762 | .bottom = mode->v_display() + 0.0f}; |
| 763 | client_layer_.SetLayerSourceCrop(source_crop); |
| 764 | |
| 765 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target, |
| 769 | int32_t acquire_fence, |
| 770 | int32_t dataspace, |
Rob Herring | 1b2685c | 2017-11-29 10:19:57 -0600 | [diff] [blame] | 771 | hwc_region_t /*damage*/) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 772 | supported(__func__); |
| 773 | UniqueFd uf(acquire_fence); |
| 774 | |
| 775 | client_layer_.set_buffer(target); |
| 776 | client_layer_.set_acquire_fence(uf.get()); |
| 777 | client_layer_.SetLayerDataspace(dataspace); |
| 778 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 782 | supported(__func__); |
Kalyan Kondapally | da5839c | 2016-11-10 10:59:50 -0800 | [diff] [blame] | 783 | |
| 784 | if (mode != HAL_COLOR_MODE_NATIVE) |
Vincent Donnefort | 7834a89 | 2019-10-09 15:53:56 +0100 | [diff] [blame] | 785 | return HWC2::Error::BadParameter; |
Kalyan Kondapally | da5839c | 2016-11-10 10:59:50 -0800 | [diff] [blame] | 786 | |
| 787 | color_mode_ = mode; |
| 788 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 792 | int32_t hint) { |
| 793 | supported(__func__); |
| 794 | // TODO: Force client composition if we get this |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 795 | return unsupported(__func__, matrix, hint); |
| 796 | } |
| 797 | |
| 798 | HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 799 | int32_t release_fence) { |
| 800 | supported(__func__); |
| 801 | // TODO: Need virtual display support |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 802 | return unsupported(__func__, buffer, release_fence); |
| 803 | } |
| 804 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 805 | HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) { |
| 806 | supported(__func__); |
| 807 | uint64_t dpms_value = 0; |
| 808 | auto mode = static_cast<HWC2::PowerMode>(mode_in); |
| 809 | switch (mode) { |
| 810 | case HWC2::PowerMode::Off: |
| 811 | dpms_value = DRM_MODE_DPMS_OFF; |
| 812 | break; |
| 813 | case HWC2::PowerMode::On: |
| 814 | dpms_value = DRM_MODE_DPMS_ON; |
| 815 | break; |
Vincent Donnefort | 60ef7eb | 2019-10-09 11:39:28 +0100 | [diff] [blame] | 816 | case HWC2::PowerMode::Doze: |
| 817 | case HWC2::PowerMode::DozeSuspend: |
| 818 | return HWC2::Error::Unsupported; |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 819 | default: |
| 820 | ALOGI("Power mode %d is unsupported\n", mode); |
Vincent Donnefort | 60ef7eb | 2019-10-09 11:39:28 +0100 | [diff] [blame] | 821 | return HWC2::Error::BadParameter; |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 822 | }; |
| 823 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 824 | std::unique_ptr<DrmDisplayComposition> composition = compositor_ |
| 825 | .CreateComposition(); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 826 | composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_); |
| 827 | composition->SetDpmsMode(dpms_value); |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 828 | int ret = compositor_.ApplyComposition(std::move(composition)); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 829 | if (ret) { |
| 830 | ALOGE("Failed to apply the dpms composition ret=%d", ret); |
| 831 | return HWC2::Error::BadParameter; |
| 832 | } |
| 833 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 837 | supported(__func__); |
Andrii Chepurnyi | 4bdd0fe | 2018-07-27 15:14:37 +0300 | [diff] [blame] | 838 | vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 839 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 843 | uint32_t *num_requests) { |
| 844 | supported(__func__); |
| 845 | *num_types = 0; |
| 846 | *num_requests = 0; |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 847 | size_t avail_planes = primary_planes_.size() + overlay_planes_.size(); |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 848 | |
| 849 | /* |
| 850 | * If more layers then planes, save one plane |
| 851 | * for client composited layers |
| 852 | */ |
| 853 | if (avail_planes < layers_.size()) |
| 854 | avail_planes--; |
| 855 | |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 856 | std::map<uint32_t, DrmHwcTwo::HwcLayer *, std::greater<int>> z_map; |
| 857 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) |
| 858 | z_map.emplace(std::make_pair(l.second.z_order(), &l.second)); |
| 859 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 860 | uint32_t total_pixops = 0, gpu_pixops = 0; |
| 861 | |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 862 | bool gpu_block = false; |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 863 | for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) { |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 864 | hwc_rect_t df = l.second->display_frame(); |
| 865 | uint32_t pixops = (df.right - df.left) * (df.bottom - df.top); |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 866 | if (gpu_block || avail_planes == 0 || |
Roman Stratiienko | afb3689 | 2019-11-08 17:16:11 +0200 | [diff] [blame] | 867 | !HardwareSupportsLayerType(l.second->sf_type()) || |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 868 | !importer_->CanImportBuffer(l.second->buffer())) { |
| 869 | gpu_block = true; |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 870 | gpu_pixops += pixops; |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 871 | ++*num_types; |
| 872 | } else { |
| 873 | avail_planes--; |
| 874 | } |
| 875 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 876 | total_pixops += pixops; |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 877 | l.second->set_validated_type(gpu_block ? HWC2::Composition::Client |
| 878 | : HWC2::Composition::Device); |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 881 | if (CreateComposition(true) != HWC2::Error::None) { |
| 882 | ++total_stats_.failed_kms_validate_; |
| 883 | gpu_pixops = total_pixops; |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 884 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) |
| 885 | l.second.set_validated_type(HWC2::Composition::Client); |
Roman Stratiienko | 0d1a2cd | 2019-11-28 17:51:16 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | total_stats_.gpu_pixops_ += gpu_pixops; |
| 889 | total_stats_.total_pixops_ += total_pixops; |
Roman Stratiienko | f264723 | 2019-11-21 01:58:35 +0200 | [diff] [blame] | 890 | |
Rob Herring | ee8f45b | 2017-06-09 15:15:55 -0500 | [diff] [blame] | 891 | return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 895 | supported(__func__); |
Kalyan Kondapally | da5839c | 2016-11-10 10:59:50 -0800 | [diff] [blame] | 896 | cursor_x_ = x; |
| 897 | cursor_y_ = y; |
| 898 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 902 | supported(__func__); |
| 903 | blending_ = static_cast<HWC2::BlendMode>(mode); |
| 904 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 908 | int32_t acquire_fence) { |
| 909 | supported(__func__); |
| 910 | UniqueFd uf(acquire_fence); |
| 911 | |
| 912 | // The buffer and acquire_fence are handled elsewhere |
| 913 | if (sf_type_ == HWC2::Composition::Client || |
| 914 | sf_type_ == HWC2::Composition::Sideband || |
| 915 | sf_type_ == HWC2::Composition::SolidColor) |
| 916 | return HWC2::Error::None; |
| 917 | |
| 918 | set_buffer(buffer); |
| 919 | set_acquire_fence(uf.get()); |
| 920 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) { |
Roman Kovalivskyi | bb37569 | 2019-12-11 17:48:44 +0200 | [diff] [blame^] | 924 | // TODO: Put to client composition here? |
| 925 | supported(__func__); |
| 926 | layer_color_ = color; |
| 927 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 931 | sf_type_ = static_cast<HWC2::Composition>(type); |
| 932 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 936 | supported(__func__); |
| 937 | dataspace_ = static_cast<android_dataspace_t>(dataspace); |
| 938 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 942 | supported(__func__); |
| 943 | display_frame_ = frame; |
| 944 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 948 | supported(__func__); |
| 949 | alpha_ = alpha; |
| 950 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream( |
| 954 | const native_handle_t *stream) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 955 | supported(__func__); |
| 956 | // TODO: We don't support sideband |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 957 | return unsupported(__func__, stream); |
| 958 | } |
| 959 | |
| 960 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 961 | supported(__func__); |
| 962 | source_crop_ = crop; |
| 963 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 967 | supported(__func__); |
| 968 | // TODO: We don't use surface damage, marking as unsupported |
| 969 | unsupported(__func__, damage); |
| 970 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 974 | supported(__func__); |
| 975 | transform_ = static_cast<HWC2::Transform>(transform); |
| 976 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) { |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 980 | supported(__func__); |
| 981 | // TODO: We don't use this information, marking as unsupported |
| 982 | unsupported(__func__, visible); |
| 983 | return HWC2::Error::None; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 984 | } |
| 985 | |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 986 | HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) { |
| 987 | supported(__func__); |
| 988 | z_order_ = order; |
| 989 | return HWC2::Error::None; |
| 990 | } |
| 991 | |
| 992 | void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) { |
| 993 | supported(__func__); |
| 994 | switch (blending_) { |
| 995 | case HWC2::BlendMode::None: |
| 996 | layer->blending = DrmHwcBlending::kNone; |
| 997 | break; |
| 998 | case HWC2::BlendMode::Premultiplied: |
| 999 | layer->blending = DrmHwcBlending::kPreMult; |
| 1000 | break; |
| 1001 | case HWC2::BlendMode::Coverage: |
| 1002 | layer->blending = DrmHwcBlending::kCoverage; |
| 1003 | break; |
| 1004 | default: |
| 1005 | ALOGE("Unknown blending mode b=%d", blending_); |
| 1006 | layer->blending = DrmHwcBlending::kNone; |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | OutputFd release_fence = release_fence_output(); |
| 1011 | |
| 1012 | layer->sf_handle = buffer_; |
| 1013 | layer->acquire_fence = acquire_fence_.Release(); |
| 1014 | layer->release_fence = std::move(release_fence); |
| 1015 | layer->SetDisplayFrame(display_frame_); |
Stefan Schake | 025d0a6 | 2018-05-04 18:03:00 +0200 | [diff] [blame] | 1016 | layer->alpha = static_cast<uint16_t>(65535.0f * alpha_ + 0.5f); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1017 | layer->SetSourceCrop(source_crop_); |
| 1018 | layer->SetTransform(static_cast<int32_t>(transform_)); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1019 | } |
| 1020 | |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 1021 | void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) { |
| 1022 | auto cb = callbacks_.find(HWC2::Callback::Hotplug); |
| 1023 | if (cb == callbacks_.end()) |
| 1024 | return; |
| 1025 | |
| 1026 | auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(cb->second.func); |
| 1027 | hotplug(cb->second.data, displayid, |
| 1028 | (state == DRM_MODE_CONNECTED ? HWC2_CONNECTION_CONNECTED |
| 1029 | : HWC2_CONNECTION_DISCONNECTED)); |
| 1030 | } |
| 1031 | |
| 1032 | void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) { |
| 1033 | for (auto &conn : drmDevice->connectors()) { |
| 1034 | if (conn->state() != DRM_MODE_CONNECTED) |
| 1035 | continue; |
| 1036 | HandleDisplayHotplug(conn->display(), conn->state()); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) { |
| 1041 | for (auto &conn : drm_->connectors()) { |
| 1042 | drmModeConnection old_state = conn->state(); |
| 1043 | drmModeConnection cur_state = conn->UpdateModes() |
| 1044 | ? DRM_MODE_UNKNOWNCONNECTION |
| 1045 | : conn->state(); |
| 1046 | |
| 1047 | if (cur_state == old_state) |
| 1048 | continue; |
| 1049 | |
| 1050 | ALOGI("%s event @%" PRIu64 " for connector %u on display %d", |
| 1051 | cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us, |
| 1052 | conn->id(), conn->display()); |
| 1053 | |
| 1054 | int display_id = conn->display(); |
| 1055 | if (cur_state == DRM_MODE_CONNECTED) { |
| 1056 | auto &display = hwc2_->displays_.at(display_id); |
| 1057 | display.ChosePreferredConfig(); |
| 1058 | } else { |
| 1059 | auto &display = hwc2_->displays_.at(display_id); |
| 1060 | display.ClearDisplay(); |
| 1061 | } |
| 1062 | |
| 1063 | hwc2_->HandleDisplayHotplug(display_id, cur_state); |
| 1064 | } |
| 1065 | } |
| 1066 | |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1067 | // static |
| 1068 | int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) { |
| 1069 | unsupported(__func__); |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | // static |
| 1074 | void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/, |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1075 | uint32_t *out_count, |
| 1076 | int32_t * /*out_capabilities*/) { |
| 1077 | supported(__func__); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1078 | *out_count = 0; |
| 1079 | } |
| 1080 | |
| 1081 | // static |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1082 | hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction( |
| 1083 | struct hwc2_device * /*dev*/, int32_t descriptor) { |
| 1084 | supported(__func__); |
| 1085 | auto func = static_cast<HWC2::FunctionDescriptor>(descriptor); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1086 | switch (func) { |
| 1087 | // Device functions |
| 1088 | case HWC2::FunctionDescriptor::CreateVirtualDisplay: |
| 1089 | return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>( |
| 1090 | DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay), |
| 1091 | &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t, |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1092 | int32_t *, hwc2_display_t *>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1093 | case HWC2::FunctionDescriptor::DestroyVirtualDisplay: |
| 1094 | return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>( |
| 1095 | DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay), |
| 1096 | &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>); |
| 1097 | case HWC2::FunctionDescriptor::Dump: |
| 1098 | return ToHook<HWC2_PFN_DUMP>( |
| 1099 | DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump, |
| 1100 | uint32_t *, char *>); |
| 1101 | case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount: |
| 1102 | return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>( |
| 1103 | DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount), |
| 1104 | &DrmHwcTwo::GetMaxVirtualDisplayCount>); |
| 1105 | case HWC2::FunctionDescriptor::RegisterCallback: |
| 1106 | return ToHook<HWC2_PFN_REGISTER_CALLBACK>( |
| 1107 | DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback), |
| 1108 | &DrmHwcTwo::RegisterCallback, int32_t, |
| 1109 | hwc2_callback_data_t, hwc2_function_pointer_t>); |
| 1110 | |
| 1111 | // Display functions |
| 1112 | case HWC2::FunctionDescriptor::AcceptDisplayChanges: |
| 1113 | return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>( |
| 1114 | DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges), |
| 1115 | &HwcDisplay::AcceptDisplayChanges>); |
| 1116 | case HWC2::FunctionDescriptor::CreateLayer: |
| 1117 | return ToHook<HWC2_PFN_CREATE_LAYER>( |
| 1118 | DisplayHook<decltype(&HwcDisplay::CreateLayer), |
| 1119 | &HwcDisplay::CreateLayer, hwc2_layer_t *>); |
| 1120 | case HWC2::FunctionDescriptor::DestroyLayer: |
| 1121 | return ToHook<HWC2_PFN_DESTROY_LAYER>( |
| 1122 | DisplayHook<decltype(&HwcDisplay::DestroyLayer), |
| 1123 | &HwcDisplay::DestroyLayer, hwc2_layer_t>); |
| 1124 | case HWC2::FunctionDescriptor::GetActiveConfig: |
| 1125 | return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>( |
| 1126 | DisplayHook<decltype(&HwcDisplay::GetActiveConfig), |
| 1127 | &HwcDisplay::GetActiveConfig, hwc2_config_t *>); |
| 1128 | case HWC2::FunctionDescriptor::GetChangedCompositionTypes: |
| 1129 | return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>( |
| 1130 | DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes), |
| 1131 | &HwcDisplay::GetChangedCompositionTypes, uint32_t *, |
| 1132 | hwc2_layer_t *, int32_t *>); |
| 1133 | case HWC2::FunctionDescriptor::GetClientTargetSupport: |
| 1134 | return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>( |
| 1135 | DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport), |
| 1136 | &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t, |
| 1137 | int32_t, int32_t>); |
| 1138 | case HWC2::FunctionDescriptor::GetColorModes: |
| 1139 | return ToHook<HWC2_PFN_GET_COLOR_MODES>( |
| 1140 | DisplayHook<decltype(&HwcDisplay::GetColorModes), |
| 1141 | &HwcDisplay::GetColorModes, uint32_t *, int32_t *>); |
| 1142 | case HWC2::FunctionDescriptor::GetDisplayAttribute: |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1143 | return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>( |
| 1144 | DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute), |
| 1145 | &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, |
| 1146 | int32_t *>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1147 | case HWC2::FunctionDescriptor::GetDisplayConfigs: |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1148 | return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>( |
| 1149 | DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs), |
| 1150 | &HwcDisplay::GetDisplayConfigs, uint32_t *, |
| 1151 | hwc2_config_t *>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1152 | case HWC2::FunctionDescriptor::GetDisplayName: |
| 1153 | return ToHook<HWC2_PFN_GET_DISPLAY_NAME>( |
| 1154 | DisplayHook<decltype(&HwcDisplay::GetDisplayName), |
| 1155 | &HwcDisplay::GetDisplayName, uint32_t *, char *>); |
| 1156 | case HWC2::FunctionDescriptor::GetDisplayRequests: |
| 1157 | return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>( |
| 1158 | DisplayHook<decltype(&HwcDisplay::GetDisplayRequests), |
| 1159 | &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *, |
| 1160 | hwc2_layer_t *, int32_t *>); |
| 1161 | case HWC2::FunctionDescriptor::GetDisplayType: |
| 1162 | return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>( |
| 1163 | DisplayHook<decltype(&HwcDisplay::GetDisplayType), |
| 1164 | &HwcDisplay::GetDisplayType, int32_t *>); |
| 1165 | case HWC2::FunctionDescriptor::GetDozeSupport: |
| 1166 | return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>( |
| 1167 | DisplayHook<decltype(&HwcDisplay::GetDozeSupport), |
| 1168 | &HwcDisplay::GetDozeSupport, int32_t *>); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1169 | case HWC2::FunctionDescriptor::GetHdrCapabilities: |
| 1170 | return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>( |
| 1171 | DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities), |
| 1172 | &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *, |
| 1173 | float *, float *, float *>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1174 | case HWC2::FunctionDescriptor::GetReleaseFences: |
| 1175 | return ToHook<HWC2_PFN_GET_RELEASE_FENCES>( |
| 1176 | DisplayHook<decltype(&HwcDisplay::GetReleaseFences), |
| 1177 | &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *, |
| 1178 | int32_t *>); |
| 1179 | case HWC2::FunctionDescriptor::PresentDisplay: |
| 1180 | return ToHook<HWC2_PFN_PRESENT_DISPLAY>( |
| 1181 | DisplayHook<decltype(&HwcDisplay::PresentDisplay), |
| 1182 | &HwcDisplay::PresentDisplay, int32_t *>); |
| 1183 | case HWC2::FunctionDescriptor::SetActiveConfig: |
| 1184 | return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>( |
| 1185 | DisplayHook<decltype(&HwcDisplay::SetActiveConfig), |
| 1186 | &HwcDisplay::SetActiveConfig, hwc2_config_t>); |
| 1187 | case HWC2::FunctionDescriptor::SetClientTarget: |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1188 | return ToHook<HWC2_PFN_SET_CLIENT_TARGET>( |
| 1189 | DisplayHook<decltype(&HwcDisplay::SetClientTarget), |
| 1190 | &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t, |
| 1191 | int32_t, hwc_region_t>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1192 | case HWC2::FunctionDescriptor::SetColorMode: |
| 1193 | return ToHook<HWC2_PFN_SET_COLOR_MODE>( |
| 1194 | DisplayHook<decltype(&HwcDisplay::SetColorMode), |
| 1195 | &HwcDisplay::SetColorMode, int32_t>); |
| 1196 | case HWC2::FunctionDescriptor::SetColorTransform: |
| 1197 | return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>( |
| 1198 | DisplayHook<decltype(&HwcDisplay::SetColorTransform), |
| 1199 | &HwcDisplay::SetColorTransform, const float *, int32_t>); |
| 1200 | case HWC2::FunctionDescriptor::SetOutputBuffer: |
| 1201 | return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>( |
| 1202 | DisplayHook<decltype(&HwcDisplay::SetOutputBuffer), |
| 1203 | &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>); |
| 1204 | case HWC2::FunctionDescriptor::SetPowerMode: |
| 1205 | return ToHook<HWC2_PFN_SET_POWER_MODE>( |
| 1206 | DisplayHook<decltype(&HwcDisplay::SetPowerMode), |
| 1207 | &HwcDisplay::SetPowerMode, int32_t>); |
| 1208 | case HWC2::FunctionDescriptor::SetVsyncEnabled: |
| 1209 | return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>( |
| 1210 | DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled), |
| 1211 | &HwcDisplay::SetVsyncEnabled, int32_t>); |
| 1212 | case HWC2::FunctionDescriptor::ValidateDisplay: |
| 1213 | return ToHook<HWC2_PFN_VALIDATE_DISPLAY>( |
| 1214 | DisplayHook<decltype(&HwcDisplay::ValidateDisplay), |
| 1215 | &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>); |
| 1216 | |
| 1217 | // Layer functions |
| 1218 | case HWC2::FunctionDescriptor::SetCursorPosition: |
| 1219 | return ToHook<HWC2_PFN_SET_CURSOR_POSITION>( |
| 1220 | LayerHook<decltype(&HwcLayer::SetCursorPosition), |
| 1221 | &HwcLayer::SetCursorPosition, int32_t, int32_t>); |
| 1222 | case HWC2::FunctionDescriptor::SetLayerBlendMode: |
| 1223 | return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>( |
| 1224 | LayerHook<decltype(&HwcLayer::SetLayerBlendMode), |
| 1225 | &HwcLayer::SetLayerBlendMode, int32_t>); |
| 1226 | case HWC2::FunctionDescriptor::SetLayerBuffer: |
| 1227 | return ToHook<HWC2_PFN_SET_LAYER_BUFFER>( |
| 1228 | LayerHook<decltype(&HwcLayer::SetLayerBuffer), |
| 1229 | &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>); |
| 1230 | case HWC2::FunctionDescriptor::SetLayerColor: |
| 1231 | return ToHook<HWC2_PFN_SET_LAYER_COLOR>( |
| 1232 | LayerHook<decltype(&HwcLayer::SetLayerColor), |
| 1233 | &HwcLayer::SetLayerColor, hwc_color_t>); |
| 1234 | case HWC2::FunctionDescriptor::SetLayerCompositionType: |
| 1235 | return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>( |
| 1236 | LayerHook<decltype(&HwcLayer::SetLayerCompositionType), |
| 1237 | &HwcLayer::SetLayerCompositionType, int32_t>); |
| 1238 | case HWC2::FunctionDescriptor::SetLayerDataspace: |
| 1239 | return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>( |
| 1240 | LayerHook<decltype(&HwcLayer::SetLayerDataspace), |
| 1241 | &HwcLayer::SetLayerDataspace, int32_t>); |
| 1242 | case HWC2::FunctionDescriptor::SetLayerDisplayFrame: |
| 1243 | return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>( |
| 1244 | LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame), |
| 1245 | &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>); |
| 1246 | case HWC2::FunctionDescriptor::SetLayerPlaneAlpha: |
| 1247 | return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>( |
| 1248 | LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha), |
| 1249 | &HwcLayer::SetLayerPlaneAlpha, float>); |
| 1250 | case HWC2::FunctionDescriptor::SetLayerSidebandStream: |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1251 | return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>( |
| 1252 | LayerHook<decltype(&HwcLayer::SetLayerSidebandStream), |
| 1253 | &HwcLayer::SetLayerSidebandStream, |
| 1254 | const native_handle_t *>); |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1255 | case HWC2::FunctionDescriptor::SetLayerSourceCrop: |
| 1256 | return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>( |
| 1257 | LayerHook<decltype(&HwcLayer::SetLayerSourceCrop), |
| 1258 | &HwcLayer::SetLayerSourceCrop, hwc_frect_t>); |
| 1259 | case HWC2::FunctionDescriptor::SetLayerSurfaceDamage: |
| 1260 | return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>( |
| 1261 | LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage), |
| 1262 | &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>); |
| 1263 | case HWC2::FunctionDescriptor::SetLayerTransform: |
| 1264 | return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>( |
| 1265 | LayerHook<decltype(&HwcLayer::SetLayerTransform), |
| 1266 | &HwcLayer::SetLayerTransform, int32_t>); |
| 1267 | case HWC2::FunctionDescriptor::SetLayerVisibleRegion: |
| 1268 | return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>( |
| 1269 | LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion), |
| 1270 | &HwcLayer::SetLayerVisibleRegion, hwc_region_t>); |
| 1271 | case HWC2::FunctionDescriptor::SetLayerZOrder: |
| 1272 | return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>( |
| 1273 | LayerHook<decltype(&HwcLayer::SetLayerZOrder), |
| 1274 | &HwcLayer::SetLayerZOrder, uint32_t>); |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1275 | case HWC2::FunctionDescriptor::Invalid: |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1276 | default: |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | } |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1280 | |
| 1281 | // static |
| 1282 | int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name, |
| 1283 | struct hw_device_t **dev) { |
| 1284 | supported(__func__); |
| 1285 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 1286 | ALOGE("Invalid module name- %s", name); |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
| 1290 | std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo()); |
| 1291 | if (!ctx) { |
| 1292 | ALOGE("Failed to allocate DrmHwcTwo"); |
| 1293 | return -ENOMEM; |
| 1294 | } |
| 1295 | |
| 1296 | HWC2::Error err = ctx->Init(); |
| 1297 | if (err != HWC2::Error::None) { |
| 1298 | ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err); |
| 1299 | return -EINVAL; |
| 1300 | } |
| 1301 | |
| 1302 | ctx->common.module = const_cast<hw_module_t *>(module); |
| 1303 | *dev = &ctx->common; |
| 1304 | ctx.release(); |
| 1305 | return 0; |
Sean Paul | ed2ec4b | 2016-03-10 15:35:40 -0500 | [diff] [blame] | 1306 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1307 | } // namespace android |
Sean Paul | ac87415 | 2016-03-10 16:00:26 -0500 | [diff] [blame] | 1308 | |
| 1309 | static struct hw_module_methods_t hwc2_module_methods = { |
| 1310 | .open = android::DrmHwcTwo::HookDevOpen, |
| 1311 | }; |
| 1312 | |
| 1313 | hw_module_t HAL_MODULE_INFO_SYM = { |
| 1314 | .tag = HARDWARE_MODULE_TAG, |
| 1315 | .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0), |
| 1316 | .id = HWC_HARDWARE_MODULE_ID, |
| 1317 | .name = "DrmHwcTwo module", |
| 1318 | .author = "The Android Open Source Project", |
| 1319 | .methods = &hwc2_module_methods, |
| 1320 | .dso = NULL, |
| 1321 | .reserved = {0}, |
| 1322 | }; |