blob: 447d75e5f82b16f686e73fc84efee9183bd503ab [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 Stratiienkodccc6fb2021-10-23 17:35:44 +030070auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
Haixia Shi3979f7d2015-10-29 14:33:37 -070071 ATRACE_CALL();
72
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030073 if (args.active && *args.active == active_kms_data.active_state) {
74 /* Don't set the same state twice */
75 args.active.reset();
76 }
Haixia Shidda2fab2015-10-22 18:12:49 -070077
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030078 if (!args.HasInputs()) {
79 /* nothing to do */
80 return 0;
81 }
82
83 if (!active_kms_data.active_state) {
84 /* Force activate display */
85 args.active = true;
86 }
87
88 if (args.clear_active_composition && args.composition) {
89 ALOGE("%s: Invalid arguments", __func__);
90 return -EINVAL;
91 }
92
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010093 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Haixia Shidda2fab2015-10-22 18:12:49 -070094
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010095 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -040096 if (!connector) {
97 ALOGE("Could not locate connector for display %d", display_);
98 return -ENODEV;
99 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100100 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400101 if (!crtc) {
102 ALOGE("Could not locate crtc for display %d", display_);
103 return -ENODEV;
104 }
105
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300106 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700107 if (!pset) {
108 ALOGE("Failed to allocate property set");
109 return -ENOMEM;
110 }
111
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300112 int64_t out_fence = -1;
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300113 if (crtc->out_fence_ptr_property() &&
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300114 !crtc->out_fence_ptr_property().AtomicSet(*pset, (uint64_t)&out_fence)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300115 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200116 }
117
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300118 DrmModeUserPropertyBlobUnique mode_blob;
119
120 if (args.active) {
121 if (!crtc->active_property().AtomicSet(*pset, *args.active) ||
122 !connector->crtc_id_property().AtomicSet(*pset, crtc->id())) {
123 return -EINVAL;
124 }
Sean Paul57355412015-09-19 09:14:34 -0400125 }
126
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300127 if (args.display_mode) {
128 mode_blob = args.display_mode.value().CreateModeBlob(
129 *resource_manager_->GetDrmDevice(display_));
Zach Reizner92f8e632015-10-12 17:47:13 -0700130
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300131 if (!mode_blob) {
132 ALOGE("Failed to create mode_blob");
133 return -EINVAL;
134 }
Sean Paulca699be2016-05-11 16:29:45 -0400135
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300136 if (!crtc->mode_property().AtomicSet(*pset, *mode_blob)) {
137 return -EINVAL;
138 }
139 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300140
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300141 if (args.composition) {
142 std::vector<DrmHwcLayer> &layers = args.composition->layers();
143 std::vector<DrmCompositionPlane> &comp_planes = args.composition
144 ->composition_planes();
145
146 for (DrmCompositionPlane &comp_plane : comp_planes) {
147 DrmPlane *plane = comp_plane.plane();
148 std::vector<size_t> &source_layers = comp_plane.source_layers();
149
150 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
151 if (source_layers.size() > 1) {
152 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
153 source_layers.size(), comp_plane.type());
154 continue;
155 }
156
157 if (source_layers.empty() || source_layers.front() >= layers.size()) {
158 ALOGE("Source layer index %zu out of bounds %zu type=%d",
159 source_layers.front(), layers.size(), comp_plane.type());
160 return -EINVAL;
161 }
162 DrmHwcLayer &layer = layers[source_layers.front()];
163
164 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
165 crtc->id()) != 0) {
166 return -EINVAL;
167 }
168 } else {
169 if (plane->AtomicDisablePlane(*pset) != 0) {
170 return -EINVAL;
171 }
Sean Paul39b37842016-05-11 13:50:28 -0400172 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300173 }
174 }
175
176 if (args.clear_active_composition && active_kms_data.composition) {
177 auto &comp_planes = active_kms_data.composition->composition_planes();
178 for (auto &comp_plane : comp_planes) {
179 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300180 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300181 }
182 }
Sean Paul98e73c82015-06-24 14:38:49 -0700183 }
184
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300185 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
186 if (args.test_only)
187 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
Sean Paulc07b2112015-11-17 16:38:10 -0500188
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300189 int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
190 if (err) {
191 if (!args.test_only)
192 ALOGE("Failed to commit pset ret=%d\n", err);
193 return err;
Sean Paul98e73c82015-06-24 14:38:49 -0700194 }
Sean Paul98e73c82015-06-24 14:38:49 -0700195
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300196 if (!args.test_only) {
197 if (args.display_mode) {
198 connector->set_active_mode(*args.display_mode);
199 active_kms_data.mode_blob = std::move(mode_blob);
Sean Paul57355412015-09-19 09:14:34 -0400200 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300201
202 if (args.clear_active_composition) {
203 active_kms_data.composition.reset();
204 }
205
206 if (args.composition) {
207 active_kms_data.composition = args.composition;
208 }
209
210 if (args.active) {
211 active_kms_data.active_state = *args.active;
212 }
Sean Paul57355412015-09-19 09:14:34 -0400213
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300214 if (crtc->out_fence_ptr_property()) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300215 args.out_fence = UniqueFd((int)out_fence);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300216 }
Robert Fossa1ade4e2017-09-27 19:28:15 +0200217 }
218
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300219 return 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700220}
221
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300222auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
223 int err = CommitFrame(args);
Sean Paul137a6a82016-06-22 22:48:22 -0400224
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300225 if (!args.test_only) {
226 if (err) {
227 ALOGE("Composite failed for display %d", display_);
228 // Disable the hw used by the last active composition. This allows us to
229 // signal the release fences from that composition to avoid hanging.
230 AtomicCommitArgs cl_args = {.clear_active_composition = true};
231 if (CommitFrame(cl_args)) {
232 ALOGE("Failed to clean-up active composition for display %d", display_);
233 }
234 return err;
235 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700236 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700237
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300238 return err;
239} // namespace android
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300240
Sean Paulf72cccd2018-08-27 13:59:08 -0400241} // namespace android