• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwc-drm-atomic-state-manager"
19 
20 #include "DrmAtomicStateManager.h"
21 
22 #include <drm/drm_mode.h>
23 #include <pthread.h>
24 #include <sched.h>
25 #include <sync/sync.h>
26 #include <utils/Trace.h>
27 
28 #include <array>
29 #include <cstdlib>
30 #include <ctime>
31 #include <sstream>
32 #include <vector>
33 
34 #include "drm/DrmCrtc.h"
35 #include "drm/DrmDevice.h"
36 #include "drm/DrmPlane.h"
37 #include "drm/DrmUnique.h"
38 #include "utils/log.h"
39 
40 namespace android {
41 
42 // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
CommitFrame(AtomicCommitArgs & args)43 auto DrmAtomicStateManager::CommitFrame(AtomicCommitArgs &args) -> int {
44   ATRACE_CALL();
45 
46   if (args.active && *args.active == active_frame_state_.crtc_active_state) {
47     /* Don't set the same state twice */
48     args.active.reset();
49   }
50 
51   if (!args.HasInputs()) {
52     /* nothing to do */
53     return 0;
54   }
55 
56   if (!active_frame_state_.crtc_active_state) {
57     /* Force activate display */
58     args.active = true;
59   }
60 
61   auto new_frame_state = NewFrameState();
62 
63   auto *drm = pipe_->device;
64   auto *connector = pipe_->connector->Get();
65   auto *crtc = pipe_->crtc->Get();
66 
67   auto pset = MakeDrmModeAtomicReqUnique();
68   if (!pset) {
69     ALOGE("Failed to allocate property set");
70     return -ENOMEM;
71   }
72 
73   int64_t out_fence = -1;
74   if (crtc->GetOutFencePtrProperty() &&
75       !crtc->GetOutFencePtrProperty().AtomicSet(*pset, uint64_t(&out_fence))) {
76     return -EINVAL;
77   }
78 
79   if (args.active) {
80     new_frame_state.crtc_active_state = *args.active;
81     if (!crtc->GetActiveProperty().AtomicSet(*pset, *args.active ? 1 : 0) ||
82         !connector->GetCrtcIdProperty().AtomicSet(*pset, crtc->GetId())) {
83       return -EINVAL;
84     }
85   }
86 
87   if (args.display_mode) {
88     new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(*drm);
89 
90     if (!new_frame_state.mode_blob) {
91       ALOGE("Failed to create mode_blob");
92       return -EINVAL;
93     }
94 
95     if (!crtc->GetModeProperty().AtomicSet(*pset, *new_frame_state.mode_blob)) {
96       return -EINVAL;
97     }
98   }
99 
100   auto unused_planes = new_frame_state.used_planes;
101 
102   if (args.composition) {
103     new_frame_state.used_framebuffers.clear();
104     new_frame_state.used_planes.clear();
105 
106     for (auto &joining : args.composition->plan) {
107       DrmPlane *plane = joining.plane->Get();
108       DrmHwcLayer &layer = joining.layer;
109 
110       new_frame_state.used_framebuffers.emplace_back(layer.fb_id_handle);
111       new_frame_state.used_planes.emplace_back(joining.plane);
112 
113       /* Remove from 'unused' list, since plane is re-used */
114       auto &v = unused_planes;
115       v.erase(std::remove(v.begin(), v.end(), joining.plane), v.end());
116 
117       if (plane->AtomicSetState(*pset, layer, joining.z_pos, crtc->GetId()) !=
118           0) {
119         return -EINVAL;
120       }
121     }
122   }
123 
124   if (args.composition) {
125     for (auto &plane : unused_planes) {
126       if (plane->Get()->AtomicDisablePlane(*pset) != 0) {
127         return -EINVAL;
128       }
129     }
130   }
131 
132   uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
133   if (args.test_only)
134     flags |= DRM_MODE_ATOMIC_TEST_ONLY;
135 
136   int err = drmModeAtomicCommit(drm->GetFd(), pset.get(), flags, drm);
137   if (err != 0) {
138     if (!args.test_only)
139       ALOGE("Failed to commit pset ret=%d\n", err);
140     return err;
141   }
142 
143   if (!args.test_only) {
144     if (args.display_mode) {
145       /* TODO(nobody): we still need this for synthetic vsync, remove after
146        * vsync reworked */
147       connector->SetActiveMode(*args.display_mode);
148     }
149 
150     active_frame_state_ = std::move(new_frame_state);
151 
152     if (crtc->GetOutFencePtrProperty()) {
153       args.out_fence = UniqueFd((int)out_fence);
154     }
155   }
156 
157   return 0;
158 }
159 
ExecuteAtomicCommit(AtomicCommitArgs & args)160 auto DrmAtomicStateManager::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
161   int err = CommitFrame(args);
162 
163   if (!args.test_only) {
164     if (err != 0) {
165       ALOGE("Composite failed for pipeline %s",
166             pipe_->connector->Get()->GetName().c_str());
167       // Disable the hw used by the last active composition. This allows us to
168       // signal the release fences from that composition to avoid hanging.
169       AtomicCommitArgs cl_args{};
170       cl_args.composition = std::make_shared<DrmKmsPlan>();
171       if (CommitFrame(cl_args) != 0) {
172         ALOGE("Failed to clean-up active composition for pipeline %s",
173               pipe_->connector->Get()->GetName().c_str());
174       }
175       return err;
176     }
177   }
178 
179   return err;
180 }  // namespace android
181 
ActivateDisplayUsingDPMS()182 auto DrmAtomicStateManager::ActivateDisplayUsingDPMS() -> int {
183   return drmModeConnectorSetProperty(pipe_->device->GetFd(),
184                                      pipe_->connector->Get()->GetId(),
185                                      pipe_->connector->Get()
186                                          ->GetDpmsProperty()
187                                          .id(),
188                                      DRM_MODE_DPMS_ON);
189 }
190 
191 }  // namespace android
192