blob: 89e7f2d0f433e1fa3bb7baf0e8b62071f56cf4ab [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
Sean Paul98e73c82015-06-24 14:38:49 -070053 initialized_ = true;
54 return 0;
55}
56
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010057std::unique_ptr<DrmDisplayComposition>
58DrmDisplayCompositor::CreateInitializedComposition() const {
59 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
60 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
61 if (!crtc) {
62 ALOGE("Failed to find crtc for display = %d", display_);
63 return std::unique_ptr<DrmDisplayComposition>();
64 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +020065
Roman Stratiienko753d1072021-12-30 18:05:27 +020066 return std::make_unique<DrmDisplayComposition>(crtc);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010067}
68
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020069// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
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 Stratiienkof815d382021-12-30 19:23:14 +020073 if (args.active && *args.active == active_frame_state_.crtc_active_state) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030074 /* 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
Roman Stratiienkof815d382021-12-30 19:23:14 +020083 if (!active_frame_state_.crtc_active_state) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030084 /* 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
Roman Stratiienko32685ba2021-12-15 13:46:05 +020093 auto new_frame_state = NewFrameState();
94
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010095 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Haixia Shidda2fab2015-10-22 18:12:49 -070096
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010097 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -040098 if (!connector) {
99 ALOGE("Could not locate connector for display %d", display_);
100 return -ENODEV;
101 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100102 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400103 if (!crtc) {
104 ALOGE("Could not locate crtc for display %d", display_);
105 return -ENODEV;
106 }
107
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300108 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700109 if (!pset) {
110 ALOGE("Failed to allocate property set");
111 return -ENOMEM;
112 }
113
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300114 int64_t out_fence = -1;
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300115 if (crtc->out_fence_ptr_property() &&
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300116 !crtc->out_fence_ptr_property().AtomicSet(*pset, (uint64_t)&out_fence)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300117 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200118 }
119
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300120 if (args.active) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200121 new_frame_state.crtc_active_state = *args.active;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300122 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) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200129 new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300130 *resource_manager_->GetDrmDevice(display_));
Zach Reizner92f8e632015-10-12 17:47:13 -0700131
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200132 if (!new_frame_state.mode_blob) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300133 ALOGE("Failed to create mode_blob");
134 return -EINVAL;
135 }
Sean Paulca699be2016-05-11 16:29:45 -0400136
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200137 if (!crtc->mode_property().AtomicSet(*pset, *new_frame_state.mode_blob)) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300138 return -EINVAL;
139 }
140 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300141
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200142 auto unused_planes = new_frame_state.used_planes;
143
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300144 if (args.composition) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200145 new_frame_state.used_framebuffers.clear();
146 new_frame_state.used_planes.clear();
147
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300148 std::vector<DrmHwcLayer> &layers = args.composition->layers();
149 std::vector<DrmCompositionPlane> &comp_planes = args.composition
150 ->composition_planes();
151
152 for (DrmCompositionPlane &comp_plane : comp_planes) {
153 DrmPlane *plane = comp_plane.plane();
Roman Stratiienko5d1f5572021-12-17 15:09:28 +0200154 size_t source_layer = comp_plane.source_layer();
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300155
Roman Stratiienko5d1f5572021-12-17 15:09:28 +0200156 if (source_layer >= layers.size()) {
157 ALOGE("Source layer index %zu out of bounds %zu", source_layer,
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200158 layers.size());
159 return -EINVAL;
160 }
Roman Stratiienko5d1f5572021-12-17 15:09:28 +0200161 DrmHwcLayer &layer = layers[source_layer];
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300162
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200163 new_frame_state.used_framebuffers.emplace_back(layer.fb_id_handle);
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200164 new_frame_state.used_planes.emplace_back(plane);
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200165
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200166 /* Remove from 'unused' list, since plane is re-used */
167 auto &v = unused_planes;
168 v.erase(std::remove(v.begin(), v.end(), plane), v.end());
169
Roman Stratiienko5d1f5572021-12-17 15:09:28 +0200170 if (plane->AtomicSetState(*pset, layer, source_layer, crtc->id()) != 0) {
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200171 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400172 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300173 }
174 }
175
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200176 if (args.clear_active_composition) {
177 new_frame_state.used_framebuffers.clear();
178 new_frame_state.used_planes.clear();
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200179 }
180
181 if (args.clear_active_composition || args.composition) {
182 for (auto *plane : unused_planes) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200183 if (plane->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300184 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300185 }
186 }
Sean Paul98e73c82015-06-24 14:38:49 -0700187 }
188
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300189 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
190 if (args.test_only)
191 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
Sean Paulc07b2112015-11-17 16:38:10 -0500192
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300193 int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
194 if (err) {
195 if (!args.test_only)
196 ALOGE("Failed to commit pset ret=%d\n", err);
197 return err;
Sean Paul98e73c82015-06-24 14:38:49 -0700198 }
Sean Paul98e73c82015-06-24 14:38:49 -0700199
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300200 if (!args.test_only) {
201 if (args.display_mode) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200202 /* TODO(nobody): we still need this for synthetic vsync, remove after
203 * vsync reworked */
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300204 connector->set_active_mode(*args.display_mode);
Sean Paul57355412015-09-19 09:14:34 -0400205 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300206
Roman Stratiienkof815d382021-12-30 19:23:14 +0200207 active_frame_state_ = std::move(new_frame_state);
Sean Paul57355412015-09-19 09:14:34 -0400208
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300209 if (crtc->out_fence_ptr_property()) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300210 args.out_fence = UniqueFd((int)out_fence);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300211 }
Robert Fossa1ade4e2017-09-27 19:28:15 +0200212 }
213
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300214 return 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700215}
216
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300217auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
218 int err = CommitFrame(args);
Sean Paul137a6a82016-06-22 22:48:22 -0400219
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300220 if (!args.test_only) {
221 if (err) {
222 ALOGE("Composite failed for display %d", display_);
223 // Disable the hw used by the last active composition. This allows us to
224 // signal the release fences from that composition to avoid hanging.
225 AtomicCommitArgs cl_args = {.clear_active_composition = true};
226 if (CommitFrame(cl_args)) {
227 ALOGE("Failed to clean-up active composition for display %d", display_);
228 }
229 return err;
230 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700231 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700232
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300233 return err;
234} // namespace android
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300235
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200236auto DrmDisplayCompositor::ActivateDisplayUsingDPMS() -> int {
237 auto *drm = resource_manager_->GetDrmDevice(display_);
238 auto *connector = drm->GetConnectorForDisplay(display_);
239 if (connector == nullptr) {
240 ALOGE("Could not locate connector for display %d", display_);
241 return -ENODEV;
242 }
243
244 if (connector->dpms_property()) {
245 drmModeConnectorSetProperty(drm->fd(), connector->id(),
246 connector->dpms_property().id(),
247 DRM_MODE_DPMS_ON);
248 }
249 return 0;
250}
251
Sean Paulf72cccd2018-08-27 13:59:08 -0400252} // namespace android