blob: 60861afd77e62e36e35619c727e95a6d85d80213 [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 Davenport97b5abc2024-11-07 10:43:54 -070024#include <hardware/gralloc.h>
25#include <ui/GraphicBufferAllocator.h>
26#include <ui/GraphicBufferMapper.h>
27#include <ui/PixelFormat.h>
28
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020029#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020030#include "backend/BackendManager.h"
31#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060032#include "compositor/DisplayInfo.h"
33#include "drm/DrmConnector.h"
34#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000035#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020036#include "utils/log.h"
37#include "utils/properties.h"
38
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060039using ::android::DrmDisplayPipeline;
40
Roman Stratiienko3627beb2022-01-04 16:02:55 +020041namespace android {
42
Drew Davenport97b5abc2024-11-07 10:43:54 -070043namespace {
44// Allocate a black buffer that can be used for an initial modeset when there.
45// is no appropriate client buffer available to be used.
46// Caller must free the returned buffer with GraphicBufferAllocator::free.
47auto GetModesetBuffer(uint32_t width, uint32_t height) -> buffer_handle_t {
48 constexpr PixelFormat format = PIXEL_FORMAT_RGBA_8888;
49 constexpr uint64_t usage = GRALLOC_USAGE_SW_READ_OFTEN |
50 GRALLOC_USAGE_SW_WRITE_OFTEN |
51 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
52
53 constexpr uint32_t layer_count = 1;
54 const std::string name = "drm-hwcomposer";
55
56 buffer_handle_t handle = nullptr;
57 uint32_t stride = 0;
58 status_t status = GraphicBufferAllocator::get().allocate(width, height,
59 format, layer_count,
60 usage, &handle,
61 &stride, name);
62 if (status != OK) {
63 ALOGE("Failed to allocate modeset buffer.");
64 return nullptr;
65 }
66
67 void *data = nullptr;
68 Rect bounds = {0, 0, static_cast<int32_t>(width),
69 static_cast<int32_t>(height)};
70 status = GraphicBufferMapper::get().lock(handle, usage, bounds, &data);
71 if (status != OK) {
72 ALOGE("Failed to map modeset buffer.");
73 GraphicBufferAllocator::get().free(handle);
74 return nullptr;
75 }
76
77 // Cast one of the multiplicands to ensure that the multiplication happens
78 // in a wider type (size_t).
79 const size_t buffer_size = static_cast<size_t>(height) * stride *
80 bytesPerPixel(format);
81 memset(data, 0, buffer_size);
82 status = GraphicBufferMapper::get().unlock(handle);
83 ALOGW_IF(status != OK, "Failed to unmap buffer.");
84 return handle;
85}
86
87auto GetModesetLayerProperties(buffer_handle_t buffer, uint32_t width,
88 uint32_t height) -> HwcLayer::LayerProperties {
89 HwcLayer::LayerProperties properties;
90 properties.buffer = {.buffer_handle = buffer, .acquire_fence = {}};
91 properties.display_frame = {
92 .left = 0,
93 .top = 0,
94 .right = int(width),
95 .bottom = int(height),
96 };
97 properties.source_crop = (hwc_frect_t){
98 .left = 0.0F,
99 .top = 0.0F,
100 .right = static_cast<float>(width),
101 .bottom = static_cast<float>(height),
102 };
103 properties.blend_mode = BufferBlendMode::kNone;
104 return properties;
105}
106} // namespace
107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200108std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
109 if (delta.total_pixops_ == 0)
110 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300111 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200112
113 std::stringstream ss;
114 ss << " Total frames count: " << delta.total_frames_ << "\n"
115 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
116 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
117 << ((delta.failed_kms_present_ > 0)
118 ? " !!! Internal failure, FIX it please\n"
119 : "")
120 << " Flattened frames: " << delta.frames_flattened_ << "\n"
121 << " Pixel operations (free units)"
122 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
123 << "]\n"
124 << " Composition efficiency: " << ratio;
125
126 return ss.str();
127}
128
129std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300130 auto connector_name = IsInHeadlessMode()
131 ? std::string("NULL-DISPLAY")
132 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200133
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200134 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200136 << "Statistics since system boot:\n"
137 << DumpDelta(total_stats_) << "\n\n"
138 << "Statistics since last dumpsys request:\n"
139 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
140
141 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
142 return ss.str();
143}
144
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200145HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000146 DrmHwc *hwc)
147 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300148 if (type_ == HWC2::DisplayType::Virtual) {
149 writeback_layer_ = std::make_unique<HwcLayer>(this);
150 }
151}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200152
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400153void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200154 color_matrix_ = std::make_shared<drm_color_ctm>();
155 for (int i = 0; i < kCtmCols; i++) {
156 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800157 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200158 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
159 }
160 }
161
162 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200163}
164
Normunds Rieksts545096d2024-03-11 16:37:45 +0000165HwcDisplay::~HwcDisplay() {
166 Deinit();
167};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200168
Drew Davenportfe70c802024-11-07 13:02:21 -0700169auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
170 -> const HwcDisplayConfig * {
171 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600172 if (config_iter == configs_.hwc_configs.end()) {
173 return nullptr;
174 }
175 return &config_iter->second;
176}
177
Drew Davenportfe70c802024-11-07 13:02:21 -0700178auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
179 return GetConfig(configs_.active_config_id);
180}
181
Drew Davenport8998f8b2024-10-24 10:15:12 -0600182auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700183 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600184}
185
Drew Davenport97b5abc2024-11-07 10:43:54 -0700186HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
187 const HwcDisplayConfig *new_config = GetConfig(config);
188 if (new_config == nullptr) {
189 ALOGE("Could not find active mode for %u", config);
190 return ConfigError::kBadConfig;
191 }
192
193 const HwcDisplayConfig *current_config = GetCurrentConfig();
194
195 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700196 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700197
198 std::optional<LayerData> modeset_layer_data;
199 // If a client layer has already been provided, and its size matches the
200 // new config, use it for the modeset.
201 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
202 current_config->mode.GetRawMode().hdisplay == width &&
203 current_config->mode.GetRawMode().vdisplay == height) {
204 ALOGV("Use existing client_layer for blocking config.");
205 modeset_layer_data = client_layer_.GetLayerData();
206 } else {
207 ALOGV("Allocate modeset buffer.");
208 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
209 if (modeset_buffer != nullptr) {
210 auto modeset_layer = std::make_unique<HwcLayer>(this);
211 modeset_layer->SetLayerProperties(
212 GetModesetLayerProperties(modeset_buffer, width, height));
213 modeset_layer->PopulateLayerData();
214 modeset_layer_data = modeset_layer->GetLayerData();
215 GraphicBufferAllocator::get().free(modeset_buffer);
216 }
217 }
218
219 ALOGV("Create modeset commit.");
220 // Create atomic commit args for a blocking modeset. There's no need to do a
221 // separate test commit, since the commit does a test anyways.
222 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
223 modeset_layer_data);
224 commit_args.blocking = true;
225 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
226
227 if (ret) {
228 ALOGE("Blocking config failed: %d", ret);
229 return HwcDisplay::ConfigError::kBadConfig;
230 }
231
232 ALOGV("Blocking config succeeded.");
233 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700234 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700235 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
236 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700237 return ConfigError::kNone;
238}
239
Drew Davenport8998f8b2024-10-24 10:15:12 -0600240auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
241 bool seamless, QueuedConfigTiming *out_timing)
242 -> ConfigError {
243 if (configs_.hwc_configs.count(config) == 0) {
244 ALOGE("Could not find active mode for %u", config);
245 return ConfigError::kBadConfig;
246 }
247
248 // TODO: Add support for seamless configuration changes.
249 if (seamless) {
250 return ConfigError::kSeamlessNotAllowed;
251 }
252
253 // Request a refresh from the client one vsync period before the desired
254 // time, or simply at the desired time if there is no active configuration.
255 const HwcDisplayConfig *current_config = GetCurrentConfig();
256 out_timing->refresh_time_ns = desired_time -
257 (current_config
258 ? current_config->mode.GetVSyncPeriodNs()
259 : 0);
260 out_timing->new_vsync_time_ns = desired_time;
261
262 // Queue the config change timing to be consistent with the requested
263 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600264 staged_mode_change_time_ = out_timing->refresh_time_ns;
265 staged_mode_config_id_ = config;
266
267 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700268 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600269
270 return ConfigError::kNone;
271}
272
Roman Stratiienko63762a92023-09-18 22:33:45 +0300273void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200274 Deinit();
275
Roman Stratiienko63762a92023-09-18 22:33:45 +0300276 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200277
Roman Stratiienko63762a92023-09-18 22:33:45 +0300278 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200279 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000280 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200281 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000282 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200283 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200284}
285
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200286void HwcDisplay::Deinit() {
287 if (pipeline_ != nullptr) {
288 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200289 a_args.composition = std::make_shared<DrmKmsPlan>();
290 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300291 a_args.composition = {};
292 a_args.active = false;
293 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200294
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200295 current_plan_.reset();
296 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200297 if (flatcon_) {
298 flatcon_->StopThread();
299 flatcon_.reset();
300 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200301 }
302
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200303 if (vsync_worker_) {
304 vsync_worker_->StopThread();
305 vsync_worker_ = {};
306 }
307
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200308 SetClientTarget(nullptr, -1, 0, {});
309}
310
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200311HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200312 ChosePreferredConfig();
313
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300314 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700315 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300316 if (!vsync_worker_) {
317 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
318 return HWC2::Error::BadDisplay;
319 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200320 }
321
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200322 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200323 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200324 if (ret) {
325 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
326 return HWC2::Error::BadDisplay;
327 }
Drew Davenport93443182023-12-14 09:25:45 +0000328 auto flatcbk = (struct FlatConCallbacks){
329 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200330 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200331 }
332
333 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
334
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400335 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200336
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200337 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200338}
339
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600340std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
341 if (IsInHeadlessMode()) {
342 // The pipeline can be nullptr in headless mode, so return the default
343 // "normal" mode.
344 return PanelOrientation::kModePanelOrientationNormal;
345 }
346
347 DrmDisplayPipeline &pipeline = GetPipe();
348 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
349 ALOGW(
350 "No display pipeline present to query the panel orientation property.");
351 return {};
352 }
353
354 return pipeline.connector->Get()->GetPanelOrientation();
355}
356
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200357HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200358 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300359 if (type_ == HWC2::DisplayType::Virtual) {
360 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
361 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200362 err = configs_.Update(*pipeline_->connector->Get());
363 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300364 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200365 }
366 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200367 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200368 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200369
Roman Stratiienko0137f862022-01-04 18:27:40 +0200370 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200371}
372
373HWC2::Error HwcDisplay::AcceptDisplayChanges() {
374 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
375 l.second.AcceptTypeChange();
376 return HWC2::Error::None;
377}
378
379HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200380 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200381 *layer = static_cast<hwc2_layer_t>(layer_idx_);
382 ++layer_idx_;
383 return HWC2::Error::None;
384}
385
386HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200387 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200388 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200389 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200390
391 layers_.erase(layer);
392 return HWC2::Error::None;
393}
394
395HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700396 // If a config has been queued, it is considered the "active" config.
397 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
398 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200399 return HWC2::Error::BadConfig;
400
Drew Davenportfe70c802024-11-07 13:02:21 -0700401 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200402 return HWC2::Error::None;
403}
404
405HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
406 hwc2_layer_t *layers,
407 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200408 if (IsInHeadlessMode()) {
409 *num_elements = 0;
410 return HWC2::Error::None;
411 }
412
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200413 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300414 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200415 if (l.second.IsTypeChanged()) {
416 if (layers && num_changes < *num_elements)
417 layers[num_changes] = l.first;
418 if (types && num_changes < *num_elements)
419 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
420 ++num_changes;
421 }
422 }
423 if (!layers && !types)
424 *num_elements = num_changes;
425 return HWC2::Error::None;
426}
427
428HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
429 int32_t /*format*/,
430 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200431 if (IsInHeadlessMode()) {
432 return HWC2::Error::None;
433 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200434
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300435 auto min = pipeline_->device->GetMinResolution();
436 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200437
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200438 if (width < min.first || height < min.second)
439 return HWC2::Error::Unsupported;
440
441 if (width > max.first || height > max.second)
442 return HWC2::Error::Unsupported;
443
444 if (dataspace != HAL_DATASPACE_UNKNOWN)
445 return HWC2::Error::Unsupported;
446
447 // TODO(nobody): Validate format can be handled by either GL or planes
448 return HWC2::Error::None;
449}
450
451HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
452 if (!modes)
453 *num_modes = 1;
454
455 if (modes)
456 *modes = HAL_COLOR_MODE_NATIVE;
457
458 return HWC2::Error::None;
459}
460
461HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
462 int32_t attribute_in,
463 int32_t *value) {
464 int conf = static_cast<int>(config);
465
Roman Stratiienko0137f862022-01-04 18:27:40 +0200466 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200467 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200468 return HWC2::Error::BadConfig;
469 }
470
Roman Stratiienko0137f862022-01-04 18:27:40 +0200471 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200472
473 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300474 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200475 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
476 switch (attribute) {
477 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200478 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479 break;
480 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200481 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200482 break;
483 case HWC2::Attribute::VsyncPeriod:
484 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600485 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200486 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000487 case HWC2::Attribute::DpiY:
488 // ideally this should be vdisplay/mm_heigth, however mm_height
489 // comes from edid parsing and is highly unreliable. Viewing the
490 // rarity of anisotropic displays, falling back to a single value
491 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200492 case HWC2::Attribute::DpiX:
493 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200494 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
495 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 : -1;
497 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200498#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200499 case HWC2::Attribute::ConfigGroup:
500 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
501 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200502 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200503 break;
504#endif
505 default:
506 *value = -1;
507 return HWC2::Error::BadConfig;
508 }
509 return HWC2::Error::None;
510}
511
Drew Davenportf7e88332024-09-06 12:54:38 -0600512HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
513 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200515 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200516 if (hwc_config.second.disabled) {
517 continue;
518 }
519
520 if (configs != nullptr) {
521 if (idx >= *num_configs) {
522 break;
523 }
524 configs[idx] = hwc_config.second.id;
525 }
526
527 idx++;
528 }
529 *num_configs = idx;
530 return HWC2::Error::None;
531}
532
533HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
534 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200535 if (IsInHeadlessMode()) {
536 stream << "null-display";
537 } else {
538 stream << "display-" << GetPipe().connector->Get()->GetId();
539 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300540 auto string = stream.str();
541 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200542 if (!name) {
543 *size = length;
544 return HWC2::Error::None;
545 }
546
547 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
548 strncpy(name, string.c_str(), *size);
549 return HWC2::Error::None;
550}
551
552HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
553 uint32_t *num_elements,
554 hwc2_layer_t * /*layers*/,
555 int32_t * /*layer_requests*/) {
556 // TODO(nobody): I think virtual display should request
557 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
558 *num_elements = 0;
559 return HWC2::Error::None;
560}
561
562HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
563 *type = static_cast<int32_t>(type_);
564 return HWC2::Error::None;
565}
566
567HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
568 *support = 0;
569 return HWC2::Error::None;
570}
571
572HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
573 int32_t * /*types*/,
574 float * /*max_luminance*/,
575 float * /*max_average_luminance*/,
576 float * /*min_luminance*/) {
577 *num_types = 0;
578 return HWC2::Error::None;
579}
580
581/* Find API details at:
582 * 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 +0300583 *
584 * Called after PresentDisplay(), CLIENT is expecting release fence for the
585 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200586 */
587HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
588 hwc2_layer_t *layers,
589 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200590 if (IsInHeadlessMode()) {
591 *num_elements = 0;
592 return HWC2::Error::None;
593 }
594
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200595 uint32_t num_layers = 0;
596
Roman Stratiienkodd214942022-05-03 18:24:49 +0300597 for (auto &l : layers_) {
598 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
599 continue;
600 }
601
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200602 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300603
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604 if (layers == nullptr || fences == nullptr)
605 continue;
606
607 if (num_layers > *num_elements) {
608 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
609 return HWC2::Error::None;
610 }
611
612 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200613 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200614 }
615 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300616
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200617 return HWC2::Error::None;
618}
619
Drew Davenport97b5abc2024-11-07 10:43:54 -0700620AtomicCommitArgs HwcDisplay::CreateModesetCommit(
621 const HwcDisplayConfig *config,
622 const std::optional<LayerData> &modeset_layer) {
623 AtomicCommitArgs args{};
624
625 args.color_matrix = color_matrix_;
626 args.content_type = content_type_;
627 args.colorspace = colorspace_;
628
629 std::vector<LayerData> composition_layers;
630 if (modeset_layer) {
631 composition_layers.emplace_back(modeset_layer.value());
632 }
633
634 if (composition_layers.empty()) {
635 ALOGW("Attempting to create a modeset commit without a layer.");
636 }
637
638 args.display_mode = config->mode;
639 args.active = true;
640 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
641 std::move(
642 composition_layers));
643 ALOGW_IF(!args.composition, "No composition for blocking modeset");
644
645 return args;
646}
647
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200649 if (IsInHeadlessMode()) {
650 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
651 return HWC2::Error::None;
652 }
653
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200654 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400655 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400656 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200657
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200658 uint32_t prev_vperiod_ns = 0;
659 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200660
Drew Davenportd387c842024-12-16 16:57:24 -0700661 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700662 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200663 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700664 const HwcDisplayConfig *staged_config = GetConfig(
665 staged_mode_config_id_.value());
666 if (staged_config == nullptr) {
667 return HWC2::Error::BadConfig;
668 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200669 client_layer_.SetLayerDisplayFrame(
670 (hwc_rect_t){.left = 0,
671 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700672 .right = int(staged_config->mode.GetRawMode().hdisplay),
673 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200674
Drew Davenportfe70c802024-11-07 13:02:21 -0700675 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700676 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200677 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700678 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200679 }
680 }
681
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200682 // order the layers by z-order
683 bool use_client_layer = false;
684 uint32_t client_z_order = UINT32_MAX;
685 std::map<uint32_t, HwcLayer *> z_map;
686 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
687 switch (l.second.GetValidatedType()) {
688 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300689 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200690 break;
691 case HWC2::Composition::Client:
692 // Place it at the z_order of the lowest client layer
693 use_client_layer = true;
694 client_z_order = std::min(client_z_order, l.second.GetZOrder());
695 break;
696 default:
697 continue;
698 }
699 }
700 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300701 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200702
703 if (z_map.empty())
704 return HWC2::Error::BadLayer;
705
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200706 std::vector<LayerData> composition_layers;
707
708 /* Import & populate */
709 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200710 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200711 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200712
713 // now that they're ordered by z, add them to the composition
714 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200715 if (!l.second->IsLayerUsableAsDevice()) {
716 /* This will be normally triggered on validation of the first frame
717 * containing CLIENT layer. At this moment client buffer is not yet
718 * provided by the CLIENT.
719 * This may be triggered once in HwcLayer lifecycle in case FB can't be
720 * imported. For example when non-contiguous buffer is imported into
721 * contiguous-only DRM/KMS driver.
722 */
723 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200724 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200725 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200726 }
727
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200728 /* Store plan to ensure shared planes won't be stolen by other display
729 * in between of ValidateDisplay() and PresentDisplay() calls
730 */
731 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
732 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300733
734 if (type_ == HWC2::DisplayType::Virtual) {
735 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
736 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
737 .acquire_fence;
738 }
739
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200740 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700741 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200742 return HWC2::Error::BadConfig;
743 }
744
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200745 a_args.composition = current_plan_;
746
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300747 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748
749 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700750 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200751 return HWC2::Error::BadParameter;
752 }
753
Drew Davenportd387c842024-12-16 16:57:24 -0700754 if (new_vsync_period_ns) {
755 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700756 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700757
758 vsync_worker_->SetVsyncTimestampTracking(false);
759 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
760 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000761 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700762 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000763 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200764 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200765 }
766
767 return HWC2::Error::None;
768}
769
770/* Find API details at:
771 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
772 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300773HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200774 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300775 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200776 return HWC2::Error::None;
777 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200778 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200779
780 ++total_stats_.total_frames_;
781
782 AtomicCommitArgs a_args{};
783 ret = CreateComposition(a_args);
784
785 if (ret != HWC2::Error::None)
786 ++total_stats_.failed_kms_present_;
787
788 if (ret == HWC2::Error::BadLayer) {
789 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300790 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200791 return HWC2::Error::None;
792 }
793 if (ret != HWC2::Error::None)
794 return ret;
795
Roman Stratiienko76892782023-01-16 17:15:53 +0200796 this->present_fence_ = a_args.out_fence;
797 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200798
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200799 // Reset the color matrix so we don't apply it over and over again.
800 color_matrix_ = {};
801
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200802 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700803
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200804 return HWC2::Error::None;
805}
806
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200807HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
808 int64_t change_time) {
809 if (configs_.hwc_configs.count(config) == 0) {
810 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200811 return HWC2::Error::BadConfig;
812 }
813
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200814 staged_mode_change_time_ = change_time;
815 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200816
817 return HWC2::Error::None;
818}
819
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200820HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
821 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
822}
823
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200824/* Find API details at:
825 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
826 */
827HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
828 int32_t acquire_fence,
829 int32_t dataspace,
830 hwc_region_t /*damage*/) {
831 client_layer_.SetLayerBuffer(target, acquire_fence);
832 client_layer_.SetLayerDataspace(dataspace);
833
834 /*
835 * target can be nullptr, this does mean the Composer Service is calling
836 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
837 * 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
838 */
839 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300840 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200841 return HWC2::Error::None;
842 }
843
Roman Stratiienko5070d512022-05-30 13:41:20 +0300844 if (IsInHeadlessMode()) {
845 return HWC2::Error::None;
846 }
847
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200848 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200849 if (!client_layer_.IsLayerUsableAsDevice()) {
850 ALOGE("Client layer must be always usable by DRM/KMS");
851 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200852 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200853
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200854 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300855 if (!bi) {
856 ALOGE("%s: Invalid state", __func__);
857 return HWC2::Error::BadLayer;
858 }
859
860 auto source_crop = (hwc_frect_t){.left = 0.0F,
861 .top = 0.0F,
862 .right = static_cast<float>(bi->width),
863 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200864 client_layer_.SetLayerSourceCrop(source_crop);
865
866 return HWC2::Error::None;
867}
868
869HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400870 /* Maps to the Colorspace DRM connector property:
871 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
872 */
873 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200874 return HWC2::Error::BadParameter;
875
Sasha McIntosh5294f092024-09-18 18:14:54 -0400876 switch (mode) {
877 case HAL_COLOR_MODE_NATIVE:
878 colorspace_ = Colorspace::kDefault;
879 break;
880 case HAL_COLOR_MODE_STANDARD_BT601_625:
881 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
882 case HAL_COLOR_MODE_STANDARD_BT601_525:
883 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
884 // The DP spec does not say whether this is the 525 or the 625 line version.
885 colorspace_ = Colorspace::kBt601Ycc;
886 break;
887 case HAL_COLOR_MODE_STANDARD_BT709:
888 case HAL_COLOR_MODE_SRGB:
889 colorspace_ = Colorspace::kBt709Ycc;
890 break;
891 case HAL_COLOR_MODE_DCI_P3:
892 case HAL_COLOR_MODE_DISPLAY_P3:
893 colorspace_ = Colorspace::kDciP3RgbD65;
894 break;
895 case HAL_COLOR_MODE_ADOBE_RGB:
896 default:
897 return HWC2::Error::Unsupported;
898 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200899
900 color_mode_ = mode;
901 return HWC2::Error::None;
902}
903
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200904#include <xf86drmMode.h>
905
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400906static uint64_t To3132FixPt(float in) {
907 constexpr uint64_t kSignMask = (1ULL << 63);
908 constexpr uint64_t kValueMask = ~(1ULL << 63);
909 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
910 if (in < 0)
911 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
912 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
913}
914
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200915HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
916 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
917 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
918 return HWC2::Error::BadParameter;
919
920 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
921 return HWC2::Error::BadParameter;
922
923 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200924
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200925 if (IsInHeadlessMode())
926 return HWC2::Error::None;
927
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200928 if (!GetPipe().crtc->Get()->GetCtmProperty())
929 return HWC2::Error::None;
930
931 switch (color_transform_hint_) {
932 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400933 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200934 break;
935 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400936 // Without HW support, we cannot correctly process matrices with an offset.
937 for (int i = 12; i < 14; i++) {
938 if (matrix[i] != 0.F)
939 return HWC2::Error::Unsupported;
940 }
941
942 /* HAL provides a 4x4 float type matrix:
943 * | 0 1 2 3|
944 * | 4 5 6 7|
945 * | 8 9 10 11|
946 * |12 13 14 15|
947 *
948 * R_out = R*0 + G*4 + B*8 + 12
949 * G_out = R*1 + G*5 + B*9 + 13
950 * B_out = R*2 + G*6 + B*10 + 14
951 *
952 * DRM expects a 3x3 s31.32 fixed point matrix:
953 * out matrix in
954 * |R| |0 1 2| |R|
955 * |G| = |3 4 5| x |G|
956 * |B| |6 7 8| |B|
957 *
958 * R_out = R*0 + G*1 + B*2
959 * G_out = R*3 + G*4 + B*5
960 * B_out = R*6 + G*7 + B*8
961 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200962 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200963 for (int i = 0; i < kCtmCols; i++) {
964 for (int j = 0; j < kCtmRows; j++) {
965 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400966 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200967 }
968 }
969 break;
970 default:
971 return HWC2::Error::Unsupported;
972 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973
974 return HWC2::Error::None;
975}
976
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200977bool HwcDisplay::CtmByGpu() {
978 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
979 return false;
980
981 if (GetPipe().crtc->Get()->GetCtmProperty())
982 return false;
983
Drew Davenport93443182023-12-14 09:25:45 +0000984 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200985 return false;
986
987 return true;
988}
989
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300990HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
991 int32_t release_fence) {
992 writeback_layer_->SetLayerBuffer(buffer, release_fence);
993 writeback_layer_->PopulateLayerData();
994 if (!writeback_layer_->IsLayerUsableAsDevice()) {
995 ALOGE("Output layer must be always usable by DRM/KMS");
996 return HWC2::Error::BadLayer;
997 }
998 /* TODO: Check if format is supported by writeback connector */
999 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001000}
1001
1002HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1003 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001004
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001005 AtomicCommitArgs a_args{};
1006
1007 switch (mode) {
1008 case HWC2::PowerMode::Off:
1009 a_args.active = false;
1010 break;
1011 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001012 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001013 break;
1014 case HWC2::PowerMode::Doze:
1015 case HWC2::PowerMode::DozeSuspend:
1016 return HWC2::Error::Unsupported;
1017 default:
John Stultzffe783c2024-02-14 10:51:27 -08001018 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001019 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001020 }
1021
1022 if (IsInHeadlessMode()) {
1023 return HWC2::Error::None;
1024 }
1025
Jia Ren80566fe2022-11-17 17:26:00 +08001026 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001027 /*
1028 * Setting the display to active before we have a composition
1029 * can break some drivers, so skip setting a_args.active to
1030 * true, as the next composition frame will implicitly activate
1031 * the display
1032 */
1033 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1034 ? HWC2::Error::None
1035 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001036 };
1037
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001038 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001039 if (err) {
1040 ALOGE("Failed to apply the dpms composition err=%d", err);
1041 return HWC2::Error::BadParameter;
1042 }
1043 return HWC2::Error::None;
1044}
1045
1046HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001047 if (type_ == HWC2::DisplayType::Virtual) {
1048 return HWC2::Error::None;
1049 }
1050
Roman Stratiienko099c3112022-01-20 11:50:54 +02001051 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Drew Davenport63a699e2024-12-13 15:00:00 -07001052
Roman Stratiienko099c3112022-01-20 11:50:54 +02001053 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001054 DrmHwc *hwc = hwc_;
1055 hwc2_display_t id = handle_;
1056 // Callback will be called from the vsync thread.
1057 auto callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
1058 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1059 };
1060 vsync_worker_->SetTimestampCallback(callback);
Drew Davenport63a699e2024-12-13 15:00:00 -07001061 } else {
1062 vsync_worker_->SetTimestampCallback(std::nullopt);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001063 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001064 return HWC2::Error::None;
1065}
1066
1067HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1068 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001069 if (IsInHeadlessMode()) {
1070 *num_types = *num_requests = 0;
1071 return HWC2::Error::None;
1072 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001073
1074 /* In current drm_hwc design in case previous frame layer was not validated as
1075 * a CLIENT, it is used by display controller (Front buffer). We have to store
1076 * this state to provide the CLIENT with the release fences for such buffers.
1077 */
1078 for (auto &l : layers_) {
1079 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1080 HWC2::Composition::Client);
1081 }
1082
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001083 return backend_->ValidateDisplay(this, num_types, num_requests);
1084}
1085
1086std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1087 std::vector<HwcLayer *> ordered_layers;
1088 ordered_layers.reserve(layers_.size());
1089
1090 for (auto &[handle, layer] : layers_) {
1091 ordered_layers.emplace_back(&layer);
1092 }
1093
1094 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1095 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1096 return lhs->GetZOrder() < rhs->GetZOrder();
1097 });
1098
1099 return ordered_layers;
1100}
1101
Roman Stratiienko099c3112022-01-20 11:50:54 +02001102HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1103 uint32_t *outVsyncPeriod /* ns */) {
1104 return GetDisplayAttribute(configs_.active_config_id,
1105 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1106 (int32_t *)(outVsyncPeriod));
1107}
1108
Roman Stratiienko6b405052022-12-10 19:09:10 +02001109#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001110HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001111 if (IsInHeadlessMode()) {
1112 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1113 return HWC2::Error::None;
1114 }
1115 /* Primary display should be always internal,
1116 * otherwise SF will be unhappy and will crash
1117 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001118 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001119 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001120 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001121 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1122 else
1123 return HWC2::Error::BadConfig;
1124
1125 return HWC2::Error::None;
1126}
1127
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001128HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001129 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001130 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1131 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001132 if (type_ == HWC2::DisplayType::Virtual) {
1133 return HWC2::Error::None;
1134 }
1135
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001136 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1137 return HWC2::Error::BadParameter;
1138 }
1139
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001140 uint32_t current_vsync_period{};
1141 GetDisplayVsyncPeriod(&current_vsync_period);
1142
1143 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1144 return HWC2::Error::SeamlessNotAllowed;
1145 }
1146
1147 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1148 ->desiredTimeNanos -
1149 current_vsync_period;
1150 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1151 if (ret != HWC2::Error::None) {
1152 return ret;
1153 }
1154
1155 outTimeline->refreshRequired = true;
1156 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1157 ->desiredTimeNanos;
1158
Drew Davenport33121b72024-12-13 14:59:35 -07001159 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001160
1161 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001162}
1163
1164HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1165 return HWC2::Error::Unsupported;
1166}
1167
1168HWC2::Error HwcDisplay::GetSupportedContentTypes(
1169 uint32_t *outNumSupportedContentTypes,
1170 const uint32_t *outSupportedContentTypes) {
1171 if (outSupportedContentTypes == nullptr)
1172 *outNumSupportedContentTypes = 0;
1173
1174 return HWC2::Error::None;
1175}
1176
1177HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001178 /* Maps exactly to the content_type DRM connector property:
1179 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001180 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001181 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1182 return HWC2::Error::BadParameter;
1183
1184 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001185
1186 return HWC2::Error::None;
1187}
1188#endif
1189
Roman Stratiienko6b405052022-12-10 19:09:10 +02001190#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001191HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1192 uint32_t *outDataSize,
1193 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001194 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001195 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001196 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001197
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001198 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001199 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001200 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001201 }
1202
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001203 *outPort = handle_; /* TDOD(nobody): What should be here? */
1204
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001205 if (outData) {
1206 *outDataSize = std::min(*outDataSize, blob->length);
1207 memcpy(outData, blob->data, *outDataSize);
1208 } else {
1209 *outDataSize = blob->length;
1210 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001211
1212 return HWC2::Error::None;
1213}
1214
1215HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001216 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001217 if (outNumCapabilities == nullptr) {
1218 return HWC2::Error::BadParameter;
1219 }
1220
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001221 bool skip_ctm = false;
1222
1223 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001224 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001225 skip_ctm = true;
1226
1227 // Skip client CTM if DRM can handle it
1228 if (!skip_ctm && !IsInHeadlessMode() &&
1229 GetPipe().crtc->Get()->GetCtmProperty())
1230 skip_ctm = true;
1231
1232 if (!skip_ctm) {
1233 *outNumCapabilities = 0;
1234 return HWC2::Error::None;
1235 }
1236
1237 *outNumCapabilities = 1;
1238 if (outCapabilities) {
1239 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1240 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001241
1242 return HWC2::Error::None;
1243}
1244
1245HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1246 *supported = false;
1247 return HWC2::Error::None;
1248}
1249
1250HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1251 return HWC2::Error::Unsupported;
1252}
1253
Roman Stratiienko6b405052022-12-10 19:09:10 +02001254#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001255
Roman Stratiienko6b405052022-12-10 19:09:10 +02001256#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001257
1258HWC2::Error HwcDisplay::GetRenderIntents(
1259 int32_t mode, uint32_t *outNumIntents,
1260 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1261 if (mode != HAL_COLOR_MODE_NATIVE) {
1262 return HWC2::Error::BadParameter;
1263 }
1264
1265 if (outIntents == nullptr) {
1266 *outNumIntents = 1;
1267 return HWC2::Error::None;
1268 }
1269 *outNumIntents = 1;
1270 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1271 return HWC2::Error::None;
1272}
1273
1274HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1275 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1276 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1277 return HWC2::Error::BadParameter;
1278
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001279 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1280 return HWC2::Error::Unsupported;
1281
Sasha McIntosh5294f092024-09-18 18:14:54 -04001282 auto err = SetColorMode(mode);
1283 if (err != HWC2::Error::None) return err;
1284
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001285 return HWC2::Error::None;
1286}
1287
Roman Stratiienko6b405052022-12-10 19:09:10 +02001288#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001289
1290const Backend *HwcDisplay::backend() const {
1291 return backend_.get();
1292}
1293
1294void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1295 backend_ = std::move(backend);
1296}
1297
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001298} // namespace android