blob: 0cbe6013ec02da01a34366fd48efd73eab5a7a31 [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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030021#include <gralloc_handle.h>
22#include <hardware/gralloc.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030023#include <xf86drm.h>
24#include <xf86drmMode.h>
25
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <cinttypes>
27
Roman Stratiienkod21071f2021-03-09 21:56:50 +020028#include "utils/log.h"
29#include "utils/properties.h"
30
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030031namespace android {
32
33DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000034 uint64_t cap_value = 0;
35 if (drmGetCap(drm_->fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
36 ALOGE("drmGetCap failed. Fallback to no modifier support.");
37 cap_value = 0;
38 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020039 has_modifier_support_ = cap_value != 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030040}
41
42int DrmGenericImporter::ImportBuffer(hwc_drm_bo_t *bo) {
Roman Stratiienko67c05422021-01-08 18:30:31 +020043 int ret = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030044
Roman Stratiienko67c05422021-01-08 18:30:31 +020045 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
46 if (bo->prime_fds[i] > 0) {
47 if (i == 0 || bo->prime_fds[i] != bo->prime_fds[0]) {
48 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[i],
49 &bo->gem_handles[i]);
50 if (ret) {
51 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[i], ret);
52 return ret;
53 }
54 } else {
55 bo->gem_handles[i] = bo->gem_handles[0];
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030056 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030057 }
58 }
59
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020060 bool has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
61 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
62
63 if (!has_modifier_support_ && has_modifiers) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000064 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
65 bo->modifiers[0]);
66 return -EINVAL;
67 }
68
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020069 if (!has_modifiers)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030070 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
71 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
72 0);
73 else
74 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
75 bo->format, bo->gem_handles, bo->pitches,
76 bo->offsets, bo->modifiers, &bo->fb_id,
Roman Stratiienkoc8dd0662020-11-20 16:21:55 +020077 DRM_MODE_FB_MODIFIERS);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030078
79 if (ret) {
80 ALOGE("could not create drm fb %d", ret);
81 return ret;
82 }
83
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020084 for (unsigned int gem_handle : bo->gem_handles) {
85 if (!gem_handle)
Roman Stratiienko67c05422021-01-08 18:30:31 +020086 continue;
87
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020088 ImportHandle(gem_handle);
Roman Stratiienko67c05422021-01-08 18:30:31 +020089 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030090
91 return ret;
92}
93
94int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
95 if (bo->fb_id)
96 if (drmModeRmFB(drm_->fd(), bo->fb_id))
97 ALOGE("Failed to rm fb");
98
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020099 for (unsigned int &gem_handle : bo->gem_handles) {
100 if (!gem_handle)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300101 continue;
102
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200103 if (ReleaseHandle(gem_handle))
104 ALOGE("Failed to release gem handle %d", gem_handle);
Roman Stratiienko67c05422021-01-08 18:30:31 +0200105 else
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200106 gem_handle = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300107 }
108 return 0;
109}
110
111int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
112 gem_refcount_[gem_handle]++;
113
114 return 0;
115}
116
117int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
118 if (--gem_refcount_[gem_handle])
119 return 0;
120
121 gem_refcount_.erase(gem_handle);
122
123 return CloseHandle(gem_handle);
124}
125
126int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200127 struct drm_gem_close gem_close {};
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300128 gem_close.handle = gem_handle;
129 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
130 if (ret)
131 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
132
133 return ret;
134}
135} // namespace android