OSDN Git Service

f62ef5c828827b72cbfaf6192ee3ecfdbac47d14
[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 <stdbool.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <unistd.h>
17 #include <xf86drm.h>
18
19 #include "dri.h"
20 #include "drv_priv.h"
21 #include "helpers.h"
22 #include "util.h"
23
24 static const struct {
25         uint32_t drm_format;
26         int dri_image_format;
27 } drm_to_dri_image_formats[] = {
28         { DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8 },
29         { DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88 },
30         { DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565 },
31         { DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888 },
32         { DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888 },
33         { DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888 },
34         { DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888 },
35         { DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010 },
36         { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
37 };
38
39 static int drm_format_to_dri_format(uint32_t drm_format)
40 {
41         uint32_t i;
42         for (i = 0; i < ARRAY_SIZE(drm_to_dri_image_formats); i++) {
43                 if (drm_to_dri_image_formats[i].drm_format == drm_format)
44                         return drm_to_dri_image_formats[i].dri_image_format;
45         }
46
47         return 0;
48 }
49
50 static bool lookup_extension(const __DRIextension *const *extensions, const char *name,
51                              int min_version, const __DRIextension **dst)
52 {
53         while (*extensions) {
54                 if ((*extensions)->name && !strcmp((*extensions)->name, name) &&
55                     (*extensions)->version >= min_version) {
56                         *dst = *extensions;
57                         return true;
58                 }
59
60                 extensions++;
61         }
62
63         return false;
64 }
65
66 /*
67  * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
68  * to import into minigbm.
69  */
70 static int import_into_minigbm(struct dri_driver *dri, struct bo *bo)
71 {
72         uint32_t handle;
73         int prime_fd, ret;
74
75         if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_FD, &prime_fd))
76                 return -errno;
77
78         ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
79         if (ret) {
80                 drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
81                 return ret;
82         }
83
84         bo->handles[0].u32 = handle;
85         close(prime_fd);
86         return 0;
87 }
88
89 /*
90  * Close Gem Handle
91  */
92 static void close_gem_handle(uint32_t handle, int fd)
93 {
94         struct drm_gem_close gem_close;
95         int ret = 0;
96
97         memset(&gem_close, 0, sizeof(gem_close));
98         gem_close.handle = handle;
99         ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
100         if (ret)
101                 drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", handle, ret);
102 }
103
104 /*
105  * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
106  */
107 int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
108 {
109         char fname[128];
110         const __DRIextension **(*get_extensions)();
111         const __DRIextension *loader_extensions[] = { NULL };
112
113         struct dri_driver *dri = drv->priv;
114         dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
115         if (!dri->driver_handle)
116                 return -ENODEV;
117
118         snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
119         get_extensions = dlsym(dri->driver_handle, fname);
120         if (!get_extensions)
121                 goto free_handle;
122
123         dri->extensions = get_extensions();
124         if (!dri->extensions)
125                 goto free_handle;
126
127         if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
128                               (const __DRIextension **)&dri->core_extension))
129                 goto free_handle;
130
131         /* Version 4 for createNewScreen2 */
132         if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
133                               (const __DRIextension **)&dri->dri2_extension))
134                 goto free_handle;
135
136         dri->device = dri->dri2_extension->createNewScreen2(0, drv_get_fd(drv), loader_extensions,
137                                                             dri->extensions, &dri->configs, NULL);
138         if (!dri->device)
139                 goto free_handle;
140
141         dri->context =
142             dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
143
144         if (!dri->context)
145                 goto free_screen;
146
147         if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
148                               (const __DRIextension **)&dri->image_extension))
149                 goto free_context;
150
151         if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
152                               (const __DRIextension **)&dri->flush_extension))
153                 goto free_context;
154
155         return 0;
156
157 free_context:
158         dri->core_extension->destroyContext(dri->context);
159 free_screen:
160         dri->core_extension->destroyScreen(dri->device);
161 free_handle:
162         dlclose(dri->driver_handle);
163         dri->driver_handle = NULL;
164         return -ENODEV;
165 }
166
167 /*
168  * The caller is responsible for freeing drv->priv.
169  */
170 void dri_close(struct driver *drv)
171 {
172         struct dri_driver *dri = drv->priv;
173
174         dri->core_extension->destroyContext(dri->context);
175         dri->core_extension->destroyScreen(dri->device);
176         dlclose(dri->driver_handle);
177         dri->driver_handle = NULL;
178 }
179
180 int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
181                   uint64_t use_flags)
182 {
183         unsigned int dri_use;
184         int ret, dri_format, stride, offset;
185         struct dri_driver *dri = bo->drv->priv;
186
187         assert(bo->num_planes == 1);
188         dri_format = drm_format_to_dri_format(format);
189
190         /* Gallium drivers require shared to get the handle and stride. */
191         dri_use = __DRI_IMAGE_USE_SHARE;
192         if (use_flags & BO_USE_SCANOUT)
193                 dri_use |= __DRI_IMAGE_USE_SCANOUT;
194         if (use_flags & BO_USE_CURSOR)
195                 dri_use |= __DRI_IMAGE_USE_CURSOR;
196         if (use_flags & BO_USE_LINEAR)
197                 dri_use |= __DRI_IMAGE_USE_LINEAR;
198
199         bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
200                                                      dri_use, NULL);
201         if (!bo->priv) {
202                 ret = -errno;
203                 return ret;
204         }
205
206         ret = import_into_minigbm(dri, bo);
207         if (ret)
208                 goto free_image;
209
210         if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_STRIDE, &stride)) {
211                 ret = -errno;
212                 goto close_handle;
213         }
214
215         if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
216                 ret = -errno;
217                 goto close_handle;
218         }
219
220         bo->strides[0] = stride;
221         bo->sizes[0] = stride * height;
222         bo->offsets[0] = offset;
223         bo->total_size = offset + bo->sizes[0];
224         return 0;
225
226 close_handle:
227         close_gem_handle(bo->handles[0].u32, bo->drv->fd);
228 free_image:
229         dri->image_extension->destroyImage(bo->priv);
230         return ret;
231 }
232
233 int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
234 {
235         int ret;
236         struct dri_driver *dri = bo->drv->priv;
237
238         assert(bo->num_planes == 1);
239
240         // clang-format off
241         bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
242                                                             data->format, data->fds, bo->num_planes,
243                                                             (int *)data->strides,
244                                                             (int *)data->offsets, NULL);
245         // clang-format on
246         if (!bo->priv)
247                 return -errno;
248
249         ret = import_into_minigbm(dri, bo);
250         if (ret) {
251                 dri->image_extension->destroyImage(bo->priv);
252                 return ret;
253         }
254
255         return 0;
256 }
257
258 int dri_bo_destroy(struct bo *bo)
259 {
260         struct dri_driver *dri = bo->drv->priv;
261
262         assert(bo->priv);
263         close_gem_handle(bo->handles[0].u32, bo->drv->fd);
264         dri->image_extension->destroyImage(bo->priv);
265         bo->priv = NULL;
266         return 0;
267 }
268
269 /*
270  * Map an image plane.
271  *
272  * This relies on the underlying driver to do a decompressing and/or de-tiling
273  * blit if necessary,
274  *
275  * This function itself is not thread-safe; we rely on the fact that the caller
276  * locks a per-driver mutex.
277  */
278 void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
279 {
280         struct dri_driver *dri = bo->drv->priv;
281
282         /* GBM flags and DRI flags are the same. */
283         vma->addr =
284             dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->width, bo->height,
285                                            map_flags, (int *)&vma->map_strides[plane], &vma->priv);
286         if (!vma->addr)
287                 return MAP_FAILED;
288
289         return vma->addr;
290 }
291
292 int dri_bo_unmap(struct bo *bo, struct vma *vma)
293 {
294         struct dri_driver *dri = bo->drv->priv;
295
296         assert(vma->priv);
297         dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
298
299         /*
300          * From gbm_dri.c in Mesa:
301          *
302          * "Not all DRI drivers use direct maps. They may queue up DMA operations
303          *  on the mapping context. Since there is no explicit gbm flush mechanism,
304          *  we need to flush here."
305          */
306
307         dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
308         return 0;
309 }
310
311 #endif