Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 1 | /* |
| 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 Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 24 | #include <inttypes.h> |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 25 | #include <log/log.h> |
| 26 | #include <xf86drm.h> |
| 27 | #include <xf86drmMode.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 32 | 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 Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DrmGenericImporter::~DrmGenericImporter() { |
| 41 | } |
| 42 | |
| 43 | int 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 Stratiienko | c8dd066 | 2020-11-20 16:21:55 +0200 | [diff] [blame^] | 62 | 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 Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 66 | ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64, |
| 67 | bo->modifiers[0]); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | |
Roman Stratiienko | c8dd066 | 2020-11-20 16:21:55 +0200 | [diff] [blame^] | 71 | if (!has_modifiers) |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 72 | 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 Stratiienko | c8dd066 | 2020-11-20 16:21:55 +0200 | [diff] [blame^] | 79 | DRM_MODE_FB_MODIFIERS); |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 80 | |
| 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 | |
| 91 | int 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 | |
| 112 | int DrmGenericImporter::ImportHandle(uint32_t gem_handle) { |
| 113 | gem_refcount_[gem_handle]++; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int 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 | |
| 127 | int 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 |