blob: 1781d93e18548eaadf4803c3f378558d9725cdfa [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 ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-display-compositor"
19
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmDisplayCompositor.h"
Zach Reiznerbff33ac2015-11-16 11:08:46 -080021
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030022#include <drm/drm_mode.h>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080023#include <pthread.h>
24#include <sched.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030025#include <sync/sync.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030026#include <utils/Trace.h>
27
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020028#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020029#include <cstdlib>
30#include <ctime>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080031#include <sstream>
32#include <vector>
33
Roman Stratiienko13cc3662020-08-29 21:35:39 +030034#include "drm/DrmCrtc.h"
35#include "drm/DrmDevice.h"
36#include "drm/DrmPlane.h"
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030037#include "drm/DrmUnique.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038#include "utils/autolock.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020039#include "utils/log.h"
Sean Paul98e73c82015-06-24 14:38:49 -070040
Sean Paul98e73c82015-06-24 14:38:49 -070041namespace android {
42
Roman Stratiienkoe8c06792021-09-30 10:09:31 +030043auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
Roman Stratiienko863a3c22021-09-29 13:00:29 +030044 -> int {
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010045 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -070046 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010047 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
48 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010049 ALOGE("Could not find drmdevice for display");
50 return -EINVAL;
51 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010052 planner_ = Planner::CreateInstance(drm);
53
Sean Paul98e73c82015-06-24 14:38:49 -070054 initialized_ = true;
55 return 0;
56}
57
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010058std::unique_ptr<DrmDisplayComposition>
59DrmDisplayCompositor::CreateInitializedComposition() const {
60 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
61 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
62 if (!crtc) {
63 ALOGE("Failed to find crtc for display = %d", display_);
64 return std::unique_ptr<DrmDisplayComposition>();
65 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +020066
67 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010068}
69
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020070// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030071auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
Haixia Shi3979f7d2015-10-29 14:33:37 -070072 ATRACE_CALL();
73
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030074 if (args.active && *args.active == active_kms_data.active_state) {
75 /* Don't set the same state twice */
76 args.active.reset();
77 }
Haixia Shidda2fab2015-10-22 18:12:49 -070078
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030079 if (!args.HasInputs()) {
80 /* nothing to do */
81 return 0;
82 }
83
84 if (!active_kms_data.active_state) {
85 /* Force activate display */
86 args.active = true;
87 }
88
89 if (args.clear_active_composition && args.composition) {
90 ALOGE("%s: Invalid arguments", __func__);
91 return -EINVAL;
92 }
93
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010094 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Haixia Shidda2fab2015-10-22 18:12:49 -070095
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010096 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -040097 if (!connector) {
98 ALOGE("Could not locate connector for display %d", display_);
99 return -ENODEV;
100 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100101 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400102 if (!crtc) {
103 ALOGE("Could not locate crtc for display %d", display_);
104 return -ENODEV;
105 }
106
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300107 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700108 if (!pset) {
109 ALOGE("Failed to allocate property set");
110 return -ENOMEM;
111 }
112
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300113 int64_t out_fence = -1;
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300114 if (crtc->out_fence_ptr_property() &&
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300115 !crtc->out_fence_ptr_property().AtomicSet(*pset, (uint64_t)&out_fence)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300116 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200117 }
118
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300119 DrmModeUserPropertyBlobUnique mode_blob;
120
121 if (args.active) {
122 if (!crtc->active_property().AtomicSet(*pset, *args.active) ||
123 !connector->crtc_id_property().AtomicSet(*pset, crtc->id())) {
124 return -EINVAL;
125 }
Sean Paul57355412015-09-19 09:14:34 -0400126 }
127
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300128 if (args.display_mode) {
129 mode_blob = args.display_mode.value().CreateModeBlob(
130 *resource_manager_->GetDrmDevice(display_));
Zach Reizner92f8e632015-10-12 17:47:13 -0700131
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300132 if (!mode_blob) {
133 ALOGE("Failed to create mode_blob");
134 return -EINVAL;
135 }
Sean Paulca699be2016-05-11 16:29:45 -0400136
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300137 if (!crtc->mode_property().AtomicSet(*pset, *mode_blob)) {
138 return -EINVAL;
139 }
140 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300141
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300142 if (args.composition) {
143 std::vector<DrmHwcLayer> &layers = args.composition->layers();
144 std::vector<DrmCompositionPlane> &comp_planes = args.composition
145 ->composition_planes();
146
147 for (DrmCompositionPlane &comp_plane : comp_planes) {
148 DrmPlane *plane = comp_plane.plane();
149 std::vector<size_t> &source_layers = comp_plane.source_layers();
150
151 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
152 if (source_layers.size() > 1) {
153 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
154 source_layers.size(), comp_plane.type());
155 continue;
156 }
157
158 if (source_layers.empty() || source_layers.front() >= layers.size()) {
159 ALOGE("Source layer index %zu out of bounds %zu type=%d",
160 source_layers.front(), layers.size(), comp_plane.type());
161 return -EINVAL;
162 }
163 DrmHwcLayer &layer = layers[source_layers.front()];
164
165 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
166 crtc->id()) != 0) {
167 return -EINVAL;
168 }
169 } else {
170 if (plane->AtomicDisablePlane(*pset) != 0) {
171 return -EINVAL;
172 }
Sean Paul39b37842016-05-11 13:50:28 -0400173 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300174 }
175 }
176
177 if (args.clear_active_composition && active_kms_data.composition) {
178 auto &comp_planes = active_kms_data.composition->composition_planes();
179 for (auto &comp_plane : comp_planes) {
180 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300181 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300182 }
183 }
Sean Paul98e73c82015-06-24 14:38:49 -0700184 }
185
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300186 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
187 if (args.test_only)
188 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
Sean Paulc07b2112015-11-17 16:38:10 -0500189
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300190 int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
191 if (err) {
192 if (!args.test_only)
193 ALOGE("Failed to commit pset ret=%d\n", err);
194 return err;
Sean Paul98e73c82015-06-24 14:38:49 -0700195 }
Sean Paul98e73c82015-06-24 14:38:49 -0700196
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300197 if (!args.test_only) {
198 if (args.display_mode) {
199 connector->set_active_mode(*args.display_mode);
200 active_kms_data.mode_blob = std::move(mode_blob);
Sean Paul57355412015-09-19 09:14:34 -0400201 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300202
203 if (args.clear_active_composition) {
204 active_kms_data.composition.reset();
205 }
206
207 if (args.composition) {
208 active_kms_data.composition = args.composition;
209 }
210
211 if (args.active) {
212 active_kms_data.active_state = *args.active;
213 }
Sean Paul57355412015-09-19 09:14:34 -0400214
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300215 if (crtc->out_fence_ptr_property()) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300216 args.out_fence = UniqueFd((int)out_fence);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300217 }
Robert Fossa1ade4e2017-09-27 19:28:15 +0200218 }
219
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300220 return 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700221}
222
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300223auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
224 int err = CommitFrame(args);
Sean Paul137a6a82016-06-22 22:48:22 -0400225
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300226 if (!args.test_only) {
227 if (err) {
228 ALOGE("Composite failed for display %d", display_);
229 // Disable the hw used by the last active composition. This allows us to
230 // signal the release fences from that composition to avoid hanging.
231 AtomicCommitArgs cl_args = {.clear_active_composition = true};
232 if (CommitFrame(cl_args)) {
233 ALOGE("Failed to clean-up active composition for display %d", display_);
234 }
235 return err;
236 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700237 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700238
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300239 return err;
240} // namespace android
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300241
Sean Paulf72cccd2018-08-27 13:59:08 -0400242} // namespace android