Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 LOG_TAG "hwc-display" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include "HwcDisplay.h" |
| 21 | |
| 22 | #include "DrmHwcTwo.h" |
| 23 | #include "backend/BackendManager.h" |
| 24 | #include "bufferinfo/BufferInfoGetter.h" |
| 25 | #include "utils/log.h" |
| 26 | #include "utils/properties.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) { |
| 31 | if (delta.total_pixops_ == 0) |
| 32 | return "No stats yet"; |
| 33 | double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_); |
| 34 | |
| 35 | std::stringstream ss; |
| 36 | ss << " Total frames count: " << delta.total_frames_ << "\n" |
| 37 | << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n" |
| 38 | << " Failed to commit frames: " << delta.failed_kms_present_ << "\n" |
| 39 | << ((delta.failed_kms_present_ > 0) |
| 40 | ? " !!! Internal failure, FIX it please\n" |
| 41 | : "") |
| 42 | << " Flattened frames: " << delta.frames_flattened_ << "\n" |
| 43 | << " Pixel operations (free units)" |
| 44 | << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_ |
| 45 | << "]\n" |
| 46 | << " Composition efficiency: " << ratio; |
| 47 | |
| 48 | return ss.str(); |
| 49 | } |
| 50 | |
| 51 | std::string HwcDisplay::Dump() { |
| 52 | std::string flattening_state_str; |
| 53 | switch (flattenning_state_) { |
| 54 | case ClientFlattenningState::Disabled: |
| 55 | flattening_state_str = "Disabled"; |
| 56 | break; |
| 57 | case ClientFlattenningState::NotRequired: |
| 58 | flattening_state_str = "Not needed"; |
| 59 | break; |
| 60 | case ClientFlattenningState::Flattened: |
| 61 | flattening_state_str = "Active"; |
| 62 | break; |
| 63 | case ClientFlattenningState::ClientRefreshRequested: |
| 64 | flattening_state_str = "Refresh requested"; |
| 65 | break; |
| 66 | default: |
| 67 | flattening_state_str = std::to_string(flattenning_state_) + |
| 68 | " VSync remains"; |
| 69 | } |
| 70 | |
| 71 | std::stringstream ss; |
| 72 | ss << "- Display on: " << connector_->name() << "\n" |
| 73 | << " Flattening state: " << flattening_state_str << "\n" |
| 74 | << "Statistics since system boot:\n" |
| 75 | << DumpDelta(total_stats_) << "\n\n" |
| 76 | << "Statistics since last dumpsys request:\n" |
| 77 | << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n"; |
| 78 | |
| 79 | memcpy(&prev_stats_, &total_stats_, sizeof(Stats)); |
| 80 | return ss.str(); |
| 81 | } |
| 82 | |
| 83 | HwcDisplay::HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm, |
| 84 | hwc2_display_t handle, HWC2::DisplayType type, |
| 85 | DrmHwcTwo *hwc2) |
| 86 | : hwc2_(hwc2), |
| 87 | resource_manager_(resource_manager), |
| 88 | drm_(drm), |
| 89 | handle_(handle), |
| 90 | type_(type), |
| 91 | color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) { |
| 92 | // clang-format off |
| 93 | color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0, |
| 94 | 0.0, 1.0, 0.0, 0.0, |
| 95 | 0.0, 0.0, 1.0, 0.0, |
| 96 | 0.0, 0.0, 0.0, 1.0}; |
| 97 | // clang-format on |
| 98 | } |
| 99 | |
| 100 | void HwcDisplay::ClearDisplay() { |
| 101 | AtomicCommitArgs a_args = {.clear_active_composition = true}; |
| 102 | compositor_.ExecuteAtomicCommit(a_args); |
| 103 | } |
| 104 | |
| 105 | HWC2::Error HwcDisplay::Init(std::vector<DrmPlane *> *planes) { |
| 106 | int display = static_cast<int>(handle_); |
| 107 | int ret = compositor_.Init(resource_manager_, display); |
| 108 | if (ret) { |
| 109 | ALOGE("Failed display compositor init for display %d (%d)", display, ret); |
| 110 | return HWC2::Error::NoResources; |
| 111 | } |
| 112 | |
| 113 | // Split up the given display planes into primary and overlay to properly |
| 114 | // interface with the composition |
| 115 | char use_overlay_planes_prop[PROPERTY_VALUE_MAX]; |
| 116 | property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop, |
| 117 | "1"); |
| 118 | bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10); |
| 119 | for (auto &plane : *planes) { |
| 120 | if (plane->GetType() == DRM_PLANE_TYPE_PRIMARY) |
| 121 | primary_planes_.push_back(plane); |
| 122 | else if (use_overlay_planes && (plane)->GetType() == DRM_PLANE_TYPE_OVERLAY) |
| 123 | overlay_planes_.push_back(plane); |
| 124 | } |
| 125 | |
| 126 | crtc_ = drm_->GetCrtcForDisplay(display); |
| 127 | if (!crtc_) { |
| 128 | ALOGE("Failed to get crtc for display %d", display); |
| 129 | return HWC2::Error::BadDisplay; |
| 130 | } |
| 131 | |
| 132 | connector_ = drm_->GetConnectorForDisplay(display); |
| 133 | if (!connector_) { |
| 134 | ALOGE("Failed to get connector for display %d", display); |
| 135 | return HWC2::Error::BadDisplay; |
| 136 | } |
| 137 | |
| 138 | ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) { |
| 139 | const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_); |
| 140 | /* vsync callback */ |
| 141 | #if PLATFORM_SDK_VERSION > 29 |
| 142 | if (hwc2_->vsync_2_4_callback_.first != nullptr && |
| 143 | hwc2_->vsync_2_4_callback_.second != nullptr) { |
| 144 | hwc2_vsync_period_t period_ns{}; |
| 145 | GetDisplayVsyncPeriod(&period_ns); |
| 146 | hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second, |
| 147 | handle_, timestamp, period_ns); |
| 148 | } else |
| 149 | #endif |
| 150 | if (hwc2_->vsync_callback_.first != nullptr && |
| 151 | hwc2_->vsync_callback_.second != nullptr) { |
| 152 | hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_, |
| 153 | timestamp); |
| 154 | } |
| 155 | }); |
| 156 | if (ret) { |
| 157 | ALOGE("Failed to create event worker for d=%d %d\n", display, ret); |
| 158 | return HWC2::Error::BadDisplay; |
| 159 | } |
| 160 | |
| 161 | ret = flattening_vsync_worker_ |
| 162 | .Init(drm_, display, [this](int64_t /*timestamp*/) { |
| 163 | const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_); |
| 164 | /* Frontend flattening */ |
| 165 | if (flattenning_state_ > |
| 166 | ClientFlattenningState::ClientRefreshRequested && |
| 167 | --flattenning_state_ == |
| 168 | ClientFlattenningState::ClientRefreshRequested && |
| 169 | hwc2_->refresh_callback_.first != nullptr && |
| 170 | hwc2_->refresh_callback_.second != nullptr) { |
| 171 | hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, |
| 172 | handle_); |
| 173 | flattening_vsync_worker_.VSyncControl(false); |
| 174 | } |
| 175 | }); |
| 176 | if (ret) { |
| 177 | ALOGE("Failed to create event worker for d=%d %d\n", display, ret); |
| 178 | return HWC2::Error::BadDisplay; |
| 179 | } |
| 180 | |
| 181 | ret = BackendManager::GetInstance().SetBackendForDisplay(this); |
| 182 | if (ret) { |
| 183 | ALOGE("Failed to set backend for d=%d %d\n", display, ret); |
| 184 | return HWC2::Error::BadDisplay; |
| 185 | } |
| 186 | |
| 187 | client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED); |
| 188 | |
| 189 | return ChosePreferredConfig(); |
| 190 | } |
| 191 | |
| 192 | HWC2::Error HwcDisplay::ChosePreferredConfig() { |
| 193 | // Fetch the number of modes from the display |
| 194 | uint32_t num_configs = 0; |
| 195 | HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr); |
| 196 | if (err != HWC2::Error::None || !num_configs) |
| 197 | return HWC2::Error::BadDisplay; |
| 198 | |
| 199 | return SetActiveConfig(preferred_config_id_); |
| 200 | } |
| 201 | |
| 202 | HWC2::Error HwcDisplay::AcceptDisplayChanges() { |
| 203 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) |
| 204 | l.second.AcceptTypeChange(); |
| 205 | return HWC2::Error::None; |
| 206 | } |
| 207 | |
| 208 | HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) { |
| 209 | layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer()); |
| 210 | *layer = static_cast<hwc2_layer_t>(layer_idx_); |
| 211 | ++layer_idx_; |
| 212 | return HWC2::Error::None; |
| 213 | } |
| 214 | |
| 215 | HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) { |
| 216 | if (!get_layer(layer)) |
| 217 | return HWC2::Error::BadLayer; |
| 218 | |
| 219 | layers_.erase(layer); |
| 220 | return HWC2::Error::None; |
| 221 | } |
| 222 | |
| 223 | HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const { |
| 224 | if (hwc_configs_.count(active_config_id_) == 0) |
| 225 | return HWC2::Error::BadConfig; |
| 226 | |
| 227 | *config = active_config_id_; |
| 228 | return HWC2::Error::None; |
| 229 | } |
| 230 | |
| 231 | HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements, |
| 232 | hwc2_layer_t *layers, |
| 233 | int32_t *types) { |
| 234 | uint32_t num_changes = 0; |
| 235 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 236 | if (l.second.IsTypeChanged()) { |
| 237 | if (layers && num_changes < *num_elements) |
| 238 | layers[num_changes] = l.first; |
| 239 | if (types && num_changes < *num_elements) |
| 240 | types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType()); |
| 241 | ++num_changes; |
| 242 | } |
| 243 | } |
| 244 | if (!layers && !types) |
| 245 | *num_elements = num_changes; |
| 246 | return HWC2::Error::None; |
| 247 | } |
| 248 | |
| 249 | HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height, |
| 250 | int32_t /*format*/, |
| 251 | int32_t dataspace) { |
| 252 | std::pair<uint32_t, uint32_t> min = drm_->min_resolution(); |
| 253 | std::pair<uint32_t, uint32_t> max = drm_->max_resolution(); |
| 254 | |
| 255 | if (width < min.first || height < min.second) |
| 256 | return HWC2::Error::Unsupported; |
| 257 | |
| 258 | if (width > max.first || height > max.second) |
| 259 | return HWC2::Error::Unsupported; |
| 260 | |
| 261 | if (dataspace != HAL_DATASPACE_UNKNOWN) |
| 262 | return HWC2::Error::Unsupported; |
| 263 | |
| 264 | // TODO(nobody): Validate format can be handled by either GL or planes |
| 265 | return HWC2::Error::None; |
| 266 | } |
| 267 | |
| 268 | HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) { |
| 269 | if (!modes) |
| 270 | *num_modes = 1; |
| 271 | |
| 272 | if (modes) |
| 273 | *modes = HAL_COLOR_MODE_NATIVE; |
| 274 | |
| 275 | return HWC2::Error::None; |
| 276 | } |
| 277 | |
| 278 | HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config, |
| 279 | int32_t attribute_in, |
| 280 | int32_t *value) { |
| 281 | int conf = static_cast<int>(config); |
| 282 | |
| 283 | if (hwc_configs_.count(conf) == 0) { |
| 284 | ALOGE("Could not find active mode for %d", conf); |
| 285 | return HWC2::Error::BadConfig; |
| 286 | } |
| 287 | |
| 288 | auto &hwc_config = hwc_configs_[conf]; |
| 289 | |
| 290 | static const int32_t kUmPerInch = 25400; |
| 291 | uint32_t mm_width = connector_->mm_width(); |
| 292 | uint32_t mm_height = connector_->mm_height(); |
| 293 | auto attribute = static_cast<HWC2::Attribute>(attribute_in); |
| 294 | switch (attribute) { |
| 295 | case HWC2::Attribute::Width: |
| 296 | *value = static_cast<int>(hwc_config.mode.h_display()); |
| 297 | break; |
| 298 | case HWC2::Attribute::Height: |
| 299 | *value = static_cast<int>(hwc_config.mode.v_display()); |
| 300 | break; |
| 301 | case HWC2::Attribute::VsyncPeriod: |
| 302 | // in nanoseconds |
| 303 | *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh()); |
| 304 | break; |
| 305 | case HWC2::Attribute::DpiX: |
| 306 | // Dots per 1000 inches |
| 307 | *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() * |
| 308 | kUmPerInch / mm_width) |
| 309 | : -1; |
| 310 | break; |
| 311 | case HWC2::Attribute::DpiY: |
| 312 | // Dots per 1000 inches |
| 313 | *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() * |
| 314 | kUmPerInch / mm_height) |
| 315 | : -1; |
| 316 | break; |
| 317 | #if PLATFORM_SDK_VERSION > 29 |
| 318 | case HWC2::Attribute::ConfigGroup: |
| 319 | /* Dispite ConfigGroup is a part of HWC2.4 API, framework |
| 320 | * able to request it even if service @2.1 is used */ |
| 321 | *value = hwc_config.group_id; |
| 322 | break; |
| 323 | #endif |
| 324 | default: |
| 325 | *value = -1; |
| 326 | return HWC2::Error::BadConfig; |
| 327 | } |
| 328 | return HWC2::Error::None; |
| 329 | } |
| 330 | |
| 331 | // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme |
| 332 | HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs, |
| 333 | hwc2_config_t *configs) { |
| 334 | // Since this callback is normally invoked twice (once to get the count, and |
| 335 | // once to populate configs), we don't really want to read the edid |
| 336 | // redundantly. Instead, only update the modes on the first invocation. While |
| 337 | // it's possible this will result in stale modes, it'll all come out in the |
| 338 | // wash when we try to set the active config later. |
| 339 | if (!configs) { |
| 340 | int ret = connector_->UpdateModes(); |
| 341 | if (ret) { |
| 342 | ALOGE("Failed to update display modes %d", ret); |
| 343 | return HWC2::Error::BadDisplay; |
| 344 | } |
| 345 | |
| 346 | hwc_configs_.clear(); |
| 347 | preferred_config_id_ = 0; |
| 348 | int preferred_config_group_id_ = 0; |
| 349 | |
| 350 | if (connector_->modes().empty()) { |
| 351 | ALOGE("No modes reported by KMS"); |
| 352 | return HWC2::Error::BadDisplay; |
| 353 | } |
| 354 | |
| 355 | int last_config_id = 1; |
| 356 | int last_group_id = 1; |
| 357 | |
| 358 | /* Group modes */ |
| 359 | for (const auto &mode : connector_->modes()) { |
| 360 | /* Find group for the new mode or create new group */ |
| 361 | int group_found = 0; |
| 362 | for (auto &hwc_config : hwc_configs_) { |
| 363 | if (mode.h_display() == hwc_config.second.mode.h_display() && |
| 364 | mode.v_display() == hwc_config.second.mode.v_display()) { |
| 365 | group_found = hwc_config.second.group_id; |
| 366 | } |
| 367 | } |
| 368 | if (group_found == 0) { |
| 369 | group_found = last_group_id++; |
| 370 | } |
| 371 | |
| 372 | bool disabled = false; |
| 373 | if (mode.flags() & DRM_MODE_FLAG_3D_MASK) { |
| 374 | ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)", |
| 375 | mode.name().c_str()); |
| 376 | disabled = true; |
| 377 | } |
| 378 | |
| 379 | /* Add config */ |
| 380 | hwc_configs_[last_config_id] = { |
| 381 | .id = last_config_id, |
| 382 | .group_id = group_found, |
| 383 | .mode = mode, |
| 384 | .disabled = disabled, |
| 385 | }; |
| 386 | |
| 387 | /* Chwck if the mode is preferred */ |
| 388 | if ((mode.type() & DRM_MODE_TYPE_PREFERRED) != 0 && |
| 389 | preferred_config_id_ == 0) { |
| 390 | preferred_config_id_ = last_config_id; |
| 391 | preferred_config_group_id_ = group_found; |
| 392 | } |
| 393 | |
| 394 | last_config_id++; |
| 395 | } |
| 396 | |
| 397 | /* We must have preferred mode. Set first mode as preferred |
| 398 | * in case KMS haven't reported anything. */ |
| 399 | if (preferred_config_id_ == 0) { |
| 400 | preferred_config_id_ = 1; |
| 401 | preferred_config_group_id_ = 1; |
| 402 | } |
| 403 | |
| 404 | for (int group = 1; group < last_group_id; group++) { |
| 405 | bool has_interlaced = false; |
| 406 | bool has_progressive = false; |
| 407 | for (auto &hwc_config : hwc_configs_) { |
| 408 | if (hwc_config.second.group_id != group || hwc_config.second.disabled) { |
| 409 | continue; |
| 410 | } |
| 411 | |
| 412 | if (hwc_config.second.IsInterlaced()) { |
| 413 | has_interlaced = true; |
| 414 | } else { |
| 415 | has_progressive = true; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | bool has_both = has_interlaced && has_progressive; |
| 420 | if (!has_both) { |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | bool group_contains_preferred_interlaced = false; |
| 425 | if (group == preferred_config_group_id_ && |
| 426 | hwc_configs_[preferred_config_id_].IsInterlaced()) { |
| 427 | group_contains_preferred_interlaced = true; |
| 428 | } |
| 429 | |
| 430 | for (auto &hwc_config : hwc_configs_) { |
| 431 | if (hwc_config.second.group_id != group || hwc_config.second.disabled) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | bool disable = group_contains_preferred_interlaced |
| 436 | ? !hwc_config.second.IsInterlaced() |
| 437 | : hwc_config.second.IsInterlaced(); |
| 438 | |
| 439 | if (disable) { |
| 440 | ALOGI( |
| 441 | "Group %i: Disabling display mode %s (This group should consist " |
| 442 | "of %s modes)", |
| 443 | group, hwc_config.second.mode.name().c_str(), |
| 444 | group_contains_preferred_interlaced ? "interlaced" |
| 445 | : "progressive"); |
| 446 | |
| 447 | hwc_config.second.disabled = true; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* Group should not contain 2 modes with FPS delta less than ~1HZ |
| 453 | * otherwise android.graphics.cts.SetFrameRateTest CTS will fail |
| 454 | */ |
| 455 | for (int m1 = 1; m1 < last_config_id; m1++) { |
| 456 | for (int m2 = 1; m2 < last_config_id; m2++) { |
| 457 | if (m1 != m2 && |
| 458 | hwc_configs_[m1].group_id == hwc_configs_[m2].group_id && |
| 459 | !hwc_configs_[m1].disabled && !hwc_configs_[m2].disabled && |
| 460 | fabsf(hwc_configs_[m1].mode.v_refresh() - |
| 461 | hwc_configs_[m2].mode.v_refresh()) < 1.0) { |
| 462 | ALOGI( |
| 463 | "Group %i: Disabling display mode %s (Refresh rate value is " |
| 464 | "too close to existing mode %s)", |
| 465 | hwc_configs_[m2].group_id, hwc_configs_[m2].mode.name().c_str(), |
| 466 | hwc_configs_[m1].mode.name().c_str()); |
| 467 | |
| 468 | hwc_configs_[m2].disabled = true; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | uint32_t idx = 0; |
| 475 | for (auto &hwc_config : hwc_configs_) { |
| 476 | if (hwc_config.second.disabled) { |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | if (configs != nullptr) { |
| 481 | if (idx >= *num_configs) { |
| 482 | break; |
| 483 | } |
| 484 | configs[idx] = hwc_config.second.id; |
| 485 | } |
| 486 | |
| 487 | idx++; |
| 488 | } |
| 489 | *num_configs = idx; |
| 490 | return HWC2::Error::None; |
| 491 | } |
| 492 | |
| 493 | HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) { |
| 494 | std::ostringstream stream; |
| 495 | stream << "display-" << connector_->id(); |
| 496 | std::string string = stream.str(); |
| 497 | size_t length = string.length(); |
| 498 | if (!name) { |
| 499 | *size = length; |
| 500 | return HWC2::Error::None; |
| 501 | } |
| 502 | |
| 503 | *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size); |
| 504 | strncpy(name, string.c_str(), *size); |
| 505 | return HWC2::Error::None; |
| 506 | } |
| 507 | |
| 508 | HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/, |
| 509 | uint32_t *num_elements, |
| 510 | hwc2_layer_t * /*layers*/, |
| 511 | int32_t * /*layer_requests*/) { |
| 512 | // TODO(nobody): I think virtual display should request |
| 513 | // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here |
| 514 | *num_elements = 0; |
| 515 | return HWC2::Error::None; |
| 516 | } |
| 517 | |
| 518 | HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) { |
| 519 | *type = static_cast<int32_t>(type_); |
| 520 | return HWC2::Error::None; |
| 521 | } |
| 522 | |
| 523 | HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) { |
| 524 | *support = 0; |
| 525 | return HWC2::Error::None; |
| 526 | } |
| 527 | |
| 528 | HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, |
| 529 | int32_t * /*types*/, |
| 530 | float * /*max_luminance*/, |
| 531 | float * /*max_average_luminance*/, |
| 532 | float * /*min_luminance*/) { |
| 533 | *num_types = 0; |
| 534 | return HWC2::Error::None; |
| 535 | } |
| 536 | |
| 537 | /* Find API details at: |
| 538 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767 |
| 539 | */ |
| 540 | HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements, |
| 541 | hwc2_layer_t *layers, |
| 542 | int32_t *fences) { |
| 543 | uint32_t num_layers = 0; |
| 544 | |
| 545 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 546 | ++num_layers; |
| 547 | if (layers == nullptr || fences == nullptr) |
| 548 | continue; |
| 549 | |
| 550 | if (num_layers > *num_elements) { |
| 551 | ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements); |
| 552 | return HWC2::Error::None; |
| 553 | } |
| 554 | |
| 555 | layers[num_layers - 1] = l.first; |
| 556 | fences[num_layers - 1] = l.second.GetReleaseFence().Release(); |
| 557 | } |
| 558 | *num_elements = num_layers; |
| 559 | return HWC2::Error::None; |
| 560 | } |
| 561 | |
| 562 | HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) { |
| 563 | // order the layers by z-order |
| 564 | bool use_client_layer = false; |
| 565 | uint32_t client_z_order = UINT32_MAX; |
| 566 | std::map<uint32_t, HwcLayer *> z_map; |
| 567 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 568 | switch (l.second.GetValidatedType()) { |
| 569 | case HWC2::Composition::Device: |
| 570 | z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second)); |
| 571 | break; |
| 572 | case HWC2::Composition::Client: |
| 573 | // Place it at the z_order of the lowest client layer |
| 574 | use_client_layer = true; |
| 575 | client_z_order = std::min(client_z_order, l.second.GetZOrder()); |
| 576 | break; |
| 577 | default: |
| 578 | continue; |
| 579 | } |
| 580 | } |
| 581 | if (use_client_layer) |
| 582 | z_map.emplace(std::make_pair(client_z_order, &client_layer_)); |
| 583 | |
| 584 | if (z_map.empty()) |
| 585 | return HWC2::Error::BadLayer; |
| 586 | |
| 587 | std::vector<DrmHwcLayer> composition_layers; |
| 588 | |
| 589 | // now that they're ordered by z, add them to the composition |
| 590 | for (std::pair<const uint32_t, HwcLayer *> &l : z_map) { |
| 591 | DrmHwcLayer layer; |
| 592 | l.second->PopulateDrmLayer(&layer); |
| 593 | int ret = layer.ImportBuffer(drm_); |
| 594 | if (ret) { |
| 595 | ALOGE("Failed to import layer, ret=%d", ret); |
| 596 | return HWC2::Error::NoResources; |
| 597 | } |
| 598 | composition_layers.emplace_back(std::move(layer)); |
| 599 | } |
| 600 | |
| 601 | auto composition = std::make_shared<DrmDisplayComposition>(crtc_); |
| 602 | |
| 603 | // TODO(nobody): Don't always assume geometry changed |
| 604 | int ret = composition->SetLayers(composition_layers.data(), |
| 605 | composition_layers.size()); |
| 606 | if (ret) { |
| 607 | ALOGE("Failed to set layers in the composition ret=%d", ret); |
| 608 | return HWC2::Error::BadLayer; |
| 609 | } |
| 610 | |
| 611 | std::vector<DrmPlane *> primary_planes(primary_planes_); |
| 612 | std::vector<DrmPlane *> overlay_planes(overlay_planes_); |
| 613 | ret = composition->Plan(&primary_planes, &overlay_planes); |
| 614 | if (ret) { |
| 615 | ALOGV("Failed to plan the composition ret=%d", ret); |
| 616 | return HWC2::Error::BadConfig; |
| 617 | } |
| 618 | |
| 619 | a_args.composition = composition; |
| 620 | if (staged_mode) { |
| 621 | a_args.display_mode = *staged_mode; |
| 622 | } |
| 623 | ret = compositor_.ExecuteAtomicCommit(a_args); |
| 624 | |
| 625 | if (ret) { |
| 626 | if (!a_args.test_only) |
| 627 | ALOGE("Failed to apply the frame composition ret=%d", ret); |
| 628 | return HWC2::Error::BadParameter; |
| 629 | } |
| 630 | |
| 631 | if (!a_args.test_only) { |
| 632 | staged_mode.reset(); |
| 633 | } |
| 634 | |
| 635 | return HWC2::Error::None; |
| 636 | } |
| 637 | |
| 638 | /* Find API details at: |
| 639 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805 |
| 640 | */ |
| 641 | HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) { |
| 642 | HWC2::Error ret; |
| 643 | |
| 644 | ++total_stats_.total_frames_; |
| 645 | |
| 646 | AtomicCommitArgs a_args{}; |
| 647 | ret = CreateComposition(a_args); |
| 648 | |
| 649 | if (ret != HWC2::Error::None) |
| 650 | ++total_stats_.failed_kms_present_; |
| 651 | |
| 652 | if (ret == HWC2::Error::BadLayer) { |
| 653 | // Can we really have no client or device layers? |
| 654 | *present_fence = -1; |
| 655 | return HWC2::Error::None; |
| 656 | } |
| 657 | if (ret != HWC2::Error::None) |
| 658 | return ret; |
| 659 | |
| 660 | *present_fence = a_args.out_fence.Release(); |
| 661 | |
| 662 | ++frame_no_; |
| 663 | return HWC2::Error::None; |
| 664 | } |
| 665 | |
| 666 | HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) { |
| 667 | int conf = static_cast<int>(config); |
| 668 | |
| 669 | if (hwc_configs_.count(conf) == 0) { |
| 670 | ALOGE("Could not find active mode for %d", conf); |
| 671 | return HWC2::Error::BadConfig; |
| 672 | } |
| 673 | |
| 674 | auto &mode = hwc_configs_[conf].mode; |
| 675 | |
| 676 | staged_mode = mode; |
| 677 | |
| 678 | active_config_id_ = conf; |
| 679 | |
| 680 | // Setup the client layer's dimensions |
| 681 | hwc_rect_t display_frame = {.left = 0, |
| 682 | .top = 0, |
| 683 | .right = static_cast<int>(mode.h_display()), |
| 684 | .bottom = static_cast<int>(mode.v_display())}; |
| 685 | client_layer_.SetLayerDisplayFrame(display_frame); |
| 686 | |
| 687 | return HWC2::Error::None; |
| 688 | } |
| 689 | |
| 690 | /* Find API details at: |
| 691 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861 |
| 692 | */ |
| 693 | HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target, |
| 694 | int32_t acquire_fence, |
| 695 | int32_t dataspace, |
| 696 | hwc_region_t /*damage*/) { |
| 697 | client_layer_.SetLayerBuffer(target, acquire_fence); |
| 698 | client_layer_.SetLayerDataspace(dataspace); |
| 699 | |
| 700 | /* |
| 701 | * target can be nullptr, this does mean the Composer Service is calling |
| 702 | * cleanDisplayResources() on after receiving HOTPLUG event. See more at: |
| 703 | * https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerClient.h;l=350;drc=944b68180b008456ed2eb4d4d329e33b19bd5166 |
| 704 | */ |
| 705 | if (target == nullptr) { |
| 706 | return HWC2::Error::None; |
| 707 | } |
| 708 | |
| 709 | /* TODO: Do not update source_crop every call. |
| 710 | * It makes sense to do it once after every hotplug event. */ |
| 711 | HwcDrmBo bo{}; |
| 712 | BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo); |
| 713 | |
| 714 | hwc_frect_t source_crop = {.left = 0.0F, |
| 715 | .top = 0.0F, |
| 716 | .right = static_cast<float>(bo.width), |
| 717 | .bottom = static_cast<float>(bo.height)}; |
| 718 | client_layer_.SetLayerSourceCrop(source_crop); |
| 719 | |
| 720 | return HWC2::Error::None; |
| 721 | } |
| 722 | |
| 723 | HWC2::Error HwcDisplay::SetColorMode(int32_t mode) { |
| 724 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 725 | return HWC2::Error::BadParameter; |
| 726 | |
| 727 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 728 | return HWC2::Error::Unsupported; |
| 729 | |
| 730 | color_mode_ = mode; |
| 731 | return HWC2::Error::None; |
| 732 | } |
| 733 | |
| 734 | HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) { |
| 735 | if (hint < HAL_COLOR_TRANSFORM_IDENTITY || |
| 736 | hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA) |
| 737 | return HWC2::Error::BadParameter; |
| 738 | |
| 739 | if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 740 | return HWC2::Error::BadParameter; |
| 741 | |
| 742 | color_transform_hint_ = static_cast<android_color_transform_t>(hint); |
| 743 | if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 744 | std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin()); |
| 745 | |
| 746 | return HWC2::Error::None; |
| 747 | } |
| 748 | |
| 749 | HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/, |
| 750 | int32_t /*release_fence*/) { |
| 751 | // TODO(nobody): Need virtual display support |
| 752 | return HWC2::Error::Unsupported; |
| 753 | } |
| 754 | |
| 755 | HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) { |
| 756 | auto mode = static_cast<HWC2::PowerMode>(mode_in); |
| 757 | AtomicCommitArgs a_args{}; |
| 758 | |
| 759 | switch (mode) { |
| 760 | case HWC2::PowerMode::Off: |
| 761 | a_args.active = false; |
| 762 | break; |
| 763 | case HWC2::PowerMode::On: |
| 764 | /* |
| 765 | * Setting the display to active before we have a composition |
| 766 | * can break some drivers, so skip setting a_args.active to |
| 767 | * true, as the next composition frame will implicitly activate |
| 768 | * the display |
| 769 | */ |
| 770 | return HWC2::Error::None; |
| 771 | break; |
| 772 | case HWC2::PowerMode::Doze: |
| 773 | case HWC2::PowerMode::DozeSuspend: |
| 774 | return HWC2::Error::Unsupported; |
| 775 | default: |
| 776 | ALOGI("Power mode %d is unsupported\n", mode); |
| 777 | return HWC2::Error::BadParameter; |
| 778 | }; |
| 779 | |
| 780 | int err = compositor_.ExecuteAtomicCommit(a_args); |
| 781 | if (err) { |
| 782 | ALOGE("Failed to apply the dpms composition err=%d", err); |
| 783 | return HWC2::Error::BadParameter; |
| 784 | } |
| 785 | return HWC2::Error::None; |
| 786 | } |
| 787 | |
| 788 | HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) { |
| 789 | vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled); |
| 790 | return HWC2::Error::None; |
| 791 | } |
| 792 | |
| 793 | HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types, |
| 794 | uint32_t *num_requests) { |
| 795 | return backend_->ValidateDisplay(this, num_types, num_requests); |
| 796 | } |
| 797 | |
| 798 | std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() { |
| 799 | std::vector<HwcLayer *> ordered_layers; |
| 800 | ordered_layers.reserve(layers_.size()); |
| 801 | |
| 802 | for (auto &[handle, layer] : layers_) { |
| 803 | ordered_layers.emplace_back(&layer); |
| 804 | } |
| 805 | |
| 806 | std::sort(std::begin(ordered_layers), std::end(ordered_layers), |
| 807 | [](const HwcLayer *lhs, const HwcLayer *rhs) { |
| 808 | return lhs->GetZOrder() < rhs->GetZOrder(); |
| 809 | }); |
| 810 | |
| 811 | return ordered_layers; |
| 812 | } |
| 813 | |
| 814 | #if PLATFORM_SDK_VERSION > 29 |
| 815 | HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) { |
| 816 | if (connector_->internal()) |
| 817 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal); |
| 818 | else if (connector_->external()) |
| 819 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External); |
| 820 | else |
| 821 | return HWC2::Error::BadConfig; |
| 822 | |
| 823 | return HWC2::Error::None; |
| 824 | } |
| 825 | |
| 826 | HWC2::Error HwcDisplay::GetDisplayVsyncPeriod( |
| 827 | hwc2_vsync_period_t *outVsyncPeriod /* ns */) { |
| 828 | return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD, |
| 829 | (int32_t *)(outVsyncPeriod)); |
| 830 | } |
| 831 | |
| 832 | HWC2::Error HwcDisplay::SetActiveConfigWithConstraints( |
| 833 | hwc2_config_t /*config*/, |
| 834 | hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints, |
| 835 | hwc_vsync_period_change_timeline_t *outTimeline) { |
| 836 | if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) { |
| 837 | return HWC2::Error::BadParameter; |
| 838 | } |
| 839 | |
| 840 | return HWC2::Error::BadConfig; |
| 841 | } |
| 842 | |
| 843 | HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) { |
| 844 | return HWC2::Error::Unsupported; |
| 845 | } |
| 846 | |
| 847 | HWC2::Error HwcDisplay::GetSupportedContentTypes( |
| 848 | uint32_t *outNumSupportedContentTypes, |
| 849 | const uint32_t *outSupportedContentTypes) { |
| 850 | if (outSupportedContentTypes == nullptr) |
| 851 | *outNumSupportedContentTypes = 0; |
| 852 | |
| 853 | return HWC2::Error::None; |
| 854 | } |
| 855 | |
| 856 | HWC2::Error HwcDisplay::SetContentType(int32_t contentType) { |
| 857 | if (contentType != HWC2_CONTENT_TYPE_NONE) |
| 858 | return HWC2::Error::Unsupported; |
| 859 | |
| 860 | /* TODO: Map to the DRM Connector property: |
| 861 | * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809 |
| 862 | */ |
| 863 | |
| 864 | return HWC2::Error::None; |
| 865 | } |
| 866 | #endif |
| 867 | |
| 868 | #if PLATFORM_SDK_VERSION > 28 |
| 869 | HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort, |
| 870 | uint32_t *outDataSize, |
| 871 | uint8_t *outData) { |
| 872 | auto blob = connector_->GetEdidBlob(); |
| 873 | |
| 874 | if (!blob) { |
| 875 | ALOGE("Failed to get edid property value."); |
| 876 | return HWC2::Error::Unsupported; |
| 877 | } |
| 878 | |
| 879 | if (outData) { |
| 880 | *outDataSize = std::min(*outDataSize, blob->length); |
| 881 | memcpy(outData, blob->data, *outDataSize); |
| 882 | } else { |
| 883 | *outDataSize = blob->length; |
| 884 | } |
| 885 | *outPort = connector_->id(); |
| 886 | |
| 887 | return HWC2::Error::None; |
| 888 | } |
| 889 | |
| 890 | HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities, |
| 891 | uint32_t * /*outCapabilities*/) { |
| 892 | if (outNumCapabilities == nullptr) { |
| 893 | return HWC2::Error::BadParameter; |
| 894 | } |
| 895 | |
| 896 | *outNumCapabilities = 0; |
| 897 | |
| 898 | return HWC2::Error::None; |
| 899 | } |
| 900 | |
| 901 | HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) { |
| 902 | *supported = false; |
| 903 | return HWC2::Error::None; |
| 904 | } |
| 905 | |
| 906 | HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) { |
| 907 | return HWC2::Error::Unsupported; |
| 908 | } |
| 909 | |
| 910 | #endif /* PLATFORM_SDK_VERSION > 28 */ |
| 911 | |
| 912 | #if PLATFORM_SDK_VERSION > 27 |
| 913 | |
| 914 | HWC2::Error HwcDisplay::GetRenderIntents( |
| 915 | int32_t mode, uint32_t *outNumIntents, |
| 916 | int32_t * /*android_render_intent_v1_1_t*/ outIntents) { |
| 917 | if (mode != HAL_COLOR_MODE_NATIVE) { |
| 918 | return HWC2::Error::BadParameter; |
| 919 | } |
| 920 | |
| 921 | if (outIntents == nullptr) { |
| 922 | *outNumIntents = 1; |
| 923 | return HWC2::Error::None; |
| 924 | } |
| 925 | *outNumIntents = 1; |
| 926 | outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC; |
| 927 | return HWC2::Error::None; |
| 928 | } |
| 929 | |
| 930 | HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) { |
| 931 | if (intent < HAL_RENDER_INTENT_COLORIMETRIC || |
| 932 | intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) |
| 933 | return HWC2::Error::BadParameter; |
| 934 | |
| 935 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 936 | return HWC2::Error::BadParameter; |
| 937 | |
| 938 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 939 | return HWC2::Error::Unsupported; |
| 940 | |
| 941 | if (intent != HAL_RENDER_INTENT_COLORIMETRIC) |
| 942 | return HWC2::Error::Unsupported; |
| 943 | |
| 944 | color_mode_ = mode; |
| 945 | return HWC2::Error::None; |
| 946 | } |
| 947 | |
| 948 | #endif /* PLATFORM_SDK_VERSION > 27 */ |
| 949 | |
| 950 | const Backend *HwcDisplay::backend() const { |
| 951 | return backend_.get(); |
| 952 | } |
| 953 | |
| 954 | void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) { |
| 955 | backend_ = std::move(backend); |
| 956 | } |
| 957 | |
| 958 | } // namespace android |