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),
81 DrmDisplayComposition::~DrmDisplayComposition() {
82 for (DrmCompositionLayerVector_t::iterator iter = layers_.begin();
83 iter != layers_.end(); ++iter) {
84 if (importer_ && iter->bo.fb_id)
85 importer_->ReleaseBuffer(&iter->bo);
88 gralloc_->unregisterBuffer(gralloc_, iter->handle);
89 free_buffer_handle(iter->handle);
92 if (iter->layer.acquireFenceFd >= 0)
93 close(iter->layer.acquireFenceFd);
96 if (timeline_fd_ >= 0) {
103 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
104 Importer *importer, uint64_t frame_no) {
106 crtc_ = crtc; // Can be NULL if we haven't modeset yet
107 importer_ = importer;
108 frame_no_ = frame_no;
110 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
111 (const hw_module_t **)&gralloc_);
113 ALOGE("Failed to open gralloc module %d", ret);
117 ret = sw_sync_timeline_create();
119 ALOGE("Failed to create sw sync timeline %d", ret);
126 DrmCompositionType DrmDisplayComposition::type() const {
130 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
131 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
134 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
135 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
136 if ((*iter)->GetCrtcSupported(*crtc)) {
137 DrmPlane *plane = *iter;
145 static DrmPlane *TakePlane(DrmCrtc *crtc,
146 std::vector<DrmPlane *> *primary_planes,
147 std::vector<DrmPlane *> *overlay_planes) {
148 DrmPlane *plane = TakePlane(crtc, primary_planes);
151 return TakePlane(crtc, overlay_planes);
154 int DrmDisplayComposition::CreateNextTimelineFence() {
156 return sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
159 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
160 int timeline_increase = point - timeline_current_;
161 if (timeline_increase <= 0)
164 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
166 ALOGE("Failed to increment sync timeline %d", ret);
168 timeline_current_ = point;
173 int DrmDisplayComposition::SetLayers(hwc_layer_1_t *layers, size_t num_layers,
174 size_t *layer_indices,
175 std::vector<DrmPlane *> *primary_planes,
176 std::vector<DrmPlane *> *overlay_planes) {
178 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
181 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
182 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
184 native_handle_t *handle_copy = dup_buffer_handle(layer->handle);
185 if (handle_copy == NULL) {
186 ALOGE("Failed to duplicate handle");
190 int ret = gralloc_->registerBuffer(gralloc_, handle_copy);
192 ALOGE("Failed to register buffer handle %d", ret);
193 free_buffer_handle(handle_copy);
197 layers_.emplace_back();
198 DrmCompositionLayer_t *c_layer = &layers_.back();
199 c_layer->layer = *layer;
200 c_layer->handle = handle_copy;
201 c_layer->crtc = crtc_;
203 ret = importer_->ImportBuffer(layer->handle, &c_layer->bo);
205 ALOGE("Failed to import handle of layer %d", ret);
209 if (pre_composition_layer_index_ == -1) {
210 c_layer->plane = TakePlane(crtc_, primary_planes, overlay_planes);
211 if (c_layer->plane == NULL) {
212 if (layers_.size() <= 1) {
213 ALOGE("Failed to match any planes to the crtc of this display");
218 layers_.emplace_back();
219 // c_layer's address might have changed when we resized the vector
220 c_layer = &layers_[layers_.size() - 2];
221 DrmCompositionLayer_t &pre_comp_layer = layers_.back();
222 pre_comp_layer.crtc = crtc_;
223 hwc_layer_1_t &pre_comp_output_layer = pre_comp_layer.layer;
224 memset(&pre_comp_output_layer, 0, sizeof(pre_comp_output_layer));
225 pre_comp_output_layer.compositionType = HWC_OVERLAY;
226 pre_comp_output_layer.acquireFenceFd = -1;
227 pre_comp_output_layer.releaseFenceFd = -1;
228 pre_comp_output_layer.planeAlpha = 0xff;
229 pre_comp_output_layer.visibleRegionScreen.numRects = 1;
230 pre_comp_output_layer.visibleRegionScreen.rects =
231 &pre_comp_output_layer.displayFrame;
233 pre_composition_layer_index_ = layers_.size() - 1;
235 // This is all to fix up the previous layer, which has now become part
236 // of the set of pre-composition layers because we are stealing its
238 DrmCompositionLayer_t &last_c_layer = layers_[layers_.size() - 3];
239 std::swap(pre_comp_layer.plane, last_c_layer.plane);
240 hwc_layer_1_t *last_layer = &layers[layer_indices[layer_index - 1]];
241 ret = last_layer->releaseFenceFd = CreateNextTimelineFence();
243 ALOGE("Could not create release fence %d", ret);
249 if (c_layer->plane == NULL) {
250 // Layers to be pre composited all get the earliest release fences as they
251 // will get released soonest.
252 ret = layer->releaseFenceFd = CreateNextTimelineFence();
254 ALOGE("Could not create release fence %d", ret);
260 timeline_pre_comp_done_ = timeline_;
262 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
263 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
264 if (layer->releaseFenceFd >= 0)
267 ret = layer->releaseFenceFd = CreateNextTimelineFence();
269 ALOGE("Could not create release fence %d", ret);
274 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
275 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
276 layer->acquireFenceFd = -1; // We own this now
279 type_ = DRM_COMPOSITION_TYPE_FRAME;
284 for (size_t c_layer_index = 0; c_layer_index < layers_.size();
286 DrmCompositionLayer_t &c_layer = layers_[c_layer_index];
287 if (c_layer.handle) {
288 gralloc_->unregisterBuffer(gralloc_, c_layer.handle);
289 free_buffer_handle(c_layer.handle);
291 if (c_layer.bo.fb_id)
292 importer_->ReleaseBuffer(&c_layer.bo);
293 if (c_layer.plane != NULL) {
294 std::vector<DrmPlane *> *return_to =
295 (c_layer.plane->type() == DRM_PLANE_TYPE_PRIMARY) ? primary_planes
297 return_to->insert(return_to->begin() + c_layer_index, c_layer.plane);
302 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
303 hwc_layer_1_t *layer = &layers[layer_indices[layer_index]];
304 if (layer->releaseFenceFd >= 0) {
305 close(layer->releaseFenceFd);
306 layer->releaseFenceFd = -1;
309 sw_sync_timeline_inc(timeline_fd_, timeline_ - timeline_current_);
311 timeline_ = timeline_current_;
315 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
316 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
318 dpms_mode_ = dpms_mode;
319 type_ = DRM_COMPOSITION_TYPE_DPMS;
323 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
324 layers_.emplace_back();
325 DrmCompositionLayer_t &c_layer = layers_.back();
327 c_layer.plane = plane;
331 void DrmDisplayComposition::RemoveNoPlaneLayers() {
332 for (auto &comp_layer : layers_) {
333 if (comp_layer.plane != NULL)
336 if (importer_ && comp_layer.bo.fb_id) {
337 importer_->ReleaseBuffer(&comp_layer.bo);
340 if (comp_layer.handle) {
341 gralloc_->unregisterBuffer(gralloc_, comp_layer.handle);
342 free_buffer_handle(comp_layer.handle);
345 if (comp_layer.layer.acquireFenceFd >= 0) {
346 close(comp_layer.layer.acquireFenceFd);
347 comp_layer.layer.acquireFenceFd = -1;
352 std::remove_if(layers_.begin(), layers_.end(),
353 [](DrmCompositionLayer_t &l) { return l.plane == NULL; }),
357 int DrmDisplayComposition::SignalPreCompositionDone() {
358 return IncreaseTimelineToPoint(timeline_pre_comp_done_);
361 int DrmDisplayComposition::FinishComposition() {
362 return IncreaseTimelineToPoint(timeline_);
365 DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
369 int DrmDisplayComposition::pre_composition_layer_index() const {
370 return pre_composition_layer_index_;
373 uint32_t DrmDisplayComposition::dpms_mode() const {
377 uint64_t DrmDisplayComposition::frame_no() const {
381 Importer *DrmDisplayComposition::importer() const {