blob: 16d8bac0b46e3324dd48528109d65442d047b536 [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
141auto GetModesetLayerProperties(buffer_handle_t buffer, uint32_t width,
142 uint32_t height) -> HwcLayer::LayerProperties {
143 HwcLayer::LayerProperties properties;
144 properties.buffer = {.buffer_handle = buffer, .acquire_fence = {}};
145 properties.display_frame = {
146 .left = 0,
147 .top = 0,
148 .right = int(width),
149 .bottom = int(height),
150 };
151 properties.source_crop = (hwc_frect_t){
152 .left = 0.0F,
153 .top = 0.0F,
154 .right = static_cast<float>(width),
155 .bottom = static_cast<float>(height),
156 };
157 properties.blend_mode = BufferBlendMode::kNone;
158 return properties;
159}
160} // namespace
161
Roman Stratiienko44b95772025-01-22 18:03:36 +0200162static BufferColorSpace Hwc2ToColorSpace(int32_t dataspace) {
163 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
164 case HAL_DATASPACE_STANDARD_BT709:
165 return BufferColorSpace::kItuRec709;
166 case HAL_DATASPACE_STANDARD_BT601_625:
167 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
168 case HAL_DATASPACE_STANDARD_BT601_525:
169 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
170 return BufferColorSpace::kItuRec601;
171 case HAL_DATASPACE_STANDARD_BT2020:
172 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
173 return BufferColorSpace::kItuRec2020;
174 default:
175 return BufferColorSpace::kUndefined;
176 }
177}
178
179static BufferSampleRange Hwc2ToSampleRange(int32_t dataspace) {
180 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
181 case HAL_DATASPACE_RANGE_FULL:
182 return BufferSampleRange::kFullRange;
183 case HAL_DATASPACE_RANGE_LIMITED:
184 return BufferSampleRange::kLimitedRange;
185 default:
186 return BufferSampleRange::kUndefined;
187 }
188}
189
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200190std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
191 if (delta.total_pixops_ == 0)
192 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200193 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194
195 std::stringstream ss;
196 ss << " Total frames count: " << delta.total_frames_ << "\n"
197 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
198 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
199 << ((delta.failed_kms_present_ > 0)
200 ? " !!! Internal failure, FIX it please\n"
201 : "")
202 << " Flattened frames: " << delta.frames_flattened_ << "\n"
203 << " Pixel operations (free units)"
204 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
205 << "]\n"
206 << " Composition efficiency: " << ratio;
207
208 return ss.str();
209}
210
211std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300212 auto connector_name = IsInHeadlessMode()
213 ? std::string("NULL-DISPLAY")
214 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200215
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200217 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218 << "Statistics since system boot:\n"
219 << DumpDelta(total_stats_) << "\n\n"
220 << "Statistics since last dumpsys request:\n"
221 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
222
223 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
224 return ss.str();
225}
226
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200227HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000228 DrmHwc *hwc)
229 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300230 if (type_ == HWC2::DisplayType::Virtual) {
231 writeback_layer_ = std::make_unique<HwcLayer>(this);
232 }
233}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200234
Drew Davenport76c17a82025-01-15 15:02:45 -0700235void HwcDisplay::SetColorTransformMatrix(
236 const std::array<float, 16> &color_transform_matrix) {
237 auto almost_equal = [](auto a, auto b) {
238 const float epsilon = 0.001F;
239 return std::abs(a - b) < epsilon;
240 };
241 const bool is_identity = std::equal(color_transform_matrix.begin(),
242 color_transform_matrix.end(),
243 kIdentityMatrix.begin(), almost_equal);
244 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
245 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
246 if (color_transform_hint_ == is_identity) {
247 SetColorMatrixToIdentity();
248 } else {
249 color_matrix_ = ToColorTransform(color_transform_matrix);
250 }
251}
252
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400253void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200254 color_matrix_ = std::make_shared<drm_color_ctm>();
255 for (int i = 0; i < kCtmCols; i++) {
256 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800257 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200258 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200259 }
260 }
261
262 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200263}
264
Normunds Rieksts545096d2024-03-11 16:37:45 +0000265HwcDisplay::~HwcDisplay() {
266 Deinit();
267};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200268
Drew Davenportfe70c802024-11-07 13:02:21 -0700269auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
270 -> const HwcDisplayConfig * {
271 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600272 if (config_iter == configs_.hwc_configs.end()) {
273 return nullptr;
274 }
275 return &config_iter->second;
276}
277
Drew Davenportfe70c802024-11-07 13:02:21 -0700278auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
279 return GetConfig(configs_.active_config_id);
280}
281
Drew Davenport8998f8b2024-10-24 10:15:12 -0600282auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700283 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600284}
285
Drew Davenport97b5abc2024-11-07 10:43:54 -0700286HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
287 const HwcDisplayConfig *new_config = GetConfig(config);
288 if (new_config == nullptr) {
289 ALOGE("Could not find active mode for %u", config);
290 return ConfigError::kBadConfig;
291 }
292
293 const HwcDisplayConfig *current_config = GetCurrentConfig();
294
295 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700296 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700297
298 std::optional<LayerData> modeset_layer_data;
299 // If a client layer has already been provided, and its size matches the
300 // new config, use it for the modeset.
301 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
302 current_config->mode.GetRawMode().hdisplay == width &&
303 current_config->mode.GetRawMode().vdisplay == height) {
304 ALOGV("Use existing client_layer for blocking config.");
305 modeset_layer_data = client_layer_.GetLayerData();
306 } else {
307 ALOGV("Allocate modeset buffer.");
308 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
309 if (modeset_buffer != nullptr) {
310 auto modeset_layer = std::make_unique<HwcLayer>(this);
311 modeset_layer->SetLayerProperties(
312 GetModesetLayerProperties(modeset_buffer, width, height));
313 modeset_layer->PopulateLayerData();
314 modeset_layer_data = modeset_layer->GetLayerData();
315 GraphicBufferAllocator::get().free(modeset_buffer);
316 }
317 }
318
319 ALOGV("Create modeset commit.");
320 // Create atomic commit args for a blocking modeset. There's no need to do a
321 // separate test commit, since the commit does a test anyways.
322 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
323 modeset_layer_data);
324 commit_args.blocking = true;
325 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
326
327 if (ret) {
328 ALOGE("Blocking config failed: %d", ret);
329 return HwcDisplay::ConfigError::kBadConfig;
330 }
331
332 ALOGV("Blocking config succeeded.");
333 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700334 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700335 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
336 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700337 return ConfigError::kNone;
338}
339
Drew Davenport8998f8b2024-10-24 10:15:12 -0600340auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
341 bool seamless, QueuedConfigTiming *out_timing)
342 -> ConfigError {
343 if (configs_.hwc_configs.count(config) == 0) {
344 ALOGE("Could not find active mode for %u", config);
345 return ConfigError::kBadConfig;
346 }
347
348 // TODO: Add support for seamless configuration changes.
349 if (seamless) {
350 return ConfigError::kSeamlessNotAllowed;
351 }
352
353 // Request a refresh from the client one vsync period before the desired
354 // time, or simply at the desired time if there is no active configuration.
355 const HwcDisplayConfig *current_config = GetCurrentConfig();
356 out_timing->refresh_time_ns = desired_time -
357 (current_config
358 ? current_config->mode.GetVSyncPeriodNs()
359 : 0);
360 out_timing->new_vsync_time_ns = desired_time;
361
362 // Queue the config change timing to be consistent with the requested
363 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600364 staged_mode_change_time_ = out_timing->refresh_time_ns;
365 staged_mode_config_id_ = config;
366
367 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700368 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600369
370 return ConfigError::kNone;
371}
372
Drew Davenport7f356c82025-01-17 16:32:06 -0700373auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
374 if (IsInHeadlessMode()) {
375 return {};
376 }
377
378 /* In current drm_hwc design in case previous frame layer was not validated as
379 * a CLIENT, it is used by display controller (Front buffer). We have to store
380 * this state to provide the CLIENT with the release fences for such buffers.
381 */
382 for (auto &l : layers_) {
383 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
384 HWC2::Composition::Client);
385 }
386
387 // ValidateDisplay returns the number of layers that may be changed.
388 uint32_t num_types = 0;
389 uint32_t num_requests = 0;
390 backend_->ValidateDisplay(this, &num_types, &num_requests);
391
392 if (num_types == 0) {
393 return {};
394 }
395
396 // Iterate through the layers to find which layers actually changed.
397 std::vector<ChangedLayer> changed_layers;
398 for (auto &l : layers_) {
399 if (l.second.IsTypeChanged()) {
400 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
401 }
402 }
403 return changed_layers;
404}
405
Drew Davenportb864ccf2025-01-22 14:57:36 -0700406auto HwcDisplay::AcceptValidatedComposition() -> void {
407 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
408 l.second.AcceptTypeChange();
409 }
410}
411
412auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200413 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
414 -> bool {
415 int out_fd = -1;
416 auto error = PresentDisplay(&out_fd);
417 out_present_fence = MakeSharedFd(out_fd);
418 if (error != HWC2::Error::None) {
419 return false;
420 }
421
422 if (!out_present_fence) {
423 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700424 }
425
426 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200427 if (l.second.GetPriorBufferScanOutFlag()) {
428 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700429 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700430 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200431
432 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700433}
434
Roman Stratiienko63762a92023-09-18 22:33:45 +0300435void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200436 Deinit();
437
Roman Stratiienko63762a92023-09-18 22:33:45 +0300438 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200439
Roman Stratiienko63762a92023-09-18 22:33:45 +0300440 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200441 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000442 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200443 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000444 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200445 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200446}
447
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200448void HwcDisplay::Deinit() {
449 if (pipeline_ != nullptr) {
450 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200451 a_args.composition = std::make_shared<DrmKmsPlan>();
452 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300453 a_args.composition = {};
454 a_args.active = false;
455 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200456
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200457 current_plan_.reset();
458 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200459 if (flatcon_) {
460 flatcon_->StopThread();
461 flatcon_.reset();
462 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200463 }
464
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200465 if (vsync_worker_) {
466 vsync_worker_->StopThread();
467 vsync_worker_ = {};
468 }
469
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200470 SetClientTarget(nullptr, -1, 0, {});
471}
472
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200473HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200474 ChosePreferredConfig();
475
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300476 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700477 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300478 if (!vsync_worker_) {
479 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
480 return HWC2::Error::BadDisplay;
481 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200482 }
483
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200484 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200485 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200486 if (ret) {
487 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
488 return HWC2::Error::BadDisplay;
489 }
Drew Davenport93443182023-12-14 09:25:45 +0000490 auto flatcbk = (struct FlatConCallbacks){
491 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200492 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200493 }
494
Roman Stratiienko44b95772025-01-22 18:03:36 +0200495 HwcLayer::LayerProperties lp;
496 lp.blend_mode = BufferBlendMode::kPreMult;
497 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200498
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400499 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200500
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200501 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502}
503
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600504std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
505 if (IsInHeadlessMode()) {
506 // The pipeline can be nullptr in headless mode, so return the default
507 // "normal" mode.
508 return PanelOrientation::kModePanelOrientationNormal;
509 }
510
511 DrmDisplayPipeline &pipeline = GetPipe();
512 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
513 ALOGW(
514 "No display pipeline present to query the panel orientation property.");
515 return {};
516 }
517
518 return pipeline.connector->Get()->GetPanelOrientation();
519}
520
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200522 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300523 if (type_ == HWC2::DisplayType::Virtual) {
524 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
525 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200526 err = configs_.Update(*pipeline_->connector->Get());
527 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300528 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200529 }
530 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200531 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200532 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533
Roman Stratiienko0137f862022-01-04 18:27:40 +0200534 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535}
536
537HWC2::Error HwcDisplay::AcceptDisplayChanges() {
538 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
539 l.second.AcceptTypeChange();
540 return HWC2::Error::None;
541}
542
543HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200544 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200545 *layer = static_cast<hwc2_layer_t>(layer_idx_);
546 ++layer_idx_;
547 return HWC2::Error::None;
548}
549
550HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200551 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200552 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200553 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554
555 layers_.erase(layer);
556 return HWC2::Error::None;
557}
558
559HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700560 // If a config has been queued, it is considered the "active" config.
561 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
562 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200563 return HWC2::Error::BadConfig;
564
Drew Davenportfe70c802024-11-07 13:02:21 -0700565 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566 return HWC2::Error::None;
567}
568
569HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
570 hwc2_layer_t *layers,
571 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200572 if (IsInHeadlessMode()) {
573 *num_elements = 0;
574 return HWC2::Error::None;
575 }
576
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300578 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200579 if (l.second.IsTypeChanged()) {
580 if (layers && num_changes < *num_elements)
581 layers[num_changes] = l.first;
582 if (types && num_changes < *num_elements)
583 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
584 ++num_changes;
585 }
586 }
587 if (!layers && !types)
588 *num_elements = num_changes;
589 return HWC2::Error::None;
590}
591
592HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
593 int32_t /*format*/,
594 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200595 if (IsInHeadlessMode()) {
596 return HWC2::Error::None;
597 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200598
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300599 auto min = pipeline_->device->GetMinResolution();
600 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200601
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200602 if (width < min.first || height < min.second)
603 return HWC2::Error::Unsupported;
604
605 if (width > max.first || height > max.second)
606 return HWC2::Error::Unsupported;
607
608 if (dataspace != HAL_DATASPACE_UNKNOWN)
609 return HWC2::Error::Unsupported;
610
611 // TODO(nobody): Validate format can be handled by either GL or planes
612 return HWC2::Error::None;
613}
614
615HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500616 if (!modes) {
617 std::vector<Colormode> temp_modes;
618 GetEdid()->GetColorModes(temp_modes);
619 *num_modes = temp_modes.size();
620 return HWC2::Error::None;
621 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500623 std::vector<Colormode> temp_modes;
624 std::vector<int32_t> out_modes(modes, modes + *num_modes);
625 GetEdid()->GetColorModes(temp_modes);
626 if (temp_modes.empty()) {
627 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
628 return HWC2::Error::None;
629 }
630
631 for (auto &c : temp_modes)
632 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200633
634 return HWC2::Error::None;
635}
636
637HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
638 int32_t attribute_in,
639 int32_t *value) {
640 int conf = static_cast<int>(config);
641
Roman Stratiienko0137f862022-01-04 18:27:40 +0200642 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200643 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200644 return HWC2::Error::BadConfig;
645 }
646
Roman Stratiienko0137f862022-01-04 18:27:40 +0200647 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648
649 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300650 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200651 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
652 switch (attribute) {
653 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200654 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655 break;
656 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200657 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200658 break;
659 case HWC2::Attribute::VsyncPeriod:
660 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600661 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200662 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000663 case HWC2::Attribute::DpiY:
664 // ideally this should be vdisplay/mm_heigth, however mm_height
665 // comes from edid parsing and is highly unreliable. Viewing the
666 // rarity of anisotropic displays, falling back to a single value
667 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200668 case HWC2::Attribute::DpiX:
669 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200670 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
671 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200672 : -1;
673 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200674#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200675 case HWC2::Attribute::ConfigGroup:
676 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
677 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200678 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200679 break;
680#endif
681 default:
682 *value = -1;
683 return HWC2::Error::BadConfig;
684 }
685 return HWC2::Error::None;
686}
687
Drew Davenportf7e88332024-09-06 12:54:38 -0600688HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
689 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200690 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200691 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200692 if (hwc_config.second.disabled) {
693 continue;
694 }
695
696 if (configs != nullptr) {
697 if (idx >= *num_configs) {
698 break;
699 }
700 configs[idx] = hwc_config.second.id;
701 }
702
703 idx++;
704 }
705 *num_configs = idx;
706 return HWC2::Error::None;
707}
708
709HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
710 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200711 if (IsInHeadlessMode()) {
712 stream << "null-display";
713 } else {
714 stream << "display-" << GetPipe().connector->Get()->GetId();
715 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300716 auto string = stream.str();
717 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200718 if (!name) {
719 *size = length;
720 return HWC2::Error::None;
721 }
722
723 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
724 strncpy(name, string.c_str(), *size);
725 return HWC2::Error::None;
726}
727
728HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
729 uint32_t *num_elements,
730 hwc2_layer_t * /*layers*/,
731 int32_t * /*layer_requests*/) {
732 // TODO(nobody): I think virtual display should request
733 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
734 *num_elements = 0;
735 return HWC2::Error::None;
736}
737
738HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
739 *type = static_cast<int32_t>(type_);
740 return HWC2::Error::None;
741}
742
743HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
744 *support = 0;
745 return HWC2::Error::None;
746}
747
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500748HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
749 float *max_luminance,
750 float *max_average_luminance,
751 float *min_luminance) {
752 if (!types) {
753 std::vector<ui::Hdr> temp_types;
754 float lums[3] = {0.F};
755 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
756 *num_types = temp_types.size();
757 return HWC2::Error::None;
758 }
759
760 std::vector<ui::Hdr> temp_types;
761 std::vector<int32_t> out_types(types, types + *num_types);
762 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
763 max_average_luminance, min_luminance);
764 for (auto &t : temp_types) {
765 switch (t) {
766 case ui::Hdr::HDR10:
767 out_types.emplace_back(HAL_HDR_HDR10);
768 break;
769 case ui::Hdr::HLG:
770 out_types.emplace_back(HAL_HDR_HLG);
771 break;
772 default:
773 // Ignore any other HDR types
774 break;
775 }
776 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200777 return HWC2::Error::None;
778}
779
780/* Find API details at:
781 * 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 +0300782 *
783 * Called after PresentDisplay(), CLIENT is expecting release fence for the
784 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 */
786HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
787 hwc2_layer_t *layers,
788 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200789 if (IsInHeadlessMode()) {
790 *num_elements = 0;
791 return HWC2::Error::None;
792 }
793
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200794 uint32_t num_layers = 0;
795
Roman Stratiienkodd214942022-05-03 18:24:49 +0300796 for (auto &l : layers_) {
797 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
798 continue;
799 }
800
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200801 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300802
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200803 if (layers == nullptr || fences == nullptr)
804 continue;
805
806 if (num_layers > *num_elements) {
807 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
808 return HWC2::Error::None;
809 }
810
811 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200812 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200813 }
814 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300815
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200816 return HWC2::Error::None;
817}
818
Drew Davenport97b5abc2024-11-07 10:43:54 -0700819AtomicCommitArgs HwcDisplay::CreateModesetCommit(
820 const HwcDisplayConfig *config,
821 const std::optional<LayerData> &modeset_layer) {
822 AtomicCommitArgs args{};
823
824 args.color_matrix = color_matrix_;
825 args.content_type = content_type_;
826 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500827 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700828
829 std::vector<LayerData> composition_layers;
830 if (modeset_layer) {
831 composition_layers.emplace_back(modeset_layer.value());
832 }
833
834 if (composition_layers.empty()) {
835 ALOGW("Attempting to create a modeset commit without a layer.");
836 }
837
838 args.display_mode = config->mode;
839 args.active = true;
840 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
841 std::move(
842 composition_layers));
843 ALOGW_IF(!args.composition, "No composition for blocking modeset");
844
845 return args;
846}
847
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200848HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200849 if (IsInHeadlessMode()) {
850 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
851 return HWC2::Error::None;
852 }
853
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200854 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400855 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400856 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500857 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200858
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200859 uint32_t prev_vperiod_ns = 0;
860 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200861
Drew Davenportd387c842024-12-16 16:57:24 -0700862 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700863 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200864 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700865 const HwcDisplayConfig *staged_config = GetConfig(
866 staged_mode_config_id_.value());
867 if (staged_config == nullptr) {
868 return HWC2::Error::BadConfig;
869 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200870 HwcLayer::LayerProperties lp;
871 lp.display_frame = {
872 .left = 0,
873 .top = 0,
874 .right = int(staged_config->mode.GetRawMode().hdisplay),
875 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
876 };
877 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200878
Drew Davenportfe70c802024-11-07 13:02:21 -0700879 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700880 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200881 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700882 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200883 }
884 }
885
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200886 // order the layers by z-order
887 bool use_client_layer = false;
888 uint32_t client_z_order = UINT32_MAX;
889 std::map<uint32_t, HwcLayer *> z_map;
890 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
891 switch (l.second.GetValidatedType()) {
892 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300893 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200894 break;
895 case HWC2::Composition::Client:
896 // Place it at the z_order of the lowest client layer
897 use_client_layer = true;
898 client_z_order = std::min(client_z_order, l.second.GetZOrder());
899 break;
900 default:
901 continue;
902 }
903 }
904 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300905 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200906
907 if (z_map.empty())
908 return HWC2::Error::BadLayer;
909
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200910 std::vector<LayerData> composition_layers;
911
912 /* Import & populate */
913 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200914 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200915 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200916
917 // now that they're ordered by z, add them to the composition
918 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200919 if (!l.second->IsLayerUsableAsDevice()) {
920 /* This will be normally triggered on validation of the first frame
921 * containing CLIENT layer. At this moment client buffer is not yet
922 * provided by the CLIENT.
923 * This may be triggered once in HwcLayer lifecycle in case FB can't be
924 * imported. For example when non-contiguous buffer is imported into
925 * contiguous-only DRM/KMS driver.
926 */
927 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200928 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200929 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200930 }
931
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200932 /* Store plan to ensure shared planes won't be stolen by other display
933 * in between of ValidateDisplay() and PresentDisplay() calls
934 */
935 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
936 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300937
938 if (type_ == HWC2::DisplayType::Virtual) {
939 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
940 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
941 .acquire_fence;
942 }
943
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200944 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700945 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200946 return HWC2::Error::BadConfig;
947 }
948
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200949 a_args.composition = current_plan_;
950
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300951 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200952
953 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700954 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200955 return HWC2::Error::BadParameter;
956 }
957
Drew Davenportd387c842024-12-16 16:57:24 -0700958 if (new_vsync_period_ns) {
959 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700960 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700961
962 vsync_worker_->SetVsyncTimestampTracking(false);
963 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
964 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000965 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700966 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000967 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200968 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200969 }
970
971 return HWC2::Error::None;
972}
973
974/* Find API details at:
975 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
976 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300977HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200978 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300979 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200980 return HWC2::Error::None;
981 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200982 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200983
984 ++total_stats_.total_frames_;
985
986 AtomicCommitArgs a_args{};
987 ret = CreateComposition(a_args);
988
989 if (ret != HWC2::Error::None)
990 ++total_stats_.failed_kms_present_;
991
992 if (ret == HWC2::Error::BadLayer) {
993 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300994 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995 return HWC2::Error::None;
996 }
997 if (ret != HWC2::Error::None)
998 return ret;
999
Roman Stratiienko76892782023-01-16 17:15:53 +02001000 this->present_fence_ = a_args.out_fence;
1001 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001002
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001003 // Reset the color matrix so we don't apply it over and over again.
1004 color_matrix_ = {};
1005
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001006 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -07001007
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001008 return HWC2::Error::None;
1009}
1010
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001011HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
1012 int64_t change_time) {
1013 if (configs_.hwc_configs.count(config) == 0) {
1014 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001015 return HWC2::Error::BadConfig;
1016 }
1017
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001018 staged_mode_change_time_ = change_time;
1019 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001020
1021 return HWC2::Error::None;
1022}
1023
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001024HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
1025 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
1026}
1027
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001028/* Find API details at:
1029 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
1030 */
1031HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
1032 int32_t acquire_fence,
1033 int32_t dataspace,
1034 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001035 HwcLayer::LayerProperties lp;
1036 lp.buffer = {.buffer_handle = target,
1037 .acquire_fence = MakeSharedFd(acquire_fence)};
1038 lp.color_space = Hwc2ToColorSpace(dataspace);
1039 lp.sample_range = Hwc2ToSampleRange(dataspace);
1040 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001041
1042 /*
1043 * target can be nullptr, this does mean the Composer Service is calling
1044 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
1045 * 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
1046 */
1047 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +03001048 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001049 return HWC2::Error::None;
1050 }
1051
Roman Stratiienko5070d512022-05-30 13:41:20 +03001052 if (IsInHeadlessMode()) {
1053 return HWC2::Error::None;
1054 }
1055
Roman Stratiienko359a9d32023-01-16 17:41:07 +02001056 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001057 if (!client_layer_.IsLayerUsableAsDevice()) {
1058 ALOGE("Client layer must be always usable by DRM/KMS");
1059 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +02001060 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001061
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001062 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001063 if (!bi) {
1064 ALOGE("%s: Invalid state", __func__);
1065 return HWC2::Error::BadLayer;
1066 }
1067
Roman Stratiienko44b95772025-01-22 18:03:36 +02001068 lp = {};
1069 lp.source_crop = {.left = 0.0F,
1070 .top = 0.0F,
1071 .right = float(bi->width),
1072 .bottom = float(bi->height)};
1073 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001074
1075 return HWC2::Error::None;
1076}
1077
1078HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001079 /* Maps to the Colorspace DRM connector property:
1080 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1081 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001082 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001083 return HWC2::Error::BadParameter;
1084
Sasha McIntosh5294f092024-09-18 18:14:54 -04001085 switch (mode) {
1086 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001087 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001088 colorspace_ = Colorspace::kDefault;
1089 break;
1090 case HAL_COLOR_MODE_STANDARD_BT601_625:
1091 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1092 case HAL_COLOR_MODE_STANDARD_BT601_525:
1093 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001094 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001095 // The DP spec does not say whether this is the 525 or the 625 line version.
1096 colorspace_ = Colorspace::kBt601Ycc;
1097 break;
1098 case HAL_COLOR_MODE_STANDARD_BT709:
1099 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001100 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001101 colorspace_ = Colorspace::kBt709Ycc;
1102 break;
1103 case HAL_COLOR_MODE_DCI_P3:
1104 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001105 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001106 colorspace_ = Colorspace::kDciP3RgbD65;
1107 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001108 case HAL_COLOR_MODE_DISPLAY_BT2020: {
1109 std::vector<ui::Hdr> hdr_types;
1110 GetEdid()->GetSupportedHdrTypes(hdr_types);
1111 if (!hdr_types.empty()) {
1112 auto ret = SetHdrOutputMetadata(hdr_types.front());
1113 if (ret != HWC2::Error::None)
1114 return ret;
1115 }
1116 colorspace_ = Colorspace::kBt2020Rgb;
1117 break;
1118 }
Sasha McIntosh5294f092024-09-18 18:14:54 -04001119 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001120 case HAL_COLOR_MODE_BT2020:
1121 case HAL_COLOR_MODE_BT2100_PQ:
1122 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001123 default:
1124 return HWC2::Error::Unsupported;
1125 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001126
1127 color_mode_ = mode;
1128 return HWC2::Error::None;
1129}
1130
1131HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1132 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1133 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1134 return HWC2::Error::BadParameter;
1135
1136 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1137 return HWC2::Error::BadParameter;
1138
1139 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001140
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001141 if (IsInHeadlessMode())
1142 return HWC2::Error::None;
1143
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001144 if (!GetPipe().crtc->Get()->GetCtmProperty())
1145 return HWC2::Error::None;
1146
1147 switch (color_transform_hint_) {
1148 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001149 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001150 break;
1151 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001152 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001153 {
1154 for (int i = 12; i < 14; i++) {
1155 if (matrix[i] != 0.F)
1156 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001157 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001158 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1159 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1160 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001161 }
1162 break;
1163 default:
1164 return HWC2::Error::Unsupported;
1165 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001166
1167 return HWC2::Error::None;
1168}
1169
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001170bool HwcDisplay::CtmByGpu() {
1171 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1172 return false;
1173
1174 if (GetPipe().crtc->Get()->GetCtmProperty())
1175 return false;
1176
Drew Davenport93443182023-12-14 09:25:45 +00001177 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001178 return false;
1179
1180 return true;
1181}
1182
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001183HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1184 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001185 HwcLayer::LayerProperties lp;
1186 lp.buffer = {.buffer_handle = buffer,
1187 .acquire_fence = MakeSharedFd(release_fence)};
1188 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001189 writeback_layer_->PopulateLayerData();
1190 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1191 ALOGE("Output layer must be always usable by DRM/KMS");
1192 return HWC2::Error::BadLayer;
1193 }
1194 /* TODO: Check if format is supported by writeback connector */
1195 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001196}
1197
1198HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1199 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001200
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001201 AtomicCommitArgs a_args{};
1202
1203 switch (mode) {
1204 case HWC2::PowerMode::Off:
1205 a_args.active = false;
1206 break;
1207 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001208 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001209 break;
1210 case HWC2::PowerMode::Doze:
1211 case HWC2::PowerMode::DozeSuspend:
1212 return HWC2::Error::Unsupported;
1213 default:
John Stultzffe783c2024-02-14 10:51:27 -08001214 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001215 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001216 }
1217
1218 if (IsInHeadlessMode()) {
1219 return HWC2::Error::None;
1220 }
1221
Jia Ren80566fe2022-11-17 17:26:00 +08001222 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001223 /*
1224 * Setting the display to active before we have a composition
1225 * can break some drivers, so skip setting a_args.active to
1226 * true, as the next composition frame will implicitly activate
1227 * the display
1228 */
1229 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1230 ? HWC2::Error::None
1231 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001232 };
1233
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001234 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001235 if (err) {
1236 ALOGE("Failed to apply the dpms composition err=%d", err);
1237 return HWC2::Error::BadParameter;
1238 }
1239 return HWC2::Error::None;
1240}
1241
1242HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001243 if (type_ == HWC2::DisplayType::Virtual) {
1244 return HWC2::Error::None;
1245 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001246 if (!vsync_worker_) {
1247 return HWC2::Error::NoResources;
1248 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001249
Roman Stratiienko099c3112022-01-20 11:50:54 +02001250 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001251 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001252 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001253 DrmHwc *hwc = hwc_;
1254 hwc2_display_t id = handle_;
1255 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001256 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001257 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1258 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001259 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001260 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001261 return HWC2::Error::None;
1262}
1263
1264HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1265 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001266 if (IsInHeadlessMode()) {
1267 *num_types = *num_requests = 0;
1268 return HWC2::Error::None;
1269 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001270
1271 /* In current drm_hwc design in case previous frame layer was not validated as
1272 * a CLIENT, it is used by display controller (Front buffer). We have to store
1273 * this state to provide the CLIENT with the release fences for such buffers.
1274 */
1275 for (auto &l : layers_) {
1276 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1277 HWC2::Composition::Client);
1278 }
1279
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001280 return backend_->ValidateDisplay(this, num_types, num_requests);
1281}
1282
1283std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1284 std::vector<HwcLayer *> ordered_layers;
1285 ordered_layers.reserve(layers_.size());
1286
1287 for (auto &[handle, layer] : layers_) {
1288 ordered_layers.emplace_back(&layer);
1289 }
1290
1291 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1292 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1293 return lhs->GetZOrder() < rhs->GetZOrder();
1294 });
1295
1296 return ordered_layers;
1297}
1298
Roman Stratiienko099c3112022-01-20 11:50:54 +02001299HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1300 uint32_t *outVsyncPeriod /* ns */) {
1301 return GetDisplayAttribute(configs_.active_config_id,
1302 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1303 (int32_t *)(outVsyncPeriod));
1304}
1305
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001306// Display primary values are coded as unsigned 16-bit values in units of
1307// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1308static uint64_t ToU16ColorValue(float in) {
1309 constexpr float kPrimariesFixedPoint = 50000.F;
1310 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1311}
1312
1313HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1314 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1315 hdr_metadata_->metadata_type = 0;
1316 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1317 m->metadata_type = 0;
1318
1319 switch (type) {
1320 case ui::Hdr::HDR10:
1321 m->eotf = 2; // PQ
1322 break;
1323 case ui::Hdr::HLG:
1324 m->eotf = 3; // HLG
1325 break;
1326 default:
1327 return HWC2::Error::Unsupported;
1328 }
1329
1330 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1331 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1332 std::vector<ui::Hdr> types;
1333 float hdr_luminance[3]{0.F, 0.F, 0.F};
1334 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1335 &hdr_luminance[2]);
1336 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1337 hdr_luminance[0]);
1338 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1339 // The min luminance value is coded as an unsigned 16-bit value in units of
1340 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1341 // represents 6.5535 cd/m2.
1342 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1343 10000.F);
1344
1345 auto gamut = ColorGamut::BT2020();
1346 auto primaries = gamut.getPrimaries();
1347 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1348 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1349 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1350 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1351 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1352 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1353
1354 auto whitePoint = gamut.getWhitePoint();
1355 m->white_point.x = ToU16ColorValue(whitePoint.x);
1356 m->white_point.y = ToU16ColorValue(whitePoint.y);
1357
1358 return HWC2::Error::None;
1359}
1360
Roman Stratiienko6b405052022-12-10 19:09:10 +02001361#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001362HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001363 if (IsInHeadlessMode()) {
1364 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1365 return HWC2::Error::None;
1366 }
1367 /* Primary display should be always internal,
1368 * otherwise SF will be unhappy and will crash
1369 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001370 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001371 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001372 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001373 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1374 else
1375 return HWC2::Error::BadConfig;
1376
1377 return HWC2::Error::None;
1378}
1379
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001380HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001381 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001382 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1383 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001384 if (type_ == HWC2::DisplayType::Virtual) {
1385 return HWC2::Error::None;
1386 }
1387
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001388 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1389 return HWC2::Error::BadParameter;
1390 }
1391
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001392 uint32_t current_vsync_period{};
1393 GetDisplayVsyncPeriod(&current_vsync_period);
1394
1395 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1396 return HWC2::Error::SeamlessNotAllowed;
1397 }
1398
1399 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1400 ->desiredTimeNanos -
1401 current_vsync_period;
1402 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1403 if (ret != HWC2::Error::None) {
1404 return ret;
1405 }
1406
1407 outTimeline->refreshRequired = true;
1408 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1409 ->desiredTimeNanos;
1410
Drew Davenport33121b72024-12-13 14:59:35 -07001411 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001412
1413 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001414}
1415
1416HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1417 return HWC2::Error::Unsupported;
1418}
1419
1420HWC2::Error HwcDisplay::GetSupportedContentTypes(
1421 uint32_t *outNumSupportedContentTypes,
1422 const uint32_t *outSupportedContentTypes) {
1423 if (outSupportedContentTypes == nullptr)
1424 *outNumSupportedContentTypes = 0;
1425
1426 return HWC2::Error::None;
1427}
1428
1429HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001430 /* Maps exactly to the content_type DRM connector property:
1431 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001432 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001433 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1434 return HWC2::Error::BadParameter;
1435
1436 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001437
1438 return HWC2::Error::None;
1439}
1440#endif
1441
Roman Stratiienko6b405052022-12-10 19:09:10 +02001442#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001443HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1444 uint32_t *outDataSize,
1445 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001446 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001447 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001448 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001449
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001450 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001451 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001452 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001453 }
1454
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001455 *outPort = handle_; /* TDOD(nobody): What should be here? */
1456
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001457 if (outData) {
1458 *outDataSize = std::min(*outDataSize, blob->length);
1459 memcpy(outData, blob->data, *outDataSize);
1460 } else {
1461 *outDataSize = blob->length;
1462 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001463
1464 return HWC2::Error::None;
1465}
1466
1467HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001468 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001469 if (outNumCapabilities == nullptr) {
1470 return HWC2::Error::BadParameter;
1471 }
1472
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001473 bool skip_ctm = false;
1474
1475 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001476 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001477 skip_ctm = true;
1478
1479 // Skip client CTM if DRM can handle it
1480 if (!skip_ctm && !IsInHeadlessMode() &&
1481 GetPipe().crtc->Get()->GetCtmProperty())
1482 skip_ctm = true;
1483
1484 if (!skip_ctm) {
1485 *outNumCapabilities = 0;
1486 return HWC2::Error::None;
1487 }
1488
1489 *outNumCapabilities = 1;
1490 if (outCapabilities) {
1491 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1492 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001493
1494 return HWC2::Error::None;
1495}
1496
1497HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1498 *supported = false;
1499 return HWC2::Error::None;
1500}
1501
1502HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1503 return HWC2::Error::Unsupported;
1504}
1505
Roman Stratiienko6b405052022-12-10 19:09:10 +02001506#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001507
Roman Stratiienko6b405052022-12-10 19:09:10 +02001508#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001509
1510HWC2::Error HwcDisplay::GetRenderIntents(
1511 int32_t mode, uint32_t *outNumIntents,
1512 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1513 if (mode != HAL_COLOR_MODE_NATIVE) {
1514 return HWC2::Error::BadParameter;
1515 }
1516
1517 if (outIntents == nullptr) {
1518 *outNumIntents = 1;
1519 return HWC2::Error::None;
1520 }
1521 *outNumIntents = 1;
1522 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1523 return HWC2::Error::None;
1524}
1525
1526HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1527 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1528 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1529 return HWC2::Error::BadParameter;
1530
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001531 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1532 return HWC2::Error::Unsupported;
1533
Sasha McIntosh5294f092024-09-18 18:14:54 -04001534 auto err = SetColorMode(mode);
1535 if (err != HWC2::Error::None) return err;
1536
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001537 return HWC2::Error::None;
1538}
1539
Roman Stratiienko6b405052022-12-10 19:09:10 +02001540#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001541
1542const Backend *HwcDisplay::backend() const {
1543 return backend_.get();
1544}
1545
1546void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1547 backend_ = std::move(backend);
1548}
1549
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001550} // namespace android