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 static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
34 native_handle_t *new_handle =
35 native_handle_create(handle->numFds, handle->numInts);
36 if (new_handle == NULL)
39 const int *old_data = handle->data;
40 int *new_data = new_handle->data;
41 for (int i = 0; i < handle->numFds; i++) {
42 *new_data = dup(*old_data);
46 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
51 static void free_buffer_handle(native_handle_t *handle) {
52 int ret = native_handle_close(handle);
54 ALOGE("Failed to close native handle %d", ret);
55 ret = native_handle_delete(handle);
57 ALOGE("Failed to delete native handle %d", ret);
60 DrmCompositionLayer::DrmCompositionLayer()
61 : crtc(NULL), plane(NULL), handle(NULL) {
62 memset(&layer, 0, sizeof(layer));
63 layer.releaseFenceFd = -1;
64 layer.acquireFenceFd = -1;
65 memset(&bo, 0, sizeof(bo));
68 DrmDisplayComposition::DrmDisplayComposition()
71 type_(DRM_COMPOSITION_TYPE_EMPTY),
75 timeline_pre_comp_done_(0),
76 pre_composition_layer_index_(-1),
77 dpms_mode_(DRM_MODE_DPMS_ON) {
80 DrmDisplayComposition::~DrmDisplayComposition() {
81 for (DrmCompositionLayerVector_t::iterator iter = layers_.begin();
82 iter != layers_.end(); ++iter) {
83 if (importer_ && iter->bo.fb_id)
84 importer_->ReleaseBuffer(&iter->bo);
87 gralloc_->unregisterBuffer(gralloc_, iter->handle);
88 free_buffer_handle(iter->handle);
91 if (iter->layer.acquireFenceFd >= 0)
92 close(iter->layer.acquireFenceFd);
95 if (timeline_fd_ >= 0) {
102 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
103 Importer *importer) {
105 crtc_ = crtc; // Can be NULL if we haven't modeset yet
106 importer_ = importer;
108 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
109 (const hw_module_t **)&gralloc_);
111 ALOGE("Failed to open gralloc module %d", ret);
115 ret = sw_sync_timeline_create();
117 ALOGE("Failed to create sw sync timeline %d", ret);
124 DrmCompositionType DrmDisplayComposition::type() const {
128 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
129 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
132 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
133 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
134 if ((*iter)->GetCrtcSupported(*crtc)) {
135 DrmPlane *plane = *iter;
143 static DrmPlane *TakePlane(DrmCrtc *crtc,
144 std::vector<DrmPlane *> *primary_planes,
145 std::vector<DrmPlane *> *overlay_planes) {
146 DrmPlane *plane = TakePlane(crtc, primary_planes);
149 return TakePlane(crtc, overlay_planes);
152 int DrmDisplayComposition::CreateNextTimelineFence() {
154 return sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
157 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
158 int timeline_increase = point - timeline_current_;
159 if (timeline_increase <= 0)
162 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
164 ALOGE("Failed to increment sync timeline %d", ret);
166 timeline_current_ = point;
171 int DrmDisplayComposition::SetLayers(hwc_layer_1_t *layers, size_t num_layers,
172 size_t *layer_indices,
173 std::vector<DrmPlane *> *primary_planes,
174 std::vector<DrmPlane *> *overlay_planes) {
176 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
179 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
180 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
182 native_handle_t *handle_copy = dup_buffer_handle(layer->handle);
183 if (handle_copy == NULL) {
184 ALOGE("Failed to duplicate handle");
188 int ret = gralloc_->registerBuffer(gralloc_, handle_copy);
190 ALOGE("Failed to register buffer handle %d", ret);
191 free_buffer_handle(handle_copy);
195 layers_.emplace_back();
196 DrmCompositionLayer_t *c_layer = &layers_.back();
197 c_layer->layer = *layer;
198 c_layer->handle = handle_copy;
199 c_layer->crtc = crtc_;
201 ret = importer_->ImportBuffer(layer->handle, &c_layer->bo);
203 ALOGE("Failed to import handle of layer %d", ret);
207 if (pre_composition_layer_index_ == -1) {
208 c_layer->plane = TakePlane(crtc_, primary_planes, overlay_planes);
209 if (c_layer->plane == NULL) {
210 if (layers_.size() <= 1) {
211 ALOGE("Failed to match any planes to the crtc of this display");
216 layers_.emplace_back();
217 // c_layer's address might have changed when we resized the vector
218 c_layer = &layers_[layers_.size() - 2];
219 DrmCompositionLayer_t &pre_comp_layer = layers_.back();
220 pre_comp_layer.crtc = crtc_;
221 hwc_layer_1_t &pre_comp_output_layer = pre_comp_layer.layer;
222 memset(&pre_comp_output_layer, 0, sizeof(pre_comp_output_layer));
223 pre_comp_output_layer.compositionType = HWC_OVERLAY;
224 pre_comp_output_layer.acquireFenceFd = -1;
225 pre_comp_output_layer.releaseFenceFd = -1;
226 pre_comp_output_layer.planeAlpha = 0xff;
227 pre_comp_output_layer.visibleRegionScreen.numRects = 1;
228 pre_comp_output_layer.visibleRegionScreen.rects =
229 &pre_comp_output_layer.displayFrame;
231 pre_composition_layer_index_ = layers_.size() - 1;
233 // This is all to fix up the previous layer, which has now become part
234 // of the set of pre-composition layers because we are stealing its
236 DrmCompositionLayer_t &last_c_layer = layers_[layers_.size() - 3];
237 std::swap(pre_comp_layer.plane, last_c_layer.plane);
238 hwc_layer_1_t *last_layer = &layers[layer_indices[layer_index - 1]];
239 ret = last_layer->releaseFenceFd = CreateNextTimelineFence();
241 ALOGE("Could not create release fence %d", ret);
247 if (c_layer->plane == NULL) {
248 // Layers to be pre composited all get the earliest release fences as they
249 // will get released soonest.
250 ret = layer->releaseFenceFd = CreateNextTimelineFence();
252 ALOGE("Could not create release fence %d", ret);
258 timeline_pre_comp_done_ = timeline_;
260 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
261 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
262 if (layer->releaseFenceFd >= 0)
265 ret = layer->releaseFenceFd = CreateNextTimelineFence();
267 ALOGE("Could not create release fence %d", ret);
272 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
273 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
274 layer->acquireFenceFd = -1; // We own this now
277 type_ = DRM_COMPOSITION_TYPE_FRAME;
282 for (size_t c_layer_index = 0; c_layer_index < layers_.size();
284 DrmCompositionLayer_t &c_layer = layers_[c_layer_index];
285 if (c_layer.handle) {
286 gralloc_->unregisterBuffer(gralloc_, c_layer.handle);
287 free_buffer_handle(c_layer.handle);
289 if (c_layer.bo.fb_id)
290 importer_->ReleaseBuffer(&c_layer.bo);
291 if (c_layer.plane != NULL) {
292 std::vector<DrmPlane *> *return_to =
293 (c_layer.plane->type() == DRM_PLANE_TYPE_PRIMARY) ? primary_planes
295 return_to->insert(return_to->begin() + c_layer_index, c_layer.plane);
300 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
301 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
302 if (layer->releaseFenceFd >= 0) {
303 close(layer->releaseFenceFd);
304 layer->releaseFenceFd = -1;
307 sw_sync_timeline_inc(timeline_fd_, timeline_ - timeline_current_);
309 timeline_ = timeline_current_;
313 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
314 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
316 dpms_mode_ = dpms_mode;
317 type_ = DRM_COMPOSITION_TYPE_DPMS;
321 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
322 layers_.emplace_back();
323 DrmCompositionLayer_t &c_layer = layers_.back();
325 c_layer.plane = plane;
329 void DrmDisplayComposition::RemoveNoPlaneLayers() {
330 for (auto &comp_layer : layers_) {
331 if (comp_layer.plane != NULL)
334 if (importer_ && comp_layer.bo.fb_id) {
335 importer_->ReleaseBuffer(&comp_layer.bo);
338 if (comp_layer.handle) {
339 gralloc_->unregisterBuffer(gralloc_, comp_layer.handle);
340 free_buffer_handle(comp_layer.handle);
343 if (comp_layer.layer.acquireFenceFd >= 0) {
344 close(comp_layer.layer.acquireFenceFd);
345 comp_layer.layer.acquireFenceFd = -1;
350 std::remove_if(layers_.begin(), layers_.end(),
351 [](DrmCompositionLayer_t &l) { return l.plane == NULL; }),
355 int DrmDisplayComposition::SignalPreCompositionDone() {
356 return IncreaseTimelineToPoint(timeline_pre_comp_done_);
359 int DrmDisplayComposition::FinishComposition() {
360 return IncreaseTimelineToPoint(timeline_);
363 DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
367 int DrmDisplayComposition::pre_composition_layer_index() const {
368 return pre_composition_layer_index_;
371 uint32_t DrmDisplayComposition::dpms_mode() const {
375 Importer *DrmDisplayComposition::importer() const {