blob: 6ea4b8e5174110d772866fb557adb0e6d2a26ecf [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesin25a26062014-09-12 16:18:59 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Yiwei Zhangb7a64442021-09-30 05:13:10 +00007#include "drv_helpers.h"
8
Yuly Novikov96c7a3b2015-12-08 22:48:29 -05009#include <assert.h>
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080010#include <errno.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070011#include <stdio.h>
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050012#include <stdlib.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Jason Macnak1de7f662020-01-24 15:05:57 -080015#include <sys/types.h>
16#include <unistd.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include <xf86drm.h>
18
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070019#include "drv_priv.h"
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050020#include "util.h"
21
Kristian H. Kristensen478343f2018-04-04 14:02:50 -070022struct planar_layout {
23 size_t num_planes;
24 int horizontal_subsampling[DRV_MAX_PLANES];
25 int vertical_subsampling[DRV_MAX_PLANES];
26 int bytes_per_pixel[DRV_MAX_PLANES];
27};
28
29// clang-format off
30
31static const struct planar_layout packed_1bpp_layout = {
32 .num_planes = 1,
33 .horizontal_subsampling = { 1 },
34 .vertical_subsampling = { 1 },
35 .bytes_per_pixel = { 1 }
36};
37
38static const struct planar_layout packed_2bpp_layout = {
39 .num_planes = 1,
40 .horizontal_subsampling = { 1 },
41 .vertical_subsampling = { 1 },
42 .bytes_per_pixel = { 2 }
43};
44
45static const struct planar_layout packed_3bpp_layout = {
46 .num_planes = 1,
47 .horizontal_subsampling = { 1 },
48 .vertical_subsampling = { 1 },
49 .bytes_per_pixel = { 3 }
50};
51
52static const struct planar_layout packed_4bpp_layout = {
53 .num_planes = 1,
54 .horizontal_subsampling = { 1 },
55 .vertical_subsampling = { 1 },
56 .bytes_per_pixel = { 4 }
57};
58
Nataraj Deshpande586e2d62019-08-21 15:19:46 -070059static const struct planar_layout packed_8bpp_layout = {
60 .num_planes = 1,
61 .horizontal_subsampling = { 1 },
62 .vertical_subsampling = { 1 },
63 .bytes_per_pixel = { 8 }
64};
65
Kristian H. Kristensen478343f2018-04-04 14:02:50 -070066static const struct planar_layout biplanar_yuv_420_layout = {
67 .num_planes = 2,
68 .horizontal_subsampling = { 1, 2 },
69 .vertical_subsampling = { 1, 2 },
70 .bytes_per_pixel = { 1, 2 }
71};
72
73static const struct planar_layout triplanar_yuv_420_layout = {
74 .num_planes = 3,
75 .horizontal_subsampling = { 1, 2, 2 },
76 .vertical_subsampling = { 1, 2, 2 },
77 .bytes_per_pixel = { 1, 1, 1 }
78};
79
Miguel Casas055a6aa2019-04-30 18:11:40 -040080static const struct planar_layout biplanar_yuv_p010_layout = {
81 .num_planes = 2,
82 .horizontal_subsampling = { 1, 2 },
83 .vertical_subsampling = { 1, 2 },
84 .bytes_per_pixel = { 2, 4 }
85};
86
Kristian H. Kristensen478343f2018-04-04 14:02:50 -070087// clang-format on
88
89static const struct planar_layout *layout_from_format(uint32_t format)
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080090{
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070091 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080092 case DRM_FORMAT_BGR233:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080093 case DRM_FORMAT_C8:
94 case DRM_FORMAT_R8:
95 case DRM_FORMAT_RGB332:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -070096 return &packed_1bpp_layout;
97
Jason Macnak1de7f662020-01-24 15:05:57 -080098 case DRM_FORMAT_R16:
99 return &packed_2bpp_layout;
100
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800101 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800102 case DRM_FORMAT_YVU420_ANDROID:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700103 return &triplanar_yuv_420_layout;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700104
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800105 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530106 case DRM_FORMAT_NV21:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700107 return &biplanar_yuv_420_layout;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500108
Miguel Casas055a6aa2019-04-30 18:11:40 -0400109 case DRM_FORMAT_P010:
110 return &biplanar_yuv_p010_layout;
111
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800112 case DRM_FORMAT_ABGR1555:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800113 case DRM_FORMAT_ABGR4444:
114 case DRM_FORMAT_ARGB1555:
115 case DRM_FORMAT_ARGB4444:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800116 case DRM_FORMAT_BGR565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800117 case DRM_FORMAT_BGRA4444:
118 case DRM_FORMAT_BGRA5551:
119 case DRM_FORMAT_BGRX4444:
120 case DRM_FORMAT_BGRX5551:
121 case DRM_FORMAT_GR88:
122 case DRM_FORMAT_RG88:
123 case DRM_FORMAT_RGB565:
124 case DRM_FORMAT_RGBA4444:
125 case DRM_FORMAT_RGBA5551:
126 case DRM_FORMAT_RGBX4444:
127 case DRM_FORMAT_RGBX5551:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800128 case DRM_FORMAT_UYVY:
129 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800130 case DRM_FORMAT_XBGR1555:
131 case DRM_FORMAT_XBGR4444:
132 case DRM_FORMAT_XRGB1555:
133 case DRM_FORMAT_XRGB4444:
134 case DRM_FORMAT_YUYV:
135 case DRM_FORMAT_YVYU:
Jasmine Chenc7aa9742019-08-14 15:28:22 +0800136 case DRM_FORMAT_MTISP_SXYZW10:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700137 return &packed_2bpp_layout;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700138
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800139 case DRM_FORMAT_BGR888:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800140 case DRM_FORMAT_RGB888:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700141 return &packed_3bpp_layout;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700142
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800143 case DRM_FORMAT_ABGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800144 case DRM_FORMAT_ABGR8888:
145 case DRM_FORMAT_ARGB2101010:
146 case DRM_FORMAT_ARGB8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800147 case DRM_FORMAT_AYUV:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800148 case DRM_FORMAT_BGRA1010102:
149 case DRM_FORMAT_BGRA8888:
150 case DRM_FORMAT_BGRX1010102:
151 case DRM_FORMAT_BGRX8888:
152 case DRM_FORMAT_RGBA1010102:
153 case DRM_FORMAT_RGBA8888:
154 case DRM_FORMAT_RGBX1010102:
155 case DRM_FORMAT_RGBX8888:
156 case DRM_FORMAT_XBGR2101010:
157 case DRM_FORMAT_XBGR8888:
158 case DRM_FORMAT_XRGB2101010:
159 case DRM_FORMAT_XRGB8888:
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700160 return &packed_4bpp_layout;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700161
Nataraj Deshpande586e2d62019-08-21 15:19:46 -0700162 case DRM_FORMAT_ABGR16161616F:
163 return &packed_8bpp_layout;
164
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700165 default:
166 drv_log("UNKNOWN FORMAT %d\n", format);
167 return NULL;
168 }
169}
170
171size_t drv_num_planes_from_format(uint32_t format)
172{
173 const struct planar_layout *layout = layout_from_format(format);
174
175 /*
176 * drv_bo_new calls this function early to query number of planes and
177 * considers 0 planes to mean unknown format, so we have to support
178 * that. All other layout_from_format() queries can assume that the
179 * format is supported and that the return value is non-NULL.
180 */
181
182 return layout ? layout->num_planes : 0;
183}
184
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100185size_t drv_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
186{
187 size_t planes = drv_num_planes_from_format(format);
188
189 /* Disallow unsupported formats. */
190 if (!planes)
191 return 0;
192
Drew Davenportb65708a2020-11-09 18:37:55 -0700193 if (drv->backend->num_planes_from_modifier && modifier != DRM_FORMAT_MOD_INVALID &&
194 modifier != DRM_FORMAT_MOD_LINEAR)
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100195 return drv->backend->num_planes_from_modifier(drv, format, modifier);
196
197 return planes;
198}
199
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700200uint32_t drv_height_from_format(uint32_t format, uint32_t height, size_t plane)
201{
202 const struct planar_layout *layout = layout_from_format(format);
203
204 assert(plane < layout->num_planes);
205
206 return DIV_ROUND_UP(height, layout->vertical_subsampling[plane]);
207}
208
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900209uint32_t drv_vertical_subsampling_from_format(uint32_t format, size_t plane)
210{
211 const struct planar_layout *layout = layout_from_format(format);
212
213 assert(plane < layout->num_planes);
214
215 return layout->vertical_subsampling[plane];
216}
217
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700218uint32_t drv_bytes_per_pixel_from_format(uint32_t format, size_t plane)
219{
220 const struct planar_layout *layout = layout_from_format(format);
221
222 assert(plane < layout->num_planes);
223
224 return layout->bytes_per_pixel[plane];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700225}
226
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700227/*
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700228 * This function returns the stride for a given format, width and plane.
229 */
230uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
231{
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700232 const struct planar_layout *layout = layout_from_format(format);
233 assert(plane < layout->num_planes);
234
Gurchetan Singh6bd78852018-06-06 17:15:31 -0700235 uint32_t plane_width = DIV_ROUND_UP(width, layout->horizontal_subsampling[plane]);
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700236 uint32_t stride = plane_width * layout->bytes_per_pixel[plane];
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700237
238 /*
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700239 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
240 * (see <system/graphics.h>).
241 */
242 if (format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800243 stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700244
245 return stride;
246}
247
Gurchetan Singhbc24bd72017-09-28 15:21:53 -0700248uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
249{
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700250 return stride * drv_height_from_format(format, height, plane);
251}
Gurchetan Singhbc24bd72017-09-28 15:21:53 -0700252
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700253static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
254{
255 if (plane != 0) {
256 switch (format) {
257 case DRM_FORMAT_YVU420:
258 case DRM_FORMAT_YVU420_ANDROID:
259 stride = DIV_ROUND_UP(stride, 2);
260 break;
261 }
Gurchetan Singhbc24bd72017-09-28 15:21:53 -0700262 }
263
Kristian H. Kristensen478343f2018-04-04 14:02:50 -0700264 return stride;
Gurchetan Singhbc24bd72017-09-28 15:21:53 -0700265}
266
Gurchetan Singh4f298f92017-03-23 11:29:26 -0700267/*
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700268 * This function fills in the buffer object given the driver aligned stride of
269 * the first plane, height and a format. This function assumes there is just
270 * one kernel buffer per buffer object.
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700271 */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800272int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700273{
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900274 uint32_t padding[DRV_MAX_PLANES] = { 0 };
275 return drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding);
276}
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700277
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900278int drv_bo_from_format_and_padding(struct bo *bo, uint32_t stride, uint32_t aligned_height,
279 uint32_t format, uint32_t padding[DRV_MAX_PLANES])
280{
Gurchetan Singh2a119342016-11-02 10:40:51 -0700281 size_t p, num_planes;
282 uint32_t offset = 0;
283
284 num_planes = drv_num_planes_from_format(format);
285 assert(num_planes);
Owen Linbbb69fd2017-06-05 14:33:08 +0800286
287 /*
288 * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
289 * - the aligned height is same as the buffer's height.
290 * - the chroma stride is 16 bytes aligned, i.e., the luma's strides
291 * is 32 bytes aligned.
292 */
Tomasz Figad846de62017-07-29 15:47:54 +0900293 if (format == DRM_FORMAT_YVU420_ANDROID) {
Gurchetan Singh298b7572019-09-19 09:55:18 -0700294 assert(aligned_height == bo->meta.height);
Owen Linbbb69fd2017-06-05 14:33:08 +0800295 assert(stride == ALIGN(stride, 32));
296 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700297
298 for (p = 0; p < num_planes; p++) {
Gurchetan Singh298b7572019-09-19 09:55:18 -0700299 bo->meta.strides[p] = subsample_stride(stride, format, p);
300 bo->meta.sizes[p] =
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900301 drv_size_from_format(format, bo->meta.strides[p], aligned_height, p) +
302 padding[p];
Gurchetan Singh298b7572019-09-19 09:55:18 -0700303 bo->meta.offsets[p] = offset;
304 offset += bo->meta.sizes[p];
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700305 }
306
Gurchetan Singh298b7572019-09-19 09:55:18 -0700307 bo->meta.total_size = offset;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700308 return 0;
309}
310
Dominik Behr6e6dc492019-10-09 15:43:52 -0700311int drv_dumb_bo_create_ex(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
312 uint64_t use_flags, uint64_t quirks)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700313{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700314 int ret;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700315 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700316 uint32_t aligned_width, aligned_height;
Gurchetan Singh99644382020-10-07 15:28:11 -0700317 struct drm_mode_create_dumb create_dumb = { 0 };
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700318
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700319 aligned_width = width;
320 aligned_height = height;
Keiichi Watanabeaf94db92018-08-06 18:37:21 +0900321 switch (format) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800322 case DRM_FORMAT_R16:
323 /* HAL_PIXEL_FORMAT_Y16 requires that the buffer's width be 16 pixel
324 * aligned. See hardware/interfaces/graphics/common/1.0/types.hal. */
325 aligned_width = ALIGN(width, 16);
326 break;
Keiichi Watanabeaf94db92018-08-06 18:37:21 +0900327 case DRM_FORMAT_YVU420_ANDROID:
Jason Macnak778e21d2020-04-28 11:21:16 -0700328 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not
329 * be aligned. Update 'height' so that drv_bo_from_format below
330 * uses the non-aligned height. */
331 height = bo->meta.height;
332
Keiichi Watanabeaf94db92018-08-06 18:37:21 +0900333 /* Align width to 32 pixels, so chroma strides are 16 bytes as
334 * Android requires. */
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700335 aligned_width = ALIGN(width, 32);
Jason Macnak778e21d2020-04-28 11:21:16 -0700336
337 /* Adjust the height to include room for chroma planes. */
338 aligned_height = 3 * DIV_ROUND_UP(height, 2);
Keiichi Watanabeaf94db92018-08-06 18:37:21 +0900339 break;
340 case DRM_FORMAT_YVU420:
341 case DRM_FORMAT_NV12:
Jason Macnak1de7f662020-01-24 15:05:57 -0800342 case DRM_FORMAT_NV21:
Jason Macnak2ce35772021-06-08 06:45:45 -0700343 case DRM_FORMAT_P010:
Keiichi Watanabeaf94db92018-08-06 18:37:21 +0900344 /* Adjust the height to include room for chroma planes */
345 aligned_height = 3 * DIV_ROUND_UP(height, 2);
346 break;
347 default:
348 break;
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700349 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500350
Dominik Behr6e6dc492019-10-09 15:43:52 -0700351 if (quirks & BO_QUIRK_DUMB32BPP) {
352 aligned_width =
353 DIV_ROUND_UP(aligned_width * layout_from_format(format)->bytes_per_pixel[0], 4);
354 create_dumb.bpp = 32;
355 } else {
356 create_dumb.bpp = layout_from_format(format)->bytes_per_pixel[0] * 8;
357 }
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700358 create_dumb.width = aligned_width;
Dominik Behr6e6dc492019-10-09 15:43:52 -0700359 create_dumb.height = aligned_height;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700360 create_dumb.flags = 0;
361
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700362 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700363 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700364 drv_log("DRM_IOCTL_MODE_CREATE_DUMB failed (%d, %d)\n", bo->drv->fd, errno);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700365 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700366 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700367
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700368 drv_bo_from_format(bo, create_dumb.pitch, height, format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700369
Gurchetan Singh298b7572019-09-19 09:55:18 -0700370 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhd5f8e442017-03-16 13:05:45 -0700371 bo->handles[plane].u32 = create_dumb.handle;
372
Gurchetan Singh298b7572019-09-19 09:55:18 -0700373 bo->meta.total_size = create_dumb.size;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700374 return 0;
375}
376
Dominik Behr6e6dc492019-10-09 15:43:52 -0700377int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
378 uint64_t use_flags)
379{
380 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_NONE);
381}
382
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700383int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700384{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700385 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700386 struct drm_mode_destroy_dumb destroy_dumb = { 0 };
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700387
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500388 destroy_dumb.handle = bo->handles[0].u32;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700389 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700390 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700391 drv_log("DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700392 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700393 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700394
395 return 0;
396}
397
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700398int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700399{
400 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500401 int ret, error = 0;
402 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700403
Gurchetan Singh298b7572019-09-19 09:55:18 -0700404 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700405 for (i = 0; i < plane; i++)
406 if (bo->handles[i].u32 == bo->handles[plane].u32)
407 break;
408 /* Make sure close hasn't already been called on this handle */
409 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500410 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700411
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500412 memset(&gem_close, 0, sizeof(gem_close));
413 gem_close.handle = bo->handles[plane].u32;
414
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700415 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500416 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700417 drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800418 bo->handles[plane].u32, ret);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700419 error = -errno;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500420 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700421 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700422
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500423 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700424}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700425
Gurchetan Singh71611d62017-01-03 16:49:56 -0800426int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
427{
428 int ret;
429 size_t plane;
430 struct drm_prime_handle prime_handle;
431
Gurchetan Singh298b7572019-09-19 09:55:18 -0700432 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh71611d62017-01-03 16:49:56 -0800433 memset(&prime_handle, 0, sizeof(prime_handle));
434 prime_handle.fd = data->fds[plane];
435
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800436 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800437
438 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700439 drv_log("DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n", prime_handle.fd);
Gurchetan Singh71611d62017-01-03 16:49:56 -0800440
441 /*
442 * Need to call GEM close on planes that were opened,
443 * if any. Adjust the num_planes variable to be the
444 * plane that failed, so GEM close will be called on
445 * planes before that plane.
446 */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700447 bo->meta.num_planes = plane;
Gurchetan Singh71611d62017-01-03 16:49:56 -0800448 drv_gem_bo_destroy(bo);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700449 return -errno;
Gurchetan Singh71611d62017-01-03 16:49:56 -0800450 }
451
452 bo->handles[plane].u32 = prime_handle.handle;
453 }
David Stevensb42624c2020-09-10 10:50:26 +0900454 bo->meta.tiling = data->tiling;
Gurchetan Singh71611d62017-01-03 16:49:56 -0800455
Gurchetan Singh71611d62017-01-03 16:49:56 -0800456 return 0;
457}
458
Gurchetan Singhee43c302017-11-14 18:20:27 -0800459void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700460{
461 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700462 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700463 struct drm_mode_map_dumb map_dumb;
464
465 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700466 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700467
468 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
469 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700470 drv_log("DRM_IOCTL_MODE_MAP_DUMB failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700471 return MAP_FAILED;
472 }
473
Gurchetan Singh298b7572019-09-19 09:55:18 -0700474 for (i = 0; i < bo->meta.num_planes; i++)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700475 if (bo->handles[i].u32 == bo->handles[plane].u32)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700476 vma->length += bo->meta.sizes[i];
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700477
Gurchetan Singhee43c302017-11-14 18:20:27 -0800478 return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700479 map_dumb.offset);
Gurchetan Singhef920532016-08-12 16:38:25 -0700480}
481
Gurchetan Singhee43c302017-11-14 18:20:27 -0800482int drv_bo_munmap(struct bo *bo, struct vma *vma)
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700483{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800484 return munmap(vma->addr, vma->length);
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700485}
486
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700487int drv_get_prot(uint32_t map_flags)
488{
489 return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
490}
491
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700492void drv_add_combination(struct driver *drv, const uint32_t format,
493 struct format_metadata *metadata, uint64_t use_flags)
494{
495 struct combination combo = { .format = format,
496 .metadata = *metadata,
497 .use_flags = use_flags };
498
499 drv_array_append(drv->combos, &combo);
500}
501
Gurchetan Singhd3001452017-11-03 17:18:36 -0700502void drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
503 struct format_metadata *metadata, uint64_t use_flags)
Gurchetan Singh179687e2016-10-28 10:07:35 -0700504{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800505 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700506
507 for (i = 0; i < num_formats; i++) {
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700508 struct combination combo = { .format = formats[i],
509 .metadata = *metadata,
510 .use_flags = use_flags };
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700511
512 drv_array_append(drv->combos, &combo);
513 }
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800514}
515
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800516void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700517 uint64_t use_flags)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800518{
519 uint32_t i;
520 struct combination *combo;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700521 /* Attempts to add the specified flags to an existing combination. */
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700522 for (i = 0; i < drv_array_size(drv->combos); i++) {
523 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800524 if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800525 combo->metadata.modifier == metadata->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700526 combo->use_flags |= use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800527 }
528}
529
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700530int drv_modify_linear_combinations(struct driver *drv)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800531{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800532 /*
533 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
Gurchetan Singh28e5ea02019-12-19 10:56:51 -0800534 * plane and as a cursor.
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800535 */
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700536 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
537 BO_USE_CURSOR | BO_USE_SCANOUT);
538 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
539 BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800540 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700541}
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700542
543/*
544 * Pick the best modifier from modifiers, according to the ordering
545 * given by modifier_order.
546 */
547uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
548 const uint64_t *modifier_order, uint32_t order_count)
549{
550 uint32_t i, j;
551
552 for (i = 0; i < order_count; i++) {
553 for (j = 0; j < count; j++) {
554 if (modifiers[j] == modifier_order[i]) {
555 return modifiers[j];
556 }
557 }
558 }
559
560 return DRM_FORMAT_MOD_LINEAR;
561}
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700562
563/*
564 * Search a list of modifiers to see if a given modifier is present
565 */
566bool drv_has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
567{
568 uint32_t i;
569 for (i = 0; i < count; i++)
570 if (list[i] == modifier)
571 return true;
572
573 return false;
574}
Roman Stratiienko142dd9c2020-12-14 17:34:09 +0200575
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000576void drv_resolve_format_and_use_flags_helper(struct driver *drv, uint32_t format,
577 uint64_t use_flags, uint32_t *out_format,
578 uint64_t *out_use_flags)
Gurchetan Singh695125c2021-02-03 08:44:09 -0800579{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000580 *out_format = format;
581 *out_use_flags = use_flags;
Gurchetan Singh695125c2021-02-03 08:44:09 -0800582 switch (format) {
583 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
584 /* Common camera implementation defined format. */
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000585 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
586 *out_format = DRM_FORMAT_NV12;
587 } else {
588 /* HACK: See b/28671744 */
589 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +0000590 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000591 }
592 break;
Gurchetan Singh695125c2021-02-03 08:44:09 -0800593 case DRM_FORMAT_FLEX_YCbCr_420_888:
594 /* Common flexible video format. */
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000595 *out_format = DRM_FORMAT_NV12;
596 break;
597 case DRM_FORMAT_YVU420_ANDROID:
598 *out_use_flags &= ~BO_USE_SCANOUT;
599 break;
Gurchetan Singh695125c2021-02-03 08:44:09 -0800600 default:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000601 break;
Gurchetan Singh695125c2021-02-03 08:44:09 -0800602 }
603}