blob: 831dfbf5a7d6e3debbd7e16faf80b25970092c3b [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 Singh46faf6b2016-08-05 14:40:07 -070033 uint64_t drv_usage;
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 Singh46faf6b2016-08-05 14:40:07 -070038 drv_usage = gbm_convert_flags(usage);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070039
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080040 return (drv_get_combination(gbm->drv, format, drv_usage) != 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,
68 uint32_t height, uint32_t format, uint32_t flags)
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,
107 uint32_t format, uint32_t flags)
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800108{
109 struct gbm_bo *bo;
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800110
Zach Reizner4fcc3c72016-02-25 11:44:36 -0800111 if (!gbm_device_is_format_supported(gbm, format, flags))
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 Singh1b1d56a2017-03-10 16:25:23 -0800119 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_flags(flags));
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));
Satyajit Sahu766381d2017-09-21 15:14:46 +0530173 drv_data.flags = gbm_convert_flags(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;
182 drv_data.sizes[0] = fd_data->height * fd_data->stride;
183 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];
Gurchetan Singh2a119342016-11-02 10:40:51 -0700198
199 drv_data.sizes[i] = drv_size_from_format(
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800200 drv_data.format, drv_data.strides[i], drv_data.height, i);
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700201 }
Gurchetan Singh2a119342016-11-02 10:40:51 -0700202
203 for (i = num_planes; i < GBM_MAX_PLANES; i++)
204 drv_data.fds[i] = -1;
205
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700206 break;
207 default:
208 return NULL;
209 }
210
211 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800212 return NULL;
213
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700214 bo = gbm_bo_new(gbm, gbm_format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500215
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800216 if (!bo)
217 return NULL;
218
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219 bo->bo = drv_bo_import(gbm->drv, &drv_data);
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800220
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221 if (!bo->bo) {
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800222 free(bo);
223 return NULL;
224 }
225
Stéphane Marchesinf4bfdba2015-11-05 11:43:59 -0800226 return bo;
227}
228
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800229PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
230 uint32_t flags, uint32_t *stride, void **map_data, size_t plane)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700231{
232 if (!bo || width == 0 || height == 0 || !stride || !map_data)
233 return NULL;
234
Gurchetan Singh5753c312016-10-05 15:16:22 -0700235 *stride = gbm_bo_get_plane_stride(bo, plane);
Joe Kniss65705852017-06-29 15:02:46 -0700236 uint32_t drv_flags = flags & GBM_BO_TRANSFER_READ ? BO_TRANSFER_READ : BO_TRANSFER_NONE;
237 drv_flags |= flags & GBM_BO_TRANSFER_WRITE ? BO_TRANSFER_WRITE : BO_TRANSFER_NONE;
238 return drv_bo_map(bo->bo, x, y, width, height, drv_flags, (struct map_info **)map_data,
239 plane);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700240}
241
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800242PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
Gurchetan Singh5753c312016-10-05 15:16:22 -0700243{
244 assert(bo);
Gurchetan Singh254dbb12017-09-14 15:16:15 -0700245 drv_bo_flush(bo->bo, map_data);
Gurchetan Singh5753c312016-10-05 15:16:22 -0700246}
247
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800248PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700249{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700250 return drv_bo_get_width(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700251}
252
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800253PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700254{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700255 return drv_bo_get_height(bo->bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700256}
257
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800258PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700259{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500260 return gbm_bo_get_plane_stride(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700261}
262
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800263PUBLIC uint32_t gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800264{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700265 return drv_bo_get_stride_or_tiling(bo->bo);
Lauri Peltonen7842d8f2014-12-17 23:01:37 -0800266}
267
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800268PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700269{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270 return bo->gbm_format;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700271}
272
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800273PUBLIC uint64_t gbm_bo_get_format_modifier(struct gbm_bo *bo)
Vince Hsua6878fe2016-05-23 10:32:25 +0800274{
275 return gbm_bo_get_plane_format_modifier(bo, 0);
276}
277
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800278PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700279{
280 return bo->gbm;
281}
282
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800283PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700284{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500285 return gbm_bo_get_plane_handle(bo, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700286}
287
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800288PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700289{
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500290 return gbm_bo_get_plane_fd(bo, 0);
291}
Stéphane Marchesin5bb6adc2014-11-05 20:21:25 -0800292
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800293PUBLIC size_t gbm_bo_get_num_planes(struct gbm_bo *bo)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500294{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700295 return drv_bo_get_num_planes(bo->bo);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500296}
297
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800298PUBLIC union gbm_bo_handle gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500299{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800300 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, plane).u64;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500301}
302
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800303PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500304{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700305 return drv_bo_get_plane_fd(bo->bo, plane);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700306}
307
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800308PUBLIC uint32_t gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500309{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700310 return drv_bo_get_plane_offset(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500311}
312
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800313PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500314{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700315 return drv_bo_get_plane_size(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500316}
317
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800318PUBLIC uint32_t gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500319{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700320 return drv_bo_get_plane_stride(bo->bo, plane);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500321}
322
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800323PUBLIC uint64_t gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
Vince Hsua6878fe2016-05-23 10:32:25 +0800324{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700325 return drv_bo_get_plane_format_modifier(bo->bo, plane);
Vince Hsua6878fe2016-05-23 10:32:25 +0800326}
327
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800328PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
329 void (*destroy_user_data)(struct gbm_bo *, void *))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700330{
331 bo->user_data = data;
332 bo->destroy_user_data = destroy_user_data;
333}
334
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800335PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700336{
337 return bo->user_data;
338}