Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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-drm-display-composition" |
| 18 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include "DrmDisplayComposition.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 20 | |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 21 | #include <sync/sync.h> |
| 22 | #include <xf86drmMode.h> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 23 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 24 | #include <algorithm> |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 25 | #include <cstdlib> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 26 | #include <unordered_set> |
| 27 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 28 | #include "DrmDisplayCompositor.h" |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 29 | #include "Planner.h" |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 30 | #include "drm/DrmDevice.h" |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 31 | #include "utils/log.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Matvii Zorin | 704ea0e | 2021-01-08 12:55:45 +0200 | [diff] [blame] | 35 | DrmDisplayComposition::DrmDisplayComposition(DrmCrtc *crtc, Planner *planner) |
| 36 | : crtc_(crtc), // Can be NULL if we haven't modeset yet |
| 37 | planner_(planner) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 40 | bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) { |
| 41 | return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des; |
| 42 | } |
| 43 | |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 44 | int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers, |
| 45 | bool geometry_changed) { |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 46 | if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME)) |
| 47 | return -EINVAL; |
| 48 | |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 49 | geometry_changed_ = geometry_changed; |
| 50 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 51 | for (size_t layer_index = 0; layer_index < num_layers; layer_index++) { |
| 52 | layers_.emplace_back(std::move(layers[layer_index])); |
| 53 | } |
| 54 | |
| 55 | type_ = DRM_COMPOSITION_TYPE_FRAME; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) { |
| 60 | if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS)) |
| 61 | return -EINVAL; |
| 62 | dpms_mode_ = dpms_mode; |
| 63 | type_ = DRM_COMPOSITION_TYPE_DPMS; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) { |
Roman Stratiienko | 6a10c4c | 2021-02-15 11:25:23 +0200 | [diff] [blame] | 68 | if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET)) { |
| 69 | ALOGE("SetDisplayMode() Failed to validate composition type"); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 70 | return -EINVAL; |
Roman Stratiienko | 6a10c4c | 2021-02-15 11:25:23 +0200 | [diff] [blame] | 71 | } |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 72 | display_mode_ = display_mode; |
| 73 | dpms_mode_ = DRM_MODE_DPMS_ON; |
| 74 | type_ = DRM_COMPOSITION_TYPE_MODESET; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) { |
Matvii Zorin | f0757c2 | 2021-01-18 15:54:22 +0200 | [diff] [blame] | 79 | composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Sean Paul | 4f4ef69 | 2016-05-03 16:40:59 -0700 | [diff] [blame] | 83 | int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) { |
| 84 | composition_planes_.emplace_back(std::move(plane)); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 88 | int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes, |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 89 | std::vector<DrmPlane *> *overlay_planes) { |
| 90 | if (type_ != DRM_COMPOSITION_TYPE_FRAME) |
| 91 | return 0; |
| 92 | |
Sean Paul | aa18d91 | 2016-05-12 14:28:05 -0400 | [diff] [blame] | 93 | std::map<size_t, DrmHwcLayer *> to_composite; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 94 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 95 | for (size_t i = 0; i < layers_.size(); ++i) |
| 96 | to_composite.emplace(std::make_pair(i, &layers_[i])); |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 97 | |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 98 | int ret = 0; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 99 | std::tie(ret, |
| 100 | composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_, |
| 101 | primary_planes, |
| 102 | overlay_planes); |
Sean Paul | aa18d91 | 2016-05-12 14:28:05 -0400 | [diff] [blame] | 103 | if (ret) { |
John Stultz | 66763d5 | 2021-08-24 04:59:25 +0000 | [diff] [blame] | 104 | ALOGV("Planner failed provisioning planes ret=%d", ret); |
Sean Paul | aa18d91 | 2016-05-12 14:28:05 -0400 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | // Remove the planes we used from the pool before returning. This ensures they |
| 109 | // won't be reused by another display in the composition. |
| 110 | for (auto &i : composition_planes_) { |
| 111 | if (!i.plane()) |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 112 | continue; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 113 | |
Adrian Salido | 1a1cf9b | 2017-09-21 16:53:48 -0700 | [diff] [blame] | 114 | // make sure that source layers are ordered based on zorder |
| 115 | std::sort(i.source_layers().begin(), i.source_layers().end()); |
| 116 | |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 117 | std::vector<DrmPlane *> *container = nullptr; |
Sean Paul | aa18d91 | 2016-05-12 14:28:05 -0400 | [diff] [blame] | 118 | if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY) |
| 119 | container = primary_planes; |
| 120 | else |
| 121 | container = overlay_planes; |
| 122 | for (auto j = container->begin(); j != container->end(); ++j) { |
| 123 | if (*j == i.plane()) { |
| 124 | container->erase(j); |
| 125 | break; |
| 126 | } |
Zach Reizner | db81fce | 2015-10-27 16:18:06 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 129 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 130 | return 0; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 133 | } // namespace android |