blob: b39d8c589f4ce3662e151a08da68e258d5f55129 [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_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600304 // TODO: There should be a mechanism to wait for this worker to complete,
305 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200306 vsync_worker_->StopThread();
307 vsync_worker_ = {};
308 }
309
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200310 SetClientTarget(nullptr, -1, 0, {});
311}
312
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200313HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200314 ChosePreferredConfig();
315
Drew Davenportb39a3f82024-12-13 15:04:56 -0700316 auto vsw_callbacks = (VSyncWorkerCallbacks){};
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200317
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300318 if (type_ != HWC2::DisplayType::Virtual) {
319 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
320 if (!vsync_worker_) {
321 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
322 return HWC2::Error::BadDisplay;
323 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200324 }
325
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200326 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200327 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200328 if (ret) {
329 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
330 return HWC2::Error::BadDisplay;
331 }
Drew Davenport93443182023-12-14 09:25:45 +0000332 auto flatcbk = (struct FlatConCallbacks){
333 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200334 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 }
336
337 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
338
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400339 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200340
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200341 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200342}
343
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600344std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
345 if (IsInHeadlessMode()) {
346 // The pipeline can be nullptr in headless mode, so return the default
347 // "normal" mode.
348 return PanelOrientation::kModePanelOrientationNormal;
349 }
350
351 DrmDisplayPipeline &pipeline = GetPipe();
352 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
353 ALOGW(
354 "No display pipeline present to query the panel orientation property.");
355 return {};
356 }
357
358 return pipeline.connector->Get()->GetPanelOrientation();
359}
360
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200361HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200362 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300363 if (type_ == HWC2::DisplayType::Virtual) {
364 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
365 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200366 err = configs_.Update(*pipeline_->connector->Get());
367 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300368 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200369 }
370 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200371 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200372 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200373
Roman Stratiienko0137f862022-01-04 18:27:40 +0200374 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200375}
376
377HWC2::Error HwcDisplay::AcceptDisplayChanges() {
378 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
379 l.second.AcceptTypeChange();
380 return HWC2::Error::None;
381}
382
383HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200384 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200385 *layer = static_cast<hwc2_layer_t>(layer_idx_);
386 ++layer_idx_;
387 return HWC2::Error::None;
388}
389
390HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200391 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200392 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200393 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200394
395 layers_.erase(layer);
396 return HWC2::Error::None;
397}
398
399HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700400 // If a config has been queued, it is considered the "active" config.
401 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
402 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200403 return HWC2::Error::BadConfig;
404
Drew Davenportfe70c802024-11-07 13:02:21 -0700405 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200406 return HWC2::Error::None;
407}
408
409HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
410 hwc2_layer_t *layers,
411 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200412 if (IsInHeadlessMode()) {
413 *num_elements = 0;
414 return HWC2::Error::None;
415 }
416
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200417 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300418 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200419 if (l.second.IsTypeChanged()) {
420 if (layers && num_changes < *num_elements)
421 layers[num_changes] = l.first;
422 if (types && num_changes < *num_elements)
423 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
424 ++num_changes;
425 }
426 }
427 if (!layers && !types)
428 *num_elements = num_changes;
429 return HWC2::Error::None;
430}
431
432HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
433 int32_t /*format*/,
434 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200435 if (IsInHeadlessMode()) {
436 return HWC2::Error::None;
437 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200438
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300439 auto min = pipeline_->device->GetMinResolution();
440 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200441
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200442 if (width < min.first || height < min.second)
443 return HWC2::Error::Unsupported;
444
445 if (width > max.first || height > max.second)
446 return HWC2::Error::Unsupported;
447
448 if (dataspace != HAL_DATASPACE_UNKNOWN)
449 return HWC2::Error::Unsupported;
450
451 // TODO(nobody): Validate format can be handled by either GL or planes
452 return HWC2::Error::None;
453}
454
455HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
456 if (!modes)
457 *num_modes = 1;
458
459 if (modes)
460 *modes = HAL_COLOR_MODE_NATIVE;
461
462 return HWC2::Error::None;
463}
464
465HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
466 int32_t attribute_in,
467 int32_t *value) {
468 int conf = static_cast<int>(config);
469
Roman Stratiienko0137f862022-01-04 18:27:40 +0200470 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200471 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200472 return HWC2::Error::BadConfig;
473 }
474
Roman Stratiienko0137f862022-01-04 18:27:40 +0200475 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200476
477 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300478 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
480 switch (attribute) {
481 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200482 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200483 break;
484 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200485 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200486 break;
487 case HWC2::Attribute::VsyncPeriod:
488 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600489 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000491 case HWC2::Attribute::DpiY:
492 // ideally this should be vdisplay/mm_heigth, however mm_height
493 // comes from edid parsing and is highly unreliable. Viewing the
494 // rarity of anisotropic displays, falling back to a single value
495 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 case HWC2::Attribute::DpiX:
497 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200498 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
499 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 : -1;
501 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200502#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200503 case HWC2::Attribute::ConfigGroup:
504 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
505 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200506 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200507 break;
508#endif
509 default:
510 *value = -1;
511 return HWC2::Error::BadConfig;
512 }
513 return HWC2::Error::None;
514}
515
Drew Davenportf7e88332024-09-06 12:54:38 -0600516HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
517 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200519 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200520 if (hwc_config.second.disabled) {
521 continue;
522 }
523
524 if (configs != nullptr) {
525 if (idx >= *num_configs) {
526 break;
527 }
528 configs[idx] = hwc_config.second.id;
529 }
530
531 idx++;
532 }
533 *num_configs = idx;
534 return HWC2::Error::None;
535}
536
537HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
538 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200539 if (IsInHeadlessMode()) {
540 stream << "null-display";
541 } else {
542 stream << "display-" << GetPipe().connector->Get()->GetId();
543 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300544 auto string = stream.str();
545 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546 if (!name) {
547 *size = length;
548 return HWC2::Error::None;
549 }
550
551 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
552 strncpy(name, string.c_str(), *size);
553 return HWC2::Error::None;
554}
555
556HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
557 uint32_t *num_elements,
558 hwc2_layer_t * /*layers*/,
559 int32_t * /*layer_requests*/) {
560 // TODO(nobody): I think virtual display should request
561 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
562 *num_elements = 0;
563 return HWC2::Error::None;
564}
565
566HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
567 *type = static_cast<int32_t>(type_);
568 return HWC2::Error::None;
569}
570
571HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
572 *support = 0;
573 return HWC2::Error::None;
574}
575
576HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
577 int32_t * /*types*/,
578 float * /*max_luminance*/,
579 float * /*max_average_luminance*/,
580 float * /*min_luminance*/) {
581 *num_types = 0;
582 return HWC2::Error::None;
583}
584
585/* Find API details at:
586 * 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 +0300587 *
588 * Called after PresentDisplay(), CLIENT is expecting release fence for the
589 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200590 */
591HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
592 hwc2_layer_t *layers,
593 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200594 if (IsInHeadlessMode()) {
595 *num_elements = 0;
596 return HWC2::Error::None;
597 }
598
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 uint32_t num_layers = 0;
600
Roman Stratiienkodd214942022-05-03 18:24:49 +0300601 for (auto &l : layers_) {
602 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
603 continue;
604 }
605
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300607
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 if (layers == nullptr || fences == nullptr)
609 continue;
610
611 if (num_layers > *num_elements) {
612 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
613 return HWC2::Error::None;
614 }
615
616 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200617 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 }
619 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300620
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200621 return HWC2::Error::None;
622}
623
Drew Davenport97b5abc2024-11-07 10:43:54 -0700624AtomicCommitArgs HwcDisplay::CreateModesetCommit(
625 const HwcDisplayConfig *config,
626 const std::optional<LayerData> &modeset_layer) {
627 AtomicCommitArgs args{};
628
629 args.color_matrix = color_matrix_;
630 args.content_type = content_type_;
631 args.colorspace = colorspace_;
632
633 std::vector<LayerData> composition_layers;
634 if (modeset_layer) {
635 composition_layers.emplace_back(modeset_layer.value());
636 }
637
638 if (composition_layers.empty()) {
639 ALOGW("Attempting to create a modeset commit without a layer.");
640 }
641
642 args.display_mode = config->mode;
643 args.active = true;
644 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
645 std::move(
646 composition_layers));
647 ALOGW_IF(!args.composition, "No composition for blocking modeset");
648
649 return args;
650}
651
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200652HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200653 if (IsInHeadlessMode()) {
654 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
655 return HWC2::Error::None;
656 }
657
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200658 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400659 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400660 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200661
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200662 uint32_t prev_vperiod_ns = 0;
663 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200664
Drew Davenportd387c842024-12-16 16:57:24 -0700665 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700666 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200667 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700668 const HwcDisplayConfig *staged_config = GetConfig(
669 staged_mode_config_id_.value());
670 if (staged_config == nullptr) {
671 return HWC2::Error::BadConfig;
672 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200673 client_layer_.SetLayerDisplayFrame(
674 (hwc_rect_t){.left = 0,
675 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700676 .right = int(staged_config->mode.GetRawMode().hdisplay),
677 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200678
Drew Davenportfe70c802024-11-07 13:02:21 -0700679 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700680 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200681 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700682 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200683 }
684 }
685
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200686 // order the layers by z-order
687 bool use_client_layer = false;
688 uint32_t client_z_order = UINT32_MAX;
689 std::map<uint32_t, HwcLayer *> z_map;
690 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
691 switch (l.second.GetValidatedType()) {
692 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300693 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200694 break;
695 case HWC2::Composition::Client:
696 // Place it at the z_order of the lowest client layer
697 use_client_layer = true;
698 client_z_order = std::min(client_z_order, l.second.GetZOrder());
699 break;
700 default:
701 continue;
702 }
703 }
704 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300705 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200706
707 if (z_map.empty())
708 return HWC2::Error::BadLayer;
709
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200710 std::vector<LayerData> composition_layers;
711
712 /* Import & populate */
713 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200714 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200715 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200716
717 // now that they're ordered by z, add them to the composition
718 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200719 if (!l.second->IsLayerUsableAsDevice()) {
720 /* This will be normally triggered on validation of the first frame
721 * containing CLIENT layer. At this moment client buffer is not yet
722 * provided by the CLIENT.
723 * This may be triggered once in HwcLayer lifecycle in case FB can't be
724 * imported. For example when non-contiguous buffer is imported into
725 * contiguous-only DRM/KMS driver.
726 */
727 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200728 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200729 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200730 }
731
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200732 /* Store plan to ensure shared planes won't be stolen by other display
733 * in between of ValidateDisplay() and PresentDisplay() calls
734 */
735 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
736 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300737
738 if (type_ == HWC2::DisplayType::Virtual) {
739 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
740 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
741 .acquire_fence;
742 }
743
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200744 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700745 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746 return HWC2::Error::BadConfig;
747 }
748
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200749 a_args.composition = current_plan_;
750
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300751 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200752
753 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700754 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200755 return HWC2::Error::BadParameter;
756 }
757
Drew Davenportd387c842024-12-16 16:57:24 -0700758 if (new_vsync_period_ns) {
759 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700760 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700761
762 vsync_worker_->SetVsyncTimestampTracking(false);
763 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
764 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000765 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700766 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000767 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200768 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200769 }
770
771 return HWC2::Error::None;
772}
773
774/* Find API details at:
775 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
776 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300777HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200778 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300779 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200780 return HWC2::Error::None;
781 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200782 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200783
784 ++total_stats_.total_frames_;
785
786 AtomicCommitArgs a_args{};
787 ret = CreateComposition(a_args);
788
789 if (ret != HWC2::Error::None)
790 ++total_stats_.failed_kms_present_;
791
792 if (ret == HWC2::Error::BadLayer) {
793 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300794 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200795 return HWC2::Error::None;
796 }
797 if (ret != HWC2::Error::None)
798 return ret;
799
Roman Stratiienko76892782023-01-16 17:15:53 +0200800 this->present_fence_ = a_args.out_fence;
801 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200802
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200803 // Reset the color matrix so we don't apply it over and over again.
804 color_matrix_ = {};
805
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200806 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700807
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200808 return HWC2::Error::None;
809}
810
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200811HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
812 int64_t change_time) {
813 if (configs_.hwc_configs.count(config) == 0) {
814 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200815 return HWC2::Error::BadConfig;
816 }
817
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200818 staged_mode_change_time_ = change_time;
819 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200820
821 return HWC2::Error::None;
822}
823
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200824HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
825 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
826}
827
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200828/* Find API details at:
829 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
830 */
831HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
832 int32_t acquire_fence,
833 int32_t dataspace,
834 hwc_region_t /*damage*/) {
835 client_layer_.SetLayerBuffer(target, acquire_fence);
836 client_layer_.SetLayerDataspace(dataspace);
837
838 /*
839 * target can be nullptr, this does mean the Composer Service is calling
840 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
841 * 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
842 */
843 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300844 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 return HWC2::Error::None;
846 }
847
Roman Stratiienko5070d512022-05-30 13:41:20 +0300848 if (IsInHeadlessMode()) {
849 return HWC2::Error::None;
850 }
851
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200852 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200853 if (!client_layer_.IsLayerUsableAsDevice()) {
854 ALOGE("Client layer must be always usable by DRM/KMS");
855 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200856 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200857
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200858 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300859 if (!bi) {
860 ALOGE("%s: Invalid state", __func__);
861 return HWC2::Error::BadLayer;
862 }
863
864 auto source_crop = (hwc_frect_t){.left = 0.0F,
865 .top = 0.0F,
866 .right = static_cast<float>(bi->width),
867 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200868 client_layer_.SetLayerSourceCrop(source_crop);
869
870 return HWC2::Error::None;
871}
872
873HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400874 /* Maps to the Colorspace DRM connector property:
875 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
876 */
877 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200878 return HWC2::Error::BadParameter;
879
Sasha McIntosh5294f092024-09-18 18:14:54 -0400880 switch (mode) {
881 case HAL_COLOR_MODE_NATIVE:
882 colorspace_ = Colorspace::kDefault;
883 break;
884 case HAL_COLOR_MODE_STANDARD_BT601_625:
885 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
886 case HAL_COLOR_MODE_STANDARD_BT601_525:
887 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
888 // The DP spec does not say whether this is the 525 or the 625 line version.
889 colorspace_ = Colorspace::kBt601Ycc;
890 break;
891 case HAL_COLOR_MODE_STANDARD_BT709:
892 case HAL_COLOR_MODE_SRGB:
893 colorspace_ = Colorspace::kBt709Ycc;
894 break;
895 case HAL_COLOR_MODE_DCI_P3:
896 case HAL_COLOR_MODE_DISPLAY_P3:
897 colorspace_ = Colorspace::kDciP3RgbD65;
898 break;
899 case HAL_COLOR_MODE_ADOBE_RGB:
900 default:
901 return HWC2::Error::Unsupported;
902 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200903
904 color_mode_ = mode;
905 return HWC2::Error::None;
906}
907
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200908#include <xf86drmMode.h>
909
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400910static uint64_t To3132FixPt(float in) {
911 constexpr uint64_t kSignMask = (1ULL << 63);
912 constexpr uint64_t kValueMask = ~(1ULL << 63);
913 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
914 if (in < 0)
915 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
916 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
917}
918
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200919HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
920 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
921 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
922 return HWC2::Error::BadParameter;
923
924 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
925 return HWC2::Error::BadParameter;
926
927 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200928
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200929 if (IsInHeadlessMode())
930 return HWC2::Error::None;
931
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200932 if (!GetPipe().crtc->Get()->GetCtmProperty())
933 return HWC2::Error::None;
934
935 switch (color_transform_hint_) {
936 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400937 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200938 break;
939 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400940 // Without HW support, we cannot correctly process matrices with an offset.
941 for (int i = 12; i < 14; i++) {
942 if (matrix[i] != 0.F)
943 return HWC2::Error::Unsupported;
944 }
945
946 /* HAL provides a 4x4 float type matrix:
947 * | 0 1 2 3|
948 * | 4 5 6 7|
949 * | 8 9 10 11|
950 * |12 13 14 15|
951 *
952 * R_out = R*0 + G*4 + B*8 + 12
953 * G_out = R*1 + G*5 + B*9 + 13
954 * B_out = R*2 + G*6 + B*10 + 14
955 *
956 * DRM expects a 3x3 s31.32 fixed point matrix:
957 * out matrix in
958 * |R| |0 1 2| |R|
959 * |G| = |3 4 5| x |G|
960 * |B| |6 7 8| |B|
961 *
962 * R_out = R*0 + G*1 + B*2
963 * G_out = R*3 + G*4 + B*5
964 * B_out = R*6 + G*7 + B*8
965 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200966 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200967 for (int i = 0; i < kCtmCols; i++) {
968 for (int j = 0; j < kCtmRows; j++) {
969 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400970 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200971 }
972 }
973 break;
974 default:
975 return HWC2::Error::Unsupported;
976 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200977
978 return HWC2::Error::None;
979}
980
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200981bool HwcDisplay::CtmByGpu() {
982 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
983 return false;
984
985 if (GetPipe().crtc->Get()->GetCtmProperty())
986 return false;
987
Drew Davenport93443182023-12-14 09:25:45 +0000988 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200989 return false;
990
991 return true;
992}
993
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300994HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
995 int32_t release_fence) {
996 writeback_layer_->SetLayerBuffer(buffer, release_fence);
997 writeback_layer_->PopulateLayerData();
998 if (!writeback_layer_->IsLayerUsableAsDevice()) {
999 ALOGE("Output layer must be always usable by DRM/KMS");
1000 return HWC2::Error::BadLayer;
1001 }
1002 /* TODO: Check if format is supported by writeback connector */
1003 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001004}
1005
1006HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1007 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001008
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001009 AtomicCommitArgs a_args{};
1010
1011 switch (mode) {
1012 case HWC2::PowerMode::Off:
1013 a_args.active = false;
1014 break;
1015 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001016 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001017 break;
1018 case HWC2::PowerMode::Doze:
1019 case HWC2::PowerMode::DozeSuspend:
1020 return HWC2::Error::Unsupported;
1021 default:
John Stultzffe783c2024-02-14 10:51:27 -08001022 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001023 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001024 }
1025
1026 if (IsInHeadlessMode()) {
1027 return HWC2::Error::None;
1028 }
1029
Jia Ren80566fe2022-11-17 17:26:00 +08001030 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001031 /*
1032 * Setting the display to active before we have a composition
1033 * can break some drivers, so skip setting a_args.active to
1034 * true, as the next composition frame will implicitly activate
1035 * the display
1036 */
1037 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1038 ? HWC2::Error::None
1039 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001040 };
1041
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001042 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001043 if (err) {
1044 ALOGE("Failed to apply the dpms composition err=%d", err);
1045 return HWC2::Error::BadParameter;
1046 }
1047 return HWC2::Error::None;
1048}
1049
1050HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001051 if (type_ == HWC2::DisplayType::Virtual) {
1052 return HWC2::Error::None;
1053 }
1054
Roman Stratiienko099c3112022-01-20 11:50:54 +02001055 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Drew Davenport63a699e2024-12-13 15:00:00 -07001056
Roman Stratiienko099c3112022-01-20 11:50:54 +02001057 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001058 DrmHwc *hwc = hwc_;
1059 hwc2_display_t id = handle_;
1060 // Callback will be called from the vsync thread.
1061 auto callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
1062 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1063 };
1064 vsync_worker_->SetTimestampCallback(callback);
Drew Davenport63a699e2024-12-13 15:00:00 -07001065 } else {
1066 vsync_worker_->SetTimestampCallback(std::nullopt);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001067 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001068 return HWC2::Error::None;
1069}
1070
1071HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1072 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001073 if (IsInHeadlessMode()) {
1074 *num_types = *num_requests = 0;
1075 return HWC2::Error::None;
1076 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001077
1078 /* In current drm_hwc design in case previous frame layer was not validated as
1079 * a CLIENT, it is used by display controller (Front buffer). We have to store
1080 * this state to provide the CLIENT with the release fences for such buffers.
1081 */
1082 for (auto &l : layers_) {
1083 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1084 HWC2::Composition::Client);
1085 }
1086
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001087 return backend_->ValidateDisplay(this, num_types, num_requests);
1088}
1089
1090std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1091 std::vector<HwcLayer *> ordered_layers;
1092 ordered_layers.reserve(layers_.size());
1093
1094 for (auto &[handle, layer] : layers_) {
1095 ordered_layers.emplace_back(&layer);
1096 }
1097
1098 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1099 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1100 return lhs->GetZOrder() < rhs->GetZOrder();
1101 });
1102
1103 return ordered_layers;
1104}
1105
Roman Stratiienko099c3112022-01-20 11:50:54 +02001106HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1107 uint32_t *outVsyncPeriod /* ns */) {
1108 return GetDisplayAttribute(configs_.active_config_id,
1109 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1110 (int32_t *)(outVsyncPeriod));
1111}
1112
Roman Stratiienko6b405052022-12-10 19:09:10 +02001113#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001114HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001115 if (IsInHeadlessMode()) {
1116 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1117 return HWC2::Error::None;
1118 }
1119 /* Primary display should be always internal,
1120 * otherwise SF will be unhappy and will crash
1121 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001122 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001123 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001124 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001125 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1126 else
1127 return HWC2::Error::BadConfig;
1128
1129 return HWC2::Error::None;
1130}
1131
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001132HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001133 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001134 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1135 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001136 if (type_ == HWC2::DisplayType::Virtual) {
1137 return HWC2::Error::None;
1138 }
1139
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001140 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1141 return HWC2::Error::BadParameter;
1142 }
1143
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001144 uint32_t current_vsync_period{};
1145 GetDisplayVsyncPeriod(&current_vsync_period);
1146
1147 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1148 return HWC2::Error::SeamlessNotAllowed;
1149 }
1150
1151 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1152 ->desiredTimeNanos -
1153 current_vsync_period;
1154 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1155 if (ret != HWC2::Error::None) {
1156 return ret;
1157 }
1158
1159 outTimeline->refreshRequired = true;
1160 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1161 ->desiredTimeNanos;
1162
Drew Davenport33121b72024-12-13 14:59:35 -07001163 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001164
1165 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001166}
1167
1168HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1169 return HWC2::Error::Unsupported;
1170}
1171
1172HWC2::Error HwcDisplay::GetSupportedContentTypes(
1173 uint32_t *outNumSupportedContentTypes,
1174 const uint32_t *outSupportedContentTypes) {
1175 if (outSupportedContentTypes == nullptr)
1176 *outNumSupportedContentTypes = 0;
1177
1178 return HWC2::Error::None;
1179}
1180
1181HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001182 /* Maps exactly to the content_type DRM connector property:
1183 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001184 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001185 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1186 return HWC2::Error::BadParameter;
1187
1188 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001189
1190 return HWC2::Error::None;
1191}
1192#endif
1193
Roman Stratiienko6b405052022-12-10 19:09:10 +02001194#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001195HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1196 uint32_t *outDataSize,
1197 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001198 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001199 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001200 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001201
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001202 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001203 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001204 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001205 }
1206
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001207 *outPort = handle_; /* TDOD(nobody): What should be here? */
1208
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001209 if (outData) {
1210 *outDataSize = std::min(*outDataSize, blob->length);
1211 memcpy(outData, blob->data, *outDataSize);
1212 } else {
1213 *outDataSize = blob->length;
1214 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001215
1216 return HWC2::Error::None;
1217}
1218
1219HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001220 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001221 if (outNumCapabilities == nullptr) {
1222 return HWC2::Error::BadParameter;
1223 }
1224
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001225 bool skip_ctm = false;
1226
1227 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001228 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001229 skip_ctm = true;
1230
1231 // Skip client CTM if DRM can handle it
1232 if (!skip_ctm && !IsInHeadlessMode() &&
1233 GetPipe().crtc->Get()->GetCtmProperty())
1234 skip_ctm = true;
1235
1236 if (!skip_ctm) {
1237 *outNumCapabilities = 0;
1238 return HWC2::Error::None;
1239 }
1240
1241 *outNumCapabilities = 1;
1242 if (outCapabilities) {
1243 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1244 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001245
1246 return HWC2::Error::None;
1247}
1248
1249HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1250 *supported = false;
1251 return HWC2::Error::None;
1252}
1253
1254HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1255 return HWC2::Error::Unsupported;
1256}
1257
Roman Stratiienko6b405052022-12-10 19:09:10 +02001258#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001259
Roman Stratiienko6b405052022-12-10 19:09:10 +02001260#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001261
1262HWC2::Error HwcDisplay::GetRenderIntents(
1263 int32_t mode, uint32_t *outNumIntents,
1264 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1265 if (mode != HAL_COLOR_MODE_NATIVE) {
1266 return HWC2::Error::BadParameter;
1267 }
1268
1269 if (outIntents == nullptr) {
1270 *outNumIntents = 1;
1271 return HWC2::Error::None;
1272 }
1273 *outNumIntents = 1;
1274 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1275 return HWC2::Error::None;
1276}
1277
1278HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1279 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1280 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1281 return HWC2::Error::BadParameter;
1282
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001283 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1284 return HWC2::Error::Unsupported;
1285
Sasha McIntosh5294f092024-09-18 18:14:54 -04001286 auto err = SetColorMode(mode);
1287 if (err != HWC2::Error::None) return err;
1288
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001289 return HWC2::Error::None;
1290}
1291
Roman Stratiienko6b405052022-12-10 19:09:10 +02001292#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001293
1294const Backend *HwcDisplay::backend() const {
1295 return backend_.get();
1296}
1297
1298void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1299 backend_ = std::move(backend);
1300}
1301
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001302} // namespace android