blob: 892b969dda90d9b4bd639d4c4f3035861d962e93 [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
Drew Davenport76c17a82025-01-15 15:02:45 -070024#include <xf86drmMode.h>
25
Drew Davenport97b5abc2024-11-07 10:43:54 -070026#include <hardware/gralloc.h>
Sasha McIntoshf9062b62024-11-12 10:55:06 -050027#include <ui/ColorSpace.h>
Drew Davenport97b5abc2024-11-07 10:43:54 -070028#include <ui/GraphicBufferAllocator.h>
29#include <ui/GraphicBufferMapper.h>
30#include <ui/PixelFormat.h>
31
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020032#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020033#include "backend/BackendManager.h"
34#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060035#include "compositor/DisplayInfo.h"
36#include "drm/DrmConnector.h"
37#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000038#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020039#include "utils/log.h"
40#include "utils/properties.h"
41
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060042using ::android::DrmDisplayPipeline;
Sasha McIntoshf9062b62024-11-12 10:55:06 -050043using ColorGamut = ::android::ColorSpace;
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060044
Roman Stratiienko3627beb2022-01-04 16:02:55 +020045namespace android {
46
Drew Davenport97b5abc2024-11-07 10:43:54 -070047namespace {
Drew Davenport76c17a82025-01-15 15:02:45 -070048
49constexpr int kCtmRows = 3;
50constexpr int kCtmCols = 3;
51
52constexpr std::array<float, 16> kIdentityMatrix = {
53 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F,
54 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F,
55};
56
57uint64_t To3132FixPt(float in) {
58 constexpr uint64_t kSignMask = (1ULL << 63);
59 constexpr uint64_t kValueMask = ~(1ULL << 63);
60 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
61 if (in < 0)
62 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
63 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
64}
65
66auto ToColorTransform(const std::array<float, 16> &color_transform_matrix) {
67 /* HAL provides a 4x4 float type matrix:
68 * | 0 1 2 3|
69 * | 4 5 6 7|
70 * | 8 9 10 11|
71 * |12 13 14 15|
72 *
73 * R_out = R*0 + G*4 + B*8 + 12
74 * G_out = R*1 + G*5 + B*9 + 13
75 * B_out = R*2 + G*6 + B*10 + 14
76 *
77 * DRM expects a 3x3 s31.32 fixed point matrix:
78 * out matrix in
79 * |R| |0 1 2| |R|
80 * |G| = |3 4 5| x |G|
81 * |B| |6 7 8| |B|
82 *
83 * R_out = R*0 + G*1 + B*2
84 * G_out = R*3 + G*4 + B*5
85 * B_out = R*6 + G*7 + B*8
86 */
87 auto color_matrix = std::make_shared<drm_color_ctm>();
88 for (int i = 0; i < kCtmCols; i++) {
89 for (int j = 0; j < kCtmRows; j++) {
90 constexpr int kInCtmRows = 4;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +020091 color_matrix->matrix[(i * kCtmRows) + j] = To3132FixPt(
92 color_transform_matrix[(j * kInCtmRows) + i]);
Drew Davenport76c17a82025-01-15 15:02:45 -070093 }
94 }
95 return color_matrix;
96}
97
Drew Davenport97b5abc2024-11-07 10:43:54 -070098// Allocate a black buffer that can be used for an initial modeset when there.
99// is no appropriate client buffer available to be used.
100// Caller must free the returned buffer with GraphicBufferAllocator::free.
101auto GetModesetBuffer(uint32_t width, uint32_t height) -> buffer_handle_t {
102 constexpr PixelFormat format = PIXEL_FORMAT_RGBA_8888;
103 constexpr uint64_t usage = GRALLOC_USAGE_SW_READ_OFTEN |
104 GRALLOC_USAGE_SW_WRITE_OFTEN |
105 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
106
107 constexpr uint32_t layer_count = 1;
108 const std::string name = "drm-hwcomposer";
109
110 buffer_handle_t handle = nullptr;
111 uint32_t stride = 0;
112 status_t status = GraphicBufferAllocator::get().allocate(width, height,
113 format, layer_count,
114 usage, &handle,
115 &stride, name);
116 if (status != OK) {
117 ALOGE("Failed to allocate modeset buffer.");
118 return nullptr;
119 }
120
121 void *data = nullptr;
122 Rect bounds = {0, 0, static_cast<int32_t>(width),
123 static_cast<int32_t>(height)};
124 status = GraphicBufferMapper::get().lock(handle, usage, bounds, &data);
125 if (status != OK) {
126 ALOGE("Failed to map modeset buffer.");
127 GraphicBufferAllocator::get().free(handle);
128 return nullptr;
129 }
130
131 // Cast one of the multiplicands to ensure that the multiplication happens
132 // in a wider type (size_t).
133 const size_t buffer_size = static_cast<size_t>(height) * stride *
134 bytesPerPixel(format);
135 memset(data, 0, buffer_size);
136 status = GraphicBufferMapper::get().unlock(handle);
137 ALOGW_IF(status != OK, "Failed to unmap buffer.");
138 return handle;
139}
140
Drew Davenport97b5abc2024-11-07 10:43:54 -0700141} // namespace
142
Roman Stratiienko44b95772025-01-22 18:03:36 +0200143static BufferColorSpace Hwc2ToColorSpace(int32_t dataspace) {
144 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
145 case HAL_DATASPACE_STANDARD_BT709:
146 return BufferColorSpace::kItuRec709;
147 case HAL_DATASPACE_STANDARD_BT601_625:
148 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
149 case HAL_DATASPACE_STANDARD_BT601_525:
150 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
151 return BufferColorSpace::kItuRec601;
152 case HAL_DATASPACE_STANDARD_BT2020:
153 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
154 return BufferColorSpace::kItuRec2020;
155 default:
156 return BufferColorSpace::kUndefined;
157 }
158}
159
160static BufferSampleRange Hwc2ToSampleRange(int32_t dataspace) {
161 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
162 case HAL_DATASPACE_RANGE_FULL:
163 return BufferSampleRange::kFullRange;
164 case HAL_DATASPACE_RANGE_LIMITED:
165 return BufferSampleRange::kLimitedRange;
166 default:
167 return BufferSampleRange::kUndefined;
168 }
169}
170
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
172 if (delta.total_pixops_ == 0)
173 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200174 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200175
176 std::stringstream ss;
177 ss << " Total frames count: " << delta.total_frames_ << "\n"
178 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
179 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
180 << ((delta.failed_kms_present_ > 0)
181 ? " !!! Internal failure, FIX it please\n"
182 : "")
183 << " Flattened frames: " << delta.frames_flattened_ << "\n"
184 << " Pixel operations (free units)"
185 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
186 << "]\n"
187 << " Composition efficiency: " << ratio;
188
189 return ss.str();
190}
191
192std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300193 auto connector_name = IsInHeadlessMode()
194 ? std::string("NULL-DISPLAY")
195 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200196
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200198 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199 << "Statistics since system boot:\n"
200 << DumpDelta(total_stats_) << "\n\n"
201 << "Statistics since last dumpsys request:\n"
202 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
203
204 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
205 return ss.str();
206}
207
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200208HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000209 DrmHwc *hwc)
210 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300211 if (type_ == HWC2::DisplayType::Virtual) {
212 writeback_layer_ = std::make_unique<HwcLayer>(this);
213 }
214}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200215
Drew Davenport76c17a82025-01-15 15:02:45 -0700216void HwcDisplay::SetColorTransformMatrix(
217 const std::array<float, 16> &color_transform_matrix) {
218 auto almost_equal = [](auto a, auto b) {
219 const float epsilon = 0.001F;
220 return std::abs(a - b) < epsilon;
221 };
222 const bool is_identity = std::equal(color_transform_matrix.begin(),
223 color_transform_matrix.end(),
224 kIdentityMatrix.begin(), almost_equal);
225 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
226 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
227 if (color_transform_hint_ == is_identity) {
228 SetColorMatrixToIdentity();
229 } else {
230 color_matrix_ = ToColorTransform(color_transform_matrix);
231 }
232}
233
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400234void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200235 color_matrix_ = std::make_shared<drm_color_ctm>();
236 for (int i = 0; i < kCtmCols; i++) {
237 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800238 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200239 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200240 }
241 }
242
243 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200244}
245
Normunds Rieksts545096d2024-03-11 16:37:45 +0000246HwcDisplay::~HwcDisplay() {
247 Deinit();
248};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200249
Drew Davenportfe70c802024-11-07 13:02:21 -0700250auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
251 -> const HwcDisplayConfig * {
252 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600253 if (config_iter == configs_.hwc_configs.end()) {
254 return nullptr;
255 }
256 return &config_iter->second;
257}
258
Drew Davenportfe70c802024-11-07 13:02:21 -0700259auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
260 return GetConfig(configs_.active_config_id);
261}
262
Drew Davenport8998f8b2024-10-24 10:15:12 -0600263auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700264 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600265}
266
Drew Davenport97b5abc2024-11-07 10:43:54 -0700267HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
268 const HwcDisplayConfig *new_config = GetConfig(config);
269 if (new_config == nullptr) {
270 ALOGE("Could not find active mode for %u", config);
271 return ConfigError::kBadConfig;
272 }
273
274 const HwcDisplayConfig *current_config = GetCurrentConfig();
275
276 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700277 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700278
279 std::optional<LayerData> modeset_layer_data;
280 // If a client layer has already been provided, and its size matches the
281 // new config, use it for the modeset.
282 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
283 current_config->mode.GetRawMode().hdisplay == width &&
284 current_config->mode.GetRawMode().vdisplay == height) {
285 ALOGV("Use existing client_layer for blocking config.");
286 modeset_layer_data = client_layer_.GetLayerData();
287 } else {
288 ALOGV("Allocate modeset buffer.");
289 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
290 if (modeset_buffer != nullptr) {
291 auto modeset_layer = std::make_unique<HwcLayer>(this);
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200292 HwcLayer::LayerProperties properties;
293 properties.buffer = {.buffer_handle = modeset_buffer};
294 properties.blend_mode = BufferBlendMode::kNone;
295 modeset_layer->SetLayerProperties(properties);
Drew Davenport97b5abc2024-11-07 10:43:54 -0700296 modeset_layer->PopulateLayerData();
297 modeset_layer_data = modeset_layer->GetLayerData();
298 GraphicBufferAllocator::get().free(modeset_buffer);
299 }
300 }
301
302 ALOGV("Create modeset commit.");
303 // Create atomic commit args for a blocking modeset. There's no need to do a
304 // separate test commit, since the commit does a test anyways.
305 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
306 modeset_layer_data);
307 commit_args.blocking = true;
308 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
309
310 if (ret) {
311 ALOGE("Blocking config failed: %d", ret);
312 return HwcDisplay::ConfigError::kBadConfig;
313 }
314
315 ALOGV("Blocking config succeeded.");
316 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700317 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700318 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
319 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700320 return ConfigError::kNone;
321}
322
Drew Davenport8998f8b2024-10-24 10:15:12 -0600323auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
324 bool seamless, QueuedConfigTiming *out_timing)
325 -> ConfigError {
326 if (configs_.hwc_configs.count(config) == 0) {
327 ALOGE("Could not find active mode for %u", config);
328 return ConfigError::kBadConfig;
329 }
330
331 // TODO: Add support for seamless configuration changes.
332 if (seamless) {
333 return ConfigError::kSeamlessNotAllowed;
334 }
335
336 // Request a refresh from the client one vsync period before the desired
337 // time, or simply at the desired time if there is no active configuration.
338 const HwcDisplayConfig *current_config = GetCurrentConfig();
339 out_timing->refresh_time_ns = desired_time -
340 (current_config
341 ? current_config->mode.GetVSyncPeriodNs()
342 : 0);
343 out_timing->new_vsync_time_ns = desired_time;
344
345 // Queue the config change timing to be consistent with the requested
346 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600347 staged_mode_change_time_ = out_timing->refresh_time_ns;
348 staged_mode_config_id_ = config;
349
350 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700351 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600352
353 return ConfigError::kNone;
354}
355
Drew Davenport7f356c82025-01-17 16:32:06 -0700356auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
357 if (IsInHeadlessMode()) {
358 return {};
359 }
360
361 /* In current drm_hwc design in case previous frame layer was not validated as
362 * a CLIENT, it is used by display controller (Front buffer). We have to store
363 * this state to provide the CLIENT with the release fences for such buffers.
364 */
365 for (auto &l : layers_) {
366 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
367 HWC2::Composition::Client);
368 }
369
370 // ValidateDisplay returns the number of layers that may be changed.
371 uint32_t num_types = 0;
372 uint32_t num_requests = 0;
373 backend_->ValidateDisplay(this, &num_types, &num_requests);
374
375 if (num_types == 0) {
376 return {};
377 }
378
379 // Iterate through the layers to find which layers actually changed.
380 std::vector<ChangedLayer> changed_layers;
381 for (auto &l : layers_) {
382 if (l.second.IsTypeChanged()) {
383 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
384 }
385 }
386 return changed_layers;
387}
388
Drew Davenportb864ccf2025-01-22 14:57:36 -0700389auto HwcDisplay::AcceptValidatedComposition() -> void {
390 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
391 l.second.AcceptTypeChange();
392 }
393}
394
395auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200396 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
397 -> bool {
398 int out_fd = -1;
399 auto error = PresentDisplay(&out_fd);
400 out_present_fence = MakeSharedFd(out_fd);
401 if (error != HWC2::Error::None) {
402 return false;
403 }
404
405 if (!out_present_fence) {
406 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700407 }
408
409 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200410 if (l.second.GetPriorBufferScanOutFlag()) {
411 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700412 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700413 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200414
415 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700416}
417
Roman Stratiienko63762a92023-09-18 22:33:45 +0300418void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200419 Deinit();
420
Roman Stratiienko63762a92023-09-18 22:33:45 +0300421 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200422
Roman Stratiienko63762a92023-09-18 22:33:45 +0300423 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200424 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000425 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200426 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000427 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200428 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200429}
430
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200431void HwcDisplay::Deinit() {
432 if (pipeline_ != nullptr) {
433 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200434 a_args.composition = std::make_shared<DrmKmsPlan>();
435 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300436 a_args.composition = {};
437 a_args.active = false;
438 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200439
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200440 current_plan_.reset();
441 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200442 if (flatcon_) {
443 flatcon_->StopThread();
444 flatcon_.reset();
445 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200446 }
447
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200448 if (vsync_worker_) {
449 vsync_worker_->StopThread();
450 vsync_worker_ = {};
451 }
452
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200453 SetClientTarget(nullptr, -1, 0, {});
454}
455
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200456HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200457 ChosePreferredConfig();
458
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300459 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700460 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300461 if (!vsync_worker_) {
462 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
463 return HWC2::Error::BadDisplay;
464 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200465 }
466
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200467 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200468 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200469 if (ret) {
470 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
471 return HWC2::Error::BadDisplay;
472 }
Drew Davenport93443182023-12-14 09:25:45 +0000473 auto flatcbk = (struct FlatConCallbacks){
474 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200475 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200476 }
477
Roman Stratiienko44b95772025-01-22 18:03:36 +0200478 HwcLayer::LayerProperties lp;
479 lp.blend_mode = BufferBlendMode::kPreMult;
480 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200481
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400482 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200483
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200484 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200485}
486
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600487std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
488 if (IsInHeadlessMode()) {
489 // The pipeline can be nullptr in headless mode, so return the default
490 // "normal" mode.
491 return PanelOrientation::kModePanelOrientationNormal;
492 }
493
494 DrmDisplayPipeline &pipeline = GetPipe();
495 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
496 ALOGW(
497 "No display pipeline present to query the panel orientation property.");
498 return {};
499 }
500
501 return pipeline.connector->Get()->GetPanelOrientation();
502}
503
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200505 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300506 if (type_ == HWC2::DisplayType::Virtual) {
507 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
508 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200509 err = configs_.Update(*pipeline_->connector->Get());
510 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300511 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200512 }
513 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200515 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200516
Roman Stratiienko0137f862022-01-04 18:27:40 +0200517 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518}
519
520HWC2::Error HwcDisplay::AcceptDisplayChanges() {
521 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
522 l.second.AcceptTypeChange();
523 return HWC2::Error::None;
524}
525
526HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200527 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528 *layer = static_cast<hwc2_layer_t>(layer_idx_);
529 ++layer_idx_;
530 return HWC2::Error::None;
531}
532
533HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200534 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200536 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537
538 layers_.erase(layer);
539 return HWC2::Error::None;
540}
541
542HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700543 // If a config has been queued, it is considered the "active" config.
544 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
545 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546 return HWC2::Error::BadConfig;
547
Drew Davenportfe70c802024-11-07 13:02:21 -0700548 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200549 return HWC2::Error::None;
550}
551
552HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
553 hwc2_layer_t *layers,
554 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200555 if (IsInHeadlessMode()) {
556 *num_elements = 0;
557 return HWC2::Error::None;
558 }
559
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200560 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300561 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200562 if (l.second.IsTypeChanged()) {
563 if (layers && num_changes < *num_elements)
564 layers[num_changes] = l.first;
565 if (types && num_changes < *num_elements)
566 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
567 ++num_changes;
568 }
569 }
570 if (!layers && !types)
571 *num_elements = num_changes;
572 return HWC2::Error::None;
573}
574
575HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
576 int32_t /*format*/,
577 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200578 if (IsInHeadlessMode()) {
579 return HWC2::Error::None;
580 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200581
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300582 auto min = pipeline_->device->GetMinResolution();
583 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200584
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200585 if (width < min.first || height < min.second)
586 return HWC2::Error::Unsupported;
587
588 if (width > max.first || height > max.second)
589 return HWC2::Error::Unsupported;
590
591 if (dataspace != HAL_DATASPACE_UNKNOWN)
592 return HWC2::Error::Unsupported;
593
594 // TODO(nobody): Validate format can be handled by either GL or planes
595 return HWC2::Error::None;
596}
597
598HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500599 if (!modes) {
600 std::vector<Colormode> temp_modes;
601 GetEdid()->GetColorModes(temp_modes);
602 *num_modes = temp_modes.size();
603 return HWC2::Error::None;
604 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200605
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500606 std::vector<Colormode> temp_modes;
607 std::vector<int32_t> out_modes(modes, modes + *num_modes);
608 GetEdid()->GetColorModes(temp_modes);
609 if (temp_modes.empty()) {
610 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
611 return HWC2::Error::None;
612 }
613
614 for (auto &c : temp_modes)
615 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200616
617 return HWC2::Error::None;
618}
619
620HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
621 int32_t attribute_in,
622 int32_t *value) {
623 int conf = static_cast<int>(config);
624
Roman Stratiienko0137f862022-01-04 18:27:40 +0200625 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200626 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 return HWC2::Error::BadConfig;
628 }
629
Roman Stratiienko0137f862022-01-04 18:27:40 +0200630 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200631
632 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300633 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200634 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
635 switch (attribute) {
636 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200637 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200638 break;
639 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200640 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641 break;
642 case HWC2::Attribute::VsyncPeriod:
643 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600644 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000646 case HWC2::Attribute::DpiY:
647 // ideally this should be vdisplay/mm_heigth, however mm_height
648 // comes from edid parsing and is highly unreliable. Viewing the
649 // rarity of anisotropic displays, falling back to a single value
650 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200651 case HWC2::Attribute::DpiX:
652 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200653 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
654 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655 : -1;
656 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200657#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200658 case HWC2::Attribute::ConfigGroup:
659 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
660 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200661 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200662 break;
663#endif
664 default:
665 *value = -1;
666 return HWC2::Error::BadConfig;
667 }
668 return HWC2::Error::None;
669}
670
Drew Davenportf7e88332024-09-06 12:54:38 -0600671HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
672 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200673 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200674 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200675 if (hwc_config.second.disabled) {
676 continue;
677 }
678
679 if (configs != nullptr) {
680 if (idx >= *num_configs) {
681 break;
682 }
683 configs[idx] = hwc_config.second.id;
684 }
685
686 idx++;
687 }
688 *num_configs = idx;
689 return HWC2::Error::None;
690}
691
692HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
693 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200694 if (IsInHeadlessMode()) {
695 stream << "null-display";
696 } else {
697 stream << "display-" << GetPipe().connector->Get()->GetId();
698 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300699 auto string = stream.str();
700 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200701 if (!name) {
702 *size = length;
703 return HWC2::Error::None;
704 }
705
706 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
707 strncpy(name, string.c_str(), *size);
708 return HWC2::Error::None;
709}
710
711HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
712 uint32_t *num_elements,
713 hwc2_layer_t * /*layers*/,
714 int32_t * /*layer_requests*/) {
715 // TODO(nobody): I think virtual display should request
716 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
717 *num_elements = 0;
718 return HWC2::Error::None;
719}
720
721HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
722 *type = static_cast<int32_t>(type_);
723 return HWC2::Error::None;
724}
725
726HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
727 *support = 0;
728 return HWC2::Error::None;
729}
730
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500731HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
732 float *max_luminance,
733 float *max_average_luminance,
734 float *min_luminance) {
735 if (!types) {
736 std::vector<ui::Hdr> temp_types;
737 float lums[3] = {0.F};
738 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
739 *num_types = temp_types.size();
740 return HWC2::Error::None;
741 }
742
743 std::vector<ui::Hdr> temp_types;
744 std::vector<int32_t> out_types(types, types + *num_types);
745 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
746 max_average_luminance, min_luminance);
747 for (auto &t : temp_types) {
748 switch (t) {
749 case ui::Hdr::HDR10:
750 out_types.emplace_back(HAL_HDR_HDR10);
751 break;
752 case ui::Hdr::HLG:
753 out_types.emplace_back(HAL_HDR_HLG);
754 break;
755 default:
756 // Ignore any other HDR types
757 break;
758 }
759 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200760 return HWC2::Error::None;
761}
762
763/* Find API details at:
764 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
Roman Stratiienkodd214942022-05-03 18:24:49 +0300765 *
766 * Called after PresentDisplay(), CLIENT is expecting release fence for the
767 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200768 */
769HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
770 hwc2_layer_t *layers,
771 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200772 if (IsInHeadlessMode()) {
773 *num_elements = 0;
774 return HWC2::Error::None;
775 }
776
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200777 uint32_t num_layers = 0;
778
Roman Stratiienkodd214942022-05-03 18:24:49 +0300779 for (auto &l : layers_) {
780 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
781 continue;
782 }
783
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200784 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300785
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200786 if (layers == nullptr || fences == nullptr)
787 continue;
788
789 if (num_layers > *num_elements) {
790 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
791 return HWC2::Error::None;
792 }
793
794 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200795 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200796 }
797 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300798
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200799 return HWC2::Error::None;
800}
801
Drew Davenport97b5abc2024-11-07 10:43:54 -0700802AtomicCommitArgs HwcDisplay::CreateModesetCommit(
803 const HwcDisplayConfig *config,
804 const std::optional<LayerData> &modeset_layer) {
805 AtomicCommitArgs args{};
806
807 args.color_matrix = color_matrix_;
808 args.content_type = content_type_;
809 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500810 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700811
812 std::vector<LayerData> composition_layers;
813 if (modeset_layer) {
814 composition_layers.emplace_back(modeset_layer.value());
815 }
816
817 if (composition_layers.empty()) {
818 ALOGW("Attempting to create a modeset commit without a layer.");
819 }
820
821 args.display_mode = config->mode;
822 args.active = true;
823 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
824 std::move(
825 composition_layers));
826 ALOGW_IF(!args.composition, "No composition for blocking modeset");
827
828 return args;
829}
830
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200831// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200832HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200833 if (IsInHeadlessMode()) {
834 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
835 return HWC2::Error::None;
836 }
837
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200838 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400839 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400840 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500841 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200842
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200843 uint32_t prev_vperiod_ns = 0;
844 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200845
Drew Davenportd387c842024-12-16 16:57:24 -0700846 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700847 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200848 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700849 const HwcDisplayConfig *staged_config = GetConfig(
850 staged_mode_config_id_.value());
851 if (staged_config == nullptr) {
852 return HWC2::Error::BadConfig;
853 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200854
Drew Davenportfe70c802024-11-07 13:02:21 -0700855 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700856 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200857 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700858 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200859 }
860 }
861
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200862 // order the layers by z-order
863 bool use_client_layer = false;
864 uint32_t client_z_order = UINT32_MAX;
865 std::map<uint32_t, HwcLayer *> z_map;
866 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
867 switch (l.second.GetValidatedType()) {
868 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300869 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200870 break;
871 case HWC2::Composition::Client:
872 // Place it at the z_order of the lowest client layer
873 use_client_layer = true;
874 client_z_order = std::min(client_z_order, l.second.GetZOrder());
875 break;
876 default:
877 continue;
878 }
879 }
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200880 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300881 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200883 client_layer_.PopulateLayerData();
884 if (!client_layer_.IsLayerUsableAsDevice()) {
885 ALOGE_IF(!a_args.test_only,
886 "Client layer must be always usable by DRM/KMS");
887 /* This may be normally triggered on validation of the first frame
888 * containing CLIENT layer. At this moment client buffer is not yet
889 * provided by the CLIENT.
890 * This may be triggered once in HwcLayer lifecycle in case FB can't be
891 * imported. For example when non-contiguous buffer is imported into
892 * contiguous-only DRM/KMS driver.
893 */
894 return HWC2::Error::BadLayer;
895 }
896 }
897
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200898 if (z_map.empty())
899 return HWC2::Error::BadLayer;
900
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200901 std::vector<LayerData> composition_layers;
902
903 /* Import & populate */
904 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200905 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200906 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907
908 // now that they're ordered by z, add them to the composition
909 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200910 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200911 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200912 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200913 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200914 }
915
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200916 /* Store plan to ensure shared planes won't be stolen by other display
917 * in between of ValidateDisplay() and PresentDisplay() calls
918 */
919 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
920 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300921
922 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200923 writeback_layer_->PopulateLayerData();
924 if (!writeback_layer_->IsLayerUsableAsDevice()) {
925 ALOGE("Output layer must be always usable by DRM/KMS");
926 return HWC2::Error::BadLayer;
927 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300928 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
929 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
930 .acquire_fence;
931 }
932
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200933 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700934 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200935 return HWC2::Error::BadConfig;
936 }
937
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200938 a_args.composition = current_plan_;
939
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300940 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200941
942 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700943 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200944 return HWC2::Error::BadParameter;
945 }
946
Drew Davenportd387c842024-12-16 16:57:24 -0700947 if (new_vsync_period_ns) {
948 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700949 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700950
951 vsync_worker_->SetVsyncTimestampTracking(false);
952 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
953 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000954 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700955 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000956 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200957 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200958 }
959
960 return HWC2::Error::None;
961}
962
963/* Find API details at:
964 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
965 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300966HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200967 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300968 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200969 return HWC2::Error::None;
970 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200971 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200972
973 ++total_stats_.total_frames_;
974
975 AtomicCommitArgs a_args{};
976 ret = CreateComposition(a_args);
977
978 if (ret != HWC2::Error::None)
979 ++total_stats_.failed_kms_present_;
980
981 if (ret == HWC2::Error::BadLayer) {
982 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300983 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200984 return HWC2::Error::None;
985 }
986 if (ret != HWC2::Error::None)
987 return ret;
988
Roman Stratiienko76892782023-01-16 17:15:53 +0200989 this->present_fence_ = a_args.out_fence;
990 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200991
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200992 // Reset the color matrix so we don't apply it over and over again.
993 color_matrix_ = {};
994
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700996
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200997 return HWC2::Error::None;
998}
999
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001000HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
1001 int64_t change_time) {
1002 if (configs_.hwc_configs.count(config) == 0) {
1003 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001004 return HWC2::Error::BadConfig;
1005 }
1006
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001007 staged_mode_change_time_ = change_time;
1008 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001009
1010 return HWC2::Error::None;
1011}
1012
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001013HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
1014 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
1015}
1016
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001017/* Find API details at:
1018 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
1019 */
1020HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
1021 int32_t acquire_fence,
1022 int32_t dataspace,
1023 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001024 HwcLayer::LayerProperties lp;
1025 lp.buffer = {.buffer_handle = target,
1026 .acquire_fence = MakeSharedFd(acquire_fence)};
1027 lp.color_space = Hwc2ToColorSpace(dataspace);
1028 lp.sample_range = Hwc2ToSampleRange(dataspace);
1029 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001030
1031 /*
1032 * target can be nullptr, this does mean the Composer Service is calling
1033 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
1034 * https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerClient.h;l=350;drc=944b68180b008456ed2eb4d4d329e33b19bd5166
1035 */
1036 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +03001037 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001038 return HWC2::Error::None;
1039 }
1040
Roman Stratiienko5070d512022-05-30 13:41:20 +03001041 if (IsInHeadlessMode()) {
1042 return HWC2::Error::None;
1043 }
1044
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001045 return HWC2::Error::None;
1046}
1047
1048HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001049 /* Maps to the Colorspace DRM connector property:
1050 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1051 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001052 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001053 return HWC2::Error::BadParameter;
1054
Sasha McIntosh5294f092024-09-18 18:14:54 -04001055 switch (mode) {
1056 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001057 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001058 colorspace_ = Colorspace::kDefault;
1059 break;
1060 case HAL_COLOR_MODE_STANDARD_BT601_625:
1061 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1062 case HAL_COLOR_MODE_STANDARD_BT601_525:
1063 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001064 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001065 // The DP spec does not say whether this is the 525 or the 625 line version.
1066 colorspace_ = Colorspace::kBt601Ycc;
1067 break;
1068 case HAL_COLOR_MODE_STANDARD_BT709:
1069 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001070 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001071 colorspace_ = Colorspace::kBt709Ycc;
1072 break;
1073 case HAL_COLOR_MODE_DCI_P3:
1074 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001075 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001076 colorspace_ = Colorspace::kDciP3RgbD65;
1077 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001078 case HAL_COLOR_MODE_DISPLAY_BT2020: {
1079 std::vector<ui::Hdr> hdr_types;
1080 GetEdid()->GetSupportedHdrTypes(hdr_types);
1081 if (!hdr_types.empty()) {
1082 auto ret = SetHdrOutputMetadata(hdr_types.front());
1083 if (ret != HWC2::Error::None)
1084 return ret;
1085 }
1086 colorspace_ = Colorspace::kBt2020Rgb;
1087 break;
1088 }
Sasha McIntosh5294f092024-09-18 18:14:54 -04001089 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001090 case HAL_COLOR_MODE_BT2020:
1091 case HAL_COLOR_MODE_BT2100_PQ:
1092 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001093 default:
1094 return HWC2::Error::Unsupported;
1095 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001096
1097 color_mode_ = mode;
1098 return HWC2::Error::None;
1099}
1100
1101HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1102 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1103 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1104 return HWC2::Error::BadParameter;
1105
1106 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1107 return HWC2::Error::BadParameter;
1108
1109 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001110
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001111 if (IsInHeadlessMode())
1112 return HWC2::Error::None;
1113
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001114 if (!GetPipe().crtc->Get()->GetCtmProperty())
1115 return HWC2::Error::None;
1116
1117 switch (color_transform_hint_) {
1118 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001119 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001120 break;
1121 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001122 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001123 {
1124 for (int i = 12; i < 14; i++) {
1125 if (matrix[i] != 0.F)
1126 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001127 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001128 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1129 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1130 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001131 }
1132 break;
1133 default:
1134 return HWC2::Error::Unsupported;
1135 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001136
1137 return HWC2::Error::None;
1138}
1139
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001140bool HwcDisplay::CtmByGpu() {
1141 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1142 return false;
1143
1144 if (GetPipe().crtc->Get()->GetCtmProperty())
1145 return false;
1146
Drew Davenport93443182023-12-14 09:25:45 +00001147 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001148 return false;
1149
1150 return true;
1151}
1152
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001153HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1154 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001155 HwcLayer::LayerProperties lp;
1156 lp.buffer = {.buffer_handle = buffer,
1157 .acquire_fence = MakeSharedFd(release_fence)};
1158 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001159 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001160}
1161
1162HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1163 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001164
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001165 AtomicCommitArgs a_args{};
1166
1167 switch (mode) {
1168 case HWC2::PowerMode::Off:
1169 a_args.active = false;
1170 break;
1171 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001172 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001173 break;
1174 case HWC2::PowerMode::Doze:
1175 case HWC2::PowerMode::DozeSuspend:
1176 return HWC2::Error::Unsupported;
1177 default:
John Stultzffe783c2024-02-14 10:51:27 -08001178 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001179 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001180 }
1181
1182 if (IsInHeadlessMode()) {
1183 return HWC2::Error::None;
1184 }
1185
Jia Ren80566fe2022-11-17 17:26:00 +08001186 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001187 /*
1188 * Setting the display to active before we have a composition
1189 * can break some drivers, so skip setting a_args.active to
1190 * true, as the next composition frame will implicitly activate
1191 * the display
1192 */
1193 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1194 ? HWC2::Error::None
1195 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001196 };
1197
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001198 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001199 if (err) {
1200 ALOGE("Failed to apply the dpms composition err=%d", err);
1201 return HWC2::Error::BadParameter;
1202 }
1203 return HWC2::Error::None;
1204}
1205
1206HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001207 if (type_ == HWC2::DisplayType::Virtual) {
1208 return HWC2::Error::None;
1209 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001210 if (!vsync_worker_) {
1211 return HWC2::Error::NoResources;
1212 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001213
Roman Stratiienko099c3112022-01-20 11:50:54 +02001214 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001215 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001216 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001217 DrmHwc *hwc = hwc_;
1218 hwc2_display_t id = handle_;
1219 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001220 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001221 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1222 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001223 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001224 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001225 return HWC2::Error::None;
1226}
1227
1228HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1229 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001230 if (IsInHeadlessMode()) {
1231 *num_types = *num_requests = 0;
1232 return HWC2::Error::None;
1233 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001234
1235 /* In current drm_hwc design in case previous frame layer was not validated as
1236 * a CLIENT, it is used by display controller (Front buffer). We have to store
1237 * this state to provide the CLIENT with the release fences for such buffers.
1238 */
1239 for (auto &l : layers_) {
1240 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1241 HWC2::Composition::Client);
1242 }
1243
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001244 return backend_->ValidateDisplay(this, num_types, num_requests);
1245}
1246
1247std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1248 std::vector<HwcLayer *> ordered_layers;
1249 ordered_layers.reserve(layers_.size());
1250
1251 for (auto &[handle, layer] : layers_) {
1252 ordered_layers.emplace_back(&layer);
1253 }
1254
1255 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1256 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1257 return lhs->GetZOrder() < rhs->GetZOrder();
1258 });
1259
1260 return ordered_layers;
1261}
1262
Roman Stratiienko099c3112022-01-20 11:50:54 +02001263HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1264 uint32_t *outVsyncPeriod /* ns */) {
1265 return GetDisplayAttribute(configs_.active_config_id,
1266 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1267 (int32_t *)(outVsyncPeriod));
1268}
1269
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001270// Display primary values are coded as unsigned 16-bit values in units of
1271// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1272static uint64_t ToU16ColorValue(float in) {
1273 constexpr float kPrimariesFixedPoint = 50000.F;
1274 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1275}
1276
1277HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1278 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1279 hdr_metadata_->metadata_type = 0;
1280 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1281 m->metadata_type = 0;
1282
1283 switch (type) {
1284 case ui::Hdr::HDR10:
1285 m->eotf = 2; // PQ
1286 break;
1287 case ui::Hdr::HLG:
1288 m->eotf = 3; // HLG
1289 break;
1290 default:
1291 return HWC2::Error::Unsupported;
1292 }
1293
1294 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1295 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1296 std::vector<ui::Hdr> types;
1297 float hdr_luminance[3]{0.F, 0.F, 0.F};
1298 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1299 &hdr_luminance[2]);
1300 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1301 hdr_luminance[0]);
1302 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1303 // The min luminance value is coded as an unsigned 16-bit value in units of
1304 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1305 // represents 6.5535 cd/m2.
1306 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1307 10000.F);
1308
1309 auto gamut = ColorGamut::BT2020();
1310 auto primaries = gamut.getPrimaries();
1311 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1312 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1313 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1314 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1315 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1316 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1317
1318 auto whitePoint = gamut.getWhitePoint();
1319 m->white_point.x = ToU16ColorValue(whitePoint.x);
1320 m->white_point.y = ToU16ColorValue(whitePoint.y);
1321
1322 return HWC2::Error::None;
1323}
1324
Roman Stratiienko6b405052022-12-10 19:09:10 +02001325#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001326HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001327 if (IsInHeadlessMode()) {
1328 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1329 return HWC2::Error::None;
1330 }
1331 /* Primary display should be always internal,
1332 * otherwise SF will be unhappy and will crash
1333 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001334 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001335 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001336 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001337 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1338 else
1339 return HWC2::Error::BadConfig;
1340
1341 return HWC2::Error::None;
1342}
1343
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001344HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001345 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001346 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1347 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001348 if (type_ == HWC2::DisplayType::Virtual) {
1349 return HWC2::Error::None;
1350 }
1351
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001352 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1353 return HWC2::Error::BadParameter;
1354 }
1355
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001356 uint32_t current_vsync_period{};
1357 GetDisplayVsyncPeriod(&current_vsync_period);
1358
1359 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1360 return HWC2::Error::SeamlessNotAllowed;
1361 }
1362
1363 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1364 ->desiredTimeNanos -
1365 current_vsync_period;
1366 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1367 if (ret != HWC2::Error::None) {
1368 return ret;
1369 }
1370
1371 outTimeline->refreshRequired = true;
1372 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1373 ->desiredTimeNanos;
1374
Drew Davenport33121b72024-12-13 14:59:35 -07001375 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001376
1377 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001378}
1379
1380HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1381 return HWC2::Error::Unsupported;
1382}
1383
1384HWC2::Error HwcDisplay::GetSupportedContentTypes(
1385 uint32_t *outNumSupportedContentTypes,
1386 const uint32_t *outSupportedContentTypes) {
1387 if (outSupportedContentTypes == nullptr)
1388 *outNumSupportedContentTypes = 0;
1389
1390 return HWC2::Error::None;
1391}
1392
1393HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001394 /* Maps exactly to the content_type DRM connector property:
1395 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001396 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001397 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1398 return HWC2::Error::BadParameter;
1399
1400 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001401
1402 return HWC2::Error::None;
1403}
1404#endif
1405
Roman Stratiienko6b405052022-12-10 19:09:10 +02001406#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001407HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1408 uint32_t *outDataSize,
1409 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001410 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001411 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001412 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001413
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001414 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001415 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001416 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001417 }
1418
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001419 *outPort = handle_; /* TDOD(nobody): What should be here? */
1420
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001421 if (outData) {
1422 *outDataSize = std::min(*outDataSize, blob->length);
1423 memcpy(outData, blob->data, *outDataSize);
1424 } else {
1425 *outDataSize = blob->length;
1426 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001427
1428 return HWC2::Error::None;
1429}
1430
1431HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001432 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001433 if (outNumCapabilities == nullptr) {
1434 return HWC2::Error::BadParameter;
1435 }
1436
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001437 bool skip_ctm = false;
1438
1439 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001440 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001441 skip_ctm = true;
1442
1443 // Skip client CTM if DRM can handle it
1444 if (!skip_ctm && !IsInHeadlessMode() &&
1445 GetPipe().crtc->Get()->GetCtmProperty())
1446 skip_ctm = true;
1447
1448 if (!skip_ctm) {
1449 *outNumCapabilities = 0;
1450 return HWC2::Error::None;
1451 }
1452
1453 *outNumCapabilities = 1;
1454 if (outCapabilities) {
1455 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1456 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001457
1458 return HWC2::Error::None;
1459}
1460
1461HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1462 *supported = false;
1463 return HWC2::Error::None;
1464}
1465
1466HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1467 return HWC2::Error::Unsupported;
1468}
1469
Roman Stratiienko6b405052022-12-10 19:09:10 +02001470#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001471
Roman Stratiienko6b405052022-12-10 19:09:10 +02001472#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001473
1474HWC2::Error HwcDisplay::GetRenderIntents(
1475 int32_t mode, uint32_t *outNumIntents,
1476 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1477 if (mode != HAL_COLOR_MODE_NATIVE) {
1478 return HWC2::Error::BadParameter;
1479 }
1480
1481 if (outIntents == nullptr) {
1482 *outNumIntents = 1;
1483 return HWC2::Error::None;
1484 }
1485 *outNumIntents = 1;
1486 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1487 return HWC2::Error::None;
1488}
1489
1490HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1491 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1492 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1493 return HWC2::Error::BadParameter;
1494
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001495 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1496 return HWC2::Error::Unsupported;
1497
Sasha McIntosh5294f092024-09-18 18:14:54 -04001498 auto err = SetColorMode(mode);
1499 if (err != HWC2::Error::None) return err;
1500
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001501 return HWC2::Error::None;
1502}
1503
Roman Stratiienko6b405052022-12-10 19:09:10 +02001504#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001505
1506const Backend *HwcDisplay::backend() const {
1507 return backend_.get();
1508}
1509
1510void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1511 backend_ = std::move(backend);
1512}
1513
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001514} // namespace android