2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwc-drm-display-compositor"
20 #include "DrmDisplayCompositor.h"
22 #include <drm/drm_mode.h>
25 #include <sync/sync.h>
26 #include <utils/Trace.h>
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"
43 auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
45 resource_manager_ = resource_manager;
47 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
49 ALOGE("Could not find drmdevice for display");
52 planner_ = Planner::CreateInstance(drm);
58 std::unique_ptr<DrmDisplayComposition>
59 DrmDisplayCompositor::CreateInitializedComposition() const {
60 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
61 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
63 ALOGE("Failed to find crtc for display = %d", display_);
64 return std::unique_ptr<DrmDisplayComposition>();
67 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
70 // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
71 auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
74 if (args.active && *args.active == active_frame_state.crtc_active_state) {
75 /* Don't set the same state twice */
79 if (!args.HasInputs()) {
84 if (!active_frame_state.crtc_active_state) {
85 /* Force activate display */
89 if (args.clear_active_composition && args.composition) {
90 ALOGE("%s: Invalid arguments", __func__);
94 auto new_frame_state = NewFrameState();
96 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
98 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
100 ALOGE("Could not locate connector for display %d", display_);
103 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
105 ALOGE("Could not locate crtc for display %d", display_);
109 auto pset = MakeDrmModeAtomicReqUnique();
111 ALOGE("Failed to allocate property set");
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)) {
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())) {
129 if (args.display_mode) {
130 new_frame_state.mode_blob = args.display_mode.value().CreateModeBlob(
131 *resource_manager_->GetDrmDevice(display_));
133 if (!new_frame_state.mode_blob) {
134 ALOGE("Failed to create mode_blob");
138 if (!crtc->mode_property().AtomicSet(*pset, *new_frame_state.mode_blob)) {
143 auto unused_planes = new_frame_state.used_planes;
145 if (args.composition) {
146 new_frame_state.used_framebuffers.clear();
147 new_frame_state.used_planes.clear();
149 std::vector<DrmHwcLayer> &layers = args.composition->layers();
150 std::vector<DrmCompositionPlane> &comp_planes = args.composition
151 ->composition_planes();
153 for (DrmCompositionPlane &comp_plane : comp_planes) {
154 DrmPlane *plane = comp_plane.plane();
155 std::vector<size_t> &source_layers = comp_plane.source_layers();
157 if (source_layers.size() > 1) {
158 ALOGE("Can't handle more than one source layer sz=%zu",
159 source_layers.size());
163 if (source_layers.empty() || source_layers.front() >= layers.size()) {
164 ALOGE("Source layer index %zu out of bounds %zu", source_layers.front(),
168 DrmHwcLayer &layer = layers[source_layers.front()];
170 new_frame_state.used_framebuffers.emplace_back(layer.FbIdHandle);
171 new_frame_state.used_planes.emplace_back(plane);
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());
177 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
184 if (args.clear_active_composition) {
185 new_frame_state.used_framebuffers.clear();
186 new_frame_state.used_planes.clear();
189 if (args.clear_active_composition || args.composition) {
190 for (auto *plane : unused_planes) {
191 if (plane->AtomicDisablePlane(*pset) != 0) {
197 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
199 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
201 int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
204 ALOGE("Failed to commit pset ret=%d\n", err);
208 if (!args.test_only) {
209 if (args.display_mode) {
210 /* TODO(nobody): we still need this for synthetic vsync, remove after
212 connector->set_active_mode(*args.display_mode);
215 active_frame_state = std::move(new_frame_state);
217 if (crtc->out_fence_ptr_property()) {
218 args.out_fence = UniqueFd((int)out_fence);
225 auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
226 int err = CommitFrame(args);
228 if (!args.test_only) {
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_);
242 } // namespace android
244 } // namespace android