blob: bb6a33b83482fd71dfa2e126b4dd02ceeb9f9a38 [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
43DrmDisplayCompositor::DrmDisplayCompositor()
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020044 : resource_manager_(nullptr),
Sean Paul98e73c82015-06-24 14:38:49 -070045 display_(-1),
Sean Paul98e73c82015-06-24 14:38:49 -070046 initialized_(false),
Roman Stratiienko36a7f282021-10-05 18:17:53 +030047 active_(false) {
Sean Paul98e73c82015-06-24 14:38:49 -070048}
49
50DrmDisplayCompositor::~DrmDisplayCompositor() {
51 if (!initialized_)
52 return;
Sean Paul35301f42015-11-17 14:46:56 -050053
Sean Paul98e73c82015-06-24 14:38:49 -070054 active_composition_.reset();
Sean Paul98e73c82015-06-24 14:38:49 -070055}
56
Roman Stratiienkoe8c06792021-09-30 10:09:31 +030057auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
Roman Stratiienko863a3c22021-09-29 13:00:29 +030058 -> int {
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010059 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -070060 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010061 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
62 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010063 ALOGE("Could not find drmdevice for display");
64 return -EINVAL;
65 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010066 planner_ = Planner::CreateInstance(drm);
67
Sean Paul98e73c82015-06-24 14:38:49 -070068 initialized_ = true;
69 return 0;
70}
71
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010072std::unique_ptr<DrmDisplayComposition>
73DrmDisplayCompositor::CreateInitializedComposition() const {
74 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
75 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
76 if (!crtc) {
77 ALOGE("Failed to find crtc for display = %d", display_);
78 return std::unique_ptr<DrmDisplayComposition>();
79 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +020080
81 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010082}
83
Sean Paul7b1e4bc2015-10-13 15:47:22 -040084int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030085 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul7b1e4bc2015-10-13 15:47:22 -040086 if (!pset) {
87 ALOGE("Failed to allocate property set");
88 return -ENOMEM;
89 }
90
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020091 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -040092 std::vector<DrmCompositionPlane> &comp_planes = display_comp
93 ->composition_planes();
Zach Reizner92f8e632015-10-12 17:47:13 -070094 for (DrmCompositionPlane &comp_plane : comp_planes) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +030095 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030096 return -EINVAL;
Sean Paul7b1e4bc2015-10-13 15:47:22 -040097 }
98 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010099 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300100 ret = drmModeAtomicCommit(drm->fd(), pset.get(), 0, drm);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400101 if (ret) {
102 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400103 return ret;
104 }
105
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400106 return 0;
107}
108
Sean Paulc07b2112015-11-17 16:38:10 -0500109int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
Roman Stratiienko74774712021-02-05 16:32:47 +0200110 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700111 ATRACE_CALL();
112
Haixia Shidda2fab2015-10-22 18:12:49 -0700113 int ret = 0;
114
115 std::vector<DrmHwcLayer> &layers = display_comp->layers();
Sean Paulf72cccd2018-08-27 13:59:08 -0400116 std::vector<DrmCompositionPlane> &comp_planes = display_comp
117 ->composition_planes();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100118 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
119 uint64_t out_fences[drm->crtcs().size()];
Haixia Shidda2fab2015-10-22 18:12:49 -0700120
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100121 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400122 if (!connector) {
123 ALOGE("Could not locate connector for display %d", display_);
124 return -ENODEV;
125 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100126 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400127 if (!crtc) {
128 ALOGE("Could not locate crtc for display %d", display_);
129 return -ENODEV;
130 }
131
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300132 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700133 if (!pset) {
134 ALOGE("Failed to allocate property set");
135 return -ENOMEM;
136 }
137
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300138 if (crtc->out_fence_ptr_property() &&
139 !crtc->out_fence_ptr_property()
140 .AtomicSet(*pset, (uint64_t)&out_fences[crtc->pipe()])) {
141 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200142 }
143
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300144 if (mode_.blob &&
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300145 (!crtc->active_property().AtomicSet(*pset, 1) ||
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300146 !crtc->mode_property().AtomicSet(*pset, *mode_.blob) ||
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300147 !connector->crtc_id_property().AtomicSet(*pset, crtc->id()))) {
148 return -EINVAL;
Sean Paul57355412015-09-19 09:14:34 -0400149 }
150
Zach Reizner92f8e632015-10-12 17:47:13 -0700151 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400152 DrmPlane *plane = comp_plane.plane();
Sean Paulca699be2016-05-11 16:29:45 -0400153 std::vector<size_t> &source_layers = comp_plane.source_layers();
Zach Reizner92f8e632015-10-12 17:47:13 -0700154
Sean Paulbbe39db2016-05-11 16:57:26 -0400155 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400156 if (source_layers.size() > 1) {
157 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
158 source_layers.size(), comp_plane.type());
159 continue;
160 }
161
162 if (source_layers.empty() || source_layers.front() >= layers.size()) {
163 ALOGE("Source layer index %zu out of bounds %zu type=%d",
164 source_layers.front(), layers.size(), comp_plane.type());
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300165 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700166 }
Sean Paulca699be2016-05-11 16:29:45 -0400167 DrmHwcLayer &layer = layers[source_layers.front()];
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300168
169 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
170 crtc->id()) != 0) {
171 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400172 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300173 } else {
174 if (plane->AtomicDisablePlane(*pset) != 0) {
175 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300176 }
177 }
Sean Paul98e73c82015-06-24 14:38:49 -0700178 }
179
180 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500181 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
182 if (test_only)
183 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
184
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300185 ret = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400186 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700187 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500188 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul57355412015-09-19 09:14:34 -0400189 return ret;
190 }
Sean Paul98e73c82015-06-24 14:38:49 -0700191 }
Sean Paul98e73c82015-06-24 14:38:49 -0700192
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300193 if (!test_only) {
194 if (mode_.blob) {
195 connector->set_active_mode(mode_.mode);
196 mode_.old_blob = std::move(mode_.blob);
Sean Paul57355412015-09-19 09:14:34 -0400197 }
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300198 active_changed_ = false;
Sean Paul57355412015-09-19 09:14:34 -0400199
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300200 if (crtc->out_fence_ptr_property()) {
201 display_comp->out_fence_ = UniqueFd((int)out_fences[crtc->pipe()]);
202 }
Robert Fossa1ade4e2017-09-27 19:28:15 +0200203 }
204
Sean Paul98e73c82015-06-24 14:38:49 -0700205 return ret;
206}
207
Sean Paul137a6a82016-06-22 22:48:22 -0400208void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400209 if (!active_composition_)
210 return;
211
212 if (DisablePlanes(active_composition_.get()))
213 return;
214
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200215 active_composition_.reset(nullptr);
Sean Paul137a6a82016-06-22 22:48:22 -0400216}
217
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300218int DrmDisplayCompositor::ApplyComposition(
219 std::unique_ptr<DrmDisplayComposition> composition) {
220 int ret = CommitFrame(composition.get(), false);
Haixia Shidda2fab2015-10-22 18:12:49 -0700221
222 if (ret) {
223 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700224 // Disable the hw used by the last active composition. This allows us to
225 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400226 ClearDisplay();
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300227 return ret;
Haixia Shidda2fab2015-10-22 18:12:49 -0700228 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700229
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300230 if (composition) {
231 active_composition_.swap(composition);
Sean Paul98e73c82015-06-24 14:38:49 -0700232 }
Sean Paul98e73c82015-06-24 14:38:49 -0700233
Sean Paul98e73c82015-06-24 14:38:49 -0700234 return ret;
235}
236
Rob Herring4f6c62e2018-05-17 14:33:02 -0500237int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
238 return CommitFrame(composition, true);
239}
240
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300241auto DrmDisplayCompositor::SetDisplayMode(const DrmMode &display_mode) -> bool {
242 mode_.mode = display_mode;
Roman Stratiienko40b374b2021-10-22 18:13:09 +0300243 mode_.blob = mode_.mode.CreateModeBlob(*resource_manager_->GetDrmDevice(display_));
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300244 return !!mode_.blob;
245}
246
Sean Paulf72cccd2018-08-27 13:59:08 -0400247} // namespace android