2 * Mesa 3-D graphics library
5 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
6 * Copyright (C) 2010-2011 LunarG Inc.
8 * Based on platform_x11, which has
10 * Copyright © 2011 Intel Corporation
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
34 /* for droid_get_pci_id */
37 #include <radeon_drm.h>
42 get_format_bpp(int native)
47 case HAL_PIXEL_FORMAT_RGBA_8888:
48 case HAL_PIXEL_FORMAT_RGBX_8888:
49 case HAL_PIXEL_FORMAT_BGRA_8888:
52 case HAL_PIXEL_FORMAT_RGB_888:
55 case HAL_PIXEL_FORMAT_RGB_565:
56 case HAL_PIXEL_FORMAT_RGBA_5551:
57 case HAL_PIXEL_FORMAT_RGBA_4444:
69 get_native_buffer_name(struct android_native_buffer_t *buf)
71 struct gralloc_drm_handle_t *handle;
73 /* check that the buffer is allocated by drm_gralloc and cast */
74 handle = gralloc_drm_handle(buf->handle);
76 return (handle) ? handle->name : 0;
80 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
82 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
85 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
86 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
92 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
94 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
96 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
97 dri2_surf->buffer = NULL;
103 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
105 /* no cancel buffer? */
106 droid_window_enqueue_buffer(dri2_surf);
110 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
111 unsigned int att, unsigned int format)
113 struct dri2_egl_display *dri2_dpy =
114 dri2_egl_display(dri2_surf->base.Resource.Display);
116 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
119 if (!dri2_surf->local_buffers[att]) {
120 dri2_surf->local_buffers[att] =
121 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
122 dri2_surf->base.Width, dri2_surf->base.Height);
125 return dri2_surf->local_buffers[att];
129 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
131 struct dri2_egl_display *dri2_dpy =
132 dri2_egl_display(dri2_surf->base.Resource.Display);
135 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
136 if (dri2_surf->local_buffers[i]) {
137 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
138 dri2_surf->local_buffers[i]);
139 dri2_surf->local_buffers[i] = NULL;
145 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
146 _EGLConfig *conf, EGLNativeWindowType window,
147 const EGLint *attrib_list)
149 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
150 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
151 struct dri2_egl_surface *dri2_surf;
156 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
157 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
160 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
161 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
164 if (format != dri2_conf->base.NativeVisualID) {
165 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
166 format, dri2_conf->base.NativeVisualID);
169 dri2_surf = calloc(1, sizeof *dri2_surf);
171 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
175 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
178 dri2_surf->dri_drawable =
179 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
180 dri2_conf->dri_double_config,
182 if (dri2_surf->dri_drawable == NULL) {
183 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
187 window->common.incRef(&window->common);
188 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
189 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
191 dri2_surf->window = window;
193 return &dri2_surf->base;
202 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
203 _EGLConfig *conf, EGLNativeWindowType window,
204 const EGLint *attrib_list)
206 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
207 window, attrib_list);
211 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
212 _EGLConfig *conf, EGLNativePixmapType pixmap,
213 const EGLint *attrib_list)
219 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
220 _EGLConfig *conf, const EGLint *attrib_list)
226 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
228 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
229 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
231 if (!_eglPutSurface(surf))
234 droid_free_local_buffers(dri2_surf);
236 if (dri2_surf->buffer)
237 droid_window_cancel_buffer(dri2_surf);
239 dri2_surf->window->common.decRef(&dri2_surf->window->common);
241 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
249 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
251 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
252 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
253 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
256 if (dri2_drv->glFlush) {
257 ctx = _eglGetCurrentContext();
258 if (ctx && ctx->DrawSurface == &dri2_surf->base)
262 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
264 if (dri2_surf->buffer)
265 droid_window_enqueue_buffer(dri2_surf);
267 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
273 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
274 struct android_native_buffer_t *buf)
276 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
277 struct dri2_egl_image *dri2_img;
281 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
282 buf->common.version != sizeof(*buf)) {
283 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
287 name = get_native_buffer_name(buf);
289 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
293 /* see the table in droid_add_configs_for_visuals */
294 switch (buf->format) {
295 case HAL_PIXEL_FORMAT_BGRA_8888:
296 format = __DRI_IMAGE_FORMAT_ARGB8888;
298 case HAL_PIXEL_FORMAT_RGB_565:
299 format = __DRI_IMAGE_FORMAT_RGB565;
301 case HAL_PIXEL_FORMAT_RGBA_8888:
302 case HAL_PIXEL_FORMAT_RGBX_8888:
303 case HAL_PIXEL_FORMAT_RGB_888:
304 case HAL_PIXEL_FORMAT_RGBA_5551:
305 case HAL_PIXEL_FORMAT_RGBA_4444:
308 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
313 dri2_img = calloc(1, sizeof(*dri2_img));
315 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
319 if (!_eglInitImage(&dri2_img->base, disp)) {
324 dri2_img->dri_image =
325 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
332 if (!dri2_img->dri_image) {
334 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
338 return &dri2_img->base;
342 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
343 _EGLContext *ctx, EGLenum target,
344 EGLClientBuffer buffer, const EGLint *attr_list)
347 case EGL_NATIVE_BUFFER_ANDROID:
348 return dri2_create_image_android_native_buffer(disp,
349 (struct android_native_buffer_t *) buffer);
351 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
356 droid_init_driver_functions(_EGLDriver *drv)
358 drv->API.CreateWindowSurface = droid_create_window_surface;
359 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
360 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
361 drv->API.DestroySurface = droid_destroy_surface;
362 drv->API.SwapBuffers = droid_swap_buffers;
364 drv->API.CreateImageKHR = droid_create_image_khr;
368 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
373 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
374 unsigned int *attachments, int count)
376 int num_buffers = 0, i;
378 /* fill dri2_surf->buffers */
379 for (i = 0; i < count * 2; i += 2) {
380 __DRIbuffer *buf, *local;
382 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
383 buf = &dri2_surf->buffers[num_buffers];
385 switch (attachments[i]) {
386 case __DRI_BUFFER_BACK_LEFT:
387 buf->attachment = attachments[i];
388 buf->name = get_native_buffer_name(dri2_surf->buffer);
389 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
390 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
396 case __DRI_BUFFER_DEPTH:
397 case __DRI_BUFFER_STENCIL:
398 case __DRI_BUFFER_ACCUM:
399 case __DRI_BUFFER_DEPTH_STENCIL:
400 case __DRI_BUFFER_HIZ:
401 local = droid_alloc_local_buffer(dri2_surf,
402 attachments[i], attachments[i + 1]);
409 case __DRI_BUFFER_FRONT_LEFT:
410 case __DRI_BUFFER_FRONT_RIGHT:
411 case __DRI_BUFFER_FAKE_FRONT_LEFT:
412 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
413 case __DRI_BUFFER_BACK_RIGHT:
415 /* no front or right buffers */
424 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
425 int *width, int *height,
426 unsigned int *attachments, int count,
427 int *out_count, void *loaderPrivate)
429 struct dri2_egl_surface *dri2_surf = loaderPrivate;
430 struct dri2_egl_display *dri2_dpy =
431 dri2_egl_display(dri2_surf->base.Resource.Display);
434 if (!dri2_surf->buffer) {
435 if (!droid_window_dequeue_buffer(dri2_surf))
439 /* free outdated buffers and update the surface size */
440 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
441 dri2_surf->base.Height != dri2_surf->buffer->height) {
442 droid_free_local_buffers(dri2_surf);
443 dri2_surf->base.Width = dri2_surf->buffer->width;
444 dri2_surf->base.Height = dri2_surf->buffer->height;
447 dri2_surf->buffer_count =
448 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
451 *width = dri2_surf->base.Width;
453 *height = dri2_surf->base.Height;
455 *out_count = dri2_surf->buffer_count;;
457 return dri2_surf->buffers;
461 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
463 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
467 unsigned int rgba_masks[4];
469 { HAL_PIXEL_FORMAT_RGBA_8888, 32, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
470 { HAL_PIXEL_FORMAT_RGBX_8888, 32, { 0xff, 0xff00, 0xff0000, 0x0 } },
471 { HAL_PIXEL_FORMAT_RGB_888, 24, { 0xff, 0xff00, 0xff0000, 0x0 } },
472 { HAL_PIXEL_FORMAT_RGB_565, 16, { 0xf800, 0x7e0, 0x1f, 0x0 } },
473 { HAL_PIXEL_FORMAT_BGRA_8888, 32, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
474 { 0, 0, { 0, 0, 0, 0 } }
479 for (i = 0; visuals[i].format; i++) {
480 int format_count = 0;
482 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
483 struct dri2_egl_config *dri2_conf;
485 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
486 count + 1, visuals[i].size, EGL_WINDOW_BIT, NULL,
487 visuals[i].rgba_masks);
489 dri2_conf->base.NativeVisualID = visuals[i].format;
490 dri2_conf->base.NativeVisualType = visuals[i].format;
497 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
502 /* post-process configs */
503 for (i = 0; i < dpy->Configs->Size; i++) {
504 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
506 /* there is no front buffer so no OpenGL */
507 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
508 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
510 /* and we want to make sure GL_DRAW_BUFFER is always GL_BACK */
511 if (!dri2_conf->dri_double_config)
512 dri2_conf->base.SurfaceType &= ~EGL_WINDOW_BIT;
519 droid_get_pci_id(int fd, int *vendor_id, int *chip_id)
521 drmVersionPtr version;
525 version = drmGetVersion(fd);
527 _eglLog(_EGL_WARNING, "invalid drm fd");
530 if (!version->name) {
531 _eglLog(_EGL_WARNING, "unable to determine the driver name");
532 drmFreeVersion(version);
536 if (strcmp(version->name, "i915") == 0) {
537 struct drm_i915_getparam gp;
542 memset(&gp, 0, sizeof(gp));
543 gp.param = I915_PARAM_CHIPSET_ID;
545 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
547 _eglLog(_EGL_WARNING, "failed to get param for i915");
551 else if (strcmp(version->name, "radeon") == 0) {
552 struct drm_radeon_info info;
557 memset(&info, 0, sizeof(info));
558 info.request = RADEON_INFO_DEVICE_ID;
559 info.value = (unsigned long) chip_id;
560 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
562 _eglLog(_EGL_WARNING, "failed to get info for radeon");
566 else if (strcmp(version->name, "nouveau") == 0) {
571 else if (strcmp(version->name, "vmwgfx") == 0) {
577 drmFreeVersion(version);
579 return (*chip_id >= 0);
582 #define DRIVER_MAP_DRI2_ONLY
583 #include "pci_ids/pci_id_driver_map.h"
585 droid_get_driver_name(int fd)
587 int vendor_id = -1, chip_id = -1;
591 if (!droid_get_pci_id(fd, &vendor_id, &chip_id))
594 for (idx = 0; driver_map[idx].driver; idx++) {
595 if (vendor_id != driver_map[idx].vendor_id)
598 if (driver_map[idx].num_chips_ids == -1)
601 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
602 if (driver_map[idx].chip_ids[i] == chip_id)
605 if (i < driver_map[idx].num_chips_ids)
609 _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s",
610 fd, vendor_id, chip_id, driver_map[idx].driver);
612 return driver_map[idx].driver;
616 droid_open_device(void)
618 const hw_module_t *mod;
621 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
623 const gralloc_module_t *gr = (gralloc_module_t *) mod;
627 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
630 _eglLog(_EGL_WARNING, "fail to get drm fd");
634 return (fd >= 0) ? dup(fd) : -1;
638 droid_log(EGLint level, const char *msg)
651 LOG_FATAL("%s", msg);
659 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
661 struct dri2_egl_display *dri2_dpy;
664 _eglSetLogProc(droid_log);
666 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
668 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
670 dpy->DriverData = (void *) dri2_dpy;
672 dri2_dpy->fd = droid_open_device();
673 if (dri2_dpy->fd < 0) {
674 err = "DRI2: failed to open device";
675 goto cleanup_display;
678 dri2_dpy->driver_name = (char *) droid_get_driver_name(dri2_dpy->fd);
679 if (dri2_dpy->driver_name == NULL) {
680 err = "DRI2: failed to get driver name";
684 if (!dri2_load_driver(dpy)) {
685 err = "DRI2: failed to load driver";
689 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
690 dri2_dpy->dri2_loader_extension.base.version = 3;
691 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
692 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
693 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
694 droid_get_buffers_with_format;
696 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
697 dri2_dpy->extensions[1] = &image_lookup_extension.base;
698 dri2_dpy->extensions[2] = &use_invalidate.base;
699 dri2_dpy->extensions[3] = NULL;
701 if (!dri2_create_screen(dpy)) {
702 err = "DRI2: failed to create screen";
706 if (!droid_add_configs_for_visuals(drv, dpy)) {
707 err = "DRI2: failed to add configs";
711 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
712 dpy->Extensions.KHR_image_base = EGL_TRUE;
714 /* we're supporting EGL 1.4 */
715 dpy->VersionMajor = 1;
716 dpy->VersionMinor = 4;
718 droid_init_driver_functions(drv);
723 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
725 dlclose(dri2_dpy->driver);
731 return _eglError(EGL_NOT_INITIALIZED, err);