blob: 5abbc4d574c0aff64d2b54ee953c471475c55666 [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
Manasi Navare3f0c01a2024-10-04 18:01:55 +000022#include <cinttypes>
23
Drew Davenport76c17a82025-01-15 15:02:45 -070024#include <xf86drmMode.h>
25
Drew Davenport97b5abc2024-11-07 10:43:54 -070026#include <hardware/gralloc.h>
Sasha McIntoshf9062b62024-11-12 10:55:06 -050027#include <ui/ColorSpace.h>
Drew Davenport97b5abc2024-11-07 10:43:54 -070028#include <ui/GraphicBufferAllocator.h>
29#include <ui/GraphicBufferMapper.h>
30#include <ui/PixelFormat.h>
31
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020032#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020033#include "backend/BackendManager.h"
34#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060035#include "compositor/DisplayInfo.h"
36#include "drm/DrmConnector.h"
37#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000038#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020039#include "utils/log.h"
40#include "utils/properties.h"
41
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060042using ::android::DrmDisplayPipeline;
Sasha McIntoshf9062b62024-11-12 10:55:06 -050043using ColorGamut = ::android::ColorSpace;
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060044
Roman Stratiienko3627beb2022-01-04 16:02:55 +020045namespace android {
46
Drew Davenport97b5abc2024-11-07 10:43:54 -070047namespace {
Drew Davenport76c17a82025-01-15 15:02:45 -070048
49constexpr int kCtmRows = 3;
50constexpr int kCtmCols = 3;
51
52constexpr std::array<float, 16> kIdentityMatrix = {
53 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F,
54 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F,
55};
56
57uint64_t To3132FixPt(float in) {
58 constexpr uint64_t kSignMask = (1ULL << 63);
59 constexpr uint64_t kValueMask = ~(1ULL << 63);
60 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
61 if (in < 0)
62 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
63 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
64}
65
66auto ToColorTransform(const std::array<float, 16> &color_transform_matrix) {
67 /* HAL provides a 4x4 float type matrix:
68 * | 0 1 2 3|
69 * | 4 5 6 7|
70 * | 8 9 10 11|
71 * |12 13 14 15|
72 *
73 * R_out = R*0 + G*4 + B*8 + 12
74 * G_out = R*1 + G*5 + B*9 + 13
75 * B_out = R*2 + G*6 + B*10 + 14
76 *
77 * DRM expects a 3x3 s31.32 fixed point matrix:
78 * out matrix in
79 * |R| |0 1 2| |R|
80 * |G| = |3 4 5| x |G|
81 * |B| |6 7 8| |B|
82 *
83 * R_out = R*0 + G*1 + B*2
84 * G_out = R*3 + G*4 + B*5
85 * B_out = R*6 + G*7 + B*8
86 */
87 auto color_matrix = std::make_shared<drm_color_ctm>();
88 for (int i = 0; i < kCtmCols; i++) {
89 for (int j = 0; j < kCtmRows; j++) {
90 constexpr int kInCtmRows = 4;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +020091 color_matrix->matrix[(i * kCtmRows) + j] = To3132FixPt(
92 color_transform_matrix[(j * kInCtmRows) + i]);
Drew Davenport76c17a82025-01-15 15:02:45 -070093 }
94 }
95 return color_matrix;
96}
97
Drew Davenport97b5abc2024-11-07 10:43:54 -070098// Allocate a black buffer that can be used for an initial modeset when there.
99// is no appropriate client buffer available to be used.
100// Caller must free the returned buffer with GraphicBufferAllocator::free.
101auto GetModesetBuffer(uint32_t width, uint32_t height) -> buffer_handle_t {
102 constexpr PixelFormat format = PIXEL_FORMAT_RGBA_8888;
103 constexpr uint64_t usage = GRALLOC_USAGE_SW_READ_OFTEN |
104 GRALLOC_USAGE_SW_WRITE_OFTEN |
105 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
106
107 constexpr uint32_t layer_count = 1;
108 const std::string name = "drm-hwcomposer";
109
110 buffer_handle_t handle = nullptr;
111 uint32_t stride = 0;
112 status_t status = GraphicBufferAllocator::get().allocate(width, height,
113 format, layer_count,
114 usage, &handle,
115 &stride, name);
116 if (status != OK) {
117 ALOGE("Failed to allocate modeset buffer.");
118 return nullptr;
119 }
120
121 void *data = nullptr;
122 Rect bounds = {0, 0, static_cast<int32_t>(width),
123 static_cast<int32_t>(height)};
124 status = GraphicBufferMapper::get().lock(handle, usage, bounds, &data);
125 if (status != OK) {
126 ALOGE("Failed to map modeset buffer.");
127 GraphicBufferAllocator::get().free(handle);
128 return nullptr;
129 }
130
131 // Cast one of the multiplicands to ensure that the multiplication happens
132 // in a wider type (size_t).
133 const size_t buffer_size = static_cast<size_t>(height) * stride *
134 bytesPerPixel(format);
135 memset(data, 0, buffer_size);
136 status = GraphicBufferMapper::get().unlock(handle);
137 ALOGW_IF(status != OK, "Failed to unmap buffer.");
138 return handle;
139}
140
141auto GetModesetLayerProperties(buffer_handle_t buffer, uint32_t width,
142 uint32_t height) -> HwcLayer::LayerProperties {
143 HwcLayer::LayerProperties properties;
144 properties.buffer = {.buffer_handle = buffer, .acquire_fence = {}};
145 properties.display_frame = {
146 .left = 0,
147 .top = 0,
148 .right = int(width),
149 .bottom = int(height),
150 };
151 properties.source_crop = (hwc_frect_t){
152 .left = 0.0F,
153 .top = 0.0F,
154 .right = static_cast<float>(width),
155 .bottom = static_cast<float>(height),
156 };
157 properties.blend_mode = BufferBlendMode::kNone;
158 return properties;
159}
160} // namespace
161
Roman Stratiienko44b95772025-01-22 18:03:36 +0200162static BufferColorSpace Hwc2ToColorSpace(int32_t dataspace) {
163 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
164 case HAL_DATASPACE_STANDARD_BT709:
165 return BufferColorSpace::kItuRec709;
166 case HAL_DATASPACE_STANDARD_BT601_625:
167 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
168 case HAL_DATASPACE_STANDARD_BT601_525:
169 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
170 return BufferColorSpace::kItuRec601;
171 case HAL_DATASPACE_STANDARD_BT2020:
172 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
173 return BufferColorSpace::kItuRec2020;
174 default:
175 return BufferColorSpace::kUndefined;
176 }
177}
178
179static BufferSampleRange Hwc2ToSampleRange(int32_t dataspace) {
180 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
181 case HAL_DATASPACE_RANGE_FULL:
182 return BufferSampleRange::kFullRange;
183 case HAL_DATASPACE_RANGE_LIMITED:
184 return BufferSampleRange::kLimitedRange;
185 default:
186 return BufferSampleRange::kUndefined;
187 }
188}
189
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200190std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
191 if (delta.total_pixops_ == 0)
192 return "No stats yet";
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200193 auto ratio = 1.0 - (double(delta.gpu_pixops_) / double(delta.total_pixops_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194
195 std::stringstream ss;
196 ss << " Total frames count: " << delta.total_frames_ << "\n"
197 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
198 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
199 << ((delta.failed_kms_present_ > 0)
200 ? " !!! Internal failure, FIX it please\n"
201 : "")
202 << " Flattened frames: " << delta.frames_flattened_ << "\n"
203 << " Pixel operations (free units)"
204 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
205 << "]\n"
206 << " Composition efficiency: " << ratio;
207
208 return ss.str();
209}
210
211std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300212 auto connector_name = IsInHeadlessMode()
213 ? std::string("NULL-DISPLAY")
214 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200215
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200217 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218 << "Statistics since system boot:\n"
219 << DumpDelta(total_stats_) << "\n\n"
220 << "Statistics since last dumpsys request:\n"
221 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
222
223 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
224 return ss.str();
225}
226
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200227HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000228 DrmHwc *hwc)
229 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300230 if (type_ == HWC2::DisplayType::Virtual) {
231 writeback_layer_ = std::make_unique<HwcLayer>(this);
232 }
233}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200234
Drew Davenport76c17a82025-01-15 15:02:45 -0700235void HwcDisplay::SetColorTransformMatrix(
236 const std::array<float, 16> &color_transform_matrix) {
237 auto almost_equal = [](auto a, auto b) {
238 const float epsilon = 0.001F;
239 return std::abs(a - b) < epsilon;
240 };
241 const bool is_identity = std::equal(color_transform_matrix.begin(),
242 color_transform_matrix.end(),
243 kIdentityMatrix.begin(), almost_equal);
244 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
245 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
246 if (color_transform_hint_ == is_identity) {
247 SetColorMatrixToIdentity();
248 } else {
249 color_matrix_ = ToColorTransform(color_transform_matrix);
250 }
251}
252
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400253void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200254 color_matrix_ = std::make_shared<drm_color_ctm>();
255 for (int i = 0; i < kCtmCols; i++) {
256 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800257 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200258 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200259 }
260 }
261
262 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200263}
264
Normunds Rieksts545096d2024-03-11 16:37:45 +0000265HwcDisplay::~HwcDisplay() {
266 Deinit();
267};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200268
Drew Davenportfe70c802024-11-07 13:02:21 -0700269auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
270 -> const HwcDisplayConfig * {
271 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600272 if (config_iter == configs_.hwc_configs.end()) {
273 return nullptr;
274 }
275 return &config_iter->second;
276}
277
Drew Davenportfe70c802024-11-07 13:02:21 -0700278auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
279 return GetConfig(configs_.active_config_id);
280}
281
Drew Davenport8998f8b2024-10-24 10:15:12 -0600282auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700283 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600284}
285
Drew Davenport97b5abc2024-11-07 10:43:54 -0700286HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
287 const HwcDisplayConfig *new_config = GetConfig(config);
288 if (new_config == nullptr) {
289 ALOGE("Could not find active mode for %u", config);
290 return ConfigError::kBadConfig;
291 }
292
293 const HwcDisplayConfig *current_config = GetCurrentConfig();
294
295 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700296 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700297
298 std::optional<LayerData> modeset_layer_data;
299 // If a client layer has already been provided, and its size matches the
300 // new config, use it for the modeset.
301 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
302 current_config->mode.GetRawMode().hdisplay == width &&
303 current_config->mode.GetRawMode().vdisplay == height) {
304 ALOGV("Use existing client_layer for blocking config.");
305 modeset_layer_data = client_layer_.GetLayerData();
306 } else {
307 ALOGV("Allocate modeset buffer.");
308 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
309 if (modeset_buffer != nullptr) {
310 auto modeset_layer = std::make_unique<HwcLayer>(this);
311 modeset_layer->SetLayerProperties(
312 GetModesetLayerProperties(modeset_buffer, width, height));
313 modeset_layer->PopulateLayerData();
314 modeset_layer_data = modeset_layer->GetLayerData();
315 GraphicBufferAllocator::get().free(modeset_buffer);
316 }
317 }
318
319 ALOGV("Create modeset commit.");
320 // Create atomic commit args for a blocking modeset. There's no need to do a
321 // separate test commit, since the commit does a test anyways.
322 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
323 modeset_layer_data);
324 commit_args.blocking = true;
325 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
326
327 if (ret) {
328 ALOGE("Blocking config failed: %d", ret);
329 return HwcDisplay::ConfigError::kBadConfig;
330 }
331
332 ALOGV("Blocking config succeeded.");
333 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700334 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700335 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
336 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700337 return ConfigError::kNone;
338}
339
Drew Davenport8998f8b2024-10-24 10:15:12 -0600340auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
341 bool seamless, QueuedConfigTiming *out_timing)
342 -> ConfigError {
343 if (configs_.hwc_configs.count(config) == 0) {
344 ALOGE("Could not find active mode for %u", config);
345 return ConfigError::kBadConfig;
346 }
347
348 // TODO: Add support for seamless configuration changes.
349 if (seamless) {
350 return ConfigError::kSeamlessNotAllowed;
351 }
352
353 // Request a refresh from the client one vsync period before the desired
354 // time, or simply at the desired time if there is no active configuration.
355 const HwcDisplayConfig *current_config = GetCurrentConfig();
356 out_timing->refresh_time_ns = desired_time -
357 (current_config
358 ? current_config->mode.GetVSyncPeriodNs()
359 : 0);
360 out_timing->new_vsync_time_ns = desired_time;
361
362 // Queue the config change timing to be consistent with the requested
363 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600364 staged_mode_change_time_ = out_timing->refresh_time_ns;
365 staged_mode_config_id_ = config;
366
367 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700368 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600369
370 return ConfigError::kNone;
371}
372
Drew Davenport7f356c82025-01-17 16:32:06 -0700373auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
374 if (IsInHeadlessMode()) {
375 return {};
376 }
377
378 /* In current drm_hwc design in case previous frame layer was not validated as
379 * a CLIENT, it is used by display controller (Front buffer). We have to store
380 * this state to provide the CLIENT with the release fences for such buffers.
381 */
382 for (auto &l : layers_) {
383 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
384 HWC2::Composition::Client);
385 }
386
387 // ValidateDisplay returns the number of layers that may be changed.
388 uint32_t num_types = 0;
389 uint32_t num_requests = 0;
390 backend_->ValidateDisplay(this, &num_types, &num_requests);
391
392 if (num_types == 0) {
393 return {};
394 }
395
396 // Iterate through the layers to find which layers actually changed.
397 std::vector<ChangedLayer> changed_layers;
398 for (auto &l : layers_) {
399 if (l.second.IsTypeChanged()) {
400 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
401 }
402 }
403 return changed_layers;
404}
405
Drew Davenportb864ccf2025-01-22 14:57:36 -0700406auto HwcDisplay::AcceptValidatedComposition() -> void {
407 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
408 l.second.AcceptTypeChange();
409 }
410}
411
412auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200413 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
414 -> bool {
415 int out_fd = -1;
416 auto error = PresentDisplay(&out_fd);
417 out_present_fence = MakeSharedFd(out_fd);
418 if (error != HWC2::Error::None) {
419 return false;
420 }
421
422 if (!out_present_fence) {
423 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700424 }
425
426 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200427 if (l.second.GetPriorBufferScanOutFlag()) {
428 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700429 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700430 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200431
432 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700433}
434
Roman Stratiienko63762a92023-09-18 22:33:45 +0300435void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200436 Deinit();
437
Roman Stratiienko63762a92023-09-18 22:33:45 +0300438 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200439
Roman Stratiienko63762a92023-09-18 22:33:45 +0300440 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200441 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000442 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200443 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000444 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200445 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200446}
447
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200448void HwcDisplay::Deinit() {
449 if (pipeline_ != nullptr) {
450 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200451 a_args.composition = std::make_shared<DrmKmsPlan>();
452 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300453 a_args.composition = {};
454 a_args.active = false;
455 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200456
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200457 current_plan_.reset();
458 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200459 if (flatcon_) {
460 flatcon_->StopThread();
461 flatcon_.reset();
462 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200463 }
464
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200465 if (vsync_worker_) {
466 vsync_worker_->StopThread();
467 vsync_worker_ = {};
468 }
469
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200470 SetClientTarget(nullptr, -1, 0, {});
471}
472
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200473HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200474 ChosePreferredConfig();
475
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300476 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700477 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300478 if (!vsync_worker_) {
479 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
480 return HWC2::Error::BadDisplay;
481 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200482 }
483
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200484 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200485 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200486 if (ret) {
487 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
488 return HWC2::Error::BadDisplay;
489 }
Drew Davenport93443182023-12-14 09:25:45 +0000490 auto flatcbk = (struct FlatConCallbacks){
491 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200492 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200493 }
494
Roman Stratiienko44b95772025-01-22 18:03:36 +0200495 HwcLayer::LayerProperties lp;
496 lp.blend_mode = BufferBlendMode::kPreMult;
497 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200498
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400499 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200500
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200501 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502}
503
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600504std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
505 if (IsInHeadlessMode()) {
506 // The pipeline can be nullptr in headless mode, so return the default
507 // "normal" mode.
508 return PanelOrientation::kModePanelOrientationNormal;
509 }
510
511 DrmDisplayPipeline &pipeline = GetPipe();
512 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
513 ALOGW(
514 "No display pipeline present to query the panel orientation property.");
515 return {};
516 }
517
518 return pipeline.connector->Get()->GetPanelOrientation();
519}
520
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200522 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300523 if (type_ == HWC2::DisplayType::Virtual) {
524 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
525 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200526 err = configs_.Update(*pipeline_->connector->Get());
527 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300528 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200529 }
530 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200531 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200532 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533
Roman Stratiienko0137f862022-01-04 18:27:40 +0200534 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535}
536
537HWC2::Error HwcDisplay::AcceptDisplayChanges() {
538 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
539 l.second.AcceptTypeChange();
540 return HWC2::Error::None;
541}
542
543HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200544 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200545 *layer = static_cast<hwc2_layer_t>(layer_idx_);
546 ++layer_idx_;
547 return HWC2::Error::None;
548}
549
550HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200551 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200552 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200553 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554
555 layers_.erase(layer);
556 return HWC2::Error::None;
557}
558
559HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700560 // If a config has been queued, it is considered the "active" config.
561 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
562 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200563 return HWC2::Error::BadConfig;
564
Drew Davenportfe70c802024-11-07 13:02:21 -0700565 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566 return HWC2::Error::None;
567}
568
569HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
570 hwc2_layer_t *layers,
571 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200572 if (IsInHeadlessMode()) {
573 *num_elements = 0;
574 return HWC2::Error::None;
575 }
576
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300578 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200579 if (l.second.IsTypeChanged()) {
580 if (layers && num_changes < *num_elements)
581 layers[num_changes] = l.first;
582 if (types && num_changes < *num_elements)
583 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
584 ++num_changes;
585 }
586 }
587 if (!layers && !types)
588 *num_elements = num_changes;
589 return HWC2::Error::None;
590}
591
592HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
593 int32_t /*format*/,
594 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200595 if (IsInHeadlessMode()) {
596 return HWC2::Error::None;
597 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200598
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300599 auto min = pipeline_->device->GetMinResolution();
600 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200601
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200602 if (width < min.first || height < min.second)
603 return HWC2::Error::Unsupported;
604
605 if (width > max.first || height > max.second)
606 return HWC2::Error::Unsupported;
607
608 if (dataspace != HAL_DATASPACE_UNKNOWN)
609 return HWC2::Error::Unsupported;
610
611 // TODO(nobody): Validate format can be handled by either GL or planes
612 return HWC2::Error::None;
613}
614
615HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
616 if (!modes)
617 *num_modes = 1;
618
619 if (modes)
620 *modes = HAL_COLOR_MODE_NATIVE;
621
622 return HWC2::Error::None;
623}
624
625HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
626 int32_t attribute_in,
627 int32_t *value) {
628 int conf = static_cast<int>(config);
629
Roman Stratiienko0137f862022-01-04 18:27:40 +0200630 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200631 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632 return HWC2::Error::BadConfig;
633 }
634
Roman Stratiienko0137f862022-01-04 18:27:40 +0200635 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200636
637 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300638 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200639 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
640 switch (attribute) {
641 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200642 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200643 break;
644 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200645 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200646 break;
647 case HWC2::Attribute::VsyncPeriod:
648 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600649 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200650 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000651 case HWC2::Attribute::DpiY:
652 // ideally this should be vdisplay/mm_heigth, however mm_height
653 // comes from edid parsing and is highly unreliable. Viewing the
654 // rarity of anisotropic displays, falling back to a single value
655 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200656 case HWC2::Attribute::DpiX:
657 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200658 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
659 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200660 : -1;
661 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200662#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200663 case HWC2::Attribute::ConfigGroup:
664 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
665 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200666 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200667 break;
668#endif
669 default:
670 *value = -1;
671 return HWC2::Error::BadConfig;
672 }
673 return HWC2::Error::None;
674}
675
Drew Davenportf7e88332024-09-06 12:54:38 -0600676HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
677 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200678 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200679 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200680 if (hwc_config.second.disabled) {
681 continue;
682 }
683
684 if (configs != nullptr) {
685 if (idx >= *num_configs) {
686 break;
687 }
688 configs[idx] = hwc_config.second.id;
689 }
690
691 idx++;
692 }
693 *num_configs = idx;
694 return HWC2::Error::None;
695}
696
697HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
698 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200699 if (IsInHeadlessMode()) {
700 stream << "null-display";
701 } else {
702 stream << "display-" << GetPipe().connector->Get()->GetId();
703 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300704 auto string = stream.str();
705 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200706 if (!name) {
707 *size = length;
708 return HWC2::Error::None;
709 }
710
711 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
712 strncpy(name, string.c_str(), *size);
713 return HWC2::Error::None;
714}
715
716HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
717 uint32_t *num_elements,
718 hwc2_layer_t * /*layers*/,
719 int32_t * /*layer_requests*/) {
720 // TODO(nobody): I think virtual display should request
721 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
722 *num_elements = 0;
723 return HWC2::Error::None;
724}
725
726HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
727 *type = static_cast<int32_t>(type_);
728 return HWC2::Error::None;
729}
730
731HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
732 *support = 0;
733 return HWC2::Error::None;
734}
735
736HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
737 int32_t * /*types*/,
738 float * /*max_luminance*/,
739 float * /*max_average_luminance*/,
740 float * /*min_luminance*/) {
741 *num_types = 0;
742 return HWC2::Error::None;
743}
744
745/* Find API details at:
746 * 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 +0300747 *
748 * Called after PresentDisplay(), CLIENT is expecting release fence for the
749 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200750 */
751HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
752 hwc2_layer_t *layers,
753 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200754 if (IsInHeadlessMode()) {
755 *num_elements = 0;
756 return HWC2::Error::None;
757 }
758
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200759 uint32_t num_layers = 0;
760
Roman Stratiienkodd214942022-05-03 18:24:49 +0300761 for (auto &l : layers_) {
762 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
763 continue;
764 }
765
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200766 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300767
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200768 if (layers == nullptr || fences == nullptr)
769 continue;
770
771 if (num_layers > *num_elements) {
772 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
773 return HWC2::Error::None;
774 }
775
776 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200777 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200778 }
779 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300780
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200781 return HWC2::Error::None;
782}
783
Drew Davenport97b5abc2024-11-07 10:43:54 -0700784AtomicCommitArgs HwcDisplay::CreateModesetCommit(
785 const HwcDisplayConfig *config,
786 const std::optional<LayerData> &modeset_layer) {
787 AtomicCommitArgs args{};
788
789 args.color_matrix = color_matrix_;
790 args.content_type = content_type_;
791 args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500792 args.hdr_metadata = hdr_metadata_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700793
794 std::vector<LayerData> composition_layers;
795 if (modeset_layer) {
796 composition_layers.emplace_back(modeset_layer.value());
797 }
798
799 if (composition_layers.empty()) {
800 ALOGW("Attempting to create a modeset commit without a layer.");
801 }
802
803 args.display_mode = config->mode;
804 args.active = true;
805 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
806 std::move(
807 composition_layers));
808 ALOGW_IF(!args.composition, "No composition for blocking modeset");
809
810 return args;
811}
812
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200813HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200814 if (IsInHeadlessMode()) {
815 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
816 return HWC2::Error::None;
817 }
818
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200819 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400820 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400821 a_args.colorspace = colorspace_;
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500822 a_args.hdr_metadata = hdr_metadata_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200823
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200824 uint32_t prev_vperiod_ns = 0;
825 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200826
Drew Davenportd387c842024-12-16 16:57:24 -0700827 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700828 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200829 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700830 const HwcDisplayConfig *staged_config = GetConfig(
831 staged_mode_config_id_.value());
832 if (staged_config == nullptr) {
833 return HWC2::Error::BadConfig;
834 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200835 HwcLayer::LayerProperties lp;
836 lp.display_frame = {
837 .left = 0,
838 .top = 0,
839 .right = int(staged_config->mode.GetRawMode().hdisplay),
840 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
841 };
842 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200843
Drew Davenportfe70c802024-11-07 13:02:21 -0700844 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700845 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200846 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700847 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200848 }
849 }
850
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200851 // order the layers by z-order
852 bool use_client_layer = false;
853 uint32_t client_z_order = UINT32_MAX;
854 std::map<uint32_t, HwcLayer *> z_map;
855 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
856 switch (l.second.GetValidatedType()) {
857 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300858 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200859 break;
860 case HWC2::Composition::Client:
861 // Place it at the z_order of the lowest client layer
862 use_client_layer = true;
863 client_z_order = std::min(client_z_order, l.second.GetZOrder());
864 break;
865 default:
866 continue;
867 }
868 }
869 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300870 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200871
872 if (z_map.empty())
873 return HWC2::Error::BadLayer;
874
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200875 std::vector<LayerData> composition_layers;
876
877 /* Import & populate */
878 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200879 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200880 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200881
882 // now that they're ordered by z, add them to the composition
883 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200884 if (!l.second->IsLayerUsableAsDevice()) {
885 /* This will be normally triggered on validation of the first frame
886 * containing CLIENT layer. At this moment client buffer is not yet
887 * provided by the CLIENT.
888 * This may be triggered once in HwcLayer lifecycle in case FB can't be
889 * imported. For example when non-contiguous buffer is imported into
890 * contiguous-only DRM/KMS driver.
891 */
892 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200893 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200894 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200895 }
896
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200897 /* Store plan to ensure shared planes won't be stolen by other display
898 * in between of ValidateDisplay() and PresentDisplay() calls
899 */
900 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
901 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300902
903 if (type_ == HWC2::DisplayType::Virtual) {
904 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
905 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
906 .acquire_fence;
907 }
908
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200909 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700910 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200911 return HWC2::Error::BadConfig;
912 }
913
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200914 a_args.composition = current_plan_;
915
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300916 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200917
918 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700919 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200920 return HWC2::Error::BadParameter;
921 }
922
Drew Davenportd387c842024-12-16 16:57:24 -0700923 if (new_vsync_period_ns) {
924 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700925 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700926
927 vsync_worker_->SetVsyncTimestampTracking(false);
928 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
929 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000930 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700931 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000932 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200933 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200934 }
935
936 return HWC2::Error::None;
937}
938
939/* Find API details at:
940 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
941 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300942HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200943 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300944 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200945 return HWC2::Error::None;
946 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200947 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200948
949 ++total_stats_.total_frames_;
950
951 AtomicCommitArgs a_args{};
952 ret = CreateComposition(a_args);
953
954 if (ret != HWC2::Error::None)
955 ++total_stats_.failed_kms_present_;
956
957 if (ret == HWC2::Error::BadLayer) {
958 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300959 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960 return HWC2::Error::None;
961 }
962 if (ret != HWC2::Error::None)
963 return ret;
964
Roman Stratiienko76892782023-01-16 17:15:53 +0200965 this->present_fence_ = a_args.out_fence;
966 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200967
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200968 // Reset the color matrix so we don't apply it over and over again.
969 color_matrix_ = {};
970
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200971 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700972
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973 return HWC2::Error::None;
974}
975
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200976HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
977 int64_t change_time) {
978 if (configs_.hwc_configs.count(config) == 0) {
979 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200980 return HWC2::Error::BadConfig;
981 }
982
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200983 staged_mode_change_time_ = change_time;
984 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200985
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500986 std::vector<ui::Hdr> hdr_types;
987 GetEdid()->GetSupportedHdrTypes(hdr_types);
988 if (hdr_types.empty()) {
989 hdr_metadata_.reset();
990 colorspace_ = Colorspace::kDefault;
991 } else {
992 auto ret = SetHdrOutputMetadata(hdr_types.front());
993 if (ret != HWC2::Error::None)
994 return ret;
995 colorspace_ = Colorspace::kBt2020Rgb;
996 }
997
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200998 return HWC2::Error::None;
999}
1000
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001001HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
1002 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
1003}
1004
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001005/* Find API details at:
1006 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
1007 */
1008HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
1009 int32_t acquire_fence,
1010 int32_t dataspace,
1011 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001012 HwcLayer::LayerProperties lp;
1013 lp.buffer = {.buffer_handle = target,
1014 .acquire_fence = MakeSharedFd(acquire_fence)};
1015 lp.color_space = Hwc2ToColorSpace(dataspace);
1016 lp.sample_range = Hwc2ToSampleRange(dataspace);
1017 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001018
1019 /*
1020 * target can be nullptr, this does mean the Composer Service is calling
1021 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
1022 * https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerClient.h;l=350;drc=944b68180b008456ed2eb4d4d329e33b19bd5166
1023 */
1024 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +03001025 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001026 return HWC2::Error::None;
1027 }
1028
Roman Stratiienko5070d512022-05-30 13:41:20 +03001029 if (IsInHeadlessMode()) {
1030 return HWC2::Error::None;
1031 }
1032
Roman Stratiienko359a9d32023-01-16 17:41:07 +02001033 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001034 if (!client_layer_.IsLayerUsableAsDevice()) {
1035 ALOGE("Client layer must be always usable by DRM/KMS");
1036 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +02001037 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001038
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001039 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001040 if (!bi) {
1041 ALOGE("%s: Invalid state", __func__);
1042 return HWC2::Error::BadLayer;
1043 }
1044
Roman Stratiienko44b95772025-01-22 18:03:36 +02001045 lp = {};
1046 lp.source_crop = {.left = 0.0F,
1047 .top = 0.0F,
1048 .right = float(bi->width),
1049 .bottom = float(bi->height)};
1050 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001051
1052 return HWC2::Error::None;
1053}
1054
1055HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001056 /* Maps to the Colorspace DRM connector property:
1057 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1058 */
1059 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001060 return HWC2::Error::BadParameter;
1061
Sasha McIntosh5294f092024-09-18 18:14:54 -04001062 switch (mode) {
1063 case HAL_COLOR_MODE_NATIVE:
1064 colorspace_ = Colorspace::kDefault;
1065 break;
1066 case HAL_COLOR_MODE_STANDARD_BT601_625:
1067 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1068 case HAL_COLOR_MODE_STANDARD_BT601_525:
1069 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
1070 // The DP spec does not say whether this is the 525 or the 625 line version.
1071 colorspace_ = Colorspace::kBt601Ycc;
1072 break;
1073 case HAL_COLOR_MODE_STANDARD_BT709:
1074 case HAL_COLOR_MODE_SRGB:
1075 colorspace_ = Colorspace::kBt709Ycc;
1076 break;
1077 case HAL_COLOR_MODE_DCI_P3:
1078 case HAL_COLOR_MODE_DISPLAY_P3:
1079 colorspace_ = Colorspace::kDciP3RgbD65;
1080 break;
1081 case HAL_COLOR_MODE_ADOBE_RGB:
1082 default:
1083 return HWC2::Error::Unsupported;
1084 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001085
1086 color_mode_ = mode;
1087 return HWC2::Error::None;
1088}
1089
1090HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1091 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1092 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1093 return HWC2::Error::BadParameter;
1094
1095 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1096 return HWC2::Error::BadParameter;
1097
1098 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001099
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001100 if (IsInHeadlessMode())
1101 return HWC2::Error::None;
1102
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001103 if (!GetPipe().crtc->Get()->GetCtmProperty())
1104 return HWC2::Error::None;
1105
1106 switch (color_transform_hint_) {
1107 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001108 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001109 break;
1110 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001111 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001112 {
1113 for (int i = 12; i < 14; i++) {
1114 if (matrix[i] != 0.F)
1115 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001116 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001117 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1118 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1119 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001120 }
1121 break;
1122 default:
1123 return HWC2::Error::Unsupported;
1124 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001125
1126 return HWC2::Error::None;
1127}
1128
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001129bool HwcDisplay::CtmByGpu() {
1130 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1131 return false;
1132
1133 if (GetPipe().crtc->Get()->GetCtmProperty())
1134 return false;
1135
Drew Davenport93443182023-12-14 09:25:45 +00001136 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001137 return false;
1138
1139 return true;
1140}
1141
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001142HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1143 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001144 HwcLayer::LayerProperties lp;
1145 lp.buffer = {.buffer_handle = buffer,
1146 .acquire_fence = MakeSharedFd(release_fence)};
1147 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001148 writeback_layer_->PopulateLayerData();
1149 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1150 ALOGE("Output layer must be always usable by DRM/KMS");
1151 return HWC2::Error::BadLayer;
1152 }
1153 /* TODO: Check if format is supported by writeback connector */
1154 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001155}
1156
1157HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1158 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001159
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001160 AtomicCommitArgs a_args{};
1161
1162 switch (mode) {
1163 case HWC2::PowerMode::Off:
1164 a_args.active = false;
1165 break;
1166 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001167 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001168 break;
1169 case HWC2::PowerMode::Doze:
1170 case HWC2::PowerMode::DozeSuspend:
1171 return HWC2::Error::Unsupported;
1172 default:
John Stultzffe783c2024-02-14 10:51:27 -08001173 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001174 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001175 }
1176
1177 if (IsInHeadlessMode()) {
1178 return HWC2::Error::None;
1179 }
1180
Jia Ren80566fe2022-11-17 17:26:00 +08001181 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001182 /*
1183 * Setting the display to active before we have a composition
1184 * can break some drivers, so skip setting a_args.active to
1185 * true, as the next composition frame will implicitly activate
1186 * the display
1187 */
1188 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1189 ? HWC2::Error::None
1190 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001191 };
1192
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001193 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001194 if (err) {
1195 ALOGE("Failed to apply the dpms composition err=%d", err);
1196 return HWC2::Error::BadParameter;
1197 }
1198 return HWC2::Error::None;
1199}
1200
1201HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001202 if (type_ == HWC2::DisplayType::Virtual) {
1203 return HWC2::Error::None;
1204 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001205 if (!vsync_worker_) {
1206 return HWC2::Error::NoResources;
1207 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001208
Roman Stratiienko099c3112022-01-20 11:50:54 +02001209 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001210 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001211 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001212 DrmHwc *hwc = hwc_;
1213 hwc2_display_t id = handle_;
1214 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001215 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001216 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1217 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001218 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001219 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001220 return HWC2::Error::None;
1221}
1222
1223HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1224 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001225 if (IsInHeadlessMode()) {
1226 *num_types = *num_requests = 0;
1227 return HWC2::Error::None;
1228 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001229
1230 /* In current drm_hwc design in case previous frame layer was not validated as
1231 * a CLIENT, it is used by display controller (Front buffer). We have to store
1232 * this state to provide the CLIENT with the release fences for such buffers.
1233 */
1234 for (auto &l : layers_) {
1235 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1236 HWC2::Composition::Client);
1237 }
1238
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001239 return backend_->ValidateDisplay(this, num_types, num_requests);
1240}
1241
1242std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1243 std::vector<HwcLayer *> ordered_layers;
1244 ordered_layers.reserve(layers_.size());
1245
1246 for (auto &[handle, layer] : layers_) {
1247 ordered_layers.emplace_back(&layer);
1248 }
1249
1250 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1251 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1252 return lhs->GetZOrder() < rhs->GetZOrder();
1253 });
1254
1255 return ordered_layers;
1256}
1257
Roman Stratiienko099c3112022-01-20 11:50:54 +02001258HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1259 uint32_t *outVsyncPeriod /* ns */) {
1260 return GetDisplayAttribute(configs_.active_config_id,
1261 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1262 (int32_t *)(outVsyncPeriod));
1263}
1264
Sasha McIntoshf9062b62024-11-12 10:55:06 -05001265// Display primary values are coded as unsigned 16-bit values in units of
1266// 0.00002, where 0x0000 represents zero and 0xC350 represents 1.0000.
1267static uint64_t ToU16ColorValue(float in) {
1268 constexpr float kPrimariesFixedPoint = 50000.F;
1269 return static_cast<uint64_t>(kPrimariesFixedPoint * in);
1270}
1271
1272HWC2::Error HwcDisplay::SetHdrOutputMetadata(ui::Hdr type) {
1273 hdr_metadata_ = std::make_shared<hdr_output_metadata>();
1274 hdr_metadata_->metadata_type = 0;
1275 auto *m = &hdr_metadata_->hdmi_metadata_type1;
1276 m->metadata_type = 0;
1277
1278 switch (type) {
1279 case ui::Hdr::HDR10:
1280 m->eotf = 2; // PQ
1281 break;
1282 case ui::Hdr::HLG:
1283 m->eotf = 3; // HLG
1284 break;
1285 default:
1286 return HWC2::Error::Unsupported;
1287 }
1288
1289 // Most luminance values are coded as an unsigned 16-bit value in units of 1
1290 // cd/m2, where 0x0001 represents 1 cd/m2 and 0xFFFF represents 65535 cd/m2.
1291 std::vector<ui::Hdr> types;
1292 float hdr_luminance[3]{0.F, 0.F, 0.F};
1293 GetEdid()->GetHdrCapabilities(types, &hdr_luminance[0], &hdr_luminance[1],
1294 &hdr_luminance[2]);
1295 m->max_display_mastering_luminance = m->max_cll = static_cast<uint64_t>(
1296 hdr_luminance[0]);
1297 m->max_fall = static_cast<uint64_t>(hdr_luminance[1]);
1298 // The min luminance value is coded as an unsigned 16-bit value in units of
1299 // 0.0001 cd/m2, where 0x0001 represents 0.0001 cd/m2 and 0xFFFF
1300 // represents 6.5535 cd/m2.
1301 m->min_display_mastering_luminance = static_cast<uint64_t>(hdr_luminance[2] *
1302 10000.F);
1303
1304 auto gamut = ColorGamut::BT2020();
1305 auto primaries = gamut.getPrimaries();
1306 m->display_primaries[0].x = ToU16ColorValue(primaries[0].x);
1307 m->display_primaries[0].y = ToU16ColorValue(primaries[0].y);
1308 m->display_primaries[1].x = ToU16ColorValue(primaries[1].x);
1309 m->display_primaries[1].y = ToU16ColorValue(primaries[1].y);
1310 m->display_primaries[2].x = ToU16ColorValue(primaries[2].x);
1311 m->display_primaries[2].y = ToU16ColorValue(primaries[2].y);
1312
1313 auto whitePoint = gamut.getWhitePoint();
1314 m->white_point.x = ToU16ColorValue(whitePoint.x);
1315 m->white_point.y = ToU16ColorValue(whitePoint.y);
1316
1317 return HWC2::Error::None;
1318}
1319
Roman Stratiienko6b405052022-12-10 19:09:10 +02001320#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001321HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001322 if (IsInHeadlessMode()) {
1323 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1324 return HWC2::Error::None;
1325 }
1326 /* Primary display should be always internal,
1327 * otherwise SF will be unhappy and will crash
1328 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001329 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001330 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001331 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001332 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1333 else
1334 return HWC2::Error::BadConfig;
1335
1336 return HWC2::Error::None;
1337}
1338
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001339HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001340 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001341 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1342 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001343 if (type_ == HWC2::DisplayType::Virtual) {
1344 return HWC2::Error::None;
1345 }
1346
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001347 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1348 return HWC2::Error::BadParameter;
1349 }
1350
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001351 uint32_t current_vsync_period{};
1352 GetDisplayVsyncPeriod(&current_vsync_period);
1353
1354 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1355 return HWC2::Error::SeamlessNotAllowed;
1356 }
1357
1358 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1359 ->desiredTimeNanos -
1360 current_vsync_period;
1361 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1362 if (ret != HWC2::Error::None) {
1363 return ret;
1364 }
1365
1366 outTimeline->refreshRequired = true;
1367 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1368 ->desiredTimeNanos;
1369
Drew Davenport33121b72024-12-13 14:59:35 -07001370 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001371
1372 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001373}
1374
1375HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1376 return HWC2::Error::Unsupported;
1377}
1378
1379HWC2::Error HwcDisplay::GetSupportedContentTypes(
1380 uint32_t *outNumSupportedContentTypes,
1381 const uint32_t *outSupportedContentTypes) {
1382 if (outSupportedContentTypes == nullptr)
1383 *outNumSupportedContentTypes = 0;
1384
1385 return HWC2::Error::None;
1386}
1387
1388HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001389 /* Maps exactly to the content_type DRM connector property:
1390 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001391 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001392 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1393 return HWC2::Error::BadParameter;
1394
1395 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001396
1397 return HWC2::Error::None;
1398}
1399#endif
1400
Roman Stratiienko6b405052022-12-10 19:09:10 +02001401#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001402HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1403 uint32_t *outDataSize,
1404 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001405 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001406 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001407 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001408
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001409 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001410 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001411 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001412 }
1413
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001414 *outPort = handle_; /* TDOD(nobody): What should be here? */
1415
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001416 if (outData) {
1417 *outDataSize = std::min(*outDataSize, blob->length);
1418 memcpy(outData, blob->data, *outDataSize);
1419 } else {
1420 *outDataSize = blob->length;
1421 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001422
1423 return HWC2::Error::None;
1424}
1425
1426HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001427 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001428 if (outNumCapabilities == nullptr) {
1429 return HWC2::Error::BadParameter;
1430 }
1431
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001432 bool skip_ctm = false;
1433
1434 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001435 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001436 skip_ctm = true;
1437
1438 // Skip client CTM if DRM can handle it
1439 if (!skip_ctm && !IsInHeadlessMode() &&
1440 GetPipe().crtc->Get()->GetCtmProperty())
1441 skip_ctm = true;
1442
1443 if (!skip_ctm) {
1444 *outNumCapabilities = 0;
1445 return HWC2::Error::None;
1446 }
1447
1448 *outNumCapabilities = 1;
1449 if (outCapabilities) {
1450 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1451 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001452
1453 return HWC2::Error::None;
1454}
1455
1456HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1457 *supported = false;
1458 return HWC2::Error::None;
1459}
1460
1461HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1462 return HWC2::Error::Unsupported;
1463}
1464
Roman Stratiienko6b405052022-12-10 19:09:10 +02001465#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001466
Roman Stratiienko6b405052022-12-10 19:09:10 +02001467#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001468
1469HWC2::Error HwcDisplay::GetRenderIntents(
1470 int32_t mode, uint32_t *outNumIntents,
1471 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1472 if (mode != HAL_COLOR_MODE_NATIVE) {
1473 return HWC2::Error::BadParameter;
1474 }
1475
1476 if (outIntents == nullptr) {
1477 *outNumIntents = 1;
1478 return HWC2::Error::None;
1479 }
1480 *outNumIntents = 1;
1481 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1482 return HWC2::Error::None;
1483}
1484
1485HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1486 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1487 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1488 return HWC2::Error::BadParameter;
1489
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001490 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1491 return HWC2::Error::Unsupported;
1492
Sasha McIntosh5294f092024-09-18 18:14:54 -04001493 auto err = SetColorMode(mode);
1494 if (err != HWC2::Error::None) return err;
1495
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001496 return HWC2::Error::None;
1497}
1498
Roman Stratiienko6b405052022-12-10 19:09:10 +02001499#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001500
1501const Backend *HwcDisplay::backend() const {
1502 return backend_.get();
1503}
1504
1505void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1506 backend_ = std::move(backend);
1507}
1508
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001509} // namespace android