blob: 6627cc8e5087f1f17b7ef97fb957b31d9a708282 [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>
24#include <log/log.h>
25#include <xf86drm.h>
26#include <xf86drmMode.h>
27
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
29
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030namespace android {
31
32DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000033 uint64_t cap_value = 0;
34 if (drmGetCap(drm_->fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
35 ALOGE("drmGetCap failed. Fallback to no modifier support.");
36 cap_value = 0;
37 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020038 has_modifier_support_ = cap_value != 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030039}
40
41int DrmGenericImporter::ImportBuffer(hwc_drm_bo_t *bo) {
Roman Stratiienko67c05422021-01-08 18:30:31 +020042 int ret = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030043
Roman Stratiienko67c05422021-01-08 18:30:31 +020044 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
45 if (bo->prime_fds[i] > 0) {
46 if (i == 0 || bo->prime_fds[i] != bo->prime_fds[0]) {
47 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[i],
48 &bo->gem_handles[i]);
49 if (ret) {
50 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[i], ret);
51 return ret;
52 }
53 } else {
54 bo->gem_handles[i] = bo->gem_handles[0];
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030055 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030056 }
57 }
58
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020059 bool has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
60 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
61
62 if (!has_modifier_support_ && has_modifiers) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000063 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
64 bo->modifiers[0]);
65 return -EINVAL;
66 }
67
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020068 if (!has_modifiers)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030069 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
70 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
71 0);
72 else
73 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
74 bo->format, bo->gem_handles, bo->pitches,
75 bo->offsets, bo->modifiers, &bo->fb_id,
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020076 DRM_MODE_FB_MODIFIERS);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030077
78 if (ret) {
79 ALOGE("could not create drm fb %d", ret);
80 return ret;
81 }
82
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020083 for (unsigned int gem_handle : bo->gem_handles) {
84 if (!gem_handle)
Roman Stratiienko67c05422021-01-08 18:30:31 +020085 continue;
86
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020087 ImportHandle(gem_handle);
Roman Stratiienko67c05422021-01-08 18:30:31 +020088 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030089
90 return ret;
91}
92
93int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
94 if (bo->fb_id)
95 if (drmModeRmFB(drm_->fd(), bo->fb_id))
96 ALOGE("Failed to rm fb");
97
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020098 for (unsigned int &gem_handle : bo->gem_handles) {
99 if (!gem_handle)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300100 continue;
101
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200102 if (ReleaseHandle(gem_handle))
103 ALOGE("Failed to release gem handle %d", gem_handle);
Roman Stratiienko67c05422021-01-08 18:30:31 +0200104 else
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200105 gem_handle = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300106 }
107 return 0;
108}
109
110int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
111 gem_refcount_[gem_handle]++;
112
113 return 0;
114}
115
116int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
117 if (--gem_refcount_[gem_handle])
118 return 0;
119
120 gem_refcount_.erase(gem_handle);
121
122 return CloseHandle(gem_handle);
123}
124
125int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
126 struct drm_gem_close gem_close;
127
128 memset(&gem_close, 0, sizeof(gem_close));
129
130 gem_close.handle = gem_handle;
131 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
132 if (ret)
133 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
134
135 return ret;
136}
137} // namespace android