blob: 7ca4549f507aa9c6c1a8a46ab8f53cedc71f2376 [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();
118 flattening_vsync_worker_.VSyncControl(false);
119 flattening_vsync_worker_.Exit();
120 vsync_worker_.VSyncControl(false);
121 vsync_worker_.Exit();
122 main_lock.lock();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200123}
124
125void HwcDisplay::ClearDisplay() {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200126 if (IsInHeadlessMode()) {
127 ALOGE("%s: Headless mode, should never reach here: ", __func__);
128 return;
129 }
130
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200131 AtomicCommitArgs a_args = {.clear_active_composition = true};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200132 pipeline_->compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200133}
134
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200135HWC2::Error HwcDisplay::Init() {
136 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200137 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200138 /* vsync callback */
139#if PLATFORM_SDK_VERSION > 29
140 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
141 hwc2_->vsync_2_4_callback_.second != nullptr) {
142 hwc2_vsync_period_t period_ns{};
143 GetDisplayVsyncPeriod(&period_ns);
144 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
145 handle_, timestamp, period_ns);
146 } else
147#endif
148 if (hwc2_->vsync_callback_.first != nullptr &&
149 hwc2_->vsync_callback_.second != nullptr) {
150 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
151 timestamp);
152 }
153 });
154 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200155 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200156 return HWC2::Error::BadDisplay;
157 }
158
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200159 ret = flattening_vsync_worker_.Init(pipeline_, [this](int64_t /*timestamp*/) {
160 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
161 /* Frontend flattening */
162 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
163 --flattenning_state_ ==
164 ClientFlattenningState::ClientRefreshRequested &&
165 hwc2_->refresh_callback_.first != nullptr &&
166 hwc2_->refresh_callback_.second != nullptr) {
167 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
168 flattening_vsync_worker_.VSyncControl(false);
169 }
170 });
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200172 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200173 return HWC2::Error::BadDisplay;
174 }
175
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200176 if (!IsInHeadlessMode()) {
177 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
178 if (ret) {
179 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
180 return HWC2::Error::BadDisplay;
181 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200182 }
183
184 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
185
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200186 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200187}
188
189HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200190 HWC2::Error err{};
191 if (!IsInHeadlessMode()) {
192 err = configs_.Update(*pipeline_->connector->Get());
193 } else {
194 configs_.FillHeadless();
195 }
196 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200198 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199
Roman Stratiienko0137f862022-01-04 18:27:40 +0200200 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201}
202
203HWC2::Error HwcDisplay::AcceptDisplayChanges() {
204 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
205 l.second.AcceptTypeChange();
206 return HWC2::Error::None;
207}
208
209HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
210 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
211 *layer = static_cast<hwc2_layer_t>(layer_idx_);
212 ++layer_idx_;
213 return HWC2::Error::None;
214}
215
216HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200217 if (!get_layer(layer)) {
218 /* Primary display don't send unplug event, instead it replaces
219 * display to headless or to another one and sends Plug event to the
220 * SF. SF can't distinguish this case from virtualized display size
221 * change case and will destroy previously used layers. If we will return
222 * BadLayer, service will print errors to the logcat.
223 *
224 * Nevertheless VTS is trying to destroy 1st layer without adding any
225 * layers prior to that, than it checks for BadLayer result. So we
226 * numbering the layers starting from 2, and use index 1 to catch VTS client
227 * to return BadLayer, making VTS pass.
228 */
229 if (layers_.empty() && layer != 1) {
230 return HWC2::Error::None;
231 }
232
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200234 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200235
236 layers_.erase(layer);
237 return HWC2::Error::None;
238}
239
240HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200241 if (configs_.hwc_configs.count(configs_.active_config_id) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200242 return HWC2::Error::BadConfig;
243
Roman Stratiienko0137f862022-01-04 18:27:40 +0200244 *config = configs_.active_config_id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 return HWC2::Error::None;
246}
247
248HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
249 hwc2_layer_t *layers,
250 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200251 if (IsInHeadlessMode()) {
252 *num_elements = 0;
253 return HWC2::Error::None;
254 }
255
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200256 uint32_t num_changes = 0;
257 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
258 if (l.second.IsTypeChanged()) {
259 if (layers && num_changes < *num_elements)
260 layers[num_changes] = l.first;
261 if (types && num_changes < *num_elements)
262 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
263 ++num_changes;
264 }
265 }
266 if (!layers && !types)
267 *num_elements = num_changes;
268 return HWC2::Error::None;
269}
270
271HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
272 int32_t /*format*/,
273 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200274 if (IsInHeadlessMode()) {
275 return HWC2::Error::None;
276 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200277
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200278 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
279 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
280
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281 if (width < min.first || height < min.second)
282 return HWC2::Error::Unsupported;
283
284 if (width > max.first || height > max.second)
285 return HWC2::Error::Unsupported;
286
287 if (dataspace != HAL_DATASPACE_UNKNOWN)
288 return HWC2::Error::Unsupported;
289
290 // TODO(nobody): Validate format can be handled by either GL or planes
291 return HWC2::Error::None;
292}
293
294HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
295 if (!modes)
296 *num_modes = 1;
297
298 if (modes)
299 *modes = HAL_COLOR_MODE_NATIVE;
300
301 return HWC2::Error::None;
302}
303
304HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
305 int32_t attribute_in,
306 int32_t *value) {
307 int conf = static_cast<int>(config);
308
Roman Stratiienko0137f862022-01-04 18:27:40 +0200309 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200310 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200311 return HWC2::Error::BadConfig;
312 }
313
Roman Stratiienko0137f862022-01-04 18:27:40 +0200314 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315
316 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200317 uint32_t mm_width = configs_.mm_width;
318 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200319 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
320 switch (attribute) {
321 case HWC2::Attribute::Width:
322 *value = static_cast<int>(hwc_config.mode.h_display());
323 break;
324 case HWC2::Attribute::Height:
325 *value = static_cast<int>(hwc_config.mode.v_display());
326 break;
327 case HWC2::Attribute::VsyncPeriod:
328 // in nanoseconds
329 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
330 break;
331 case HWC2::Attribute::DpiX:
332 // Dots per 1000 inches
333 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
334 kUmPerInch / mm_width)
335 : -1;
336 break;
337 case HWC2::Attribute::DpiY:
338 // Dots per 1000 inches
339 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
340 kUmPerInch / mm_height)
341 : -1;
342 break;
343#if PLATFORM_SDK_VERSION > 29
344 case HWC2::Attribute::ConfigGroup:
345 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
346 * able to request it even if service @2.1 is used */
347 *value = hwc_config.group_id;
348 break;
349#endif
350 default:
351 *value = -1;
352 return HWC2::Error::BadConfig;
353 }
354 return HWC2::Error::None;
355}
356
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200357HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
358 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200359 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200360 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200361 if (hwc_config.second.disabled) {
362 continue;
363 }
364
365 if (configs != nullptr) {
366 if (idx >= *num_configs) {
367 break;
368 }
369 configs[idx] = hwc_config.second.id;
370 }
371
372 idx++;
373 }
374 *num_configs = idx;
375 return HWC2::Error::None;
376}
377
378HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
379 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200380 if (IsInHeadlessMode()) {
381 stream << "null-display";
382 } else {
383 stream << "display-" << GetPipe().connector->Get()->GetId();
384 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200385 std::string string = stream.str();
386 size_t length = string.length();
387 if (!name) {
388 *size = length;
389 return HWC2::Error::None;
390 }
391
392 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
393 strncpy(name, string.c_str(), *size);
394 return HWC2::Error::None;
395}
396
397HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
398 uint32_t *num_elements,
399 hwc2_layer_t * /*layers*/,
400 int32_t * /*layer_requests*/) {
401 // TODO(nobody): I think virtual display should request
402 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
403 *num_elements = 0;
404 return HWC2::Error::None;
405}
406
407HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
408 *type = static_cast<int32_t>(type_);
409 return HWC2::Error::None;
410}
411
412HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
413 *support = 0;
414 return HWC2::Error::None;
415}
416
417HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
418 int32_t * /*types*/,
419 float * /*max_luminance*/,
420 float * /*max_average_luminance*/,
421 float * /*min_luminance*/) {
422 *num_types = 0;
423 return HWC2::Error::None;
424}
425
426/* Find API details at:
427 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
428 */
429HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
430 hwc2_layer_t *layers,
431 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200432 if (IsInHeadlessMode()) {
433 *num_elements = 0;
434 return HWC2::Error::None;
435 }
436
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 uint32_t num_layers = 0;
438
439 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
440 ++num_layers;
441 if (layers == nullptr || fences == nullptr)
442 continue;
443
444 if (num_layers > *num_elements) {
445 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
446 return HWC2::Error::None;
447 }
448
449 layers[num_layers - 1] = l.first;
450 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
451 }
452 *num_elements = num_layers;
453 return HWC2::Error::None;
454}
455
456HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200457 if (IsInHeadlessMode()) {
458 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
459 return HWC2::Error::None;
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;
524 if (staged_mode) {
525 a_args.display_mode = *staged_mode;
526 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200527 ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528
529 if (ret) {
530 if (!a_args.test_only)
531 ALOGE("Failed to apply the frame composition ret=%d", ret);
532 return HWC2::Error::BadParameter;
533 }
534
535 if (!a_args.test_only) {
536 staged_mode.reset();
537 }
538
539 return HWC2::Error::None;
540}
541
542/* Find API details at:
543 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
544 */
545HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200546 if (IsInHeadlessMode()) {
547 *present_fence = -1;
548 return HWC2::Error::None;
549 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200550 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200551
552 ++total_stats_.total_frames_;
553
554 AtomicCommitArgs a_args{};
555 ret = CreateComposition(a_args);
556
557 if (ret != HWC2::Error::None)
558 ++total_stats_.failed_kms_present_;
559
560 if (ret == HWC2::Error::BadLayer) {
561 // Can we really have no client or device layers?
562 *present_fence = -1;
563 return HWC2::Error::None;
564 }
565 if (ret != HWC2::Error::None)
566 return ret;
567
568 *present_fence = a_args.out_fence.Release();
569
570 ++frame_no_;
571 return HWC2::Error::None;
572}
573
574HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
575 int conf = static_cast<int>(config);
576
Roman Stratiienko0137f862022-01-04 18:27:40 +0200577 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200578 ALOGE("Could not find active mode for %d", conf);
579 return HWC2::Error::BadConfig;
580 }
581
Roman Stratiienko0137f862022-01-04 18:27:40 +0200582 auto &mode = configs_.hwc_configs[conf].mode;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200583
584 staged_mode = mode;
585
Roman Stratiienko0137f862022-01-04 18:27:40 +0200586 configs_.active_config_id = conf;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200587
588 // Setup the client layer's dimensions
589 hwc_rect_t display_frame = {.left = 0,
590 .top = 0,
591 .right = static_cast<int>(mode.h_display()),
592 .bottom = static_cast<int>(mode.v_display())};
593 client_layer_.SetLayerDisplayFrame(display_frame);
594
595 return HWC2::Error::None;
596}
597
598/* Find API details at:
599 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
600 */
601HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
602 int32_t acquire_fence,
603 int32_t dataspace,
604 hwc_region_t /*damage*/) {
605 client_layer_.SetLayerBuffer(target, acquire_fence);
606 client_layer_.SetLayerDataspace(dataspace);
607
608 /*
609 * target can be nullptr, this does mean the Composer Service is calling
610 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
611 * 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
612 */
613 if (target == nullptr) {
614 return HWC2::Error::None;
615 }
616
617 /* TODO: Do not update source_crop every call.
618 * It makes sense to do it once after every hotplug event. */
619 HwcDrmBo bo{};
620 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
621
622 hwc_frect_t source_crop = {.left = 0.0F,
623 .top = 0.0F,
624 .right = static_cast<float>(bo.width),
625 .bottom = static_cast<float>(bo.height)};
626 client_layer_.SetLayerSourceCrop(source_crop);
627
628 return HWC2::Error::None;
629}
630
631HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
632 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
633 return HWC2::Error::BadParameter;
634
635 if (mode != HAL_COLOR_MODE_NATIVE)
636 return HWC2::Error::Unsupported;
637
638 color_mode_ = mode;
639 return HWC2::Error::None;
640}
641
642HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
643 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
644 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
645 return HWC2::Error::BadParameter;
646
647 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
648 return HWC2::Error::BadParameter;
649
650 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
651 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
652 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
653
654 return HWC2::Error::None;
655}
656
657HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
658 int32_t /*release_fence*/) {
659 // TODO(nobody): Need virtual display support
660 return HWC2::Error::Unsupported;
661}
662
663HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200664 if (IsInHeadlessMode()) {
665 return HWC2::Error::None;
666 }
667
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200668 auto mode = static_cast<HWC2::PowerMode>(mode_in);
669 AtomicCommitArgs a_args{};
670
671 switch (mode) {
672 case HWC2::PowerMode::Off:
673 a_args.active = false;
674 break;
675 case HWC2::PowerMode::On:
676 /*
677 * Setting the display to active before we have a composition
678 * can break some drivers, so skip setting a_args.active to
679 * true, as the next composition frame will implicitly activate
680 * the display
681 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200682 return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200683 ? HWC2::Error::None
684 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200685 break;
686 case HWC2::PowerMode::Doze:
687 case HWC2::PowerMode::DozeSuspend:
688 return HWC2::Error::Unsupported;
689 default:
690 ALOGI("Power mode %d is unsupported\n", mode);
691 return HWC2::Error::BadParameter;
692 };
693
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200694 int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200695 if (err) {
696 ALOGE("Failed to apply the dpms composition err=%d", err);
697 return HWC2::Error::BadParameter;
698 }
699 return HWC2::Error::None;
700}
701
702HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
703 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
704 return HWC2::Error::None;
705}
706
707HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
708 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200709 if (IsInHeadlessMode()) {
710 *num_types = *num_requests = 0;
711 return HWC2::Error::None;
712 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200713 return backend_->ValidateDisplay(this, num_types, num_requests);
714}
715
716std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
717 std::vector<HwcLayer *> ordered_layers;
718 ordered_layers.reserve(layers_.size());
719
720 for (auto &[handle, layer] : layers_) {
721 ordered_layers.emplace_back(&layer);
722 }
723
724 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
725 [](const HwcLayer *lhs, const HwcLayer *rhs) {
726 return lhs->GetZOrder() < rhs->GetZOrder();
727 });
728
729 return ordered_layers;
730}
731
732#if PLATFORM_SDK_VERSION > 29
733HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200734 if (IsInHeadlessMode()) {
735 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
736 return HWC2::Error::None;
737 }
738 /* Primary display should be always internal,
739 * otherwise SF will be unhappy and will crash
740 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200741 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200742 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200743 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200744 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
745 else
746 return HWC2::Error::BadConfig;
747
748 return HWC2::Error::None;
749}
750
751HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
752 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200753 return GetDisplayAttribute(configs_.active_config_id,
754 HWC2_ATTRIBUTE_VSYNC_PERIOD,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200755 (int32_t *)(outVsyncPeriod));
756}
757
758HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
759 hwc2_config_t /*config*/,
760 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
761 hwc_vsync_period_change_timeline_t *outTimeline) {
762 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
763 return HWC2::Error::BadParameter;
764 }
765
766 return HWC2::Error::BadConfig;
767}
768
769HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
770 return HWC2::Error::Unsupported;
771}
772
773HWC2::Error HwcDisplay::GetSupportedContentTypes(
774 uint32_t *outNumSupportedContentTypes,
775 const uint32_t *outSupportedContentTypes) {
776 if (outSupportedContentTypes == nullptr)
777 *outNumSupportedContentTypes = 0;
778
779 return HWC2::Error::None;
780}
781
782HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
783 if (contentType != HWC2_CONTENT_TYPE_NONE)
784 return HWC2::Error::Unsupported;
785
786 /* TODO: Map to the DRM Connector property:
787 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
788 */
789
790 return HWC2::Error::None;
791}
792#endif
793
794#if PLATFORM_SDK_VERSION > 28
795HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
796 uint32_t *outDataSize,
797 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200798 if (IsInHeadlessMode()) {
799 return HWC2::Error::None;
800 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200801 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200802
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200803 *outPort = handle_ - 1;
804
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200805 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200806 if (outData == nullptr) {
807 *outDataSize = 0;
808 }
809 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200810 }
811
812 if (outData) {
813 *outDataSize = std::min(*outDataSize, blob->length);
814 memcpy(outData, blob->data, *outDataSize);
815 } else {
816 *outDataSize = blob->length;
817 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200818
819 return HWC2::Error::None;
820}
821
822HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
823 uint32_t * /*outCapabilities*/) {
824 if (outNumCapabilities == nullptr) {
825 return HWC2::Error::BadParameter;
826 }
827
828 *outNumCapabilities = 0;
829
830 return HWC2::Error::None;
831}
832
833HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
834 *supported = false;
835 return HWC2::Error::None;
836}
837
838HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
839 return HWC2::Error::Unsupported;
840}
841
842#endif /* PLATFORM_SDK_VERSION > 28 */
843
844#if PLATFORM_SDK_VERSION > 27
845
846HWC2::Error HwcDisplay::GetRenderIntents(
847 int32_t mode, uint32_t *outNumIntents,
848 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
849 if (mode != HAL_COLOR_MODE_NATIVE) {
850 return HWC2::Error::BadParameter;
851 }
852
853 if (outIntents == nullptr) {
854 *outNumIntents = 1;
855 return HWC2::Error::None;
856 }
857 *outNumIntents = 1;
858 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
859 return HWC2::Error::None;
860}
861
862HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
863 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
864 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
865 return HWC2::Error::BadParameter;
866
867 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
868 return HWC2::Error::BadParameter;
869
870 if (mode != HAL_COLOR_MODE_NATIVE)
871 return HWC2::Error::Unsupported;
872
873 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
874 return HWC2::Error::Unsupported;
875
876 color_mode_ = mode;
877 return HWC2::Error::None;
878}
879
880#endif /* PLATFORM_SDK_VERSION > 27 */
881
882const Backend *HwcDisplay::backend() const {
883 return backend_.get();
884}
885
886void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
887 backend_ = std::move(backend);
888}
889
890} // namespace android