blob: 159314c512882cc3b0a96fd11f1f287f92cffb7c [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 Davenportfe70c802024-11-07 13:02:21 -070099auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
100 -> const HwcDisplayConfig * {
101 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600102 if (config_iter == configs_.hwc_configs.end()) {
103 return nullptr;
104 }
105 return &config_iter->second;
106}
107
Drew Davenportfe70c802024-11-07 13:02:21 -0700108auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
109 return GetConfig(configs_.active_config_id);
110}
111
Drew Davenport8998f8b2024-10-24 10:15:12 -0600112auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700113 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600114}
115
Drew Davenport8998f8b2024-10-24 10:15:12 -0600116auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
117 bool seamless, QueuedConfigTiming *out_timing)
118 -> ConfigError {
119 if (configs_.hwc_configs.count(config) == 0) {
120 ALOGE("Could not find active mode for %u", config);
121 return ConfigError::kBadConfig;
122 }
123
124 // TODO: Add support for seamless configuration changes.
125 if (seamless) {
126 return ConfigError::kSeamlessNotAllowed;
127 }
128
129 // Request a refresh from the client one vsync period before the desired
130 // time, or simply at the desired time if there is no active configuration.
131 const HwcDisplayConfig *current_config = GetCurrentConfig();
132 out_timing->refresh_time_ns = desired_time -
133 (current_config
134 ? current_config->mode.GetVSyncPeriodNs()
135 : 0);
136 out_timing->new_vsync_time_ns = desired_time;
137
138 // Queue the config change timing to be consistent with the requested
139 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600140 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 {
Drew Davenportfe70c802024-11-07 13:02:21 -0700299 // If a config has been queued, it is considered the "active" config.
300 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
301 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200302 return HWC2::Error::BadConfig;
303
Drew Davenportfe70c802024-11-07 13:02:21 -0700304 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200305 return HWC2::Error::None;
306}
307
308HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
309 hwc2_layer_t *layers,
310 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200311 if (IsInHeadlessMode()) {
312 *num_elements = 0;
313 return HWC2::Error::None;
314 }
315
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200316 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300317 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200318 if (l.second.IsTypeChanged()) {
319 if (layers && num_changes < *num_elements)
320 layers[num_changes] = l.first;
321 if (types && num_changes < *num_elements)
322 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
323 ++num_changes;
324 }
325 }
326 if (!layers && !types)
327 *num_elements = num_changes;
328 return HWC2::Error::None;
329}
330
331HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
332 int32_t /*format*/,
333 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200334 if (IsInHeadlessMode()) {
335 return HWC2::Error::None;
336 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200337
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300338 auto min = pipeline_->device->GetMinResolution();
339 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200340
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 if (width < min.first || height < min.second)
342 return HWC2::Error::Unsupported;
343
344 if (width > max.first || height > max.second)
345 return HWC2::Error::Unsupported;
346
347 if (dataspace != HAL_DATASPACE_UNKNOWN)
348 return HWC2::Error::Unsupported;
349
350 // TODO(nobody): Validate format can be handled by either GL or planes
351 return HWC2::Error::None;
352}
353
354HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
355 if (!modes)
356 *num_modes = 1;
357
358 if (modes)
359 *modes = HAL_COLOR_MODE_NATIVE;
360
361 return HWC2::Error::None;
362}
363
364HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
365 int32_t attribute_in,
366 int32_t *value) {
367 int conf = static_cast<int>(config);
368
Roman Stratiienko0137f862022-01-04 18:27:40 +0200369 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200370 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200371 return HWC2::Error::BadConfig;
372 }
373
Roman Stratiienko0137f862022-01-04 18:27:40 +0200374 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200375
376 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300377 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200378 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
379 switch (attribute) {
380 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200381 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200382 break;
383 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200384 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200385 break;
386 case HWC2::Attribute::VsyncPeriod:
387 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600388 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200389 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000390 case HWC2::Attribute::DpiY:
391 // ideally this should be vdisplay/mm_heigth, however mm_height
392 // comes from edid parsing and is highly unreliable. Viewing the
393 // rarity of anisotropic displays, falling back to a single value
394 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200395 case HWC2::Attribute::DpiX:
396 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200397 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
398 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200399 : -1;
400 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200401#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200402 case HWC2::Attribute::ConfigGroup:
403 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
404 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200405 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200406 break;
407#endif
408 default:
409 *value = -1;
410 return HWC2::Error::BadConfig;
411 }
412 return HWC2::Error::None;
413}
414
Drew Davenportf7e88332024-09-06 12:54:38 -0600415HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
416 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200417 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200418 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200419 if (hwc_config.second.disabled) {
420 continue;
421 }
422
423 if (configs != nullptr) {
424 if (idx >= *num_configs) {
425 break;
426 }
427 configs[idx] = hwc_config.second.id;
428 }
429
430 idx++;
431 }
432 *num_configs = idx;
433 return HWC2::Error::None;
434}
435
436HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
437 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200438 if (IsInHeadlessMode()) {
439 stream << "null-display";
440 } else {
441 stream << "display-" << GetPipe().connector->Get()->GetId();
442 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300443 auto string = stream.str();
444 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200445 if (!name) {
446 *size = length;
447 return HWC2::Error::None;
448 }
449
450 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
451 strncpy(name, string.c_str(), *size);
452 return HWC2::Error::None;
453}
454
455HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
456 uint32_t *num_elements,
457 hwc2_layer_t * /*layers*/,
458 int32_t * /*layer_requests*/) {
459 // TODO(nobody): I think virtual display should request
460 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
461 *num_elements = 0;
462 return HWC2::Error::None;
463}
464
465HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
466 *type = static_cast<int32_t>(type_);
467 return HWC2::Error::None;
468}
469
470HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
471 *support = 0;
472 return HWC2::Error::None;
473}
474
475HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
476 int32_t * /*types*/,
477 float * /*max_luminance*/,
478 float * /*max_average_luminance*/,
479 float * /*min_luminance*/) {
480 *num_types = 0;
481 return HWC2::Error::None;
482}
483
484/* Find API details at:
485 * 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 +0300486 *
487 * Called after PresentDisplay(), CLIENT is expecting release fence for the
488 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200489 */
490HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
491 hwc2_layer_t *layers,
492 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200493 if (IsInHeadlessMode()) {
494 *num_elements = 0;
495 return HWC2::Error::None;
496 }
497
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200498 uint32_t num_layers = 0;
499
Roman Stratiienkodd214942022-05-03 18:24:49 +0300500 for (auto &l : layers_) {
501 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
502 continue;
503 }
504
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200505 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300506
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507 if (layers == nullptr || fences == nullptr)
508 continue;
509
510 if (num_layers > *num_elements) {
511 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
512 return HWC2::Error::None;
513 }
514
515 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200516 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200517 }
518 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300519
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200520 return HWC2::Error::None;
521}
522
523HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200524 if (IsInHeadlessMode()) {
525 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
526 return HWC2::Error::None;
527 }
528
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200529 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400530 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400531 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200532
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200533 uint32_t prev_vperiod_ns = 0;
534 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200535
536 auto mode_update_commited_ = false;
Drew Davenportfe70c802024-11-07 13:02:21 -0700537 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200538 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700539 const HwcDisplayConfig *staged_config = GetConfig(
540 staged_mode_config_id_.value());
541 if (staged_config == nullptr) {
542 return HWC2::Error::BadConfig;
543 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200544 client_layer_.SetLayerDisplayFrame(
545 (hwc_rect_t){.left = 0,
546 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700547 .right = int(staged_config->mode.GetRawMode().hdisplay),
548 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200549
Drew Davenportfe70c802024-11-07 13:02:21 -0700550 configs_.active_config_id = staged_mode_config_id_.value();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200551
Drew Davenportfe70c802024-11-07 13:02:21 -0700552 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200553 if (!a_args.test_only) {
554 mode_update_commited_ = true;
555 }
556 }
557
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200558 // order the layers by z-order
559 bool use_client_layer = false;
560 uint32_t client_z_order = UINT32_MAX;
561 std::map<uint32_t, HwcLayer *> z_map;
562 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
563 switch (l.second.GetValidatedType()) {
564 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300565 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566 break;
567 case HWC2::Composition::Client:
568 // Place it at the z_order of the lowest client layer
569 use_client_layer = true;
570 client_z_order = std::min(client_z_order, l.second.GetZOrder());
571 break;
572 default:
573 continue;
574 }
575 }
576 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300577 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200578
579 if (z_map.empty())
580 return HWC2::Error::BadLayer;
581
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200582 std::vector<LayerData> composition_layers;
583
584 /* Import & populate */
585 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200586 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200587 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200588
589 // now that they're ordered by z, add them to the composition
590 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200591 if (!l.second->IsLayerUsableAsDevice()) {
592 /* This will be normally triggered on validation of the first frame
593 * containing CLIENT layer. At this moment client buffer is not yet
594 * provided by the CLIENT.
595 * This may be triggered once in HwcLayer lifecycle in case FB can't be
596 * imported. For example when non-contiguous buffer is imported into
597 * contiguous-only DRM/KMS driver.
598 */
599 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200600 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200601 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200602 }
603
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200604 /* Store plan to ensure shared planes won't be stolen by other display
605 * in between of ValidateDisplay() and PresentDisplay() calls
606 */
607 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
608 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300609
610 if (type_ == HWC2::DisplayType::Virtual) {
611 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
612 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
613 .acquire_fence;
614 }
615
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200616 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700617 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 return HWC2::Error::BadConfig;
619 }
620
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200621 a_args.composition = current_plan_;
622
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300623 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200624
625 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700626 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 return HWC2::Error::BadParameter;
628 }
629
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200630 if (mode_update_commited_) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700631 staged_mode_config_id_.reset();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200632 vsync_tracking_en_ = false;
633 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000634 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
635 last_vsync_ts_ +
636 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200637 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200638 }
639
640 return HWC2::Error::None;
641}
642
643/* Find API details at:
644 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
645 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300646HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200647 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300648 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200649 return HWC2::Error::None;
650 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200651 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200652
653 ++total_stats_.total_frames_;
654
655 AtomicCommitArgs a_args{};
656 ret = CreateComposition(a_args);
657
658 if (ret != HWC2::Error::None)
659 ++total_stats_.failed_kms_present_;
660
661 if (ret == HWC2::Error::BadLayer) {
662 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300663 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200664 return HWC2::Error::None;
665 }
666 if (ret != HWC2::Error::None)
667 return ret;
668
Roman Stratiienko76892782023-01-16 17:15:53 +0200669 this->present_fence_ = a_args.out_fence;
670 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200671
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200672 // Reset the color matrix so we don't apply it over and over again.
673 color_matrix_ = {};
674
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200675 ++frame_no_;
676 return HWC2::Error::None;
677}
678
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200679HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
680 int64_t change_time) {
681 if (configs_.hwc_configs.count(config) == 0) {
682 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200683 return HWC2::Error::BadConfig;
684 }
685
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200686 staged_mode_change_time_ = change_time;
687 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200688
689 return HWC2::Error::None;
690}
691
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200692HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
693 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
694}
695
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200696/* Find API details at:
697 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
698 */
699HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
700 int32_t acquire_fence,
701 int32_t dataspace,
702 hwc_region_t /*damage*/) {
703 client_layer_.SetLayerBuffer(target, acquire_fence);
704 client_layer_.SetLayerDataspace(dataspace);
705
706 /*
707 * target can be nullptr, this does mean the Composer Service is calling
708 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
709 * 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
710 */
711 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300712 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200713 return HWC2::Error::None;
714 }
715
Roman Stratiienko5070d512022-05-30 13:41:20 +0300716 if (IsInHeadlessMode()) {
717 return HWC2::Error::None;
718 }
719
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200720 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200721 if (!client_layer_.IsLayerUsableAsDevice()) {
722 ALOGE("Client layer must be always usable by DRM/KMS");
723 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200724 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200725
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200726 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300727 if (!bi) {
728 ALOGE("%s: Invalid state", __func__);
729 return HWC2::Error::BadLayer;
730 }
731
732 auto source_crop = (hwc_frect_t){.left = 0.0F,
733 .top = 0.0F,
734 .right = static_cast<float>(bi->width),
735 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200736 client_layer_.SetLayerSourceCrop(source_crop);
737
738 return HWC2::Error::None;
739}
740
741HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400742 /* Maps to the Colorspace DRM connector property:
743 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
744 */
745 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746 return HWC2::Error::BadParameter;
747
Sasha McIntosh5294f092024-09-18 18:14:54 -0400748 switch (mode) {
749 case HAL_COLOR_MODE_NATIVE:
750 colorspace_ = Colorspace::kDefault;
751 break;
752 case HAL_COLOR_MODE_STANDARD_BT601_625:
753 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
754 case HAL_COLOR_MODE_STANDARD_BT601_525:
755 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
756 // The DP spec does not say whether this is the 525 or the 625 line version.
757 colorspace_ = Colorspace::kBt601Ycc;
758 break;
759 case HAL_COLOR_MODE_STANDARD_BT709:
760 case HAL_COLOR_MODE_SRGB:
761 colorspace_ = Colorspace::kBt709Ycc;
762 break;
763 case HAL_COLOR_MODE_DCI_P3:
764 case HAL_COLOR_MODE_DISPLAY_P3:
765 colorspace_ = Colorspace::kDciP3RgbD65;
766 break;
767 case HAL_COLOR_MODE_ADOBE_RGB:
768 default:
769 return HWC2::Error::Unsupported;
770 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200771
772 color_mode_ = mode;
773 return HWC2::Error::None;
774}
775
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200776#include <xf86drmMode.h>
777
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400778static uint64_t To3132FixPt(float in) {
779 constexpr uint64_t kSignMask = (1ULL << 63);
780 constexpr uint64_t kValueMask = ~(1ULL << 63);
781 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
782 if (in < 0)
783 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
784 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
785}
786
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200787HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
788 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
789 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
790 return HWC2::Error::BadParameter;
791
792 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
793 return HWC2::Error::BadParameter;
794
795 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200796
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200797 if (IsInHeadlessMode())
798 return HWC2::Error::None;
799
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200800 if (!GetPipe().crtc->Get()->GetCtmProperty())
801 return HWC2::Error::None;
802
803 switch (color_transform_hint_) {
804 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400805 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200806 break;
807 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400808 // Without HW support, we cannot correctly process matrices with an offset.
809 for (int i = 12; i < 14; i++) {
810 if (matrix[i] != 0.F)
811 return HWC2::Error::Unsupported;
812 }
813
814 /* HAL provides a 4x4 float type matrix:
815 * | 0 1 2 3|
816 * | 4 5 6 7|
817 * | 8 9 10 11|
818 * |12 13 14 15|
819 *
820 * R_out = R*0 + G*4 + B*8 + 12
821 * G_out = R*1 + G*5 + B*9 + 13
822 * B_out = R*2 + G*6 + B*10 + 14
823 *
824 * DRM expects a 3x3 s31.32 fixed point matrix:
825 * out matrix in
826 * |R| |0 1 2| |R|
827 * |G| = |3 4 5| x |G|
828 * |B| |6 7 8| |B|
829 *
830 * R_out = R*0 + G*1 + B*2
831 * G_out = R*3 + G*4 + B*5
832 * B_out = R*6 + G*7 + B*8
833 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200834 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200835 for (int i = 0; i < kCtmCols; i++) {
836 for (int j = 0; j < kCtmRows; j++) {
837 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400838 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200839 }
840 }
841 break;
842 default:
843 return HWC2::Error::Unsupported;
844 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845
846 return HWC2::Error::None;
847}
848
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200849bool HwcDisplay::CtmByGpu() {
850 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
851 return false;
852
853 if (GetPipe().crtc->Get()->GetCtmProperty())
854 return false;
855
Drew Davenport93443182023-12-14 09:25:45 +0000856 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200857 return false;
858
859 return true;
860}
861
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300862HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
863 int32_t release_fence) {
864 writeback_layer_->SetLayerBuffer(buffer, release_fence);
865 writeback_layer_->PopulateLayerData();
866 if (!writeback_layer_->IsLayerUsableAsDevice()) {
867 ALOGE("Output layer must be always usable by DRM/KMS");
868 return HWC2::Error::BadLayer;
869 }
870 /* TODO: Check if format is supported by writeback connector */
871 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200872}
873
874HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
875 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300876
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200877 AtomicCommitArgs a_args{};
878
879 switch (mode) {
880 case HWC2::PowerMode::Off:
881 a_args.active = false;
882 break;
883 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300884 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200885 break;
886 case HWC2::PowerMode::Doze:
887 case HWC2::PowerMode::DozeSuspend:
888 return HWC2::Error::Unsupported;
889 default:
John Stultzffe783c2024-02-14 10:51:27 -0800890 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200891 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300892 }
893
894 if (IsInHeadlessMode()) {
895 return HWC2::Error::None;
896 }
897
Jia Ren80566fe2022-11-17 17:26:00 +0800898 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300899 /*
900 * Setting the display to active before we have a composition
901 * can break some drivers, so skip setting a_args.active to
902 * true, as the next composition frame will implicitly activate
903 * the display
904 */
905 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
906 ? HWC2::Error::None
907 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200908 };
909
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300910 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200911 if (err) {
912 ALOGE("Failed to apply the dpms composition err=%d", err);
913 return HWC2::Error::BadParameter;
914 }
915 return HWC2::Error::None;
916}
917
918HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300919 if (type_ == HWC2::DisplayType::Virtual) {
920 return HWC2::Error::None;
921 }
922
Roman Stratiienko099c3112022-01-20 11:50:54 +0200923 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
924 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200925 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200926 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927 return HWC2::Error::None;
928}
929
930HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
931 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200932 if (IsInHeadlessMode()) {
933 *num_types = *num_requests = 0;
934 return HWC2::Error::None;
935 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300936
937 /* In current drm_hwc design in case previous frame layer was not validated as
938 * a CLIENT, it is used by display controller (Front buffer). We have to store
939 * this state to provide the CLIENT with the release fences for such buffers.
940 */
941 for (auto &l : layers_) {
942 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
943 HWC2::Composition::Client);
944 }
945
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200946 return backend_->ValidateDisplay(this, num_types, num_requests);
947}
948
949std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
950 std::vector<HwcLayer *> ordered_layers;
951 ordered_layers.reserve(layers_.size());
952
953 for (auto &[handle, layer] : layers_) {
954 ordered_layers.emplace_back(&layer);
955 }
956
957 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
958 [](const HwcLayer *lhs, const HwcLayer *rhs) {
959 return lhs->GetZOrder() < rhs->GetZOrder();
960 });
961
962 return ordered_layers;
963}
964
Roman Stratiienko099c3112022-01-20 11:50:54 +0200965HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
966 uint32_t *outVsyncPeriod /* ns */) {
967 return GetDisplayAttribute(configs_.active_config_id,
968 HWC2_ATTRIBUTE_VSYNC_PERIOD,
969 (int32_t *)(outVsyncPeriod));
970}
971
Roman Stratiienko6b405052022-12-10 19:09:10 +0200972#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200974 if (IsInHeadlessMode()) {
975 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
976 return HWC2::Error::None;
977 }
978 /* Primary display should be always internal,
979 * otherwise SF will be unhappy and will crash
980 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200981 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200982 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200983 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200984 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
985 else
986 return HWC2::Error::BadConfig;
987
988 return HWC2::Error::None;
989}
990
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200991HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200992 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200993 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
994 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300995 if (type_ == HWC2::DisplayType::Virtual) {
996 return HWC2::Error::None;
997 }
998
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200999 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1000 return HWC2::Error::BadParameter;
1001 }
1002
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001003 uint32_t current_vsync_period{};
1004 GetDisplayVsyncPeriod(&current_vsync_period);
1005
1006 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1007 return HWC2::Error::SeamlessNotAllowed;
1008 }
1009
1010 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1011 ->desiredTimeNanos -
1012 current_vsync_period;
1013 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1014 if (ret != HWC2::Error::None) {
1015 return ret;
1016 }
1017
1018 outTimeline->refreshRequired = true;
1019 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1020 ->desiredTimeNanos;
1021
1022 last_vsync_ts_ = 0;
1023 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001024 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001025
1026 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001027}
1028
1029HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1030 return HWC2::Error::Unsupported;
1031}
1032
1033HWC2::Error HwcDisplay::GetSupportedContentTypes(
1034 uint32_t *outNumSupportedContentTypes,
1035 const uint32_t *outSupportedContentTypes) {
1036 if (outSupportedContentTypes == nullptr)
1037 *outNumSupportedContentTypes = 0;
1038
1039 return HWC2::Error::None;
1040}
1041
1042HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001043 /* Maps exactly to the content_type DRM connector property:
1044 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001045 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001046 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1047 return HWC2::Error::BadParameter;
1048
1049 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001050
1051 return HWC2::Error::None;
1052}
1053#endif
1054
Roman Stratiienko6b405052022-12-10 19:09:10 +02001055#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001056HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1057 uint32_t *outDataSize,
1058 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001059 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001060 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001061 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001062
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001063 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001064 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001065 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001066 }
1067
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001068 *outPort = handle_; /* TDOD(nobody): What should be here? */
1069
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001070 if (outData) {
1071 *outDataSize = std::min(*outDataSize, blob->length);
1072 memcpy(outData, blob->data, *outDataSize);
1073 } else {
1074 *outDataSize = blob->length;
1075 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001076
1077 return HWC2::Error::None;
1078}
1079
1080HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001081 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001082 if (outNumCapabilities == nullptr) {
1083 return HWC2::Error::BadParameter;
1084 }
1085
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001086 bool skip_ctm = false;
1087
1088 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001089 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001090 skip_ctm = true;
1091
1092 // Skip client CTM if DRM can handle it
1093 if (!skip_ctm && !IsInHeadlessMode() &&
1094 GetPipe().crtc->Get()->GetCtmProperty())
1095 skip_ctm = true;
1096
1097 if (!skip_ctm) {
1098 *outNumCapabilities = 0;
1099 return HWC2::Error::None;
1100 }
1101
1102 *outNumCapabilities = 1;
1103 if (outCapabilities) {
1104 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1105 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001106
1107 return HWC2::Error::None;
1108}
1109
1110HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1111 *supported = false;
1112 return HWC2::Error::None;
1113}
1114
1115HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1116 return HWC2::Error::Unsupported;
1117}
1118
Roman Stratiienko6b405052022-12-10 19:09:10 +02001119#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001120
Roman Stratiienko6b405052022-12-10 19:09:10 +02001121#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001122
1123HWC2::Error HwcDisplay::GetRenderIntents(
1124 int32_t mode, uint32_t *outNumIntents,
1125 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1126 if (mode != HAL_COLOR_MODE_NATIVE) {
1127 return HWC2::Error::BadParameter;
1128 }
1129
1130 if (outIntents == nullptr) {
1131 *outNumIntents = 1;
1132 return HWC2::Error::None;
1133 }
1134 *outNumIntents = 1;
1135 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1136 return HWC2::Error::None;
1137}
1138
1139HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1140 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1141 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1142 return HWC2::Error::BadParameter;
1143
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001144 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1145 return HWC2::Error::Unsupported;
1146
Sasha McIntosh5294f092024-09-18 18:14:54 -04001147 auto err = SetColorMode(mode);
1148 if (err != HWC2::Error::None) return err;
1149
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001150 return HWC2::Error::None;
1151}
1152
Roman Stratiienko6b405052022-12-10 19:09:10 +02001153#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001154
1155const Backend *HwcDisplay::backend() const {
1156 return backend_.get();
1157}
1158
1159void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1160 backend_ = std::move(backend);
1161}
1162
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001163} // namespace android