blob: 43564d4cbf1694ef98e6a884eda6fb4643df8fed [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;
196 const uint32_t height = new_config->mode.GetRawMode().hdisplay;
197
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;
234 return ConfigError::kNone;
235}
236
Drew Davenport8998f8b2024-10-24 10:15:12 -0600237auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
238 bool seamless, QueuedConfigTiming *out_timing)
239 -> ConfigError {
240 if (configs_.hwc_configs.count(config) == 0) {
241 ALOGE("Could not find active mode for %u", config);
242 return ConfigError::kBadConfig;
243 }
244
245 // TODO: Add support for seamless configuration changes.
246 if (seamless) {
247 return ConfigError::kSeamlessNotAllowed;
248 }
249
250 // Request a refresh from the client one vsync period before the desired
251 // time, or simply at the desired time if there is no active configuration.
252 const HwcDisplayConfig *current_config = GetCurrentConfig();
253 out_timing->refresh_time_ns = desired_time -
254 (current_config
255 ? current_config->mode.GetVSyncPeriodNs()
256 : 0);
257 out_timing->new_vsync_time_ns = desired_time;
258
259 // Queue the config change timing to be consistent with the requested
260 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600261 staged_mode_change_time_ = out_timing->refresh_time_ns;
262 staged_mode_config_id_ = config;
263
264 // Enable vsync events until the mode has been applied.
265 last_vsync_ts_ = 0;
266 vsync_tracking_en_ = true;
267 vsync_worker_->VSyncControl(true);
268
269 return ConfigError::kNone;
270}
271
Roman Stratiienko63762a92023-09-18 22:33:45 +0300272void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200273 Deinit();
274
Roman Stratiienko63762a92023-09-18 22:33:45 +0300275 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200276
Roman Stratiienko63762a92023-09-18 22:33:45 +0300277 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200278 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000279 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200280 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000281 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200282 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200283}
284
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200285void HwcDisplay::Deinit() {
286 if (pipeline_ != nullptr) {
287 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200288 a_args.composition = std::make_shared<DrmKmsPlan>();
289 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300290 a_args.composition = {};
291 a_args.active = false;
292 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200293
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200294 current_plan_.reset();
295 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200296 if (flatcon_) {
297 flatcon_->StopThread();
298 flatcon_.reset();
299 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200300 }
301
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200302 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600303 // TODO: There should be a mechanism to wait for this worker to complete,
304 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200305 vsync_worker_->StopThread();
306 vsync_worker_ = {};
307 }
308
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200309 SetClientTarget(nullptr, -1, 0, {});
310}
311
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200312HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200313 ChosePreferredConfig();
314
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200315 auto vsw_callbacks = (VSyncWorkerCallbacks){
316 .out_event =
317 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000318 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200319 if (vsync_event_en_) {
320 uint32_t period_ns{};
321 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000322 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200323 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200324 if (vsync_tracking_en_) {
325 last_vsync_ts_ = timestamp;
326 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200327 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200328 vsync_worker_->VSyncControl(false);
329 }
330 },
331 .get_vperiod_ns = [this]() -> uint32_t {
332 uint32_t outVsyncPeriod = 0;
333 GetDisplayVsyncPeriod(&outVsyncPeriod);
334 return outVsyncPeriod;
335 },
336 };
337
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300338 if (type_ != HWC2::DisplayType::Virtual) {
339 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
340 if (!vsync_worker_) {
341 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
342 return HWC2::Error::BadDisplay;
343 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200344 }
345
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200346 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200347 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200348 if (ret) {
349 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
350 return HWC2::Error::BadDisplay;
351 }
Drew Davenport93443182023-12-14 09:25:45 +0000352 auto flatcbk = (struct FlatConCallbacks){
353 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200354 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200355 }
356
357 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
358
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400359 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200360
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200361 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200362}
363
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600364std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
365 if (IsInHeadlessMode()) {
366 // The pipeline can be nullptr in headless mode, so return the default
367 // "normal" mode.
368 return PanelOrientation::kModePanelOrientationNormal;
369 }
370
371 DrmDisplayPipeline &pipeline = GetPipe();
372 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
373 ALOGW(
374 "No display pipeline present to query the panel orientation property.");
375 return {};
376 }
377
378 return pipeline.connector->Get()->GetPanelOrientation();
379}
380
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200381HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200382 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300383 if (type_ == HWC2::DisplayType::Virtual) {
384 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
385 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200386 err = configs_.Update(*pipeline_->connector->Get());
387 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300388 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200389 }
390 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200391 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200392 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200393
Roman Stratiienko0137f862022-01-04 18:27:40 +0200394 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200395}
396
397HWC2::Error HwcDisplay::AcceptDisplayChanges() {
398 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
399 l.second.AcceptTypeChange();
400 return HWC2::Error::None;
401}
402
403HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200404 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200405 *layer = static_cast<hwc2_layer_t>(layer_idx_);
406 ++layer_idx_;
407 return HWC2::Error::None;
408}
409
410HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200411 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200412 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200413 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200414
415 layers_.erase(layer);
416 return HWC2::Error::None;
417}
418
419HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700420 // If a config has been queued, it is considered the "active" config.
421 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
422 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200423 return HWC2::Error::BadConfig;
424
Drew Davenportfe70c802024-11-07 13:02:21 -0700425 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200426 return HWC2::Error::None;
427}
428
429HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
430 hwc2_layer_t *layers,
431 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200432 if (IsInHeadlessMode()) {
433 *num_elements = 0;
434 return HWC2::Error::None;
435 }
436
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300438 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200439 if (l.second.IsTypeChanged()) {
440 if (layers && num_changes < *num_elements)
441 layers[num_changes] = l.first;
442 if (types && num_changes < *num_elements)
443 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
444 ++num_changes;
445 }
446 }
447 if (!layers && !types)
448 *num_elements = num_changes;
449 return HWC2::Error::None;
450}
451
452HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
453 int32_t /*format*/,
454 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200455 if (IsInHeadlessMode()) {
456 return HWC2::Error::None;
457 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200458
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300459 auto min = pipeline_->device->GetMinResolution();
460 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200461
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200462 if (width < min.first || height < min.second)
463 return HWC2::Error::Unsupported;
464
465 if (width > max.first || height > max.second)
466 return HWC2::Error::Unsupported;
467
468 if (dataspace != HAL_DATASPACE_UNKNOWN)
469 return HWC2::Error::Unsupported;
470
471 // TODO(nobody): Validate format can be handled by either GL or planes
472 return HWC2::Error::None;
473}
474
475HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
476 if (!modes)
477 *num_modes = 1;
478
479 if (modes)
480 *modes = HAL_COLOR_MODE_NATIVE;
481
482 return HWC2::Error::None;
483}
484
485HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
486 int32_t attribute_in,
487 int32_t *value) {
488 int conf = static_cast<int>(config);
489
Roman Stratiienko0137f862022-01-04 18:27:40 +0200490 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200491 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200492 return HWC2::Error::BadConfig;
493 }
494
Roman Stratiienko0137f862022-01-04 18:27:40 +0200495 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496
497 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300498 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200499 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
500 switch (attribute) {
501 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200502 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200503 break;
504 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200505 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200506 break;
507 case HWC2::Attribute::VsyncPeriod:
508 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600509 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000511 case HWC2::Attribute::DpiY:
512 // ideally this should be vdisplay/mm_heigth, however mm_height
513 // comes from edid parsing and is highly unreliable. Viewing the
514 // rarity of anisotropic displays, falling back to a single value
515 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200516 case HWC2::Attribute::DpiX:
517 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200518 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
519 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200520 : -1;
521 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200522#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200523 case HWC2::Attribute::ConfigGroup:
524 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
525 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200526 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200527 break;
528#endif
529 default:
530 *value = -1;
531 return HWC2::Error::BadConfig;
532 }
533 return HWC2::Error::None;
534}
535
Drew Davenportf7e88332024-09-06 12:54:38 -0600536HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
537 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200538 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200539 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200540 if (hwc_config.second.disabled) {
541 continue;
542 }
543
544 if (configs != nullptr) {
545 if (idx >= *num_configs) {
546 break;
547 }
548 configs[idx] = hwc_config.second.id;
549 }
550
551 idx++;
552 }
553 *num_configs = idx;
554 return HWC2::Error::None;
555}
556
557HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
558 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200559 if (IsInHeadlessMode()) {
560 stream << "null-display";
561 } else {
562 stream << "display-" << GetPipe().connector->Get()->GetId();
563 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300564 auto string = stream.str();
565 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566 if (!name) {
567 *size = length;
568 return HWC2::Error::None;
569 }
570
571 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
572 strncpy(name, string.c_str(), *size);
573 return HWC2::Error::None;
574}
575
576HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
577 uint32_t *num_elements,
578 hwc2_layer_t * /*layers*/,
579 int32_t * /*layer_requests*/) {
580 // TODO(nobody): I think virtual display should request
581 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
582 *num_elements = 0;
583 return HWC2::Error::None;
584}
585
586HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
587 *type = static_cast<int32_t>(type_);
588 return HWC2::Error::None;
589}
590
591HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
592 *support = 0;
593 return HWC2::Error::None;
594}
595
596HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
597 int32_t * /*types*/,
598 float * /*max_luminance*/,
599 float * /*max_average_luminance*/,
600 float * /*min_luminance*/) {
601 *num_types = 0;
602 return HWC2::Error::None;
603}
604
605/* Find API details at:
606 * 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 +0300607 *
608 * Called after PresentDisplay(), CLIENT is expecting release fence for the
609 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200610 */
611HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
612 hwc2_layer_t *layers,
613 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200614 if (IsInHeadlessMode()) {
615 *num_elements = 0;
616 return HWC2::Error::None;
617 }
618
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200619 uint32_t num_layers = 0;
620
Roman Stratiienkodd214942022-05-03 18:24:49 +0300621 for (auto &l : layers_) {
622 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
623 continue;
624 }
625
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200626 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300627
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200628 if (layers == nullptr || fences == nullptr)
629 continue;
630
631 if (num_layers > *num_elements) {
632 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
633 return HWC2::Error::None;
634 }
635
636 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200637 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200638 }
639 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300640
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641 return HWC2::Error::None;
642}
643
Drew Davenport97b5abc2024-11-07 10:43:54 -0700644AtomicCommitArgs HwcDisplay::CreateModesetCommit(
645 const HwcDisplayConfig *config,
646 const std::optional<LayerData> &modeset_layer) {
647 AtomicCommitArgs args{};
648
649 args.color_matrix = color_matrix_;
650 args.content_type = content_type_;
651 args.colorspace = colorspace_;
652
653 std::vector<LayerData> composition_layers;
654 if (modeset_layer) {
655 composition_layers.emplace_back(modeset_layer.value());
656 }
657
658 if (composition_layers.empty()) {
659 ALOGW("Attempting to create a modeset commit without a layer.");
660 }
661
662 args.display_mode = config->mode;
663 args.active = true;
664 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
665 std::move(
666 composition_layers));
667 ALOGW_IF(!args.composition, "No composition for blocking modeset");
668
669 return args;
670}
671
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200672HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200673 if (IsInHeadlessMode()) {
674 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
675 return HWC2::Error::None;
676 }
677
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200678 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400679 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400680 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200681
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200682 uint32_t prev_vperiod_ns = 0;
683 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200684
685 auto mode_update_commited_ = false;
Drew Davenportfe70c802024-11-07 13:02:21 -0700686 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200687 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700688 const HwcDisplayConfig *staged_config = GetConfig(
689 staged_mode_config_id_.value());
690 if (staged_config == nullptr) {
691 return HWC2::Error::BadConfig;
692 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200693 client_layer_.SetLayerDisplayFrame(
694 (hwc_rect_t){.left = 0,
695 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700696 .right = int(staged_config->mode.GetRawMode().hdisplay),
697 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200698
Drew Davenportfe70c802024-11-07 13:02:21 -0700699 configs_.active_config_id = staged_mode_config_id_.value();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200700
Drew Davenportfe70c802024-11-07 13:02:21 -0700701 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200702 if (!a_args.test_only) {
703 mode_update_commited_ = true;
704 }
705 }
706
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200707 // order the layers by z-order
708 bool use_client_layer = false;
709 uint32_t client_z_order = UINT32_MAX;
710 std::map<uint32_t, HwcLayer *> z_map;
711 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
712 switch (l.second.GetValidatedType()) {
713 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300714 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200715 break;
716 case HWC2::Composition::Client:
717 // Place it at the z_order of the lowest client layer
718 use_client_layer = true;
719 client_z_order = std::min(client_z_order, l.second.GetZOrder());
720 break;
721 default:
722 continue;
723 }
724 }
725 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300726 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200727
728 if (z_map.empty())
729 return HWC2::Error::BadLayer;
730
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200731 std::vector<LayerData> composition_layers;
732
733 /* Import & populate */
734 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200735 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200736 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200737
738 // now that they're ordered by z, add them to the composition
739 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200740 if (!l.second->IsLayerUsableAsDevice()) {
741 /* This will be normally triggered on validation of the first frame
742 * containing CLIENT layer. At this moment client buffer is not yet
743 * provided by the CLIENT.
744 * This may be triggered once in HwcLayer lifecycle in case FB can't be
745 * imported. For example when non-contiguous buffer is imported into
746 * contiguous-only DRM/KMS driver.
747 */
748 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200749 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200750 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200751 }
752
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200753 /* Store plan to ensure shared planes won't be stolen by other display
754 * in between of ValidateDisplay() and PresentDisplay() calls
755 */
756 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
757 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300758
759 if (type_ == HWC2::DisplayType::Virtual) {
760 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
761 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
762 .acquire_fence;
763 }
764
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200765 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700766 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200767 return HWC2::Error::BadConfig;
768 }
769
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200770 a_args.composition = current_plan_;
771
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300772 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200773
774 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700775 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200776 return HWC2::Error::BadParameter;
777 }
778
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200779 if (mode_update_commited_) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700780 staged_mode_config_id_.reset();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200781 vsync_tracking_en_ = false;
782 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000783 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
784 last_vsync_ts_ +
785 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200786 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200787 }
788
789 return HWC2::Error::None;
790}
791
792/* Find API details at:
793 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
794 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300795HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200796 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300797 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200798 return HWC2::Error::None;
799 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200800 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200801
802 ++total_stats_.total_frames_;
803
804 AtomicCommitArgs a_args{};
805 ret = CreateComposition(a_args);
806
807 if (ret != HWC2::Error::None)
808 ++total_stats_.failed_kms_present_;
809
810 if (ret == HWC2::Error::BadLayer) {
811 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300812 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200813 return HWC2::Error::None;
814 }
815 if (ret != HWC2::Error::None)
816 return ret;
817
Roman Stratiienko76892782023-01-16 17:15:53 +0200818 this->present_fence_ = a_args.out_fence;
819 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200820
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200821 // Reset the color matrix so we don't apply it over and over again.
822 color_matrix_ = {};
823
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200824 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700825
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200826 return HWC2::Error::None;
827}
828
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200829HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
830 int64_t change_time) {
831 if (configs_.hwc_configs.count(config) == 0) {
832 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200833 return HWC2::Error::BadConfig;
834 }
835
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200836 staged_mode_change_time_ = change_time;
837 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838
839 return HWC2::Error::None;
840}
841
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200842HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
843 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
844}
845
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200846/* Find API details at:
847 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
848 */
849HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
850 int32_t acquire_fence,
851 int32_t dataspace,
852 hwc_region_t /*damage*/) {
853 client_layer_.SetLayerBuffer(target, acquire_fence);
854 client_layer_.SetLayerDataspace(dataspace);
855
856 /*
857 * target can be nullptr, this does mean the Composer Service is calling
858 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
859 * 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
860 */
861 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300862 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200863 return HWC2::Error::None;
864 }
865
Roman Stratiienko5070d512022-05-30 13:41:20 +0300866 if (IsInHeadlessMode()) {
867 return HWC2::Error::None;
868 }
869
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200870 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200871 if (!client_layer_.IsLayerUsableAsDevice()) {
872 ALOGE("Client layer must be always usable by DRM/KMS");
873 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200874 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200875
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200876 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300877 if (!bi) {
878 ALOGE("%s: Invalid state", __func__);
879 return HWC2::Error::BadLayer;
880 }
881
882 auto source_crop = (hwc_frect_t){.left = 0.0F,
883 .top = 0.0F,
884 .right = static_cast<float>(bi->width),
885 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200886 client_layer_.SetLayerSourceCrop(source_crop);
887
888 return HWC2::Error::None;
889}
890
891HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400892 /* Maps to the Colorspace DRM connector property:
893 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
894 */
895 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200896 return HWC2::Error::BadParameter;
897
Sasha McIntosh5294f092024-09-18 18:14:54 -0400898 switch (mode) {
899 case HAL_COLOR_MODE_NATIVE:
900 colorspace_ = Colorspace::kDefault;
901 break;
902 case HAL_COLOR_MODE_STANDARD_BT601_625:
903 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
904 case HAL_COLOR_MODE_STANDARD_BT601_525:
905 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
906 // The DP spec does not say whether this is the 525 or the 625 line version.
907 colorspace_ = Colorspace::kBt601Ycc;
908 break;
909 case HAL_COLOR_MODE_STANDARD_BT709:
910 case HAL_COLOR_MODE_SRGB:
911 colorspace_ = Colorspace::kBt709Ycc;
912 break;
913 case HAL_COLOR_MODE_DCI_P3:
914 case HAL_COLOR_MODE_DISPLAY_P3:
915 colorspace_ = Colorspace::kDciP3RgbD65;
916 break;
917 case HAL_COLOR_MODE_ADOBE_RGB:
918 default:
919 return HWC2::Error::Unsupported;
920 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200921
922 color_mode_ = mode;
923 return HWC2::Error::None;
924}
925
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200926#include <xf86drmMode.h>
927
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400928static uint64_t To3132FixPt(float in) {
929 constexpr uint64_t kSignMask = (1ULL << 63);
930 constexpr uint64_t kValueMask = ~(1ULL << 63);
931 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
932 if (in < 0)
933 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
934 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
935}
936
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200937HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
938 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
939 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
940 return HWC2::Error::BadParameter;
941
942 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
943 return HWC2::Error::BadParameter;
944
945 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200946
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200947 if (IsInHeadlessMode())
948 return HWC2::Error::None;
949
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200950 if (!GetPipe().crtc->Get()->GetCtmProperty())
951 return HWC2::Error::None;
952
953 switch (color_transform_hint_) {
954 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400955 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200956 break;
957 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400958 // Without HW support, we cannot correctly process matrices with an offset.
959 for (int i = 12; i < 14; i++) {
960 if (matrix[i] != 0.F)
961 return HWC2::Error::Unsupported;
962 }
963
964 /* HAL provides a 4x4 float type matrix:
965 * | 0 1 2 3|
966 * | 4 5 6 7|
967 * | 8 9 10 11|
968 * |12 13 14 15|
969 *
970 * R_out = R*0 + G*4 + B*8 + 12
971 * G_out = R*1 + G*5 + B*9 + 13
972 * B_out = R*2 + G*6 + B*10 + 14
973 *
974 * DRM expects a 3x3 s31.32 fixed point matrix:
975 * out matrix in
976 * |R| |0 1 2| |R|
977 * |G| = |3 4 5| x |G|
978 * |B| |6 7 8| |B|
979 *
980 * R_out = R*0 + G*1 + B*2
981 * G_out = R*3 + G*4 + B*5
982 * B_out = R*6 + G*7 + B*8
983 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200984 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200985 for (int i = 0; i < kCtmCols; i++) {
986 for (int j = 0; j < kCtmRows; j++) {
987 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400988 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200989 }
990 }
991 break;
992 default:
993 return HWC2::Error::Unsupported;
994 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995
996 return HWC2::Error::None;
997}
998
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200999bool HwcDisplay::CtmByGpu() {
1000 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1001 return false;
1002
1003 if (GetPipe().crtc->Get()->GetCtmProperty())
1004 return false;
1005
Drew Davenport93443182023-12-14 09:25:45 +00001006 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001007 return false;
1008
1009 return true;
1010}
1011
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001012HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1013 int32_t release_fence) {
1014 writeback_layer_->SetLayerBuffer(buffer, release_fence);
1015 writeback_layer_->PopulateLayerData();
1016 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1017 ALOGE("Output layer must be always usable by DRM/KMS");
1018 return HWC2::Error::BadLayer;
1019 }
1020 /* TODO: Check if format is supported by writeback connector */
1021 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001022}
1023
1024HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1025 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001026
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001027 AtomicCommitArgs a_args{};
1028
1029 switch (mode) {
1030 case HWC2::PowerMode::Off:
1031 a_args.active = false;
1032 break;
1033 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001034 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001035 break;
1036 case HWC2::PowerMode::Doze:
1037 case HWC2::PowerMode::DozeSuspend:
1038 return HWC2::Error::Unsupported;
1039 default:
John Stultzffe783c2024-02-14 10:51:27 -08001040 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001041 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001042 }
1043
1044 if (IsInHeadlessMode()) {
1045 return HWC2::Error::None;
1046 }
1047
Jia Ren80566fe2022-11-17 17:26:00 +08001048 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001049 /*
1050 * Setting the display to active before we have a composition
1051 * can break some drivers, so skip setting a_args.active to
1052 * true, as the next composition frame will implicitly activate
1053 * the display
1054 */
1055 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1056 ? HWC2::Error::None
1057 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001058 };
1059
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001060 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001061 if (err) {
1062 ALOGE("Failed to apply the dpms composition err=%d", err);
1063 return HWC2::Error::BadParameter;
1064 }
1065 return HWC2::Error::None;
1066}
1067
1068HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001069 if (type_ == HWC2::DisplayType::Virtual) {
1070 return HWC2::Error::None;
1071 }
1072
Roman Stratiienko099c3112022-01-20 11:50:54 +02001073 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
1074 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001075 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001076 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001077 return HWC2::Error::None;
1078}
1079
1080HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1081 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001082 if (IsInHeadlessMode()) {
1083 *num_types = *num_requests = 0;
1084 return HWC2::Error::None;
1085 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001086
1087 /* In current drm_hwc design in case previous frame layer was not validated as
1088 * a CLIENT, it is used by display controller (Front buffer). We have to store
1089 * this state to provide the CLIENT with the release fences for such buffers.
1090 */
1091 for (auto &l : layers_) {
1092 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1093 HWC2::Composition::Client);
1094 }
1095
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001096 return backend_->ValidateDisplay(this, num_types, num_requests);
1097}
1098
1099std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1100 std::vector<HwcLayer *> ordered_layers;
1101 ordered_layers.reserve(layers_.size());
1102
1103 for (auto &[handle, layer] : layers_) {
1104 ordered_layers.emplace_back(&layer);
1105 }
1106
1107 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1108 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1109 return lhs->GetZOrder() < rhs->GetZOrder();
1110 });
1111
1112 return ordered_layers;
1113}
1114
Roman Stratiienko099c3112022-01-20 11:50:54 +02001115HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1116 uint32_t *outVsyncPeriod /* ns */) {
1117 return GetDisplayAttribute(configs_.active_config_id,
1118 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1119 (int32_t *)(outVsyncPeriod));
1120}
1121
Roman Stratiienko6b405052022-12-10 19:09:10 +02001122#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001123HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001124 if (IsInHeadlessMode()) {
1125 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1126 return HWC2::Error::None;
1127 }
1128 /* Primary display should be always internal,
1129 * otherwise SF will be unhappy and will crash
1130 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001131 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001132 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001133 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001134 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1135 else
1136 return HWC2::Error::BadConfig;
1137
1138 return HWC2::Error::None;
1139}
1140
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001141HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001142 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001143 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1144 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001145 if (type_ == HWC2::DisplayType::Virtual) {
1146 return HWC2::Error::None;
1147 }
1148
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001149 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1150 return HWC2::Error::BadParameter;
1151 }
1152
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001153 uint32_t current_vsync_period{};
1154 GetDisplayVsyncPeriod(&current_vsync_period);
1155
1156 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1157 return HWC2::Error::SeamlessNotAllowed;
1158 }
1159
1160 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1161 ->desiredTimeNanos -
1162 current_vsync_period;
1163 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1164 if (ret != HWC2::Error::None) {
1165 return ret;
1166 }
1167
1168 outTimeline->refreshRequired = true;
1169 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1170 ->desiredTimeNanos;
1171
1172 last_vsync_ts_ = 0;
1173 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001174 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001175
1176 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001177}
1178
1179HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1180 return HWC2::Error::Unsupported;
1181}
1182
1183HWC2::Error HwcDisplay::GetSupportedContentTypes(
1184 uint32_t *outNumSupportedContentTypes,
1185 const uint32_t *outSupportedContentTypes) {
1186 if (outSupportedContentTypes == nullptr)
1187 *outNumSupportedContentTypes = 0;
1188
1189 return HWC2::Error::None;
1190}
1191
1192HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001193 /* Maps exactly to the content_type DRM connector property:
1194 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001195 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001196 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1197 return HWC2::Error::BadParameter;
1198
1199 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001200
1201 return HWC2::Error::None;
1202}
1203#endif
1204
Roman Stratiienko6b405052022-12-10 19:09:10 +02001205#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001206HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1207 uint32_t *outDataSize,
1208 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001209 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001210 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001211 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001212
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001213 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001214 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001215 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001216 }
1217
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001218 *outPort = handle_; /* TDOD(nobody): What should be here? */
1219
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001220 if (outData) {
1221 *outDataSize = std::min(*outDataSize, blob->length);
1222 memcpy(outData, blob->data, *outDataSize);
1223 } else {
1224 *outDataSize = blob->length;
1225 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001226
1227 return HWC2::Error::None;
1228}
1229
1230HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001231 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001232 if (outNumCapabilities == nullptr) {
1233 return HWC2::Error::BadParameter;
1234 }
1235
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001236 bool skip_ctm = false;
1237
1238 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001239 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001240 skip_ctm = true;
1241
1242 // Skip client CTM if DRM can handle it
1243 if (!skip_ctm && !IsInHeadlessMode() &&
1244 GetPipe().crtc->Get()->GetCtmProperty())
1245 skip_ctm = true;
1246
1247 if (!skip_ctm) {
1248 *outNumCapabilities = 0;
1249 return HWC2::Error::None;
1250 }
1251
1252 *outNumCapabilities = 1;
1253 if (outCapabilities) {
1254 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1255 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001256
1257 return HWC2::Error::None;
1258}
1259
1260HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1261 *supported = false;
1262 return HWC2::Error::None;
1263}
1264
1265HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1266 return HWC2::Error::Unsupported;
1267}
1268
Roman Stratiienko6b405052022-12-10 19:09:10 +02001269#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001270
Roman Stratiienko6b405052022-12-10 19:09:10 +02001271#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001272
1273HWC2::Error HwcDisplay::GetRenderIntents(
1274 int32_t mode, uint32_t *outNumIntents,
1275 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1276 if (mode != HAL_COLOR_MODE_NATIVE) {
1277 return HWC2::Error::BadParameter;
1278 }
1279
1280 if (outIntents == nullptr) {
1281 *outNumIntents = 1;
1282 return HWC2::Error::None;
1283 }
1284 *outNumIntents = 1;
1285 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1286 return HWC2::Error::None;
1287}
1288
1289HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1290 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1291 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1292 return HWC2::Error::BadParameter;
1293
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001294 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1295 return HWC2::Error::Unsupported;
1296
Sasha McIntosh5294f092024-09-18 18:14:54 -04001297 auto err = SetColorMode(mode);
1298 if (err != HWC2::Error::None) return err;
1299
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001300 return HWC2::Error::None;
1301}
1302
Roman Stratiienko6b405052022-12-10 19:09:10 +02001303#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001304
1305const Backend *HwcDisplay::backend() const {
1306 return backend_.get();
1307}
1308
1309void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1310 backend_ = std::move(backend);
1311}
1312
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001313} // namespace android