OSDN Git Service

drm_hwcomposer: Make single atomic function for all atomic commit ops.
[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 auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
71   ATRACE_CALL();
72
73   if (args.active && *args.active == active_kms_data.active_state) {
74     /* Don't set the same state twice */
75     args.active.reset();
76   }
77
78   if (!args.HasInputs()) {
79     /* nothing to do */
80     return 0;
81   }
82
83   if (!active_kms_data.active_state) {
84     /* Force activate display */
85     args.active = true;
86   }
87
88   if (args.clear_active_composition && args.composition) {
89     ALOGE("%s: Invalid arguments", __func__);
90     return -EINVAL;
91   }
92
93   DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
94
95   DrmConnector *connector = drm->GetConnectorForDisplay(display_);
96   if (!connector) {
97     ALOGE("Could not locate connector for display %d", display_);
98     return -ENODEV;
99   }
100   DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
101   if (!crtc) {
102     ALOGE("Could not locate crtc for display %d", display_);
103     return -ENODEV;
104   }
105
106   auto pset = MakeDrmModeAtomicReqUnique();
107   if (!pset) {
108     ALOGE("Failed to allocate property set");
109     return -ENOMEM;
110   }
111
112   int64_t out_fence = -1;
113   if (crtc->out_fence_ptr_property() &&
114       !crtc->out_fence_ptr_property().AtomicSet(*pset, (uint64_t)&out_fence)) {
115     return -EINVAL;
116   }
117
118   DrmModeUserPropertyBlobUnique mode_blob;
119
120   if (args.active) {
121     if (!crtc->active_property().AtomicSet(*pset, *args.active) ||
122         !connector->crtc_id_property().AtomicSet(*pset, crtc->id())) {
123       return -EINVAL;
124     }
125   }
126
127   if (args.display_mode) {
128     mode_blob = args.display_mode.value().CreateModeBlob(
129         *resource_manager_->GetDrmDevice(display_));
130
131     if (!mode_blob) {
132       ALOGE("Failed to create mode_blob");
133       return -EINVAL;
134     }
135
136     if (!crtc->mode_property().AtomicSet(*pset, *mode_blob)) {
137       return -EINVAL;
138     }
139   }
140
141   if (args.composition) {
142     std::vector<DrmHwcLayer> &layers = args.composition->layers();
143     std::vector<DrmCompositionPlane> &comp_planes = args.composition
144                                                         ->composition_planes();
145
146     for (DrmCompositionPlane &comp_plane : comp_planes) {
147       DrmPlane *plane = comp_plane.plane();
148       std::vector<size_t> &source_layers = comp_plane.source_layers();
149
150       if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
151         if (source_layers.size() > 1) {
152           ALOGE("Can't handle more than one source layer sz=%zu type=%d",
153                 source_layers.size(), comp_plane.type());
154           continue;
155         }
156
157         if (source_layers.empty() || source_layers.front() >= layers.size()) {
158           ALOGE("Source layer index %zu out of bounds %zu type=%d",
159                 source_layers.front(), layers.size(), comp_plane.type());
160           return -EINVAL;
161         }
162         DrmHwcLayer &layer = layers[source_layers.front()];
163
164         if (plane->AtomicSetState(*pset, layer, source_layers.front(),
165                                   crtc->id()) != 0) {
166           return -EINVAL;
167         }
168       } else {
169         if (plane->AtomicDisablePlane(*pset) != 0) {
170           return -EINVAL;
171         }
172       }
173     }
174   }
175
176   if (args.clear_active_composition && active_kms_data.composition) {
177     auto &comp_planes = active_kms_data.composition->composition_planes();
178     for (auto &comp_plane : comp_planes) {
179       if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
180         return -EINVAL;
181       }
182     }
183   }
184
185   uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
186   if (args.test_only)
187     flags |= DRM_MODE_ATOMIC_TEST_ONLY;
188
189   int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
190   if (err) {
191     if (!args.test_only)
192       ALOGE("Failed to commit pset ret=%d\n", err);
193     return err;
194   }
195
196   if (!args.test_only) {
197     if (args.display_mode) {
198       connector->set_active_mode(*args.display_mode);
199       active_kms_data.mode_blob = std::move(mode_blob);
200     }
201
202     if (args.clear_active_composition) {
203       active_kms_data.composition.reset();
204     }
205
206     if (args.composition) {
207       active_kms_data.composition = args.composition;
208     }
209
210     if (args.active) {
211       active_kms_data.active_state = *args.active;
212     }
213
214     if (crtc->out_fence_ptr_property()) {
215       args.out_fence = UniqueFd((int)out_fence);
216     }
217   }
218
219   return 0;
220 }
221
222 auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
223   int err = CommitFrame(args);
224
225   if (!args.test_only) {
226     if (err) {
227       ALOGE("Composite failed for display %d", display_);
228       // Disable the hw used by the last active composition. This allows us to
229       // signal the release fences from that composition to avoid hanging.
230       AtomicCommitArgs cl_args = {.clear_active_composition = true};
231       if (CommitFrame(cl_args)) {
232         ALOGE("Failed to clean-up active composition for display %d", display_);
233       }
234       return err;
235     }
236   }
237
238   return err;
239 }  // namespace android
240
241 }  // namespace android