Debian Codegraph @

src/egl/drivers/dri2/egl_dri2.h

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