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 LOG_TAG "hwc-drm-composition"
19 #include "drmcomposition.h"
22 #include "drmresources.h"
26 #include <cutils/log.h>
28 #include <sync/sync.h>
32 static const bool kUseOverlayPlanes = false;
34 DrmCompositionLayer::DrmCompositionLayer() : crtc(NULL), plane(NULL) {
35 memset(&layer, 0, sizeof(layer));
36 layer.acquireFenceFd = -1;
37 memset(&bo, 0, sizeof(bo));
40 DrmCompositionLayer::~DrmCompositionLayer() {
43 DrmComposition::DrmComposition(DrmResources *drm, Importer *importer,
50 for (DrmResources::PlaneIter iter = drm_->begin_planes();
51 iter != drm_->end_planes(); ++iter) {
52 if ((*iter)->type() == DRM_PLANE_TYPE_PRIMARY)
53 primary_planes_.push_back(*iter);
54 else if (kUseOverlayPlanes && (*iter)->type() == DRM_PLANE_TYPE_OVERLAY)
55 overlay_planes_.push_back(*iter);
59 DrmComposition::~DrmComposition() {
60 for (DrmCompositionLayerMap_t::iterator iter = composition_map_.begin();
61 iter != composition_map_.end(); ++iter) {
62 importer_->ReleaseBuffer(&iter->second.bo);
64 if (iter->second.layer.acquireFenceFd >= 0)
65 close(iter->second.layer.acquireFenceFd);
68 if (timeline_fd_ >= 0)
72 int DrmComposition::Init() {
73 int ret = sw_sync_timeline_create();
75 ALOGE("Failed to create sw sync timeline %d", ret);
82 unsigned DrmComposition::GetRemainingLayers(int display,
83 unsigned num_needed) const {
84 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
86 ALOGW("Failed to find crtc for display %d", display);
90 unsigned num_planes = 0;
91 for (std::vector<DrmPlane *>::const_iterator iter = primary_planes_.begin();
92 iter != primary_planes_.end(); ++iter) {
93 if ((*iter)->GetCrtcSupported(*crtc))
96 for (std::deque<DrmPlane *>::const_iterator iter = overlay_planes_.begin();
97 iter != overlay_planes_.end(); ++iter) {
98 if ((*iter)->GetCrtcSupported(*crtc))
101 return std::min(num_planes, num_needed);
104 int DrmComposition::AddLayer(int display, hwc_layer_1_t *layer,
106 if (layer->transform != 0)
109 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
111 ALOGE("Could not find crtc for display %d", display);
116 layer->releaseFenceFd =
117 sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
118 if (layer->releaseFenceFd < 0) {
119 ALOGE("Could not create release fence %d", layer->releaseFenceFd);
120 return layer->releaseFenceFd;
123 DrmCompositionLayer_t c_layer;
124 c_layer.layer = *layer;
128 // First try to find a primary plane for the layer, then fallback on overlays
129 for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin();
130 iter != primary_planes_.end(); ++iter) {
131 if ((*iter)->GetCrtcSupported(*crtc)) {
132 c_layer.plane = (*iter);
133 primary_planes_.erase(iter);
137 for (std::deque<DrmPlane *>::iterator iter = overlay_planes_.begin();
138 !c_layer.plane && iter != overlay_planes_.end(); ++iter) {
139 if ((*iter)->GetCrtcSupported(*crtc)) {
140 c_layer.plane = (*iter);
141 overlay_planes_.erase(iter);
145 if (!c_layer.plane) {
146 close(layer->releaseFenceFd);
147 layer->releaseFenceFd = -1;
151 layer->acquireFenceFd = -1; // We own this now
152 composition_map_.insert(DrmCompositionLayerPair_t(display, c_layer));
156 int DrmComposition::FinishComposition() {
157 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_);
159 ALOGE("Failed to increment sync timeline %d", ret);
164 DrmCompositionLayerMap_t *DrmComposition::GetCompositionMap() {
165 return &composition_map_;