blob: 576c5332c87ccf29867b2901593f63b9782fb37a [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),
Sean Pauldb7a17d2015-06-24 18:46:05 -070047 active_(false),
Roman Stratiienkod3a2cf42021-09-30 10:09:46 +030048 use_hw_overlays_(true) {
Sean Paul98e73c82015-06-24 14:38:49 -070049}
50
51DrmDisplayCompositor::~DrmDisplayCompositor() {
52 if (!initialized_)
53 return;
Sean Paul35301f42015-11-17 14:46:56 -050054
Sean Paul98e73c82015-06-24 14:38:49 -070055 active_composition_.reset();
Sean Paul98e73c82015-06-24 14:38:49 -070056}
57
Roman Stratiienkoe8c06792021-09-30 10:09:31 +030058auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
Roman Stratiienko863a3c22021-09-29 13:00:29 +030059 -> int {
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010060 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -070061 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010062 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
63 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010064 ALOGE("Could not find drmdevice for display");
65 return -EINVAL;
66 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010067 planner_ = Planner::CreateInstance(drm);
68
Sean Paul98e73c82015-06-24 14:38:49 -070069 initialized_ = true;
70 return 0;
71}
72
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010073std::unique_ptr<DrmDisplayComposition>
74DrmDisplayCompositor::CreateInitializedComposition() const {
75 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
76 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
77 if (!crtc) {
78 ALOGE("Failed to find crtc for display = %d", display_);
79 return std::unique_ptr<DrmDisplayComposition>();
80 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +020081
82 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010083}
84
Zach Reizner92f8e632015-10-12 17:47:13 -070085std::tuple<uint32_t, uint32_t, int>
86DrmDisplayCompositor::GetActiveModeResolution() {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010087 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
88 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020089 if (connector == nullptr) {
Zach Reizner92f8e632015-10-12 17:47:13 -070090 ALOGE("Failed to determine display mode: no connector for display %d",
91 display_);
92 return std::make_tuple(0, 0, -ENODEV);
93 }
94
95 const DrmMode &mode = connector->active_mode();
96 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -070097}
98
Sean Paul7b1e4bc2015-10-13 15:47:22 -040099int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300100 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400101 if (!pset) {
102 ALOGE("Failed to allocate property set");
103 return -ENOMEM;
104 }
105
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200106 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400107 std::vector<DrmCompositionPlane> &comp_planes = display_comp
108 ->composition_planes();
Zach Reizner92f8e632015-10-12 17:47:13 -0700109 for (DrmCompositionPlane &comp_plane : comp_planes) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300110 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300111 return -EINVAL;
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400112 }
113 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100114 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300115 ret = drmModeAtomicCommit(drm->fd(), pset.get(), 0, drm);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400116 if (ret) {
117 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400118 return ret;
119 }
120
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400121 return 0;
122}
123
Sean Paulc07b2112015-11-17 16:38:10 -0500124int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
Roman Stratiienko74774712021-02-05 16:32:47 +0200125 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700126 ATRACE_CALL();
127
Haixia Shidda2fab2015-10-22 18:12:49 -0700128 int ret = 0;
129
130 std::vector<DrmHwcLayer> &layers = display_comp->layers();
Sean Paulf72cccd2018-08-27 13:59:08 -0400131 std::vector<DrmCompositionPlane> &comp_planes = display_comp
132 ->composition_planes();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100133 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
134 uint64_t out_fences[drm->crtcs().size()];
Haixia Shidda2fab2015-10-22 18:12:49 -0700135
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100136 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400137 if (!connector) {
138 ALOGE("Could not locate connector for display %d", display_);
139 return -ENODEV;
140 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100141 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400142 if (!crtc) {
143 ALOGE("Could not locate crtc for display %d", display_);
144 return -ENODEV;
145 }
146
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300147 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700148 if (!pset) {
149 ALOGE("Failed to allocate property set");
150 return -ENOMEM;
151 }
152
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300153 if (crtc->out_fence_ptr_property() &&
154 !crtc->out_fence_ptr_property()
155 .AtomicSet(*pset, (uint64_t)&out_fences[crtc->pipe()])) {
156 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200157 }
158
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300159 if (mode_.blob &&
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300160 (!crtc->active_property().AtomicSet(*pset, 1) ||
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300161 !crtc->mode_property().AtomicSet(*pset, *mode_.blob) ||
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300162 !connector->crtc_id_property().AtomicSet(*pset, crtc->id()))) {
163 return -EINVAL;
Sean Paul57355412015-09-19 09:14:34 -0400164 }
165
Zach Reizner92f8e632015-10-12 17:47:13 -0700166 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400167 DrmPlane *plane = comp_plane.plane();
Sean Paulca699be2016-05-11 16:29:45 -0400168 std::vector<size_t> &source_layers = comp_plane.source_layers();
Zach Reizner92f8e632015-10-12 17:47:13 -0700169
Sean Paulbbe39db2016-05-11 16:57:26 -0400170 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400171 if (source_layers.size() > 1) {
172 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
173 source_layers.size(), comp_plane.type());
174 continue;
175 }
176
177 if (source_layers.empty() || source_layers.front() >= layers.size()) {
178 ALOGE("Source layer index %zu out of bounds %zu type=%d",
179 source_layers.front(), layers.size(), comp_plane.type());
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300180 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700181 }
Sean Paulca699be2016-05-11 16:29:45 -0400182 DrmHwcLayer &layer = layers[source_layers.front()];
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300183
184 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
185 crtc->id()) != 0) {
186 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400187 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300188 } else {
189 if (plane->AtomicDisablePlane(*pset) != 0) {
190 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300191 }
192 }
Sean Paul98e73c82015-06-24 14:38:49 -0700193 }
194
195 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500196 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
197 if (test_only)
198 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
199
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300200 ret = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400201 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700202 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500203 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul57355412015-09-19 09:14:34 -0400204 return ret;
205 }
Sean Paul98e73c82015-06-24 14:38:49 -0700206 }
Sean Paul98e73c82015-06-24 14:38:49 -0700207
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300208 if (!test_only && mode_.blob) {
Sean Paul57355412015-09-19 09:14:34 -0400209 /* TODO: Add dpms to the pset when the kernel supports it */
210 ret = ApplyDpms(display_comp);
211 if (ret) {
212 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
213 return ret;
214 }
215
Sean Paul35301f42015-11-17 14:46:56 -0500216 connector->set_active_mode(mode_.mode);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300217 mode_.old_blob = std::move(mode_.blob);
Sean Paul57355412015-09-19 09:14:34 -0400218 }
219
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300220 if (crtc->out_fence_ptr_property()) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200221 display_comp->out_fence_ = UniqueFd((int)out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200222 }
223
Sean Paul98e73c82015-06-24 14:38:49 -0700224 return ret;
225}
226
Sean Pauldb7a17d2015-06-24 18:46:05 -0700227int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100228 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
229 DrmConnector *conn = drm->GetConnectorForDisplay(display_);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700230 if (!conn) {
231 ALOGE("Failed to get DrmConnector for display %d", display_);
232 return -ENODEV;
233 }
234
235 const DrmProperty &prop = conn->dpms_property();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100236 int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700237 display_comp->dpms_mode());
238 if (ret) {
239 ALOGE("Failed to set DPMS property for connector %d", conn->id());
240 return ret;
241 }
242 return 0;
243}
244
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300245auto DrmDisplayCompositor::CreateModeBlob(const DrmMode &mode)
246 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200247 struct drm_mode_modeinfo drm_mode {};
Sean Paul35301f42015-11-17 14:46:56 -0500248 mode.ToDrmModeModeInfo(&drm_mode);
249
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100250 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300251 return drm->RegisterUserPropertyBlob(&drm_mode,
252 sizeof(struct drm_mode_modeinfo));
Sean Paul35301f42015-11-17 14:46:56 -0500253}
254
Sean Paul137a6a82016-06-22 22:48:22 -0400255void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400256 if (!active_composition_)
257 return;
258
259 if (DisablePlanes(active_composition_.get()))
260 return;
261
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200262 active_composition_.reset(nullptr);
Sean Paul137a6a82016-06-22 22:48:22 -0400263}
264
Haixia Shidda2fab2015-10-22 18:12:49 -0700265void DrmDisplayCompositor::ApplyFrame(
Roman Stratiienko74774712021-02-05 16:32:47 +0200266 std::unique_ptr<DrmDisplayComposition> composition, int status) {
Haixia Shidda2fab2015-10-22 18:12:49 -0700267 int ret = status;
268
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100269 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500270 ret = CommitFrame(composition.get(), false);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100271 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700272
273 if (ret) {
274 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700275 // Disable the hw used by the last active composition. This allows us to
276 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400277 ClearDisplay();
278 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700279 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700280
Haixia Shidda2fab2015-10-22 18:12:49 -0700281 active_composition_.swap(composition);
Haixia Shidda2fab2015-10-22 18:12:49 -0700282}
283
Sean Pauled45a8e2017-02-28 13:17:34 -0500284int DrmDisplayCompositor::ApplyComposition(
285 std::unique_ptr<DrmDisplayComposition> composition) {
286 int ret = 0;
Sean Paulacb2a442015-06-24 18:43:01 -0700287 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700288 case DRM_COMPOSITION_TYPE_FRAME:
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800289 if (composition->geometry_changed()) {
290 // Send the composition to the kernel to ensure we can commit it. This
Rob Herringaf0d9752018-05-04 16:34:19 -0500291 // is just a test, it won't actually commit the frame.
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800292 ret = CommitFrame(composition.get(), true);
Rob Herringaf0d9752018-05-04 16:34:19 -0500293 if (ret) {
294 ALOGE("Commit test failed for display %d, FIXME", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500295 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500296 }
297 }
Rob Herringaf0d9752018-05-04 16:34:19 -0500298
Sean Pauled45a8e2017-02-28 13:17:34 -0500299 ApplyFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700300 break;
301 case DRM_COMPOSITION_TYPE_DPMS:
Sean Pauled45a8e2017-02-28 13:17:34 -0500302 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700303 ret = ApplyDpms(composition.get());
304 if (ret)
305 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700306 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400307 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500308 mode_.mode = composition->display_mode();
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300309 mode_.blob = CreateModeBlob(mode_.mode);
310 if (!mode_.blob) {
Sean Paul35301f42015-11-17 14:46:56 -0500311 ALOGE("Failed to create mode blob for display %d", display_);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300312 return -EINVAL;
Sean Paul35301f42015-11-17 14:46:56 -0500313 }
Sean Paul57355412015-09-19 09:14:34 -0400314 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700315 default:
316 ALOGE("Unknown composition type %d", composition->type());
317 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700318 }
Sean Paul98e73c82015-06-24 14:38:49 -0700319
Sean Paul98e73c82015-06-24 14:38:49 -0700320 return ret;
321}
322
Rob Herring4f6c62e2018-05-17 14:33:02 -0500323int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
324 return CommitFrame(composition, true);
325}
326
Sean Paulf72cccd2018-08-27 13:59:08 -0400327} // namespace android