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) { |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame^] | 59 | dri2_surf->current->locked = true; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 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) { |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame^] | 75 | dri2_surf->color_buffers[i].locked = false; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 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 = |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [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 = |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [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 | |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 194 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 197 | if (dri2_surf->color_buffers[i].bo) |
| 198 | gbm_bo_destroy(dri2_surf->color_buffers[i].bo); |
| 199 | } |
| 200 | |
| 201 | for (i = 0; i < __DRI_BUFFER_COUNT; i++) { |
| 202 | if (dri2_surf->dri_buffers[i]) |
| 203 | dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen, |
| 204 | dri2_surf->dri_buffers[i]); |
| 205 | } |
| 206 | |
| 207 | free(surf); |
| 208 | |
| 209 | return EGL_TRUE; |
| 210 | } |
| 211 | |
| 212 | static int |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 213 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 214 | { |
| 215 | struct dri2_egl_display *dri2_dpy = |
| 216 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 217 | struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
Derek Foreman | 4f1d27a | 2016-11-23 16:40:42 -0600 | [diff] [blame] | 218 | int age = 0; |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 219 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 220 | |
| 221 | if (dri2_surf->back == NULL) { |
| 222 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Derek Foreman | 4f1d27a | 2016-11-23 16:40:42 -0600 | [diff] [blame] | 223 | if (!dri2_surf->color_buffers[i].locked && |
| 224 | dri2_surf->color_buffers[i].age >= age) { |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 225 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Derek Foreman | 4f1d27a | 2016-11-23 16:40:42 -0600 | [diff] [blame] | 226 | age = dri2_surf->color_buffers[i].age; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (dri2_surf->back == NULL) |
| 232 | return -1; |
Ben Widawsky | 191ff91 | 2017-03-13 18:19:00 -0700 | [diff] [blame] | 233 | if (dri2_surf->back->bo == NULL) { |
| 234 | if (surf->base.modifiers) |
Emil Velikov | 628af2b | 2017-05-09 18:47:20 +0100 | [diff] [blame] | 235 | dri2_surf->back->bo = gbm_bo_create_with_modifiers(&dri2_dpy->gbm_dri->base, |
Ben Widawsky | 191ff91 | 2017-03-13 18:19:00 -0700 | [diff] [blame] | 236 | surf->base.width, surf->base.height, |
| 237 | surf->base.format, |
| 238 | surf->base.modifiers, |
| 239 | surf->base.count); |
| 240 | else |
Emil Velikov | 628af2b | 2017-05-09 18:47:20 +0100 | [diff] [blame] | 241 | dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base, |
Ben Widawsky | 191ff91 | 2017-03-13 18:19:00 -0700 | [diff] [blame] | 242 | surf->base.width, |
| 243 | surf->base.height, |
| 244 | surf->base.format, |
| 245 | surf->base.flags); |
| 246 | |
| 247 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 248 | if (dri2_surf->back->bo == NULL) |
| 249 | return -1; |
| 250 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 254 | static int |
| 255 | get_swrast_front_bo(struct dri2_egl_surface *dri2_surf) |
| 256 | { |
| 257 | struct dri2_egl_display *dri2_dpy = |
| 258 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 259 | struct gbm_dri_surface *surf = dri2_surf->gbm_surf; |
| 260 | |
| 261 | if (dri2_surf->current == NULL) { |
| 262 | assert(!dri2_surf->color_buffers[0].locked); |
| 263 | dri2_surf->current = &dri2_surf->color_buffers[0]; |
| 264 | } |
| 265 | |
| 266 | if (dri2_surf->current->bo == NULL) |
Emil Velikov | 628af2b | 2017-05-09 18:47:20 +0100 | [diff] [blame] | 267 | dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base, |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 268 | surf->base.width, surf->base.height, |
| 269 | surf->base.format, surf->base.flags); |
| 270 | if (dri2_surf->current->bo == NULL) |
| 271 | return -1; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 276 | static void |
| 277 | back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer) |
| 278 | { |
| 279 | struct dri2_egl_display *dri2_dpy = |
| 280 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 281 | struct gbm_dri_bo *bo; |
| 282 | int name, pitch; |
| 283 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 284 | bo = (struct gbm_dri_bo *) dri2_surf->back->bo; |
| 285 | |
| 286 | dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 287 | dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch); |
| 288 | |
| 289 | buffer->attachment = __DRI_BUFFER_BACK_LEFT; |
| 290 | buffer->name = name; |
| 291 | buffer->pitch = pitch; |
| 292 | buffer->cpp = 4; |
| 293 | buffer->flags = 0; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int |
| 297 | get_aux_bo(struct dri2_egl_surface *dri2_surf, |
| 298 | unsigned int attachment, unsigned int format, __DRIbuffer *buffer) |
| 299 | { |
| 300 | struct dri2_egl_display *dri2_dpy = |
| 301 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 302 | __DRIbuffer *b = dri2_surf->dri_buffers[attachment]; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 303 | |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 304 | if (b == NULL) { |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 305 | b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, |
| 306 | attachment, format, |
| 307 | dri2_surf->base.Width, |
| 308 | dri2_surf->base.Height); |
Mandeep Singh Baines | 0695cf6 | 2012-04-10 14:48:14 -0700 | [diff] [blame] | 309 | dri2_surf->dri_buffers[attachment] = b; |
| 310 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 311 | if (b == NULL) |
| 312 | return -1; |
| 313 | |
| 314 | memcpy(buffer, b, sizeof *buffer); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 320 | dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable, |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 321 | int *width, int *height, |
| 322 | unsigned int *attachments, int count, |
| 323 | int *out_count, void *loaderPrivate) |
| 324 | { |
| 325 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 326 | int i, j; |
| 327 | |
| 328 | dri2_surf->buffer_count = 0; |
| 329 | for (i = 0, j = 0; i < 2 * count; i += 2, j++) { |
| 330 | assert(attachments[i] < __DRI_BUFFER_COUNT); |
| 331 | assert(dri2_surf->buffer_count < 5); |
| 332 | |
| 333 | switch (attachments[i]) { |
| 334 | case __DRI_BUFFER_BACK_LEFT: |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 335 | if (get_back_bo(dri2_surf) < 0) { |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 336 | _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer"); |
| 337 | return NULL; |
| 338 | } |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 339 | 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] | 340 | break; |
| 341 | default: |
| 342 | if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1], |
| 343 | &dri2_surf->buffers[j]) < 0) { |
| 344 | _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer"); |
| 345 | return NULL; |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | *out_count = j; |
| 352 | if (j == 0) |
| 353 | return NULL; |
| 354 | |
| 355 | *width = dri2_surf->base.Width; |
| 356 | *height = dri2_surf->base.Height; |
| 357 | |
| 358 | return dri2_surf->buffers; |
| 359 | } |
| 360 | |
| 361 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 362 | dri2_drm_get_buffers(__DRIdrawable * driDrawable, |
| 363 | int *width, int *height, |
| 364 | unsigned int *attachments, int count, |
| 365 | int *out_count, void *loaderPrivate) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 366 | { |
| 367 | unsigned int *attachments_with_format; |
| 368 | __DRIbuffer *buffer; |
| 369 | const unsigned int format = 32; |
| 370 | int i; |
| 371 | |
Carl Worth | ecc89e4 | 2014-09-03 14:33:18 -0700 | [diff] [blame] | 372 | attachments_with_format = calloc(count, 2 * sizeof(unsigned int)); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 373 | if (!attachments_with_format) { |
| 374 | *out_count = 0; |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | for (i = 0; i < count; ++i) { |
| 379 | attachments_with_format[2*i] = attachments[i]; |
| 380 | attachments_with_format[2*i + 1] = format; |
| 381 | } |
| 382 | |
| 383 | buffer = |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 384 | dri2_drm_get_buffers_with_format(driDrawable, |
| 385 | width, height, |
| 386 | attachments_with_format, count, |
| 387 | out_count, loaderPrivate); |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 388 | |
| 389 | free(attachments_with_format); |
| 390 | |
| 391 | return buffer; |
| 392 | } |
| 393 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 394 | static int |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 395 | dri2_drm_image_get_buffers(__DRIdrawable *driDrawable, |
| 396 | unsigned int format, |
| 397 | uint32_t *stamp, |
| 398 | void *loaderPrivate, |
| 399 | uint32_t buffer_mask, |
| 400 | struct __DRIimageList *buffers) |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 401 | { |
| 402 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 403 | struct gbm_dri_bo *bo; |
| 404 | |
| 405 | if (get_back_bo(dri2_surf) < 0) |
| 406 | return 0; |
| 407 | |
| 408 | bo = (struct gbm_dri_bo *) dri2_surf->back->bo; |
| 409 | buffers->image_mask = __DRI_IMAGE_BUFFER_BACK; |
| 410 | buffers->back = bo->image; |
| 411 | |
| 412 | return 1; |
| 413 | } |
| 414 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 415 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 416 | dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 417 | { |
| 418 | (void) driDrawable; |
| 419 | (void) loaderPrivate; |
| 420 | } |
| 421 | |
| 422 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 423 | dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 424 | { |
| 425 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 426 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Rhys Kidd | 4864977 | 2015-08-06 16:34:07 +1000 | [diff] [blame] | 427 | unsigned i; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 428 | |
Emil Velikov | 4db5e83 | 2017-05-12 15:11:27 +0100 | [diff] [blame] | 429 | if (!dri2_dpy->flush) { |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 430 | dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable); |
Emil Velikov | 1398ece | 2017-05-22 17:28:12 +0100 | [diff] [blame] | 431 | return EGL_TRUE; |
| 432 | } |
Lionel Landwerlin | d175e7c | 2014-10-14 10:39:47 +0100 | [diff] [blame] | 433 | |
Emil Velikov | 1398ece | 2017-05-22 17:28:12 +0100 | [diff] [blame] | 434 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 435 | if (dri2_surf->current) |
| 436 | _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers"); |
| 437 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 438 | if (dri2_surf->color_buffers[i].age > 0) |
| 439 | dri2_surf->color_buffers[i].age++; |
Lionel Landwerlin | d175e7c | 2014-10-14 10:39:47 +0100 | [diff] [blame] | 440 | |
Emil Velikov | 1398ece | 2017-05-22 17:28:12 +0100 | [diff] [blame] | 441 | /* Make sure we have a back buffer in case we're swapping without |
| 442 | * ever rendering. */ |
| 443 | if (get_back_bo(dri2_surf) < 0) { |
| 444 | _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers"); |
| 445 | return EGL_FALSE; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 446 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 447 | |
Emil Velikov | 1398ece | 2017-05-22 17:28:12 +0100 | [diff] [blame] | 448 | dri2_surf->current = dri2_surf->back; |
| 449 | dri2_surf->current->age = 1; |
| 450 | dri2_surf->back = NULL; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 451 | } |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 452 | |
Emil Velikov | 1398ece | 2017-05-22 17:28:12 +0100 | [diff] [blame] | 453 | dri2_flush_drawable_for_swapbuffers(disp, draw); |
| 454 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
| 455 | |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 456 | return EGL_TRUE; |
| 457 | } |
| 458 | |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 459 | static EGLint |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 460 | dri2_drm_query_buffer_age(_EGLDriver *drv, |
| 461 | _EGLDisplay *disp, _EGLSurface *surface) |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 462 | { |
| 463 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 464 | |
Kristian Høgsberg | 04e3ef0 | 2013-11-08 22:06:51 -0800 | [diff] [blame] | 465 | if (get_back_bo(dri2_surf) < 0) { |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 466 | _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age"); |
Tapani Pälli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 467 | return -1; |
Kristian Høgsberg | 4e42e56 | 2012-12-13 23:39:45 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return dri2_surf->back->age; |
| 471 | } |
| 472 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 473 | static _EGLImage * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 474 | dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx, |
| 475 | EGLClientBuffer buffer, const EGLint *attr_list) |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 476 | { |
| 477 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 478 | struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer); |
| 479 | struct dri2_egl_image *dri2_img; |
| 480 | |
| 481 | dri2_img = malloc(sizeof *dri2_img); |
| 482 | if (!dri2_img) { |
| 483 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 484 | return NULL; |
| 485 | } |
| 486 | |
| 487 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 488 | free(dri2_img); |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img); |
| 493 | if (dri2_img->dri_image == NULL) { |
| 494 | free(dri2_img); |
| 495 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap"); |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | return &dri2_img->base; |
| 500 | } |
| 501 | |
| 502 | static _EGLImage * |
| 503 | dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 504 | _EGLContext *ctx, EGLenum target, |
| 505 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 506 | { |
| 507 | (void) drv; |
| 508 | |
| 509 | switch (target) { |
| 510 | case EGL_NATIVE_PIXMAP_KHR: |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 511 | return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list); |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 512 | default: |
| 513 | return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list); |
| 514 | } |
| 515 | } |
| 516 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 517 | static int |
| 518 | dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id) |
| 519 | { |
| 520 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 521 | |
| 522 | return drmAuthMagic(dri2_dpy->fd, id); |
| 523 | } |
| 524 | |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 525 | static void |
| 526 | swrast_put_image2(__DRIdrawable *driDrawable, |
| 527 | int op, |
| 528 | int x, |
| 529 | int y, |
| 530 | int width, |
| 531 | int height, |
| 532 | int stride, |
| 533 | char *data, |
| 534 | void *loaderPrivate) |
| 535 | { |
| 536 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 537 | int internal_stride, i; |
| 538 | struct gbm_dri_bo *bo; |
| 539 | |
| 540 | if (op != __DRI_SWRAST_IMAGE_OP_DRAW && |
| 541 | op != __DRI_SWRAST_IMAGE_OP_SWAP) |
| 542 | return; |
| 543 | |
| 544 | if (get_swrast_front_bo(dri2_surf) < 0) |
| 545 | return; |
| 546 | |
| 547 | bo = gbm_dri_bo(dri2_surf->current->bo); |
Rob Herring | 0a4275b | 2016-05-16 21:14:21 -0500 | [diff] [blame] | 548 | if (gbm_dri_bo_map_dumb(bo) == NULL) |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 549 | return; |
| 550 | |
Emil Velikov | 2204ea6 | 2017-05-09 18:36:47 +0100 | [diff] [blame] | 551 | internal_stride = bo->base.stride; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 552 | |
| 553 | for (i = 0; i < height; i++) { |
| 554 | memcpy(bo->map + (x + i) * internal_stride + y, |
| 555 | data + i * stride, stride); |
| 556 | } |
| 557 | |
Rob Herring | 0a4275b | 2016-05-16 21:14:21 -0500 | [diff] [blame] | 558 | gbm_dri_bo_unmap_dumb(bo); |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void |
| 562 | swrast_get_image(__DRIdrawable *driDrawable, |
| 563 | int x, |
| 564 | int y, |
| 565 | int width, |
| 566 | int height, |
| 567 | char *data, |
| 568 | void *loaderPrivate) |
| 569 | { |
| 570 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 571 | int internal_stride, stride, i; |
| 572 | struct gbm_dri_bo *bo; |
| 573 | |
| 574 | if (get_swrast_front_bo(dri2_surf) < 0) |
| 575 | return; |
| 576 | |
| 577 | bo = gbm_dri_bo(dri2_surf->current->bo); |
Rob Herring | 0a4275b | 2016-05-16 21:14:21 -0500 | [diff] [blame] | 578 | if (gbm_dri_bo_map_dumb(bo) == NULL) |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 579 | return; |
| 580 | |
Emil Velikov | 2204ea6 | 2017-05-09 18:36:47 +0100 | [diff] [blame] | 581 | internal_stride = bo->base.stride; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 582 | stride = width * 4; |
| 583 | |
| 584 | for (i = 0; i < height; i++) { |
| 585 | memcpy(data + i * stride, |
| 586 | bo->map + (x + i) * internal_stride + y, stride); |
| 587 | } |
| 588 | |
Rob Herring | 0a4275b | 2016-05-16 21:14:21 -0500 | [diff] [blame] | 589 | gbm_dri_bo_unmap_dumb(bo); |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Emil Velikov | 36fe590 | 2016-08-25 14:00:50 +0100 | [diff] [blame] | 592 | static EGLBoolean |
| 593 | drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp) |
| 594 | { |
| 595 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 596 | static const struct { |
| 597 | int format; |
| 598 | unsigned int red_mask; |
| 599 | unsigned int alpha_mask; |
| 600 | } visuals[] = { |
| 601 | { GBM_FORMAT_XRGB2101010, 0x3ff00000, 0x00000000 }, |
| 602 | { GBM_FORMAT_ARGB2101010, 0x3ff00000, 0xc0000000 }, |
| 603 | { GBM_FORMAT_XRGB8888, 0x00ff0000, 0x00000000 }, |
| 604 | { GBM_FORMAT_ARGB8888, 0x00ff0000, 0xff000000 }, |
| 605 | { GBM_FORMAT_RGB565, 0x0000f800, 0x00000000 }, |
| 606 | }; |
| 607 | EGLint attr_list[] = { |
| 608 | EGL_NATIVE_VISUAL_ID, 0, |
| 609 | EGL_NONE, |
| 610 | }; |
| 611 | unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 }; |
| 612 | unsigned int count, i, j; |
| 613 | |
| 614 | count = 0; |
| 615 | for (i = 0; dri2_dpy->driver_configs[i]; i++) { |
| 616 | unsigned int red, alpha; |
| 617 | |
| 618 | dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i], |
| 619 | __DRI_ATTRIB_RED_MASK, &red); |
| 620 | dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i], |
| 621 | __DRI_ATTRIB_ALPHA_MASK, &alpha); |
| 622 | |
| 623 | for (j = 0; j < ARRAY_SIZE(visuals); j++) { |
| 624 | struct dri2_egl_config *dri2_conf; |
| 625 | |
| 626 | if (visuals[j].red_mask != red || visuals[j].alpha_mask != alpha) |
| 627 | continue; |
| 628 | |
| 629 | attr_list[1] = visuals[j].format; |
| 630 | |
| 631 | dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i], |
| 632 | count + 1, EGL_WINDOW_BIT, attr_list, NULL); |
| 633 | if (dri2_conf) { |
| 634 | count++; |
| 635 | format_count[j]++; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | for (i = 0; i < ARRAY_SIZE(format_count); i++) { |
| 641 | if (!format_count[i]) { |
| 642 | _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x", |
| 643 | visuals[i].format); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | return (count != 0); |
| 648 | } |
| 649 | |
Emil Velikov | 79d1fb9 | 2017-05-15 16:14:17 +0100 | [diff] [blame] | 650 | static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = { |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 651 | .authenticate = dri2_drm_authenticate, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 652 | .create_window_surface = dri2_drm_create_window_surface, |
Chad Versace | 1787f56 | 2014-02-09 09:13:08 -0800 | [diff] [blame] | 653 | .create_pixmap_surface = dri2_drm_create_pixmap_surface, |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 654 | .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 655 | .destroy_surface = dri2_drm_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 656 | .create_image = dri2_drm_create_image_khr, |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 657 | .swap_interval = dri2_fallback_swap_interval, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 658 | .swap_buffers = dri2_drm_swap_buffers, |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 659 | .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 660 | .swap_buffers_region = dri2_fallback_swap_buffers_region, |
Harish Krupo | 9827547 | 2017-06-09 20:13:34 +0530 | [diff] [blame] | 661 | .set_damage_region = dri2_fallback_set_damage_region, |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 662 | .post_sub_buffer = dri2_fallback_post_sub_buffer, |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 663 | .copy_buffers = dri2_fallback_copy_buffers, |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 664 | .query_buffer_age = dri2_drm_query_buffer_age, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 665 | .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image, |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 666 | .get_sync_values = dri2_fallback_get_sync_values, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 667 | .get_dri_drawable = dri2_surface_get_dri_drawable, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 668 | }; |
| 669 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 670 | EGLBoolean |
| 671 | dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp) |
| 672 | { |
| 673 | struct dri2_egl_display *dri2_dpy; |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 674 | struct gbm_device *gbm; |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 675 | const char *err; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 676 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 677 | loader_set_logger(_eglLog); |
| 678 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 679 | dri2_dpy = calloc(1, sizeof *dri2_dpy); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 680 | if (!dri2_dpy) |
| 681 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 682 | |
Emil Velikov | 7ec07be | 2017-05-11 17:21:13 +0100 | [diff] [blame] | 683 | dri2_dpy->fd = -1; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 684 | disp->DriverData = (void *) dri2_dpy; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 685 | |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 686 | gbm = disp->PlatformDisplay; |
| 687 | if (gbm == NULL) { |
Jonathan Gray | c973e44 | 2014-04-03 16:22:26 +1100 | [diff] [blame] | 688 | char buf[64]; |
| 689 | int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0); |
| 690 | if (n != -1 && n < sizeof(buf)) |
Eric Engestrom | 11da77e | 2017-05-31 16:57:42 +0100 | [diff] [blame] | 691 | dri2_dpy->fd = loader_open_device(buf); |
| 692 | if (dri2_dpy->fd < 0) |
| 693 | dri2_dpy->fd = loader_open_device("/dev/dri/card0"); |
| 694 | gbm = gbm_create_device(dri2_dpy->fd); |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 695 | if (gbm == NULL) { |
| 696 | err = "DRI2: failed to create gbm device"; |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 697 | goto cleanup; |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 698 | } |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame^] | 699 | dri2_dpy->own_device = true; |
Emil Velikov | bd5bcb5 | 2015-09-07 09:53:53 +0100 | [diff] [blame] | 700 | } else { |
Eric Engestrom | 11da77e | 2017-05-31 16:57:42 +0100 | [diff] [blame] | 701 | dri2_dpy->fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3); |
| 702 | if (dri2_dpy->fd < 0) { |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 703 | err = "DRI2: failed to fcntl() existing gbm device"; |
Emil Velikov | bd5bcb5 | 2015-09-07 09:53:53 +0100 | [diff] [blame] | 704 | goto cleanup; |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 705 | } |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 708 | if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) { |
| 709 | err = "DRI2: gbm device using incorrect/incompatible backend"; |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 710 | goto cleanup; |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 711 | } |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 712 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 713 | dri2_dpy->gbm_dri = gbm_dri_device(gbm); |
Emil Velikov | e183c55 | 2017-05-09 18:41:50 +0100 | [diff] [blame] | 714 | dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 715 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 716 | dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen; |
| 717 | dri2_dpy->core = dri2_dpy->gbm_dri->core; |
| 718 | dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 719 | dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast; |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 720 | dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs; |
| 721 | |
| 722 | dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image; |
| 723 | dri2_dpy->gbm_dri->lookup_user_data = disp; |
| 724 | |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 725 | dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers; |
| 726 | dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer; |
| 727 | dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format; |
| 728 | dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 729 | dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2; |
| 730 | dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 731 | |
Emil Velikov | 628af2b | 2017-05-09 18:47:20 +0100 | [diff] [blame] | 732 | dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer; |
| 733 | dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer; |
| 734 | dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers; |
Ander Conselvan de Oliveira | 4a976b6 | 2012-01-25 16:24:17 +0200 | [diff] [blame] | 735 | |
Emil Velikov | 90d0ad1 | 2017-05-11 16:34:47 +0100 | [diff] [blame] | 736 | if (!dri2_setup_extensions(disp)) { |
| 737 | err = "DRI2: failed to find required DRI extensions"; |
| 738 | goto cleanup; |
| 739 | } |
| 740 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 741 | dri2_setup_screen(disp); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 742 | |
Emil Velikov | 36fe590 | 2016-08-25 14:00:50 +0100 | [diff] [blame] | 743 | if (!drm_add_configs_for_visuals(drv, disp)) { |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 744 | err = "DRI2: failed to add configs"; |
Emil Velikov | 36fe590 | 2016-08-25 14:00:50 +0100 | [diff] [blame] | 745 | goto cleanup; |
Kristian Høgsberg | df479cf | 2013-09-25 14:57:22 -0700 | [diff] [blame] | 746 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 747 | |
Andreas Pokorny | 53b614b | 2014-09-03 22:43:41 +0200 | [diff] [blame] | 748 | disp->Extensions.KHR_image_pixmap = EGL_TRUE; |
Giovanni Campagna | 8430af5 | 2014-06-15 13:49:49 +0200 | [diff] [blame] | 749 | if (dri2_dpy->dri2) |
| 750 | disp->Extensions.EXT_buffer_age = EGL_TRUE; |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 751 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 752 | #ifdef HAVE_WAYLAND_PLATFORM |
Emil Velikov | 98f5d01 | 2016-08-25 13:15:43 +0100 | [diff] [blame] | 753 | dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 754 | #endif |
Emil Velikov | 98f5d01 | 2016-08-25 13:15:43 +0100 | [diff] [blame] | 755 | dri2_set_WL_bind_wayland_display(drv, disp); |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 756 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 757 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 758 | * initialization. |
| 759 | */ |
| 760 | dri2_dpy->vtbl = &dri2_drm_display_vtbl; |
| 761 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 762 | return EGL_TRUE; |
Matt Turner | e4f0d26 | 2015-07-15 09:00:41 -0700 | [diff] [blame] | 763 | |
| 764 | cleanup: |
Emil Velikov | 7ec07be | 2017-05-11 17:21:13 +0100 | [diff] [blame] | 765 | dri2_display_destroy(disp); |
Emil Velikov | 2847956 | 2016-08-25 15:52:58 +0100 | [diff] [blame] | 766 | return _eglError(EGL_NOT_INITIALIZED, err); |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 767 | } |