blob: 190347ee51edd6f9cdf5cf027d9ea6047b038e79 [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
Yuly Novikov96c7a3b2015-12-08 22:48:29 -05007#include <assert.h>
Dominik Behrf7b33d72014-11-11 07:17:11 -08008#include <fcntl.h>
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -08009#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
Gurchetan Singh1ef809e2017-11-06 11:07:52 -080013#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <xf86drm.h>
15
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "gbm_helpers.h"
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080018#include "gbm_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070019#include "util.h"
20
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080021PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070022{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070023
24 return drv_get_fd(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070025}
26
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080027PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070028{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070029 return drv_get_name(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070030}
31
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080032PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070033{
Gurchetan Singha1892b22017-09-28 16:40:52 -070034 uint64_t use_flags;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070035
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080036 if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070037 return 0;
38
Gurchetan Singha1892b22017-09-28 16:40:52 -070039 use_flags = gbm_convert_usage(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040
Gurchetan Singha1892b22017-09-28 16:40:52 -070041 return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070042}
43
Gurchetan Singh1cfdc312019-12-06 10:24:53 -080044PUBLIC int gbm_device_get_format_modifier_plane_count(struct gbm_device *gbm, uint32_t format,
45 uint64_t modifier)
46{
47 return 0;
48}
49
Stéphane Marchesin25a26062014-09-12 16:18:59 -070050PUBLIC struct gbm_device *gbm_create_device(int fd)
51{
52 struct gbm_device *gbm;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070053
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080054 gbm = (struct gbm_device *)malloc(sizeof(*gbm));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070055
Stéphane Marchesin25a26062014-09-12 16:18:59 -070056 if (!gbm)
57 return NULL;
58
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070059 gbm->drv = drv_create(fd);
60 if (!gbm->drv) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -070061 free(gbm);
62 return NULL;
63 }
64
Stéphane Marchesin25a26062014-09-12 16:18:59 -070065 return gbm;
66}
67
68PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
69{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070070 drv_destroy(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070071 free(gbm);
72}
73
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080074PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
Gurchetan Singha1892b22017-09-28 16:40:52 -070075 uint32_t height, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070076{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080077 struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
Stéphane Marchesinec88e892015-11-03 16:14:59 -080078
Stéphane Marchesin25a26062014-09-12 16:18:59 -070079 if (!surface)
80 return NULL;
81
82 return surface;
83}
84
Gurchetan Singh1cfdc312019-12-06 10:24:53 -080085PUBLIC struct gbm_surface *gbm_surface_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
86 uint32_t height, uint32_t format,
87 const uint64_t *modifiers,
88 const unsigned int count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070089{
Gurchetan Singh1cfdc312019-12-06 10:24:53 -080090 if (count != 0 || modifiers != NULL)
91 return NULL;
92
93 return gbm_surface_create(gbm, width, height, format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070094}
95
96PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
97{
98 return NULL;
99}
100
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800101PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700102{
103}
104
Gurchetan Singh1cfdc312019-12-06 10:24:53 -0800105PUBLIC int gbm_surface_has_free_buffers(struct gbm_surface *surface)
106{
107 return 0;
108}
109
110PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
111{
112 free(surface);
113}
114
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700116{
117 struct gbm_bo *bo;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700118
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800119 bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700120 if (!bo)
121 return NULL;
122
123 bo->gbm = gbm;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700124 bo->gbm_format = format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700125
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800126 return bo;
127}
128
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800129PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700130 uint32_t format, uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800131{
132 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800133
Gurchetan Singha1892b22017-09-28 16:40:52 -0700134 if (!gbm_device_is_format_supported(gbm, format, usage))
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500135 return NULL;
136
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700137 bo = gbm_bo_new(gbm, format);
138
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800139 if (!bo)
140 return NULL;
141
Gurchetan Singhc062c3b2019-06-20 20:48:01 -0700142 /*
Yiwei Zhangc1413ea2021-09-17 08:20:21 +0000143 * HACK: See b/132939420. This is for HAL_PIXEL_FORMAT_YV12 buffers allocated by arcvm. None
144 * of our platforms can display YV12, so we can treat as a SW buffer. Remove once this can
145 * be intelligently resolved in the guest. Also see virgl_resolve_use_flags.
Gurchetan Singhc062c3b2019-06-20 20:48:01 -0700146 */
147 if (format == GBM_FORMAT_YVU420 && (usage & GBM_BO_USE_LINEAR))
148 format = DRM_FORMAT_YVU420_ANDROID;
149
Gurchetan Singha1892b22017-09-28 16:40:52 -0700150 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700151
152 if (!bo->bo) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700153 free(bo);
154 return NULL;
155 }
156
157 return bo;
158}
159
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800160PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
161 uint32_t height, uint32_t format,
162 const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700163{
164 struct gbm_bo *bo;
165
166 bo = gbm_bo_new(gbm, format);
167
168 if (!bo)
169 return NULL;
170
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800171 bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700172
173 if (!bo->bo) {
174 free(bo);
175 return NULL;
176 }
177
178 return bo;
179}
180
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700181PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
182{
183 if (bo->destroy_user_data) {
184 bo->destroy_user_data(bo, bo->user_data);
185 bo->destroy_user_data = NULL;
186 bo->user_data = NULL;
187 }
188
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700189 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700190 free(bo);
191}
192
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800193PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
194 uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800195{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800196 struct gbm_bo *bo;
Gurchetan Singh99644382020-10-07 15:28:11 -0700197 struct drv_import_fd_data drv_data = { 0 };
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198 struct gbm_import_fd_data *fd_data = buffer;
Maksim Sisov4fe30382019-01-07 15:11:47 +0200199 struct gbm_import_fd_modifier_data *fd_modifier_data = buffer;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700200 uint32_t gbm_format;
Maksim Sisov4fe30382019-01-07 15:11:47 +0200201 size_t num_planes, i, num_fds;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800202
Gurchetan Singha1892b22017-09-28 16:40:52 -0700203 drv_data.use_flags = gbm_convert_usage(usage);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700204 switch (type) {
205 case GBM_BO_IMPORT_FD:
206 gbm_format = fd_data->format;
207 drv_data.width = fd_data->width;
208 drv_data.height = fd_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800209 drv_data.format = fd_data->format;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700210 drv_data.fds[0] = fd_data->fd;
211 drv_data.strides[0] = fd_data->stride;
Gurchetan Singh52155b42021-01-27 17:55:17 -0800212 drv_data.format_modifier = DRM_FORMAT_MOD_INVALID;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100213
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700214 break;
Maksim Sisov4fe30382019-01-07 15:11:47 +0200215 case GBM_BO_IMPORT_FD_MODIFIER:
216 gbm_format = fd_modifier_data->format;
217 drv_data.width = fd_modifier_data->width;
218 drv_data.height = fd_modifier_data->height;
219 drv_data.format = fd_modifier_data->format;
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100220 num_planes = drv_num_planes_from_modifier(gbm->drv, drv_data.format,
221 fd_modifier_data->modifier);
Mark Yacoub769de642019-07-09 13:59:50 -0400222 assert(num_planes);
Maksim Sisov4fe30382019-01-07 15:11:47 +0200223
Mark Yacoub769de642019-07-09 13:59:50 -0400224 num_fds = fd_modifier_data->num_fds;
225 if (!num_fds || num_fds > num_planes)
226 return NULL;
227
Gurchetan Singh52155b42021-01-27 17:55:17 -0800228 drv_data.format_modifier = fd_modifier_data->modifier;
Maksim Sisov4fe30382019-01-07 15:11:47 +0200229 for (i = 0; i < num_planes; i++) {
230 if (num_fds != num_planes)
231 drv_data.fds[i] = fd_modifier_data->fds[0];
232 else
233 drv_data.fds[i] = fd_modifier_data->fds[i];
234 drv_data.offsets[i] = fd_modifier_data->offsets[i];
235 drv_data.strides[i] = fd_modifier_data->strides[i];
Maksim Sisov4fe30382019-01-07 15:11:47 +0200236 }
237
238 for (i = num_planes; i < GBM_MAX_PLANES; i++)
239 drv_data.fds[i] = -1;
240
241 break;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700242 default:
243 return NULL;
244 }
245
246 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800247 return NULL;
248
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700249 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500250
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800251 if (!bo)
252 return NULL;
253
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800255
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700256 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800257 free(bo);
258 return NULL;
259 }
260
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800261 return bo;
262}
263
Gurchetan Singhdcdd2452020-03-24 11:05:03 -0700264PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
265 uint32_t transfer_flags, uint32_t *stride, void **map_data)
266{
267 return gbm_bo_map2(bo, x, y, width, height, transfer_flags, stride, map_data, 0);
268}
269
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800270PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700271{
272 assert(bo);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700273 drv_bo_flush_or_unmap(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700274}
275
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800276PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700277{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700278 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700279}
280
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800281PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700282{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700283 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700284}
285
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800286PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700287{
Keiichi Watanabe79155d72018-08-13 16:44:54 +0900288 return gbm_bo_get_stride_for_plane(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700289}
290
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800291PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700292{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700293 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700294}
295
Gurchetan Singh1cfdc312019-12-06 10:24:53 -0800296PUBLIC uint32_t gbm_bo_get_bpp(struct gbm_bo *bo)
297{
298 return drv_bytes_per_pixel_from_format(drv_bo_get_format(bo->bo), 0);
299}
300
Maksim Sisov79205a52018-08-03 16:31:20 +0300301PUBLIC uint64_t gbm_bo_get_modifier(struct gbm_bo *bo)
302{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800303 return drv_bo_get_format_modifier(bo->bo);
Vince Hsua6878fe2016-05-23 10:32:25 +0800304}
305
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800306PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700307{
308 return bo->gbm;
309}
310
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800311PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700312{
Maksim Sisovff1ecaf2018-08-10 14:53:34 +0300313 return gbm_bo_get_handle_for_plane(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700314}
315
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800316PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700317{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500318 return gbm_bo_get_plane_fd(bo, 0);
319}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800320
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800321PUBLIC int gbm_bo_get_plane_count(struct gbm_bo *bo)
Maksim Sisov79205a52018-08-03 16:31:20 +0300322{
Keiichi Watanabe79155d72018-08-13 16:44:54 +0900323 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500324}
325
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700326PUBLIC union gbm_bo_handle gbm_bo_get_handle_for_plane(struct gbm_bo *bo, size_t plane)
Maksim Sisovff1ecaf2018-08-10 14:53:34 +0300327{
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800328 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, (size_t)plane).u64;
Maksim Sisov79205a52018-08-03 16:31:20 +0300329}
330
Gurchetan Singh146ee022021-04-02 16:34:05 -0700331PUBLIC int gbm_bo_get_fd_for_plane(struct gbm_bo *bo, int plane)
332{
333 return drv_bo_get_plane_fd(bo->bo, plane);
334}
335
Maksim Sisov79205a52018-08-03 16:31:20 +0300336PUBLIC uint32_t gbm_bo_get_offset(struct gbm_bo *bo, size_t plane)
337{
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800338 return drv_bo_get_plane_offset(bo->bo, (size_t)plane);
Maksim Sisov79205a52018-08-03 16:31:20 +0300339}
340
341PUBLIC uint32_t gbm_bo_get_stride_for_plane(struct gbm_bo *bo, size_t plane)
342{
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800343 return drv_bo_get_plane_stride(bo->bo, (size_t)plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800344}
345
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800346PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
347 void (*destroy_user_data)(struct gbm_bo *, void *))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700348{
349 bo->user_data = data;
350 bo->destroy_user_data = destroy_user_data;
351}
352
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800353PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700354{
355 return bo->user_data;
356}
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800357
Gurchetan Singh1cfdc312019-12-06 10:24:53 -0800358/* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
359 * formats of the same name. We want to accept them whenever someone
360 * has a GBM format, but never return them to the user.
361 */
362static uint32_t gbm_format_canonicalize(uint32_t gbm_format)
363{
364 switch (gbm_format) {
365 case GBM_BO_FORMAT_XRGB8888:
366 return GBM_FORMAT_XRGB8888;
367 case GBM_BO_FORMAT_ARGB8888:
368 return GBM_FORMAT_ARGB8888;
369 default:
370 return gbm_format;
371 }
372}
373
374/**
375 * Returns a string representing the fourcc format name.
376 */
377PUBLIC char *gbm_format_get_name(uint32_t gbm_format, struct gbm_format_name_desc *desc)
378{
379 gbm_format = gbm_format_canonicalize(gbm_format);
380
381 desc->name[0] = gbm_format;
382 desc->name[1] = gbm_format >> 8;
383 desc->name[2] = gbm_format >> 16;
384 desc->name[3] = gbm_format >> 24;
385 desc->name[4] = 0;
386
387 return desc->name;
388}
389
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800390/*
391 * The following functions are not deprecated, but not in the Mesa the gbm
392 * header. The main difference is minigbm allows for the possibility of
393 * disjoint YUV images, while Mesa GBM does not.
394 */
395PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
396{
397 return drv_bo_get_plane_size(bo->bo, plane);
398}
399
400PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
401{
402 return drv_bo_get_plane_fd(bo->bo, plane);
403}
404
Gurchetan Singh064a36b2019-12-12 10:20:41 -0800405PUBLIC void *gbm_bo_map2(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
406 uint32_t transfer_flags, uint32_t *stride, void **map_data, int plane)
407{
Gurchetan Singhe32ec0b2019-12-06 09:52:03 -0800408 void *addr;
409 off_t offset;
410 uint32_t map_flags;
411 plane = (size_t)plane;
412 struct rectangle rect = { .x = x, .y = y, .width = width, .height = height };
413 if (!bo || width == 0 || height == 0 || !stride || !map_data)
414 return NULL;
415
416 map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
417 map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
418
419 addr = drv_bo_map(bo->bo, &rect, map_flags, (struct mapping **)map_data, plane);
420 if (addr == MAP_FAILED)
421 return MAP_FAILED;
422
423 *stride = ((struct mapping *)*map_data)->vma->map_strides[plane];
424
425 offset = *stride * rect.y;
426 offset += rect.x * drv_bytes_per_pixel_from_format(bo->gbm_format, plane);
427 return (void *)((uint8_t *)addr + offset);
428}