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(DrmCrtc *crtc, DrmHwcLayer &&l)
35 sf_handle(l.sf_handle),
36 buffer(std::move(l.buffer)),
37 handle(std::move(l.handle)),
38 transform(l.transform),
41 source_crop(l.source_crop),
42 display_frame(l.display_frame),
43 source_damage(l.source_damage),
44 acquire_fence(std::move(l.acquire_fence)) {
47 DrmDisplayComposition::DrmDisplayComposition()
50 type_(DRM_COMPOSITION_TYPE_EMPTY),
54 timeline_pre_comp_done_(0),
55 pre_composition_layer_index_(-1),
56 dpms_mode_(DRM_MODE_DPMS_ON),
60 DrmDisplayComposition::~DrmDisplayComposition() {
61 if (timeline_fd_ >= 0) {
68 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
69 Importer *importer, uint64_t frame_no) {
71 crtc_ = crtc; // Can be NULL if we haven't modeset yet
75 int ret = sw_sync_timeline_create();
77 ALOGE("Failed to create sw sync timeline %d", ret);
84 DrmCompositionType DrmDisplayComposition::type() const {
88 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
89 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
92 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
93 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
94 if ((*iter)->GetCrtcSupported(*crtc)) {
95 DrmPlane *plane = *iter;
103 static DrmPlane *TakePlane(DrmCrtc *crtc,
104 std::vector<DrmPlane *> *primary_planes,
105 std::vector<DrmPlane *> *overlay_planes) {
106 DrmPlane *plane = TakePlane(crtc, primary_planes);
109 return TakePlane(crtc, overlay_planes);
112 int DrmDisplayComposition::CreateNextTimelineFence() {
114 return sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
117 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
118 int timeline_increase = point - timeline_current_;
119 if (timeline_increase <= 0)
122 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
124 ALOGE("Failed to increment sync timeline %d", ret);
126 timeline_current_ = point;
131 int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
132 std::vector<DrmPlane *> *primary_planes,
133 std::vector<DrmPlane *> *overlay_planes) {
135 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
138 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
139 DrmHwcLayer *layer = &layers[layer_index];
141 layers_.emplace_back(crtc_, std::move(*layer));
142 DrmCompositionLayer *c_layer = &layers_.back();
144 if (pre_composition_layer_index_ == -1) {
145 c_layer->plane = TakePlane(crtc_, primary_planes, overlay_planes);
146 if (c_layer->plane == NULL) {
147 if (layers_.size() <= 1) {
148 ALOGE("Failed to match any planes to the crtc of this display");
153 layers_.emplace_back();
154 // c_layer's address might have changed when we resized the vector
155 c_layer = &layers_[layers_.size() - 2];
156 DrmCompositionLayer &pre_comp_layer = layers_.back();
157 pre_comp_layer.crtc = crtc_;
159 pre_composition_layer_index_ = layers_.size() - 1;
161 // This is all to fix up the previous layer, which has now become part
162 // of the set of pre-composition layers because we are stealing its
164 DrmCompositionLayer &last_c_layer = layers_[layers_.size() - 3];
165 std::swap(pre_comp_layer.plane, last_c_layer.plane);
166 OutputFd &last_release_fence = layers[layer_index - 1].release_fence;
167 last_release_fence.Set(CreateNextTimelineFence());
168 ret = last_release_fence.get();
170 ALOGE("Could not create release fence %d", ret);
176 if (c_layer->plane == NULL) {
177 // Layers to be pre composited all get the earliest release fences as they
178 // will get released soonest.
179 layer->release_fence.Set(CreateNextTimelineFence());
180 ret = layer->release_fence.get();
182 ALOGE("Could not create release fence %d", ret);
188 timeline_pre_comp_done_ = timeline_;
190 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
191 DrmHwcLayer *layer = &layers[layer_index];
192 if (layer->release_fence.get() >= 0)
195 ret = layer->release_fence.Set(CreateNextTimelineFence());
197 ALOGE("Could not create release fence %d", ret);
202 type_ = DRM_COMPOSITION_TYPE_FRAME;
207 for (size_t c_layer_index = 0; c_layer_index < layers_.size();
209 DrmCompositionLayer &c_layer = layers_[c_layer_index];
210 if (c_layer.plane != NULL) {
211 std::vector<DrmPlane *> *return_to =
212 (c_layer.plane->type() == DRM_PLANE_TYPE_PRIMARY) ? primary_planes
214 return_to->insert(return_to->begin() + c_layer_index, c_layer.plane);
220 sw_sync_timeline_inc(timeline_fd_, timeline_ - timeline_current_);
222 timeline_ = timeline_current_;
226 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
227 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
229 dpms_mode_ = dpms_mode;
230 type_ = DRM_COMPOSITION_TYPE_DPMS;
234 int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
235 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
237 display_mode_ = display_mode;
238 dpms_mode_ = DRM_MODE_DPMS_ON;
239 type_ = DRM_COMPOSITION_TYPE_MODESET;
243 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
244 layers_.emplace_back();
245 DrmCompositionLayer &c_layer = layers_.back();
247 c_layer.plane = plane;
251 void DrmDisplayComposition::RemoveNoPlaneLayers() {
253 std::remove_if(layers_.begin(), layers_.end(),
254 [](DrmCompositionLayer &l) { return l.plane == NULL; }),
258 int DrmDisplayComposition::SignalPreCompositionDone() {
259 return IncreaseTimelineToPoint(timeline_pre_comp_done_);
262 int DrmDisplayComposition::FinishComposition() {
263 return IncreaseTimelineToPoint(timeline_);
266 std::vector<DrmCompositionLayer>
267 *DrmDisplayComposition::GetCompositionLayers() {
271 int DrmDisplayComposition::pre_composition_layer_index() const {
272 return pre_composition_layer_index_;
275 uint32_t DrmDisplayComposition::dpms_mode() const {
279 uint64_t DrmDisplayComposition::frame_no() const {
283 const DrmMode &DrmDisplayComposition::display_mode() const {
284 return display_mode_;
287 Importer *DrmDisplayComposition::importer() const {