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() { |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 193 | HWC2::Error err = configs_.Update(*connector_); |
| 194 | if (err != HWC2::Error::None) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 195 | return HWC2::Error::BadDisplay; |
| 196 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 197 | return SetActiveConfig(configs_.preferred_config_id); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | HWC2::Error HwcDisplay::AcceptDisplayChanges() { |
| 201 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) |
| 202 | l.second.AcceptTypeChange(); |
| 203 | return HWC2::Error::None; |
| 204 | } |
| 205 | |
| 206 | HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) { |
| 207 | layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer()); |
| 208 | *layer = static_cast<hwc2_layer_t>(layer_idx_); |
| 209 | ++layer_idx_; |
| 210 | return HWC2::Error::None; |
| 211 | } |
| 212 | |
| 213 | HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) { |
| 214 | if (!get_layer(layer)) |
| 215 | return HWC2::Error::BadLayer; |
| 216 | |
| 217 | layers_.erase(layer); |
| 218 | return HWC2::Error::None; |
| 219 | } |
| 220 | |
| 221 | HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const { |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 222 | if (configs_.hwc_configs.count(configs_.active_config_id) == 0) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 223 | return HWC2::Error::BadConfig; |
| 224 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 225 | *config = configs_.active_config_id; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 226 | return HWC2::Error::None; |
| 227 | } |
| 228 | |
| 229 | HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements, |
| 230 | hwc2_layer_t *layers, |
| 231 | int32_t *types) { |
| 232 | uint32_t num_changes = 0; |
| 233 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 234 | if (l.second.IsTypeChanged()) { |
| 235 | if (layers && num_changes < *num_elements) |
| 236 | layers[num_changes] = l.first; |
| 237 | if (types && num_changes < *num_elements) |
| 238 | types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType()); |
| 239 | ++num_changes; |
| 240 | } |
| 241 | } |
| 242 | if (!layers && !types) |
| 243 | *num_elements = num_changes; |
| 244 | return HWC2::Error::None; |
| 245 | } |
| 246 | |
| 247 | HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height, |
| 248 | int32_t /*format*/, |
| 249 | int32_t dataspace) { |
| 250 | std::pair<uint32_t, uint32_t> min = drm_->min_resolution(); |
| 251 | std::pair<uint32_t, uint32_t> max = drm_->max_resolution(); |
| 252 | |
| 253 | if (width < min.first || height < min.second) |
| 254 | return HWC2::Error::Unsupported; |
| 255 | |
| 256 | if (width > max.first || height > max.second) |
| 257 | return HWC2::Error::Unsupported; |
| 258 | |
| 259 | if (dataspace != HAL_DATASPACE_UNKNOWN) |
| 260 | return HWC2::Error::Unsupported; |
| 261 | |
| 262 | // TODO(nobody): Validate format can be handled by either GL or planes |
| 263 | return HWC2::Error::None; |
| 264 | } |
| 265 | |
| 266 | HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) { |
| 267 | if (!modes) |
| 268 | *num_modes = 1; |
| 269 | |
| 270 | if (modes) |
| 271 | *modes = HAL_COLOR_MODE_NATIVE; |
| 272 | |
| 273 | return HWC2::Error::None; |
| 274 | } |
| 275 | |
| 276 | HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config, |
| 277 | int32_t attribute_in, |
| 278 | int32_t *value) { |
| 279 | int conf = static_cast<int>(config); |
| 280 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 281 | if (configs_.hwc_configs.count(conf) == 0) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 282 | ALOGE("Could not find active mode for %d", conf); |
| 283 | return HWC2::Error::BadConfig; |
| 284 | } |
| 285 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 286 | auto &hwc_config = configs_.hwc_configs[conf]; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 287 | |
| 288 | static const int32_t kUmPerInch = 25400; |
| 289 | uint32_t mm_width = connector_->mm_width(); |
| 290 | uint32_t mm_height = connector_->mm_height(); |
| 291 | auto attribute = static_cast<HWC2::Attribute>(attribute_in); |
| 292 | switch (attribute) { |
| 293 | case HWC2::Attribute::Width: |
| 294 | *value = static_cast<int>(hwc_config.mode.h_display()); |
| 295 | break; |
| 296 | case HWC2::Attribute::Height: |
| 297 | *value = static_cast<int>(hwc_config.mode.v_display()); |
| 298 | break; |
| 299 | case HWC2::Attribute::VsyncPeriod: |
| 300 | // in nanoseconds |
| 301 | *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh()); |
| 302 | break; |
| 303 | case HWC2::Attribute::DpiX: |
| 304 | // Dots per 1000 inches |
| 305 | *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() * |
| 306 | kUmPerInch / mm_width) |
| 307 | : -1; |
| 308 | break; |
| 309 | case HWC2::Attribute::DpiY: |
| 310 | // Dots per 1000 inches |
| 311 | *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() * |
| 312 | kUmPerInch / mm_height) |
| 313 | : -1; |
| 314 | break; |
| 315 | #if PLATFORM_SDK_VERSION > 29 |
| 316 | case HWC2::Attribute::ConfigGroup: |
| 317 | /* Dispite ConfigGroup is a part of HWC2.4 API, framework |
| 318 | * able to request it even if service @2.1 is used */ |
| 319 | *value = hwc_config.group_id; |
| 320 | break; |
| 321 | #endif |
| 322 | default: |
| 323 | *value = -1; |
| 324 | return HWC2::Error::BadConfig; |
| 325 | } |
| 326 | return HWC2::Error::None; |
| 327 | } |
| 328 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 329 | HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs, |
| 330 | hwc2_config_t *configs) { |
| 331 | // Since this callback is normally invoked twice (once to get the count, and |
| 332 | // once to populate configs), we don't really want to read the edid |
| 333 | // redundantly. Instead, only update the modes on the first invocation. While |
| 334 | // it's possible this will result in stale modes, it'll all come out in the |
| 335 | // wash when we try to set the active config later. |
| 336 | if (!configs) { |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 337 | configs_.Update(*connector_); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | uint32_t idx = 0; |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 341 | for (auto &hwc_config : configs_.hwc_configs) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 342 | if (hwc_config.second.disabled) { |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | if (configs != nullptr) { |
| 347 | if (idx >= *num_configs) { |
| 348 | break; |
| 349 | } |
| 350 | configs[idx] = hwc_config.second.id; |
| 351 | } |
| 352 | |
| 353 | idx++; |
| 354 | } |
| 355 | *num_configs = idx; |
| 356 | return HWC2::Error::None; |
| 357 | } |
| 358 | |
| 359 | HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) { |
| 360 | std::ostringstream stream; |
| 361 | stream << "display-" << connector_->id(); |
| 362 | std::string string = stream.str(); |
| 363 | size_t length = string.length(); |
| 364 | if (!name) { |
| 365 | *size = length; |
| 366 | return HWC2::Error::None; |
| 367 | } |
| 368 | |
| 369 | *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size); |
| 370 | strncpy(name, string.c_str(), *size); |
| 371 | return HWC2::Error::None; |
| 372 | } |
| 373 | |
| 374 | HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/, |
| 375 | uint32_t *num_elements, |
| 376 | hwc2_layer_t * /*layers*/, |
| 377 | int32_t * /*layer_requests*/) { |
| 378 | // TODO(nobody): I think virtual display should request |
| 379 | // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here |
| 380 | *num_elements = 0; |
| 381 | return HWC2::Error::None; |
| 382 | } |
| 383 | |
| 384 | HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) { |
| 385 | *type = static_cast<int32_t>(type_); |
| 386 | return HWC2::Error::None; |
| 387 | } |
| 388 | |
| 389 | HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) { |
| 390 | *support = 0; |
| 391 | return HWC2::Error::None; |
| 392 | } |
| 393 | |
| 394 | HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, |
| 395 | int32_t * /*types*/, |
| 396 | float * /*max_luminance*/, |
| 397 | float * /*max_average_luminance*/, |
| 398 | float * /*min_luminance*/) { |
| 399 | *num_types = 0; |
| 400 | return HWC2::Error::None; |
| 401 | } |
| 402 | |
| 403 | /* Find API details at: |
| 404 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767 |
| 405 | */ |
| 406 | HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements, |
| 407 | hwc2_layer_t *layers, |
| 408 | int32_t *fences) { |
| 409 | uint32_t num_layers = 0; |
| 410 | |
| 411 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 412 | ++num_layers; |
| 413 | if (layers == nullptr || fences == nullptr) |
| 414 | continue; |
| 415 | |
| 416 | if (num_layers > *num_elements) { |
| 417 | ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements); |
| 418 | return HWC2::Error::None; |
| 419 | } |
| 420 | |
| 421 | layers[num_layers - 1] = l.first; |
| 422 | fences[num_layers - 1] = l.second.GetReleaseFence().Release(); |
| 423 | } |
| 424 | *num_elements = num_layers; |
| 425 | return HWC2::Error::None; |
| 426 | } |
| 427 | |
| 428 | HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) { |
| 429 | // order the layers by z-order |
| 430 | bool use_client_layer = false; |
| 431 | uint32_t client_z_order = UINT32_MAX; |
| 432 | std::map<uint32_t, HwcLayer *> z_map; |
| 433 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 434 | switch (l.second.GetValidatedType()) { |
| 435 | case HWC2::Composition::Device: |
| 436 | z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second)); |
| 437 | break; |
| 438 | case HWC2::Composition::Client: |
| 439 | // Place it at the z_order of the lowest client layer |
| 440 | use_client_layer = true; |
| 441 | client_z_order = std::min(client_z_order, l.second.GetZOrder()); |
| 442 | break; |
| 443 | default: |
| 444 | continue; |
| 445 | } |
| 446 | } |
| 447 | if (use_client_layer) |
| 448 | z_map.emplace(std::make_pair(client_z_order, &client_layer_)); |
| 449 | |
| 450 | if (z_map.empty()) |
| 451 | return HWC2::Error::BadLayer; |
| 452 | |
| 453 | std::vector<DrmHwcLayer> composition_layers; |
| 454 | |
| 455 | // now that they're ordered by z, add them to the composition |
| 456 | for (std::pair<const uint32_t, HwcLayer *> &l : z_map) { |
| 457 | DrmHwcLayer layer; |
| 458 | l.second->PopulateDrmLayer(&layer); |
| 459 | int ret = layer.ImportBuffer(drm_); |
| 460 | if (ret) { |
| 461 | ALOGE("Failed to import layer, ret=%d", ret); |
| 462 | return HWC2::Error::NoResources; |
| 463 | } |
| 464 | composition_layers.emplace_back(std::move(layer)); |
| 465 | } |
| 466 | |
| 467 | auto composition = std::make_shared<DrmDisplayComposition>(crtc_); |
| 468 | |
| 469 | // TODO(nobody): Don't always assume geometry changed |
| 470 | int ret = composition->SetLayers(composition_layers.data(), |
| 471 | composition_layers.size()); |
| 472 | if (ret) { |
| 473 | ALOGE("Failed to set layers in the composition ret=%d", ret); |
| 474 | return HWC2::Error::BadLayer; |
| 475 | } |
| 476 | |
| 477 | std::vector<DrmPlane *> primary_planes(primary_planes_); |
| 478 | std::vector<DrmPlane *> overlay_planes(overlay_planes_); |
| 479 | ret = composition->Plan(&primary_planes, &overlay_planes); |
| 480 | if (ret) { |
| 481 | ALOGV("Failed to plan the composition ret=%d", ret); |
| 482 | return HWC2::Error::BadConfig; |
| 483 | } |
| 484 | |
| 485 | a_args.composition = composition; |
| 486 | if (staged_mode) { |
| 487 | a_args.display_mode = *staged_mode; |
| 488 | } |
| 489 | ret = compositor_.ExecuteAtomicCommit(a_args); |
| 490 | |
| 491 | if (ret) { |
| 492 | if (!a_args.test_only) |
| 493 | ALOGE("Failed to apply the frame composition ret=%d", ret); |
| 494 | return HWC2::Error::BadParameter; |
| 495 | } |
| 496 | |
| 497 | if (!a_args.test_only) { |
| 498 | staged_mode.reset(); |
| 499 | } |
| 500 | |
| 501 | return HWC2::Error::None; |
| 502 | } |
| 503 | |
| 504 | /* Find API details at: |
| 505 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805 |
| 506 | */ |
| 507 | HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) { |
| 508 | HWC2::Error ret; |
| 509 | |
| 510 | ++total_stats_.total_frames_; |
| 511 | |
| 512 | AtomicCommitArgs a_args{}; |
| 513 | ret = CreateComposition(a_args); |
| 514 | |
| 515 | if (ret != HWC2::Error::None) |
| 516 | ++total_stats_.failed_kms_present_; |
| 517 | |
| 518 | if (ret == HWC2::Error::BadLayer) { |
| 519 | // Can we really have no client or device layers? |
| 520 | *present_fence = -1; |
| 521 | return HWC2::Error::None; |
| 522 | } |
| 523 | if (ret != HWC2::Error::None) |
| 524 | return ret; |
| 525 | |
| 526 | *present_fence = a_args.out_fence.Release(); |
| 527 | |
| 528 | ++frame_no_; |
| 529 | return HWC2::Error::None; |
| 530 | } |
| 531 | |
| 532 | HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) { |
| 533 | int conf = static_cast<int>(config); |
| 534 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 535 | if (configs_.hwc_configs.count(conf) == 0) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 536 | ALOGE("Could not find active mode for %d", conf); |
| 537 | return HWC2::Error::BadConfig; |
| 538 | } |
| 539 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 540 | auto &mode = configs_.hwc_configs[conf].mode; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 541 | |
| 542 | staged_mode = mode; |
| 543 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 544 | configs_.active_config_id = conf; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 545 | |
| 546 | // Setup the client layer's dimensions |
| 547 | hwc_rect_t display_frame = {.left = 0, |
| 548 | .top = 0, |
| 549 | .right = static_cast<int>(mode.h_display()), |
| 550 | .bottom = static_cast<int>(mode.v_display())}; |
| 551 | client_layer_.SetLayerDisplayFrame(display_frame); |
| 552 | |
| 553 | return HWC2::Error::None; |
| 554 | } |
| 555 | |
| 556 | /* Find API details at: |
| 557 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861 |
| 558 | */ |
| 559 | HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target, |
| 560 | int32_t acquire_fence, |
| 561 | int32_t dataspace, |
| 562 | hwc_region_t /*damage*/) { |
| 563 | client_layer_.SetLayerBuffer(target, acquire_fence); |
| 564 | client_layer_.SetLayerDataspace(dataspace); |
| 565 | |
| 566 | /* |
| 567 | * target can be nullptr, this does mean the Composer Service is calling |
| 568 | * cleanDisplayResources() on after receiving HOTPLUG event. See more at: |
| 569 | * 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 |
| 570 | */ |
| 571 | if (target == nullptr) { |
| 572 | return HWC2::Error::None; |
| 573 | } |
| 574 | |
| 575 | /* TODO: Do not update source_crop every call. |
| 576 | * It makes sense to do it once after every hotplug event. */ |
| 577 | HwcDrmBo bo{}; |
| 578 | BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo); |
| 579 | |
| 580 | hwc_frect_t source_crop = {.left = 0.0F, |
| 581 | .top = 0.0F, |
| 582 | .right = static_cast<float>(bo.width), |
| 583 | .bottom = static_cast<float>(bo.height)}; |
| 584 | client_layer_.SetLayerSourceCrop(source_crop); |
| 585 | |
| 586 | return HWC2::Error::None; |
| 587 | } |
| 588 | |
| 589 | HWC2::Error HwcDisplay::SetColorMode(int32_t mode) { |
| 590 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 591 | return HWC2::Error::BadParameter; |
| 592 | |
| 593 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 594 | return HWC2::Error::Unsupported; |
| 595 | |
| 596 | color_mode_ = mode; |
| 597 | return HWC2::Error::None; |
| 598 | } |
| 599 | |
| 600 | HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) { |
| 601 | if (hint < HAL_COLOR_TRANSFORM_IDENTITY || |
| 602 | hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA) |
| 603 | return HWC2::Error::BadParameter; |
| 604 | |
| 605 | if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 606 | return HWC2::Error::BadParameter; |
| 607 | |
| 608 | color_transform_hint_ = static_cast<android_color_transform_t>(hint); |
| 609 | if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 610 | std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin()); |
| 611 | |
| 612 | return HWC2::Error::None; |
| 613 | } |
| 614 | |
| 615 | HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/, |
| 616 | int32_t /*release_fence*/) { |
| 617 | // TODO(nobody): Need virtual display support |
| 618 | return HWC2::Error::Unsupported; |
| 619 | } |
| 620 | |
| 621 | HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) { |
| 622 | auto mode = static_cast<HWC2::PowerMode>(mode_in); |
| 623 | AtomicCommitArgs a_args{}; |
| 624 | |
| 625 | switch (mode) { |
| 626 | case HWC2::PowerMode::Off: |
| 627 | a_args.active = false; |
| 628 | break; |
| 629 | case HWC2::PowerMode::On: |
| 630 | /* |
| 631 | * Setting the display to active before we have a composition |
| 632 | * can break some drivers, so skip setting a_args.active to |
| 633 | * true, as the next composition frame will implicitly activate |
| 634 | * the display |
| 635 | */ |
Roman Stratiienko | d37b308 | 2022-01-13 16:37:27 +0200 | [diff] [blame] | 636 | return compositor_.ActivateDisplayUsingDPMS() == 0 |
| 637 | ? HWC2::Error::None |
| 638 | : HWC2::Error::BadParameter; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 639 | break; |
| 640 | case HWC2::PowerMode::Doze: |
| 641 | case HWC2::PowerMode::DozeSuspend: |
| 642 | return HWC2::Error::Unsupported; |
| 643 | default: |
| 644 | ALOGI("Power mode %d is unsupported\n", mode); |
| 645 | return HWC2::Error::BadParameter; |
| 646 | }; |
| 647 | |
| 648 | int err = compositor_.ExecuteAtomicCommit(a_args); |
| 649 | if (err) { |
| 650 | ALOGE("Failed to apply the dpms composition err=%d", err); |
| 651 | return HWC2::Error::BadParameter; |
| 652 | } |
| 653 | return HWC2::Error::None; |
| 654 | } |
| 655 | |
| 656 | HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) { |
| 657 | vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled); |
| 658 | return HWC2::Error::None; |
| 659 | } |
| 660 | |
| 661 | HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types, |
| 662 | uint32_t *num_requests) { |
| 663 | return backend_->ValidateDisplay(this, num_types, num_requests); |
| 664 | } |
| 665 | |
| 666 | std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() { |
| 667 | std::vector<HwcLayer *> ordered_layers; |
| 668 | ordered_layers.reserve(layers_.size()); |
| 669 | |
| 670 | for (auto &[handle, layer] : layers_) { |
| 671 | ordered_layers.emplace_back(&layer); |
| 672 | } |
| 673 | |
| 674 | std::sort(std::begin(ordered_layers), std::end(ordered_layers), |
| 675 | [](const HwcLayer *lhs, const HwcLayer *rhs) { |
| 676 | return lhs->GetZOrder() < rhs->GetZOrder(); |
| 677 | }); |
| 678 | |
| 679 | return ordered_layers; |
| 680 | } |
| 681 | |
| 682 | #if PLATFORM_SDK_VERSION > 29 |
| 683 | HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) { |
| 684 | if (connector_->internal()) |
| 685 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal); |
| 686 | else if (connector_->external()) |
| 687 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External); |
| 688 | else |
| 689 | return HWC2::Error::BadConfig; |
| 690 | |
| 691 | return HWC2::Error::None; |
| 692 | } |
| 693 | |
| 694 | HWC2::Error HwcDisplay::GetDisplayVsyncPeriod( |
| 695 | hwc2_vsync_period_t *outVsyncPeriod /* ns */) { |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 696 | return GetDisplayAttribute(configs_.active_config_id, |
| 697 | HWC2_ATTRIBUTE_VSYNC_PERIOD, |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 698 | (int32_t *)(outVsyncPeriod)); |
| 699 | } |
| 700 | |
| 701 | HWC2::Error HwcDisplay::SetActiveConfigWithConstraints( |
| 702 | hwc2_config_t /*config*/, |
| 703 | hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints, |
| 704 | hwc_vsync_period_change_timeline_t *outTimeline) { |
| 705 | if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) { |
| 706 | return HWC2::Error::BadParameter; |
| 707 | } |
| 708 | |
| 709 | return HWC2::Error::BadConfig; |
| 710 | } |
| 711 | |
| 712 | HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) { |
| 713 | return HWC2::Error::Unsupported; |
| 714 | } |
| 715 | |
| 716 | HWC2::Error HwcDisplay::GetSupportedContentTypes( |
| 717 | uint32_t *outNumSupportedContentTypes, |
| 718 | const uint32_t *outSupportedContentTypes) { |
| 719 | if (outSupportedContentTypes == nullptr) |
| 720 | *outNumSupportedContentTypes = 0; |
| 721 | |
| 722 | return HWC2::Error::None; |
| 723 | } |
| 724 | |
| 725 | HWC2::Error HwcDisplay::SetContentType(int32_t contentType) { |
| 726 | if (contentType != HWC2_CONTENT_TYPE_NONE) |
| 727 | return HWC2::Error::Unsupported; |
| 728 | |
| 729 | /* TODO: Map to the DRM Connector property: |
| 730 | * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809 |
| 731 | */ |
| 732 | |
| 733 | return HWC2::Error::None; |
| 734 | } |
| 735 | #endif |
| 736 | |
| 737 | #if PLATFORM_SDK_VERSION > 28 |
| 738 | HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort, |
| 739 | uint32_t *outDataSize, |
| 740 | uint8_t *outData) { |
| 741 | auto blob = connector_->GetEdidBlob(); |
| 742 | |
| 743 | if (!blob) { |
| 744 | ALOGE("Failed to get edid property value."); |
| 745 | return HWC2::Error::Unsupported; |
| 746 | } |
| 747 | |
| 748 | if (outData) { |
| 749 | *outDataSize = std::min(*outDataSize, blob->length); |
| 750 | memcpy(outData, blob->data, *outDataSize); |
| 751 | } else { |
| 752 | *outDataSize = blob->length; |
| 753 | } |
| 754 | *outPort = connector_->id(); |
| 755 | |
| 756 | return HWC2::Error::None; |
| 757 | } |
| 758 | |
| 759 | HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities, |
| 760 | uint32_t * /*outCapabilities*/) { |
| 761 | if (outNumCapabilities == nullptr) { |
| 762 | return HWC2::Error::BadParameter; |
| 763 | } |
| 764 | |
| 765 | *outNumCapabilities = 0; |
| 766 | |
| 767 | return HWC2::Error::None; |
| 768 | } |
| 769 | |
| 770 | HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) { |
| 771 | *supported = false; |
| 772 | return HWC2::Error::None; |
| 773 | } |
| 774 | |
| 775 | HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) { |
| 776 | return HWC2::Error::Unsupported; |
| 777 | } |
| 778 | |
| 779 | #endif /* PLATFORM_SDK_VERSION > 28 */ |
| 780 | |
| 781 | #if PLATFORM_SDK_VERSION > 27 |
| 782 | |
| 783 | HWC2::Error HwcDisplay::GetRenderIntents( |
| 784 | int32_t mode, uint32_t *outNumIntents, |
| 785 | int32_t * /*android_render_intent_v1_1_t*/ outIntents) { |
| 786 | if (mode != HAL_COLOR_MODE_NATIVE) { |
| 787 | return HWC2::Error::BadParameter; |
| 788 | } |
| 789 | |
| 790 | if (outIntents == nullptr) { |
| 791 | *outNumIntents = 1; |
| 792 | return HWC2::Error::None; |
| 793 | } |
| 794 | *outNumIntents = 1; |
| 795 | outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC; |
| 796 | return HWC2::Error::None; |
| 797 | } |
| 798 | |
| 799 | HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) { |
| 800 | if (intent < HAL_RENDER_INTENT_COLORIMETRIC || |
| 801 | intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) |
| 802 | return HWC2::Error::BadParameter; |
| 803 | |
| 804 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 805 | return HWC2::Error::BadParameter; |
| 806 | |
| 807 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 808 | return HWC2::Error::Unsupported; |
| 809 | |
| 810 | if (intent != HAL_RENDER_INTENT_COLORIMETRIC) |
| 811 | return HWC2::Error::Unsupported; |
| 812 | |
| 813 | color_mode_ = mode; |
| 814 | return HWC2::Error::None; |
| 815 | } |
| 816 | |
| 817 | #endif /* PLATFORM_SDK_VERSION > 27 */ |
| 818 | |
| 819 | const Backend *HwcDisplay::backend() const { |
| 820 | return backend_.get(); |
| 821 | } |
| 822 | |
| 823 | void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) { |
| 824 | backend_ = std::move(backend); |
| 825 | } |
| 826 | |
| 827 | } // namespace android |