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" |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 23 | #include "backend/Backend.h" |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 24 | #include "backend/BackendManager.h" |
| 25 | #include "bufferinfo/BufferInfoGetter.h" |
| 26 | #include "utils/log.h" |
| 27 | #include "utils/properties.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) { |
| 32 | if (delta.total_pixops_ == 0) |
| 33 | return "No stats yet"; |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 34 | auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 35 | |
| 36 | std::stringstream ss; |
| 37 | ss << " Total frames count: " << delta.total_frames_ << "\n" |
| 38 | << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n" |
| 39 | << " Failed to commit frames: " << delta.failed_kms_present_ << "\n" |
| 40 | << ((delta.failed_kms_present_ > 0) |
| 41 | ? " !!! Internal failure, FIX it please\n" |
| 42 | : "") |
| 43 | << " Flattened frames: " << delta.frames_flattened_ << "\n" |
| 44 | << " Pixel operations (free units)" |
| 45 | << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_ |
| 46 | << "]\n" |
| 47 | << " Composition efficiency: " << ratio; |
| 48 | |
| 49 | return ss.str(); |
| 50 | } |
| 51 | |
| 52 | std::string HwcDisplay::Dump() { |
| 53 | std::string flattening_state_str; |
| 54 | switch (flattenning_state_) { |
| 55 | case ClientFlattenningState::Disabled: |
| 56 | flattening_state_str = "Disabled"; |
| 57 | break; |
| 58 | case ClientFlattenningState::NotRequired: |
| 59 | flattening_state_str = "Not needed"; |
| 60 | break; |
| 61 | case ClientFlattenningState::Flattened: |
| 62 | flattening_state_str = "Active"; |
| 63 | break; |
| 64 | case ClientFlattenningState::ClientRefreshRequested: |
| 65 | flattening_state_str = "Refresh requested"; |
| 66 | break; |
| 67 | default: |
| 68 | flattening_state_str = std::to_string(flattenning_state_) + |
| 69 | " VSync remains"; |
| 70 | } |
| 71 | |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 72 | auto connector_name = IsInHeadlessMode() |
| 73 | ? std::string("NULL-DISPLAY") |
| 74 | : GetPipe().connector->Get()->GetName(); |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 75 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 76 | std::stringstream ss; |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 77 | ss << "- Display on: " << connector_name << "\n" |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 78 | << " Flattening state: " << flattening_state_str << "\n" |
| 79 | << "Statistics since system boot:\n" |
| 80 | << DumpDelta(total_stats_) << "\n\n" |
| 81 | << "Statistics since last dumpsys request:\n" |
| 82 | << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n"; |
| 83 | |
| 84 | memcpy(&prev_stats_, &total_stats_, sizeof(Stats)); |
| 85 | return ss.str(); |
| 86 | } |
| 87 | |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 88 | HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, |
| 89 | DrmHwcTwo *hwc2) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 90 | : hwc2_(hwc2), |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 91 | handle_(handle), |
| 92 | type_(type), |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 93 | client_layer_(this), |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 94 | color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) { |
| 95 | // clang-format off |
| 96 | color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0, |
| 97 | 0.0, 1.0, 0.0, 0.0, |
| 98 | 0.0, 0.0, 1.0, 0.0, |
| 99 | 0.0, 0.0, 0.0, 1.0}; |
| 100 | // clang-format on |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 103 | HwcDisplay::~HwcDisplay() = default; |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 104 | |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 105 | void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) { |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 106 | Deinit(); |
| 107 | |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 108 | pipeline_ = pipeline; |
| 109 | |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 110 | if (pipeline != nullptr || handle_ == kPrimaryDisplay) { |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 111 | Init(); |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 112 | hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true); |
| 113 | } else { |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 114 | hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false); |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 115 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 118 | void HwcDisplay::Deinit() { |
| 119 | if (pipeline_ != nullptr) { |
| 120 | AtomicCommitArgs a_args{}; |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 121 | a_args.composition = std::make_shared<DrmKmsPlan>(); |
| 122 | GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args); |
John Stultz | 799e8c7 | 2022-06-30 19:49:42 +0000 | [diff] [blame] | 123 | /* |
| 124 | * TODO: |
| 125 | * Unfortunately the following causes regressions on db845c |
| 126 | * with VtsHalGraphicsComposerV2_3TargetTest due to the display |
| 127 | * never coming back. Patches to avoiding that issue on the |
| 128 | * the kernel side unfortunately causes further crashes in |
| 129 | * drm_hwcomposer, because the client detach takes longer then the |
| 130 | * 1 second max VTS expects. So for now as a workaround, lets skip |
| 131 | * deactivating the display on deinit, which matches previous |
| 132 | * behavior prior to commit d0494d9b8097 |
| 133 | */ |
| 134 | #if 0 |
Roman Stratiienko | af862a5 | 2022-06-22 12:14:22 +0300 | [diff] [blame] | 135 | a_args.composition = {}; |
| 136 | a_args.active = false; |
| 137 | GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args); |
John Stultz | 799e8c7 | 2022-06-30 19:49:42 +0000 | [diff] [blame] | 138 | #endif |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 139 | |
| 140 | vsync_worker_.Init(nullptr, [](int64_t) {}); |
| 141 | current_plan_.reset(); |
| 142 | backend_.reset(); |
| 143 | } |
| 144 | |
| 145 | SetClientTarget(nullptr, -1, 0, {}); |
| 146 | } |
| 147 | |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 148 | HWC2::Error HwcDisplay::Init() { |
Roman Stratiienko | d0494d9 | 2022-03-15 18:02:04 +0200 | [diff] [blame] | 149 | ChosePreferredConfig(); |
| 150 | |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 151 | int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) { |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame] | 152 | const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock()); |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 153 | if (vsync_event_en_) { |
| 154 | uint32_t period_ns{}; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 155 | GetDisplayVsyncPeriod(&period_ns); |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 156 | hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 157 | } |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 158 | if (vsync_flattening_en_) { |
| 159 | ProcessFlatenningVsyncInternal(); |
| 160 | } |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 161 | if (vsync_tracking_en_) { |
| 162 | last_vsync_ts_ = timestamp; |
| 163 | } |
| 164 | if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) { |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 165 | vsync_worker_.VSyncControl(false); |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 166 | } |
| 167 | }); |
Roman Stratiienko | bb594ba | 2022-02-18 16:52:03 +0200 | [diff] [blame] | 168 | if (ret && ret != -EALREADY) { |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 169 | ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 170 | return HWC2::Error::BadDisplay; |
| 171 | } |
| 172 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 173 | if (!IsInHeadlessMode()) { |
| 174 | ret = BackendManager::GetInstance().SetBackendForDisplay(this); |
| 175 | if (ret) { |
| 176 | ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret); |
| 177 | return HWC2::Error::BadDisplay; |
| 178 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED); |
| 182 | |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 183 | return HWC2::Error::None; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | HWC2::Error HwcDisplay::ChosePreferredConfig() { |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 187 | HWC2::Error err{}; |
| 188 | if (!IsInHeadlessMode()) { |
| 189 | err = configs_.Update(*pipeline_->connector->Get()); |
| 190 | } else { |
| 191 | configs_.FillHeadless(); |
| 192 | } |
| 193 | if (!IsInHeadlessMode() && err != HWC2::Error::None) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 194 | return HWC2::Error::BadDisplay; |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 195 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 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) { |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 207 | layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this)); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 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) { |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 214 | if (!get_layer(layer)) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 215 | return HWC2::Error::BadLayer; |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 216 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 217 | |
| 218 | layers_.erase(layer); |
| 219 | return HWC2::Error::None; |
| 220 | } |
| 221 | |
| 222 | HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const { |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 223 | if (configs_.hwc_configs.count(staged_mode_config_id_) == 0) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 224 | return HWC2::Error::BadConfig; |
| 225 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 226 | *config = staged_mode_config_id_; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 227 | return HWC2::Error::None; |
| 228 | } |
| 229 | |
| 230 | HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements, |
| 231 | hwc2_layer_t *layers, |
| 232 | int32_t *types) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 233 | if (IsInHeadlessMode()) { |
| 234 | *num_elements = 0; |
| 235 | return HWC2::Error::None; |
| 236 | } |
| 237 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 238 | uint32_t num_changes = 0; |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 239 | for (auto &l : layers_) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 240 | if (l.second.IsTypeChanged()) { |
| 241 | if (layers && num_changes < *num_elements) |
| 242 | layers[num_changes] = l.first; |
| 243 | if (types && num_changes < *num_elements) |
| 244 | types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType()); |
| 245 | ++num_changes; |
| 246 | } |
| 247 | } |
| 248 | if (!layers && !types) |
| 249 | *num_elements = num_changes; |
| 250 | return HWC2::Error::None; |
| 251 | } |
| 252 | |
| 253 | HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height, |
| 254 | int32_t /*format*/, |
| 255 | int32_t dataspace) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 256 | if (IsInHeadlessMode()) { |
| 257 | return HWC2::Error::None; |
| 258 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 259 | |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 260 | auto min = pipeline_->device->GetMinResolution(); |
| 261 | auto max = pipeline_->device->GetMaxResolution(); |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 262 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 263 | if (width < min.first || height < min.second) |
| 264 | return HWC2::Error::Unsupported; |
| 265 | |
| 266 | if (width > max.first || height > max.second) |
| 267 | return HWC2::Error::Unsupported; |
| 268 | |
| 269 | if (dataspace != HAL_DATASPACE_UNKNOWN) |
| 270 | return HWC2::Error::Unsupported; |
| 271 | |
| 272 | // TODO(nobody): Validate format can be handled by either GL or planes |
| 273 | return HWC2::Error::None; |
| 274 | } |
| 275 | |
| 276 | HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) { |
| 277 | if (!modes) |
| 278 | *num_modes = 1; |
| 279 | |
| 280 | if (modes) |
| 281 | *modes = HAL_COLOR_MODE_NATIVE; |
| 282 | |
| 283 | return HWC2::Error::None; |
| 284 | } |
| 285 | |
| 286 | HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config, |
| 287 | int32_t attribute_in, |
| 288 | int32_t *value) { |
| 289 | int conf = static_cast<int>(config); |
| 290 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 291 | if (configs_.hwc_configs.count(conf) == 0) { |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 292 | ALOGE("Could not find mode #%d", conf); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 293 | return HWC2::Error::BadConfig; |
| 294 | } |
| 295 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 296 | auto &hwc_config = configs_.hwc_configs[conf]; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 297 | |
| 298 | static const int32_t kUmPerInch = 25400; |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 299 | auto mm_width = configs_.mm_width; |
| 300 | auto mm_height = configs_.mm_height; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 301 | auto attribute = static_cast<HWC2::Attribute>(attribute_in); |
| 302 | switch (attribute) { |
| 303 | case HWC2::Attribute::Width: |
| 304 | *value = static_cast<int>(hwc_config.mode.h_display()); |
| 305 | break; |
| 306 | case HWC2::Attribute::Height: |
| 307 | *value = static_cast<int>(hwc_config.mode.v_display()); |
| 308 | break; |
| 309 | case HWC2::Attribute::VsyncPeriod: |
| 310 | // in nanoseconds |
| 311 | *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh()); |
| 312 | break; |
| 313 | case HWC2::Attribute::DpiX: |
| 314 | // Dots per 1000 inches |
| 315 | *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() * |
| 316 | kUmPerInch / mm_width) |
| 317 | : -1; |
| 318 | break; |
| 319 | case HWC2::Attribute::DpiY: |
| 320 | // Dots per 1000 inches |
| 321 | *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() * |
| 322 | kUmPerInch / mm_height) |
| 323 | : -1; |
| 324 | break; |
| 325 | #if PLATFORM_SDK_VERSION > 29 |
| 326 | case HWC2::Attribute::ConfigGroup: |
| 327 | /* Dispite ConfigGroup is a part of HWC2.4 API, framework |
| 328 | * able to request it even if service @2.1 is used */ |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 329 | *value = int(hwc_config.group_id); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 330 | break; |
| 331 | #endif |
| 332 | default: |
| 333 | *value = -1; |
| 334 | return HWC2::Error::BadConfig; |
| 335 | } |
| 336 | return HWC2::Error::None; |
| 337 | } |
| 338 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 339 | HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs, |
| 340 | hwc2_config_t *configs) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 341 | uint32_t idx = 0; |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 342 | for (auto &hwc_config : configs_.hwc_configs) { |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 343 | if (hwc_config.second.disabled) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | if (configs != nullptr) { |
| 348 | if (idx >= *num_configs) { |
| 349 | break; |
| 350 | } |
| 351 | configs[idx] = hwc_config.second.id; |
| 352 | } |
| 353 | |
| 354 | idx++; |
| 355 | } |
| 356 | *num_configs = idx; |
| 357 | return HWC2::Error::None; |
| 358 | } |
| 359 | |
| 360 | HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) { |
| 361 | std::ostringstream stream; |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 362 | if (IsInHeadlessMode()) { |
| 363 | stream << "null-display"; |
| 364 | } else { |
| 365 | stream << "display-" << GetPipe().connector->Get()->GetId(); |
| 366 | } |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 367 | auto string = stream.str(); |
| 368 | auto length = string.length(); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 369 | if (!name) { |
| 370 | *size = length; |
| 371 | return HWC2::Error::None; |
| 372 | } |
| 373 | |
| 374 | *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size); |
| 375 | strncpy(name, string.c_str(), *size); |
| 376 | return HWC2::Error::None; |
| 377 | } |
| 378 | |
| 379 | HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/, |
| 380 | uint32_t *num_elements, |
| 381 | hwc2_layer_t * /*layers*/, |
| 382 | int32_t * /*layer_requests*/) { |
| 383 | // TODO(nobody): I think virtual display should request |
| 384 | // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here |
| 385 | *num_elements = 0; |
| 386 | return HWC2::Error::None; |
| 387 | } |
| 388 | |
| 389 | HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) { |
| 390 | *type = static_cast<int32_t>(type_); |
| 391 | return HWC2::Error::None; |
| 392 | } |
| 393 | |
| 394 | HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) { |
| 395 | *support = 0; |
| 396 | return HWC2::Error::None; |
| 397 | } |
| 398 | |
| 399 | HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, |
| 400 | int32_t * /*types*/, |
| 401 | float * /*max_luminance*/, |
| 402 | float * /*max_average_luminance*/, |
| 403 | float * /*min_luminance*/) { |
| 404 | *num_types = 0; |
| 405 | return HWC2::Error::None; |
| 406 | } |
| 407 | |
| 408 | /* Find API details at: |
| 409 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767 |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 410 | * |
| 411 | * Called after PresentDisplay(), CLIENT is expecting release fence for the |
| 412 | * prior buffer (not the one assigned to the layer at the moment). |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 413 | */ |
| 414 | HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements, |
| 415 | hwc2_layer_t *layers, |
| 416 | int32_t *fences) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 417 | if (IsInHeadlessMode()) { |
| 418 | *num_elements = 0; |
| 419 | return HWC2::Error::None; |
| 420 | } |
| 421 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 422 | uint32_t num_layers = 0; |
| 423 | |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 424 | for (auto &l : layers_) { |
| 425 | if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) { |
| 426 | continue; |
| 427 | } |
| 428 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 429 | ++num_layers; |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 430 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 431 | if (layers == nullptr || fences == nullptr) |
| 432 | continue; |
| 433 | |
| 434 | if (num_layers > *num_elements) { |
| 435 | ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements); |
| 436 | return HWC2::Error::None; |
| 437 | } |
| 438 | |
| 439 | layers[num_layers - 1] = l.first; |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 440 | fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release(); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 441 | } |
| 442 | *num_elements = num_layers; |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 443 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 444 | return HWC2::Error::None; |
| 445 | } |
| 446 | |
| 447 | HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 448 | if (IsInHeadlessMode()) { |
| 449 | ALOGE("%s: Display is in headless mode, should never reach here", __func__); |
| 450 | return HWC2::Error::None; |
| 451 | } |
| 452 | |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 453 | auto PrevModeVsyncPeriodNs = static_cast<int>( |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 454 | 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh()); |
| 455 | |
| 456 | auto mode_update_commited_ = false; |
| 457 | if (staged_mode_ && |
| 458 | staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) { |
| 459 | client_layer_.SetLayerDisplayFrame( |
| 460 | (hwc_rect_t){.left = 0, |
| 461 | .top = 0, |
| 462 | .right = static_cast<int>(staged_mode_->h_display()), |
| 463 | .bottom = static_cast<int>(staged_mode_->v_display())}); |
| 464 | |
| 465 | configs_.active_config_id = staged_mode_config_id_; |
| 466 | |
| 467 | a_args.display_mode = *staged_mode_; |
| 468 | if (!a_args.test_only) { |
| 469 | mode_update_commited_ = true; |
| 470 | } |
| 471 | } |
| 472 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 473 | // order the layers by z-order |
| 474 | bool use_client_layer = false; |
| 475 | uint32_t client_z_order = UINT32_MAX; |
| 476 | std::map<uint32_t, HwcLayer *> z_map; |
| 477 | for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) { |
| 478 | switch (l.second.GetValidatedType()) { |
| 479 | case HWC2::Composition::Device: |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 480 | z_map.emplace(l.second.GetZOrder(), &l.second); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 481 | break; |
| 482 | case HWC2::Composition::Client: |
| 483 | // Place it at the z_order of the lowest client layer |
| 484 | use_client_layer = true; |
| 485 | client_z_order = std::min(client_z_order, l.second.GetZOrder()); |
| 486 | break; |
| 487 | default: |
| 488 | continue; |
| 489 | } |
| 490 | } |
| 491 | if (use_client_layer) |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 492 | z_map.emplace(client_z_order, &client_layer_); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 493 | |
| 494 | if (z_map.empty()) |
| 495 | return HWC2::Error::BadLayer; |
| 496 | |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 497 | std::vector<LayerData> composition_layers; |
| 498 | |
| 499 | /* Import & populate */ |
| 500 | for (std::pair<const uint32_t, HwcLayer *> &l : z_map) { |
| 501 | l.second->PopulateLayerData(a_args.test_only); |
| 502 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 503 | |
| 504 | // now that they're ordered by z, add them to the composition |
| 505 | for (std::pair<const uint32_t, HwcLayer *> &l : z_map) { |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 506 | if (!l.second->IsLayerUsableAsDevice()) { |
| 507 | /* This will be normally triggered on validation of the first frame |
| 508 | * containing CLIENT layer. At this moment client buffer is not yet |
| 509 | * provided by the CLIENT. |
| 510 | * This may be triggered once in HwcLayer lifecycle in case FB can't be |
| 511 | * imported. For example when non-contiguous buffer is imported into |
| 512 | * contiguous-only DRM/KMS driver. |
| 513 | */ |
| 514 | return HWC2::Error::BadLayer; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 515 | } |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 516 | composition_layers.emplace_back(l.second->GetLayerData().Clone()); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Roman Stratiienko | 9362cef | 2022-02-02 09:53:50 +0200 | [diff] [blame] | 519 | /* Store plan to ensure shared planes won't be stolen by other display |
| 520 | * in between of ValidateDisplay() and PresentDisplay() calls |
| 521 | */ |
| 522 | current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(), |
| 523 | std::move(composition_layers)); |
| 524 | if (!current_plan_) { |
| 525 | if (!a_args.test_only) { |
| 526 | ALOGE("Failed to create DrmKmsPlan"); |
| 527 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 528 | return HWC2::Error::BadConfig; |
| 529 | } |
| 530 | |
Roman Stratiienko | 9362cef | 2022-02-02 09:53:50 +0200 | [diff] [blame] | 531 | a_args.composition = current_plan_; |
| 532 | |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 533 | auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 534 | |
| 535 | if (ret) { |
| 536 | if (!a_args.test_only) |
| 537 | ALOGE("Failed to apply the frame composition ret=%d", ret); |
| 538 | return HWC2::Error::BadParameter; |
| 539 | } |
| 540 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 541 | if (mode_update_commited_) { |
| 542 | staged_mode_.reset(); |
| 543 | vsync_tracking_en_ = false; |
| 544 | if (last_vsync_ts_ != 0) { |
| 545 | hwc2_->SendVsyncPeriodTimingChangedEventToClient( |
| 546 | handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs); |
| 547 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | return HWC2::Error::None; |
| 551 | } |
| 552 | |
| 553 | /* Find API details at: |
| 554 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805 |
| 555 | */ |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 556 | HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 557 | if (IsInHeadlessMode()) { |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 558 | *out_present_fence = -1; |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 559 | return HWC2::Error::None; |
| 560 | } |
Roman Stratiienko | 780f7da | 2022-01-10 16:04:15 +0200 | [diff] [blame] | 561 | HWC2::Error ret{}; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 562 | |
| 563 | ++total_stats_.total_frames_; |
| 564 | |
| 565 | AtomicCommitArgs a_args{}; |
| 566 | ret = CreateComposition(a_args); |
| 567 | |
| 568 | if (ret != HWC2::Error::None) |
| 569 | ++total_stats_.failed_kms_present_; |
| 570 | |
| 571 | if (ret == HWC2::Error::BadLayer) { |
| 572 | // Can we really have no client or device layers? |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 573 | *out_present_fence = -1; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 574 | return HWC2::Error::None; |
| 575 | } |
| 576 | if (ret != HWC2::Error::None) |
| 577 | return ret; |
| 578 | |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 579 | this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get()); |
| 580 | *out_present_fence = a_args.out_fence.Release(); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 581 | |
| 582 | ++frame_no_; |
| 583 | return HWC2::Error::None; |
| 584 | } |
| 585 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 586 | HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config, |
| 587 | int64_t change_time) { |
| 588 | if (configs_.hwc_configs.count(config) == 0) { |
| 589 | ALOGE("Could not find active mode for %u", config); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 590 | return HWC2::Error::BadConfig; |
| 591 | } |
| 592 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 593 | staged_mode_ = configs_.hwc_configs[config].mode; |
| 594 | staged_mode_change_time_ = change_time; |
| 595 | staged_mode_config_id_ = config; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 596 | |
| 597 | return HWC2::Error::None; |
| 598 | } |
| 599 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 600 | HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) { |
| 601 | return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs()); |
| 602 | } |
| 603 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 604 | /* Find API details at: |
| 605 | * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861 |
| 606 | */ |
| 607 | HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target, |
| 608 | int32_t acquire_fence, |
| 609 | int32_t dataspace, |
| 610 | hwc_region_t /*damage*/) { |
| 611 | client_layer_.SetLayerBuffer(target, acquire_fence); |
| 612 | client_layer_.SetLayerDataspace(dataspace); |
| 613 | |
| 614 | /* |
| 615 | * target can be nullptr, this does mean the Composer Service is calling |
| 616 | * cleanDisplayResources() on after receiving HOTPLUG event. See more at: |
| 617 | * 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 |
| 618 | */ |
| 619 | if (target == nullptr) { |
Roman Stratiienko | a32f907 | 2022-05-13 12:12:20 +0300 | [diff] [blame] | 620 | client_layer_.SwChainClearCache(); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 621 | return HWC2::Error::None; |
| 622 | } |
| 623 | |
Roman Stratiienko | 5070d51 | 2022-05-30 13:41:20 +0300 | [diff] [blame] | 624 | if (IsInHeadlessMode()) { |
| 625 | return HWC2::Error::None; |
| 626 | } |
| 627 | |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 628 | client_layer_.PopulateLayerData(/*test = */ true); |
| 629 | if (!client_layer_.IsLayerUsableAsDevice()) { |
| 630 | ALOGE("Client layer must be always usable by DRM/KMS"); |
| 631 | return HWC2::Error::BadLayer; |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame] | 632 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 633 | |
Roman Stratiienko | 4b2cc48 | 2022-02-21 14:53:58 +0200 | [diff] [blame] | 634 | auto &bi = client_layer_.GetLayerData().bi; |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 635 | if (!bi) { |
| 636 | ALOGE("%s: Invalid state", __func__); |
| 637 | return HWC2::Error::BadLayer; |
| 638 | } |
| 639 | |
| 640 | auto source_crop = (hwc_frect_t){.left = 0.0F, |
| 641 | .top = 0.0F, |
| 642 | .right = static_cast<float>(bi->width), |
| 643 | .bottom = static_cast<float>(bi->height)}; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 644 | client_layer_.SetLayerSourceCrop(source_crop); |
| 645 | |
| 646 | return HWC2::Error::None; |
| 647 | } |
| 648 | |
| 649 | HWC2::Error HwcDisplay::SetColorMode(int32_t mode) { |
| 650 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 651 | return HWC2::Error::BadParameter; |
| 652 | |
| 653 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 654 | return HWC2::Error::Unsupported; |
| 655 | |
| 656 | color_mode_ = mode; |
| 657 | return HWC2::Error::None; |
| 658 | } |
| 659 | |
| 660 | HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) { |
| 661 | if (hint < HAL_COLOR_TRANSFORM_IDENTITY || |
| 662 | hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA) |
| 663 | return HWC2::Error::BadParameter; |
| 664 | |
| 665 | if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 666 | return HWC2::Error::BadParameter; |
| 667 | |
| 668 | color_transform_hint_ = static_cast<android_color_transform_t>(hint); |
| 669 | if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX) |
| 670 | std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin()); |
| 671 | |
| 672 | return HWC2::Error::None; |
| 673 | } |
| 674 | |
| 675 | HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/, |
| 676 | int32_t /*release_fence*/) { |
| 677 | // TODO(nobody): Need virtual display support |
| 678 | return HWC2::Error::Unsupported; |
| 679 | } |
| 680 | |
| 681 | HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) { |
| 682 | auto mode = static_cast<HWC2::PowerMode>(mode_in); |
Roman Stratiienko | ccaf516 | 2022-04-01 19:26:30 +0300 | [diff] [blame] | 683 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 684 | AtomicCommitArgs a_args{}; |
| 685 | |
| 686 | switch (mode) { |
| 687 | case HWC2::PowerMode::Off: |
| 688 | a_args.active = false; |
| 689 | break; |
| 690 | case HWC2::PowerMode::On: |
Roman Stratiienko | ccaf516 | 2022-04-01 19:26:30 +0300 | [diff] [blame] | 691 | a_args.active = true; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 692 | break; |
| 693 | case HWC2::PowerMode::Doze: |
| 694 | case HWC2::PowerMode::DozeSuspend: |
| 695 | return HWC2::Error::Unsupported; |
| 696 | default: |
Roman Stratiienko | ccaf516 | 2022-04-01 19:26:30 +0300 | [diff] [blame] | 697 | ALOGE("Incorrect power mode value (%d)\n", mode); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 698 | return HWC2::Error::BadParameter; |
Roman Stratiienko | ccaf516 | 2022-04-01 19:26:30 +0300 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | if (IsInHeadlessMode()) { |
| 702 | return HWC2::Error::None; |
| 703 | } |
| 704 | |
Jia Ren | 80566fe | 2022-11-17 17:26:00 +0800 | [diff] [blame^] | 705 | if (a_args.active && *a_args.active) { |
Roman Stratiienko | ccaf516 | 2022-04-01 19:26:30 +0300 | [diff] [blame] | 706 | /* |
| 707 | * Setting the display to active before we have a composition |
| 708 | * can break some drivers, so skip setting a_args.active to |
| 709 | * true, as the next composition frame will implicitly activate |
| 710 | * the display |
| 711 | */ |
| 712 | return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0 |
| 713 | ? HWC2::Error::None |
| 714 | : HWC2::Error::BadParameter; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 715 | }; |
| 716 | |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 717 | auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 718 | if (err) { |
| 719 | ALOGE("Failed to apply the dpms composition err=%d", err); |
| 720 | return HWC2::Error::BadParameter; |
| 721 | } |
| 722 | return HWC2::Error::None; |
| 723 | } |
| 724 | |
| 725 | HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) { |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 726 | vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled; |
| 727 | if (vsync_event_en_) { |
| 728 | vsync_worker_.VSyncControl(true); |
| 729 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 730 | return HWC2::Error::None; |
| 731 | } |
| 732 | |
| 733 | HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types, |
| 734 | uint32_t *num_requests) { |
Roman Stratiienko | f0c507f | 2022-01-17 18:29:24 +0200 | [diff] [blame] | 735 | if (IsInHeadlessMode()) { |
| 736 | *num_types = *num_requests = 0; |
| 737 | return HWC2::Error::None; |
| 738 | } |
Roman Stratiienko | dd21494 | 2022-05-03 18:24:49 +0300 | [diff] [blame] | 739 | |
| 740 | /* In current drm_hwc design in case previous frame layer was not validated as |
| 741 | * a CLIENT, it is used by display controller (Front buffer). We have to store |
| 742 | * this state to provide the CLIENT with the release fences for such buffers. |
| 743 | */ |
| 744 | for (auto &l : layers_) { |
| 745 | l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() != |
| 746 | HWC2::Composition::Client); |
| 747 | } |
| 748 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 749 | return backend_->ValidateDisplay(this, num_types, num_requests); |
| 750 | } |
| 751 | |
| 752 | std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() { |
| 753 | std::vector<HwcLayer *> ordered_layers; |
| 754 | ordered_layers.reserve(layers_.size()); |
| 755 | |
| 756 | for (auto &[handle, layer] : layers_) { |
| 757 | ordered_layers.emplace_back(&layer); |
| 758 | } |
| 759 | |
| 760 | std::sort(std::begin(ordered_layers), std::end(ordered_layers), |
| 761 | [](const HwcLayer *lhs, const HwcLayer *rhs) { |
| 762 | return lhs->GetZOrder() < rhs->GetZOrder(); |
| 763 | }); |
| 764 | |
| 765 | return ordered_layers; |
| 766 | } |
| 767 | |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 768 | HWC2::Error HwcDisplay::GetDisplayVsyncPeriod( |
| 769 | uint32_t *outVsyncPeriod /* ns */) { |
| 770 | return GetDisplayAttribute(configs_.active_config_id, |
| 771 | HWC2_ATTRIBUTE_VSYNC_PERIOD, |
| 772 | (int32_t *)(outVsyncPeriod)); |
| 773 | } |
| 774 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 775 | #if PLATFORM_SDK_VERSION > 29 |
| 776 | HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) { |
Roman Stratiienko | 456e2d6 | 2022-01-29 01:17:39 +0200 | [diff] [blame] | 777 | if (IsInHeadlessMode()) { |
| 778 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal); |
| 779 | return HWC2::Error::None; |
| 780 | } |
| 781 | /* Primary display should be always internal, |
| 782 | * otherwise SF will be unhappy and will crash |
| 783 | */ |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 784 | if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 785 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal); |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 786 | else if (GetPipe().connector->Get()->IsExternal()) |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 787 | *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External); |
| 788 | else |
| 789 | return HWC2::Error::BadConfig; |
| 790 | |
| 791 | return HWC2::Error::None; |
| 792 | } |
| 793 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 794 | HWC2::Error HwcDisplay::SetActiveConfigWithConstraints( |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 795 | hwc2_config_t config, |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 796 | hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints, |
| 797 | hwc_vsync_period_change_timeline_t *outTimeline) { |
| 798 | if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) { |
| 799 | return HWC2::Error::BadParameter; |
| 800 | } |
| 801 | |
Roman Stratiienko | d0c035b | 2022-01-21 15:12:56 +0200 | [diff] [blame] | 802 | uint32_t current_vsync_period{}; |
| 803 | GetDisplayVsyncPeriod(¤t_vsync_period); |
| 804 | |
| 805 | if (vsyncPeriodChangeConstraints->seamlessRequired) { |
| 806 | return HWC2::Error::SeamlessNotAllowed; |
| 807 | } |
| 808 | |
| 809 | outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints |
| 810 | ->desiredTimeNanos - |
| 811 | current_vsync_period; |
| 812 | auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos); |
| 813 | if (ret != HWC2::Error::None) { |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | outTimeline->refreshRequired = true; |
| 818 | outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints |
| 819 | ->desiredTimeNanos; |
| 820 | |
| 821 | last_vsync_ts_ = 0; |
| 822 | vsync_tracking_en_ = true; |
| 823 | vsync_worker_.VSyncControl(true); |
| 824 | |
| 825 | return HWC2::Error::None; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) { |
| 829 | return HWC2::Error::Unsupported; |
| 830 | } |
| 831 | |
| 832 | HWC2::Error HwcDisplay::GetSupportedContentTypes( |
| 833 | uint32_t *outNumSupportedContentTypes, |
| 834 | const uint32_t *outSupportedContentTypes) { |
| 835 | if (outSupportedContentTypes == nullptr) |
| 836 | *outNumSupportedContentTypes = 0; |
| 837 | |
| 838 | return HWC2::Error::None; |
| 839 | } |
| 840 | |
| 841 | HWC2::Error HwcDisplay::SetContentType(int32_t contentType) { |
| 842 | if (contentType != HWC2_CONTENT_TYPE_NONE) |
| 843 | return HWC2::Error::Unsupported; |
| 844 | |
| 845 | /* TODO: Map to the DRM Connector property: |
| 846 | * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809 |
| 847 | */ |
| 848 | |
| 849 | return HWC2::Error::None; |
| 850 | } |
| 851 | #endif |
| 852 | |
| 853 | #if PLATFORM_SDK_VERSION > 28 |
| 854 | HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort, |
| 855 | uint32_t *outDataSize, |
| 856 | uint8_t *outData) { |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 857 | if (IsInHeadlessMode()) { |
Roman Stratiienko | f87d808 | 2022-05-06 11:33:56 +0300 | [diff] [blame] | 858 | return HWC2::Error::Unsupported; |
Roman Stratiienko | 3dacd47 | 2022-01-11 19:18:34 +0200 | [diff] [blame] | 859 | } |
Roman Stratiienko | f87d808 | 2022-05-06 11:33:56 +0300 | [diff] [blame] | 860 | |
Roman Stratiienko | 19c162f | 2022-02-01 09:35:08 +0200 | [diff] [blame] | 861 | auto blob = GetPipe().connector->Get()->GetEdidBlob(); |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 862 | if (!blob) { |
Roman Stratiienko | f87d808 | 2022-05-06 11:33:56 +0300 | [diff] [blame] | 863 | return HWC2::Error::Unsupported; |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Roman Stratiienko | f87d808 | 2022-05-06 11:33:56 +0300 | [diff] [blame] | 866 | *outPort = handle_; /* TDOD(nobody): What should be here? */ |
| 867 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 868 | if (outData) { |
| 869 | *outDataSize = std::min(*outDataSize, blob->length); |
| 870 | memcpy(outData, blob->data, *outDataSize); |
| 871 | } else { |
| 872 | *outDataSize = blob->length; |
| 873 | } |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 874 | |
| 875 | return HWC2::Error::None; |
| 876 | } |
| 877 | |
| 878 | HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities, |
| 879 | uint32_t * /*outCapabilities*/) { |
| 880 | if (outNumCapabilities == nullptr) { |
| 881 | return HWC2::Error::BadParameter; |
| 882 | } |
| 883 | |
| 884 | *outNumCapabilities = 0; |
| 885 | |
| 886 | return HWC2::Error::None; |
| 887 | } |
| 888 | |
| 889 | HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) { |
| 890 | *supported = false; |
| 891 | return HWC2::Error::None; |
| 892 | } |
| 893 | |
| 894 | HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) { |
| 895 | return HWC2::Error::Unsupported; |
| 896 | } |
| 897 | |
| 898 | #endif /* PLATFORM_SDK_VERSION > 28 */ |
| 899 | |
| 900 | #if PLATFORM_SDK_VERSION > 27 |
| 901 | |
| 902 | HWC2::Error HwcDisplay::GetRenderIntents( |
| 903 | int32_t mode, uint32_t *outNumIntents, |
| 904 | int32_t * /*android_render_intent_v1_1_t*/ outIntents) { |
| 905 | if (mode != HAL_COLOR_MODE_NATIVE) { |
| 906 | return HWC2::Error::BadParameter; |
| 907 | } |
| 908 | |
| 909 | if (outIntents == nullptr) { |
| 910 | *outNumIntents = 1; |
| 911 | return HWC2::Error::None; |
| 912 | } |
| 913 | *outNumIntents = 1; |
| 914 | outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC; |
| 915 | return HWC2::Error::None; |
| 916 | } |
| 917 | |
| 918 | HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) { |
| 919 | if (intent < HAL_RENDER_INTENT_COLORIMETRIC || |
| 920 | intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) |
| 921 | return HWC2::Error::BadParameter; |
| 922 | |
| 923 | if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG) |
| 924 | return HWC2::Error::BadParameter; |
| 925 | |
| 926 | if (mode != HAL_COLOR_MODE_NATIVE) |
| 927 | return HWC2::Error::Unsupported; |
| 928 | |
| 929 | if (intent != HAL_RENDER_INTENT_COLORIMETRIC) |
| 930 | return HWC2::Error::Unsupported; |
| 931 | |
| 932 | color_mode_ = mode; |
| 933 | return HWC2::Error::None; |
| 934 | } |
| 935 | |
| 936 | #endif /* PLATFORM_SDK_VERSION > 27 */ |
| 937 | |
| 938 | const Backend *HwcDisplay::backend() const { |
| 939 | return backend_.get(); |
| 940 | } |
| 941 | |
| 942 | void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) { |
| 943 | backend_ = std::move(backend); |
| 944 | } |
| 945 | |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 946 | /* returns true if composition should be sent to client */ |
| 947 | bool HwcDisplay::ProcessClientFlatteningState(bool skip) { |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 948 | const int flattenning_state = flattenning_state_; |
Roman Stratiienko | 099c311 | 2022-01-20 11:50:54 +0200 | [diff] [blame] | 949 | if (flattenning_state == ClientFlattenningState::Disabled) { |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | if (skip) { |
| 954 | flattenning_state_ = ClientFlattenningState::NotRequired; |
| 955 | return false; |
| 956 | } |
| 957 | |
| 958 | if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) { |
| 959 | flattenning_state_ = ClientFlattenningState::Flattened; |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | vsync_flattening_en_ = true; |
| 964 | vsync_worker_.VSyncControl(true); |
| 965 | flattenning_state_ = ClientFlattenningState::VsyncCountdownMax; |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | void HwcDisplay::ProcessFlatenningVsyncInternal() { |
| 970 | if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested && |
| 971 | --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested && |
| 972 | hwc2_->refresh_callback_.first != nullptr && |
| 973 | hwc2_->refresh_callback_.second != nullptr) { |
| 974 | hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_); |
| 975 | vsync_flattening_en_ = false; |
| 976 | } |
| 977 | } |
| 978 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 979 | } // namespace android |