blob: 76e2745aaa444214c7df739329f194363068ae9c [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{};
121 a_args.active = false;
122 a_args.composition = std::make_shared<DrmKmsPlan>();
123 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
124
125 vsync_worker_.Init(nullptr, [](int64_t) {});
126 current_plan_.reset();
127 backend_.reset();
128 }
129
130 SetClientTarget(nullptr, -1, 0, {});
131}
132
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200133HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200134 ChosePreferredConfig();
135
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200136 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200137 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200138 if (vsync_event_en_) {
139 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200141 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200142 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200143 if (vsync_flattening_en_) {
144 ProcessFlatenningVsyncInternal();
145 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200146 if (vsync_tracking_en_) {
147 last_vsync_ts_ = timestamp;
148 }
149 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200150 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200151 }
152 });
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200153 if (ret && ret != -EALREADY) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200154 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200155 return HWC2::Error::BadDisplay;
156 }
157
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200158 if (!IsInHeadlessMode()) {
159 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
160 if (ret) {
161 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
162 return HWC2::Error::BadDisplay;
163 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200164 }
165
166 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
167
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200168 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200169}
170
171HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200172 HWC2::Error err{};
173 if (!IsInHeadlessMode()) {
174 err = configs_.Update(*pipeline_->connector->Get());
175 } else {
176 configs_.FillHeadless();
177 }
178 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200179 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200180 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200181
Roman Stratiienko0137f862022-01-04 18:27:40 +0200182 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200183}
184
185HWC2::Error HwcDisplay::AcceptDisplayChanges() {
186 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
187 l.second.AcceptTypeChange();
188 return HWC2::Error::None;
189}
190
191HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200192 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200193 *layer = static_cast<hwc2_layer_t>(layer_idx_);
194 ++layer_idx_;
195 return HWC2::Error::None;
196}
197
198HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200199 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200200 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200201 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202
203 layers_.erase(layer);
204 return HWC2::Error::None;
205}
206
207HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200208 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209 return HWC2::Error::BadConfig;
210
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200211 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200212 return HWC2::Error::None;
213}
214
215HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
216 hwc2_layer_t *layers,
217 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200218 if (IsInHeadlessMode()) {
219 *num_elements = 0;
220 return HWC2::Error::None;
221 }
222
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223 uint32_t num_changes = 0;
224 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
225 if (l.second.IsTypeChanged()) {
226 if (layers && num_changes < *num_elements)
227 layers[num_changes] = l.first;
228 if (types && num_changes < *num_elements)
229 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
230 ++num_changes;
231 }
232 }
233 if (!layers && !types)
234 *num_elements = num_changes;
235 return HWC2::Error::None;
236}
237
238HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
239 int32_t /*format*/,
240 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200241 if (IsInHeadlessMode()) {
242 return HWC2::Error::None;
243 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200244
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200245 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
246 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
247
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200248 if (width < min.first || height < min.second)
249 return HWC2::Error::Unsupported;
250
251 if (width > max.first || height > max.second)
252 return HWC2::Error::Unsupported;
253
254 if (dataspace != HAL_DATASPACE_UNKNOWN)
255 return HWC2::Error::Unsupported;
256
257 // TODO(nobody): Validate format can be handled by either GL or planes
258 return HWC2::Error::None;
259}
260
261HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
262 if (!modes)
263 *num_modes = 1;
264
265 if (modes)
266 *modes = HAL_COLOR_MODE_NATIVE;
267
268 return HWC2::Error::None;
269}
270
271HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
272 int32_t attribute_in,
273 int32_t *value) {
274 int conf = static_cast<int>(config);
275
Roman Stratiienko0137f862022-01-04 18:27:40 +0200276 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200277 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200278 return HWC2::Error::BadConfig;
279 }
280
Roman Stratiienko0137f862022-01-04 18:27:40 +0200281 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200282
283 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200284 uint32_t mm_width = configs_.mm_width;
285 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200286 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
287 switch (attribute) {
288 case HWC2::Attribute::Width:
289 *value = static_cast<int>(hwc_config.mode.h_display());
290 break;
291 case HWC2::Attribute::Height:
292 *value = static_cast<int>(hwc_config.mode.v_display());
293 break;
294 case HWC2::Attribute::VsyncPeriod:
295 // in nanoseconds
296 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
297 break;
298 case HWC2::Attribute::DpiX:
299 // Dots per 1000 inches
300 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
301 kUmPerInch / mm_width)
302 : -1;
303 break;
304 case HWC2::Attribute::DpiY:
305 // Dots per 1000 inches
306 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
307 kUmPerInch / mm_height)
308 : -1;
309 break;
310#if PLATFORM_SDK_VERSION > 29
311 case HWC2::Attribute::ConfigGroup:
312 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
313 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200314 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315 break;
316#endif
317 default:
318 *value = -1;
319 return HWC2::Error::BadConfig;
320 }
321 return HWC2::Error::None;
322}
323
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200324HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
325 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200326 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200327 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 if (hwc_config.second.disabled) {
329 continue;
330 }
331
332 if (configs != nullptr) {
333 if (idx >= *num_configs) {
334 break;
335 }
336 configs[idx] = hwc_config.second.id;
337 }
338
339 idx++;
340 }
341 *num_configs = idx;
342 return HWC2::Error::None;
343}
344
345HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
346 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200347 if (IsInHeadlessMode()) {
348 stream << "null-display";
349 } else {
350 stream << "display-" << GetPipe().connector->Get()->GetId();
351 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200352 std::string string = stream.str();
353 size_t length = string.length();
354 if (!name) {
355 *size = length;
356 return HWC2::Error::None;
357 }
358
359 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
360 strncpy(name, string.c_str(), *size);
361 return HWC2::Error::None;
362}
363
364HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
365 uint32_t *num_elements,
366 hwc2_layer_t * /*layers*/,
367 int32_t * /*layer_requests*/) {
368 // TODO(nobody): I think virtual display should request
369 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
370 *num_elements = 0;
371 return HWC2::Error::None;
372}
373
374HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
375 *type = static_cast<int32_t>(type_);
376 return HWC2::Error::None;
377}
378
379HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
380 *support = 0;
381 return HWC2::Error::None;
382}
383
384HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
385 int32_t * /*types*/,
386 float * /*max_luminance*/,
387 float * /*max_average_luminance*/,
388 float * /*min_luminance*/) {
389 *num_types = 0;
390 return HWC2::Error::None;
391}
392
393/* Find API details at:
394 * 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 +0300395 *
396 * Called after PresentDisplay(), CLIENT is expecting release fence for the
397 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200398 */
399HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
400 hwc2_layer_t *layers,
401 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200402 if (IsInHeadlessMode()) {
403 *num_elements = 0;
404 return HWC2::Error::None;
405 }
406
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200407 uint32_t num_layers = 0;
408
Roman Stratiienkodd214942022-05-03 18:24:49 +0300409 for (auto &l : layers_) {
410 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
411 continue;
412 }
413
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200414 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300415
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200416 if (layers == nullptr || fences == nullptr)
417 continue;
418
419 if (num_layers > *num_elements) {
420 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
421 return HWC2::Error::None;
422 }
423
424 layers[num_layers - 1] = l.first;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300425 fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200426 }
427 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300428
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200429 return HWC2::Error::None;
430}
431
432HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200433 if (IsInHeadlessMode()) {
434 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
435 return HWC2::Error::None;
436 }
437
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200438 int PrevModeVsyncPeriodNs = static_cast<int>(
439 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
440
441 auto mode_update_commited_ = false;
442 if (staged_mode_ &&
443 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
444 client_layer_.SetLayerDisplayFrame(
445 (hwc_rect_t){.left = 0,
446 .top = 0,
447 .right = static_cast<int>(staged_mode_->h_display()),
448 .bottom = static_cast<int>(staged_mode_->v_display())});
449
450 configs_.active_config_id = staged_mode_config_id_;
451
452 a_args.display_mode = *staged_mode_;
453 if (!a_args.test_only) {
454 mode_update_commited_ = true;
455 }
456 }
457
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200458 // order the layers by z-order
459 bool use_client_layer = false;
460 uint32_t client_z_order = UINT32_MAX;
461 std::map<uint32_t, HwcLayer *> z_map;
462 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
463 switch (l.second.GetValidatedType()) {
464 case HWC2::Composition::Device:
465 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
466 break;
467 case HWC2::Composition::Client:
468 // Place it at the z_order of the lowest client layer
469 use_client_layer = true;
470 client_z_order = std::min(client_z_order, l.second.GetZOrder());
471 break;
472 default:
473 continue;
474 }
475 }
476 if (use_client_layer)
477 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
478
479 if (z_map.empty())
480 return HWC2::Error::BadLayer;
481
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200482 std::vector<LayerData> composition_layers;
483
484 /* Import & populate */
485 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
486 l.second->PopulateLayerData(a_args.test_only);
487 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488
489 // now that they're ordered by z, add them to the composition
490 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200491 if (!l.second->IsLayerUsableAsDevice()) {
492 /* This will be normally triggered on validation of the first frame
493 * containing CLIENT layer. At this moment client buffer is not yet
494 * provided by the CLIENT.
495 * This may be triggered once in HwcLayer lifecycle in case FB can't be
496 * imported. For example when non-contiguous buffer is imported into
497 * contiguous-only DRM/KMS driver.
498 */
499 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200501 composition_layers.emplace_back(l.second->GetLayerData().Clone());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502 }
503
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200504 /* Store plan to ensure shared planes won't be stolen by other display
505 * in between of ValidateDisplay() and PresentDisplay() calls
506 */
507 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
508 std::move(composition_layers));
509 if (!current_plan_) {
510 if (!a_args.test_only) {
511 ALOGE("Failed to create DrmKmsPlan");
512 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200513 return HWC2::Error::BadConfig;
514 }
515
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200516 a_args.composition = current_plan_;
517
Roman Stratiienko4e994052022-02-09 17:40:35 +0200518 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200519
520 if (ret) {
521 if (!a_args.test_only)
522 ALOGE("Failed to apply the frame composition ret=%d", ret);
523 return HWC2::Error::BadParameter;
524 }
525
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200526 if (mode_update_commited_) {
527 staged_mode_.reset();
528 vsync_tracking_en_ = false;
529 if (last_vsync_ts_ != 0) {
530 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
531 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
532 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533 }
534
535 return HWC2::Error::None;
536}
537
538/* Find API details at:
539 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
540 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300541HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200542 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300543 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200544 return HWC2::Error::None;
545 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200546 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200547
548 ++total_stats_.total_frames_;
549
550 AtomicCommitArgs a_args{};
551 ret = CreateComposition(a_args);
552
553 if (ret != HWC2::Error::None)
554 ++total_stats_.failed_kms_present_;
555
556 if (ret == HWC2::Error::BadLayer) {
557 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300558 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200559 return HWC2::Error::None;
560 }
561 if (ret != HWC2::Error::None)
562 return ret;
563
Roman Stratiienkodd214942022-05-03 18:24:49 +0300564 this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get());
565 *out_present_fence = a_args.out_fence.Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566
567 ++frame_no_;
568 return HWC2::Error::None;
569}
570
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200571HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
572 int64_t change_time) {
573 if (configs_.hwc_configs.count(config) == 0) {
574 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200575 return HWC2::Error::BadConfig;
576 }
577
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200578 staged_mode_ = configs_.hwc_configs[config].mode;
579 staged_mode_change_time_ = change_time;
580 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200581
582 return HWC2::Error::None;
583}
584
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200585HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
586 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
587}
588
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200589/* Find API details at:
590 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
591 */
592HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
593 int32_t acquire_fence,
594 int32_t dataspace,
595 hwc_region_t /*damage*/) {
596 client_layer_.SetLayerBuffer(target, acquire_fence);
597 client_layer_.SetLayerDataspace(dataspace);
598
599 /*
600 * target can be nullptr, this does mean the Composer Service is calling
601 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
602 * 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
603 */
604 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300605 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606 return HWC2::Error::None;
607 }
608
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200609 client_layer_.PopulateLayerData(/*test = */ true);
610 if (!client_layer_.IsLayerUsableAsDevice()) {
611 ALOGE("Client layer must be always usable by DRM/KMS");
612 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200613 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200614
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200615 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200616 hwc_frect_t source_crop = {.left = 0.0F,
617 .top = 0.0F,
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200618 .right = static_cast<float>(bi->width),
619 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620 client_layer_.SetLayerSourceCrop(source_crop);
621
622 return HWC2::Error::None;
623}
624
625HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
626 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
627 return HWC2::Error::BadParameter;
628
629 if (mode != HAL_COLOR_MODE_NATIVE)
630 return HWC2::Error::Unsupported;
631
632 color_mode_ = mode;
633 return HWC2::Error::None;
634}
635
636HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
637 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
638 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
639 return HWC2::Error::BadParameter;
640
641 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
642 return HWC2::Error::BadParameter;
643
644 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
645 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
646 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
647
648 return HWC2::Error::None;
649}
650
651HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
652 int32_t /*release_fence*/) {
653 // TODO(nobody): Need virtual display support
654 return HWC2::Error::Unsupported;
655}
656
657HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
658 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300659
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200660 AtomicCommitArgs a_args{};
661
662 switch (mode) {
663 case HWC2::PowerMode::Off:
664 a_args.active = false;
665 break;
666 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300667 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200668 break;
669 case HWC2::PowerMode::Doze:
670 case HWC2::PowerMode::DozeSuspend:
671 return HWC2::Error::Unsupported;
672 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300673 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200674 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300675 }
676
677 if (IsInHeadlessMode()) {
678 return HWC2::Error::None;
679 }
680
681 if (a_args.active) {
682 /*
683 * Setting the display to active before we have a composition
684 * can break some drivers, so skip setting a_args.active to
685 * true, as the next composition frame will implicitly activate
686 * the display
687 */
688 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
689 ? HWC2::Error::None
690 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200691 };
692
Roman Stratiienko4e994052022-02-09 17:40:35 +0200693 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200694 if (err) {
695 ALOGE("Failed to apply the dpms composition err=%d", err);
696 return HWC2::Error::BadParameter;
697 }
698 return HWC2::Error::None;
699}
700
701HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200702 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
703 if (vsync_event_en_) {
704 vsync_worker_.VSyncControl(true);
705 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200706 return HWC2::Error::None;
707}
708
709HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
710 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200711 if (IsInHeadlessMode()) {
712 *num_types = *num_requests = 0;
713 return HWC2::Error::None;
714 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300715
716 /* In current drm_hwc design in case previous frame layer was not validated as
717 * a CLIENT, it is used by display controller (Front buffer). We have to store
718 * this state to provide the CLIENT with the release fences for such buffers.
719 */
720 for (auto &l : layers_) {
721 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
722 HWC2::Composition::Client);
723 }
724
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200725 return backend_->ValidateDisplay(this, num_types, num_requests);
726}
727
728std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
729 std::vector<HwcLayer *> ordered_layers;
730 ordered_layers.reserve(layers_.size());
731
732 for (auto &[handle, layer] : layers_) {
733 ordered_layers.emplace_back(&layer);
734 }
735
736 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
737 [](const HwcLayer *lhs, const HwcLayer *rhs) {
738 return lhs->GetZOrder() < rhs->GetZOrder();
739 });
740
741 return ordered_layers;
742}
743
Roman Stratiienko099c3112022-01-20 11:50:54 +0200744HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
745 uint32_t *outVsyncPeriod /* ns */) {
746 return GetDisplayAttribute(configs_.active_config_id,
747 HWC2_ATTRIBUTE_VSYNC_PERIOD,
748 (int32_t *)(outVsyncPeriod));
749}
750
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200751#if PLATFORM_SDK_VERSION > 29
752HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200753 if (IsInHeadlessMode()) {
754 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
755 return HWC2::Error::None;
756 }
757 /* Primary display should be always internal,
758 * otherwise SF will be unhappy and will crash
759 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200760 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200761 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200762 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200763 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
764 else
765 return HWC2::Error::BadConfig;
766
767 return HWC2::Error::None;
768}
769
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200770HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200771 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200772 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
773 hwc_vsync_period_change_timeline_t *outTimeline) {
774 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
775 return HWC2::Error::BadParameter;
776 }
777
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200778 uint32_t current_vsync_period{};
779 GetDisplayVsyncPeriod(&current_vsync_period);
780
781 if (vsyncPeriodChangeConstraints->seamlessRequired) {
782 return HWC2::Error::SeamlessNotAllowed;
783 }
784
785 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
786 ->desiredTimeNanos -
787 current_vsync_period;
788 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
789 if (ret != HWC2::Error::None) {
790 return ret;
791 }
792
793 outTimeline->refreshRequired = true;
794 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
795 ->desiredTimeNanos;
796
797 last_vsync_ts_ = 0;
798 vsync_tracking_en_ = true;
799 vsync_worker_.VSyncControl(true);
800
801 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200802}
803
804HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
805 return HWC2::Error::Unsupported;
806}
807
808HWC2::Error HwcDisplay::GetSupportedContentTypes(
809 uint32_t *outNumSupportedContentTypes,
810 const uint32_t *outSupportedContentTypes) {
811 if (outSupportedContentTypes == nullptr)
812 *outNumSupportedContentTypes = 0;
813
814 return HWC2::Error::None;
815}
816
817HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
818 if (contentType != HWC2_CONTENT_TYPE_NONE)
819 return HWC2::Error::Unsupported;
820
821 /* TODO: Map to the DRM Connector property:
822 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
823 */
824
825 return HWC2::Error::None;
826}
827#endif
828
829#if PLATFORM_SDK_VERSION > 28
830HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
831 uint32_t *outDataSize,
832 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200833 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300834 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200835 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300836
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200837 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300839 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200840 }
841
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300842 *outPort = handle_; /* TDOD(nobody): What should be here? */
843
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200844 if (outData) {
845 *outDataSize = std::min(*outDataSize, blob->length);
846 memcpy(outData, blob->data, *outDataSize);
847 } else {
848 *outDataSize = blob->length;
849 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200850
851 return HWC2::Error::None;
852}
853
854HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
855 uint32_t * /*outCapabilities*/) {
856 if (outNumCapabilities == nullptr) {
857 return HWC2::Error::BadParameter;
858 }
859
860 *outNumCapabilities = 0;
861
862 return HWC2::Error::None;
863}
864
865HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
866 *supported = false;
867 return HWC2::Error::None;
868}
869
870HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
871 return HWC2::Error::Unsupported;
872}
873
874#endif /* PLATFORM_SDK_VERSION > 28 */
875
876#if PLATFORM_SDK_VERSION > 27
877
878HWC2::Error HwcDisplay::GetRenderIntents(
879 int32_t mode, uint32_t *outNumIntents,
880 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
881 if (mode != HAL_COLOR_MODE_NATIVE) {
882 return HWC2::Error::BadParameter;
883 }
884
885 if (outIntents == nullptr) {
886 *outNumIntents = 1;
887 return HWC2::Error::None;
888 }
889 *outNumIntents = 1;
890 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
891 return HWC2::Error::None;
892}
893
894HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
895 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
896 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
897 return HWC2::Error::BadParameter;
898
899 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
900 return HWC2::Error::BadParameter;
901
902 if (mode != HAL_COLOR_MODE_NATIVE)
903 return HWC2::Error::Unsupported;
904
905 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
906 return HWC2::Error::Unsupported;
907
908 color_mode_ = mode;
909 return HWC2::Error::None;
910}
911
912#endif /* PLATFORM_SDK_VERSION > 27 */
913
914const Backend *HwcDisplay::backend() const {
915 return backend_.get();
916}
917
918void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
919 backend_ = std::move(backend);
920}
921
Roman Stratiienko099c3112022-01-20 11:50:54 +0200922/* returns true if composition should be sent to client */
923bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
924 int flattenning_state = flattenning_state_;
925 if (flattenning_state == ClientFlattenningState::Disabled) {
926 return false;
927 }
928
929 if (skip) {
930 flattenning_state_ = ClientFlattenningState::NotRequired;
931 return false;
932 }
933
934 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
935 flattenning_state_ = ClientFlattenningState::Flattened;
936 return true;
937 }
938
939 vsync_flattening_en_ = true;
940 vsync_worker_.VSyncControl(true);
941 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
942 return false;
943}
944
945void HwcDisplay::ProcessFlatenningVsyncInternal() {
946 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
947 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
948 hwc2_->refresh_callback_.first != nullptr &&
949 hwc2_->refresh_callback_.second != nullptr) {
950 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
951 vsync_flattening_en_ = false;
952 }
953}
954
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200955} // namespace android