blob: ac5d1965d8b0a1a1386ccb2a654383094bf1f453 [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
30std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
31 if (delta.total_pixops_ == 0)
32 return "No stats yet";
33 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
34
35 std::stringstream ss;
36 ss << " Total frames count: " << delta.total_frames_ << "\n"
37 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
38 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
39 << ((delta.failed_kms_present_ > 0)
40 ? " !!! Internal failure, FIX it please\n"
41 : "")
42 << " Flattened frames: " << delta.frames_flattened_ << "\n"
43 << " Pixel operations (free units)"
44 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
45 << "]\n"
46 << " Composition efficiency: " << ratio;
47
48 return ss.str();
49}
50
51std::string HwcDisplay::Dump() {
52 std::string flattening_state_str;
53 switch (flattenning_state_) {
54 case ClientFlattenningState::Disabled:
55 flattening_state_str = "Disabled";
56 break;
57 case ClientFlattenningState::NotRequired:
58 flattening_state_str = "Not needed";
59 break;
60 case ClientFlattenningState::Flattened:
61 flattening_state_str = "Active";
62 break;
63 case ClientFlattenningState::ClientRefreshRequested:
64 flattening_state_str = "Refresh requested";
65 break;
66 default:
67 flattening_state_str = std::to_string(flattenning_state_) +
68 " VSync remains";
69 }
70
Roman Stratiienko19c162f2022-02-01 09:35:08 +020071 std::string connector_name = IsInHeadlessMode()
72 ? "NULL-DISPLAY"
73 : GetPipe().connector->Get()->GetName();
74
Roman Stratiienko3627beb2022-01-04 16:02:55 +020075 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020076 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020077 << " Flattening state: " << flattening_state_str << "\n"
78 << "Statistics since system boot:\n"
79 << DumpDelta(total_stats_) << "\n\n"
80 << "Statistics since last dumpsys request:\n"
81 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
82
83 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
84 return ss.str();
85}
86
Roman Stratiienko19c162f2022-02-01 09:35:08 +020087HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
88 DrmDisplayPipeline *pipeline, hwc2_display_t handle,
89 HWC2::DisplayType type, DrmHwcTwo *hwc2)
Roman Stratiienko3627beb2022-01-04 16:02:55 +020090 : hwc2_(hwc2),
91 resource_manager_(resource_manager),
Roman Stratiienko19c162f2022-02-01 09:35:08 +020092 pipeline_(pipeline),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020093 handle_(handle),
94 type_(type),
95 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
96 // clang-format off
97 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
98 0.0, 1.0, 0.0, 0.0,
99 0.0, 0.0, 1.0, 0.0,
100 0.0, 0.0, 0.0, 1.0};
101 // clang-format on
102}
103
104void HwcDisplay::ClearDisplay() {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200105 if (IsInHeadlessMode()) {
106 ALOGE("%s: Headless mode, should never reach here: ", __func__);
107 return;
108 }
109
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200110 AtomicCommitArgs a_args = {.clear_active_composition = true};
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200111 GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200112}
113
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200114HWC2::Error HwcDisplay::Init() {
115 int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200116 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200117 /* vsync callback */
118#if PLATFORM_SDK_VERSION > 29
119 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
120 hwc2_->vsync_2_4_callback_.second != nullptr) {
121 hwc2_vsync_period_t period_ns{};
122 GetDisplayVsyncPeriod(&period_ns);
123 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
124 handle_, timestamp, period_ns);
125 } else
126#endif
127 if (hwc2_->vsync_callback_.first != nullptr &&
128 hwc2_->vsync_callback_.second != nullptr) {
129 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
130 timestamp);
131 }
132 });
133 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200134 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200135 return HWC2::Error::BadDisplay;
136 }
137
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200138 ret = flattening_vsync_worker_.Init(pipeline_, [this](int64_t /*timestamp*/) {
139 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
140 /* Frontend flattening */
141 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
142 --flattenning_state_ ==
143 ClientFlattenningState::ClientRefreshRequested &&
144 hwc2_->refresh_callback_.first != nullptr &&
145 hwc2_->refresh_callback_.second != nullptr) {
146 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
147 flattening_vsync_worker_.VSyncControl(false);
148 }
149 });
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200150 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200151 ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200152 return HWC2::Error::BadDisplay;
153 }
154
155 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
156 if (ret) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200157 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200158 return HWC2::Error::BadDisplay;
159 }
160
161 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
162
163 return ChosePreferredConfig();
164}
165
166HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200167 HWC2::Error err = configs_.Update(*GetPipe().connector->Get());
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200168 if (!IsInHeadlessMode() && err != HWC2::Error::None)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200169 return HWC2::Error::BadDisplay;
170
Roman Stratiienko0137f862022-01-04 18:27:40 +0200171 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200172}
173
174HWC2::Error HwcDisplay::AcceptDisplayChanges() {
175 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
176 l.second.AcceptTypeChange();
177 return HWC2::Error::None;
178}
179
180HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
181 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
182 *layer = static_cast<hwc2_layer_t>(layer_idx_);
183 ++layer_idx_;
184 return HWC2::Error::None;
185}
186
187HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
188 if (!get_layer(layer))
189 return HWC2::Error::BadLayer;
190
191 layers_.erase(layer);
192 return HWC2::Error::None;
193}
194
195HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200196 if (configs_.hwc_configs.count(configs_.active_config_id) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197 return HWC2::Error::BadConfig;
198
Roman Stratiienko0137f862022-01-04 18:27:40 +0200199 *config = configs_.active_config_id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200200 return HWC2::Error::None;
201}
202
203HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
204 hwc2_layer_t *layers,
205 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200206 if (IsInHeadlessMode()) {
207 *num_elements = 0;
208 return HWC2::Error::None;
209 }
210
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 uint32_t num_changes = 0;
212 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
213 if (l.second.IsTypeChanged()) {
214 if (layers && num_changes < *num_elements)
215 layers[num_changes] = l.first;
216 if (types && num_changes < *num_elements)
217 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
218 ++num_changes;
219 }
220 }
221 if (!layers && !types)
222 *num_elements = num_changes;
223 return HWC2::Error::None;
224}
225
226HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
227 int32_t /*format*/,
228 int32_t dataspace) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200229 std::pair<uint32_t, uint32_t> min = GetPipe().device->GetMinResolution();
230 std::pair<uint32_t, uint32_t> max = GetPipe().device->GetMaxResolution();
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200231 if (IsInHeadlessMode()) {
232 return HWC2::Error::None;
233 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200234
235 if (width < min.first || height < min.second)
236 return HWC2::Error::Unsupported;
237
238 if (width > max.first || height > max.second)
239 return HWC2::Error::Unsupported;
240
241 if (dataspace != HAL_DATASPACE_UNKNOWN)
242 return HWC2::Error::Unsupported;
243
244 // TODO(nobody): Validate format can be handled by either GL or planes
245 return HWC2::Error::None;
246}
247
248HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
249 if (!modes)
250 *num_modes = 1;
251
252 if (modes)
253 *modes = HAL_COLOR_MODE_NATIVE;
254
255 return HWC2::Error::None;
256}
257
258HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
259 int32_t attribute_in,
260 int32_t *value) {
261 int conf = static_cast<int>(config);
262
Roman Stratiienko0137f862022-01-04 18:27:40 +0200263 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200264 ALOGE("Could not find active mode for %d", conf);
265 return HWC2::Error::BadConfig;
266 }
267
Roman Stratiienko0137f862022-01-04 18:27:40 +0200268 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200269
270 static const int32_t kUmPerInch = 25400;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200271 uint32_t mm_width = configs_.mm_width;
272 uint32_t mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200273 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
274 switch (attribute) {
275 case HWC2::Attribute::Width:
276 *value = static_cast<int>(hwc_config.mode.h_display());
277 break;
278 case HWC2::Attribute::Height:
279 *value = static_cast<int>(hwc_config.mode.v_display());
280 break;
281 case HWC2::Attribute::VsyncPeriod:
282 // in nanoseconds
283 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
284 break;
285 case HWC2::Attribute::DpiX:
286 // Dots per 1000 inches
287 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
288 kUmPerInch / mm_width)
289 : -1;
290 break;
291 case HWC2::Attribute::DpiY:
292 // Dots per 1000 inches
293 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
294 kUmPerInch / mm_height)
295 : -1;
296 break;
297#if PLATFORM_SDK_VERSION > 29
298 case HWC2::Attribute::ConfigGroup:
299 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
300 * able to request it even if service @2.1 is used */
301 *value = hwc_config.group_id;
302 break;
303#endif
304 default:
305 *value = -1;
306 return HWC2::Error::BadConfig;
307 }
308 return HWC2::Error::None;
309}
310
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200311HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
312 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200313 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200314 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200315 if (hwc_config.second.disabled) {
316 continue;
317 }
318
319 if (configs != nullptr) {
320 if (idx >= *num_configs) {
321 break;
322 }
323 configs[idx] = hwc_config.second.id;
324 }
325
326 idx++;
327 }
328 *num_configs = idx;
329 return HWC2::Error::None;
330}
331
332HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
333 std::ostringstream stream;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200334 stream << "display-" << GetPipe().connector->Get()->GetId();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 std::string string = stream.str();
336 size_t length = string.length();
337 if (!name) {
338 *size = length;
339 return HWC2::Error::None;
340 }
341
342 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
343 strncpy(name, string.c_str(), *size);
344 return HWC2::Error::None;
345}
346
347HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
348 uint32_t *num_elements,
349 hwc2_layer_t * /*layers*/,
350 int32_t * /*layer_requests*/) {
351 // TODO(nobody): I think virtual display should request
352 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
353 *num_elements = 0;
354 return HWC2::Error::None;
355}
356
357HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
358 *type = static_cast<int32_t>(type_);
359 return HWC2::Error::None;
360}
361
362HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
363 *support = 0;
364 return HWC2::Error::None;
365}
366
367HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
368 int32_t * /*types*/,
369 float * /*max_luminance*/,
370 float * /*max_average_luminance*/,
371 float * /*min_luminance*/) {
372 *num_types = 0;
373 return HWC2::Error::None;
374}
375
376/* Find API details at:
377 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
378 */
379HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
380 hwc2_layer_t *layers,
381 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200382 if (IsInHeadlessMode()) {
383 *num_elements = 0;
384 return HWC2::Error::None;
385 }
386
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200387 uint32_t num_layers = 0;
388
389 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
390 ++num_layers;
391 if (layers == nullptr || fences == nullptr)
392 continue;
393
394 if (num_layers > *num_elements) {
395 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
396 return HWC2::Error::None;
397 }
398
399 layers[num_layers - 1] = l.first;
400 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
401 }
402 *num_elements = num_layers;
403 return HWC2::Error::None;
404}
405
406HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200407 if (IsInHeadlessMode()) {
408 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
409 return HWC2::Error::None;
410 }
411
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200412 // order the layers by z-order
413 bool use_client_layer = false;
414 uint32_t client_z_order = UINT32_MAX;
415 std::map<uint32_t, HwcLayer *> z_map;
416 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
417 switch (l.second.GetValidatedType()) {
418 case HWC2::Composition::Device:
419 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
420 break;
421 case HWC2::Composition::Client:
422 // Place it at the z_order of the lowest client layer
423 use_client_layer = true;
424 client_z_order = std::min(client_z_order, l.second.GetZOrder());
425 break;
426 default:
427 continue;
428 }
429 }
430 if (use_client_layer)
431 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
432
433 if (z_map.empty())
434 return HWC2::Error::BadLayer;
435
436 std::vector<DrmHwcLayer> composition_layers;
437
438 // now that they're ordered by z, add them to the composition
439 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
440 DrmHwcLayer layer;
441 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200442 int ret = layer.ImportBuffer(GetPipe().device);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200443 if (ret) {
444 ALOGE("Failed to import layer, ret=%d", ret);
445 return HWC2::Error::NoResources;
446 }
447 composition_layers.emplace_back(std::move(layer));
448 }
449
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200450 auto composition = std::make_shared<DrmDisplayComposition>(
451 GetPipe().crtc->Get());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200452
453 // TODO(nobody): Don't always assume geometry changed
454 int ret = composition->SetLayers(composition_layers.data(),
455 composition_layers.size());
456 if (ret) {
457 ALOGE("Failed to set layers in the composition ret=%d", ret);
458 return HWC2::Error::BadLayer;
459 }
460
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200461 std::vector<DrmPlane *> primary_planes;
462 primary_planes.emplace_back(pipeline_->primary_plane->Get());
463 std::vector<DrmPlane *> overlay_planes;
464 for (const auto &owned_plane : pipeline_->overlay_planes) {
465 overlay_planes.emplace_back(owned_plane->Get());
466 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200467 ret = composition->Plan(&primary_planes, &overlay_planes);
468 if (ret) {
469 ALOGV("Failed to plan the composition ret=%d", ret);
470 return HWC2::Error::BadConfig;
471 }
472
473 a_args.composition = composition;
474 if (staged_mode) {
475 a_args.display_mode = *staged_mode;
476 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200477 ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200478
479 if (ret) {
480 if (!a_args.test_only)
481 ALOGE("Failed to apply the frame composition ret=%d", ret);
482 return HWC2::Error::BadParameter;
483 }
484
485 if (!a_args.test_only) {
486 staged_mode.reset();
487 }
488
489 return HWC2::Error::None;
490}
491
492/* Find API details at:
493 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
494 */
495HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200496 if (IsInHeadlessMode()) {
497 *present_fence = -1;
498 return HWC2::Error::None;
499 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200500 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200501
502 ++total_stats_.total_frames_;
503
504 AtomicCommitArgs a_args{};
505 ret = CreateComposition(a_args);
506
507 if (ret != HWC2::Error::None)
508 ++total_stats_.failed_kms_present_;
509
510 if (ret == HWC2::Error::BadLayer) {
511 // Can we really have no client or device layers?
512 *present_fence = -1;
513 return HWC2::Error::None;
514 }
515 if (ret != HWC2::Error::None)
516 return ret;
517
518 *present_fence = a_args.out_fence.Release();
519
520 ++frame_no_;
521 return HWC2::Error::None;
522}
523
524HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
525 int conf = static_cast<int>(config);
526
Roman Stratiienko0137f862022-01-04 18:27:40 +0200527 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200528 ALOGE("Could not find active mode for %d", conf);
529 return HWC2::Error::BadConfig;
530 }
531
Roman Stratiienko0137f862022-01-04 18:27:40 +0200532 auto &mode = configs_.hwc_configs[conf].mode;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533
534 staged_mode = mode;
535
Roman Stratiienko0137f862022-01-04 18:27:40 +0200536 configs_.active_config_id = conf;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537
538 // Setup the client layer's dimensions
539 hwc_rect_t display_frame = {.left = 0,
540 .top = 0,
541 .right = static_cast<int>(mode.h_display()),
542 .bottom = static_cast<int>(mode.v_display())};
543 client_layer_.SetLayerDisplayFrame(display_frame);
544
545 return HWC2::Error::None;
546}
547
548/* Find API details at:
549 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
550 */
551HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
552 int32_t acquire_fence,
553 int32_t dataspace,
554 hwc_region_t /*damage*/) {
555 client_layer_.SetLayerBuffer(target, acquire_fence);
556 client_layer_.SetLayerDataspace(dataspace);
557
558 /*
559 * target can be nullptr, this does mean the Composer Service is calling
560 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
561 * 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
562 */
563 if (target == nullptr) {
564 return HWC2::Error::None;
565 }
566
567 /* TODO: Do not update source_crop every call.
568 * It makes sense to do it once after every hotplug event. */
569 HwcDrmBo bo{};
570 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
571
572 hwc_frect_t source_crop = {.left = 0.0F,
573 .top = 0.0F,
574 .right = static_cast<float>(bo.width),
575 .bottom = static_cast<float>(bo.height)};
576 client_layer_.SetLayerSourceCrop(source_crop);
577
578 return HWC2::Error::None;
579}
580
581HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
582 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
583 return HWC2::Error::BadParameter;
584
585 if (mode != HAL_COLOR_MODE_NATIVE)
586 return HWC2::Error::Unsupported;
587
588 color_mode_ = mode;
589 return HWC2::Error::None;
590}
591
592HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
593 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
594 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
595 return HWC2::Error::BadParameter;
596
597 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
598 return HWC2::Error::BadParameter;
599
600 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
601 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
602 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
603
604 return HWC2::Error::None;
605}
606
607HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
608 int32_t /*release_fence*/) {
609 // TODO(nobody): Need virtual display support
610 return HWC2::Error::Unsupported;
611}
612
613HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200614 if (IsInHeadlessMode()) {
615 return HWC2::Error::None;
616 }
617
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200618 auto mode = static_cast<HWC2::PowerMode>(mode_in);
619 AtomicCommitArgs a_args{};
620
621 switch (mode) {
622 case HWC2::PowerMode::Off:
623 a_args.active = false;
624 break;
625 case HWC2::PowerMode::On:
626 /*
627 * Setting the display to active before we have a composition
628 * can break some drivers, so skip setting a_args.active to
629 * true, as the next composition frame will implicitly activate
630 * the display
631 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200632 return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200633 ? HWC2::Error::None
634 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 break;
636 case HWC2::PowerMode::Doze:
637 case HWC2::PowerMode::DozeSuspend:
638 return HWC2::Error::Unsupported;
639 default:
640 ALOGI("Power mode %d is unsupported\n", mode);
641 return HWC2::Error::BadParameter;
642 };
643
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200644 int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200645 if (err) {
646 ALOGE("Failed to apply the dpms composition err=%d", err);
647 return HWC2::Error::BadParameter;
648 }
649 return HWC2::Error::None;
650}
651
652HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
653 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
654 return HWC2::Error::None;
655}
656
657HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
658 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200659 if (IsInHeadlessMode()) {
660 *num_types = *num_requests = 0;
661 return HWC2::Error::None;
662 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200663 return backend_->ValidateDisplay(this, num_types, num_requests);
664}
665
666std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
667 std::vector<HwcLayer *> ordered_layers;
668 ordered_layers.reserve(layers_.size());
669
670 for (auto &[handle, layer] : layers_) {
671 ordered_layers.emplace_back(&layer);
672 }
673
674 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
675 [](const HwcLayer *lhs, const HwcLayer *rhs) {
676 return lhs->GetZOrder() < rhs->GetZOrder();
677 });
678
679 return ordered_layers;
680}
681
682#if PLATFORM_SDK_VERSION > 29
683HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200684 if (IsInHeadlessMode()) {
685 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
686 return HWC2::Error::None;
687 }
688 /* Primary display should be always internal,
689 * otherwise SF will be unhappy and will crash
690 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200691 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200692 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200693 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200694 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
695 else
696 return HWC2::Error::BadConfig;
697
698 return HWC2::Error::None;
699}
700
701HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
702 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200703 return GetDisplayAttribute(configs_.active_config_id,
704 HWC2_ATTRIBUTE_VSYNC_PERIOD,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200705 (int32_t *)(outVsyncPeriod));
706}
707
708HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
709 hwc2_config_t /*config*/,
710 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
711 hwc_vsync_period_change_timeline_t *outTimeline) {
712 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
713 return HWC2::Error::BadParameter;
714 }
715
716 return HWC2::Error::BadConfig;
717}
718
719HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
720 return HWC2::Error::Unsupported;
721}
722
723HWC2::Error HwcDisplay::GetSupportedContentTypes(
724 uint32_t *outNumSupportedContentTypes,
725 const uint32_t *outSupportedContentTypes) {
726 if (outSupportedContentTypes == nullptr)
727 *outNumSupportedContentTypes = 0;
728
729 return HWC2::Error::None;
730}
731
732HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
733 if (contentType != HWC2_CONTENT_TYPE_NONE)
734 return HWC2::Error::Unsupported;
735
736 /* TODO: Map to the DRM Connector property:
737 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
738 */
739
740 return HWC2::Error::None;
741}
742#endif
743
744#if PLATFORM_SDK_VERSION > 28
745HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
746 uint32_t *outDataSize,
747 uint8_t *outData) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200748 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200749
750 if (!blob) {
751 ALOGE("Failed to get edid property value.");
752 return HWC2::Error::Unsupported;
753 }
754
755 if (outData) {
756 *outDataSize = std::min(*outDataSize, blob->length);
757 memcpy(outData, blob->data, *outDataSize);
758 } else {
759 *outDataSize = blob->length;
760 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200761 *outPort = GetPipe().connector->Get()->GetId();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200762
763 return HWC2::Error::None;
764}
765
766HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
767 uint32_t * /*outCapabilities*/) {
768 if (outNumCapabilities == nullptr) {
769 return HWC2::Error::BadParameter;
770 }
771
772 *outNumCapabilities = 0;
773
774 return HWC2::Error::None;
775}
776
777HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
778 *supported = false;
779 return HWC2::Error::None;
780}
781
782HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
783 return HWC2::Error::Unsupported;
784}
785
786#endif /* PLATFORM_SDK_VERSION > 28 */
787
788#if PLATFORM_SDK_VERSION > 27
789
790HWC2::Error HwcDisplay::GetRenderIntents(
791 int32_t mode, uint32_t *outNumIntents,
792 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
793 if (mode != HAL_COLOR_MODE_NATIVE) {
794 return HWC2::Error::BadParameter;
795 }
796
797 if (outIntents == nullptr) {
798 *outNumIntents = 1;
799 return HWC2::Error::None;
800 }
801 *outNumIntents = 1;
802 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
803 return HWC2::Error::None;
804}
805
806HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
807 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
808 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
809 return HWC2::Error::BadParameter;
810
811 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
812 return HWC2::Error::BadParameter;
813
814 if (mode != HAL_COLOR_MODE_NATIVE)
815 return HWC2::Error::Unsupported;
816
817 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
818 return HWC2::Error::Unsupported;
819
820 color_mode_ = mode;
821 return HWC2::Error::None;
822}
823
824#endif /* PLATFORM_SDK_VERSION > 27 */
825
826const Backend *HwcDisplay::backend() const {
827 return backend_.get();
828}
829
830void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
831 backend_ = std::move(backend);
832}
833
834} // namespace android