blob: 58b888ef29323dccbf445e82e223887373de84d2 [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);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300123 a_args.composition = {};
124 a_args.active = false;
125 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200126
127 vsync_worker_.Init(nullptr, [](int64_t) {});
128 current_plan_.reset();
129 backend_.reset();
130 }
131
132 SetClientTarget(nullptr, -1, 0, {});
133}
134
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200136 ChosePreferredConfig();
137
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200138 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200139 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200140 if (vsync_event_en_) {
141 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200142 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200143 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200144 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200145 if (vsync_flattening_en_) {
146 ProcessFlatenningVsyncInternal();
147 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200148 if (vsync_tracking_en_) {
149 last_vsync_ts_ = timestamp;
150 }
151 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200152 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200153 }
154 });
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200155 if (ret && ret != -EALREADY) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200156 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200157 return HWC2::Error::BadDisplay;
158 }
159
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200160 if (!IsInHeadlessMode()) {
161 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
162 if (ret) {
163 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
164 return HWC2::Error::BadDisplay;
165 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200166 }
167
168 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
169
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200170 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171}
172
173HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200174 HWC2::Error err{};
175 if (!IsInHeadlessMode()) {
176 err = configs_.Update(*pipeline_->connector->Get());
177 } else {
178 configs_.FillHeadless();
179 }
180 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200181 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200182 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200183
Roman Stratiienko0137f862022-01-04 18:27:40 +0200184 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185}
186
187HWC2::Error HwcDisplay::AcceptDisplayChanges() {
188 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
189 l.second.AcceptTypeChange();
190 return HWC2::Error::None;
191}
192
193HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200194 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195 *layer = static_cast<hwc2_layer_t>(layer_idx_);
196 ++layer_idx_;
197 return HWC2::Error::None;
198}
199
200HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200201 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200203 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200204
205 layers_.erase(layer);
206 return HWC2::Error::None;
207}
208
209HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200210 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 return HWC2::Error::BadConfig;
212
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200213 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200214 return HWC2::Error::None;
215}
216
217HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
218 hwc2_layer_t *layers,
219 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200220 if (IsInHeadlessMode()) {
221 *num_elements = 0;
222 return HWC2::Error::None;
223 }
224
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200225 uint32_t num_changes = 0;
226 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
227 if (l.second.IsTypeChanged()) {
228 if (layers && num_changes < *num_elements)
229 layers[num_changes] = l.first;
230 if (types && num_changes < *num_elements)
231 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
232 ++num_changes;
233 }
234 }
235 if (!layers && !types)
236 *num_elements = num_changes;
237 return HWC2::Error::None;
238}
239
240HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
241 int32_t /*format*/,
242 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200243 if (IsInHeadlessMode()) {
244 return HWC2::Error::None;
245 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200246
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200247 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
248 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
249
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200250 if (width < min.first || height < min.second)
251 return HWC2::Error::Unsupported;
252
253 if (width > max.first || height > max.second)
254 return HWC2::Error::Unsupported;
255
256 if (dataspace != HAL_DATASPACE_UNKNOWN)
257 return HWC2::Error::Unsupported;
258
259 // TODO(nobody): Validate format can be handled by either GL or planes
260 return HWC2::Error::None;
261}
262
263HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
264 if (!modes)
265 *num_modes = 1;
266
267 if (modes)
268 *modes = HAL_COLOR_MODE_NATIVE;
269
270 return HWC2::Error::None;
271}
272
273HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
274 int32_t attribute_in,
275 int32_t *value) {
276 int conf = static_cast<int>(config);
277
Roman Stratiienko0137f862022-01-04 18:27:40 +0200278 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200279 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200280 return HWC2::Error::BadConfig;
281 }
282
Roman Stratiienko0137f862022-01-04 18:27:40 +0200283 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200284
285 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200286 uint32_t mm_width = configs_.mm_width;
287 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200288 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
289 switch (attribute) {
290 case HWC2::Attribute::Width:
291 *value = static_cast<int>(hwc_config.mode.h_display());
292 break;
293 case HWC2::Attribute::Height:
294 *value = static_cast<int>(hwc_config.mode.v_display());
295 break;
296 case HWC2::Attribute::VsyncPeriod:
297 // in nanoseconds
298 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
299 break;
300 case HWC2::Attribute::DpiX:
301 // Dots per 1000 inches
302 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
303 kUmPerInch / mm_width)
304 : -1;
305 break;
306 case HWC2::Attribute::DpiY:
307 // Dots per 1000 inches
308 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
309 kUmPerInch / mm_height)
310 : -1;
311 break;
312#if PLATFORM_SDK_VERSION > 29
313 case HWC2::Attribute::ConfigGroup:
314 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
315 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200316 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200317 break;
318#endif
319 default:
320 *value = -1;
321 return HWC2::Error::BadConfig;
322 }
323 return HWC2::Error::None;
324}
325
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200326HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
327 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200329 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330 if (hwc_config.second.disabled) {
331 continue;
332 }
333
334 if (configs != nullptr) {
335 if (idx >= *num_configs) {
336 break;
337 }
338 configs[idx] = hwc_config.second.id;
339 }
340
341 idx++;
342 }
343 *num_configs = idx;
344 return HWC2::Error::None;
345}
346
347HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
348 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200349 if (IsInHeadlessMode()) {
350 stream << "null-display";
351 } else {
352 stream << "display-" << GetPipe().connector->Get()->GetId();
353 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200354 std::string string = stream.str();
355 size_t length = string.length();
356 if (!name) {
357 *size = length;
358 return HWC2::Error::None;
359 }
360
361 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
362 strncpy(name, string.c_str(), *size);
363 return HWC2::Error::None;
364}
365
366HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
367 uint32_t *num_elements,
368 hwc2_layer_t * /*layers*/,
369 int32_t * /*layer_requests*/) {
370 // TODO(nobody): I think virtual display should request
371 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
372 *num_elements = 0;
373 return HWC2::Error::None;
374}
375
376HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
377 *type = static_cast<int32_t>(type_);
378 return HWC2::Error::None;
379}
380
381HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
382 *support = 0;
383 return HWC2::Error::None;
384}
385
386HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
387 int32_t * /*types*/,
388 float * /*max_luminance*/,
389 float * /*max_average_luminance*/,
390 float * /*min_luminance*/) {
391 *num_types = 0;
392 return HWC2::Error::None;
393}
394
395/* Find API details at:
396 * 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 +0300397 *
398 * Called after PresentDisplay(), CLIENT is expecting release fence for the
399 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200400 */
401HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
402 hwc2_layer_t *layers,
403 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200404 if (IsInHeadlessMode()) {
405 *num_elements = 0;
406 return HWC2::Error::None;
407 }
408
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200409 uint32_t num_layers = 0;
410
Roman Stratiienkodd214942022-05-03 18:24:49 +0300411 for (auto &l : layers_) {
412 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
413 continue;
414 }
415
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200416 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300417
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200418 if (layers == nullptr || fences == nullptr)
419 continue;
420
421 if (num_layers > *num_elements) {
422 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
423 return HWC2::Error::None;
424 }
425
426 layers[num_layers - 1] = l.first;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300427 fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200428 }
429 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300430
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200431 return HWC2::Error::None;
432}
433
434HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200435 if (IsInHeadlessMode()) {
436 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
437 return HWC2::Error::None;
438 }
439
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200440 int PrevModeVsyncPeriodNs = static_cast<int>(
441 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
442
443 auto mode_update_commited_ = false;
444 if (staged_mode_ &&
445 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
446 client_layer_.SetLayerDisplayFrame(
447 (hwc_rect_t){.left = 0,
448 .top = 0,
449 .right = static_cast<int>(staged_mode_->h_display()),
450 .bottom = static_cast<int>(staged_mode_->v_display())});
451
452 configs_.active_config_id = staged_mode_config_id_;
453
454 a_args.display_mode = *staged_mode_;
455 if (!a_args.test_only) {
456 mode_update_commited_ = true;
457 }
458 }
459
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200460 // order the layers by z-order
461 bool use_client_layer = false;
462 uint32_t client_z_order = UINT32_MAX;
463 std::map<uint32_t, HwcLayer *> z_map;
464 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
465 switch (l.second.GetValidatedType()) {
466 case HWC2::Composition::Device:
467 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
468 break;
469 case HWC2::Composition::Client:
470 // Place it at the z_order of the lowest client layer
471 use_client_layer = true;
472 client_z_order = std::min(client_z_order, l.second.GetZOrder());
473 break;
474 default:
475 continue;
476 }
477 }
478 if (use_client_layer)
479 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
480
481 if (z_map.empty())
482 return HWC2::Error::BadLayer;
483
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200484 std::vector<LayerData> composition_layers;
485
486 /* Import & populate */
487 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
488 l.second->PopulateLayerData(a_args.test_only);
489 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490
491 // now that they're ordered by z, add them to the composition
492 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200493 if (!l.second->IsLayerUsableAsDevice()) {
494 /* This will be normally triggered on validation of the first frame
495 * containing CLIENT layer. At this moment client buffer is not yet
496 * provided by the CLIENT.
497 * This may be triggered once in HwcLayer lifecycle in case FB can't be
498 * imported. For example when non-contiguous buffer is imported into
499 * contiguous-only DRM/KMS driver.
500 */
501 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200503 composition_layers.emplace_back(l.second->GetLayerData().Clone());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504 }
505
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200506 /* Store plan to ensure shared planes won't be stolen by other display
507 * in between of ValidateDisplay() and PresentDisplay() calls
508 */
509 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
510 std::move(composition_layers));
511 if (!current_plan_) {
512 if (!a_args.test_only) {
513 ALOGE("Failed to create DrmKmsPlan");
514 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 return HWC2::Error::BadConfig;
516 }
517
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200518 a_args.composition = current_plan_;
519
Roman Stratiienko4e994052022-02-09 17:40:35 +0200520 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521
522 if (ret) {
523 if (!a_args.test_only)
524 ALOGE("Failed to apply the frame composition ret=%d", ret);
525 return HWC2::Error::BadParameter;
526 }
527
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200528 if (mode_update_commited_) {
529 staged_mode_.reset();
530 vsync_tracking_en_ = false;
531 if (last_vsync_ts_ != 0) {
532 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
533 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
534 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535 }
536
537 return HWC2::Error::None;
538}
539
540/* Find API details at:
541 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
542 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300543HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200544 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300545 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200546 return HWC2::Error::None;
547 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200548 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200549
550 ++total_stats_.total_frames_;
551
552 AtomicCommitArgs a_args{};
553 ret = CreateComposition(a_args);
554
555 if (ret != HWC2::Error::None)
556 ++total_stats_.failed_kms_present_;
557
558 if (ret == HWC2::Error::BadLayer) {
559 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300560 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200561 return HWC2::Error::None;
562 }
563 if (ret != HWC2::Error::None)
564 return ret;
565
Roman Stratiienkodd214942022-05-03 18:24:49 +0300566 this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get());
567 *out_present_fence = a_args.out_fence.Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200568
569 ++frame_no_;
570 return HWC2::Error::None;
571}
572
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200573HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
574 int64_t change_time) {
575 if (configs_.hwc_configs.count(config) == 0) {
576 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577 return HWC2::Error::BadConfig;
578 }
579
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200580 staged_mode_ = configs_.hwc_configs[config].mode;
581 staged_mode_change_time_ = change_time;
582 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200583
584 return HWC2::Error::None;
585}
586
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200587HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
588 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
589}
590
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200591/* Find API details at:
592 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
593 */
594HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
595 int32_t acquire_fence,
596 int32_t dataspace,
597 hwc_region_t /*damage*/) {
598 client_layer_.SetLayerBuffer(target, acquire_fence);
599 client_layer_.SetLayerDataspace(dataspace);
600
601 /*
602 * target can be nullptr, this does mean the Composer Service is calling
603 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
604 * 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
605 */
606 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300607 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 return HWC2::Error::None;
609 }
610
Roman Stratiienko5070d512022-05-30 13:41:20 +0300611 if (IsInHeadlessMode()) {
612 return HWC2::Error::None;
613 }
614
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200615 client_layer_.PopulateLayerData(/*test = */ true);
616 if (!client_layer_.IsLayerUsableAsDevice()) {
617 ALOGE("Client layer must be always usable by DRM/KMS");
618 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200619 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200621 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200622 hwc_frect_t source_crop = {.left = 0.0F,
623 .top = 0.0F,
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200624 .right = static_cast<float>(bi->width),
625 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200626 client_layer_.SetLayerSourceCrop(source_crop);
627
628 return HWC2::Error::None;
629}
630
631HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
632 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
633 return HWC2::Error::BadParameter;
634
635 if (mode != HAL_COLOR_MODE_NATIVE)
636 return HWC2::Error::Unsupported;
637
638 color_mode_ = mode;
639 return HWC2::Error::None;
640}
641
642HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
643 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
644 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
645 return HWC2::Error::BadParameter;
646
647 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
648 return HWC2::Error::BadParameter;
649
650 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
651 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
652 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
653
654 return HWC2::Error::None;
655}
656
657HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
658 int32_t /*release_fence*/) {
659 // TODO(nobody): Need virtual display support
660 return HWC2::Error::Unsupported;
661}
662
663HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
664 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300665
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200666 AtomicCommitArgs a_args{};
667
668 switch (mode) {
669 case HWC2::PowerMode::Off:
670 a_args.active = false;
671 break;
672 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300673 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200674 break;
675 case HWC2::PowerMode::Doze:
676 case HWC2::PowerMode::DozeSuspend:
677 return HWC2::Error::Unsupported;
678 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300679 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200680 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300681 }
682
683 if (IsInHeadlessMode()) {
684 return HWC2::Error::None;
685 }
686
687 if (a_args.active) {
688 /*
689 * Setting the display to active before we have a composition
690 * can break some drivers, so skip setting a_args.active to
691 * true, as the next composition frame will implicitly activate
692 * the display
693 */
694 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
695 ? HWC2::Error::None
696 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200697 };
698
Roman Stratiienko4e994052022-02-09 17:40:35 +0200699 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200700 if (err) {
701 ALOGE("Failed to apply the dpms composition err=%d", err);
702 return HWC2::Error::BadParameter;
703 }
704 return HWC2::Error::None;
705}
706
707HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200708 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
709 if (vsync_event_en_) {
710 vsync_worker_.VSyncControl(true);
711 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200712 return HWC2::Error::None;
713}
714
715HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
716 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200717 if (IsInHeadlessMode()) {
718 *num_types = *num_requests = 0;
719 return HWC2::Error::None;
720 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300721
722 /* In current drm_hwc design in case previous frame layer was not validated as
723 * a CLIENT, it is used by display controller (Front buffer). We have to store
724 * this state to provide the CLIENT with the release fences for such buffers.
725 */
726 for (auto &l : layers_) {
727 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
728 HWC2::Composition::Client);
729 }
730
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200731 return backend_->ValidateDisplay(this, num_types, num_requests);
732}
733
734std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
735 std::vector<HwcLayer *> ordered_layers;
736 ordered_layers.reserve(layers_.size());
737
738 for (auto &[handle, layer] : layers_) {
739 ordered_layers.emplace_back(&layer);
740 }
741
742 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
743 [](const HwcLayer *lhs, const HwcLayer *rhs) {
744 return lhs->GetZOrder() < rhs->GetZOrder();
745 });
746
747 return ordered_layers;
748}
749
Roman Stratiienko099c3112022-01-20 11:50:54 +0200750HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
751 uint32_t *outVsyncPeriod /* ns */) {
752 return GetDisplayAttribute(configs_.active_config_id,
753 HWC2_ATTRIBUTE_VSYNC_PERIOD,
754 (int32_t *)(outVsyncPeriod));
755}
756
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200757#if PLATFORM_SDK_VERSION > 29
758HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200759 if (IsInHeadlessMode()) {
760 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
761 return HWC2::Error::None;
762 }
763 /* Primary display should be always internal,
764 * otherwise SF will be unhappy and will crash
765 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200766 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200767 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200768 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200769 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
770 else
771 return HWC2::Error::BadConfig;
772
773 return HWC2::Error::None;
774}
775
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200776HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200777 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200778 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
779 hwc_vsync_period_change_timeline_t *outTimeline) {
780 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
781 return HWC2::Error::BadParameter;
782 }
783
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200784 uint32_t current_vsync_period{};
785 GetDisplayVsyncPeriod(&current_vsync_period);
786
787 if (vsyncPeriodChangeConstraints->seamlessRequired) {
788 return HWC2::Error::SeamlessNotAllowed;
789 }
790
791 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
792 ->desiredTimeNanos -
793 current_vsync_period;
794 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
795 if (ret != HWC2::Error::None) {
796 return ret;
797 }
798
799 outTimeline->refreshRequired = true;
800 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
801 ->desiredTimeNanos;
802
803 last_vsync_ts_ = 0;
804 vsync_tracking_en_ = true;
805 vsync_worker_.VSyncControl(true);
806
807 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200808}
809
810HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
811 return HWC2::Error::Unsupported;
812}
813
814HWC2::Error HwcDisplay::GetSupportedContentTypes(
815 uint32_t *outNumSupportedContentTypes,
816 const uint32_t *outSupportedContentTypes) {
817 if (outSupportedContentTypes == nullptr)
818 *outNumSupportedContentTypes = 0;
819
820 return HWC2::Error::None;
821}
822
823HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
824 if (contentType != HWC2_CONTENT_TYPE_NONE)
825 return HWC2::Error::Unsupported;
826
827 /* TODO: Map to the DRM Connector property:
828 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
829 */
830
831 return HWC2::Error::None;
832}
833#endif
834
835#if PLATFORM_SDK_VERSION > 28
836HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
837 uint32_t *outDataSize,
838 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200839 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300840 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200841 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300842
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200843 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200844 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300845 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200846 }
847
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300848 *outPort = handle_; /* TDOD(nobody): What should be here? */
849
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200850 if (outData) {
851 *outDataSize = std::min(*outDataSize, blob->length);
852 memcpy(outData, blob->data, *outDataSize);
853 } else {
854 *outDataSize = blob->length;
855 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200856
857 return HWC2::Error::None;
858}
859
860HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
861 uint32_t * /*outCapabilities*/) {
862 if (outNumCapabilities == nullptr) {
863 return HWC2::Error::BadParameter;
864 }
865
866 *outNumCapabilities = 0;
867
868 return HWC2::Error::None;
869}
870
871HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
872 *supported = false;
873 return HWC2::Error::None;
874}
875
876HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
877 return HWC2::Error::Unsupported;
878}
879
880#endif /* PLATFORM_SDK_VERSION > 28 */
881
882#if PLATFORM_SDK_VERSION > 27
883
884HWC2::Error HwcDisplay::GetRenderIntents(
885 int32_t mode, uint32_t *outNumIntents,
886 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
887 if (mode != HAL_COLOR_MODE_NATIVE) {
888 return HWC2::Error::BadParameter;
889 }
890
891 if (outIntents == nullptr) {
892 *outNumIntents = 1;
893 return HWC2::Error::None;
894 }
895 *outNumIntents = 1;
896 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
897 return HWC2::Error::None;
898}
899
900HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
901 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
902 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
903 return HWC2::Error::BadParameter;
904
905 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
906 return HWC2::Error::BadParameter;
907
908 if (mode != HAL_COLOR_MODE_NATIVE)
909 return HWC2::Error::Unsupported;
910
911 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
912 return HWC2::Error::Unsupported;
913
914 color_mode_ = mode;
915 return HWC2::Error::None;
916}
917
918#endif /* PLATFORM_SDK_VERSION > 27 */
919
920const Backend *HwcDisplay::backend() const {
921 return backend_.get();
922}
923
924void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
925 backend_ = std::move(backend);
926}
927
Roman Stratiienko099c3112022-01-20 11:50:54 +0200928/* returns true if composition should be sent to client */
929bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
930 int flattenning_state = flattenning_state_;
931 if (flattenning_state == ClientFlattenningState::Disabled) {
932 return false;
933 }
934
935 if (skip) {
936 flattenning_state_ = ClientFlattenningState::NotRequired;
937 return false;
938 }
939
940 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
941 flattenning_state_ = ClientFlattenningState::Flattened;
942 return true;
943 }
944
945 vsync_flattening_en_ = true;
946 vsync_worker_.VSyncControl(true);
947 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
948 return false;
949}
950
951void HwcDisplay::ProcessFlatenningVsyncInternal() {
952 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
953 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
954 hwc2_->refresh_callback_.first != nullptr &&
955 hwc2_->refresh_callback_.second != nullptr) {
956 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
957 vsync_flattening_en_ = false;
958 }
959}
960
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200961} // namespace android