blob: 50110eae9e8f8d2625dc7dbec3bad77cc0c65368 [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * 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 uint32_t drv_format;
38 uint64_t drv_usage;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070039
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040 if (usage & GBM_BO_USE_CURSOR &&
41 usage & GBM_BO_USE_RENDERING)
42 return 0;
43
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070044 drv_format = gbm_convert_format(format);
45 drv_usage = gbm_convert_flags(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070046
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070047 return drv_is_format_supported(gbm->drv, drv_format, drv_usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070048}
49
50PUBLIC struct gbm_device *gbm_create_device(int fd)
51{
52 struct gbm_device *gbm;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070053
54 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
Stéphane Marchesinec88e892015-11-03 16:14:59 -080074PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm,
75 uint32_t width, uint32_t height,
76 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077{
Stéphane Marchesinec88e892015-11-03 16:14:59 -080078 struct gbm_surface *surface =
79 (struct gbm_surface*) malloc(sizeof(*surface));
80
Stéphane Marchesin25a26062014-09-12 16:18:59 -070081 if (!surface)
82 return NULL;
83
84 return surface;
85}
86
87PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
88{
89 free(surface);
90}
91
92PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
93{
94 return NULL;
95}
96
Stéphane Marchesinec88e892015-11-03 16:14:59 -080097PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface,
98 struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070099{
100}
101
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700103{
104 struct gbm_bo *bo;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700105
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500106 bo = (struct gbm_bo*) calloc(1, sizeof(*bo));
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700107 if (!bo)
108 return NULL;
109
110 bo->gbm = gbm;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700111 bo->gbm_format = format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700112
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800113 return bo;
114}
115
116PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width,
117 uint32_t height, uint32_t format,
118 uint32_t flags)
119{
120 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800121
Zach Reizner4fcc3c72016-02-25 11:44:36 -0800122 if (!gbm_device_is_format_supported(gbm, format, flags))
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500123 return NULL;
124
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700125 bo = gbm_bo_new(gbm, format);
126
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800127 if (!bo)
128 return NULL;
129
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130 bo->bo = drv_bo_create(gbm->drv, width, height,
131 gbm_convert_format(format),
132 gbm_convert_flags(flags));
133
134 if (!bo->bo) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700135 free(bo);
136 return NULL;
137 }
138
139 return bo;
140}
141
142PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
143{
144 if (bo->destroy_user_data) {
145 bo->destroy_user_data(bo, bo->user_data);
146 bo->destroy_user_data = NULL;
147 bo->user_data = NULL;
148 }
149
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700150 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151 free(bo);
152}
153
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800154PUBLIC struct gbm_bo *
155gbm_bo_import(struct gbm_device *gbm, uint32_t type,
156 void *buffer, uint32_t usage)
157{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800158 struct gbm_bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700159 struct drv_import_fd_data drv_data;
160 struct gbm_import_fd_data *fd_data = buffer;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800161
Mike Frysinger602bd162016-10-09 02:34:30 +0000162 if (type != GBM_BO_IMPORT_FD)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800163 return NULL;
164
165 if (!gbm_device_is_format_supported(gbm, fd_data->format, usage))
166 return NULL;
167
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168 bo = gbm_bo_new(gbm, fd_data->format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500169
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800170 if (!bo)
171 return NULL;
172
Mike Frysinger602bd162016-10-09 02:34:30 +0000173 /*
174 * Minigbm only supports importing single-plane formats at moment.
175 * If multi-plane import is desired, the interface will have to be
176 * modified.
177 */
178
179 memset(&drv_data, 0, sizeof(drv_data));
180 drv_data.fds[0] = fd_data->fd;
181 drv_data.strides[0] = fd_data->stride;
182 drv_data.offsets[0] = 0;
183 drv_data.sizes[0] = fd_data->height * fd_data->stride;
184 drv_data.width = fd_data->width;
185 drv_data.height = fd_data->height;
186 drv_data.format = gbm_convert_format(fd_data->format);
187
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700188 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800189
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800191 free(bo);
192 return NULL;
193 }
194
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800195 return bo;
196}
197
Gurchetan Singh5753c312016-10-05 15:16:22 -0700198PUBLIC void *
199gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width,
200 uint32_t height, uint32_t flags, uint32_t *stride, void **map_data,
201 size_t plane)
202{
203 if (!bo || width == 0 || height == 0 || !stride || !map_data)
204 return NULL;
205
Gurchetan Singh5753c312016-10-05 15:16:22 -0700206 *stride = gbm_bo_get_plane_stride(bo, plane);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700207 return drv_bo_map(bo->bo, x, y, width, height, 0, map_data, plane);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700208}
209
210PUBLIC void
211gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
212{
213 assert(bo);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700214 drv_bo_unmap(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700215}
216
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217PUBLIC uint32_t
218gbm_bo_get_width(struct gbm_bo *bo)
219{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700220 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700221}
222
223PUBLIC uint32_t
224gbm_bo_get_height(struct gbm_bo *bo)
225{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700226 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700227}
228
229PUBLIC uint32_t
230gbm_bo_get_stride(struct gbm_bo *bo)
231{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500232 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700233}
234
235PUBLIC uint32_t
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800236gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
237{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700238 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800239}
240
241PUBLIC uint32_t
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700242gbm_bo_get_format(struct gbm_bo *bo)
243{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700244 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700245}
246
Vince Hsua6878fe2016-05-23 10:32:25 +0800247PUBLIC uint64_t
248gbm_bo_get_format_modifier(struct gbm_bo *bo)
249{
250 return gbm_bo_get_plane_format_modifier(bo, 0);
251}
252
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700253PUBLIC struct gbm_device *
254gbm_bo_get_device(struct gbm_bo *bo)
255{
256 return bo->gbm;
257}
258
259PUBLIC union gbm_bo_handle
260gbm_bo_get_handle(struct gbm_bo *bo)
261{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500262 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700263}
264
265PUBLIC int
266gbm_bo_get_fd(struct gbm_bo *bo)
267{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500268 return gbm_bo_get_plane_fd(bo, 0);
269}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800270
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500271PUBLIC size_t
272gbm_bo_get_num_planes(struct gbm_bo *bo)
273{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700274 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500275}
276
277PUBLIC union gbm_bo_handle
278gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
279{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700280 return (union gbm_bo_handle) drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500281}
282
283PUBLIC int
284gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
285{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700286 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700287}
288
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500289PUBLIC uint32_t
290gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
291{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700292 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500293}
294
295PUBLIC uint32_t
296gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
297{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700298 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500299}
300
301PUBLIC uint32_t
302gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
303{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700304 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500305}
306
Vince Hsua6878fe2016-05-23 10:32:25 +0800307PUBLIC uint64_t
308gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
309{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700310 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800311}
312
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700313PUBLIC void
314gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
315 void (*destroy_user_data)(struct gbm_bo *, void *))
316{
317 bo->user_data = data;
318 bo->destroy_user_data = destroy_user_data;
319}
320
321PUBLIC void *
322gbm_bo_get_user_data(struct gbm_bo *bo)
323{
324 return bo->user_data;
325}