blob: c8f5e69b554dcdd881038f525d67f6f70258329e [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
71 std::stringstream ss;
72 ss << "- Display on: " << connector_->name() << "\n"
73 << " Flattening state: " << flattening_state_str << "\n"
74 << "Statistics since system boot:\n"
75 << DumpDelta(total_stats_) << "\n\n"
76 << "Statistics since last dumpsys request:\n"
77 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
78
79 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
80 return ss.str();
81}
82
83HwcDisplay::HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
84 hwc2_display_t handle, HWC2::DisplayType type,
85 DrmHwcTwo *hwc2)
86 : hwc2_(hwc2),
87 resource_manager_(resource_manager),
88 drm_(drm),
89 handle_(handle),
90 type_(type),
91 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
92 // clang-format off
93 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
94 0.0, 1.0, 0.0, 0.0,
95 0.0, 0.0, 1.0, 0.0,
96 0.0, 0.0, 0.0, 1.0};
97 // clang-format on
98}
99
100void HwcDisplay::ClearDisplay() {
101 AtomicCommitArgs a_args = {.clear_active_composition = true};
102 compositor_.ExecuteAtomicCommit(a_args);
103}
104
105HWC2::Error HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
106 int display = static_cast<int>(handle_);
107 int ret = compositor_.Init(resource_manager_, display);
108 if (ret) {
109 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
110 return HWC2::Error::NoResources;
111 }
112
113 // Split up the given display planes into primary and overlay to properly
114 // interface with the composition
115 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
116 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
117 "1");
118 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
119 for (auto &plane : *planes) {
120 if (plane->GetType() == DRM_PLANE_TYPE_PRIMARY)
121 primary_planes_.push_back(plane);
122 else if (use_overlay_planes && (plane)->GetType() == DRM_PLANE_TYPE_OVERLAY)
123 overlay_planes_.push_back(plane);
124 }
125
126 crtc_ = drm_->GetCrtcForDisplay(display);
127 if (!crtc_) {
128 ALOGE("Failed to get crtc for display %d", display);
129 return HWC2::Error::BadDisplay;
130 }
131
132 connector_ = drm_->GetConnectorForDisplay(display);
133 if (!connector_) {
134 ALOGE("Failed to get connector for display %d", display);
135 return HWC2::Error::BadDisplay;
136 }
137
138 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200139 const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 /* vsync callback */
141#if PLATFORM_SDK_VERSION > 29
142 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
143 hwc2_->vsync_2_4_callback_.second != nullptr) {
144 hwc2_vsync_period_t period_ns{};
145 GetDisplayVsyncPeriod(&period_ns);
146 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
147 handle_, timestamp, period_ns);
148 } else
149#endif
150 if (hwc2_->vsync_callback_.first != nullptr &&
151 hwc2_->vsync_callback_.second != nullptr) {
152 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
153 timestamp);
154 }
155 });
156 if (ret) {
157 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
158 return HWC2::Error::BadDisplay;
159 }
160
161 ret = flattening_vsync_worker_
162 .Init(drm_, display, [this](int64_t /*timestamp*/) {
Roman Stratiienko74923582022-01-17 11:24:21 +0200163 const std::lock_guard<std::mutex> lock(
164 hwc2_->GetResMan().GetMainLock());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200165 /* Frontend flattening */
166 if (flattenning_state_ >
167 ClientFlattenningState::ClientRefreshRequested &&
168 --flattenning_state_ ==
169 ClientFlattenningState::ClientRefreshRequested &&
170 hwc2_->refresh_callback_.first != nullptr &&
171 hwc2_->refresh_callback_.second != nullptr) {
172 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second,
173 handle_);
174 flattening_vsync_worker_.VSyncControl(false);
175 }
176 });
177 if (ret) {
178 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
179 return HWC2::Error::BadDisplay;
180 }
181
182 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
183 if (ret) {
184 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
185 return HWC2::Error::BadDisplay;
186 }
187
188 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
189
190 return ChosePreferredConfig();
191}
192
193HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200194 HWC2::Error err = configs_.Update(*connector_);
195 if (err != HWC2::Error::None)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200196 return HWC2::Error::BadDisplay;
197
Roman Stratiienko0137f862022-01-04 18:27:40 +0200198 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199}
200
201HWC2::Error HwcDisplay::AcceptDisplayChanges() {
202 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
203 l.second.AcceptTypeChange();
204 return HWC2::Error::None;
205}
206
207HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
208 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
209 *layer = static_cast<hwc2_layer_t>(layer_idx_);
210 ++layer_idx_;
211 return HWC2::Error::None;
212}
213
214HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
215 if (!get_layer(layer))
216 return HWC2::Error::BadLayer;
217
218 layers_.erase(layer);
219 return HWC2::Error::None;
220}
221
222HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200223 if (configs_.hwc_configs.count(configs_.active_config_id) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200224 return HWC2::Error::BadConfig;
225
Roman Stratiienko0137f862022-01-04 18:27:40 +0200226 *config = configs_.active_config_id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200227 return HWC2::Error::None;
228}
229
230HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
231 hwc2_layer_t *layers,
232 int32_t *types) {
233 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) {
251 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
252 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
253
254 if (width < min.first || height < min.second)
255 return HWC2::Error::Unsupported;
256
257 if (width > max.first || height > max.second)
258 return HWC2::Error::Unsupported;
259
260 if (dataspace != HAL_DATASPACE_UNKNOWN)
261 return HWC2::Error::Unsupported;
262
263 // TODO(nobody): Validate format can be handled by either GL or planes
264 return HWC2::Error::None;
265}
266
267HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
268 if (!modes)
269 *num_modes = 1;
270
271 if (modes)
272 *modes = HAL_COLOR_MODE_NATIVE;
273
274 return HWC2::Error::None;
275}
276
277HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
278 int32_t attribute_in,
279 int32_t *value) {
280 int conf = static_cast<int>(config);
281
Roman Stratiienko0137f862022-01-04 18:27:40 +0200282 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200283 ALOGE("Could not find active mode for %d", conf);
284 return HWC2::Error::BadConfig;
285 }
286
Roman Stratiienko0137f862022-01-04 18:27:40 +0200287 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200288
289 static const int32_t kUmPerInch = 25400;
290 uint32_t mm_width = connector_->mm_width();
291 uint32_t mm_height = connector_->mm_height();
292 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
293 switch (attribute) {
294 case HWC2::Attribute::Width:
295 *value = static_cast<int>(hwc_config.mode.h_display());
296 break;
297 case HWC2::Attribute::Height:
298 *value = static_cast<int>(hwc_config.mode.v_display());
299 break;
300 case HWC2::Attribute::VsyncPeriod:
301 // in nanoseconds
302 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
303 break;
304 case HWC2::Attribute::DpiX:
305 // Dots per 1000 inches
306 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
307 kUmPerInch / mm_width)
308 : -1;
309 break;
310 case HWC2::Attribute::DpiY:
311 // Dots per 1000 inches
312 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
313 kUmPerInch / mm_height)
314 : -1;
315 break;
316#if PLATFORM_SDK_VERSION > 29
317 case HWC2::Attribute::ConfigGroup:
318 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
319 * able to request it even if service @2.1 is used */
320 *value = hwc_config.group_id;
321 break;
322#endif
323 default:
324 *value = -1;
325 return HWC2::Error::BadConfig;
326 }
327 return HWC2::Error::None;
328}
329
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200330HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
331 hwc2_config_t *configs) {
332 // Since this callback is normally invoked twice (once to get the count, and
333 // once to populate configs), we don't really want to read the edid
334 // redundantly. Instead, only update the modes on the first invocation. While
335 // it's possible this will result in stale modes, it'll all come out in the
336 // wash when we try to set the active config later.
337 if (!configs) {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200338 configs_.Update(*connector_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200339 }
340
341 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200342 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200343 if (hwc_config.second.disabled) {
344 continue;
345 }
346
347 if (configs != nullptr) {
348 if (idx >= *num_configs) {
349 break;
350 }
351 configs[idx] = hwc_config.second.id;
352 }
353
354 idx++;
355 }
356 *num_configs = idx;
357 return HWC2::Error::None;
358}
359
360HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
361 std::ostringstream stream;
362 stream << "display-" << connector_->id();
363 std::string string = stream.str();
364 size_t length = string.length();
365 if (!name) {
366 *size = length;
367 return HWC2::Error::None;
368 }
369
370 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
371 strncpy(name, string.c_str(), *size);
372 return HWC2::Error::None;
373}
374
375HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
376 uint32_t *num_elements,
377 hwc2_layer_t * /*layers*/,
378 int32_t * /*layer_requests*/) {
379 // TODO(nobody): I think virtual display should request
380 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
381 *num_elements = 0;
382 return HWC2::Error::None;
383}
384
385HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
386 *type = static_cast<int32_t>(type_);
387 return HWC2::Error::None;
388}
389
390HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
391 *support = 0;
392 return HWC2::Error::None;
393}
394
395HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
396 int32_t * /*types*/,
397 float * /*max_luminance*/,
398 float * /*max_average_luminance*/,
399 float * /*min_luminance*/) {
400 *num_types = 0;
401 return HWC2::Error::None;
402}
403
404/* Find API details at:
405 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
406 */
407HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
408 hwc2_layer_t *layers,
409 int32_t *fences) {
410 uint32_t num_layers = 0;
411
412 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
413 ++num_layers;
414 if (layers == nullptr || fences == nullptr)
415 continue;
416
417 if (num_layers > *num_elements) {
418 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
419 return HWC2::Error::None;
420 }
421
422 layers[num_layers - 1] = l.first;
423 fences[num_layers - 1] = l.second.GetReleaseFence().Release();
424 }
425 *num_elements = num_layers;
426 return HWC2::Error::None;
427}
428
429HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
430 // order the layers by z-order
431 bool use_client_layer = false;
432 uint32_t client_z_order = UINT32_MAX;
433 std::map<uint32_t, HwcLayer *> z_map;
434 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
435 switch (l.second.GetValidatedType()) {
436 case HWC2::Composition::Device:
437 z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
438 break;
439 case HWC2::Composition::Client:
440 // Place it at the z_order of the lowest client layer
441 use_client_layer = true;
442 client_z_order = std::min(client_z_order, l.second.GetZOrder());
443 break;
444 default:
445 continue;
446 }
447 }
448 if (use_client_layer)
449 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
450
451 if (z_map.empty())
452 return HWC2::Error::BadLayer;
453
454 std::vector<DrmHwcLayer> composition_layers;
455
456 // now that they're ordered by z, add them to the composition
457 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
458 DrmHwcLayer layer;
459 l.second->PopulateDrmLayer(&layer);
460 int ret = layer.ImportBuffer(drm_);
461 if (ret) {
462 ALOGE("Failed to import layer, ret=%d", ret);
463 return HWC2::Error::NoResources;
464 }
465 composition_layers.emplace_back(std::move(layer));
466 }
467
468 auto composition = std::make_shared<DrmDisplayComposition>(crtc_);
469
470 // TODO(nobody): Don't always assume geometry changed
471 int ret = composition->SetLayers(composition_layers.data(),
472 composition_layers.size());
473 if (ret) {
474 ALOGE("Failed to set layers in the composition ret=%d", ret);
475 return HWC2::Error::BadLayer;
476 }
477
478 std::vector<DrmPlane *> primary_planes(primary_planes_);
479 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
480 ret = composition->Plan(&primary_planes, &overlay_planes);
481 if (ret) {
482 ALOGV("Failed to plan the composition ret=%d", ret);
483 return HWC2::Error::BadConfig;
484 }
485
486 a_args.composition = composition;
487 if (staged_mode) {
488 a_args.display_mode = *staged_mode;
489 }
490 ret = compositor_.ExecuteAtomicCommit(a_args);
491
492 if (ret) {
493 if (!a_args.test_only)
494 ALOGE("Failed to apply the frame composition ret=%d", ret);
495 return HWC2::Error::BadParameter;
496 }
497
498 if (!a_args.test_only) {
499 staged_mode.reset();
500 }
501
502 return HWC2::Error::None;
503}
504
505/* Find API details at:
506 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
507 */
508HWC2::Error HwcDisplay::PresentDisplay(int32_t *present_fence) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200509 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510
511 ++total_stats_.total_frames_;
512
513 AtomicCommitArgs a_args{};
514 ret = CreateComposition(a_args);
515
516 if (ret != HWC2::Error::None)
517 ++total_stats_.failed_kms_present_;
518
519 if (ret == HWC2::Error::BadLayer) {
520 // Can we really have no client or device layers?
521 *present_fence = -1;
522 return HWC2::Error::None;
523 }
524 if (ret != HWC2::Error::None)
525 return ret;
526
527 *present_fence = a_args.out_fence.Release();
528
529 ++frame_no_;
530 return HWC2::Error::None;
531}
532
533HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
534 int conf = static_cast<int>(config);
535
Roman Stratiienko0137f862022-01-04 18:27:40 +0200536 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537 ALOGE("Could not find active mode for %d", conf);
538 return HWC2::Error::BadConfig;
539 }
540
Roman Stratiienko0137f862022-01-04 18:27:40 +0200541 auto &mode = configs_.hwc_configs[conf].mode;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200542
543 staged_mode = mode;
544
Roman Stratiienko0137f862022-01-04 18:27:40 +0200545 configs_.active_config_id = conf;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546
547 // Setup the client layer's dimensions
548 hwc_rect_t display_frame = {.left = 0,
549 .top = 0,
550 .right = static_cast<int>(mode.h_display()),
551 .bottom = static_cast<int>(mode.v_display())};
552 client_layer_.SetLayerDisplayFrame(display_frame);
553
554 return HWC2::Error::None;
555}
556
557/* Find API details at:
558 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
559 */
560HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
561 int32_t acquire_fence,
562 int32_t dataspace,
563 hwc_region_t /*damage*/) {
564 client_layer_.SetLayerBuffer(target, acquire_fence);
565 client_layer_.SetLayerDataspace(dataspace);
566
567 /*
568 * target can be nullptr, this does mean the Composer Service is calling
569 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
570 * 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
571 */
572 if (target == nullptr) {
573 return HWC2::Error::None;
574 }
575
576 /* TODO: Do not update source_crop every call.
577 * It makes sense to do it once after every hotplug event. */
578 HwcDrmBo bo{};
579 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
580
581 hwc_frect_t source_crop = {.left = 0.0F,
582 .top = 0.0F,
583 .right = static_cast<float>(bo.width),
584 .bottom = static_cast<float>(bo.height)};
585 client_layer_.SetLayerSourceCrop(source_crop);
586
587 return HWC2::Error::None;
588}
589
590HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
591 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
592 return HWC2::Error::BadParameter;
593
594 if (mode != HAL_COLOR_MODE_NATIVE)
595 return HWC2::Error::Unsupported;
596
597 color_mode_ = mode;
598 return HWC2::Error::None;
599}
600
601HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
602 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
603 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
604 return HWC2::Error::BadParameter;
605
606 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
607 return HWC2::Error::BadParameter;
608
609 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
610 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
611 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
612
613 return HWC2::Error::None;
614}
615
616HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
617 int32_t /*release_fence*/) {
618 // TODO(nobody): Need virtual display support
619 return HWC2::Error::Unsupported;
620}
621
622HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
623 auto mode = static_cast<HWC2::PowerMode>(mode_in);
624 AtomicCommitArgs a_args{};
625
626 switch (mode) {
627 case HWC2::PowerMode::Off:
628 a_args.active = false;
629 break;
630 case HWC2::PowerMode::On:
631 /*
632 * Setting the display to active before we have a composition
633 * can break some drivers, so skip setting a_args.active to
634 * true, as the next composition frame will implicitly activate
635 * the display
636 */
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200637 return compositor_.ActivateDisplayUsingDPMS() == 0
638 ? HWC2::Error::None
639 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200640 break;
641 case HWC2::PowerMode::Doze:
642 case HWC2::PowerMode::DozeSuspend:
643 return HWC2::Error::Unsupported;
644 default:
645 ALOGI("Power mode %d is unsupported\n", mode);
646 return HWC2::Error::BadParameter;
647 };
648
649 int err = compositor_.ExecuteAtomicCommit(a_args);
650 if (err) {
651 ALOGE("Failed to apply the dpms composition err=%d", err);
652 return HWC2::Error::BadParameter;
653 }
654 return HWC2::Error::None;
655}
656
657HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
658 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
659 return HWC2::Error::None;
660}
661
662HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
663 uint32_t *num_requests) {
664 return backend_->ValidateDisplay(this, num_types, num_requests);
665}
666
667std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
668 std::vector<HwcLayer *> ordered_layers;
669 ordered_layers.reserve(layers_.size());
670
671 for (auto &[handle, layer] : layers_) {
672 ordered_layers.emplace_back(&layer);
673 }
674
675 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
676 [](const HwcLayer *lhs, const HwcLayer *rhs) {
677 return lhs->GetZOrder() < rhs->GetZOrder();
678 });
679
680 return ordered_layers;
681}
682
683#if PLATFORM_SDK_VERSION > 29
684HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
685 if (connector_->internal())
686 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
687 else if (connector_->external())
688 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
689 else
690 return HWC2::Error::BadConfig;
691
692 return HWC2::Error::None;
693}
694
695HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
696 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
Roman Stratiienko0137f862022-01-04 18:27:40 +0200697 return GetDisplayAttribute(configs_.active_config_id,
698 HWC2_ATTRIBUTE_VSYNC_PERIOD,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200699 (int32_t *)(outVsyncPeriod));
700}
701
702HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
703 hwc2_config_t /*config*/,
704 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
705 hwc_vsync_period_change_timeline_t *outTimeline) {
706 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
707 return HWC2::Error::BadParameter;
708 }
709
710 return HWC2::Error::BadConfig;
711}
712
713HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
714 return HWC2::Error::Unsupported;
715}
716
717HWC2::Error HwcDisplay::GetSupportedContentTypes(
718 uint32_t *outNumSupportedContentTypes,
719 const uint32_t *outSupportedContentTypes) {
720 if (outSupportedContentTypes == nullptr)
721 *outNumSupportedContentTypes = 0;
722
723 return HWC2::Error::None;
724}
725
726HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
727 if (contentType != HWC2_CONTENT_TYPE_NONE)
728 return HWC2::Error::Unsupported;
729
730 /* TODO: Map to the DRM Connector property:
731 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
732 */
733
734 return HWC2::Error::None;
735}
736#endif
737
738#if PLATFORM_SDK_VERSION > 28
739HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
740 uint32_t *outDataSize,
741 uint8_t *outData) {
742 auto blob = connector_->GetEdidBlob();
743
744 if (!blob) {
745 ALOGE("Failed to get edid property value.");
746 return HWC2::Error::Unsupported;
747 }
748
749 if (outData) {
750 *outDataSize = std::min(*outDataSize, blob->length);
751 memcpy(outData, blob->data, *outDataSize);
752 } else {
753 *outDataSize = blob->length;
754 }
755 *outPort = connector_->id();
756
757 return HWC2::Error::None;
758}
759
760HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
761 uint32_t * /*outCapabilities*/) {
762 if (outNumCapabilities == nullptr) {
763 return HWC2::Error::BadParameter;
764 }
765
766 *outNumCapabilities = 0;
767
768 return HWC2::Error::None;
769}
770
771HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
772 *supported = false;
773 return HWC2::Error::None;
774}
775
776HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
777 return HWC2::Error::Unsupported;
778}
779
780#endif /* PLATFORM_SDK_VERSION > 28 */
781
782#if PLATFORM_SDK_VERSION > 27
783
784HWC2::Error HwcDisplay::GetRenderIntents(
785 int32_t mode, uint32_t *outNumIntents,
786 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
787 if (mode != HAL_COLOR_MODE_NATIVE) {
788 return HWC2::Error::BadParameter;
789 }
790
791 if (outIntents == nullptr) {
792 *outNumIntents = 1;
793 return HWC2::Error::None;
794 }
795 *outNumIntents = 1;
796 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
797 return HWC2::Error::None;
798}
799
800HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
801 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
802 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
803 return HWC2::Error::BadParameter;
804
805 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
806 return HWC2::Error::BadParameter;
807
808 if (mode != HAL_COLOR_MODE_NATIVE)
809 return HWC2::Error::Unsupported;
810
811 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
812 return HWC2::Error::Unsupported;
813
814 color_mode_ = mode;
815 return HWC2::Error::None;
816}
817
818#endif /* PLATFORM_SDK_VERSION > 27 */
819
820const Backend *HwcDisplay::backend() const {
821 return backend_.get();
822}
823
824void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
825 backend_ = std::move(backend);
826}
827
828} // namespace android