blob: 656aa94d286dd2ce6ad1ebcb93d660e8a9aafed1 [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 }
144 if (!vsync_event_en_ && !vsync_flattening_en_) {
145 vsync_worker_.VSyncControl(false);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200146 }
147 });
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200148 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200149 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200150 return HWC2::Error::BadDisplay;
151 }
152
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200153 if (!IsInHeadlessMode()) {
154 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
155 if (ret) {
156 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
157 return HWC2::Error::BadDisplay;
158 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200159 }
160
161 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
162
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200163 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200164}
165
166HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200167 HWC2::Error err{};
168 if (!IsInHeadlessMode()) {
169 err = configs_.Update(*pipeline_->connector->Get());
170 } else {
171 configs_.FillHeadless();
172 }
173 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200174 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200175 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200176
Roman Stratiienko0137f862022-01-04 18:27:40 +0200177 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200178}
179
180HWC2::Error HwcDisplay::AcceptDisplayChanges() {
181 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
182 l.second.AcceptTypeChange();
183 return HWC2::Error::None;
184}
185
186HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
187 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
188 *layer = static_cast<hwc2_layer_t>(layer_idx_);
189 ++layer_idx_;
190 return HWC2::Error::None;
191}
192
193HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200194 if (!get_layer(layer)) {
195 /* Primary display don't send unplug event, instead it replaces
196 * display to headless or to another one and sends Plug event to the
197 * SF. SF can't distinguish this case from virtualized display size
198 * change case and will destroy previously used layers. If we will return
199 * BadLayer, service will print errors to the logcat.
200 *
201 * Nevertheless VTS is trying to destroy 1st layer without adding any
202 * layers prior to that, than it checks for BadLayer result. So we
203 * numbering the layers starting from 2, and use index 1 to catch VTS client
204 * to return BadLayer, making VTS pass.
205 */
206 if (layers_.empty() && layer != 1) {
207 return HWC2::Error::None;
208 }
209
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200210 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200211 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200212
213 layers_.erase(layer);
214 return HWC2::Error::None;
215}
216
217HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200218 if (configs_.hwc_configs.count(configs_.active_config_id) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200219 return HWC2::Error::BadConfig;
220
Roman Stratiienko0137f862022-01-04 18:27:40 +0200221 *config = configs_.active_config_id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222 return HWC2::Error::None;
223}
224
225HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
226 hwc2_layer_t *layers,
227 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200228 if (IsInHeadlessMode()) {
229 *num_elements = 0;
230 return HWC2::Error::None;
231 }
232
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233 uint32_t num_changes = 0;
234 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
235 if (l.second.IsTypeChanged()) {
236 if (layers && num_changes < *num_elements)
237 layers[num_changes] = l.first;
238 if (types && num_changes < *num_elements)
239 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
240 ++num_changes;
241 }
242 }
243 if (!layers && !types)
244 *num_elements = num_changes;
245 return HWC2::Error::None;
246}
247
248HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
249 int32_t /*format*/,
250 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200251 if (IsInHeadlessMode()) {
252 return HWC2::Error::None;
253 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200254
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200255 std::pair<uint32_t, uint32_t> min = pipeline_->device->GetMinResolution();
256 std::pair<uint32_t, uint32_t> max = pipeline_->device->GetMaxResolution();
257
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200258 if (width < min.first || height < min.second)
259 return HWC2::Error::Unsupported;
260
261 if (width > max.first || height > max.second)
262 return HWC2::Error::Unsupported;
263
264 if (dataspace != HAL_DATASPACE_UNKNOWN)
265 return HWC2::Error::Unsupported;
266
267 // TODO(nobody): Validate format can be handled by either GL or planes
268 return HWC2::Error::None;
269}
270
271HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
272 if (!modes)
273 *num_modes = 1;
274
275 if (modes)
276 *modes = HAL_COLOR_MODE_NATIVE;
277
278 return HWC2::Error::None;
279}
280
281HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
282 int32_t attribute_in,
283 int32_t *value) {
284 int conf = static_cast<int>(config);
285
Roman Stratiienko0137f862022-01-04 18:27:40 +0200286 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200287 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200288 return HWC2::Error::BadConfig;
289 }
290
Roman Stratiienko0137f862022-01-04 18:27:40 +0200291 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200292
293 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200294 uint32_t mm_width = configs_.mm_width;
295 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200296 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
297 switch (attribute) {
298 case HWC2::Attribute::Width:
299 *value = static_cast<int>(hwc_config.mode.h_display());
300 break;
301 case HWC2::Attribute::Height:
302 *value = static_cast<int>(hwc_config.mode.v_display());
303 break;
304 case HWC2::Attribute::VsyncPeriod:
305 // in nanoseconds
306 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
307 break;
308 case HWC2::Attribute::DpiX:
309 // Dots per 1000 inches
310 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
311 kUmPerInch / mm_width)
312 : -1;
313 break;
314 case HWC2::Attribute::DpiY:
315 // Dots per 1000 inches
316 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
317 kUmPerInch / mm_height)
318 : -1;
319 break;
320#if PLATFORM_SDK_VERSION > 29
321 case HWC2::Attribute::ConfigGroup:
322 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
323 * able to request it even if service @2.1 is used */
324 *value = hwc_config.group_id;
325 break;
326#endif
327 default:
328 *value = -1;
329 return HWC2::Error::BadConfig;
330 }
331 return HWC2::Error::None;
332}
333
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200334HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
335 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200336 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200337 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200338 if (hwc_config.second.disabled) {
339 continue;
340 }
341
342 if (configs != nullptr) {
343 if (idx >= *num_configs) {
344 break;
345 }
346 configs[idx] = hwc_config.second.id;
347 }
348
349 idx++;
350 }
351 *num_configs = idx;
352 return HWC2::Error::None;
353}
354
355HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
356 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200357 if (IsInHeadlessMode()) {
358 stream << "null-display";
359 } else {
360 stream << "display-" << GetPipe().connector->Get()->GetId();
361 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200362 std::string string = stream.str();
363 size_t length = string.length();
364 if (!name) {
365 *size = length;
366 return HWC2::Error::None;
367 }
368
369 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
370 strncpy(name, string.c_str(), *size);
371 return HWC2::Error::None;
372}
373
374HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
375 uint32_t *num_elements,
376 hwc2_layer_t * /*layers*/,
377 int32_t * /*layer_requests*/) {
378 // TODO(nobody): I think virtual display should request
379 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
380 *num_elements = 0;
381 return HWC2::Error::None;
382}
383
384HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
385 *type = static_cast<int32_t>(type_);
386 return HWC2::Error::None;
387}
388
389HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
390 *support = 0;
391 return HWC2::Error::None;
392}
393
394HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
395 int32_t * /*types*/,
396 float * /*max_luminance*/,
397 float * /*max_average_luminance*/,
398 float * /*min_luminance*/) {
399 *num_types = 0;
400 return HWC2::Error::None;
401}
402
403/* Find API details at:
404 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
405 */
406HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
407 hwc2_layer_t *layers,
408 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200409 if (IsInHeadlessMode()) {
410 *num_elements = 0;
411 return HWC2::Error::None;
412 }
413
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200414 uint32_t num_layers = 0;
415
416 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
417 ++num_layers;
418 if (layers == nullptr || fences == nullptr)
419 continue;
420
421 if (num_layers > *num_elements) {
422 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
423 return HWC2::Error::None;
424 }
425
426 layers[num_layers - 1] = l.first;
427 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
428 }
429 *num_elements = num_layers;
430 return HWC2::Error::None;
431}
432
433HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200434 if (IsInHeadlessMode()) {
435 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
436 return HWC2::Error::None;
437 }
438
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200439 // order the layers by z-order
440 bool use_client_layer = false;
441 uint32_t client_z_order = UINT32_MAX;
442 std::map<uint32_t, HwcLayer *> z_map;
443 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
444 switch (l.second.GetValidatedType()) {
445 case HWC2::Composition::Device:
446 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
447 break;
448 case HWC2::Composition::Client:
449 // Place it at the z_order of the lowest client layer
450 use_client_layer = true;
451 client_z_order = std::min(client_z_order, l.second.GetZOrder());
452 break;
453 default:
454 continue;
455 }
456 }
457 if (use_client_layer)
458 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
459
460 if (z_map.empty())
461 return HWC2::Error::BadLayer;
462
463 std::vector<DrmHwcLayer> composition_layers;
464
465 // now that they're ordered by z, add them to the composition
466 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
467 DrmHwcLayer layer;
468 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200469 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200470 if (ret) {
471 ALOGE("Failed to import layer, ret=%d", ret);
472 return HWC2::Error::NoResources;
473 }
474 composition_layers.emplace_back(std::move(layer));
475 }
476
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200477 auto composition = std::make_shared<DrmDisplayComposition>(
478 GetPipe().crtc->Get());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200479
480 // TODO(nobody): Don't always assume geometry changed
481 int ret = composition->SetLayers(composition_layers.data(),
482 composition_layers.size());
483 if (ret) {
484 ALOGE("Failed to set layers in the composition ret=%d", ret);
485 return HWC2::Error::BadLayer;
486 }
487
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200488 std::vector<DrmPlane *> primary_planes;
489 primary_planes.emplace_back(pipeline_->primary_plane->Get());
490 std::vector<DrmPlane *> overlay_planes;
491 for (const auto &owned_plane : pipeline_->overlay_planes) {
492 overlay_planes.emplace_back(owned_plane->Get());
493 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200494 ret = composition->Plan(&primary_planes, &overlay_planes);
495 if (ret) {
496 ALOGV("Failed to plan the composition ret=%d", ret);
497 return HWC2::Error::BadConfig;
498 }
499
500 a_args.composition = composition;
501 if (staged_mode) {
502 a_args.display_mode = *staged_mode;
503 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200504 ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200505
506 if (ret) {
507 if (!a_args.test_only)
508 ALOGE("Failed to apply the frame composition ret=%d", ret);
509 return HWC2::Error::BadParameter;
510 }
511
512 if (!a_args.test_only) {
513 staged_mode.reset();
514 }
515
516 return HWC2::Error::None;
517}
518
519/* Find API details at:
520 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
521 */
522HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200523 if (IsInHeadlessMode()) {
524 *present_fence = -1;
525 return HWC2::Error::None;
526 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200527 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528
529 ++total_stats_.total_frames_;
530
531 AtomicCommitArgs a_args{};
532 ret = CreateComposition(a_args);
533
534 if (ret != HWC2::Error::None)
535 ++total_stats_.failed_kms_present_;
536
537 if (ret == HWC2::Error::BadLayer) {
538 // Can we really have no client or device layers?
539 *present_fence = -1;
540 return HWC2::Error::None;
541 }
542 if (ret != HWC2::Error::None)
543 return ret;
544
545 *present_fence = a_args.out_fence.Release();
546
547 ++frame_no_;
548 return HWC2::Error::None;
549}
550
551HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
552 int conf = static_cast<int>(config);
553
Roman Stratiienko0137f862022-01-04 18:27:40 +0200554 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200555 ALOGE("Could not find active mode for %d", conf);
556 return HWC2::Error::BadConfig;
557 }
558
Roman Stratiienko0137f862022-01-04 18:27:40 +0200559 auto &mode = configs_.hwc_configs[conf].mode;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200560
561 staged_mode = mode;
562
Roman Stratiienko0137f862022-01-04 18:27:40 +0200563 configs_.active_config_id = conf;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200564
565 // Setup the client layer's dimensions
566 hwc_rect_t display_frame = {.left = 0,
567 .top = 0,
568 .right = static_cast<int>(mode.h_display()),
569 .bottom = static_cast<int>(mode.v_display())};
570 client_layer_.SetLayerDisplayFrame(display_frame);
571
572 return HWC2::Error::None;
573}
574
575/* Find API details at:
576 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
577 */
578HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
579 int32_t acquire_fence,
580 int32_t dataspace,
581 hwc_region_t /*damage*/) {
582 client_layer_.SetLayerBuffer(target, acquire_fence);
583 client_layer_.SetLayerDataspace(dataspace);
584
585 /*
586 * target can be nullptr, this does mean the Composer Service is calling
587 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
588 * 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
589 */
590 if (target == nullptr) {
591 return HWC2::Error::None;
592 }
593
594 /* TODO: Do not update source_crop every call.
595 * It makes sense to do it once after every hotplug event. */
596 HwcDrmBo bo{};
597 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
598
599 hwc_frect_t source_crop = {.left = 0.0F,
600 .top = 0.0F,
601 .right = static_cast<float>(bo.width),
602 .bottom = static_cast<float>(bo.height)};
603 client_layer_.SetLayerSourceCrop(source_crop);
604
605 return HWC2::Error::None;
606}
607
608HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
609 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
610 return HWC2::Error::BadParameter;
611
612 if (mode != HAL_COLOR_MODE_NATIVE)
613 return HWC2::Error::Unsupported;
614
615 color_mode_ = mode;
616 return HWC2::Error::None;
617}
618
619HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
620 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
621 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
622 return HWC2::Error::BadParameter;
623
624 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
625 return HWC2::Error::BadParameter;
626
627 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
628 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
629 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
630
631 return HWC2::Error::None;
632}
633
634HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
635 int32_t /*release_fence*/) {
636 // TODO(nobody): Need virtual display support
637 return HWC2::Error::Unsupported;
638}
639
640HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200641 if (IsInHeadlessMode()) {
642 return HWC2::Error::None;
643 }
644
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 auto mode = static_cast<HWC2::PowerMode>(mode_in);
646 AtomicCommitArgs a_args{};
647
648 switch (mode) {
649 case HWC2::PowerMode::Off:
650 a_args.active = false;
651 break;
652 case HWC2::PowerMode::On:
653 /*
654 * Setting the display to active before we have a composition
655 * can break some drivers, so skip setting a_args.active to
656 * true, as the next composition frame will implicitly activate
657 * the display
658 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200659 return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200660 ? HWC2::Error::None
661 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200662 break;
663 case HWC2::PowerMode::Doze:
664 case HWC2::PowerMode::DozeSuspend:
665 return HWC2::Error::Unsupported;
666 default:
667 ALOGI("Power mode %d is unsupported\n", mode);
668 return HWC2::Error::BadParameter;
669 };
670
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200671 int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200672 if (err) {
673 ALOGE("Failed to apply the dpms composition err=%d", err);
674 return HWC2::Error::BadParameter;
675 }
676 return HWC2::Error::None;
677}
678
679HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200680 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
681 if (vsync_event_en_) {
682 vsync_worker_.VSyncControl(true);
683 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200684 return HWC2::Error::None;
685}
686
687HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
688 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200689 if (IsInHeadlessMode()) {
690 *num_types = *num_requests = 0;
691 return HWC2::Error::None;
692 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200693 return backend_->ValidateDisplay(this, num_types, num_requests);
694}
695
696std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
697 std::vector<HwcLayer *> ordered_layers;
698 ordered_layers.reserve(layers_.size());
699
700 for (auto &[handle, layer] : layers_) {
701 ordered_layers.emplace_back(&layer);
702 }
703
704 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
705 [](const HwcLayer *lhs, const HwcLayer *rhs) {
706 return lhs->GetZOrder() < rhs->GetZOrder();
707 });
708
709 return ordered_layers;
710}
711
Roman Stratiienko099c3112022-01-20 11:50:54 +0200712HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
713 uint32_t *outVsyncPeriod /* ns */) {
714 return GetDisplayAttribute(configs_.active_config_id,
715 HWC2_ATTRIBUTE_VSYNC_PERIOD,
716 (int32_t *)(outVsyncPeriod));
717}
718
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200719#if PLATFORM_SDK_VERSION > 29
720HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200721 if (IsInHeadlessMode()) {
722 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
723 return HWC2::Error::None;
724 }
725 /* Primary display should be always internal,
726 * otherwise SF will be unhappy and will crash
727 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200728 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200729 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200730 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200731 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
732 else
733 return HWC2::Error::BadConfig;
734
735 return HWC2::Error::None;
736}
737
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200738HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
739 hwc2_config_t /*config*/,
740 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
741 hwc_vsync_period_change_timeline_t *outTimeline) {
742 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
743 return HWC2::Error::BadParameter;
744 }
745
746 return HWC2::Error::BadConfig;
747}
748
749HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
750 return HWC2::Error::Unsupported;
751}
752
753HWC2::Error HwcDisplay::GetSupportedContentTypes(
754 uint32_t *outNumSupportedContentTypes,
755 const uint32_t *outSupportedContentTypes) {
756 if (outSupportedContentTypes == nullptr)
757 *outNumSupportedContentTypes = 0;
758
759 return HWC2::Error::None;
760}
761
762HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
763 if (contentType != HWC2_CONTENT_TYPE_NONE)
764 return HWC2::Error::Unsupported;
765
766 /* TODO: Map to the DRM Connector property:
767 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
768 */
769
770 return HWC2::Error::None;
771}
772#endif
773
774#if PLATFORM_SDK_VERSION > 28
775HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
776 uint32_t *outDataSize,
777 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200778 if (IsInHeadlessMode()) {
779 return HWC2::Error::None;
780 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200781 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200782
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200783 *outPort = handle_ - 1;
784
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 if (!blob) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200786 if (outData == nullptr) {
787 *outDataSize = 0;
788 }
789 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200790 }
791
792 if (outData) {
793 *outDataSize = std::min(*outDataSize, blob->length);
794 memcpy(outData, blob->data, *outDataSize);
795 } else {
796 *outDataSize = blob->length;
797 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200798
799 return HWC2::Error::None;
800}
801
802HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
803 uint32_t * /*outCapabilities*/) {
804 if (outNumCapabilities == nullptr) {
805 return HWC2::Error::BadParameter;
806 }
807
808 *outNumCapabilities = 0;
809
810 return HWC2::Error::None;
811}
812
813HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
814 *supported = false;
815 return HWC2::Error::None;
816}
817
818HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
819 return HWC2::Error::Unsupported;
820}
821
822#endif /* PLATFORM_SDK_VERSION > 28 */
823
824#if PLATFORM_SDK_VERSION > 27
825
826HWC2::Error HwcDisplay::GetRenderIntents(
827 int32_t mode, uint32_t *outNumIntents,
828 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
829 if (mode != HAL_COLOR_MODE_NATIVE) {
830 return HWC2::Error::BadParameter;
831 }
832
833 if (outIntents == nullptr) {
834 *outNumIntents = 1;
835 return HWC2::Error::None;
836 }
837 *outNumIntents = 1;
838 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
839 return HWC2::Error::None;
840}
841
842HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
843 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
844 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
845 return HWC2::Error::BadParameter;
846
847 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
848 return HWC2::Error::BadParameter;
849
850 if (mode != HAL_COLOR_MODE_NATIVE)
851 return HWC2::Error::Unsupported;
852
853 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
854 return HWC2::Error::Unsupported;
855
856 color_mode_ = mode;
857 return HWC2::Error::None;
858}
859
860#endif /* PLATFORM_SDK_VERSION > 27 */
861
862const Backend *HwcDisplay::backend() const {
863 return backend_.get();
864}
865
866void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
867 backend_ = std::move(backend);
868}
869
Roman Stratiienko099c3112022-01-20 11:50:54 +0200870/* returns true if composition should be sent to client */
871bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
872 int flattenning_state = flattenning_state_;
873 if (flattenning_state == ClientFlattenningState::Disabled) {
874 return false;
875 }
876
877 if (skip) {
878 flattenning_state_ = ClientFlattenningState::NotRequired;
879 return false;
880 }
881
882 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
883 flattenning_state_ = ClientFlattenningState::Flattened;
884 return true;
885 }
886
887 vsync_flattening_en_ = true;
888 vsync_worker_.VSyncControl(true);
889 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
890 return false;
891}
892
893void HwcDisplay::ProcessFlatenningVsyncInternal() {
894 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
895 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
896 hwc2_->refresh_callback_.first != nullptr &&
897 hwc2_->refresh_callback_.second != nullptr) {
898 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
899 vsync_flattening_en_ = false;
900 }
901}
902
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200903} // namespace android