blob: 7f65e657a72a1324f00550e43ea41abc927bab80 [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"
Drew Davenport93443182023-12-14 09:25:45 +000027#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020028#include "utils/log.h"
29#include "utils/properties.h"
30
31namespace android {
32
33std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
34 if (delta.total_pixops_ == 0)
35 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030036 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020037
38 std::stringstream ss;
39 ss << " Total frames count: " << delta.total_frames_ << "\n"
40 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
41 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
42 << ((delta.failed_kms_present_ > 0)
43 ? " !!! Internal failure, FIX it please\n"
44 : "")
45 << " Flattened frames: " << delta.frames_flattened_ << "\n"
46 << " Pixel operations (free units)"
47 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
48 << "]\n"
49 << " Composition efficiency: " << ratio;
50
51 return ss.str();
52}
53
54std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030055 auto connector_name = IsInHeadlessMode()
56 ? std::string("NULL-DISPLAY")
57 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020058
Roman Stratiienko3627beb2022-01-04 16:02:55 +020059 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020060 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020061 << "Statistics since system boot:\n"
62 << DumpDelta(total_stats_) << "\n\n"
63 << "Statistics since last dumpsys request:\n"
64 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
65
66 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
67 return ss.str();
68}
69
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020070HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +000071 DrmHwc *hwc)
72 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030073 if (type_ == HWC2::DisplayType::Virtual) {
74 writeback_layer_ = std::make_unique<HwcLayer>(this);
75 }
76}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020077
Sasha McIntosha37df7c2024-09-20 12:31:08 -040078void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020079 color_matrix_ = std::make_shared<drm_color_ctm>();
80 for (int i = 0; i < kCtmCols; i++) {
81 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080082 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020083 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
84 }
85 }
86
87 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020088}
89
Normunds Rieksts545096d2024-03-11 16:37:45 +000090HwcDisplay::~HwcDisplay() {
91 Deinit();
92};
Roman Stratiienko3dacd472022-01-11 19:18:34 +020093
Roman Stratiienko63762a92023-09-18 22:33:45 +030094void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +020095 Deinit();
96
Roman Stratiienko63762a92023-09-18 22:33:45 +030097 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020098
Roman Stratiienko63762a92023-09-18 22:33:45 +030099 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200100 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000101 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200102 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000103 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200104 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200105}
106
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200107void HwcDisplay::Deinit() {
108 if (pipeline_ != nullptr) {
109 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200110 a_args.composition = std::make_shared<DrmKmsPlan>();
111 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000112/*
113 * TODO:
114 * Unfortunately the following causes regressions on db845c
115 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
116 * never coming back. Patches to avoiding that issue on the
117 * the kernel side unfortunately causes further crashes in
118 * drm_hwcomposer, because the client detach takes longer then the
119 * 1 second max VTS expects. So for now as a workaround, lets skip
120 * deactivating the display on deinit, which matches previous
121 * behavior prior to commit d0494d9b8097
122 */
123#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300124 a_args.composition = {};
125 a_args.active = false;
126 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000127#endif
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
199HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300201 if (type_ == HWC2::DisplayType::Virtual) {
202 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
203 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200204 err = configs_.Update(*pipeline_->connector->Get());
205 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300206 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200207 }
208 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200210 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211
Roman Stratiienko0137f862022-01-04 18:27:40 +0200212 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200213}
214
215HWC2::Error HwcDisplay::AcceptDisplayChanges() {
216 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
217 l.second.AcceptTypeChange();
218 return HWC2::Error::None;
219}
220
221HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200222 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223 *layer = static_cast<hwc2_layer_t>(layer_idx_);
224 ++layer_idx_;
225 return HWC2::Error::None;
226}
227
228HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200229 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200231 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232
233 layers_.erase(layer);
234 return HWC2::Error::None;
235}
236
237HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200238 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200239 return HWC2::Error::BadConfig;
240
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200241 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200242 return HWC2::Error::None;
243}
244
245HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
246 hwc2_layer_t *layers,
247 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200248 if (IsInHeadlessMode()) {
249 *num_elements = 0;
250 return HWC2::Error::None;
251 }
252
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200253 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300254 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200255 if (l.second.IsTypeChanged()) {
256 if (layers && num_changes < *num_elements)
257 layers[num_changes] = l.first;
258 if (types && num_changes < *num_elements)
259 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
260 ++num_changes;
261 }
262 }
263 if (!layers && !types)
264 *num_elements = num_changes;
265 return HWC2::Error::None;
266}
267
268HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
269 int32_t /*format*/,
270 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200271 if (IsInHeadlessMode()) {
272 return HWC2::Error::None;
273 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200274
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300275 auto min = pipeline_->device->GetMinResolution();
276 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200277
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200278 if (width < min.first || height < min.second)
279 return HWC2::Error::Unsupported;
280
281 if (width > max.first || height > max.second)
282 return HWC2::Error::Unsupported;
283
284 if (dataspace != HAL_DATASPACE_UNKNOWN)
285 return HWC2::Error::Unsupported;
286
287 // TODO(nobody): Validate format can be handled by either GL or planes
288 return HWC2::Error::None;
289}
290
291HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
292 if (!modes)
293 *num_modes = 1;
294
295 if (modes)
296 *modes = HAL_COLOR_MODE_NATIVE;
297
298 return HWC2::Error::None;
299}
300
301HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
302 int32_t attribute_in,
303 int32_t *value) {
304 int conf = static_cast<int>(config);
305
Roman Stratiienko0137f862022-01-04 18:27:40 +0200306 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200307 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200308 return HWC2::Error::BadConfig;
309 }
310
Roman Stratiienko0137f862022-01-04 18:27:40 +0200311 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200312
313 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300314 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
316 switch (attribute) {
317 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200318 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200319 break;
320 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200321 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200322 break;
323 case HWC2::Attribute::VsyncPeriod:
324 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600325 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200326 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000327 case HWC2::Attribute::DpiY:
328 // ideally this should be vdisplay/mm_heigth, however mm_height
329 // comes from edid parsing and is highly unreliable. Viewing the
330 // rarity of anisotropic displays, falling back to a single value
331 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200332 case HWC2::Attribute::DpiX:
333 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200334 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
335 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200336 : -1;
337 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200338#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 case HWC2::Attribute::ConfigGroup:
340 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
341 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200342 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200343 break;
344#endif
345 default:
346 *value = -1;
347 return HWC2::Error::BadConfig;
348 }
349 return HWC2::Error::None;
350}
351
Drew Davenportf7e88332024-09-06 12:54:38 -0600352HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
353 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200354 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200355 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200356 if (hwc_config.second.disabled) {
357 continue;
358 }
359
360 if (configs != nullptr) {
361 if (idx >= *num_configs) {
362 break;
363 }
364 configs[idx] = hwc_config.second.id;
365 }
366
367 idx++;
368 }
369 *num_configs = idx;
370 return HWC2::Error::None;
371}
372
373HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
374 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200375 if (IsInHeadlessMode()) {
376 stream << "null-display";
377 } else {
378 stream << "display-" << GetPipe().connector->Get()->GetId();
379 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300380 auto string = stream.str();
381 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200382 if (!name) {
383 *size = length;
384 return HWC2::Error::None;
385 }
386
387 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
388 strncpy(name, string.c_str(), *size);
389 return HWC2::Error::None;
390}
391
392HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
393 uint32_t *num_elements,
394 hwc2_layer_t * /*layers*/,
395 int32_t * /*layer_requests*/) {
396 // TODO(nobody): I think virtual display should request
397 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
398 *num_elements = 0;
399 return HWC2::Error::None;
400}
401
402HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
403 *type = static_cast<int32_t>(type_);
404 return HWC2::Error::None;
405}
406
407HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
408 *support = 0;
409 return HWC2::Error::None;
410}
411
412HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
413 int32_t * /*types*/,
414 float * /*max_luminance*/,
415 float * /*max_average_luminance*/,
416 float * /*min_luminance*/) {
417 *num_types = 0;
418 return HWC2::Error::None;
419}
420
421/* Find API details at:
422 * 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 +0300423 *
424 * Called after PresentDisplay(), CLIENT is expecting release fence for the
425 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200426 */
427HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
428 hwc2_layer_t *layers,
429 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200430 if (IsInHeadlessMode()) {
431 *num_elements = 0;
432 return HWC2::Error::None;
433 }
434
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200435 uint32_t num_layers = 0;
436
Roman Stratiienkodd214942022-05-03 18:24:49 +0300437 for (auto &l : layers_) {
438 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
439 continue;
440 }
441
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200442 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300443
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444 if (layers == nullptr || fences == nullptr)
445 continue;
446
447 if (num_layers > *num_elements) {
448 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
449 return HWC2::Error::None;
450 }
451
452 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200453 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200454 }
455 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300456
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200457 return HWC2::Error::None;
458}
459
460HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200461 if (IsInHeadlessMode()) {
462 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
463 return HWC2::Error::None;
464 }
465
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200466 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400467 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400468 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200469
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200470 uint32_t prev_vperiod_ns = 0;
471 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200472
473 auto mode_update_commited_ = false;
474 if (staged_mode_ &&
475 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
476 client_layer_.SetLayerDisplayFrame(
477 (hwc_rect_t){.left = 0,
478 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200479 .right = int(staged_mode_->GetRawMode().hdisplay),
480 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200481
482 configs_.active_config_id = staged_mode_config_id_;
483
484 a_args.display_mode = *staged_mode_;
485 if (!a_args.test_only) {
486 mode_update_commited_ = true;
487 }
488 }
489
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490 // order the layers by z-order
491 bool use_client_layer = false;
492 uint32_t client_z_order = UINT32_MAX;
493 std::map<uint32_t, HwcLayer *> z_map;
494 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
495 switch (l.second.GetValidatedType()) {
496 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300497 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200498 break;
499 case HWC2::Composition::Client:
500 // Place it at the z_order of the lowest client layer
501 use_client_layer = true;
502 client_z_order = std::min(client_z_order, l.second.GetZOrder());
503 break;
504 default:
505 continue;
506 }
507 }
508 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300509 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510
511 if (z_map.empty())
512 return HWC2::Error::BadLayer;
513
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200514 std::vector<LayerData> composition_layers;
515
516 /* Import & populate */
517 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200518 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200519 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200520
521 // now that they're ordered by z, add them to the composition
522 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200523 if (!l.second->IsLayerUsableAsDevice()) {
524 /* This will be normally triggered on validation of the first frame
525 * containing CLIENT layer. At this moment client buffer is not yet
526 * provided by the CLIENT.
527 * This may be triggered once in HwcLayer lifecycle in case FB can't be
528 * imported. For example when non-contiguous buffer is imported into
529 * contiguous-only DRM/KMS driver.
530 */
531 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200533 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200534 }
535
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200536 /* Store plan to ensure shared planes won't be stolen by other display
537 * in between of ValidateDisplay() and PresentDisplay() calls
538 */
539 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
540 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300541
542 if (type_ == HWC2::DisplayType::Virtual) {
543 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
544 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
545 .acquire_fence;
546 }
547
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200548 if (!current_plan_) {
549 if (!a_args.test_only) {
550 ALOGE("Failed to create DrmKmsPlan");
551 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200552 return HWC2::Error::BadConfig;
553 }
554
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200555 a_args.composition = current_plan_;
556
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300557 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200558
559 if (ret) {
560 if (!a_args.test_only)
561 ALOGE("Failed to apply the frame composition ret=%d", ret);
562 return HWC2::Error::BadParameter;
563 }
564
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200565 if (mode_update_commited_) {
566 staged_mode_.reset();
567 vsync_tracking_en_ = false;
568 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000569 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
570 last_vsync_ts_ +
571 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200572 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200573 }
574
575 return HWC2::Error::None;
576}
577
578/* Find API details at:
579 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
580 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300581HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200582 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300583 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200584 return HWC2::Error::None;
585 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200586 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200587
588 ++total_stats_.total_frames_;
589
590 AtomicCommitArgs a_args{};
591 ret = CreateComposition(a_args);
592
593 if (ret != HWC2::Error::None)
594 ++total_stats_.failed_kms_present_;
595
596 if (ret == HWC2::Error::BadLayer) {
597 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300598 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 return HWC2::Error::None;
600 }
601 if (ret != HWC2::Error::None)
602 return ret;
603
Roman Stratiienko76892782023-01-16 17:15:53 +0200604 this->present_fence_ = a_args.out_fence;
605 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200607 // Reset the color matrix so we don't apply it over and over again.
608 color_matrix_ = {};
609
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200610 ++frame_no_;
611 return HWC2::Error::None;
612}
613
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200614HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
615 int64_t change_time) {
616 if (configs_.hwc_configs.count(config) == 0) {
617 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 return HWC2::Error::BadConfig;
619 }
620
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200621 staged_mode_ = configs_.hwc_configs[config].mode;
622 staged_mode_change_time_ = change_time;
623 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200624
625 return HWC2::Error::None;
626}
627
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200628HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
629 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
630}
631
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632/* Find API details at:
633 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
634 */
635HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
636 int32_t acquire_fence,
637 int32_t dataspace,
638 hwc_region_t /*damage*/) {
639 client_layer_.SetLayerBuffer(target, acquire_fence);
640 client_layer_.SetLayerDataspace(dataspace);
641
642 /*
643 * target can be nullptr, this does mean the Composer Service is calling
644 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
645 * 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
646 */
647 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300648 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649 return HWC2::Error::None;
650 }
651
Roman Stratiienko5070d512022-05-30 13:41:20 +0300652 if (IsInHeadlessMode()) {
653 return HWC2::Error::None;
654 }
655
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200656 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200657 if (!client_layer_.IsLayerUsableAsDevice()) {
658 ALOGE("Client layer must be always usable by DRM/KMS");
659 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200660 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200661
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200662 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300663 if (!bi) {
664 ALOGE("%s: Invalid state", __func__);
665 return HWC2::Error::BadLayer;
666 }
667
668 auto source_crop = (hwc_frect_t){.left = 0.0F,
669 .top = 0.0F,
670 .right = static_cast<float>(bi->width),
671 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200672 client_layer_.SetLayerSourceCrop(source_crop);
673
674 return HWC2::Error::None;
675}
676
677HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400678 /* Maps to the Colorspace DRM connector property:
679 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
680 */
681 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200682 return HWC2::Error::BadParameter;
683
Sasha McIntosh5294f092024-09-18 18:14:54 -0400684 switch (mode) {
685 case HAL_COLOR_MODE_NATIVE:
686 colorspace_ = Colorspace::kDefault;
687 break;
688 case HAL_COLOR_MODE_STANDARD_BT601_625:
689 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
690 case HAL_COLOR_MODE_STANDARD_BT601_525:
691 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
692 // The DP spec does not say whether this is the 525 or the 625 line version.
693 colorspace_ = Colorspace::kBt601Ycc;
694 break;
695 case HAL_COLOR_MODE_STANDARD_BT709:
696 case HAL_COLOR_MODE_SRGB:
697 colorspace_ = Colorspace::kBt709Ycc;
698 break;
699 case HAL_COLOR_MODE_DCI_P3:
700 case HAL_COLOR_MODE_DISPLAY_P3:
701 colorspace_ = Colorspace::kDciP3RgbD65;
702 break;
703 case HAL_COLOR_MODE_ADOBE_RGB:
704 default:
705 return HWC2::Error::Unsupported;
706 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200707
708 color_mode_ = mode;
709 return HWC2::Error::None;
710}
711
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200712#include <xf86drmMode.h>
713
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400714static uint64_t To3132FixPt(float in) {
715 constexpr uint64_t kSignMask = (1ULL << 63);
716 constexpr uint64_t kValueMask = ~(1ULL << 63);
717 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
718 if (in < 0)
719 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
720 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
721}
722
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200723HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
724 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
725 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
726 return HWC2::Error::BadParameter;
727
728 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
729 return HWC2::Error::BadParameter;
730
731 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200732
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200733 if (IsInHeadlessMode())
734 return HWC2::Error::None;
735
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200736 if (!GetPipe().crtc->Get()->GetCtmProperty())
737 return HWC2::Error::None;
738
739 switch (color_transform_hint_) {
740 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400741 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200742 break;
743 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400744 // Without HW support, we cannot correctly process matrices with an offset.
745 for (int i = 12; i < 14; i++) {
746 if (matrix[i] != 0.F)
747 return HWC2::Error::Unsupported;
748 }
749
750 /* HAL provides a 4x4 float type matrix:
751 * | 0 1 2 3|
752 * | 4 5 6 7|
753 * | 8 9 10 11|
754 * |12 13 14 15|
755 *
756 * R_out = R*0 + G*4 + B*8 + 12
757 * G_out = R*1 + G*5 + B*9 + 13
758 * B_out = R*2 + G*6 + B*10 + 14
759 *
760 * DRM expects a 3x3 s31.32 fixed point matrix:
761 * out matrix in
762 * |R| |0 1 2| |R|
763 * |G| = |3 4 5| x |G|
764 * |B| |6 7 8| |B|
765 *
766 * R_out = R*0 + G*1 + B*2
767 * G_out = R*3 + G*4 + B*5
768 * B_out = R*6 + G*7 + B*8
769 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200770 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200771 for (int i = 0; i < kCtmCols; i++) {
772 for (int j = 0; j < kCtmRows; j++) {
773 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400774 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200775 }
776 }
777 break;
778 default:
779 return HWC2::Error::Unsupported;
780 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200781
782 return HWC2::Error::None;
783}
784
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200785bool HwcDisplay::CtmByGpu() {
786 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
787 return false;
788
789 if (GetPipe().crtc->Get()->GetCtmProperty())
790 return false;
791
Drew Davenport93443182023-12-14 09:25:45 +0000792 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200793 return false;
794
795 return true;
796}
797
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300798HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
799 int32_t release_fence) {
800 writeback_layer_->SetLayerBuffer(buffer, release_fence);
801 writeback_layer_->PopulateLayerData();
802 if (!writeback_layer_->IsLayerUsableAsDevice()) {
803 ALOGE("Output layer must be always usable by DRM/KMS");
804 return HWC2::Error::BadLayer;
805 }
806 /* TODO: Check if format is supported by writeback connector */
807 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200808}
809
810HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
811 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300812
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200813 AtomicCommitArgs a_args{};
814
815 switch (mode) {
816 case HWC2::PowerMode::Off:
817 a_args.active = false;
818 break;
819 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300820 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200821 break;
822 case HWC2::PowerMode::Doze:
823 case HWC2::PowerMode::DozeSuspend:
824 return HWC2::Error::Unsupported;
825 default:
John Stultzffe783c2024-02-14 10:51:27 -0800826 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200827 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300828 }
829
830 if (IsInHeadlessMode()) {
831 return HWC2::Error::None;
832 }
833
Jia Ren80566fe2022-11-17 17:26:00 +0800834 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300835 /*
836 * Setting the display to active before we have a composition
837 * can break some drivers, so skip setting a_args.active to
838 * true, as the next composition frame will implicitly activate
839 * the display
840 */
841 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
842 ? HWC2::Error::None
843 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200844 };
845
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300846 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200847 if (err) {
848 ALOGE("Failed to apply the dpms composition err=%d", err);
849 return HWC2::Error::BadParameter;
850 }
851 return HWC2::Error::None;
852}
853
854HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300855 if (type_ == HWC2::DisplayType::Virtual) {
856 return HWC2::Error::None;
857 }
858
Roman Stratiienko099c3112022-01-20 11:50:54 +0200859 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
860 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200861 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200862 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200863 return HWC2::Error::None;
864}
865
866HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
867 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200868 if (IsInHeadlessMode()) {
869 *num_types = *num_requests = 0;
870 return HWC2::Error::None;
871 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300872
873 /* In current drm_hwc design in case previous frame layer was not validated as
874 * a CLIENT, it is used by display controller (Front buffer). We have to store
875 * this state to provide the CLIENT with the release fences for such buffers.
876 */
877 for (auto &l : layers_) {
878 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
879 HWC2::Composition::Client);
880 }
881
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882 return backend_->ValidateDisplay(this, num_types, num_requests);
883}
884
885std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
886 std::vector<HwcLayer *> ordered_layers;
887 ordered_layers.reserve(layers_.size());
888
889 for (auto &[handle, layer] : layers_) {
890 ordered_layers.emplace_back(&layer);
891 }
892
893 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
894 [](const HwcLayer *lhs, const HwcLayer *rhs) {
895 return lhs->GetZOrder() < rhs->GetZOrder();
896 });
897
898 return ordered_layers;
899}
900
Roman Stratiienko099c3112022-01-20 11:50:54 +0200901HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
902 uint32_t *outVsyncPeriod /* ns */) {
903 return GetDisplayAttribute(configs_.active_config_id,
904 HWC2_ATTRIBUTE_VSYNC_PERIOD,
905 (int32_t *)(outVsyncPeriod));
906}
907
Roman Stratiienko6b405052022-12-10 19:09:10 +0200908#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200909HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200910 if (IsInHeadlessMode()) {
911 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
912 return HWC2::Error::None;
913 }
914 /* Primary display should be always internal,
915 * otherwise SF will be unhappy and will crash
916 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200917 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200918 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200919 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200920 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
921 else
922 return HWC2::Error::BadConfig;
923
924 return HWC2::Error::None;
925}
926
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200928 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200929 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
930 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300931 if (type_ == HWC2::DisplayType::Virtual) {
932 return HWC2::Error::None;
933 }
934
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200935 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
936 return HWC2::Error::BadParameter;
937 }
938
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200939 uint32_t current_vsync_period{};
940 GetDisplayVsyncPeriod(&current_vsync_period);
941
942 if (vsyncPeriodChangeConstraints->seamlessRequired) {
943 return HWC2::Error::SeamlessNotAllowed;
944 }
945
946 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
947 ->desiredTimeNanos -
948 current_vsync_period;
949 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
950 if (ret != HWC2::Error::None) {
951 return ret;
952 }
953
954 outTimeline->refreshRequired = true;
955 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
956 ->desiredTimeNanos;
957
958 last_vsync_ts_ = 0;
959 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200960 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200961
962 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200963}
964
965HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
966 return HWC2::Error::Unsupported;
967}
968
969HWC2::Error HwcDisplay::GetSupportedContentTypes(
970 uint32_t *outNumSupportedContentTypes,
971 const uint32_t *outSupportedContentTypes) {
972 if (outSupportedContentTypes == nullptr)
973 *outNumSupportedContentTypes = 0;
974
975 return HWC2::Error::None;
976}
977
978HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -0400979 /* Maps exactly to the content_type DRM connector property:
980 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981 */
Sasha McIntosh173247b2024-09-18 18:06:52 -0400982 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
983 return HWC2::Error::BadParameter;
984
985 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200986
987 return HWC2::Error::None;
988}
989#endif
990
Roman Stratiienko6b405052022-12-10 19:09:10 +0200991#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200992HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
993 uint32_t *outDataSize,
994 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200995 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300996 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200997 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300998
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200999 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001000 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001001 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001002 }
1003
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001004 *outPort = handle_; /* TDOD(nobody): What should be here? */
1005
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001006 if (outData) {
1007 *outDataSize = std::min(*outDataSize, blob->length);
1008 memcpy(outData, blob->data, *outDataSize);
1009 } else {
1010 *outDataSize = blob->length;
1011 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001012
1013 return HWC2::Error::None;
1014}
1015
1016HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001017 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001018 if (outNumCapabilities == nullptr) {
1019 return HWC2::Error::BadParameter;
1020 }
1021
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001022 bool skip_ctm = false;
1023
1024 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001025 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001026 skip_ctm = true;
1027
1028 // Skip client CTM if DRM can handle it
1029 if (!skip_ctm && !IsInHeadlessMode() &&
1030 GetPipe().crtc->Get()->GetCtmProperty())
1031 skip_ctm = true;
1032
1033 if (!skip_ctm) {
1034 *outNumCapabilities = 0;
1035 return HWC2::Error::None;
1036 }
1037
1038 *outNumCapabilities = 1;
1039 if (outCapabilities) {
1040 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1041 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001042
1043 return HWC2::Error::None;
1044}
1045
1046HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1047 *supported = false;
1048 return HWC2::Error::None;
1049}
1050
1051HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1052 return HWC2::Error::Unsupported;
1053}
1054
Roman Stratiienko6b405052022-12-10 19:09:10 +02001055#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001056
Roman Stratiienko6b405052022-12-10 19:09:10 +02001057#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001058
1059HWC2::Error HwcDisplay::GetRenderIntents(
1060 int32_t mode, uint32_t *outNumIntents,
1061 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1062 if (mode != HAL_COLOR_MODE_NATIVE) {
1063 return HWC2::Error::BadParameter;
1064 }
1065
1066 if (outIntents == nullptr) {
1067 *outNumIntents = 1;
1068 return HWC2::Error::None;
1069 }
1070 *outNumIntents = 1;
1071 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1072 return HWC2::Error::None;
1073}
1074
1075HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1076 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1077 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1078 return HWC2::Error::BadParameter;
1079
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001080 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1081 return HWC2::Error::Unsupported;
1082
Sasha McIntosh5294f092024-09-18 18:14:54 -04001083 auto err = SetColorMode(mode);
1084 if (err != HWC2::Error::None) return err;
1085
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001086 return HWC2::Error::None;
1087}
1088
Roman Stratiienko6b405052022-12-10 19:09:10 +02001089#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001090
1091const Backend *HwcDisplay::backend() const {
1092 return backend_.get();
1093}
1094
1095void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1096 backend_ = std::move(backend);
1097}
1098
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001099} // namespace android