blob: abc15fda22334a8c5af139f589f16aa882e13c82 [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
51uint64_t To3132FixPt(float in) {
52 constexpr uint64_t kSignMask = (1ULL << 63);
53 constexpr uint64_t kValueMask = ~(1ULL << 63);
54 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
55 if (in < 0)
56 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
57 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
58}
59
60auto ToColorTransform(const std::array<float, 16> &color_transform_matrix) {
61 /* HAL provides a 4x4 float type matrix:
62 * | 0 1 2 3|
63 * | 4 5 6 7|
64 * | 8 9 10 11|
65 * |12 13 14 15|
66 *
67 * R_out = R*0 + G*4 + B*8 + 12
68 * G_out = R*1 + G*5 + B*9 + 13
69 * B_out = R*2 + G*6 + B*10 + 14
70 *
71 * DRM expects a 3x3 s31.32 fixed point matrix:
72 * out matrix in
73 * |R| |0 1 2| |R|
74 * |G| = |3 4 5| x |G|
75 * |B| |6 7 8| |B|
76 *
77 * R_out = R*0 + G*1 + B*2
78 * G_out = R*3 + G*4 + B*5
79 * B_out = R*6 + G*7 + B*8
80 */
81 auto color_matrix = std::make_shared<drm_color_ctm>();
82 for (int i = 0; i < kCtmCols; i++) {
83 for (int j = 0; j < kCtmRows; j++) {
84 constexpr int kInCtmRows = 4;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +020085 color_matrix->matrix[(i * kCtmRows) + j] = To3132FixPt(
86 color_transform_matrix[(j * kInCtmRows) + i]);
Drew Davenport76c17a82025-01-15 15:02:45 -070087 }
88 }
89 return color_matrix;
90}
91
Drew Davenport97b5abc2024-11-07 10:43:54 -070092} // namespace
93
Roman Stratiienko3627beb2022-01-04 16:02:55 +020094std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
95 if (delta.total_pixops_ == 0)
96 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +020097 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +020098
99 std::stringstream ss;
100 ss << " Total frames count: " << delta.total_frames_ << "\n"
101 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
102 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
103 << ((delta.failed_kms_present_ > 0)
104 ? " !!! Internal failure, FIX it please\n"
105 : "")
106 << " Flattened frames: " << delta.frames_flattened_ << "\n"
107 << " Pixel operations (free units)"
108 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
109 << "]\n"
110 << " Composition efficiency: " << ratio;
111
112 return ss.str();
113}
114
115std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300116 auto connector_name = IsInHeadlessMode()
117 ? std::string("NULL-DISPLAY")
118 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200119
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200120 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200121 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200122 << "Statistics since system boot:\n"
123 << DumpDelta(total_stats_) << "\n\n"
124 << "Statistics since last dumpsys request:\n"
125 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
126
127 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
128 return ss.str();
129}
130
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200131HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000132 DrmHwc *hwc)
133 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300134 if (type_ == HWC2::DisplayType::Virtual) {
135 writeback_layer_ = std::make_unique<HwcLayer>(this);
136 }
137}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200138
Drew Davenport76c17a82025-01-15 15:02:45 -0700139void HwcDisplay::SetColorTransformMatrix(
140 const std::array<float, 16> &color_transform_matrix) {
141 auto almost_equal = [](auto a, auto b) {
142 const float epsilon = 0.001F;
143 return std::abs(a - b) < epsilon;
144 };
145 const bool is_identity = std::equal(color_transform_matrix.begin(),
146 color_transform_matrix.end(),
147 kIdentityMatrix.begin(), almost_equal);
148 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
149 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
150 if (color_transform_hint_ == is_identity) {
151 SetColorMatrixToIdentity();
152 } else {
153 color_matrix_ = ToColorTransform(color_transform_matrix);
154 }
155}
156
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400157void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200158 color_matrix_ = std::make_shared<drm_color_ctm>();
159 for (int i = 0; i < kCtmCols; i++) {
160 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800161 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200162 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200163 }
164 }
165
166 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200167}
168
Normunds Rieksts545096d2024-03-11 16:37:45 +0000169HwcDisplay::~HwcDisplay() {
170 Deinit();
171};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200172
Drew Davenportfe70c802024-11-07 13:02:21 -0700173auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
174 -> const HwcDisplayConfig * {
175 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600176 if (config_iter == configs_.hwc_configs.end()) {
177 return nullptr;
178 }
179 return &config_iter->second;
180}
181
Drew Davenportfe70c802024-11-07 13:02:21 -0700182auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
183 return GetConfig(configs_.active_config_id);
184}
185
Drew Davenport8998f8b2024-10-24 10:15:12 -0600186auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700187 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600188}
189
Drew Davenport97b5abc2024-11-07 10:43:54 -0700190HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
191 const HwcDisplayConfig *new_config = GetConfig(config);
192 if (new_config == nullptr) {
193 ALOGE("Could not find active mode for %u", config);
194 return ConfigError::kBadConfig;
195 }
196
197 const HwcDisplayConfig *current_config = GetCurrentConfig();
198
199 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700200 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700201
202 std::optional<LayerData> modeset_layer_data;
203 // If a client layer has already been provided, and its size matches the
204 // new config, use it for the modeset.
205 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
206 current_config->mode.GetRawMode().hdisplay == width &&
207 current_config->mode.GetRawMode().vdisplay == height) {
208 ALOGV("Use existing client_layer for blocking config.");
209 modeset_layer_data = client_layer_.GetLayerData();
210 } else {
211 ALOGV("Allocate modeset buffer.");
Roman Stratiienko2290dc62025-02-09 12:53:35 +0200212 auto modeset_buffer = //
213 GetPipe().device->CreateBufferForModeset(width, height);
214 if (modeset_buffer) {
Drew Davenport97b5abc2024-11-07 10:43:54 -0700215 auto modeset_layer = std::make_unique<HwcLayer>(this);
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200216 HwcLayer::LayerProperties properties;
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200217 properties.slot_buffer = {
218 .slot_id = 0,
Roman Stratiienko2290dc62025-02-09 12:53:35 +0200219 .bi = modeset_buffer,
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200220 };
221 properties.active_slot = {
222 .slot_id = 0,
223 .fence = {},
224 };
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200225 properties.blend_mode = BufferBlendMode::kNone;
226 modeset_layer->SetLayerProperties(properties);
Drew Davenport97b5abc2024-11-07 10:43:54 -0700227 modeset_layer->PopulateLayerData();
228 modeset_layer_data = modeset_layer->GetLayerData();
Drew Davenport97b5abc2024-11-07 10:43:54 -0700229 }
230 }
231
232 ALOGV("Create modeset commit.");
233 // Create atomic commit args for a blocking modeset. There's no need to do a
234 // separate test commit, since the commit does a test anyways.
235 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
236 modeset_layer_data);
237 commit_args.blocking = true;
238 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
239
240 if (ret) {
241 ALOGE("Blocking config failed: %d", ret);
242 return HwcDisplay::ConfigError::kBadConfig;
243 }
244
245 ALOGV("Blocking config succeeded.");
246 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700247 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700248 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
249 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700250 return ConfigError::kNone;
251}
252
Drew Davenport8998f8b2024-10-24 10:15:12 -0600253auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
254 bool seamless, QueuedConfigTiming *out_timing)
255 -> ConfigError {
256 if (configs_.hwc_configs.count(config) == 0) {
257 ALOGE("Could not find active mode for %u", config);
258 return ConfigError::kBadConfig;
259 }
260
261 // TODO: Add support for seamless configuration changes.
262 if (seamless) {
263 return ConfigError::kSeamlessNotAllowed;
264 }
265
266 // Request a refresh from the client one vsync period before the desired
267 // time, or simply at the desired time if there is no active configuration.
268 const HwcDisplayConfig *current_config = GetCurrentConfig();
269 out_timing->refresh_time_ns = desired_time -
270 (current_config
271 ? current_config->mode.GetVSyncPeriodNs()
272 : 0);
273 out_timing->new_vsync_time_ns = desired_time;
274
275 // Queue the config change timing to be consistent with the requested
276 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600277 staged_mode_change_time_ = out_timing->refresh_time_ns;
278 staged_mode_config_id_ = config;
279
280 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700281 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600282
283 return ConfigError::kNone;
284}
285
Drew Davenport7f356c82025-01-17 16:32:06 -0700286auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
287 if (IsInHeadlessMode()) {
288 return {};
289 }
290
291 /* In current drm_hwc design in case previous frame layer was not validated as
292 * a CLIENT, it is used by display controller (Front buffer). We have to store
293 * this state to provide the CLIENT with the release fences for such buffers.
294 */
295 for (auto &l : layers_) {
296 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
297 HWC2::Composition::Client);
298 }
299
300 // ValidateDisplay returns the number of layers that may be changed.
301 uint32_t num_types = 0;
302 uint32_t num_requests = 0;
303 backend_->ValidateDisplay(this, &num_types, &num_requests);
304
305 if (num_types == 0) {
306 return {};
307 }
308
309 // Iterate through the layers to find which layers actually changed.
310 std::vector<ChangedLayer> changed_layers;
311 for (auto &l : layers_) {
312 if (l.second.IsTypeChanged()) {
313 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
314 }
315 }
316 return changed_layers;
317}
318
Lucas Berthou30808a22025-02-05 17:52:33 +0000319auto HwcDisplay::GetDisplayBoundsMm() -> std::pair<int32_t, int32_t> {
320
321 const auto bounds = GetEdid()->GetBoundsMm();
322 if (bounds.first > 0 || bounds.second > 0) {
323 return bounds;
324 }
325
326 ALOGE("Failed to get display bounds for d=%d\n", int(handle_));
327 // mm_width and mm_height are unreliable. so only provide mm_width to avoid
328 // wrong dpi computations or other use of the values.
329 return {configs_.mm_width, -1};
330}
331
Drew Davenportb864ccf2025-01-22 14:57:36 -0700332auto HwcDisplay::AcceptValidatedComposition() -> void {
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200333 for (auto &[_, layer] : layers_) {
334 layer.AcceptTypeChange();
Drew Davenportb864ccf2025-01-22 14:57:36 -0700335 }
336}
337
338auto HwcDisplay::PresentStagedComposition(
Drew Davenportf6383482025-02-21 10:05:39 -0700339 std::optional<int64_t> desired_present_time, SharedFd &out_present_fence,
340 std::vector<ReleaseFence> &out_release_fences) -> bool {
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200341 if (IsInHeadlessMode()) {
342 return true;
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200343 }
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200344 HWC2::Error ret{};
345
346 ++total_stats_.total_frames_;
347
Drew Davenportf6383482025-02-21 10:05:39 -0700348 uint32_t vperiod_ns = 0;
349 GetDisplayVsyncPeriod(&vperiod_ns);
350
351 if (desired_present_time && vperiod_ns != 0) {
352 // DRM atomic uAPI does not support specifying that a commit should be
353 // applied to some future vsync. Until such uAPI is available, sleep in
354 // userspace until the next expected vsync time is consistent with the
355 // desired present time.
356 WaitForPresentTime(desired_present_time.value(), vperiod_ns);
357 }
358
Roman Stratiienko72ff8c32025-02-09 00:48:44 +0200359 AtomicCommitArgs a_args{};
360 ret = CreateComposition(a_args);
361
362 if (ret != HWC2::Error::None)
363 ++total_stats_.failed_kms_present_;
364
365 if (ret == HWC2::Error::BadLayer) {
366 // Can we really have no client or device layers?
367 return true;
368 }
369 if (ret != HWC2::Error::None)
370 return false;
371
372 out_present_fence = a_args.out_fence;
373
374 // Reset the color matrix so we don't apply it over and over again.
375 color_matrix_ = {};
376
377 ++frame_no_;
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200378
379 if (!out_present_fence) {
380 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700381 }
382
383 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200384 if (l.second.GetPriorBufferScanOutFlag()) {
385 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700386 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700387 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200388
389 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700390}
391
Roman Stratiienko63762a92023-09-18 22:33:45 +0300392void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200393 Deinit();
394
Roman Stratiienko63762a92023-09-18 22:33:45 +0300395 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200396
Roman Stratiienko63762a92023-09-18 22:33:45 +0300397 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200398 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000399 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200400 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000401 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200402 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200403}
404
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200405void HwcDisplay::Deinit() {
406 if (pipeline_ != nullptr) {
407 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200408 a_args.composition = std::make_shared<DrmKmsPlan>();
409 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300410 a_args.composition = {};
411 a_args.active = false;
412 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200413
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200414 current_plan_.reset();
415 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200416 if (flatcon_) {
417 flatcon_->StopThread();
418 flatcon_.reset();
419 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200420 }
421
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200422 if (vsync_worker_) {
423 vsync_worker_->StopThread();
424 vsync_worker_ = {};
425 }
426
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200427 client_layer_.ClearSlots();
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200428}
429
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200430HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200431 ChosePreferredConfig();
432
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300433 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700434 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300435 if (!vsync_worker_) {
436 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
437 return HWC2::Error::BadDisplay;
438 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200439 }
440
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200441 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200442 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200443 if (ret) {
444 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
445 return HWC2::Error::BadDisplay;
446 }
Drew Davenport93443182023-12-14 09:25:45 +0000447 auto flatcbk = (struct FlatConCallbacks){
448 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200449 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200450 }
451
Roman Stratiienko44b95772025-01-22 18:03:36 +0200452 HwcLayer::LayerProperties lp;
453 lp.blend_mode = BufferBlendMode::kPreMult;
454 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200455
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400456 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200457
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200458 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200459}
460
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600461std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
462 if (IsInHeadlessMode()) {
463 // The pipeline can be nullptr in headless mode, so return the default
464 // "normal" mode.
465 return PanelOrientation::kModePanelOrientationNormal;
466 }
467
468 DrmDisplayPipeline &pipeline = GetPipe();
469 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
470 ALOGW(
471 "No display pipeline present to query the panel orientation property.");
472 return {};
473 }
474
475 return pipeline.connector->Get()->GetPanelOrientation();
476}
477
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200478HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200479 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300480 if (type_ == HWC2::DisplayType::Virtual) {
481 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
482 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200483 err = configs_.Update(*pipeline_->connector->Get());
484 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300485 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200486 }
487 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200489 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490
Roman Stratiienko0137f862022-01-04 18:27:40 +0200491 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200492}
493
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200494auto HwcDisplay::CreateLayer(ILayerId new_layer_id) -> bool {
495 if (layers_.count(new_layer_id) > 0)
496 return false;
497
498 layers_.emplace(new_layer_id, HwcLayer(this));
499
500 return true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501}
502
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200503auto HwcDisplay::DestroyLayer(ILayerId layer_id) -> bool {
504 auto count = layers_.erase(layer_id);
505 return count != 0;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200506}
507
508HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700509 // If a config has been queued, it is considered the "active" config.
510 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
511 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200512 return HWC2::Error::BadConfig;
513
Drew Davenportfe70c802024-11-07 13:02:21 -0700514 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 return HWC2::Error::None;
516}
517
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200519 if (IsInHeadlessMode()) {
520 *num_modes = 1;
521 if (modes)
522 modes[0] = HAL_COLOR_MODE_NATIVE;
523 return HWC2::Error::None;
524 }
525
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500526 if (!modes) {
527 std::vector<Colormode> temp_modes;
528 GetEdid()->GetColorModes(temp_modes);
529 *num_modes = temp_modes.size();
530 return HWC2::Error::None;
531 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500533 std::vector<Colormode> temp_modes;
534 std::vector<int32_t> out_modes(modes, modes + *num_modes);
535 GetEdid()->GetColorModes(temp_modes);
536 if (temp_modes.empty()) {
537 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
538 return HWC2::Error::None;
539 }
540
541 for (auto &c : temp_modes)
542 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543
544 return HWC2::Error::None;
545}
546
547HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
548 int32_t attribute_in,
549 int32_t *value) {
550 int conf = static_cast<int>(config);
551
Roman Stratiienko0137f862022-01-04 18:27:40 +0200552 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200553 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554 return HWC2::Error::BadConfig;
555 }
556
Roman Stratiienko0137f862022-01-04 18:27:40 +0200557 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200558
559 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300560 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200561 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
562 switch (attribute) {
563 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200564 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200565 break;
566 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200567 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200568 break;
569 case HWC2::Attribute::VsyncPeriod:
570 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600571 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200572 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000573 case HWC2::Attribute::DpiY:
Lucas Berthou30808a22025-02-05 17:52:33 +0000574 *value = GetEdid()->GetDpiY();
575 if (*value < 0) {
576 // default to raw mode DpiX for both x and y when no good value
577 // can be provided from edid.
578 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
579 kUmPerInch / mm_width)
580 : -1;
581 }
582 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200583 case HWC2::Attribute::DpiX:
584 // Dots per 1000 inches
Lucas Berthou30808a22025-02-05 17:52:33 +0000585 *value = GetEdid()->GetDpiX();
586 if (*value < 0) {
587 // default to raw mode DpiX for both x and y when no good value
588 // can be provided from edid.
589 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
590 kUmPerInch / mm_width)
591 : -1;
592 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200593 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200594#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200595 case HWC2::Attribute::ConfigGroup:
596 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
597 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200598 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 break;
600#endif
601 default:
602 *value = -1;
603 return HWC2::Error::BadConfig;
604 }
605 return HWC2::Error::None;
606}
607
Drew Davenportf7e88332024-09-06 12:54:38 -0600608HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
609 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200610 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200611 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200612 if (hwc_config.second.disabled) {
613 continue;
614 }
615
616 if (configs != nullptr) {
617 if (idx >= *num_configs) {
618 break;
619 }
620 configs[idx] = hwc_config.second.id;
621 }
622
623 idx++;
624 }
625 *num_configs = idx;
626 return HWC2::Error::None;
627}
628
629HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
630 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200631 if (IsInHeadlessMode()) {
632 stream << "null-display";
633 } else {
634 stream << "display-" << GetPipe().connector->Get()->GetId();
635 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300636 auto string = stream.str();
637 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200638 if (!name) {
639 *size = length;
640 return HWC2::Error::None;
641 }
642
643 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
644 strncpy(name, string.c_str(), *size);
645 return HWC2::Error::None;
646}
647
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
649 *type = static_cast<int32_t>(type_);
650 return HWC2::Error::None;
651}
652
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500653HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
654 float *max_luminance,
655 float *max_average_luminance,
656 float *min_luminance) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200657 if (IsInHeadlessMode()) {
658 *num_types = 0;
659 return HWC2::Error::None;
660 }
661
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500662 if (!types) {
663 std::vector<ui::Hdr> temp_types;
664 float lums[3] = {0.F};
665 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
666 *num_types = temp_types.size();
667 return HWC2::Error::None;
668 }
669
670 std::vector<ui::Hdr> temp_types;
671 std::vector<int32_t> out_types(types, types + *num_types);
672 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
673 max_average_luminance, min_luminance);
674 for (auto &t : temp_types) {
675 switch (t) {
676 case ui::Hdr::HDR10:
677 out_types.emplace_back(HAL_HDR_HDR10);
678 break;
679 case ui::Hdr::HLG:
680 out_types.emplace_back(HAL_HDR_HLG);
681 break;
682 default:
683 // Ignore any other HDR types
684 break;
685 }
686 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200687 return HWC2::Error::None;
688}
689
Drew Davenport97b5abc2024-11-07 10:43:54 -0700690AtomicCommitArgs HwcDisplay::CreateModesetCommit(
691 const HwcDisplayConfig *config,
692 const std::optional<LayerData> &modeset_layer) {
693 AtomicCommitArgs args{};
694
695 args.color_matrix = color_matrix_;
696 args.content_type = content_type_;
697 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500698 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700699
700 std::vector<LayerData> composition_layers;
701 if (modeset_layer) {
702 composition_layers.emplace_back(modeset_layer.value());
703 }
704
705 if (composition_layers.empty()) {
706 ALOGW("Attempting to create a modeset commit without a layer.");
707 }
708
709 args.display_mode = config->mode;
710 args.active = true;
711 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
712 std::move(
713 composition_layers));
714 ALOGW_IF(!args.composition, "No composition for blocking modeset");
715
716 return args;
717}
718
Drew Davenportf6383482025-02-21 10:05:39 -0700719void HwcDisplay::WaitForPresentTime(int64_t present_time,
720 uint32_t vsync_period_ns) {
721 const int64_t current_time = ResourceManager::GetTimeMonotonicNs();
722 int64_t next_vsync_time = vsync_worker_->GetNextVsyncTimestamp(current_time);
723
724 int64_t vsync_after_present_time = vsync_worker_->GetNextVsyncTimestamp(
725 present_time);
726 int64_t vsync_before_present_time = vsync_after_present_time -
727 vsync_period_ns;
728
729 // Check if |present_time| is closer to the expected vsync before or after.
730 int64_t desired_vsync = (vsync_after_present_time - present_time) <
731 (present_time - vsync_before_present_time)
732 ? vsync_after_present_time
733 : vsync_before_present_time;
734
735 // Don't sleep if desired_vsync is before or nearly equal to vsync_period of
736 // the next expected vsync.
737 const int64_t quarter_vsync_period = vsync_period_ns / 4;
738 if ((desired_vsync - next_vsync_time) < quarter_vsync_period) {
739 return;
740 }
741
742 // Sleep until 75% vsync_period before the desired_vsync.
743 int64_t sleep_until = desired_vsync - (quarter_vsync_period * 3);
744 struct timespec sleep_until_ts{};
745 constexpr int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
746 sleep_until_ts.tv_sec = int(sleep_until / kOneSecondNs);
747 sleep_until_ts.tv_nsec = int(sleep_until -
748 (sleep_until_ts.tv_sec * kOneSecondNs));
749 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &sleep_until_ts, nullptr);
750}
751
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200752// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200753HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200754 if (IsInHeadlessMode()) {
755 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
756 return HWC2::Error::None;
757 }
758
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200759 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400760 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400761 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500762 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200763
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200764 uint32_t prev_vperiod_ns = 0;
765 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200766
Drew Davenportd387c842024-12-16 16:57:24 -0700767 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700768 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200769 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700770 const HwcDisplayConfig *staged_config = GetConfig(
771 staged_mode_config_id_.value());
772 if (staged_config == nullptr) {
773 return HWC2::Error::BadConfig;
774 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200775
Drew Davenportfe70c802024-11-07 13:02:21 -0700776 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700777 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200778 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700779 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200780 }
781 }
782
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200783 // order the layers by z-order
784 bool use_client_layer = false;
785 uint32_t client_z_order = UINT32_MAX;
786 std::map<uint32_t, HwcLayer *> z_map;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000787 std::optional<LayerData> cursor_layer = std::nullopt;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200788 for (auto &[_, layer] : layers_) {
789 switch (layer.GetValidatedType()) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200790 case HWC2::Composition::Device:
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200791 z_map.emplace(layer.GetZOrder(), &layer);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200792 break;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000793 case HWC2::Composition::Cursor:
794 if (!cursor_layer.has_value()) {
795 layer.PopulateLayerData();
796 cursor_layer = layer.GetLayerData();
797 } else {
798 ALOGW("Detected multiple cursor layers");
799 z_map.emplace(layer.GetZOrder(), &layer);
800 }
801 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200802 case HWC2::Composition::Client:
803 // Place it at the z_order of the lowest client layer
804 use_client_layer = true;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200805 client_z_order = std::min(client_z_order, layer.GetZOrder());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200806 break;
807 default:
808 continue;
809 }
810 }
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200811 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300812 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200813
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200814 client_layer_.PopulateLayerData();
815 if (!client_layer_.IsLayerUsableAsDevice()) {
816 ALOGE_IF(!a_args.test_only,
817 "Client layer must be always usable by DRM/KMS");
818 /* This may be normally triggered on validation of the first frame
819 * containing CLIENT layer. At this moment client buffer is not yet
820 * provided by the CLIENT.
821 * This may be triggered once in HwcLayer lifecycle in case FB can't be
822 * imported. For example when non-contiguous buffer is imported into
823 * contiguous-only DRM/KMS driver.
824 */
825 return HWC2::Error::BadLayer;
826 }
827 }
828
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200829 if (z_map.empty())
830 return HWC2::Error::BadLayer;
831
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200832 std::vector<LayerData> composition_layers;
833
834 /* Import & populate */
835 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200836 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200837 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838
839 // now that they're ordered by z, add them to the composition
840 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200841 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200842 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200843 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200844 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 }
846
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200847 /* Store plan to ensure shared planes won't be stolen by other display
848 * in between of ValidateDisplay() and PresentDisplay() calls
849 */
850 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
Andrew Wolfers5c530832024-12-03 16:30:26 +0000851 std::move(composition_layers),
852 cursor_layer);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300853
854 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200855 writeback_layer_->PopulateLayerData();
856 if (!writeback_layer_->IsLayerUsableAsDevice()) {
857 ALOGE("Output layer must be always usable by DRM/KMS");
858 return HWC2::Error::BadLayer;
859 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300860 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
861 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
862 .acquire_fence;
863 }
864
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200865 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700866 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200867 return HWC2::Error::BadConfig;
868 }
869
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200870 a_args.composition = current_plan_;
871
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300872 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200873
874 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700875 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200876 return HWC2::Error::BadParameter;
877 }
878
Drew Davenportd387c842024-12-16 16:57:24 -0700879 if (new_vsync_period_ns) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700880 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700881
882 vsync_worker_->SetVsyncTimestampTracking(false);
883 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
884 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000885 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700886 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000887 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200888 }
Drew Davenportbf711eb2025-02-19 09:41:20 -0700889 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200890 }
891
892 return HWC2::Error::None;
893}
894
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200895HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
896 int64_t change_time) {
897 if (configs_.hwc_configs.count(config) == 0) {
898 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200899 return HWC2::Error::BadConfig;
900 }
901
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200902 staged_mode_change_time_ = change_time;
903 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200904
905 return HWC2::Error::None;
906}
907
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200908HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
909 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
910}
911
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200912HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400913 /* Maps to the Colorspace DRM connector property:
914 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
915 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500916 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200917 return HWC2::Error::BadParameter;
918
Sasha McIntosh5294f092024-09-18 18:14:54 -0400919 switch (mode) {
920 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400921 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400922 colorspace_ = Colorspace::kDefault;
923 break;
924 case HAL_COLOR_MODE_STANDARD_BT601_625:
925 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
926 case HAL_COLOR_MODE_STANDARD_BT601_525:
927 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400928 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400929 // The DP spec does not say whether this is the 525 or the 625 line version.
930 colorspace_ = Colorspace::kBt601Ycc;
931 break;
932 case HAL_COLOR_MODE_STANDARD_BT709:
933 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400934 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400935 colorspace_ = Colorspace::kBt709Ycc;
936 break;
937 case HAL_COLOR_MODE_DCI_P3:
938 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400939 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400940 colorspace_ = Colorspace::kDciP3RgbD65;
941 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500942 case HAL_COLOR_MODE_DISPLAY_BT2020: {
943 std::vector<ui::Hdr> hdr_types;
944 GetEdid()->GetSupportedHdrTypes(hdr_types);
945 if (!hdr_types.empty()) {
946 auto ret = SetHdrOutputMetadata(hdr_types.front());
947 if (ret != HWC2::Error::None)
948 return ret;
949 }
950 colorspace_ = Colorspace::kBt2020Rgb;
951 break;
952 }
Sasha McIntosh5294f092024-09-18 18:14:54 -0400953 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500954 case HAL_COLOR_MODE_BT2020:
955 case HAL_COLOR_MODE_BT2100_PQ:
956 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -0400957 default:
958 return HWC2::Error::Unsupported;
959 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960
961 color_mode_ = mode;
962 return HWC2::Error::None;
963}
964
965HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
966 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
967 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
968 return HWC2::Error::BadParameter;
969
970 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
971 return HWC2::Error::BadParameter;
972
973 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200974
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200975 if (IsInHeadlessMode())
976 return HWC2::Error::None;
977
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200978 if (!GetPipe().crtc->Get()->GetCtmProperty())
979 return HWC2::Error::None;
980
981 switch (color_transform_hint_) {
982 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400983 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200984 break;
985 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400986 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -0700987 {
988 for (int i = 12; i < 14; i++) {
989 if (matrix[i] != 0.F)
990 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200991 }
Drew Davenport76c17a82025-01-15 15:02:45 -0700992 std::array<float, 16> aidl_matrix = kIdentityMatrix;
993 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
994 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200995 }
996 break;
997 default:
998 return HWC2::Error::Unsupported;
999 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001000
1001 return HWC2::Error::None;
1002}
1003
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001004bool HwcDisplay::CtmByGpu() {
1005 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1006 return false;
1007
1008 if (GetPipe().crtc->Get()->GetCtmProperty())
1009 return false;
1010
Drew Davenport93443182023-12-14 09:25:45 +00001011 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001012 return false;
1013
1014 return true;
1015}
1016
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001017HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1018 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001019
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001020 AtomicCommitArgs a_args{};
1021
1022 switch (mode) {
1023 case HWC2::PowerMode::Off:
1024 a_args.active = false;
1025 break;
1026 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001027 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001028 break;
1029 case HWC2::PowerMode::Doze:
1030 case HWC2::PowerMode::DozeSuspend:
1031 return HWC2::Error::Unsupported;
1032 default:
John Stultzffe783c2024-02-14 10:51:27 -08001033 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001034 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001035 }
1036
1037 if (IsInHeadlessMode()) {
1038 return HWC2::Error::None;
1039 }
1040
Jia Ren80566fe2022-11-17 17:26:00 +08001041 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001042 /*
1043 * Setting the display to active before we have a composition
1044 * can break some drivers, so skip setting a_args.active to
1045 * true, as the next composition frame will implicitly activate
1046 * the display
1047 */
1048 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1049 ? HWC2::Error::None
1050 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001051 };
1052
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001053 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001054 if (err) {
1055 ALOGE("Failed to apply the dpms composition err=%d", err);
1056 return HWC2::Error::BadParameter;
1057 }
1058 return HWC2::Error::None;
1059}
1060
1061HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001062 if (type_ == HWC2::DisplayType::Virtual) {
1063 return HWC2::Error::None;
1064 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001065 if (!vsync_worker_) {
1066 return HWC2::Error::NoResources;
1067 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001068
Roman Stratiienko099c3112022-01-20 11:50:54 +02001069 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001070 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001071 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001072 DrmHwc *hwc = hwc_;
1073 hwc2_display_t id = handle_;
1074 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001075 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001076 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1077 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001078 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001079 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001080 return HWC2::Error::None;
1081}
1082
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001083std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1084 std::vector<HwcLayer *> ordered_layers;
1085 ordered_layers.reserve(layers_.size());
1086
1087 for (auto &[handle, layer] : layers_) {
1088 ordered_layers.emplace_back(&layer);
1089 }
1090
1091 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1092 [](const HwcLayer *lhs, const HwcLayer *rhs) {
Andrew Wolfers5c530832024-12-03 16:30:26 +00001093 // Cursor layers should always have highest zpos.
1094 if ((lhs->GetSfType() == HWC2::Composition::Cursor) !=
1095 (rhs->GetSfType() == HWC2::Composition::Cursor)) {
1096 return rhs->GetSfType() == HWC2::Composition::Cursor;
1097 }
1098
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001099 return lhs->GetZOrder() < rhs->GetZOrder();
1100 });
1101
1102 return ordered_layers;
1103}
1104
Roman Stratiienko099c3112022-01-20 11:50:54 +02001105HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1106 uint32_t *outVsyncPeriod /* ns */) {
1107 return GetDisplayAttribute(configs_.active_config_id,
1108 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1109 (int32_t *)(outVsyncPeriod));
1110}
1111
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001112// Display primary values are coded as unsigned 16-bit values in units of
1113// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1114static uint64_t ToU16ColorValue(float in) {
1115 constexpr float kPrimariesFixedPoint = 50000.F;
1116 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1117}
1118
1119HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1120 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1121 hdr_metadata_->metadata_type = 0;
1122 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1123 m->metadata_type = 0;
1124
1125 switch (type) {
1126 case ui::Hdr::HDR10:
1127 m->eotf = 2; // PQ
1128 break;
1129 case ui::Hdr::HLG:
1130 m->eotf = 3; // HLG
1131 break;
1132 default:
1133 return HWC2::Error::Unsupported;
1134 }
1135
1136 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1137 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1138 std::vector<ui::Hdr> types;
1139 float hdr_luminance[3]{0.F, 0.F, 0.F};
1140 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1141 &hdr_luminance[2]);
1142 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1143 hdr_luminance[0]);
1144 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1145 // The min luminance value is coded as an unsigned 16-bit value in units of
1146 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1147 // represents 6.5535 cd/m2.
1148 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1149 10000.F);
1150
1151 auto gamut = ColorGamut::BT2020();
1152 auto primaries = gamut.getPrimaries();
1153 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1154 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1155 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1156 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1157 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1158 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1159
1160 auto whitePoint = gamut.getWhitePoint();
1161 m->white_point.x = ToU16ColorValue(whitePoint.x);
1162 m->white_point.y = ToU16ColorValue(whitePoint.y);
1163
1164 return HWC2::Error::None;
1165}
1166
Roman Stratiienko6b405052022-12-10 19:09:10 +02001167#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001168HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001169 if (IsInHeadlessMode()) {
1170 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1171 return HWC2::Error::None;
1172 }
1173 /* Primary display should be always internal,
1174 * otherwise SF will be unhappy and will crash
1175 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001176 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001177 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001178 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001179 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1180 else
1181 return HWC2::Error::BadConfig;
1182
1183 return HWC2::Error::None;
1184}
1185
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001186HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001187 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001188 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1189 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001190 if (type_ == HWC2::DisplayType::Virtual) {
1191 return HWC2::Error::None;
1192 }
1193
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001194 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1195 return HWC2::Error::BadParameter;
1196 }
1197
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001198 uint32_t current_vsync_period{};
1199 GetDisplayVsyncPeriod(&current_vsync_period);
1200
1201 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1202 return HWC2::Error::SeamlessNotAllowed;
1203 }
1204
1205 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1206 ->desiredTimeNanos -
1207 current_vsync_period;
1208 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1209 if (ret != HWC2::Error::None) {
1210 return ret;
1211 }
1212
1213 outTimeline->refreshRequired = true;
1214 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1215 ->desiredTimeNanos;
1216
Drew Davenport33121b72024-12-13 14:59:35 -07001217 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001218
1219 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001220}
1221
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001222HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001223 /* Maps exactly to the content_type DRM connector property:
1224 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001225 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001226 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1227 return HWC2::Error::BadParameter;
1228
1229 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001230
1231 return HWC2::Error::None;
1232}
1233#endif
1234
Roman Stratiienko6b405052022-12-10 19:09:10 +02001235#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001236HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1237 uint32_t *outDataSize,
1238 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001239 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001240 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001241 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001242
Gil Dekel907a51a2025-02-14 22:44:01 -05001243 auto *connector = GetPipe().connector->Get();
1244 auto blob = connector->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001245 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001246 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001247 }
1248
Gil Dekel907a51a2025-02-14 22:44:01 -05001249 constexpr uint8_t kDrmDeviceBitShift = 5U;
1250 constexpr uint8_t kDrmDeviceBitMask = 0xE0;
1251 constexpr uint8_t kConnectorBitMask = 0x1F;
1252 const auto kDrmIdx = static_cast<uint8_t>(
1253 connector->GetDev().GetIndexInDevArray());
1254 const auto kConnectorIdx = static_cast<uint8_t>(
1255 connector->GetIndexInResArray());
1256 *outPort = (((kDrmIdx << kDrmDeviceBitShift) & kDrmDeviceBitMask) |
1257 (kConnectorIdx & kConnectorBitMask));
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001258
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001259 if (outData) {
1260 *outDataSize = std::min(*outDataSize, blob->length);
1261 memcpy(outData, blob->data, *outDataSize);
1262 } else {
1263 *outDataSize = blob->length;
1264 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001265
1266 return HWC2::Error::None;
1267}
1268
1269HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001270 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001271 if (outNumCapabilities == nullptr) {
1272 return HWC2::Error::BadParameter;
1273 }
1274
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001275 bool skip_ctm = false;
1276
1277 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001278 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001279 skip_ctm = true;
1280
1281 // Skip client CTM if DRM can handle it
1282 if (!skip_ctm && !IsInHeadlessMode() &&
1283 GetPipe().crtc->Get()->GetCtmProperty())
1284 skip_ctm = true;
1285
1286 if (!skip_ctm) {
1287 *outNumCapabilities = 0;
1288 return HWC2::Error::None;
1289 }
1290
1291 *outNumCapabilities = 1;
1292 if (outCapabilities) {
1293 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1294 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001295
1296 return HWC2::Error::None;
1297}
1298
Roman Stratiienko6b405052022-12-10 19:09:10 +02001299#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001300
Roman Stratiienko6b405052022-12-10 19:09:10 +02001301#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001302
1303HWC2::Error HwcDisplay::GetRenderIntents(
1304 int32_t mode, uint32_t *outNumIntents,
1305 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1306 if (mode != HAL_COLOR_MODE_NATIVE) {
1307 return HWC2::Error::BadParameter;
1308 }
1309
1310 if (outIntents == nullptr) {
1311 *outNumIntents = 1;
1312 return HWC2::Error::None;
1313 }
1314 *outNumIntents = 1;
1315 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1316 return HWC2::Error::None;
1317}
1318
1319HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1320 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1321 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1322 return HWC2::Error::BadParameter;
1323
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001324 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1325 return HWC2::Error::Unsupported;
1326
Sasha McIntosh5294f092024-09-18 18:14:54 -04001327 auto err = SetColorMode(mode);
1328 if (err != HWC2::Error::None) return err;
1329
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001330 return HWC2::Error::None;
1331}
1332
Roman Stratiienko6b405052022-12-10 19:09:10 +02001333#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001334
1335const Backend *HwcDisplay::backend() const {
1336 return backend_.get();
1337}
1338
1339void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1340 backend_ = std::move(backend);
1341}
1342
Roman Stratiienko45cdacc2025-02-12 04:26:10 +02001343bool HwcDisplay::NeedsClientLayerUpdate() const {
1344 return std::any_of(layers_.begin(), layers_.end(), [](const auto &pair) {
1345 const auto &layer = pair.second;
1346 return layer.GetSfType() == HWC2::Composition::Client ||
1347 layer.GetValidatedType() == HWC2::Composition::Client;
1348 });
1349}
1350
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001351} // namespace android