1 /*
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 #undef NDEBUG /* Required for assert to work */
18
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 #define LOG_TAG "hwc-drm-atomic-state-manager"
21
22 #include "DrmAtomicStateManager.h"
23
24 #include <drm/drm_mode.h>
25 #include <sync/sync.h>
26 #include <utils/Trace.h>
27
28 #include <cassert>
29
30 #include "drm/DrmCrtc.h"
31 #include "drm/DrmDevice.h"
32 #include "drm/DrmPlane.h"
33 #include "drm/DrmUnique.h"
34 #include "utils/log.h"
35
36 namespace android {
37
CreateInstance(DrmDisplayPipeline * pipe)38 auto DrmAtomicStateManager::CreateInstance(DrmDisplayPipeline *pipe)
39 -> std::shared_ptr<DrmAtomicStateManager> {
40 auto dasm = std::shared_ptr<DrmAtomicStateManager>(
41 new DrmAtomicStateManager());
42
43 dasm->pipe_ = pipe;
44 std::thread(&DrmAtomicStateManager::ThreadFn, dasm.get(), dasm).detach();
45
46 return dasm;
47 }
48
49 // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
CommitFrame(AtomicCommitArgs & args)50 auto DrmAtomicStateManager::CommitFrame(AtomicCommitArgs &args) -> int {
51 // NOLINTNEXTLINE(misc-const-correctness)
52 ATRACE_CALL();
53
54 if (args.active && *args.active == active_frame_state_.crtc_active_state) {
55 /* Don't set the same state twice */
56 args.active.reset();
57 }
58
59 if (!args.HasInputs()) {
60 /* nothing to do */
61 return 0;
62 }
63
64 if (!active_frame_state_.crtc_active_state) {
65 /* Force activate display */
66 args.active = true;
67 }
68
69 auto new_frame_state = NewFrameState();
70
71 auto *drm = pipe_->device;
72 auto *connector = pipe_->connector->Get();
73 auto *crtc = pipe_->crtc->Get();
74
75 auto pset = MakeDrmModeAtomicReqUnique();
76 if (!pset) {
77 ALOGE("Failed to allocate property set");
78 return -ENOMEM;
79 }
80
81 int out_fence = -1;
82 if (!args.writeback_fb) {
83 if (!crtc->GetOutFencePtrProperty(). //
84 AtomicSet(*pset, uint64_t(&out_fence))) {
85 return -EINVAL;
86 }
87 } else {
88 if (!connector->GetWritebackOutFenceProperty(). //
89 AtomicSet(*pset, uint64_t(&out_fence))) {
90 return -EINVAL;
91 }
92
93 if (!connector->GetWritebackFbIdProperty(). //
94 AtomicSet(*pset, args.writeback_fb->GetFbId())) {
95 return -EINVAL;
96 }
97
98 if (args.writeback_release_fence) {
99 sync_wait(*args.writeback_release_fence, -1);
100 args.writeback_release_fence.reset();
101 }
102 }
103
104 bool nonblock = true;
105
106 if (args.active) {
107 nonblock = false;
108 new_frame_state.crtc_active_state = *args.active;
109 if (!crtc->GetActiveProperty().AtomicSet(*pset, *args.active ? 1 : 0) ||
110 !connector->GetCrtcIdProperty().AtomicSet(*pset, crtc->GetId())) {
111 return -EINVAL;
112 }
113 }
114
115 if (args.display_mode) {
116 new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(*drm);
117
118 if (!new_frame_state.mode_blob) {
119 ALOGE("Failed to create mode_blob");
120 return -EINVAL;
121 }
122
123 if (!crtc->GetModeProperty().AtomicSet(*pset, *new_frame_state.mode_blob)) {
124 return -EINVAL;
125 }
126 }
127
128 if (args.color_matrix && crtc->GetCtmProperty()) {
129 auto blob = drm->RegisterUserPropertyBlob(args.color_matrix.get(),
130 sizeof(drm_color_ctm));
131 new_frame_state.ctm_blob = std::move(blob);
132
133 if (!new_frame_state.ctm_blob) {
134 ALOGE("Failed to create CTM blob");
135 return -EINVAL;
136 }
137
138 if (!crtc->GetCtmProperty().AtomicSet(*pset, *new_frame_state.ctm_blob))
139 return -EINVAL;
140 }
141
142 auto unused_planes = new_frame_state.used_planes;
143
144 if (args.composition) {
145 new_frame_state.used_planes.clear();
146
147 for (auto &joining : args.composition->plan) {
148 DrmPlane *plane = joining.plane->Get();
149 LayerData &layer = joining.layer;
150
151 new_frame_state.used_framebuffers.emplace_back(layer.fb);
152 new_frame_state.used_planes.emplace_back(joining.plane);
153
154 /* Remove from 'unused' list, since plane is re-used */
155 auto &v = unused_planes;
156 v.erase(std::remove(v.begin(), v.end(), joining.plane), v.end());
157
158 if (plane->AtomicSetState(*pset, layer, joining.z_pos, crtc->GetId()) !=
159 0) {
160 return -EINVAL;
161 }
162 }
163 }
164
165 if (args.composition) {
166 for (auto &plane : unused_planes) {
167 if (plane->Get()->AtomicDisablePlane(*pset) != 0) {
168 return -EINVAL;
169 }
170 }
171 }
172
173 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
174
175 if (args.test_only) {
176 return drmModeAtomicCommit(*drm->GetFd(), pset.get(),
177 flags | DRM_MODE_ATOMIC_TEST_ONLY, drm);
178 }
179
180 if (last_present_fence_) {
181 // NOLINTNEXTLINE(misc-const-correctness)
182 ATRACE_NAME("WaitPriorFramePresented");
183
184 constexpr int kTimeoutMs = 500;
185 const int err = sync_wait(*last_present_fence_, kTimeoutMs);
186 if (err != 0) {
187 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)", *last_present_fence_,
188 err, errno);
189 }
190
191 CleanupPriorFrameResources();
192 }
193
194 if (nonblock) {
195 flags |= DRM_MODE_ATOMIC_NONBLOCK;
196 }
197
198 auto err = drmModeAtomicCommit(*drm->GetFd(), pset.get(), flags, drm);
199
200 if (err != 0) {
201 ALOGE("Failed to commit pset ret=%d\n", err);
202 return err;
203 }
204
205 args.out_fence = MakeSharedFd(out_fence);
206
207 if (nonblock) {
208 {
209 const std::unique_lock lock(mutex_);
210 last_present_fence_ = args.out_fence;
211 staged_frame_state_ = std::move(new_frame_state);
212 frames_staged_++;
213 }
214 cv_.notify_all();
215 } else {
216 active_frame_state_ = std::move(new_frame_state);
217 }
218
219 return 0;
220 }
221
ThreadFn(const std::shared_ptr<DrmAtomicStateManager> & dasm)222 void DrmAtomicStateManager::ThreadFn(
223 const std::shared_ptr<DrmAtomicStateManager> &dasm) {
224 int tracking_at_the_moment = -1;
225 auto &main_mutex = pipe_->device->GetResMan().GetMainLock();
226
227 for (;;) {
228 SharedFd present_fence;
229
230 {
231 std::unique_lock lk(mutex_);
232 cv_.wait(lk);
233
234 if (exit_thread_ || dasm.use_count() == 1)
235 break;
236
237 if (frames_staged_ <= tracking_at_the_moment)
238 continue;
239
240 tracking_at_the_moment = frames_staged_;
241
242 present_fence = last_present_fence_;
243 if (!present_fence)
244 continue;
245 }
246
247 {
248 // NOLINTNEXTLINE(misc-const-correctness)
249 ATRACE_NAME("AsyncWaitForBuffersSwap");
250 constexpr int kTimeoutMs = 500;
251 auto err = sync_wait(*present_fence, kTimeoutMs);
252 if (err != 0) {
253 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)", *present_fence, err,
254 errno);
255 }
256 }
257
258 {
259 const std::unique_lock mlk(main_mutex);
260 const std::unique_lock lk(mutex_);
261 if (exit_thread_)
262 break;
263
264 /* If resources is already cleaned-up by main thread, skip */
265 if (tracking_at_the_moment > frames_tracked_)
266 CleanupPriorFrameResources();
267 }
268 }
269
270 ALOGI("DrmAtomicStateManager thread exit");
271 }
272
CleanupPriorFrameResources()273 void DrmAtomicStateManager::CleanupPriorFrameResources() {
274 assert(frames_staged_ - frames_tracked_ == 1);
275 assert(last_present_fence_);
276
277 // NOLINTNEXTLINE(misc-const-correctness)
278 ATRACE_NAME("CleanupPriorFrameResources");
279 frames_tracked_++;
280 active_frame_state_ = std::move(staged_frame_state_);
281 last_present_fence_ = {};
282 }
283
ExecuteAtomicCommit(AtomicCommitArgs & args)284 auto DrmAtomicStateManager::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
285 auto err = CommitFrame(args);
286
287 if (!args.test_only) {
288 if (err != 0) {
289 ALOGE("Composite failed for pipeline %s",
290 pipe_->connector->Get()->GetName().c_str());
291 // Disable the hw used by the last active composition. This allows us to
292 // signal the release fences from that composition to avoid hanging.
293 AtomicCommitArgs cl_args{};
294 cl_args.composition = std::make_shared<DrmKmsPlan>();
295 if (CommitFrame(cl_args) != 0) {
296 ALOGE("Failed to clean-up active composition for pipeline %s",
297 pipe_->connector->Get()->GetName().c_str());
298 }
299 return err;
300 }
301 }
302
303 return err;
304 } // namespace android
305
ActivateDisplayUsingDPMS()306 auto DrmAtomicStateManager::ActivateDisplayUsingDPMS() -> int {
307 return drmModeConnectorSetProperty(*pipe_->device->GetFd(),
308 pipe_->connector->Get()->GetId(),
309 pipe_->connector->Get()
310 ->GetDpmsProperty()
311 .GetId(),
312 DRM_MODE_DPMS_ON);
313 }
314
315 } // namespace android
316