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-display-composition"
19 #include "drmdisplaycomposition.h"
22 #include "drmresources.h"
26 #include <cutils/log.h>
28 #include <sync/sync.h>
29 #include <xf86drmMode.h>
33 DrmCompositionLayer::DrmCompositionLayer() : crtc(NULL), plane(NULL) {
34 memset(&layer, 0, sizeof(layer));
35 layer.acquireFenceFd = -1;
36 memset(&bo, 0, sizeof(bo));
39 DrmCompositionLayer::~DrmCompositionLayer() {
42 DrmDisplayComposition::DrmDisplayComposition()
45 type_(DRM_COMPOSITION_TYPE_EMPTY),
49 dpms_mode_(DRM_MODE_DPMS_ON) {
52 DrmDisplayComposition::~DrmDisplayComposition() {
53 for (DrmCompositionLayerVector_t::iterator iter = layers_.begin();
54 iter != layers_.end(); ++iter) {
55 if (importer_ && iter->bo.fb_id)
56 importer_->ReleaseBuffer(&iter->bo);
58 if (iter->layer.acquireFenceFd >= 0)
59 close(iter->layer.acquireFenceFd);
62 if (timeline_fd_ >= 0) {
69 int DrmDisplayComposition::Init(DrmResources *drm, Importer *importer) {
73 int ret = sw_sync_timeline_create();
75 ALOGE("Failed to create sw sync timeline %d", ret);
82 DrmCompositionType DrmDisplayComposition::type() const {
86 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
87 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
90 int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, hwc_drm_bo_t *bo,
91 DrmCrtc *crtc, DrmPlane *plane) {
92 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
96 layer->releaseFenceFd =
97 sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
98 if (layer->releaseFenceFd < 0) {
99 ALOGE("Could not create release fence %d", layer->releaseFenceFd);
100 return layer->releaseFenceFd;
103 DrmCompositionLayer_t c_layer;
104 c_layer.layer = *layer;
107 c_layer.plane = plane;
109 layer->acquireFenceFd = -1; // We own this now
110 layers_.push_back(c_layer);
111 type_ = DRM_COMPOSITION_TYPE_FRAME;
115 int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, DrmCrtc *crtc,
117 if (layer->transform != 0)
120 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
124 int ret = importer_->ImportBuffer(layer->handle, &bo);
126 ALOGE("Failed to import handle of layer %d", ret);
130 ret = AddLayer(layer, &bo, crtc, plane);
132 importer_->ReleaseBuffer(&bo);
137 int DrmDisplayComposition::AddDpmsMode(uint32_t dpms_mode) {
138 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
140 dpms_mode_ = dpms_mode;
141 type_ = DRM_COMPOSITION_TYPE_DPMS;
145 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
146 DrmCompositionLayer_t c_layer;
148 c_layer.plane = plane;
149 layers_.push_back(c_layer);
153 int DrmDisplayComposition::FinishComposition() {
154 int timeline_increase = timeline_ - timeline_current_;
155 if (timeline_increase <= 0)
158 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
160 ALOGE("Failed to increment sync timeline %d", ret);
162 timeline_current_ = timeline_;
167 DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
171 uint32_t DrmDisplayComposition::dpms_mode() const {