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