blob: 45396644df6fcfb0ad2e062355cafbab80168e5d [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;
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010054 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Sean Paul35301f42015-11-17 14:46:56 -050055 if (mode_.blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010056 drm->DestroyPropertyBlob(mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050057 if (mode_.old_blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010058 drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050059
Sean Paul98e73c82015-06-24 14:38:49 -070060 active_composition_.reset();
Sean Paul98e73c82015-06-24 14:38:49 -070061}
62
Roman Stratiienkoe8c06792021-09-30 10:09:31 +030063auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
Roman Stratiienko863a3c22021-09-29 13:00:29 +030064 -> int {
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010065 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -070066 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010067 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
68 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010069 ALOGE("Could not find drmdevice for display");
70 return -EINVAL;
71 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010072 planner_ = Planner::CreateInstance(drm);
73
Sean Paul98e73c82015-06-24 14:38:49 -070074 initialized_ = true;
75 return 0;
76}
77
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010078std::unique_ptr<DrmDisplayComposition>
79DrmDisplayCompositor::CreateInitializedComposition() const {
80 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
81 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
82 if (!crtc) {
83 ALOGE("Failed to find crtc for display = %d", display_);
84 return std::unique_ptr<DrmDisplayComposition>();
85 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +020086
87 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010088}
89
Zach Reizner92f8e632015-10-12 17:47:13 -070090std::tuple<uint32_t, uint32_t, int>
91DrmDisplayCompositor::GetActiveModeResolution() {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010092 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
93 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020094 if (connector == nullptr) {
Zach Reizner92f8e632015-10-12 17:47:13 -070095 ALOGE("Failed to determine display mode: no connector for display %d",
96 display_);
97 return std::make_tuple(0, 0, -ENODEV);
98 }
99
100 const DrmMode &mode = connector->active_mode();
101 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -0700102}
103
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400104int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300105 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400106 if (!pset) {
107 ALOGE("Failed to allocate property set");
108 return -ENOMEM;
109 }
110
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200111 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400112 std::vector<DrmCompositionPlane> &comp_planes = display_comp
113 ->composition_planes();
Zach Reizner92f8e632015-10-12 17:47:13 -0700114 for (DrmCompositionPlane &comp_plane : comp_planes) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300115 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300116 return -EINVAL;
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400117 }
118 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100119 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300120 ret = drmModeAtomicCommit(drm->fd(), pset.get(), 0, drm);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400121 if (ret) {
122 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400123 return ret;
124 }
125
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400126 return 0;
127}
128
Sean Paulc07b2112015-11-17 16:38:10 -0500129int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
Roman Stratiienko74774712021-02-05 16:32:47 +0200130 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700131 ATRACE_CALL();
132
Haixia Shidda2fab2015-10-22 18:12:49 -0700133 int ret = 0;
134
135 std::vector<DrmHwcLayer> &layers = display_comp->layers();
Sean Paulf72cccd2018-08-27 13:59:08 -0400136 std::vector<DrmCompositionPlane> &comp_planes = display_comp
137 ->composition_planes();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100138 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
139 uint64_t out_fences[drm->crtcs().size()];
Haixia Shidda2fab2015-10-22 18:12:49 -0700140
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100141 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400142 if (!connector) {
143 ALOGE("Could not locate connector for display %d", display_);
144 return -ENODEV;
145 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100146 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400147 if (!crtc) {
148 ALOGE("Could not locate crtc for display %d", display_);
149 return -ENODEV;
150 }
151
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300152 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700153 if (!pset) {
154 ALOGE("Failed to allocate property set");
155 return -ENOMEM;
156 }
157
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300158 if (crtc->out_fence_ptr_property() &&
159 !crtc->out_fence_ptr_property()
160 .AtomicSet(*pset, (uint64_t)&out_fences[crtc->pipe()])) {
161 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200162 }
163
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300164 if (mode_.needs_modeset &&
165 (!crtc->active_property().AtomicSet(*pset, 1) ||
166 !crtc->mode_property().AtomicSet(*pset, mode_.blob_id) ||
167 !connector->crtc_id_property().AtomicSet(*pset, crtc->id()))) {
168 return -EINVAL;
Sean Paul57355412015-09-19 09:14:34 -0400169 }
170
Zach Reizner92f8e632015-10-12 17:47:13 -0700171 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400172 DrmPlane *plane = comp_plane.plane();
Sean Paulca699be2016-05-11 16:29:45 -0400173 std::vector<size_t> &source_layers = comp_plane.source_layers();
Zach Reizner92f8e632015-10-12 17:47:13 -0700174
Sean Paulbbe39db2016-05-11 16:57:26 -0400175 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400176 if (source_layers.size() > 1) {
177 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
178 source_layers.size(), comp_plane.type());
179 continue;
180 }
181
182 if (source_layers.empty() || source_layers.front() >= layers.size()) {
183 ALOGE("Source layer index %zu out of bounds %zu type=%d",
184 source_layers.front(), layers.size(), comp_plane.type());
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300185 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700186 }
Sean Paulca699be2016-05-11 16:29:45 -0400187 DrmHwcLayer &layer = layers[source_layers.front()];
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300188
189 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
190 crtc->id()) != 0) {
191 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400192 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300193 } else {
194 if (plane->AtomicDisablePlane(*pset) != 0) {
195 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300196 }
197 }
Sean Paul98e73c82015-06-24 14:38:49 -0700198 }
199
200 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500201 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
202 if (test_only)
203 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
204
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300205 ret = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400206 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700207 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500208 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul57355412015-09-19 09:14:34 -0400209 return ret;
210 }
Sean Paul98e73c82015-06-24 14:38:49 -0700211 }
Sean Paul98e73c82015-06-24 14:38:49 -0700212
Sean Paulc07b2112015-11-17 16:38:10 -0500213 if (!test_only && mode_.needs_modeset) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100214 ret = drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400215 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400216 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
Sean Paul35301f42015-11-17 14:46:56 -0500217 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400218 return ret;
219 }
220
221 /* TODO: Add dpms to the pset when the kernel supports it */
222 ret = ApplyDpms(display_comp);
223 if (ret) {
224 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
225 return ret;
226 }
227
Sean Paul35301f42015-11-17 14:46:56 -0500228 connector->set_active_mode(mode_.mode);
229 mode_.old_blob_id = mode_.blob_id;
230 mode_.blob_id = 0;
231 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400232 }
233
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300234 if (crtc->out_fence_ptr_property()) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200235 display_comp->out_fence_ = UniqueFd((int)out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200236 }
237
Sean Paul98e73c82015-06-24 14:38:49 -0700238 return ret;
239}
240
Sean Pauldb7a17d2015-06-24 18:46:05 -0700241int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100242 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
243 DrmConnector *conn = drm->GetConnectorForDisplay(display_);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700244 if (!conn) {
245 ALOGE("Failed to get DrmConnector for display %d", display_);
246 return -ENODEV;
247 }
248
249 const DrmProperty &prop = conn->dpms_property();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100250 int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700251 display_comp->dpms_mode());
252 if (ret) {
253 ALOGE("Failed to set DPMS property for connector %d", conn->id());
254 return ret;
255 }
256 return 0;
257}
258
Sean Paul35301f42015-11-17 14:46:56 -0500259std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
260 const DrmMode &mode) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200261 struct drm_mode_modeinfo drm_mode {};
Sean Paul35301f42015-11-17 14:46:56 -0500262 mode.ToDrmModeModeInfo(&drm_mode);
263
264 uint32_t id = 0;
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100265 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
266 int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
267 &id);
Sean Paul35301f42015-11-17 14:46:56 -0500268 if (ret) {
269 ALOGE("Failed to create mode property blob %d", ret);
270 return std::make_tuple(ret, 0);
271 }
Sean Paulf741c672016-05-11 13:49:38 -0400272 ALOGE("Create blob_id %" PRIu32 "\n", id);
Sean Paul35301f42015-11-17 14:46:56 -0500273 return std::make_tuple(ret, id);
274}
275
Sean Paul137a6a82016-06-22 22:48:22 -0400276void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400277 if (!active_composition_)
278 return;
279
280 if (DisablePlanes(active_composition_.get()))
281 return;
282
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200283 active_composition_.reset(nullptr);
Sean Paul137a6a82016-06-22 22:48:22 -0400284}
285
Haixia Shidda2fab2015-10-22 18:12:49 -0700286void DrmDisplayCompositor::ApplyFrame(
Roman Stratiienko74774712021-02-05 16:32:47 +0200287 std::unique_ptr<DrmDisplayComposition> composition, int status) {
Haixia Shidda2fab2015-10-22 18:12:49 -0700288 int ret = status;
289
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100290 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500291 ret = CommitFrame(composition.get(), false);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100292 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700293
294 if (ret) {
295 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700296 // Disable the hw used by the last active composition. This allows us to
297 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400298 ClearDisplay();
299 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700300 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700301
Haixia Shidda2fab2015-10-22 18:12:49 -0700302 active_composition_.swap(composition);
Haixia Shidda2fab2015-10-22 18:12:49 -0700303}
304
Sean Pauled45a8e2017-02-28 13:17:34 -0500305int DrmDisplayCompositor::ApplyComposition(
306 std::unique_ptr<DrmDisplayComposition> composition) {
307 int ret = 0;
Sean Paulacb2a442015-06-24 18:43:01 -0700308 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700309 case DRM_COMPOSITION_TYPE_FRAME:
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800310 if (composition->geometry_changed()) {
311 // Send the composition to the kernel to ensure we can commit it. This
Rob Herringaf0d9752018-05-04 16:34:19 -0500312 // is just a test, it won't actually commit the frame.
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800313 ret = CommitFrame(composition.get(), true);
Rob Herringaf0d9752018-05-04 16:34:19 -0500314 if (ret) {
315 ALOGE("Commit test failed for display %d, FIXME", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500316 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500317 }
318 }
Rob Herringaf0d9752018-05-04 16:34:19 -0500319
Sean Pauled45a8e2017-02-28 13:17:34 -0500320 ApplyFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700321 break;
322 case DRM_COMPOSITION_TYPE_DPMS:
Sean Pauled45a8e2017-02-28 13:17:34 -0500323 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700324 ret = ApplyDpms(composition.get());
325 if (ret)
326 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700327 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400328 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500329 mode_.mode = composition->display_mode();
330 if (mode_.blob_id)
Sean Paulf72cccd2018-08-27 13:59:08 -0400331 resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob(
332 mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -0500333 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
334 if (ret) {
335 ALOGE("Failed to create mode blob for display %d", display_);
336 return ret;
337 }
338 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400339 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700340 default:
341 ALOGE("Unknown composition type %d", composition->type());
342 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700343 }
Sean Paul98e73c82015-06-24 14:38:49 -0700344
Sean Paul98e73c82015-06-24 14:38:49 -0700345 return ret;
346}
347
Rob Herring4f6c62e2018-05-17 14:33:02 -0500348int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
349 return CommitFrame(composition, true);
350}
351
Sean Paulf72cccd2018-08-27 13:59:08 -0400352} // namespace android