blob: cdcf518ace51489f83295b2c6be3ec7e2d0975fd [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
17#define LOG_TAG "hwc-display"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
22#include "DrmHwcTwo.h"
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020023#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020024#include "backend/BackendManager.h"
25#include "bufferinfo/BufferInfoGetter.h"
26#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() {
53 std::string flattening_state_str;
54 switch (flattenning_state_) {
55 case ClientFlattenningState::Disabled:
56 flattening_state_str = "Disabled";
57 break;
58 case ClientFlattenningState::NotRequired:
59 flattening_state_str = "Not needed";
60 break;
61 case ClientFlattenningState::Flattened:
62 flattening_state_str = "Active";
63 break;
64 case ClientFlattenningState::ClientRefreshRequested:
65 flattening_state_str = "Refresh requested";
66 break;
67 default:
68 flattening_state_str = std::to_string(flattenning_state_) +
69 " VSync remains";
70 }
71
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030072 auto connector_name = IsInHeadlessMode()
73 ? std::string("NULL-DISPLAY")
74 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020075
Roman Stratiienko3627beb2022-01-04 16:02:55 +020076 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020077 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020078 << " Flattening state: " << flattening_state_str << "\n"
79 << "Statistics since system boot:\n"
80 << DumpDelta(total_stats_) << "\n\n"
81 << "Statistics since last dumpsys request:\n"
82 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
83
84 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
85 return ss.str();
86}
87
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020088HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
89 DrmHwcTwo *hwc2)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020090 : hwc2_(hwc2), handle_(handle), type_(type), client_layer_(this){};
91
92void HwcDisplay::SetColorMarixToIdentity() {
93 color_matrix_ = std::make_shared<drm_color_ctm>();
94 for (int i = 0; i < kCtmCols; i++) {
95 for (int j = 0; j < kCtmRows; j++) {
96 constexpr uint64_t kOne = 1L << 32; /* 1.0 in s31.32 format */
97 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
98 }
99 }
100
101 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200102}
103
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200104HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200105
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200106void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200107 Deinit();
108
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200109 pipeline_ = pipeline;
110
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200111 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200112 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200113 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
114 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200115 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200116 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200117}
118
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200119void HwcDisplay::Deinit() {
120 if (pipeline_ != nullptr) {
121 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200122 a_args.composition = std::make_shared<DrmKmsPlan>();
123 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000124/*
125 * TODO:
126 * Unfortunately the following causes regressions on db845c
127 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
128 * never coming back. Patches to avoiding that issue on the
129 * the kernel side unfortunately causes further crashes in
130 * drm_hwcomposer, because the client detach takes longer then the
131 * 1 second max VTS expects. So for now as a workaround, lets skip
132 * deactivating the display on deinit, which matches previous
133 * behavior prior to commit d0494d9b8097
134 */
135#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300136 a_args.composition = {};
137 a_args.active = false;
138 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000139#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200140
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200141 current_plan_.reset();
142 backend_.reset();
143 }
144
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200145 if (vsync_worker_) {
146 vsync_worker_->StopThread();
147 vsync_worker_ = {};
148 }
149
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200150 SetClientTarget(nullptr, -1, 0, {});
151}
152
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200153HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200154 ChosePreferredConfig();
155
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200156 auto vsw_callbacks = (VSyncWorkerCallbacks){
157 .out_event =
158 [this](int64_t timestamp) {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +0200159 const std::unique_lock lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200160 if (vsync_event_en_) {
161 uint32_t period_ns{};
162 GetDisplayVsyncPeriod(&period_ns);
163 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
164 }
165 if (vsync_flattening_en_) {
166 ProcessFlatenningVsyncInternal();
167 }
168 if (vsync_tracking_en_) {
169 last_vsync_ts_ = timestamp;
170 }
171 if (!vsync_event_en_ && !vsync_flattening_en_ &&
172 !vsync_tracking_en_) {
173 vsync_worker_->VSyncControl(false);
174 }
175 },
176 .get_vperiod_ns = [this]() -> uint32_t {
177 uint32_t outVsyncPeriod = 0;
178 GetDisplayVsyncPeriod(&outVsyncPeriod);
179 return outVsyncPeriod;
180 },
181 };
182
183 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
184 if (!vsync_worker_) {
185 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200186 return HWC2::Error::BadDisplay;
187 }
188
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200189 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200190 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200191 if (ret) {
192 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
193 return HWC2::Error::BadDisplay;
194 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195 }
196
197 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
198
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200199 SetColorMarixToIdentity();
200
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200201 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202}
203
204HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200205 HWC2::Error err{};
206 if (!IsInHeadlessMode()) {
207 err = configs_.Update(*pipeline_->connector->Get());
208 } else {
209 configs_.FillHeadless();
210 }
211 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200212 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200213 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200214
Roman Stratiienko0137f862022-01-04 18:27:40 +0200215 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216}
217
218HWC2::Error HwcDisplay::AcceptDisplayChanges() {
219 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
220 l.second.AcceptTypeChange();
221 return HWC2::Error::None;
222}
223
224HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200225 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226 *layer = static_cast<hwc2_layer_t>(layer_idx_);
227 ++layer_idx_;
228 return HWC2::Error::None;
229}
230
231HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200232 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200234 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200235
236 layers_.erase(layer);
237 return HWC2::Error::None;
238}
239
240HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200241 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200242 return HWC2::Error::BadConfig;
243
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200244 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 return HWC2::Error::None;
246}
247
248HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
249 hwc2_layer_t *layers,
250 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200251 if (IsInHeadlessMode()) {
252 *num_elements = 0;
253 return HWC2::Error::None;
254 }
255
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200256 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300257 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200258 if (l.second.IsTypeChanged()) {
259 if (layers && num_changes < *num_elements)
260 layers[num_changes] = l.first;
261 if (types && num_changes < *num_elements)
262 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
263 ++num_changes;
264 }
265 }
266 if (!layers && !types)
267 *num_elements = num_changes;
268 return HWC2::Error::None;
269}
270
271HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
272 int32_t /*format*/,
273 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200274 if (IsInHeadlessMode()) {
275 return HWC2::Error::None;
276 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200277
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300278 auto min = pipeline_->device->GetMinResolution();
279 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200280
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281 if (width < min.first || height < min.second)
282 return HWC2::Error::Unsupported;
283
284 if (width > max.first || height > max.second)
285 return HWC2::Error::Unsupported;
286
287 if (dataspace != HAL_DATASPACE_UNKNOWN)
288 return HWC2::Error::Unsupported;
289
290 // TODO(nobody): Validate format can be handled by either GL or planes
291 return HWC2::Error::None;
292}
293
294HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
295 if (!modes)
296 *num_modes = 1;
297
298 if (modes)
299 *modes = HAL_COLOR_MODE_NATIVE;
300
301 return HWC2::Error::None;
302}
303
304HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
305 int32_t attribute_in,
306 int32_t *value) {
307 int conf = static_cast<int>(config);
308
Roman Stratiienko0137f862022-01-04 18:27:40 +0200309 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200310 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200311 return HWC2::Error::BadConfig;
312 }
313
Roman Stratiienko0137f862022-01-04 18:27:40 +0200314 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315
316 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300317 auto mm_width = configs_.mm_width;
318 auto mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200319 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
320 switch (attribute) {
321 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200322 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200323 break;
324 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200325 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200326 break;
327 case HWC2::Attribute::VsyncPeriod:
328 // in nanoseconds
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200329 *value = static_cast<int>(1E9 / hwc_config.mode.GetVRefresh());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330 break;
331 case HWC2::Attribute::DpiX:
332 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200333 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
334 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 : -1;
336 break;
337 case HWC2::Attribute::DpiY:
338 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200339 *value = mm_height ? int(hwc_config.mode.GetRawMode().vdisplay *
340 kUmPerInch / mm_height)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 : -1;
342 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200343#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200344 case HWC2::Attribute::ConfigGroup:
345 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
346 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200347 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200348 break;
349#endif
350 default:
351 *value = -1;
352 return HWC2::Error::BadConfig;
353 }
354 return HWC2::Error::None;
355}
356
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200357HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
358 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200359 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200360 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200361 if (hwc_config.second.disabled) {
362 continue;
363 }
364
365 if (configs != nullptr) {
366 if (idx >= *num_configs) {
367 break;
368 }
369 configs[idx] = hwc_config.second.id;
370 }
371
372 idx++;
373 }
374 *num_configs = idx;
375 return HWC2::Error::None;
376}
377
378HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
379 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200380 if (IsInHeadlessMode()) {
381 stream << "null-display";
382 } else {
383 stream << "display-" << GetPipe().connector->Get()->GetId();
384 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300385 auto string = stream.str();
386 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200387 if (!name) {
388 *size = length;
389 return HWC2::Error::None;
390 }
391
392 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
393 strncpy(name, string.c_str(), *size);
394 return HWC2::Error::None;
395}
396
397HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
398 uint32_t *num_elements,
399 hwc2_layer_t * /*layers*/,
400 int32_t * /*layer_requests*/) {
401 // TODO(nobody): I think virtual display should request
402 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
403 *num_elements = 0;
404 return HWC2::Error::None;
405}
406
407HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
408 *type = static_cast<int32_t>(type_);
409 return HWC2::Error::None;
410}
411
412HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
413 *support = 0;
414 return HWC2::Error::None;
415}
416
417HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
418 int32_t * /*types*/,
419 float * /*max_luminance*/,
420 float * /*max_average_luminance*/,
421 float * /*min_luminance*/) {
422 *num_types = 0;
423 return HWC2::Error::None;
424}
425
426/* Find API details at:
427 * 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 +0300428 *
429 * Called after PresentDisplay(), CLIENT is expecting release fence for the
430 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200431 */
432HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
433 hwc2_layer_t *layers,
434 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200435 if (IsInHeadlessMode()) {
436 *num_elements = 0;
437 return HWC2::Error::None;
438 }
439
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200440 uint32_t num_layers = 0;
441
Roman Stratiienkodd214942022-05-03 18:24:49 +0300442 for (auto &l : layers_) {
443 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
444 continue;
445 }
446
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200447 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300448
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200449 if (layers == nullptr || fences == nullptr)
450 continue;
451
452 if (num_layers > *num_elements) {
453 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
454 return HWC2::Error::None;
455 }
456
457 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200458 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200459 }
460 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300461
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200462 return HWC2::Error::None;
463}
464
465HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200466 if (IsInHeadlessMode()) {
467 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
468 return HWC2::Error::None;
469 }
470
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200471 a_args.color_matrix = color_matrix_;
472
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200473 uint32_t prev_vperiod_ns = 0;
474 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200475
476 auto mode_update_commited_ = false;
477 if (staged_mode_ &&
478 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
479 client_layer_.SetLayerDisplayFrame(
480 (hwc_rect_t){.left = 0,
481 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200482 .right = int(staged_mode_->GetRawMode().hdisplay),
483 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200484
485 configs_.active_config_id = staged_mode_config_id_;
486
487 a_args.display_mode = *staged_mode_;
488 if (!a_args.test_only) {
489 mode_update_commited_ = true;
490 }
491 }
492
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200493 // order the layers by z-order
494 bool use_client_layer = false;
495 uint32_t client_z_order = UINT32_MAX;
496 std::map<uint32_t, HwcLayer *> z_map;
497 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
498 switch (l.second.GetValidatedType()) {
499 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300500 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501 break;
502 case HWC2::Composition::Client:
503 // Place it at the z_order of the lowest client layer
504 use_client_layer = true;
505 client_z_order = std::min(client_z_order, l.second.GetZOrder());
506 break;
507 default:
508 continue;
509 }
510 }
511 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300512 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200513
514 if (z_map.empty())
515 return HWC2::Error::BadLayer;
516
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200517 std::vector<LayerData> composition_layers;
518
519 /* Import & populate */
520 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200521 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200522 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200523
524 // now that they're ordered by z, add them to the composition
525 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200526 if (!l.second->IsLayerUsableAsDevice()) {
527 /* This will be normally triggered on validation of the first frame
528 * containing CLIENT layer. At this moment client buffer is not yet
529 * provided by the CLIENT.
530 * This may be triggered once in HwcLayer lifecycle in case FB can't be
531 * imported. For example when non-contiguous buffer is imported into
532 * contiguous-only DRM/KMS driver.
533 */
534 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200536 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537 }
538
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200539 /* Store plan to ensure shared planes won't be stolen by other display
540 * in between of ValidateDisplay() and PresentDisplay() calls
541 */
542 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
543 std::move(composition_layers));
544 if (!current_plan_) {
545 if (!a_args.test_only) {
546 ALOGE("Failed to create DrmKmsPlan");
547 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200548 return HWC2::Error::BadConfig;
549 }
550
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200551 a_args.composition = current_plan_;
552
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300553 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200554
555 if (ret) {
556 if (!a_args.test_only)
557 ALOGE("Failed to apply the frame composition ret=%d", ret);
558 return HWC2::Error::BadParameter;
559 }
560
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200561 if (mode_update_commited_) {
562 staged_mode_.reset();
563 vsync_tracking_en_ = false;
564 if (last_vsync_ts_ != 0) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200565 hwc2_->SendVsyncPeriodTimingChangedEventToClient(handle_,
566 last_vsync_ts_ +
567 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200568 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200569 }
570
571 return HWC2::Error::None;
572}
573
574/* Find API details at:
575 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
576 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300577HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200578 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300579 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200580 return HWC2::Error::None;
581 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200582 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200583
584 ++total_stats_.total_frames_;
585
586 AtomicCommitArgs a_args{};
587 ret = CreateComposition(a_args);
588
589 if (ret != HWC2::Error::None)
590 ++total_stats_.failed_kms_present_;
591
592 if (ret == HWC2::Error::BadLayer) {
593 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300594 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200595 return HWC2::Error::None;
596 }
597 if (ret != HWC2::Error::None)
598 return ret;
599
Roman Stratiienko76892782023-01-16 17:15:53 +0200600 this->present_fence_ = a_args.out_fence;
601 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200602
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200603 // Reset the color matrix so we don't apply it over and over again.
604 color_matrix_ = {};
605
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606 ++frame_no_;
607 return HWC2::Error::None;
608}
609
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200610HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
611 int64_t change_time) {
612 if (configs_.hwc_configs.count(config) == 0) {
613 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200614 return HWC2::Error::BadConfig;
615 }
616
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200617 staged_mode_ = configs_.hwc_configs[config].mode;
618 staged_mode_change_time_ = change_time;
619 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620
621 return HWC2::Error::None;
622}
623
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200624HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
625 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
626}
627
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200628/* Find API details at:
629 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
630 */
631HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
632 int32_t acquire_fence,
633 int32_t dataspace,
634 hwc_region_t /*damage*/) {
635 client_layer_.SetLayerBuffer(target, acquire_fence);
636 client_layer_.SetLayerDataspace(dataspace);
637
638 /*
639 * target can be nullptr, this does mean the Composer Service is calling
640 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
641 * 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
642 */
643 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300644 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 return HWC2::Error::None;
646 }
647
Roman Stratiienko5070d512022-05-30 13:41:20 +0300648 if (IsInHeadlessMode()) {
649 return HWC2::Error::None;
650 }
651
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200652 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200653 if (!client_layer_.IsLayerUsableAsDevice()) {
654 ALOGE("Client layer must be always usable by DRM/KMS");
655 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200656 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200657
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200658 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300659 if (!bi) {
660 ALOGE("%s: Invalid state", __func__);
661 return HWC2::Error::BadLayer;
662 }
663
664 auto source_crop = (hwc_frect_t){.left = 0.0F,
665 .top = 0.0F,
666 .right = static_cast<float>(bi->width),
667 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200668 client_layer_.SetLayerSourceCrop(source_crop);
669
670 return HWC2::Error::None;
671}
672
673HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
674 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
675 return HWC2::Error::BadParameter;
676
677 if (mode != HAL_COLOR_MODE_NATIVE)
678 return HWC2::Error::Unsupported;
679
680 color_mode_ = mode;
681 return HWC2::Error::None;
682}
683
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200684#include <xf86drmMode.h>
685
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200686HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
687 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
688 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
689 return HWC2::Error::BadParameter;
690
691 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
692 return HWC2::Error::BadParameter;
693
694 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200695
696 if (!GetPipe().crtc->Get()->GetCtmProperty())
697 return HWC2::Error::None;
698
699 switch (color_transform_hint_) {
700 case HAL_COLOR_TRANSFORM_IDENTITY:
701 SetColorMarixToIdentity();
702 break;
703 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
704 color_matrix_ = std::make_shared<drm_color_ctm>();
705 /* DRM expects a 3x3 matrix, but the HAL provides a 4x4 matrix. */
706 for (int i = 0; i < kCtmCols; i++) {
707 for (int j = 0; j < kCtmRows; j++) {
708 constexpr int kInCtmRows = 4;
709 /* HAL matrix type is float, but DRM expects a s31.32 fix point */
710 auto value = uint64_t(matrix[i * kInCtmRows + j] * float(1L << 32));
711 color_matrix_->matrix[i * kCtmRows + j] = value;
712 }
713 }
714 break;
715 default:
716 return HWC2::Error::Unsupported;
717 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200718
719 return HWC2::Error::None;
720}
721
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200722bool HwcDisplay::CtmByGpu() {
723 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
724 return false;
725
726 if (GetPipe().crtc->Get()->GetCtmProperty())
727 return false;
728
729 if (GetHwc2()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
730 return false;
731
732 return true;
733}
734
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200735HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
736 int32_t /*release_fence*/) {
737 // TODO(nobody): Need virtual display support
738 return HWC2::Error::Unsupported;
739}
740
741HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
742 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300743
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200744 AtomicCommitArgs a_args{};
745
746 switch (mode) {
747 case HWC2::PowerMode::Off:
748 a_args.active = false;
749 break;
750 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300751 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200752 break;
753 case HWC2::PowerMode::Doze:
754 case HWC2::PowerMode::DozeSuspend:
755 return HWC2::Error::Unsupported;
756 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300757 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200758 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300759 }
760
761 if (IsInHeadlessMode()) {
762 return HWC2::Error::None;
763 }
764
Jia Ren80566fe2022-11-17 17:26:00 +0800765 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300766 /*
767 * Setting the display to active before we have a composition
768 * can break some drivers, so skip setting a_args.active to
769 * true, as the next composition frame will implicitly activate
770 * the display
771 */
772 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
773 ? HWC2::Error::None
774 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200775 };
776
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300777 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200778 if (err) {
779 ALOGE("Failed to apply the dpms composition err=%d", err);
780 return HWC2::Error::BadParameter;
781 }
782 return HWC2::Error::None;
783}
784
785HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200786 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
787 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200788 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200789 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200790 return HWC2::Error::None;
791}
792
793HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
794 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200795 if (IsInHeadlessMode()) {
796 *num_types = *num_requests = 0;
797 return HWC2::Error::None;
798 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300799
800 /* In current drm_hwc design in case previous frame layer was not validated as
801 * a CLIENT, it is used by display controller (Front buffer). We have to store
802 * this state to provide the CLIENT with the release fences for such buffers.
803 */
804 for (auto &l : layers_) {
805 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
806 HWC2::Composition::Client);
807 }
808
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200809 return backend_->ValidateDisplay(this, num_types, num_requests);
810}
811
812std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
813 std::vector<HwcLayer *> ordered_layers;
814 ordered_layers.reserve(layers_.size());
815
816 for (auto &[handle, layer] : layers_) {
817 ordered_layers.emplace_back(&layer);
818 }
819
820 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
821 [](const HwcLayer *lhs, const HwcLayer *rhs) {
822 return lhs->GetZOrder() < rhs->GetZOrder();
823 });
824
825 return ordered_layers;
826}
827
Roman Stratiienko099c3112022-01-20 11:50:54 +0200828HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
829 uint32_t *outVsyncPeriod /* ns */) {
830 return GetDisplayAttribute(configs_.active_config_id,
831 HWC2_ATTRIBUTE_VSYNC_PERIOD,
832 (int32_t *)(outVsyncPeriod));
833}
834
Roman Stratiienko6b405052022-12-10 19:09:10 +0200835#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200836HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200837 if (IsInHeadlessMode()) {
838 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
839 return HWC2::Error::None;
840 }
841 /* Primary display should be always internal,
842 * otherwise SF will be unhappy and will crash
843 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200844 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200846 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200847 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
848 else
849 return HWC2::Error::BadConfig;
850
851 return HWC2::Error::None;
852}
853
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200854HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200855 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200856 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
857 hwc_vsync_period_change_timeline_t *outTimeline) {
858 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
859 return HWC2::Error::BadParameter;
860 }
861
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200862 uint32_t current_vsync_period{};
863 GetDisplayVsyncPeriod(&current_vsync_period);
864
865 if (vsyncPeriodChangeConstraints->seamlessRequired) {
866 return HWC2::Error::SeamlessNotAllowed;
867 }
868
869 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
870 ->desiredTimeNanos -
871 current_vsync_period;
872 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
873 if (ret != HWC2::Error::None) {
874 return ret;
875 }
876
877 outTimeline->refreshRequired = true;
878 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
879 ->desiredTimeNanos;
880
881 last_vsync_ts_ = 0;
882 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200883 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200884
885 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200886}
887
888HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
889 return HWC2::Error::Unsupported;
890}
891
892HWC2::Error HwcDisplay::GetSupportedContentTypes(
893 uint32_t *outNumSupportedContentTypes,
894 const uint32_t *outSupportedContentTypes) {
895 if (outSupportedContentTypes == nullptr)
896 *outNumSupportedContentTypes = 0;
897
898 return HWC2::Error::None;
899}
900
901HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
902 if (contentType != HWC2_CONTENT_TYPE_NONE)
903 return HWC2::Error::Unsupported;
904
905 /* TODO: Map to the DRM Connector property:
906 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
907 */
908
909 return HWC2::Error::None;
910}
911#endif
912
Roman Stratiienko6b405052022-12-10 19:09:10 +0200913#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200914HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
915 uint32_t *outDataSize,
916 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200917 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300918 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200919 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300920
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200921 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200922 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300923 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200924 }
925
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300926 *outPort = handle_; /* TDOD(nobody): What should be here? */
927
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200928 if (outData) {
929 *outDataSize = std::min(*outDataSize, blob->length);
930 memcpy(outData, blob->data, *outDataSize);
931 } else {
932 *outDataSize = blob->length;
933 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200934
935 return HWC2::Error::None;
936}
937
938HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200939 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200940 if (outNumCapabilities == nullptr) {
941 return HWC2::Error::BadParameter;
942 }
943
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200944 bool skip_ctm = false;
945
946 // Skip client CTM if user requested DRM_OR_IGNORE
947 if (GetHwc2()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
948 skip_ctm = true;
949
950 // Skip client CTM if DRM can handle it
951 if (!skip_ctm && !IsInHeadlessMode() &&
952 GetPipe().crtc->Get()->GetCtmProperty())
953 skip_ctm = true;
954
955 if (!skip_ctm) {
956 *outNumCapabilities = 0;
957 return HWC2::Error::None;
958 }
959
960 *outNumCapabilities = 1;
961 if (outCapabilities) {
962 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
963 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200964
965 return HWC2::Error::None;
966}
967
968HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
969 *supported = false;
970 return HWC2::Error::None;
971}
972
973HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
974 return HWC2::Error::Unsupported;
975}
976
Roman Stratiienko6b405052022-12-10 19:09:10 +0200977#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200978
Roman Stratiienko6b405052022-12-10 19:09:10 +0200979#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200980
981HWC2::Error HwcDisplay::GetRenderIntents(
982 int32_t mode, uint32_t *outNumIntents,
983 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
984 if (mode != HAL_COLOR_MODE_NATIVE) {
985 return HWC2::Error::BadParameter;
986 }
987
988 if (outIntents == nullptr) {
989 *outNumIntents = 1;
990 return HWC2::Error::None;
991 }
992 *outNumIntents = 1;
993 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
994 return HWC2::Error::None;
995}
996
997HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
998 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
999 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1000 return HWC2::Error::BadParameter;
1001
1002 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1003 return HWC2::Error::BadParameter;
1004
1005 if (mode != HAL_COLOR_MODE_NATIVE)
1006 return HWC2::Error::Unsupported;
1007
1008 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1009 return HWC2::Error::Unsupported;
1010
1011 color_mode_ = mode;
1012 return HWC2::Error::None;
1013}
1014
Roman Stratiienko6b405052022-12-10 19:09:10 +02001015#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001016
1017const Backend *HwcDisplay::backend() const {
1018 return backend_.get();
1019}
1020
1021void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1022 backend_ = std::move(backend);
1023}
1024
Roman Stratiienko099c3112022-01-20 11:50:54 +02001025/* returns true if composition should be sent to client */
1026bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001027 const int flattenning_state = flattenning_state_;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001028 if (flattenning_state == ClientFlattenningState::Disabled) {
1029 return false;
1030 }
1031
1032 if (skip) {
1033 flattenning_state_ = ClientFlattenningState::NotRequired;
1034 return false;
1035 }
1036
1037 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
1038 flattenning_state_ = ClientFlattenningState::Flattened;
1039 return true;
1040 }
1041
1042 vsync_flattening_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +02001043 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +02001044 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
1045 return false;
1046}
1047
1048void HwcDisplay::ProcessFlatenningVsyncInternal() {
1049 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
1050 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
1051 hwc2_->refresh_callback_.first != nullptr &&
1052 hwc2_->refresh_callback_.second != nullptr) {
1053 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
1054 vsync_flattening_en_ = false;
1055 }
1056}
1057
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001058} // namespace android