blob: 7279c023cdeaf4e054f391c2a072d254f37267de [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "hwc-display"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
22#include "DrmHwcTwo.h"
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020023#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020024#include "backend/BackendManager.h"
25#include "bufferinfo/BufferInfoGetter.h"
26#include "utils/log.h"
27#include "utils/properties.h"
28
29namespace android {
30
31std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
32 if (delta.total_pixops_ == 0)
33 return "No stats yet";
34 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
35
36 std::stringstream ss;
37 ss << " Total frames count: " << delta.total_frames_ << "\n"
38 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
39 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
40 << ((delta.failed_kms_present_ > 0)
41 ? " !!! Internal failure, FIX it please\n"
42 : "")
43 << " Flattened frames: " << delta.frames_flattened_ << "\n"
44 << " Pixel operations (free units)"
45 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
46 << "]\n"
47 << " Composition efficiency: " << ratio;
48
49 return ss.str();
50}
51
52std::string HwcDisplay::Dump() {
53 std::string flattening_state_str;
54 switch (flattenning_state_) {
55 case ClientFlattenningState::Disabled:
56 flattening_state_str = "Disabled";
57 break;
58 case ClientFlattenningState::NotRequired:
59 flattening_state_str = "Not needed";
60 break;
61 case ClientFlattenningState::Flattened:
62 flattening_state_str = "Active";
63 break;
64 case ClientFlattenningState::ClientRefreshRequested:
65 flattening_state_str = "Refresh requested";
66 break;
67 default:
68 flattening_state_str = std::to_string(flattenning_state_) +
69 " VSync remains";
70 }
71
Roman Stratiienko19c162f2022-02-01 09:35:08 +020072 std::string connector_name = IsInHeadlessMode()
73 ? "NULL-DISPLAY"
74 : GetPipe().connector->Get()->GetName();
75
Roman Stratiienko3627beb2022-01-04 16:02:55 +020076 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020077 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020078 << " Flattening state: " << flattening_state_str << "\n"
79 << "Statistics since system boot:\n"
80 << DumpDelta(total_stats_) << "\n\n"
81 << "Statistics since last dumpsys request:\n"
82 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
83
84 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
85 return ss.str();
86}
87
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020088HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
89 DrmHwcTwo *hwc2)
Roman Stratiienko3627beb2022-01-04 16:02:55 +020090 : hwc2_(hwc2),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020091 handle_(handle),
92 type_(type),
93 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
94 // clang-format off
95 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
96 0.0, 1.0, 0.0, 0.0,
97 0.0, 0.0, 1.0, 0.0,
98 0.0, 0.0, 0.0, 1.0};
99 // clang-format on
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200100}
101
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200102HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200103
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200104void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200105 Deinit();
106
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200107 pipeline_ = pipeline;
108
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200109 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200110 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200111 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
112 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200113 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200114 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200115}
116
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200117void HwcDisplay::Deinit() {
118 if (pipeline_ != nullptr) {
119 AtomicCommitArgs a_args{};
120 a_args.active = false;
121 a_args.composition = std::make_shared<DrmKmsPlan>();
122 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
123
124 vsync_worker_.Init(nullptr, [](int64_t) {});
125 current_plan_.reset();
126 backend_.reset();
127 }
128
129 SetClientTarget(nullptr, -1, 0, {});
130}
131
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200132HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200133 ChosePreferredConfig();
134
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200136 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko099c3112022-01-20 11:50:54 +0200137 if (vsync_event_en_) {
138 uint32_t period_ns{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200139 GetDisplayVsyncPeriod(&period_ns);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200140 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200141 }
Roman Stratiienko099c3112022-01-20 11:50:54 +0200142 if (vsync_flattening_en_) {
143 ProcessFlatenningVsyncInternal();
144 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200145 if (vsync_tracking_en_) {
146 last_vsync_ts_ = timestamp;
147 }
148 if (!vsync_event_en_ && !vsync_flattening_en_ && !vsync_tracking_en_) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200149 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200150 }
151 });
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200152 if (ret && ret != -EALREADY) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200153 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200154 return HWC2::Error::BadDisplay;
155 }
156
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200157 if (!IsInHeadlessMode()) {
158 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
159 if (ret) {
160 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
161 return HWC2::Error::BadDisplay;
162 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200163 }
164
165 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
166
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200167 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200168}
169
170HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200171 HWC2::Error err{};
172 if (!IsInHeadlessMode()) {
173 err = configs_.Update(*pipeline_->connector->Get());
174 } else {
175 configs_.FillHeadless();
176 }
177 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200178 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200179 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200180
Roman Stratiienko0137f862022-01-04 18:27:40 +0200181 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200182}
183
184HWC2::Error HwcDisplay::AcceptDisplayChanges() {
185 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
186 l.second.AcceptTypeChange();
187 return HWC2::Error::None;
188}
189
190HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
191 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
192 *layer = static_cast<hwc2_layer_t>(layer_idx_);
193 ++layer_idx_;
194 return HWC2::Error::None;
195}
196
197HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200198 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201
202 layers_.erase(layer);
203 return HWC2::Error::None;
204}
205
206HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200207 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200208 return HWC2::Error::BadConfig;
209
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200210 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 return HWC2::Error::None;
212}
213
214HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
215 hwc2_layer_t *layers,
216 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200217 if (IsInHeadlessMode()) {
218 *num_elements = 0;
219 return HWC2::Error::None;
220 }
221
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222 uint32_t num_changes = 0;
223 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
224 if (l.second.IsTypeChanged()) {
225 if (layers && num_changes < *num_elements)
226 layers[num_changes] = l.first;
227 if (types && num_changes < *num_elements)
228 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
229 ++num_changes;
230 }
231 }
232 if (!layers && !types)
233 *num_elements = num_changes;
234 return HWC2::Error::None;
235}
236
237HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
238 int32_t /*format*/,
239 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200240 if (IsInHeadlessMode()) {
241 return HWC2::Error::None;
242 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200243
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200244 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
245 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
246
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200247 if (width < min.first || height < min.second)
248 return HWC2::Error::Unsupported;
249
250 if (width > max.first || height > max.second)
251 return HWC2::Error::Unsupported;
252
253 if (dataspace != HAL_DATASPACE_UNKNOWN)
254 return HWC2::Error::Unsupported;
255
256 // TODO(nobody): Validate format can be handled by either GL or planes
257 return HWC2::Error::None;
258}
259
260HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
261 if (!modes)
262 *num_modes = 1;
263
264 if (modes)
265 *modes = HAL_COLOR_MODE_NATIVE;
266
267 return HWC2::Error::None;
268}
269
270HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
271 int32_t attribute_in,
272 int32_t *value) {
273 int conf = static_cast<int>(config);
274
Roman Stratiienko0137f862022-01-04 18:27:40 +0200275 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200276 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200277 return HWC2::Error::BadConfig;
278 }
279
Roman Stratiienko0137f862022-01-04 18:27:40 +0200280 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281
282 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200283 uint32_t mm_width = configs_.mm_width;
284 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
286 switch (attribute) {
287 case HWC2::Attribute::Width:
288 *value = static_cast<int>(hwc_config.mode.h_display());
289 break;
290 case HWC2::Attribute::Height:
291 *value = static_cast<int>(hwc_config.mode.v_display());
292 break;
293 case HWC2::Attribute::VsyncPeriod:
294 // in nanoseconds
295 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
296 break;
297 case HWC2::Attribute::DpiX:
298 // Dots per 1000 inches
299 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
300 kUmPerInch / mm_width)
301 : -1;
302 break;
303 case HWC2::Attribute::DpiY:
304 // Dots per 1000 inches
305 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
306 kUmPerInch / mm_height)
307 : -1;
308 break;
309#if PLATFORM_SDK_VERSION > 29
310 case HWC2::Attribute::ConfigGroup:
311 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
312 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200313 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200314 break;
315#endif
316 default:
317 *value = -1;
318 return HWC2::Error::BadConfig;
319 }
320 return HWC2::Error::None;
321}
322
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200323HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
324 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200325 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200326 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200327 if (hwc_config.second.disabled) {
328 continue;
329 }
330
331 if (configs != nullptr) {
332 if (idx >= *num_configs) {
333 break;
334 }
335 configs[idx] = hwc_config.second.id;
336 }
337
338 idx++;
339 }
340 *num_configs = idx;
341 return HWC2::Error::None;
342}
343
344HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
345 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200346 if (IsInHeadlessMode()) {
347 stream << "null-display";
348 } else {
349 stream << "display-" << GetPipe().connector->Get()->GetId();
350 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200351 std::string string = stream.str();
352 size_t length = string.length();
353 if (!name) {
354 *size = length;
355 return HWC2::Error::None;
356 }
357
358 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
359 strncpy(name, string.c_str(), *size);
360 return HWC2::Error::None;
361}
362
363HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
364 uint32_t *num_elements,
365 hwc2_layer_t * /*layers*/,
366 int32_t * /*layer_requests*/) {
367 // TODO(nobody): I think virtual display should request
368 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
369 *num_elements = 0;
370 return HWC2::Error::None;
371}
372
373HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
374 *type = static_cast<int32_t>(type_);
375 return HWC2::Error::None;
376}
377
378HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
379 *support = 0;
380 return HWC2::Error::None;
381}
382
383HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
384 int32_t * /*types*/,
385 float * /*max_luminance*/,
386 float * /*max_average_luminance*/,
387 float * /*min_luminance*/) {
388 *num_types = 0;
389 return HWC2::Error::None;
390}
391
392/* Find API details at:
393 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
Roman Stratiienkodd214942022-05-03 18:24:49 +0300394 *
395 * Called after PresentDisplay(), CLIENT is expecting release fence for the
396 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200397 */
398HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
399 hwc2_layer_t *layers,
400 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200401 if (IsInHeadlessMode()) {
402 *num_elements = 0;
403 return HWC2::Error::None;
404 }
405
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200406 uint32_t num_layers = 0;
407
Roman Stratiienkodd214942022-05-03 18:24:49 +0300408 for (auto &l : layers_) {
409 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
410 continue;
411 }
412
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200413 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300414
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200415 if (layers == nullptr || fences == nullptr)
416 continue;
417
418 if (num_layers > *num_elements) {
419 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
420 return HWC2::Error::None;
421 }
422
423 layers[num_layers - 1] = l.first;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300424 fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200425 }
426 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300427
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200428 return HWC2::Error::None;
429}
430
431HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200432 if (IsInHeadlessMode()) {
433 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
434 return HWC2::Error::None;
435 }
436
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200437 int PrevModeVsyncPeriodNs = static_cast<int>(
438 1E9 / GetPipe().connector->Get()->GetActiveMode().v_refresh());
439
440 auto mode_update_commited_ = false;
441 if (staged_mode_ &&
442 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
443 client_layer_.SetLayerDisplayFrame(
444 (hwc_rect_t){.left = 0,
445 .top = 0,
446 .right = static_cast<int>(staged_mode_->h_display()),
447 .bottom = static_cast<int>(staged_mode_->v_display())});
448
449 configs_.active_config_id = staged_mode_config_id_;
450
451 a_args.display_mode = *staged_mode_;
452 if (!a_args.test_only) {
453 mode_update_commited_ = true;
454 }
455 }
456
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200457 // order the layers by z-order
458 bool use_client_layer = false;
459 uint32_t client_z_order = UINT32_MAX;
460 std::map<uint32_t, HwcLayer *> z_map;
461 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
462 switch (l.second.GetValidatedType()) {
463 case HWC2::Composition::Device:
464 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
465 break;
466 case HWC2::Composition::Client:
467 // Place it at the z_order of the lowest client layer
468 use_client_layer = true;
469 client_z_order = std::min(client_z_order, l.second.GetZOrder());
470 break;
471 default:
472 continue;
473 }
474 }
475 if (use_client_layer)
476 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
477
478 if (z_map.empty())
479 return HWC2::Error::BadLayer;
480
481 std::vector<DrmHwcLayer> composition_layers;
482
483 // now that they're ordered by z, add them to the composition
484 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
485 DrmHwcLayer layer;
486 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200487 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 if (ret) {
489 ALOGE("Failed to import layer, ret=%d", ret);
490 return HWC2::Error::NoResources;
491 }
492 composition_layers.emplace_back(std::move(layer));
493 }
494
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200495 /* Store plan to ensure shared planes won't be stolen by other display
496 * in between of ValidateDisplay() and PresentDisplay() calls
497 */
498 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
499 std::move(composition_layers));
500 if (!current_plan_) {
501 if (!a_args.test_only) {
502 ALOGE("Failed to create DrmKmsPlan");
503 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504 return HWC2::Error::BadConfig;
505 }
506
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200507 a_args.composition = current_plan_;
508
Roman Stratiienko4e994052022-02-09 17:40:35 +0200509 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510
511 if (ret) {
512 if (!a_args.test_only)
513 ALOGE("Failed to apply the frame composition ret=%d", ret);
514 return HWC2::Error::BadParameter;
515 }
516
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200517 if (mode_update_commited_) {
518 staged_mode_.reset();
519 vsync_tracking_en_ = false;
520 if (last_vsync_ts_ != 0) {
521 hwc2_->SendVsyncPeriodTimingChangedEventToClient(
522 handle_, last_vsync_ts_ + PrevModeVsyncPeriodNs);
523 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200524 }
525
526 return HWC2::Error::None;
527}
528
529/* Find API details at:
530 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
531 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300532HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200533 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300534 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200535 return HWC2::Error::None;
536 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200537 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200538
539 ++total_stats_.total_frames_;
540
541 AtomicCommitArgs a_args{};
542 ret = CreateComposition(a_args);
543
544 if (ret != HWC2::Error::None)
545 ++total_stats_.failed_kms_present_;
546
547 if (ret == HWC2::Error::BadLayer) {
548 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300549 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200550 return HWC2::Error::None;
551 }
552 if (ret != HWC2::Error::None)
553 return ret;
554
Roman Stratiienkodd214942022-05-03 18:24:49 +0300555 this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get());
556 *out_present_fence = a_args.out_fence.Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200557
558 ++frame_no_;
559 return HWC2::Error::None;
560}
561
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200562HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
563 int64_t change_time) {
564 if (configs_.hwc_configs.count(config) == 0) {
565 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200566 return HWC2::Error::BadConfig;
567 }
568
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200569 staged_mode_ = configs_.hwc_configs[config].mode;
570 staged_mode_change_time_ = change_time;
571 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200572
573 return HWC2::Error::None;
574}
575
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200576HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
577 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
578}
579
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200580/* Find API details at:
581 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
582 */
583HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
584 int32_t acquire_fence,
585 int32_t dataspace,
586 hwc_region_t /*damage*/) {
587 client_layer_.SetLayerBuffer(target, acquire_fence);
588 client_layer_.SetLayerDataspace(dataspace);
589
590 /*
591 * target can be nullptr, this does mean the Composer Service is calling
592 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
593 * 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
594 */
595 if (target == nullptr) {
596 return HWC2::Error::None;
597 }
598
599 /* TODO: Do not update source_crop every call.
600 * It makes sense to do it once after every hotplug event. */
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200601 auto bi = BufferInfoGetter::GetInstance()->GetBoInfo(target);
602
603 if (!bi) {
604 return HWC2::Error::BadParameter;
605 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606
607 hwc_frect_t source_crop = {.left = 0.0F,
608 .top = 0.0F,
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200609 .right = static_cast<float>(bi->width),
610 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200611 client_layer_.SetLayerSourceCrop(source_crop);
612
613 return HWC2::Error::None;
614}
615
616HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
617 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
618 return HWC2::Error::BadParameter;
619
620 if (mode != HAL_COLOR_MODE_NATIVE)
621 return HWC2::Error::Unsupported;
622
623 color_mode_ = mode;
624 return HWC2::Error::None;
625}
626
627HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
628 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
629 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
630 return HWC2::Error::BadParameter;
631
632 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
633 return HWC2::Error::BadParameter;
634
635 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
636 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
637 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
638
639 return HWC2::Error::None;
640}
641
642HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
643 int32_t /*release_fence*/) {
644 // TODO(nobody): Need virtual display support
645 return HWC2::Error::Unsupported;
646}
647
648HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
649 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300650
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200651 AtomicCommitArgs a_args{};
652
653 switch (mode) {
654 case HWC2::PowerMode::Off:
655 a_args.active = false;
656 break;
657 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300658 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200659 break;
660 case HWC2::PowerMode::Doze:
661 case HWC2::PowerMode::DozeSuspend:
662 return HWC2::Error::Unsupported;
663 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300664 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200665 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300666 }
667
668 if (IsInHeadlessMode()) {
669 return HWC2::Error::None;
670 }
671
672 if (a_args.active) {
673 /*
674 * Setting the display to active before we have a composition
675 * can break some drivers, so skip setting a_args.active to
676 * true, as the next composition frame will implicitly activate
677 * the display
678 */
679 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
680 ? HWC2::Error::None
681 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200682 };
683
Roman Stratiienko4e994052022-02-09 17:40:35 +0200684 int err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200685 if (err) {
686 ALOGE("Failed to apply the dpms composition err=%d", err);
687 return HWC2::Error::BadParameter;
688 }
689 return HWC2::Error::None;
690}
691
692HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200693 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
694 if (vsync_event_en_) {
695 vsync_worker_.VSyncControl(true);
696 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200697 return HWC2::Error::None;
698}
699
700HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
701 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200702 if (IsInHeadlessMode()) {
703 *num_types = *num_requests = 0;
704 return HWC2::Error::None;
705 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300706
707 /* In current drm_hwc design in case previous frame layer was not validated as
708 * a CLIENT, it is used by display controller (Front buffer). We have to store
709 * this state to provide the CLIENT with the release fences for such buffers.
710 */
711 for (auto &l : layers_) {
712 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
713 HWC2::Composition::Client);
714 }
715
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200716 return backend_->ValidateDisplay(this, num_types, num_requests);
717}
718
719std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
720 std::vector<HwcLayer *> ordered_layers;
721 ordered_layers.reserve(layers_.size());
722
723 for (auto &[handle, layer] : layers_) {
724 ordered_layers.emplace_back(&layer);
725 }
726
727 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
728 [](const HwcLayer *lhs, const HwcLayer *rhs) {
729 return lhs->GetZOrder() < rhs->GetZOrder();
730 });
731
732 return ordered_layers;
733}
734
Roman Stratiienko099c3112022-01-20 11:50:54 +0200735HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
736 uint32_t *outVsyncPeriod /* ns */) {
737 return GetDisplayAttribute(configs_.active_config_id,
738 HWC2_ATTRIBUTE_VSYNC_PERIOD,
739 (int32_t *)(outVsyncPeriod));
740}
741
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200742#if PLATFORM_SDK_VERSION > 29
743HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200744 if (IsInHeadlessMode()) {
745 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
746 return HWC2::Error::None;
747 }
748 /* Primary display should be always internal,
749 * otherwise SF will be unhappy and will crash
750 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200751 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200752 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200753 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200754 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
755 else
756 return HWC2::Error::BadConfig;
757
758 return HWC2::Error::None;
759}
760
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200761HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200762 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200763 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
764 hwc_vsync_period_change_timeline_t *outTimeline) {
765 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
766 return HWC2::Error::BadParameter;
767 }
768
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200769 uint32_t current_vsync_period{};
770 GetDisplayVsyncPeriod(&current_vsync_period);
771
772 if (vsyncPeriodChangeConstraints->seamlessRequired) {
773 return HWC2::Error::SeamlessNotAllowed;
774 }
775
776 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
777 ->desiredTimeNanos -
778 current_vsync_period;
779 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
780 if (ret != HWC2::Error::None) {
781 return ret;
782 }
783
784 outTimeline->refreshRequired = true;
785 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
786 ->desiredTimeNanos;
787
788 last_vsync_ts_ = 0;
789 vsync_tracking_en_ = true;
790 vsync_worker_.VSyncControl(true);
791
792 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200793}
794
795HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
796 return HWC2::Error::Unsupported;
797}
798
799HWC2::Error HwcDisplay::GetSupportedContentTypes(
800 uint32_t *outNumSupportedContentTypes,
801 const uint32_t *outSupportedContentTypes) {
802 if (outSupportedContentTypes == nullptr)
803 *outNumSupportedContentTypes = 0;
804
805 return HWC2::Error::None;
806}
807
808HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
809 if (contentType != HWC2_CONTENT_TYPE_NONE)
810 return HWC2::Error::Unsupported;
811
812 /* TODO: Map to the DRM Connector property:
813 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
814 */
815
816 return HWC2::Error::None;
817}
818#endif
819
820#if PLATFORM_SDK_VERSION > 28
821HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
822 uint32_t *outDataSize,
823 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200824 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300825 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200826 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300827
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200828 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200829 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300830 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200831 }
832
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300833 *outPort = handle_; /* TDOD(nobody): What should be here? */
834
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200835 if (outData) {
836 *outDataSize = std::min(*outDataSize, blob->length);
837 memcpy(outData, blob->data, *outDataSize);
838 } else {
839 *outDataSize = blob->length;
840 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200841
842 return HWC2::Error::None;
843}
844
845HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
846 uint32_t * /*outCapabilities*/) {
847 if (outNumCapabilities == nullptr) {
848 return HWC2::Error::BadParameter;
849 }
850
851 *outNumCapabilities = 0;
852
853 return HWC2::Error::None;
854}
855
856HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
857 *supported = false;
858 return HWC2::Error::None;
859}
860
861HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
862 return HWC2::Error::Unsupported;
863}
864
865#endif /* PLATFORM_SDK_VERSION > 28 */
866
867#if PLATFORM_SDK_VERSION > 27
868
869HWC2::Error HwcDisplay::GetRenderIntents(
870 int32_t mode, uint32_t *outNumIntents,
871 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
872 if (mode != HAL_COLOR_MODE_NATIVE) {
873 return HWC2::Error::BadParameter;
874 }
875
876 if (outIntents == nullptr) {
877 *outNumIntents = 1;
878 return HWC2::Error::None;
879 }
880 *outNumIntents = 1;
881 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
882 return HWC2::Error::None;
883}
884
885HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
886 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
887 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
888 return HWC2::Error::BadParameter;
889
890 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
891 return HWC2::Error::BadParameter;
892
893 if (mode != HAL_COLOR_MODE_NATIVE)
894 return HWC2::Error::Unsupported;
895
896 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
897 return HWC2::Error::Unsupported;
898
899 color_mode_ = mode;
900 return HWC2::Error::None;
901}
902
903#endif /* PLATFORM_SDK_VERSION > 27 */
904
905const Backend *HwcDisplay::backend() const {
906 return backend_.get();
907}
908
909void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
910 backend_ = std::move(backend);
911}
912
Roman Stratiienko099c3112022-01-20 11:50:54 +0200913/* returns true if composition should be sent to client */
914bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
915 int flattenning_state = flattenning_state_;
916 if (flattenning_state == ClientFlattenningState::Disabled) {
917 return false;
918 }
919
920 if (skip) {
921 flattenning_state_ = ClientFlattenningState::NotRequired;
922 return false;
923 }
924
925 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
926 flattenning_state_ = ClientFlattenningState::Flattened;
927 return true;
928 }
929
930 vsync_flattening_en_ = true;
931 vsync_worker_.VSyncControl(true);
932 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
933 return false;
934}
935
936void HwcDisplay::ProcessFlatenningVsyncInternal() {
937 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
938 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
939 hwc2_->refresh_callback_.first != nullptr &&
940 hwc2_->refresh_callback_.second != nullptr) {
941 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
942 vsync_flattening_en_ = false;
943 }
944}
945
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200946} // namespace android