blob: 2a47a2a8d5e53a6323287f3bbc730402a4af3835 [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
Sasha McIntoshf9062b62024-11-12 10:55:06 -050024#include <ui/ColorSpace.h>
Drew Davenport97b5abc2024-11-07 10:43:54 -070025
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020026#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020027#include "backend/BackendManager.h"
28#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060029#include "compositor/DisplayInfo.h"
30#include "drm/DrmConnector.h"
31#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000032#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020033#include "utils/log.h"
34#include "utils/properties.h"
35
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060036using ::android::DrmDisplayPipeline;
Sasha McIntoshf9062b62024-11-12 10:55:06 -050037using ColorGamut = ::android::ColorSpace;
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060038
Roman Stratiienko3627beb2022-01-04 16:02:55 +020039namespace android {
40
Drew Davenport97b5abc2024-11-07 10:43:54 -070041namespace {
Drew Davenport76c17a82025-01-15 15:02:45 -070042
43constexpr int kCtmRows = 3;
44constexpr int kCtmCols = 3;
45
46constexpr std::array<float, 16> kIdentityMatrix = {
47 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F,
48 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F,
49};
50
Sasha McIntosh12d302c2025-01-30 15:29:06 -050051bool float_equals(float a, float b) {
52 const float epsilon = 0.001F;
53 return std::abs(a - b) < epsilon;
54}
55
Drew Davenport76c17a82025-01-15 15:02:45 -070056uint64_t To3132FixPt(float in) {
57 constexpr uint64_t kSignMask = (1ULL << 63);
58 constexpr uint64_t kValueMask = ~(1ULL << 63);
59 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
60 if (in < 0)
61 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
62 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
63}
64
Sasha McIntosh12d302c2025-01-30 15:29:06 -050065bool TransformHasOffsetValue(const float *matrix) {
66 for (int i = 12; i < 14; i++) {
67 if (!float_equals(matrix[i], 0.F)) {
68 ALOGW("DRM API does not support CTM with offsets.");
69 return true;
70 }
71 }
72 return false;
73}
74
Drew Davenport76c17a82025-01-15 15:02:45 -070075auto ToColorTransform(const std::array<float, 16> &color_transform_matrix) {
76 /* HAL provides a 4x4 float type matrix:
77 * | 0 1 2 3|
78 * | 4 5 6 7|
79 * | 8 9 10 11|
80 * |12 13 14 15|
81 *
82 * R_out = R*0 + G*4 + B*8 + 12
83 * G_out = R*1 + G*5 + B*9 + 13
84 * B_out = R*2 + G*6 + B*10 + 14
85 *
86 * DRM expects a 3x3 s31.32 fixed point matrix:
87 * out matrix in
88 * |R| |0 1 2| |R|
89 * |G| = |3 4 5| x |G|
90 * |B| |6 7 8| |B|
91 *
92 * R_out = R*0 + G*1 + B*2
93 * G_out = R*3 + G*4 + B*5
94 * B_out = R*6 + G*7 + B*8
95 */
96 auto color_matrix = std::make_shared<drm_color_ctm>();
97 for (int i = 0; i < kCtmCols; i++) {
98 for (int j = 0; j < kCtmRows; j++) {
99 constexpr int kInCtmRows = 4;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200100 color_matrix->matrix[(i * kCtmRows) + j] = To3132FixPt(
101 color_transform_matrix[(j * kInCtmRows) + i]);
Drew Davenport76c17a82025-01-15 15:02:45 -0700102 }
103 }
104 return color_matrix;
105}
106
Drew Davenport97b5abc2024-11-07 10:43:54 -0700107} // namespace
108
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200109std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
110 if (delta.total_pixops_ == 0)
111 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200112 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200113
114 std::stringstream ss;
115 ss << " Total frames count: " << delta.total_frames_ << "\n"
116 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
117 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
118 << ((delta.failed_kms_present_ > 0)
119 ? " !!! Internal failure, FIX it please\n"
120 : "")
121 << " Flattened frames: " << delta.frames_flattened_ << "\n"
122 << " Pixel operations (free units)"
123 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
124 << "]\n"
125 << " Composition efficiency: " << ratio;
126
127 return ss.str();
128}
129
130std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300131 auto connector_name = IsInHeadlessMode()
132 ? std::string("NULL-DISPLAY")
133 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200134
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200135 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200136 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200137 << "Statistics since system boot:\n"
138 << DumpDelta(total_stats_) << "\n\n"
139 << "Statistics since last dumpsys request:\n"
140 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
141
142 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
143 return ss.str();
144}
145
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200146HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000147 DrmHwc *hwc)
148 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300149 if (type_ == HWC2::DisplayType::Virtual) {
150 writeback_layer_ = std::make_unique<HwcLayer>(this);
151 }
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500152
153 identity_color_matrix_ = ToColorTransform(kIdentityMatrix);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300154}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200155
Drew Davenport76c17a82025-01-15 15:02:45 -0700156void HwcDisplay::SetColorTransformMatrix(
157 const std::array<float, 16> &color_transform_matrix) {
Drew Davenport76c17a82025-01-15 15:02:45 -0700158 const bool is_identity = std::equal(color_transform_matrix.begin(),
159 color_transform_matrix.end(),
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500160 kIdentityMatrix.begin(), float_equals);
Drew Davenport76c17a82025-01-15 15:02:45 -0700161 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
162 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500163 ctm_has_offset_ = false;
164
Drew Davenport76c17a82025-01-15 15:02:45 -0700165 if (color_transform_hint_ == is_identity) {
166 SetColorMatrixToIdentity();
167 } else {
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500168 if (TransformHasOffsetValue(color_transform_matrix.data()))
169 ctm_has_offset_ = true;
170
Drew Davenport76c17a82025-01-15 15:02:45 -0700171 color_matrix_ = ToColorTransform(color_transform_matrix);
172 }
173}
174
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400175void HwcDisplay::SetColorMatrixToIdentity() {
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500176 color_matrix_ = identity_color_matrix_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200177 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200178}
179
Normunds Rieksts545096d2024-03-11 16:37:45 +0000180HwcDisplay::~HwcDisplay() {
181 Deinit();
182};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200183
Drew Davenportfe70c802024-11-07 13:02:21 -0700184auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
185 -> const HwcDisplayConfig * {
186 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600187 if (config_iter == configs_.hwc_configs.end()) {
188 return nullptr;
189 }
190 return &config_iter->second;
191}
192
Drew Davenportfe70c802024-11-07 13:02:21 -0700193auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
194 return GetConfig(configs_.active_config_id);
195}
196
Drew Davenport8998f8b2024-10-24 10:15:12 -0600197auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700198 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600199}
200
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500201HWC2::Error HwcDisplay::SetOutputType(uint32_t hdr_output_type) {
202 switch (hdr_output_type) {
203 case 3: { // HDR10
204 auto ret = SetHdrOutputMetadata(ui::Hdr::HDR10);
205 if (ret != HWC2::Error::None)
206 return ret;
207 min_bpc_ = 8;
208 colorspace_ = Colorspace::kBt2020Rgb;
209 break;
210 }
211 case 1: { // SYSTEM
212 std::vector<ui::Hdr> hdr_types;
213 GetEdid()->GetSupportedHdrTypes(hdr_types);
214 if (!hdr_types.empty()) {
215 auto ret = SetHdrOutputMetadata(hdr_types.front());
216 if (ret != HWC2::Error::None)
217 return ret;
218 min_bpc_ = 8;
219 colorspace_ = Colorspace::kBt2020Rgb;
220 break;
221 } else {
222 [[fallthrough]];
223 }
224 }
225 case 0: // INVALID
226 [[fallthrough]];
227 case 2: // SDR
228 [[fallthrough]];
229 default:
Sasha McIntoshe0a19bd2025-03-13 15:23:21 -0400230 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500231 min_bpc_ = 6;
232 colorspace_ = Colorspace::kDefault;
233 }
234
235 return HWC2::Error::None;
236}
237
Drew Davenport97b5abc2024-11-07 10:43:54 -0700238HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
239 const HwcDisplayConfig *new_config = GetConfig(config);
240 if (new_config == nullptr) {
241 ALOGE("Could not find active mode for %u", config);
242 return ConfigError::kBadConfig;
243 }
244
245 const HwcDisplayConfig *current_config = GetCurrentConfig();
246
247 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700248 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700249
250 std::optional<LayerData> modeset_layer_data;
251 // If a client layer has already been provided, and its size matches the
252 // new config, use it for the modeset.
253 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
254 current_config->mode.GetRawMode().hdisplay == width &&
255 current_config->mode.GetRawMode().vdisplay == height) {
256 ALOGV("Use existing client_layer for blocking config.");
257 modeset_layer_data = client_layer_.GetLayerData();
258 } else {
259 ALOGV("Allocate modeset buffer.");
Roman Stratiienko2290dc62025-02-09 12:53:35 +0200260 auto modeset_buffer = //
261 GetPipe().device->CreateBufferForModeset(width, height);
262 if (modeset_buffer) {
Drew Davenport97b5abc2024-11-07 10:43:54 -0700263 auto modeset_layer = std::make_unique<HwcLayer>(this);
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200264 HwcLayer::LayerProperties properties;
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200265 properties.slot_buffer = {
266 .slot_id = 0,
Roman Stratiienko2290dc62025-02-09 12:53:35 +0200267 .bi = modeset_buffer,
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200268 };
269 properties.active_slot = {
270 .slot_id = 0,
271 .fence = {},
272 };
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200273 properties.blend_mode = BufferBlendMode::kNone;
274 modeset_layer->SetLayerProperties(properties);
Drew Davenport97b5abc2024-11-07 10:43:54 -0700275 modeset_layer->PopulateLayerData();
276 modeset_layer_data = modeset_layer->GetLayerData();
Drew Davenport97b5abc2024-11-07 10:43:54 -0700277 }
278 }
279
280 ALOGV("Create modeset commit.");
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500281 SetOutputType(new_config->output_type);
282
Drew Davenport97b5abc2024-11-07 10:43:54 -0700283 // Create atomic commit args for a blocking modeset. There's no need to do a
284 // separate test commit, since the commit does a test anyways.
285 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
286 modeset_layer_data);
287 commit_args.blocking = true;
288 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
289
290 if (ret) {
291 ALOGE("Blocking config failed: %d", ret);
Manasi Navare23bcfb72025-01-29 22:57:19 +0000292 return HwcDisplay::ConfigError::kConfigFailed;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700293 }
294
295 ALOGV("Blocking config succeeded.");
296 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700297 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700298 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
299 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700300 return ConfigError::kNone;
301}
302
Drew Davenport8998f8b2024-10-24 10:15:12 -0600303auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
304 bool seamless, QueuedConfigTiming *out_timing)
305 -> ConfigError {
306 if (configs_.hwc_configs.count(config) == 0) {
307 ALOGE("Could not find active mode for %u", config);
308 return ConfigError::kBadConfig;
309 }
310
311 // TODO: Add support for seamless configuration changes.
312 if (seamless) {
313 return ConfigError::kSeamlessNotAllowed;
314 }
315
316 // Request a refresh from the client one vsync period before the desired
317 // time, or simply at the desired time if there is no active configuration.
318 const HwcDisplayConfig *current_config = GetCurrentConfig();
319 out_timing->refresh_time_ns = desired_time -
320 (current_config
321 ? current_config->mode.GetVSyncPeriodNs()
322 : 0);
323 out_timing->new_vsync_time_ns = desired_time;
324
325 // Queue the config change timing to be consistent with the requested
326 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600327 staged_mode_change_time_ = out_timing->refresh_time_ns;
328 staged_mode_config_id_ = config;
329
330 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700331 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600332
333 return ConfigError::kNone;
334}
335
Drew Davenport7f356c82025-01-17 16:32:06 -0700336auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
337 if (IsInHeadlessMode()) {
338 return {};
339 }
340
341 /* In current drm_hwc design in case previous frame layer was not validated as
342 * a CLIENT, it is used by display controller (Front buffer). We have to store
343 * this state to provide the CLIENT with the release fences for such buffers.
344 */
345 for (auto &l : layers_) {
346 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
347 HWC2::Composition::Client);
348 }
349
350 // ValidateDisplay returns the number of layers that may be changed.
351 uint32_t num_types = 0;
352 uint32_t num_requests = 0;
353 backend_->ValidateDisplay(this, &num_types, &num_requests);
354
355 if (num_types == 0) {
356 return {};
357 }
358
359 // Iterate through the layers to find which layers actually changed.
360 std::vector<ChangedLayer> changed_layers;
361 for (auto &l : layers_) {
362 if (l.second.IsTypeChanged()) {
363 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
364 }
365 }
366 return changed_layers;
367}
368
Lucas Berthou30808a22025-02-05 17:52:33 +0000369auto HwcDisplay::GetDisplayBoundsMm() -> std::pair<int32_t, int32_t> {
370
371 const auto bounds = GetEdid()->GetBoundsMm();
372 if (bounds.first > 0 || bounds.second > 0) {
373 return bounds;
374 }
375
376 ALOGE("Failed to get display bounds for d=%d\n", int(handle_));
377 // mm_width and mm_height are unreliable. so only provide mm_width to avoid
378 // wrong dpi computations or other use of the values.
379 return {configs_.mm_width, -1};
380}
381
Drew Davenportb864ccf2025-01-22 14:57:36 -0700382auto HwcDisplay::AcceptValidatedComposition() -> void {
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200383 for (auto &[_, layer] : layers_) {
384 layer.AcceptTypeChange();
Drew Davenportb864ccf2025-01-22 14:57:36 -0700385 }
386}
387
388auto HwcDisplay::PresentStagedComposition(
Drew Davenportf6383482025-02-21 10:05:39 -0700389 std::optional<int64_t> desired_present_time, SharedFd &out_present_fence,
390 std::vector<ReleaseFence> &out_release_fences) -> bool {
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200391 if (IsInHeadlessMode()) {
392 return true;
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200393 }
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200394 HWC2::Error ret{};
395
396 ++total_stats_.total_frames_;
397
Drew Davenportf6383482025-02-21 10:05:39 -0700398 uint32_t vperiod_ns = 0;
399 GetDisplayVsyncPeriod(&vperiod_ns);
400
401 if (desired_present_time && vperiod_ns != 0) {
402 // DRM atomic uAPI does not support specifying that a commit should be
403 // applied to some future vsync. Until such uAPI is available, sleep in
404 // userspace until the next expected vsync time is consistent with the
405 // desired present time.
406 WaitForPresentTime(desired_present_time.value(), vperiod_ns);
407 }
408
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200409 AtomicCommitArgs a_args{};
410 ret = CreateComposition(a_args);
411
412 if (ret != HWC2::Error::None)
413 ++total_stats_.failed_kms_present_;
414
415 if (ret == HWC2::Error::BadLayer) {
416 // Can we really have no client or device layers?
417 return true;
418 }
419 if (ret != HWC2::Error::None)
420 return false;
421
422 out_present_fence = a_args.out_fence;
423
424 // Reset the color matrix so we don't apply it over and over again.
425 color_matrix_ = {};
426
427 ++frame_no_;
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200428
429 if (!out_present_fence) {
430 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700431 }
432
433 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200434 if (l.second.GetPriorBufferScanOutFlag()) {
435 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700436 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700437 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200438
439 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700440}
441
Roman Stratiienko63762a92023-09-18 22:33:45 +0300442void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200443 Deinit();
444
Roman Stratiienko63762a92023-09-18 22:33:45 +0300445 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200446
Roman Stratiienko63762a92023-09-18 22:33:45 +0300447 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200448 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000449 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200450 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000451 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200452 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200453}
454
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200455void HwcDisplay::Deinit() {
456 if (pipeline_ != nullptr) {
457 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200458 a_args.composition = std::make_shared<DrmKmsPlan>();
459 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300460 a_args.composition = {};
461 a_args.active = false;
462 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200463
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200464 current_plan_.reset();
465 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200466 if (flatcon_) {
467 flatcon_->StopThread();
468 flatcon_.reset();
469 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200470 }
471
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200472 if (vsync_worker_) {
473 vsync_worker_->StopThread();
474 vsync_worker_ = {};
475 }
476
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200477 client_layer_.ClearSlots();
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200478}
479
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200480HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200481 ChosePreferredConfig();
482
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300483 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700484 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300485 if (!vsync_worker_) {
486 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
487 return HWC2::Error::BadDisplay;
488 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200489 }
490
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200491 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200492 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200493 if (ret) {
494 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
495 return HWC2::Error::BadDisplay;
496 }
Drew Davenport93443182023-12-14 09:25:45 +0000497 auto flatcbk = (struct FlatConCallbacks){
498 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200499 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 }
501
Roman Stratiienko44b95772025-01-22 18:03:36 +0200502 HwcLayer::LayerProperties lp;
503 lp.blend_mode = BufferBlendMode::kPreMult;
504 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200505
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400506 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200507
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200508 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200509}
510
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600511std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
512 if (IsInHeadlessMode()) {
513 // The pipeline can be nullptr in headless mode, so return the default
514 // "normal" mode.
515 return PanelOrientation::kModePanelOrientationNormal;
516 }
517
518 DrmDisplayPipeline &pipeline = GetPipe();
519 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
520 ALOGW(
521 "No display pipeline present to query the panel orientation property.");
522 return {};
523 }
524
525 return pipeline.connector->Get()->GetPanelOrientation();
526}
527
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200529 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300530 if (type_ == HWC2::DisplayType::Virtual) {
531 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
532 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200533 err = configs_.Update(*pipeline_->connector->Get());
534 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300535 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200536 }
537 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200538 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200539 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200540
Roman Stratiienko0137f862022-01-04 18:27:40 +0200541 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200542}
543
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200544auto HwcDisplay::CreateLayer(ILayerId new_layer_id) -> bool {
545 if (layers_.count(new_layer_id) > 0)
546 return false;
547
548 layers_.emplace(new_layer_id, HwcLayer(this));
549
550 return true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200551}
552
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200553auto HwcDisplay::DestroyLayer(ILayerId layer_id) -> bool {
554 auto count = layers_.erase(layer_id);
555 return count != 0;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200556}
557
558HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700559 // If a config has been queued, it is considered the "active" config.
560 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
561 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200562 return HWC2::Error::BadConfig;
563
Drew Davenportfe70c802024-11-07 13:02:21 -0700564 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200565 return HWC2::Error::None;
566}
567
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200568HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200569 if (IsInHeadlessMode()) {
570 *num_modes = 1;
571 if (modes)
572 modes[0] = HAL_COLOR_MODE_NATIVE;
573 return HWC2::Error::None;
574 }
575
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500576 if (!modes) {
577 std::vector<Colormode> temp_modes;
578 GetEdid()->GetColorModes(temp_modes);
579 *num_modes = temp_modes.size();
580 return HWC2::Error::None;
581 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200582
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500583 std::vector<Colormode> temp_modes;
584 std::vector<int32_t> out_modes(modes, modes + *num_modes);
585 GetEdid()->GetColorModes(temp_modes);
586 if (temp_modes.empty()) {
587 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
588 return HWC2::Error::None;
589 }
590
591 for (auto &c : temp_modes)
592 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200593
594 return HWC2::Error::None;
595}
596
597HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
598 int32_t attribute_in,
599 int32_t *value) {
600 int conf = static_cast<int>(config);
601
Roman Stratiienko0137f862022-01-04 18:27:40 +0200602 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200603 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604 return HWC2::Error::BadConfig;
605 }
606
Roman Stratiienko0137f862022-01-04 18:27:40 +0200607 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608
609 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300610 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200611 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
612 switch (attribute) {
613 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200614 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200615 break;
616 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200617 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 break;
619 case HWC2::Attribute::VsyncPeriod:
620 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600621 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000623 case HWC2::Attribute::DpiY:
Lucas Berthou30808a22025-02-05 17:52:33 +0000624 *value = GetEdid()->GetDpiY();
625 if (*value < 0) {
626 // default to raw mode DpiX for both x and y when no good value
627 // can be provided from edid.
628 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
629 kUmPerInch / mm_width)
630 : -1;
631 }
632 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200633 case HWC2::Attribute::DpiX:
634 // Dots per 1000 inches
Lucas Berthou30808a22025-02-05 17:52:33 +0000635 *value = GetEdid()->GetDpiX();
636 if (*value < 0) {
637 // default to raw mode DpiX for both x and y when no good value
638 // can be provided from edid.
639 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
640 kUmPerInch / mm_width)
641 : -1;
642 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200643 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200644#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 case HWC2::Attribute::ConfigGroup:
646 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
647 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200648 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649 break;
650#endif
651 default:
652 *value = -1;
653 return HWC2::Error::BadConfig;
654 }
655 return HWC2::Error::None;
656}
657
Drew Davenportf7e88332024-09-06 12:54:38 -0600658HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
659 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200660 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200661 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200662 if (hwc_config.second.disabled) {
663 continue;
664 }
665
666 if (configs != nullptr) {
667 if (idx >= *num_configs) {
668 break;
669 }
670 configs[idx] = hwc_config.second.id;
671 }
672
673 idx++;
674 }
675 *num_configs = idx;
676 return HWC2::Error::None;
677}
678
679HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
680 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200681 if (IsInHeadlessMode()) {
682 stream << "null-display";
683 } else {
684 stream << "display-" << GetPipe().connector->Get()->GetId();
685 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300686 auto string = stream.str();
687 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200688 if (!name) {
689 *size = length;
690 return HWC2::Error::None;
691 }
692
693 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
694 strncpy(name, string.c_str(), *size);
695 return HWC2::Error::None;
696}
697
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200698HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
699 *type = static_cast<int32_t>(type_);
700 return HWC2::Error::None;
701}
702
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500703HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
704 float *max_luminance,
705 float *max_average_luminance,
706 float *min_luminance) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200707 if (IsInHeadlessMode()) {
708 *num_types = 0;
709 return HWC2::Error::None;
710 }
711
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500712 if (!types) {
713 std::vector<ui::Hdr> temp_types;
714 float lums[3] = {0.F};
715 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
716 *num_types = temp_types.size();
717 return HWC2::Error::None;
718 }
719
720 std::vector<ui::Hdr> temp_types;
721 std::vector<int32_t> out_types(types, types + *num_types);
722 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
723 max_average_luminance, min_luminance);
724 for (auto &t : temp_types) {
725 switch (t) {
726 case ui::Hdr::HDR10:
727 out_types.emplace_back(HAL_HDR_HDR10);
728 break;
729 case ui::Hdr::HLG:
730 out_types.emplace_back(HAL_HDR_HLG);
731 break;
732 default:
733 // Ignore any other HDR types
734 break;
735 }
736 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200737 return HWC2::Error::None;
738}
739
Drew Davenport97b5abc2024-11-07 10:43:54 -0700740AtomicCommitArgs HwcDisplay::CreateModesetCommit(
741 const HwcDisplayConfig *config,
742 const std::optional<LayerData> &modeset_layer) {
743 AtomicCommitArgs args{};
744
745 args.color_matrix = color_matrix_;
746 args.content_type = content_type_;
747 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500748 args.hdr_metadata = hdr_metadata_;
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500749 args.min_bpc = min_bpc_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700750
751 std::vector<LayerData> composition_layers;
752 if (modeset_layer) {
753 composition_layers.emplace_back(modeset_layer.value());
754 }
755
756 if (composition_layers.empty()) {
757 ALOGW("Attempting to create a modeset commit without a layer.");
758 }
759
760 args.display_mode = config->mode;
761 args.active = true;
762 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
763 std::move(
764 composition_layers));
765 ALOGW_IF(!args.composition, "No composition for blocking modeset");
766
767 return args;
768}
769
Drew Davenportf6383482025-02-21 10:05:39 -0700770void HwcDisplay::WaitForPresentTime(int64_t present_time,
771 uint32_t vsync_period_ns) {
772 const int64_t current_time = ResourceManager::GetTimeMonotonicNs();
773 int64_t next_vsync_time = vsync_worker_->GetNextVsyncTimestamp(current_time);
774
775 int64_t vsync_after_present_time = vsync_worker_->GetNextVsyncTimestamp(
776 present_time);
777 int64_t vsync_before_present_time = vsync_after_present_time -
778 vsync_period_ns;
779
780 // Check if |present_time| is closer to the expected vsync before or after.
781 int64_t desired_vsync = (vsync_after_present_time - present_time) <
782 (present_time - vsync_before_present_time)
783 ? vsync_after_present_time
784 : vsync_before_present_time;
785
786 // Don't sleep if desired_vsync is before or nearly equal to vsync_period of
787 // the next expected vsync.
788 const int64_t quarter_vsync_period = vsync_period_ns / 4;
789 if ((desired_vsync - next_vsync_time) < quarter_vsync_period) {
790 return;
791 }
792
793 // Sleep until 75% vsync_period before the desired_vsync.
794 int64_t sleep_until = desired_vsync - (quarter_vsync_period * 3);
795 struct timespec sleep_until_ts{};
796 constexpr int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
797 sleep_until_ts.tv_sec = int(sleep_until / kOneSecondNs);
798 sleep_until_ts.tv_nsec = int(sleep_until -
799 (sleep_until_ts.tv_sec * kOneSecondNs));
800 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &sleep_until_ts, nullptr);
801}
802
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200803// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200804HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200805 if (IsInHeadlessMode()) {
806 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
807 return HWC2::Error::None;
808 }
809
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200810 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400811 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400812 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500813 a_args.hdr_metadata = hdr_metadata_;
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500814 a_args.min_bpc = min_bpc_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200815
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200816 uint32_t prev_vperiod_ns = 0;
817 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200818
Drew Davenportd387c842024-12-16 16:57:24 -0700819 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700820 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200821 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700822 const HwcDisplayConfig *staged_config = GetConfig(
823 staged_mode_config_id_.value());
824 if (staged_config == nullptr) {
825 return HWC2::Error::BadConfig;
826 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200827
Drew Davenportfe70c802024-11-07 13:02:21 -0700828 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700829 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200830 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700831 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200832 }
833 }
834
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200835 // order the layers by z-order
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500836 size_t client_layer_count = 0;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200837 bool use_client_layer = false;
838 uint32_t client_z_order = UINT32_MAX;
839 std::map<uint32_t, HwcLayer *> z_map;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000840 std::optional<LayerData> cursor_layer = std::nullopt;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200841 for (auto &[_, layer] : layers_) {
842 switch (layer.GetValidatedType()) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200843 case HWC2::Composition::Device:
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200844 z_map.emplace(layer.GetZOrder(), &layer);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 break;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000846 case HWC2::Composition::Cursor:
847 if (!cursor_layer.has_value()) {
848 layer.PopulateLayerData();
849 cursor_layer = layer.GetLayerData();
850 } else {
851 ALOGW("Detected multiple cursor layers");
852 z_map.emplace(layer.GetZOrder(), &layer);
853 }
854 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200855 case HWC2::Composition::Client:
856 // Place it at the z_order of the lowest client layer
857 use_client_layer = true;
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500858 client_layer_count++;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200859 client_z_order = std::min(client_z_order, layer.GetZOrder());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200860 break;
861 default:
862 continue;
863 }
864 }
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500865
866 // CTM will be applied by the client, don't apply DRM CTM
867 if (client_layer_count == layers_.size())
868 a_args.color_matrix = identity_color_matrix_;
869 else
870 a_args.color_matrix = color_matrix_;
871
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200872 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300873 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200874
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200875 client_layer_.PopulateLayerData();
876 if (!client_layer_.IsLayerUsableAsDevice()) {
877 ALOGE_IF(!a_args.test_only,
878 "Client layer must be always usable by DRM/KMS");
879 /* This may be normally triggered on validation of the first frame
880 * containing CLIENT layer. At this moment client buffer is not yet
881 * provided by the CLIENT.
882 * This may be triggered once in HwcLayer lifecycle in case FB can't be
883 * imported. For example when non-contiguous buffer is imported into
884 * contiguous-only DRM/KMS driver.
885 */
886 return HWC2::Error::BadLayer;
887 }
888 }
889
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200890 if (z_map.empty())
891 return HWC2::Error::BadLayer;
892
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200893 std::vector<LayerData> composition_layers;
894
895 /* Import & populate */
896 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200897 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200898 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200899
900 // now that they're ordered by z, add them to the composition
901 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200902 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200903 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200904 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200905 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200906 }
907
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200908 /* Store plan to ensure shared planes won't be stolen by other display
909 * in between of ValidateDisplay() and PresentDisplay() calls
910 */
911 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
Andrew Wolfers5c530832024-12-03 16:30:26 +0000912 std::move(composition_layers),
913 cursor_layer);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300914
915 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200916 writeback_layer_->PopulateLayerData();
917 if (!writeback_layer_->IsLayerUsableAsDevice()) {
918 ALOGE("Output layer must be always usable by DRM/KMS");
919 return HWC2::Error::BadLayer;
920 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300921 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
922 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
923 .acquire_fence;
924 }
925
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200926 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700927 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200928 return HWC2::Error::BadConfig;
929 }
930
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200931 a_args.composition = current_plan_;
932
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300933 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200934
935 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700936 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200937 return HWC2::Error::BadParameter;
938 }
939
Drew Davenportd387c842024-12-16 16:57:24 -0700940 if (new_vsync_period_ns) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700941 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700942
943 vsync_worker_->SetVsyncTimestampTracking(false);
944 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
945 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000946 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700947 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000948 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200949 }
Drew Davenportbf711eb2025-02-19 09:41:20 -0700950 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200951 }
952
953 return HWC2::Error::None;
954}
955
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200956HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
957 int64_t change_time) {
958 if (configs_.hwc_configs.count(config) == 0) {
959 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960 return HWC2::Error::BadConfig;
961 }
962
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200963 staged_mode_change_time_ = change_time;
964 staged_mode_config_id_ = config;
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500965 if (const HwcDisplayConfig *new_config = GetConfig(config))
966 SetOutputType(new_config->output_type);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200967
968 return HWC2::Error::None;
969}
970
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200971HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
972 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
973}
974
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200975HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400976 /* Maps to the Colorspace DRM connector property:
977 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
978 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500979 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200980 return HWC2::Error::BadParameter;
981
Sasha McIntosh5294f092024-09-18 18:14:54 -0400982 switch (mode) {
983 case HAL_COLOR_MODE_NATIVE:
984 colorspace_ = Colorspace::kDefault;
985 break;
986 case HAL_COLOR_MODE_STANDARD_BT601_625:
987 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
988 case HAL_COLOR_MODE_STANDARD_BT601_525:
989 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
990 // The DP spec does not say whether this is the 525 or the 625 line version.
991 colorspace_ = Colorspace::kBt601Ycc;
992 break;
993 case HAL_COLOR_MODE_STANDARD_BT709:
994 case HAL_COLOR_MODE_SRGB:
995 colorspace_ = Colorspace::kBt709Ycc;
996 break;
997 case HAL_COLOR_MODE_DCI_P3:
998 case HAL_COLOR_MODE_DISPLAY_P3:
999 colorspace_ = Colorspace::kDciP3RgbD65;
1000 break;
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -05001001 case HAL_COLOR_MODE_DISPLAY_BT2020:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001002 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001003 case HAL_COLOR_MODE_BT2020:
1004 case HAL_COLOR_MODE_BT2100_PQ:
1005 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001006 default:
1007 return HWC2::Error::Unsupported;
1008 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001009
1010 color_mode_ = mode;
1011 return HWC2::Error::None;
1012}
1013
1014HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1015 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1016 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1017 return HWC2::Error::BadParameter;
1018
1019 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1020 return HWC2::Error::BadParameter;
1021
1022 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Sasha McIntosh12d302c2025-01-30 15:29:06 -05001023 ctm_has_offset_ = false;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001024
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001025 if (IsInHeadlessMode())
1026 return HWC2::Error::None;
1027
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001028 if (!GetPipe().crtc->Get()->GetCtmProperty())
1029 return HWC2::Error::None;
1030
1031 switch (color_transform_hint_) {
1032 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001033 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001034 break;
1035 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001036 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001037 {
Sasha McIntosh12d302c2025-01-30 15:29:06 -05001038 if (TransformHasOffsetValue(matrix))
1039 ctm_has_offset_ = true;
1040
Drew Davenport76c17a82025-01-15 15:02:45 -07001041 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1042 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1043 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001044 }
1045 break;
1046 default:
1047 return HWC2::Error::Unsupported;
1048 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001049
1050 return HWC2::Error::None;
1051}
1052
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001053bool HwcDisplay::CtmByGpu() {
1054 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1055 return false;
1056
Sasha McIntosh12d302c2025-01-30 15:29:06 -05001057 if (GetPipe().crtc->Get()->GetCtmProperty() && !ctm_has_offset_)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001058 return false;
1059
Drew Davenport93443182023-12-14 09:25:45 +00001060 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001061 return false;
1062
1063 return true;
1064}
1065
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001066HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1067 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001068
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001069 AtomicCommitArgs a_args{};
1070
1071 switch (mode) {
1072 case HWC2::PowerMode::Off:
1073 a_args.active = false;
1074 break;
1075 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001076 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001077 break;
1078 case HWC2::PowerMode::Doze:
1079 case HWC2::PowerMode::DozeSuspend:
1080 return HWC2::Error::Unsupported;
1081 default:
John Stultzffe783c2024-02-14 10:51:27 -08001082 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001083 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001084 }
1085
1086 if (IsInHeadlessMode()) {
1087 return HWC2::Error::None;
1088 }
1089
Jia Ren80566fe2022-11-17 17:26:00 +08001090 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001091 /*
1092 * Setting the display to active before we have a composition
1093 * can break some drivers, so skip setting a_args.active to
1094 * true, as the next composition frame will implicitly activate
1095 * the display
1096 */
1097 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1098 ? HWC2::Error::None
1099 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001100 };
1101
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001102 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001103 if (err) {
1104 ALOGE("Failed to apply the dpms composition err=%d", err);
1105 return HWC2::Error::BadParameter;
1106 }
1107 return HWC2::Error::None;
1108}
1109
1110HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001111 if (type_ == HWC2::DisplayType::Virtual) {
1112 return HWC2::Error::None;
1113 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001114 if (!vsync_worker_) {
1115 return HWC2::Error::NoResources;
1116 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001117
Roman Stratiienko099c3112022-01-20 11:50:54 +02001118 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001119 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001120 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001121 DrmHwc *hwc = hwc_;
1122 hwc2_display_t id = handle_;
1123 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001124 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001125 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1126 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001127 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001128 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001129 return HWC2::Error::None;
1130}
1131
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001132std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1133 std::vector<HwcLayer *> ordered_layers;
1134 ordered_layers.reserve(layers_.size());
1135
1136 for (auto &[handle, layer] : layers_) {
1137 ordered_layers.emplace_back(&layer);
1138 }
1139
1140 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1141 [](const HwcLayer *lhs, const HwcLayer *rhs) {
Andrew Wolfers5c530832024-12-03 16:30:26 +00001142 // Cursor layers should always have highest zpos.
1143 if ((lhs->GetSfType() == HWC2::Composition::Cursor) !=
1144 (rhs->GetSfType() == HWC2::Composition::Cursor)) {
1145 return rhs->GetSfType() == HWC2::Composition::Cursor;
1146 }
1147
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001148 return lhs->GetZOrder() < rhs->GetZOrder();
1149 });
1150
1151 return ordered_layers;
1152}
1153
Roman Stratiienko099c3112022-01-20 11:50:54 +02001154HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1155 uint32_t *outVsyncPeriod /* ns */) {
1156 return GetDisplayAttribute(configs_.active_config_id,
1157 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1158 (int32_t *)(outVsyncPeriod));
1159}
1160
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001161// Display primary values are coded as unsigned 16-bit values in units of
1162// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1163static uint64_t ToU16ColorValue(float in) {
1164 constexpr float kPrimariesFixedPoint = 50000.F;
1165 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1166}
1167
1168HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1169 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1170 hdr_metadata_->metadata_type = 0;
1171 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1172 m->metadata_type = 0;
1173
1174 switch (type) {
1175 case ui::Hdr::HDR10:
1176 m->eotf = 2; // PQ
1177 break;
1178 case ui::Hdr::HLG:
1179 m->eotf = 3; // HLG
1180 break;
1181 default:
1182 return HWC2::Error::Unsupported;
1183 }
1184
1185 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1186 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1187 std::vector<ui::Hdr> types;
1188 float hdr_luminance[3]{0.F, 0.F, 0.F};
1189 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1190 &hdr_luminance[2]);
1191 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1192 hdr_luminance[0]);
1193 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1194 // The min luminance value is coded as an unsigned 16-bit value in units of
1195 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1196 // represents 6.5535 cd/m2.
1197 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1198 10000.F);
1199
1200 auto gamut = ColorGamut::BT2020();
1201 auto primaries = gamut.getPrimaries();
1202 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1203 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1204 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1205 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1206 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1207 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1208
1209 auto whitePoint = gamut.getWhitePoint();
1210 m->white_point.x = ToU16ColorValue(whitePoint.x);
1211 m->white_point.y = ToU16ColorValue(whitePoint.y);
1212
1213 return HWC2::Error::None;
1214}
1215
Roman Stratiienko6b405052022-12-10 19:09:10 +02001216#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001217HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001218 if (IsInHeadlessMode()) {
1219 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1220 return HWC2::Error::None;
1221 }
1222 /* Primary display should be always internal,
1223 * otherwise SF will be unhappy and will crash
1224 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001225 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001226 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001227 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001228 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1229 else
1230 return HWC2::Error::BadConfig;
1231
1232 return HWC2::Error::None;
1233}
1234
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001235HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001236 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001237 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1238 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001239 if (type_ == HWC2::DisplayType::Virtual) {
1240 return HWC2::Error::None;
1241 }
1242
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001243 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1244 return HWC2::Error::BadParameter;
1245 }
1246
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001247 uint32_t current_vsync_period{};
1248 GetDisplayVsyncPeriod(&current_vsync_period);
1249
1250 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1251 return HWC2::Error::SeamlessNotAllowed;
1252 }
1253
1254 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1255 ->desiredTimeNanos -
1256 current_vsync_period;
1257 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1258 if (ret != HWC2::Error::None) {
1259 return ret;
1260 }
1261
1262 outTimeline->refreshRequired = true;
1263 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1264 ->desiredTimeNanos;
1265
Drew Davenport33121b72024-12-13 14:59:35 -07001266 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001267
1268 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001269}
1270
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001271HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001272 /* Maps exactly to the content_type DRM connector property:
1273 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001274 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001275 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1276 return HWC2::Error::BadParameter;
1277
1278 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001279
1280 return HWC2::Error::None;
1281}
1282#endif
1283
Roman Stratiienko6b405052022-12-10 19:09:10 +02001284#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001285HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1286 uint32_t *outDataSize,
1287 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001288 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001289 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001290 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001291
Gil Dekel907a51a2025-02-14 22:44:01 -05001292 auto *connector = GetPipe().connector->Get();
1293 auto blob = connector->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001294 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001295 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001296 }
1297
Gil Dekel907a51a2025-02-14 22:44:01 -05001298 constexpr uint8_t kDrmDeviceBitShift = 5U;
1299 constexpr uint8_t kDrmDeviceBitMask = 0xE0;
1300 constexpr uint8_t kConnectorBitMask = 0x1F;
1301 const auto kDrmIdx = static_cast<uint8_t>(
1302 connector->GetDev().GetIndexInDevArray());
1303 const auto kConnectorIdx = static_cast<uint8_t>(
1304 connector->GetIndexInResArray());
1305 *outPort = (((kDrmIdx << kDrmDeviceBitShift) & kDrmDeviceBitMask) |
1306 (kConnectorIdx & kConnectorBitMask));
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001307
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001308 if (outData) {
1309 *outDataSize = std::min(*outDataSize, blob->length);
1310 memcpy(outData, blob->data, *outDataSize);
1311 } else {
1312 *outDataSize = blob->length;
1313 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001314
1315 return HWC2::Error::None;
1316}
1317
1318HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001319 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001320 if (outNumCapabilities == nullptr) {
1321 return HWC2::Error::BadParameter;
1322 }
1323
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001324 bool skip_ctm = false;
1325
1326 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001327 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001328 skip_ctm = true;
1329
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001330 if (!skip_ctm) {
1331 *outNumCapabilities = 0;
1332 return HWC2::Error::None;
1333 }
1334
1335 *outNumCapabilities = 1;
1336 if (outCapabilities) {
1337 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1338 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001339
1340 return HWC2::Error::None;
1341}
1342
Roman Stratiienko6b405052022-12-10 19:09:10 +02001343#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001344
Roman Stratiienko6b405052022-12-10 19:09:10 +02001345#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001346
1347HWC2::Error HwcDisplay::GetRenderIntents(
1348 int32_t mode, uint32_t *outNumIntents,
1349 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1350 if (mode != HAL_COLOR_MODE_NATIVE) {
1351 return HWC2::Error::BadParameter;
1352 }
1353
1354 if (outIntents == nullptr) {
1355 *outNumIntents = 1;
1356 return HWC2::Error::None;
1357 }
1358 *outNumIntents = 1;
1359 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1360 return HWC2::Error::None;
1361}
1362
1363HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1364 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1365 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1366 return HWC2::Error::BadParameter;
1367
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001368 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1369 return HWC2::Error::Unsupported;
1370
Sasha McIntosh5294f092024-09-18 18:14:54 -04001371 auto err = SetColorMode(mode);
1372 if (err != HWC2::Error::None) return err;
1373
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001374 return HWC2::Error::None;
1375}
1376
Roman Stratiienko6b405052022-12-10 19:09:10 +02001377#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001378
1379const Backend *HwcDisplay::backend() const {
1380 return backend_.get();
1381}
1382
1383void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1384 backend_ = std::move(backend);
1385}
1386
Roman Stratiienko45cdacc2025-02-12 04:26:10 +02001387bool HwcDisplay::NeedsClientLayerUpdate() const {
1388 return std::any_of(layers_.begin(), layers_.end(), [](const auto &pair) {
1389 const auto &layer = pair.second;
1390 return layer.GetSfType() == HWC2::Composition::Client ||
1391 layer.GetValidatedType() == HWC2::Composition::Client;
1392 });
1393}
1394
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001395} // namespace android