blob: 0f957a764bf719ffe1a1570648408bce5f5aa677 [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),
93 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
94 // clang-format off
95 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
96 0.0, 1.0, 0.0, 0.0,
97 0.0, 0.0, 1.0, 0.0,
98 0.0, 0.0, 0.0, 1.0};
99 // clang-format on
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200100}
101
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200102HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200103
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200104void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200105 Deinit();
106
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200107 pipeline_ = pipeline;
108
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200109 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200110 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200111 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
112 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200113 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200114 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200115}
116
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200117void HwcDisplay::Deinit() {
118 if (pipeline_ != nullptr) {
119 AtomicCommitArgs a_args{};
120 a_args.active = false;
121 a_args.composition = std::make_shared<DrmKmsPlan>();
122 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
123
124 vsync_worker_.Init(nullptr, [](int64_t) {});
125 current_plan_.reset();
126 backend_.reset();
127 }
128
129 SetClientTarget(nullptr, -1, 0, {});
130}
131
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200132HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200133 ChosePreferredConfig();
134
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200136 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200137 if (vsync_event_en_) {
138 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200139 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200140 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200141 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200142 if (vsync_flattening_en_) {
143 ProcessFlatenningVsyncInternal();
144 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200145 if (vsync_tracking_en_) {
146 last_vsync_ts_ = timestamp;
147 }
148 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200149 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200150 }
151 });
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200152 if (ret && ret != -EALREADY) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200153 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200154 return HWC2::Error::BadDisplay;
155 }
156
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200157 if (!IsInHeadlessMode()) {
158 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
159 if (ret) {
160 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
161 return HWC2::Error::BadDisplay;
162 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200163 }
164
165 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
166
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200167 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200168}
169
170HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200171 HWC2::Error err{};
172 if (!IsInHeadlessMode()) {
173 err = configs_.Update(*pipeline_->connector->Get());
174 } else {
175 configs_.FillHeadless();
176 }
177 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200178 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200179 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200180
Roman Stratiienko0137f862022-01-04 18:27:40 +0200181 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200182}
183
184HWC2::Error HwcDisplay::AcceptDisplayChanges() {
185 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
186 l.second.AcceptTypeChange();
187 return HWC2::Error::None;
188}
189
190HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
191 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
192 *layer = static_cast<hwc2_layer_t>(layer_idx_);
193 ++layer_idx_;
194 return HWC2::Error::None;
195}
196
197HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200198 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201
202 layers_.erase(layer);
203 return HWC2::Error::None;
204}
205
206HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200207 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200208 return HWC2::Error::BadConfig;
209
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200210 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 return HWC2::Error::None;
212}
213
214HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
215 hwc2_layer_t *layers,
216 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200217 if (IsInHeadlessMode()) {
218 *num_elements = 0;
219 return HWC2::Error::None;
220 }
221
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222 uint32_t num_changes = 0;
223 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
224 if (l.second.IsTypeChanged()) {
225 if (layers && num_changes < *num_elements)
226 layers[num_changes] = l.first;
227 if (types && num_changes < *num_elements)
228 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
229 ++num_changes;
230 }
231 }
232 if (!layers && !types)
233 *num_elements = num_changes;
234 return HWC2::Error::None;
235}
236
237HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
238 int32_t /*format*/,
239 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200240 if (IsInHeadlessMode()) {
241 return HWC2::Error::None;
242 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200243
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200244 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
245 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
246
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200247 if (width < min.first || height < min.second)
248 return HWC2::Error::Unsupported;
249
250 if (width > max.first || height > max.second)
251 return HWC2::Error::Unsupported;
252
253 if (dataspace != HAL_DATASPACE_UNKNOWN)
254 return HWC2::Error::Unsupported;
255
256 // TODO(nobody): Validate format can be handled by either GL or planes
257 return HWC2::Error::None;
258}
259
260HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
261 if (!modes)
262 *num_modes = 1;
263
264 if (modes)
265 *modes = HAL_COLOR_MODE_NATIVE;
266
267 return HWC2::Error::None;
268}
269
270HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
271 int32_t attribute_in,
272 int32_t *value) {
273 int conf = static_cast<int>(config);
274
Roman Stratiienko0137f862022-01-04 18:27:40 +0200275 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200276 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200277 return HWC2::Error::BadConfig;
278 }
279
Roman Stratiienko0137f862022-01-04 18:27:40 +0200280 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281
282 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200283 uint32_t mm_width = configs_.mm_width;
284 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
286 switch (attribute) {
287 case HWC2::Attribute::Width:
288 *value = static_cast<int>(hwc_config.mode.h_display());
289 break;
290 case HWC2::Attribute::Height:
291 *value = static_cast<int>(hwc_config.mode.v_display());
292 break;
293 case HWC2::Attribute::VsyncPeriod:
294 // in nanoseconds
295 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
296 break;
297 case HWC2::Attribute::DpiX:
298 // Dots per 1000 inches
299 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
300 kUmPerInch / mm_width)
301 : -1;
302 break;
303 case HWC2::Attribute::DpiY:
304 // Dots per 1000 inches
305 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
306 kUmPerInch / mm_height)
307 : -1;
308 break;
309#if PLATFORM_SDK_VERSION > 29
310 case HWC2::Attribute::ConfigGroup:
311 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
312 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200313 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200314 break;
315#endif
316 default:
317 *value = -1;
318 return HWC2::Error::BadConfig;
319 }
320 return HWC2::Error::None;
321}
322
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200323HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
324 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200325 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200326 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200327 if (hwc_config.second.disabled) {
328 continue;
329 }
330
331 if (configs != nullptr) {
332 if (idx >= *num_configs) {
333 break;
334 }
335 configs[idx] = hwc_config.second.id;
336 }
337
338 idx++;
339 }
340 *num_configs = idx;
341 return HWC2::Error::None;
342}
343
344HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
345 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200346 if (IsInHeadlessMode()) {
347 stream << "null-display";
348 } else {
349 stream << "display-" << GetPipe().connector->Get()->GetId();
350 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200351 std::string string = stream.str();
352 size_t length = string.length();
353 if (!name) {
354 *size = length;
355 return HWC2::Error::None;
356 }
357
358 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
359 strncpy(name, string.c_str(), *size);
360 return HWC2::Error::None;
361}
362
363HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
364 uint32_t *num_elements,
365 hwc2_layer_t * /*layers*/,
366 int32_t * /*layer_requests*/) {
367 // TODO(nobody): I think virtual display should request
368 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
369 *num_elements = 0;
370 return HWC2::Error::None;
371}
372
373HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
374 *type = static_cast<int32_t>(type_);
375 return HWC2::Error::None;
376}
377
378HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
379 *support = 0;
380 return HWC2::Error::None;
381}
382
383HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
384 int32_t * /*types*/,
385 float * /*max_luminance*/,
386 float * /*max_average_luminance*/,
387 float * /*min_luminance*/) {
388 *num_types = 0;
389 return HWC2::Error::None;
390}
391
392/* Find API details at:
393 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
394 */
395HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
396 hwc2_layer_t *layers,
397 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200398 if (IsInHeadlessMode()) {
399 *num_elements = 0;
400 return HWC2::Error::None;
401 }
402
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200403 uint32_t num_layers = 0;
404
405 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
406 ++num_layers;
407 if (layers == nullptr || fences == nullptr)
408 continue;
409
410 if (num_layers > *num_elements) {
411 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
412 return HWC2::Error::None;
413 }
414
415 layers[num_layers - 1] = l.first;
416 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
417 }
418 *num_elements = num_layers;
419 return HWC2::Error::None;
420}
421
422HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200423 if (IsInHeadlessMode()) {
424 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
425 return HWC2::Error::None;
426 }
427
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200428 int PrevModeVsyncPeriodNs = static_cast<int>(
429 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
430
431 auto mode_update_commited_ = false;
432 if (staged_mode_ &&
433 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
434 client_layer_.SetLayerDisplayFrame(
435 (hwc_rect_t){.left = 0,
436 .top = 0,
437 .right = static_cast<int>(staged_mode_->h_display()),
438 .bottom = static_cast<int>(staged_mode_->v_display())});
439
440 configs_.active_config_id = staged_mode_config_id_;
441
442 a_args.display_mode = *staged_mode_;
443 if (!a_args.test_only) {
444 mode_update_commited_ = true;
445 }
446 }
447
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200448 // order the layers by z-order
449 bool use_client_layer = false;
450 uint32_t client_z_order = UINT32_MAX;
451 std::map<uint32_t, HwcLayer *> z_map;
452 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
453 switch (l.second.GetValidatedType()) {
454 case HWC2::Composition::Device:
455 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
456 break;
457 case HWC2::Composition::Client:
458 // Place it at the z_order of the lowest client layer
459 use_client_layer = true;
460 client_z_order = std::min(client_z_order, l.second.GetZOrder());
461 break;
462 default:
463 continue;
464 }
465 }
466 if (use_client_layer)
467 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
468
469 if (z_map.empty())
470 return HWC2::Error::BadLayer;
471
472 std::vector<DrmHwcLayer> composition_layers;
473
474 // now that they're ordered by z, add them to the composition
475 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
476 DrmHwcLayer layer;
477 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200478 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479 if (ret) {
480 ALOGE("Failed to import layer, ret=%d", ret);
481 return HWC2::Error::NoResources;
482 }
483 composition_layers.emplace_back(std::move(layer));
484 }
485
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200486 /* Store plan to ensure shared planes won't be stolen by other display
487 * in between of ValidateDisplay() and PresentDisplay() calls
488 */
489 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
490 std::move(composition_layers));
491 if (!current_plan_) {
492 if (!a_args.test_only) {
493 ALOGE("Failed to create DrmKmsPlan");
494 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200495 return HWC2::Error::BadConfig;
496 }
497
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200498 a_args.composition = current_plan_;
499
Roman Stratiienko4e994052022-02-09 17:40:35 +0200500 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501
502 if (ret) {
503 if (!a_args.test_only)
504 ALOGE("Failed to apply the frame composition ret=%d", ret);
505 return HWC2::Error::BadParameter;
506 }
507
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200508 if (mode_update_commited_) {
509 staged_mode_.reset();
510 vsync_tracking_en_ = false;
511 if (last_vsync_ts_ != 0) {
512 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
513 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
514 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 }
516
517 return HWC2::Error::None;
518}
519
520/* Find API details at:
521 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
522 */
523HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200524 if (IsInHeadlessMode()) {
525 *present_fence = -1;
526 return HWC2::Error::None;
527 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200528 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200529
530 ++total_stats_.total_frames_;
531
532 AtomicCommitArgs a_args{};
533 ret = CreateComposition(a_args);
534
535 if (ret != HWC2::Error::None)
536 ++total_stats_.failed_kms_present_;
537
538 if (ret == HWC2::Error::BadLayer) {
539 // Can we really have no client or device layers?
540 *present_fence = -1;
541 return HWC2::Error::None;
542 }
543 if (ret != HWC2::Error::None)
544 return ret;
545
546 *present_fence = a_args.out_fence.Release();
547
548 ++frame_no_;
549 return HWC2::Error::None;
550}
551
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200552HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
553 int64_t change_time) {
554 if (configs_.hwc_configs.count(config) == 0) {
555 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200556 return HWC2::Error::BadConfig;
557 }
558
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200559 staged_mode_ = configs_.hwc_configs[config].mode;
560 staged_mode_change_time_ = change_time;
561 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200562
563 return HWC2::Error::None;
564}
565
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200566HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
567 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
568}
569
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200570/* Find API details at:
571 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
572 */
573HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
574 int32_t acquire_fence,
575 int32_t dataspace,
576 hwc_region_t /*damage*/) {
577 client_layer_.SetLayerBuffer(target, acquire_fence);
578 client_layer_.SetLayerDataspace(dataspace);
579
580 /*
581 * target can be nullptr, this does mean the Composer Service is calling
582 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
583 * 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
584 */
585 if (target == nullptr) {
586 return HWC2::Error::None;
587 }
588
589 /* TODO: Do not update source_crop every call.
590 * It makes sense to do it once after every hotplug event. */
591 HwcDrmBo bo{};
592 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
593
594 hwc_frect_t source_crop = {.left = 0.0F,
595 .top = 0.0F,
596 .right = static_cast<float>(bo.width),
597 .bottom = static_cast<float>(bo.height)};
598 client_layer_.SetLayerSourceCrop(source_crop);
599
600 return HWC2::Error::None;
601}
602
603HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
604 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
605 return HWC2::Error::BadParameter;
606
607 if (mode != HAL_COLOR_MODE_NATIVE)
608 return HWC2::Error::Unsupported;
609
610 color_mode_ = mode;
611 return HWC2::Error::None;
612}
613
614HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
615 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
616 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
617 return HWC2::Error::BadParameter;
618
619 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
620 return HWC2::Error::BadParameter;
621
622 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
623 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
624 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
625
626 return HWC2::Error::None;
627}
628
629HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
630 int32_t /*release_fence*/) {
631 // TODO(nobody): Need virtual display support
632 return HWC2::Error::Unsupported;
633}
634
635HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200636 if (IsInHeadlessMode()) {
637 return HWC2::Error::None;
638 }
639
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200640 auto mode = static_cast<HWC2::PowerMode>(mode_in);
641 AtomicCommitArgs a_args{};
642
643 switch (mode) {
644 case HWC2::PowerMode::Off:
645 a_args.active = false;
646 break;
647 case HWC2::PowerMode::On:
648 /*
649 * Setting the display to active before we have a composition
650 * can break some drivers, so skip setting a_args.active to
651 * true, as the next composition frame will implicitly activate
652 * the display
653 */
Roman Stratiienko4e994052022-02-09 17:40:35 +0200654 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200655 ? HWC2::Error::None
656 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200657 break;
658 case HWC2::PowerMode::Doze:
659 case HWC2::PowerMode::DozeSuspend:
660 return HWC2::Error::Unsupported;
661 default:
662 ALOGI("Power mode %d is unsupported\n", mode);
663 return HWC2::Error::BadParameter;
664 };
665
Roman Stratiienko4e994052022-02-09 17:40:35 +0200666 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200667 if (err) {
668 ALOGE("Failed to apply the dpms composition err=%d", err);
669 return HWC2::Error::BadParameter;
670 }
671 return HWC2::Error::None;
672}
673
674HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200675 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
676 if (vsync_event_en_) {
677 vsync_worker_.VSyncControl(true);
678 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200679 return HWC2::Error::None;
680}
681
682HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
683 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200684 if (IsInHeadlessMode()) {
685 *num_types = *num_requests = 0;
686 return HWC2::Error::None;
687 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200688 return backend_->ValidateDisplay(this, num_types, num_requests);
689}
690
691std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
692 std::vector<HwcLayer *> ordered_layers;
693 ordered_layers.reserve(layers_.size());
694
695 for (auto &[handle, layer] : layers_) {
696 ordered_layers.emplace_back(&layer);
697 }
698
699 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
700 [](const HwcLayer *lhs, const HwcLayer *rhs) {
701 return lhs->GetZOrder() < rhs->GetZOrder();
702 });
703
704 return ordered_layers;
705}
706
Roman Stratiienko099c3112022-01-20 11:50:54 +0200707HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
708 uint32_t *outVsyncPeriod /* ns */) {
709 return GetDisplayAttribute(configs_.active_config_id,
710 HWC2_ATTRIBUTE_VSYNC_PERIOD,
711 (int32_t *)(outVsyncPeriod));
712}
713
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200714#if PLATFORM_SDK_VERSION > 29
715HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200716 if (IsInHeadlessMode()) {
717 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
718 return HWC2::Error::None;
719 }
720 /* Primary display should be always internal,
721 * otherwise SF will be unhappy and will crash
722 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200723 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200724 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200725 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200726 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
727 else
728 return HWC2::Error::BadConfig;
729
730 return HWC2::Error::None;
731}
732
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200733HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200734 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200735 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
736 hwc_vsync_period_change_timeline_t *outTimeline) {
737 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
738 return HWC2::Error::BadParameter;
739 }
740
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200741 uint32_t current_vsync_period{};
742 GetDisplayVsyncPeriod(&current_vsync_period);
743
744 if (vsyncPeriodChangeConstraints->seamlessRequired) {
745 return HWC2::Error::SeamlessNotAllowed;
746 }
747
748 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
749 ->desiredTimeNanos -
750 current_vsync_period;
751 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
752 if (ret != HWC2::Error::None) {
753 return ret;
754 }
755
756 outTimeline->refreshRequired = true;
757 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
758 ->desiredTimeNanos;
759
760 last_vsync_ts_ = 0;
761 vsync_tracking_en_ = true;
762 vsync_worker_.VSyncControl(true);
763
764 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200765}
766
767HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
768 return HWC2::Error::Unsupported;
769}
770
771HWC2::Error HwcDisplay::GetSupportedContentTypes(
772 uint32_t *outNumSupportedContentTypes,
773 const uint32_t *outSupportedContentTypes) {
774 if (outSupportedContentTypes == nullptr)
775 *outNumSupportedContentTypes = 0;
776
777 return HWC2::Error::None;
778}
779
780HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
781 if (contentType != HWC2_CONTENT_TYPE_NONE)
782 return HWC2::Error::Unsupported;
783
784 /* TODO: Map to the DRM Connector property:
785 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
786 */
787
788 return HWC2::Error::None;
789}
790#endif
791
792#if PLATFORM_SDK_VERSION > 28
793HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
794 uint32_t *outDataSize,
795 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200796 if (IsInHeadlessMode()) {
797 return HWC2::Error::None;
798 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200799 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200800
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200801 *outPort = handle_ - 1;
802
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200803 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200804 if (outData == nullptr) {
805 *outDataSize = 0;
806 }
807 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200808 }
809
810 if (outData) {
811 *outDataSize = std::min(*outDataSize, blob->length);
812 memcpy(outData, blob->data, *outDataSize);
813 } else {
814 *outDataSize = blob->length;
815 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200816
817 return HWC2::Error::None;
818}
819
820HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
821 uint32_t * /*outCapabilities*/) {
822 if (outNumCapabilities == nullptr) {
823 return HWC2::Error::BadParameter;
824 }
825
826 *outNumCapabilities = 0;
827
828 return HWC2::Error::None;
829}
830
831HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
832 *supported = false;
833 return HWC2::Error::None;
834}
835
836HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
837 return HWC2::Error::Unsupported;
838}
839
840#endif /* PLATFORM_SDK_VERSION > 28 */
841
842#if PLATFORM_SDK_VERSION > 27
843
844HWC2::Error HwcDisplay::GetRenderIntents(
845 int32_t mode, uint32_t *outNumIntents,
846 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
847 if (mode != HAL_COLOR_MODE_NATIVE) {
848 return HWC2::Error::BadParameter;
849 }
850
851 if (outIntents == nullptr) {
852 *outNumIntents = 1;
853 return HWC2::Error::None;
854 }
855 *outNumIntents = 1;
856 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
857 return HWC2::Error::None;
858}
859
860HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
861 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
862 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
863 return HWC2::Error::BadParameter;
864
865 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
866 return HWC2::Error::BadParameter;
867
868 if (mode != HAL_COLOR_MODE_NATIVE)
869 return HWC2::Error::Unsupported;
870
871 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
872 return HWC2::Error::Unsupported;
873
874 color_mode_ = mode;
875 return HWC2::Error::None;
876}
877
878#endif /* PLATFORM_SDK_VERSION > 27 */
879
880const Backend *HwcDisplay::backend() const {
881 return backend_.get();
882}
883
884void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
885 backend_ = std::move(backend);
886}
887
Roman Stratiienko099c3112022-01-20 11:50:54 +0200888/* returns true if composition should be sent to client */
889bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
890 int flattenning_state = flattenning_state_;
891 if (flattenning_state == ClientFlattenningState::Disabled) {
892 return false;
893 }
894
895 if (skip) {
896 flattenning_state_ = ClientFlattenningState::NotRequired;
897 return false;
898 }
899
900 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
901 flattenning_state_ = ClientFlattenningState::Flattened;
902 return true;
903 }
904
905 vsync_flattening_en_ = true;
906 vsync_worker_.VSyncControl(true);
907 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
908 return false;
909}
910
911void HwcDisplay::ProcessFlatenningVsyncInternal() {
912 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
913 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
914 hwc2_->refresh_callback_.first != nullptr &&
915 hwc2_->refresh_callback_.second != nullptr) {
916 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
917 vsync_flattening_en_ = false;
918 }
919}
920
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200921} // namespace android