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 "drmhwc"
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 = !args.blocking;
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 if (args.colorspace && connector->GetColorspaceProperty()) {
143 if (!connector->GetColorspaceProperty()
144 .AtomicSet(*pset, connector->GetColorspacePropertyValue(*args.colorspace)))
145 return -EINVAL;
146 }
147
148 if (args.content_type && connector->GetContentTypeProperty()) {
149 if (!connector->GetContentTypeProperty().AtomicSet(*pset, *args.content_type))
150 return -EINVAL;
151 }
152
153 auto unused_planes = new_frame_state.used_planes;
154
155 if (args.composition) {
156 new_frame_state.used_planes.clear();
157
158 for (auto &joining : args.composition->plan) {
159 DrmPlane *plane = joining.plane->Get();
160 LayerData &layer = joining.layer;
161
162 new_frame_state.used_framebuffers.emplace_back(layer.fb);
163 new_frame_state.used_planes.emplace_back(joining.plane);
164
165 /* Remove from 'unused' list, since plane is re-used */
166 auto &v = unused_planes;
167 v.erase(std::remove(v.begin(), v.end(), joining.plane), v.end());
168
169 if (plane->AtomicSetState(*pset, layer, joining.z_pos, crtc->GetId()) !=
170 0) {
171 return -EINVAL;
172 }
173 }
174 }
175
176 if (args.composition) {
177 for (auto &plane : unused_planes) {
178 if (plane->Get()->AtomicDisablePlane(*pset) != 0) {
179 return -EINVAL;
180 }
181 }
182 }
183
184 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
185
186 if (args.test_only) {
187 return drmModeAtomicCommit(*drm->GetFd(), pset.get(),
188 flags | DRM_MODE_ATOMIC_TEST_ONLY, drm);
189 }
190
191 if (last_present_fence_) {
192 // NOLINTNEXTLINE(misc-const-correctness)
193 ATRACE_NAME("WaitPriorFramePresented");
194
195 constexpr int kTimeoutMs = 500;
196 const int err = sync_wait(*last_present_fence_, kTimeoutMs);
197 if (err != 0) {
198 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)", *last_present_fence_,
199 err, errno);
200 }
201
202 CleanupPriorFrameResources();
203 }
204
205 if (nonblock) {
206 flags |= DRM_MODE_ATOMIC_NONBLOCK;
207 }
208
209 auto err = drmModeAtomicCommit(*drm->GetFd(), pset.get(), flags, drm);
210
211 if (err != 0) {
212 ALOGE("Failed to commit pset ret=%d\n", err);
213 return err;
214 }
215
216 args.out_fence = MakeSharedFd(out_fence);
217
218 if (nonblock) {
219 {
220 const std::unique_lock lock(mutex_);
221 last_present_fence_ = args.out_fence;
222 staged_frame_state_ = std::move(new_frame_state);
223 frames_staged_++;
224 }
225 cv_.notify_all();
226 } else {
227 active_frame_state_ = std::move(new_frame_state);
228 }
229
230 return 0;
231 }
232
ThreadFn(const std::shared_ptr<DrmAtomicStateManager> & dasm)233 void DrmAtomicStateManager::ThreadFn(
234 const std::shared_ptr<DrmAtomicStateManager> &dasm) {
235 int tracking_at_the_moment = -1;
236 auto &main_mutex = pipe_->device->GetResMan().GetMainLock();
237
238 for (;;) {
239 SharedFd present_fence;
240
241 {
242 std::unique_lock lk(mutex_);
243 cv_.wait(lk);
244
245 if (exit_thread_ || dasm.use_count() == 1)
246 break;
247
248 if (frames_staged_ <= tracking_at_the_moment)
249 continue;
250
251 tracking_at_the_moment = frames_staged_;
252
253 present_fence = last_present_fence_;
254 if (!present_fence)
255 continue;
256 }
257
258 {
259 // NOLINTNEXTLINE(misc-const-correctness)
260 ATRACE_NAME("AsyncWaitForBuffersSwap");
261 constexpr int kTimeoutMs = 500;
262 auto err = sync_wait(*present_fence, kTimeoutMs);
263 if (err != 0) {
264 ALOGE("sync_wait(fd=%i) returned: %i (errno: %i)", *present_fence, err,
265 errno);
266 }
267 }
268
269 {
270 const std::unique_lock mlk(main_mutex);
271 const std::unique_lock lk(mutex_);
272 if (exit_thread_)
273 break;
274
275 /* If resources is already cleaned-up by main thread, skip */
276 if (tracking_at_the_moment > frames_tracked_)
277 CleanupPriorFrameResources();
278 }
279 }
280
281 ALOGI("DrmAtomicStateManager thread exit");
282 }
283
CleanupPriorFrameResources()284 void DrmAtomicStateManager::CleanupPriorFrameResources() {
285 assert(frames_staged_ - frames_tracked_ == 1);
286 assert(last_present_fence_);
287
288 // NOLINTNEXTLINE(misc-const-correctness)
289 ATRACE_NAME("CleanupPriorFrameResources");
290 frames_tracked_++;
291 active_frame_state_ = std::move(staged_frame_state_);
292 last_present_fence_ = {};
293 }
294
ExecuteAtomicCommit(AtomicCommitArgs & args)295 auto DrmAtomicStateManager::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
296 auto err = CommitFrame(args);
297
298 if (!args.test_only) {
299 if (err != 0) {
300 ALOGE("Composite failed for pipeline %s",
301 pipe_->connector->Get()->GetName().c_str());
302 // Disable the hw used by the last active composition. This allows us to
303 // signal the release fences from that composition to avoid hanging.
304 AtomicCommitArgs cl_args{};
305 cl_args.composition = std::make_shared<DrmKmsPlan>();
306 if (CommitFrame(cl_args) != 0) {
307 ALOGE("Failed to clean-up active composition for pipeline %s",
308 pipe_->connector->Get()->GetName().c_str());
309 }
310 return err;
311 }
312 }
313
314 return err;
315 } // namespace android
316
ActivateDisplayUsingDPMS()317 auto DrmAtomicStateManager::ActivateDisplayUsingDPMS() -> int {
318 return drmModeConnectorSetProperty(*pipe_->device->GetFd(),
319 pipe_->connector->Get()->GetId(),
320 pipe_->connector->Get()
321 ->GetDpmsProperty()
322 .GetId(),
323 DRM_MODE_DPMS_ON);
324 }
325
326 } // namespace android
327