Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Kristian Høgsberg <krh@bitplanet.net> |
| 26 | */ |
| 27 | |
Emil Velikov | bf0e4d2 | 2015-02-28 17:12:40 +0000 | [diff] [blame] | 28 | #include <stdint.h> |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 29 | #include <stdlib.h> |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 30 | #include <stdio.h> |
Benjamin Franzke | 7f881c4 | 2011-05-30 10:49:55 +0200 | [diff] [blame] | 31 | #include <string.h> |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 32 | #include <xf86drm.h> |
Benjamin Franzke | ac3c2c8 | 2011-06-07 22:19:21 +0200 | [diff] [blame] | 33 | #include <dlfcn.h> |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <unistd.h> |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 38 | |
| 39 | #include "egl_dri2.h" |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 40 | #include "egl_dri2_fallbacks.h" |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 41 | #include "loader.h" |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 42 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 43 | static struct gbm_bo * |
| 44 | lock_front_buffer(struct gbm_surface *_surf) |
| 45 | { |
| 46 | struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf; |
| 47 | struct dri2_egl_surface *dri2_surf = surf->dri_private; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 48 | struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 49 | struct gbm_bo *bo; |
| 50 | |
| 51 | if (dri2_surf->current == NULL) { |
| 52 | _eglError(EGL_BAD_SURFACE, "no front buffer"); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | bo = dri2_surf->current->bo; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 57 | |
| 58 | if (device->dri2) { |
| 59 | dri2_surf->current->locked = 1; |
| 60 | dri2_surf->current = NULL; |
| 61 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 62 | |
| 63 | return bo; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo) |
| 68 | { |
| 69 | struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf; |
| 70 | struct dri2_egl_surface *dri2_surf = surf->dri_private; |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 71 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 72 | |
| 73 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 74 | if (dri2_surf->color_buffers[i].bo == bo) { |
| 75 | dri2_surf->color_buffers[i].locked = 0; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static int |
| 81 | has_free_buffers(struct gbm_surface *_surf) |
| 82 | { |
| 83 | struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf; |
| 84 | struct dri2_egl_surface *dri2_surf = surf->dri_private; |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 85 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 86 | |
| 87 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 88 | if (!dri2_surf->color_buffers[i].locked) |
| 89 | return 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static _EGLSurface * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 95 | dri2_drm_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 96 | _EGLConfig *conf, void *native_window, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 97 | const EGLint *attrib_list) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 98 | { |
| 99 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 100 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 101 | struct dri2_egl_surface *dri2_surf; |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 102 | struct gbm_surface *window = native_window; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 103 | struct gbm_dri_surface *surf; |
Emil Velikov | 4ea5ed9 | 2015-09-13 12:36:54 +0100 | [diff] [blame^] | 104 | const __DRIconfig *config; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 105 | |
| 106 | (void) drv; |
| 107 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 108 | dri2_surf = calloc(1, sizeof *dri2_surf); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 109 | if (!dri2_surf) { |
| 110 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 114 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 115 | goto cleanup_surf; |
| 116 | |
| 117 | switch (type) { |
| 118 | case EGL_WINDOW_BIT: |
Emil Velikov | 6098ef8 | 2015-06-18 20:16:46 +0100 | [diff] [blame] | 119 | if (!window) { |
| 120 | _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface"); |
| 121 | goto cleanup_surf; |
| 122 | } |
| 123 | |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 124 | surf = gbm_dri_surface(window); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 125 | dri2_surf->gbm_surf = surf; |
| 126 | dri2_surf->base.Width = surf->base.width; |
| 127 | dri2_surf->base.Height = surf->base.height; |
| 128 | surf->dri_private = dri2_surf; |
| 129 | break; |
| 130 | default: |
| 131 | goto cleanup_surf; |
| 132 | } |
| 133 | |
Emil Velikov | 4ea5ed9 | 2015-09-13 12:36:54 +0100 | [diff] [blame^] | 134 | config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT, |
| 135 | dri2_surf->base.GLColorspace); |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 136 | |
Emil Velikov | 4ea5ed9 | 2015-09-13 12:36:54 +0100 | [diff] [blame^] | 137 | if (dri2_dpy->dri2) { |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 138 | dri2_surf->dri_drawable = |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 139 | (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config, |
| 140 | dri2_surf->gbm_surf); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 141 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 142 | } else { |
| 143 | assert(dri2_dpy->swrast != NULL); |
Emil Velikov | 4ea5ed9 | 2015-09-13 12:36:54 +0100 | [diff] [blame^] | 144 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 145 | dri2_surf->dri_drawable = |
Emil Velikov | 4ea5ed9 | 2015-09-13 12:36:54 +0100 | [diff] [blame^] | 146 | (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen, config, |
| 147 | dri2_surf->gbm_surf); |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 148 | |
| 149 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 150 | if (dri2_surf->dri_drawable == NULL) { |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 151 | _eglError(EGL_BAD_ALLOC, "createNewDrawable()"); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 152 | goto cleanup_surf; |
| 153 | } |
| 154 | |
| 155 | return &dri2_surf->base; |
| 156 | |
| 157 | cleanup_surf: |
| 158 | free(dri2_surf); |
| 159 | |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | static _EGLSurface * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 164 | dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 165 | _EGLConfig *conf, void *native_window, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 166 | const EGLint *attrib_list) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 167 | { |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 168 | return dri2_drm_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 169 | native_window, attrib_list); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Chad Versace | 1787f56 | 2014-02-09 09:13:08 -0800 | [diff] [blame] | 172 | static _EGLSurface * |
| 173 | dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 174 | _EGLConfig *conf, void *native_window, |
| 175 | const EGLint *attrib_list) |
| 176 | { |
| 177 | /* From the EGL_MESA_platform_gbm spec, version 5: |
| 178 | * |
| 179 | * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy> |
| 180 | * that belongs to the GBM platform. Any such call fails and generates |
| 181 | * EGL_BAD_PARAMETER. |
| 182 | */ |
| 183 | _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM"); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 187 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 188 | dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 189 | { |
| 190 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 191 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 192 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 193 | |
| 194 | if (!_eglPutSurface(surf)) |
| 195 | return EGL_TRUE; |
| 196 | |
| 197 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 200 | if (dri2_surf->color_buffers[i].bo) |
| 201 | gbm_bo_destroy(dri2_surf->color_buffers[i].bo); |
| 202 | } |
| 203 | |
| 204 | for (i = 0; i < __DRI_BUFFER_COUNT; i++) { |
| 205 | if (dri2_surf->dri_buffers[i]) |
| 206 | dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen, |
| 207 | dri2_surf->dri_buffers[i]); |
| 208 | } |
| 209 | |
| 210 | free(surf); |
| 211 | |
| 212 | return EGL_TRUE; |
| 213 | } |
| 214 | |
| 215 | static int |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 216 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 217 | { |
| 218 | struct dri2_egl_display *dri2_dpy = |
| 219 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 220 | struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 221 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 222 | |
| 223 | if (dri2_surf->back == NULL) { |
| 224 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 225 | if (!dri2_surf->color_buffers[i].locked) { |
| 226 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (dri2_surf->back == NULL) |
| 233 | return -1; |
| 234 | if (dri2_surf->back->bo == NULL) |
| 235 | dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base, |
| 236 | surf->base.width, surf->base.height, |
| 237 | surf->base.format, surf->base.flags); |
| 238 | if (dri2_surf->back->bo == NULL) |
| 239 | return -1; |
| 240 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 244 | static int |
| 245 | get_swrast_front_bo(struct dri2_egl_surface *dri2_surf) |
| 246 | { |
| 247 | struct dri2_egl_display *dri2_dpy = |
| 248 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 249 | struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
| 250 | |
| 251 | if (dri2_surf->current == NULL) { |
| 252 | assert(!dri2_surf->color_buffers[0].locked); |
| 253 | dri2_surf->current = &dri2_surf->color_buffers[0]; |
| 254 | } |
| 255 | |
| 256 | if (dri2_surf->current->bo == NULL) |
| 257 | dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base, |
| 258 | surf->base.width, surf->base.height, |
| 259 | surf->base.format, surf->base.flags); |
| 260 | if (dri2_surf->current->bo == NULL) |
| 261 | return -1; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 266 | static void |
| 267 | back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer) |
| 268 | { |
| 269 | struct dri2_egl_display *dri2_dpy = |
| 270 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 271 | struct gbm_dri_bo *bo; |
| 272 | int name, pitch; |
| 273 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 274 | bo = (struct gbm_dri_bo *) dri2_surf->back->bo; |
| 275 | |
| 276 | dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 277 | dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch); |
| 278 | |
| 279 | buffer->attachment = __DRI_BUFFER_BACK_LEFT; |
| 280 | buffer->name = name; |
| 281 | buffer->pitch = pitch; |
| 282 | buffer->cpp = 4; |
| 283 | buffer->flags = 0; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static int |
| 287 | get_aux_bo(struct dri2_egl_surface *dri2_surf, |
| 288 | unsigned int attachment, unsigned int format, __DRIbuffer *buffer) |
| 289 | { |
| 290 | struct dri2_egl_display *dri2_dpy = |
| 291 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 292 | __DRIbuffer *b = dri2_surf->dri_buffers[attachment]; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 293 | |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 294 | if (b == NULL) { |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 295 | b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, |
| 296 | attachment, format, |
| 297 | dri2_surf->base.Width, |
| 298 | dri2_surf->base.Height); |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 299 | dri2_surf->dri_buffers[attachment] = b; |
| 300 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 301 | if (b == NULL) |
| 302 | return -1; |
| 303 | |
| 304 | memcpy(buffer, b, sizeof *buffer); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 310 | dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable, |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 311 | int *width, int *height, |
| 312 | unsigned int *attachments, int count, |
| 313 | int *out_count, void *loaderPrivate) |
| 314 | { |
| 315 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 316 | int i, j; |
| 317 | |
| 318 | dri2_surf->buffer_count = 0; |
| 319 | for (i = 0, j = 0; i < 2 * count; i += 2, j++) { |
| 320 | assert(attachments[i] < __DRI_BUFFER_COUNT); |
| 321 | assert(dri2_surf->buffer_count < 5); |
| 322 | |
| 323 | switch (attachments[i]) { |
| 324 | case __DRI_BUFFER_BACK_LEFT: |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 325 | if (get_back_bo(dri2_surf) < 0) { |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 326 | _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer"); |
| 327 | return NULL; |
| 328 | } |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 329 | back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1], |
| 333 | &dri2_surf->buffers[j]) < 0) { |
| 334 | _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer"); |
| 335 | return NULL; |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | *out_count = j; |
| 342 | if (j == 0) |
| 343 | return NULL; |
| 344 | |
| 345 | *width = dri2_surf->base.Width; |
| 346 | *height = dri2_surf->base.Height; |
| 347 | |
| 348 | return dri2_surf->buffers; |
| 349 | } |
| 350 | |
| 351 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 352 | dri2_drm_get_buffers(__DRIdrawable * driDrawable, |
| 353 | int *width, int *height, |
| 354 | unsigned int *attachments, int count, |
| 355 | int *out_count, void *loaderPrivate) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 356 | { |
| 357 | unsigned int *attachments_with_format; |
| 358 | __DRIbuffer *buffer; |
| 359 | const unsigned int format = 32; |
| 360 | int i; |
| 361 | |
Carl Worth | ecc89e4 | 2014-09-03 14:33:18 -0700 | [diff] [blame] | 362 | attachments_with_format = calloc(count, 2 * sizeof(unsigned int)); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 363 | if (!attachments_with_format) { |
| 364 | *out_count = 0; |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | for (i = 0; i < count; ++i) { |
| 369 | attachments_with_format[2*i] = attachments[i]; |
| 370 | attachments_with_format[2*i + 1] = format; |
| 371 | } |
| 372 | |
| 373 | buffer = |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 374 | dri2_drm_get_buffers_with_format(driDrawable, |
| 375 | width, height, |
| 376 | attachments_with_format, count, |
| 377 | out_count, loaderPrivate); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 378 | |
| 379 | free(attachments_with_format); |
| 380 | |
| 381 | return buffer; |
| 382 | } |
| 383 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 384 | static int |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 385 | dri2_drm_image_get_buffers(__DRIdrawable *driDrawable, |
| 386 | unsigned int format, |
| 387 | uint32_t *stamp, |
| 388 | void *loaderPrivate, |
| 389 | uint32_t buffer_mask, |
| 390 | struct __DRIimageList *buffers) |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 391 | { |
| 392 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 393 | struct gbm_dri_bo *bo; |
| 394 | |
| 395 | if (get_back_bo(dri2_surf) < 0) |
| 396 | return 0; |
| 397 | |
| 398 | bo = (struct gbm_dri_bo *) dri2_surf->back->bo; |
| 399 | buffers->image_mask = __DRI_IMAGE_BUFFER_BACK; |
| 400 | buffers->back = bo->image; |
| 401 | |
| 402 | return 1; |
| 403 | } |
| 404 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 405 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 406 | dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 407 | { |
| 408 | (void) driDrawable; |
| 409 | (void) loaderPrivate; |
| 410 | } |
| 411 | |
| 412 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 413 | dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 414 | { |
| 415 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 416 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 417 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 418 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 419 | if (dri2_dpy->swrast) { |
| 420 | (*dri2_dpy->core->swapBuffers)(dri2_surf->dri_drawable); |
| 421 | } else { |
| 422 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 423 | if (dri2_surf->current) |
| 424 | _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers"); |
| 425 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 426 | if (dri2_surf->color_buffers[i].age > 0) |
| 427 | dri2_surf->color_buffers[i].age++; |
Lionel Landwerlin | d175e7c | 2014-10-14 10:39:47 +0100 | [diff] [blame] | 428 | |
| 429 | /* Make sure we have a back buffer in case we're swapping without |
| 430 | * ever rendering. */ |
| 431 | if (get_back_bo(dri2_surf) < 0) { |
| 432 | _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers"); |
| 433 | return EGL_FALSE; |
| 434 | } |
| 435 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 436 | dri2_surf->current = dri2_surf->back; |
| 437 | dri2_surf->current->age = 1; |
| 438 | dri2_surf->back = NULL; |
| 439 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 440 | |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 441 | dri2_flush_drawable_for_swapbuffers(disp, draw); |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 442 | (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable); |
| 443 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 444 | |
| 445 | return EGL_TRUE; |
| 446 | } |
| 447 | |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 448 | static EGLint |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 449 | dri2_drm_query_buffer_age(_EGLDriver *drv, |
| 450 | _EGLDisplay *disp, _EGLSurface *surface) |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 451 | { |
| 452 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 453 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 454 | if (get_back_bo(dri2_surf) < 0) { |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 455 | _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age"); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | return dri2_surf->back->age; |
| 460 | } |
| 461 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 462 | static _EGLImage * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 463 | dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx, |
| 464 | EGLClientBuffer buffer, const EGLint *attr_list) |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 465 | { |
| 466 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 467 | struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer); |
| 468 | struct dri2_egl_image *dri2_img; |
| 469 | |
| 470 | dri2_img = malloc(sizeof *dri2_img); |
| 471 | if (!dri2_img) { |
| 472 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 477 | free(dri2_img); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img); |
| 482 | if (dri2_img->dri_image == NULL) { |
| 483 | free(dri2_img); |
| 484 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | return &dri2_img->base; |
| 489 | } |
| 490 | |
| 491 | static _EGLImage * |
| 492 | dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 493 | _EGLContext *ctx, EGLenum target, |
| 494 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 495 | { |
| 496 | (void) drv; |
| 497 | |
| 498 | switch (target) { |
| 499 | case EGL_NATIVE_PIXMAP_KHR: |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 500 | return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list); |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 501 | default: |
| 502 | return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); |
| 503 | } |
| 504 | } |
| 505 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 506 | static int |
| 507 | dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id) |
| 508 | { |
| 509 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 510 | |
| 511 | return drmAuthMagic(dri2_dpy->fd, id); |
| 512 | } |
| 513 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 514 | static void |
| 515 | swrast_put_image2(__DRIdrawable *driDrawable, |
| 516 | int op, |
| 517 | int x, |
| 518 | int y, |
| 519 | int width, |
| 520 | int height, |
| 521 | int stride, |
| 522 | char *data, |
| 523 | void *loaderPrivate) |
| 524 | { |
| 525 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 526 | int internal_stride, i; |
| 527 | struct gbm_dri_bo *bo; |
| 528 | |
| 529 | if (op != __DRI_SWRAST_IMAGE_OP_DRAW && |
| 530 | op != __DRI_SWRAST_IMAGE_OP_SWAP) |
| 531 | return; |
| 532 | |
| 533 | if (get_swrast_front_bo(dri2_surf) < 0) |
| 534 | return; |
| 535 | |
| 536 | bo = gbm_dri_bo(dri2_surf->current->bo); |
| 537 | if (gbm_dri_bo_map(bo) == NULL) |
| 538 | return; |
| 539 | |
| 540 | internal_stride = bo->base.base.stride; |
| 541 | |
| 542 | for (i = 0; i < height; i++) { |
| 543 | memcpy(bo->map + (x + i) * internal_stride + y, |
| 544 | data + i * stride, stride); |
| 545 | } |
| 546 | |
| 547 | gbm_dri_bo_unmap(bo); |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | swrast_get_image(__DRIdrawable *driDrawable, |
| 552 | int x, |
| 553 | int y, |
| 554 | int width, |
| 555 | int height, |
| 556 | char *data, |
| 557 | void *loaderPrivate) |
| 558 | { |
| 559 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 560 | int internal_stride, stride, i; |
| 561 | struct gbm_dri_bo *bo; |
| 562 | |
| 563 | if (get_swrast_front_bo(dri2_surf) < 0) |
| 564 | return; |
| 565 | |
| 566 | bo = gbm_dri_bo(dri2_surf->current->bo); |
| 567 | if (gbm_dri_bo_map(bo) == NULL) |
| 568 | return; |
| 569 | |
| 570 | internal_stride = bo->base.base.stride; |
| 571 | stride = width * 4; |
| 572 | |
| 573 | for (i = 0; i < height; i++) { |
| 574 | memcpy(data + i * stride, |
| 575 | bo->map + (x + i) * internal_stride + y, stride); |
| 576 | } |
| 577 | |
| 578 | gbm_dri_bo_unmap(bo); |
| 579 | } |
| 580 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 581 | static struct dri2_egl_display_vtbl dri2_drm_display_vtbl = { |
| 582 | .authenticate = dri2_drm_authenticate, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 583 | .create_window_surface = dri2_drm_create_window_surface, |
Chad Versace | 1787f56 | 2014-02-09 09:13:08 -0800 | [diff] [blame] | 584 | .create_pixmap_surface = dri2_drm_create_pixmap_surface, |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 585 | .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 586 | .destroy_surface = dri2_drm_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 587 | .create_image = dri2_drm_create_image_khr, |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 588 | .swap_interval = dri2_fallback_swap_interval, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 589 | .swap_buffers = dri2_drm_swap_buffers, |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 590 | .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 591 | .swap_buffers_region = dri2_fallback_swap_buffers_region, |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 592 | .post_sub_buffer = dri2_fallback_post_sub_buffer, |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 593 | .copy_buffers = dri2_fallback_copy_buffers, |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 594 | .query_buffer_age = dri2_drm_query_buffer_age, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 595 | .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image, |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 596 | .get_sync_values = dri2_fallback_get_sync_values, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 597 | }; |
| 598 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 599 | EGLBoolean |
| 600 | dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp) |
| 601 | { |
| 602 | struct dri2_egl_display *dri2_dpy; |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 603 | struct gbm_device *gbm; |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 604 | int fd = -1; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 605 | int i; |
| 606 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 607 | loader_set_logger(_eglLog); |
| 608 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 609 | dri2_dpy = calloc(1, sizeof *dri2_dpy); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 610 | if (!dri2_dpy) |
| 611 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 612 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 613 | disp->DriverData = (void *) dri2_dpy; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 614 | |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 615 | gbm = disp->PlatformDisplay; |
| 616 | if (gbm == NULL) { |
Jonathan Gray | c973e44 | 2014-04-03 16:22:26 +1100 | [diff] [blame] | 617 | char buf[64]; |
| 618 | int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0); |
| 619 | if (n != -1 && n < sizeof(buf)) |
Derek Foreman | 4f8f790 | 2015-06-17 11:28:51 -0500 | [diff] [blame] | 620 | fd = loader_open_device(buf); |
Jonathan Gray | c973e44 | 2014-04-03 16:22:26 +1100 | [diff] [blame] | 621 | if (fd < 0) |
Derek Foreman | 4f8f790 | 2015-06-17 11:28:51 -0500 | [diff] [blame] | 622 | fd = loader_open_device("/dev/dri/card0"); |
Benjamin Franzke | 2a58453 | 2011-12-13 14:43:48 +0100 | [diff] [blame] | 623 | dri2_dpy->own_device = 1; |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 624 | gbm = gbm_create_device(fd); |
| 625 | if (gbm == NULL) |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 626 | goto cleanup; |
Emil Velikov | bd5bcb5 | 2015-09-07 09:53:53 +0100 | [diff] [blame] | 627 | } else { |
| 628 | fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3); |
| 629 | if (fd < 0) |
| 630 | goto cleanup; |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 633 | if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) |
| 634 | goto cleanup; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 635 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 636 | dri2_dpy->gbm_dri = gbm_dri_device(gbm); |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 637 | if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) |
| 638 | goto cleanup; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 639 | |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 640 | dri2_dpy->fd = fd; |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 641 | dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd); |
Emil Velikov | 5cb1cad | 2014-06-02 12:26:17 +0100 | [diff] [blame] | 642 | dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 643 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 644 | dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen; |
| 645 | dri2_dpy->core = dri2_dpy->gbm_dri->core; |
| 646 | dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2; |
| 647 | dri2_dpy->image = dri2_dpy->gbm_dri->image; |
Ander Conselvan de Oliveira | 410aa3e | 2012-01-25 16:24:15 +0200 | [diff] [blame] | 648 | dri2_dpy->flush = dri2_dpy->gbm_dri->flush; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 649 | dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast; |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 650 | dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs; |
| 651 | |
| 652 | dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image; |
| 653 | dri2_dpy->gbm_dri->lookup_user_data = disp; |
| 654 | |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 655 | dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers; |
| 656 | dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer; |
| 657 | dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format; |
| 658 | dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 659 | dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2; |
| 660 | dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 661 | |
| 662 | dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer; |
| 663 | dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer; |
| 664 | dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers; |
| 665 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 666 | dri2_setup_screen(disp); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 667 | |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 668 | for (i = 0; dri2_dpy->driver_configs[i]; i++) { |
| 669 | EGLint format, attr_list[3]; |
Daniel Stone | 65c8965 | 2015-03-02 13:52:59 +0000 | [diff] [blame] | 670 | unsigned int red, alpha; |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 671 | |
| 672 | dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i], |
Daniel Stone | 65c8965 | 2015-03-02 13:52:59 +0000 | [diff] [blame] | 673 | __DRI_ATTRIB_RED_MASK, &red); |
| 674 | dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i], |
| 675 | __DRI_ATTRIB_ALPHA_MASK, &alpha); |
| 676 | if (red == 0x3ff00000 && alpha == 0x00000000) |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 677 | format = GBM_FORMAT_XRGB2101010; |
Daniel Stone | 65c8965 | 2015-03-02 13:52:59 +0000 | [diff] [blame] | 678 | else if (red == 0x3ff00000 && alpha == 0xc0000000) |
| 679 | format = GBM_FORMAT_ARGB2101010; |
| 680 | else if (red == 0x00ff0000 && alpha == 0x00000000) |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 681 | format = GBM_FORMAT_XRGB8888; |
Daniel Stone | 65c8965 | 2015-03-02 13:52:59 +0000 | [diff] [blame] | 682 | else if (red == 0x00ff0000 && alpha == 0xff000000) |
| 683 | format = GBM_FORMAT_ARGB8888; |
| 684 | else if (red == 0xf800) |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 685 | format = GBM_FORMAT_RGB565; |
| 686 | else |
| 687 | continue; |
| 688 | |
| 689 | attr_list[0] = EGL_NATIVE_VISUAL_ID; |
| 690 | attr_list[1] = format; |
| 691 | attr_list[2] = EGL_NONE; |
| 692 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 693 | dri2_add_config(disp, dri2_dpy->driver_configs[i], |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 694 | i + 1, EGL_WINDOW_BIT, attr_list, NULL); |
| 695 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 696 | |
Andreas Pokorny | 53b614b | 2014-09-03 22:43:41 +0200 | [diff] [blame] | 697 | disp->Extensions.KHR_image_pixmap = EGL_TRUE; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 698 | if (dri2_dpy->dri2) |
| 699 | disp->Extensions.EXT_buffer_age = EGL_TRUE; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 700 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 701 | #ifdef HAVE_WAYLAND_PLATFORM |
Giovanni Campagna | e57ad3d | 2014-07-23 19:28:52 +0100 | [diff] [blame] | 702 | if (dri2_dpy->image) { |
| 703 | if (dri2_dpy->image->base.version >= 10 && |
| 704 | dri2_dpy->image->getCapabilities != NULL) { |
| 705 | int capabilities; |
| 706 | |
| 707 | capabilities = |
| 708 | dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen); |
| 709 | disp->Extensions.WL_bind_wayland_display = |
| 710 | (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0; |
| 711 | } else |
| 712 | disp->Extensions.WL_bind_wayland_display = EGL_TRUE; |
| 713 | } |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 714 | #endif |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 715 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 716 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 717 | * initialization. |
| 718 | */ |
| 719 | dri2_dpy->vtbl = &dri2_drm_display_vtbl; |
| 720 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 721 | return EGL_TRUE; |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 722 | |
| 723 | cleanup: |
| 724 | if (fd >= 0) |
| 725 | close(fd); |
| 726 | |
| 727 | free(dri2_dpy); |
| 728 | return EGL_FALSE; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 729 | } |