blob: bca446c045707ca35fc1c9405ab7c52ad4aa4cc4 [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
Roman Stratiienko59bb4812022-04-09 16:13:09 +030017#undef NDEBUG /* Required for assert to work */
18
Sean Paul98e73c82015-06-24 14:38:49 -070019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Roman Stratiienko4e994052022-02-09 17:40:35 +020020#define LOG_TAG "hwc-drm-atomic-state-manager"
Sean Paul98e73c82015-06-24 14:38:49 -070021
Roman Stratiienko4e994052022-02-09 17:40:35 +020022#include "DrmAtomicStateManager.h"
Zach Reiznerbff33ac2015-11-16 11:08:46 -080023
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030024#include <drm/drm_mode.h>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080025#include <pthread.h>
26#include <sched.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030027#include <sync/sync.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030028#include <utils/Trace.h>
29
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020030#include <array>
Roman Stratiienko59bb4812022-04-09 16:13:09 +030031#include <cassert>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020032#include <cstdlib>
33#include <ctime>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080034#include <sstream>
35#include <vector>
36
Roman Stratiienko13cc3662020-08-29 21:35:39 +030037#include "drm/DrmCrtc.h"
38#include "drm/DrmDevice.h"
39#include "drm/DrmPlane.h"
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030040#include "drm/DrmUnique.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020041#include "utils/log.h"
Sean Paul98e73c82015-06-24 14:38:49 -070042
Sean Paul98e73c82015-06-24 14:38:49 -070043namespace android {
44
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020045// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Roman Stratiienko4e994052022-02-09 17:40:35 +020046auto DrmAtomicStateManager::CommitFrame(AtomicCommitArgs &args) -> int {
Haixia Shi3979f7d2015-10-29 14:33:37 -070047 ATRACE_CALL();
48
Roman Stratiienkof815d382021-12-30 19:23:14 +020049 if (args.active && *args.active == active_frame_state_.crtc_active_state) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030050 /* Don't set the same state twice */
51 args.active.reset();
52 }
Haixia Shidda2fab2015-10-22 18:12:49 -070053
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030054 if (!args.HasInputs()) {
55 /* nothing to do */
56 return 0;
57 }
58
Roman Stratiienkof815d382021-12-30 19:23:14 +020059 if (!active_frame_state_.crtc_active_state) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030060 /* Force activate display */
61 args.active = true;
62 }
63
Roman Stratiienko32685ba2021-12-15 13:46:05 +020064 auto new_frame_state = NewFrameState();
65
Roman Stratiienko19c162f2022-02-01 09:35:08 +020066 auto *drm = pipe_->device;
67 auto *connector = pipe_->connector->Get();
68 auto *crtc = pipe_->crtc->Get();
Sean Paul57355412015-09-19 09:14:34 -040069
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030070 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -070071 if (!pset) {
72 ALOGE("Failed to allocate property set");
73 return -ENOMEM;
74 }
75
Roman Stratiienko59bb4812022-04-09 16:13:09 +030076 int out_fence = -1;
77 if (!crtc->GetOutFencePtrProperty().AtomicSet(*pset, uint64_t(&out_fence))) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030078 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +020079 }
80
Roman Stratiienko59bb4812022-04-09 16:13:09 +030081 bool nonblock = true;
82
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030083 if (args.active) {
Roman Stratiienko59bb4812022-04-09 16:13:09 +030084 nonblock = false;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020085 new_frame_state.crtc_active_state = *args.active;
Roman Stratiienko4e994052022-02-09 17:40:35 +020086 if (!crtc->GetActiveProperty().AtomicSet(*pset, *args.active ? 1 : 0) ||
Roman Stratiienko650299a2022-01-30 23:46:10 +020087 !connector->GetCrtcIdProperty().AtomicSet(*pset, crtc->GetId())) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030088 return -EINVAL;
89 }
Sean Paul57355412015-09-19 09:14:34 -040090 }
91
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030092 if (args.display_mode) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +020093 new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(*drm);
Zach Reizner92f8e632015-10-12 17:47:13 -070094
Roman Stratiienko32685ba2021-12-15 13:46:05 +020095 if (!new_frame_state.mode_blob) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030096 ALOGE("Failed to create mode_blob");
97 return -EINVAL;
98 }
Sean Paulca699be2016-05-11 16:29:45 -040099
Roman Stratiienko10be8752022-01-30 20:28:46 +0200100 if (!crtc->GetModeProperty().AtomicSet(*pset, *new_frame_state.mode_blob)) {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300101 return -EINVAL;
102 }
103 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300104
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200105 auto unused_planes = new_frame_state.used_planes;
106
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300107 if (args.composition) {
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200108 new_frame_state.used_planes.clear();
109
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200110 for (auto &joining : args.composition->plan) {
111 DrmPlane *plane = joining.plane->Get();
112 DrmHwcLayer &layer = joining.layer;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300113
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200114 new_frame_state.used_framebuffers.emplace_back(layer.fb_id_handle);
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200115 new_frame_state.used_planes.emplace_back(joining.plane);
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200116
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200117 /* Remove from 'unused' list, since plane is re-used */
118 auto &v = unused_planes;
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200119 v.erase(std::remove(v.begin(), v.end(), joining.plane), v.end());
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200120
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200121 if (plane->AtomicSetState(*pset, layer, joining.z_pos, crtc->GetId()) !=
Roman Stratiienko10be8752022-01-30 20:28:46 +0200122 0) {
Roman Stratiienko67cebf52021-12-17 13:31:27 +0200123 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400124 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300125 }
126 }
127
Roman Stratiienkoef5348b2022-02-09 17:19:56 +0200128 if (args.composition) {
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200129 for (auto &plane : unused_planes) {
130 if (plane->Get()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300131 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300132 }
133 }
Sean Paul98e73c82015-06-24 14:38:49 -0700134 }
135
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300136 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300137
138 if (args.test_only) {
139 return drmModeAtomicCommit(drm->GetFd(), pset.get(),
140 flags | DRM_MODE_ATOMIC_TEST_ONLY, drm);
141 }
142
143 if (last_present_fence_) {
144 ATRACE_NAME("WaitPriorFramePresented");
145
146 constexpr int kTimeoutMs = 500;
147 int err = sync_wait(last_present_fence_.Get(), kTimeoutMs);
148 if (err != 0) {
149 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)",
150 last_present_fence_.Get(), err, errno);
151 }
152
153 CleanupPriorFrameResources();
154 }
155
156 if (nonblock) {
157 flags |= DRM_MODE_ATOMIC_NONBLOCK;
158 }
Sean Paulc07b2112015-11-17 16:38:10 -0500159
Roman Stratiienko7d899112022-01-31 11:30:27 +0200160 int err = drmModeAtomicCommit(drm->GetFd(), pset.get(), flags, drm);
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300161
Roman Stratiienko4e994052022-02-09 17:40:35 +0200162 if (err != 0) {
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300163 ALOGE("Failed to commit pset ret=%d\n", err);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300164 return err;
Sean Paul98e73c82015-06-24 14:38:49 -0700165 }
Sean Paul98e73c82015-06-24 14:38:49 -0700166
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300167 if (nonblock) {
168 last_present_fence_ = UniqueFd::Dup(out_fence);
169 staged_frame_state_ = std::move(new_frame_state);
170 frames_staged_++;
171 ptt_->Notify();
172 } else {
Roman Stratiienkof815d382021-12-30 19:23:14 +0200173 active_frame_state_ = std::move(new_frame_state);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200174 }
175
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300176 if (args.display_mode) {
177 /* TODO(nobody): we still need this for synthetic vsync, remove after
178 * vsync reworked */
179 connector->SetActiveMode(*args.display_mode);
180 }
181
182 args.out_fence = UniqueFd(out_fence);
183
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300184 return 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700185}
186
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300187PresentTrackerThread::PresentTrackerThread(DrmAtomicStateManager *st_man)
188 : st_man_(st_man),
189 mutex_(&st_man_->pipe_->device->GetResMan().GetMainLock()) {
190 pt_ = std::thread(&PresentTrackerThread::PresentTrackerThreadFn, this);
191}
192
193PresentTrackerThread::~PresentTrackerThread() {
194 ALOGI("PresentTrackerThread successfully destroyed");
195}
196
197void PresentTrackerThread::PresentTrackerThreadFn() {
198 /* object should be destroyed on thread exit */
199 auto self = std::unique_ptr<PresentTrackerThread>(this);
200
201 int tracking_at_the_moment = -1;
202
203 for (;;) {
204 UniqueFd present_fence;
205
206 {
207 std::unique_lock lk(*mutex_);
208 cv_.wait(lk, [&] {
209 return st_man_ == nullptr ||
210 st_man_->frames_staged_ > tracking_at_the_moment;
211 });
212
213 if (st_man_ == nullptr) {
214 break;
215 }
216
217 tracking_at_the_moment = st_man_->frames_staged_;
218
219 present_fence = UniqueFd::Dup(st_man_->last_present_fence_.Get());
220 if (!present_fence) {
221 continue;
222 }
223 }
224
225 {
226 ATRACE_NAME("AsyncWaitForBuffersSwap");
227 constexpr int kTimeoutMs = 500;
228 int err = sync_wait(present_fence.Get(), kTimeoutMs);
229 if (err != 0) {
230 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)", present_fence.Get(),
231 err, errno);
232 }
233 }
234
235 {
236 std::unique_lock lk(*mutex_);
237 if (st_man_ == nullptr) {
238 break;
239 }
240
241 /* If resources is already cleaned-up by main thread, skip */
242 if (tracking_at_the_moment > st_man_->frames_tracked_) {
243 st_man_->CleanupPriorFrameResources();
244 }
245 }
246 }
247}
248
249void DrmAtomicStateManager::CleanupPriorFrameResources() {
250 assert(frames_staged_ - frames_tracked_ == 1);
251 assert(last_present_fence_);
252
253 ATRACE_NAME("CleanupPriorFrameResources");
254 frames_tracked_++;
255 active_frame_state_ = std::move(staged_frame_state_);
256 last_present_fence_ = {};
257}
258
Roman Stratiienko4e994052022-02-09 17:40:35 +0200259auto DrmAtomicStateManager::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300260 int err = CommitFrame(args);
Sean Paul137a6a82016-06-22 22:48:22 -0400261
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300262 if (!args.test_only) {
Roman Stratiienko4e994052022-02-09 17:40:35 +0200263 if (err != 0) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200264 ALOGE("Composite failed for pipeline %s",
265 pipe_->connector->Get()->GetName().c_str());
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300266 // Disable the hw used by the last active composition. This allows us to
267 // signal the release fences from that composition to avoid hanging.
Roman Stratiienkoef5348b2022-02-09 17:19:56 +0200268 AtomicCommitArgs cl_args{};
269 cl_args.composition = std::make_shared<DrmKmsPlan>();
Roman Stratiienko4e994052022-02-09 17:40:35 +0200270 if (CommitFrame(cl_args) != 0) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200271 ALOGE("Failed to clean-up active composition for pipeline %s",
272 pipe_->connector->Get()->GetName().c_str());
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300273 }
274 return err;
275 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700276 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700277
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300278 return err;
279} // namespace android
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300280
Roman Stratiienko4e994052022-02-09 17:40:35 +0200281auto DrmAtomicStateManager::ActivateDisplayUsingDPMS() -> int {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200282 return drmModeConnectorSetProperty(pipe_->device->GetFd(),
283 pipe_->connector->Get()->GetId(),
284 pipe_->connector->Get()
285 ->GetDpmsProperty()
286 .id(),
287 DRM_MODE_DPMS_ON);
Roman Stratiienkod37b3082022-01-13 16:37:27 +0200288}
289
Sean Paulf72cccd2018-08-27 13:59:08 -0400290} // namespace android