blob: ec481e1fc97516cd2b4fb515f381ca10e113cc2d [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
Drew Davenport85be25d2024-10-23 10:26:34 -0600107const HwcDisplayConfig *HwcDisplay::GetLastRequestedConfig() const {
108 auto config_iter = configs_.hwc_configs.find(staged_mode_config_id_);
109 if (config_iter == configs_.hwc_configs.end()) {
110 return nullptr;
111 }
112 return &config_iter->second;
113}
114
Roman Stratiienko63762a92023-09-18 22:33:45 +0300115void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200116 Deinit();
117
Roman Stratiienko63762a92023-09-18 22:33:45 +0300118 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200119
Roman Stratiienko63762a92023-09-18 22:33:45 +0300120 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200121 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000122 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200123 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000124 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200125 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200126}
127
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200128void HwcDisplay::Deinit() {
129 if (pipeline_ != nullptr) {
130 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200131 a_args.composition = std::make_shared<DrmKmsPlan>();
132 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300133 a_args.composition = {};
134 a_args.active = false;
135 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200136
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200137 current_plan_.reset();
138 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200139 if (flatcon_) {
140 flatcon_->StopThread();
141 flatcon_.reset();
142 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200143 }
144
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200145 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600146 // TODO: There should be a mechanism to wait for this worker to complete,
147 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200148 vsync_worker_->StopThread();
149 vsync_worker_ = {};
150 }
151
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200152 SetClientTarget(nullptr, -1, 0, {});
153}
154
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200155HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200156 ChosePreferredConfig();
157
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200158 auto vsw_callbacks = (VSyncWorkerCallbacks){
159 .out_event =
160 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000161 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200162 if (vsync_event_en_) {
163 uint32_t period_ns{};
164 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000165 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200166 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200167 if (vsync_tracking_en_) {
168 last_vsync_ts_ = timestamp;
169 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200170 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200171 vsync_worker_->VSyncControl(false);
172 }
173 },
174 .get_vperiod_ns = [this]() -> uint32_t {
175 uint32_t outVsyncPeriod = 0;
176 GetDisplayVsyncPeriod(&outVsyncPeriod);
177 return outVsyncPeriod;
178 },
179 };
180
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300181 if (type_ != HWC2::DisplayType::Virtual) {
182 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
183 if (!vsync_worker_) {
184 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
185 return HWC2::Error::BadDisplay;
186 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200187 }
188
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200189 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200190 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200191 if (ret) {
192 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
193 return HWC2::Error::BadDisplay;
194 }
Drew Davenport93443182023-12-14 09:25:45 +0000195 auto flatcbk = (struct FlatConCallbacks){
196 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200197 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198 }
199
200 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
201
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400202 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200203
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200204 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200205}
206
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600207std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
208 if (IsInHeadlessMode()) {
209 // The pipeline can be nullptr in headless mode, so return the default
210 // "normal" mode.
211 return PanelOrientation::kModePanelOrientationNormal;
212 }
213
214 DrmDisplayPipeline &pipeline = GetPipe();
215 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
216 ALOGW(
217 "No display pipeline present to query the panel orientation property.");
218 return {};
219 }
220
221 return pipeline.connector->Get()->GetPanelOrientation();
222}
223
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200224HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200225 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300226 if (type_ == HWC2::DisplayType::Virtual) {
227 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
228 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200229 err = configs_.Update(*pipeline_->connector->Get());
230 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300231 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200232 }
233 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200234 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200235 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200236
Roman Stratiienko0137f862022-01-04 18:27:40 +0200237 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200238}
239
240HWC2::Error HwcDisplay::AcceptDisplayChanges() {
241 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
242 l.second.AcceptTypeChange();
243 return HWC2::Error::None;
244}
245
246HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200247 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200248 *layer = static_cast<hwc2_layer_t>(layer_idx_);
249 ++layer_idx_;
250 return HWC2::Error::None;
251}
252
253HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200254 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200255 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200256 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200257
258 layers_.erase(layer);
259 return HWC2::Error::None;
260}
261
262HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200263 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200264 return HWC2::Error::BadConfig;
265
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200266 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200267 return HWC2::Error::None;
268}
269
270HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
271 hwc2_layer_t *layers,
272 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200273 if (IsInHeadlessMode()) {
274 *num_elements = 0;
275 return HWC2::Error::None;
276 }
277
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200278 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300279 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200280 if (l.second.IsTypeChanged()) {
281 if (layers && num_changes < *num_elements)
282 layers[num_changes] = l.first;
283 if (types && num_changes < *num_elements)
284 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
285 ++num_changes;
286 }
287 }
288 if (!layers && !types)
289 *num_elements = num_changes;
290 return HWC2::Error::None;
291}
292
293HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
294 int32_t /*format*/,
295 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200296 if (IsInHeadlessMode()) {
297 return HWC2::Error::None;
298 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200299
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300300 auto min = pipeline_->device->GetMinResolution();
301 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200302
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200303 if (width < min.first || height < min.second)
304 return HWC2::Error::Unsupported;
305
306 if (width > max.first || height > max.second)
307 return HWC2::Error::Unsupported;
308
309 if (dataspace != HAL_DATASPACE_UNKNOWN)
310 return HWC2::Error::Unsupported;
311
312 // TODO(nobody): Validate format can be handled by either GL or planes
313 return HWC2::Error::None;
314}
315
316HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
317 if (!modes)
318 *num_modes = 1;
319
320 if (modes)
321 *modes = HAL_COLOR_MODE_NATIVE;
322
323 return HWC2::Error::None;
324}
325
326HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
327 int32_t attribute_in,
328 int32_t *value) {
329 int conf = static_cast<int>(config);
330
Roman Stratiienko0137f862022-01-04 18:27:40 +0200331 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200332 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200333 return HWC2::Error::BadConfig;
334 }
335
Roman Stratiienko0137f862022-01-04 18:27:40 +0200336 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200337
338 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300339 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200340 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
341 switch (attribute) {
342 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200343 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200344 break;
345 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200346 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200347 break;
348 case HWC2::Attribute::VsyncPeriod:
349 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600350 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200351 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000352 case HWC2::Attribute::DpiY:
353 // ideally this should be vdisplay/mm_heigth, however mm_height
354 // comes from edid parsing and is highly unreliable. Viewing the
355 // rarity of anisotropic displays, falling back to a single value
356 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200357 case HWC2::Attribute::DpiX:
358 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200359 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
360 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200361 : -1;
362 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200363#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200364 case HWC2::Attribute::ConfigGroup:
365 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
366 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200367 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200368 break;
369#endif
370 default:
371 *value = -1;
372 return HWC2::Error::BadConfig;
373 }
374 return HWC2::Error::None;
375}
376
Drew Davenportf7e88332024-09-06 12:54:38 -0600377HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
378 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200379 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200380 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200381 if (hwc_config.second.disabled) {
382 continue;
383 }
384
385 if (configs != nullptr) {
386 if (idx >= *num_configs) {
387 break;
388 }
389 configs[idx] = hwc_config.second.id;
390 }
391
392 idx++;
393 }
394 *num_configs = idx;
395 return HWC2::Error::None;
396}
397
398HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
399 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200400 if (IsInHeadlessMode()) {
401 stream << "null-display";
402 } else {
403 stream << "display-" << GetPipe().connector->Get()->GetId();
404 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300405 auto string = stream.str();
406 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200407 if (!name) {
408 *size = length;
409 return HWC2::Error::None;
410 }
411
412 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
413 strncpy(name, string.c_str(), *size);
414 return HWC2::Error::None;
415}
416
417HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
418 uint32_t *num_elements,
419 hwc2_layer_t * /*layers*/,
420 int32_t * /*layer_requests*/) {
421 // TODO(nobody): I think virtual display should request
422 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
423 *num_elements = 0;
424 return HWC2::Error::None;
425}
426
427HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
428 *type = static_cast<int32_t>(type_);
429 return HWC2::Error::None;
430}
431
432HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
433 *support = 0;
434 return HWC2::Error::None;
435}
436
437HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
438 int32_t * /*types*/,
439 float * /*max_luminance*/,
440 float * /*max_average_luminance*/,
441 float * /*min_luminance*/) {
442 *num_types = 0;
443 return HWC2::Error::None;
444}
445
446/* Find API details at:
447 * 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 +0300448 *
449 * Called after PresentDisplay(), CLIENT is expecting release fence for the
450 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200451 */
452HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
453 hwc2_layer_t *layers,
454 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200455 if (IsInHeadlessMode()) {
456 *num_elements = 0;
457 return HWC2::Error::None;
458 }
459
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200460 uint32_t num_layers = 0;
461
Roman Stratiienkodd214942022-05-03 18:24:49 +0300462 for (auto &l : layers_) {
463 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
464 continue;
465 }
466
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200467 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300468
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200469 if (layers == nullptr || fences == nullptr)
470 continue;
471
472 if (num_layers > *num_elements) {
473 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
474 return HWC2::Error::None;
475 }
476
477 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200478 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479 }
480 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300481
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200482 return HWC2::Error::None;
483}
484
485HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200486 if (IsInHeadlessMode()) {
487 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
488 return HWC2::Error::None;
489 }
490
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200491 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400492 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400493 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200494
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200495 uint32_t prev_vperiod_ns = 0;
496 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200497
498 auto mode_update_commited_ = false;
499 if (staged_mode_ &&
500 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
501 client_layer_.SetLayerDisplayFrame(
502 (hwc_rect_t){.left = 0,
503 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200504 .right = int(staged_mode_->GetRawMode().hdisplay),
505 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200506
507 configs_.active_config_id = staged_mode_config_id_;
508
509 a_args.display_mode = *staged_mode_;
510 if (!a_args.test_only) {
511 mode_update_commited_ = true;
512 }
513 }
514
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 // order the layers by z-order
516 bool use_client_layer = false;
517 uint32_t client_z_order = UINT32_MAX;
518 std::map<uint32_t, HwcLayer *> z_map;
519 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
520 switch (l.second.GetValidatedType()) {
521 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300522 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200523 break;
524 case HWC2::Composition::Client:
525 // Place it at the z_order of the lowest client layer
526 use_client_layer = true;
527 client_z_order = std::min(client_z_order, l.second.GetZOrder());
528 break;
529 default:
530 continue;
531 }
532 }
533 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300534 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535
536 if (z_map.empty())
537 return HWC2::Error::BadLayer;
538
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200539 std::vector<LayerData> composition_layers;
540
541 /* Import & populate */
542 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200543 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200544 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200545
546 // now that they're ordered by z, add them to the composition
547 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200548 if (!l.second->IsLayerUsableAsDevice()) {
549 /* This will be normally triggered on validation of the first frame
550 * containing CLIENT layer. At this moment client buffer is not yet
551 * provided by the CLIENT.
552 * This may be triggered once in HwcLayer lifecycle in case FB can't be
553 * imported. For example when non-contiguous buffer is imported into
554 * contiguous-only DRM/KMS driver.
555 */
556 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200557 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200558 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200559 }
560
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200561 /* Store plan to ensure shared planes won't be stolen by other display
562 * in between of ValidateDisplay() and PresentDisplay() calls
563 */
564 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
565 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300566
567 if (type_ == HWC2::DisplayType::Virtual) {
568 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
569 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
570 .acquire_fence;
571 }
572
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200573 if (!current_plan_) {
574 if (!a_args.test_only) {
575 ALOGE("Failed to create DrmKmsPlan");
576 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577 return HWC2::Error::BadConfig;
578 }
579
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200580 a_args.composition = current_plan_;
581
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300582 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200583
584 if (ret) {
585 if (!a_args.test_only)
586 ALOGE("Failed to apply the frame composition ret=%d", ret);
587 return HWC2::Error::BadParameter;
588 }
589
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200590 if (mode_update_commited_) {
591 staged_mode_.reset();
592 vsync_tracking_en_ = false;
593 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000594 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
595 last_vsync_ts_ +
596 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200597 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200598 }
599
600 return HWC2::Error::None;
601}
602
603/* Find API details at:
604 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
605 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300606HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200607 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300608 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200609 return HWC2::Error::None;
610 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200611 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200612
613 ++total_stats_.total_frames_;
614
615 AtomicCommitArgs a_args{};
616 ret = CreateComposition(a_args);
617
618 if (ret != HWC2::Error::None)
619 ++total_stats_.failed_kms_present_;
620
621 if (ret == HWC2::Error::BadLayer) {
622 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300623 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200624 return HWC2::Error::None;
625 }
626 if (ret != HWC2::Error::None)
627 return ret;
628
Roman Stratiienko76892782023-01-16 17:15:53 +0200629 this->present_fence_ = a_args.out_fence;
630 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200631
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200632 // Reset the color matrix so we don't apply it over and over again.
633 color_matrix_ = {};
634
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 ++frame_no_;
636 return HWC2::Error::None;
637}
638
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200639HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
640 int64_t change_time) {
641 if (configs_.hwc_configs.count(config) == 0) {
642 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200643 return HWC2::Error::BadConfig;
644 }
645
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200646 staged_mode_ = configs_.hwc_configs[config].mode;
647 staged_mode_change_time_ = change_time;
648 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649
650 return HWC2::Error::None;
651}
652
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200653HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
654 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
655}
656
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200657/* Find API details at:
658 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
659 */
660HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
661 int32_t acquire_fence,
662 int32_t dataspace,
663 hwc_region_t /*damage*/) {
664 client_layer_.SetLayerBuffer(target, acquire_fence);
665 client_layer_.SetLayerDataspace(dataspace);
666
667 /*
668 * target can be nullptr, this does mean the Composer Service is calling
669 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
670 * 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
671 */
672 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300673 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200674 return HWC2::Error::None;
675 }
676
Roman Stratiienko5070d512022-05-30 13:41:20 +0300677 if (IsInHeadlessMode()) {
678 return HWC2::Error::None;
679 }
680
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200681 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200682 if (!client_layer_.IsLayerUsableAsDevice()) {
683 ALOGE("Client layer must be always usable by DRM/KMS");
684 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200685 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200686
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200687 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300688 if (!bi) {
689 ALOGE("%s: Invalid state", __func__);
690 return HWC2::Error::BadLayer;
691 }
692
693 auto source_crop = (hwc_frect_t){.left = 0.0F,
694 .top = 0.0F,
695 .right = static_cast<float>(bi->width),
696 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200697 client_layer_.SetLayerSourceCrop(source_crop);
698
699 return HWC2::Error::None;
700}
701
702HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400703 /* Maps to the Colorspace DRM connector property:
704 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
705 */
706 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200707 return HWC2::Error::BadParameter;
708
Sasha McIntosh5294f092024-09-18 18:14:54 -0400709 switch (mode) {
710 case HAL_COLOR_MODE_NATIVE:
711 colorspace_ = Colorspace::kDefault;
712 break;
713 case HAL_COLOR_MODE_STANDARD_BT601_625:
714 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
715 case HAL_COLOR_MODE_STANDARD_BT601_525:
716 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
717 // The DP spec does not say whether this is the 525 or the 625 line version.
718 colorspace_ = Colorspace::kBt601Ycc;
719 break;
720 case HAL_COLOR_MODE_STANDARD_BT709:
721 case HAL_COLOR_MODE_SRGB:
722 colorspace_ = Colorspace::kBt709Ycc;
723 break;
724 case HAL_COLOR_MODE_DCI_P3:
725 case HAL_COLOR_MODE_DISPLAY_P3:
726 colorspace_ = Colorspace::kDciP3RgbD65;
727 break;
728 case HAL_COLOR_MODE_ADOBE_RGB:
729 default:
730 return HWC2::Error::Unsupported;
731 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200732
733 color_mode_ = mode;
734 return HWC2::Error::None;
735}
736
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200737#include <xf86drmMode.h>
738
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400739static uint64_t To3132FixPt(float in) {
740 constexpr uint64_t kSignMask = (1ULL << 63);
741 constexpr uint64_t kValueMask = ~(1ULL << 63);
742 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
743 if (in < 0)
744 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
745 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
746}
747
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
749 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
750 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
751 return HWC2::Error::BadParameter;
752
753 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
754 return HWC2::Error::BadParameter;
755
756 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200757
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200758 if (IsInHeadlessMode())
759 return HWC2::Error::None;
760
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200761 if (!GetPipe().crtc->Get()->GetCtmProperty())
762 return HWC2::Error::None;
763
764 switch (color_transform_hint_) {
765 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400766 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200767 break;
768 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400769 // Without HW support, we cannot correctly process matrices with an offset.
770 for (int i = 12; i < 14; i++) {
771 if (matrix[i] != 0.F)
772 return HWC2::Error::Unsupported;
773 }
774
775 /* HAL provides a 4x4 float type matrix:
776 * | 0 1 2 3|
777 * | 4 5 6 7|
778 * | 8 9 10 11|
779 * |12 13 14 15|
780 *
781 * R_out = R*0 + G*4 + B*8 + 12
782 * G_out = R*1 + G*5 + B*9 + 13
783 * B_out = R*2 + G*6 + B*10 + 14
784 *
785 * DRM expects a 3x3 s31.32 fixed point matrix:
786 * out matrix in
787 * |R| |0 1 2| |R|
788 * |G| = |3 4 5| x |G|
789 * |B| |6 7 8| |B|
790 *
791 * R_out = R*0 + G*1 + B*2
792 * G_out = R*3 + G*4 + B*5
793 * B_out = R*6 + G*7 + B*8
794 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200795 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200796 for (int i = 0; i < kCtmCols; i++) {
797 for (int j = 0; j < kCtmRows; j++) {
798 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400799 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200800 }
801 }
802 break;
803 default:
804 return HWC2::Error::Unsupported;
805 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200806
807 return HWC2::Error::None;
808}
809
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200810bool HwcDisplay::CtmByGpu() {
811 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
812 return false;
813
814 if (GetPipe().crtc->Get()->GetCtmProperty())
815 return false;
816
Drew Davenport93443182023-12-14 09:25:45 +0000817 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200818 return false;
819
820 return true;
821}
822
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300823HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
824 int32_t release_fence) {
825 writeback_layer_->SetLayerBuffer(buffer, release_fence);
826 writeback_layer_->PopulateLayerData();
827 if (!writeback_layer_->IsLayerUsableAsDevice()) {
828 ALOGE("Output layer must be always usable by DRM/KMS");
829 return HWC2::Error::BadLayer;
830 }
831 /* TODO: Check if format is supported by writeback connector */
832 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200833}
834
835HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
836 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300837
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838 AtomicCommitArgs a_args{};
839
840 switch (mode) {
841 case HWC2::PowerMode::Off:
842 a_args.active = false;
843 break;
844 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300845 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200846 break;
847 case HWC2::PowerMode::Doze:
848 case HWC2::PowerMode::DozeSuspend:
849 return HWC2::Error::Unsupported;
850 default:
John Stultzffe783c2024-02-14 10:51:27 -0800851 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200852 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300853 }
854
855 if (IsInHeadlessMode()) {
856 return HWC2::Error::None;
857 }
858
Jia Ren80566fe2022-11-17 17:26:00 +0800859 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300860 /*
861 * Setting the display to active before we have a composition
862 * can break some drivers, so skip setting a_args.active to
863 * true, as the next composition frame will implicitly activate
864 * the display
865 */
866 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
867 ? HWC2::Error::None
868 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200869 };
870
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300871 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200872 if (err) {
873 ALOGE("Failed to apply the dpms composition err=%d", err);
874 return HWC2::Error::BadParameter;
875 }
876 return HWC2::Error::None;
877}
878
879HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300880 if (type_ == HWC2::DisplayType::Virtual) {
881 return HWC2::Error::None;
882 }
883
Roman Stratiienko099c3112022-01-20 11:50:54 +0200884 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
885 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200886 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200887 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200888 return HWC2::Error::None;
889}
890
891HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
892 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200893 if (IsInHeadlessMode()) {
894 *num_types = *num_requests = 0;
895 return HWC2::Error::None;
896 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300897
898 /* In current drm_hwc design in case previous frame layer was not validated as
899 * a CLIENT, it is used by display controller (Front buffer). We have to store
900 * this state to provide the CLIENT with the release fences for such buffers.
901 */
902 for (auto &l : layers_) {
903 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
904 HWC2::Composition::Client);
905 }
906
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907 return backend_->ValidateDisplay(this, num_types, num_requests);
908}
909
910std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
911 std::vector<HwcLayer *> ordered_layers;
912 ordered_layers.reserve(layers_.size());
913
914 for (auto &[handle, layer] : layers_) {
915 ordered_layers.emplace_back(&layer);
916 }
917
918 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
919 [](const HwcLayer *lhs, const HwcLayer *rhs) {
920 return lhs->GetZOrder() < rhs->GetZOrder();
921 });
922
923 return ordered_layers;
924}
925
Roman Stratiienko099c3112022-01-20 11:50:54 +0200926HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
927 uint32_t *outVsyncPeriod /* ns */) {
928 return GetDisplayAttribute(configs_.active_config_id,
929 HWC2_ATTRIBUTE_VSYNC_PERIOD,
930 (int32_t *)(outVsyncPeriod));
931}
932
Roman Stratiienko6b405052022-12-10 19:09:10 +0200933#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200934HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200935 if (IsInHeadlessMode()) {
936 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
937 return HWC2::Error::None;
938 }
939 /* Primary display should be always internal,
940 * otherwise SF will be unhappy and will crash
941 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200942 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200943 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200944 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200945 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
946 else
947 return HWC2::Error::BadConfig;
948
949 return HWC2::Error::None;
950}
951
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200952HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200953 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200954 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
955 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300956 if (type_ == HWC2::DisplayType::Virtual) {
957 return HWC2::Error::None;
958 }
959
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
961 return HWC2::Error::BadParameter;
962 }
963
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200964 uint32_t current_vsync_period{};
965 GetDisplayVsyncPeriod(&current_vsync_period);
966
967 if (vsyncPeriodChangeConstraints->seamlessRequired) {
968 return HWC2::Error::SeamlessNotAllowed;
969 }
970
971 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
972 ->desiredTimeNanos -
973 current_vsync_period;
974 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
975 if (ret != HWC2::Error::None) {
976 return ret;
977 }
978
979 outTimeline->refreshRequired = true;
980 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
981 ->desiredTimeNanos;
982
983 last_vsync_ts_ = 0;
984 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200985 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200986
987 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200988}
989
990HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
991 return HWC2::Error::Unsupported;
992}
993
994HWC2::Error HwcDisplay::GetSupportedContentTypes(
995 uint32_t *outNumSupportedContentTypes,
996 const uint32_t *outSupportedContentTypes) {
997 if (outSupportedContentTypes == nullptr)
998 *outNumSupportedContentTypes = 0;
999
1000 return HWC2::Error::None;
1001}
1002
1003HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001004 /* Maps exactly to the content_type DRM connector property:
1005 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001006 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001007 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1008 return HWC2::Error::BadParameter;
1009
1010 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001011
1012 return HWC2::Error::None;
1013}
1014#endif
1015
Roman Stratiienko6b405052022-12-10 19:09:10 +02001016#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001017HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1018 uint32_t *outDataSize,
1019 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001020 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001021 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001022 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001023
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001024 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001025 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001026 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001027 }
1028
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001029 *outPort = handle_; /* TDOD(nobody): What should be here? */
1030
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001031 if (outData) {
1032 *outDataSize = std::min(*outDataSize, blob->length);
1033 memcpy(outData, blob->data, *outDataSize);
1034 } else {
1035 *outDataSize = blob->length;
1036 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001037
1038 return HWC2::Error::None;
1039}
1040
1041HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001042 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001043 if (outNumCapabilities == nullptr) {
1044 return HWC2::Error::BadParameter;
1045 }
1046
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001047 bool skip_ctm = false;
1048
1049 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001050 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001051 skip_ctm = true;
1052
1053 // Skip client CTM if DRM can handle it
1054 if (!skip_ctm && !IsInHeadlessMode() &&
1055 GetPipe().crtc->Get()->GetCtmProperty())
1056 skip_ctm = true;
1057
1058 if (!skip_ctm) {
1059 *outNumCapabilities = 0;
1060 return HWC2::Error::None;
1061 }
1062
1063 *outNumCapabilities = 1;
1064 if (outCapabilities) {
1065 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1066 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001067
1068 return HWC2::Error::None;
1069}
1070
1071HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1072 *supported = false;
1073 return HWC2::Error::None;
1074}
1075
1076HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1077 return HWC2::Error::Unsupported;
1078}
1079
Roman Stratiienko6b405052022-12-10 19:09:10 +02001080#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001081
Roman Stratiienko6b405052022-12-10 19:09:10 +02001082#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001083
1084HWC2::Error HwcDisplay::GetRenderIntents(
1085 int32_t mode, uint32_t *outNumIntents,
1086 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1087 if (mode != HAL_COLOR_MODE_NATIVE) {
1088 return HWC2::Error::BadParameter;
1089 }
1090
1091 if (outIntents == nullptr) {
1092 *outNumIntents = 1;
1093 return HWC2::Error::None;
1094 }
1095 *outNumIntents = 1;
1096 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1097 return HWC2::Error::None;
1098}
1099
1100HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1101 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1102 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1103 return HWC2::Error::BadParameter;
1104
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001105 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1106 return HWC2::Error::Unsupported;
1107
Sasha McIntosh5294f092024-09-18 18:14:54 -04001108 auto err = SetColorMode(mode);
1109 if (err != HWC2::Error::None) return err;
1110
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001111 return HWC2::Error::None;
1112}
1113
Roman Stratiienko6b405052022-12-10 19:09:10 +02001114#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001115
1116const Backend *HwcDisplay::backend() const {
1117 return backend_.get();
1118}
1119
1120void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1121 backend_ = std::move(backend);
1122}
1123
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001124} // namespace android