android: use gralloc_drm_get_gem_handle api
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.12
4 *
5 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
6 * Copyright (C) 2010-2011 LunarG Inc.
7 *
8 * Based on platform_x11, which has
9 *
10 * Copyright © 2011 Intel Corporation
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 */
30
31 #include <errno.h>
32 #include <dlfcn.h>
33
34 #if ANDROID_VERSION >= 0x402
35 #include <sync/sync.h>
36 #endif
37
38 /* for droid_get_pci_id */
39 #include <xf86drm.h>
40 #include <i915_drm.h>
41 #include <radeon_drm.h>
42
43 #include "egl_dri2.h"
44 #include "gralloc_drm.h"
45
46 static int
47 get_format_bpp(int native)
48 {
49 int bpp;
50
51 switch (native) {
52 case HAL_PIXEL_FORMAT_RGBA_8888:
53 case HAL_PIXEL_FORMAT_RGBX_8888:
54 case HAL_PIXEL_FORMAT_BGRA_8888:
55 bpp = 4;
56 break;
57 case HAL_PIXEL_FORMAT_RGB_888:
58 bpp = 3;
59 break;
60 case HAL_PIXEL_FORMAT_RGB_565:
61 case HAL_PIXEL_FORMAT_RGBA_5551:
62 case HAL_PIXEL_FORMAT_RGBA_4444:
63 bpp = 2;
64 break;
65 default:
66 bpp = 0;
67 break;
68 }
69
70 return bpp;
71 }
72
73 static int
74 get_native_buffer_name(struct ANativeWindowBuffer *buf)
75 {
76 return gralloc_drm_get_gem_handle(buf->handle);
77 }
78
79 static EGLBoolean
80 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
81 {
82 #if ANDROID_VERSION >= 0x0402
83 int fence_fd;
84
85 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
86 &fence_fd))
87 return EGL_FALSE;
88
89 /* If access to the buffer is controlled by a sync fence, then block on the
90 * fence.
91 *
92 * It may be more performant to postpone blocking until there is an
93 * immediate need to write to the buffer. But doing so would require adding
94 * hooks to the DRI2 loader.
95 *
96 * From the ANativeWindow::dequeueBuffer documentation:
97 *
98 * The libsync fence file descriptor returned in the int pointed to by
99 * the fenceFd argument will refer to the fence that must signal
100 * before the dequeued buffer may be written to. A value of -1
101 * indicates that the caller may access the buffer immediately without
102 * waiting on a fence. If a valid file descriptor is returned (i.e.
103 * any value except -1) then the caller is responsible for closing the
104 * file descriptor.
105 */
106 if (fence_fd >= 0) {
107 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
108 *
109 * Waits indefinitely if timeout < 0.
110 */
111 int timeout = -1;
112 sync_wait(fence_fd, timeout);
113 close(fence_fd);
114 }
115
116 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
117 #else
118 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
119 return EGL_FALSE;
120
121 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
122 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
123 #endif
124
125 return EGL_TRUE;
126 }
127
128 static EGLBoolean
129 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
130 {
131 #if ANDROID_VERSION >= 0x0402
132 /* Queue the buffer without a sync fence. This informs the ANativeWindow
133 * that it may access the buffer immediately.
134 *
135 * From ANativeWindow::dequeueBuffer:
136 *
137 * The fenceFd argument specifies a libsync fence file descriptor for
138 * a fence that must signal before the buffer can be accessed. If
139 * the buffer can be accessed immediately then a value of -1 should
140 * be used. The caller must not use the file descriptor after it
141 * is passed to queueBuffer, and the ANativeWindow implementation
142 * is responsible for closing it.
143 */
144 int fence_fd = -1;
145 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
146 fence_fd);
147 #else
148 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
149 #endif
150
151 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
152 dri2_surf->buffer = NULL;
153
154 return EGL_TRUE;
155 }
156
157 static void
158 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
159 {
160 /* no cancel buffer? */
161 droid_window_enqueue_buffer(dri2_surf);
162 }
163
164 static __DRIbuffer *
165 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
166 unsigned int att, unsigned int format)
167 {
168 struct dri2_egl_display *dri2_dpy =
169 dri2_egl_display(dri2_surf->base.Resource.Display);
170
171 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
172 return NULL;
173
174 if (!dri2_surf->local_buffers[att]) {
175 dri2_surf->local_buffers[att] =
176 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
177 dri2_surf->base.Width, dri2_surf->base.Height);
178 }
179
180 return dri2_surf->local_buffers[att];
181 }
182
183 static void
184 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
185 {
186 struct dri2_egl_display *dri2_dpy =
187 dri2_egl_display(dri2_surf->base.Resource.Display);
188 int i;
189
190 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
191 if (dri2_surf->local_buffers[i]) {
192 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
193 dri2_surf->local_buffers[i]);
194 dri2_surf->local_buffers[i] = NULL;
195 }
196 }
197 }
198
199 static _EGLSurface *
200 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
201 _EGLConfig *conf, EGLNativeWindowType window,
202 const EGLint *attrib_list)
203 {
204 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
205 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
206 struct dri2_egl_surface *dri2_surf;
207 dri2_surf = calloc(1, sizeof *dri2_surf);
208 if (!dri2_surf) {
209 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
210 return NULL;
211 }
212
213 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
214 goto cleanup_surface;
215
216 if (type == EGL_WINDOW_BIT) {
217 int format;
218
219 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
220 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
221 goto cleanup_surface;
222 }
223 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
224 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
225 goto cleanup_surface;
226 }
227
228 if (format != dri2_conf->base.NativeVisualID) {
229 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
230 format, dri2_conf->base.NativeVisualID);
231 }
232
233 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
234 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
235 }
236
237 dri2_surf->dri_drawable =
238 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
239 dri2_conf->dri_double_config,
240 dri2_surf);
241 if (dri2_surf->dri_drawable == NULL) {
242 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
243 goto cleanup_surface;
244 }
245
246 if (window) {
247 window->common.incRef(&window->common);
248 dri2_surf->window = window;
249 }
250
251 return &dri2_surf->base;
252
253 cleanup_surface:
254 free(dri2_surf);
255
256 return NULL;
257 }
258
259 static _EGLSurface *
260 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
261 _EGLConfig *conf, EGLNativeWindowType window,
262 const EGLint *attrib_list)
263 {
264 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
265 window, attrib_list);
266 }
267
268 static _EGLSurface *
269 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
270 _EGLConfig *conf, EGLNativePixmapType pixmap,
271 const EGLint *attrib_list)
272 {
273 return NULL;
274 }
275
276 static _EGLSurface *
277 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
278 _EGLConfig *conf, const EGLint *attrib_list)
279 {
280 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
281 NULL, attrib_list);
282 }
283
284 static EGLBoolean
285 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
286 {
287 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
288 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
289
290 if (!_eglPutSurface(surf))
291 return EGL_TRUE;
292
293 droid_free_local_buffers(dri2_surf);
294
295 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
296 if (dri2_surf->buffer)
297 droid_window_cancel_buffer(dri2_surf);
298
299 dri2_surf->window->common.decRef(&dri2_surf->window->common);
300 }
301
302 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
303
304 free(dri2_surf);
305
306 return EGL_TRUE;
307 }
308
309 static EGLBoolean
310 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
311 {
312 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
313 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
314 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
315 _EGLContext *ctx;
316
317 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
318 return EGL_TRUE;
319
320 if (dri2_drv->glFlush) {
321 ctx = _eglGetCurrentContext();
322 if (ctx && ctx->DrawSurface == &dri2_surf->base)
323 dri2_drv->glFlush();
324 }
325
326 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
327
328 if (dri2_surf->buffer)
329 droid_window_enqueue_buffer(dri2_surf);
330
331 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
332
333 return EGL_TRUE;
334 }
335
336 static _EGLImage *
337 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
338 struct ANativeWindowBuffer *buf)
339 {
340 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
341 struct dri2_egl_image *dri2_img;
342 int name;
343 EGLint format;
344
345 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
346 buf->common.version != sizeof(*buf)) {
347 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
348 return NULL;
349 }
350
351 name = get_native_buffer_name(buf);
352 if (!name) {
353 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
354 return NULL;
355 }
356
357 /* see the table in droid_add_configs_for_visuals */
358 switch (buf->format) {
359 case HAL_PIXEL_FORMAT_BGRA_8888:
360 format = __DRI_IMAGE_FORMAT_ARGB8888;
361 break;
362 case HAL_PIXEL_FORMAT_RGB_565:
363 format = __DRI_IMAGE_FORMAT_RGB565;
364 break;
365 case HAL_PIXEL_FORMAT_RGBA_8888:
366 format = __DRI_IMAGE_FORMAT_ABGR8888;
367 break;
368 case HAL_PIXEL_FORMAT_RGBX_8888:
369 format = __DRI_IMAGE_FORMAT_XBGR8888;
370 break;
371 case HAL_PIXEL_FORMAT_RGB_888:
372 case HAL_PIXEL_FORMAT_RGBA_5551:
373 case HAL_PIXEL_FORMAT_RGBA_4444:
374 /* unsupported */
375 default:
376 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
377 return NULL;
378 break;
379 }
380
381 dri2_img = calloc(1, sizeof(*dri2_img));
382 if (!dri2_img) {
383 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
384 return NULL;
385 }
386
387 if (!_eglInitImage(&dri2_img->base, disp)) {
388 free(dri2_img);
389 return NULL;
390 }
391
392 dri2_img->dri_image =
393 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
394 buf->width,
395 buf->height,
396 format,
397 name,
398 buf->stride,
399 dri2_img);
400 if (!dri2_img->dri_image) {
401 free(dri2_img);
402 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
403 return NULL;
404 }
405
406 return &dri2_img->base;
407 }
408
409 static _EGLImage *
410 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
411 _EGLContext *ctx, EGLenum target,
412 EGLClientBuffer buffer, const EGLint *attr_list)
413 {
414 switch (target) {
415 case EGL_NATIVE_BUFFER_ANDROID:
416 return dri2_create_image_android_native_buffer(disp,
417 (struct ANativeWindowBuffer *) buffer);
418 default:
419 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
420 }
421 }
422
423 static void
424 droid_init_driver_functions(_EGLDriver *drv)
425 {
426 drv->API.CreateWindowSurface = droid_create_window_surface;
427 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
428 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
429 drv->API.DestroySurface = droid_destroy_surface;
430 drv->API.SwapBuffers = droid_swap_buffers;
431
432 drv->API.CreateImageKHR = droid_create_image_khr;
433 }
434
435 static void
436 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
437 {
438 }
439
440 static int
441 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
442 unsigned int *attachments, int count)
443 {
444 int num_buffers = 0, i;
445
446 /* fill dri2_surf->buffers */
447 for (i = 0; i < count * 2; i += 2) {
448 __DRIbuffer *buf, *local;
449
450 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
451 buf = &dri2_surf->buffers[num_buffers];
452
453 switch (attachments[i]) {
454 case __DRI_BUFFER_BACK_LEFT:
455 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
456 buf->attachment = attachments[i];
457 buf->name = get_native_buffer_name(dri2_surf->buffer);
458 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
459 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
460 buf->flags = 0;
461
462 if (buf->name)
463 num_buffers++;
464
465 break;
466 }
467 /* fall through for pbuffers */
468 case __DRI_BUFFER_DEPTH:
469 case __DRI_BUFFER_STENCIL:
470 case __DRI_BUFFER_ACCUM:
471 case __DRI_BUFFER_DEPTH_STENCIL:
472 case __DRI_BUFFER_HIZ:
473 local = droid_alloc_local_buffer(dri2_surf,
474 attachments[i], attachments[i + 1]);
475
476 if (local) {
477 *buf = *local;
478 num_buffers++;
479 }
480 break;
481 case __DRI_BUFFER_FRONT_LEFT:
482 case __DRI_BUFFER_FRONT_RIGHT:
483 case __DRI_BUFFER_FAKE_FRONT_LEFT:
484 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
485 case __DRI_BUFFER_BACK_RIGHT:
486 default:
487 /* no front or right buffers */
488 break;
489 }
490 }
491
492 return num_buffers;
493 }
494
495 static __DRIbuffer *
496 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
497 int *width, int *height,
498 unsigned int *attachments, int count,
499 int *out_count, void *loaderPrivate)
500 {
501 struct dri2_egl_surface *dri2_surf = loaderPrivate;
502 struct dri2_egl_display *dri2_dpy =
503 dri2_egl_display(dri2_surf->base.Resource.Display);
504 int i;
505
506 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
507 /* try to dequeue the next back buffer */
508 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
509 return NULL;
510
511 /* free outdated buffers and update the surface size */
512 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
513 dri2_surf->base.Height != dri2_surf->buffer->height) {
514 droid_free_local_buffers(dri2_surf);
515 dri2_surf->base.Width = dri2_surf->buffer->width;
516 dri2_surf->base.Height = dri2_surf->buffer->height;
517 }
518 }
519
520 dri2_surf->buffer_count =
521 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
522
523 if (width)
524 *width = dri2_surf->base.Width;
525 if (height)
526 *height = dri2_surf->base.Height;
527
528 *out_count = dri2_surf->buffer_count;;
529
530 return dri2_surf->buffers;
531 }
532
533 static EGLBoolean
534 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
535 {
536 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
537 const struct {
538 int format;
539 int size;
540 unsigned int rgba_masks[4];
541 } visuals[] = {
542 { HAL_PIXEL_FORMAT_RGBA_8888, 32, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
543 { HAL_PIXEL_FORMAT_RGBX_8888, 32, { 0xff, 0xff00, 0xff0000, 0x0 } },
544 { HAL_PIXEL_FORMAT_RGB_888, 24, { 0xff, 0xff00, 0xff0000, 0x0 } },
545 { HAL_PIXEL_FORMAT_RGB_565, 16, { 0xf800, 0x7e0, 0x1f, 0x0 } },
546 { HAL_PIXEL_FORMAT_BGRA_8888, 32, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
547 { 0, 0, { 0, 0, 0, 0 } }
548 };
549 int count, i, j;
550
551 count = 0;
552 for (i = 0; visuals[i].format; i++) {
553 int format_count = 0;
554
555 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
556 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
557 struct dri2_egl_config *dri2_conf;
558 unsigned int double_buffered = 0;
559
560 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
561 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
562
563 /* support only double buffered configs */
564 if (!double_buffered)
565 continue;
566
567 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
568 count + 1, visuals[i].size, surface_type, NULL,
569 visuals[i].rgba_masks);
570 if (dri2_conf) {
571 dri2_conf->base.NativeVisualID = visuals[i].format;
572 dri2_conf->base.NativeVisualType = visuals[i].format;
573 count++;
574 format_count++;
575 }
576 }
577
578 if (!format_count) {
579 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
580 visuals[i].format);
581 }
582 }
583
584 /* post-process configs */
585 for (i = 0; i < dpy->Configs->Size; i++) {
586 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
587
588 /* there is no front buffer so no OpenGL */
589 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
590 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
591 }
592
593 return (count != 0);
594 }
595
596 static EGLBoolean
597 droid_get_pci_id(int fd, int *vendor_id, int *chip_id)
598 {
599 drmVersionPtr version;
600
601 *chip_id = -1;
602
603 version = drmGetVersion(fd);
604 if (!version) {
605 _eglLog(_EGL_WARNING, "invalid drm fd");
606 return EGL_FALSE;
607 }
608 if (!version->name) {
609 _eglLog(_EGL_WARNING, "unable to determine the driver name");
610 drmFreeVersion(version);
611 return EGL_FALSE;
612 }
613
614 if (strcmp(version->name, "i915") == 0) {
615 struct drm_i915_getparam gp;
616 int ret;
617
618 *vendor_id = 0x8086;
619
620 memset(&gp, 0, sizeof(gp));
621 gp.param = I915_PARAM_CHIPSET_ID;
622 gp.value = chip_id;
623 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
624 if (ret) {
625 _eglLog(_EGL_WARNING, "failed to get param for i915");
626 *chip_id = -1;
627 }
628 }
629 else if (strcmp(version->name, "radeon") == 0) {
630 struct drm_radeon_info info;
631 int ret;
632
633 *vendor_id = 0x1002;
634
635 memset(&info, 0, sizeof(info));
636 info.request = RADEON_INFO_DEVICE_ID;
637 info.value = (unsigned long) chip_id;
638 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
639 if (ret) {
640 _eglLog(_EGL_WARNING, "failed to get info for radeon");
641 *chip_id = -1;
642 }
643 }
644 else if (strcmp(version->name, "nouveau") == 0) {
645 *vendor_id = 0x10de;
646 /* not used */
647 *chip_id = 0;
648 }
649 else if (strcmp(version->name, "vmwgfx") == 0) {
650 *vendor_id = 0x15ad;
651 /* assume SVGA II */
652 *chip_id = 0x0405;
653 }
654
655 drmFreeVersion(version);
656
657 return (*chip_id >= 0);
658 }
659
660 #define DRIVER_MAP_DRI2_ONLY
661 #include "pci_ids/pci_id_driver_map.h"
662 static const char *
663 droid_get_driver_name(int fd)
664 {
665 int vendor_id = -1, chip_id = -1;
666 int idx, i;
667 char *name;
668
669 if (!droid_get_pci_id(fd, &vendor_id, &chip_id))
670 return NULL;
671
672 for (idx = 0; driver_map[idx].driver; idx++) {
673 if (vendor_id != driver_map[idx].vendor_id)
674 continue;
675
676 if (driver_map[idx].num_chips_ids == -1)
677 break;
678
679 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
680 if (driver_map[idx].chip_ids[i] == chip_id)
681 break;
682 }
683 if (i < driver_map[idx].num_chips_ids)
684 break;
685 }
686
687 _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s",
688 fd, vendor_id, chip_id, driver_map[idx].driver);
689
690 return driver_map[idx].driver;
691 }
692
693 static int
694 droid_open_device(void)
695 {
696 const hw_module_t *mod;
697 int fd = -1, err;
698
699 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
700 if (!err) {
701 const gralloc_module_t *gr = (gralloc_module_t *) mod;
702
703 err = -EINVAL;
704 if (gr->perform)
705 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
706 }
707 if (err || fd < 0) {
708 _eglLog(_EGL_WARNING, "fail to get drm fd");
709 fd = -1;
710 }
711
712 return (fd >= 0) ? dup(fd) : -1;
713 }
714
715 /* support versions < JellyBean */
716 #ifndef ALOGW
717 #define ALOGW LOGW
718 #endif
719 #ifndef ALOGD
720 #define ALOGD LOGD
721 #endif
722 #ifndef ALOGI
723 #define ALOGI LOGI
724 #endif
725
726 static void
727 droid_log(EGLint level, const char *msg)
728 {
729 switch (level) {
730 case _EGL_DEBUG:
731 ALOGD("%s", msg);
732 break;
733 case _EGL_INFO:
734 ALOGI("%s", msg);
735 break;
736 case _EGL_WARNING:
737 ALOGW("%s", msg);
738 break;
739 case _EGL_FATAL:
740 LOG_FATAL("%s", msg);
741 break;
742 default:
743 break;
744 }
745 }
746
747 EGLBoolean
748 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
749 {
750 struct dri2_egl_display *dri2_dpy;
751 const char *err;
752
753 _eglSetLogProc(droid_log);
754
755 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
756 if (!dri2_dpy)
757 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
758
759 dpy->DriverData = (void *) dri2_dpy;
760
761 dri2_dpy->fd = droid_open_device();
762 if (dri2_dpy->fd < 0) {
763 err = "DRI2: failed to open device";
764 goto cleanup_display;
765 }
766
767 dri2_dpy->driver_name = (char *) droid_get_driver_name(dri2_dpy->fd);
768 if (dri2_dpy->driver_name == NULL) {
769 err = "DRI2: failed to get driver name";
770 goto cleanup_device;
771 }
772
773 if (!dri2_load_driver(dpy)) {
774 err = "DRI2: failed to load driver";
775 goto cleanup_device;
776 }
777
778 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
779 dri2_dpy->dri2_loader_extension.base.version = 3;
780 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
781 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
782 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
783 droid_get_buffers_with_format;
784
785 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
786 dri2_dpy->extensions[1] = &image_lookup_extension.base;
787 dri2_dpy->extensions[2] = &use_invalidate.base;
788 dri2_dpy->extensions[3] = NULL;
789
790 if (!dri2_create_screen(dpy)) {
791 err = "DRI2: failed to create screen";
792 goto cleanup_driver;
793 }
794
795 if (!droid_add_configs_for_visuals(drv, dpy)) {
796 err = "DRI2: failed to add configs";
797 goto cleanup_screen;
798 }
799
800 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
801 dpy->Extensions.KHR_image_base = EGL_TRUE;
802
803 /* we're supporting EGL 1.4 */
804 dpy->VersionMajor = 1;
805 dpy->VersionMinor = 4;
806
807 droid_init_driver_functions(drv);
808
809 return EGL_TRUE;
810
811 cleanup_screen:
812 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
813 cleanup_driver:
814 dlclose(dri2_dpy->driver);
815 cleanup_device:
816 close(dri2_dpy->fd);
817 cleanup_display:
818 free(dri2_dpy);
819
820 return _eglError(EGL_NOT_INITIALIZED, err);
821 }