blob: 252276f36dd622f3a67fb721c1e201f4205c8d6e [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;
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200265 auto bi = BufferInfoGetter::GetInstance()->GetBoInfo(modeset_buffer);
266 if (!bi) {
267 ALOGE("Failed to get buffer info for modeset buffer.");
268 return ConfigError::kBadConfig;
269 }
270 properties.slot_buffer = {
271 .slot_id = 0,
272 .bi = bi,
273 };
274 properties.active_slot = {
275 .slot_id = 0,
276 .fence = {},
277 };
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200278 properties.blend_mode = BufferBlendMode::kNone;
279 modeset_layer->SetLayerProperties(properties);
Drew Davenport97b5abc2024-11-07 10:43:54 -0700280 modeset_layer->PopulateLayerData();
281 modeset_layer_data = modeset_layer->GetLayerData();
282 GraphicBufferAllocator::get().free(modeset_buffer);
283 }
284 }
285
286 ALOGV("Create modeset commit.");
287 // Create atomic commit args for a blocking modeset. There's no need to do a
288 // separate test commit, since the commit does a test anyways.
289 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
290 modeset_layer_data);
291 commit_args.blocking = true;
292 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
293
294 if (ret) {
295 ALOGE("Blocking config failed: %d", ret);
296 return HwcDisplay::ConfigError::kBadConfig;
297 }
298
299 ALOGV("Blocking config succeeded.");
300 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700301 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700302 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
303 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700304 return ConfigError::kNone;
305}
306
Drew Davenport8998f8b2024-10-24 10:15:12 -0600307auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
308 bool seamless, QueuedConfigTiming *out_timing)
309 -> ConfigError {
310 if (configs_.hwc_configs.count(config) == 0) {
311 ALOGE("Could not find active mode for %u", config);
312 return ConfigError::kBadConfig;
313 }
314
315 // TODO: Add support for seamless configuration changes.
316 if (seamless) {
317 return ConfigError::kSeamlessNotAllowed;
318 }
319
320 // Request a refresh from the client one vsync period before the desired
321 // time, or simply at the desired time if there is no active configuration.
322 const HwcDisplayConfig *current_config = GetCurrentConfig();
323 out_timing->refresh_time_ns = desired_time -
324 (current_config
325 ? current_config->mode.GetVSyncPeriodNs()
326 : 0);
327 out_timing->new_vsync_time_ns = desired_time;
328
329 // Queue the config change timing to be consistent with the requested
330 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600331 staged_mode_change_time_ = out_timing->refresh_time_ns;
332 staged_mode_config_id_ = config;
333
334 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700335 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600336
337 return ConfigError::kNone;
338}
339
Drew Davenport7f356c82025-01-17 16:32:06 -0700340auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
341 if (IsInHeadlessMode()) {
342 return {};
343 }
344
345 /* In current drm_hwc design in case previous frame layer was not validated as
346 * a CLIENT, it is used by display controller (Front buffer). We have to store
347 * this state to provide the CLIENT with the release fences for such buffers.
348 */
349 for (auto &l : layers_) {
350 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
351 HWC2::Composition::Client);
352 }
353
354 // ValidateDisplay returns the number of layers that may be changed.
355 uint32_t num_types = 0;
356 uint32_t num_requests = 0;
357 backend_->ValidateDisplay(this, &num_types, &num_requests);
358
359 if (num_types == 0) {
360 return {};
361 }
362
363 // Iterate through the layers to find which layers actually changed.
364 std::vector<ChangedLayer> changed_layers;
365 for (auto &l : layers_) {
366 if (l.second.IsTypeChanged()) {
367 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
368 }
369 }
370 return changed_layers;
371}
372
Drew Davenportb864ccf2025-01-22 14:57:36 -0700373auto HwcDisplay::AcceptValidatedComposition() -> void {
374 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
375 l.second.AcceptTypeChange();
376 }
377}
378
379auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200380 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
381 -> bool {
382 int out_fd = -1;
383 auto error = PresentDisplay(&out_fd);
384 out_present_fence = MakeSharedFd(out_fd);
385 if (error != HWC2::Error::None) {
386 return false;
387 }
388
389 if (!out_present_fence) {
390 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700391 }
392
393 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200394 if (l.second.GetPriorBufferScanOutFlag()) {
395 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700396 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700397 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200398
399 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700400}
401
Roman Stratiienko63762a92023-09-18 22:33:45 +0300402void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200403 Deinit();
404
Roman Stratiienko63762a92023-09-18 22:33:45 +0300405 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200406
Roman Stratiienko63762a92023-09-18 22:33:45 +0300407 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200408 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000409 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200410 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000411 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200412 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200413}
414
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200415void HwcDisplay::Deinit() {
416 if (pipeline_ != nullptr) {
417 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200418 a_args.composition = std::make_shared<DrmKmsPlan>();
419 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300420 a_args.composition = {};
421 a_args.active = false;
422 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200423
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200424 current_plan_.reset();
425 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200426 if (flatcon_) {
427 flatcon_->StopThread();
428 flatcon_.reset();
429 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200430 }
431
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200432 if (vsync_worker_) {
433 vsync_worker_->StopThread();
434 vsync_worker_ = {};
435 }
436
Roman Stratiienko7c8cc4e2025-01-25 22:41:53 +0200437 client_layer_.ClearSlots();
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200438}
439
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200440HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200441 ChosePreferredConfig();
442
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300443 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700444 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300445 if (!vsync_worker_) {
446 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
447 return HWC2::Error::BadDisplay;
448 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200449 }
450
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200451 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200452 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200453 if (ret) {
454 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
455 return HWC2::Error::BadDisplay;
456 }
Drew Davenport93443182023-12-14 09:25:45 +0000457 auto flatcbk = (struct FlatConCallbacks){
458 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200459 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200460 }
461
Roman Stratiienko44b95772025-01-22 18:03:36 +0200462 HwcLayer::LayerProperties lp;
463 lp.blend_mode = BufferBlendMode::kPreMult;
464 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200465
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400466 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200467
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200468 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200469}
470
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600471std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
472 if (IsInHeadlessMode()) {
473 // The pipeline can be nullptr in headless mode, so return the default
474 // "normal" mode.
475 return PanelOrientation::kModePanelOrientationNormal;
476 }
477
478 DrmDisplayPipeline &pipeline = GetPipe();
479 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
480 ALOGW(
481 "No display pipeline present to query the panel orientation property.");
482 return {};
483 }
484
485 return pipeline.connector->Get()->GetPanelOrientation();
486}
487
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200489 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300490 if (type_ == HWC2::DisplayType::Virtual) {
491 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
492 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200493 err = configs_.Update(*pipeline_->connector->Get());
494 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300495 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200496 }
497 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200498 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200499 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500
Roman Stratiienko0137f862022-01-04 18:27:40 +0200501 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502}
503
504HWC2::Error HwcDisplay::AcceptDisplayChanges() {
505 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
506 l.second.AcceptTypeChange();
507 return HWC2::Error::None;
508}
509
510HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200511 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200512 *layer = static_cast<hwc2_layer_t>(layer_idx_);
513 ++layer_idx_;
514 return HWC2::Error::None;
515}
516
517HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200518 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200519 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200520 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521
522 layers_.erase(layer);
523 return HWC2::Error::None;
524}
525
526HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700527 // If a config has been queued, it is considered the "active" config.
528 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
529 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200530 return HWC2::Error::BadConfig;
531
Drew Davenportfe70c802024-11-07 13:02:21 -0700532 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533 return HWC2::Error::None;
534}
535
536HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
537 hwc2_layer_t *layers,
538 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200539 if (IsInHeadlessMode()) {
540 *num_elements = 0;
541 return HWC2::Error::None;
542 }
543
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200544 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300545 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546 if (l.second.IsTypeChanged()) {
547 if (layers && num_changes < *num_elements)
548 layers[num_changes] = l.first;
549 if (types && num_changes < *num_elements)
550 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
551 ++num_changes;
552 }
553 }
554 if (!layers && !types)
555 *num_elements = num_changes;
556 return HWC2::Error::None;
557}
558
559HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
560 int32_t /*format*/,
561 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200562 if (IsInHeadlessMode()) {
563 return HWC2::Error::None;
564 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200565
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300566 auto min = pipeline_->device->GetMinResolution();
567 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200568
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200569 if (width < min.first || height < min.second)
570 return HWC2::Error::Unsupported;
571
572 if (width > max.first || height > max.second)
573 return HWC2::Error::Unsupported;
574
575 if (dataspace != HAL_DATASPACE_UNKNOWN)
576 return HWC2::Error::Unsupported;
577
578 // TODO(nobody): Validate format can be handled by either GL or planes
579 return HWC2::Error::None;
580}
581
582HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500583 if (!modes) {
584 std::vector<Colormode> temp_modes;
585 GetEdid()->GetColorModes(temp_modes);
586 *num_modes = temp_modes.size();
587 return HWC2::Error::None;
588 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200589
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500590 std::vector<Colormode> temp_modes;
591 std::vector<int32_t> out_modes(modes, modes + *num_modes);
592 GetEdid()->GetColorModes(temp_modes);
593 if (temp_modes.empty()) {
594 out_modes.emplace_back(HAL_COLOR_MODE_NATIVE);
595 return HWC2::Error::None;
596 }
597
598 for (auto &c : temp_modes)
599 out_modes.emplace_back(static_cast<int32_t>(c));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200600
601 return HWC2::Error::None;
602}
603
604HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
605 int32_t attribute_in,
606 int32_t *value) {
607 int conf = static_cast<int>(config);
608
Roman Stratiienko0137f862022-01-04 18:27:40 +0200609 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200610 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200611 return HWC2::Error::BadConfig;
612 }
613
Roman Stratiienko0137f862022-01-04 18:27:40 +0200614 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200615
616 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300617 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
619 switch (attribute) {
620 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200621 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622 break;
623 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200624 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200625 break;
626 case HWC2::Attribute::VsyncPeriod:
627 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600628 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200629 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000630 case HWC2::Attribute::DpiY:
631 // ideally this should be vdisplay/mm_heigth, however mm_height
632 // comes from edid parsing and is highly unreliable. Viewing the
633 // rarity of anisotropic displays, falling back to a single value
634 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 case HWC2::Attribute::DpiX:
636 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200637 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
638 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200639 : -1;
640 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200641#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200642 case HWC2::Attribute::ConfigGroup:
643 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
644 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200645 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200646 break;
647#endif
648 default:
649 *value = -1;
650 return HWC2::Error::BadConfig;
651 }
652 return HWC2::Error::None;
653}
654
Drew Davenportf7e88332024-09-06 12:54:38 -0600655HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
656 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200657 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200658 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200659 if (hwc_config.second.disabled) {
660 continue;
661 }
662
663 if (configs != nullptr) {
664 if (idx >= *num_configs) {
665 break;
666 }
667 configs[idx] = hwc_config.second.id;
668 }
669
670 idx++;
671 }
672 *num_configs = idx;
673 return HWC2::Error::None;
674}
675
676HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
677 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200678 if (IsInHeadlessMode()) {
679 stream << "null-display";
680 } else {
681 stream << "display-" << GetPipe().connector->Get()->GetId();
682 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300683 auto string = stream.str();
684 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200685 if (!name) {
686 *size = length;
687 return HWC2::Error::None;
688 }
689
690 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
691 strncpy(name, string.c_str(), *size);
692 return HWC2::Error::None;
693}
694
695HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
696 uint32_t *num_elements,
697 hwc2_layer_t * /*layers*/,
698 int32_t * /*layer_requests*/) {
699 // TODO(nobody): I think virtual display should request
700 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
701 *num_elements = 0;
702 return HWC2::Error::None;
703}
704
705HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
706 *type = static_cast<int32_t>(type_);
707 return HWC2::Error::None;
708}
709
710HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
711 *support = 0;
712 return HWC2::Error::None;
713}
714
Sasha McIntosh851ea4d2024-12-04 17:14:55 -0500715HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types, int32_t *types,
716 float *max_luminance,
717 float *max_average_luminance,
718 float *min_luminance) {
719 if (!types) {
720 std::vector<ui::Hdr> temp_types;
721 float lums[3] = {0.F};
722 GetEdid()->GetHdrCapabilities(temp_types, &lums[0], &lums[1], &lums[2]);
723 *num_types = temp_types.size();
724 return HWC2::Error::None;
725 }
726
727 std::vector<ui::Hdr> temp_types;
728 std::vector<int32_t> out_types(types, types + *num_types);
729 GetEdid()->GetHdrCapabilities(temp_types, max_luminance,
730 max_average_luminance, min_luminance);
731 for (auto &t : temp_types) {
732 switch (t) {
733 case ui::Hdr::HDR10:
734 out_types.emplace_back(HAL_HDR_HDR10);
735 break;
736 case ui::Hdr::HLG:
737 out_types.emplace_back(HAL_HDR_HLG);
738 break;
739 default:
740 // Ignore any other HDR types
741 break;
742 }
743 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200744 return HWC2::Error::None;
745}
746
747/* Find API details at:
748 * 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 +0300749 *
750 * Called after PresentDisplay(), CLIENT is expecting release fence for the
751 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200752 */
753HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
754 hwc2_layer_t *layers,
755 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200756 if (IsInHeadlessMode()) {
757 *num_elements = 0;
758 return HWC2::Error::None;
759 }
760
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200761 uint32_t num_layers = 0;
762
Roman Stratiienkodd214942022-05-03 18:24:49 +0300763 for (auto &l : layers_) {
764 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
765 continue;
766 }
767
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200768 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300769
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200770 if (layers == nullptr || fences == nullptr)
771 continue;
772
773 if (num_layers > *num_elements) {
774 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
775 return HWC2::Error::None;
776 }
777
778 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200779 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200780 }
781 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300782
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200783 return HWC2::Error::None;
784}
785
Drew Davenport97b5abc2024-11-07 10:43:54 -0700786AtomicCommitArgs HwcDisplay::CreateModesetCommit(
787 const HwcDisplayConfig *config,
788 const std::optional<LayerData> &modeset_layer) {
789 AtomicCommitArgs args{};
790
791 args.color_matrix = color_matrix_;
792 args.content_type = content_type_;
793 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500794 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700795
796 std::vector<LayerData> composition_layers;
797 if (modeset_layer) {
798 composition_layers.emplace_back(modeset_layer.value());
799 }
800
801 if (composition_layers.empty()) {
802 ALOGW("Attempting to create a modeset commit without a layer.");
803 }
804
805 args.display_mode = config->mode;
806 args.active = true;
807 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
808 std::move(
809 composition_layers));
810 ALOGW_IF(!args.composition, "No composition for blocking modeset");
811
812 return args;
813}
814
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200815// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200816HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200817 if (IsInHeadlessMode()) {
818 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
819 return HWC2::Error::None;
820 }
821
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200822 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400823 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400824 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500825 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200826
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200827 uint32_t prev_vperiod_ns = 0;
828 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200829
Drew Davenportd387c842024-12-16 16:57:24 -0700830 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700831 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200832 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700833 const HwcDisplayConfig *staged_config = GetConfig(
834 staged_mode_config_id_.value());
835 if (staged_config == nullptr) {
836 return HWC2::Error::BadConfig;
837 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200838
Drew Davenportfe70c802024-11-07 13:02:21 -0700839 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700840 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200841 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700842 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200843 }
844 }
845
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200846 // order the layers by z-order
847 bool use_client_layer = false;
848 uint32_t client_z_order = UINT32_MAX;
849 std::map<uint32_t, HwcLayer *> z_map;
850 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
851 switch (l.second.GetValidatedType()) {
852 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300853 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200854 break;
855 case HWC2::Composition::Client:
856 // Place it at the z_order of the lowest client layer
857 use_client_layer = true;
858 client_z_order = std::min(client_z_order, l.second.GetZOrder());
859 break;
860 default:
861 continue;
862 }
863 }
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200864 if (use_client_layer) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300865 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200866
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200867 client_layer_.PopulateLayerData();
868 if (!client_layer_.IsLayerUsableAsDevice()) {
869 ALOGE_IF(!a_args.test_only,
870 "Client layer must be always usable by DRM/KMS");
871 /* This may be normally triggered on validation of the first frame
872 * containing CLIENT layer. At this moment client buffer is not yet
873 * provided by the CLIENT.
874 * This may be triggered once in HwcLayer lifecycle in case FB can't be
875 * imported. For example when non-contiguous buffer is imported into
876 * contiguous-only DRM/KMS driver.
877 */
878 return HWC2::Error::BadLayer;
879 }
880 }
881
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882 if (z_map.empty())
883 return HWC2::Error::BadLayer;
884
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200885 std::vector<LayerData> composition_layers;
886
887 /* Import & populate */
888 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200889 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200890 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200891
892 // now that they're ordered by z, add them to the composition
893 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200894 if (!l.second->IsLayerUsableAsDevice()) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200895 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200896 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200897 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200898 }
899
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200900 /* Store plan to ensure shared planes won't be stolen by other display
901 * in between of ValidateDisplay() and PresentDisplay() calls
902 */
903 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
904 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300905
906 if (type_ == HWC2::DisplayType::Virtual) {
Roman Stratiienkoe0bbf222025-01-23 02:48:44 +0200907 writeback_layer_->PopulateLayerData();
908 if (!writeback_layer_->IsLayerUsableAsDevice()) {
909 ALOGE("Output layer must be always usable by DRM/KMS");
910 return HWC2::Error::BadLayer;
911 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300912 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
913 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
914 .acquire_fence;
915 }
916
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200917 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700918 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200919 return HWC2::Error::BadConfig;
920 }
921
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200922 a_args.composition = current_plan_;
923
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300924 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200925
926 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700927 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200928 return HWC2::Error::BadParameter;
929 }
930
Drew Davenportd387c842024-12-16 16:57:24 -0700931 if (new_vsync_period_ns) {
932 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700933 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700934
935 vsync_worker_->SetVsyncTimestampTracking(false);
936 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
937 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000938 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700939 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000940 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200941 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200942 }
943
944 return HWC2::Error::None;
945}
946
947/* Find API details at:
948 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
949 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300950HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200951 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300952 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200953 return HWC2::Error::None;
954 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200955 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200956
957 ++total_stats_.total_frames_;
958
959 AtomicCommitArgs a_args{};
960 ret = CreateComposition(a_args);
961
962 if (ret != HWC2::Error::None)
963 ++total_stats_.failed_kms_present_;
964
965 if (ret == HWC2::Error::BadLayer) {
966 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300967 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200968 return HWC2::Error::None;
969 }
970 if (ret != HWC2::Error::None)
971 return ret;
972
Roman Stratiienko76892782023-01-16 17:15:53 +0200973 this->present_fence_ = a_args.out_fence;
974 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200975
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200976 // Reset the color matrix so we don't apply it over and over again.
977 color_matrix_ = {};
978
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200979 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700980
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981 return HWC2::Error::None;
982}
983
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200984HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
985 int64_t change_time) {
986 if (configs_.hwc_configs.count(config) == 0) {
987 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200988 return HWC2::Error::BadConfig;
989 }
990
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200991 staged_mode_change_time_ = change_time;
992 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200993
994 return HWC2::Error::None;
995}
996
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200997HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
998 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
999}
1000
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001001HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001002 /* Maps to the Colorspace DRM connector property:
1003 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1004 */
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001005 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_BT2020)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001006 return HWC2::Error::BadParameter;
1007
Sasha McIntosh5294f092024-09-18 18:14:54 -04001008 switch (mode) {
1009 case HAL_COLOR_MODE_NATIVE:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001010 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001011 colorspace_ = Colorspace::kDefault;
1012 break;
1013 case HAL_COLOR_MODE_STANDARD_BT601_625:
1014 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1015 case HAL_COLOR_MODE_STANDARD_BT601_525:
1016 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001017 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001018 // The DP spec does not say whether this is the 525 or the 625 line version.
1019 colorspace_ = Colorspace::kBt601Ycc;
1020 break;
1021 case HAL_COLOR_MODE_STANDARD_BT709:
1022 case HAL_COLOR_MODE_SRGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001023 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001024 colorspace_ = Colorspace::kBt709Ycc;
1025 break;
1026 case HAL_COLOR_MODE_DCI_P3:
1027 case HAL_COLOR_MODE_DISPLAY_P3:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001028 hdr_metadata_.reset();
Sasha McIntosh5294f092024-09-18 18:14:54 -04001029 colorspace_ = Colorspace::kDciP3RgbD65;
1030 break;
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001031 case HAL_COLOR_MODE_DISPLAY_BT2020: {
1032 std::vector<ui::Hdr> hdr_types;
1033 GetEdid()->GetSupportedHdrTypes(hdr_types);
1034 if (!hdr_types.empty()) {
1035 auto ret = SetHdrOutputMetadata(hdr_types.front());
1036 if (ret != HWC2::Error::None)
1037 return ret;
1038 }
1039 colorspace_ = Colorspace::kBt2020Rgb;
1040 break;
1041 }
Sasha McIntosh5294f092024-09-18 18:14:54 -04001042 case HAL_COLOR_MODE_ADOBE_RGB:
Sasha McIntosh851ea4d2024-12-04 17:14:55 -05001043 case HAL_COLOR_MODE_BT2020:
1044 case HAL_COLOR_MODE_BT2100_PQ:
1045 case HAL_COLOR_MODE_BT2100_HLG:
Sasha McIntosh5294f092024-09-18 18:14:54 -04001046 default:
1047 return HWC2::Error::Unsupported;
1048 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001049
1050 color_mode_ = mode;
1051 return HWC2::Error::None;
1052}
1053
1054HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1055 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1056 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1057 return HWC2::Error::BadParameter;
1058
1059 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1060 return HWC2::Error::BadParameter;
1061
1062 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001063
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001064 if (IsInHeadlessMode())
1065 return HWC2::Error::None;
1066
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001067 if (!GetPipe().crtc->Get()->GetCtmProperty())
1068 return HWC2::Error::None;
1069
1070 switch (color_transform_hint_) {
1071 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001072 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001073 break;
1074 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001075 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001076 {
1077 for (int i = 12; i < 14; i++) {
1078 if (matrix[i] != 0.F)
1079 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001080 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001081 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1082 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1083 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001084 }
1085 break;
1086 default:
1087 return HWC2::Error::Unsupported;
1088 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001089
1090 return HWC2::Error::None;
1091}
1092
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001093bool HwcDisplay::CtmByGpu() {
1094 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1095 return false;
1096
1097 if (GetPipe().crtc->Get()->GetCtmProperty())
1098 return false;
1099
Drew Davenport93443182023-12-14 09:25:45 +00001100 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001101 return false;
1102
1103 return true;
1104}
1105
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001106HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1107 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001108
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001109 AtomicCommitArgs a_args{};
1110
1111 switch (mode) {
1112 case HWC2::PowerMode::Off:
1113 a_args.active = false;
1114 break;
1115 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001116 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001117 break;
1118 case HWC2::PowerMode::Doze:
1119 case HWC2::PowerMode::DozeSuspend:
1120 return HWC2::Error::Unsupported;
1121 default:
John Stultzffe783c2024-02-14 10:51:27 -08001122 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001123 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001124 }
1125
1126 if (IsInHeadlessMode()) {
1127 return HWC2::Error::None;
1128 }
1129
Jia Ren80566fe2022-11-17 17:26:00 +08001130 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001131 /*
1132 * Setting the display to active before we have a composition
1133 * can break some drivers, so skip setting a_args.active to
1134 * true, as the next composition frame will implicitly activate
1135 * the display
1136 */
1137 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1138 ? HWC2::Error::None
1139 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001140 };
1141
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001142 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001143 if (err) {
1144 ALOGE("Failed to apply the dpms composition err=%d", err);
1145 return HWC2::Error::BadParameter;
1146 }
1147 return HWC2::Error::None;
1148}
1149
1150HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001151 if (type_ == HWC2::DisplayType::Virtual) {
1152 return HWC2::Error::None;
1153 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001154 if (!vsync_worker_) {
1155 return HWC2::Error::NoResources;
1156 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001157
Roman Stratiienko099c3112022-01-20 11:50:54 +02001158 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001159 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001160 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001161 DrmHwc *hwc = hwc_;
1162 hwc2_display_t id = handle_;
1163 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001164 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001165 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1166 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001167 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001168 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001169 return HWC2::Error::None;
1170}
1171
1172HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1173 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001174 if (IsInHeadlessMode()) {
1175 *num_types = *num_requests = 0;
1176 return HWC2::Error::None;
1177 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001178
1179 /* In current drm_hwc design in case previous frame layer was not validated as
1180 * a CLIENT, it is used by display controller (Front buffer). We have to store
1181 * this state to provide the CLIENT with the release fences for such buffers.
1182 */
1183 for (auto &l : layers_) {
1184 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1185 HWC2::Composition::Client);
1186 }
1187
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001188 return backend_->ValidateDisplay(this, num_types, num_requests);
1189}
1190
1191std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1192 std::vector<HwcLayer *> ordered_layers;
1193 ordered_layers.reserve(layers_.size());
1194
1195 for (auto &[handle, layer] : layers_) {
1196 ordered_layers.emplace_back(&layer);
1197 }
1198
1199 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1200 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1201 return lhs->GetZOrder() < rhs->GetZOrder();
1202 });
1203
1204 return ordered_layers;
1205}
1206
Roman Stratiienko099c3112022-01-20 11:50:54 +02001207HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1208 uint32_t *outVsyncPeriod /* ns */) {
1209 return GetDisplayAttribute(configs_.active_config_id,
1210 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1211 (int32_t *)(outVsyncPeriod));
1212}
1213
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001214// Display primary values are coded as unsigned 16-bit values in units of
1215// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1216static uint64_t ToU16ColorValue(float in) {
1217 constexpr float kPrimariesFixedPoint = 50000.F;
1218 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1219}
1220
1221HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1222 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1223 hdr_metadata_->metadata_type = 0;
1224 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1225 m->metadata_type = 0;
1226
1227 switch (type) {
1228 case ui::Hdr::HDR10:
1229 m->eotf = 2; // PQ
1230 break;
1231 case ui::Hdr::HLG:
1232 m->eotf = 3; // HLG
1233 break;
1234 default:
1235 return HWC2::Error::Unsupported;
1236 }
1237
1238 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1239 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1240 std::vector<ui::Hdr> types;
1241 float hdr_luminance[3]{0.F, 0.F, 0.F};
1242 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1243 &hdr_luminance[2]);
1244 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1245 hdr_luminance[0]);
1246 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1247 // The min luminance value is coded as an unsigned 16-bit value in units of
1248 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1249 // represents 6.5535 cd/m2.
1250 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1251 10000.F);
1252
1253 auto gamut = ColorGamut::BT2020();
1254 auto primaries = gamut.getPrimaries();
1255 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1256 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1257 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1258 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1259 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1260 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1261
1262 auto whitePoint = gamut.getWhitePoint();
1263 m->white_point.x = ToU16ColorValue(whitePoint.x);
1264 m->white_point.y = ToU16ColorValue(whitePoint.y);
1265
1266 return HWC2::Error::None;
1267}
1268
Roman Stratiienko6b405052022-12-10 19:09:10 +02001269#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001270HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001271 if (IsInHeadlessMode()) {
1272 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1273 return HWC2::Error::None;
1274 }
1275 /* Primary display should be always internal,
1276 * otherwise SF will be unhappy and will crash
1277 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001278 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001279 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001280 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001281 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1282 else
1283 return HWC2::Error::BadConfig;
1284
1285 return HWC2::Error::None;
1286}
1287
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001288HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001289 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001290 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1291 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001292 if (type_ == HWC2::DisplayType::Virtual) {
1293 return HWC2::Error::None;
1294 }
1295
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001296 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1297 return HWC2::Error::BadParameter;
1298 }
1299
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001300 uint32_t current_vsync_period{};
1301 GetDisplayVsyncPeriod(&current_vsync_period);
1302
1303 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1304 return HWC2::Error::SeamlessNotAllowed;
1305 }
1306
1307 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1308 ->desiredTimeNanos -
1309 current_vsync_period;
1310 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1311 if (ret != HWC2::Error::None) {
1312 return ret;
1313 }
1314
1315 outTimeline->refreshRequired = true;
1316 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1317 ->desiredTimeNanos;
1318
Drew Davenport33121b72024-12-13 14:59:35 -07001319 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001320
1321 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001322}
1323
1324HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1325 return HWC2::Error::Unsupported;
1326}
1327
1328HWC2::Error HwcDisplay::GetSupportedContentTypes(
1329 uint32_t *outNumSupportedContentTypes,
1330 const uint32_t *outSupportedContentTypes) {
1331 if (outSupportedContentTypes == nullptr)
1332 *outNumSupportedContentTypes = 0;
1333
1334 return HWC2::Error::None;
1335}
1336
1337HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001338 /* Maps exactly to the content_type DRM connector property:
1339 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001340 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001341 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1342 return HWC2::Error::BadParameter;
1343
1344 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001345
1346 return HWC2::Error::None;
1347}
1348#endif
1349
Roman Stratiienko6b405052022-12-10 19:09:10 +02001350#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001351HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1352 uint32_t *outDataSize,
1353 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001354 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001355 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001356 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001357
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001358 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001359 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001360 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001361 }
1362
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001363 *outPort = handle_; /* TDOD(nobody): What should be here? */
1364
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001365 if (outData) {
1366 *outDataSize = std::min(*outDataSize, blob->length);
1367 memcpy(outData, blob->data, *outDataSize);
1368 } else {
1369 *outDataSize = blob->length;
1370 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001371
1372 return HWC2::Error::None;
1373}
1374
1375HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001376 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001377 if (outNumCapabilities == nullptr) {
1378 return HWC2::Error::BadParameter;
1379 }
1380
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001381 bool skip_ctm = false;
1382
1383 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001384 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001385 skip_ctm = true;
1386
1387 // Skip client CTM if DRM can handle it
1388 if (!skip_ctm && !IsInHeadlessMode() &&
1389 GetPipe().crtc->Get()->GetCtmProperty())
1390 skip_ctm = true;
1391
1392 if (!skip_ctm) {
1393 *outNumCapabilities = 0;
1394 return HWC2::Error::None;
1395 }
1396
1397 *outNumCapabilities = 1;
1398 if (outCapabilities) {
1399 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1400 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001401
1402 return HWC2::Error::None;
1403}
1404
1405HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1406 *supported = false;
1407 return HWC2::Error::None;
1408}
1409
1410HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1411 return HWC2::Error::Unsupported;
1412}
1413
Roman Stratiienko6b405052022-12-10 19:09:10 +02001414#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001415
Roman Stratiienko6b405052022-12-10 19:09:10 +02001416#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001417
1418HWC2::Error HwcDisplay::GetRenderIntents(
1419 int32_t mode, uint32_t *outNumIntents,
1420 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1421 if (mode != HAL_COLOR_MODE_NATIVE) {
1422 return HWC2::Error::BadParameter;
1423 }
1424
1425 if (outIntents == nullptr) {
1426 *outNumIntents = 1;
1427 return HWC2::Error::None;
1428 }
1429 *outNumIntents = 1;
1430 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1431 return HWC2::Error::None;
1432}
1433
1434HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1435 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1436 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1437 return HWC2::Error::BadParameter;
1438
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001439 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1440 return HWC2::Error::Unsupported;
1441
Sasha McIntosh5294f092024-09-18 18:14:54 -04001442 auto err = SetColorMode(mode);
1443 if (err != HWC2::Error::None) return err;
1444
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001445 return HWC2::Error::None;
1446}
1447
Roman Stratiienko6b405052022-12-10 19:09:10 +02001448#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001449
1450const Backend *HwcDisplay::backend() const {
1451 return backend_.get();
1452}
1453
1454void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1455 backend_ = std::move(backend);
1456}
1457
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001458} // namespace android