Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 1 | /* |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 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 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 17 | #define LOG_TAG "hwc-bufferinfo-libdrm" |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 18 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 19 | #include "BufferInfoLibdrm.h" |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 20 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 21 | #include <gralloc_handle.h> |
| 22 | #include <hardware/gralloc.h> |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 23 | #include <xf86drm.h> |
| 24 | #include <xf86drmMode.h> |
| 25 | |
Roman Stratiienko | 1e053b4 | 2021-10-25 22:54:20 +0300 | [diff] [blame] | 26 | #include <mutex> |
| 27 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 28 | #include "utils/log.h" |
| 29 | #include "utils/properties.h" |
| 30 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 33 | LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm); |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 34 | |
| 35 | enum chroma_order { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 36 | kYCbCr, |
| 37 | kYCrCb, |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 38 | }; |
| 39 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 40 | struct DroidYuvFormat { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 41 | /* Lookup keys */ |
Roman Stratiienko | d26619b | 2021-08-04 19:55:37 +0300 | [diff] [blame] | 42 | uint32_t native; /* HAL_PIXEL_FORMAT_ */ |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 43 | enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */ |
Roman Stratiienko | d26619b | 2021-08-04 19:55:37 +0300 | [diff] [blame] | 44 | size_t chroma_step; /* Distance in bytes between subsequent chroma pixels. */ |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 45 | |
| 46 | /* Result */ |
| 47 | int fourcc; /* DRM_FORMAT_ */ |
| 48 | }; |
| 49 | |
Roman Stratiienko | 2c63b33 | 2022-02-10 10:45:06 +0200 | [diff] [blame] | 50 | #ifndef DRM_FORMAT_XYUV8888 |
| 51 | #define DRM_FORMAT_XYUV8888 \ |
| 52 | fourcc_code('X', 'Y', 'U', 'V') /* [31:0] X:Y:Cb:Cr 8:8:8:8 little endian */ |
| 53 | #endif |
| 54 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 55 | /* The following table is used to look up a DRI image FourCC based |
| 56 | * on native format and information contained in android_ycbcr struct. */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 57 | static const struct DroidYuvFormat kDroidYuvFormats[] = { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 58 | /* Native format, YCrCb, Chroma step, DRI image FourCC */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 59 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 2, DRM_FORMAT_NV12}, |
| 60 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 1, DRM_FORMAT_YUV420}, |
| 61 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCrCb, 1, DRM_FORMAT_YVU420}, |
| 62 | {HAL_PIXEL_FORMAT_YV12, kYCrCb, 1, DRM_FORMAT_YVU420}, |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 63 | /* HACK: See droid_create_image_from_prime_fds() and |
| 64 | * https://issuetracker.google.com/32077885. */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 65 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 2, DRM_FORMAT_NV12}, |
| 66 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 1, DRM_FORMAT_YUV420}, |
| 67 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_YVU420}, |
| 68 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_AYUV}, |
| 69 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_XYUV8888}, |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 73 | |
Roman Stratiienko | deb7735 | 2021-12-06 12:59:26 +0200 | [diff] [blame] | 74 | static uint32_t get_fourcc_yuv(uint32_t native, enum chroma_order chroma_order, |
| 75 | size_t chroma_step) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 76 | for (auto droid_yuv_format : kDroidYuvFormats) |
| 77 | if (droid_yuv_format.native == native && |
| 78 | droid_yuv_format.chroma_order == chroma_order && |
| 79 | droid_yuv_format.chroma_step == chroma_step) |
| 80 | return droid_yuv_format.fourcc; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 81 | |
Roman Stratiienko | deb7735 | 2021-12-06 12:59:26 +0200 | [diff] [blame] | 82 | return UINT32_MAX; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 83 | } |
| 84 | |
Roman Stratiienko | d26619b | 2021-08-04 19:55:37 +0300 | [diff] [blame] | 85 | static bool is_yuv(uint32_t native) { |
Roman Stratiienko | 568e598 | 2022-01-04 11:42:33 +0200 | [diff] [blame] | 86 | // NOLINTNEXTLINE(readability-use-anyofallof) |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 87 | for (auto droid_yuv_format : kDroidYuvFormats) |
| 88 | if (droid_yuv_format.native == native) |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 89 | return true; |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame] | 94 | bool BufferInfoLibdrm::GetYuvPlaneInfo(uint32_t hal_format, int num_fds, |
| 95 | buffer_handle_t handle, BufferInfo *bo) { |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 96 | struct android_ycbcr ycbcr {}; |
| 97 | enum chroma_order chroma_order {}; |
| 98 | int ret = 0; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 99 | |
| 100 | if (!gralloc_->lock_ycbcr) { |
| 101 | static std::once_flag once; |
| 102 | std::call_once(once, |
| 103 | []() { ALOGW("Gralloc does not support lock_ycbcr()"); }); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | memset(&ycbcr, 0, sizeof(ycbcr)); |
| 108 | ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr); |
| 109 | if (ret) { |
| 110 | ALOGW("gralloc->lock_ycbcr failed: %d", ret); |
| 111 | return false; |
| 112 | } |
| 113 | gralloc_->unlock(gralloc_, handle); |
| 114 | |
| 115 | /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags |
| 116 | * it will return the .y/.cb/.cr pointers based on a NULL pointer, |
| 117 | * so they can be interpreted as offsets. */ |
| 118 | bo->offsets[0] = (size_t)ycbcr.y; |
| 119 | /* We assume here that all the planes are located in one DMA-buf. */ |
| 120 | if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 121 | chroma_order = kYCrCb; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 122 | bo->offsets[1] = (size_t)ycbcr.cr; |
| 123 | bo->offsets[2] = (size_t)ycbcr.cb; |
| 124 | } else { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 125 | chroma_order = kYCbCr; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 126 | bo->offsets[1] = (size_t)ycbcr.cb; |
| 127 | bo->offsets[2] = (size_t)ycbcr.cr; |
| 128 | } |
| 129 | |
| 130 | /* .ystride is the line length (in bytes) of the Y plane, |
| 131 | * .cstride is the line length (in bytes) of any of the remaining |
| 132 | * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully |
| 133 | * planar formats. */ |
| 134 | bo->pitches[0] = ycbcr.ystride; |
| 135 | bo->pitches[1] = bo->pitches[2] = ycbcr.cstride; |
| 136 | |
| 137 | /* .chroma_step is the byte distance between the same chroma channel |
| 138 | * values of subsequent pixels, assumed to be the same for Cb and Cr. */ |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame] | 139 | bo->format = get_fourcc_yuv(hal_format, chroma_order, ycbcr.chroma_step); |
Roman Stratiienko | deb7735 | 2021-12-06 12:59:26 +0200 | [diff] [blame] | 140 | if (bo->format == UINT32_MAX) { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 141 | ALOGW( |
| 142 | "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = " |
| 143 | "%d", |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame] | 144 | hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb", |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 145 | (int)ycbcr.chroma_step); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that |
| 151 | * the single-fd case cannot happen. So handle eithe single |
| 152 | * fd or fd-per-plane case: |
| 153 | */ |
| 154 | if (num_fds == 1) { |
| 155 | bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0]; |
| 156 | } else { |
| 157 | int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3; |
| 158 | if (num_fds != expected_planes) |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 165 | auto BufferInfoLibdrm::GetBoInfo(buffer_handle_t handle) |
| 166 | -> std::optional<BufferInfo> { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 167 | gralloc_handle_t *gr_handle = gralloc_handle(handle); |
| 168 | if (!gr_handle) |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 169 | return {}; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 170 | |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 171 | BufferInfo bi{}; |
| 172 | |
| 173 | bi.width = gr_handle->width; |
| 174 | bi.height = gr_handle->height; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 175 | |
| 176 | #if GRALLOC_HANDLE_VERSION < 4 |
| 177 | static std::once_flag once; |
| 178 | std::call_once(once, []() { |
| 179 | ALOGE( |
| 180 | "libdrm < v2.4.97 has broken gralloc_handle structure. Please update."); |
| 181 | }); |
| 182 | #endif |
| 183 | #if GRALLOC_HANDLE_VERSION == 4 |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 184 | bi.modifiers[0] = gr_handle->modifier; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 185 | #endif |
| 186 | |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 187 | bi.prime_fds[0] = gr_handle->prime_fd; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 188 | |
| 189 | if (is_yuv(gr_handle->format)) { |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 190 | if (!GetYuvPlaneInfo(gr_handle->format, handle->numFds, handle, &bi)) |
| 191 | return {}; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 192 | } else { |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 193 | bi.pitches[0] = gr_handle->stride; |
| 194 | bi.offsets[0] = 0; |
Roman Stratiienko | 32819fe | 2020-10-30 13:53:00 +0200 | [diff] [blame] | 195 | |
| 196 | /* FOSS graphic components (gbm_gralloc, mesa3d) are translating |
| 197 | * HAL_PIXEL_FORMAT_RGB_565 to DRM_FORMAT_RGB565 without swapping |
| 198 | * the R and B components. Same must be done here. */ |
Roman Stratiienko | 1cbaaf9 | 2022-02-21 10:52:18 +0200 | [diff] [blame] | 199 | switch (gr_handle->format) { |
Roman Stratiienko | 32819fe | 2020-10-30 13:53:00 +0200 | [diff] [blame] | 200 | case HAL_PIXEL_FORMAT_RGB_565: |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 201 | bi.format = DRM_FORMAT_RGB565; |
Roman Stratiienko | 32819fe | 2020-10-30 13:53:00 +0200 | [diff] [blame] | 202 | break; |
| 203 | default: |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 204 | bi.format = ConvertHalFormatToDrm(gr_handle->format); |
Roman Stratiienko | 32819fe | 2020-10-30 13:53:00 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 207 | if (bi.format == DRM_FORMAT_INVALID) |
| 208 | return {}; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 209 | } |
| 210 | |
Roman Stratiienko | e9fbd8d | 2022-02-21 13:03:29 +0200 | [diff] [blame^] | 211 | return bi; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Roman Stratiienko | 39d8f7e | 2021-11-30 17:06:44 +0200 | [diff] [blame] | 214 | constexpr char gbm_gralloc_module_name[] = "GBM Memory Allocator"; |
| 215 | constexpr char drm_gralloc_module_name[] = "DRM Memory Allocator"; |
| 216 | |
| 217 | int BufferInfoLibdrm::ValidateGralloc() { |
| 218 | if (strcmp(gralloc_->common.name, drm_gralloc_module_name) != 0 && |
| 219 | strcmp(gralloc_->common.name, gbm_gralloc_module_name) != 0) { |
| 220 | ALOGE( |
| 221 | "Gralloc name isn't valid: Expected: \"%s\" or \"%s\", Actual: \"%s\"", |
| 222 | gbm_gralloc_module_name, drm_gralloc_module_name, |
| 223 | gralloc_->common.name); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 230 | } // namespace android |