blob: e8bd94510d3bc6185096f11191379953ab57c2af [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
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020022#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020023#include "backend/BackendManager.h"
24#include "bufferinfo/BufferInfoGetter.h"
Drew Davenport93443182023-12-14 09:25:45 +000025#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020026#include "utils/log.h"
27#include "utils/properties.h"
28
29namespace android {
30
31std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
32 if (delta.total_pixops_ == 0)
33 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030034 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020035
36 std::stringstream ss;
37 ss << " Total frames count: " << delta.total_frames_ << "\n"
38 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
39 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
40 << ((delta.failed_kms_present_ > 0)
41 ? " !!! Internal failure, FIX it please\n"
42 : "")
43 << " Flattened frames: " << delta.frames_flattened_ << "\n"
44 << " Pixel operations (free units)"
45 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
46 << "]\n"
47 << " Composition efficiency: " << ratio;
48
49 return ss.str();
50}
51
52std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030053 auto connector_name = IsInHeadlessMode()
54 ? std::string("NULL-DISPLAY")
55 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020056
Roman Stratiienko3627beb2022-01-04 16:02:55 +020057 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020058 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020059 << "Statistics since system boot:\n"
60 << DumpDelta(total_stats_) << "\n\n"
61 << "Statistics since last dumpsys request:\n"
62 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
63
64 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
65 return ss.str();
66}
67
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020068HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +000069 DrmHwc *hwc)
70 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030071 if (type_ == HWC2::DisplayType::Virtual) {
72 writeback_layer_ = std::make_unique<HwcLayer>(this);
73 }
74}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020075
76void HwcDisplay::SetColorMarixToIdentity() {
77 color_matrix_ = std::make_shared<drm_color_ctm>();
78 for (int i = 0; i < kCtmCols; i++) {
79 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080080 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020081 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
82 }
83 }
84
85 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020086}
87
Normunds Rieksts545096d2024-03-11 16:37:45 +000088HwcDisplay::~HwcDisplay() {
89 Deinit();
90};
Roman Stratiienko3dacd472022-01-11 19:18:34 +020091
Roman Stratiienko63762a92023-09-18 22:33:45 +030092void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +020093 Deinit();
94
Roman Stratiienko63762a92023-09-18 22:33:45 +030095 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020096
Roman Stratiienko63762a92023-09-18 22:33:45 +030097 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020098 Init();
Drew Davenport93443182023-12-14 09:25:45 +000099 hwc_->ScheduleHotplugEvent(handle_, /*connected = */ true);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200100 } else {
Drew Davenport93443182023-12-14 09:25:45 +0000101 hwc_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200102 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200103}
104
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200105void HwcDisplay::Deinit() {
106 if (pipeline_ != nullptr) {
107 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200108 a_args.composition = std::make_shared<DrmKmsPlan>();
109 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000110/*
111 * TODO:
112 * Unfortunately the following causes regressions on db845c
113 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
114 * never coming back. Patches to avoiding that issue on the
115 * the kernel side unfortunately causes further crashes in
116 * drm_hwcomposer, because the client detach takes longer then the
117 * 1 second max VTS expects. So for now as a workaround, lets skip
118 * deactivating the display on deinit, which matches previous
119 * behavior prior to commit d0494d9b8097
120 */
121#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300122 a_args.composition = {};
123 a_args.active = false;
124 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000125#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200126
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200127 current_plan_.reset();
128 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200129 if (flatcon_) {
130 flatcon_->StopThread();
131 flatcon_.reset();
132 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200133 }
134
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200135 if (vsync_worker_) {
136 vsync_worker_->StopThread();
137 vsync_worker_ = {};
138 }
139
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200140 SetClientTarget(nullptr, -1, 0, {});
141}
142
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200143HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200144 ChosePreferredConfig();
145
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200146 auto vsw_callbacks = (VSyncWorkerCallbacks){
147 .out_event =
148 [this](int64_t timestamp) {
Drew Davenport93443182023-12-14 09:25:45 +0000149 const std::unique_lock lock(hwc_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200150 if (vsync_event_en_) {
151 uint32_t period_ns{};
152 GetDisplayVsyncPeriod(&period_ns);
Drew Davenport93443182023-12-14 09:25:45 +0000153 hwc_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200154 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200155 if (vsync_tracking_en_) {
156 last_vsync_ts_ = timestamp;
157 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200158 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200159 vsync_worker_->VSyncControl(false);
160 }
161 },
162 .get_vperiod_ns = [this]() -> uint32_t {
163 uint32_t outVsyncPeriod = 0;
164 GetDisplayVsyncPeriod(&outVsyncPeriod);
165 return outVsyncPeriod;
166 },
167 };
168
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300169 if (type_ != HWC2::DisplayType::Virtual) {
170 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
171 if (!vsync_worker_) {
172 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
173 return HWC2::Error::BadDisplay;
174 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200175 }
176
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200177 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200178 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200179 if (ret) {
180 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
181 return HWC2::Error::BadDisplay;
182 }
Drew Davenport93443182023-12-14 09:25:45 +0000183 auto flatcbk = (struct FlatConCallbacks){
184 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200185 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200186 }
187
188 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
189
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200190 SetColorMarixToIdentity();
191
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200192 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200193}
194
195HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200196 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300197 if (type_ == HWC2::DisplayType::Virtual) {
198 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
199 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 err = configs_.Update(*pipeline_->connector->Get());
201 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300202 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200203 }
204 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200205 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200206 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207
Roman Stratiienko0137f862022-01-04 18:27:40 +0200208 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209}
210
211HWC2::Error HwcDisplay::AcceptDisplayChanges() {
212 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
213 l.second.AcceptTypeChange();
214 return HWC2::Error::None;
215}
216
217HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200218 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200219 *layer = static_cast<hwc2_layer_t>(layer_idx_);
220 ++layer_idx_;
221 return HWC2::Error::None;
222}
223
224HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200225 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200227 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200228
229 layers_.erase(layer);
230 return HWC2::Error::None;
231}
232
233HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200234 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200235 return HWC2::Error::BadConfig;
236
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200237 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200238 return HWC2::Error::None;
239}
240
241HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
242 hwc2_layer_t *layers,
243 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200244 if (IsInHeadlessMode()) {
245 *num_elements = 0;
246 return HWC2::Error::None;
247 }
248
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200249 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300250 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200251 if (l.second.IsTypeChanged()) {
252 if (layers && num_changes < *num_elements)
253 layers[num_changes] = l.first;
254 if (types && num_changes < *num_elements)
255 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
256 ++num_changes;
257 }
258 }
259 if (!layers && !types)
260 *num_elements = num_changes;
261 return HWC2::Error::None;
262}
263
264HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
265 int32_t /*format*/,
266 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200267 if (IsInHeadlessMode()) {
268 return HWC2::Error::None;
269 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200270
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300271 auto min = pipeline_->device->GetMinResolution();
272 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200273
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200274 if (width < min.first || height < min.second)
275 return HWC2::Error::Unsupported;
276
277 if (width > max.first || height > max.second)
278 return HWC2::Error::Unsupported;
279
280 if (dataspace != HAL_DATASPACE_UNKNOWN)
281 return HWC2::Error::Unsupported;
282
283 // TODO(nobody): Validate format can be handled by either GL or planes
284 return HWC2::Error::None;
285}
286
287HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
288 if (!modes)
289 *num_modes = 1;
290
291 if (modes)
292 *modes = HAL_COLOR_MODE_NATIVE;
293
294 return HWC2::Error::None;
295}
296
297HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
298 int32_t attribute_in,
299 int32_t *value) {
300 int conf = static_cast<int>(config);
301
Roman Stratiienko0137f862022-01-04 18:27:40 +0200302 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200303 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200304 return HWC2::Error::BadConfig;
305 }
306
Roman Stratiienko0137f862022-01-04 18:27:40 +0200307 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200308
309 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300310 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200311 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
312 switch (attribute) {
313 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200314 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315 break;
316 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200317 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200318 break;
319 case HWC2::Attribute::VsyncPeriod:
320 // in nanoseconds
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200321 *value = static_cast<int>(1E9 / hwc_config.mode.GetVRefresh());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200322 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000323 case HWC2::Attribute::DpiY:
324 // ideally this should be vdisplay/mm_heigth, however mm_height
325 // comes from edid parsing and is highly unreliable. Viewing the
326 // rarity of anisotropic displays, falling back to a single value
327 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 case HWC2::Attribute::DpiX:
329 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200330 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
331 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200332 : -1;
333 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200334#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 case HWC2::Attribute::ConfigGroup:
336 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
337 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200338 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 break;
340#endif
341 default:
342 *value = -1;
343 return HWC2::Error::BadConfig;
344 }
345 return HWC2::Error::None;
346}
347
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200348HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
349 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200350 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200351 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200352 if (hwc_config.second.disabled) {
353 continue;
354 }
355
356 if (configs != nullptr) {
357 if (idx >= *num_configs) {
358 break;
359 }
360 configs[idx] = hwc_config.second.id;
361 }
362
363 idx++;
364 }
365 *num_configs = idx;
366 return HWC2::Error::None;
367}
368
369HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
370 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200371 if (IsInHeadlessMode()) {
372 stream << "null-display";
373 } else {
374 stream << "display-" << GetPipe().connector->Get()->GetId();
375 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300376 auto string = stream.str();
377 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200378 if (!name) {
379 *size = length;
380 return HWC2::Error::None;
381 }
382
383 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
384 strncpy(name, string.c_str(), *size);
385 return HWC2::Error::None;
386}
387
388HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
389 uint32_t *num_elements,
390 hwc2_layer_t * /*layers*/,
391 int32_t * /*layer_requests*/) {
392 // TODO(nobody): I think virtual display should request
393 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
394 *num_elements = 0;
395 return HWC2::Error::None;
396}
397
398HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
399 *type = static_cast<int32_t>(type_);
400 return HWC2::Error::None;
401}
402
403HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
404 *support = 0;
405 return HWC2::Error::None;
406}
407
408HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
409 int32_t * /*types*/,
410 float * /*max_luminance*/,
411 float * /*max_average_luminance*/,
412 float * /*min_luminance*/) {
413 *num_types = 0;
414 return HWC2::Error::None;
415}
416
417/* Find API details at:
418 * 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 +0300419 *
420 * Called after PresentDisplay(), CLIENT is expecting release fence for the
421 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200422 */
423HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
424 hwc2_layer_t *layers,
425 int32_t *fences) {
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_layers = 0;
432
Roman Stratiienkodd214942022-05-03 18:24:49 +0300433 for (auto &l : layers_) {
434 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
435 continue;
436 }
437
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200438 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300439
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200440 if (layers == nullptr || fences == nullptr)
441 continue;
442
443 if (num_layers > *num_elements) {
444 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
445 return HWC2::Error::None;
446 }
447
448 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200449 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200450 }
451 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300452
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200453 return HWC2::Error::None;
454}
455
456HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200457 if (IsInHeadlessMode()) {
458 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
459 return HWC2::Error::None;
460 }
461
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200462 a_args.color_matrix = color_matrix_;
463
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200464 uint32_t prev_vperiod_ns = 0;
465 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200466
467 auto mode_update_commited_ = false;
468 if (staged_mode_ &&
469 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
470 client_layer_.SetLayerDisplayFrame(
471 (hwc_rect_t){.left = 0,
472 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200473 .right = int(staged_mode_->GetRawMode().hdisplay),
474 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200475
476 configs_.active_config_id = staged_mode_config_id_;
477
478 a_args.display_mode = *staged_mode_;
479 if (!a_args.test_only) {
480 mode_update_commited_ = true;
481 }
482 }
483
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200484 // order the layers by z-order
485 bool use_client_layer = false;
486 uint32_t client_z_order = UINT32_MAX;
487 std::map<uint32_t, HwcLayer *> z_map;
488 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
489 switch (l.second.GetValidatedType()) {
490 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300491 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200492 break;
493 case HWC2::Composition::Client:
494 // Place it at the z_order of the lowest client layer
495 use_client_layer = true;
496 client_z_order = std::min(client_z_order, l.second.GetZOrder());
497 break;
498 default:
499 continue;
500 }
501 }
502 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300503 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504
505 if (z_map.empty())
506 return HWC2::Error::BadLayer;
507
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200508 std::vector<LayerData> composition_layers;
509
510 /* Import & populate */
511 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200512 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200513 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514
515 // now that they're ordered by z, add them to the composition
516 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200517 if (!l.second->IsLayerUsableAsDevice()) {
518 /* This will be normally triggered on validation of the first frame
519 * containing CLIENT layer. At this moment client buffer is not yet
520 * provided by the CLIENT.
521 * This may be triggered once in HwcLayer lifecycle in case FB can't be
522 * imported. For example when non-contiguous buffer is imported into
523 * contiguous-only DRM/KMS driver.
524 */
525 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200526 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200527 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528 }
529
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200530 /* Store plan to ensure shared planes won't be stolen by other display
531 * in between of ValidateDisplay() and PresentDisplay() calls
532 */
533 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
534 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300535
536 if (type_ == HWC2::DisplayType::Virtual) {
537 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
538 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
539 .acquire_fence;
540 }
541
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200542 if (!current_plan_) {
543 if (!a_args.test_only) {
544 ALOGE("Failed to create DrmKmsPlan");
545 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546 return HWC2::Error::BadConfig;
547 }
548
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200549 a_args.composition = current_plan_;
550
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300551 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200552
553 if (ret) {
554 if (!a_args.test_only)
555 ALOGE("Failed to apply the frame composition ret=%d", ret);
556 return HWC2::Error::BadParameter;
557 }
558
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200559 if (mode_update_commited_) {
560 staged_mode_.reset();
561 vsync_tracking_en_ = false;
562 if (last_vsync_ts_ != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000563 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
564 last_vsync_ts_ +
565 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200566 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200567 }
568
569 return HWC2::Error::None;
570}
571
572/* Find API details at:
573 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
574 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300575HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200576 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300577 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200578 return HWC2::Error::None;
579 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200580 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200581
582 ++total_stats_.total_frames_;
583
584 AtomicCommitArgs a_args{};
585 ret = CreateComposition(a_args);
586
587 if (ret != HWC2::Error::None)
588 ++total_stats_.failed_kms_present_;
589
590 if (ret == HWC2::Error::BadLayer) {
591 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300592 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200593 return HWC2::Error::None;
594 }
595 if (ret != HWC2::Error::None)
596 return ret;
597
Roman Stratiienko76892782023-01-16 17:15:53 +0200598 this->present_fence_ = a_args.out_fence;
599 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200600
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200601 // Reset the color matrix so we don't apply it over and over again.
602 color_matrix_ = {};
603
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604 ++frame_no_;
605 return HWC2::Error::None;
606}
607
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200608HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
609 int64_t change_time) {
610 if (configs_.hwc_configs.count(config) == 0) {
611 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200612 return HWC2::Error::BadConfig;
613 }
614
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200615 staged_mode_ = configs_.hwc_configs[config].mode;
616 staged_mode_change_time_ = change_time;
617 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618
619 return HWC2::Error::None;
620}
621
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200622HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
623 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
624}
625
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200626/* Find API details at:
627 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
628 */
629HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
630 int32_t acquire_fence,
631 int32_t dataspace,
632 hwc_region_t /*damage*/) {
633 client_layer_.SetLayerBuffer(target, acquire_fence);
634 client_layer_.SetLayerDataspace(dataspace);
635
636 /*
637 * target can be nullptr, this does mean the Composer Service is calling
638 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
639 * 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
640 */
641 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300642 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200643 return HWC2::Error::None;
644 }
645
Roman Stratiienko5070d512022-05-30 13:41:20 +0300646 if (IsInHeadlessMode()) {
647 return HWC2::Error::None;
648 }
649
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200650 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200651 if (!client_layer_.IsLayerUsableAsDevice()) {
652 ALOGE("Client layer must be always usable by DRM/KMS");
653 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200654 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200656 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300657 if (!bi) {
658 ALOGE("%s: Invalid state", __func__);
659 return HWC2::Error::BadLayer;
660 }
661
662 auto source_crop = (hwc_frect_t){.left = 0.0F,
663 .top = 0.0F,
664 .right = static_cast<float>(bi->width),
665 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200666 client_layer_.SetLayerSourceCrop(source_crop);
667
668 return HWC2::Error::None;
669}
670
671HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
672 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
673 return HWC2::Error::BadParameter;
674
675 if (mode != HAL_COLOR_MODE_NATIVE)
676 return HWC2::Error::Unsupported;
677
678 color_mode_ = mode;
679 return HWC2::Error::None;
680}
681
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200682#include <xf86drmMode.h>
683
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200684HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
685 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
686 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
687 return HWC2::Error::BadParameter;
688
689 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
690 return HWC2::Error::BadParameter;
691
692 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200693
Roman Stratiienko5de61b52023-02-01 16:29:45 +0200694 if (IsInHeadlessMode())
695 return HWC2::Error::None;
696
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200697 if (!GetPipe().crtc->Get()->GetCtmProperty())
698 return HWC2::Error::None;
699
700 switch (color_transform_hint_) {
701 case HAL_COLOR_TRANSFORM_IDENTITY:
702 SetColorMarixToIdentity();
703 break;
704 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
705 color_matrix_ = std::make_shared<drm_color_ctm>();
706 /* DRM expects a 3x3 matrix, but the HAL provides a 4x4 matrix. */
707 for (int i = 0; i < kCtmCols; i++) {
708 for (int j = 0; j < kCtmRows; j++) {
709 constexpr int kInCtmRows = 4;
710 /* HAL matrix type is float, but DRM expects a s31.32 fix point */
Yongqin Liu152bc622023-01-29 00:48:10 +0800711 auto value = uint64_t(matrix[i * kInCtmRows + j] * float(1ULL << 32));
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200712 color_matrix_->matrix[i * kCtmRows + j] = value;
713 }
714 }
715 break;
716 default:
717 return HWC2::Error::Unsupported;
718 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200719
720 return HWC2::Error::None;
721}
722
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200723bool HwcDisplay::CtmByGpu() {
724 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
725 return false;
726
727 if (GetPipe().crtc->Get()->GetCtmProperty())
728 return false;
729
Drew Davenport93443182023-12-14 09:25:45 +0000730 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200731 return false;
732
733 return true;
734}
735
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300736HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
737 int32_t release_fence) {
738 writeback_layer_->SetLayerBuffer(buffer, release_fence);
739 writeback_layer_->PopulateLayerData();
740 if (!writeback_layer_->IsLayerUsableAsDevice()) {
741 ALOGE("Output layer must be always usable by DRM/KMS");
742 return HWC2::Error::BadLayer;
743 }
744 /* TODO: Check if format is supported by writeback connector */
745 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746}
747
748HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
749 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300750
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200751 AtomicCommitArgs a_args{};
752
753 switch (mode) {
754 case HWC2::PowerMode::Off:
755 a_args.active = false;
756 break;
757 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300758 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200759 break;
760 case HWC2::PowerMode::Doze:
761 case HWC2::PowerMode::DozeSuspend:
762 return HWC2::Error::Unsupported;
763 default:
John Stultzffe783c2024-02-14 10:51:27 -0800764 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200765 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300766 }
767
768 if (IsInHeadlessMode()) {
769 return HWC2::Error::None;
770 }
771
Jia Ren80566fe2022-11-17 17:26:00 +0800772 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300773 /*
774 * Setting the display to active before we have a composition
775 * can break some drivers, so skip setting a_args.active to
776 * true, as the next composition frame will implicitly activate
777 * the display
778 */
779 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
780 ? HWC2::Error::None
781 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200782 };
783
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300784 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 if (err) {
786 ALOGE("Failed to apply the dpms composition err=%d", err);
787 return HWC2::Error::BadParameter;
788 }
789 return HWC2::Error::None;
790}
791
792HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300793 if (type_ == HWC2::DisplayType::Virtual) {
794 return HWC2::Error::None;
795 }
796
Roman Stratiienko099c3112022-01-20 11:50:54 +0200797 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
798 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200799 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200800 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200801 return HWC2::Error::None;
802}
803
804HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
805 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200806 if (IsInHeadlessMode()) {
807 *num_types = *num_requests = 0;
808 return HWC2::Error::None;
809 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300810
811 /* In current drm_hwc design in case previous frame layer was not validated as
812 * a CLIENT, it is used by display controller (Front buffer). We have to store
813 * this state to provide the CLIENT with the release fences for such buffers.
814 */
815 for (auto &l : layers_) {
816 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
817 HWC2::Composition::Client);
818 }
819
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200820 return backend_->ValidateDisplay(this, num_types, num_requests);
821}
822
823std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
824 std::vector<HwcLayer *> ordered_layers;
825 ordered_layers.reserve(layers_.size());
826
827 for (auto &[handle, layer] : layers_) {
828 ordered_layers.emplace_back(&layer);
829 }
830
831 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
832 [](const HwcLayer *lhs, const HwcLayer *rhs) {
833 return lhs->GetZOrder() < rhs->GetZOrder();
834 });
835
836 return ordered_layers;
837}
838
Roman Stratiienko099c3112022-01-20 11:50:54 +0200839HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
840 uint32_t *outVsyncPeriod /* ns */) {
841 return GetDisplayAttribute(configs_.active_config_id,
842 HWC2_ATTRIBUTE_VSYNC_PERIOD,
843 (int32_t *)(outVsyncPeriod));
844}
845
Roman Stratiienko6b405052022-12-10 19:09:10 +0200846#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200847HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200848 if (IsInHeadlessMode()) {
849 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
850 return HWC2::Error::None;
851 }
852 /* Primary display should be always internal,
853 * otherwise SF will be unhappy and will crash
854 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200855 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200856 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200857 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200858 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
859 else
860 return HWC2::Error::BadConfig;
861
862 return HWC2::Error::None;
863}
864
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200865HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200866 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200867 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
868 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300869 if (type_ == HWC2::DisplayType::Virtual) {
870 return HWC2::Error::None;
871 }
872
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200873 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
874 return HWC2::Error::BadParameter;
875 }
876
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200877 uint32_t current_vsync_period{};
878 GetDisplayVsyncPeriod(&current_vsync_period);
879
880 if (vsyncPeriodChangeConstraints->seamlessRequired) {
881 return HWC2::Error::SeamlessNotAllowed;
882 }
883
884 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
885 ->desiredTimeNanos -
886 current_vsync_period;
887 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
888 if (ret != HWC2::Error::None) {
889 return ret;
890 }
891
892 outTimeline->refreshRequired = true;
893 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
894 ->desiredTimeNanos;
895
896 last_vsync_ts_ = 0;
897 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200898 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200899
900 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200901}
902
903HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
904 return HWC2::Error::Unsupported;
905}
906
907HWC2::Error HwcDisplay::GetSupportedContentTypes(
908 uint32_t *outNumSupportedContentTypes,
909 const uint32_t *outSupportedContentTypes) {
910 if (outSupportedContentTypes == nullptr)
911 *outNumSupportedContentTypes = 0;
912
913 return HWC2::Error::None;
914}
915
916HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
917 if (contentType != HWC2_CONTENT_TYPE_NONE)
918 return HWC2::Error::Unsupported;
919
920 /* TODO: Map to the DRM Connector property:
921 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
922 */
923
924 return HWC2::Error::None;
925}
926#endif
927
Roman Stratiienko6b405052022-12-10 19:09:10 +0200928#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200929HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
930 uint32_t *outDataSize,
931 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200932 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300933 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200934 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300935
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200936 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200937 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300938 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200939 }
940
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300941 *outPort = handle_; /* TDOD(nobody): What should be here? */
942
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200943 if (outData) {
944 *outDataSize = std::min(*outDataSize, blob->length);
945 memcpy(outData, blob->data, *outDataSize);
946 } else {
947 *outDataSize = blob->length;
948 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200949
950 return HWC2::Error::None;
951}
952
953HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200954 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200955 if (outNumCapabilities == nullptr) {
956 return HWC2::Error::BadParameter;
957 }
958
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200959 bool skip_ctm = false;
960
961 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +0000962 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200963 skip_ctm = true;
964
965 // Skip client CTM if DRM can handle it
966 if (!skip_ctm && !IsInHeadlessMode() &&
967 GetPipe().crtc->Get()->GetCtmProperty())
968 skip_ctm = true;
969
970 if (!skip_ctm) {
971 *outNumCapabilities = 0;
972 return HWC2::Error::None;
973 }
974
975 *outNumCapabilities = 1;
976 if (outCapabilities) {
977 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
978 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200979
980 return HWC2::Error::None;
981}
982
983HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
984 *supported = false;
985 return HWC2::Error::None;
986}
987
988HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
989 return HWC2::Error::Unsupported;
990}
991
Roman Stratiienko6b405052022-12-10 19:09:10 +0200992#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200993
Roman Stratiienko6b405052022-12-10 19:09:10 +0200994#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995
996HWC2::Error HwcDisplay::GetRenderIntents(
997 int32_t mode, uint32_t *outNumIntents,
998 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
999 if (mode != HAL_COLOR_MODE_NATIVE) {
1000 return HWC2::Error::BadParameter;
1001 }
1002
1003 if (outIntents == nullptr) {
1004 *outNumIntents = 1;
1005 return HWC2::Error::None;
1006 }
1007 *outNumIntents = 1;
1008 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1009 return HWC2::Error::None;
1010}
1011
1012HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1013 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1014 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1015 return HWC2::Error::BadParameter;
1016
1017 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1018 return HWC2::Error::BadParameter;
1019
1020 if (mode != HAL_COLOR_MODE_NATIVE)
1021 return HWC2::Error::Unsupported;
1022
1023 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1024 return HWC2::Error::Unsupported;
1025
1026 color_mode_ = mode;
1027 return HWC2::Error::None;
1028}
1029
Roman Stratiienko6b405052022-12-10 19:09:10 +02001030#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001031
1032const Backend *HwcDisplay::backend() const {
1033 return backend_.get();
1034}
1035
1036void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1037 backend_ = std::move(backend);
1038}
1039
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001040} // namespace android