OSDN Git Service

660052a0fabbc092ef21a606966870984f2abbec
[android-x86/external-drm_hwcomposer.git] / compositor / DrmDisplayCompositor.cpp
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-display-compositor"
19
20 #include "DrmDisplayCompositor.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/autolock.h"
39 #include "utils/log.h"
40
41 namespace android {
42
43 auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
44     -> int {
45   resource_manager_ = resource_manager;
46   display_ = display;
47   DrmDevice *drm = resource_manager_->GetDrmDevice(display);
48   if (!drm) {
49     ALOGE("Could not find drmdevice for display");
50     return -EINVAL;
51   }
52   planner_ = Planner::CreateInstance(drm);
53
54   initialized_ = true;
55   return 0;
56 }
57
58 std::unique_ptr<DrmDisplayComposition>
59 DrmDisplayCompositor::CreateInitializedComposition() const {
60   DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
61   DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
62   if (!crtc) {
63     ALOGE("Failed to find crtc for display = %d", display_);
64     return std::unique_ptr<DrmDisplayComposition>();
65   }
66
67   return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
68 }
69
70 // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
71 auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
72   ATRACE_CALL();
73
74   if (args.active && *args.active == active_frame_state.crtc_active_state) {
75     /* Don't set the same state twice */
76     args.active.reset();
77   }
78
79   if (!args.HasInputs()) {
80     /* nothing to do */
81     return 0;
82   }
83
84   if (!active_frame_state.crtc_active_state) {
85     /* Force activate display */
86     args.active = true;
87   }
88
89   if (args.clear_active_composition && args.composition) {
90     ALOGE("%s: Invalid arguments", __func__);
91     return -EINVAL;
92   }
93
94   auto new_frame_state = NewFrameState();
95
96   DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
97
98   DrmConnector *connector = drm->GetConnectorForDisplay(display_);
99   if (!connector) {
100     ALOGE("Could not locate connector for display %d", display_);
101     return -ENODEV;
102   }
103   DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
104   if (!crtc) {
105     ALOGE("Could not locate crtc for display %d", display_);
106     return -ENODEV;
107   }
108
109   auto pset = MakeDrmModeAtomicReqUnique();
110   if (!pset) {
111     ALOGE("Failed to allocate property set");
112     return -ENOMEM;
113   }
114
115   int64_t out_fence = -1;
116   if (crtc->out_fence_ptr_property() &&
117       !crtc->out_fence_ptr_property().AtomicSet(*pset, (uint64_t)&out_fence)) {
118     return -EINVAL;
119   }
120
121   if (args.active) {
122     new_frame_state.crtc_active_state = *args.active;
123     if (!crtc->active_property().AtomicSet(*pset, *args.active) ||
124         !connector->crtc_id_property().AtomicSet(*pset, crtc->id())) {
125       return -EINVAL;
126     }
127   }
128
129   if (args.display_mode) {
130     new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(
131         *resource_manager_->GetDrmDevice(display_));
132
133     if (!new_frame_state.mode_blob) {
134       ALOGE("Failed to create mode_blob");
135       return -EINVAL;
136     }
137
138     if (!crtc->mode_property().AtomicSet(*pset, *new_frame_state.mode_blob)) {
139       return -EINVAL;
140     }
141   }
142
143   auto unused_planes = new_frame_state.used_planes;
144
145   if (args.composition) {
146     new_frame_state.used_framebuffers.clear();
147     new_frame_state.used_planes.clear();
148
149     std::vector<DrmHwcLayer> &layers = args.composition->layers();
150     std::vector<DrmCompositionPlane> &comp_planes = args.composition
151                                                         ->composition_planes();
152
153     for (DrmCompositionPlane &comp_plane : comp_planes) {
154       DrmPlane *plane = comp_plane.plane();
155       std::vector<size_t> &source_layers = comp_plane.source_layers();
156
157       if (source_layers.size() > 1) {
158         ALOGE("Can't handle more than one source layer sz=%zu",
159               source_layers.size());
160         continue;
161       }
162
163       if (source_layers.empty() || source_layers.front() >= layers.size()) {
164         ALOGE("Source layer index %zu out of bounds %zu", source_layers.front(),
165               layers.size());
166         return -EINVAL;
167       }
168       DrmHwcLayer &layer = layers[source_layers.front()];
169
170       new_frame_state.used_framebuffers.emplace_back(layer.FbIdHandle);
171       new_frame_state.used_planes.emplace_back(plane);
172
173       /* Remove from 'unused' list, since plane is re-used */
174       auto &v = unused_planes;
175       v.erase(std::remove(v.begin(), v.end(), plane), v.end());
176
177       if (plane->AtomicSetState(*pset, layer, source_layers.front(),
178                                 crtc->id()) != 0) {
179         return -EINVAL;
180       }
181     }
182   }
183
184   if (args.clear_active_composition) {
185     new_frame_state.used_framebuffers.clear();
186     new_frame_state.used_planes.clear();
187   }
188
189   if (args.clear_active_composition || args.composition) {
190     for (auto *plane : unused_planes) {
191       if (plane->AtomicDisablePlane(*pset) != 0) {
192         return -EINVAL;
193       }
194     }
195   }
196
197   uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
198   if (args.test_only)
199     flags |= DRM_MODE_ATOMIC_TEST_ONLY;
200
201   int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
202   if (err) {
203     if (!args.test_only)
204       ALOGE("Failed to commit pset ret=%d\n", err);
205     return err;
206   }
207
208   if (!args.test_only) {
209     if (args.display_mode) {
210       /* TODO(nobody): we still need this for synthetic vsync, remove after
211        * vsync reworked */
212       connector->set_active_mode(*args.display_mode);
213     }
214
215     active_frame_state = std::move(new_frame_state);
216
217     if (crtc->out_fence_ptr_property()) {
218       args.out_fence = UniqueFd((int)out_fence);
219     }
220   }
221
222   return 0;
223 }
224
225 auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
226   int err = CommitFrame(args);
227
228   if (!args.test_only) {
229     if (err) {
230       ALOGE("Composite failed for display %d", display_);
231       // Disable the hw used by the last active composition. This allows us to
232       // signal the release fences from that composition to avoid hanging.
233       AtomicCommitArgs cl_args = {.clear_active_composition = true};
234       if (CommitFrame(cl_args)) {
235         ALOGE("Failed to clean-up active composition for display %d", display_);
236       }
237       return err;
238     }
239   }
240
241   return err;
242 }  // namespace android
243
244 }  // namespace android