blob: 6ad32ce6bbe2947c017660cce3f7e1401e4e096a [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>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <xf86drm.h>
14
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070015#include "drv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070016#include "gbm_priv.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "gbm_helpers.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070018#include "util.h"
19
Stéphane Marchesin25a26062014-09-12 16:18:59 -070020PUBLIC int
21gbm_device_get_fd(struct gbm_device *gbm)
22{
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
27PUBLIC const char *
28gbm_device_get_backend_name(struct gbm_device *gbm)
29{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070030 return drv_get_name(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070031}
32
33PUBLIC int
34gbm_device_is_format_supported(struct gbm_device *gbm,
Stéphane Marchesinec88e892015-11-03 16:14:59 -080035 uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070036{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070037 uint64_t drv_usage;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070038
Stéphane Marchesin25a26062014-09-12 16:18:59 -070039 if (usage & GBM_BO_USE_CURSOR &&
40 usage & GBM_BO_USE_RENDERING)
41 return 0;
42
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070043 drv_usage = gbm_convert_flags(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070044
Gurchetan Singh179687e2016-10-28 10:07:35 -070045 return drv_is_combination_supported(gbm->drv, format, drv_usage,
46 DRM_FORMAT_MOD_NONE);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070047}
48
49PUBLIC struct gbm_device *gbm_create_device(int fd)
50{
51 struct gbm_device *gbm;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070052
53 gbm = (struct gbm_device*) malloc(sizeof(*gbm));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070054
Stéphane Marchesin25a26062014-09-12 16:18:59 -070055 if (!gbm)
56 return NULL;
57
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058 gbm->drv = drv_create(fd);
59 if (!gbm->drv) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -070060 free(gbm);
61 return NULL;
62 }
63
Stéphane Marchesin25a26062014-09-12 16:18:59 -070064 return gbm;
65}
66
67PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
68{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070069 drv_destroy(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070070 free(gbm);
71}
72
Stéphane Marchesinec88e892015-11-03 16:14:59 -080073PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm,
74 uint32_t width, uint32_t height,
75 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070076{
Stéphane Marchesinec88e892015-11-03 16:14:59 -080077 struct gbm_surface *surface =
78 (struct gbm_surface*) malloc(sizeof(*surface));
79
Stéphane Marchesin25a26062014-09-12 16:18:59 -070080 if (!surface)
81 return NULL;
82
83 return surface;
84}
85
86PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
87{
88 free(surface);
89}
90
91PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
92{
93 return NULL;
94}
95
Stéphane Marchesinec88e892015-11-03 16:14:59 -080096PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface,
97 struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070098{
99}
100
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700101static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700102{
103 struct gbm_bo *bo;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700104
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500105 bo = (struct gbm_bo*) calloc(1, sizeof(*bo));
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700106 if (!bo)
107 return NULL;
108
109 bo->gbm = gbm;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700110 bo->gbm_format = format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700111
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800112 return bo;
113}
114
115PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width,
116 uint32_t height, uint32_t format,
117 uint32_t flags)
118{
119 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800120
Zach Reizner4fcc3c72016-02-25 11:44:36 -0800121 if (!gbm_device_is_format_supported(gbm, format, flags))
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500122 return NULL;
123
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700124 bo = gbm_bo_new(gbm, format);
125
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800126 if (!bo)
127 return NULL;
128
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800129 bo->bo = drv_bo_create(gbm->drv, width, height, format,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130 gbm_convert_flags(flags));
131
132 if (!bo->bo) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700133 free(bo);
134 return NULL;
135 }
136
137 return bo;
138}
139
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700140PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm,
141 uint32_t width,
142 uint32_t height,
143 uint32_t format,
144 const uint64_t *modifiers,
145 uint32_t count)
146{
147 struct gbm_bo *bo;
148
149 bo = gbm_bo_new(gbm, format);
150
151 if (!bo)
152 return NULL;
153
154 bo->bo = drv_bo_create_with_modifiers(gbm->drv,
155 width, height, format,
156 modifiers, count);
157
158 if (!bo->bo) {
159 free(bo);
160 return NULL;
161 }
162
163 return bo;
164}
165
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700166PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
167{
168 if (bo->destroy_user_data) {
169 bo->destroy_user_data(bo, bo->user_data);
170 bo->destroy_user_data = NULL;
171 bo->user_data = NULL;
172 }
173
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700175 free(bo);
176}
177
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800178PUBLIC struct gbm_bo *
179gbm_bo_import(struct gbm_device *gbm, uint32_t type,
180 void *buffer, uint32_t usage)
181{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800182 struct gbm_bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700183 struct drv_import_fd_data drv_data;
184 struct gbm_import_fd_data *fd_data = buffer;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700185 struct gbm_import_fd_planar_data *fd_planar_data = buffer;
186 uint32_t gbm_format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700187 size_t num_planes, i;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800188
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700189 memset(&drv_data, 0, sizeof(drv_data));
190
191 switch (type) {
192 case GBM_BO_IMPORT_FD:
193 gbm_format = fd_data->format;
194 drv_data.width = fd_data->width;
195 drv_data.height = fd_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800196 drv_data.format = fd_data->format;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700197 drv_data.fds[0] = fd_data->fd;
198 drv_data.strides[0] = fd_data->stride;
199 drv_data.sizes[0] = fd_data->height * fd_data->stride;
200 break;
201 case GBM_BO_IMPORT_FD_PLANAR:
202 gbm_format = fd_planar_data->format;
203 drv_data.width = fd_planar_data->width;
204 drv_data.height = fd_planar_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800205 drv_data.format = fd_planar_data->format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700206 num_planes = drv_num_planes_from_format(drv_data.format);
207
208 assert(num_planes);
209
210 for (i = 0; i < num_planes; i++) {
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700211 drv_data.fds[i] = fd_planar_data->fds[i];
212 drv_data.offsets[i] = fd_planar_data->offsets[i];
213 drv_data.strides[i] = fd_planar_data->strides[i];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700214 drv_data.format_modifiers[i] =
215 fd_planar_data->format_modifiers[i];
Gurchetan Singh2a119342016-11-02 10:40:51 -0700216
217 drv_data.sizes[i] = drv_size_from_format(
218 drv_data.format,
219 drv_data.strides[i],
220 drv_data.height,
221 i);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700222 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700223
224 for (i = num_planes; i < GBM_MAX_PLANES; i++)
225 drv_data.fds[i] = -1;
226
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700227 break;
228 default:
229 return NULL;
230 }
231
232 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800233 return NULL;
234
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700235 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500236
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800237 if (!bo)
238 return NULL;
239
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700240 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800241
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800243 free(bo);
244 return NULL;
245 }
246
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800247 return bo;
248}
249
Gurchetan Singh5753c312016-10-05 15:16:22 -0700250PUBLIC void *
251gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width,
252 uint32_t height, uint32_t flags, uint32_t *stride, void **map_data,
253 size_t plane)
254{
255 if (!bo || width == 0 || height == 0 || !stride || !map_data)
256 return NULL;
257
Gurchetan Singh5753c312016-10-05 15:16:22 -0700258 *stride = gbm_bo_get_plane_stride(bo, plane);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700259 return drv_bo_map(bo->bo, x, y, width, height, 0, map_data, plane);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700260}
261
262PUBLIC void
263gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
264{
265 assert(bo);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700266 drv_bo_unmap(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700267}
268
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700269PUBLIC uint32_t
270gbm_bo_get_width(struct gbm_bo *bo)
271{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700272 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700273}
274
275PUBLIC uint32_t
276gbm_bo_get_height(struct gbm_bo *bo)
277{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700278 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700279}
280
281PUBLIC uint32_t
282gbm_bo_get_stride(struct gbm_bo *bo)
283{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500284 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700285}
286
287PUBLIC uint32_t
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800288gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
289{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700290 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800291}
292
293PUBLIC uint32_t
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700294gbm_bo_get_format(struct gbm_bo *bo)
295{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700296 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700297}
298
Vince Hsua6878fe2016-05-23 10:32:25 +0800299PUBLIC uint64_t
300gbm_bo_get_format_modifier(struct gbm_bo *bo)
301{
302 return gbm_bo_get_plane_format_modifier(bo, 0);
303}
304
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700305PUBLIC struct gbm_device *
306gbm_bo_get_device(struct gbm_bo *bo)
307{
308 return bo->gbm;
309}
310
311PUBLIC union gbm_bo_handle
312gbm_bo_get_handle(struct gbm_bo *bo)
313{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500314 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700315}
316
317PUBLIC int
318gbm_bo_get_fd(struct gbm_bo *bo)
319{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500320 return gbm_bo_get_plane_fd(bo, 0);
321}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800322
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500323PUBLIC size_t
324gbm_bo_get_num_planes(struct gbm_bo *bo)
325{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700326 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500327}
328
329PUBLIC union gbm_bo_handle
330gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
331{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700332 return (union gbm_bo_handle) drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500333}
334
335PUBLIC int
336gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
337{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700338 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700339}
340
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500341PUBLIC uint32_t
342gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
343{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500345}
346
347PUBLIC uint32_t
348gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
349{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700350 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500351}
352
353PUBLIC uint32_t
354gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
355{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700356 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500357}
358
Vince Hsua6878fe2016-05-23 10:32:25 +0800359PUBLIC uint64_t
360gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
361{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700362 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800363}
364
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700365PUBLIC void
366gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
367 void (*destroy_user_data)(struct gbm_bo *, void *))
368{
369 bo->user_data = data;
370 bo->destroy_user_data = destroy_user_data;
371}
372
373PUBLIC void *
374gbm_bo_get_user_data(struct gbm_bo *bo)
375{
376 return bo->user_data;
377}