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 auto DrmDisplayCompositor::CommitFrame(AtomicCommitArgs &args) -> int {
73 if (args.active && *args.active == active_kms_data.active_state) {
74 /* Don't set the same state twice */
78 if (!args.HasInputs()) {
83 if (!active_kms_data.active_state) {
84 /* Force activate display */
88 if (args.clear_active_composition && args.composition) {
89 ALOGE("%s: Invalid arguments", __func__);
93 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
95 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
97 ALOGE("Could not locate connector for display %d", display_);
100 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
102 ALOGE("Could not locate crtc for display %d", display_);
106 auto pset = MakeDrmModeAtomicReqUnique();
108 ALOGE("Failed to allocate property set");
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)) {
118 DrmModeUserPropertyBlobUnique mode_blob;
121 if (!crtc->active_property().AtomicSet(*pset, *args.active) ||
122 !connector->crtc_id_property().AtomicSet(*pset, crtc->id())) {
127 if (args.display_mode) {
128 mode_blob = args.display_mode.value().CreateModeBlob(
129 *resource_manager_->GetDrmDevice(display_));
132 ALOGE("Failed to create mode_blob");
136 if (!crtc->mode_property().AtomicSet(*pset, *mode_blob)) {
141 if (args.composition) {
142 std::vector<DrmHwcLayer> &layers = args.composition->layers();
143 std::vector<DrmCompositionPlane> &comp_planes = args.composition
144 ->composition_planes();
146 for (DrmCompositionPlane &comp_plane : comp_planes) {
147 DrmPlane *plane = comp_plane.plane();
148 std::vector<size_t> &source_layers = comp_plane.source_layers();
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());
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());
162 DrmHwcLayer &layer = layers[source_layers.front()];
164 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
169 if (plane->AtomicDisablePlane(*pset) != 0) {
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) {
185 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
187 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
189 int err = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
192 ALOGE("Failed to commit pset ret=%d\n", err);
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);
202 if (args.clear_active_composition) {
203 active_kms_data.composition.reset();
206 if (args.composition) {
207 active_kms_data.composition = args.composition;
211 active_kms_data.active_state = *args.active;
214 if (crtc->out_fence_ptr_property()) {
215 args.out_fence = UniqueFd((int)out_fence);
222 auto DrmDisplayCompositor::ExecuteAtomicCommit(AtomicCommitArgs &args) -> int {
223 int err = CommitFrame(args);
225 if (!args.test_only) {
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_);
239 } // namespace android
241 } // namespace android