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 ANativeWindowBuffer *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;
152 dri2_surf = calloc(1, sizeof *dri2_surf);
154 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
158 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
159 goto cleanup_surface;
161 if (type == EGL_WINDOW_BIT) {
164 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
165 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
166 goto cleanup_surface;
168 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
169 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
170 goto cleanup_surface;
173 if (format != dri2_conf->base.NativeVisualID) {
174 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
175 format, dri2_conf->base.NativeVisualID);
178 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
179 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
182 dri2_surf->dri_drawable =
183 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
184 dri2_conf->dri_double_config,
186 if (dri2_surf->dri_drawable == NULL) {
187 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
188 goto cleanup_surface;
192 window->common.incRef(&window->common);
193 dri2_surf->window = window;
196 return &dri2_surf->base;
205 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
206 _EGLConfig *conf, EGLNativeWindowType window,
207 const EGLint *attrib_list)
209 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
210 window, attrib_list);
214 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
215 _EGLConfig *conf, EGLNativePixmapType pixmap,
216 const EGLint *attrib_list)
222 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
223 _EGLConfig *conf, const EGLint *attrib_list)
225 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
230 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
232 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
233 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
235 if (!_eglPutSurface(surf))
238 droid_free_local_buffers(dri2_surf);
240 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
241 if (dri2_surf->buffer)
242 droid_window_cancel_buffer(dri2_surf);
244 dri2_surf->window->common.decRef(&dri2_surf->window->common);
247 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
255 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
257 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
258 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
259 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
262 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
265 if (dri2_drv->glFlush) {
266 ctx = _eglGetCurrentContext();
267 if (ctx && ctx->DrawSurface == &dri2_surf->base)
271 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
273 if (dri2_surf->buffer)
274 droid_window_enqueue_buffer(dri2_surf);
276 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
282 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
283 struct ANativeWindowBuffer *buf)
285 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
286 struct dri2_egl_image *dri2_img;
290 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
291 buf->common.version != sizeof(*buf)) {
292 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
296 name = get_native_buffer_name(buf);
298 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
302 /* see the table in droid_add_configs_for_visuals */
303 switch (buf->format) {
304 case HAL_PIXEL_FORMAT_BGRA_8888:
305 format = __DRI_IMAGE_FORMAT_ARGB8888;
307 case HAL_PIXEL_FORMAT_RGB_565:
308 format = __DRI_IMAGE_FORMAT_RGB565;
310 case HAL_PIXEL_FORMAT_RGBA_8888:
311 format = __DRI_IMAGE_FORMAT_ABGR8888;
313 case HAL_PIXEL_FORMAT_RGBX_8888:
314 format = __DRI_IMAGE_FORMAT_XBGR8888;
316 case HAL_PIXEL_FORMAT_RGB_888:
317 case HAL_PIXEL_FORMAT_RGBA_5551:
318 case HAL_PIXEL_FORMAT_RGBA_4444:
321 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
326 dri2_img = calloc(1, sizeof(*dri2_img));
328 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
332 if (!_eglInitImage(&dri2_img->base, disp)) {
337 dri2_img->dri_image =
338 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
345 if (!dri2_img->dri_image) {
347 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
351 return &dri2_img->base;
355 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
356 _EGLContext *ctx, EGLenum target,
357 EGLClientBuffer buffer, const EGLint *attr_list)
360 case EGL_NATIVE_BUFFER_ANDROID:
361 return dri2_create_image_android_native_buffer(disp,
362 (struct ANativeWindowBuffer *) buffer);
364 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
369 droid_init_driver_functions(_EGLDriver *drv)
371 drv->API.CreateWindowSurface = droid_create_window_surface;
372 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
373 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
374 drv->API.DestroySurface = droid_destroy_surface;
375 drv->API.SwapBuffers = droid_swap_buffers;
377 drv->API.CreateImageKHR = droid_create_image_khr;
381 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
386 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
387 unsigned int *attachments, int count)
389 int num_buffers = 0, i;
391 /* fill dri2_surf->buffers */
392 for (i = 0; i < count * 2; i += 2) {
393 __DRIbuffer *buf, *local;
395 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
396 buf = &dri2_surf->buffers[num_buffers];
398 switch (attachments[i]) {
399 case __DRI_BUFFER_BACK_LEFT:
400 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
401 buf->attachment = attachments[i];
402 buf->name = get_native_buffer_name(dri2_surf->buffer);
403 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
404 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
412 /* fall through for pbuffers */
413 case __DRI_BUFFER_DEPTH:
414 case __DRI_BUFFER_STENCIL:
415 case __DRI_BUFFER_ACCUM:
416 case __DRI_BUFFER_DEPTH_STENCIL:
417 case __DRI_BUFFER_HIZ:
418 local = droid_alloc_local_buffer(dri2_surf,
419 attachments[i], attachments[i + 1]);
426 case __DRI_BUFFER_FRONT_LEFT:
427 case __DRI_BUFFER_FRONT_RIGHT:
428 case __DRI_BUFFER_FAKE_FRONT_LEFT:
429 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
430 case __DRI_BUFFER_BACK_RIGHT:
432 /* no front or right buffers */
441 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
442 int *width, int *height,
443 unsigned int *attachments, int count,
444 int *out_count, void *loaderPrivate)
446 struct dri2_egl_surface *dri2_surf = loaderPrivate;
447 struct dri2_egl_display *dri2_dpy =
448 dri2_egl_display(dri2_surf->base.Resource.Display);
451 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
452 /* try to dequeue the next back buffer */
453 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
456 /* free outdated buffers and update the surface size */
457 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
458 dri2_surf->base.Height != dri2_surf->buffer->height) {
459 droid_free_local_buffers(dri2_surf);
460 dri2_surf->base.Width = dri2_surf->buffer->width;
461 dri2_surf->base.Height = dri2_surf->buffer->height;
465 dri2_surf->buffer_count =
466 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
469 *width = dri2_surf->base.Width;
471 *height = dri2_surf->base.Height;
473 *out_count = dri2_surf->buffer_count;;
475 return dri2_surf->buffers;
479 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
481 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
485 unsigned int rgba_masks[4];
487 { HAL_PIXEL_FORMAT_RGBA_8888, 32, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
488 { HAL_PIXEL_FORMAT_RGBX_8888, 32, { 0xff, 0xff00, 0xff0000, 0x0 } },
489 { HAL_PIXEL_FORMAT_RGB_888, 24, { 0xff, 0xff00, 0xff0000, 0x0 } },
490 { HAL_PIXEL_FORMAT_RGB_565, 16, { 0xf800, 0x7e0, 0x1f, 0x0 } },
491 { HAL_PIXEL_FORMAT_BGRA_8888, 32, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
492 { 0, 0, { 0, 0, 0, 0 } }
497 for (i = 0; visuals[i].format; i++) {
498 int format_count = 0;
500 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
501 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
502 struct dri2_egl_config *dri2_conf;
503 unsigned int double_buffered = 0;
505 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
506 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
508 /* support only double buffered configs */
509 if (!double_buffered)
512 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
513 count + 1, visuals[i].size, surface_type, NULL,
514 visuals[i].rgba_masks);
516 dri2_conf->base.NativeVisualID = visuals[i].format;
517 dri2_conf->base.NativeVisualType = visuals[i].format;
524 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
529 /* post-process configs */
530 for (i = 0; i < dpy->Configs->Size; i++) {
531 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
533 /* there is no front buffer so no OpenGL */
534 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
535 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
542 droid_get_pci_id(int fd, int *vendor_id, int *chip_id)
544 drmVersionPtr version;
548 version = drmGetVersion(fd);
550 _eglLog(_EGL_WARNING, "invalid drm fd");
553 if (!version->name) {
554 _eglLog(_EGL_WARNING, "unable to determine the driver name");
555 drmFreeVersion(version);
559 if (strcmp(version->name, "i915") == 0) {
560 struct drm_i915_getparam gp;
565 memset(&gp, 0, sizeof(gp));
566 gp.param = I915_PARAM_CHIPSET_ID;
568 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
570 _eglLog(_EGL_WARNING, "failed to get param for i915");
574 else if (strcmp(version->name, "radeon") == 0) {
575 struct drm_radeon_info info;
580 memset(&info, 0, sizeof(info));
581 info.request = RADEON_INFO_DEVICE_ID;
582 info.value = (unsigned long) chip_id;
583 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
585 _eglLog(_EGL_WARNING, "failed to get info for radeon");
589 else if (strcmp(version->name, "nouveau") == 0) {
594 else if (strcmp(version->name, "vmwgfx") == 0) {
600 drmFreeVersion(version);
602 return (*chip_id >= 0);
605 #define DRIVER_MAP_DRI2_ONLY
606 #include "pci_ids/pci_id_driver_map.h"
608 droid_get_driver_name(int fd)
610 int vendor_id = -1, chip_id = -1;
614 if (!droid_get_pci_id(fd, &vendor_id, &chip_id))
617 for (idx = 0; driver_map[idx].driver; idx++) {
618 if (vendor_id != driver_map[idx].vendor_id)
621 if (driver_map[idx].num_chips_ids == -1)
624 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
625 if (driver_map[idx].chip_ids[i] == chip_id)
628 if (i < driver_map[idx].num_chips_ids)
632 _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s",
633 fd, vendor_id, chip_id, driver_map[idx].driver);
635 return driver_map[idx].driver;
639 droid_open_device(void)
641 const hw_module_t *mod;
644 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
646 const gralloc_module_t *gr = (gralloc_module_t *) mod;
650 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
653 _eglLog(_EGL_WARNING, "fail to get drm fd");
657 return (fd >= 0) ? dup(fd) : -1;
660 /* support versions < JellyBean */
672 droid_log(EGLint level, const char *msg)
685 LOG_FATAL("%s", msg);
693 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
695 struct dri2_egl_display *dri2_dpy;
698 _eglSetLogProc(droid_log);
700 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
702 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
704 dpy->DriverData = (void *) dri2_dpy;
706 dri2_dpy->fd = droid_open_device();
707 if (dri2_dpy->fd < 0) {
708 err = "DRI2: failed to open device";
709 goto cleanup_display;
712 dri2_dpy->driver_name = (char *) droid_get_driver_name(dri2_dpy->fd);
713 if (dri2_dpy->driver_name == NULL) {
714 err = "DRI2: failed to get driver name";
718 if (!dri2_load_driver(dpy)) {
719 err = "DRI2: failed to load driver";
723 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
724 dri2_dpy->dri2_loader_extension.base.version = 3;
725 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
726 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
727 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
728 droid_get_buffers_with_format;
730 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
731 dri2_dpy->extensions[1] = &image_lookup_extension.base;
732 dri2_dpy->extensions[2] = &use_invalidate.base;
733 dri2_dpy->extensions[3] = NULL;
735 if (!dri2_create_screen(dpy)) {
736 err = "DRI2: failed to create screen";
740 if (!droid_add_configs_for_visuals(drv, dpy)) {
741 err = "DRI2: failed to add configs";
745 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
746 dpy->Extensions.KHR_image_base = EGL_TRUE;
748 /* we're supporting EGL 1.4 */
749 dpy->VersionMajor = 1;
750 dpy->VersionMinor = 4;
752 droid_init_driver_functions(drv);
757 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
759 dlclose(dri2_dpy->driver);
765 return _eglError(EGL_NOT_INITIALIZED, err);