blob: d7b753a1972dfe346c43c38961385c390b421f04 [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
123void HwcDisplay::ClearDisplay() {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200124 if (IsInHeadlessMode()) {
125 ALOGE("%s: Headless mode, should never reach here: ", __func__);
126 return;
127 }
128
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200129 AtomicCommitArgs a_args = {.clear_active_composition = true};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200130 pipeline_->compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200131}
132
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200133HWC2::Error HwcDisplay::Init() {
134 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200135 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200136 if (vsync_event_en_) {
137 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200138 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200139 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200141 if (vsync_flattening_en_) {
142 ProcessFlatenningVsyncInternal();
143 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200144 if (vsync_tracking_en_) {
145 last_vsync_ts_ = timestamp;
146 }
147 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200148 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200149 }
150 });
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200151 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200152 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200153 return HWC2::Error::BadDisplay;
154 }
155
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200156 if (!IsInHeadlessMode()) {
157 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
158 if (ret) {
159 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
160 return HWC2::Error::BadDisplay;
161 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200162 }
163
164 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
165
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200166 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200167}
168
169HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200170 HWC2::Error err{};
171 if (!IsInHeadlessMode()) {
172 err = configs_.Update(*pipeline_->connector->Get());
173 } else {
174 configs_.FillHeadless();
175 }
176 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200177 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200178 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200179
Roman Stratiienko0137f862022-01-04 18:27:40 +0200180 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200181}
182
183HWC2::Error HwcDisplay::AcceptDisplayChanges() {
184 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
185 l.second.AcceptTypeChange();
186 return HWC2::Error::None;
187}
188
189HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
190 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
191 *layer = static_cast<hwc2_layer_t>(layer_idx_);
192 ++layer_idx_;
193 return HWC2::Error::None;
194}
195
196HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200197 if (!get_layer(layer)) {
198 /* Primary display don't send unplug event, instead it replaces
199 * display to headless or to another one and sends Plug event to the
200 * SF. SF can't distinguish this case from virtualized display size
201 * change case and will destroy previously used layers. If we will return
202 * BadLayer, service will print errors to the logcat.
203 *
204 * Nevertheless VTS is trying to destroy 1st layer without adding any
205 * layers prior to that, than it checks for BadLayer result. So we
206 * numbering the layers starting from 2, and use index 1 to catch VTS client
207 * to return BadLayer, making VTS pass.
208 */
209 if (layers_.empty() && layer != 1) {
210 return HWC2::Error::None;
211 }
212
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200213 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200214 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215
216 layers_.erase(layer);
217 return HWC2::Error::None;
218}
219
220HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200221 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222 return HWC2::Error::BadConfig;
223
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200224 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200225 return HWC2::Error::None;
226}
227
228HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
229 hwc2_layer_t *layers,
230 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200231 if (IsInHeadlessMode()) {
232 *num_elements = 0;
233 return HWC2::Error::None;
234 }
235
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200236 uint32_t num_changes = 0;
237 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
238 if (l.second.IsTypeChanged()) {
239 if (layers && num_changes < *num_elements)
240 layers[num_changes] = l.first;
241 if (types && num_changes < *num_elements)
242 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
243 ++num_changes;
244 }
245 }
246 if (!layers && !types)
247 *num_elements = num_changes;
248 return HWC2::Error::None;
249}
250
251HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
252 int32_t /*format*/,
253 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200254 if (IsInHeadlessMode()) {
255 return HWC2::Error::None;
256 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200257
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200258 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
259 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
260
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200261 if (width < min.first || height < min.second)
262 return HWC2::Error::Unsupported;
263
264 if (width > max.first || height > max.second)
265 return HWC2::Error::Unsupported;
266
267 if (dataspace != HAL_DATASPACE_UNKNOWN)
268 return HWC2::Error::Unsupported;
269
270 // TODO(nobody): Validate format can be handled by either GL or planes
271 return HWC2::Error::None;
272}
273
274HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
275 if (!modes)
276 *num_modes = 1;
277
278 if (modes)
279 *modes = HAL_COLOR_MODE_NATIVE;
280
281 return HWC2::Error::None;
282}
283
284HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
285 int32_t attribute_in,
286 int32_t *value) {
287 int conf = static_cast<int>(config);
288
Roman Stratiienko0137f862022-01-04 18:27:40 +0200289 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200290 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200291 return HWC2::Error::BadConfig;
292 }
293
Roman Stratiienko0137f862022-01-04 18:27:40 +0200294 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200295
296 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200297 uint32_t mm_width = configs_.mm_width;
298 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200299 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
300 switch (attribute) {
301 case HWC2::Attribute::Width:
302 *value = static_cast<int>(hwc_config.mode.h_display());
303 break;
304 case HWC2::Attribute::Height:
305 *value = static_cast<int>(hwc_config.mode.v_display());
306 break;
307 case HWC2::Attribute::VsyncPeriod:
308 // in nanoseconds
309 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
310 break;
311 case HWC2::Attribute::DpiX:
312 // Dots per 1000 inches
313 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
314 kUmPerInch / mm_width)
315 : -1;
316 break;
317 case HWC2::Attribute::DpiY:
318 // Dots per 1000 inches
319 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
320 kUmPerInch / mm_height)
321 : -1;
322 break;
323#if PLATFORM_SDK_VERSION > 29
324 case HWC2::Attribute::ConfigGroup:
325 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
326 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200327 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 break;
329#endif
330 default:
331 *value = -1;
332 return HWC2::Error::BadConfig;
333 }
334 return HWC2::Error::None;
335}
336
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200337HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
338 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200340 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 if (hwc_config.second.disabled) {
342 continue;
343 }
344
345 if (configs != nullptr) {
346 if (idx >= *num_configs) {
347 break;
348 }
349 configs[idx] = hwc_config.second.id;
350 }
351
352 idx++;
353 }
354 *num_configs = idx;
355 return HWC2::Error::None;
356}
357
358HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
359 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200360 if (IsInHeadlessMode()) {
361 stream << "null-display";
362 } else {
363 stream << "display-" << GetPipe().connector->Get()->GetId();
364 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200365 std::string string = stream.str();
366 size_t length = string.length();
367 if (!name) {
368 *size = length;
369 return HWC2::Error::None;
370 }
371
372 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
373 strncpy(name, string.c_str(), *size);
374 return HWC2::Error::None;
375}
376
377HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
378 uint32_t *num_elements,
379 hwc2_layer_t * /*layers*/,
380 int32_t * /*layer_requests*/) {
381 // TODO(nobody): I think virtual display should request
382 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
383 *num_elements = 0;
384 return HWC2::Error::None;
385}
386
387HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
388 *type = static_cast<int32_t>(type_);
389 return HWC2::Error::None;
390}
391
392HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
393 *support = 0;
394 return HWC2::Error::None;
395}
396
397HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
398 int32_t * /*types*/,
399 float * /*max_luminance*/,
400 float * /*max_average_luminance*/,
401 float * /*min_luminance*/) {
402 *num_types = 0;
403 return HWC2::Error::None;
404}
405
406/* Find API details at:
407 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
408 */
409HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
410 hwc2_layer_t *layers,
411 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200412 if (IsInHeadlessMode()) {
413 *num_elements = 0;
414 return HWC2::Error::None;
415 }
416
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200417 uint32_t num_layers = 0;
418
419 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
420 ++num_layers;
421 if (layers == nullptr || fences == nullptr)
422 continue;
423
424 if (num_layers > *num_elements) {
425 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
426 return HWC2::Error::None;
427 }
428
429 layers[num_layers - 1] = l.first;
430 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
431 }
432 *num_elements = num_layers;
433 return HWC2::Error::None;
434}
435
436HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200437 if (IsInHeadlessMode()) {
438 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
439 return HWC2::Error::None;
440 }
441
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200442 int PrevModeVsyncPeriodNs = static_cast<int>(
443 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
444
445 auto mode_update_commited_ = false;
446 if (staged_mode_ &&
447 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
448 client_layer_.SetLayerDisplayFrame(
449 (hwc_rect_t){.left = 0,
450 .top = 0,
451 .right = static_cast<int>(staged_mode_->h_display()),
452 .bottom = static_cast<int>(staged_mode_->v_display())});
453
454 configs_.active_config_id = staged_mode_config_id_;
455
456 a_args.display_mode = *staged_mode_;
457 if (!a_args.test_only) {
458 mode_update_commited_ = true;
459 }
460 }
461
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200462 // order the layers by z-order
463 bool use_client_layer = false;
464 uint32_t client_z_order = UINT32_MAX;
465 std::map<uint32_t, HwcLayer *> z_map;
466 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
467 switch (l.second.GetValidatedType()) {
468 case HWC2::Composition::Device:
469 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
470 break;
471 case HWC2::Composition::Client:
472 // Place it at the z_order of the lowest client layer
473 use_client_layer = true;
474 client_z_order = std::min(client_z_order, l.second.GetZOrder());
475 break;
476 default:
477 continue;
478 }
479 }
480 if (use_client_layer)
481 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
482
483 if (z_map.empty())
484 return HWC2::Error::BadLayer;
485
486 std::vector<DrmHwcLayer> composition_layers;
487
488 // now that they're ordered by z, add them to the composition
489 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
490 DrmHwcLayer layer;
491 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200492 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200493 if (ret) {
494 ALOGE("Failed to import layer, ret=%d", ret);
495 return HWC2::Error::NoResources;
496 }
497 composition_layers.emplace_back(std::move(layer));
498 }
499
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200500 auto composition = std::make_shared<DrmDisplayComposition>(
501 GetPipe().crtc->Get());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502
503 // TODO(nobody): Don't always assume geometry changed
504 int ret = composition->SetLayers(composition_layers.data(),
505 composition_layers.size());
506 if (ret) {
507 ALOGE("Failed to set layers in the composition ret=%d", ret);
508 return HWC2::Error::BadLayer;
509 }
510
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200511 std::vector<DrmPlane *> primary_planes;
512 primary_planes.emplace_back(pipeline_->primary_plane->Get());
513 std::vector<DrmPlane *> overlay_planes;
514 for (const auto &owned_plane : pipeline_->overlay_planes) {
515 overlay_planes.emplace_back(owned_plane->Get());
516 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200517 ret = composition->Plan(&primary_planes, &overlay_planes);
518 if (ret) {
519 ALOGV("Failed to plan the composition ret=%d", ret);
520 return HWC2::Error::BadConfig;
521 }
522
523 a_args.composition = composition;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200524 ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200525
526 if (ret) {
527 if (!a_args.test_only)
528 ALOGE("Failed to apply the frame composition ret=%d", ret);
529 return HWC2::Error::BadParameter;
530 }
531
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200532 if (mode_update_commited_) {
533 staged_mode_.reset();
534 vsync_tracking_en_ = false;
535 if (last_vsync_ts_ != 0) {
536 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
537 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
538 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200539 }
540
541 return HWC2::Error::None;
542}
543
544/* Find API details at:
545 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
546 */
547HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200548 if (IsInHeadlessMode()) {
549 *present_fence = -1;
550 return HWC2::Error::None;
551 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200552 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200553
554 ++total_stats_.total_frames_;
555
556 AtomicCommitArgs a_args{};
557 ret = CreateComposition(a_args);
558
559 if (ret != HWC2::Error::None)
560 ++total_stats_.failed_kms_present_;
561
562 if (ret == HWC2::Error::BadLayer) {
563 // Can we really have no client or device layers?
564 *present_fence = -1;
565 return HWC2::Error::None;
566 }
567 if (ret != HWC2::Error::None)
568 return ret;
569
570 *present_fence = a_args.out_fence.Release();
571
572 ++frame_no_;
573 return HWC2::Error::None;
574}
575
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200576HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
577 int64_t change_time) {
578 if (configs_.hwc_configs.count(config) == 0) {
579 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200580 return HWC2::Error::BadConfig;
581 }
582
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200583 staged_mode_ = configs_.hwc_configs[config].mode;
584 staged_mode_change_time_ = change_time;
585 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200586
587 return HWC2::Error::None;
588}
589
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200590HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
591 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
592}
593
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200594/* Find API details at:
595 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
596 */
597HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
598 int32_t acquire_fence,
599 int32_t dataspace,
600 hwc_region_t /*damage*/) {
601 client_layer_.SetLayerBuffer(target, acquire_fence);
602 client_layer_.SetLayerDataspace(dataspace);
603
604 /*
605 * target can be nullptr, this does mean the Composer Service is calling
606 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
607 * 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
608 */
609 if (target == nullptr) {
610 return HWC2::Error::None;
611 }
612
613 /* TODO: Do not update source_crop every call.
614 * It makes sense to do it once after every hotplug event. */
615 HwcDrmBo bo{};
616 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
617
618 hwc_frect_t source_crop = {.left = 0.0F,
619 .top = 0.0F,
620 .right = static_cast<float>(bo.width),
621 .bottom = static_cast<float>(bo.height)};
622 client_layer_.SetLayerSourceCrop(source_crop);
623
624 return HWC2::Error::None;
625}
626
627HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
628 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
629 return HWC2::Error::BadParameter;
630
631 if (mode != HAL_COLOR_MODE_NATIVE)
632 return HWC2::Error::Unsupported;
633
634 color_mode_ = mode;
635 return HWC2::Error::None;
636}
637
638HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
639 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
640 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
641 return HWC2::Error::BadParameter;
642
643 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
644 return HWC2::Error::BadParameter;
645
646 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
647 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
648 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
649
650 return HWC2::Error::None;
651}
652
653HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
654 int32_t /*release_fence*/) {
655 // TODO(nobody): Need virtual display support
656 return HWC2::Error::Unsupported;
657}
658
659HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200660 if (IsInHeadlessMode()) {
661 return HWC2::Error::None;
662 }
663
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200664 auto mode = static_cast<HWC2::PowerMode>(mode_in);
665 AtomicCommitArgs a_args{};
666
667 switch (mode) {
668 case HWC2::PowerMode::Off:
669 a_args.active = false;
670 break;
671 case HWC2::PowerMode::On:
672 /*
673 * Setting the display to active before we have a composition
674 * can break some drivers, so skip setting a_args.active to
675 * true, as the next composition frame will implicitly activate
676 * the display
677 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200678 return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200679 ? HWC2::Error::None
680 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200681 break;
682 case HWC2::PowerMode::Doze:
683 case HWC2::PowerMode::DozeSuspend:
684 return HWC2::Error::Unsupported;
685 default:
686 ALOGI("Power mode %d is unsupported\n", mode);
687 return HWC2::Error::BadParameter;
688 };
689
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200690 int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200691 if (err) {
692 ALOGE("Failed to apply the dpms composition err=%d", err);
693 return HWC2::Error::BadParameter;
694 }
695 return HWC2::Error::None;
696}
697
698HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200699 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
700 if (vsync_event_en_) {
701 vsync_worker_.VSyncControl(true);
702 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200703 return HWC2::Error::None;
704}
705
706HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
707 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200708 if (IsInHeadlessMode()) {
709 *num_types = *num_requests = 0;
710 return HWC2::Error::None;
711 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200712 return backend_->ValidateDisplay(this, num_types, num_requests);
713}
714
715std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
716 std::vector<HwcLayer *> ordered_layers;
717 ordered_layers.reserve(layers_.size());
718
719 for (auto &[handle, layer] : layers_) {
720 ordered_layers.emplace_back(&layer);
721 }
722
723 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
724 [](const HwcLayer *lhs, const HwcLayer *rhs) {
725 return lhs->GetZOrder() < rhs->GetZOrder();
726 });
727
728 return ordered_layers;
729}
730
Roman Stratiienko099c3112022-01-20 11:50:54 +0200731HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
732 uint32_t *outVsyncPeriod /* ns */) {
733 return GetDisplayAttribute(configs_.active_config_id,
734 HWC2_ATTRIBUTE_VSYNC_PERIOD,
735 (int32_t *)(outVsyncPeriod));
736}
737
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200738#if PLATFORM_SDK_VERSION > 29
739HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200740 if (IsInHeadlessMode()) {
741 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
742 return HWC2::Error::None;
743 }
744 /* Primary display should be always internal,
745 * otherwise SF will be unhappy and will crash
746 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200747 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200749 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200750 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
751 else
752 return HWC2::Error::BadConfig;
753
754 return HWC2::Error::None;
755}
756
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200757HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200758 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200759 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
760 hwc_vsync_period_change_timeline_t *outTimeline) {
761 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
762 return HWC2::Error::BadParameter;
763 }
764
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200765 uint32_t current_vsync_period{};
766 GetDisplayVsyncPeriod(&current_vsync_period);
767
768 if (vsyncPeriodChangeConstraints->seamlessRequired) {
769 return HWC2::Error::SeamlessNotAllowed;
770 }
771
772 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
773 ->desiredTimeNanos -
774 current_vsync_period;
775 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
776 if (ret != HWC2::Error::None) {
777 return ret;
778 }
779
780 outTimeline->refreshRequired = true;
781 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
782 ->desiredTimeNanos;
783
784 last_vsync_ts_ = 0;
785 vsync_tracking_en_ = true;
786 vsync_worker_.VSyncControl(true);
787
788 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200789}
790
791HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
792 return HWC2::Error::Unsupported;
793}
794
795HWC2::Error HwcDisplay::GetSupportedContentTypes(
796 uint32_t *outNumSupportedContentTypes,
797 const uint32_t *outSupportedContentTypes) {
798 if (outSupportedContentTypes == nullptr)
799 *outNumSupportedContentTypes = 0;
800
801 return HWC2::Error::None;
802}
803
804HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
805 if (contentType != HWC2_CONTENT_TYPE_NONE)
806 return HWC2::Error::Unsupported;
807
808 /* TODO: Map to the DRM Connector property:
809 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
810 */
811
812 return HWC2::Error::None;
813}
814#endif
815
816#if PLATFORM_SDK_VERSION > 28
817HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
818 uint32_t *outDataSize,
819 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200820 if (IsInHeadlessMode()) {
821 return HWC2::Error::None;
822 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200823 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200824
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200825 *outPort = handle_ - 1;
826
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200827 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200828 if (outData == nullptr) {
829 *outDataSize = 0;
830 }
831 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200832 }
833
834 if (outData) {
835 *outDataSize = std::min(*outDataSize, blob->length);
836 memcpy(outData, blob->data, *outDataSize);
837 } else {
838 *outDataSize = blob->length;
839 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200840
841 return HWC2::Error::None;
842}
843
844HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
845 uint32_t * /*outCapabilities*/) {
846 if (outNumCapabilities == nullptr) {
847 return HWC2::Error::BadParameter;
848 }
849
850 *outNumCapabilities = 0;
851
852 return HWC2::Error::None;
853}
854
855HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
856 *supported = false;
857 return HWC2::Error::None;
858}
859
860HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
861 return HWC2::Error::Unsupported;
862}
863
864#endif /* PLATFORM_SDK_VERSION > 28 */
865
866#if PLATFORM_SDK_VERSION > 27
867
868HWC2::Error HwcDisplay::GetRenderIntents(
869 int32_t mode, uint32_t *outNumIntents,
870 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
871 if (mode != HAL_COLOR_MODE_NATIVE) {
872 return HWC2::Error::BadParameter;
873 }
874
875 if (outIntents == nullptr) {
876 *outNumIntents = 1;
877 return HWC2::Error::None;
878 }
879 *outNumIntents = 1;
880 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
881 return HWC2::Error::None;
882}
883
884HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
885 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
886 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
887 return HWC2::Error::BadParameter;
888
889 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
890 return HWC2::Error::BadParameter;
891
892 if (mode != HAL_COLOR_MODE_NATIVE)
893 return HWC2::Error::Unsupported;
894
895 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
896 return HWC2::Error::Unsupported;
897
898 color_mode_ = mode;
899 return HWC2::Error::None;
900}
901
902#endif /* PLATFORM_SDK_VERSION > 27 */
903
904const Backend *HwcDisplay::backend() const {
905 return backend_.get();
906}
907
908void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
909 backend_ = std::move(backend);
910}
911
Roman Stratiienko099c3112022-01-20 11:50:54 +0200912/* returns true if composition should be sent to client */
913bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
914 int flattenning_state = flattenning_state_;
915 if (flattenning_state == ClientFlattenningState::Disabled) {
916 return false;
917 }
918
919 if (skip) {
920 flattenning_state_ = ClientFlattenningState::NotRequired;
921 return false;
922 }
923
924 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
925 flattenning_state_ = ClientFlattenningState::Flattened;
926 return true;
927 }
928
929 vsync_flattening_en_ = true;
930 vsync_worker_.VSyncControl(true);
931 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
932 return false;
933}
934
935void HwcDisplay::ProcessFlatenningVsyncInternal() {
936 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
937 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
938 hwc2_->refresh_callback_.first != nullptr &&
939 hwc2_->refresh_callback_.second != nullptr) {
940 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
941 vsync_flattening_en_ = false;
942 }
943}
944
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200945} // namespace android