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 <[email protected]>
26 */
27
28 #ifndef EGL_DRI2_INCLUDED
29 #define EGL_DRI2_INCLUDED
30
31 #include <stdbool.h>
32 #include <stdint.h>
33
34 #ifdef HAVE_X11_PLATFORM
35 #include <X11/Xlib-xcb.h>
36 #ifdef HAVE_X11_DRI2
37 #include <xcb/dri2.h>
38 #endif
39 #include <xcb/randr.h>
40 #include <xcb/xcb.h>
41 #include <xcb/xfixes.h>
42
43 #include "loader_dri_helper.h"
44 #ifdef HAVE_LIBDRM
45 #include "loader_dri3_helper.h"
46 #endif
47 #endif
48
49 #ifdef HAVE_WAYLAND_PLATFORM
50 /* forward declarations to avoid pulling wayland headers everywhere */
51 struct wl_egl_window;
52 struct wl_event_queue;
53 struct wl_callback;
54 struct wl_display;
55 struct wl_drm;
56 struct wl_registry;
57 struct wl_shm;
58 struct wl_surface;
59 struct zwp_linux_dmabuf_v1;
60 struct zwp_linux_dmabuf_feedback_v1;
61 #endif
62
63 #include <GL/gl.h>
64 #include "mesa_interface.h"
65 #include "kopper_interface.h"
66
67 #ifdef HAVE_DRM_PLATFORM
68 #include <gbm_driint.h>
69 #endif
70
71 #ifdef HAVE_ANDROID_PLATFORM
72 #define LOG_TAG "EGL-DRI2"
73
74 #include <hardware/gralloc.h>
75
76 #include "util/u_gralloc/u_gralloc.h"
77
78 #if ANDROID_API_LEVEL >= 26
79 #include <vndk/window.h>
80 #else
81 #include <system/window.h>
82 #endif
83
84 #endif /* HAVE_ANDROID_PLATFORM */
85
86 #include "eglconfig.h"
87 #include "eglcontext.h"
88 #include "eglcurrent.h"
89 #include "egldevice.h"
90 #include "egldisplay.h"
91 #include "egldriver.h"
92 #include "eglimage.h"
93 #include "egllog.h"
94 #include "eglsurface.h"
95 #include "eglsync.h"
96
97 #include "util/bitset.h"
98 #include "util/u_dynarray.h"
99 #include "util/u_vector.h"
100 #include "util/format/u_format.h"
101
102 struct wl_buffer;
103
104 struct dri2_egl_display_vtbl {
105 /* mandatory on Wayland, unused otherwise */
106 int (*authenticate)(_EGLDisplay *disp, uint32_t id);
107
108 /* mandatory */
109 _EGLSurface *(*create_window_surface)(_EGLDisplay *disp, _EGLConfig *config,
110 void *native_window,
111 const EGLint *attrib_list);
112
113 /* optional */
114 _EGLSurface *(*create_pixmap_surface)(_EGLDisplay *disp, _EGLConfig *config,
115 void *native_pixmap,
116 const EGLint *attrib_list);
117
118 /* optional */
119 _EGLSurface *(*create_pbuffer_surface)(_EGLDisplay *disp, _EGLConfig *config,
120 const EGLint *attrib_list);
121
122 /* mandatory */
123 EGLBoolean (*destroy_surface)(_EGLDisplay *disp, _EGLSurface *surface);
124
125 /* optional */
126 EGLBoolean (*swap_interval)(_EGLDisplay *disp, _EGLSurface *surf,
127 EGLint interval);
128
129 /* mandatory */
130 _EGLImage *(*create_image)(_EGLDisplay *disp, _EGLContext *ctx,
131 EGLenum target, EGLClientBuffer buffer,
132 const EGLint *attr_list);
133
134 /* mandatory */
135 EGLBoolean (*swap_buffers)(_EGLDisplay *disp, _EGLSurface *surf);
136
137 /* optional - falls back to .swap_buffers */
138 EGLBoolean (*swap_buffers_with_damage)(_EGLDisplay *disp,
139 _EGLSurface *surface,
140 const EGLint *rects, EGLint n_rects);
141
142 /* optional */
143 EGLBoolean (*swap_buffers_region)(_EGLDisplay *disp, _EGLSurface *surf,
144 EGLint numRects, const EGLint *rects);
145
146 /* optional */
147 EGLBoolean (*post_sub_buffer)(_EGLDisplay *disp, _EGLSurface *surf, EGLint x,
148 EGLint y, EGLint width, EGLint height);
149
150 /* optional */
151 EGLBoolean (*copy_buffers)(_EGLDisplay *disp, _EGLSurface *surf,
152 void *native_pixmap_target);
153
154 /* optional */
155 EGLint (*query_buffer_age)(_EGLDisplay *disp, _EGLSurface *surf);
156
157 /* optional */
158 EGLBoolean (*query_surface)(_EGLDisplay *disp, _EGLSurface *surf,
159 EGLint attribute, EGLint *value);
160
161 /* optional */
162 struct wl_buffer *(*create_wayland_buffer_from_image)(_EGLDisplay *disp,
163 _EGLImage *img);
164
165 /* optional */
166 EGLBoolean (*get_sync_values)(_EGLDisplay *display, _EGLSurface *surface,
167 EGLuint64KHR *ust, EGLuint64KHR *msc,
168 EGLuint64KHR *sbc);
169
170 /* optional */
171 EGLBoolean (*get_msc_rate)(_EGLDisplay *display, _EGLSurface *surface,
172 EGLint *numerator, EGLint *denominator);
173
174 /* mandatory */
175 __DRIdrawable *(*get_dri_drawable)(_EGLSurface *surf);
176
177 /* optional */
178 void (*close_screen_notify)(_EGLDisplay *disp);
179
180 /* Used in EGL_KHR_mutable_render_buffer to update the native window's
181 * shared buffer mode.
182 * optional
183 */
184 bool (*set_shared_buffer_mode)(_EGLDisplay *disp, _EGLSurface *surf,
185 bool mode);
186 };
187
188 #ifdef HAVE_WAYLAND_PLATFORM
189 struct dri2_wl_formats {
190 unsigned int num_formats;
191
192 /* Bitmap referencing dri2_wl_visuals */
193 unsigned int *formats_bitmap;
194
195 /* Array of vectors. Contains one modifier vector per format */
196 struct u_vector *modifiers;
197 };
198
199 struct dmabuf_feedback_format_table {
200 unsigned int size;
201 struct {
202 uint32_t format;
203 uint32_t padding; /* unused */
204 uint64_t modifier;
205 } *data;
206 };
207
208 struct dmabuf_feedback_tranche {
209 dev_t target_device;
210 uint32_t flags;
211 struct dri2_wl_formats formats;
212 };
213
214 struct dmabuf_feedback {
215 dev_t main_device;
216 struct dmabuf_feedback_format_table format_table;
217 struct util_dynarray tranches;
218 struct dmabuf_feedback_tranche pending_tranche;
219 };
220 #endif
221
222 enum dri2_egl_driver_fail {
223 DRI2_EGL_DRIVER_LOADED = 0,
224 DRI2_EGL_DRIVER_FAILED = 1,
225 DRI2_EGL_DRIVER_PREFER_ZINK = 2,
226 };
227
228 struct dri2_egl_display {
229 const struct dri2_egl_display_vtbl *vtbl;
230
231 mtx_t lock;
232
233 int dri2_major;
234 int dri2_minor;
235 __DRIscreen *dri_screen_render_gpu;
236 /* dri_screen_display_gpu holds display GPU in case of prime gpu offloading
237 * else dri_screen_render_gpu and dri_screen_display_gpu is same. In case of
238 * prime gpu offloading, if display and render driver names are different
239 * (potentially not compatible), dri_screen_display_gpu will be NULL but
240 * fd_display_gpu will still hold fd for display driver.
241 */
242 __DRIscreen *dri_screen_display_gpu;
243 bool own_dri_screen;
244 const __DRIconfig **driver_configs;
245 /* fd of the GPU used for rendering. */
246 int fd_render_gpu;
247 /* fd of the GPU used for display. If the same GPU is used for display
248 * and rendering, then fd_render_gpu == fd_display_gpu (no need to use
249 * os_same_file_description).
250 */
251 int fd_display_gpu;
252
253 /* dri2_initialize/dri2_terminate increment/decrement this count, so does
254 * dri2_make_current (tracks if there are active contexts/surfaces). */
255 int ref_count;
256
257 bool has_compression_modifiers;
258 bool own_device;
259 bool invalidate_available;
260 bool kopper;
261 bool kopper_without_modifiers;
262 bool swrast;
263 bool swrast_not_kms;
264 int min_swap_interval;
265 int max_swap_interval;
266 int default_swap_interval;
267 #ifdef HAVE_DRM_PLATFORM
268 struct gbm_dri_device *gbm_dri;
269 #endif
270
271 char *driver_name;
272
273 const __DRIextension **loader_extensions;
274
275 bool has_dmabuf_import;
276 bool has_dmabuf_export;
277 bool explicit_modifiers;
278 bool multibuffers_available;
279 #ifdef HAVE_X11_PLATFORM
280 xcb_connection_t *conn;
281 xcb_screen_t *screen;
282 bool swap_available;
283 #ifdef HAVE_LIBDRM
284 struct loader_screen_resources screen_resources;
285 #endif
286 #endif
287
288 #ifdef HAVE_WAYLAND_PLATFORM
289 struct wl_display *wl_dpy;
290 struct wl_display *wl_dpy_wrapper;
291 struct wl_registry *wl_registry;
292 struct wl_drm *wl_server_drm;
293 struct wl_drm *wl_drm;
294 uint32_t wl_drm_version, wl_drm_name;
295 struct wl_shm *wl_shm;
296 struct wl_event_queue *wl_queue;
297 struct zwp_linux_dmabuf_v1 *wl_dmabuf;
298 struct dri2_wl_formats formats;
299 struct zwp_linux_dmabuf_feedback_v1 *wl_dmabuf_feedback;
300 struct dmabuf_feedback_format_table format_table;
301 bool authenticated;
302 uint32_t capabilities;
303 char *device_name;
304 bool is_render_node;
305 #endif
306
307 #ifdef HAVE_ANDROID_PLATFORM
308 struct u_gralloc *gralloc;
309 /* gralloc vendor usage bit for front rendering */
310 uint32_t front_rendering_usage;
311 bool has_native_fence_fd;
312 bool pure_swrast;
313 #endif
314 };
315
316 struct dri2_egl_context {
317 _EGLContext base;
318 __DRIcontext *dri_context;
319 };
320
321 struct dri2_egl_surface {
322 _EGLSurface base;
323 __DRIdrawable *dri_drawable;
324 __DRIbuffer buffers[5];
325 bool have_fake_front;
326
327 #ifdef HAVE_X11_PLATFORM
328 xcb_drawable_t drawable;
329 xcb_xfixes_region_t region;
330 int depth;
331 int bytes_per_pixel;
332 xcb_gcontext_t gc;
333 xcb_gcontext_t swapgc;
334 #endif
335
336 #ifdef HAVE_WAYLAND_PLATFORM
337 struct wl_egl_window *wl_win;
338 int dx;
339 int dy;
340 struct wl_event_queue *wl_queue;
341 struct wl_surface *wl_surface_wrapper;
342 struct wl_display *wl_dpy_wrapper;
343 struct wl_drm *wl_drm_wrapper;
344 struct wl_callback *throttle_callback;
345 struct zwp_linux_dmabuf_feedback_v1 *wl_dmabuf_feedback;
346 struct dmabuf_feedback dmabuf_feedback, pending_dmabuf_feedback;
347 bool compositor_using_another_device;
348 int format;
349 bool resized;
350 bool received_dmabuf_feedback;
351 #endif
352
353 #ifdef HAVE_DRM_PLATFORM
354 struct gbm_dri_surface *gbm_surf;
355 #endif
356
357 #if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)
358 struct {
359 #ifdef HAVE_WAYLAND_PLATFORM
360 struct wl_buffer *wl_buffer;
361 bool wl_release;
362 __DRIimage *dri_image;
363 /* for is_different_gpu case. NULL else */
364 __DRIimage *linear_copy;
365 /* for swrast */
366 void *data;
367 int data_size;
368 #endif
369 #ifdef HAVE_DRM_PLATFORM
370 struct gbm_bo *bo;
371 #endif
372 bool locked;
373 int age;
374 } color_buffers[4], *back, *current;
375 #endif
376
377 #ifdef HAVE_ANDROID_PLATFORM
378 struct ANativeWindow *window;
379 struct ANativeWindowBuffer *buffer;
380
381 /* in-fence associated with buffer, -1 once passed down to dri layer: */
382 int in_fence_fd;
383
384 __DRIimage *dri_image_back;
385 __DRIimage *dri_image_front;
386
387 /* Used to record all the buffers created by ANativeWindow and their ages.
388 * Allocate number of color_buffers based on query to android bufferqueue
389 * and save color_buffers_count.
390 */
391 int color_buffers_count;
392 struct {
393 struct ANativeWindowBuffer *buffer;
394 int age;
395 } *color_buffers, *back;
396 uint32_t gralloc_usage;
397 #endif
398
399 /* surfaceless and device */
400 __DRIimage *front;
401 enum pipe_format visual;
402
403 int out_fence_fd;
404 EGLBoolean enable_out_fence;
405
406 /* swrast device */
407 char *swrast_device_buffer;
408 };
409
410 struct dri2_egl_config {
411 _EGLConfig base;
412 const __DRIconfig *dri_config[2][2];
413 };
414
415 struct dri2_egl_image {
416 _EGLImage base;
417 __DRIimage *dri_image;
418 };
419
420 struct dri2_egl_sync {
421 _EGLSync base;
422 mtx_t mutex;
423 cnd_t cond;
424 int refcount;
425 void *fence;
426 };
427
428 /* standard typecasts */
429 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
_EGL_DRIVER_TYPECAST(dri2_egl_image,_EGLImage,obj)430 _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
431 _EGL_DRIVER_TYPECAST(dri2_egl_sync, _EGLSync, obj)
432
433 static inline struct dri2_egl_display *
434 dri2_egl_display_lock(_EGLDisplay *disp)
435 {
436 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
437
438 if (dri2_dpy)
439 mtx_lock(&dri2_dpy->lock);
440
441 return dri2_dpy;
442 }
443
444 static inline EGLBoolean
dri2_egl_error_unlock(struct dri2_egl_display * dri2_dpy,EGLint err,const char * msg)445 dri2_egl_error_unlock(struct dri2_egl_display *dri2_dpy, EGLint err,
446 const char *msg)
447 {
448 mtx_unlock(&dri2_dpy->lock);
449 return _eglError(err, msg);
450 }
451
452 extern const __DRIimageLookupExtension image_lookup_extension;
453 extern const __DRIuseInvalidateExtension use_invalidate;
454 extern const __DRIbackgroundCallableExtension background_callable_extension;
455 extern const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension;
456
457 EGLBoolean
458 dri2_load_driver(_EGLDisplay *disp);
459
460 /* Helper for platforms not using dri2_create_screen */
461 void
462 dri2_setup_screen(_EGLDisplay *disp);
463
464 void
465 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval);
466
467 EGLBoolean
468 dri2_create_screen(_EGLDisplay *disp);
469
470 EGLBoolean
471 dri2_setup_device(_EGLDisplay *disp, EGLBoolean software);
472
473 __DRIdrawable *
474 dri2_surface_get_dri_drawable(_EGLSurface *surf);
475
476 GLboolean
477 dri2_validate_egl_image(void *image, void *data);
478
479 __DRIimage *
480 dri2_lookup_egl_image_validated(void *image, void *data);
481
482 void
483 dri2_get_shifts_and_sizes(const __DRIconfig *config, int *shifts,
484 unsigned int *sizes);
485
486 enum pipe_format
487 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
488 const __DRIconfig *config);
489
490 struct dri2_egl_config *
491 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config,
492 EGLint surface_type, const EGLint *attr_list);
493
494 void
495 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp);
496
497 EGLint
498 dri2_from_dri_compression_rate(enum __DRIFixedRateCompression rate);
499
500 enum __DRIFixedRateCompression
501 dri2_to_dri_compression_rate(EGLint rate);
502
503 _EGLImage *
504 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
505 EGLClientBuffer buffer, const EGLint *attr_list);
506
507 _EGLImage *
508 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
509 EGLClientBuffer buffer, const EGLint *attr_list);
510
511 _EGLImage *
512 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image);
513
514 #ifdef HAVE_X11_PLATFORM
515 EGLBoolean
516 dri2_initialize_x11(_EGLDisplay *disp);
517 void
518 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy);
519 unsigned int
520 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth);
521 #else
522 static inline EGLBoolean
dri2_initialize_x11(_EGLDisplay * disp)523 dri2_initialize_x11(_EGLDisplay *disp)
524 {
525 return _eglError(EGL_NOT_INITIALIZED, "X11 platform not built");
526 }
527 static inline void
dri2_teardown_x11(struct dri2_egl_display * dri2_dpy)528 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy)
529 {
530 }
531 static inline unsigned int
dri2_x11_get_red_mask_for_depth(struct dri2_egl_display * dri2_dpy,int depth)532 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
533 {
534 return 0;
535 }
536 #endif
537
538 #ifdef HAVE_DRM_PLATFORM
539 EGLBoolean
540 dri2_initialize_drm(_EGLDisplay *disp);
541 void
542 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy);
543 #else
544 static inline EGLBoolean
dri2_initialize_drm(_EGLDisplay * disp)545 dri2_initialize_drm(_EGLDisplay *disp)
546 {
547 return _eglError(EGL_NOT_INITIALIZED, "GBM/DRM platform not built");
548 }
549 static inline void
dri2_teardown_drm(struct dri2_egl_display * dri2_dpy)550 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy)
551 {
552 }
553 #endif
554
555 #ifdef HAVE_WAYLAND_PLATFORM
556 EGLBoolean
557 dri2_initialize_wayland(_EGLDisplay *disp);
558 void
559 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy);
560 bool
561 dri2_wl_is_format_supported(void *user_data, uint32_t format);
562 #else
563 static inline EGLBoolean
dri2_initialize_wayland(_EGLDisplay * disp)564 dri2_initialize_wayland(_EGLDisplay *disp)
565 {
566 return _eglError(EGL_NOT_INITIALIZED, "Wayland platform not built");
567 }
568 static inline void
dri2_teardown_wayland(struct dri2_egl_display * dri2_dpy)569 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
570 {
571 }
572 #endif
573
574 #ifdef HAVE_ANDROID_PLATFORM
575 EGLBoolean
576 dri2_initialize_android(_EGLDisplay *disp);
577 #else
578 static inline EGLBoolean
dri2_initialize_android(_EGLDisplay * disp)579 dri2_initialize_android(_EGLDisplay *disp)
580 {
581 return _eglError(EGL_NOT_INITIALIZED, "Android platform not built");
582 }
583 #endif
584
585 EGLBoolean
586 dri2_initialize_surfaceless(_EGLDisplay *disp);
587
588 EGLBoolean
589 dri2_initialize_device(_EGLDisplay *disp);
590 static inline void
dri2_teardown_device(struct dri2_egl_display * dri2_dpy)591 dri2_teardown_device(struct dri2_egl_display *dri2_dpy)
592 { /* noop */
593 }
594
595 void
596 dri2_flush_drawable_for_swapbuffers_flags(
597 _EGLDisplay *disp, _EGLSurface *draw,
598 enum __DRI2throttleReason throttle_reason);
599 void
600 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);
601
602 const __DRIconfig *
603 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
604 EGLenum colorspace);
605 #include "dri_util.h"
606 static inline void
dri2_set_WL_bind_wayland_display(_EGLDisplay * disp)607 dri2_set_WL_bind_wayland_display(_EGLDisplay *disp)
608 {
609 #ifdef HAVE_WAYLAND_PLATFORM
610 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
611
612 int capabilities;
613
614 capabilities =
615 dri2_get_capabilities(dri2_dpy->dri_screen_render_gpu);
616 disp->Extensions.WL_bind_wayland_display =
617 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
618 #endif
619 }
620
621 void
622 dri2_display_destroy(_EGLDisplay *disp);
623
624 struct dri2_egl_display *
625 dri2_display_create(void);
626
627 EGLBoolean
628 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
629 _EGLConfig *conf, const EGLint *attrib_list,
630 EGLBoolean enable_out_fence, void *native_surface);
631
632 void
633 dri2_fini_surface(_EGLSurface *surf);
634
635 EGLBoolean
636 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
637 const __DRIconfig *config,
638 struct dri2_egl_surface *dri2_surf, void *loaderPrivate);
639
640 static inline uint64_t
combine_u32_into_u64(uint32_t hi,uint32_t lo)641 combine_u32_into_u64(uint32_t hi, uint32_t lo)
642 {
643 return (((uint64_t)hi) << 32) | (((uint64_t)lo) & 0xffffffff);
644 }
645
646 #endif /* EGL_DRI2_INCLUDED */
647