summaryrefslogtreecommitdiff
path: root/src/gbm/gallium_drm.c
blob: 19bc6a9a14cc11c4d8c360d67e2ff45b333a8dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <limits.h>

#define EGL_EGLEXT_PROTOTYPES
#include "EGL/egl.h"
#include "EGL/eglext.h"

#include "util/u_memory.h"
#include "util/u_string.h"
#include "util/u_dl.h"
#include "util/u_inlines.h"
#include "util/u_drm_screen.h"

#include "state_tracker/drm_driver.h"

#include <unistd.h>
#include <sys/types.h>

#include "gallium_drm_internal.h"

static struct pipe_module {
   boolean initialized;
   char *name;
   struct util_dl_library *lib;
   const struct drm_driver_descriptor *drmdd;
} pipe_modules[16];

static char *
loader_strdup(const char *s)
{
   size_t len = (s) ? strlen(s) : 0;
   char *t = MALLOC(len + 1);
   if (t) {
      memcpy(t, s, len);
      t[len] = '\0';
   }
   return t;
}

static void
find_pipe_module(struct pipe_module *pmod, const char *name)
{
   char *search_paths, *end, *next, *p;
   char path[PATH_MAX];
   int ret;
   
   search_paths = NULL;
   if (geteuid() == getuid()) {
      /* don't allow setuid apps to use EGL_DRIVERS_PATH */
      search_paths = getenv("EGL_DRIVERS_PATH");
   }
   if (search_paths == NULL)
      search_paths = _EGL_GALLIUM_DRIVER_SEARCH_DIR;

   end = search_paths + strlen(search_paths);
   for (p = search_paths; p < end && pmod->lib == NULL; p = next + 1) {
      int len;
      next = strchr(p, ':');
      if (next == NULL)
         next = end;

      len = next - p;

      if (len) {
         ret = util_snprintf(path, sizeof(path),
                             "%.*s/" PIPE_PREFIX "%s" UTIL_DL_EXT, len, p, pmod->name);
      }
      else {
         ret = util_snprintf(path, sizeof(path),
                             PIPE_PREFIX "%s" UTIL_DL_EXT, pmod->name);
      }
      if (ret > 0 && ret < sizeof(path)) {
         pmod->lib = util_dl_open(path);
#if 0
         if (pmod->lib)
            _eglLog(_EGL_DEBUG, "loaded %s", path);
#endif
      }

   }
}

static boolean
load_pipe_module(struct pipe_module *pmod, const char *name)
{
   pmod->name = loader_strdup(name);
   if (!pmod->name)
      return FALSE;

   find_pipe_module(pmod, name);

   if (pmod->lib) {
      pmod->drmdd = (const struct drm_driver_descriptor *)
         util_dl_get_proc_address(pmod->lib, "driver_descriptor");

      /* sanity check on the name */
      if (pmod->drmdd && strcmp(pmod->drmdd->name, pmod->name) != 0)
         pmod->drmdd = NULL;

      if (!pmod->drmdd) {
         util_dl_close(pmod->lib);
         pmod->lib = NULL;
      }
   }

   return (pmod->drmdd != NULL);
}

static struct pipe_module *
get_pipe_module(const char *name)
{
   struct pipe_module *pmod = NULL;
   int i;

   if (!name)
      return NULL;

   for (i = 0; i < Elements(pipe_modules); i++) {
      if (!pipe_modules[i].initialized ||
          strcmp(pipe_modules[i].name, name) == 0) {
         pmod = &pipe_modules[i];
         break;
      }
   }
   if (!pmod)
      return NULL;

   if (!pmod->initialized) {
      load_pipe_module(pmod, name);
      pmod->initialized = TRUE;
   }

   return pmod;
}

static struct pipe_screen *
create_drm_screen(const char *name, int fd)
{
   struct pipe_module *pmod = get_pipe_module(name);
 
   return (pmod && pmod->drmdd && pmod->drmdd->create_screen) ?
      pmod->drmdd->create_screen(fd) : NULL;
}










static int
gallium_screen_create(struct gbm_gallium_drm_device *gdrm)
{
   gdrm->base.driver_name = util_drm_fd_get_screen_name(gdrm->base.base.fd);
   printf("driver name. %s\n", gdrm->base.driver_name);

   gdrm->screen = create_drm_screen(gdrm->base.driver_name, gdrm->base.base.fd);
   if (gdrm->screen == NULL) {
      fprintf(stderr, "failed to load driver: %s\n", gdrm->base.driver_name);
      return -1;
   };

   return 0;
}

static void
gbm_gallium_drm_bo_destroy(struct gbm_bo *_bo)
{
   struct gbm_gallium_drm_bo *bo = gbm_gallium_drm_bo(_bo);

   pipe_resource_reference(&bo->resource, NULL);
}

static struct gbm_bo *
gbm_gallium_drm_bo_create_from_egl_image(struct gbm_device *gbm,
                                         void *egl_dpy, void *egl_image,
                                         uint32_t width, uint32_t height,
                                         enum gbm_bo_format format)
{
   struct gbm_gallium_drm_device *gdrm = gbm_gallium_drm_device(gbm);
   struct gbm_gallium_drm_bo *bo;
   struct pipe_resource templ;
   struct winsys_handle whandle;
   uint resource_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
   enum pipe_format pf;

