blob: 82f826b408b41648000cb0fdd9f06b4ed63ee34f [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(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200339 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
340 -> 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
348 AtomicCommitArgs a_args{};
349 ret = CreateComposition(a_args);
350
351 if (ret != HWC2::Error::None)
352 ++total_stats_.failed_kms_present_;
353
354 if (ret == HWC2::Error::BadLayer) {
355 // Can we really have no client or device layers?
356 return true;
357 }
358 if (ret != HWC2::Error::None)
359 return false;
360
361 out_present_fence = a_args.out_fence;
362
363 // Reset the color matrix so we don't apply it over and over again.
364 color_matrix_ = {};
365
366 ++frame_no_;
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200367
368 if (!out_present_fence) {
369 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700370 }
371
372 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200373 if (l.second.GetPriorBufferScanOutFlag()) {
374 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700375 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700376 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200377
378 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700379}
380
Roman Stratiienko63762a92023-09-18 22:33:45 +0300381void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200382 Deinit();
383
Roman Stratiienko63762a92023-09-18 22:33:45 +0300384 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200385
Roman Stratiienko63762a92023-09-18 22:33:45 +0300386 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200387 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000388 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200389 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000390 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200391 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200392}
393
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200394void HwcDisplay::Deinit() {
395 if (pipeline_ != nullptr) {
396 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200397 a_args.composition = std::make_shared<DrmKmsPlan>();
398 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300399 a_args.composition = {};
400 a_args.active = false;
401 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200402
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200403 current_plan_.reset();
404 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200405 if (flatcon_) {
406 flatcon_->StopThread();
407 flatcon_.reset();
408 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200409 }
410
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200411 if (vsync_worker_) {
412 vsync_worker_->StopThread();
413 vsync_worker_ = {};
414 }
415
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200416 client_layer_.ClearSlots();
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200417}
418
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200419HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200420 ChosePreferredConfig();
421
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300422 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700423 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300424 if (!vsync_worker_) {
425 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
426 return HWC2::Error::BadDisplay;
427 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200428 }
429
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200430 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200431 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200432 if (ret) {
433 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
434 return HWC2::Error::BadDisplay;
435 }
Drew Davenport93443182023-12-14 09:25:45 +0000436 auto flatcbk = (struct FlatConCallbacks){
437 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200438 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200439 }
440
Roman Stratiienko44b95772025-01-22 18:03:36 +0200441 HwcLayer::LayerProperties lp;
442 lp.blend_mode = BufferBlendMode::kPreMult;
443 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400445 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200446
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200447 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200448}
449
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600450std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
451 if (IsInHeadlessMode()) {
452 // The pipeline can be nullptr in headless mode, so return the default
453 // "normal" mode.
454 return PanelOrientation::kModePanelOrientationNormal;
455 }
456
457 DrmDisplayPipeline &pipeline = GetPipe();
458 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
459 ALOGW(
460 "No display pipeline present to query the panel orientation property.");
461 return {};
462 }
463
464 return pipeline.connector->Get()->GetPanelOrientation();
465}
466
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200467HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200468 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300469 if (type_ == HWC2::DisplayType::Virtual) {
470 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
471 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200472 err = configs_.Update(*pipeline_->connector->Get());
473 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300474 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200475 }
476 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200477 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200478 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479
Roman Stratiienko0137f862022-01-04 18:27:40 +0200480 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200481}
482
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200483auto HwcDisplay::CreateLayer(ILayerId new_layer_id) -> bool {
484 if (layers_.count(new_layer_id) > 0)
485 return false;
486
487 layers_.emplace(new_layer_id, HwcLayer(this));
488
489 return true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490}
491
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200492auto HwcDisplay::DestroyLayer(ILayerId layer_id) -> bool {
493 auto count = layers_.erase(layer_id);
494 return count != 0;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200495}
496
497HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700498 // If a config has been queued, it is considered the "active" config.
499 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
500 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501 return HWC2::Error::BadConfig;
502
Drew Davenportfe70c802024-11-07 13:02:21 -0700503 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504 return HWC2::Error::None;
505}
506
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200508 if (IsInHeadlessMode()) {
509 *num_modes = 1;
510 if (modes)
511 modes[0] = HAL_COLOR_MODE_NATIVE;
512 return HWC2::Error::None;
513 }
514
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500515 if (!modes) {
516 std::vector<Colormode> temp_modes;
517 GetEdid()->GetColorModes(temp_modes);
518 *num_modes = temp_modes.size();
519 return HWC2::Error::None;
520 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500522 std::vector<Colormode> temp_modes;
523 std::vector<int32_t> out_modes(modes, modes + *num_modes);
524 GetEdid()->GetColorModes(temp_modes);
525 if (temp_modes.empty()) {
526 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
527 return HWC2::Error::None;
528 }
529
530 for (auto &c : temp_modes)
531 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532
533 return HWC2::Error::None;
534}
535
536HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
537 int32_t attribute_in,
538 int32_t *value) {
539 int conf = static_cast<int>(config);
540
Roman Stratiienko0137f862022-01-04 18:27:40 +0200541 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200542 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543 return HWC2::Error::BadConfig;
544 }
545
Roman Stratiienko0137f862022-01-04 18:27:40 +0200546 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200547
548 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300549 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200550 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
551 switch (attribute) {
552 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200553 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554 break;
555 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200556 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200557 break;
558 case HWC2::Attribute::VsyncPeriod:
559 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600560 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200561 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000562 case HWC2::Attribute::DpiY:
Lucas Berthou30808a22025-02-05 17:52:33 +0000563 *value = GetEdid()->GetDpiY();
564 if (*value < 0) {
565 // default to raw mode DpiX for both x and y when no good value
566 // can be provided from edid.
567 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
568 kUmPerInch / mm_width)
569 : -1;
570 }
571 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200572 case HWC2::Attribute::DpiX:
573 // Dots per 1000 inches
Lucas Berthou30808a22025-02-05 17:52:33 +0000574 *value = GetEdid()->GetDpiX();
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 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200582 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200583#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200584 case HWC2::Attribute::ConfigGroup:
585 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
586 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200587 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200588 break;
589#endif
590 default:
591 *value = -1;
592 return HWC2::Error::BadConfig;
593 }
594 return HWC2::Error::None;
595}
596
Drew Davenportf7e88332024-09-06 12:54:38 -0600597HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
598 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200600 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200601 if (hwc_config.second.disabled) {
602 continue;
603 }
604
605 if (configs != nullptr) {
606 if (idx >= *num_configs) {
607 break;
608 }
609 configs[idx] = hwc_config.second.id;
610 }
611
612 idx++;
613 }
614 *num_configs = idx;
615 return HWC2::Error::None;
616}
617
618HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
619 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200620 if (IsInHeadlessMode()) {
621 stream << "null-display";
622 } else {
623 stream << "display-" << GetPipe().connector->Get()->GetId();
624 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300625 auto string = stream.str();
626 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 if (!name) {
628 *size = length;
629 return HWC2::Error::None;
630 }
631
632 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
633 strncpy(name, string.c_str(), *size);
634 return HWC2::Error::None;
635}
636
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200637HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
638 *type = static_cast<int32_t>(type_);
639 return HWC2::Error::None;
640}
641
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500642HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
643 float *max_luminance,
644 float *max_average_luminance,
645 float *min_luminance) {
Roman Stratiienkod7e108e2025-02-11 04:44:00 +0200646 if (IsInHeadlessMode()) {
647 *num_types = 0;
648 return HWC2::Error::None;
649 }
650
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500651 if (!types) {
652 std::vector<ui::Hdr> temp_types;
653 float lums[3] = {0.F};
654 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
655 *num_types = temp_types.size();
656 return HWC2::Error::None;
657 }
658
659 std::vector<ui::Hdr> temp_types;
660 std::vector<int32_t> out_types(types, types + *num_types);
661 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
662 max_average_luminance, min_luminance);
663 for (auto &t : temp_types) {
664 switch (t) {
665 case ui::Hdr::HDR10:
666 out_types.emplace_back(HAL_HDR_HDR10);
667 break;
668 case ui::Hdr::HLG:
669 out_types.emplace_back(HAL_HDR_HLG);
670 break;
671 default:
672 // Ignore any other HDR types
673 break;
674 }
675 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200676 return HWC2::Error::None;
677}
678
Drew Davenport97b5abc2024-11-07 10:43:54 -0700679AtomicCommitArgs HwcDisplay::CreateModesetCommit(
680 const HwcDisplayConfig *config,
681 const std::optional<LayerData> &modeset_layer) {
682 AtomicCommitArgs args{};
683
684 args.color_matrix = color_matrix_;
685 args.content_type = content_type_;
686 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500687 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700688
689 std::vector<LayerData> composition_layers;
690 if (modeset_layer) {
691 composition_layers.emplace_back(modeset_layer.value());
692 }
693
694 if (composition_layers.empty()) {
695 ALOGW("Attempting to create a modeset commit without a layer.");
696 }
697
698 args.display_mode = config->mode;
699 args.active = true;
700 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
701 std::move(
702 composition_layers));
703 ALOGW_IF(!args.composition, "No composition for blocking modeset");
704
705 return args;
706}
707
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200708// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200709HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200710 if (IsInHeadlessMode()) {
711 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
712 return HWC2::Error::None;
713 }
714
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200715 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400716 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400717 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500718 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200719
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200720 uint32_t prev_vperiod_ns = 0;
721 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200722
Drew Davenportd387c842024-12-16 16:57:24 -0700723 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700724 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200725 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700726 const HwcDisplayConfig *staged_config = GetConfig(
727 staged_mode_config_id_.value());
728 if (staged_config == nullptr) {
729 return HWC2::Error::BadConfig;
730 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200731
Drew Davenportfe70c802024-11-07 13:02:21 -0700732 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700733 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200734 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700735 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200736 }
737 }
738
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200739 // order the layers by z-order
740 bool use_client_layer = false;
741 uint32_t client_z_order = UINT32_MAX;
742 std::map<uint32_t, HwcLayer *> z_map;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000743 std::optional<LayerData> cursor_layer = std::nullopt;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200744 for (auto &[_, layer] : layers_) {
745 switch (layer.GetValidatedType()) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746 case HWC2::Composition::Device:
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200747 z_map.emplace(layer.GetZOrder(), &layer);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748 break;
Andrew Wolfers5c530832024-12-03 16:30:26 +0000749 case HWC2::Composition::Cursor:
750 if (!cursor_layer.has_value()) {
751 layer.PopulateLayerData();
752 cursor_layer = layer.GetLayerData();
753 } else {
754 ALOGW("Detected multiple cursor layers");
755 z_map.emplace(layer.GetZOrder(), &layer);
756 }
757 break;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200758 case HWC2::Composition::Client:
759 // Place it at the z_order of the lowest client layer
760 use_client_layer = true;
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200761 client_z_order = std::min(client_z_order, layer.GetZOrder());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200762 break;
763 default:
764 continue;
765 }
766 }
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200767 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300768 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200769
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200770 client_layer_.PopulateLayerData();
771 if (!client_layer_.IsLayerUsableAsDevice()) {
772 ALOGE_IF(!a_args.test_only,
773 "Client layer must be always usable by DRM/KMS");
774 /* This may be normally triggered on validation of the first frame
775 * containing CLIENT layer. At this moment client buffer is not yet
776 * provided by the CLIENT.
777 * This may be triggered once in HwcLayer lifecycle in case FB can't be
778 * imported. For example when non-contiguous buffer is imported into
779 * contiguous-only DRM/KMS driver.
780 */
781 return HWC2::Error::BadLayer;
782 }
783 }
784
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 if (z_map.empty())
786 return HWC2::Error::BadLayer;
787
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200788 std::vector<LayerData> composition_layers;
789
790 /* Import & populate */
791 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200792 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200793 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200794
795 // now that they're ordered by z, add them to the composition
796 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200797 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200798 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200799 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200800 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200801 }
802
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200803 /* Store plan to ensure shared planes won't be stolen by other display
804 * in between of ValidateDisplay() and PresentDisplay() calls
805 */
806 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
Andrew Wolfers5c530832024-12-03 16:30:26 +0000807 std::move(composition_layers),
808 cursor_layer);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300809
810 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200811 writeback_layer_->PopulateLayerData();
812 if (!writeback_layer_->IsLayerUsableAsDevice()) {
813 ALOGE("Output layer must be always usable by DRM/KMS");
814 return HWC2::Error::BadLayer;
815 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300816 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
817 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
818 .acquire_fence;
819 }
820
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200821 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700822 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200823 return HWC2::Error::BadConfig;
824 }
825
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200826 a_args.composition = current_plan_;
827
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300828 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200829
830 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700831 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200832 return HWC2::Error::BadParameter;
833 }
834
Drew Davenportd387c842024-12-16 16:57:24 -0700835 if (new_vsync_period_ns) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700836 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700837
838 vsync_worker_->SetVsyncTimestampTracking(false);
839 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
840 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000841 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700842 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000843 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200844 }
Drew Davenportbf711eb2025-02-19 09:41:20 -0700845 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200846 }
847
848 return HWC2::Error::None;
849}
850
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200851HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
852 int64_t change_time) {
853 if (configs_.hwc_configs.count(config) == 0) {
854 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200855 return HWC2::Error::BadConfig;
856 }
857
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200858 staged_mode_change_time_ = change_time;
859 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200860
861 return HWC2::Error::None;
862}
863
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200864HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
865 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
866}
867
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200868HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400869 /* Maps to the Colorspace DRM connector property:
870 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
871 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500872 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200873 return HWC2::Error::BadParameter;
874
Sasha McIntosh5294f092024-09-18 18:14:54 -0400875 switch (mode) {
876 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400877 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400878 colorspace_ = Colorspace::kDefault;
879 break;
880 case HAL_COLOR_MODE_STANDARD_BT601_625:
881 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
882 case HAL_COLOR_MODE_STANDARD_BT601_525:
883 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400884 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400885 // The DP spec does not say whether this is the 525 or the 625 line version.
886 colorspace_ = Colorspace::kBt601Ycc;
887 break;
888 case HAL_COLOR_MODE_STANDARD_BT709:
889 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400890 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400891 colorspace_ = Colorspace::kBt709Ycc;
892 break;
893 case HAL_COLOR_MODE_DCI_P3:
894 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh7009cc12025-03-11 17:58:37 -0400895 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400896 colorspace_ = Colorspace::kDciP3RgbD65;
897 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500898 case HAL_COLOR_MODE_DISPLAY_BT2020: {
899 std::vector<ui::Hdr> hdr_types;
900 GetEdid()->GetSupportedHdrTypes(hdr_types);
901 if (!hdr_types.empty()) {
902 auto ret = SetHdrOutputMetadata(hdr_types.front());
903 if (ret != HWC2::Error::None)
904 return ret;
905 }
906 colorspace_ = Colorspace::kBt2020Rgb;
907 break;
908 }
Sasha McIntosh5294f092024-09-18 18:14:54 -0400909 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500910 case HAL_COLOR_MODE_BT2020:
911 case HAL_COLOR_MODE_BT2100_PQ:
912 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -0400913 default:
914 return HWC2::Error::Unsupported;
915 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200916
917 color_mode_ = mode;
918 return HWC2::Error::None;
919}
920
921HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
922 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
923 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
924 return HWC2::Error::BadParameter;
925
926 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
927 return HWC2::Error::BadParameter;
928
929 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200930
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200931 if (IsInHeadlessMode())
932 return HWC2::Error::None;
933
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200934 if (!GetPipe().crtc->Get()->GetCtmProperty())
935 return HWC2::Error::None;
936
937 switch (color_transform_hint_) {
938 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400939 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200940 break;
941 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400942 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -0700943 {
944 for (int i = 12; i < 14; i++) {
945 if (matrix[i] != 0.F)
946 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200947 }
Drew Davenport76c17a82025-01-15 15:02:45 -0700948 std::array<float, 16> aidl_matrix = kIdentityMatrix;
949 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
950 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200951 }
952 break;
953 default:
954 return HWC2::Error::Unsupported;
955 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200956
957 return HWC2::Error::None;
958}
959
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200960bool HwcDisplay::CtmByGpu() {
961 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
962 return false;
963
964 if (GetPipe().crtc->Get()->GetCtmProperty())
965 return false;
966
Drew Davenport93443182023-12-14 09:25:45 +0000967 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200968 return false;
969
970 return true;
971}
972
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
974 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300975
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200976 AtomicCommitArgs a_args{};
977
978 switch (mode) {
979 case HWC2::PowerMode::Off:
980 a_args.active = false;
981 break;
982 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300983 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200984 break;
985 case HWC2::PowerMode::Doze:
986 case HWC2::PowerMode::DozeSuspend:
987 return HWC2::Error::Unsupported;
988 default:
John Stultzffe783c2024-02-14 10:51:27 -0800989 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200990 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300991 }
992
993 if (IsInHeadlessMode()) {
994 return HWC2::Error::None;
995 }
996
Jia Ren80566fe2022-11-17 17:26:00 +0800997 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300998 /*
999 * Setting the display to active before we have a composition
1000 * can break some drivers, so skip setting a_args.active to
1001 * true, as the next composition frame will implicitly activate
1002 * the display
1003 */
1004 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1005 ? HWC2::Error::None
1006 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001007 };
1008
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001009 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001010 if (err) {
1011 ALOGE("Failed to apply the dpms composition err=%d", err);
1012 return HWC2::Error::BadParameter;
1013 }
1014 return HWC2::Error::None;
1015}
1016
1017HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001018 if (type_ == HWC2::DisplayType::Virtual) {
1019 return HWC2::Error::None;
1020 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001021 if (!vsync_worker_) {
1022 return HWC2::Error::NoResources;
1023 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001024
Roman Stratiienko099c3112022-01-20 11:50:54 +02001025 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001026 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001027 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001028 DrmHwc *hwc = hwc_;
1029 hwc2_display_t id = handle_;
1030 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001031 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001032 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1033 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001034 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001035 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001036 return HWC2::Error::None;
1037}
1038
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001039std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1040 std::vector<HwcLayer *> ordered_layers;
1041 ordered_layers.reserve(layers_.size());
1042
1043 for (auto &[handle, layer] : layers_) {
1044 ordered_layers.emplace_back(&layer);
1045 }
1046
1047 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1048 [](const HwcLayer *lhs, const HwcLayer *rhs) {
Andrew Wolfers5c530832024-12-03 16:30:26 +00001049 // Cursor layers should always have highest zpos.
1050 if ((lhs->GetSfType() == HWC2::Composition::Cursor) !=
1051 (rhs->GetSfType() == HWC2::Composition::Cursor)) {
1052 return rhs->GetSfType() == HWC2::Composition::Cursor;
1053 }
1054
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001055 return lhs->GetZOrder() < rhs->GetZOrder();
1056 });
1057
1058 return ordered_layers;
1059}
1060
Roman Stratiienko099c3112022-01-20 11:50:54 +02001061HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1062 uint32_t *outVsyncPeriod /* ns */) {
1063 return GetDisplayAttribute(configs_.active_config_id,
1064 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1065 (int32_t *)(outVsyncPeriod));
1066}
1067
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001068// Display primary values are coded as unsigned 16-bit values in units of
1069// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1070static uint64_t ToU16ColorValue(float in) {
1071 constexpr float kPrimariesFixedPoint = 50000.F;
1072 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1073}
1074
1075HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1076 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1077 hdr_metadata_->metadata_type = 0;
1078 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1079 m->metadata_type = 0;
1080
1081 switch (type) {
1082 case ui::Hdr::HDR10:
1083 m->eotf = 2; // PQ
1084 break;
1085 case ui::Hdr::HLG:
1086 m->eotf = 3; // HLG
1087 break;
1088 default:
1089 return HWC2::Error::Unsupported;
1090 }
1091
1092 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1093 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1094 std::vector<ui::Hdr> types;
1095 float hdr_luminance[3]{0.F, 0.F, 0.F};
1096 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1097 &hdr_luminance[2]);
1098 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1099 hdr_luminance[0]);
1100 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1101 // The min luminance value is coded as an unsigned 16-bit value in units of
1102 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1103 // represents 6.5535 cd/m2.
1104 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1105 10000.F);
1106
1107 auto gamut = ColorGamut::BT2020();
1108 auto primaries = gamut.getPrimaries();
1109 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1110 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1111 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1112 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1113 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1114 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1115
1116 auto whitePoint = gamut.getWhitePoint();
1117 m->white_point.x = ToU16ColorValue(whitePoint.x);
1118 m->white_point.y = ToU16ColorValue(whitePoint.y);
1119
1120 return HWC2::Error::None;
1121}
1122
Roman Stratiienko6b405052022-12-10 19:09:10 +02001123#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001124HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001125 if (IsInHeadlessMode()) {
1126 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1127 return HWC2::Error::None;
1128 }
1129 /* Primary display should be always internal,
1130 * otherwise SF will be unhappy and will crash
1131 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001132 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001133 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001134 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001135 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1136 else
1137 return HWC2::Error::BadConfig;
1138
1139 return HWC2::Error::None;
1140}
1141
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001142HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001143 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001144 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1145 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001146 if (type_ == HWC2::DisplayType::Virtual) {
1147 return HWC2::Error::None;
1148 }
1149
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001150 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1151 return HWC2::Error::BadParameter;
1152 }
1153
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001154 uint32_t current_vsync_period{};
1155 GetDisplayVsyncPeriod(&current_vsync_period);
1156
1157 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1158 return HWC2::Error::SeamlessNotAllowed;
1159 }
1160
1161 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1162 ->desiredTimeNanos -
1163 current_vsync_period;
1164 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1165 if (ret != HWC2::Error::None) {
1166 return ret;
1167 }
1168
1169 outTimeline->refreshRequired = true;
1170 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1171 ->desiredTimeNanos;
1172
Drew Davenport33121b72024-12-13 14:59:35 -07001173 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001174
1175 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001176}
1177
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001178HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001179 /* Maps exactly to the content_type DRM connector property:
1180 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001181 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001182 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1183 return HWC2::Error::BadParameter;
1184
1185 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001186
1187 return HWC2::Error::None;
1188}
1189#endif
1190
Roman Stratiienko6b405052022-12-10 19:09:10 +02001191#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001192HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1193 uint32_t *outDataSize,
1194 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001195 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001196 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001197 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001198
Gil Dekel907a51a2025-02-14 22:44:01 -05001199 auto *connector = GetPipe().connector->Get();
1200 auto blob = connector->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001201 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001202 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001203 }
1204
Gil Dekel907a51a2025-02-14 22:44:01 -05001205 constexpr uint8_t kDrmDeviceBitShift = 5U;
1206 constexpr uint8_t kDrmDeviceBitMask = 0xE0;
1207 constexpr uint8_t kConnectorBitMask = 0x1F;
1208 const auto kDrmIdx = static_cast<uint8_t>(
1209 connector->GetDev().GetIndexInDevArray());
1210 const auto kConnectorIdx = static_cast<uint8_t>(
1211 connector->GetIndexInResArray());
1212 *outPort = (((kDrmIdx << kDrmDeviceBitShift) & kDrmDeviceBitMask) |
1213 (kConnectorIdx & kConnectorBitMask));
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001214
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001215 if (outData) {
1216 *outDataSize = std::min(*outDataSize, blob->length);
1217 memcpy(outData, blob->data, *outDataSize);
1218 } else {
1219 *outDataSize = blob->length;
1220 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001221
1222 return HWC2::Error::None;
1223}
1224
1225HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001226 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001227 if (outNumCapabilities == nullptr) {
1228 return HWC2::Error::BadParameter;
1229 }
1230
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001231 bool skip_ctm = false;
1232
1233 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001234 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001235 skip_ctm = true;
1236
1237 // Skip client CTM if DRM can handle it
1238 if (!skip_ctm && !IsInHeadlessMode() &&
1239 GetPipe().crtc->Get()->GetCtmProperty())
1240 skip_ctm = true;
1241
1242 if (!skip_ctm) {
1243 *outNumCapabilities = 0;
1244 return HWC2::Error::None;
1245 }
1246
1247 *outNumCapabilities = 1;
1248 if (outCapabilities) {
1249 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1250 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001251
1252 return HWC2::Error::None;
1253}
1254
Roman Stratiienko6b405052022-12-10 19:09:10 +02001255#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001256
Roman Stratiienko6b405052022-12-10 19:09:10 +02001257#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001258
1259HWC2::Error HwcDisplay::GetRenderIntents(
1260 int32_t mode, uint32_t *outNumIntents,
1261 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1262 if (mode != HAL_COLOR_MODE_NATIVE) {
1263 return HWC2::Error::BadParameter;
1264 }
1265
1266 if (outIntents == nullptr) {
1267 *outNumIntents = 1;
1268 return HWC2::Error::None;
1269 }
1270 *outNumIntents = 1;
1271 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1272 return HWC2::Error::None;
1273}
1274
1275HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1276 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1277 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1278 return HWC2::Error::BadParameter;
1279
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001280 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1281 return HWC2::Error::Unsupported;
1282
Sasha McIntosh5294f092024-09-18 18:14:54 -04001283 auto err = SetColorMode(mode);
1284 if (err != HWC2::Error::None) return err;
1285
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001286 return HWC2::Error::None;
1287}
1288
Roman Stratiienko6b405052022-12-10 19:09:10 +02001289#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001290
1291const Backend *HwcDisplay::backend() const {
1292 return backend_.get();
1293}
1294
1295void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1296 backend_ = std::move(backend);
1297}
1298
Roman Stratiienko45cdacc2025-02-12 04:26:10 +02001299bool HwcDisplay::NeedsClientLayerUpdate() const {
1300 return std::any_of(layers_.begin(), layers_.end(), [](const auto &pair) {
1301 const auto &layer = pair.second;
1302 return layer.GetSfType() == HWC2::Composition::Client ||
1303 layer.GetValidatedType() == HWC2::Composition::Client;
1304 });
1305}
1306
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001307} // namespace android