blob: 3f00fbff281474ccdf10b0b373197fba54c284c2 [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"
23#include "backend/BackendManager.h"
24#include "bufferinfo/BufferInfoGetter.h"
25#include "utils/log.h"
26#include "utils/properties.h"
27
28namespace android {
29
Roman Stratiienko3dacd472022-01-11 19:18:34 +020030// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
31uint32_t HwcDisplay::layer_idx_ = 2; /* Start from 2. See destroyLayer() */
32
Roman Stratiienko3627beb2022-01-04 16:02:55 +020033std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
34 if (delta.total_pixops_ == 0)
35 return "No stats yet";
36 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
37
38 std::stringstream ss;
39 ss << " Total frames count: " << delta.total_frames_ << "\n"
40 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
41 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
42 << ((delta.failed_kms_present_ > 0)
43 ? " !!! Internal failure, FIX it please\n"
44 : "")
45 << " Flattened frames: " << delta.frames_flattened_ << "\n"
46 << " Pixel operations (free units)"
47 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
48 << "]\n"
49 << " Composition efficiency: " << ratio;
50
51 return ss.str();
52}
53
54std::string HwcDisplay::Dump() {
55 std::string flattening_state_str;
56 switch (flattenning_state_) {
57 case ClientFlattenningState::Disabled:
58 flattening_state_str = "Disabled";
59 break;
60 case ClientFlattenningState::NotRequired:
61 flattening_state_str = "Not needed";
62 break;
63 case ClientFlattenningState::Flattened:
64 flattening_state_str = "Active";
65 break;
66 case ClientFlattenningState::ClientRefreshRequested:
67 flattening_state_str = "Refresh requested";
68 break;
69 default:
70 flattening_state_str = std::to_string(flattenning_state_) +
71 " VSync remains";
72 }
73
Roman Stratiienko19c162f2022-02-01 09:35:08 +020074 std::string connector_name = IsInHeadlessMode()
75 ? "NULL-DISPLAY"
76 : GetPipe().connector->Get()->GetName();
77
Roman Stratiienko3627beb2022-01-04 16:02:55 +020078 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020079 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020080 << " Flattening state: " << flattening_state_str << "\n"
81 << "Statistics since system boot:\n"
82 << DumpDelta(total_stats_) << "\n\n"
83 << "Statistics since last dumpsys request:\n"
84 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
85
86 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
87 return ss.str();
88}
89
Roman Stratiienko3dacd472022-01-11 19:18:34 +020090HwcDisplay::HwcDisplay(DrmDisplayPipeline *pipeline, hwc2_display_t handle,
Roman Stratiienko19c162f2022-02-01 09:35:08 +020091 HWC2::DisplayType type, DrmHwcTwo *hwc2)
Roman Stratiienko3627beb2022-01-04 16:02:55 +020092 : hwc2_(hwc2),
Roman Stratiienko19c162f2022-02-01 09:35:08 +020093 pipeline_(pipeline),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020094 handle_(handle),
95 type_(type),
96 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
97 // clang-format off
98 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
99 0.0, 1.0, 0.0, 0.0,
100 0.0, 0.0, 1.0, 0.0,
101 0.0, 0.0, 0.0, 1.0};
102 // clang-format on
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200103
104 ChosePreferredConfig();
105 Init();
106
107 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
108}
109
110HwcDisplay::~HwcDisplay() {
111 if (handle_ != kPrimaryDisplay) {
112 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
113 }
114
115 auto &main_lock = hwc2_->GetResMan().GetMainLock();
116 /* Unlock to allow pending vsync callbacks to finish */
117 main_lock.unlock();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200118 vsync_worker_.VSyncControl(false);
119 vsync_worker_.Exit();
120 main_lock.lock();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200121}
122
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200123HWC2::Error HwcDisplay::Init() {
124 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200125 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200126 if (vsync_event_en_) {
127 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200128 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200129 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200130 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200131 if (vsync_flattening_en_) {
132 ProcessFlatenningVsyncInternal();
133 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200134 if (vsync_tracking_en_) {
135 last_vsync_ts_ = timestamp;
136 }
137 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200138 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200139 }
140 });
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200141 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200142 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200143 return HWC2::Error::BadDisplay;
144 }
145
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200146 if (!IsInHeadlessMode()) {
147 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
148 if (ret) {
149 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
150 return HWC2::Error::BadDisplay;
151 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200152 }
153
154 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
155
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200156 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200157}
158
159HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200160 HWC2::Error err{};
161 if (!IsInHeadlessMode()) {
162 err = configs_.Update(*pipeline_->connector->Get());
163 } else {
164 configs_.FillHeadless();
165 }
166 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200167 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200168 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200169
Roman Stratiienko0137f862022-01-04 18:27:40 +0200170 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171}
172
173HWC2::Error HwcDisplay::AcceptDisplayChanges() {
174 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
175 l.second.AcceptTypeChange();
176 return HWC2::Error::None;
177}
178
179HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
180 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
181 *layer = static_cast<hwc2_layer_t>(layer_idx_);
182 ++layer_idx_;
183 return HWC2::Error::None;
184}
185
186HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200187 if (!get_layer(layer)) {
188 /* Primary display don't send unplug event, instead it replaces
189 * display to headless or to another one and sends Plug event to the
190 * SF. SF can't distinguish this case from virtualized display size
191 * change case and will destroy previously used layers. If we will return
192 * BadLayer, service will print errors to the logcat.
193 *
194 * Nevertheless VTS is trying to destroy 1st layer without adding any
195 * layers prior to that, than it checks for BadLayer result. So we
196 * numbering the layers starting from 2, and use index 1 to catch VTS client
197 * to return BadLayer, making VTS pass.
198 */
199 if (layers_.empty() && layer != 1) {
200 return HWC2::Error::None;
201 }
202
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200203 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200204 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200205
206 layers_.erase(layer);
207 return HWC2::Error::None;
208}
209
210HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200211 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200212 return HWC2::Error::BadConfig;
213
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200214 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215 return HWC2::Error::None;
216}
217
218HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
219 hwc2_layer_t *layers,
220 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200221 if (IsInHeadlessMode()) {
222 *num_elements = 0;
223 return HWC2::Error::None;
224 }
225
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226 uint32_t num_changes = 0;
227 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
228 if (l.second.IsTypeChanged()) {
229 if (layers && num_changes < *num_elements)
230 layers[num_changes] = l.first;
231 if (types && num_changes < *num_elements)
232 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
233 ++num_changes;
234 }
235 }
236 if (!layers && !types)
237 *num_elements = num_changes;
238 return HWC2::Error::None;
239}
240
241HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
242 int32_t /*format*/,
243 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200244 if (IsInHeadlessMode()) {
245 return HWC2::Error::None;
246 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200247
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200248 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
249 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
250
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200251 if (width < min.first || height < min.second)
252 return HWC2::Error::Unsupported;
253
254 if (width > max.first || height > max.second)
255 return HWC2::Error::Unsupported;
256
257 if (dataspace != HAL_DATASPACE_UNKNOWN)
258 return HWC2::Error::Unsupported;
259
260 // TODO(nobody): Validate format can be handled by either GL or planes
261 return HWC2::Error::None;
262}
263
264HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
265 if (!modes)
266 *num_modes = 1;
267
268 if (modes)
269 *modes = HAL_COLOR_MODE_NATIVE;
270
271 return HWC2::Error::None;
272}
273
274HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
275 int32_t attribute_in,
276 int32_t *value) {
277 int conf = static_cast<int>(config);
278
Roman Stratiienko0137f862022-01-04 18:27:40 +0200279 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200280 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281 return HWC2::Error::BadConfig;
282 }
283
Roman Stratiienko0137f862022-01-04 18:27:40 +0200284 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285
286 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200287 uint32_t mm_width = configs_.mm_width;
288 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200289 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
290 switch (attribute) {
291 case HWC2::Attribute::Width:
292 *value = static_cast<int>(hwc_config.mode.h_display());
293 break;
294 case HWC2::Attribute::Height:
295 *value = static_cast<int>(hwc_config.mode.v_display());
296 break;
297 case HWC2::Attribute::VsyncPeriod:
298 // in nanoseconds
299 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
300 break;
301 case HWC2::Attribute::DpiX:
302 // Dots per 1000 inches
303 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
304 kUmPerInch / mm_width)
305 : -1;
306 break;
307 case HWC2::Attribute::DpiY:
308 // Dots per 1000 inches
309 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
310 kUmPerInch / mm_height)
311 : -1;
312 break;
313#if PLATFORM_SDK_VERSION > 29
314 case HWC2::Attribute::ConfigGroup:
315 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
316 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200317 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200318 break;
319#endif
320 default:
321 *value = -1;
322 return HWC2::Error::BadConfig;
323 }
324 return HWC2::Error::None;
325}
326
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200327HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
328 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200329 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200330 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200331 if (hwc_config.second.disabled) {
332 continue;
333 }
334
335 if (configs != nullptr) {
336 if (idx >= *num_configs) {
337 break;
338 }
339 configs[idx] = hwc_config.second.id;
340 }
341
342 idx++;
343 }
344 *num_configs = idx;
345 return HWC2::Error::None;
346}
347
348HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
349 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200350 if (IsInHeadlessMode()) {
351 stream << "null-display";
352 } else {
353 stream << "display-" << GetPipe().connector->Get()->GetId();
354 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200355 std::string string = stream.str();
356 size_t length = string.length();
357 if (!name) {
358 *size = length;
359 return HWC2::Error::None;
360 }
361
362 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
363 strncpy(name, string.c_str(), *size);
364 return HWC2::Error::None;
365}
366
367HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
368 uint32_t *num_elements,
369 hwc2_layer_t * /*layers*/,
370 int32_t * /*layer_requests*/) {
371 // TODO(nobody): I think virtual display should request
372 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
373 *num_elements = 0;
374 return HWC2::Error::None;
375}
376
377HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
378 *type = static_cast<int32_t>(type_);
379 return HWC2::Error::None;
380}
381
382HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
383 *support = 0;
384 return HWC2::Error::None;
385}
386
387HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
388 int32_t * /*types*/,
389 float * /*max_luminance*/,
390 float * /*max_average_luminance*/,
391 float * /*min_luminance*/) {
392 *num_types = 0;
393 return HWC2::Error::None;
394}
395
396/* Find API details at:
397 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
398 */
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
409 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
410 ++num_layers;
411 if (layers == nullptr || fences == nullptr)
412 continue;
413
414 if (num_layers > *num_elements) {
415 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
416 return HWC2::Error::None;
417 }
418
419 layers[num_layers - 1] = l.first;
420 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
421 }
422 *num_elements = num_layers;
423 return HWC2::Error::None;
424}
425
426HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200427 if (IsInHeadlessMode()) {
428 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
429 return HWC2::Error::None;
430 }
431
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200432 int PrevModeVsyncPeriodNs = static_cast<int>(
433 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
434
435 auto mode_update_commited_ = false;
436 if (staged_mode_ &&
437 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
438 client_layer_.SetLayerDisplayFrame(
439 (hwc_rect_t){.left = 0,
440 .top = 0,
441 .right = static_cast<int>(staged_mode_->h_display()),
442 .bottom = static_cast<int>(staged_mode_->v_display())});
443
444 configs_.active_config_id = staged_mode_config_id_;
445
446 a_args.display_mode = *staged_mode_;
447 if (!a_args.test_only) {
448 mode_update_commited_ = true;
449 }
450 }
451
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200452 // order the layers by z-order
453 bool use_client_layer = false;
454 uint32_t client_z_order = UINT32_MAX;
455 std::map<uint32_t, HwcLayer *> z_map;
456 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
457 switch (l.second.GetValidatedType()) {
458 case HWC2::Composition::Device:
459 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
460 break;
461 case HWC2::Composition::Client:
462 // Place it at the z_order of the lowest client layer
463 use_client_layer = true;
464 client_z_order = std::min(client_z_order, l.second.GetZOrder());
465 break;
466 default:
467 continue;
468 }
469 }
470 if (use_client_layer)
471 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
472
473 if (z_map.empty())
474 return HWC2::Error::BadLayer;
475
476 std::vector<DrmHwcLayer> composition_layers;
477
478 // now that they're ordered by z, add them to the composition
479 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
480 DrmHwcLayer layer;
481 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200482 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200483 if (ret) {
484 ALOGE("Failed to import layer, ret=%d", ret);
485 return HWC2::Error::NoResources;
486 }
487 composition_layers.emplace_back(std::move(layer));
488 }
489
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200490 /* Store plan to ensure shared planes won't be stolen by other display
491 * in between of ValidateDisplay() and PresentDisplay() calls
492 */
493 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
494 std::move(composition_layers));
495 if (!current_plan_) {
496 if (!a_args.test_only) {
497 ALOGE("Failed to create DrmKmsPlan");
498 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200499 return HWC2::Error::BadConfig;
500 }
501
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200502 a_args.composition = current_plan_;
503
Roman Stratiienko4e994052022-02-09 17:40:35 +0200504 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200505
506 if (ret) {
507 if (!a_args.test_only)
508 ALOGE("Failed to apply the frame composition ret=%d", ret);
509 return HWC2::Error::BadParameter;
510 }
511
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200512 if (mode_update_commited_) {
513 staged_mode_.reset();
514 vsync_tracking_en_ = false;
515 if (last_vsync_ts_ != 0) {
516 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
517 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
518 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200519 }
520
521 return HWC2::Error::None;
522}
523
524/* Find API details at:
525 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
526 */
527HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200528 if (IsInHeadlessMode()) {
529 *present_fence = -1;
530 return HWC2::Error::None;
531 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200532 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533
534 ++total_stats_.total_frames_;
535
536 AtomicCommitArgs a_args{};
537 ret = CreateComposition(a_args);
538
539 if (ret != HWC2::Error::None)
540 ++total_stats_.failed_kms_present_;
541
542 if (ret == HWC2::Error::BadLayer) {
543 // Can we really have no client or device layers?
544 *present_fence = -1;
545 return HWC2::Error::None;
546 }
547 if (ret != HWC2::Error::None)
548 return ret;
549
550 *present_fence = a_args.out_fence.Release();
551
552 ++frame_no_;
553 return HWC2::Error::None;
554}
555
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200556HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
557 int64_t change_time) {
558 if (configs_.hwc_configs.count(config) == 0) {
559 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200560 return HWC2::Error::BadConfig;
561 }
562
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200563 staged_mode_ = configs_.hwc_configs[config].mode;
564 staged_mode_change_time_ = change_time;
565 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566
567 return HWC2::Error::None;
568}
569
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200570HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
571 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
572}
573
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200574/* Find API details at:
575 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
576 */
577HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
578 int32_t acquire_fence,
579 int32_t dataspace,
580 hwc_region_t /*damage*/) {
581 client_layer_.SetLayerBuffer(target, acquire_fence);
582 client_layer_.SetLayerDataspace(dataspace);
583
584 /*
585 * target can be nullptr, this does mean the Composer Service is calling
586 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
587 * 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
588 */
589 if (target == nullptr) {
590 return HWC2::Error::None;
591 }
592
593 /* TODO: Do not update source_crop every call.
594 * It makes sense to do it once after every hotplug event. */
595 HwcDrmBo bo{};
596 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
597
598 hwc_frect_t source_crop = {.left = 0.0F,
599 .top = 0.0F,
600 .right = static_cast<float>(bo.width),
601 .bottom = static_cast<float>(bo.height)};
602 client_layer_.SetLayerSourceCrop(source_crop);
603
604 return HWC2::Error::None;
605}
606
607HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
608 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
609 return HWC2::Error::BadParameter;
610
611 if (mode != HAL_COLOR_MODE_NATIVE)
612 return HWC2::Error::Unsupported;
613
614 color_mode_ = mode;
615 return HWC2::Error::None;
616}
617
618HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
619 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
620 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
621 return HWC2::Error::BadParameter;
622
623 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
624 return HWC2::Error::BadParameter;
625
626 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
627 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
628 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
629
630 return HWC2::Error::None;
631}
632
633HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
634 int32_t /*release_fence*/) {
635 // TODO(nobody): Need virtual display support
636 return HWC2::Error::Unsupported;
637}
638
639HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200640 if (IsInHeadlessMode()) {
641 return HWC2::Error::None;
642 }
643
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200644 auto mode = static_cast<HWC2::PowerMode>(mode_in);
645 AtomicCommitArgs a_args{};
646
647 switch (mode) {
648 case HWC2::PowerMode::Off:
649 a_args.active = false;
650 break;
651 case HWC2::PowerMode::On:
652 /*
653 * Setting the display to active before we have a composition
654 * can break some drivers, so skip setting a_args.active to
655 * true, as the next composition frame will implicitly activate
656 * the display
657 */
Roman Stratiienko4e994052022-02-09 17:40:35 +0200658 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200659 ? HWC2::Error::None
660 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200661 break;
662 case HWC2::PowerMode::Doze:
663 case HWC2::PowerMode::DozeSuspend:
664 return HWC2::Error::Unsupported;
665 default:
666 ALOGI("Power mode %d is unsupported\n", mode);
667 return HWC2::Error::BadParameter;
668 };
669
Roman Stratiienko4e994052022-02-09 17:40:35 +0200670 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200671 if (err) {
672 ALOGE("Failed to apply the dpms composition err=%d", err);
673 return HWC2::Error::BadParameter;
674 }
675 return HWC2::Error::None;
676}
677
678HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200679 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
680 if (vsync_event_en_) {
681 vsync_worker_.VSyncControl(true);
682 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200683 return HWC2::Error::None;
684}
685
686HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
687 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200688 if (IsInHeadlessMode()) {
689 *num_types = *num_requests = 0;
690 return HWC2::Error::None;
691 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200692 return backend_->ValidateDisplay(this, num_types, num_requests);
693}
694
695std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
696 std::vector<HwcLayer *> ordered_layers;
697 ordered_layers.reserve(layers_.size());
698
699 for (auto &[handle, layer] : layers_) {
700 ordered_layers.emplace_back(&layer);
701 }
702
703 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
704 [](const HwcLayer *lhs, const HwcLayer *rhs) {
705 return lhs->GetZOrder() < rhs->GetZOrder();
706 });
707
708 return ordered_layers;
709}
710
Roman Stratiienko099c3112022-01-20 11:50:54 +0200711HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
712 uint32_t *outVsyncPeriod /* ns */) {
713 return GetDisplayAttribute(configs_.active_config_id,
714 HWC2_ATTRIBUTE_VSYNC_PERIOD,
715 (int32_t *)(outVsyncPeriod));
716}
717
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200718#if PLATFORM_SDK_VERSION > 29
719HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200720 if (IsInHeadlessMode()) {
721 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
722 return HWC2::Error::None;
723 }
724 /* Primary display should be always internal,
725 * otherwise SF will be unhappy and will crash
726 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200727 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200728 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200729 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200730 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
731 else
732 return HWC2::Error::BadConfig;
733
734 return HWC2::Error::None;
735}
736
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200737HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200738 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200739 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
740 hwc_vsync_period_change_timeline_t *outTimeline) {
741 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
742 return HWC2::Error::BadParameter;
743 }
744
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200745 uint32_t current_vsync_period{};
746 GetDisplayVsyncPeriod(&current_vsync_period);
747
748 if (vsyncPeriodChangeConstraints->seamlessRequired) {
749 return HWC2::Error::SeamlessNotAllowed;
750 }
751
752 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
753 ->desiredTimeNanos -
754 current_vsync_period;
755 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
756 if (ret != HWC2::Error::None) {
757 return ret;
758 }
759
760 outTimeline->refreshRequired = true;
761 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
762 ->desiredTimeNanos;
763
764 last_vsync_ts_ = 0;
765 vsync_tracking_en_ = true;
766 vsync_worker_.VSyncControl(true);
767
768 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200769}
770
771HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
772 return HWC2::Error::Unsupported;
773}
774
775HWC2::Error HwcDisplay::GetSupportedContentTypes(
776 uint32_t *outNumSupportedContentTypes,
777 const uint32_t *outSupportedContentTypes) {
778 if (outSupportedContentTypes == nullptr)
779 *outNumSupportedContentTypes = 0;
780
781 return HWC2::Error::None;
782}
783
784HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
785 if (contentType != HWC2_CONTENT_TYPE_NONE)
786 return HWC2::Error::Unsupported;
787
788 /* TODO: Map to the DRM Connector property:
789 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
790 */
791
792 return HWC2::Error::None;
793}
794#endif
795
796#if PLATFORM_SDK_VERSION > 28
797HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
798 uint32_t *outDataSize,
799 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200800 if (IsInHeadlessMode()) {
801 return HWC2::Error::None;
802 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200803 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200804
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200805 *outPort = handle_ - 1;
806
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200807 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200808 if (outData == nullptr) {
809 *outDataSize = 0;
810 }
811 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200812 }
813
814 if (outData) {
815 *outDataSize = std::min(*outDataSize, blob->length);
816 memcpy(outData, blob->data, *outDataSize);
817 } else {
818 *outDataSize = blob->length;
819 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200820
821 return HWC2::Error::None;
822}
823
824HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
825 uint32_t * /*outCapabilities*/) {
826 if (outNumCapabilities == nullptr) {
827 return HWC2::Error::BadParameter;
828 }
829
830 *outNumCapabilities = 0;
831
832 return HWC2::Error::None;
833}
834
835HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
836 *supported = false;
837 return HWC2::Error::None;
838}
839
840HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
841 return HWC2::Error::Unsupported;
842}
843
844#endif /* PLATFORM_SDK_VERSION > 28 */
845
846#if PLATFORM_SDK_VERSION > 27
847
848HWC2::Error HwcDisplay::GetRenderIntents(
849 int32_t mode, uint32_t *outNumIntents,
850 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
851 if (mode != HAL_COLOR_MODE_NATIVE) {
852 return HWC2::Error::BadParameter;
853 }
854
855 if (outIntents == nullptr) {
856 *outNumIntents = 1;
857 return HWC2::Error::None;
858 }
859 *outNumIntents = 1;
860 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
861 return HWC2::Error::None;
862}
863
864HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
865 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
866 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
867 return HWC2::Error::BadParameter;
868
869 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
870 return HWC2::Error::BadParameter;
871
872 if (mode != HAL_COLOR_MODE_NATIVE)
873 return HWC2::Error::Unsupported;
874
875 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
876 return HWC2::Error::Unsupported;
877
878 color_mode_ = mode;
879 return HWC2::Error::None;
880}
881
882#endif /* PLATFORM_SDK_VERSION > 27 */
883
884const Backend *HwcDisplay::backend() const {
885 return backend_.get();
886}
887
888void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
889 backend_ = std::move(backend);
890}
891
Roman Stratiienko099c3112022-01-20 11:50:54 +0200892/* returns true if composition should be sent to client */
893bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
894 int flattenning_state = flattenning_state_;
895 if (flattenning_state == ClientFlattenningState::Disabled) {
896 return false;
897 }
898
899 if (skip) {
900 flattenning_state_ = ClientFlattenningState::NotRequired;
901 return false;
902 }
903
904 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
905 flattenning_state_ = ClientFlattenningState::Flattened;
906 return true;
907 }
908
909 vsync_flattening_en_ = true;
910 vsync_worker_.VSyncControl(true);
911 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
912 return false;
913}
914
915void HwcDisplay::ProcessFlatenningVsyncInternal() {
916 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
917 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
918 hwc2_->refresh_callback_.first != nullptr &&
919 hwc2_->refresh_callback_.second != nullptr) {
920 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
921 vsync_flattening_en_ = false;
922 }
923}
924
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200925} // namespace android