blob: 6978b08954f928ed007bbfedaf73df22069fe234 [file] [log] [blame]
Roman Stratiienko946126c2020-10-02 19:01:10 +03001/*
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +03002 * Copyright (C) 2020 The Android Open Source Project
Roman Stratiienko946126c2020-10-02 19:01:10 +03003 *
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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko946126c2020-10-02 19:01:10 +030018
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030019#include "BufferInfoLibdrm.h"
Roman Stratiienko946126c2020-10-02 19:01:10 +030020
Roman Stratiienko946126c2020-10-02 19:01:10 +030021#include <gralloc_handle.h>
22#include <hardware/gralloc.h>
Roman Stratiienko946126c2020-10-02 19:01:10 +030023#include <xf86drm.h>
24#include <xf86drmMode.h>
25
Roman Stratiienko1e053b42021-10-25 22:54:20 +030026#include <mutex>
27
Roman Stratiienkod21071f2021-03-09 21:56:50 +020028#include "utils/log.h"
29#include "utils/properties.h"
30
Roman Stratiienko946126c2020-10-02 19:01:10 +030031namespace android {
32
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030033LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm);
Roman Stratiienko946126c2020-10-02 19:01:10 +030034
35enum chroma_order {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020036 kYCbCr,
37 kYCrCb,
Roman Stratiienko946126c2020-10-02 19:01:10 +030038};
39
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020040struct DroidYuvFormat {
Roman Stratiienko946126c2020-10-02 19:01:10 +030041 /* Lookup keys */
Roman Stratiienkod26619b2021-08-04 19:55:37 +030042 uint32_t native; /* HAL_PIXEL_FORMAT_ */
Roman Stratiienko946126c2020-10-02 19:01:10 +030043 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
Roman Stratiienkod26619b2021-08-04 19:55:37 +030044 size_t chroma_step; /* Distance in bytes between subsequent chroma pixels. */
Roman Stratiienko946126c2020-10-02 19:01:10 +030045
46 /* Result */
47 int fourcc; /* DRM_FORMAT_ */
48};
49
Roman Stratiienko2c63b332022-02-10 10:45:06 +020050#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 Stratiienko946126c2020-10-02 19:01:10 +030055/* 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 Stratiienkoe2f2c922021-02-13 10:57:47 +020057static const struct DroidYuvFormat kDroidYuvFormats[] = {
Roman Stratiienko946126c2020-10-02 19:01:10 +030058 /* Native format, YCrCb, Chroma step, DRI image FourCC */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020059 {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 Stratiienko946126c2020-10-02 19:01:10 +030063 /* HACK: See droid_create_image_from_prime_fds() and
64 * https://issuetracker.google.com/32077885. */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020065 {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 Stratiienko946126c2020-10-02 19:01:10 +030070};
71
72#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
Roman Stratiienkodeb77352021-12-06 12:59:26 +020074static uint32_t get_fourcc_yuv(uint32_t native, enum chroma_order chroma_order,
75 size_t chroma_step) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020076 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 Stratiienko946126c2020-10-02 19:01:10 +030081
Roman Stratiienkodeb77352021-12-06 12:59:26 +020082 return UINT32_MAX;
Roman Stratiienko946126c2020-10-02 19:01:10 +030083}
84
Roman Stratiienkod26619b2021-08-04 19:55:37 +030085static bool is_yuv(uint32_t native) {
Roman Stratiienko568e5982022-01-04 11:42:33 +020086 // NOLINTNEXTLINE(readability-use-anyofallof)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020087 for (auto droid_yuv_format : kDroidYuvFormats)
88 if (droid_yuv_format.native == native)
Roman Stratiienko946126c2020-10-02 19:01:10 +030089 return true;
90
91 return false;
92}
93
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020094bool BufferInfoLibdrm::GetYuvPlaneInfo(uint32_t hal_format, int num_fds,
95 buffer_handle_t handle, BufferInfo *bo) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020096 struct android_ycbcr ycbcr {};
97 enum chroma_order chroma_order {};
98 int ret = 0;
Roman Stratiienko946126c2020-10-02 19:01:10 +030099
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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200121 chroma_order = kYCrCb;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300122 bo->offsets[1] = (size_t)ycbcr.cr;
123 bo->offsets[2] = (size_t)ycbcr.cb;
124 } else {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200125 chroma_order = kYCbCr;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300126 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 Stratiienko1cbaaf92022-02-21 10:52:18 +0200139 bo->format = get_fourcc_yuv(hal_format, chroma_order, ycbcr.chroma_step);
Roman Stratiienkodeb77352021-12-06 12:59:26 +0200140 if (bo->format == UINT32_MAX) {
Roman Stratiienko946126c2020-10-02 19:01:10 +0300141 ALOGW(
142 "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
143 "%d",
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +0200144 hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb",
Roman Stratiienko946126c2020-10-02 19:01:10 +0300145 (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 {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300157 const int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300158 if (num_fds != expected_planes)
159 return false;
160 }
161
162 return true;
163}
164
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200165auto BufferInfoLibdrm::GetBoInfo(buffer_handle_t handle)
166 -> std::optional<BufferInfo> {
Roman Stratiienko946126c2020-10-02 19:01:10 +0300167 gralloc_handle_t *gr_handle = gralloc_handle(handle);
168 if (!gr_handle)
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200169 return {};
Roman Stratiienko946126c2020-10-02 19:01:10 +0300170
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200171 BufferInfo bi{};
172
173 bi.width = gr_handle->width;
174 bi.height = gr_handle->height;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300175
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 Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200184 bi.modifiers[0] = gr_handle->modifier;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300185#endif
186
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200187 bi.prime_fds[0] = gr_handle->prime_fd;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300188
189 if (is_yuv(gr_handle->format)) {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200190 if (!GetYuvPlaneInfo(gr_handle->format, handle->numFds, handle, &bi))
191 return {};
Roman Stratiienko946126c2020-10-02 19:01:10 +0300192 } else {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200193 bi.pitches[0] = gr_handle->stride;
194 bi.offsets[0] = 0;
Roman Stratiienko32819fe2020-10-30 13:53:00 +0200195
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 Stratiienko1cbaaf92022-02-21 10:52:18 +0200199 switch (gr_handle->format) {
Roman Stratiienko32819fe2020-10-30 13:53:00 +0200200 case HAL_PIXEL_FORMAT_RGB_565:
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200201 bi.format = DRM_FORMAT_RGB565;
Roman Stratiienko32819fe2020-10-30 13:53:00 +0200202 break;
203 default:
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200204 bi.format = ConvertHalFormatToDrm(gr_handle->format);
Roman Stratiienko32819fe2020-10-30 13:53:00 +0200205 }
206
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200207 if (bi.format == DRM_FORMAT_INVALID)
208 return {};
Roman Stratiienko946126c2020-10-02 19:01:10 +0300209 }
210
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200211 return bi;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300212}
213
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +0200214constexpr char gbm_gralloc_module_name[] = "GBM Memory Allocator";
215constexpr char drm_gralloc_module_name[] = "DRM Memory Allocator";
216
217int 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 Stratiienko946126c2020-10-02 19:01:10 +0300230} // namespace android