blob: d968ab395ef8721a942e37fc4dfb681e077daa3d [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";
34 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
35
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 Stratiienko19c162f2022-02-01 09:35:08 +020072 std::string connector_name = IsInHeadlessMode()
73 ? "NULL-DISPLAY"
74 : GetPipe().connector->Get()->GetName();
75
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 Stratiienko3627beb2022-01-04 16:02:55 +020090 : hwc2_(hwc2),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020091 handle_(handle),
92 type_(type),
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020093 client_layer_(this),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020094 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
95 // clang-format off
96 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
97 0.0, 1.0, 0.0, 0.0,
98 0.0, 0.0, 1.0, 0.0,
99 0.0, 0.0, 0.0, 1.0};
100 // clang-format on
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200101}
102
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200103HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200104
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200105void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200106 Deinit();
107
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200108 pipeline_ = pipeline;
109
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200110 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200111 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200112 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
113 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200114 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200115 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200116}
117
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200118void HwcDisplay::Deinit() {
119 if (pipeline_ != nullptr) {
120 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200121 a_args.composition = std::make_shared<DrmKmsPlan>();
122 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000123/*
124 * TODO:
125 * Unfortunately the following causes regressions on db845c
126 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
127 * never coming back. Patches to avoiding that issue on the
128 * the kernel side unfortunately causes further crashes in
129 * drm_hwcomposer, because the client detach takes longer then the
130 * 1 second max VTS expects. So for now as a workaround, lets skip
131 * deactivating the display on deinit, which matches previous
132 * behavior prior to commit d0494d9b8097
133 */
134#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300135 a_args.composition = {};
136 a_args.active = false;
137 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000138#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200139
140 vsync_worker_.Init(nullptr, [](int64_t) {});
141 current_plan_.reset();
142 backend_.reset();
143 }
144
145 SetClientTarget(nullptr, -1, 0, {});
146}
147
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200148HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200149 ChosePreferredConfig();
150
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200151 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200152 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200153 if (vsync_event_en_) {
154 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200155 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200156 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200157 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200158 if (vsync_flattening_en_) {
159 ProcessFlatenningVsyncInternal();
160 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200161 if (vsync_tracking_en_) {
162 last_vsync_ts_ = timestamp;
163 }
164 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200165 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200166 }
167 });
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200168 if (ret && ret != -EALREADY) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200169 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200170 return HWC2::Error::BadDisplay;
171 }
172
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200173 if (!IsInHeadlessMode()) {
174 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
175 if (ret) {
176 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
177 return HWC2::Error::BadDisplay;
178 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200179 }
180
181 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
182
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200183 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200184}
185
186HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200187 HWC2::Error err{};
188 if (!IsInHeadlessMode()) {
189 err = configs_.Update(*pipeline_->connector->Get());
190 } else {
191 configs_.FillHeadless();
192 }
193 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200195 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200196
Roman Stratiienko0137f862022-01-04 18:27:40 +0200197 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198}
199
200HWC2::Error HwcDisplay::AcceptDisplayChanges() {
201 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
202 l.second.AcceptTypeChange();
203 return HWC2::Error::None;
204}
205
206HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200207 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200208 *layer = static_cast<hwc2_layer_t>(layer_idx_);
209 ++layer_idx_;
210 return HWC2::Error::None;
211}
212
213HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200214 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200216 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200217
218 layers_.erase(layer);
219 return HWC2::Error::None;
220}
221
222HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200223 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200224 return HWC2::Error::BadConfig;
225
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200226 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200227 return HWC2::Error::None;
228}
229
230HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
231 hwc2_layer_t *layers,
232 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200233 if (IsInHeadlessMode()) {
234 *num_elements = 0;
235 return HWC2::Error::None;
236 }
237
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200238 uint32_t num_changes = 0;
239 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
240 if (l.second.IsTypeChanged()) {
241 if (layers && num_changes < *num_elements)
242 layers[num_changes] = l.first;
243 if (types && num_changes < *num_elements)
244 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
245 ++num_changes;
246 }
247 }
248 if (!layers && !types)
249 *num_elements = num_changes;
250 return HWC2::Error::None;
251}
252
253HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
254 int32_t /*format*/,
255 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200256 if (IsInHeadlessMode()) {
257 return HWC2::Error::None;
258 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200259
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200260 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
261 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
262
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200263 if (width < min.first || height < min.second)
264 return HWC2::Error::Unsupported;
265
266 if (width > max.first || height > max.second)
267 return HWC2::Error::Unsupported;
268
269 if (dataspace != HAL_DATASPACE_UNKNOWN)
270 return HWC2::Error::Unsupported;
271
272 // TODO(nobody): Validate format can be handled by either GL or planes
273 return HWC2::Error::None;
274}
275
276HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
277 if (!modes)
278 *num_modes = 1;
279
280 if (modes)
281 *modes = HAL_COLOR_MODE_NATIVE;
282
283 return HWC2::Error::None;
284}
285
286HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
287 int32_t attribute_in,
288 int32_t *value) {
289 int conf = static_cast<int>(config);
290
Roman Stratiienko0137f862022-01-04 18:27:40 +0200291 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200292 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200293 return HWC2::Error::BadConfig;
294 }
295
Roman Stratiienko0137f862022-01-04 18:27:40 +0200296 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200297
298 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200299 uint32_t mm_width = configs_.mm_width;
300 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200301 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
302 switch (attribute) {
303 case HWC2::Attribute::Width:
304 *value = static_cast<int>(hwc_config.mode.h_display());
305 break;
306 case HWC2::Attribute::Height:
307 *value = static_cast<int>(hwc_config.mode.v_display());
308 break;
309 case HWC2::Attribute::VsyncPeriod:
310 // in nanoseconds
311 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
312 break;
313 case HWC2::Attribute::DpiX:
314 // Dots per 1000 inches
315 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
316 kUmPerInch / mm_width)
317 : -1;
318 break;
319 case HWC2::Attribute::DpiY:
320 // Dots per 1000 inches
321 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
322 kUmPerInch / mm_height)
323 : -1;
324 break;
325#if PLATFORM_SDK_VERSION > 29
326 case HWC2::Attribute::ConfigGroup:
327 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
328 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200329 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330 break;
331#endif
332 default:
333 *value = -1;
334 return HWC2::Error::BadConfig;
335 }
336 return HWC2::Error::None;
337}
338
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
340 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200342 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200343 if (hwc_config.second.disabled) {
344 continue;
345 }
346
347 if (configs != nullptr) {
348 if (idx >= *num_configs) {
349 break;
350 }
351 configs[idx] = hwc_config.second.id;
352 }
353
354 idx++;
355 }
356 *num_configs = idx;
357 return HWC2::Error::None;
358}
359
360HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
361 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200362 if (IsInHeadlessMode()) {
363 stream << "null-display";
364 } else {
365 stream << "display-" << GetPipe().connector->Get()->GetId();
366 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200367 std::string string = stream.str();
368 size_t length = string.length();
369 if (!name) {
370 *size = length;
371 return HWC2::Error::None;
372 }
373
374 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
375 strncpy(name, string.c_str(), *size);
376 return HWC2::Error::None;
377}
378
379HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
380 uint32_t *num_elements,
381 hwc2_layer_t * /*layers*/,
382 int32_t * /*layer_requests*/) {
383 // TODO(nobody): I think virtual display should request
384 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
385 *num_elements = 0;
386 return HWC2::Error::None;
387}
388
389HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
390 *type = static_cast<int32_t>(type_);
391 return HWC2::Error::None;
392}
393
394HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
395 *support = 0;
396 return HWC2::Error::None;
397}
398
399HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
400 int32_t * /*types*/,
401 float * /*max_luminance*/,
402 float * /*max_average_luminance*/,
403 float * /*min_luminance*/) {
404 *num_types = 0;
405 return HWC2::Error::None;
406}
407
408/* Find API details at:
409 * 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 +0300410 *
411 * Called after PresentDisplay(), CLIENT is expecting release fence for the
412 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200413 */
414HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
415 hwc2_layer_t *layers,
416 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200417 if (IsInHeadlessMode()) {
418 *num_elements = 0;
419 return HWC2::Error::None;
420 }
421
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200422 uint32_t num_layers = 0;
423
Roman Stratiienkodd214942022-05-03 18:24:49 +0300424 for (auto &l : layers_) {
425 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
426 continue;
427 }
428
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200429 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300430
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200431 if (layers == nullptr || fences == nullptr)
432 continue;
433
434 if (num_layers > *num_elements) {
435 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
436 return HWC2::Error::None;
437 }
438
439 layers[num_layers - 1] = l.first;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300440 fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200441 }
442 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300443
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444 return HWC2::Error::None;
445}
446
447HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200448 if (IsInHeadlessMode()) {
449 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
450 return HWC2::Error::None;
451 }
452
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200453 int PrevModeVsyncPeriodNs = static_cast<int>(
454 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
455
456 auto mode_update_commited_ = false;
457 if (staged_mode_ &&
458 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
459 client_layer_.SetLayerDisplayFrame(
460 (hwc_rect_t){.left = 0,
461 .top = 0,
462 .right = static_cast<int>(staged_mode_->h_display()),
463 .bottom = static_cast<int>(staged_mode_->v_display())});
464
465 configs_.active_config_id = staged_mode_config_id_;
466
467 a_args.display_mode = *staged_mode_;
468 if (!a_args.test_only) {
469 mode_update_commited_ = true;
470 }
471 }
472
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200473 // order the layers by z-order
474 bool use_client_layer = false;
475 uint32_t client_z_order = UINT32_MAX;
476 std::map<uint32_t, HwcLayer *> z_map;
477 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
478 switch (l.second.GetValidatedType()) {
479 case HWC2::Composition::Device:
480 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
481 break;
482 case HWC2::Composition::Client:
483 // Place it at the z_order of the lowest client layer
484 use_client_layer = true;
485 client_z_order = std::min(client_z_order, l.second.GetZOrder());
486 break;
487 default:
488 continue;
489 }
490 }
491 if (use_client_layer)
492 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
493
494 if (z_map.empty())
495 return HWC2::Error::BadLayer;
496
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200497 std::vector<LayerData> composition_layers;
498
499 /* Import & populate */
500 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
501 l.second->PopulateLayerData(a_args.test_only);
502 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200503
504 // now that they're ordered by z, add them to the composition
505 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200506 if (!l.second->IsLayerUsableAsDevice()) {
507 /* This will be normally triggered on validation of the first frame
508 * containing CLIENT layer. At this moment client buffer is not yet
509 * provided by the CLIENT.
510 * This may be triggered once in HwcLayer lifecycle in case FB can't be
511 * imported. For example when non-contiguous buffer is imported into
512 * contiguous-only DRM/KMS driver.
513 */
514 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200516 composition_layers.emplace_back(l.second->GetLayerData().Clone());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200517 }
518
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200519 /* Store plan to ensure shared planes won't be stolen by other display
520 * in between of ValidateDisplay() and PresentDisplay() calls
521 */
522 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
523 std::move(composition_layers));
524 if (!current_plan_) {
525 if (!a_args.test_only) {
526 ALOGE("Failed to create DrmKmsPlan");
527 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528 return HWC2::Error::BadConfig;
529 }
530
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200531 a_args.composition = current_plan_;
532
Roman Stratiienko4e994052022-02-09 17:40:35 +0200533 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200534
535 if (ret) {
536 if (!a_args.test_only)
537 ALOGE("Failed to apply the frame composition ret=%d", ret);
538 return HWC2::Error::BadParameter;
539 }
540
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200541 if (mode_update_commited_) {
542 staged_mode_.reset();
543 vsync_tracking_en_ = false;
544 if (last_vsync_ts_ != 0) {
545 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
546 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
547 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200548 }
549
550 return HWC2::Error::None;
551}
552
553/* Find API details at:
554 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
555 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300556HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200557 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300558 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200559 return HWC2::Error::None;
560 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200561 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200562
563 ++total_stats_.total_frames_;
564
565 AtomicCommitArgs a_args{};
566 ret = CreateComposition(a_args);
567
568 if (ret != HWC2::Error::None)
569 ++total_stats_.failed_kms_present_;
570
571 if (ret == HWC2::Error::BadLayer) {
572 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300573 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200574 return HWC2::Error::None;
575 }
576 if (ret != HWC2::Error::None)
577 return ret;
578
Roman Stratiienkodd214942022-05-03 18:24:49 +0300579 this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get());
580 *out_present_fence = a_args.out_fence.Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200581
582 ++frame_no_;
583 return HWC2::Error::None;
584}
585
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200586HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
587 int64_t change_time) {
588 if (configs_.hwc_configs.count(config) == 0) {
589 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200590 return HWC2::Error::BadConfig;
591 }
592
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200593 staged_mode_ = configs_.hwc_configs[config].mode;
594 staged_mode_change_time_ = change_time;
595 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200596
597 return HWC2::Error::None;
598}
599
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200600HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
601 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
602}
603
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200604/* Find API details at:
605 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
606 */
607HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
608 int32_t acquire_fence,
609 int32_t dataspace,
610 hwc_region_t /*damage*/) {
611 client_layer_.SetLayerBuffer(target, acquire_fence);
612 client_layer_.SetLayerDataspace(dataspace);
613
614 /*
615 * target can be nullptr, this does mean the Composer Service is calling
616 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
617 * 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
618 */
619 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300620 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200621 return HWC2::Error::None;
622 }
623
Roman Stratiienko5070d512022-05-30 13:41:20 +0300624 if (IsInHeadlessMode()) {
625 return HWC2::Error::None;
626 }
627
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200628 client_layer_.PopulateLayerData(/*test = */ true);
629 if (!client_layer_.IsLayerUsableAsDevice()) {
630 ALOGE("Client layer must be always usable by DRM/KMS");
631 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200632 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200633
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200634 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 hwc_frect_t source_crop = {.left = 0.0F,
636 .top = 0.0F,
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200637 .right = static_cast<float>(bi->width),
638 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200639 client_layer_.SetLayerSourceCrop(source_crop);
640
641 return HWC2::Error::None;
642}
643
644HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
645 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
646 return HWC2::Error::BadParameter;
647
648 if (mode != HAL_COLOR_MODE_NATIVE)
649 return HWC2::Error::Unsupported;
650
651 color_mode_ = mode;
652 return HWC2::Error::None;
653}
654
655HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
656 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
657 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
658 return HWC2::Error::BadParameter;
659
660 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
661 return HWC2::Error::BadParameter;
662
663 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
664 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
665 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
666
667 return HWC2::Error::None;
668}
669
670HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
671 int32_t /*release_fence*/) {
672 // TODO(nobody): Need virtual display support
673 return HWC2::Error::Unsupported;
674}
675
676HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
677 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300678
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200679 AtomicCommitArgs a_args{};
680
681 switch (mode) {
682 case HWC2::PowerMode::Off:
683 a_args.active = false;
684 break;
685 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300686 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200687 break;
688 case HWC2::PowerMode::Doze:
689 case HWC2::PowerMode::DozeSuspend:
690 return HWC2::Error::Unsupported;
691 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300692 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200693 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300694 }
695
696 if (IsInHeadlessMode()) {
697 return HWC2::Error::None;
698 }
699
700 if (a_args.active) {
701 /*
702 * Setting the display to active before we have a composition
703 * can break some drivers, so skip setting a_args.active to
704 * true, as the next composition frame will implicitly activate
705 * the display
706 */
707 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
708 ? HWC2::Error::None
709 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200710 };
711
Roman Stratiienko4e994052022-02-09 17:40:35 +0200712 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200713 if (err) {
714 ALOGE("Failed to apply the dpms composition err=%d", err);
715 return HWC2::Error::BadParameter;
716 }
717 return HWC2::Error::None;
718}
719
720HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200721 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
722 if (vsync_event_en_) {
723 vsync_worker_.VSyncControl(true);
724 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200725 return HWC2::Error::None;
726}
727
728HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
729 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200730 if (IsInHeadlessMode()) {
731 *num_types = *num_requests = 0;
732 return HWC2::Error::None;
733 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300734
735 /* In current drm_hwc design in case previous frame layer was not validated as
736 * a CLIENT, it is used by display controller (Front buffer). We have to store
737 * this state to provide the CLIENT with the release fences for such buffers.
738 */
739 for (auto &l : layers_) {
740 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
741 HWC2::Composition::Client);
742 }
743
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200744 return backend_->ValidateDisplay(this, num_types, num_requests);
745}
746
747std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
748 std::vector<HwcLayer *> ordered_layers;
749 ordered_layers.reserve(layers_.size());
750
751 for (auto &[handle, layer] : layers_) {
752 ordered_layers.emplace_back(&layer);
753 }
754
755 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
756 [](const HwcLayer *lhs, const HwcLayer *rhs) {
757 return lhs->GetZOrder() < rhs->GetZOrder();
758 });
759
760 return ordered_layers;
761}
762
Roman Stratiienko099c3112022-01-20 11:50:54 +0200763HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
764 uint32_t *outVsyncPeriod /* ns */) {
765 return GetDisplayAttribute(configs_.active_config_id,
766 HWC2_ATTRIBUTE_VSYNC_PERIOD,
767 (int32_t *)(outVsyncPeriod));
768}
769
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200770#if PLATFORM_SDK_VERSION > 29
771HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200772 if (IsInHeadlessMode()) {
773 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
774 return HWC2::Error::None;
775 }
776 /* Primary display should be always internal,
777 * otherwise SF will be unhappy and will crash
778 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200779 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200780 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200781 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200782 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
783 else
784 return HWC2::Error::BadConfig;
785
786 return HWC2::Error::None;
787}
788
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200789HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200790 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200791 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
792 hwc_vsync_period_change_timeline_t *outTimeline) {
793 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
794 return HWC2::Error::BadParameter;
795 }
796
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200797 uint32_t current_vsync_period{};
798 GetDisplayVsyncPeriod(&current_vsync_period);
799
800 if (vsyncPeriodChangeConstraints->seamlessRequired) {
801 return HWC2::Error::SeamlessNotAllowed;
802 }
803
804 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
805 ->desiredTimeNanos -
806 current_vsync_period;
807 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
808 if (ret != HWC2::Error::None) {
809 return ret;
810 }
811
812 outTimeline->refreshRequired = true;
813 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
814 ->desiredTimeNanos;
815
816 last_vsync_ts_ = 0;
817 vsync_tracking_en_ = true;
818 vsync_worker_.VSyncControl(true);
819
820 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200821}
822
823HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
824 return HWC2::Error::Unsupported;
825}
826
827HWC2::Error HwcDisplay::GetSupportedContentTypes(
828 uint32_t *outNumSupportedContentTypes,
829 const uint32_t *outSupportedContentTypes) {
830 if (outSupportedContentTypes == nullptr)
831 *outNumSupportedContentTypes = 0;
832
833 return HWC2::Error::None;
834}
835
836HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
837 if (contentType != HWC2_CONTENT_TYPE_NONE)
838 return HWC2::Error::Unsupported;
839
840 /* TODO: Map to the DRM Connector property:
841 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
842 */
843
844 return HWC2::Error::None;
845}
846#endif
847
848#if PLATFORM_SDK_VERSION > 28
849HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
850 uint32_t *outDataSize,
851 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200852 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300853 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200854 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300855
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200856 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200857 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300858 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200859 }
860
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300861 *outPort = handle_; /* TDOD(nobody): What should be here? */
862
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200863 if (outData) {
864 *outDataSize = std::min(*outDataSize, blob->length);
865 memcpy(outData, blob->data, *outDataSize);
866 } else {
867 *outDataSize = blob->length;
868 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200869
870 return HWC2::Error::None;
871}
872
873HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
874 uint32_t * /*outCapabilities*/) {
875 if (outNumCapabilities == nullptr) {
876 return HWC2::Error::BadParameter;
877 }
878
879 *outNumCapabilities = 0;
880
881 return HWC2::Error::None;
882}
883
884HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
885 *supported = false;
886 return HWC2::Error::None;
887}
888
889HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
890 return HWC2::Error::Unsupported;
891}
892
893#endif /* PLATFORM_SDK_VERSION > 28 */
894
895#if PLATFORM_SDK_VERSION > 27
896
897HWC2::Error HwcDisplay::GetRenderIntents(
898 int32_t mode, uint32_t *outNumIntents,
899 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
900 if (mode != HAL_COLOR_MODE_NATIVE) {
901 return HWC2::Error::BadParameter;
902 }
903
904 if (outIntents == nullptr) {
905 *outNumIntents = 1;
906 return HWC2::Error::None;
907 }
908 *outNumIntents = 1;
909 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
910 return HWC2::Error::None;
911}
912
913HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
914 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
915 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
916 return HWC2::Error::BadParameter;
917
918 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
919 return HWC2::Error::BadParameter;
920
921 if (mode != HAL_COLOR_MODE_NATIVE)
922 return HWC2::Error::Unsupported;
923
924 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
925 return HWC2::Error::Unsupported;
926
927 color_mode_ = mode;
928 return HWC2::Error::None;
929}
930
931#endif /* PLATFORM_SDK_VERSION > 27 */
932
933const Backend *HwcDisplay::backend() const {
934 return backend_.get();
935}
936
937void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
938 backend_ = std::move(backend);
939}
940
Roman Stratiienko099c3112022-01-20 11:50:54 +0200941/* returns true if composition should be sent to client */
942bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
943 int flattenning_state = flattenning_state_;
944 if (flattenning_state == ClientFlattenningState::Disabled) {
945 return false;
946 }
947
948 if (skip) {
949 flattenning_state_ = ClientFlattenningState::NotRequired;
950 return false;
951 }
952
953 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
954 flattenning_state_ = ClientFlattenningState::Flattened;
955 return true;
956 }
957
958 vsync_flattening_en_ = true;
959 vsync_worker_.VSyncControl(true);
960 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
961 return false;
962}
963
964void HwcDisplay::ProcessFlatenningVsyncInternal() {
965 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
966 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
967 hwc2_->refresh_callback_.first != nullptr &&
968 hwc2_->refresh_callback_.second != nullptr) {
969 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
970 vsync_flattening_en_ = false;
971 }
972}
973
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200974} // namespace android