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.acquireFenceFd = -1;
64 memset(&bo, 0, sizeof(bo));
67 DrmCompositionLayer::~DrmCompositionLayer() {
70 DrmDisplayComposition::DrmDisplayComposition()
73 type_(DRM_COMPOSITION_TYPE_EMPTY),
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, Importer *importer) {
104 importer_ = importer;
106 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
107 (const hw_module_t **)&gralloc_);
109 ALOGE("Failed to open gralloc module %d", ret);
113 ret = sw_sync_timeline_create();
115 ALOGE("Failed to create sw sync timeline %d", ret);
122 DrmCompositionType DrmDisplayComposition::type() const {
126 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
127 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
130 int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, hwc_drm_bo_t *bo,
131 DrmCrtc *crtc, DrmPlane *plane) {
132 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
135 native_handle_t *handle_copy = dup_buffer_handle(layer->handle);
136 if (handle_copy == NULL) {
137 ALOGE("Failed to duplicate handle");
141 int ret = gralloc_->registerBuffer(gralloc_, handle_copy);
143 ALOGE("Failed to register buffer handle %d", ret);
144 free_buffer_handle(handle_copy);
149 layer->releaseFenceFd =
150 sw_sync_fence_create(timeline_fd_, "drm_fence", timeline_);
151 if (layer->releaseFenceFd < 0) {
152 free_buffer_handle(handle_copy);
153 ALOGE("Could not create release fence %d", layer->releaseFenceFd);
154 return layer->releaseFenceFd;
157 DrmCompositionLayer_t c_layer;
158 c_layer.layer = *layer;
161 c_layer.plane = plane;
162 c_layer.handle = handle_copy;
164 layer->acquireFenceFd = -1; // We own this now
165 layers_.push_back(c_layer);
166 type_ = DRM_COMPOSITION_TYPE_FRAME;
170 int DrmDisplayComposition::AddLayer(hwc_layer_1_t *layer, DrmCrtc *crtc,
172 if (layer->transform != 0)
175 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
179 int ret = importer_->ImportBuffer(layer->handle, &bo);
181 ALOGE("Failed to import handle of layer %d", ret);
185 ret = AddLayer(layer, &bo, crtc, plane);
187 importer_->ReleaseBuffer(&bo);
192 int DrmDisplayComposition::AddDpmsMode(uint32_t dpms_mode) {
193 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
195 dpms_mode_ = dpms_mode;
196 type_ = DRM_COMPOSITION_TYPE_DPMS;
200 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
201 DrmCompositionLayer_t c_layer;
203 c_layer.plane = plane;
204 layers_.push_back(c_layer);
208 void DrmDisplayComposition::RemoveNoPlaneLayers() {
209 for (auto &comp_layer : layers_) {
210 if (comp_layer.plane != NULL)
213 if (importer_ && comp_layer.bo.fb_id) {
214 importer_->ReleaseBuffer(&comp_layer.bo);
217 if (comp_layer.handle) {
218 gralloc_->unregisterBuffer(gralloc_, comp_layer.handle);
219 free_buffer_handle(comp_layer.handle);
222 if (comp_layer.layer.acquireFenceFd >= 0) {
223 close(comp_layer.layer.acquireFenceFd);
224 comp_layer.layer.acquireFenceFd = -1;
229 std::remove_if(layers_.begin(), layers_.end(),
230 [](DrmCompositionLayer_t &l) { return l.plane == NULL; }),
234 int DrmDisplayComposition::FinishComposition() {
235 int timeline_increase = timeline_ - timeline_current_;
236 if (timeline_increase <= 0)
239 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
241 ALOGE("Failed to increment sync timeline %d", ret);
243 timeline_current_ = timeline_;
248 DrmCompositionLayerVector_t *DrmDisplayComposition::GetCompositionLayers() {
252 uint32_t DrmDisplayComposition::dpms_mode() const {
256 Importer *DrmDisplayComposition::importer() const {