blob: da40416068433b1a7fd6a76431201b3bc8cc00e9 [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 Stratiienko3627beb2022-01-04 16:02:55 +0200143std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
144 if (delta.total_pixops_ == 0)
145 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200146 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200147
148 std::stringstream ss;
149 ss << " Total frames count: " << delta.total_frames_ << "\n"
150 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
151 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
152 << ((delta.failed_kms_present_ > 0)
153 ? " !!! Internal failure, FIX it please\n"
154 : "")
155 << " Flattened frames: " << delta.frames_flattened_ << "\n"
156 << " Pixel operations (free units)"
157 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
158 << "]\n"
159 << " Composition efficiency: " << ratio;
160
161 return ss.str();
162}
163
164std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300165 auto connector_name = IsInHeadlessMode()
166 ? std::string("NULL-DISPLAY")
167 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200168
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200169 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200170 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171 << "Statistics since system boot:\n"
172 << DumpDelta(total_stats_) << "\n\n"
173 << "Statistics since last dumpsys request:\n"
174 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
175
176 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
177 return ss.str();
178}
179
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200180HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000181 DrmHwc *hwc)
182 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300183 if (type_ == HWC2::DisplayType::Virtual) {
184 writeback_layer_ = std::make_unique<HwcLayer>(this);
185 }
186}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200187
Drew Davenport76c17a82025-01-15 15:02:45 -0700188void HwcDisplay::SetColorTransformMatrix(
189 const std::array<float, 16> &color_transform_matrix) {
190 auto almost_equal = [](auto a, auto b) {
191 const float epsilon = 0.001F;
192 return std::abs(a - b) < epsilon;
193 };
194 const bool is_identity = std::equal(color_transform_matrix.begin(),
195 color_transform_matrix.end(),
196 kIdentityMatrix.begin(), almost_equal);
197 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
198 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
199 if (color_transform_hint_ == is_identity) {
200 SetColorMatrixToIdentity();
201 } else {
202 color_matrix_ = ToColorTransform(color_transform_matrix);
203 }
204}
205
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400206void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200207 color_matrix_ = std::make_shared<drm_color_ctm>();
208 for (int i = 0; i < kCtmCols; i++) {
209 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800210 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200211 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200212 }
213 }
214
215 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200216}
217
Normunds Rieksts545096d2024-03-11 16:37:45 +0000218HwcDisplay::~HwcDisplay() {
219 Deinit();
220};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200221
Drew Davenportfe70c802024-11-07 13:02:21 -0700222auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
223 -> const HwcDisplayConfig * {
224 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600225 if (config_iter == configs_.hwc_configs.end()) {
226 return nullptr;
227 }
228 return &config_iter->second;
229}
230
Drew Davenportfe70c802024-11-07 13:02:21 -0700231auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
232 return GetConfig(configs_.active_config_id);
233}
234
Drew Davenport8998f8b2024-10-24 10:15:12 -0600235auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700236 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600237}
238
Drew Davenport97b5abc2024-11-07 10:43:54 -0700239HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
240 const HwcDisplayConfig *new_config = GetConfig(config);
241 if (new_config == nullptr) {
242 ALOGE("Could not find active mode for %u", config);
243 return ConfigError::kBadConfig;
244 }
245
246 const HwcDisplayConfig *current_config = GetCurrentConfig();
247
248 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700249 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700250
251 std::optional<LayerData> modeset_layer_data;
252 // If a client layer has already been provided, and its size matches the
253 // new config, use it for the modeset.
254 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
255 current_config->mode.GetRawMode().hdisplay == width &&
256 current_config->mode.GetRawMode().vdisplay == height) {
257 ALOGV("Use existing client_layer for blocking config.");
258 modeset_layer_data = client_layer_.GetLayerData();
259 } else {
260 ALOGV("Allocate modeset buffer.");
261 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
262 if (modeset_buffer != nullptr) {
263 auto modeset_layer = std::make_unique<HwcLayer>(this);
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200264 HwcLayer::LayerProperties properties;
265 properties.buffer = {.buffer_handle = modeset_buffer};
266 properties.blend_mode = BufferBlendMode::kNone;
267 modeset_layer->SetLayerProperties(properties);
Drew Davenport97b5abc2024-11-07 10:43:54 -0700268 modeset_layer->PopulateLayerData();
269 modeset_layer_data = modeset_layer->GetLayerData();
270 GraphicBufferAllocator::get().free(modeset_buffer);
271 }
272 }
273
274 ALOGV("Create modeset commit.");
275 // Create atomic commit args for a blocking modeset. There's no need to do a
276 // separate test commit, since the commit does a test anyways.
277 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
278 modeset_layer_data);
279 commit_args.blocking = true;
280 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
281
282 if (ret) {
283 ALOGE("Blocking config failed: %d", ret);
284 return HwcDisplay::ConfigError::kBadConfig;
285 }
286
287 ALOGV("Blocking config succeeded.");
288 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700289 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700290 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
291 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700292 return ConfigError::kNone;
293}
294
Drew Davenport8998f8b2024-10-24 10:15:12 -0600295auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
296 bool seamless, QueuedConfigTiming *out_timing)
297 -> ConfigError {
298 if (configs_.hwc_configs.count(config) == 0) {
299 ALOGE("Could not find active mode for %u", config);
300 return ConfigError::kBadConfig;
301 }
302
303 // TODO: Add support for seamless configuration changes.
304 if (seamless) {
305 return ConfigError::kSeamlessNotAllowed;
306 }
307
308 // Request a refresh from the client one vsync period before the desired
309 // time, or simply at the desired time if there is no active configuration.
310 const HwcDisplayConfig *current_config = GetCurrentConfig();
311 out_timing->refresh_time_ns = desired_time -
312 (current_config
313 ? current_config->mode.GetVSyncPeriodNs()
314 : 0);
315 out_timing->new_vsync_time_ns = desired_time;
316
317 // Queue the config change timing to be consistent with the requested
318 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600319 staged_mode_change_time_ = out_timing->refresh_time_ns;
320 staged_mode_config_id_ = config;
321
322 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700323 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600324
325 return ConfigError::kNone;
326}
327
Drew Davenport7f356c82025-01-17 16:32:06 -0700328auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
329 if (IsInHeadlessMode()) {
330 return {};
331 }
332
333 /* In current drm_hwc design in case previous frame layer was not validated as
334 * a CLIENT, it is used by display controller (Front buffer). We have to store
335 * this state to provide the CLIENT with the release fences for such buffers.
336 */
337 for (auto &l : layers_) {
338 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
339 HWC2::Composition::Client);
340 }
341
342 // ValidateDisplay returns the number of layers that may be changed.
343 uint32_t num_types = 0;
344 uint32_t num_requests = 0;
345 backend_->ValidateDisplay(this, &num_types, &num_requests);
346
347 if (num_types == 0) {
348 return {};
349 }
350
351 // Iterate through the layers to find which layers actually changed.
352 std::vector<ChangedLayer> changed_layers;
353 for (auto &l : layers_) {
354 if (l.second.IsTypeChanged()) {
355 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
356 }
357 }
358 return changed_layers;
359}
360
Drew Davenportb864ccf2025-01-22 14:57:36 -0700361auto HwcDisplay::AcceptValidatedComposition() -> void {
362 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
363 l.second.AcceptTypeChange();
364 }
365}
366
367auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200368 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
369 -> bool {
370 int out_fd = -1;
371 auto error = PresentDisplay(&out_fd);
372 out_present_fence = MakeSharedFd(out_fd);
373 if (error != HWC2::Error::None) {
374 return false;
375 }
376
377 if (!out_present_fence) {
378 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700379 }
380
381 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200382 if (l.second.GetPriorBufferScanOutFlag()) {
383 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700384 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700385 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200386
387 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700388}
389
Roman Stratiienko63762a92023-09-18 22:33:45 +0300390void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200391 Deinit();
392
Roman Stratiienko63762a92023-09-18 22:33:45 +0300393 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200394
Roman Stratiienko63762a92023-09-18 22:33:45 +0300395 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200396 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000397 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200398 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000399 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200400 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200401}
402
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200403void HwcDisplay::Deinit() {
404 if (pipeline_ != nullptr) {
405 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200406 a_args.composition = std::make_shared<DrmKmsPlan>();
407 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300408 a_args.composition = {};
409 a_args.active = false;
410 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200411
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200412 current_plan_.reset();
413 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200414 if (flatcon_) {
415 flatcon_->StopThread();
416 flatcon_.reset();
417 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200418 }
419
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200420 if (vsync_worker_) {
421 vsync_worker_->StopThread();
422 vsync_worker_ = {};
423 }
424
Roman Stratiienko70ab9392025-01-23 12:11:48 +0200425 client_layer_.SwChainClearCache();
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200426}
427
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200428HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200429 ChosePreferredConfig();
430
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300431 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700432 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300433 if (!vsync_worker_) {
434 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
435 return HWC2::Error::BadDisplay;
436 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 }
438
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200439 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200440 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200441 if (ret) {
442 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
443 return HWC2::Error::BadDisplay;
444 }
Drew Davenport93443182023-12-14 09:25:45 +0000445 auto flatcbk = (struct FlatConCallbacks){
446 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200447 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200448 }
449
Roman Stratiienko44b95772025-01-22 18:03:36 +0200450 HwcLayer::LayerProperties lp;
451 lp.blend_mode = BufferBlendMode::kPreMult;
452 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200453
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400454 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200455
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200456 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200457}
458
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600459std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
460 if (IsInHeadlessMode()) {
461 // The pipeline can be nullptr in headless mode, so return the default
462 // "normal" mode.
463 return PanelOrientation::kModePanelOrientationNormal;
464 }
465
466 DrmDisplayPipeline &pipeline = GetPipe();
467 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
468 ALOGW(
469 "No display pipeline present to query the panel orientation property.");
470 return {};
471 }
472
473 return pipeline.connector->Get()->GetPanelOrientation();
474}
475
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200476HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200477 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300478 if (type_ == HWC2::DisplayType::Virtual) {
479 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
480 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200481 err = configs_.Update(*pipeline_->connector->Get());
482 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300483 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200484 }
485 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200486 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200487 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488
Roman Stratiienko0137f862022-01-04 18:27:40 +0200489 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490}
491
492HWC2::Error HwcDisplay::AcceptDisplayChanges() {
493 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
494 l.second.AcceptTypeChange();
495 return HWC2::Error::None;
496}
497
498HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200499 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 *layer = static_cast<hwc2_layer_t>(layer_idx_);
501 ++layer_idx_;
502 return HWC2::Error::None;
503}
504
505HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200506 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200508 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200509
510 layers_.erase(layer);
511 return HWC2::Error::None;
512}
513
514HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700515 // If a config has been queued, it is considered the "active" config.
516 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
517 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518 return HWC2::Error::BadConfig;
519
Drew Davenportfe70c802024-11-07 13:02:21 -0700520 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521 return HWC2::Error::None;
522}
523
524HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
525 hwc2_layer_t *layers,
526 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200527 if (IsInHeadlessMode()) {
528 *num_elements = 0;
529 return HWC2::Error::None;
530 }
531
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300533 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200534 if (l.second.IsTypeChanged()) {
535 if (layers && num_changes < *num_elements)
536 layers[num_changes] = l.first;
537 if (types && num_changes < *num_elements)
538 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
539 ++num_changes;
540 }
541 }
542 if (!layers && !types)
543 *num_elements = num_changes;
544 return HWC2::Error::None;
545}
546
547HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
548 int32_t /*format*/,
549 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200550 if (IsInHeadlessMode()) {
551 return HWC2::Error::None;
552 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200553
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300554 auto min = pipeline_->device->GetMinResolution();
555 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200556
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200557 if (width < min.first || height < min.second)
558 return HWC2::Error::Unsupported;
559
560 if (width > max.first || height > max.second)
561 return HWC2::Error::Unsupported;
562
563 if (dataspace != HAL_DATASPACE_UNKNOWN)
564 return HWC2::Error::Unsupported;
565
566 // TODO(nobody): Validate format can be handled by either GL or planes
567 return HWC2::Error::None;
568}
569
570HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500571 if (!modes) {
572 std::vector<Colormode> temp_modes;
573 GetEdid()->GetColorModes(temp_modes);
574 *num_modes = temp_modes.size();
575 return HWC2::Error::None;
576 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500578 std::vector<Colormode> temp_modes;
579 std::vector<int32_t> out_modes(modes, modes + *num_modes);
580 GetEdid()->GetColorModes(temp_modes);
581 if (temp_modes.empty()) {
582 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
583 return HWC2::Error::None;
584 }
585
586 for (auto &c : temp_modes)
587 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200588
589 return HWC2::Error::None;
590}
591
592HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
593 int32_t attribute_in,
594 int32_t *value) {
595 int conf = static_cast<int>(config);
596
Roman Stratiienko0137f862022-01-04 18:27:40 +0200597 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200598 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 return HWC2::Error::BadConfig;
600 }
601
Roman Stratiienko0137f862022-01-04 18:27:40 +0200602 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200603
604 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300605 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
607 switch (attribute) {
608 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200609 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200610 break;
611 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200612 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200613 break;
614 case HWC2::Attribute::VsyncPeriod:
615 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600616 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200617 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000618 case HWC2::Attribute::DpiY:
619 // ideally this should be vdisplay/mm_heigth, however mm_height
620 // comes from edid parsing and is highly unreliable. Viewing the
621 // rarity of anisotropic displays, falling back to a single value
622 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200623 case HWC2::Attribute::DpiX:
624 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200625 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
626 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200627 : -1;
628 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200629#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200630 case HWC2::Attribute::ConfigGroup:
631 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
632 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200633 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200634 break;
635#endif
636 default:
637 *value = -1;
638 return HWC2::Error::BadConfig;
639 }
640 return HWC2::Error::None;
641}
642
Drew Davenportf7e88332024-09-06 12:54:38 -0600643HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
644 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200646 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200647 if (hwc_config.second.disabled) {
648 continue;
649 }
650
651 if (configs != nullptr) {
652 if (idx >= *num_configs) {
653 break;
654 }
655 configs[idx] = hwc_config.second.id;
656 }
657
658 idx++;
659 }
660 *num_configs = idx;
661 return HWC2::Error::None;
662}
663
664HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
665 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200666 if (IsInHeadlessMode()) {
667 stream << "null-display";
668 } else {
669 stream << "display-" << GetPipe().connector->Get()->GetId();
670 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300671 auto string = stream.str();
672 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200673 if (!name) {
674 *size = length;
675 return HWC2::Error::None;
676 }
677
678 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
679 strncpy(name, string.c_str(), *size);
680 return HWC2::Error::None;
681}
682
683HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
684 uint32_t *num_elements,
685 hwc2_layer_t * /*layers*/,
686 int32_t * /*layer_requests*/) {
687 // TODO(nobody): I think virtual display should request
688 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
689 *num_elements = 0;
690 return HWC2::Error::None;
691}
692
693HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
694 *type = static_cast<int32_t>(type_);
695 return HWC2::Error::None;
696}
697
698HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
699 *support = 0;
700 return HWC2::Error::None;
701}
702
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500703HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
704 float *max_luminance,
705 float *max_average_luminance,
706 float *min_luminance) {
707 if (!types) {
708 std::vector<ui::Hdr> temp_types;
709 float lums[3] = {0.F};
710 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
711 *num_types = temp_types.size();
712 return HWC2::Error::None;
713 }
714
715 std::vector<ui::Hdr> temp_types;
716 std::vector<int32_t> out_types(types, types + *num_types);
717 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
718 max_average_luminance, min_luminance);
719 for (auto &t : temp_types) {
720 switch (t) {
721 case ui::Hdr::HDR10:
722 out_types.emplace_back(HAL_HDR_HDR10);
723 break;
724 case ui::Hdr::HLG:
725 out_types.emplace_back(HAL_HDR_HLG);
726 break;
727 default:
728 // Ignore any other HDR types
729 break;
730 }
731 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200732 return HWC2::Error::None;
733}
734
735/* Find API details at:
736 * 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 +0300737 *
738 * Called after PresentDisplay(), CLIENT is expecting release fence for the
739 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200740 */
741HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
742 hwc2_layer_t *layers,
743 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200744 if (IsInHeadlessMode()) {
745 *num_elements = 0;
746 return HWC2::Error::None;
747 }
748
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200749 uint32_t num_layers = 0;
750
Roman Stratiienkodd214942022-05-03 18:24:49 +0300751 for (auto &l : layers_) {
752 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
753 continue;
754 }
755
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200756 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300757
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200758 if (layers == nullptr || fences == nullptr)
759 continue;
760
761 if (num_layers > *num_elements) {
762 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
763 return HWC2::Error::None;
764 }
765
766 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200767 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200768 }
769 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300770
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200771 return HWC2::Error::None;
772}
773
Drew Davenport97b5abc2024-11-07 10:43:54 -0700774AtomicCommitArgs HwcDisplay::CreateModesetCommit(
775 const HwcDisplayConfig *config,
776 const std::optional<LayerData> &modeset_layer) {
777 AtomicCommitArgs args{};
778
779 args.color_matrix = color_matrix_;
780 args.content_type = content_type_;
781 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500782 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700783
784 std::vector<LayerData> composition_layers;
785 if (modeset_layer) {
786 composition_layers.emplace_back(modeset_layer.value());
787 }
788
789 if (composition_layers.empty()) {
790 ALOGW("Attempting to create a modeset commit without a layer.");
791 }
792
793 args.display_mode = config->mode;
794 args.active = true;
795 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
796 std::move(
797 composition_layers));
798 ALOGW_IF(!args.composition, "No composition for blocking modeset");
799
800 return args;
801}
802
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200803// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200804HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200805 if (IsInHeadlessMode()) {
806 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
807 return HWC2::Error::None;
808 }
809
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200810 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400811 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400812 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500813 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200814
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200815 uint32_t prev_vperiod_ns = 0;
816 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200817
Drew Davenportd387c842024-12-16 16:57:24 -0700818 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700819 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200820 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700821 const HwcDisplayConfig *staged_config = GetConfig(
822 staged_mode_config_id_.value());
823 if (staged_config == nullptr) {
824 return HWC2::Error::BadConfig;
825 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200826
Drew Davenportfe70c802024-11-07 13:02:21 -0700827 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700828 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200829 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700830 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200831 }
832 }
833
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200834 // order the layers by z-order
835 bool use_client_layer = false;
836 uint32_t client_z_order = UINT32_MAX;
837 std::map<uint32_t, HwcLayer *> z_map;
838 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
839 switch (l.second.GetValidatedType()) {
840 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300841 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200842 break;
843 case HWC2::Composition::Client:
844 // Place it at the z_order of the lowest client layer
845 use_client_layer = true;
846 client_z_order = std::min(client_z_order, l.second.GetZOrder());
847 break;
848 default:
849 continue;
850 }
851 }
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200852 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300853 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200854
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200855 client_layer_.PopulateLayerData();
856 if (!client_layer_.IsLayerUsableAsDevice()) {
857 ALOGE_IF(!a_args.test_only,
858 "Client layer must be always usable by DRM/KMS");
859 /* This may be normally triggered on validation of the first frame
860 * containing CLIENT layer. At this moment client buffer is not yet
861 * provided by the CLIENT.
862 * This may be triggered once in HwcLayer lifecycle in case FB can't be
863 * imported. For example when non-contiguous buffer is imported into
864 * contiguous-only DRM/KMS driver.
865 */
866 return HWC2::Error::BadLayer;
867 }
868 }
869
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200870 if (z_map.empty())
871 return HWC2::Error::BadLayer;
872
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200873 std::vector<LayerData> composition_layers;
874
875 /* Import & populate */
876 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200877 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200878 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200879
880 // now that they're ordered by z, add them to the composition
881 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200882 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200883 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200884 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200885 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200886 }
887
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200888 /* Store plan to ensure shared planes won't be stolen by other display
889 * in between of ValidateDisplay() and PresentDisplay() calls
890 */
891 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
892 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300893
894 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200895 writeback_layer_->PopulateLayerData();
896 if (!writeback_layer_->IsLayerUsableAsDevice()) {
897 ALOGE("Output layer must be always usable by DRM/KMS");
898 return HWC2::Error::BadLayer;
899 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300900 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
901 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
902 .acquire_fence;
903 }
904
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200905 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700906 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907 return HWC2::Error::BadConfig;
908 }
909
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200910 a_args.composition = current_plan_;
911
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300912 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200913
914 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700915 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200916 return HWC2::Error::BadParameter;
917 }
918
Drew Davenportd387c842024-12-16 16:57:24 -0700919 if (new_vsync_period_ns) {
920 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700921 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700922
923 vsync_worker_->SetVsyncTimestampTracking(false);
924 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
925 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000926 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700927 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000928 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200929 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200930 }
931
932 return HWC2::Error::None;
933}
934
935/* Find API details at:
936 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
937 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300938HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200939 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300940 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200941 return HWC2::Error::None;
942 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200943 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200944
945 ++total_stats_.total_frames_;
946
947 AtomicCommitArgs a_args{};
948 ret = CreateComposition(a_args);
949
950 if (ret != HWC2::Error::None)
951 ++total_stats_.failed_kms_present_;
952
953 if (ret == HWC2::Error::BadLayer) {
954 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300955 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200956 return HWC2::Error::None;
957 }
958 if (ret != HWC2::Error::None)
959 return ret;
960
Roman Stratiienko76892782023-01-16 17:15:53 +0200961 this->present_fence_ = a_args.out_fence;
962 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200963
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200964 // Reset the color matrix so we don't apply it over and over again.
965 color_matrix_ = {};
966
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200967 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700968
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200969 return HWC2::Error::None;
970}
971
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200972HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
973 int64_t change_time) {
974 if (configs_.hwc_configs.count(config) == 0) {
975 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200976 return HWC2::Error::BadConfig;
977 }
978
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200979 staged_mode_change_time_ = change_time;
980 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981
982 return HWC2::Error::None;
983}
984
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200985HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
986 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
987}
988
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200989HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400990 /* Maps to the Colorspace DRM connector property:
991 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
992 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500993 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200994 return HWC2::Error::BadParameter;
995
Sasha McIntosh5294f092024-09-18 18:14:54 -0400996 switch (mode) {
997 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500998 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -0400999 colorspace_ = Colorspace::kDefault;
1000 break;
1001 case HAL_COLOR_MODE_STANDARD_BT601_625:
1002 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1003 case HAL_COLOR_MODE_STANDARD_BT601_525:
1004 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001005 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001006 // The DP spec does not say whether this is the 525 or the 625 line version.
1007 colorspace_ = Colorspace::kBt601Ycc;
1008 break;
1009 case HAL_COLOR_MODE_STANDARD_BT709:
1010 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001011 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001012 colorspace_ = Colorspace::kBt709Ycc;
1013 break;
1014 case HAL_COLOR_MODE_DCI_P3:
1015 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001016 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001017 colorspace_ = Colorspace::kDciP3RgbD65;
1018 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001019 case HAL_COLOR_MODE_DISPLAY_BT2020: {
1020 std::vector<ui::Hdr> hdr_types;
1021 GetEdid()->GetSupportedHdrTypes(hdr_types);
1022 if (!hdr_types.empty()) {
1023 auto ret = SetHdrOutputMetadata(hdr_types.front());
1024 if (ret != HWC2::Error::None)
1025 return ret;
1026 }
1027 colorspace_ = Colorspace::kBt2020Rgb;
1028 break;
1029 }
Sasha McIntosh5294f092024-09-18 18:14:54 -04001030 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001031 case HAL_COLOR_MODE_BT2020:
1032 case HAL_COLOR_MODE_BT2100_PQ:
1033 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001034 default:
1035 return HWC2::Error::Unsupported;
1036 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001037
1038 color_mode_ = mode;
1039 return HWC2::Error::None;
1040}
1041
1042HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1043 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1044 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1045 return HWC2::Error::BadParameter;
1046
1047 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1048 return HWC2::Error::BadParameter;
1049
1050 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001051
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001052 if (IsInHeadlessMode())
1053 return HWC2::Error::None;
1054
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001055 if (!GetPipe().crtc->Get()->GetCtmProperty())
1056 return HWC2::Error::None;
1057
1058 switch (color_transform_hint_) {
1059 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001060 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001061 break;
1062 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001063 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001064 {
1065 for (int i = 12; i < 14; i++) {
1066 if (matrix[i] != 0.F)
1067 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001068 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001069 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1070 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1071 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001072 }
1073 break;
1074 default:
1075 return HWC2::Error::Unsupported;
1076 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001077
1078 return HWC2::Error::None;
1079}
1080
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001081bool HwcDisplay::CtmByGpu() {
1082 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1083 return false;
1084
1085 if (GetPipe().crtc->Get()->GetCtmProperty())
1086 return false;
1087
Drew Davenport93443182023-12-14 09:25:45 +00001088 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001089 return false;
1090
1091 return true;
1092}
1093
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001094HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1095 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001096
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001097 AtomicCommitArgs a_args{};
1098
1099 switch (mode) {
1100 case HWC2::PowerMode::Off:
1101 a_args.active = false;
1102 break;
1103 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001104 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001105 break;
1106 case HWC2::PowerMode::Doze:
1107 case HWC2::PowerMode::DozeSuspend:
1108 return HWC2::Error::Unsupported;
1109 default:
John Stultzffe783c2024-02-14 10:51:27 -08001110 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001111 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001112 }
1113
1114 if (IsInHeadlessMode()) {
1115 return HWC2::Error::None;
1116 }
1117
Jia Ren80566fe2022-11-17 17:26:00 +08001118 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001119 /*
1120 * Setting the display to active before we have a composition
1121 * can break some drivers, so skip setting a_args.active to
1122 * true, as the next composition frame will implicitly activate
1123 * the display
1124 */
1125 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1126 ? HWC2::Error::None
1127 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001128 };
1129
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001130 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001131 if (err) {
1132 ALOGE("Failed to apply the dpms composition err=%d", err);
1133 return HWC2::Error::BadParameter;
1134 }
1135 return HWC2::Error::None;
1136}
1137
1138HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001139 if (type_ == HWC2::DisplayType::Virtual) {
1140 return HWC2::Error::None;
1141 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001142 if (!vsync_worker_) {
1143 return HWC2::Error::NoResources;
1144 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001145
Roman Stratiienko099c3112022-01-20 11:50:54 +02001146 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001147 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001148 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001149 DrmHwc *hwc = hwc_;
1150 hwc2_display_t id = handle_;
1151 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001152 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001153 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1154 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001155 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001156 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001157 return HWC2::Error::None;
1158}
1159
1160HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1161 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001162 if (IsInHeadlessMode()) {
1163 *num_types = *num_requests = 0;
1164 return HWC2::Error::None;
1165 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001166
1167 /* In current drm_hwc design in case previous frame layer was not validated as
1168 * a CLIENT, it is used by display controller (Front buffer). We have to store
1169 * this state to provide the CLIENT with the release fences for such buffers.
1170 */
1171 for (auto &l : layers_) {
1172 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1173 HWC2::Composition::Client);
1174 }
1175
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001176 return backend_->ValidateDisplay(this, num_types, num_requests);
1177}
1178
1179std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1180 std::vector<HwcLayer *> ordered_layers;
1181 ordered_layers.reserve(layers_.size());
1182
1183 for (auto &[handle, layer] : layers_) {
1184 ordered_layers.emplace_back(&layer);
1185 }
1186
1187 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1188 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1189 return lhs->GetZOrder() < rhs->GetZOrder();
1190 });
1191
1192 return ordered_layers;
1193}
1194
Roman Stratiienko099c3112022-01-20 11:50:54 +02001195HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1196 uint32_t *outVsyncPeriod /* ns */) {
1197 return GetDisplayAttribute(configs_.active_config_id,
1198 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1199 (int32_t *)(outVsyncPeriod));
1200}
1201
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001202// Display primary values are coded as unsigned 16-bit values in units of
1203// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1204static uint64_t ToU16ColorValue(float in) {
1205 constexpr float kPrimariesFixedPoint = 50000.F;
1206 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1207}
1208
1209HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1210 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1211 hdr_metadata_->metadata_type = 0;
1212 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1213 m->metadata_type = 0;
1214
1215 switch (type) {
1216 case ui::Hdr::HDR10:
1217 m->eotf = 2; // PQ
1218 break;
1219 case ui::Hdr::HLG:
1220 m->eotf = 3; // HLG
1221 break;
1222 default:
1223 return HWC2::Error::Unsupported;
1224 }
1225
1226 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1227 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1228 std::vector<ui::Hdr> types;
1229 float hdr_luminance[3]{0.F, 0.F, 0.F};
1230 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1231 &hdr_luminance[2]);
1232 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1233 hdr_luminance[0]);
1234 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1235 // The min luminance value is coded as an unsigned 16-bit value in units of
1236 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1237 // represents 6.5535 cd/m2.
1238 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1239 10000.F);
1240
1241 auto gamut = ColorGamut::BT2020();
1242 auto primaries = gamut.getPrimaries();
1243 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1244 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1245 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1246 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1247 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1248 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1249
1250 auto whitePoint = gamut.getWhitePoint();
1251 m->white_point.x = ToU16ColorValue(whitePoint.x);
1252 m->white_point.y = ToU16ColorValue(whitePoint.y);
1253
1254 return HWC2::Error::None;
1255}
1256
Roman Stratiienko6b405052022-12-10 19:09:10 +02001257#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001258HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001259 if (IsInHeadlessMode()) {
1260 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1261 return HWC2::Error::None;
1262 }
1263 /* Primary display should be always internal,
1264 * otherwise SF will be unhappy and will crash
1265 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001266 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001267 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001268 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001269 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1270 else
1271 return HWC2::Error::BadConfig;
1272
1273 return HWC2::Error::None;
1274}
1275
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001276HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001277 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001278 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1279 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001280 if (type_ == HWC2::DisplayType::Virtual) {
1281 return HWC2::Error::None;
1282 }
1283
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001284 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1285 return HWC2::Error::BadParameter;
1286 }
1287
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001288 uint32_t current_vsync_period{};
1289 GetDisplayVsyncPeriod(&current_vsync_period);
1290
1291 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1292 return HWC2::Error::SeamlessNotAllowed;
1293 }
1294
1295 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1296 ->desiredTimeNanos -
1297 current_vsync_period;
1298 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1299 if (ret != HWC2::Error::None) {
1300 return ret;
1301 }
1302
1303 outTimeline->refreshRequired = true;
1304 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1305 ->desiredTimeNanos;
1306
Drew Davenport33121b72024-12-13 14:59:35 -07001307 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001308
1309 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001310}
1311
1312HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1313 return HWC2::Error::Unsupported;
1314}
1315
1316HWC2::Error HwcDisplay::GetSupportedContentTypes(
1317 uint32_t *outNumSupportedContentTypes,
1318 const uint32_t *outSupportedContentTypes) {
1319 if (outSupportedContentTypes == nullptr)
1320 *outNumSupportedContentTypes = 0;
1321
1322 return HWC2::Error::None;
1323}
1324
1325HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001326 /* Maps exactly to the content_type DRM connector property:
1327 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001328 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001329 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1330 return HWC2::Error::BadParameter;
1331
1332 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001333
1334 return HWC2::Error::None;
1335}
1336#endif
1337
Roman Stratiienko6b405052022-12-10 19:09:10 +02001338#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001339HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1340 uint32_t *outDataSize,
1341 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001342 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001343 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001344 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001345
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001346 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001347 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001348 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001349 }
1350
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001351 *outPort = handle_; /* TDOD(nobody): What should be here? */
1352
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001353 if (outData) {
1354 *outDataSize = std::min(*outDataSize, blob->length);
1355 memcpy(outData, blob->data, *outDataSize);
1356 } else {
1357 *outDataSize = blob->length;
1358 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001359
1360 return HWC2::Error::None;
1361}
1362
1363HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001364 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001365 if (outNumCapabilities == nullptr) {
1366 return HWC2::Error::BadParameter;
1367 }
1368
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001369 bool skip_ctm = false;
1370
1371 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001372 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001373 skip_ctm = true;
1374
1375 // Skip client CTM if DRM can handle it
1376 if (!skip_ctm && !IsInHeadlessMode() &&
1377 GetPipe().crtc->Get()->GetCtmProperty())
1378 skip_ctm = true;
1379
1380 if (!skip_ctm) {
1381 *outNumCapabilities = 0;
1382 return HWC2::Error::None;
1383 }
1384
1385 *outNumCapabilities = 1;
1386 if (outCapabilities) {
1387 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1388 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001389
1390 return HWC2::Error::None;
1391}
1392
1393HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1394 *supported = false;
1395 return HWC2::Error::None;
1396}
1397
1398HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1399 return HWC2::Error::Unsupported;
1400}
1401
Roman Stratiienko6b405052022-12-10 19:09:10 +02001402#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001403
Roman Stratiienko6b405052022-12-10 19:09:10 +02001404#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001405
1406HWC2::Error HwcDisplay::GetRenderIntents(
1407 int32_t mode, uint32_t *outNumIntents,
1408 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1409 if (mode != HAL_COLOR_MODE_NATIVE) {
1410 return HWC2::Error::BadParameter;
1411 }
1412
1413 if (outIntents == nullptr) {
1414 *outNumIntents = 1;
1415 return HWC2::Error::None;
1416 }
1417 *outNumIntents = 1;
1418 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1419 return HWC2::Error::None;
1420}
1421
1422HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1423 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1424 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1425 return HWC2::Error::BadParameter;
1426
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001427 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1428 return HWC2::Error::Unsupported;
1429
Sasha McIntosh5294f092024-09-18 18:14:54 -04001430 auto err = SetColorMode(mode);
1431 if (err != HWC2::Error::None) return err;
1432
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001433 return HWC2::Error::None;
1434}
1435
Roman Stratiienko6b405052022-12-10 19:09:10 +02001436#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001437
1438const Backend *HwcDisplay::backend() const {
1439 return backend_.get();
1440}
1441
1442void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1443 backend_ = std::move(backend);
1444}
1445
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001446} // namespace android