blob: 7772175ff1e73f691df1450a553989f03dc907a6 [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
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020022#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020023#include "backend/BackendManager.h"
24#include "bufferinfo/BufferInfoGetter.h"
Drew Davenport93443182023-12-14 09:25:45 +000025#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020026#include "utils/log.h"
27#include "utils/properties.h"
28
29namespace android {
30
31std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
32 if (delta.total_pixops_ == 0)
33 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030034 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020035
36 std::stringstream ss;
37 ss << " Total frames count: " << delta.total_frames_ << "\n"
38 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
39 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
40 << ((delta.failed_kms_present_ > 0)
41 ? " !!! Internal failure, FIX it please\n"
42 : "")
43 << " Flattened frames: " << delta.frames_flattened_ << "\n"
44 << " Pixel operations (free units)"
45 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
46 << "]\n"
47 << " Composition efficiency: " << ratio;
48
49 return ss.str();
50}
51
52std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030053 auto connector_name = IsInHeadlessMode()
54 ? std::string("NULL-DISPLAY")
55 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020056
Roman Stratiienko3627beb2022-01-04 16:02:55 +020057 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020058 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020059 << "Statistics since system boot:\n"
60 << DumpDelta(total_stats_) << "\n\n"
61 << "Statistics since last dumpsys request:\n"
62 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
63
64 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
65 return ss.str();
66}
67
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020068HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +000069 DrmHwc *hwc)
70 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030071 if (type_ == HWC2::DisplayType::Virtual) {
72 writeback_layer_ = std::make_unique<HwcLayer>(this);
73 }
74}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020075
Sasha McIntosha37df7c2024-09-20 12:31:08 -040076void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020077 color_matrix_ = std::make_shared<drm_color_ctm>();
78 for (int i = 0; i < kCtmCols; i++) {
79 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080080 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020081 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
82 }
83 }
84
85 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020086}
87
Normunds Rieksts545096d2024-03-11 16:37:45 +000088HwcDisplay::~HwcDisplay() {
89 Deinit();
90};
Roman Stratiienko3dacd472022-01-11 19:18:34 +020091
Roman Stratiienko63762a92023-09-18 22:33:45 +030092void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +020093 Deinit();
94
Roman Stratiienko63762a92023-09-18 22:33:45 +030095 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020096
Roman Stratiienko63762a92023-09-18 22:33:45 +030097 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020098 Init();
Drew Davenport93443182023-12-14 09:25:45 +000099 hwc_->ScheduleHotplugEvent(handle_, /*connected = */ true);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200100 } else {
Drew Davenport93443182023-12-14 09:25:45 +0000101 hwc_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200102 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200103}
104
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200105void HwcDisplay::Deinit() {
106 if (pipeline_ != nullptr) {
107 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200108 a_args.composition = std::make_shared<DrmKmsPlan>();
109 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000110/*
111 * TODO:
112 * Unfortunately the following causes regressions on db845c
113 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
114 * never coming back. Patches to avoiding that issue on the
115 * the kernel side unfortunately causes further crashes in
116 * drm_hwcomposer, because the client detach takes longer then the
117 * 1 second max VTS expects. So for now as a workaround, lets skip
118 * deactivating the display on deinit, which matches previous
119 * behavior prior to commit d0494d9b8097
120 */
121#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300122 a_args.composition = {};
123 a_args.active = false;
124 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000125#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200126
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200127 current_plan_.reset();
128 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200129 if (flatcon_) {
130 flatcon_->StopThread();
131 flatcon_.reset();
132 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200133 }
134
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200135 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600136 // TODO: There should be a mechanism to wait for this worker to complete,
137 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200138 vsync_worker_->StopThread();
139 vsync_worker_ = {};
140 }
141
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200142 SetClientTarget(nullptr, -1, 0, {});
143}
144
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200145HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200146 ChosePreferredConfig();
147
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200148 auto vsw_callbacks = (VSyncWorkerCallbacks){
149 .out_event =
150 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000151 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200152 if (vsync_event_en_) {
153 uint32_t period_ns{};
154 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000155 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200156 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200157 if (vsync_tracking_en_) {
158 last_vsync_ts_ = timestamp;
159 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200160 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200161 vsync_worker_->VSyncControl(false);
162 }
163 },
164 .get_vperiod_ns = [this]() -> uint32_t {
165 uint32_t outVsyncPeriod = 0;
166 GetDisplayVsyncPeriod(&outVsyncPeriod);
167 return outVsyncPeriod;
168 },
169 };
170
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300171 if (type_ != HWC2::DisplayType::Virtual) {
172 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
173 if (!vsync_worker_) {
174 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
175 return HWC2::Error::BadDisplay;
176 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200177 }
178
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200179 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200180 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200181 if (ret) {
182 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
183 return HWC2::Error::BadDisplay;
184 }
Drew Davenport93443182023-12-14 09:25:45 +0000185 auto flatcbk = (struct FlatConCallbacks){
186 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200187 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200188 }
189
190 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
191
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400192 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200193
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200194 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195}
196
197HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200198 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300199 if (type_ == HWC2::DisplayType::Virtual) {
200 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
201 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200202 err = configs_.Update(*pipeline_->connector->Get());
203 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300204 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200205 }
206 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200208 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209
Roman Stratiienko0137f862022-01-04 18:27:40 +0200210 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211}
212
213HWC2::Error HwcDisplay::AcceptDisplayChanges() {
214 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
215 l.second.AcceptTypeChange();
216 return HWC2::Error::None;
217}
218
219HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200220 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200221 *layer = static_cast<hwc2_layer_t>(layer_idx_);
222 ++layer_idx_;
223 return HWC2::Error::None;
224}
225
226HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200227 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200228 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200229 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230
231 layers_.erase(layer);
232 return HWC2::Error::None;
233}
234
235HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200236 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200237 return HWC2::Error::BadConfig;
238
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200239 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200240 return HWC2::Error::None;
241}
242
243HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
244 hwc2_layer_t *layers,
245 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200246 if (IsInHeadlessMode()) {
247 *num_elements = 0;
248 return HWC2::Error::None;
249 }
250
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200251 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300252 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200253 if (l.second.IsTypeChanged()) {
254 if (layers && num_changes < *num_elements)
255 layers[num_changes] = l.first;
256 if (types && num_changes < *num_elements)
257 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
258 ++num_changes;
259 }
260 }
261 if (!layers && !types)
262 *num_elements = num_changes;
263 return HWC2::Error::None;
264}
265
266HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
267 int32_t /*format*/,
268 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200269 if (IsInHeadlessMode()) {
270 return HWC2::Error::None;
271 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200272
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300273 auto min = pipeline_->device->GetMinResolution();
274 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200275
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200276 if (width < min.first || height < min.second)
277 return HWC2::Error::Unsupported;
278
279 if (width > max.first || height > max.second)
280 return HWC2::Error::Unsupported;
281
282 if (dataspace != HAL_DATASPACE_UNKNOWN)
283 return HWC2::Error::Unsupported;
284
285 // TODO(nobody): Validate format can be handled by either GL or planes
286 return HWC2::Error::None;
287}
288
289HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
290 if (!modes)
291 *num_modes = 1;
292
293 if (modes)
294 *modes = HAL_COLOR_MODE_NATIVE;
295
296 return HWC2::Error::None;
297}
298
299HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
300 int32_t attribute_in,
301 int32_t *value) {
302 int conf = static_cast<int>(config);
303
Roman Stratiienko0137f862022-01-04 18:27:40 +0200304 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200305 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200306 return HWC2::Error::BadConfig;
307 }
308
Roman Stratiienko0137f862022-01-04 18:27:40 +0200309 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200310
311 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300312 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200313 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
314 switch (attribute) {
315 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200316 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200317 break;
318 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200319 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200320 break;
321 case HWC2::Attribute::VsyncPeriod:
322 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600323 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200324 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000325 case HWC2::Attribute::DpiY:
326 // ideally this should be vdisplay/mm_heigth, however mm_height
327 // comes from edid parsing and is highly unreliable. Viewing the
328 // rarity of anisotropic displays, falling back to a single value
329 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330 case HWC2::Attribute::DpiX:
331 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200332 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
333 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200334 : -1;
335 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200336#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200337 case HWC2::Attribute::ConfigGroup:
338 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
339 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200340 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 break;
342#endif
343 default:
344 *value = -1;
345 return HWC2::Error::BadConfig;
346 }
347 return HWC2::Error::None;
348}
349
Drew Davenportf7e88332024-09-06 12:54:38 -0600350HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
351 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200352 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200353 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200354 if (hwc_config.second.disabled) {
355 continue;
356 }
357
358 if (configs != nullptr) {
359 if (idx >= *num_configs) {
360 break;
361 }
362 configs[idx] = hwc_config.second.id;
363 }
364
365 idx++;
366 }
367 *num_configs = idx;
368 return HWC2::Error::None;
369}
370
371HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
372 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200373 if (IsInHeadlessMode()) {
374 stream << "null-display";
375 } else {
376 stream << "display-" << GetPipe().connector->Get()->GetId();
377 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300378 auto string = stream.str();
379 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200380 if (!name) {
381 *size = length;
382 return HWC2::Error::None;
383 }
384
385 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
386 strncpy(name, string.c_str(), *size);
387 return HWC2::Error::None;
388}
389
390HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
391 uint32_t *num_elements,
392 hwc2_layer_t * /*layers*/,
393 int32_t * /*layer_requests*/) {
394 // TODO(nobody): I think virtual display should request
395 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
396 *num_elements = 0;
397 return HWC2::Error::None;
398}
399
400HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
401 *type = static_cast<int32_t>(type_);
402 return HWC2::Error::None;
403}
404
405HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
406 *support = 0;
407 return HWC2::Error::None;
408}
409
410HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
411 int32_t * /*types*/,
412 float * /*max_luminance*/,
413 float * /*max_average_luminance*/,
414 float * /*min_luminance*/) {
415 *num_types = 0;
416 return HWC2::Error::None;
417}
418
419/* Find API details at:
420 * 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 +0300421 *
422 * Called after PresentDisplay(), CLIENT is expecting release fence for the
423 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200424 */
425HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
426 hwc2_layer_t *layers,
427 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200428 if (IsInHeadlessMode()) {
429 *num_elements = 0;
430 return HWC2::Error::None;
431 }
432
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200433 uint32_t num_layers = 0;
434
Roman Stratiienkodd214942022-05-03 18:24:49 +0300435 for (auto &l : layers_) {
436 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
437 continue;
438 }
439
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200440 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300441
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200442 if (layers == nullptr || fences == nullptr)
443 continue;
444
445 if (num_layers > *num_elements) {
446 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
447 return HWC2::Error::None;
448 }
449
450 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200451 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200452 }
453 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300454
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200455 return HWC2::Error::None;
456}
457
458HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200459 if (IsInHeadlessMode()) {
460 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
461 return HWC2::Error::None;
462 }
463
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200464 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400465 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400466 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200467
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200468 uint32_t prev_vperiod_ns = 0;
469 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200470
471 auto mode_update_commited_ = false;
472 if (staged_mode_ &&
473 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
474 client_layer_.SetLayerDisplayFrame(
475 (hwc_rect_t){.left = 0,
476 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200477 .right = int(staged_mode_->GetRawMode().hdisplay),
478 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200479
480 configs_.active_config_id = staged_mode_config_id_;
481
482 a_args.display_mode = *staged_mode_;
483 if (!a_args.test_only) {
484 mode_update_commited_ = true;
485 }
486 }
487
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 // order the layers by z-order
489 bool use_client_layer = false;
490 uint32_t client_z_order = UINT32_MAX;
491 std::map<uint32_t, HwcLayer *> z_map;
492 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
493 switch (l.second.GetValidatedType()) {
494 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300495 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 break;
497 case HWC2::Composition::Client:
498 // Place it at the z_order of the lowest client layer
499 use_client_layer = true;
500 client_z_order = std::min(client_z_order, l.second.GetZOrder());
501 break;
502 default:
503 continue;
504 }
505 }
506 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300507 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200508
509 if (z_map.empty())
510 return HWC2::Error::BadLayer;
511
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200512 std::vector<LayerData> composition_layers;
513
514 /* Import & populate */
515 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200516 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200517 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518
519 // now that they're ordered by z, add them to the composition
520 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200521 if (!l.second->IsLayerUsableAsDevice()) {
522 /* This will be normally triggered on validation of the first frame
523 * containing CLIENT layer. At this moment client buffer is not yet
524 * provided by the CLIENT.
525 * This may be triggered once in HwcLayer lifecycle in case FB can't be
526 * imported. For example when non-contiguous buffer is imported into
527 * contiguous-only DRM/KMS driver.
528 */
529 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200530 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200531 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 }
533
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200534 /* Store plan to ensure shared planes won't be stolen by other display
535 * in between of ValidateDisplay() and PresentDisplay() calls
536 */
537 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
538 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300539
540 if (type_ == HWC2::DisplayType::Virtual) {
541 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
542 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
543 .acquire_fence;
544 }
545
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200546 if (!current_plan_) {
547 if (!a_args.test_only) {
548 ALOGE("Failed to create DrmKmsPlan");
549 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200550 return HWC2::Error::BadConfig;
551 }
552
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200553 a_args.composition = current_plan_;
554
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300555 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200556
557 if (ret) {
558 if (!a_args.test_only)
559 ALOGE("Failed to apply the frame composition ret=%d", ret);
560 return HWC2::Error::BadParameter;
561 }
562
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200563 if (mode_update_commited_) {
564 staged_mode_.reset();
565 vsync_tracking_en_ = false;
566 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000567 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
568 last_vsync_ts_ +
569 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200570 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200571 }
572
573 return HWC2::Error::None;
574}
575
576/* Find API details at:
577 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
578 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300579HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200580 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300581 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200582 return HWC2::Error::None;
583 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200584 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200585
586 ++total_stats_.total_frames_;
587
588 AtomicCommitArgs a_args{};
589 ret = CreateComposition(a_args);
590
591 if (ret != HWC2::Error::None)
592 ++total_stats_.failed_kms_present_;
593
594 if (ret == HWC2::Error::BadLayer) {
595 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300596 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200597 return HWC2::Error::None;
598 }
599 if (ret != HWC2::Error::None)
600 return ret;
601
Roman Stratiienko76892782023-01-16 17:15:53 +0200602 this->present_fence_ = a_args.out_fence;
603 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200605 // Reset the color matrix so we don't apply it over and over again.
606 color_matrix_ = {};
607
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 ++frame_no_;
609 return HWC2::Error::None;
610}
611
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200612HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
613 int64_t change_time) {
614 if (configs_.hwc_configs.count(config) == 0) {
615 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200616 return HWC2::Error::BadConfig;
617 }
618
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200619 staged_mode_ = configs_.hwc_configs[config].mode;
620 staged_mode_change_time_ = change_time;
621 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622
623 return HWC2::Error::None;
624}
625
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200626HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
627 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
628}
629
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200630/* Find API details at:
631 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
632 */
633HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
634 int32_t acquire_fence,
635 int32_t dataspace,
636 hwc_region_t /*damage*/) {
637 client_layer_.SetLayerBuffer(target, acquire_fence);
638 client_layer_.SetLayerDataspace(dataspace);
639
640 /*
641 * target can be nullptr, this does mean the Composer Service is calling
642 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
643 * 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
644 */
645 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300646 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200647 return HWC2::Error::None;
648 }
649
Roman Stratiienko5070d512022-05-30 13:41:20 +0300650 if (IsInHeadlessMode()) {
651 return HWC2::Error::None;
652 }
653
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200654 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200655 if (!client_layer_.IsLayerUsableAsDevice()) {
656 ALOGE("Client layer must be always usable by DRM/KMS");
657 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200658 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200659
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200660 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300661 if (!bi) {
662 ALOGE("%s: Invalid state", __func__);
663 return HWC2::Error::BadLayer;
664 }
665
666 auto source_crop = (hwc_frect_t){.left = 0.0F,
667 .top = 0.0F,
668 .right = static_cast<float>(bi->width),
669 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200670 client_layer_.SetLayerSourceCrop(source_crop);
671
672 return HWC2::Error::None;
673}
674
675HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400676 /* Maps to the Colorspace DRM connector property:
677 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
678 */
679 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200680 return HWC2::Error::BadParameter;
681
Sasha McIntosh5294f092024-09-18 18:14:54 -0400682 switch (mode) {
683 case HAL_COLOR_MODE_NATIVE:
684 colorspace_ = Colorspace::kDefault;
685 break;
686 case HAL_COLOR_MODE_STANDARD_BT601_625:
687 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
688 case HAL_COLOR_MODE_STANDARD_BT601_525:
689 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
690 // The DP spec does not say whether this is the 525 or the 625 line version.
691 colorspace_ = Colorspace::kBt601Ycc;
692 break;
693 case HAL_COLOR_MODE_STANDARD_BT709:
694 case HAL_COLOR_MODE_SRGB:
695 colorspace_ = Colorspace::kBt709Ycc;
696 break;
697 case HAL_COLOR_MODE_DCI_P3:
698 case HAL_COLOR_MODE_DISPLAY_P3:
699 colorspace_ = Colorspace::kDciP3RgbD65;
700 break;
701 case HAL_COLOR_MODE_ADOBE_RGB:
702 default:
703 return HWC2::Error::Unsupported;
704 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200705
706 color_mode_ = mode;
707 return HWC2::Error::None;
708}
709
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200710#include <xf86drmMode.h>
711
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400712static uint64_t To3132FixPt(float in) {
713 constexpr uint64_t kSignMask = (1ULL << 63);
714 constexpr uint64_t kValueMask = ~(1ULL << 63);
715 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
716 if (in < 0)
717 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
718 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
719}
720
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200721HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
722 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
723 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
724 return HWC2::Error::BadParameter;
725
726 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
727 return HWC2::Error::BadParameter;
728
729 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200730
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200731 if (IsInHeadlessMode())
732 return HWC2::Error::None;
733
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200734 if (!GetPipe().crtc->Get()->GetCtmProperty())
735 return HWC2::Error::None;
736
737 switch (color_transform_hint_) {
738 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400739 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200740 break;
741 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400742 // Without HW support, we cannot correctly process matrices with an offset.
743 for (int i = 12; i < 14; i++) {
744 if (matrix[i] != 0.F)
745 return HWC2::Error::Unsupported;
746 }
747
748 /* HAL provides a 4x4 float type matrix:
749 * | 0 1 2 3|
750 * | 4 5 6 7|
751 * | 8 9 10 11|
752 * |12 13 14 15|
753 *
754 * R_out = R*0 + G*4 + B*8 + 12
755 * G_out = R*1 + G*5 + B*9 + 13
756 * B_out = R*2 + G*6 + B*10 + 14
757 *
758 * DRM expects a 3x3 s31.32 fixed point matrix:
759 * out matrix in
760 * |R| |0 1 2| |R|
761 * |G| = |3 4 5| x |G|
762 * |B| |6 7 8| |B|
763 *
764 * R_out = R*0 + G*1 + B*2
765 * G_out = R*3 + G*4 + B*5
766 * B_out = R*6 + G*7 + B*8
767 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200768 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200769 for (int i = 0; i < kCtmCols; i++) {
770 for (int j = 0; j < kCtmRows; j++) {
771 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400772 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200773 }
774 }
775 break;
776 default:
777 return HWC2::Error::Unsupported;
778 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200779
780 return HWC2::Error::None;
781}
782
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200783bool HwcDisplay::CtmByGpu() {
784 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
785 return false;
786
787 if (GetPipe().crtc->Get()->GetCtmProperty())
788 return false;
789
Drew Davenport93443182023-12-14 09:25:45 +0000790 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200791 return false;
792
793 return true;
794}
795
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300796HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
797 int32_t release_fence) {
798 writeback_layer_->SetLayerBuffer(buffer, release_fence);
799 writeback_layer_->PopulateLayerData();
800 if (!writeback_layer_->IsLayerUsableAsDevice()) {
801 ALOGE("Output layer must be always usable by DRM/KMS");
802 return HWC2::Error::BadLayer;
803 }
804 /* TODO: Check if format is supported by writeback connector */
805 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200806}
807
808HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
809 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300810
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200811 AtomicCommitArgs a_args{};
812
813 switch (mode) {
814 case HWC2::PowerMode::Off:
815 a_args.active = false;
816 break;
817 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300818 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200819 break;
820 case HWC2::PowerMode::Doze:
821 case HWC2::PowerMode::DozeSuspend:
822 return HWC2::Error::Unsupported;
823 default:
John Stultzffe783c2024-02-14 10:51:27 -0800824 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200825 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300826 }
827
828 if (IsInHeadlessMode()) {
829 return HWC2::Error::None;
830 }
831
Jia Ren80566fe2022-11-17 17:26:00 +0800832 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300833 /*
834 * Setting the display to active before we have a composition
835 * can break some drivers, so skip setting a_args.active to
836 * true, as the next composition frame will implicitly activate
837 * the display
838 */
839 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
840 ? HWC2::Error::None
841 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200842 };
843
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300844 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 if (err) {
846 ALOGE("Failed to apply the dpms composition err=%d", err);
847 return HWC2::Error::BadParameter;
848 }
849 return HWC2::Error::None;
850}
851
852HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300853 if (type_ == HWC2::DisplayType::Virtual) {
854 return HWC2::Error::None;
855 }
856
Roman Stratiienko099c3112022-01-20 11:50:54 +0200857 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
858 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200859 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200860 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200861 return HWC2::Error::None;
862}
863
864HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
865 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200866 if (IsInHeadlessMode()) {
867 *num_types = *num_requests = 0;
868 return HWC2::Error::None;
869 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300870
871 /* In current drm_hwc design in case previous frame layer was not validated as
872 * a CLIENT, it is used by display controller (Front buffer). We have to store
873 * this state to provide the CLIENT with the release fences for such buffers.
874 */
875 for (auto &l : layers_) {
876 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
877 HWC2::Composition::Client);
878 }
879
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200880 return backend_->ValidateDisplay(this, num_types, num_requests);
881}
882
883std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
884 std::vector<HwcLayer *> ordered_layers;
885 ordered_layers.reserve(layers_.size());
886
887 for (auto &[handle, layer] : layers_) {
888 ordered_layers.emplace_back(&layer);
889 }
890
891 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
892 [](const HwcLayer *lhs, const HwcLayer *rhs) {
893 return lhs->GetZOrder() < rhs->GetZOrder();
894 });
895
896 return ordered_layers;
897}
898
Roman Stratiienko099c3112022-01-20 11:50:54 +0200899HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
900 uint32_t *outVsyncPeriod /* ns */) {
901 return GetDisplayAttribute(configs_.active_config_id,
902 HWC2_ATTRIBUTE_VSYNC_PERIOD,
903 (int32_t *)(outVsyncPeriod));
904}
905
Roman Stratiienko6b405052022-12-10 19:09:10 +0200906#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200908 if (IsInHeadlessMode()) {
909 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
910 return HWC2::Error::None;
911 }
912 /* Primary display should be always internal,
913 * otherwise SF will be unhappy and will crash
914 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200915 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200916 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200917 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200918 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
919 else
920 return HWC2::Error::BadConfig;
921
922 return HWC2::Error::None;
923}
924
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200925HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200926 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
928 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300929 if (type_ == HWC2::DisplayType::Virtual) {
930 return HWC2::Error::None;
931 }
932
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200933 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
934 return HWC2::Error::BadParameter;
935 }
936
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200937 uint32_t current_vsync_period{};
938 GetDisplayVsyncPeriod(&current_vsync_period);
939
940 if (vsyncPeriodChangeConstraints->seamlessRequired) {
941 return HWC2::Error::SeamlessNotAllowed;
942 }
943
944 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
945 ->desiredTimeNanos -
946 current_vsync_period;
947 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
948 if (ret != HWC2::Error::None) {
949 return ret;
950 }
951
952 outTimeline->refreshRequired = true;
953 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
954 ->desiredTimeNanos;
955
956 last_vsync_ts_ = 0;
957 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200958 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200959
960 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200961}
962
963HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
964 return HWC2::Error::Unsupported;
965}
966
967HWC2::Error HwcDisplay::GetSupportedContentTypes(
968 uint32_t *outNumSupportedContentTypes,
969 const uint32_t *outSupportedContentTypes) {
970 if (outSupportedContentTypes == nullptr)
971 *outNumSupportedContentTypes = 0;
972
973 return HWC2::Error::None;
974}
975
976HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -0400977 /* Maps exactly to the content_type DRM connector property:
978 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200979 */
Sasha McIntosh173247b2024-09-18 18:06:52 -0400980 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
981 return HWC2::Error::BadParameter;
982
983 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200984
985 return HWC2::Error::None;
986}
987#endif
988
Roman Stratiienko6b405052022-12-10 19:09:10 +0200989#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200990HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
991 uint32_t *outDataSize,
992 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200993 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300994 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200995 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300996
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200997 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200998 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300999 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001000 }
1001
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001002 *outPort = handle_; /* TDOD(nobody): What should be here? */
1003
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001004 if (outData) {
1005 *outDataSize = std::min(*outDataSize, blob->length);
1006 memcpy(outData, blob->data, *outDataSize);
1007 } else {
1008 *outDataSize = blob->length;
1009 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001010
1011 return HWC2::Error::None;
1012}
1013
1014HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001015 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001016 if (outNumCapabilities == nullptr) {
1017 return HWC2::Error::BadParameter;
1018 }
1019
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001020 bool skip_ctm = false;
1021
1022 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001023 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001024 skip_ctm = true;
1025
1026 // Skip client CTM if DRM can handle it
1027 if (!skip_ctm && !IsInHeadlessMode() &&
1028 GetPipe().crtc->Get()->GetCtmProperty())
1029 skip_ctm = true;
1030
1031 if (!skip_ctm) {
1032 *outNumCapabilities = 0;
1033 return HWC2::Error::None;
1034 }
1035
1036 *outNumCapabilities = 1;
1037 if (outCapabilities) {
1038 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1039 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001040
1041 return HWC2::Error::None;
1042}
1043
1044HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1045 *supported = false;
1046 return HWC2::Error::None;
1047}
1048
1049HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1050 return HWC2::Error::Unsupported;
1051}
1052
Roman Stratiienko6b405052022-12-10 19:09:10 +02001053#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001054
Roman Stratiienko6b405052022-12-10 19:09:10 +02001055#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001056
1057HWC2::Error HwcDisplay::GetRenderIntents(
1058 int32_t mode, uint32_t *outNumIntents,
1059 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1060 if (mode != HAL_COLOR_MODE_NATIVE) {
1061 return HWC2::Error::BadParameter;
1062 }
1063
1064 if (outIntents == nullptr) {
1065 *outNumIntents = 1;
1066 return HWC2::Error::None;
1067 }
1068 *outNumIntents = 1;
1069 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1070 return HWC2::Error::None;
1071}
1072
1073HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1074 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1075 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1076 return HWC2::Error::BadParameter;
1077
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001078 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1079 return HWC2::Error::Unsupported;
1080
Sasha McIntosh5294f092024-09-18 18:14:54 -04001081 auto err = SetColorMode(mode);
1082 if (err != HWC2::Error::None) return err;
1083
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001084 return HWC2::Error::None;
1085}
1086
Roman Stratiienko6b405052022-12-10 19:09:10 +02001087#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001088
1089const Backend *HwcDisplay::backend() const {
1090 return backend_.get();
1091}
1092
1093void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1094 backend_ = std::move(backend);
1095}
1096
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001097} // namespace android