blob: f778e228a4d7b97554ea45fc58c7a3c3802e5884 [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 Stratiienko9362cef2022-02-02 09:53:50 +0200500 /* Store plan to ensure shared planes won't be stolen by other display
501 * in between of ValidateDisplay() and PresentDisplay() calls
502 */
503 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
504 std::move(composition_layers));
505 if (!current_plan_) {
506 if (!a_args.test_only) {
507 ALOGE("Failed to create DrmKmsPlan");
508 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200509 return HWC2::Error::BadConfig;
510 }
511
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200512 a_args.composition = current_plan_;
513
514 int ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515
516 if (ret) {
517 if (!a_args.test_only)
518 ALOGE("Failed to apply the frame composition ret=%d", ret);
519 return HWC2::Error::BadParameter;
520 }
521
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200522 if (mode_update_commited_) {
523 staged_mode_.reset();
524 vsync_tracking_en_ = false;
525 if (last_vsync_ts_ != 0) {
526 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
527 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
528 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200529 }
530
531 return HWC2::Error::None;
532}
533
534/* Find API details at:
535 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
536 */
537HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200538 if (IsInHeadlessMode()) {
539 *present_fence = -1;
540 return HWC2::Error::None;
541 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200542 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543
544 ++total_stats_.total_frames_;
545
546 AtomicCommitArgs a_args{};
547 ret = CreateComposition(a_args);
548
549 if (ret != HWC2::Error::None)
550 ++total_stats_.failed_kms_present_;
551
552 if (ret == HWC2::Error::BadLayer) {
553 // Can we really have no client or device layers?
554 *present_fence = -1;
555 return HWC2::Error::None;
556 }
557 if (ret != HWC2::Error::None)
558 return ret;
559
560 *present_fence = a_args.out_fence.Release();
561
562 ++frame_no_;
563 return HWC2::Error::None;
564}
565
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200566HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
567 int64_t change_time) {
568 if (configs_.hwc_configs.count(config) == 0) {
569 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200570 return HWC2::Error::BadConfig;
571 }
572
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200573 staged_mode_ = configs_.hwc_configs[config].mode;
574 staged_mode_change_time_ = change_time;
575 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200576
577 return HWC2::Error::None;
578}
579
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200580HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
581 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
582}
583
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200584/* Find API details at:
585 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
586 */
587HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
588 int32_t acquire_fence,
589 int32_t dataspace,
590 hwc_region_t /*damage*/) {
591 client_layer_.SetLayerBuffer(target, acquire_fence);
592 client_layer_.SetLayerDataspace(dataspace);
593
594 /*
595 * target can be nullptr, this does mean the Composer Service is calling
596 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
597 * 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
598 */
599 if (target == nullptr) {
600 return HWC2::Error::None;
601 }
602
603 /* TODO: Do not update source_crop every call.
604 * It makes sense to do it once after every hotplug event. */
605 HwcDrmBo bo{};
606 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
607
608 hwc_frect_t source_crop = {.left = 0.0F,
609 .top = 0.0F,
610 .right = static_cast<float>(bo.width),
611 .bottom = static_cast<float>(bo.height)};
612 client_layer_.SetLayerSourceCrop(source_crop);
613
614 return HWC2::Error::None;
615}
616
617HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
618 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
619 return HWC2::Error::BadParameter;
620
621 if (mode != HAL_COLOR_MODE_NATIVE)
622 return HWC2::Error::Unsupported;
623
624 color_mode_ = mode;
625 return HWC2::Error::None;
626}
627
628HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
629 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
630 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
631 return HWC2::Error::BadParameter;
632
633 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
634 return HWC2::Error::BadParameter;
635
636 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
637 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
638 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
639
640 return HWC2::Error::None;
641}
642
643HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
644 int32_t /*release_fence*/) {
645 // TODO(nobody): Need virtual display support
646 return HWC2::Error::Unsupported;
647}
648
649HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200650 if (IsInHeadlessMode()) {
651 return HWC2::Error::None;
652 }
653
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200654 auto mode = static_cast<HWC2::PowerMode>(mode_in);
655 AtomicCommitArgs a_args{};
656
657 switch (mode) {
658 case HWC2::PowerMode::Off:
659 a_args.active = false;
660 break;
661 case HWC2::PowerMode::On:
662 /*
663 * Setting the display to active before we have a composition
664 * can break some drivers, so skip setting a_args.active to
665 * true, as the next composition frame will implicitly activate
666 * the display
667 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200668 return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200669 ? HWC2::Error::None
670 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200671 break;
672 case HWC2::PowerMode::Doze:
673 case HWC2::PowerMode::DozeSuspend:
674 return HWC2::Error::Unsupported;
675 default:
676 ALOGI("Power mode %d is unsupported\n", mode);
677 return HWC2::Error::BadParameter;
678 };
679
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200680 int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200681 if (err) {
682 ALOGE("Failed to apply the dpms composition err=%d", err);
683 return HWC2::Error::BadParameter;
684 }
685 return HWC2::Error::None;
686}
687
688HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200689 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
690 if (vsync_event_en_) {
691 vsync_worker_.VSyncControl(true);
692 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200693 return HWC2::Error::None;
694}
695
696HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
697 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200698 if (IsInHeadlessMode()) {
699 *num_types = *num_requests = 0;
700 return HWC2::Error::None;
701 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200702 return backend_->ValidateDisplay(this, num_types, num_requests);
703}
704
705std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
706 std::vector<HwcLayer *> ordered_layers;
707 ordered_layers.reserve(layers_.size());
708
709 for (auto &[handle, layer] : layers_) {
710 ordered_layers.emplace_back(&layer);
711 }
712
713 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
714 [](const HwcLayer *lhs, const HwcLayer *rhs) {
715 return lhs->GetZOrder() < rhs->GetZOrder();
716 });
717
718 return ordered_layers;
719}
720
Roman Stratiienko099c3112022-01-20 11:50:54 +0200721HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
722 uint32_t *outVsyncPeriod /* ns */) {
723 return GetDisplayAttribute(configs_.active_config_id,
724 HWC2_ATTRIBUTE_VSYNC_PERIOD,
725 (int32_t *)(outVsyncPeriod));
726}
727
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200728#if PLATFORM_SDK_VERSION > 29
729HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200730 if (IsInHeadlessMode()) {
731 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
732 return HWC2::Error::None;
733 }
734 /* Primary display should be always internal,
735 * otherwise SF will be unhappy and will crash
736 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200737 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200738 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200739 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200740 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
741 else
742 return HWC2::Error::BadConfig;
743
744 return HWC2::Error::None;
745}
746
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200747HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200748 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200749 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
750 hwc_vsync_period_change_timeline_t *outTimeline) {
751 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
752 return HWC2::Error::BadParameter;
753 }
754
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200755 uint32_t current_vsync_period{};
756 GetDisplayVsyncPeriod(&current_vsync_period);
757
758 if (vsyncPeriodChangeConstraints->seamlessRequired) {
759 return HWC2::Error::SeamlessNotAllowed;
760 }
761
762 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
763 ->desiredTimeNanos -
764 current_vsync_period;
765 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
766 if (ret != HWC2::Error::None) {
767 return ret;
768 }
769
770 outTimeline->refreshRequired = true;
771 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
772 ->desiredTimeNanos;
773
774 last_vsync_ts_ = 0;
775 vsync_tracking_en_ = true;
776 vsync_worker_.VSyncControl(true);
777
778 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200779}
780
781HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
782 return HWC2::Error::Unsupported;
783}
784
785HWC2::Error HwcDisplay::GetSupportedContentTypes(
786 uint32_t *outNumSupportedContentTypes,
787 const uint32_t *outSupportedContentTypes) {
788 if (outSupportedContentTypes == nullptr)
789 *outNumSupportedContentTypes = 0;
790
791 return HWC2::Error::None;
792}
793
794HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
795 if (contentType != HWC2_CONTENT_TYPE_NONE)
796 return HWC2::Error::Unsupported;
797
798 /* TODO: Map to the DRM Connector property:
799 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
800 */
801
802 return HWC2::Error::None;
803}
804#endif
805
806#if PLATFORM_SDK_VERSION > 28
807HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
808 uint32_t *outDataSize,
809 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200810 if (IsInHeadlessMode()) {
811 return HWC2::Error::None;
812 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200813 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200814
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200815 *outPort = handle_ - 1;
816
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200817 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200818 if (outData == nullptr) {
819 *outDataSize = 0;
820 }
821 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200822 }
823
824 if (outData) {
825 *outDataSize = std::min(*outDataSize, blob->length);
826 memcpy(outData, blob->data, *outDataSize);
827 } else {
828 *outDataSize = blob->length;
829 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200830
831 return HWC2::Error::None;
832}
833
834HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
835 uint32_t * /*outCapabilities*/) {
836 if (outNumCapabilities == nullptr) {
837 return HWC2::Error::BadParameter;
838 }
839
840 *outNumCapabilities = 0;
841
842 return HWC2::Error::None;
843}
844
845HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
846 *supported = false;
847 return HWC2::Error::None;
848}
849
850HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
851 return HWC2::Error::Unsupported;
852}
853
854#endif /* PLATFORM_SDK_VERSION > 28 */
855
856#if PLATFORM_SDK_VERSION > 27
857
858HWC2::Error HwcDisplay::GetRenderIntents(
859 int32_t mode, uint32_t *outNumIntents,
860 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
861 if (mode != HAL_COLOR_MODE_NATIVE) {
862 return HWC2::Error::BadParameter;
863 }
864
865 if (outIntents == nullptr) {
866 *outNumIntents = 1;
867 return HWC2::Error::None;
868 }
869 *outNumIntents = 1;
870 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
871 return HWC2::Error::None;
872}
873
874HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
875 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
876 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
877 return HWC2::Error::BadParameter;
878
879 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
880 return HWC2::Error::BadParameter;
881
882 if (mode != HAL_COLOR_MODE_NATIVE)
883 return HWC2::Error::Unsupported;
884
885 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
886 return HWC2::Error::Unsupported;
887
888 color_mode_ = mode;
889 return HWC2::Error::None;
890}
891
892#endif /* PLATFORM_SDK_VERSION > 27 */
893
894const Backend *HwcDisplay::backend() const {
895 return backend_.get();
896}
897
898void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
899 backend_ = std::move(backend);
900}
901
Roman Stratiienko099c3112022-01-20 11:50:54 +0200902/* returns true if composition should be sent to client */
903bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
904 int flattenning_state = flattenning_state_;
905 if (flattenning_state == ClientFlattenningState::Disabled) {
906 return false;
907 }
908
909 if (skip) {
910 flattenning_state_ = ClientFlattenningState::NotRequired;
911 return false;
912 }
913
914 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
915 flattenning_state_ = ClientFlattenningState::Flattened;
916 return true;
917 }
918
919 vsync_flattening_en_ = true;
920 vsync_worker_.VSyncControl(true);
921 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
922 return false;
923}
924
925void HwcDisplay::ProcessFlatenningVsyncInternal() {
926 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
927 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
928 hwc2_->refresh_callback_.first != nullptr &&
929 hwc2_->refresh_callback_.second != nullptr) {
930 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
931 vsync_flattening_en_ = false;
932 }
933}
934
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200935} // namespace android