blob: f4e8d9df46e3d500268e80e355c2a7a7654e68c4 [file] [log] [blame]
Sean Paulb386f1b2015-05-13 06:33:23 -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-composition"
18
19#include "drmcomposition.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
Sean Paulc51b0c52015-06-10 14:32:51 -040032static const bool kUseOverlayPlanes = true;
Sean Paulb386f1b2015-05-13 06:33:23 -070033
Sean Paul98e73c82015-06-24 14:38:49 -070034DrmComposition::DrmComposition(DrmResources *drm, Importer *importer)
35 : drm_(drm), importer_(importer) {
Sean Paulb386f1b2015-05-13 06:33:23 -070036 for (DrmResources::PlaneIter iter = drm_->begin_planes();
37 iter != drm_->end_planes(); ++iter) {
38 if ((*iter)->type() == DRM_PLANE_TYPE_PRIMARY)
39 primary_planes_.push_back(*iter);
40 else if (kUseOverlayPlanes && (*iter)->type() == DRM_PLANE_TYPE_OVERLAY)
41 overlay_planes_.push_back(*iter);
42 }
43}
44
45DrmComposition::~DrmComposition() {
Sean Paulb386f1b2015-05-13 06:33:23 -070046}
47
48int DrmComposition::Init() {
Sean Paul98e73c82015-06-24 14:38:49 -070049 for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
50 iter != drm_->end_connectors(); ++iter) {
51 int display = (*iter)->display();
52 composition_map_[display].reset(new DrmDisplayComposition());
53 if (!composition_map_[display]) {
54 ALOGE("Failed to allocate new display composition\n");
55 return -ENOMEM;
56 }
57 int ret = composition_map_[(*iter)->display()]->Init(drm_, importer_);
58 if (ret) {
59 ALOGE("Failed to init display composition for %d", (*iter)->display());
60 return ret;
61 }
Sean Paulb386f1b2015-05-13 06:33:23 -070062 }
Sean Paulb386f1b2015-05-13 06:33:23 -070063 return 0;
64}
65
Zach Reizner713a6782015-07-31 15:12:44 -070066unsigned DrmComposition::GetRemainingLayers(int /*display*/,
Sean Paulb386f1b2015-05-13 06:33:23 -070067 unsigned num_needed) const {
Zach Reizner713a6782015-07-31 15:12:44 -070068 return num_needed;
Sean Paulb386f1b2015-05-13 06:33:23 -070069}
70
71int DrmComposition::AddLayer(int display, hwc_layer_1_t *layer,
72 hwc_drm_bo *bo) {
Sean Paulb386f1b2015-05-13 06:33:23 -070073 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
74 if (!crtc) {
Sean Paul98e73c82015-06-24 14:38:49 -070075 ALOGE("Failed to find crtc for display %d", display);
Sean Paulb386f1b2015-05-13 06:33:23 -070076 return -ENODEV;
77 }
78
Sean Paul98e73c82015-06-24 14:38:49 -070079 // Find a plane for the layer
80 DrmPlane *plane = NULL;
Sean Paulb386f1b2015-05-13 06:33:23 -070081 for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin();
82 iter != primary_planes_.end(); ++iter) {
83 if ((*iter)->GetCrtcSupported(*crtc)) {
Sean Paul98e73c82015-06-24 14:38:49 -070084 plane = *iter;
Sean Paulb386f1b2015-05-13 06:33:23 -070085 primary_planes_.erase(iter);
86 break;
87 }
88 }
Sean Paul9099aa52015-07-09 17:51:50 -040089 for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin();
Sean Paul98e73c82015-06-24 14:38:49 -070090 !plane && iter != overlay_planes_.end(); ++iter) {
Sean Paulb386f1b2015-05-13 06:33:23 -070091 if ((*iter)->GetCrtcSupported(*crtc)) {
Sean Paul98e73c82015-06-24 14:38:49 -070092 plane = *iter;
Sean Paulb386f1b2015-05-13 06:33:23 -070093 overlay_planes_.erase(iter);
94 break;
95 }
96 }
Sean Paul98e73c82015-06-24 14:38:49 -070097 return composition_map_[display]->AddLayer(layer, bo, crtc, plane);
Sean Paulb386f1b2015-05-13 06:33:23 -070098}
99
Sean Pauldb7a17d2015-06-24 18:46:05 -0700100int DrmComposition::AddDpmsMode(int display, uint32_t dpms_mode) {
101 return composition_map_[display]->AddDpmsMode(dpms_mode);
102}
103
Sean Paul98e73c82015-06-24 14:38:49 -0700104std::unique_ptr<DrmDisplayComposition> DrmComposition::TakeDisplayComposition(
105 int display) {
106 return std::move(composition_map_[display]);
Sean Paulb386f1b2015-05-13 06:33:23 -0700107}
Sean Paul2e46fbd2015-07-09 17:22:22 -0400108
109int DrmComposition::DisableUnusedPlanes() {
110 for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
111 iter != drm_->end_connectors(); ++iter) {
112 int display = (*iter)->display();
113 DrmDisplayComposition *comp = GetDisplayComposition(display);
114
115 /*
116 * Leave empty compositions alone
117 * TODO: re-visit this and potentially disable leftover planes after the
118 * active compositions have gobbled up all they can
119 */
120 if (comp->type() == DRM_COMPOSITION_TYPE_EMPTY)
121 continue;
122
123 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
124 if (!crtc) {
125 ALOGE("Failed to find crtc for display %d", display);
126 continue;
127 }
128
129 for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin();
130 iter != primary_planes_.end(); ++iter) {
131 if ((*iter)->GetCrtcSupported(*crtc)) {
132 comp->AddPlaneDisable(*iter);
133 primary_planes_.erase(iter);
134 break;
135 }
136 }
137 for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin();
138 iter != overlay_planes_.end();) {
139 if ((*iter)->GetCrtcSupported(*crtc)) {
140 comp->AddPlaneDisable(*iter);
141 iter = overlay_planes_.erase(iter);
142 } else {
143 iter++;
144 }
145 }
146 }
147 return 0;
148}
149
150DrmDisplayComposition *DrmComposition::GetDisplayComposition(int display) {
151 return composition_map_[display].get();
152}
Sean Paulb386f1b2015-05-13 06:33:23 -0700153}