OSDN Git Service

6d7c9a0e04d57cb4ed22d0291a3fa2e240d7e074
[android-x86/external-minigbm.git] / dri.c
1 /*
2  * Copyright 2017 Advanced Micro Devices. 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
7 #ifdef DRV_AMDGPU
8
9 #include <assert.h>
10 #include <dlfcn.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
18 #include <xf86drm.h>
19
20 #include "dri.h"
21 #include "drv_priv.h"
22 #include "helpers.h"
23 #include "util.h"
24
25 static const struct {
26         uint32_t drm_format;
27         int dri_image_format;
28 } drm_to_dri_image_formats[] = {
29         { DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8 },
30         { DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88 },
31         { DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565 },
32         { DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888 },
33         { DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888 },
34         { DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888 },
35         { DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888 },
36         { DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010 },
37         { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
38 };
39
40 static int drm_format_to_dri_format(uint32_t drm_format)
41 {
42         uint32_t i;
43         for (i = 0; i < ARRAY_SIZE(drm_to_dri_image_formats); i++) {
44                 if (drm_to_dri_image_formats[i].drm_format == drm_format)
45                         return drm_to_dri_image_formats[i].dri_image_format;
46         }
47
48         return 0;
49 }
50
51 static bool lookup_extension(const __DRIextension *const *extensions, const char *name,
52                              int min_version, const __DRIextension **dst)
53 {
54         while (*extensions) {
55                 if ((*extensions)->name && !strcmp((*extensions)->name, name) &&
56                     (*extensions)->version >= min_version) {
57                         *dst = *extensions;
58                         return true;
59                 }
60
61                 extensions++;
62         }
63
64         return false;
65 }
66
67 /*
68  * Close Gem Handle
69  */
70 static void close_gem_handle(uint32_t handle, int fd)
71 {
72         struct drm_gem_close gem_close;
73         int ret = 0;
74
75         memset(&gem_close, 0, sizeof(gem_close));
76         gem_close.handle = handle;
77         ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
78         if (ret)
79                 drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", handle, ret);
80 }
81
82 /*
83  * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
84  * to import into minigbm.
85  */
86 static int import_into_minigbm(struct dri_driver *dri, struct bo *bo)
87 {
88         uint32_t handle;
89         int ret, modifier_upper, modifier_lower, num_planes, i, j;
90         __DRIimage *plane_image = NULL;
91
92         if (dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
93                                              &modifier_upper) &&
94             dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
95                                              &modifier_lower)) {
96                 bo->meta.format_modifiers[0] =
97                     ((uint64_t)modifier_upper << 32) | (uint32_t)modifier_lower;
98         } else {
99                 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_INVALID;
100         }
101
102         if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_NUM_PLANES,
103                                               &num_planes)) {
104                 return -errno;
105         }
106
107         bo->meta.num_planes = num_planes;
108
109         for (i = 0; i < num_planes; ++i) {
110                 int prime_fd, stride, offset;
111                 plane_image = dri->image_extension->fromPlanar(bo->priv, i, NULL);
112                 __DRIimage *image = plane_image ? plane_image : bo->priv;
113
114                 if (i)
115                         bo->meta.format_modifiers[i] = bo->meta.format_modifiers[0];
116
117                 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride) ||
118                     !dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
119                         ret = -errno;
120                         goto cleanup;
121                 }
122
123                 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &prime_fd)) {
124                         ret = -errno;
125                         goto cleanup;
126                 }
127
128                 ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
129
130                 close(prime_fd);
131
132                 if (ret) {
133                         drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
134                         goto cleanup;
135                 }
136
137                 bo->handles[i].u32 = handle;
138
139                 bo->meta.strides[i] = stride;
140                 bo->meta.offsets[i] = offset;
141
142                 /* Not setting sizes[i] and total_size as these are not
143                  * provided by DRI. The best way to "approximate" would be
144                  * what drv_bo_import does, but I see nothing in the minigbm
145                  * core that needs it. */
146
147                 if (plane_image)
148                         dri->image_extension->destroyImage(plane_image);
149         }
150
151         return 0;
152
153 cleanup:
154         if (plane_image)
155                 dri->image_extension->destroyImage(plane_image);
156         while (--i >= 0) {
157                 for (j = 0; j <= i; ++j)
158                         if (bo->handles[j].u32 == bo->handles[i].u32)
159                                 break;
160
161                 /* Multiple equivalent handles) */
162                 if (i == j)
163                         break;
164
165                 /* This kind of goes horribly wrong when we already imported
166                  * the same handles earlier, as we should really reference
167                  * count handles. */
168                 close_gem_handle(bo->handles[i].u32, bo->drv->fd);
169         }
170         return ret;
171 }
172
173 /*
174  * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
175  */
176 int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
177 {
178         char fname[128];
179         const __DRIextension **(*get_extensions)();
180         const __DRIextension *loader_extensions[] = { NULL };
181
182         struct dri_driver *dri = drv->priv;
183
184         dri->fd = open(drmGetRenderDeviceNameFromFd(drv_get_fd(drv)), O_RDWR);
185         if (dri->fd < 0)
186                 return -ENODEV;
187
188         dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
189         if (!dri->driver_handle)
190                 goto close_dri_fd;
191
192         snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
193         get_extensions = dlsym(dri->driver_handle, fname);
194         if (!get_extensions)
195                 goto free_handle;
196
197         dri->extensions = get_extensions();
198         if (!dri->extensions)
199                 goto free_handle;
200
201         if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
202                               (const __DRIextension **)&dri->core_extension))
203                 goto free_handle;
204
205         /* Version 4 for createNewScreen2 */
206         if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
207                               (const __DRIextension **)&dri->dri2_extension))
208                 goto free_handle;
209
210         dri->device = dri->dri2_extension->createNewScreen2(0, dri->fd, loader_extensions,
211                                                             dri->extensions, &dri->configs, NULL);
212         if (!dri->device)
213                 goto free_handle;
214
215         dri->context =
216             dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
217
218         if (!dri->context)
219                 goto free_screen;
220
221         if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
222                               (const __DRIextension **)&dri->image_extension))
223                 goto free_context;
224
225         if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
226                               (const __DRIextension **)&dri->flush_extension))
227                 goto free_context;
228
229         return 0;
230
231 free_context:
232         dri->core_extension->destroyContext(dri->context);
233 free_screen:
234         dri->core_extension->destroyScreen(dri->device);
235 free_handle:
236         dlclose(dri->driver_handle);
237         dri->driver_handle = NULL;
238 close_dri_fd:
239         close(dri->fd);
240         return -ENODEV;
241 }
242
243 /*
244  * The caller is responsible for freeing drv->priv.
245  */
246 void dri_close(struct driver *drv)
247 {
248         struct dri_driver *dri = drv->priv;
249
250         dri->core_extension->destroyContext(dri->context);
251         dri->core_extension->destroyScreen(dri->device);
252         dlclose(dri->driver_handle);
253         dri->driver_handle = NULL;
254         close(dri->fd);
255 }
256
257 int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
258                   uint64_t use_flags)
259 {
260         unsigned int dri_use;
261         int ret, dri_format;
262         struct dri_driver *dri = bo->drv->priv;
263
264         dri_format = drm_format_to_dri_format(format);
265
266         /* Gallium drivers require shared to get the handle and stride. */
267         dri_use = __DRI_IMAGE_USE_SHARE;
268         if (use_flags & BO_USE_SCANOUT)
269                 dri_use |= __DRI_IMAGE_USE_SCANOUT;
270         if (use_flags & BO_USE_CURSOR)
271                 dri_use |= __DRI_IMAGE_USE_CURSOR;
272         if (use_flags & BO_USE_LINEAR)
273                 dri_use |= __DRI_IMAGE_USE_LINEAR;
274
275         bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
276                                                      dri_use, NULL);
277         if (!bo->priv) {
278                 ret = -errno;
279                 return ret;
280         }
281
282         ret = import_into_minigbm(dri, bo);
283         if (ret)
284                 goto free_image;
285
286         return 0;
287
288 free_image:
289         dri->image_extension->destroyImage(bo->priv);
290         return ret;
291 }
292
293 int dri_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
294                                  const uint64_t *modifiers, uint32_t modifier_count)
295 {
296         int ret, dri_format;
297         struct dri_driver *dri = bo->drv->priv;
298
299         if (!dri->image_extension->createImageWithModifiers) {
300                 return -ENOENT;
301         }
302
303         dri_format = drm_format_to_dri_format(format);
304
305         bo->priv = dri->image_extension->createImageWithModifiers(
306             dri->device, width, height, dri_format, modifiers, modifier_count, NULL);
307         if (!bo->priv) {
308                 ret = -errno;
309                 return ret;
310         }
311
312         ret = import_into_minigbm(dri, bo);
313         if (ret)
314                 goto free_image;
315
316         return 0;
317
318 free_image:
319         dri->image_extension->destroyImage(bo->priv);
320         return ret;
321 }
322
323 int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
324 {
325         int ret;
326         struct dri_driver *dri = bo->drv->priv;
327
328         if (data->format_modifiers[0] != DRM_FORMAT_MOD_INVALID) {
329                 unsigned error;
330
331                 if (!dri->image_extension->createImageFromDmaBufs2)
332                         return -ENOSYS;
333
334                 // clang-format off
335                 bo->priv = dri->image_extension->createImageFromDmaBufs2(dri->device, data->width, data->height,
336                                                                          data->format,
337                                                                          data->format_modifiers[0],
338                                                                          data->fds,
339                                                                          bo->meta.num_planes,
340                                                                          (int *)data->strides,
341                                                                          (int *)data->offsets,
342                                                                          __DRI_YUV_COLOR_SPACE_UNDEFINED,
343                                                                          __DRI_YUV_RANGE_UNDEFINED,
344                                                                          __DRI_YUV_CHROMA_SITING_UNDEFINED,
345                                                                          __DRI_YUV_CHROMA_SITING_UNDEFINED,
346                                                                          &error, NULL);
347                 // clang-format on
348
349                 /* Could translate the DRI error, but the Mesa GBM also returns ENOSYS. */
350                 if (!bo->priv)
351                         return -ENOSYS;
352         } else {
353                 // clang-format off
354                 bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
355                                                                     data->format, data->fds,
356                                                                     bo->meta.num_planes,
357                                                                     (int *)data->strides,
358                                                                     (int *)data->offsets, NULL);
359                 // clang-format on
360                 if (!bo->priv)
361                         return -errno;
362         }
363
364         ret = import_into_minigbm(dri, bo);
365         if (ret) {
366                 dri->image_extension->destroyImage(bo->priv);
367                 return ret;
368         }
369
370         return 0;
371 }
372
373 int dri_bo_destroy(struct bo *bo)
374 {
375         struct dri_driver *dri = bo->drv->priv;
376
377         assert(bo->priv);
378         close_gem_handle(bo->handles[0].u32, bo->drv->fd);
379         dri->image_extension->destroyImage(bo->priv);
380         bo->priv = NULL;
381         return 0;
382 }
383
384 /*
385  * Map an image plane.
386  *
387  * This relies on the underlying driver to do a decompressing and/or de-tiling
388  * blit if necessary,
389  *
390  * This function itself is not thread-safe; we rely on the fact that the caller
391  * locks a per-driver mutex.
392  */
393 void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
394 {
395         struct dri_driver *dri = bo->drv->priv;
396
397         /* GBM flags and DRI flags are the same. */
398         vma->addr = dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->meta.width,
399                                                    bo->meta.height, map_flags,
400                                                    (int *)&vma->map_strides[plane], &vma->priv);
401         if (!vma->addr)
402                 return MAP_FAILED;
403
404         return vma->addr;
405 }
406
407 int dri_bo_unmap(struct bo *bo, struct vma *vma)
408 {
409         struct dri_driver *dri = bo->drv->priv;
410
411         assert(vma->priv);
412         dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
413
414         /*
415          * From gbm_dri.c in Mesa:
416          *
417          * "Not all DRI drivers use direct maps. They may queue up DMA operations
418          *  on the mapping context. Since there is no explicit gbm flush mechanism,
419          *  we need to flush here."
420          */
421
422         dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
423         return 0;
424 }
425
426 size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
427 {
428         struct dri_driver *dri = drv->priv;
429         if (!dri->image_extension->queryDmaBufFormatModifierAttribs) {
430                 /* We do not do any modifier checks here. The create will fail
431                  * later if the modifier is not supported. */
432                 return drv_num_planes_from_format(format);
433         }
434
435         uint64_t planes;
436         GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
437             dri->device, format, modifier, __DRI_IMAGE_ATTRIB_NUM_PLANES, &planes);
438         if (!ret)
439                 return 0;
440
441         return planes;
442 }
443
444 #endif