blob: 25b4fa0bc6845d6b0c16c44e08aca0b7996f9961 [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>
Gurchetan Singh1ef809e2017-11-06 11:07:52 -080013#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <xf86drm.h>
15
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "gbm_helpers.h"
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080018#include "gbm_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070019#include "util.h"
20
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080021PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070022{
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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080027PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070028{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070029 return drv_get_name(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070030}
31
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080032PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070033{
Gurchetan Singha1892b22017-09-28 16:40:52 -070034 uint64_t use_flags;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070035
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080036 if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070037 return 0;
38
Gurchetan Singha1892b22017-09-28 16:40:52 -070039 use_flags = gbm_convert_usage(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040
Gurchetan Singha1892b22017-09-28 16:40:52 -070041 return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070042}
43
44PUBLIC struct gbm_device *gbm_create_device(int fd)
45{
46 struct gbm_device *gbm;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070047
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080048 gbm = (struct gbm_device *)malloc(sizeof(*gbm));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049
Stéphane Marchesin25a26062014-09-12 16:18:59 -070050 if (!gbm)
51 return NULL;
52
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070053 gbm->drv = drv_create(fd);
54 if (!gbm->drv) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -070055 free(gbm);
56 return NULL;
57 }
58
Stéphane Marchesin25a26062014-09-12 16:18:59 -070059 return gbm;
60}
61
62PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
63{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070064 drv_destroy(gbm->drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070065 free(gbm);
66}
67
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080068PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
Gurchetan Singha1892b22017-09-28 16:40:52 -070069 uint32_t height, uint32_t format, uint32_t usage)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070070{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080071 struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
Stéphane Marchesinec88e892015-11-03 16:14:59 -080072
Stéphane Marchesin25a26062014-09-12 16:18:59 -070073 if (!surface)
74 return NULL;
75
76 return surface;
77}
78
79PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
80{
81 free(surface);
82}
83
84PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
85{
86 return NULL;
87}
88
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080089PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070090{
91}
92
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070093static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070094{
95 struct gbm_bo *bo;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070096
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080097 bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
Stéphane Marchesin25a26062014-09-12 16:18:59 -070098 if (!bo)
99 return NULL;
100
101 bo->gbm = gbm;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102 bo->gbm_format = format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700103
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800104 return bo;
105}
106
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800107PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700108 uint32_t format, uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800109{
110 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800111
Gurchetan Singha1892b22017-09-28 16:40:52 -0700112 if (!gbm_device_is_format_supported(gbm, format, usage))
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500113 return NULL;
114
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 bo = gbm_bo_new(gbm, format);
116
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800117 if (!bo)
118 return NULL;
119
Gurchetan Singha1892b22017-09-28 16:40:52 -0700120 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700121
122 if (!bo->bo) {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700123 free(bo);
124 return NULL;
125 }
126
127 return bo;
128}
129
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800130PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
131 uint32_t height, uint32_t format,
132 const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700133{
134 struct gbm_bo *bo;
135
136 bo = gbm_bo_new(gbm, format);
137
138 if (!bo)
139 return NULL;
140
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800141 bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700142
143 if (!bo->bo) {
144 free(bo);
145 return NULL;
146 }
147
148 return bo;
149}
150
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
152{
153 if (bo->destroy_user_data) {
154 bo->destroy_user_data(bo, bo->user_data);
155 bo->destroy_user_data = NULL;
156 bo->user_data = NULL;
157 }
158
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700159 drv_bo_destroy(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700160 free(bo);
161}
162
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800163PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
164 uint32_t usage)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800165{
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800166 struct gbm_bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700167 struct drv_import_fd_data drv_data;
168 struct gbm_import_fd_data *fd_data = buffer;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700169 struct gbm_import_fd_planar_data *fd_planar_data = buffer;
170 uint32_t gbm_format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700171 size_t num_planes, i;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800172
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700173 memset(&drv_data, 0, sizeof(drv_data));
Gurchetan Singha1892b22017-09-28 16:40:52 -0700174 drv_data.use_flags = gbm_convert_usage(usage);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700175 switch (type) {
176 case GBM_BO_IMPORT_FD:
177 gbm_format = fd_data->format;
178 drv_data.width = fd_data->width;
179 drv_data.height = fd_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800180 drv_data.format = fd_data->format;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700181 drv_data.fds[0] = fd_data->fd;
182 drv_data.strides[0] = fd_data->stride;
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700183 break;
184 case GBM_BO_IMPORT_FD_PLANAR:
185 gbm_format = fd_planar_data->format;
186 drv_data.width = fd_planar_data->width;
187 drv_data.height = fd_planar_data->height;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800188 drv_data.format = fd_planar_data->format;
Gurchetan Singh2a119342016-11-02 10:40:51 -0700189 num_planes = drv_num_planes_from_format(drv_data.format);
190
191 assert(num_planes);
192
193 for (i = 0; i < num_planes; i++) {
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700194 drv_data.fds[i] = fd_planar_data->fds[i];
195 drv_data.offsets[i] = fd_planar_data->offsets[i];
196 drv_data.strides[i] = fd_planar_data->strides[i];
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800197 drv_data.format_modifiers[i] = fd_planar_data->format_modifiers[i];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700198 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700199
200 for (i = num_planes; i < GBM_MAX_PLANES; i++)
201 drv_data.fds[i] = -1;
202
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700203 break;
204 default:
205 return NULL;
206 }
207
208 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800209 return NULL;
210
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700211 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500212
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800213 if (!bo)
214 return NULL;
215
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800217
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800219 free(bo);
220 return NULL;
221 }
222
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800223 return bo;
224}
225
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800226PUBLIC 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 -0700227 uint32_t transfer_flags, uint32_t *stride, void **map_data, size_t plane)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700228{
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800229 void *addr;
230 off_t offset;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700231 uint32_t map_flags;
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800232 struct rectangle rect = { .x = x, .y = y, .width = width, .height = height };
Gurchetan Singh5753c312016-10-05 15:16:22 -0700233 if (!bo || width == 0 || height == 0 || !stride || !map_data)
234 return NULL;
235
Gurchetan Singh5753c312016-10-05 15:16:22 -0700236 *stride = gbm_bo_get_plane_stride(bo, plane);
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700237 map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
238 map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800239
240 addr = drv_bo_map(bo->bo, &rect, map_flags, (struct mapping **)map_data, plane);
241 if (addr == MAP_FAILED)
242 return MAP_FAILED;
243
244 offset = gbm_bo_get_plane_stride(bo, plane) * rect.y;
245 offset += drv_stride_from_format(bo->gbm_format, rect.x, plane);
246 return (void *)((uint8_t *)addr + offset);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700247}
248
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800249PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700250{
251 assert(bo);
Gurchetan Singh07d36cc2017-10-10 12:11:16 -0700252 drv_bo_flush(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700253}
254
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800255PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700256{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700257 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700258}
259
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800260PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700261{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700262 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700263}
264
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800265PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700266{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500267 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700268}
269
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800270PUBLIC uint32_t gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800271{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700272 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800273}
274
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800275PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700276{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700277 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700278}
279
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800280PUBLIC uint64_t gbm_bo_get_format_modifier(struct gbm_bo *bo)
Vince Hsua6878fe2016-05-23 10:32:25 +0800281{
282 return gbm_bo_get_plane_format_modifier(bo, 0);
283}
284
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800285PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700286{
287 return bo->gbm;
288}
289
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800290PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700291{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500292 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700293}
294
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800295PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700296{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500297 return gbm_bo_get_plane_fd(bo, 0);
298}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800299
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800300PUBLIC size_t gbm_bo_get_num_planes(struct gbm_bo *bo)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500301{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700302 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500303}
304
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800305PUBLIC union gbm_bo_handle gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500306{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800307 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500308}
309
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800310PUBLIC int gbm_bo_get_plane_fd(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_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700313}
314
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800315PUBLIC uint32_t gbm_bo_get_plane_offset(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_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500318}
319
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800320PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500321{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700322 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500323}
324
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800325PUBLIC uint32_t gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500326{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700327 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500328}
329
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800330PUBLIC uint64_t gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
Vince Hsua6878fe2016-05-23 10:32:25 +0800331{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700332 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800333}
334
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800335PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
336 void (*destroy_user_data)(struct gbm_bo *, void *))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700337{
338 bo->user_data = data;
339 bo->destroy_user_data = destroy_user_data;
340}
341
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800342PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700343{
344 return bo->user_data;
345}