blob: 077ff23850e2a7928dee3a48b4a08b14d4f42049 [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
162 if (type != GBM_BO_IMPORT_FD)
163 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
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700173 drv_data.fd = fd_data->fd;
174 drv_data.width = fd_data->width;
175 drv_data.height = fd_data->height;
176 drv_data.format = gbm_convert_format(fd_data->format);
177 drv_data.stride = fd_data->stride;
Stéphane Marchesine04c9b52016-03-11 22:01:35 -0800178
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700179 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800180
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800182 free(bo);
183 return NULL;
184 }
185
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800186 return bo;
187}
188
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700189PUBLIC uint32_t
190gbm_bo_get_width(struct gbm_bo *bo)
191{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700193}
194
195PUBLIC uint32_t
196gbm_bo_get_height(struct gbm_bo *bo)
197{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700199}
200
201PUBLIC uint32_t
202gbm_bo_get_stride(struct gbm_bo *bo)
203{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500204 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700205}
206
207PUBLIC uint32_t
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800208gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
209{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700210 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800211}
212
213PUBLIC uint32_t
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214gbm_bo_get_format(struct gbm_bo *bo)
215{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217}
218
Vince Hsua6878fe2016-05-23 10:32:25 +0800219PUBLIC uint64_t
220gbm_bo_get_format_modifier(struct gbm_bo *bo)
221{
222 return gbm_bo_get_plane_format_modifier(bo, 0);
223}
224
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700225PUBLIC struct gbm_device *
226gbm_bo_get_device(struct gbm_bo *bo)
227{
228 return bo->gbm;
229}
230
231PUBLIC union gbm_bo_handle
232gbm_bo_get_handle(struct gbm_bo *bo)
233{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500234 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700235}
236
237PUBLIC int
238gbm_bo_get_fd(struct gbm_bo *bo)
239{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500240 return gbm_bo_get_plane_fd(bo, 0);
241}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800242
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500243PUBLIC size_t
244gbm_bo_get_num_planes(struct gbm_bo *bo)
245{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500247}
248
249PUBLIC union gbm_bo_handle
250gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
251{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252 return (union gbm_bo_handle) drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500253}
254
255PUBLIC int
256gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
257{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700258 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700259}
260
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500261PUBLIC uint32_t
262gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
263{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700264 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500265}
266
267PUBLIC uint32_t
268gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
269{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500271}
272
273PUBLIC uint32_t
274gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
275{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700276 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500277}
278
Vince Hsua6878fe2016-05-23 10:32:25 +0800279PUBLIC uint64_t
280gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
281{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700282 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800283}
284
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700285PUBLIC void
286gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
287 void (*destroy_user_data)(struct gbm_bo *, void *))
288{
289 bo->user_data = data;
290 bo->destroy_user_data = destroy_user_data;
291}
292
293PUBLIC void *
294gbm_bo_get_user_data(struct gbm_bo *bo)
295{
296 return bo->user_data;
297}