blob: 0ab43f9e3f60e2d5e507f9edd27caf00ef7a59f8 [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
Manasi Navare3f0c01a2024-10-04 18:01:55 +000022#include <cinttypes>
23
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020024#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020025#include "backend/BackendManager.h"
26#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060027#include "compositor/DisplayInfo.h"
28#include "drm/DrmConnector.h"
29#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000030#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020031#include "utils/log.h"
32#include "utils/properties.h"
33
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060034using ::android::DrmDisplayPipeline;
35
Roman Stratiienko3627beb2022-01-04 16:02:55 +020036namespace android {
37
38std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
39 if (delta.total_pixops_ == 0)
40 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030041 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020042
43 std::stringstream ss;
44 ss << " Total frames count: " << delta.total_frames_ << "\n"
45 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
46 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
47 << ((delta.failed_kms_present_ > 0)
48 ? " !!! Internal failure, FIX it please\n"
49 : "")
50 << " Flattened frames: " << delta.frames_flattened_ << "\n"
51 << " Pixel operations (free units)"
52 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
53 << "]\n"
54 << " Composition efficiency: " << ratio;
55
56 return ss.str();
57}
58
59std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030060 auto connector_name = IsInHeadlessMode()
61 ? std::string("NULL-DISPLAY")
62 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020063
Roman Stratiienko3627beb2022-01-04 16:02:55 +020064 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020065 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020066 << "Statistics since system boot:\n"
67 << DumpDelta(total_stats_) << "\n\n"
68 << "Statistics since last dumpsys request:\n"
69 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
70
71 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
72 return ss.str();
73}
74
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020075HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +000076 DrmHwc *hwc)
77 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030078 if (type_ == HWC2::DisplayType::Virtual) {
79 writeback_layer_ = std::make_unique<HwcLayer>(this);
80 }
81}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020082
Sasha McIntosha37df7c2024-09-20 12:31:08 -040083void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020084 color_matrix_ = std::make_shared<drm_color_ctm>();
85 for (int i = 0; i < kCtmCols; i++) {
86 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080087 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020088 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
89 }
90 }
91
92 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020093}
94
Normunds Rieksts545096d2024-03-11 16:37:45 +000095HwcDisplay::~HwcDisplay() {
96 Deinit();
97};
Roman Stratiienko3dacd472022-01-11 19:18:34 +020098
Drew Davenport9799ab82024-10-23 10:15:45 -060099const HwcDisplayConfig *HwcDisplay::GetCurrentConfig() const {
100 auto config_iter = configs_.hwc_configs.find(configs_.active_config_id);
101 if (config_iter == configs_.hwc_configs.end()) {
102 return nullptr;
103 }
104 return &config_iter->second;
105}
106
Roman Stratiienko63762a92023-09-18 22:33:45 +0300107void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200108 Deinit();
109
Roman Stratiienko63762a92023-09-18 22:33:45 +0300110 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200111
Roman Stratiienko63762a92023-09-18 22:33:45 +0300112 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200113 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000114 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200115 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000116 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200117 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200118}
119
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200120void HwcDisplay::Deinit() {
121 if (pipeline_ != nullptr) {
122 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200123 a_args.composition = std::make_shared<DrmKmsPlan>();
124 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300125 a_args.composition = {};
126 a_args.active = false;
127 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200128
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200129 current_plan_.reset();
130 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200131 if (flatcon_) {
132 flatcon_->StopThread();
133 flatcon_.reset();
134 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200135 }
136
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200137 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600138 // TODO: There should be a mechanism to wait for this worker to complete,
139 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200140 vsync_worker_->StopThread();
141 vsync_worker_ = {};
142 }
143
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200144 SetClientTarget(nullptr, -1, 0, {});
145}
146
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200147HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200148 ChosePreferredConfig();
149
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200150 auto vsw_callbacks = (VSyncWorkerCallbacks){
151 .out_event =
152 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000153 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200154 if (vsync_event_en_) {
155 uint32_t period_ns{};
156 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000157 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200158 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200159 if (vsync_tracking_en_) {
160 last_vsync_ts_ = timestamp;
161 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200162 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200163 vsync_worker_->VSyncControl(false);
164 }
165 },
166 .get_vperiod_ns = [this]() -> uint32_t {
167 uint32_t outVsyncPeriod = 0;
168 GetDisplayVsyncPeriod(&outVsyncPeriod);
169 return outVsyncPeriod;
170 },
171 };
172
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300173 if (type_ != HWC2::DisplayType::Virtual) {
174 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
175 if (!vsync_worker_) {
176 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
177 return HWC2::Error::BadDisplay;
178 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200179 }
180
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200181 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200182 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200183 if (ret) {
184 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
185 return HWC2::Error::BadDisplay;
186 }
Drew Davenport93443182023-12-14 09:25:45 +0000187 auto flatcbk = (struct FlatConCallbacks){
188 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200189 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200190 }
191
192 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
193
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400194 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200195
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200196 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197}
198
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600199std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
200 if (IsInHeadlessMode()) {
201 // The pipeline can be nullptr in headless mode, so return the default
202 // "normal" mode.
203 return PanelOrientation::kModePanelOrientationNormal;
204 }
205
206 DrmDisplayPipeline &pipeline = GetPipe();
207 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
208 ALOGW(
209 "No display pipeline present to query the panel orientation property.");
210 return {};
211 }
212
213 return pipeline.connector->Get()->GetPanelOrientation();
214}
215
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200217 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300218 if (type_ == HWC2::DisplayType::Virtual) {
219 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
220 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200221 err = configs_.Update(*pipeline_->connector->Get());
222 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300223 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200224 }
225 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200227 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200228
Roman Stratiienko0137f862022-01-04 18:27:40 +0200229 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230}
231
232HWC2::Error HwcDisplay::AcceptDisplayChanges() {
233 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
234 l.second.AcceptTypeChange();
235 return HWC2::Error::None;
236}
237
238HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200239 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200240 *layer = static_cast<hwc2_layer_t>(layer_idx_);
241 ++layer_idx_;
242 return HWC2::Error::None;
243}
244
245HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200246 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200247 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200248 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200249
250 layers_.erase(layer);
251 return HWC2::Error::None;
252}
253
254HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200255 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200256 return HWC2::Error::BadConfig;
257
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200258 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200259 return HWC2::Error::None;
260}
261
262HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
263 hwc2_layer_t *layers,
264 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200265 if (IsInHeadlessMode()) {
266 *num_elements = 0;
267 return HWC2::Error::None;
268 }
269
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200270 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300271 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200272 if (l.second.IsTypeChanged()) {
273 if (layers && num_changes < *num_elements)
274 layers[num_changes] = l.first;
275 if (types && num_changes < *num_elements)
276 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
277 ++num_changes;
278 }
279 }
280 if (!layers && !types)
281 *num_elements = num_changes;
282 return HWC2::Error::None;
283}
284
285HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
286 int32_t /*format*/,
287 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200288 if (IsInHeadlessMode()) {
289 return HWC2::Error::None;
290 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200291
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300292 auto min = pipeline_->device->GetMinResolution();
293 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200294
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200295 if (width < min.first || height < min.second)
296 return HWC2::Error::Unsupported;
297
298 if (width > max.first || height > max.second)
299 return HWC2::Error::Unsupported;
300
301 if (dataspace != HAL_DATASPACE_UNKNOWN)
302 return HWC2::Error::Unsupported;
303
304 // TODO(nobody): Validate format can be handled by either GL or planes
305 return HWC2::Error::None;
306}
307
308HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
309 if (!modes)
310 *num_modes = 1;
311
312 if (modes)
313 *modes = HAL_COLOR_MODE_NATIVE;
314
315 return HWC2::Error::None;
316}
317
318HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
319 int32_t attribute_in,
320 int32_t *value) {
321 int conf = static_cast<int>(config);
322
Roman Stratiienko0137f862022-01-04 18:27:40 +0200323 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200324 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200325 return HWC2::Error::BadConfig;
326 }
327
Roman Stratiienko0137f862022-01-04 18:27:40 +0200328 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200329
330 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300331 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200332 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
333 switch (attribute) {
334 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200335 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200336 break;
337 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200338 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 break;
340 case HWC2::Attribute::VsyncPeriod:
341 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600342 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200343 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000344 case HWC2::Attribute::DpiY:
345 // ideally this should be vdisplay/mm_heigth, however mm_height
346 // comes from edid parsing and is highly unreliable. Viewing the
347 // rarity of anisotropic displays, falling back to a single value
348 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200349 case HWC2::Attribute::DpiX:
350 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200351 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
352 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200353 : -1;
354 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200355#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200356 case HWC2::Attribute::ConfigGroup:
357 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
358 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200359 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200360 break;
361#endif
362 default:
363 *value = -1;
364 return HWC2::Error::BadConfig;
365 }
366 return HWC2::Error::None;
367}
368
Drew Davenportf7e88332024-09-06 12:54:38 -0600369HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
370 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200371 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200372 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200373 if (hwc_config.second.disabled) {
374 continue;
375 }
376
377 if (configs != nullptr) {
378 if (idx >= *num_configs) {
379 break;
380 }
381 configs[idx] = hwc_config.second.id;
382 }
383
384 idx++;
385 }
386 *num_configs = idx;
387 return HWC2::Error::None;
388}
389
390HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
391 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200392 if (IsInHeadlessMode()) {
393 stream << "null-display";
394 } else {
395 stream << "display-" << GetPipe().connector->Get()->GetId();
396 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300397 auto string = stream.str();
398 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200399 if (!name) {
400 *size = length;
401 return HWC2::Error::None;
402 }
403
404 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
405 strncpy(name, string.c_str(), *size);
406 return HWC2::Error::None;
407}
408
409HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
410 uint32_t *num_elements,
411 hwc2_layer_t * /*layers*/,
412 int32_t * /*layer_requests*/) {
413 // TODO(nobody): I think virtual display should request
414 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
415 *num_elements = 0;
416 return HWC2::Error::None;
417}
418
419HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
420 *type = static_cast<int32_t>(type_);
421 return HWC2::Error::None;
422}
423
424HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
425 *support = 0;
426 return HWC2::Error::None;
427}
428
429HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
430 int32_t * /*types*/,
431 float * /*max_luminance*/,
432 float * /*max_average_luminance*/,
433 float * /*min_luminance*/) {
434 *num_types = 0;
435 return HWC2::Error::None;
436}
437
438/* Find API details at:
439 * 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 +0300440 *
441 * Called after PresentDisplay(), CLIENT is expecting release fence for the
442 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200443 */
444HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
445 hwc2_layer_t *layers,
446 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200447 if (IsInHeadlessMode()) {
448 *num_elements = 0;
449 return HWC2::Error::None;
450 }
451
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200452 uint32_t num_layers = 0;
453
Roman Stratiienkodd214942022-05-03 18:24:49 +0300454 for (auto &l : layers_) {
455 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
456 continue;
457 }
458
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200459 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300460
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200461 if (layers == nullptr || fences == nullptr)
462 continue;
463
464 if (num_layers > *num_elements) {
465 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
466 return HWC2::Error::None;
467 }
468
469 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200470 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200471 }
472 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300473
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200474 return HWC2::Error::None;
475}
476
477HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200478 if (IsInHeadlessMode()) {
479 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
480 return HWC2::Error::None;
481 }
482
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200483 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400484 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400485 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200486
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200487 uint32_t prev_vperiod_ns = 0;
488 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200489
490 auto mode_update_commited_ = false;
491 if (staged_mode_ &&
492 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
493 client_layer_.SetLayerDisplayFrame(
494 (hwc_rect_t){.left = 0,
495 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200496 .right = int(staged_mode_->GetRawMode().hdisplay),
497 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200498
499 configs_.active_config_id = staged_mode_config_id_;
500
501 a_args.display_mode = *staged_mode_;
502 if (!a_args.test_only) {
503 mode_update_commited_ = true;
504 }
505 }
506
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507 // order the layers by z-order
508 bool use_client_layer = false;
509 uint32_t client_z_order = UINT32_MAX;
510 std::map<uint32_t, HwcLayer *> z_map;
511 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
512 switch (l.second.GetValidatedType()) {
513 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300514 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 break;
516 case HWC2::Composition::Client:
517 // Place it at the z_order of the lowest client layer
518 use_client_layer = true;
519 client_z_order = std::min(client_z_order, l.second.GetZOrder());
520 break;
521 default:
522 continue;
523 }
524 }
525 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300526 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200527
528 if (z_map.empty())
529 return HWC2::Error::BadLayer;
530
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200531 std::vector<LayerData> composition_layers;
532
533 /* Import & populate */
534 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200535 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200536 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537
538 // now that they're ordered by z, add them to the composition
539 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200540 if (!l.second->IsLayerUsableAsDevice()) {
541 /* This will be normally triggered on validation of the first frame
542 * containing CLIENT layer. At this moment client buffer is not yet
543 * provided by the CLIENT.
544 * This may be triggered once in HwcLayer lifecycle in case FB can't be
545 * imported. For example when non-contiguous buffer is imported into
546 * contiguous-only DRM/KMS driver.
547 */
548 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200549 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200550 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200551 }
552
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200553 /* Store plan to ensure shared planes won't be stolen by other display
554 * in between of ValidateDisplay() and PresentDisplay() calls
555 */
556 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
557 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300558
559 if (type_ == HWC2::DisplayType::Virtual) {
560 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
561 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
562 .acquire_fence;
563 }
564
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200565 if (!current_plan_) {
566 if (!a_args.test_only) {
567 ALOGE("Failed to create DrmKmsPlan");
568 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200569 return HWC2::Error::BadConfig;
570 }
571
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200572 a_args.composition = current_plan_;
573
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300574 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200575
576 if (ret) {
577 if (!a_args.test_only)
578 ALOGE("Failed to apply the frame composition ret=%d", ret);
579 return HWC2::Error::BadParameter;
580 }
581
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200582 if (mode_update_commited_) {
583 staged_mode_.reset();
584 vsync_tracking_en_ = false;
585 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000586 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
587 last_vsync_ts_ +
588 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200589 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200590 }
591
592 return HWC2::Error::None;
593}
594
595/* Find API details at:
596 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
597 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300598HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200599 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300600 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200601 return HWC2::Error::None;
602 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200603 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604
605 ++total_stats_.total_frames_;
606
607 AtomicCommitArgs a_args{};
608 ret = CreateComposition(a_args);
609
610 if (ret != HWC2::Error::None)
611 ++total_stats_.failed_kms_present_;
612
613 if (ret == HWC2::Error::BadLayer) {
614 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300615 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200616 return HWC2::Error::None;
617 }
618 if (ret != HWC2::Error::None)
619 return ret;
620
Roman Stratiienko76892782023-01-16 17:15:53 +0200621 this->present_fence_ = a_args.out_fence;
622 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200623
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200624 // Reset the color matrix so we don't apply it over and over again.
625 color_matrix_ = {};
626
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 ++frame_no_;
628 return HWC2::Error::None;
629}
630
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200631HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
632 int64_t change_time) {
633 if (configs_.hwc_configs.count(config) == 0) {
634 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 return HWC2::Error::BadConfig;
636 }
637
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200638 staged_mode_ = configs_.hwc_configs[config].mode;
639 staged_mode_change_time_ = change_time;
640 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641
642 return HWC2::Error::None;
643}
644
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200645HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
646 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
647}
648
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649/* Find API details at:
650 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
651 */
652HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
653 int32_t acquire_fence,
654 int32_t dataspace,
655 hwc_region_t /*damage*/) {
656 client_layer_.SetLayerBuffer(target, acquire_fence);
657 client_layer_.SetLayerDataspace(dataspace);
658
659 /*
660 * target can be nullptr, this does mean the Composer Service is calling
661 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
662 * 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
663 */
664 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300665 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200666 return HWC2::Error::None;
667 }
668
Roman Stratiienko5070d512022-05-30 13:41:20 +0300669 if (IsInHeadlessMode()) {
670 return HWC2::Error::None;
671 }
672
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200673 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200674 if (!client_layer_.IsLayerUsableAsDevice()) {
675 ALOGE("Client layer must be always usable by DRM/KMS");
676 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200677 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200678
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200679 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300680 if (!bi) {
681 ALOGE("%s: Invalid state", __func__);
682 return HWC2::Error::BadLayer;
683 }
684
685 auto source_crop = (hwc_frect_t){.left = 0.0F,
686 .top = 0.0F,
687 .right = static_cast<float>(bi->width),
688 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200689 client_layer_.SetLayerSourceCrop(source_crop);
690
691 return HWC2::Error::None;
692}
693
694HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400695 /* Maps to the Colorspace DRM connector property:
696 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
697 */
698 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200699 return HWC2::Error::BadParameter;
700
Sasha McIntosh5294f092024-09-18 18:14:54 -0400701 switch (mode) {
702 case HAL_COLOR_MODE_NATIVE:
703 colorspace_ = Colorspace::kDefault;
704 break;
705 case HAL_COLOR_MODE_STANDARD_BT601_625:
706 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
707 case HAL_COLOR_MODE_STANDARD_BT601_525:
708 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
709 // The DP spec does not say whether this is the 525 or the 625 line version.
710 colorspace_ = Colorspace::kBt601Ycc;
711 break;
712 case HAL_COLOR_MODE_STANDARD_BT709:
713 case HAL_COLOR_MODE_SRGB:
714 colorspace_ = Colorspace::kBt709Ycc;
715 break;
716 case HAL_COLOR_MODE_DCI_P3:
717 case HAL_COLOR_MODE_DISPLAY_P3:
718 colorspace_ = Colorspace::kDciP3RgbD65;
719 break;
720 case HAL_COLOR_MODE_ADOBE_RGB:
721 default:
722 return HWC2::Error::Unsupported;
723 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200724
725 color_mode_ = mode;
726 return HWC2::Error::None;
727}
728
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200729#include <xf86drmMode.h>
730
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400731static uint64_t To3132FixPt(float in) {
732 constexpr uint64_t kSignMask = (1ULL << 63);
733 constexpr uint64_t kValueMask = ~(1ULL << 63);
734 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
735 if (in < 0)
736 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
737 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
738}
739
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200740HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
741 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
742 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
743 return HWC2::Error::BadParameter;
744
745 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
746 return HWC2::Error::BadParameter;
747
748 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200749
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200750 if (IsInHeadlessMode())
751 return HWC2::Error::None;
752
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200753 if (!GetPipe().crtc->Get()->GetCtmProperty())
754 return HWC2::Error::None;
755
756 switch (color_transform_hint_) {
757 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400758 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200759 break;
760 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400761 // Without HW support, we cannot correctly process matrices with an offset.
762 for (int i = 12; i < 14; i++) {
763 if (matrix[i] != 0.F)
764 return HWC2::Error::Unsupported;
765 }
766
767 /* HAL provides a 4x4 float type matrix:
768 * | 0 1 2 3|
769 * | 4 5 6 7|
770 * | 8 9 10 11|
771 * |12 13 14 15|
772 *
773 * R_out = R*0 + G*4 + B*8 + 12
774 * G_out = R*1 + G*5 + B*9 + 13
775 * B_out = R*2 + G*6 + B*10 + 14
776 *
777 * DRM expects a 3x3 s31.32 fixed point matrix:
778 * out matrix in
779 * |R| |0 1 2| |R|
780 * |G| = |3 4 5| x |G|
781 * |B| |6 7 8| |B|
782 *
783 * R_out = R*0 + G*1 + B*2
784 * G_out = R*3 + G*4 + B*5
785 * B_out = R*6 + G*7 + B*8
786 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200787 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200788 for (int i = 0; i < kCtmCols; i++) {
789 for (int j = 0; j < kCtmRows; j++) {
790 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400791 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200792 }
793 }
794 break;
795 default:
796 return HWC2::Error::Unsupported;
797 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200798
799 return HWC2::Error::None;
800}
801
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200802bool HwcDisplay::CtmByGpu() {
803 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
804 return false;
805
806 if (GetPipe().crtc->Get()->GetCtmProperty())
807 return false;
808
Drew Davenport93443182023-12-14 09:25:45 +0000809 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200810 return false;
811
812 return true;
813}
814
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300815HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
816 int32_t release_fence) {
817 writeback_layer_->SetLayerBuffer(buffer, release_fence);
818 writeback_layer_->PopulateLayerData();
819 if (!writeback_layer_->IsLayerUsableAsDevice()) {
820 ALOGE("Output layer must be always usable by DRM/KMS");
821 return HWC2::Error::BadLayer;
822 }
823 /* TODO: Check if format is supported by writeback connector */
824 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200825}
826
827HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
828 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300829
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200830 AtomicCommitArgs a_args{};
831
832 switch (mode) {
833 case HWC2::PowerMode::Off:
834 a_args.active = false;
835 break;
836 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300837 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838 break;
839 case HWC2::PowerMode::Doze:
840 case HWC2::PowerMode::DozeSuspend:
841 return HWC2::Error::Unsupported;
842 default:
John Stultzffe783c2024-02-14 10:51:27 -0800843 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200844 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300845 }
846
847 if (IsInHeadlessMode()) {
848 return HWC2::Error::None;
849 }
850
Jia Ren80566fe2022-11-17 17:26:00 +0800851 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300852 /*
853 * Setting the display to active before we have a composition
854 * can break some drivers, so skip setting a_args.active to
855 * true, as the next composition frame will implicitly activate
856 * the display
857 */
858 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
859 ? HWC2::Error::None
860 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200861 };
862
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300863 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200864 if (err) {
865 ALOGE("Failed to apply the dpms composition err=%d", err);
866 return HWC2::Error::BadParameter;
867 }
868 return HWC2::Error::None;
869}
870
871HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300872 if (type_ == HWC2::DisplayType::Virtual) {
873 return HWC2::Error::None;
874 }
875
Roman Stratiienko099c3112022-01-20 11:50:54 +0200876 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
877 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200878 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200879 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200880 return HWC2::Error::None;
881}
882
883HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
884 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200885 if (IsInHeadlessMode()) {
886 *num_types = *num_requests = 0;
887 return HWC2::Error::None;
888 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300889
890 /* In current drm_hwc design in case previous frame layer was not validated as
891 * a CLIENT, it is used by display controller (Front buffer). We have to store
892 * this state to provide the CLIENT with the release fences for such buffers.
893 */
894 for (auto &l : layers_) {
895 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
896 HWC2::Composition::Client);
897 }
898
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200899 return backend_->ValidateDisplay(this, num_types, num_requests);
900}
901
902std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
903 std::vector<HwcLayer *> ordered_layers;
904 ordered_layers.reserve(layers_.size());
905
906 for (auto &[handle, layer] : layers_) {
907 ordered_layers.emplace_back(&layer);
908 }
909
910 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
911 [](const HwcLayer *lhs, const HwcLayer *rhs) {
912 return lhs->GetZOrder() < rhs->GetZOrder();
913 });
914
915 return ordered_layers;
916}
917
Roman Stratiienko099c3112022-01-20 11:50:54 +0200918HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
919 uint32_t *outVsyncPeriod /* ns */) {
920 return GetDisplayAttribute(configs_.active_config_id,
921 HWC2_ATTRIBUTE_VSYNC_PERIOD,
922 (int32_t *)(outVsyncPeriod));
923}
924
Roman Stratiienko6b405052022-12-10 19:09:10 +0200925#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200926HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200927 if (IsInHeadlessMode()) {
928 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
929 return HWC2::Error::None;
930 }
931 /* Primary display should be always internal,
932 * otherwise SF will be unhappy and will crash
933 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200934 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200935 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200936 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200937 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
938 else
939 return HWC2::Error::BadConfig;
940
941 return HWC2::Error::None;
942}
943
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200944HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200945 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200946 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
947 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300948 if (type_ == HWC2::DisplayType::Virtual) {
949 return HWC2::Error::None;
950 }
951
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200952 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
953 return HWC2::Error::BadParameter;
954 }
955
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200956 uint32_t current_vsync_period{};
957 GetDisplayVsyncPeriod(&current_vsync_period);
958
959 if (vsyncPeriodChangeConstraints->seamlessRequired) {
960 return HWC2::Error::SeamlessNotAllowed;
961 }
962
963 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
964 ->desiredTimeNanos -
965 current_vsync_period;
966 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
967 if (ret != HWC2::Error::None) {
968 return ret;
969 }
970
971 outTimeline->refreshRequired = true;
972 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
973 ->desiredTimeNanos;
974
975 last_vsync_ts_ = 0;
976 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200977 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200978
979 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200980}
981
982HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
983 return HWC2::Error::Unsupported;
984}
985
986HWC2::Error HwcDisplay::GetSupportedContentTypes(
987 uint32_t *outNumSupportedContentTypes,
988 const uint32_t *outSupportedContentTypes) {
989 if (outSupportedContentTypes == nullptr)
990 *outNumSupportedContentTypes = 0;
991
992 return HWC2::Error::None;
993}
994
995HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -0400996 /* Maps exactly to the content_type DRM connector property:
997 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200998 */
Sasha McIntosh173247b2024-09-18 18:06:52 -0400999 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1000 return HWC2::Error::BadParameter;
1001
1002 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001003
1004 return HWC2::Error::None;
1005}
1006#endif
1007
Roman Stratiienko6b405052022-12-10 19:09:10 +02001008#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001009HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1010 uint32_t *outDataSize,
1011 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001012 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001013 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001014 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001015
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001016 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001017 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001018 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001019 }
1020
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001021 *outPort = handle_; /* TDOD(nobody): What should be here? */
1022
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001023 if (outData) {
1024 *outDataSize = std::min(*outDataSize, blob->length);
1025 memcpy(outData, blob->data, *outDataSize);
1026 } else {
1027 *outDataSize = blob->length;
1028 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001029
1030 return HWC2::Error::None;
1031}
1032
1033HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001034 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001035 if (outNumCapabilities == nullptr) {
1036 return HWC2::Error::BadParameter;
1037 }
1038
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001039 bool skip_ctm = false;
1040
1041 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001042 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001043 skip_ctm = true;
1044
1045 // Skip client CTM if DRM can handle it
1046 if (!skip_ctm && !IsInHeadlessMode() &&
1047 GetPipe().crtc->Get()->GetCtmProperty())
1048 skip_ctm = true;
1049
1050 if (!skip_ctm) {
1051 *outNumCapabilities = 0;
1052 return HWC2::Error::None;
1053 }
1054
1055 *outNumCapabilities = 1;
1056 if (outCapabilities) {
1057 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1058 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001059
1060 return HWC2::Error::None;
1061}
1062
1063HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1064 *supported = false;
1065 return HWC2::Error::None;
1066}
1067
1068HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1069 return HWC2::Error::Unsupported;
1070}
1071
Roman Stratiienko6b405052022-12-10 19:09:10 +02001072#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001073
Roman Stratiienko6b405052022-12-10 19:09:10 +02001074#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001075
1076HWC2::Error HwcDisplay::GetRenderIntents(
1077 int32_t mode, uint32_t *outNumIntents,
1078 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1079 if (mode != HAL_COLOR_MODE_NATIVE) {
1080 return HWC2::Error::BadParameter;
1081 }
1082
1083 if (outIntents == nullptr) {
1084 *outNumIntents = 1;
1085 return HWC2::Error::None;
1086 }
1087 *outNumIntents = 1;
1088 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1089 return HWC2::Error::None;
1090}
1091
1092HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1093 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1094 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1095 return HWC2::Error::BadParameter;
1096
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001097 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1098 return HWC2::Error::Unsupported;
1099
Sasha McIntosh5294f092024-09-18 18:14:54 -04001100 auto err = SetColorMode(mode);
1101 if (err != HWC2::Error::None) return err;
1102
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001103 return HWC2::Error::None;
1104}
1105
Roman Stratiienko6b405052022-12-10 19:09:10 +02001106#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001107
1108const Backend *HwcDisplay::backend() const {
1109 return backend_.get();
1110}
1111
1112void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1113 backend_ = std::move(backend);
1114}
1115
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001116} // namespace android