blob: c0fbba07b40d52853216d371013d1e187a26109d [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
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 Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmDisplayComposition.h"
Sean Paul98e73c82015-06-24 14:38:49 -070020
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030021#include <sync/sync.h>
22#include <xf86drmMode.h>
Sean Paul98e73c82015-06-24 14:38:49 -070023
Zach Reizner92f8e632015-10-12 17:47:13 -070024#include <algorithm>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020025#include <cstdlib>
Zach Reizner92f8e632015-10-12 17:47:13 -070026#include <unordered_set>
27
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "DrmDisplayCompositor.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030029#include "Planner.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "drm/DrmDevice.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020031#include "utils/log.h"
Sean Paul98e73c82015-06-24 14:38:49 -070032
33namespace android {
34
Matvii Zorin704ea0e2021-01-08 12:55:45 +020035DrmDisplayComposition::DrmDisplayComposition(DrmCrtc *crtc, Planner *planner)
36 : crtc_(crtc), // Can be NULL if we haven't modeset yet
37 planner_(planner) {
Sean Paul98e73c82015-06-24 14:38:49 -070038}
39
Sean Paulacb2a442015-06-24 18:43:01 -070040bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
41 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
42}
43
Zach Reizner5757e822015-10-16 19:06:31 -070044int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
45 bool geometry_changed) {
Zach Reizner92f8e632015-10-12 17:47:13 -070046 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
47 return -EINVAL;
48
Zach Reizner5757e822015-10-16 19:06:31 -070049 geometry_changed_ = geometry_changed;
50
Zach Reizner92f8e632015-10-12 17:47:13 -070051 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
59int 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
67int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020068 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET)) {
69 ALOGE("SetDisplayMode() Failed to validate composition type");
Zach Reizner92f8e632015-10-12 17:47:13 -070070 return -EINVAL;
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020071 }
Zach Reizner92f8e632015-10-12 17:47:13 -070072 display_mode_ = display_mode;
73 dpms_mode_ = DRM_MODE_DPMS_ON;
74 type_ = DRM_COMPOSITION_TYPE_MODESET;
75 return 0;
76}
77
78int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Matvii Zorinf0757c22021-01-18 15:54:22 +020079 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane);
Zach Reizner92f8e632015-10-12 17:47:13 -070080 return 0;
81}
82
Sean Paul4f4ef692016-05-03 16:40:59 -070083int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
84 composition_planes_.emplace_back(std::move(plane));
85 return 0;
86}
87
Rob Herringaf0d9752018-05-04 16:34:19 -050088int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -070089 std::vector<DrmPlane *> *overlay_planes) {
90 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
91 return 0;
92
Sean Paulaa18d912016-05-12 14:28:05 -040093 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -070094
Rob Herringaf0d9752018-05-04 16:34:19 -050095 for (size_t i = 0; i < layers_.size(); ++i)
96 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -070097
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020098 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -040099 std::tie(ret,
100 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
101 primary_planes,
102 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400103 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000104 ALOGV("Planner failed provisioning planes ret=%d", ret);
Sean Paulaa18d912016-05-12 14:28:05 -0400105 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 Reizner5757e822015-10-16 19:06:31 -0700112 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700113
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700114 // make sure that source layers are ordered based on zorder
115 std::sort(i.source_layers().begin(), i.source_layers().end());
116
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200117 std::vector<DrmPlane *> *container = nullptr;
Sean Paulaa18d912016-05-12 14:28:05 -0400118 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 Reiznerdb81fce2015-10-27 16:18:06 -0700127 }
128 }
Zach Reizner5757e822015-10-16 19:06:31 -0700129
Rob Herringaf0d9752018-05-04 16:34:19 -0500130 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700131}
132
Sean Paulf72cccd2018-08-27 13:59:08 -0400133} // namespace android