blob: 8d6b0a3a6cd75adc7f7796cb1c1110111ebc2945 [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
Manasi Navare3f0c01a2024-10-04 18:01:55 +000022#include <cinttypes>
23
Drew Davenport97b5abc2024-11-07 10:43:54 -070024#include <hardware/gralloc.h>
25#include <ui/GraphicBufferAllocator.h>
26#include <ui/GraphicBufferMapper.h>
27#include <ui/PixelFormat.h>
28
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020029#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020030#include "backend/BackendManager.h"
31#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060032#include "compositor/DisplayInfo.h"
33#include "drm/DrmConnector.h"
34#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000035#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020036#include "utils/log.h"
37#include "utils/properties.h"
38
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060039using ::android::DrmDisplayPipeline;
40
Roman Stratiienko3627beb2022-01-04 16:02:55 +020041namespace android {
42
Drew Davenport97b5abc2024-11-07 10:43:54 -070043namespace {
44// Allocate a black buffer that can be used for an initial modeset when there.
45// is no appropriate client buffer available to be used.
46// Caller must free the returned buffer with GraphicBufferAllocator::free.
47auto GetModesetBuffer(uint32_t width, uint32_t height) -> buffer_handle_t {
48 constexpr PixelFormat format = PIXEL_FORMAT_RGBA_8888;
49 constexpr uint64_t usage = GRALLOC_USAGE_SW_READ_OFTEN |
50 GRALLOC_USAGE_SW_WRITE_OFTEN |
51 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
52
53 constexpr uint32_t layer_count = 1;
54 const std::string name = "drm-hwcomposer";
55
56 buffer_handle_t handle = nullptr;
57 uint32_t stride = 0;
58 status_t status = GraphicBufferAllocator::get().allocate(width, height,
59 format, layer_count,
60 usage, &handle,
61 &stride, name);
62 if (status != OK) {
63 ALOGE("Failed to allocate modeset buffer.");
64 return nullptr;
65 }
66
67 void *data = nullptr;
68 Rect bounds = {0, 0, static_cast<int32_t>(width),
69 static_cast<int32_t>(height)};
70 status = GraphicBufferMapper::get().lock(handle, usage, bounds, &data);
71 if (status != OK) {
72 ALOGE("Failed to map modeset buffer.");
73 GraphicBufferAllocator::get().free(handle);
74 return nullptr;
75 }
76
77 // Cast one of the multiplicands to ensure that the multiplication happens
78 // in a wider type (size_t).
79 const size_t buffer_size = static_cast<size_t>(height) * stride *
80 bytesPerPixel(format);
81 memset(data, 0, buffer_size);
82 status = GraphicBufferMapper::get().unlock(handle);
83 ALOGW_IF(status != OK, "Failed to unmap buffer.");
84 return handle;
85}
86
87auto GetModesetLayerProperties(buffer_handle_t buffer, uint32_t width,
88 uint32_t height) -> HwcLayer::LayerProperties {
89 HwcLayer::LayerProperties properties;
90 properties.buffer = {.buffer_handle = buffer, .acquire_fence = {}};
91 properties.display_frame = {
92 .left = 0,
93 .top = 0,
94 .right = int(width),
95 .bottom = int(height),
96 };
97 properties.source_crop = (hwc_frect_t){
98 .left = 0.0F,
99 .top = 0.0F,
100 .right = static_cast<float>(width),
101 .bottom = static_cast<float>(height),
102 };
103 properties.blend_mode = BufferBlendMode::kNone;
104 return properties;
105}
106} // namespace
107
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200108std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
109 if (delta.total_pixops_ == 0)
110 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300111 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200112
113 std::stringstream ss;
114 ss << " Total frames count: " << delta.total_frames_ << "\n"
115 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
116 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
117 << ((delta.failed_kms_present_ > 0)
118 ? " !!! Internal failure, FIX it please\n"
119 : "")
120 << " Flattened frames: " << delta.frames_flattened_ << "\n"
121 << " Pixel operations (free units)"
122 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
123 << "]\n"
124 << " Composition efficiency: " << ratio;
125
126 return ss.str();
127}
128
129std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300130 auto connector_name = IsInHeadlessMode()
131 ? std::string("NULL-DISPLAY")
132 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200133
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200134 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200136 << "Statistics since system boot:\n"
137 << DumpDelta(total_stats_) << "\n\n"
138 << "Statistics since last dumpsys request:\n"
139 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
140
141 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
142 return ss.str();
143}
144
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200145HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000146 DrmHwc *hwc)
147 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300148 if (type_ == HWC2::DisplayType::Virtual) {
149 writeback_layer_ = std::make_unique<HwcLayer>(this);
150 }
151}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200152
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400153void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200154 color_matrix_ = std::make_shared<drm_color_ctm>();
155 for (int i = 0; i < kCtmCols; i++) {
156 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800157 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200158 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
159 }
160 }
161
162 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200163}
164
Normunds Rieksts545096d2024-03-11 16:37:45 +0000165HwcDisplay::~HwcDisplay() {
166 Deinit();
167};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200168
Drew Davenportfe70c802024-11-07 13:02:21 -0700169auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
170 -> const HwcDisplayConfig * {
171 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600172 if (config_iter == configs_.hwc_configs.end()) {
173 return nullptr;
174 }
175 return &config_iter->second;
176}
177
Drew Davenportfe70c802024-11-07 13:02:21 -0700178auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
179 return GetConfig(configs_.active_config_id);
180}
181
Drew Davenport8998f8b2024-10-24 10:15:12 -0600182auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700183 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600184}
185
Drew Davenport97b5abc2024-11-07 10:43:54 -0700186HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
187 const HwcDisplayConfig *new_config = GetConfig(config);
188 if (new_config == nullptr) {
189 ALOGE("Could not find active mode for %u", config);
190 return ConfigError::kBadConfig;
191 }
192
193 const HwcDisplayConfig *current_config = GetCurrentConfig();
194
195 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700196 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700197
198 std::optional<LayerData> modeset_layer_data;
199 // If a client layer has already been provided, and its size matches the
200 // new config, use it for the modeset.
201 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
202 current_config->mode.GetRawMode().hdisplay == width &&
203 current_config->mode.GetRawMode().vdisplay == height) {
204 ALOGV("Use existing client_layer for blocking config.");
205 modeset_layer_data = client_layer_.GetLayerData();
206 } else {
207 ALOGV("Allocate modeset buffer.");
208 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
209 if (modeset_buffer != nullptr) {
210 auto modeset_layer = std::make_unique<HwcLayer>(this);
211 modeset_layer->SetLayerProperties(
212 GetModesetLayerProperties(modeset_buffer, width, height));
213 modeset_layer->PopulateLayerData();
214 modeset_layer_data = modeset_layer->GetLayerData();
215 GraphicBufferAllocator::get().free(modeset_buffer);
216 }
217 }
218
219 ALOGV("Create modeset commit.");
220 // Create atomic commit args for a blocking modeset. There's no need to do a
221 // separate test commit, since the commit does a test anyways.
222 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
223 modeset_layer_data);
224 commit_args.blocking = true;
225 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
226
227 if (ret) {
228 ALOGE("Blocking config failed: %d", ret);
229 return HwcDisplay::ConfigError::kBadConfig;
230 }
231
232 ALOGV("Blocking config succeeded.");
233 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700234 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700235 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
236 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700237 return ConfigError::kNone;
238}
239
Drew Davenport8998f8b2024-10-24 10:15:12 -0600240auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
241 bool seamless, QueuedConfigTiming *out_timing)
242 -> ConfigError {
243 if (configs_.hwc_configs.count(config) == 0) {
244 ALOGE("Could not find active mode for %u", config);
245 return ConfigError::kBadConfig;
246 }
247
248 // TODO: Add support for seamless configuration changes.
249 if (seamless) {
250 return ConfigError::kSeamlessNotAllowed;
251 }
252
253 // Request a refresh from the client one vsync period before the desired
254 // time, or simply at the desired time if there is no active configuration.
255 const HwcDisplayConfig *current_config = GetCurrentConfig();
256 out_timing->refresh_time_ns = desired_time -
257 (current_config
258 ? current_config->mode.GetVSyncPeriodNs()
259 : 0);
260 out_timing->new_vsync_time_ns = desired_time;
261
262 // Queue the config change timing to be consistent with the requested
263 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600264 staged_mode_change_time_ = out_timing->refresh_time_ns;
265 staged_mode_config_id_ = config;
266
267 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700268 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600269 vsync_worker_->VSyncControl(true);
270
271 return ConfigError::kNone;
272}
273
Roman Stratiienko63762a92023-09-18 22:33:45 +0300274void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200275 Deinit();
276
Roman Stratiienko63762a92023-09-18 22:33:45 +0300277 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200278
Roman Stratiienko63762a92023-09-18 22:33:45 +0300279 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200280 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000281 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200282 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000283 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200284 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285}
286
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200287void HwcDisplay::Deinit() {
288 if (pipeline_ != nullptr) {
289 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200290 a_args.composition = std::make_shared<DrmKmsPlan>();
291 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300292 a_args.composition = {};
293 a_args.active = false;
294 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200295
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200296 current_plan_.reset();
297 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200298 if (flatcon_) {
299 flatcon_->StopThread();
300 flatcon_.reset();
301 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200302 }
303
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200304 if (vsync_worker_) {
Drew Davenport1ac3b622024-09-05 10:59:16 -0600305 // TODO: There should be a mechanism to wait for this worker to complete,
306 // otherwise there is a race condition while destructing the HwcDisplay.
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200307 vsync_worker_->StopThread();
308 vsync_worker_ = {};
309 }
310
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200311 SetClientTarget(nullptr, -1, 0, {});
312}
313
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200314HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200315 ChosePreferredConfig();
316
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200317 auto vsw_callbacks = (VSyncWorkerCallbacks){
318 .out_event =
319 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000320 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200321 if (vsync_event_en_) {
322 uint32_t period_ns{};
323 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000324 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200325 }
Drew Davenport33121b72024-12-13 14:59:35 -0700326 if (!vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200327 vsync_worker_->VSyncControl(false);
328 }
329 },
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200330 };
331
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300332 if (type_ != HWC2::DisplayType::Virtual) {
333 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
334 if (!vsync_worker_) {
335 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
336 return HWC2::Error::BadDisplay;
337 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200338 }
339
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200340 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200341 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200342 if (ret) {
343 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
344 return HWC2::Error::BadDisplay;
345 }
Drew Davenport93443182023-12-14 09:25:45 +0000346 auto flatcbk = (struct FlatConCallbacks){
347 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200348 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200349 }
350
351 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
352
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400353 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200354
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200355 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200356}
357
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600358std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
359 if (IsInHeadlessMode()) {
360 // The pipeline can be nullptr in headless mode, so return the default
361 // "normal" mode.
362 return PanelOrientation::kModePanelOrientationNormal;
363 }
364
365 DrmDisplayPipeline &pipeline = GetPipe();
366 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
367 ALOGW(
368 "No display pipeline present to query the panel orientation property.");
369 return {};
370 }
371
372 return pipeline.connector->Get()->GetPanelOrientation();
373}
374
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200375HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200376 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300377 if (type_ == HWC2::DisplayType::Virtual) {
378 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
379 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200380 err = configs_.Update(*pipeline_->connector->Get());
381 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300382 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200383 }
384 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200385 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200386 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200387
Roman Stratiienko0137f862022-01-04 18:27:40 +0200388 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200389}
390
391HWC2::Error HwcDisplay::AcceptDisplayChanges() {
392 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
393 l.second.AcceptTypeChange();
394 return HWC2::Error::None;
395}
396
397HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200398 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200399 *layer = static_cast<hwc2_layer_t>(layer_idx_);
400 ++layer_idx_;
401 return HWC2::Error::None;
402}
403
404HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200405 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200406 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200407 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200408
409 layers_.erase(layer);
410 return HWC2::Error::None;
411}
412
413HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700414 // If a config has been queued, it is considered the "active" config.
415 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
416 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200417 return HWC2::Error::BadConfig;
418
Drew Davenportfe70c802024-11-07 13:02:21 -0700419 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200420 return HWC2::Error::None;
421}
422
423HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
424 hwc2_layer_t *layers,
425 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200426 if (IsInHeadlessMode()) {
427 *num_elements = 0;
428 return HWC2::Error::None;
429 }
430
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200431 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300432 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200433 if (l.second.IsTypeChanged()) {
434 if (layers && num_changes < *num_elements)
435 layers[num_changes] = l.first;
436 if (types && num_changes < *num_elements)
437 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
438 ++num_changes;
439 }
440 }
441 if (!layers && !types)
442 *num_elements = num_changes;
443 return HWC2::Error::None;
444}
445
446HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
447 int32_t /*format*/,
448 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200449 if (IsInHeadlessMode()) {
450 return HWC2::Error::None;
451 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200452
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300453 auto min = pipeline_->device->GetMinResolution();
454 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200455
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200456 if (width < min.first || height < min.second)
457 return HWC2::Error::Unsupported;
458
459 if (width > max.first || height > max.second)
460 return HWC2::Error::Unsupported;
461
462 if (dataspace != HAL_DATASPACE_UNKNOWN)
463 return HWC2::Error::Unsupported;
464
465 // TODO(nobody): Validate format can be handled by either GL or planes
466 return HWC2::Error::None;
467}
468
469HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
470 if (!modes)
471 *num_modes = 1;
472
473 if (modes)
474 *modes = HAL_COLOR_MODE_NATIVE;
475
476 return HWC2::Error::None;
477}
478
479HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
480 int32_t attribute_in,
481 int32_t *value) {
482 int conf = static_cast<int>(config);
483
Roman Stratiienko0137f862022-01-04 18:27:40 +0200484 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200485 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200486 return HWC2::Error::BadConfig;
487 }
488
Roman Stratiienko0137f862022-01-04 18:27:40 +0200489 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490
491 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300492 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200493 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
494 switch (attribute) {
495 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200496 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200497 break;
498 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200499 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 break;
501 case HWC2::Attribute::VsyncPeriod:
502 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600503 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000505 case HWC2::Attribute::DpiY:
506 // ideally this should be vdisplay/mm_heigth, however mm_height
507 // comes from edid parsing and is highly unreliable. Viewing the
508 // rarity of anisotropic displays, falling back to a single value
509 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510 case HWC2::Attribute::DpiX:
511 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200512 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
513 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514 : -1;
515 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200516#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200517 case HWC2::Attribute::ConfigGroup:
518 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
519 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200520 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521 break;
522#endif
523 default:
524 *value = -1;
525 return HWC2::Error::BadConfig;
526 }
527 return HWC2::Error::None;
528}
529
Drew Davenportf7e88332024-09-06 12:54:38 -0600530HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
531 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200533 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200534 if (hwc_config.second.disabled) {
535 continue;
536 }
537
538 if (configs != nullptr) {
539 if (idx >= *num_configs) {
540 break;
541 }
542 configs[idx] = hwc_config.second.id;
543 }
544
545 idx++;
546 }
547 *num_configs = idx;
548 return HWC2::Error::None;
549}
550
551HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
552 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200553 if (IsInHeadlessMode()) {
554 stream << "null-display";
555 } else {
556 stream << "display-" << GetPipe().connector->Get()->GetId();
557 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300558 auto string = stream.str();
559 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200560 if (!name) {
561 *size = length;
562 return HWC2::Error::None;
563 }
564
565 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
566 strncpy(name, string.c_str(), *size);
567 return HWC2::Error::None;
568}
569
570HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
571 uint32_t *num_elements,
572 hwc2_layer_t * /*layers*/,
573 int32_t * /*layer_requests*/) {
574 // TODO(nobody): I think virtual display should request
575 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
576 *num_elements = 0;
577 return HWC2::Error::None;
578}
579
580HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
581 *type = static_cast<int32_t>(type_);
582 return HWC2::Error::None;
583}
584
585HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
586 *support = 0;
587 return HWC2::Error::None;
588}
589
590HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
591 int32_t * /*types*/,
592 float * /*max_luminance*/,
593 float * /*max_average_luminance*/,
594 float * /*min_luminance*/) {
595 *num_types = 0;
596 return HWC2::Error::None;
597}
598
599/* Find API details at:
600 * 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 +0300601 *
602 * Called after PresentDisplay(), CLIENT is expecting release fence for the
603 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604 */
605HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
606 hwc2_layer_t *layers,
607 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200608 if (IsInHeadlessMode()) {
609 *num_elements = 0;
610 return HWC2::Error::None;
611 }
612
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200613 uint32_t num_layers = 0;
614
Roman Stratiienkodd214942022-05-03 18:24:49 +0300615 for (auto &l : layers_) {
616 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
617 continue;
618 }
619
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300621
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622 if (layers == nullptr || fences == nullptr)
623 continue;
624
625 if (num_layers > *num_elements) {
626 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
627 return HWC2::Error::None;
628 }
629
630 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200631 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632 }
633 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300634
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 return HWC2::Error::None;
636}
637
Drew Davenport97b5abc2024-11-07 10:43:54 -0700638AtomicCommitArgs HwcDisplay::CreateModesetCommit(
639 const HwcDisplayConfig *config,
640 const std::optional<LayerData> &modeset_layer) {
641 AtomicCommitArgs args{};
642
643 args.color_matrix = color_matrix_;
644 args.content_type = content_type_;
645 args.colorspace = colorspace_;
646
647 std::vector<LayerData> composition_layers;
648 if (modeset_layer) {
649 composition_layers.emplace_back(modeset_layer.value());
650 }
651
652 if (composition_layers.empty()) {
653 ALOGW("Attempting to create a modeset commit without a layer.");
654 }
655
656 args.display_mode = config->mode;
657 args.active = true;
658 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
659 std::move(
660 composition_layers));
661 ALOGW_IF(!args.composition, "No composition for blocking modeset");
662
663 return args;
664}
665
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200666HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200667 if (IsInHeadlessMode()) {
668 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
669 return HWC2::Error::None;
670 }
671
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200672 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400673 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400674 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200675
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200676 uint32_t prev_vperiod_ns = 0;
677 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200678
Drew Davenportd387c842024-12-16 16:57:24 -0700679 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700680 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200681 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700682 const HwcDisplayConfig *staged_config = GetConfig(
683 staged_mode_config_id_.value());
684 if (staged_config == nullptr) {
685 return HWC2::Error::BadConfig;
686 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200687 client_layer_.SetLayerDisplayFrame(
688 (hwc_rect_t){.left = 0,
689 .top = 0,
Drew Davenportfe70c802024-11-07 13:02:21 -0700690 .right = int(staged_config->mode.GetRawMode().hdisplay),
691 .bottom = int(staged_config->mode.GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200692
Drew Davenportfe70c802024-11-07 13:02:21 -0700693 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700694 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200695 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700696 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200697 }
698 }
699
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200700 // order the layers by z-order
701 bool use_client_layer = false;
702 uint32_t client_z_order = UINT32_MAX;
703 std::map<uint32_t, HwcLayer *> z_map;
704 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
705 switch (l.second.GetValidatedType()) {
706 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300707 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200708 break;
709 case HWC2::Composition::Client:
710 // Place it at the z_order of the lowest client layer
711 use_client_layer = true;
712 client_z_order = std::min(client_z_order, l.second.GetZOrder());
713 break;
714 default:
715 continue;
716 }
717 }
718 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300719 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200720
721 if (z_map.empty())
722 return HWC2::Error::BadLayer;
723
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200724 std::vector<LayerData> composition_layers;
725
726 /* Import & populate */
727 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200728 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200729 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200730
731 // now that they're ordered by z, add them to the composition
732 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200733 if (!l.second->IsLayerUsableAsDevice()) {
734 /* This will be normally triggered on validation of the first frame
735 * containing CLIENT layer. At this moment client buffer is not yet
736 * provided by the CLIENT.
737 * This may be triggered once in HwcLayer lifecycle in case FB can't be
738 * imported. For example when non-contiguous buffer is imported into
739 * contiguous-only DRM/KMS driver.
740 */
741 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200742 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200743 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200744 }
745
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200746 /* Store plan to ensure shared planes won't be stolen by other display
747 * in between of ValidateDisplay() and PresentDisplay() calls
748 */
749 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
750 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300751
752 if (type_ == HWC2::DisplayType::Virtual) {
753 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
754 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
755 .acquire_fence;
756 }
757
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200758 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700759 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200760 return HWC2::Error::BadConfig;
761 }
762
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200763 a_args.composition = current_plan_;
764
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300765 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200766
767 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700768 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200769 return HWC2::Error::BadParameter;
770 }
771
Drew Davenportd387c842024-12-16 16:57:24 -0700772 if (new_vsync_period_ns) {
773 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700774 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700775
776 vsync_worker_->SetVsyncTimestampTracking(false);
777 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
778 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000779 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700780 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000781 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200782 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200783 }
784
785 return HWC2::Error::None;
786}
787
788/* Find API details at:
789 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
790 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300791HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200792 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300793 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200794 return HWC2::Error::None;
795 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200796 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200797
798 ++total_stats_.total_frames_;
799
800 AtomicCommitArgs a_args{};
801 ret = CreateComposition(a_args);
802
803 if (ret != HWC2::Error::None)
804 ++total_stats_.failed_kms_present_;
805
806 if (ret == HWC2::Error::BadLayer) {
807 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300808 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200809 return HWC2::Error::None;
810 }
811 if (ret != HWC2::Error::None)
812 return ret;
813
Roman Stratiienko76892782023-01-16 17:15:53 +0200814 this->present_fence_ = a_args.out_fence;
815 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200816
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200817 // Reset the color matrix so we don't apply it over and over again.
818 color_matrix_ = {};
819
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200820 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700821
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200822 return HWC2::Error::None;
823}
824
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200825HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
826 int64_t change_time) {
827 if (configs_.hwc_configs.count(config) == 0) {
828 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200829 return HWC2::Error::BadConfig;
830 }
831
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200832 staged_mode_change_time_ = change_time;
833 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200834
835 return HWC2::Error::None;
836}
837
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200838HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
839 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
840}
841
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200842/* Find API details at:
843 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
844 */
845HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
846 int32_t acquire_fence,
847 int32_t dataspace,
848 hwc_region_t /*damage*/) {
849 client_layer_.SetLayerBuffer(target, acquire_fence);
850 client_layer_.SetLayerDataspace(dataspace);
851
852 /*
853 * target can be nullptr, this does mean the Composer Service is calling
854 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
855 * 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
856 */
857 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300858 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200859 return HWC2::Error::None;
860 }
861
Roman Stratiienko5070d512022-05-30 13:41:20 +0300862 if (IsInHeadlessMode()) {
863 return HWC2::Error::None;
864 }
865
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200866 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200867 if (!client_layer_.IsLayerUsableAsDevice()) {
868 ALOGE("Client layer must be always usable by DRM/KMS");
869 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200870 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200871
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200872 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300873 if (!bi) {
874 ALOGE("%s: Invalid state", __func__);
875 return HWC2::Error::BadLayer;
876 }
877
878 auto source_crop = (hwc_frect_t){.left = 0.0F,
879 .top = 0.0F,
880 .right = static_cast<float>(bi->width),
881 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882 client_layer_.SetLayerSourceCrop(source_crop);
883
884 return HWC2::Error::None;
885}
886
887HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400888 /* Maps to the Colorspace DRM connector property:
889 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
890 */
891 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200892 return HWC2::Error::BadParameter;
893
Sasha McIntosh5294f092024-09-18 18:14:54 -0400894 switch (mode) {
895 case HAL_COLOR_MODE_NATIVE:
896 colorspace_ = Colorspace::kDefault;
897 break;
898 case HAL_COLOR_MODE_STANDARD_BT601_625:
899 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
900 case HAL_COLOR_MODE_STANDARD_BT601_525:
901 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
902 // The DP spec does not say whether this is the 525 or the 625 line version.
903 colorspace_ = Colorspace::kBt601Ycc;
904 break;
905 case HAL_COLOR_MODE_STANDARD_BT709:
906 case HAL_COLOR_MODE_SRGB:
907 colorspace_ = Colorspace::kBt709Ycc;
908 break;
909 case HAL_COLOR_MODE_DCI_P3:
910 case HAL_COLOR_MODE_DISPLAY_P3:
911 colorspace_ = Colorspace::kDciP3RgbD65;
912 break;
913 case HAL_COLOR_MODE_ADOBE_RGB:
914 default:
915 return HWC2::Error::Unsupported;
916 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200917
918 color_mode_ = mode;
919 return HWC2::Error::None;
920}
921
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200922#include <xf86drmMode.h>
923
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400924static uint64_t To3132FixPt(float in) {
925 constexpr uint64_t kSignMask = (1ULL << 63);
926 constexpr uint64_t kValueMask = ~(1ULL << 63);
927 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
928 if (in < 0)
929 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
930 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
931}
932
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200933HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
934 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
935 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
936 return HWC2::Error::BadParameter;
937
938 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
939 return HWC2::Error::BadParameter;
940
941 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200942
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200943 if (IsInHeadlessMode())
944 return HWC2::Error::None;
945
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200946 if (!GetPipe().crtc->Get()->GetCtmProperty())
947 return HWC2::Error::None;
948
949 switch (color_transform_hint_) {
950 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400951 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200952 break;
953 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400954 // Without HW support, we cannot correctly process matrices with an offset.
955 for (int i = 12; i < 14; i++) {
956 if (matrix[i] != 0.F)
957 return HWC2::Error::Unsupported;
958 }
959
960 /* HAL provides a 4x4 float type matrix:
961 * | 0 1 2 3|
962 * | 4 5 6 7|
963 * | 8 9 10 11|
964 * |12 13 14 15|
965 *
966 * R_out = R*0 + G*4 + B*8 + 12
967 * G_out = R*1 + G*5 + B*9 + 13
968 * B_out = R*2 + G*6 + B*10 + 14
969 *
970 * DRM expects a 3x3 s31.32 fixed point matrix:
971 * out matrix in
972 * |R| |0 1 2| |R|
973 * |G| = |3 4 5| x |G|
974 * |B| |6 7 8| |B|
975 *
976 * R_out = R*0 + G*1 + B*2
977 * G_out = R*3 + G*4 + B*5
978 * B_out = R*6 + G*7 + B*8
979 */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200980 color_matrix_ = std::make_shared<drm_color_ctm>();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200981 for (int i = 0; i < kCtmCols; i++) {
982 for (int j = 0; j < kCtmRows; j++) {
983 constexpr int kInCtmRows = 4;
Sasha McIntosh921c1cd2024-10-09 19:50:52 -0400984 color_matrix_->matrix[i * kCtmRows + j] = To3132FixPt(matrix[j * kInCtmRows + i]);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200985 }
986 }
987 break;
988 default:
989 return HWC2::Error::Unsupported;
990 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200991
992 return HWC2::Error::None;
993}
994
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200995bool HwcDisplay::CtmByGpu() {
996 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
997 return false;
998
999 if (GetPipe().crtc->Get()->GetCtmProperty())
1000 return false;
1001
Drew Davenport93443182023-12-14 09:25:45 +00001002 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001003 return false;
1004
1005 return true;
1006}
1007
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001008HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1009 int32_t release_fence) {
1010 writeback_layer_->SetLayerBuffer(buffer, release_fence);
1011 writeback_layer_->PopulateLayerData();
1012 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1013 ALOGE("Output layer must be always usable by DRM/KMS");
1014 return HWC2::Error::BadLayer;
1015 }
1016 /* TODO: Check if format is supported by writeback connector */
1017 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001018}
1019
1020HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1021 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001022
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001023 AtomicCommitArgs a_args{};
1024
1025 switch (mode) {
1026 case HWC2::PowerMode::Off:
1027 a_args.active = false;
1028 break;
1029 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001030 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001031 break;
1032 case HWC2::PowerMode::Doze:
1033 case HWC2::PowerMode::DozeSuspend:
1034 return HWC2::Error::Unsupported;
1035 default:
John Stultzffe783c2024-02-14 10:51:27 -08001036 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001037 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001038 }
1039
1040 if (IsInHeadlessMode()) {
1041 return HWC2::Error::None;
1042 }
1043
Jia Ren80566fe2022-11-17 17:26:00 +08001044 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001045 /*
1046 * Setting the display to active before we have a composition
1047 * can break some drivers, so skip setting a_args.active to
1048 * true, as the next composition frame will implicitly activate
1049 * the display
1050 */
1051 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1052 ? HWC2::Error::None
1053 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001054 };
1055
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001056 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001057 if (err) {
1058 ALOGE("Failed to apply the dpms composition err=%d", err);
1059 return HWC2::Error::BadParameter;
1060 }
1061 return HWC2::Error::None;
1062}
1063
1064HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001065 if (type_ == HWC2::DisplayType::Virtual) {
1066 return HWC2::Error::None;
1067 }
1068
Roman Stratiienko099c3112022-01-20 11:50:54 +02001069 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
1070 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001071 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001072 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001073 return HWC2::Error::None;
1074}
1075
1076HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1077 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001078 if (IsInHeadlessMode()) {
1079 *num_types = *num_requests = 0;
1080 return HWC2::Error::None;
1081 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001082
1083 /* In current drm_hwc design in case previous frame layer was not validated as
1084 * a CLIENT, it is used by display controller (Front buffer). We have to store
1085 * this state to provide the CLIENT with the release fences for such buffers.
1086 */
1087 for (auto &l : layers_) {
1088 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1089 HWC2::Composition::Client);
1090 }
1091
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001092 return backend_->ValidateDisplay(this, num_types, num_requests);
1093}
1094
1095std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1096 std::vector<HwcLayer *> ordered_layers;
1097 ordered_layers.reserve(layers_.size());
1098
1099 for (auto &[handle, layer] : layers_) {
1100 ordered_layers.emplace_back(&layer);
1101 }
1102
1103 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1104 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1105 return lhs->GetZOrder() < rhs->GetZOrder();
1106 });
1107
1108 return ordered_layers;
1109}
1110
Roman Stratiienko099c3112022-01-20 11:50:54 +02001111HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1112 uint32_t *outVsyncPeriod /* ns */) {
1113 return GetDisplayAttribute(configs_.active_config_id,
1114 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1115 (int32_t *)(outVsyncPeriod));
1116}
1117
Roman Stratiienko6b405052022-12-10 19:09:10 +02001118#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001119HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001120 if (IsInHeadlessMode()) {
1121 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1122 return HWC2::Error::None;
1123 }
1124 /* Primary display should be always internal,
1125 * otherwise SF will be unhappy and will crash
1126 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001127 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001128 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001129 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001130 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1131 else
1132 return HWC2::Error::BadConfig;
1133
1134 return HWC2::Error::None;
1135}
1136
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001137HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001138 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001139 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1140 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001141 if (type_ == HWC2::DisplayType::Virtual) {
1142 return HWC2::Error::None;
1143 }
1144
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001145 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1146 return HWC2::Error::BadParameter;
1147 }
1148
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001149 uint32_t current_vsync_period{};
1150 GetDisplayVsyncPeriod(&current_vsync_period);
1151
1152 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1153 return HWC2::Error::SeamlessNotAllowed;
1154 }
1155
1156 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1157 ->desiredTimeNanos -
1158 current_vsync_period;
1159 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1160 if (ret != HWC2::Error::None) {
1161 return ret;
1162 }
1163
1164 outTimeline->refreshRequired = true;
1165 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1166 ->desiredTimeNanos;
1167
Drew Davenport33121b72024-12-13 14:59:35 -07001168 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001169 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001170
1171 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001172}
1173
1174HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1175 return HWC2::Error::Unsupported;
1176}
1177
1178HWC2::Error HwcDisplay::GetSupportedContentTypes(
1179 uint32_t *outNumSupportedContentTypes,
1180 const uint32_t *outSupportedContentTypes) {
1181 if (outSupportedContentTypes == nullptr)
1182 *outNumSupportedContentTypes = 0;
1183
1184 return HWC2::Error::None;
1185}
1186
1187HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001188 /* Maps exactly to the content_type DRM connector property:
1189 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001190 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001191 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1192 return HWC2::Error::BadParameter;
1193
1194 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001195
1196 return HWC2::Error::None;
1197}
1198#endif
1199
Roman Stratiienko6b405052022-12-10 19:09:10 +02001200#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001201HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1202 uint32_t *outDataSize,
1203 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001204 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001205 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001206 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001207
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001208 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001209 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001210 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001211 }
1212
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001213 *outPort = handle_; /* TDOD(nobody): What should be here? */
1214
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001215 if (outData) {
1216 *outDataSize = std::min(*outDataSize, blob->length);
1217 memcpy(outData, blob->data, *outDataSize);
1218 } else {
1219 *outDataSize = blob->length;
1220 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001221
1222 return HWC2::Error::None;
1223}
1224
1225HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001226 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001227 if (outNumCapabilities == nullptr) {
1228 return HWC2::Error::BadParameter;
1229 }
1230
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001231 bool skip_ctm = false;
1232
1233 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001234 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001235 skip_ctm = true;
1236
1237 // Skip client CTM if DRM can handle it
1238 if (!skip_ctm && !IsInHeadlessMode() &&
1239 GetPipe().crtc->Get()->GetCtmProperty())
1240 skip_ctm = true;
1241
1242 if (!skip_ctm) {
1243 *outNumCapabilities = 0;
1244 return HWC2::Error::None;
1245 }
1246
1247 *outNumCapabilities = 1;
1248 if (outCapabilities) {
1249 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1250 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001251
1252 return HWC2::Error::None;
1253}
1254
1255HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1256 *supported = false;
1257 return HWC2::Error::None;
1258}
1259
1260HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1261 return HWC2::Error::Unsupported;
1262}
1263
Roman Stratiienko6b405052022-12-10 19:09:10 +02001264#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001265
Roman Stratiienko6b405052022-12-10 19:09:10 +02001266#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001267
1268HWC2::Error HwcDisplay::GetRenderIntents(
1269 int32_t mode, uint32_t *outNumIntents,
1270 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1271 if (mode != HAL_COLOR_MODE_NATIVE) {
1272 return HWC2::Error::BadParameter;
1273 }
1274
1275 if (outIntents == nullptr) {
1276 *outNumIntents = 1;
1277 return HWC2::Error::None;
1278 }
1279 *outNumIntents = 1;
1280 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1281 return HWC2::Error::None;
1282}
1283
1284HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1285 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1286 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1287 return HWC2::Error::BadParameter;
1288
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001289 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1290 return HWC2::Error::Unsupported;
1291
Sasha McIntosh5294f092024-09-18 18:14:54 -04001292 auto err = SetColorMode(mode);
1293 if (err != HWC2::Error::None) return err;
1294
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001295 return HWC2::Error::None;
1296}
1297
Roman Stratiienko6b405052022-12-10 19:09:10 +02001298#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001299
1300const Backend *HwcDisplay::backend() const {
1301 return backend_.get();
1302}
1303
1304void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1305 backend_ = std::move(backend);
1306}
1307
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001308} // namespace android