   int32_t name, pitch;

   eglExportDRMImageMESA(egl_dpy, egl_image, &name, NULL, &pitch);

   bo = CALLOC_STRUCT(gbm_gallium_drm_bo);
   if (bo == NULL)
      return NULL;

   bo->base.base.gbm = gbm;
   bo->base.base.width = width;
   bo->base.base.height = height;

   switch (format) {
   case GBM_BO_FORMAT_XRGB8888:
      pf = PIPE_FORMAT_B8G8R8X8_UNORM;
      break;
   case GBM_BO_FORMAT_ARGB8888:
      pf = PIPE_FORMAT_B8G8R8A8_UNORM;
      break;
   default:
      return NULL;
   }

   memset(&templ, 0, sizeof(templ));
   templ.bind = resource_usage;
   templ.format = pf;
   templ.target = PIPE_TEXTURE_2D;
   templ.last_level = 0;
   templ.width0 = width;
   templ.height0 = height;
   templ.depth0 = 1;
   templ.array_size = 1;

   memset(&whandle, 0, sizeof(whandle));
   whandle.type = DRM_API_HANDLE_TYPE_SHARED;
   whandle.handle = name;
   whandle.stride = pitch;

   bo->resource = gdrm->screen->resource_from_handle(gdrm->screen, &templ, &whandle);
   if (bo->resource == NULL) {
      FREE(bo);
      return NULL;
   }

   memset(&whandle, 0, sizeof(whandle));
   whandle.type = DRM_API_HANDLE_TYPE_KMS;
   gdrm->screen->resource_get_handle(gdrm->screen, bo->resource, &whandle);

   bo->base.base.handle.u32 = whandle.handle;
   bo->base.base.pitch      = whandle.stride;

   return &bo->base.base;
}

static struct gbm_bo *
gbm_gallium_drm_bo_create(struct gbm_device *gbm,
                  uint32_t width, uint32_t height,
                  enum gbm_bo_format format, uint32_t usage)
{
   struct gbm_gallium_drm_device *gdrm = gbm_gallium_drm_device(gbm);
   struct gbm_gallium_drm_bo *bo;
   struct pipe_resource templ;
   struct winsys_handle whandle;
   uint resource_usage = 0;
   enum pipe_format pf;

   bo = CALLOC_STRUCT(gbm_gallium_drm_bo);
   if (bo == NULL)
      return NULL;

   bo->base.base.gbm = gbm;
   bo->base.base.width = width;
   bo->base.base.height = height;

   switch (format) {
   case GBM_BO_FORMAT_XRGB8888:
      pf = PIPE_FORMAT_B8G8R8X8_UNORM;
      break;
   case GBM_BO_FORMAT_ARGB8888:
      pf = PIPE_FORMAT_B8G8R8A8_UNORM;
      break;
   default:
      return NULL;
   }

   if (usage & GBM_BO_USE_SCANOUT)
      resource_usage |= PIPE_BIND_SCANOUT;

   if (usage & GBM_BO_USE_RENDERING)
      resource_usage |= PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;

#if 0
   if (usage & GBM_BO_USE_CURSOR_64X64)
      resource_usage |= PIPE_BIND_CURSOR;
#endif

   memset(&templ, 0, sizeof(templ));
   templ.bind = resource_usage;
   templ.format = pf;
   templ.target = PIPE_TEXTURE_2D;
   templ.last_level = 0;
   templ.width0 = width;
   templ.height0 = height;
   templ.depth0 = 1;
   templ.array_size = 1;

   bo->resource = gdrm->screen->resource_create(gdrm->screen, &templ);
   if (bo->resource == NULL) {
      FREE(bo);
      return NULL;
   }

   memset(&whandle, 0, sizeof(whandle));
   whandle.type = DRM_API_HANDLE_TYPE_KMS;
   gdrm->screen->resource_get_handle(gdrm->screen, bo->resource, &whandle);

   bo->base.base.handle.u32 = whandle.handle;
   bo->base.base.pitch      = whandle.stride;

   return &bo->base.base;
}

static boolean
pipe_modules_destroy(void)
{
   //util_dl_close
   
   return TRUE;
}

static void
gbm_gallium_drm_destroy(struct gbm_device *gbm)
{
   struct gbm_gallium_drm_device *gdrm = gbm_gallium_drm_device(gbm);

   gdrm->screen->destroy(gdrm->screen);

   pipe_modules_destroy();
   FREE(gdrm->base.driver_name);

   FREE(gdrm);
}

struct gbm_device *
gbm_gallium_drm_device_create(int fd)
{
   struct gbm_gallium_drm_device *dri;
   int ret;

   dri = calloc(1, sizeof *dri);

   dri->base.base.fd = fd;
   dri->base.base.bo_create = gbm_gallium_drm_bo_create;
   dri->base.base.bo_create_from_egl_image =
      gbm_gallium_drm_bo_create_from_egl_image;
   dri->base.base.bo_destroy = gbm_gallium_drm_bo_destroy;
   dri->base.base.destroy = gbm_gallium_drm_destroy;

   dri->base.type = GBM_DRM_DRIVER_TYPE_GALLIUM;
   dri->base.base.name = "drm";

   ret = gallium_screen_create(dri);
   if (ret) {
      free(dri);
      return NULL;
   }

   return &dri->base.base;
}