blob: 6797c5664753d0b4640242bbba6dfe5166de2554 [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
Roman Stratiienko63762a92023-09-18 22:33:45 +030099void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200100 Deinit();
101
Roman Stratiienko63762a92023-09-18 22:33:45 +0300102 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200103
Roman Stratiienko63762a92023-09-18 22:33:45 +0300104 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200105 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000106 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200107 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000108 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200109 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200110}
111
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200112void HwcDisplay::Deinit() {
113 if (pipeline_ != nullptr) {
114 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200115 a_args.composition = std::make_shared<DrmKmsPlan>();
116 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000117/*
118 * TODO:
119 * Unfortunately the following causes regressions on db845c
120 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
121 * never coming back. Patches to avoiding that issue on the
122 * the kernel side unfortunately causes further crashes in
123 * drm_hwcomposer, because the client detach takes longer then the
124 * 1 second max VTS expects. So for now as a workaround, lets skip
125 * deactivating the display on deinit, which matches previous
126 * behavior prior to commit d0494d9b8097
127 */
128#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300129 a_args.composition = {};
130 a_args.active = false;
131 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000132#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200133
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200134 current_plan_.reset();
135 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200136 if (flatcon_) {
137 flatcon_->StopThread();
138 flatcon_.reset();
139 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200140 }
141
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200142 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600143 // TODO: There should be a mechanism to wait for this worker to complete,
144 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200145 vsync_worker_->StopThread();
146 vsync_worker_ = {};
147 }
148
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200149 SetClientTarget(nullptr, -1, 0, {});
150}
151
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200152HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200153 ChosePreferredConfig();
154
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200155 auto vsw_callbacks = (VSyncWorkerCallbacks){
156 .out_event =
157 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000158 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200159 if (vsync_event_en_) {
160 uint32_t period_ns{};
161 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000162 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200163 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200164 if (vsync_tracking_en_) {
165 last_vsync_ts_ = timestamp;
166 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200167 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200168 vsync_worker_->VSyncControl(false);
169 }
170 },
171 .get_vperiod_ns = [this]() -> uint32_t {
172 uint32_t outVsyncPeriod = 0;
173 GetDisplayVsyncPeriod(&outVsyncPeriod);
174 return outVsyncPeriod;
175 },
176 };
177
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300178 if (type_ != HWC2::DisplayType::Virtual) {
179 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
180 if (!vsync_worker_) {
181 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
182 return HWC2::Error::BadDisplay;
183 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200184 }
185
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200186 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200187 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200188 if (ret) {
189 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
190 return HWC2::Error::BadDisplay;
191 }
Drew Davenport93443182023-12-14 09:25:45 +0000192 auto flatcbk = (struct FlatConCallbacks){
193 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200194 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195 }
196
197 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
198
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400199 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200200
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200201 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202}
203
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600204std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
205 if (IsInHeadlessMode()) {
206 // The pipeline can be nullptr in headless mode, so return the default
207 // "normal" mode.
208 return PanelOrientation::kModePanelOrientationNormal;
209 }
210
211 DrmDisplayPipeline &pipeline = GetPipe();
212 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
213 ALOGW(
214 "No display pipeline present to query the panel orientation property.");
215 return {};
216 }
217
218 return pipeline.connector->Get()->GetPanelOrientation();
219}
220
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200221HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200222 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300223 if (type_ == HWC2::DisplayType::Virtual) {
224 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
225 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200226 err = configs_.Update(*pipeline_->connector->Get());
227 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300228 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200229 }
230 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200231 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200232 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233
Roman Stratiienko0137f862022-01-04 18:27:40 +0200234 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200235}
236
237HWC2::Error HwcDisplay::AcceptDisplayChanges() {
238 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
239 l.second.AcceptTypeChange();
240 return HWC2::Error::None;
241}
242
243HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200244 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 *layer = static_cast<hwc2_layer_t>(layer_idx_);
246 ++layer_idx_;
247 return HWC2::Error::None;
248}
249
250HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200251 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200252 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200253 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200254
255 layers_.erase(layer);
256 return HWC2::Error::None;
257}
258
259HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200260 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200261 return HWC2::Error::BadConfig;
262
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200263 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200264 return HWC2::Error::None;
265}
266
267HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
268 hwc2_layer_t *layers,
269 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200270 if (IsInHeadlessMode()) {
271 *num_elements = 0;
272 return HWC2::Error::None;
273 }
274
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200275 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300276 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200277 if (l.second.IsTypeChanged()) {
278 if (layers && num_changes < *num_elements)
279 layers[num_changes] = l.first;
280 if (types && num_changes < *num_elements)
281 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
282 ++num_changes;
283 }
284 }
285 if (!layers && !types)
286 *num_elements = num_changes;
287 return HWC2::Error::None;
288}
289
290HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
291 int32_t /*format*/,
292 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200293 if (IsInHeadlessMode()) {
294 return HWC2::Error::None;
295 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200296
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300297 auto min = pipeline_->device->GetMinResolution();
298 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200299
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200300 if (width < min.first || height < min.second)
301 return HWC2::Error::Unsupported;
302
303 if (width > max.first || height > max.second)
304 return HWC2::Error::Unsupported;
305
306 if (dataspace != HAL_DATASPACE_UNKNOWN)
307 return HWC2::Error::Unsupported;
308
309 // TODO(nobody): Validate format can be handled by either GL or planes
310 return HWC2::Error::None;
311}
312
313HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
314 if (!modes)
315 *num_modes = 1;
316
317 if (modes)
318 *modes = HAL_COLOR_MODE_NATIVE;
319
320 return HWC2::Error::None;
321}
322
323HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
324 int32_t attribute_in,
325 int32_t *value) {
326 int conf = static_cast<int>(config);
327
Roman Stratiienko0137f862022-01-04 18:27:40 +0200328 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200329 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330 return HWC2::Error::BadConfig;
331 }
332
Roman Stratiienko0137f862022-01-04 18:27:40 +0200333 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200334
335 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300336 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200337 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
338 switch (attribute) {
339 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200340 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 break;
342 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200343 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200344 break;
345 case HWC2::Attribute::VsyncPeriod:
346 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600347 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200348 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000349 case HWC2::Attribute::DpiY:
350 // ideally this should be vdisplay/mm_heigth, however mm_height
351 // comes from edid parsing and is highly unreliable. Viewing the
352 // rarity of anisotropic displays, falling back to a single value
353 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200354 case HWC2::Attribute::DpiX:
355 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200356 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
357 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200358 : -1;
359 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200360#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200361 case HWC2::Attribute::ConfigGroup:
362 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
363 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200364 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200365 break;
366#endif
367 default:
368 *value = -1;
369 return HWC2::Error::BadConfig;
370 }
371 return HWC2::Error::None;
372}
373
Drew Davenportf7e88332024-09-06 12:54:38 -0600374HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
375 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200376 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200377 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200378 if (hwc_config.second.disabled) {
379 continue;
380 }
381
382 if (configs != nullptr) {
383 if (idx >= *num_configs) {
384 break;
385 }
386 configs[idx] = hwc_config.second.id;
387 }
388
389 idx++;
390 }
391 *num_configs = idx;
392 return HWC2::Error::None;
393}
394
395HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
396 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200397 if (IsInHeadlessMode()) {
398 stream << "null-display";
399 } else {
400 stream << "display-" << GetPipe().connector->Get()->GetId();
401 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300402 auto string = stream.str();
403 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200404 if (!name) {
405 *size = length;
406 return HWC2::Error::None;
407 }
408
409 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
410 strncpy(name, string.c_str(), *size);
411 return HWC2::Error::None;
412}
413
414HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
415 uint32_t *num_elements,
416 hwc2_layer_t * /*layers*/,
417 int32_t * /*layer_requests*/) {
418 // TODO(nobody): I think virtual display should request
419 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
420 *num_elements = 0;
421 return HWC2::Error::None;
422}
423
424HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
425 *type = static_cast<int32_t>(type_);
426 return HWC2::Error::None;
427}
428
429HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
430 *support = 0;
431 return HWC2::Error::None;
432}
433
434HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
435 int32_t * /*types*/,
436 float * /*max_luminance*/,
437 float * /*max_average_luminance*/,
438 float * /*min_luminance*/) {
439 *num_types = 0;
440 return HWC2::Error::None;
441}
442
443/* Find API details at:
444 * 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 +0300445 *
446 * Called after PresentDisplay(), CLIENT is expecting release fence for the
447 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200448 */
449HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
450 hwc2_layer_t *layers,
451 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200452 if (IsInHeadlessMode()) {
453 *num_elements = 0;
454 return HWC2::Error::None;
455 }
456
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200457 uint32_t num_layers = 0;
458
Roman Stratiienkodd214942022-05-03 18:24:49 +0300459 for (auto &l : layers_) {
460 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
461 continue;
462 }
463
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200464 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300465
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200466 if (layers == nullptr || fences == nullptr)
467 continue;
468
469 if (num_layers > *num_elements) {
470 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
471 return HWC2::Error::None;
472 }
473
474 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200475 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200476 }
477 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300478
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479 return HWC2::Error::None;
480}
481
482HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200483 if (IsInHeadlessMode()) {
484 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
485 return HWC2::Error::None;
486 }
487
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200488 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400489 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400490 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200491
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200492 uint32_t prev_vperiod_ns = 0;
493 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200494
495 auto mode_update_commited_ = false;
496 if (staged_mode_ &&
497 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
498 client_layer_.SetLayerDisplayFrame(
499 (hwc_rect_t){.left = 0,
500 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200501 .right = int(staged_mode_->GetRawMode().hdisplay),
502 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200503
504 configs_.active_config_id = staged_mode_config_id_;
505
506 a_args.display_mode = *staged_mode_;
507 if (!a_args.test_only) {
508 mode_update_commited_ = true;
509 }
510 }
511
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200512 // order the layers by z-order
513 bool use_client_layer = false;
514 uint32_t client_z_order = UINT32_MAX;
515 std::map<uint32_t, HwcLayer *> z_map;
516 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
517 switch (l.second.GetValidatedType()) {
518 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300519 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200520 break;
521 case HWC2::Composition::Client:
522 // Place it at the z_order of the lowest client layer
523 use_client_layer = true;
524 client_z_order = std::min(client_z_order, l.second.GetZOrder());
525 break;
526 default:
527 continue;
528 }
529 }
530 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300531 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532
533 if (z_map.empty())
534 return HWC2::Error::BadLayer;
535
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200536 std::vector<LayerData> composition_layers;
537
538 /* Import & populate */
539 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200540 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200541 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200542
543 // now that they're ordered by z, add them to the composition
544 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200545 if (!l.second->IsLayerUsableAsDevice()) {
546 /* This will be normally triggered on validation of the first frame
547 * containing CLIENT layer. At this moment client buffer is not yet
548 * provided by the CLIENT.
549 * This may be triggered once in HwcLayer lifecycle in case FB can't be
550 * imported. For example when non-contiguous buffer is imported into
551 * contiguous-only DRM/KMS driver.
552 */
553 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200555 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200556 }
557
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200558 /* Store plan to ensure shared planes won't be stolen by other display
559 * in between of ValidateDisplay() and PresentDisplay() calls
560 */
561 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
562 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300563
564 if (type_ == HWC2::DisplayType::Virtual) {
565 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
566 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
567 .acquire_fence;
568 }
569
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200570 if (!current_plan_) {
571 if (!a_args.test_only) {
572 ALOGE("Failed to create DrmKmsPlan");
573 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200574 return HWC2::Error::BadConfig;
575 }
576
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200577 a_args.composition = current_plan_;
578
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300579 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200580
581 if (ret) {
582 if (!a_args.test_only)
583 ALOGE("Failed to apply the frame composition ret=%d", ret);
584 return HWC2::Error::BadParameter;
585 }
586
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200587 if (mode_update_commited_) {
588 staged_mode_.reset();
589 vsync_tracking_en_ = false;
590 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000591 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
592 last_vsync_ts_ +
593 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200594 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200595 }
596
597 return HWC2::Error::None;
598}
599
600/* Find API details at:
601 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
602 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300603HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200604 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300605 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200606 return HWC2::Error::None;
607 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200608 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200609
610 ++total_stats_.total_frames_;
611
612 AtomicCommitArgs a_args{};
613 ret = CreateComposition(a_args);
614
615 if (ret != HWC2::Error::None)
616 ++total_stats_.failed_kms_present_;
617
618 if (ret == HWC2::Error::BadLayer) {
619 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300620 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200621 return HWC2::Error::None;
622 }
623 if (ret != HWC2::Error::None)
624 return ret;
625
Roman Stratiienko76892782023-01-16 17:15:53 +0200626 this->present_fence_ = a_args.out_fence;
627 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200628
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200629 // Reset the color matrix so we don't apply it over and over again.
630 color_matrix_ = {};
631
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632 ++frame_no_;
633 return HWC2::Error::None;
634}
635
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200636HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
637 int64_t change_time) {
638 if (configs_.hwc_configs.count(config) == 0) {
639 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200640 return HWC2::Error::BadConfig;
641 }
642
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200643 staged_mode_ = configs_.hwc_configs[config].mode;
644 staged_mode_change_time_ = change_time;
645 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200646
647 return HWC2::Error::None;
648}
649
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200650HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
651 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
652}
653
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200654/* Find API details at:
655 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
656 */
657HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
658 int32_t acquire_fence,
659 int32_t dataspace,
660 hwc_region_t /*damage*/) {
661 client_layer_.SetLayerBuffer(target, acquire_fence);
662 client_layer_.SetLayerDataspace(dataspace);
663
664 /*
665 * target can be nullptr, this does mean the Composer Service is calling
666 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
667 * 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
668 */
669 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300670 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200671 return HWC2::Error::None;
672 }
673
Roman Stratiienko5070d512022-05-30 13:41:20 +0300674 if (IsInHeadlessMode()) {
675 return HWC2::Error::None;
676 }
677
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200678 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200679 if (!client_layer_.IsLayerUsableAsDevice()) {
680 ALOGE("Client layer must be always usable by DRM/KMS");
681 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200682 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200683
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200684 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300685 if (!bi) {
686 ALOGE("%s: Invalid state", __func__);
687 return HWC2::Error::BadLayer;
688 }
689
690 auto source_crop = (hwc_frect_t){.left = 0.0F,
691 .top = 0.0F,
692 .right = static_cast<float>(bi->width),
693 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200694 client_layer_.SetLayerSourceCrop(source_crop);
695
696 return HWC2::Error::None;
697}
698
699HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400700 /* Maps to the Colorspace DRM connector property:
701 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
702 */
703 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200704 return HWC2::Error::BadParameter;
705
Sasha McIntosh5294f092024-09-18 18:14:54 -0400706 switch (mode) {
707 case HAL_COLOR_MODE_NATIVE:
708 colorspace_ = Colorspace::kDefault;
709 break;
710 case HAL_COLOR_MODE_STANDARD_BT601_625:
711 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
712 case HAL_COLOR_MODE_STANDARD_BT601_525:
713 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
714 // The DP spec does not say whether this is the 525 or the 625 line version.
715 colorspace_ = Colorspace::kBt601Ycc;
716 break;
717 case HAL_COLOR_MODE_STANDARD_BT709:
718 case HAL_COLOR_MODE_SRGB:
719 colorspace_ = Colorspace::kBt709Ycc;
720 break;
721 case HAL_COLOR_MODE_DCI_P3:
722 case HAL_COLOR_MODE_DISPLAY_P3:
723 colorspace_ = Colorspace::kDciP3RgbD65;
724 break;
725 case HAL_COLOR_MODE_ADOBE_RGB:
726 default:
727 return HWC2::Error::Unsupported;
728 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200729
730 color_mode_ = mode;
731 return HWC2::Error::None;
732}
733
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200734#include <xf86drmMode.h>
735
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400736static uint64_t To3132FixPt(float in) {
737 constexpr uint64_t kSignMask = (1ULL << 63);
738 constexpr uint64_t kValueMask = ~(1ULL << 63);
739 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
740 if (in < 0)
741 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
742 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
743}
744
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200745HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
746 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
747 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
748 return HWC2::Error::BadParameter;
749
750 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
751 return HWC2::Error::BadParameter;
752
753 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200754
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200755 if (IsInHeadlessMode())
756 return HWC2::Error::None;
757
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200758 if (!GetPipe().crtc->Get()->GetCtmProperty())
759 return HWC2::Error::None;
760
761 switch (color_transform_hint_) {
762 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400763 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200764 break;
765 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400766 // Without HW support, we cannot correctly process matrices with an offset.
767 for (int i = 12; i < 14; i++) {
768 if (matrix[i] != 0.F)
769 return HWC2::Error::Unsupported;
770 }
771
772 /* HAL provides a 4x4 float type matrix:
773 * | 0 1 2 3|
774 * | 4 5 6 7|
775 * | 8 9 10 11|
776 * |12 13 14 15|
777 *
778 * R_out = R*0 + G*4 + B*8 + 12
779 * G_out = R*1 + G*5 + B*9 + 13
780 * B_out = R*2 + G*6 + B*10 + 14
781 *
782 * DRM expects a 3x3 s31.32 fixed point matrix:
783 * out matrix in
784 * |R| |0 1 2| |R|
785 * |G| = |3 4 5| x |G|
786 * |B| |6 7 8| |B|
787 *
788 * R_out = R*0 + G*1 + B*2
789 * G_out = R*3 + G*4 + B*5
790 * B_out = R*6 + G*7 + B*8
791 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200792 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200793 for (int i = 0; i < kCtmCols; i++) {
794 for (int j = 0; j < kCtmRows; j++) {
795 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400796 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200797 }
798 }
799 break;
800 default:
801 return HWC2::Error::Unsupported;
802 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200803
804 return HWC2::Error::None;
805}
806
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200807bool HwcDisplay::CtmByGpu() {
808 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
809 return false;
810
811 if (GetPipe().crtc->Get()->GetCtmProperty())
812 return false;
813
Drew Davenport93443182023-12-14 09:25:45 +0000814 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200815 return false;
816
817 return true;
818}
819
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300820HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
821 int32_t release_fence) {
822 writeback_layer_->SetLayerBuffer(buffer, release_fence);
823 writeback_layer_->PopulateLayerData();
824 if (!writeback_layer_->IsLayerUsableAsDevice()) {
825 ALOGE("Output layer must be always usable by DRM/KMS");
826 return HWC2::Error::BadLayer;
827 }
828 /* TODO: Check if format is supported by writeback connector */
829 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200830}
831
832HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
833 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300834
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200835 AtomicCommitArgs a_args{};
836
837 switch (mode) {
838 case HWC2::PowerMode::Off:
839 a_args.active = false;
840 break;
841 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300842 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200843 break;
844 case HWC2::PowerMode::Doze:
845 case HWC2::PowerMode::DozeSuspend:
846 return HWC2::Error::Unsupported;
847 default:
John Stultzffe783c2024-02-14 10:51:27 -0800848 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200849 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300850 }
851
852 if (IsInHeadlessMode()) {
853 return HWC2::Error::None;
854 }
855
Jia Ren80566fe2022-11-17 17:26:00 +0800856 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300857 /*
858 * Setting the display to active before we have a composition
859 * can break some drivers, so skip setting a_args.active to
860 * true, as the next composition frame will implicitly activate
861 * the display
862 */
863 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
864 ? HWC2::Error::None
865 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200866 };
867
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300868 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200869 if (err) {
870 ALOGE("Failed to apply the dpms composition err=%d", err);
871 return HWC2::Error::BadParameter;
872 }
873 return HWC2::Error::None;
874}
875
876HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300877 if (type_ == HWC2::DisplayType::Virtual) {
878 return HWC2::Error::None;
879 }
880
Roman Stratiienko099c3112022-01-20 11:50:54 +0200881 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
882 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200883 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200884 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200885 return HWC2::Error::None;
886}
887
888HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
889 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200890 if (IsInHeadlessMode()) {
891 *num_types = *num_requests = 0;
892 return HWC2::Error::None;
893 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300894
895 /* In current drm_hwc design in case previous frame layer was not validated as
896 * a CLIENT, it is used by display controller (Front buffer). We have to store
897 * this state to provide the CLIENT with the release fences for such buffers.
898 */
899 for (auto &l : layers_) {
900 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
901 HWC2::Composition::Client);
902 }
903
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200904 return backend_->ValidateDisplay(this, num_types, num_requests);
905}
906
907std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
908 std::vector<HwcLayer *> ordered_layers;
909 ordered_layers.reserve(layers_.size());
910
911 for (auto &[handle, layer] : layers_) {
912 ordered_layers.emplace_back(&layer);
913 }
914
915 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
916 [](const HwcLayer *lhs, const HwcLayer *rhs) {
917 return lhs->GetZOrder() < rhs->GetZOrder();
918 });
919
920 return ordered_layers;
921}
922
Roman Stratiienko099c3112022-01-20 11:50:54 +0200923HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
924 uint32_t *outVsyncPeriod /* ns */) {
925 return GetDisplayAttribute(configs_.active_config_id,
926 HWC2_ATTRIBUTE_VSYNC_PERIOD,
927 (int32_t *)(outVsyncPeriod));
928}
929
Roman Stratiienko6b405052022-12-10 19:09:10 +0200930#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200931HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200932 if (IsInHeadlessMode()) {
933 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
934 return HWC2::Error::None;
935 }
936 /* Primary display should be always internal,
937 * otherwise SF will be unhappy and will crash
938 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200939 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200940 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200941 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200942 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
943 else
944 return HWC2::Error::BadConfig;
945
946 return HWC2::Error::None;
947}
948
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200949HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200950 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200951 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
952 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300953 if (type_ == HWC2::DisplayType::Virtual) {
954 return HWC2::Error::None;
955 }
956
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200957 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
958 return HWC2::Error::BadParameter;
959 }
960
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200961 uint32_t current_vsync_period{};
962 GetDisplayVsyncPeriod(&current_vsync_period);
963
964 if (vsyncPeriodChangeConstraints->seamlessRequired) {
965 return HWC2::Error::SeamlessNotAllowed;
966 }
967
968 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
969 ->desiredTimeNanos -
970 current_vsync_period;
971 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
972 if (ret != HWC2::Error::None) {
973 return ret;
974 }
975
976 outTimeline->refreshRequired = true;
977 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
978 ->desiredTimeNanos;
979
980 last_vsync_ts_ = 0;
981 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200982 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200983
984 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200985}
986
987HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
988 return HWC2::Error::Unsupported;
989}
990
991HWC2::Error HwcDisplay::GetSupportedContentTypes(
992 uint32_t *outNumSupportedContentTypes,
993 const uint32_t *outSupportedContentTypes) {
994 if (outSupportedContentTypes == nullptr)
995 *outNumSupportedContentTypes = 0;
996
997 return HWC2::Error::None;
998}
999
1000HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001001 /* Maps exactly to the content_type DRM connector property:
1002 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001003 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001004 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1005 return HWC2::Error::BadParameter;
1006
1007 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001008
1009 return HWC2::Error::None;
1010}
1011#endif
1012
Roman Stratiienko6b405052022-12-10 19:09:10 +02001013#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001014HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1015 uint32_t *outDataSize,
1016 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001017 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001018 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001019 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001020
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001021 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001022 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001023 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001024 }
1025
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001026 *outPort = handle_; /* TDOD(nobody): What should be here? */
1027
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001028 if (outData) {
1029 *outDataSize = std::min(*outDataSize, blob->length);
1030 memcpy(outData, blob->data, *outDataSize);
1031 } else {
1032 *outDataSize = blob->length;
1033 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001034
1035 return HWC2::Error::None;
1036}
1037
1038HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001039 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001040 if (outNumCapabilities == nullptr) {
1041 return HWC2::Error::BadParameter;
1042 }
1043
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001044 bool skip_ctm = false;
1045
1046 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001047 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001048 skip_ctm = true;
1049
1050 // Skip client CTM if DRM can handle it
1051 if (!skip_ctm && !IsInHeadlessMode() &&
1052 GetPipe().crtc->Get()->GetCtmProperty())
1053 skip_ctm = true;
1054
1055 if (!skip_ctm) {
1056 *outNumCapabilities = 0;
1057 return HWC2::Error::None;
1058 }
1059
1060 *outNumCapabilities = 1;
1061 if (outCapabilities) {
1062 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1063 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001064
1065 return HWC2::Error::None;
1066}
1067
1068HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1069 *supported = false;
1070 return HWC2::Error::None;
1071}
1072
1073HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1074 return HWC2::Error::Unsupported;
1075}
1076
Roman Stratiienko6b405052022-12-10 19:09:10 +02001077#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001078
Roman Stratiienko6b405052022-12-10 19:09:10 +02001079#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001080
1081HWC2::Error HwcDisplay::GetRenderIntents(
1082 int32_t mode, uint32_t *outNumIntents,
1083 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1084 if (mode != HAL_COLOR_MODE_NATIVE) {
1085 return HWC2::Error::BadParameter;
1086 }
1087
1088 if (outIntents == nullptr) {
1089 *outNumIntents = 1;
1090 return HWC2::Error::None;
1091 }
1092 *outNumIntents = 1;
1093 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1094 return HWC2::Error::None;
1095}
1096
1097HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1098 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1099 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1100 return HWC2::Error::BadParameter;
1101
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001102 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1103 return HWC2::Error::Unsupported;
1104
Sasha McIntosh5294f092024-09-18 18:14:54 -04001105 auto err = SetColorMode(mode);
1106 if (err != HWC2::Error::None) return err;
1107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001108 return HWC2::Error::None;
1109}
1110
Roman Stratiienko6b405052022-12-10 19:09:10 +02001111#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001112
1113const Backend *HwcDisplay::backend() const {
1114 return backend_.get();
1115}
1116
1117void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1118 backend_ = std::move(backend);
1119}
1120
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001121} // namespace android