blob: 7389cd8a1b06e5948e574fae2894e54d0a54dd43 [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 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
140PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
141{
142 if (bo->destroy_user_data) {
143 bo->destroy_user_data(bo, bo->user_data);
144 bo->destroy_user_data = NULL;
145 bo->user_data = NULL;
146 }
147
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700148 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700149 free(bo);
150}
151
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800152PUBLIC struct gbm_bo *
153gbm_bo_import(struct gbm_device *gbm, uint32_t type,
154 void *buffer, uint32_t usage)
155{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800156 struct gbm_bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700157 struct drv_import_fd_data drv_data;
158 struct gbm_import_fd_data *fd_data = buffer;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700159 struct gbm_import_fd_planar_data *fd_planar_data = buffer;
160 uint32_t gbm_format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700161 size_t num_planes, i;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800162
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700163 memset(&drv_data, 0, sizeof(drv_data));
164
165 switch (type) {
166 case GBM_BO_IMPORT_FD:
167 gbm_format = fd_data->format;
168 drv_data.width = fd_data->width;
169 drv_data.height = fd_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800170 drv_data.format = fd_data->format;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700171 drv_data.fds[0] = fd_data->fd;
172 drv_data.strides[0] = fd_data->stride;
173 drv_data.sizes[0] = fd_data->height * fd_data->stride;
174 break;
175 case GBM_BO_IMPORT_FD_PLANAR:
176 gbm_format = fd_planar_data->format;
177 drv_data.width = fd_planar_data->width;
178 drv_data.height = fd_planar_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800179 drv_data.format = fd_planar_data->format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700180 num_planes = drv_num_planes_from_format(drv_data.format);
181
182 assert(num_planes);
183
184 for (i = 0; i < num_planes; i++) {
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700185 drv_data.fds[i] = fd_planar_data->fds[i];
186 drv_data.offsets[i] = fd_planar_data->offsets[i];
187 drv_data.strides[i] = fd_planar_data->strides[i];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700188 drv_data.format_modifiers[i] =
189 fd_planar_data->format_modifiers[i];
Gurchetan Singh2a119342016-11-02 10:40:51 -0700190
191 drv_data.sizes[i] = drv_size_from_format(
192 drv_data.format,
193 drv_data.strides[i],
194 drv_data.height,
195 i);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700196 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700197
198 for (i = num_planes; i < GBM_MAX_PLANES; i++)
199 drv_data.fds[i] = -1;
200
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700201 break;
202 default:
203 return NULL;
204 }
205
206 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800207 return NULL;
208
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700209 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500210
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800211 if (!bo)
212 return NULL;
213
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800215
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800217 free(bo);
218 return NULL;
219 }
220
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800221 return bo;
222}
223
Gurchetan Singh5753c312016-10-05 15:16:22 -0700224PUBLIC void *
225gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width,
226 uint32_t height, uint32_t flags, uint32_t *stride, void **map_data,
227 size_t plane)
228{
229 if (!bo || width == 0 || height == 0 || !stride || !map_data)
230 return NULL;
231
Gurchetan Singh5753c312016-10-05 15:16:22 -0700232 *stride = gbm_bo_get_plane_stride(bo, plane);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700233 return drv_bo_map(bo->bo, x, y, width, height, 0, map_data, plane);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700234}
235
236PUBLIC void
237gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
238{
239 assert(bo);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700240 drv_bo_unmap(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700241}
242
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700243PUBLIC uint32_t
244gbm_bo_get_width(struct gbm_bo *bo)
245{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700247}
248
249PUBLIC uint32_t
250gbm_bo_get_height(struct gbm_bo *bo)
251{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700253}
254
255PUBLIC uint32_t
256gbm_bo_get_stride(struct gbm_bo *bo)
257{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500258 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700259}
260
261PUBLIC uint32_t
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800262gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
263{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700264 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800265}
266
267PUBLIC uint32_t
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700268gbm_bo_get_format(struct gbm_bo *bo)
269{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700271}
272
Vince Hsua6878fe2016-05-23 10:32:25 +0800273PUBLIC uint64_t
274gbm_bo_get_format_modifier(struct gbm_bo *bo)
275{
276 return gbm_bo_get_plane_format_modifier(bo, 0);
277}
278
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700279PUBLIC struct gbm_device *
280gbm_bo_get_device(struct gbm_bo *bo)
281{
282 return bo->gbm;
283}
284
285PUBLIC union gbm_bo_handle
286gbm_bo_get_handle(struct gbm_bo *bo)
287{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500288 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700289}
290
291PUBLIC int
292gbm_bo_get_fd(struct gbm_bo *bo)
293{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500294 return gbm_bo_get_plane_fd(bo, 0);
295}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800296
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500297PUBLIC size_t
298gbm_bo_get_num_planes(struct gbm_bo *bo)
299{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700300 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500301}
302
303PUBLIC union gbm_bo_handle
304gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
305{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700306 return (union gbm_bo_handle) drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500307}
308
309PUBLIC int
310gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
311{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700312 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700313}
314
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500315PUBLIC uint32_t
316gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
317{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700318 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500319}
320
321PUBLIC uint32_t
322gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
323{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700324 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500325}
326
327PUBLIC uint32_t
328gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
329{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700330 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500331}
332
Vince Hsua6878fe2016-05-23 10:32:25 +0800333PUBLIC uint64_t
334gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
335{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700336 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800337}
338
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700339PUBLIC void
340gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
341 void (*destroy_user_data)(struct gbm_bo *, void *))
342{
343 bo->user_data = data;
344 bo->destroy_user_data = destroy_user_data;
345}
346
347PUBLIC void *
348gbm_bo_get_user_data(struct gbm_bo *bo)
349{
350 return bo->user_data;
351}