Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 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 | /* for droid_get_pci_id */ |
| 35 | #include <xf86drm.h> |
| 36 | #include <i915_drm.h> |
| 37 | #include <radeon_drm.h> |
| 38 | |
| 39 | #include "egl_dri2.h" |
| 40 | |
| 41 | static int |
| 42 | get_format_bpp(int native) |
| 43 | { |
| 44 | int bpp; |
| 45 | |
| 46 | switch (native) { |
| 47 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 48 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 49 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 50 | bpp = 4; |
| 51 | break; |
| 52 | case HAL_PIXEL_FORMAT_RGB_888: |
| 53 | bpp = 3; |
| 54 | break; |
| 55 | case HAL_PIXEL_FORMAT_RGB_565: |
| 56 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 57 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 58 | bpp = 2; |
| 59 | break; |
| 60 | default: |
| 61 | bpp = 0; |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | return bpp; |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | get_native_buffer_name(struct android_native_buffer_t *buf) |
| 70 | { |
| 71 | struct gralloc_drm_handle_t *handle; |
| 72 | |
| 73 | /* check that the buffer is allocated by drm_gralloc and cast */ |
| 74 | handle = gralloc_drm_handle(buf->handle); |
| 75 | |
| 76 | return (handle) ? handle->name : 0; |
| 77 | } |
| 78 | |
| 79 | static EGLBoolean |
| 80 | droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) |
| 81 | { |
| 82 | if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer)) |
| 83 | return EGL_FALSE; |
| 84 | |
| 85 | dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common); |
| 86 | dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer); |
| 87 | |
| 88 | return EGL_TRUE; |
| 89 | } |
| 90 | |
| 91 | static EGLBoolean |
| 92 | droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf) |
| 93 | { |
| 94 | dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer); |
| 95 | |
| 96 | dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common); |
| 97 | dri2_surf->buffer = NULL; |
| 98 | |
| 99 | return EGL_TRUE; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf) |
| 104 | { |
| 105 | /* no cancel buffer? */ |
| 106 | droid_window_enqueue_buffer(dri2_surf); |
| 107 | } |
| 108 | |
| 109 | static __DRIbuffer * |
| 110 | droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf, |
| 111 | unsigned int att, unsigned int format) |
| 112 | { |
| 113 | struct dri2_egl_display *dri2_dpy = |
| 114 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 115 | |
| 116 | if (att >= ARRAY_SIZE(dri2_surf->local_buffers)) |
| 117 | return NULL; |
| 118 | |
| 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); |
| 123 | } |
| 124 | |
| 125 | return dri2_surf->local_buffers[att]; |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | droid_free_local_buffers(struct dri2_egl_surface *dri2_surf) |
| 130 | { |
| 131 | struct dri2_egl_display *dri2_dpy = |
| 132 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 133 | int i; |
| 134 | |
| 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; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static _EGLSurface * |
| 145 | droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
| 146 | _EGLConfig *conf, EGLNativeWindowType window, |
| 147 | const EGLint *attrib_list) |
| 148 | { |
| 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 | int format; |
| 153 | |
| 154 | (void) drv; |
| 155 | |
| 156 | if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) { |
| 157 | _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 158 | return NULL; |
| 159 | } |
| 160 | if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) { |
| 161 | _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface"); |
| 162 | return NULL; |
| 163 | } |
| 164 | if (format != dri2_conf->base.NativeVisualID) { |
| 165 | _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x", |
| 166 | format, dri2_conf->base.NativeVisualID); |
| 167 | } |
| 168 | |
| 169 | dri2_surf = calloc(1, sizeof *dri2_surf); |
| 170 | if (!dri2_surf) { |
| 171 | _eglError(EGL_BAD_ALLOC, "droid_create_surface"); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 176 | goto cleanup_surf; |
| 177 | |
| 178 | dri2_surf->dri_drawable = |
| 179 | (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, |
| 180 | dri2_conf->dri_double_config, |
| 181 | dri2_surf); |
| 182 | if (dri2_surf->dri_drawable == NULL) { |
| 183 | _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); |
| 184 | goto cleanup_surf; |
| 185 | } |
| 186 | |
| 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); |
| 190 | |
| 191 | dri2_surf->window = window; |
| 192 | |
| 193 | return &dri2_surf->base; |
| 194 | |
| 195 | cleanup_surf: |
| 196 | free(dri2_surf); |
| 197 | |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static _EGLSurface * |
| 202 | droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 203 | _EGLConfig *conf, EGLNativeWindowType window, |
| 204 | const EGLint *attrib_list) |
| 205 | { |
| 206 | return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
| 207 | window, attrib_list); |
| 208 | } |
| 209 | |
| 210 | static _EGLSurface * |
| 211 | droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 212 | _EGLConfig *conf, EGLNativePixmapType pixmap, |
| 213 | const EGLint *attrib_list) |
| 214 | { |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | static _EGLSurface * |
| 219 | droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 220 | _EGLConfig *conf, const EGLint *attrib_list) |
| 221 | { |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | static EGLBoolean |
| 226 | droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
| 227 | { |
| 228 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 229 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 230 | |
| 231 | if (!_eglPutSurface(surf)) |
| 232 | return EGL_TRUE; |
| 233 | |
| 234 | droid_free_local_buffers(dri2_surf); |
| 235 | |
| 236 | if (dri2_surf->buffer) |
| 237 | droid_window_cancel_buffer(dri2_surf); |
| 238 | |
| 239 | dri2_surf->window->common.decRef(&dri2_surf->window->common); |
| 240 | |
| 241 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 242 | |
| 243 | free(dri2_surf); |
| 244 | |
| 245 | return EGL_TRUE; |
| 246 | } |
| 247 | |
| 248 | static EGLBoolean |
| 249 | droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 250 | { |
| 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); |
| 254 | _EGLContext *ctx; |
| 255 | |
| 256 | if (dri2_drv->glFlush) { |
| 257 | ctx = _eglGetCurrentContext(); |
| 258 | if (ctx && ctx->DrawSurface == &dri2_surf->base) |
| 259 | dri2_drv->glFlush(); |
| 260 | } |
| 261 | |
| 262 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
| 263 | |
| 264 | if (dri2_surf->buffer) |
| 265 | droid_window_enqueue_buffer(dri2_surf); |
| 266 | |
| 267 | (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable); |
| 268 | |
| 269 | return EGL_TRUE; |
| 270 | } |
| 271 | |
| 272 | static _EGLImage * |
| 273 | dri2_create_image_android_native_buffer(_EGLDisplay *disp, |
| 274 | struct android_native_buffer_t *buf) |
| 275 | { |
| 276 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 277 | struct dri2_egl_image *dri2_img; |
| 278 | int name; |
| 279 | EGLint format; |
| 280 | |
| 281 | if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC || |
| 282 | buf->common.version != sizeof(*buf)) { |
| 283 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | name = get_native_buffer_name(buf); |
| 288 | if (!name) { |
| 289 | _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 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; |
| 297 | break; |
| 298 | case HAL_PIXEL_FORMAT_RGB_565: |
| 299 | format = __DRI_IMAGE_FORMAT_RGB565; |
| 300 | break; |
| 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: |
| 306 | /* unsupported */ |
| 307 | default: |
| 308 | _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format); |
| 309 | return NULL; |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | dri2_img = calloc(1, sizeof(*dri2_img)); |
| 314 | if (!dri2_img) { |
| 315 | _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 320 | free(dri2_img); |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | dri2_img->dri_image = |
| 325 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 326 | buf->width, |
| 327 | buf->height, |
| 328 | format, |
| 329 | name, |
| 330 | buf->stride, |
| 331 | dri2_img); |
| 332 | if (!dri2_img->dri_image) { |
| 333 | free(dri2_img); |
| 334 | _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm"); |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | return &dri2_img->base; |
| 339 | } |
| 340 | |
| 341 | static _EGLImage * |
| 342 | droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 343 | _EGLContext *ctx, EGLenum target, |
| 344 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 345 | { |
| 346 | switch (target) { |
| 347 | case EGL_NATIVE_BUFFER_ANDROID: |
| 348 | return dri2_create_image_android_native_buffer(disp, |
| 349 | (struct android_native_buffer_t *) buffer); |
| 350 | default: |
| 351 | return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | droid_init_driver_functions(_EGLDriver *drv) |
| 357 | { |
| 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; |
| 363 | |
| 364 | drv->API.CreateImageKHR = droid_create_image_khr; |
| 365 | } |
| 366 | |
| 367 | static void |
| 368 | droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 369 | { |
| 370 | } |
| 371 | |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame^] | 372 | static int |
| 373 | droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf, |
| 374 | unsigned int *attachments, int count) |
| 375 | { |
| 376 | int num_buffers = 0, i; |
| 377 | |
| 378 | /* fill dri2_surf->buffers */ |
| 379 | for (i = 0; i < count * 2; i += 2) { |
| 380 | __DRIbuffer *buf, *local; |
| 381 | |
| 382 | assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers)); |
| 383 | buf = &dri2_surf->buffers[num_buffers]; |
| 384 | |
| 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; |
| 391 | buf->flags = 0; |
| 392 | |
| 393 | if (buf->name) |
| 394 | num_buffers++; |
| 395 | break; |
| 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]); |
| 403 | |
| 404 | if (local) { |
| 405 | *buf = *local; |
| 406 | num_buffers++; |
| 407 | } |
| 408 | break; |
| 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: |
| 414 | default: |
| 415 | /* no front or right buffers */ |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return num_buffers; |
| 421 | } |
| 422 | |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 423 | static __DRIbuffer * |
| 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) |
| 428 | { |
| 429 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 430 | struct dri2_egl_display *dri2_dpy = |
| 431 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 432 | int i; |
| 433 | |
| 434 | if (!dri2_surf->buffer) { |
| 435 | if (!droid_window_dequeue_buffer(dri2_surf)) |
| 436 | return NULL; |
| 437 | } |
| 438 | |
| 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; |
| 445 | } |
| 446 | |
Chia-I Wu | 384f228 | 2011-08-26 23:49:04 +0800 | [diff] [blame^] | 447 | dri2_surf->buffer_count = |
| 448 | droid_get_buffers_parse_attachments(dri2_surf, attachments, count); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 449 | |
| 450 | if (width) |
| 451 | *width = dri2_surf->base.Width; |
| 452 | if (height) |
| 453 | *height = dri2_surf->base.Height; |
| 454 | |
| 455 | *out_count = dri2_surf->buffer_count;; |
| 456 | |
| 457 | return dri2_surf->buffers; |
| 458 | } |
| 459 | |
| 460 | static EGLBoolean |
| 461 | droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy) |
| 462 | { |
| 463 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 464 | const struct { |
| 465 | int format; |
| 466 | int size; |
| 467 | unsigned int rgba_masks[4]; |
| 468 | } visuals[] = { |
| 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 } } |
| 475 | }; |
| 476 | int count, i, j; |
| 477 | |
| 478 | count = 0; |
| 479 | for (i = 0; visuals[i].format; i++) { |
| 480 | int format_count = 0; |
| 481 | |
| 482 | for (j = 0; dri2_dpy->driver_configs[j]; j++) { |
| 483 | struct dri2_egl_config *dri2_conf; |
| 484 | |
| 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); |
| 488 | if (dri2_conf) { |
| 489 | dri2_conf->base.NativeVisualID = visuals[i].format; |
| 490 | dri2_conf->base.NativeVisualType = visuals[i].format; |
| 491 | count++; |
| 492 | format_count++; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (!format_count) { |
| 497 | _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", |
| 498 | visuals[i].format); |
| 499 | } |
| 500 | } |
| 501 | |
| 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]); |
| 505 | |
| 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; |
| 509 | |
| 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; |
| 513 | } |
| 514 | |
| 515 | return (count != 0); |
| 516 | } |
| 517 | |
| 518 | static EGLBoolean |
| 519 | droid_get_pci_id(int fd, int *vendor_id, int *chip_id) |
| 520 | { |
| 521 | drmVersionPtr version; |
| 522 | |
| 523 | *chip_id = -1; |
| 524 | |
| 525 | version = drmGetVersion(fd); |
| 526 | if (!version) { |
| 527 | _eglLog(_EGL_WARNING, "invalid drm fd"); |
| 528 | return EGL_FALSE; |
| 529 | } |
| 530 | if (!version->name) { |
| 531 | _eglLog(_EGL_WARNING, "unable to determine the driver name"); |
| 532 | drmFreeVersion(version); |
| 533 | return EGL_FALSE; |
| 534 | } |
| 535 | |
| 536 | if (strcmp(version->name, "i915") == 0) { |
| 537 | struct drm_i915_getparam gp; |
| 538 | int ret; |
| 539 | |
| 540 | *vendor_id = 0x8086; |
| 541 | |
| 542 | memset(&gp, 0, sizeof(gp)); |
| 543 | gp.param = I915_PARAM_CHIPSET_ID; |
| 544 | gp.value = chip_id; |
| 545 | ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp)); |
| 546 | if (ret) { |
| 547 | _eglLog(_EGL_WARNING, "failed to get param for i915"); |
| 548 | *chip_id = -1; |
| 549 | } |
| 550 | } |
| 551 | else if (strcmp(version->name, "radeon") == 0) { |
| 552 | struct drm_radeon_info info; |
| 553 | int ret; |
| 554 | |
| 555 | *vendor_id = 0x1002; |
| 556 | |
| 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)); |
| 561 | if (ret) { |
| 562 | _eglLog(_EGL_WARNING, "failed to get info for radeon"); |
| 563 | *chip_id = -1; |
| 564 | } |
| 565 | } |
| 566 | else if (strcmp(version->name, "nouveau") == 0) { |
| 567 | *vendor_id = 0x10de; |
| 568 | /* not used */ |
| 569 | *chip_id = 0; |
| 570 | } |
| 571 | else if (strcmp(version->name, "vmwgfx") == 0) { |
| 572 | *vendor_id = 0x15ad; |
| 573 | /* assume SVGA II */ |
| 574 | *chip_id = 0x0405; |
| 575 | } |
| 576 | |
| 577 | drmFreeVersion(version); |
| 578 | |
| 579 | return (*chip_id >= 0); |
| 580 | } |
| 581 | |
| 582 | #define DRIVER_MAP_DRI2_ONLY |
| 583 | #include "pci_ids/pci_id_driver_map.h" |
| 584 | static const char * |
| 585 | droid_get_driver_name(int fd) |
| 586 | { |
| 587 | int vendor_id = -1, chip_id = -1; |
| 588 | int idx, i; |
| 589 | char *name; |
| 590 | |
| 591 | if (!droid_get_pci_id(fd, &vendor_id, &chip_id)) |
| 592 | return NULL; |
| 593 | |
| 594 | for (idx = 0; driver_map[idx].driver; idx++) { |
| 595 | if (vendor_id != driver_map[idx].vendor_id) |
| 596 | continue; |
| 597 | |
| 598 | if (driver_map[idx].num_chips_ids == -1) |
| 599 | break; |
| 600 | |
| 601 | for (i = 0; i < driver_map[idx].num_chips_ids; i++) { |
| 602 | if (driver_map[idx].chip_ids[i] == chip_id) |
| 603 | break; |
| 604 | } |
| 605 | if (i < driver_map[idx].num_chips_ids) |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s", |
| 610 | fd, vendor_id, chip_id, driver_map[idx].driver); |
| 611 | |
| 612 | return driver_map[idx].driver; |
| 613 | } |
| 614 | |
| 615 | static int |
| 616 | droid_open_device(void) |
| 617 | { |
| 618 | const hw_module_t *mod; |
| 619 | int fd = -1, err; |
| 620 | |
| 621 | err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod); |
| 622 | if (!err) { |
| 623 | const gralloc_module_t *gr = (gralloc_module_t *) mod; |
| 624 | |
| 625 | err = -EINVAL; |
| 626 | if (gr->perform) |
| 627 | err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd); |
| 628 | } |
| 629 | if (err || fd < 0) { |
| 630 | _eglLog(_EGL_WARNING, "fail to get drm fd"); |
| 631 | fd = -1; |
| 632 | } |
| 633 | |
| 634 | return (fd >= 0) ? dup(fd) : -1; |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | droid_log(EGLint level, const char *msg) |
| 639 | { |
| 640 | switch (level) { |
| 641 | case _EGL_DEBUG: |
| 642 | LOGD("%s", msg); |
| 643 | break; |
| 644 | case _EGL_INFO: |
| 645 | LOGI("%s", msg); |
| 646 | break; |
| 647 | case _EGL_WARNING: |
| 648 | LOGW("%s", msg); |
| 649 | break; |
| 650 | case _EGL_FATAL: |
| 651 | LOG_FATAL("%s", msg); |
| 652 | break; |
| 653 | default: |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | EGLBoolean |
| 659 | dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy) |
| 660 | { |
| 661 | struct dri2_egl_display *dri2_dpy; |
| 662 | const char *err; |
| 663 | |
| 664 | _eglSetLogProc(droid_log); |
| 665 | |
| 666 | dri2_dpy = calloc(1, sizeof(*dri2_dpy)); |
| 667 | if (!dri2_dpy) |
| 668 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 669 | |
| 670 | dpy->DriverData = (void *) dri2_dpy; |
| 671 | |
| 672 | dri2_dpy->fd = droid_open_device(); |
| 673 | if (dri2_dpy->fd < 0) { |
| 674 | err = "DRI2: failed to open device"; |
| 675 | goto cleanup_display; |
| 676 | } |
| 677 | |
| 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"; |
| 681 | goto cleanup_device; |
| 682 | } |
| 683 | |
| 684 | if (!dri2_load_driver(dpy)) { |
| 685 | err = "DRI2: failed to load driver"; |
| 686 | goto cleanup_device; |
| 687 | } |
| 688 | |
| 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; |
| 695 | |
| 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; |
| 700 | |
| 701 | if (!dri2_create_screen(dpy)) { |
| 702 | err = "DRI2: failed to create screen"; |
| 703 | goto cleanup_driver; |
| 704 | } |
| 705 | |
| 706 | if (!droid_add_configs_for_visuals(drv, dpy)) { |
| 707 | err = "DRI2: failed to add configs"; |
| 708 | goto cleanup_screen; |
| 709 | } |
| 710 | |
| 711 | dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE; |
| 712 | dpy->Extensions.KHR_image_base = EGL_TRUE; |
| 713 | |
| 714 | /* we're supporting EGL 1.4 */ |
| 715 | dpy->VersionMajor = 1; |
| 716 | dpy->VersionMinor = 4; |
| 717 | |
| 718 | droid_init_driver_functions(drv); |
| 719 | |
| 720 | return EGL_TRUE; |
| 721 | |
| 722 | cleanup_screen: |
| 723 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 724 | cleanup_driver: |
| 725 | dlclose(dri2_dpy->driver); |
| 726 | cleanup_device: |
| 727 | close(dri2_dpy->fd); |
| 728 | cleanup_display: |
| 729 | free(dri2_dpy); |
| 730 | |
| 731 | return _eglError(EGL_NOT_INITIALIZED, err); |
| 732 | } |