blob: f049f2dce6499b017b32ba00b3abb2f381534748 [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()
Sean Paulacb2a442015-06-24 18:43:01 -070042 : drm_(NULL),
43 importer_(NULL),
44 type_(DRM_COMPOSITION_TYPE_EMPTY),
45 timeline_fd_(-1),
46 timeline_(0) {
Sean Paul98e73c82015-06-24 14:38:49 -070047}
48
49DrmDisplayComposition::~DrmDisplayComposition() {
50 for (DrmCompositionLayerVector_t::iterator iter = layers_.begin();
51 iter != layers_.end(); ++iter) {
52 if (importer_)
53 importer_->ReleaseBuffer(&iter->bo);
54
55 if (iter->layer.acquireFenceFd >= 0)
56 close(iter->layer.acquireFenceFd);
57 }
58
59 if (timeline_fd_ >= 0)
60 close(timeline_fd_);
61}
62
63int DrmDisplayComposition::Init(DrmResources *drm, Importer *importer) {
64 drm_ = drm;
65 importer_ = importer;
66
67 int ret = sw_sync_timeline_create();
68 if (ret < 0) {
69 ALOGE("Failed to create sw sync timeline %d", ret);
70 return ret;
71 }
72 timeline_fd_ = ret;
73 return 0;
74}
75
Sean Paulacb2a442015-06-24 18:43:01 -070076DrmCompositionType DrmDisplayComposition::type() const {
77 return type_;
78}
79
80bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
81 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
82}
83
Sean Paul98e73c82015-06-24 14:38:49 -070084int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, hwc_drm_bo_t *bo,
85 DrmCrtc *crtc, DrmPlane *plane) {
86 if (layer->transform != 0)
87 return -EINVAL;
88
Sean Paulacb2a442015-06-24 18:43:01 -070089 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
90 return -EINVAL;
91
Sean Paul98e73c82015-06-24 14:38:49 -070092 ++timeline_;
93 layer->releaseFenceFd =
94 sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
95 if (layer->releaseFenceFd < 0) {
96 ALOGE("Could not create release fence %d", layer->releaseFenceFd);
97 return layer->releaseFenceFd;
98 }
99
100 DrmCompositionLayer_t c_layer;
101 c_layer.layer = *layer;
102 c_layer.bo = *bo;
103 c_layer.crtc = crtc;
104 c_layer.plane = plane;
105
106 layer->acquireFenceFd = -1; // We own this now
107 layers_.push_back(c_layer);
Sean Paulacb2a442015-06-24 18:43:01 -0700108 type_ = DRM_COMPOSITION_TYPE_FRAME;
Sean Paul98e73c82015-06-24 14:38:49 -0700109 return 0;
110}
111
112int DrmDisplayComposition::FinishComposition() {
113 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_);
114 if (ret)
115 ALOGE("Failed to increment sync timeline %d", ret);
116
117 return ret;
118}
119
120DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
121 return &layers_;
122}
123}