blob: 51694c2753bb4d1c744ab7c1a89700e85c485a97 [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);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300117 a_args.composition = {};
118 a_args.active = false;
119 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200120
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200121 current_plan_.reset();
122 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200123 if (flatcon_) {
124 flatcon_->StopThread();
125 flatcon_.reset();
126 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200127 }
128
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200129 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600130 // TODO: There should be a mechanism to wait for this worker to complete,
131 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200132 vsync_worker_->StopThread();
133 vsync_worker_ = {};
134 }
135
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200136 SetClientTarget(nullptr, -1, 0, {});
137}
138
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200139HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200140 ChosePreferredConfig();
141
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200142 auto vsw_callbacks = (VSyncWorkerCallbacks){
143 .out_event =
144 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000145 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200146 if (vsync_event_en_) {
147 uint32_t period_ns{};
148 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000149 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200150 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200151 if (vsync_tracking_en_) {
152 last_vsync_ts_ = timestamp;
153 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200154 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200155 vsync_worker_->VSyncControl(false);
156 }
157 },
158 .get_vperiod_ns = [this]() -> uint32_t {
159 uint32_t outVsyncPeriod = 0;
160 GetDisplayVsyncPeriod(&outVsyncPeriod);
161 return outVsyncPeriod;
162 },
163 };
164
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300165 if (type_ != HWC2::DisplayType::Virtual) {
166 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
167 if (!vsync_worker_) {
168 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
169 return HWC2::Error::BadDisplay;
170 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171 }
172
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200173 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200174 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200175 if (ret) {
176 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
177 return HWC2::Error::BadDisplay;
178 }
Drew Davenport93443182023-12-14 09:25:45 +0000179 auto flatcbk = (struct FlatConCallbacks){
180 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200181 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200182 }
183
184 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
185
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400186 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200187
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200188 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189}
190
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600191std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
192 if (IsInHeadlessMode()) {
193 // The pipeline can be nullptr in headless mode, so return the default
194 // "normal" mode.
195 return PanelOrientation::kModePanelOrientationNormal;
196 }
197
198 DrmDisplayPipeline &pipeline = GetPipe();
199 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
200 ALOGW(
201 "No display pipeline present to query the panel orientation property.");
202 return {};
203 }
204
205 return pipeline.connector->Get()->GetPanelOrientation();
206}
207
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200208HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200209 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300210 if (type_ == HWC2::DisplayType::Virtual) {
211 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
212 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200213 err = configs_.Update(*pipeline_->connector->Get());
214 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300215 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200216 }
217 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200219 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220
Roman Stratiienko0137f862022-01-04 18:27:40 +0200221 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222}
223
224HWC2::Error HwcDisplay::AcceptDisplayChanges() {
225 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
226 l.second.AcceptTypeChange();
227 return HWC2::Error::None;
228}
229
230HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200231 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232 *layer = static_cast<hwc2_layer_t>(layer_idx_);
233 ++layer_idx_;
234 return HWC2::Error::None;
235}
236
237HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200238 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200239 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200240 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200241
242 layers_.erase(layer);
243 return HWC2::Error::None;
244}
245
246HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200247 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200248 return HWC2::Error::BadConfig;
249
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200250 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200251 return HWC2::Error::None;
252}
253
254HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
255 hwc2_layer_t *layers,
256 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200257 if (IsInHeadlessMode()) {
258 *num_elements = 0;
259 return HWC2::Error::None;
260 }
261
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200262 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300263 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200264 if (l.second.IsTypeChanged()) {
265 if (layers && num_changes < *num_elements)
266 layers[num_changes] = l.first;
267 if (types && num_changes < *num_elements)
268 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
269 ++num_changes;
270 }
271 }
272 if (!layers && !types)
273 *num_elements = num_changes;
274 return HWC2::Error::None;
275}
276
277HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
278 int32_t /*format*/,
279 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200280 if (IsInHeadlessMode()) {
281 return HWC2::Error::None;
282 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200283
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300284 auto min = pipeline_->device->GetMinResolution();
285 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200286
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200287 if (width < min.first || height < min.second)
288 return HWC2::Error::Unsupported;
289
290 if (width > max.first || height > max.second)
291 return HWC2::Error::Unsupported;
292
293 if (dataspace != HAL_DATASPACE_UNKNOWN)
294 return HWC2::Error::Unsupported;
295
296 // TODO(nobody): Validate format can be handled by either GL or planes
297 return HWC2::Error::None;
298}
299
300HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
301 if (!modes)
302 *num_modes = 1;
303
304 if (modes)
305 *modes = HAL_COLOR_MODE_NATIVE;
306
307 return HWC2::Error::None;
308}
309
310HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
311 int32_t attribute_in,
312 int32_t *value) {
313 int conf = static_cast<int>(config);
314
Roman Stratiienko0137f862022-01-04 18:27:40 +0200315 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200316 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200317 return HWC2::Error::BadConfig;
318 }
319
Roman Stratiienko0137f862022-01-04 18:27:40 +0200320 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200321
322 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300323 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200324 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
325 switch (attribute) {
326 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200327 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 break;
329 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200330 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200331 break;
332 case HWC2::Attribute::VsyncPeriod:
333 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600334 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000336 case HWC2::Attribute::DpiY:
337 // ideally this should be vdisplay/mm_heigth, however mm_height
338 // comes from edid parsing and is highly unreliable. Viewing the
339 // rarity of anisotropic displays, falling back to a single value
340 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 case HWC2::Attribute::DpiX:
342 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200343 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
344 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200345 : -1;
346 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200347#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200348 case HWC2::Attribute::ConfigGroup:
349 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
350 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200351 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200352 break;
353#endif
354 default:
355 *value = -1;
356 return HWC2::Error::BadConfig;
357 }
358 return HWC2::Error::None;
359}
360
Drew Davenportf7e88332024-09-06 12:54:38 -0600361HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
362 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200363 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200364 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200365 if (hwc_config.second.disabled) {
366 continue;
367 }
368
369 if (configs != nullptr) {
370 if (idx >= *num_configs) {
371 break;
372 }
373 configs[idx] = hwc_config.second.id;
374 }
375
376 idx++;
377 }
378 *num_configs = idx;
379 return HWC2::Error::None;
380}
381
382HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
383 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200384 if (IsInHeadlessMode()) {
385 stream << "null-display";
386 } else {
387 stream << "display-" << GetPipe().connector->Get()->GetId();
388 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300389 auto string = stream.str();
390 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200391 if (!name) {
392 *size = length;
393 return HWC2::Error::None;
394 }
395
396 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
397 strncpy(name, string.c_str(), *size);
398 return HWC2::Error::None;
399}
400
401HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
402 uint32_t *num_elements,
403 hwc2_layer_t * /*layers*/,
404 int32_t * /*layer_requests*/) {
405 // TODO(nobody): I think virtual display should request
406 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
407 *num_elements = 0;
408 return HWC2::Error::None;
409}
410
411HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
412 *type = static_cast<int32_t>(type_);
413 return HWC2::Error::None;
414}
415
416HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
417 *support = 0;
418 return HWC2::Error::None;
419}
420
421HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
422 int32_t * /*types*/,
423 float * /*max_luminance*/,
424 float * /*max_average_luminance*/,
425 float * /*min_luminance*/) {
426 *num_types = 0;
427 return HWC2::Error::None;
428}
429
430/* Find API details at:
431 * 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 +0300432 *
433 * Called after PresentDisplay(), CLIENT is expecting release fence for the
434 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200435 */
436HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
437 hwc2_layer_t *layers,
438 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200439 if (IsInHeadlessMode()) {
440 *num_elements = 0;
441 return HWC2::Error::None;
442 }
443
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444 uint32_t num_layers = 0;
445
Roman Stratiienkodd214942022-05-03 18:24:49 +0300446 for (auto &l : layers_) {
447 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
448 continue;
449 }
450
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200451 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300452
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200453 if (layers == nullptr || fences == nullptr)
454 continue;
455
456 if (num_layers > *num_elements) {
457 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
458 return HWC2::Error::None;
459 }
460
461 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200462 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200463 }
464 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300465
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200466 return HWC2::Error::None;
467}
468
469HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200470 if (IsInHeadlessMode()) {
471 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
472 return HWC2::Error::None;
473 }
474
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200475 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400476 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400477 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200478
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200479 uint32_t prev_vperiod_ns = 0;
480 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200481
482 auto mode_update_commited_ = false;
483 if (staged_mode_ &&
484 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
485 client_layer_.SetLayerDisplayFrame(
486 (hwc_rect_t){.left = 0,
487 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200488 .right = int(staged_mode_->GetRawMode().hdisplay),
489 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200490
491 configs_.active_config_id = staged_mode_config_id_;
492
493 a_args.display_mode = *staged_mode_;
494 if (!a_args.test_only) {
495 mode_update_commited_ = true;
496 }
497 }
498
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200499 // order the layers by z-order
500 bool use_client_layer = false;
501 uint32_t client_z_order = UINT32_MAX;
502 std::map<uint32_t, HwcLayer *> z_map;
503 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
504 switch (l.second.GetValidatedType()) {
505 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300506 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507 break;
508 case HWC2::Composition::Client:
509 // Place it at the z_order of the lowest client layer
510 use_client_layer = true;
511 client_z_order = std::min(client_z_order, l.second.GetZOrder());
512 break;
513 default:
514 continue;
515 }
516 }
517 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300518 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200519
520 if (z_map.empty())
521 return HWC2::Error::BadLayer;
522
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200523 std::vector<LayerData> composition_layers;
524
525 /* Import & populate */
526 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200527 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200528 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200529
530 // now that they're ordered by z, add them to the composition
531 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200532 if (!l.second->IsLayerUsableAsDevice()) {
533 /* This will be normally triggered on validation of the first frame
534 * containing CLIENT layer. At this moment client buffer is not yet
535 * provided by the CLIENT.
536 * This may be triggered once in HwcLayer lifecycle in case FB can't be
537 * imported. For example when non-contiguous buffer is imported into
538 * contiguous-only DRM/KMS driver.
539 */
540 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200541 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200542 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543 }
544
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200545 /* Store plan to ensure shared planes won't be stolen by other display
546 * in between of ValidateDisplay() and PresentDisplay() calls
547 */
548 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
549 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300550
551 if (type_ == HWC2::DisplayType::Virtual) {
552 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
553 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
554 .acquire_fence;
555 }
556
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200557 if (!current_plan_) {
558 if (!a_args.test_only) {
559 ALOGE("Failed to create DrmKmsPlan");
560 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200561 return HWC2::Error::BadConfig;
562 }
563
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200564 a_args.composition = current_plan_;
565
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300566 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200567
568 if (ret) {
569 if (!a_args.test_only)
570 ALOGE("Failed to apply the frame composition ret=%d", ret);
571 return HWC2::Error::BadParameter;
572 }
573
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200574 if (mode_update_commited_) {
575 staged_mode_.reset();
576 vsync_tracking_en_ = false;
577 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000578 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
579 last_vsync_ts_ +
580 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200581 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200582 }
583
584 return HWC2::Error::None;
585}
586
587/* Find API details at:
588 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
589 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300590HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200591 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300592 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200593 return HWC2::Error::None;
594 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200595 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200596
597 ++total_stats_.total_frames_;
598
599 AtomicCommitArgs a_args{};
600 ret = CreateComposition(a_args);
601
602 if (ret != HWC2::Error::None)
603 ++total_stats_.failed_kms_present_;
604
605 if (ret == HWC2::Error::BadLayer) {
606 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300607 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 return HWC2::Error::None;
609 }
610 if (ret != HWC2::Error::None)
611 return ret;
612
Roman Stratiienko76892782023-01-16 17:15:53 +0200613 this->present_fence_ = a_args.out_fence;
614 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200615
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200616 // Reset the color matrix so we don't apply it over and over again.
617 color_matrix_ = {};
618
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200619 ++frame_no_;
620 return HWC2::Error::None;
621}
622
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200623HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
624 int64_t change_time) {
625 if (configs_.hwc_configs.count(config) == 0) {
626 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 return HWC2::Error::BadConfig;
628 }
629
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200630 staged_mode_ = configs_.hwc_configs[config].mode;
631 staged_mode_change_time_ = change_time;
632 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200633
634 return HWC2::Error::None;
635}
636
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200637HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
638 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
639}
640
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641/* Find API details at:
642 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
643 */
644HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
645 int32_t acquire_fence,
646 int32_t dataspace,
647 hwc_region_t /*damage*/) {
648 client_layer_.SetLayerBuffer(target, acquire_fence);
649 client_layer_.SetLayerDataspace(dataspace);
650
651 /*
652 * target can be nullptr, this does mean the Composer Service is calling
653 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
654 * 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
655 */
656 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300657 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200658 return HWC2::Error::None;
659 }
660
Roman Stratiienko5070d512022-05-30 13:41:20 +0300661 if (IsInHeadlessMode()) {
662 return HWC2::Error::None;
663 }
664
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200665 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200666 if (!client_layer_.IsLayerUsableAsDevice()) {
667 ALOGE("Client layer must be always usable by DRM/KMS");
668 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200669 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200670
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200671 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300672 if (!bi) {
673 ALOGE("%s: Invalid state", __func__);
674 return HWC2::Error::BadLayer;
675 }
676
677 auto source_crop = (hwc_frect_t){.left = 0.0F,
678 .top = 0.0F,
679 .right = static_cast<float>(bi->width),
680 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200681 client_layer_.SetLayerSourceCrop(source_crop);
682
683 return HWC2::Error::None;
684}
685
686HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400687 /* Maps to the Colorspace DRM connector property:
688 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
689 */
690 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200691 return HWC2::Error::BadParameter;
692
Sasha McIntosh5294f092024-09-18 18:14:54 -0400693 switch (mode) {
694 case HAL_COLOR_MODE_NATIVE:
695 colorspace_ = Colorspace::kDefault;
696 break;
697 case HAL_COLOR_MODE_STANDARD_BT601_625:
698 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
699 case HAL_COLOR_MODE_STANDARD_BT601_525:
700 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
701 // The DP spec does not say whether this is the 525 or the 625 line version.
702 colorspace_ = Colorspace::kBt601Ycc;
703 break;
704 case HAL_COLOR_MODE_STANDARD_BT709:
705 case HAL_COLOR_MODE_SRGB:
706 colorspace_ = Colorspace::kBt709Ycc;
707 break;
708 case HAL_COLOR_MODE_DCI_P3:
709 case HAL_COLOR_MODE_DISPLAY_P3:
710 colorspace_ = Colorspace::kDciP3RgbD65;
711 break;
712 case HAL_COLOR_MODE_ADOBE_RGB:
713 default:
714 return HWC2::Error::Unsupported;
715 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200716
717 color_mode_ = mode;
718 return HWC2::Error::None;
719}
720
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200721#include <xf86drmMode.h>
722
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400723static uint64_t To3132FixPt(float in) {
724 constexpr uint64_t kSignMask = (1ULL << 63);
725 constexpr uint64_t kValueMask = ~(1ULL << 63);
726 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
727 if (in < 0)
728 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
729 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
730}
731
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200732HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
733 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
734 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
735 return HWC2::Error::BadParameter;
736
737 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
738 return HWC2::Error::BadParameter;
739
740 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200741
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200742 if (IsInHeadlessMode())
743 return HWC2::Error::None;
744
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200745 if (!GetPipe().crtc->Get()->GetCtmProperty())
746 return HWC2::Error::None;
747
748 switch (color_transform_hint_) {
749 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400750 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200751 break;
752 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400753 // Without HW support, we cannot correctly process matrices with an offset.
754 for (int i = 12; i < 14; i++) {
755 if (matrix[i] != 0.F)
756 return HWC2::Error::Unsupported;
757 }
758
759 /* HAL provides a 4x4 float type matrix:
760 * | 0 1 2 3|
761 * | 4 5 6 7|
762 * | 8 9 10 11|
763 * |12 13 14 15|
764 *
765 * R_out = R*0 + G*4 + B*8 + 12
766 * G_out = R*1 + G*5 + B*9 + 13
767 * B_out = R*2 + G*6 + B*10 + 14
768 *
769 * DRM expects a 3x3 s31.32 fixed point matrix:
770 * out matrix in
771 * |R| |0 1 2| |R|
772 * |G| = |3 4 5| x |G|
773 * |B| |6 7 8| |B|
774 *
775 * R_out = R*0 + G*1 + B*2
776 * G_out = R*3 + G*4 + B*5
777 * B_out = R*6 + G*7 + B*8
778 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200779 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200780 for (int i = 0; i < kCtmCols; i++) {
781 for (int j = 0; j < kCtmRows; j++) {
782 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400783 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200784 }
785 }
786 break;
787 default:
788 return HWC2::Error::Unsupported;
789 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200790
791 return HWC2::Error::None;
792}
793
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200794bool HwcDisplay::CtmByGpu() {
795 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
796 return false;
797
798 if (GetPipe().crtc->Get()->GetCtmProperty())
799 return false;
800
Drew Davenport93443182023-12-14 09:25:45 +0000801 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200802 return false;
803
804 return true;
805}
806
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300807HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
808 int32_t release_fence) {
809 writeback_layer_->SetLayerBuffer(buffer, release_fence);
810 writeback_layer_->PopulateLayerData();
811 if (!writeback_layer_->IsLayerUsableAsDevice()) {
812 ALOGE("Output layer must be always usable by DRM/KMS");
813 return HWC2::Error::BadLayer;
814 }
815 /* TODO: Check if format is supported by writeback connector */
816 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200817}
818
819HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
820 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300821
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200822 AtomicCommitArgs a_args{};
823
824 switch (mode) {
825 case HWC2::PowerMode::Off:
826 a_args.active = false;
827 break;
828 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300829 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200830 break;
831 case HWC2::PowerMode::Doze:
832 case HWC2::PowerMode::DozeSuspend:
833 return HWC2::Error::Unsupported;
834 default:
John Stultzffe783c2024-02-14 10:51:27 -0800835 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200836 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300837 }
838
839 if (IsInHeadlessMode()) {
840 return HWC2::Error::None;
841 }
842
Jia Ren80566fe2022-11-17 17:26:00 +0800843 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300844 /*
845 * Setting the display to active before we have a composition
846 * can break some drivers, so skip setting a_args.active to
847 * true, as the next composition frame will implicitly activate
848 * the display
849 */
850 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
851 ? HWC2::Error::None
852 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200853 };
854
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300855 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200856 if (err) {
857 ALOGE("Failed to apply the dpms composition err=%d", err);
858 return HWC2::Error::BadParameter;
859 }
860 return HWC2::Error::None;
861}
862
863HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300864 if (type_ == HWC2::DisplayType::Virtual) {
865 return HWC2::Error::None;
866 }
867
Roman Stratiienko099c3112022-01-20 11:50:54 +0200868 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
869 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200870 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200871 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200872 return HWC2::Error::None;
873}
874
875HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
876 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200877 if (IsInHeadlessMode()) {
878 *num_types = *num_requests = 0;
879 return HWC2::Error::None;
880 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300881
882 /* In current drm_hwc design in case previous frame layer was not validated as
883 * a CLIENT, it is used by display controller (Front buffer). We have to store
884 * this state to provide the CLIENT with the release fences for such buffers.
885 */
886 for (auto &l : layers_) {
887 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
888 HWC2::Composition::Client);
889 }
890
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200891 return backend_->ValidateDisplay(this, num_types, num_requests);
892}
893
894std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
895 std::vector<HwcLayer *> ordered_layers;
896 ordered_layers.reserve(layers_.size());
897
898 for (auto &[handle, layer] : layers_) {
899 ordered_layers.emplace_back(&layer);
900 }
901
902 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
903 [](const HwcLayer *lhs, const HwcLayer *rhs) {
904 return lhs->GetZOrder() < rhs->GetZOrder();
905 });
906
907 return ordered_layers;
908}
909
Roman Stratiienko099c3112022-01-20 11:50:54 +0200910HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
911 uint32_t *outVsyncPeriod /* ns */) {
912 return GetDisplayAttribute(configs_.active_config_id,
913 HWC2_ATTRIBUTE_VSYNC_PERIOD,
914 (int32_t *)(outVsyncPeriod));
915}
916
Roman Stratiienko6b405052022-12-10 19:09:10 +0200917#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200918HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200919 if (IsInHeadlessMode()) {
920 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
921 return HWC2::Error::None;
922 }
923 /* Primary display should be always internal,
924 * otherwise SF will be unhappy and will crash
925 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200926 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200928 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200929 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
930 else
931 return HWC2::Error::BadConfig;
932
933 return HWC2::Error::None;
934}
935
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200936HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200937 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200938 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
939 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300940 if (type_ == HWC2::DisplayType::Virtual) {
941 return HWC2::Error::None;
942 }
943
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200944 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
945 return HWC2::Error::BadParameter;
946 }
947
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200948 uint32_t current_vsync_period{};
949 GetDisplayVsyncPeriod(&current_vsync_period);
950
951 if (vsyncPeriodChangeConstraints->seamlessRequired) {
952 return HWC2::Error::SeamlessNotAllowed;
953 }
954
955 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
956 ->desiredTimeNanos -
957 current_vsync_period;
958 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
959 if (ret != HWC2::Error::None) {
960 return ret;
961 }
962
963 outTimeline->refreshRequired = true;
964 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
965 ->desiredTimeNanos;
966
967 last_vsync_ts_ = 0;
968 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200969 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200970
971 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200972}
973
974HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
975 return HWC2::Error::Unsupported;
976}
977
978HWC2::Error HwcDisplay::GetSupportedContentTypes(
979 uint32_t *outNumSupportedContentTypes,
980 const uint32_t *outSupportedContentTypes) {
981 if (outSupportedContentTypes == nullptr)
982 *outNumSupportedContentTypes = 0;
983
984 return HWC2::Error::None;
985}
986
987HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -0400988 /* Maps exactly to the content_type DRM connector property:
989 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200990 */
Sasha McIntosh173247b2024-09-18 18:06:52 -0400991 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
992 return HWC2::Error::BadParameter;
993
994 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995
996 return HWC2::Error::None;
997}
998#endif
999
Roman Stratiienko6b405052022-12-10 19:09:10 +02001000#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001001HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1002 uint32_t *outDataSize,
1003 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001004 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001005 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001006 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001007
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001008 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001009 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001010 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001011 }
1012
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001013 *outPort = handle_; /* TDOD(nobody): What should be here? */
1014
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001015 if (outData) {
1016 *outDataSize = std::min(*outDataSize, blob->length);
1017 memcpy(outData, blob->data, *outDataSize);
1018 } else {
1019 *outDataSize = blob->length;
1020 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001021
1022 return HWC2::Error::None;
1023}
1024
1025HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001026 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001027 if (outNumCapabilities == nullptr) {
1028 return HWC2::Error::BadParameter;
1029 }
1030
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001031 bool skip_ctm = false;
1032
1033 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001034 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001035 skip_ctm = true;
1036
1037 // Skip client CTM if DRM can handle it
1038 if (!skip_ctm && !IsInHeadlessMode() &&
1039 GetPipe().crtc->Get()->GetCtmProperty())
1040 skip_ctm = true;
1041
1042 if (!skip_ctm) {
1043 *outNumCapabilities = 0;
1044 return HWC2::Error::None;
1045 }
1046
1047 *outNumCapabilities = 1;
1048 if (outCapabilities) {
1049 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1050 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001051
1052 return HWC2::Error::None;
1053}
1054
1055HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1056 *supported = false;
1057 return HWC2::Error::None;
1058}
1059
1060HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1061 return HWC2::Error::Unsupported;
1062}
1063
Roman Stratiienko6b405052022-12-10 19:09:10 +02001064#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001065
Roman Stratiienko6b405052022-12-10 19:09:10 +02001066#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001067
1068HWC2::Error HwcDisplay::GetRenderIntents(
1069 int32_t mode, uint32_t *outNumIntents,
1070 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1071 if (mode != HAL_COLOR_MODE_NATIVE) {
1072 return HWC2::Error::BadParameter;
1073 }
1074
1075 if (outIntents == nullptr) {
1076 *outNumIntents = 1;
1077 return HWC2::Error::None;
1078 }
1079 *outNumIntents = 1;
1080 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1081 return HWC2::Error::None;
1082}
1083
1084HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1085 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1086 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1087 return HWC2::Error::BadParameter;
1088
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001089 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1090 return HWC2::Error::Unsupported;
1091
Sasha McIntosh5294f092024-09-18 18:14:54 -04001092 auto err = SetColorMode(mode);
1093 if (err != HWC2::Error::None) return err;
1094
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001095 return HWC2::Error::None;
1096}
1097
Roman Stratiienko6b405052022-12-10 19:09:10 +02001098#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001099
1100const Backend *HwcDisplay::backend() const {
1101 return backend_.get();
1102}
1103
1104void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1105 backend_ = std::move(backend);
1106}
1107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001108} // namespace android