• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 
17 #define LOG_TAG "hwc-bufferinfo-libdrm"
18 
19 #include "BufferInfoLibdrm.h"
20 
21 #include <gralloc_handle.h>
22 #include <hardware/gralloc.h>
23 #include <xf86drm.h>
24 #include <xf86drmMode.h>
25 
26 #include <mutex>
27 
28 #include "utils/log.h"
29 #include "utils/properties.h"
30 
31 namespace android {
32 
33 LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm);
34 
35 enum chroma_order {
36   kYCbCr,
37   kYCrCb,
38 };
39 
40 struct DroidYuvFormat {
41   /* Lookup keys */
42   uint32_t native;                /* HAL_PIXEL_FORMAT_ */
43   enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
44   size_t chroma_step; /* Distance in bytes between subsequent chroma pixels. */
45 
46   /* Result */
47   int fourcc; /* DRM_FORMAT_ */
48 };
49 
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 
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. */
57 static const struct DroidYuvFormat kDroidYuvFormats[] = {
58     /* Native format, YCrCb, Chroma step, DRI image FourCC */
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},
63     /* HACK: See droid_create_image_from_prime_fds() and
64      * https://issuetracker.google.com/32077885. */
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},
70 };
71 
72 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73 
get_fourcc_yuv(uint32_t native,enum chroma_order chroma_order,size_t chroma_step)74 static uint32_t get_fourcc_yuv(uint32_t native, enum chroma_order chroma_order,
75                                size_t chroma_step) {
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;
81 
82   return UINT32_MAX;
83 }
84 
is_yuv(uint32_t native)85 static bool is_yuv(uint32_t native) {
86   // NOLINTNEXTLINE(readability-use-anyofallof)
87   for (auto droid_yuv_format : kDroidYuvFormats)
88     if (droid_yuv_format.native == native)
89       return true;
90 
91   return false;
92 }
93 
GetYuvPlaneInfo(int num_fds,buffer_handle_t handle,hwc_drm_bo_t * bo)94 bool BufferInfoLibdrm::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
95                                        hwc_drm_bo_t *bo) {
96   struct android_ycbcr ycbcr {};
97   enum chroma_order chroma_order {};
98   int ret = 0;
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) {
121     chroma_order = kYCrCb;
122     bo->offsets[1] = (size_t)ycbcr.cr;
123     bo->offsets[2] = (size_t)ycbcr.cb;
124   } else {
125     chroma_order = kYCbCr;
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. */
139   bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step);
140   if (bo->format == UINT32_MAX) {
141     ALOGW(
142         "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
143         "%d",
144         bo->hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb",
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 
ConvertBoInfo(buffer_handle_t handle,hwc_drm_bo_t * bo)165 int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
166   gralloc_handle_t *gr_handle = gralloc_handle(handle);
167   if (!gr_handle)
168     return -EINVAL;
169 
170   bo->width = gr_handle->width;
171   bo->height = gr_handle->height;
172   bo->hal_format = gr_handle->format;
173 
174 #if GRALLOC_HANDLE_VERSION < 4
175   static std::once_flag once;
176   std::call_once(once, []() {
177     ALOGE(
178         "libdrm < v2.4.97 has broken gralloc_handle structure. Please update.");
179   });
180 #endif
181 #if GRALLOC_HANDLE_VERSION == 4
182   bo->modifiers[0] = gr_handle->modifier;
183 #endif
184 
185   bo->usage = gr_handle->usage;
186   bo->prime_fds[0] = gr_handle->prime_fd;
187 
188   if (is_yuv(gr_handle->format)) {
189     if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
190       return -EINVAL;
191   } else {
192     bo->pitches[0] = gr_handle->stride;
193     bo->offsets[0] = 0;
194 
195     /* FOSS graphic components (gbm_gralloc, mesa3d) are translating
196      * HAL_PIXEL_FORMAT_RGB_565 to DRM_FORMAT_RGB565 without swapping
197      * the R and B components. Same must be done here. */
198     switch (bo->hal_format) {
199       case HAL_PIXEL_FORMAT_RGB_565:
200         bo->format = DRM_FORMAT_RGB565;
201         break;
202       default:
203         bo->format = ConvertHalFormatToDrm(gr_handle->format);
204     }
205 
206     if (bo->format == DRM_FORMAT_INVALID)
207       return -EINVAL;
208   }
209 
210   return 0;
211 }
212 
213 constexpr char gbm_gralloc_module_name[] = "GBM Memory Allocator";
214 constexpr char drm_gralloc_module_name[] = "DRM Memory Allocator";
215 
ValidateGralloc()216 int BufferInfoLibdrm::ValidateGralloc() {
217   if (strcmp(gralloc_->common.name, drm_gralloc_module_name) != 0 &&
218       strcmp(gralloc_->common.name, gbm_gralloc_module_name) != 0) {
219     ALOGE(
220         "Gralloc name isn't valid: Expected: \"%s\" or \"%s\", Actual: \"%s\"",
221         gbm_gralloc_module_name, drm_gralloc_module_name,
222         gralloc_->common.name);
223     return -EINVAL;
224   }
225 
226   return 0;
227 }
228 
229 }  // namespace android
230