blob: 906560a1f73629b485395b37c29fa1bb0c77dd63 [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"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "gbm_helpers.h"
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080017#include "gbm_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070018#include "util.h"
19
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080020PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070021{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070022
23 return drv_get_fd(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070024}
25
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080026PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070027{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070028 return drv_get_name(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070029}
30
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080031PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070032{
Gurchetan Singha1892b22017-09-28 16:40:52 -070033 uint64_t use_flags;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070034
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080035 if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070036 return 0;
37
Gurchetan Singha1892b22017-09-28 16:40:52 -070038 use_flags = gbm_convert_usage(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070039
Gurchetan Singha1892b22017-09-28 16:40:52 -070040 return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070041}
42
43PUBLIC struct gbm_device *gbm_create_device(int fd)
44{
45 struct gbm_device *gbm;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070046
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080047 gbm = (struct gbm_device *)malloc(sizeof(*gbm));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070048
Stéphane Marchesin25a26062014-09-12 16:18:59 -070049 if (!gbm)
50 return NULL;
51
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070052 gbm->drv = drv_create(fd);
53 if (!gbm->drv) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -070054 free(gbm);
55 return NULL;
56 }
57
Stéphane Marchesin25a26062014-09-12 16:18:59 -070058 return gbm;
59}
60
61PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
62{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063 drv_destroy(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070064 free(gbm);
65}
66
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080067PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
Gurchetan Singha1892b22017-09-28 16:40:52 -070068 uint32_t height, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070069{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080070 struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
Stéphane Marchesinec88e892015-11-03 16:14:59 -080071
Stéphane Marchesin25a26062014-09-12 16:18:59 -070072 if (!surface)
73 return NULL;
74
75 return surface;
76}
77
78PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
79{
80 free(surface);
81}
82
83PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
84{
85 return NULL;
86}
87
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080088PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070089{
90}
91
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070092static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070093{
94 struct gbm_bo *bo;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070095
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080096 bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
Stéphane Marchesin25a26062014-09-12 16:18:59 -070097 if (!bo)
98 return NULL;
99
100 bo->gbm = gbm;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700101 bo->gbm_format = format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700102
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800103 return bo;
104}
105
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800106PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700107 uint32_t format, uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800108{
109 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800110
Gurchetan Singha1892b22017-09-28 16:40:52 -0700111 if (!gbm_device_is_format_supported(gbm, format, usage))
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500112 return NULL;
113
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700114 bo = gbm_bo_new(gbm, format);
115
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800116 if (!bo)
117 return NULL;
118
Gurchetan Singha1892b22017-09-28 16:40:52 -0700119 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700120
121 if (!bo->bo) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700122 free(bo);
123 return NULL;
124 }
125
126 return bo;
127}
128
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800129PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
130 uint32_t height, uint32_t format,
131 const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700132{
133 struct gbm_bo *bo;
134
135 bo = gbm_bo_new(gbm, format);
136
137 if (!bo)
138 return NULL;
139
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800140 bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700141
142 if (!bo->bo) {
143 free(bo);
144 return NULL;
145 }
146
147 return bo;
148}
149
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700150PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
151{
152 if (bo->destroy_user_data) {
153 bo->destroy_user_data(bo, bo->user_data);
154 bo->destroy_user_data = NULL;
155 bo->user_data = NULL;
156 }
157
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700158 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700159 free(bo);
160}
161
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800162PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
163 uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800164{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800165 struct gbm_bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700166 struct drv_import_fd_data drv_data;
167 struct gbm_import_fd_data *fd_data = buffer;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700168 struct gbm_import_fd_planar_data *fd_planar_data = buffer;
169 uint32_t gbm_format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700170 size_t num_planes, i;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800171
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700172 memset(&drv_data, 0, sizeof(drv_data));
Gurchetan Singha1892b22017-09-28 16:40:52 -0700173 drv_data.use_flags = gbm_convert_usage(usage);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700174 switch (type) {
175 case GBM_BO_IMPORT_FD:
176 gbm_format = fd_data->format;
177 drv_data.width = fd_data->width;
178 drv_data.height = fd_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800179 drv_data.format = fd_data->format;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700180 drv_data.fds[0] = fd_data->fd;
181 drv_data.strides[0] = fd_data->stride;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700182 break;
183 case GBM_BO_IMPORT_FD_PLANAR:
184 gbm_format = fd_planar_data->format;
185 drv_data.width = fd_planar_data->width;
186 drv_data.height = fd_planar_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800187 drv_data.format = fd_planar_data->format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700188 num_planes = drv_num_planes_from_format(drv_data.format);
189
190 assert(num_planes);
191
192 for (i = 0; i < num_planes; i++) {
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700193 drv_data.fds[i] = fd_planar_data->fds[i];
194 drv_data.offsets[i] = fd_planar_data->offsets[i];
195 drv_data.strides[i] = fd_planar_data->strides[i];
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800196 drv_data.format_modifiers[i] = fd_planar_data->format_modifiers[i];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700197 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700198
199 for (i = num_planes; i < GBM_MAX_PLANES; i++)
200 drv_data.fds[i] = -1;
201
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700202 break;
203 default:
204 return NULL;
205 }
206
207 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800208 return NULL;
209
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700210 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500211
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800212 if (!bo)
213 return NULL;
214
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700215 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800216
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800218 free(bo);
219 return NULL;
220 }
221
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800222 return bo;
223}
224
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800225PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700226 uint32_t transfer_flags, uint32_t *stride, void **map_data, size_t plane)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700227{
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700228 uint32_t map_flags;
Gurchetan Singh5753c312016-10-05 15:16:22 -0700229 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 Singhf7f633a2017-09-28 17:02:12 -0700233 map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
234 map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
235 return drv_bo_map(bo->bo, x, y, width, height, map_flags, (struct map_info **)map_data,
Joe Kniss65705852017-06-29 15:02:46 -0700236 plane);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700237}
238
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800239PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700240{
241 assert(bo);
Gurchetan Singh254dbb12017-09-14 15:16:15 -0700242 drv_bo_flush(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700243}
244
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800245PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700246{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700248}
249
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800250PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700251{
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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800255PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700256{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500257 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700258}
259
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800260PUBLIC uint32_t gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800261{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700262 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800263}
264
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800265PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700266{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700267 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700268}
269
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800270PUBLIC uint64_t gbm_bo_get_format_modifier(struct gbm_bo *bo)
Vince Hsua6878fe2016-05-23 10:32:25 +0800271{
272 return gbm_bo_get_plane_format_modifier(bo, 0);
273}
274
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800275PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700276{
277 return bo->gbm;
278}
279
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800280PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700281{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500282 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700283}
284
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800285PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700286{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500287 return gbm_bo_get_plane_fd(bo, 0);
288}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800289
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800290PUBLIC size_t gbm_bo_get_num_planes(struct gbm_bo *bo)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500291{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700292 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500293}
294
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800295PUBLIC union gbm_bo_handle gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500296{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800297 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500298}
299
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800300PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500301{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700302 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700303}
304
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800305PUBLIC uint32_t gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500306{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700307 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500308}
309
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800310PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500311{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700312 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500313}
314
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800315PUBLIC uint32_t gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500316{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700317 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500318}
319
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800320PUBLIC uint64_t gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
Vince Hsua6878fe2016-05-23 10:32:25 +0800321{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700322 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800323}
324
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800325PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
326 void (*destroy_user_data)(struct gbm_bo *, void *))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700327{
328 bo->user_data = data;
329 bo->destroy_user_data = destroy_user_data;
330}
331
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800332PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700333{
334 return bo->user_data;
335}