blob: 75046fb03d1e2a2a735d8a0e2ed3bc050f444a64 [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
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 LOG_TAG "hwc-drm-display-composition"
18
19#include "drmdisplaycomposition.h"
20#include "drmcrtc.h"
21#include "drmplane.h"
22#include "drmresources.h"
23
24#include <stdlib.h>
25
26#include <cutils/log.h>
27#include <sw_sync.h>
28#include <sync/sync.h>
29
30namespace android {
31
32DrmCompositionLayer::DrmCompositionLayer() : crtc(NULL), plane(NULL) {
33 memset(&layer, 0, sizeof(layer));
34 layer.acquireFenceFd = -1;
35 memset(&bo, 0, sizeof(bo));
36}
37
38DrmCompositionLayer::~DrmCompositionLayer() {
39}
40
41DrmDisplayComposition::DrmDisplayComposition()
42 : drm_(NULL), importer_(NULL), timeline_fd_(-1), timeline_(0) {
43}
44
45DrmDisplayComposition::~DrmDisplayComposition() {
46 for (DrmCompositionLayerVector_t::iterator iter = layers_.begin();
47 iter != layers_.end(); ++iter) {
48 if (importer_)
49 importer_->ReleaseBuffer(&iter->bo);
50
51 if (iter->layer.acquireFenceFd >= 0)
52 close(iter->layer.acquireFenceFd);
53 }
54
55 if (timeline_fd_ >= 0)
56 close(timeline_fd_);
57}
58
59int DrmDisplayComposition::Init(DrmResources *drm, Importer *importer) {
60 drm_ = drm;
61 importer_ = importer;
62
63 int ret = sw_sync_timeline_create();
64 if (ret < 0) {
65 ALOGE("Failed to create sw sync timeline %d", ret);
66 return ret;
67 }
68 timeline_fd_ = ret;
69 return 0;
70}
71
72int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, hwc_drm_bo_t *bo,
73 DrmCrtc *crtc, DrmPlane *plane) {
74 if (layer->transform != 0)
75 return -EINVAL;
76
77 ++timeline_;
78 layer->releaseFenceFd =
79 sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
80 if (layer->releaseFenceFd < 0) {
81 ALOGE("Could not create release fence %d", layer->releaseFenceFd);
82 return layer->releaseFenceFd;
83 }
84
85 DrmCompositionLayer_t c_layer;
86 c_layer.layer = *layer;
87 c_layer.bo = *bo;
88 c_layer.crtc = crtc;
89 c_layer.plane = plane;
90
91 layer->acquireFenceFd = -1; // We own this now
92 layers_.push_back(c_layer);
93 return 0;
94}
95
96int DrmDisplayComposition::FinishComposition() {
97 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_);
98 if (ret)
99 ALOGE("Failed to increment sync timeline %d", ret);
100
101 return ret;
102}
103
104DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
105 return &layers_;
106}
107}