blob: 7559835e25413bd519c369488cb3394c033db1af [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_) {
617 if (!a_args.test_only) {
618 ALOGE("Failed to create DrmKmsPlan");
619 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620 return HWC2::Error::BadConfig;
621 }
622
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200623 a_args.composition = current_plan_;
624
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300625 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200626
627 if (ret) {
628 if (!a_args.test_only)
629 ALOGE("Failed to apply the frame composition ret=%d", ret);
630 return HWC2::Error::BadParameter;
631 }
632
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200633 if (mode_update_commited_) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700634 staged_mode_config_id_.reset();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200635 vsync_tracking_en_ = false;
636 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000637 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
638 last_vsync_ts_ +
639 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200640 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641 }
642
643 return HWC2::Error::None;
644}
645
646/* Find API details at:
647 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
648 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300649HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200650 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300651 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200652 return HWC2::Error::None;
653 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200654 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655
656 ++total_stats_.total_frames_;
657
658 AtomicCommitArgs a_args{};
659 ret = CreateComposition(a_args);
660
661 if (ret != HWC2::Error::None)
662 ++total_stats_.failed_kms_present_;
663
664 if (ret == HWC2::Error::BadLayer) {
665 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300666 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200667 return HWC2::Error::None;
668 }
669 if (ret != HWC2::Error::None)
670 return ret;
671
Roman Stratiienko76892782023-01-16 17:15:53 +0200672 this->present_fence_ = a_args.out_fence;
673 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200674
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200675 // Reset the color matrix so we don't apply it over and over again.
676 color_matrix_ = {};
677
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200678 ++frame_no_;
679 return HWC2::Error::None;
680}
681
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200682HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
683 int64_t change_time) {
684 if (configs_.hwc_configs.count(config) == 0) {
685 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200686 return HWC2::Error::BadConfig;
687 }
688
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200689 staged_mode_change_time_ = change_time;
690 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200691
692 return HWC2::Error::None;
693}
694
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200695HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
696 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
697}
698
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200699/* Find API details at:
700 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
701 */
702HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
703 int32_t acquire_fence,
704 int32_t dataspace,
705 hwc_region_t /*damage*/) {
706 client_layer_.SetLayerBuffer(target, acquire_fence);
707 client_layer_.SetLayerDataspace(dataspace);
708
709 /*
710 * target can be nullptr, this does mean the Composer Service is calling
711 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
712 * 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
713 */
714 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300715 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200716 return HWC2::Error::None;
717 }
718
Roman Stratiienko5070d512022-05-30 13:41:20 +0300719 if (IsInHeadlessMode()) {
720 return HWC2::Error::None;
721 }
722
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200723 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200724 if (!client_layer_.IsLayerUsableAsDevice()) {
725 ALOGE("Client layer must be always usable by DRM/KMS");
726 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200727 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200728
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200729 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300730 if (!bi) {
731 ALOGE("%s: Invalid state", __func__);
732 return HWC2::Error::BadLayer;
733 }
734
735 auto source_crop = (hwc_frect_t){.left = 0.0F,
736 .top = 0.0F,
737 .right = static_cast<float>(bi->width),
738 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200739 client_layer_.SetLayerSourceCrop(source_crop);
740
741 return HWC2::Error::None;
742}
743
744HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400745 /* Maps to the Colorspace DRM connector property:
746 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
747 */
748 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200749 return HWC2::Error::BadParameter;
750
Sasha McIntosh5294f092024-09-18 18:14:54 -0400751 switch (mode) {
752 case HAL_COLOR_MODE_NATIVE:
753 colorspace_ = Colorspace::kDefault;
754 break;
755 case HAL_COLOR_MODE_STANDARD_BT601_625:
756 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
757 case HAL_COLOR_MODE_STANDARD_BT601_525:
758 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
759 // The DP spec does not say whether this is the 525 or the 625 line version.
760 colorspace_ = Colorspace::kBt601Ycc;
761 break;
762 case HAL_COLOR_MODE_STANDARD_BT709:
763 case HAL_COLOR_MODE_SRGB:
764 colorspace_ = Colorspace::kBt709Ycc;
765 break;
766 case HAL_COLOR_MODE_DCI_P3:
767 case HAL_COLOR_MODE_DISPLAY_P3:
768 colorspace_ = Colorspace::kDciP3RgbD65;
769 break;
770 case HAL_COLOR_MODE_ADOBE_RGB:
771 default:
772 return HWC2::Error::Unsupported;
773 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200774
775 color_mode_ = mode;
776 return HWC2::Error::None;
777}
778
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200779#include <xf86drmMode.h>
780
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400781static uint64_t To3132FixPt(float in) {
782 constexpr uint64_t kSignMask = (1ULL << 63);
783 constexpr uint64_t kValueMask = ~(1ULL << 63);
784 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
785 if (in < 0)
786 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
787 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
788}
789
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200790HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
791 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
792 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
793 return HWC2::Error::BadParameter;
794
795 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
796 return HWC2::Error::BadParameter;
797
798 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200799
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200800 if (IsInHeadlessMode())
801 return HWC2::Error::None;
802
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200803 if (!GetPipe().crtc->Get()->GetCtmProperty())
804 return HWC2::Error::None;
805
806 switch (color_transform_hint_) {
807 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400808 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200809 break;
810 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400811 // Without HW support, we cannot correctly process matrices with an offset.
812 for (int i = 12; i < 14; i++) {
813 if (matrix[i] != 0.F)
814 return HWC2::Error::Unsupported;
815 }
816
817 /* HAL provides a 4x4 float type matrix:
818 * | 0 1 2 3|
819 * | 4 5 6 7|
820 * | 8 9 10 11|
821 * |12 13 14 15|
822 *
823 * R_out = R*0 + G*4 + B*8 + 12
824 * G_out = R*1 + G*5 + B*9 + 13
825 * B_out = R*2 + G*6 + B*10 + 14
826 *
827 * DRM expects a 3x3 s31.32 fixed point matrix:
828 * out matrix in
829 * |R| |0 1 2| |R|
830 * |G| = |3 4 5| x |G|
831 * |B| |6 7 8| |B|
832 *
833 * R_out = R*0 + G*1 + B*2
834 * G_out = R*3 + G*4 + B*5
835 * B_out = R*6 + G*7 + B*8
836 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200837 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200838 for (int i = 0; i < kCtmCols; i++) {
839 for (int j = 0; j < kCtmRows; j++) {
840 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400841 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200842 }
843 }
844 break;
845 default:
846 return HWC2::Error::Unsupported;
847 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200848
849 return HWC2::Error::None;
850}
851
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200852bool HwcDisplay::CtmByGpu() {
853 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
854 return false;
855
856 if (GetPipe().crtc->Get()->GetCtmProperty())
857 return false;
858
Drew Davenport93443182023-12-14 09:25:45 +0000859 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200860 return false;
861
862 return true;
863}
864
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300865HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
866 int32_t release_fence) {
867 writeback_layer_->SetLayerBuffer(buffer, release_fence);
868 writeback_layer_->PopulateLayerData();
869 if (!writeback_layer_->IsLayerUsableAsDevice()) {
870 ALOGE("Output layer must be always usable by DRM/KMS");
871 return HWC2::Error::BadLayer;
872 }
873 /* TODO: Check if format is supported by writeback connector */
874 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200875}
876
877HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
878 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300879
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200880 AtomicCommitArgs a_args{};
881
882 switch (mode) {
883 case HWC2::PowerMode::Off:
884 a_args.active = false;
885 break;
886 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300887 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200888 break;
889 case HWC2::PowerMode::Doze:
890 case HWC2::PowerMode::DozeSuspend:
891 return HWC2::Error::Unsupported;
892 default:
John Stultzffe783c2024-02-14 10:51:27 -0800893 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200894 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300895 }
896
897 if (IsInHeadlessMode()) {
898 return HWC2::Error::None;
899 }
900
Jia Ren80566fe2022-11-17 17:26:00 +0800901 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300902 /*
903 * Setting the display to active before we have a composition
904 * can break some drivers, so skip setting a_args.active to
905 * true, as the next composition frame will implicitly activate
906 * the display
907 */
908 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
909 ? HWC2::Error::None
910 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200911 };
912
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300913 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200914 if (err) {
915 ALOGE("Failed to apply the dpms composition err=%d", err);
916 return HWC2::Error::BadParameter;
917 }
918 return HWC2::Error::None;
919}
920
921HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300922 if (type_ == HWC2::DisplayType::Virtual) {
923 return HWC2::Error::None;
924 }
925
Roman Stratiienko099c3112022-01-20 11:50:54 +0200926 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
927 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200928 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200929 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200930 return HWC2::Error::None;
931}
932
933HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
934 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200935 if (IsInHeadlessMode()) {
936 *num_types = *num_requests = 0;
937 return HWC2::Error::None;
938 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300939
940 /* In current drm_hwc design in case previous frame layer was not validated as
941 * a CLIENT, it is used by display controller (Front buffer). We have to store
942 * this state to provide the CLIENT with the release fences for such buffers.
943 */
944 for (auto &l : layers_) {
945 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
946 HWC2::Composition::Client);
947 }
948
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200949 return backend_->ValidateDisplay(this, num_types, num_requests);
950}
951
952std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
953 std::vector<HwcLayer *> ordered_layers;
954 ordered_layers.reserve(layers_.size());
955
956 for (auto &[handle, layer] : layers_) {
957 ordered_layers.emplace_back(&layer);
958 }
959
960 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
961 [](const HwcLayer *lhs, const HwcLayer *rhs) {
962 return lhs->GetZOrder() < rhs->GetZOrder();
963 });
964
965 return ordered_layers;
966}
967
Roman Stratiienko099c3112022-01-20 11:50:54 +0200968HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
969 uint32_t *outVsyncPeriod /* ns */) {
970 return GetDisplayAttribute(configs_.active_config_id,
971 HWC2_ATTRIBUTE_VSYNC_PERIOD,
972 (int32_t *)(outVsyncPeriod));
973}
974
Roman Stratiienko6b405052022-12-10 19:09:10 +0200975#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200976HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200977 if (IsInHeadlessMode()) {
978 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
979 return HWC2::Error::None;
980 }
981 /* Primary display should be always internal,
982 * otherwise SF will be unhappy and will crash
983 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200984 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200985 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200986 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200987 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
988 else
989 return HWC2::Error::BadConfig;
990
991 return HWC2::Error::None;
992}
993
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200994HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200995 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200996 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
997 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300998 if (type_ == HWC2::DisplayType::Virtual) {
999 return HWC2::Error::None;
1000 }
1001
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001002 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1003 return HWC2::Error::BadParameter;
1004 }
1005
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001006 uint32_t current_vsync_period{};
1007 GetDisplayVsyncPeriod(&current_vsync_period);
1008
1009 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1010 return HWC2::Error::SeamlessNotAllowed;
1011 }
1012
1013 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1014 ->desiredTimeNanos -
1015 current_vsync_period;
1016 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1017 if (ret != HWC2::Error::None) {
1018 return ret;
1019 }
1020
1021 outTimeline->refreshRequired = true;
1022 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1023 ->desiredTimeNanos;
1024
1025 last_vsync_ts_ = 0;
1026 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001027 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001028
1029 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001030}
1031
1032HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1033 return HWC2::Error::Unsupported;
1034}
1035
1036HWC2::Error HwcDisplay::GetSupportedContentTypes(
1037 uint32_t *outNumSupportedContentTypes,
1038 const uint32_t *outSupportedContentTypes) {
1039 if (outSupportedContentTypes == nullptr)
1040 *outNumSupportedContentTypes = 0;
1041
1042 return HWC2::Error::None;
1043}
1044
1045HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001046 /* Maps exactly to the content_type DRM connector property:
1047 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001048 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001049 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1050 return HWC2::Error::BadParameter;
1051
1052 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001053
1054 return HWC2::Error::None;
1055}
1056#endif
1057
Roman Stratiienko6b405052022-12-10 19:09:10 +02001058#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001059HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1060 uint32_t *outDataSize,
1061 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001062 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001063 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001064 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001065
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001066 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001067 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001068 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001069 }
1070
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001071 *outPort = handle_; /* TDOD(nobody): What should be here? */
1072
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001073 if (outData) {
1074 *outDataSize = std::min(*outDataSize, blob->length);
1075 memcpy(outData, blob->data, *outDataSize);
1076 } else {
1077 *outDataSize = blob->length;
1078 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001079
1080 return HWC2::Error::None;
1081}
1082
1083HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001084 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001085 if (outNumCapabilities == nullptr) {
1086 return HWC2::Error::BadParameter;
1087 }
1088
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001089 bool skip_ctm = false;
1090
1091 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001092 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001093 skip_ctm = true;
1094
1095 // Skip client CTM if DRM can handle it
1096 if (!skip_ctm && !IsInHeadlessMode() &&
1097 GetPipe().crtc->Get()->GetCtmProperty())
1098 skip_ctm = true;
1099
1100 if (!skip_ctm) {
1101 *outNumCapabilities = 0;
1102 return HWC2::Error::None;
1103 }
1104
1105 *outNumCapabilities = 1;
1106 if (outCapabilities) {
1107 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1108 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001109
1110 return HWC2::Error::None;
1111}
1112
1113HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1114 *supported = false;
1115 return HWC2::Error::None;
1116}
1117
1118HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1119 return HWC2::Error::Unsupported;
1120}
1121
Roman Stratiienko6b405052022-12-10 19:09:10 +02001122#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001123
Roman Stratiienko6b405052022-12-10 19:09:10 +02001124#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001125
1126HWC2::Error HwcDisplay::GetRenderIntents(
1127 int32_t mode, uint32_t *outNumIntents,
1128 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1129 if (mode != HAL_COLOR_MODE_NATIVE) {
1130 return HWC2::Error::BadParameter;
1131 }
1132
1133 if (outIntents == nullptr) {
1134 *outNumIntents = 1;
1135 return HWC2::Error::None;
1136 }
1137 *outNumIntents = 1;
1138 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1139 return HWC2::Error::None;
1140}
1141
1142HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1143 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1144 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1145 return HWC2::Error::BadParameter;
1146
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001147 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1148 return HWC2::Error::Unsupported;
1149
Sasha McIntosh5294f092024-09-18 18:14:54 -04001150 auto err = SetColorMode(mode);
1151 if (err != HWC2::Error::None) return err;
1152
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001153 return HWC2::Error::None;
1154}
1155
Roman Stratiienko6b405052022-12-10 19:09:10 +02001156#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001157
1158const Backend *HwcDisplay::backend() const {
1159 return backend_.get();
1160}
1161
1162void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1163 backend_ = std::move(backend);
1164}
1165
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001166} // namespace android