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 | |
| 50 | /* The following table is used to look up a DRI image FourCC based |
| 51 | * on native format and information contained in android_ycbcr struct. */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 52 | static const struct DroidYuvFormat kDroidYuvFormats[] = { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 53 | /* Native format, YCrCb, Chroma step, DRI image FourCC */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 54 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 2, DRM_FORMAT_NV12}, |
| 55 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 1, DRM_FORMAT_YUV420}, |
| 56 | {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCrCb, 1, DRM_FORMAT_YVU420}, |
| 57 | {HAL_PIXEL_FORMAT_YV12, kYCrCb, 1, DRM_FORMAT_YVU420}, |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 58 | /* HACK: See droid_create_image_from_prime_fds() and |
| 59 | * https://issuetracker.google.com/32077885. */ |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 60 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 2, DRM_FORMAT_NV12}, |
| 61 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 1, DRM_FORMAT_YUV420}, |
| 62 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_YVU420}, |
| 63 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_AYUV}, |
| 64 | {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_XYUV8888}, |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 68 | |
Roman Stratiienko | d26619b | 2021-08-04 19:55:37 +0300 | [diff] [blame] | 69 | static int get_fourcc_yuv(uint32_t native, enum chroma_order chroma_order, |
| 70 | size_t chroma_step) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 71 | for (auto droid_yuv_format : kDroidYuvFormats) |
| 72 | if (droid_yuv_format.native == native && |
| 73 | droid_yuv_format.chroma_order == chroma_order && |
| 74 | droid_yuv_format.chroma_step == chroma_step) |
| 75 | return droid_yuv_format.fourcc; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 76 | |
| 77 | return -1; |
| 78 | } |
| 79 | |
Roman Stratiienko | d26619b | 2021-08-04 19:55:37 +0300 | [diff] [blame] | 80 | static bool is_yuv(uint32_t native) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 81 | for (auto droid_yuv_format : kDroidYuvFormats) |
| 82 | if (droid_yuv_format.native == native) |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 83 | return true; |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 88 | bool BufferInfoLibdrm::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle, |
| 89 | hwc_drm_bo_t *bo) { |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 90 | struct android_ycbcr ycbcr {}; |
| 91 | enum chroma_order chroma_order {}; |
| 92 | int ret = 0; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 93 | |
| 94 | if (!gralloc_->lock_ycbcr) { |
| 95 | static std::once_flag once; |
| 96 | std::call_once(once, |
| 97 | []() { ALOGW("Gralloc does not support lock_ycbcr()"); }); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | memset(&ycbcr, 0, sizeof(ycbcr)); |
| 102 | ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr); |
| 103 | if (ret) { |
| 104 | ALOGW("gralloc->lock_ycbcr failed: %d", ret); |
| 105 | return false; |
| 106 | } |
| 107 | gralloc_->unlock(gralloc_, handle); |
| 108 | |
| 109 | /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags |
| 110 | * it will return the .y/.cb/.cr pointers based on a NULL pointer, |
| 111 | * so they can be interpreted as offsets. */ |
| 112 | bo->offsets[0] = (size_t)ycbcr.y; |
| 113 | /* We assume here that all the planes are located in one DMA-buf. */ |
| 114 | if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 115 | chroma_order = kYCrCb; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 116 | bo->offsets[1] = (size_t)ycbcr.cr; |
| 117 | bo->offsets[2] = (size_t)ycbcr.cb; |
| 118 | } else { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 119 | chroma_order = kYCbCr; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 120 | bo->offsets[1] = (size_t)ycbcr.cb; |
| 121 | bo->offsets[2] = (size_t)ycbcr.cr; |
| 122 | } |
| 123 | |
| 124 | /* .ystride is the line length (in bytes) of the Y plane, |
| 125 | * .cstride is the line length (in bytes) of any of the remaining |
| 126 | * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully |
| 127 | * planar formats. */ |
| 128 | bo->pitches[0] = ycbcr.ystride; |
| 129 | bo->pitches[1] = bo->pitches[2] = ycbcr.cstride; |
| 130 | |
| 131 | /* .chroma_step is the byte distance between the same chroma channel |
| 132 | * values of subsequent pixels, assumed to be the same for Cb and Cr. */ |
| 133 | bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step); |
| 134 | if (bo->format == -1) { |
| 135 | ALOGW( |
| 136 | "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = " |
| 137 | "%d", |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 138 | bo->hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb", |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 139 | (int)ycbcr.chroma_step); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that |
| 145 | * the single-fd case cannot happen. So handle eithe single |
| 146 | * fd or fd-per-plane case: |
| 147 | */ |
| 148 | if (num_fds == 1) { |
| 149 | bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0]; |
| 150 | } else { |
| 151 | int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3; |
| 152 | if (num_fds != expected_planes) |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 159 | int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 160 | gralloc_handle_t *gr_handle = gralloc_handle(handle); |
| 161 | if (!gr_handle) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | bo->width = gr_handle->width; |
| 165 | bo->height = gr_handle->height; |
| 166 | bo->hal_format = gr_handle->format; |
| 167 | |
| 168 | #if GRALLOC_HANDLE_VERSION < 4 |
| 169 | static std::once_flag once; |
| 170 | std::call_once(once, []() { |
| 171 | ALOGE( |
| 172 | "libdrm < v2.4.97 has broken gralloc_handle structure. Please update."); |
| 173 | }); |
| 174 | #endif |
| 175 | #if GRALLOC_HANDLE_VERSION == 4 |
| 176 | bo->modifiers[0] = gr_handle->modifier; |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | bo->usage = gr_handle->usage; |
| 180 | bo->prime_fds[0] = gr_handle->prime_fd; |
| 181 | |
| 182 | if (is_yuv(gr_handle->format)) { |
| 183 | if (!GetYuvPlaneInfo(handle->numFds, handle, bo)) |
| 184 | return -EINVAL; |
| 185 | } else { |
| 186 | bo->pitches[0] = gr_handle->stride; |
| 187 | bo->offsets[0] = 0; |
Roman Stratiienko | 32819fe | 2020-10-30 13:53:00 +0200 | [diff] [blame] | 188 | |
| 189 | /* FOSS graphic components (gbm_gralloc, mesa3d) are translating |
| 190 | * HAL_PIXEL_FORMAT_RGB_565 to DRM_FORMAT_RGB565 without swapping |
| 191 | * the R and B components. Same must be done here. */ |
| 192 | switch (bo->hal_format) { |
| 193 | case HAL_PIXEL_FORMAT_RGB_565: |
| 194 | bo->format = DRM_FORMAT_RGB565; |
| 195 | break; |
| 196 | default: |
| 197 | bo->format = ConvertHalFormatToDrm(gr_handle->format); |
| 198 | } |
| 199 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 200 | if (bo->format == DRM_FORMAT_INVALID) |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
Roman Stratiienko | 946126c | 2020-10-02 19:01:10 +0300 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | } // namespace android |