blob: ffe6b11d0c4dd2559958705a5096121669fc5c23 [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
Manasi Navare3f0c01a2024-10-04 18:01:55 +000022#include <cinttypes>
23
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020024#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020025#include "backend/BackendManager.h"
26#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060027#include "compositor/DisplayInfo.h"
28#include "drm/DrmConnector.h"
29#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000030#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020031#include "utils/log.h"
32#include "utils/properties.h"
33
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060034using ::android::DrmDisplayPipeline;
35
Roman Stratiienko3627beb2022-01-04 16:02:55 +020036namespace android {
37
38std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
39 if (delta.total_pixops_ == 0)
40 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030041 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020042
43 std::stringstream ss;
44 ss << " Total frames count: " << delta.total_frames_ << "\n"
45 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
46 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
47 << ((delta.failed_kms_present_ > 0)
48 ? " !!! Internal failure, FIX it please\n"
49 : "")
50 << " Flattened frames: " << delta.frames_flattened_ << "\n"
51 << " Pixel operations (free units)"
52 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
53 << "]\n"
54 << " Composition efficiency: " << ratio;
55
56 return ss.str();
57}
58
59std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030060 auto connector_name = IsInHeadlessMode()
61 ? std::string("NULL-DISPLAY")
62 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020063
Roman Stratiienko3627beb2022-01-04 16:02:55 +020064 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020065 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020066 << "Statistics since system boot:\n"
67 << DumpDelta(total_stats_) << "\n\n"
68 << "Statistics since last dumpsys request:\n"
69 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
70
71 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
72 return ss.str();
73}
74
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020075HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +000076 DrmHwc *hwc)
77 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030078 if (type_ == HWC2::DisplayType::Virtual) {
79 writeback_layer_ = std::make_unique<HwcLayer>(this);
80 }
81}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020082
Sasha McIntosha37df7c2024-09-20 12:31:08 -040083void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020084 color_matrix_ = std::make_shared<drm_color_ctm>();
85 for (int i = 0; i < kCtmCols; i++) {
86 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080087 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020088 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
89 }
90 }
91
92 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020093}
94
Normunds Rieksts545096d2024-03-11 16:37:45 +000095HwcDisplay::~HwcDisplay() {
96 Deinit();
97};
Roman Stratiienko3dacd472022-01-11 19:18:34 +020098
Drew Davenport8998f8b2024-10-24 10:15:12 -060099auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
Drew Davenport9799ab82024-10-23 10:15:45 -0600100 auto config_iter = configs_.hwc_configs.find(configs_.active_config_id);
101 if (config_iter == configs_.hwc_configs.end()) {
102 return nullptr;
103 }
104 return &config_iter->second;
105}
106
Drew Davenport8998f8b2024-10-24 10:15:12 -0600107auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenport85be25d2024-10-23 10:26:34 -0600108 auto config_iter = configs_.hwc_configs.find(staged_mode_config_id_);
109 if (config_iter == configs_.hwc_configs.end()) {
110 return nullptr;
111 }
112 return &config_iter->second;
113}
114
Drew Davenport8998f8b2024-10-24 10:15:12 -0600115auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
116 bool seamless, QueuedConfigTiming *out_timing)
117 -> ConfigError {
118 if (configs_.hwc_configs.count(config) == 0) {
119 ALOGE("Could not find active mode for %u", config);
120 return ConfigError::kBadConfig;
121 }
122
123 // TODO: Add support for seamless configuration changes.
124 if (seamless) {
125 return ConfigError::kSeamlessNotAllowed;
126 }
127
128 // Request a refresh from the client one vsync period before the desired
129 // time, or simply at the desired time if there is no active configuration.
130 const HwcDisplayConfig *current_config = GetCurrentConfig();
131 out_timing->refresh_time_ns = desired_time -
132 (current_config
133 ? current_config->mode.GetVSyncPeriodNs()
134 : 0);
135 out_timing->new_vsync_time_ns = desired_time;
136
137 // Queue the config change timing to be consistent with the requested
138 // refresh time.
139 staged_mode_ = configs_.hwc_configs[config].mode;
140 staged_mode_change_time_ = out_timing->refresh_time_ns;
141 staged_mode_config_id_ = config;
142
143 // Enable vsync events until the mode has been applied.
144 last_vsync_ts_ = 0;
145 vsync_tracking_en_ = true;
146 vsync_worker_->VSyncControl(true);
147
148 return ConfigError::kNone;
149}
150
Roman Stratiienko63762a92023-09-18 22:33:45 +0300151void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200152 Deinit();
153
Roman Stratiienko63762a92023-09-18 22:33:45 +0300154 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200155
Roman Stratiienko63762a92023-09-18 22:33:45 +0300156 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200157 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000158 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200159 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000160 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200161 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200162}
163
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200164void HwcDisplay::Deinit() {
165 if (pipeline_ != nullptr) {
166 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200167 a_args.composition = std::make_shared<DrmKmsPlan>();
168 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300169 a_args.composition = {};
170 a_args.active = false;
171 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200172
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200173 current_plan_.reset();
174 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200175 if (flatcon_) {
176 flatcon_->StopThread();
177 flatcon_.reset();
178 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200179 }
180
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200181 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600182 // TODO: There should be a mechanism to wait for this worker to complete,
183 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200184 vsync_worker_->StopThread();
185 vsync_worker_ = {};
186 }
187
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200188 SetClientTarget(nullptr, -1, 0, {});
189}
190
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200191HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200192 ChosePreferredConfig();
193
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200194 auto vsw_callbacks = (VSyncWorkerCallbacks){
195 .out_event =
196 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000197 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200198 if (vsync_event_en_) {
199 uint32_t period_ns{};
200 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000201 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200202 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200203 if (vsync_tracking_en_) {
204 last_vsync_ts_ = timestamp;
205 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200206 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200207 vsync_worker_->VSyncControl(false);
208 }
209 },
210 .get_vperiod_ns = [this]() -> uint32_t {
211 uint32_t outVsyncPeriod = 0;
212 GetDisplayVsyncPeriod(&outVsyncPeriod);
213 return outVsyncPeriod;
214 },
215 };
216
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300217 if (type_ != HWC2::DisplayType::Virtual) {
218 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
219 if (!vsync_worker_) {
220 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
221 return HWC2::Error::BadDisplay;
222 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223 }
224
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200225 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200226 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200227 if (ret) {
228 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
229 return HWC2::Error::BadDisplay;
230 }
Drew Davenport93443182023-12-14 09:25:45 +0000231 auto flatcbk = (struct FlatConCallbacks){
232 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200233 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200234 }
235
236 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
237
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400238 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200239
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200240 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200241}
242
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600243std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
244 if (IsInHeadlessMode()) {
245 // The pipeline can be nullptr in headless mode, so return the default
246 // "normal" mode.
247 return PanelOrientation::kModePanelOrientationNormal;
248 }
249
250 DrmDisplayPipeline &pipeline = GetPipe();
251 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
252 ALOGW(
253 "No display pipeline present to query the panel orientation property.");
254 return {};
255 }
256
257 return pipeline.connector->Get()->GetPanelOrientation();
258}
259
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200260HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200261 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300262 if (type_ == HWC2::DisplayType::Virtual) {
263 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
264 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200265 err = configs_.Update(*pipeline_->connector->Get());
266 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300267 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200268 }
269 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200270 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200271 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200272
Roman Stratiienko0137f862022-01-04 18:27:40 +0200273 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200274}
275
276HWC2::Error HwcDisplay::AcceptDisplayChanges() {
277 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
278 l.second.AcceptTypeChange();
279 return HWC2::Error::None;
280}
281
282HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200283 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200284 *layer = static_cast<hwc2_layer_t>(layer_idx_);
285 ++layer_idx_;
286 return HWC2::Error::None;
287}
288
289HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200290 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200291 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200292 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200293
294 layers_.erase(layer);
295 return HWC2::Error::None;
296}
297
298HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200299 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200300 return HWC2::Error::BadConfig;
301
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200302 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200303 return HWC2::Error::None;
304}
305
306HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
307 hwc2_layer_t *layers,
308 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200309 if (IsInHeadlessMode()) {
310 *num_elements = 0;
311 return HWC2::Error::None;
312 }
313
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200314 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300315 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200316 if (l.second.IsTypeChanged()) {
317 if (layers && num_changes < *num_elements)
318 layers[num_changes] = l.first;
319 if (types && num_changes < *num_elements)
320 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
321 ++num_changes;
322 }
323 }
324 if (!layers && !types)
325 *num_elements = num_changes;
326 return HWC2::Error::None;
327}
328
329HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
330 int32_t /*format*/,
331 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200332 if (IsInHeadlessMode()) {
333 return HWC2::Error::None;
334 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300336 auto min = pipeline_->device->GetMinResolution();
337 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200338
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 if (width < min.first || height < min.second)
340 return HWC2::Error::Unsupported;
341
342 if (width > max.first || height > max.second)
343 return HWC2::Error::Unsupported;
344
345 if (dataspace != HAL_DATASPACE_UNKNOWN)
346 return HWC2::Error::Unsupported;
347
348 // TODO(nobody): Validate format can be handled by either GL or planes
349 return HWC2::Error::None;
350}
351
352HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
353 if (!modes)
354 *num_modes = 1;
355
356 if (modes)
357 *modes = HAL_COLOR_MODE_NATIVE;
358
359 return HWC2::Error::None;
360}
361
362HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
363 int32_t attribute_in,
364 int32_t *value) {
365 int conf = static_cast<int>(config);
366
Roman Stratiienko0137f862022-01-04 18:27:40 +0200367 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200368 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200369 return HWC2::Error::BadConfig;
370 }
371
Roman Stratiienko0137f862022-01-04 18:27:40 +0200372 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200373
374 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300375 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200376 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
377 switch (attribute) {
378 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200379 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200380 break;
381 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200382 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200383 break;
384 case HWC2::Attribute::VsyncPeriod:
385 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600386 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200387 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000388 case HWC2::Attribute::DpiY:
389 // ideally this should be vdisplay/mm_heigth, however mm_height
390 // comes from edid parsing and is highly unreliable. Viewing the
391 // rarity of anisotropic displays, falling back to a single value
392 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200393 case HWC2::Attribute::DpiX:
394 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200395 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
396 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200397 : -1;
398 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200399#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200400 case HWC2::Attribute::ConfigGroup:
401 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
402 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200403 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200404 break;
405#endif
406 default:
407 *value = -1;
408 return HWC2::Error::BadConfig;
409 }
410 return HWC2::Error::None;
411}
412
Drew Davenportf7e88332024-09-06 12:54:38 -0600413HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
414 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200415 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200416 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200417 if (hwc_config.second.disabled) {
418 continue;
419 }
420
421 if (configs != nullptr) {
422 if (idx >= *num_configs) {
423 break;
424 }
425 configs[idx] = hwc_config.second.id;
426 }
427
428 idx++;
429 }
430 *num_configs = idx;
431 return HWC2::Error::None;
432}
433
434HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
435 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200436 if (IsInHeadlessMode()) {
437 stream << "null-display";
438 } else {
439 stream << "display-" << GetPipe().connector->Get()->GetId();
440 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300441 auto string = stream.str();
442 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200443 if (!name) {
444 *size = length;
445 return HWC2::Error::None;
446 }
447
448 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
449 strncpy(name, string.c_str(), *size);
450 return HWC2::Error::None;
451}
452
453HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
454 uint32_t *num_elements,
455 hwc2_layer_t * /*layers*/,
456 int32_t * /*layer_requests*/) {
457 // TODO(nobody): I think virtual display should request
458 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
459 *num_elements = 0;
460 return HWC2::Error::None;
461}
462
463HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
464 *type = static_cast<int32_t>(type_);
465 return HWC2::Error::None;
466}
467
468HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
469 *support = 0;
470 return HWC2::Error::None;
471}
472
473HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
474 int32_t * /*types*/,
475 float * /*max_luminance*/,
476 float * /*max_average_luminance*/,
477 float * /*min_luminance*/) {
478 *num_types = 0;
479 return HWC2::Error::None;
480}
481
482/* Find API details at:
483 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
Roman Stratiienkodd214942022-05-03 18:24:49 +0300484 *
485 * Called after PresentDisplay(), CLIENT is expecting release fence for the
486 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200487 */
488HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
489 hwc2_layer_t *layers,
490 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200491 if (IsInHeadlessMode()) {
492 *num_elements = 0;
493 return HWC2::Error::None;
494 }
495
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 uint32_t num_layers = 0;
497
Roman Stratiienkodd214942022-05-03 18:24:49 +0300498 for (auto &l : layers_) {
499 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
500 continue;
501 }
502
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200503 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300504
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200505 if (layers == nullptr || fences == nullptr)
506 continue;
507
508 if (num_layers > *num_elements) {
509 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
510 return HWC2::Error::None;
511 }
512
513 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200514 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 }
516 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300517
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518 return HWC2::Error::None;
519}
520
521HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200522 if (IsInHeadlessMode()) {
523 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
524 return HWC2::Error::None;
525 }
526
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200527 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400528 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400529 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200530
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200531 uint32_t prev_vperiod_ns = 0;
532 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200533
534 auto mode_update_commited_ = false;
535 if (staged_mode_ &&
536 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
537 client_layer_.SetLayerDisplayFrame(
538 (hwc_rect_t){.left = 0,
539 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200540 .right = int(staged_mode_->GetRawMode().hdisplay),
541 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200542
543 configs_.active_config_id = staged_mode_config_id_;
544
545 a_args.display_mode = *staged_mode_;
546 if (!a_args.test_only) {
547 mode_update_commited_ = true;
548 }
549 }
550
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200551 // order the layers by z-order
552 bool use_client_layer = false;
553 uint32_t client_z_order = UINT32_MAX;
554 std::map<uint32_t, HwcLayer *> z_map;
555 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
556 switch (l.second.GetValidatedType()) {
557 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300558 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200559 break;
560 case HWC2::Composition::Client:
561 // Place it at the z_order of the lowest client layer
562 use_client_layer = true;
563 client_z_order = std::min(client_z_order, l.second.GetZOrder());
564 break;
565 default:
566 continue;
567 }
568 }
569 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300570 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200571
572 if (z_map.empty())
573 return HWC2::Error::BadLayer;
574
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200575 std::vector<LayerData> composition_layers;
576
577 /* Import & populate */
578 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200579 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200580 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200581
582 // now that they're ordered by z, add them to the composition
583 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200584 if (!l.second->IsLayerUsableAsDevice()) {
585 /* This will be normally triggered on validation of the first frame
586 * containing CLIENT layer. At this moment client buffer is not yet
587 * provided by the CLIENT.
588 * This may be triggered once in HwcLayer lifecycle in case FB can't be
589 * imported. For example when non-contiguous buffer is imported into
590 * contiguous-only DRM/KMS driver.
591 */
592 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200593 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200594 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200595 }
596
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200597 /* Store plan to ensure shared planes won't be stolen by other display
598 * in between of ValidateDisplay() and PresentDisplay() calls
599 */
600 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
601 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300602
603 if (type_ == HWC2::DisplayType::Virtual) {
604 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
605 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
606 .acquire_fence;
607 }
608
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200609 if (!current_plan_) {
610 if (!a_args.test_only) {
611 ALOGE("Failed to create DrmKmsPlan");
612 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200613 return HWC2::Error::BadConfig;
614 }
615
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200616 a_args.composition = current_plan_;
617
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300618 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200619
620 if (ret) {
621 if (!a_args.test_only)
622 ALOGE("Failed to apply the frame composition ret=%d", ret);
623 return HWC2::Error::BadParameter;
624 }
625
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200626 if (mode_update_commited_) {
627 staged_mode_.reset();
628 vsync_tracking_en_ = false;
629 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000630 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
631 last_vsync_ts_ +
632 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200633 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200634 }
635
636 return HWC2::Error::None;
637}
638
639/* Find API details at:
640 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
641 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300642HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200643 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300644 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200645 return HWC2::Error::None;
646 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200647 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648
649 ++total_stats_.total_frames_;
650
651 AtomicCommitArgs a_args{};
652 ret = CreateComposition(a_args);
653
654 if (ret != HWC2::Error::None)
655 ++total_stats_.failed_kms_present_;
656
657 if (ret == HWC2::Error::BadLayer) {
658 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300659 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200660 return HWC2::Error::None;
661 }
662 if (ret != HWC2::Error::None)
663 return ret;
664
Roman Stratiienko76892782023-01-16 17:15:53 +0200665 this->present_fence_ = a_args.out_fence;
666 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200667
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200668 // Reset the color matrix so we don't apply it over and over again.
669 color_matrix_ = {};
670
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200671 ++frame_no_;
672 return HWC2::Error::None;
673}
674
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200675HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
676 int64_t change_time) {
677 if (configs_.hwc_configs.count(config) == 0) {
678 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200679 return HWC2::Error::BadConfig;
680 }
681
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200682 staged_mode_ = configs_.hwc_configs[config].mode;
683 staged_mode_change_time_ = change_time;
684 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200685
686 return HWC2::Error::None;
687}
688
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200689HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
690 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
691}
692
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200693/* Find API details at:
694 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
695 */
696HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
697 int32_t acquire_fence,
698 int32_t dataspace,
699 hwc_region_t /*damage*/) {
700 client_layer_.SetLayerBuffer(target, acquire_fence);
701 client_layer_.SetLayerDataspace(dataspace);
702
703 /*
704 * target can be nullptr, this does mean the Composer Service is calling
705 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
706 * 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
707 */
708 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300709 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200710 return HWC2::Error::None;
711 }
712
Roman Stratiienko5070d512022-05-30 13:41:20 +0300713 if (IsInHeadlessMode()) {
714 return HWC2::Error::None;
715 }
716
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200717 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200718 if (!client_layer_.IsLayerUsableAsDevice()) {
719 ALOGE("Client layer must be always usable by DRM/KMS");
720 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200721 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200722
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200723 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300724 if (!bi) {
725 ALOGE("%s: Invalid state", __func__);
726 return HWC2::Error::BadLayer;
727 }
728
729 auto source_crop = (hwc_frect_t){.left = 0.0F,
730 .top = 0.0F,
731 .right = static_cast<float>(bi->width),
732 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200733 client_layer_.SetLayerSourceCrop(source_crop);
734
735 return HWC2::Error::None;
736}
737
738HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400739 /* Maps to the Colorspace DRM connector property:
740 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
741 */
742 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200743 return HWC2::Error::BadParameter;
744
Sasha McIntosh5294f092024-09-18 18:14:54 -0400745 switch (mode) {
746 case HAL_COLOR_MODE_NATIVE:
747 colorspace_ = Colorspace::kDefault;
748 break;
749 case HAL_COLOR_MODE_STANDARD_BT601_625:
750 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
751 case HAL_COLOR_MODE_STANDARD_BT601_525:
752 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
753 // The DP spec does not say whether this is the 525 or the 625 line version.
754 colorspace_ = Colorspace::kBt601Ycc;
755 break;
756 case HAL_COLOR_MODE_STANDARD_BT709:
757 case HAL_COLOR_MODE_SRGB:
758 colorspace_ = Colorspace::kBt709Ycc;
759 break;
760 case HAL_COLOR_MODE_DCI_P3:
761 case HAL_COLOR_MODE_DISPLAY_P3:
762 colorspace_ = Colorspace::kDciP3RgbD65;
763 break;
764 case HAL_COLOR_MODE_ADOBE_RGB:
765 default:
766 return HWC2::Error::Unsupported;
767 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200768
769 color_mode_ = mode;
770 return HWC2::Error::None;
771}
772
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200773#include <xf86drmMode.h>
774
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400775static uint64_t To3132FixPt(float in) {
776 constexpr uint64_t kSignMask = (1ULL << 63);
777 constexpr uint64_t kValueMask = ~(1ULL << 63);
778 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
779 if (in < 0)
780 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
781 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
782}
783
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200784HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
785 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
786 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
787 return HWC2::Error::BadParameter;
788
789 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
790 return HWC2::Error::BadParameter;
791
792 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200793
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200794 if (IsInHeadlessMode())
795 return HWC2::Error::None;
796
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200797 if (!GetPipe().crtc->Get()->GetCtmProperty())
798 return HWC2::Error::None;
799
800 switch (color_transform_hint_) {
801 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400802 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200803 break;
804 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400805 // Without HW support, we cannot correctly process matrices with an offset.
806 for (int i = 12; i < 14; i++) {
807 if (matrix[i] != 0.F)
808 return HWC2::Error::Unsupported;
809 }
810
811 /* HAL provides a 4x4 float type matrix:
812 * | 0 1 2 3|
813 * | 4 5 6 7|
814 * | 8 9 10 11|
815 * |12 13 14 15|
816 *
817 * R_out = R*0 + G*4 + B*8 + 12
818 * G_out = R*1 + G*5 + B*9 + 13
819 * B_out = R*2 + G*6 + B*10 + 14
820 *
821 * DRM expects a 3x3 s31.32 fixed point matrix:
822 * out matrix in
823 * |R| |0 1 2| |R|
824 * |G| = |3 4 5| x |G|
825 * |B| |6 7 8| |B|
826 *
827 * R_out = R*0 + G*1 + B*2
828 * G_out = R*3 + G*4 + B*5
829 * B_out = R*6 + G*7 + B*8
830 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200831 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200832 for (int i = 0; i < kCtmCols; i++) {
833 for (int j = 0; j < kCtmRows; j++) {
834 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400835 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200836 }
837 }
838 break;
839 default:
840 return HWC2::Error::Unsupported;
841 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200842
843 return HWC2::Error::None;
844}
845
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200846bool HwcDisplay::CtmByGpu() {
847 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
848 return false;
849
850 if (GetPipe().crtc->Get()->GetCtmProperty())
851 return false;
852
Drew Davenport93443182023-12-14 09:25:45 +0000853 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200854 return false;
855
856 return true;
857}
858
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300859HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
860 int32_t release_fence) {
861 writeback_layer_->SetLayerBuffer(buffer, release_fence);
862 writeback_layer_->PopulateLayerData();
863 if (!writeback_layer_->IsLayerUsableAsDevice()) {
864 ALOGE("Output layer must be always usable by DRM/KMS");
865 return HWC2::Error::BadLayer;
866 }
867 /* TODO: Check if format is supported by writeback connector */
868 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200869}
870
871HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
872 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300873
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200874 AtomicCommitArgs a_args{};
875
876 switch (mode) {
877 case HWC2::PowerMode::Off:
878 a_args.active = false;
879 break;
880 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300881 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882 break;
883 case HWC2::PowerMode::Doze:
884 case HWC2::PowerMode::DozeSuspend:
885 return HWC2::Error::Unsupported;
886 default:
John Stultzffe783c2024-02-14 10:51:27 -0800887 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200888 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300889 }
890
891 if (IsInHeadlessMode()) {
892 return HWC2::Error::None;
893 }
894
Jia Ren80566fe2022-11-17 17:26:00 +0800895 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300896 /*
897 * Setting the display to active before we have a composition
898 * can break some drivers, so skip setting a_args.active to
899 * true, as the next composition frame will implicitly activate
900 * the display
901 */
902 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
903 ? HWC2::Error::None
904 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200905 };
906
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300907 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200908 if (err) {
909 ALOGE("Failed to apply the dpms composition err=%d", err);
910 return HWC2::Error::BadParameter;
911 }
912 return HWC2::Error::None;
913}
914
915HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300916 if (type_ == HWC2::DisplayType::Virtual) {
917 return HWC2::Error::None;
918 }
919
Roman Stratiienko099c3112022-01-20 11:50:54 +0200920 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
921 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200922 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200923 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200924 return HWC2::Error::None;
925}
926
927HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
928 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200929 if (IsInHeadlessMode()) {
930 *num_types = *num_requests = 0;
931 return HWC2::Error::None;
932 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300933
934 /* In current drm_hwc design in case previous frame layer was not validated as
935 * a CLIENT, it is used by display controller (Front buffer). We have to store
936 * this state to provide the CLIENT with the release fences for such buffers.
937 */
938 for (auto &l : layers_) {
939 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
940 HWC2::Composition::Client);
941 }
942
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200943 return backend_->ValidateDisplay(this, num_types, num_requests);
944}
945
946std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
947 std::vector<HwcLayer *> ordered_layers;
948 ordered_layers.reserve(layers_.size());
949
950 for (auto &[handle, layer] : layers_) {
951 ordered_layers.emplace_back(&layer);
952 }
953
954 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
955 [](const HwcLayer *lhs, const HwcLayer *rhs) {
956 return lhs->GetZOrder() < rhs->GetZOrder();
957 });
958
959 return ordered_layers;
960}
961
Roman Stratiienko099c3112022-01-20 11:50:54 +0200962HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
963 uint32_t *outVsyncPeriod /* ns */) {
964 return GetDisplayAttribute(configs_.active_config_id,
965 HWC2_ATTRIBUTE_VSYNC_PERIOD,
966 (int32_t *)(outVsyncPeriod));
967}
968
Roman Stratiienko6b405052022-12-10 19:09:10 +0200969#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200970HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200971 if (IsInHeadlessMode()) {
972 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
973 return HWC2::Error::None;
974 }
975 /* Primary display should be always internal,
976 * otherwise SF will be unhappy and will crash
977 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200978 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200979 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200980 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
982 else
983 return HWC2::Error::BadConfig;
984
985 return HWC2::Error::None;
986}
987
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200988HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200989 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200990 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
991 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300992 if (type_ == HWC2::DisplayType::Virtual) {
993 return HWC2::Error::None;
994 }
995
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200996 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
997 return HWC2::Error::BadParameter;
998 }
999
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001000 uint32_t current_vsync_period{};
1001 GetDisplayVsyncPeriod(&current_vsync_period);
1002
1003 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1004 return HWC2::Error::SeamlessNotAllowed;
1005 }
1006
1007 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1008 ->desiredTimeNanos -
1009 current_vsync_period;
1010 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1011 if (ret != HWC2::Error::None) {
1012 return ret;
1013 }
1014
1015 outTimeline->refreshRequired = true;
1016 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1017 ->desiredTimeNanos;
1018
1019 last_vsync_ts_ = 0;
1020 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001021 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001022
1023 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001024}
1025
1026HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1027 return HWC2::Error::Unsupported;
1028}
1029
1030HWC2::Error HwcDisplay::GetSupportedContentTypes(
1031 uint32_t *outNumSupportedContentTypes,
1032 const uint32_t *outSupportedContentTypes) {
1033 if (outSupportedContentTypes == nullptr)
1034 *outNumSupportedContentTypes = 0;
1035
1036 return HWC2::Error::None;
1037}
1038
1039HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001040 /* Maps exactly to the content_type DRM connector property:
1041 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001042 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001043 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1044 return HWC2::Error::BadParameter;
1045
1046 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001047
1048 return HWC2::Error::None;
1049}
1050#endif
1051
Roman Stratiienko6b405052022-12-10 19:09:10 +02001052#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001053HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1054 uint32_t *outDataSize,
1055 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001056 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001057 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001058 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001059
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001060 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001061 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001062 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001063 }
1064
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001065 *outPort = handle_; /* TDOD(nobody): What should be here? */
1066
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001067 if (outData) {
1068 *outDataSize = std::min(*outDataSize, blob->length);
1069 memcpy(outData, blob->data, *outDataSize);
1070 } else {
1071 *outDataSize = blob->length;
1072 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001073
1074 return HWC2::Error::None;
1075}
1076
1077HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001078 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001079 if (outNumCapabilities == nullptr) {
1080 return HWC2::Error::BadParameter;
1081 }
1082
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001083 bool skip_ctm = false;
1084
1085 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001086 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001087 skip_ctm = true;
1088
1089 // Skip client CTM if DRM can handle it
1090 if (!skip_ctm && !IsInHeadlessMode() &&
1091 GetPipe().crtc->Get()->GetCtmProperty())
1092 skip_ctm = true;
1093
1094 if (!skip_ctm) {
1095 *outNumCapabilities = 0;
1096 return HWC2::Error::None;
1097 }
1098
1099 *outNumCapabilities = 1;
1100 if (outCapabilities) {
1101 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1102 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001103
1104 return HWC2::Error::None;
1105}
1106
1107HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1108 *supported = false;
1109 return HWC2::Error::None;
1110}
1111
1112HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1113 return HWC2::Error::Unsupported;
1114}
1115
Roman Stratiienko6b405052022-12-10 19:09:10 +02001116#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001117
Roman Stratiienko6b405052022-12-10 19:09:10 +02001118#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001119
1120HWC2::Error HwcDisplay::GetRenderIntents(
1121 int32_t mode, uint32_t *outNumIntents,
1122 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1123 if (mode != HAL_COLOR_MODE_NATIVE) {
1124 return HWC2::Error::BadParameter;
1125 }
1126
1127 if (outIntents == nullptr) {
1128 *outNumIntents = 1;
1129 return HWC2::Error::None;
1130 }
1131 *outNumIntents = 1;
1132 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1133 return HWC2::Error::None;
1134}
1135
1136HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1137 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1138 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1139 return HWC2::Error::BadParameter;
1140
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001141 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1142 return HWC2::Error::Unsupported;
1143
Sasha McIntosh5294f092024-09-18 18:14:54 -04001144 auto err = SetColorMode(mode);
1145 if (err != HWC2::Error::None) return err;
1146
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001147 return HWC2::Error::None;
1148}
1149
Roman Stratiienko6b405052022-12-10 19:09:10 +02001150#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001151
1152const Backend *HwcDisplay::backend() const {
1153 return backend_.get();
1154}
1155
1156void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1157 backend_ = std::move(backend);
1158}
1159
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001160} // namespace android