blob: 1634f8adf9a85aef0671caf37bd2f721e24d5adb [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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030017#define LOG_TAG "hwc-bufferinfo-libdrm"
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
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
28namespace android {
29
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm);
Roman Stratiienko946126c2020-10-02 19:01:10 +030031
32enum chroma_order {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020033 kYCbCr,
34 kYCrCb,
Roman Stratiienko946126c2020-10-02 19:01:10 +030035};
36
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020037struct DroidYuvFormat {
Roman Stratiienko946126c2020-10-02 19:01:10 +030038 /* 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 Stratiienkoe2f2c922021-02-13 10:57:47 +020049static const struct DroidYuvFormat kDroidYuvFormats[] = {
Roman Stratiienko946126c2020-10-02 19:01:10 +030050 /* Native format, YCrCb, Chroma step, DRI image FourCC */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020051 {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 Stratiienko946126c2020-10-02 19:01:10 +030055 /* HACK: See droid_create_image_from_prime_fds() and
56 * https://issuetracker.google.com/32077885. */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020057 {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 Stratiienko946126c2020-10-02 19:01:10 +030062};
63
64#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
65
66static int get_fourcc_yuv(int native, enum chroma_order chroma_order,
67 int chroma_step) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020068 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 Stratiienko946126c2020-10-02 19:01:10 +030073
74 return -1;
75}
76
77static bool is_yuv(int native) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020078 for (auto droid_yuv_format : kDroidYuvFormats)
79 if (droid_yuv_format.native == native)
Roman Stratiienko946126c2020-10-02 19:01:10 +030080 return true;
81
82 return false;
83}
84
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030085bool BufferInfoLibdrm::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
86 hwc_drm_bo_t *bo) {
Roman Stratiienko946126c2020-10-02 19:01:10 +030087 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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200112 chroma_order = kYCrCb;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300113 bo->offsets[1] = (size_t)ycbcr.cr;
114 bo->offsets[2] = (size_t)ycbcr.cb;
115 } else {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200116 chroma_order = kYCbCr;
Roman Stratiienko946126c2020-10-02 19:01:10 +0300117 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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200135 bo->hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb",
Roman Stratiienko946126c2020-10-02 19:01:10 +0300136 (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 Stratiienkob2e9fe22020-10-03 10:52:36 +0300156int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
Roman Stratiienko946126c2020-10-02 19:01:10 +0300157 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 Stratiienko946126c2020-10-02 19:01:10 +0300174#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 Stratiienko32819fe2020-10-30 13:53:00 +0200185
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 Stratiienko946126c2020-10-02 19:01:10 +0300197 if (bo->format == DRM_FORMAT_INVALID)
198 return -EINVAL;
199 }
200
Roman Stratiienko946126c2020-10-02 19:01:10 +0300201 return 0;
202}
203
204} // namespace android