blob: 7fbbdc690689ef192f3b9434d515c125397a422a [file] [log] [blame]
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +03001/*
2 * Copyright (C) 2020 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-platform-drm-generic"
18
19#include "DrmGenericImporter.h"
20
21#include <cutils/properties.h>
22#include <gralloc_handle.h>
23#include <hardware/gralloc.h>
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000024#include <inttypes.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030025#include <log/log.h>
26#include <xf86drm.h>
27#include <xf86drmMode.h>
28
29namespace android {
30
31DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000032 uint64_t cap_value = 0;
33 if (drmGetCap(drm_->fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
34 ALOGE("drmGetCap failed. Fallback to no modifier support.");
35 cap_value = 0;
36 }
37 has_modifier_support_ = cap_value;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030038}
39
40DrmGenericImporter::~DrmGenericImporter() {
41}
42
43int DrmGenericImporter::ImportBuffer(hwc_drm_bo_t *bo) {
44 int ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0],
45 &bo->gem_handles[0]);
46 if (ret) {
47 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
48 return ret;
49 }
50
51 for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
52 int fd = bo->prime_fds[i];
53 if (fd != 0) {
54 if (fd != bo->prime_fds[0]) {
55 ALOGE("Multiplanar FBs are not supported by this version of composer");
56 return -ENOTSUP;
57 }
58 bo->gem_handles[i] = bo->gem_handles[0];
59 }
60 }
61
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020062 bool has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
63 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
64
65 if (!has_modifier_support_ && has_modifiers) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000066 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
67 bo->modifiers[0]);
68 return -EINVAL;
69 }
70
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020071 if (!has_modifiers)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030072 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
73 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
74 0);
75 else
76 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
77 bo->format, bo->gem_handles, bo->pitches,
78 bo->offsets, bo->modifiers, &bo->fb_id,
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020079 DRM_MODE_FB_MODIFIERS);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030080
81 if (ret) {
82 ALOGE("could not create drm fb %d", ret);
83 return ret;
84 }
85
86 ImportHandle(bo->gem_handles[0]);
87
88 return ret;
89}
90
91int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
92 if (bo->fb_id)
93 if (drmModeRmFB(drm_->fd(), bo->fb_id))
94 ALOGE("Failed to rm fb");
95
96 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
97 if (!bo->gem_handles[i])
98 continue;
99
100 if (ReleaseHandle(bo->gem_handles[i])) {
101 ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
102 } else {
103 for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
104 if (bo->gem_handles[j] == bo->gem_handles[i])
105 bo->gem_handles[j] = 0;
106 bo->gem_handles[i] = 0;
107 }
108 }
109 return 0;
110}
111
112int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
113 gem_refcount_[gem_handle]++;
114
115 return 0;
116}
117
118int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
119 if (--gem_refcount_[gem_handle])
120 return 0;
121
122 gem_refcount_.erase(gem_handle);
123
124 return CloseHandle(gem_handle);
125}
126
127int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
128 struct drm_gem_close gem_close;
129
130 memset(&gem_close, 0, sizeof(gem_close));
131
132 gem_close.handle = gem_handle;
133 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
134 if (ret)
135 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
136
137 return ret;
138}
139} // namespace android