OSDN Git Service

de5b87fa5bb3a896a4c0ceaea2f81db89fa6d72a
[android-x86/external-minigbm.git] / drv.c
1 /*
2  * Copyright (c) 2016 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 #include <assert.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <xf86drm.h>
14
15 #include "drv_priv.h"
16 #include "helpers.h"
17 #include "util.h"
18
19 extern struct backend backend_cirrus;
20 extern struct backend backend_evdi;
21 #ifdef DRV_EXYNOS
22 extern struct backend backend_exynos;
23 #endif
24 extern struct backend backend_gma500;
25 #ifdef DRV_I915
26 extern struct backend backend_i915;
27 #endif
28 #ifdef DRV_MARVELL
29 extern struct backend backend_marvell;
30 #endif
31 #ifdef DRV_MEDIATEK
32 extern struct backend backend_mediatek;
33 #endif
34 #ifdef DRV_ROCKCHIP
35 extern struct backend backend_rockchip;
36 #endif
37 #ifdef DRV_TEGRA
38 extern struct backend backend_tegra;
39 #endif
40 extern struct backend backend_udl;
41 extern struct backend backend_virtio_gpu;
42
43 static struct backend *drv_get_backend(int fd)
44 {
45         drmVersionPtr drm_version;
46         unsigned int i;
47
48         drm_version = drmGetVersion(fd);
49
50         if (!drm_version)
51                 return NULL;
52
53         struct backend *backend_list[] = {
54                 &backend_cirrus,
55                 &backend_evdi,
56 #ifdef DRV_EXYNOS
57                 &backend_exynos,
58 #endif
59                 &backend_gma500,
60 #ifdef DRV_I915
61                 &backend_i915,
62 #endif
63 #ifdef DRV_MARVELL
64                 &backend_marvell,
65 #endif
66 #ifdef DRV_MEDIATEK
67                 &backend_mediatek,
68 #endif
69 #ifdef DRV_ROCKCHIP
70                 &backend_rockchip,
71 #endif
72 #ifdef DRV_TEGRA
73                 &backend_tegra,
74 #endif
75                 &backend_udl,
76                 &backend_virtio_gpu,
77         };
78
79         for(i = 0; i < ARRAY_SIZE(backend_list); i++)
80                 if (!strcmp(drm_version->name, backend_list[i]->name)) {
81                         drmFreeVersion(drm_version);
82                         return backend_list[i];
83                 }
84
85         drmFreeVersion(drm_version);
86         return NULL;
87 }
88
89 struct driver *drv_create(int fd)
90 {
91         struct driver *drv;
92         int ret;
93
94         drv = (struct driver *) calloc(1, sizeof(*drv));
95
96         if (!drv)
97                 return NULL;
98
99         drv->fd = fd;
100         drv->backend = drv_get_backend(fd);
101
102         if (!drv->backend) {
103                 free(drv);
104                 return NULL;
105         }
106
107         if (pthread_mutex_init(&drv->table_lock, NULL)) {
108                 free(drv);
109                 return NULL;
110         }
111
112         drv->buffer_table = drmHashCreate();
113         if (!drv->buffer_table) {
114                 free(drv);
115                 return NULL;
116         }
117
118         if (drv->backend->init) {
119                 ret = drv->backend->init(drv);
120                 if (ret) {
121                         free(drv);
122                         return NULL;
123                 }
124         }
125
126         return drv;
127 }
128
129 void drv_destroy(struct driver *drv)
130 {
131         if (drv->backend->close)
132                 drv->backend->close(drv);
133
134         pthread_mutex_destroy(&drv->table_lock);
135         drmHashDestroy(drv->buffer_table);
136
137         free(drv);
138 }
139
140 int drv_get_fd(struct driver *drv)
141 {
142         return drv->fd;
143 }
144
145 const char *
146 drv_get_name(struct driver *drv)
147 {
148         return drv->backend->name;
149 }
150
151 int drv_is_format_supported(struct driver *drv, drv_format_t format,
152                             uint64_t usage)
153 {
154         unsigned int i;
155
156         if (format == DRV_FORMAT_NONE || usage == DRV_BO_USE_NONE)
157                 return 0;
158
159         for (i = 0 ; i < ARRAY_SIZE(drv->backend->format_list); i++)
160         {
161                 if (!drv->backend->format_list[i].format)
162                         break;
163
164                 if (drv->backend->format_list[i].format == format &&
165                       (drv->backend->format_list[i].usage & usage) == usage)
166                         return 1;
167         }
168
169         return 0;
170 }
171
172 struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height,
173                       drv_format_t format)
174 {
175
176         struct bo *bo;
177         bo = (struct bo *) calloc(1, sizeof(*bo));
178
179         if (!bo)
180                 return NULL;
181
182         bo->drv = drv;
183         bo->width = width;
184         bo->height = height;
185         bo->format = format;
186         bo->num_planes = drv_num_planes_from_format(format);
187
188         if (!bo->num_planes) {
189                 free(bo);
190                 return NULL;
191         }
192
193         return bo;
194 }
195
196 struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height,
197                          drv_format_t format, uint64_t flags)
198 {
199         int ret;
200         size_t plane;
201         struct bo *bo;
202
203         bo = drv_bo_new(drv, width, height, format);
204
205         if (!bo)
206                 return NULL;
207
208         ret = drv->backend->bo_create(bo, width, height, format, flags);
209
210         if (ret) {
211                 free(bo);
212                 return NULL;
213         }
214
215         pthread_mutex_lock(&drv->table_lock);
216
217         for (plane = 0; plane < bo->num_planes; plane++)
218                 drv_increment_reference_count(drv, bo, plane);
219
220         pthread_mutex_unlock(&drv->table_lock);
221
222         return bo;
223 }
224
225 void drv_bo_destroy(struct bo *bo)
226 {
227         size_t plane;
228         uintptr_t total = 0;
229         struct driver *drv = bo->drv;
230
231         pthread_mutex_lock(&drv->table_lock);
232
233         for (plane = 0; plane < bo->num_planes; plane++)
234                 drv_decrement_reference_count(drv, bo, plane);
235
236         for (plane = 0; plane < bo->num_planes; plane++)
237                 total += drv_get_reference_count(drv, bo, plane);
238
239         pthread_mutex_unlock(&drv->table_lock);
240
241         if (total == 0)
242                 bo->drv->backend->bo_destroy(bo);
243
244         free(bo);
245 }
246
247 struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
248 {
249         int ret;
250         struct bo *bo;
251         struct drm_prime_handle prime_handle;
252
253         memset(&prime_handle, 0, sizeof(prime_handle));
254         prime_handle.fd = data->fd;
255
256         /* This function can support only single plane formats. */
257         /* If multi-plane import is desired, new function should be added. */
258         if (drv_num_planes_from_format(data->format) != 1)
259                 return NULL;
260
261         bo = drv_bo_new(drv, data->width, data->height, data->format);
262
263         ret = drmIoctl(drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
264                        &prime_handle);
265
266         if (ret) {
267                 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed "
268                                 "(fd=%u)\n", prime_handle.fd);
269                 free(bo);
270                 return NULL;
271         }
272
273         bo->strides[0] = data->stride;
274         bo->sizes[0] = data->height * data->stride;
275         bo->handles[0].u32 = prime_handle.handle;
276
277         pthread_mutex_lock(&drv->table_lock);
278         drv_increment_reference_count(drv, bo, 0);
279         pthread_mutex_unlock(&drv->table_lock);
280
281         return bo;
282 }
283
284 uint32_t drv_bo_get_width(struct bo *bo)
285 {
286         return bo->width;
287 }
288
289 uint32_t drv_bo_get_height(struct bo *bo)
290 {
291         return bo->height;
292 }
293
294 uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
295 {
296         return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
297 }
298
299 size_t drv_bo_get_num_planes(struct bo *bo)
300 {
301         return bo->num_planes;
302 }
303
304 union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
305 {
306         return bo->handles[plane];
307 }
308
309 #ifndef DRM_RDWR
310 #define DRM_RDWR O_RDWR
311 #endif
312
313 int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
314 {
315
316         int ret, fd;
317         assert(plane < bo->num_planes);
318
319         ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32,
320                                  DRM_CLOEXEC | DRM_RDWR, &fd);
321
322         return (ret) ? ret : fd;
323
324 }
325
326 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
327 {
328         assert(plane < bo->num_planes);
329         return bo->offsets[plane];
330 }
331
332 uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
333 {
334         assert(plane < bo->num_planes);
335         return bo->sizes[plane];
336 }
337
338 uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
339 {
340         assert(plane < bo->num_planes);
341         return bo->strides[plane];
342 }
343
344 uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
345 {
346         assert(plane < bo->num_planes);
347         return bo->format_modifiers[plane];
348 }