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 | #if PLATFORM_SDK_VERSION >= 30 |
| 18 | |
| 19 | #define LOG_TAG "hwc-bufferinfo-mappermetadata" |
| 20 | |
| 21 | #include "BufferInfoMapperMetadata.h" |
| 22 | |
| 23 | #include <drm/drm_fourcc.h> |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 24 | #include <ui/GraphicBufferMapper.h> |
| 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
| 27 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 28 | #include <cinttypes> |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 29 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 30 | #include "utils/log.h" |
| 31 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 32 | namespace android { |
| 33 | |
| 34 | BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() { |
| 35 | if (GraphicBufferMapper::getInstance().getMapperVersion() < |
| 36 | GraphicBufferMapper::GRALLOC_4) |
| 37 | return nullptr; |
| 38 | |
| 39 | return new BufferInfoMapperMetadata(); |
| 40 | } |
| 41 | |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 42 | /* The implementation below makes assumptions on the order and number of file |
| 43 | * descriptors that Gralloc places in the native_handle_t and as such it very |
| 44 | * likely needs to be adapted to match the particular Gralloc implementation |
| 45 | * used in the system. For this reason it is been declared as a weak symbol, |
| 46 | * so that it can be overridden. |
| 47 | */ |
| 48 | int __attribute__((weak)) |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame^] | 49 | BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, BufferInfo *bo) { |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 50 | int fd_index = 0; |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 51 | |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 52 | if (handle->numFds <= 0) { |
| 53 | ALOGE("Handle has no fds"); |
| 54 | return android::BAD_VALUE; |
| 55 | } |
| 56 | |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame^] | 57 | for (int i = 0; i < kBufferMaxPlanes; i++) { |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 58 | /* If no size, we're out of usable planes */ |
| 59 | if (bo->sizes[i] <= 0) { |
| 60 | if (i == 0) { |
| 61 | ALOGE("Bad handle metadata"); |
| 62 | return android::BAD_VALUE; |
| 63 | } |
| 64 | break; |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 67 | /* |
| 68 | * If the offset is zero, its multi-buffer |
| 69 | * so move to the next fd |
| 70 | */ |
| 71 | if (i != 0 && bo->offsets[i] == 0) { |
| 72 | fd_index++; |
| 73 | if (fd_index >= handle->numFds) { |
| 74 | ALOGE("Handle has no more fds"); |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 75 | return android::BAD_VALUE; |
| 76 | } |
| 77 | } |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 78 | |
| 79 | bo->prime_fds[i] = handle->data[fd_index]; |
| 80 | if (bo->prime_fds[i] <= 0) { |
| 81 | ALOGE("Invalid prime fd"); |
| 82 | return android::BAD_VALUE; |
| 83 | } |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 84 | } |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 85 | |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle, |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame^] | 90 | BufferInfo *bo) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 91 | GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance(); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 92 | if (handle == nullptr) |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 93 | return -EINVAL; |
| 94 | |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame^] | 95 | int err = mapper.getPixelFormatFourCC(handle, &bo->format); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 96 | if (err != 0) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 97 | ALOGE("Failed to get FourCC format err=%d", err); |
| 98 | return err; |
| 99 | } |
| 100 | |
| 101 | err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 102 | if (err != 0) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 103 | ALOGE("Failed to get DRM Modifier err=%d", err); |
| 104 | return err; |
| 105 | } |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 106 | |
| 107 | uint64_t width = 0; |
| 108 | err = mapper.getWidth(handle, &width); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 109 | if (err != 0) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 110 | ALOGE("Failed to get Width err=%d", err); |
| 111 | return err; |
| 112 | } |
| 113 | bo->width = static_cast<uint32_t>(width); |
| 114 | |
| 115 | uint64_t height = 0; |
| 116 | err = mapper.getHeight(handle, &height); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 117 | if (err != 0) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 118 | ALOGE("Failed to get Height err=%d", err); |
| 119 | return err; |
| 120 | } |
| 121 | bo->height = static_cast<uint32_t>(height); |
| 122 | |
| 123 | std::vector<ui::PlaneLayout> layouts; |
| 124 | err = mapper.getPlaneLayouts(handle, &layouts); |
Roman Stratiienko | fc014f5 | 2021-12-23 19:04:29 +0200 | [diff] [blame] | 125 | if (err != 0) { |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 126 | ALOGE("Failed to get Plane Layouts err=%d", err); |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | for (uint32_t i = 0; i < layouts.size(); i++) { |
| 131 | bo->modifiers[i] = bo->modifiers[0]; |
| 132 | bo->pitches[i] = layouts[i].strideInBytes; |
| 133 | bo->offsets[i] = layouts[i].offsetInBytes; |
Roman Stratiienko | 875f397 | 2021-10-22 12:34:36 +0300 | [diff] [blame] | 134 | bo->sizes[i] = layouts[i].totalSizeInBytes; |
Dennis Tsiang | 3a5fb50 | 2020-10-30 08:43:33 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return GetFds(handle, bo); |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // namespace android |
| 141 | |
| 142 | #endif |