blob: 2260e368c14e67d3c9ac5ae5522a62405dc6faa2 [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.
268 last_vsync_ts_ = 0;
269 vsync_tracking_en_ = true;
270 vsync_worker_->VSyncControl(true);
271
272 return ConfigError::kNone;
273}
274
Roman Stratiienko63762a92023-09-18 22:33:45 +0300275void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200276 Deinit();
277
Roman Stratiienko63762a92023-09-18 22:33:45 +0300278 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200279
Roman Stratiienko63762a92023-09-18 22:33:45 +0300280 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200281 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000282 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200283 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000284 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200285 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200286}
287
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200288void HwcDisplay::Deinit() {
289 if (pipeline_ != nullptr) {
290 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200291 a_args.composition = std::make_shared<DrmKmsPlan>();
292 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300293 a_args.composition = {};
294 a_args.active = false;
295 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200296
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200297 current_plan_.reset();
298 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200299 if (flatcon_) {
300 flatcon_->StopThread();
301 flatcon_.reset();
302 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200303 }
304
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200305 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600306 // TODO: There should be a mechanism to wait for this worker to complete,
307 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200308 vsync_worker_->StopThread();
309 vsync_worker_ = {};
310 }
311
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200312 SetClientTarget(nullptr, -1, 0, {});
313}
314
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200315HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200316 ChosePreferredConfig();
317
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200318 auto vsw_callbacks = (VSyncWorkerCallbacks){
319 .out_event =
320 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000321 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200322 if (vsync_event_en_) {
323 uint32_t period_ns{};
324 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000325 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200326 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200327 if (vsync_tracking_en_) {
328 last_vsync_ts_ = timestamp;
329 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200330 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200331 vsync_worker_->VSyncControl(false);
332 }
333 },
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200334 };
335
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300336 if (type_ != HWC2::DisplayType::Virtual) {
337 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
338 if (!vsync_worker_) {
339 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
340 return HWC2::Error::BadDisplay;
341 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200342 }
343
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200344 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200345 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200346 if (ret) {
347 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
348 return HWC2::Error::BadDisplay;
349 }
Drew Davenport93443182023-12-14 09:25:45 +0000350 auto flatcbk = (struct FlatConCallbacks){
351 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200352 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200353 }
354
355 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
356
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400357 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200358
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200359 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200360}
361
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600362std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
363 if (IsInHeadlessMode()) {
364 // The pipeline can be nullptr in headless mode, so return the default
365 // "normal" mode.
366 return PanelOrientation::kModePanelOrientationNormal;
367 }
368
369 DrmDisplayPipeline &pipeline = GetPipe();
370 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
371 ALOGW(
372 "No display pipeline present to query the panel orientation property.");
373 return {};
374 }
375
376 return pipeline.connector->Get()->GetPanelOrientation();
377}
378
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200379HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200380 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300381 if (type_ == HWC2::DisplayType::Virtual) {
382 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
383 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200384 err = configs_.Update(*pipeline_->connector->Get());
385 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300386 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200387 }
388 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200389 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200390 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200391
Roman Stratiienko0137f862022-01-04 18:27:40 +0200392 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200393}
394
395HWC2::Error HwcDisplay::AcceptDisplayChanges() {
396 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
397 l.second.AcceptTypeChange();
398 return HWC2::Error::None;
399}
400
401HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200402 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200403 *layer = static_cast<hwc2_layer_t>(layer_idx_);
404 ++layer_idx_;
405 return HWC2::Error::None;
406}
407
408HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200409 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200410 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200411 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200412
413 layers_.erase(layer);
414 return HWC2::Error::None;
415}
416
417HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700418 // If a config has been queued, it is considered the "active" config.
419 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
420 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200421 return HWC2::Error::BadConfig;
422
Drew Davenportfe70c802024-11-07 13:02:21 -0700423 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200424 return HWC2::Error::None;
425}
426
427HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
428 hwc2_layer_t *layers,
429 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200430 if (IsInHeadlessMode()) {
431 *num_elements = 0;
432 return HWC2::Error::None;
433 }
434
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200435 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300436 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 if (l.second.IsTypeChanged()) {
438 if (layers && num_changes < *num_elements)
439 layers[num_changes] = l.first;
440 if (types && num_changes < *num_elements)
441 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
442 ++num_changes;
443 }
444 }
445 if (!layers && !types)
446 *num_elements = num_changes;
447 return HWC2::Error::None;
448}
449
450HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
451 int32_t /*format*/,
452 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200453 if (IsInHeadlessMode()) {
454 return HWC2::Error::None;
455 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200456
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300457 auto min = pipeline_->device->GetMinResolution();
458 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200459
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200460 if (width < min.first || height < min.second)
461 return HWC2::Error::Unsupported;
462
463 if (width > max.first || height > max.second)
464 return HWC2::Error::Unsupported;
465
466 if (dataspace != HAL_DATASPACE_UNKNOWN)
467 return HWC2::Error::Unsupported;
468
469 // TODO(nobody): Validate format can be handled by either GL or planes
470 return HWC2::Error::None;
471}
472
473HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
474 if (!modes)
475 *num_modes = 1;
476
477 if (modes)
478 *modes = HAL_COLOR_MODE_NATIVE;
479
480 return HWC2::Error::None;
481}
482
483HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
484 int32_t attribute_in,
485 int32_t *value) {
486 int conf = static_cast<int>(config);
487
Roman Stratiienko0137f862022-01-04 18:27:40 +0200488 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200489 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490 return HWC2::Error::BadConfig;
491 }
492
Roman Stratiienko0137f862022-01-04 18:27:40 +0200493 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200494
495 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300496 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200497 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
498 switch (attribute) {
499 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200500 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501 break;
502 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200503 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504 break;
505 case HWC2::Attribute::VsyncPeriod:
506 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600507 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200508 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000509 case HWC2::Attribute::DpiY:
510 // ideally this should be vdisplay/mm_heigth, however mm_height
511 // comes from edid parsing and is highly unreliable. Viewing the
512 // rarity of anisotropic displays, falling back to a single value
513 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514 case HWC2::Attribute::DpiX:
515 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200516 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
517 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518 : -1;
519 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200520#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521 case HWC2::Attribute::ConfigGroup:
522 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
523 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200524 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200525 break;
526#endif
527 default:
528 *value = -1;
529 return HWC2::Error::BadConfig;
530 }
531 return HWC2::Error::None;
532}
533
Drew Davenportf7e88332024-09-06 12:54:38 -0600534HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
535 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200536 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200537 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200538 if (hwc_config.second.disabled) {
539 continue;
540 }
541
542 if (configs != nullptr) {
543 if (idx >= *num_configs) {
544 break;
545 }
546 configs[idx] = hwc_config.second.id;
547 }
548
549 idx++;
550 }
551 *num_configs = idx;
552 return HWC2::Error::None;
553}
554
555HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
556 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200557 if (IsInHeadlessMode()) {
558 stream << "null-display";
559 } else {
560 stream << "display-" << GetPipe().connector->Get()->GetId();
561 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300562 auto string = stream.str();
563 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200564 if (!name) {
565 *size = length;
566 return HWC2::Error::None;
567 }
568
569 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
570 strncpy(name, string.c_str(), *size);
571 return HWC2::Error::None;
572}
573
574HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
575 uint32_t *num_elements,
576 hwc2_layer_t * /*layers*/,
577 int32_t * /*layer_requests*/) {
578 // TODO(nobody): I think virtual display should request
579 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
580 *num_elements = 0;
581 return HWC2::Error::None;
582}
583
584HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
585 *type = static_cast<int32_t>(type_);
586 return HWC2::Error::None;
587}
588
589HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
590 *support = 0;
591 return HWC2::Error::None;
592}
593
594HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
595 int32_t * /*types*/,
596 float * /*max_luminance*/,
597 float * /*max_average_luminance*/,
598 float * /*min_luminance*/) {
599 *num_types = 0;
600 return HWC2::Error::None;
601}
602
603/* Find API details at:
604 * 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 +0300605 *
606 * Called after PresentDisplay(), CLIENT is expecting release fence for the
607 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 */
609HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
610 hwc2_layer_t *layers,
611 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200612 if (IsInHeadlessMode()) {
613 *num_elements = 0;
614 return HWC2::Error::None;
615 }
616
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200617 uint32_t num_layers = 0;
618
Roman Stratiienkodd214942022-05-03 18:24:49 +0300619 for (auto &l : layers_) {
620 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
621 continue;
622 }
623
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200624 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300625
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200626 if (layers == nullptr || fences == nullptr)
627 continue;
628
629 if (num_layers > *num_elements) {
630 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
631 return HWC2::Error::None;
632 }
633
634 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200635 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200636 }
637 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300638
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200639 return HWC2::Error::None;
640}
641
Drew Davenport97b5abc2024-11-07 10:43:54 -0700642AtomicCommitArgs HwcDisplay::CreateModesetCommit(
643 const HwcDisplayConfig *config,
644 const std::optional<LayerData> &modeset_layer) {
645 AtomicCommitArgs args{};
646
647 args.color_matrix = color_matrix_;
648 args.content_type = content_type_;
649 args.colorspace = colorspace_;
650
651 std::vector<LayerData> composition_layers;
652 if (modeset_layer) {
653 composition_layers.emplace_back(modeset_layer.value());
654 }
655
656 if (composition_layers.empty()) {
657 ALOGW("Attempting to create a modeset commit without a layer.");
658 }
659
660 args.display_mode = config->mode;
661 args.active = true;
662 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
663 std::move(
664 composition_layers));
665 ALOGW_IF(!args.composition, "No composition for blocking modeset");
666
667 return args;
668}
669
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200670HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200671 if (IsInHeadlessMode()) {
672 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
673 return HWC2::Error::None;
674 }
675
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200676 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400677 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400678 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200679
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200680 uint32_t prev_vperiod_ns = 0;
681 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200682
Drew Davenportd387c842024-12-16 16:57:24 -0700683 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700684 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200685 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700686 const HwcDisplayConfig *staged_config = GetConfig(
687 staged_mode_config_id_.value());
688 if (staged_config == nullptr) {
689 return HWC2::Error::BadConfig;
690 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200691 client_layer_.SetLayerDisplayFrame(
692 (hwc_rect_t){.left = 0,
693 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700694 .right = int(staged_config->mode.GetRawMode().hdisplay),
695 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200696
Drew Davenportfe70c802024-11-07 13:02:21 -0700697 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700698 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200699 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700700 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200701 }
702 }
703
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200704 // order the layers by z-order
705 bool use_client_layer = false;
706 uint32_t client_z_order = UINT32_MAX;
707 std::map<uint32_t, HwcLayer *> z_map;
708 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
709 switch (l.second.GetValidatedType()) {
710 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300711 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200712 break;
713 case HWC2::Composition::Client:
714 // Place it at the z_order of the lowest client layer
715 use_client_layer = true;
716 client_z_order = std::min(client_z_order, l.second.GetZOrder());
717 break;
718 default:
719 continue;
720 }
721 }
722 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300723 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200724
725 if (z_map.empty())
726 return HWC2::Error::BadLayer;
727
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200728 std::vector<LayerData> composition_layers;
729
730 /* Import & populate */
731 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200732 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200733 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200734
735 // now that they're ordered by z, add them to the composition
736 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200737 if (!l.second->IsLayerUsableAsDevice()) {
738 /* This will be normally triggered on validation of the first frame
739 * containing CLIENT layer. At this moment client buffer is not yet
740 * provided by the CLIENT.
741 * This may be triggered once in HwcLayer lifecycle in case FB can't be
742 * imported. For example when non-contiguous buffer is imported into
743 * contiguous-only DRM/KMS driver.
744 */
745 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200747 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748 }
749
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200750 /* Store plan to ensure shared planes won't be stolen by other display
751 * in between of ValidateDisplay() and PresentDisplay() calls
752 */
753 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
754 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300755
756 if (type_ == HWC2::DisplayType::Virtual) {
757 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
758 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
759 .acquire_fence;
760 }
761
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200762 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700763 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200764 return HWC2::Error::BadConfig;
765 }
766
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200767 a_args.composition = current_plan_;
768
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300769 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200770
771 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700772 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200773 return HWC2::Error::BadParameter;
774 }
775
Drew Davenportd387c842024-12-16 16:57:24 -0700776 if (new_vsync_period_ns) {
777 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700778 staged_mode_config_id_.reset();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200779 vsync_tracking_en_ = false;
780 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000781 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
782 last_vsync_ts_ +
783 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200784 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 }
786
787 return HWC2::Error::None;
788}
789
790/* Find API details at:
791 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
792 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300793HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200794 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300795 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200796 return HWC2::Error::None;
797 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200798 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200799
800 ++total_stats_.total_frames_;
801
802 AtomicCommitArgs a_args{};
803 ret = CreateComposition(a_args);
804
805 if (ret != HWC2::Error::None)
806 ++total_stats_.failed_kms_present_;
807
808 if (ret == HWC2::Error::BadLayer) {
809 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300810 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200811 return HWC2::Error::None;
812 }
813 if (ret != HWC2::Error::None)
814 return ret;
815
Roman Stratiienko76892782023-01-16 17:15:53 +0200816 this->present_fence_ = a_args.out_fence;
817 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200818
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200819 // Reset the color matrix so we don't apply it over and over again.
820 color_matrix_ = {};
821
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200822 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700823
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200824 return HWC2::Error::None;
825}
826
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200827HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
828 int64_t change_time) {
829 if (configs_.hwc_configs.count(config) == 0) {
830 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200831 return HWC2::Error::BadConfig;
832 }
833
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200834 staged_mode_change_time_ = change_time;
835 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200836
837 return HWC2::Error::None;
838}
839
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200840HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
841 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
842}
843
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200844/* Find API details at:
845 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
846 */
847HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
848 int32_t acquire_fence,
849 int32_t dataspace,
850 hwc_region_t /*damage*/) {
851 client_layer_.SetLayerBuffer(target, acquire_fence);
852 client_layer_.SetLayerDataspace(dataspace);
853
854 /*
855 * target can be nullptr, this does mean the Composer Service is calling
856 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
857 * 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
858 */
859 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300860 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200861 return HWC2::Error::None;
862 }
863
Roman Stratiienko5070d512022-05-30 13:41:20 +0300864 if (IsInHeadlessMode()) {
865 return HWC2::Error::None;
866 }
867
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200868 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200869 if (!client_layer_.IsLayerUsableAsDevice()) {
870 ALOGE("Client layer must be always usable by DRM/KMS");
871 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200872 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200873
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200874 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300875 if (!bi) {
876 ALOGE("%s: Invalid state", __func__);
877 return HWC2::Error::BadLayer;
878 }
879
880 auto source_crop = (hwc_frect_t){.left = 0.0F,
881 .top = 0.0F,
882 .right = static_cast<float>(bi->width),
883 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200884 client_layer_.SetLayerSourceCrop(source_crop);
885
886 return HWC2::Error::None;
887}
888
889HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400890 /* Maps to the Colorspace DRM connector property:
891 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
892 */
893 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200894 return HWC2::Error::BadParameter;
895
Sasha McIntosh5294f092024-09-18 18:14:54 -0400896 switch (mode) {
897 case HAL_COLOR_MODE_NATIVE:
898 colorspace_ = Colorspace::kDefault;
899 break;
900 case HAL_COLOR_MODE_STANDARD_BT601_625:
901 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
902 case HAL_COLOR_MODE_STANDARD_BT601_525:
903 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
904 // The DP spec does not say whether this is the 525 or the 625 line version.
905 colorspace_ = Colorspace::kBt601Ycc;
906 break;
907 case HAL_COLOR_MODE_STANDARD_BT709:
908 case HAL_COLOR_MODE_SRGB:
909 colorspace_ = Colorspace::kBt709Ycc;
910 break;
911 case HAL_COLOR_MODE_DCI_P3:
912 case HAL_COLOR_MODE_DISPLAY_P3:
913 colorspace_ = Colorspace::kDciP3RgbD65;
914 break;
915 case HAL_COLOR_MODE_ADOBE_RGB:
916 default:
917 return HWC2::Error::Unsupported;
918 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200919
920 color_mode_ = mode;
921 return HWC2::Error::None;
922}
923
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200924#include <xf86drmMode.h>
925
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400926static uint64_t To3132FixPt(float in) {
927 constexpr uint64_t kSignMask = (1ULL << 63);
928 constexpr uint64_t kValueMask = ~(1ULL << 63);
929 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
930 if (in < 0)
931 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
932 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
933}
934
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200935HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
936 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
937 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
938 return HWC2::Error::BadParameter;
939
940 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
941 return HWC2::Error::BadParameter;
942
943 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200944
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200945 if (IsInHeadlessMode())
946 return HWC2::Error::None;
947
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200948 if (!GetPipe().crtc->Get()->GetCtmProperty())
949 return HWC2::Error::None;
950
951 switch (color_transform_hint_) {
952 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400953 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200954 break;
955 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400956 // Without HW support, we cannot correctly process matrices with an offset.
957 for (int i = 12; i < 14; i++) {
958 if (matrix[i] != 0.F)
959 return HWC2::Error::Unsupported;
960 }
961
962 /* HAL provides a 4x4 float type matrix:
963 * | 0 1 2 3|
964 * | 4 5 6 7|
965 * | 8 9 10 11|
966 * |12 13 14 15|
967 *
968 * R_out = R*0 + G*4 + B*8 + 12
969 * G_out = R*1 + G*5 + B*9 + 13
970 * B_out = R*2 + G*6 + B*10 + 14
971 *
972 * DRM expects a 3x3 s31.32 fixed point matrix:
973 * out matrix in
974 * |R| |0 1 2| |R|
975 * |G| = |3 4 5| x |G|
976 * |B| |6 7 8| |B|
977 *
978 * R_out = R*0 + G*1 + B*2
979 * G_out = R*3 + G*4 + B*5
980 * B_out = R*6 + G*7 + B*8
981 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200982 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200983 for (int i = 0; i < kCtmCols; i++) {
984 for (int j = 0; j < kCtmRows; j++) {
985 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400986 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200987 }
988 }
989 break;
990 default:
991 return HWC2::Error::Unsupported;
992 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200993
994 return HWC2::Error::None;
995}
996
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200997bool HwcDisplay::CtmByGpu() {
998 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
999 return false;
1000
1001 if (GetPipe().crtc->Get()->GetCtmProperty())
1002 return false;
1003
Drew Davenport93443182023-12-14 09:25:45 +00001004 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001005 return false;
1006
1007 return true;
1008}
1009
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001010HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1011 int32_t release_fence) {
1012 writeback_layer_->SetLayerBuffer(buffer, release_fence);
1013 writeback_layer_->PopulateLayerData();
1014 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1015 ALOGE("Output layer must be always usable by DRM/KMS");
1016 return HWC2::Error::BadLayer;
1017 }
1018 /* TODO: Check if format is supported by writeback connector */
1019 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001020}
1021
1022HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1023 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001024
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001025 AtomicCommitArgs a_args{};
1026
1027 switch (mode) {
1028 case HWC2::PowerMode::Off:
1029 a_args.active = false;
1030 break;
1031 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001032 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001033 break;
1034 case HWC2::PowerMode::Doze:
1035 case HWC2::PowerMode::DozeSuspend:
1036 return HWC2::Error::Unsupported;
1037 default:
John Stultzffe783c2024-02-14 10:51:27 -08001038 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001039 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001040 }
1041
1042 if (IsInHeadlessMode()) {
1043 return HWC2::Error::None;
1044 }
1045
Jia Ren80566fe2022-11-17 17:26:00 +08001046 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001047 /*
1048 * Setting the display to active before we have a composition
1049 * can break some drivers, so skip setting a_args.active to
1050 * true, as the next composition frame will implicitly activate
1051 * the display
1052 */
1053 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1054 ? HWC2::Error::None
1055 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001056 };
1057
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001058 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001059 if (err) {
1060 ALOGE("Failed to apply the dpms composition err=%d", err);
1061 return HWC2::Error::BadParameter;
1062 }
1063 return HWC2::Error::None;
1064}
1065
1066HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001067 if (type_ == HWC2::DisplayType::Virtual) {
1068 return HWC2::Error::None;
1069 }
1070
Roman Stratiienko099c3112022-01-20 11:50:54 +02001071 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
1072 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001073 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001074 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001075 return HWC2::Error::None;
1076}
1077
1078HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1079 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001080 if (IsInHeadlessMode()) {
1081 *num_types = *num_requests = 0;
1082 return HWC2::Error::None;
1083 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001084
1085 /* In current drm_hwc design in case previous frame layer was not validated as
1086 * a CLIENT, it is used by display controller (Front buffer). We have to store
1087 * this state to provide the CLIENT with the release fences for such buffers.
1088 */
1089 for (auto &l : layers_) {
1090 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1091 HWC2::Composition::Client);
1092 }
1093
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001094 return backend_->ValidateDisplay(this, num_types, num_requests);
1095}
1096
1097std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1098 std::vector<HwcLayer *> ordered_layers;
1099 ordered_layers.reserve(layers_.size());
1100
1101 for (auto &[handle, layer] : layers_) {
1102 ordered_layers.emplace_back(&layer);
1103 }
1104
1105 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1106 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1107 return lhs->GetZOrder() < rhs->GetZOrder();
1108 });
1109
1110 return ordered_layers;
1111}
1112
Roman Stratiienko099c3112022-01-20 11:50:54 +02001113HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1114 uint32_t *outVsyncPeriod /* ns */) {
1115 return GetDisplayAttribute(configs_.active_config_id,
1116 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1117 (int32_t *)(outVsyncPeriod));
1118}
1119
Roman Stratiienko6b405052022-12-10 19:09:10 +02001120#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001121HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001122 if (IsInHeadlessMode()) {
1123 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1124 return HWC2::Error::None;
1125 }
1126 /* Primary display should be always internal,
1127 * otherwise SF will be unhappy and will crash
1128 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001129 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001130 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001131 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001132 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1133 else
1134 return HWC2::Error::BadConfig;
1135
1136 return HWC2::Error::None;
1137}
1138
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001139HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001140 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001141 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1142 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001143 if (type_ == HWC2::DisplayType::Virtual) {
1144 return HWC2::Error::None;
1145 }
1146
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001147 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1148 return HWC2::Error::BadParameter;
1149 }
1150
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001151 uint32_t current_vsync_period{};
1152 GetDisplayVsyncPeriod(&current_vsync_period);
1153
1154 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1155 return HWC2::Error::SeamlessNotAllowed;
1156 }
1157
1158 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1159 ->desiredTimeNanos -
1160 current_vsync_period;
1161 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1162 if (ret != HWC2::Error::None) {
1163 return ret;
1164 }
1165
1166 outTimeline->refreshRequired = true;
1167 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1168 ->desiredTimeNanos;
1169
1170 last_vsync_ts_ = 0;
1171 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001172 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001173
1174 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001175}
1176
1177HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1178 return HWC2::Error::Unsupported;
1179}
1180
1181HWC2::Error HwcDisplay::GetSupportedContentTypes(
1182 uint32_t *outNumSupportedContentTypes,
1183 const uint32_t *outSupportedContentTypes) {
1184 if (outSupportedContentTypes == nullptr)
1185 *outNumSupportedContentTypes = 0;
1186
1187 return HWC2::Error::None;
1188}
1189
1190HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001191 /* Maps exactly to the content_type DRM connector property:
1192 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001193 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001194 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1195 return HWC2::Error::BadParameter;
1196
1197 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001198
1199 return HWC2::Error::None;
1200}
1201#endif
1202
Roman Stratiienko6b405052022-12-10 19:09:10 +02001203#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001204HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1205 uint32_t *outDataSize,
1206 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001207 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001208 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001209 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001210
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001211 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001212 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001213 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001214 }
1215
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001216 *outPort = handle_; /* TDOD(nobody): What should be here? */
1217
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001218 if (outData) {
1219 *outDataSize = std::min(*outDataSize, blob->length);
1220 memcpy(outData, blob->data, *outDataSize);
1221 } else {
1222 *outDataSize = blob->length;
1223 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001224
1225 return HWC2::Error::None;
1226}
1227
1228HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001229 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001230 if (outNumCapabilities == nullptr) {
1231 return HWC2::Error::BadParameter;
1232 }
1233
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001234 bool skip_ctm = false;
1235
1236 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001237 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001238 skip_ctm = true;
1239
1240 // Skip client CTM if DRM can handle it
1241 if (!skip_ctm && !IsInHeadlessMode() &&
1242 GetPipe().crtc->Get()->GetCtmProperty())
1243 skip_ctm = true;
1244
1245 if (!skip_ctm) {
1246 *outNumCapabilities = 0;
1247 return HWC2::Error::None;
1248 }
1249
1250 *outNumCapabilities = 1;
1251 if (outCapabilities) {
1252 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1253 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001254
1255 return HWC2::Error::None;
1256}
1257
1258HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1259 *supported = false;
1260 return HWC2::Error::None;
1261}
1262
1263HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1264 return HWC2::Error::Unsupported;
1265}
1266
Roman Stratiienko6b405052022-12-10 19:09:10 +02001267#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001268
Roman Stratiienko6b405052022-12-10 19:09:10 +02001269#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001270
1271HWC2::Error HwcDisplay::GetRenderIntents(
1272 int32_t mode, uint32_t *outNumIntents,
1273 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1274 if (mode != HAL_COLOR_MODE_NATIVE) {
1275 return HWC2::Error::BadParameter;
1276 }
1277
1278 if (outIntents == nullptr) {
1279 *outNumIntents = 1;
1280 return HWC2::Error::None;
1281 }
1282 *outNumIntents = 1;
1283 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1284 return HWC2::Error::None;
1285}
1286
1287HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1288 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1289 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1290 return HWC2::Error::BadParameter;
1291
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001292 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1293 return HWC2::Error::Unsupported;
1294
Sasha McIntosh5294f092024-09-18 18:14:54 -04001295 auto err = SetColorMode(mode);
1296 if (err != HWC2::Error::None) return err;
1297
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001298 return HWC2::Error::None;
1299}
1300
Roman Stratiienko6b405052022-12-10 19:09:10 +02001301#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001302
1303const Backend *HwcDisplay::backend() const {
1304 return backend_.get();
1305}
1306
1307void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1308 backend_ = std::move(backend);
1309}
1310
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001311} // namespace android