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
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 <xcb/xcb.h>
36 #include <xcb/dri2.h>
37 #include <xcb/xfixes.h>
38 #include <X11/Xlib-xcb.h>
39
40 #ifdef HAVE_DRI3
41 #include "loader_dri3_helper.h"
42 #endif
43 #endif
44
45 #ifdef HAVE_WAYLAND_PLATFORM
46 /* forward declarations to avoid pulling wayland headers everywhere */
47 struct wl_egl_window;
48 struct wl_event_queue;
49 struct wl_callback;
50 struct wl_display;
51 struct wl_drm;
52 struct wl_registry;
53 struct wl_shm;
54 struct wl_surface;
55 struct zwp_linux_dmabuf_v1;
56 #endif
57
58 #include <GL/gl.h>
59 #include <GL/internal/dri_interface.h>
60
61 #ifdef HAVE_DRM_PLATFORM
62 #include <gbm_driint.h>
63 #endif
64
65 #ifdef HAVE_ANDROID_PLATFORM
66 #define LOG_TAG "EGL-DRI2"
67
68 #include <hardware/gralloc.h>
69
70 #if ANDROID_API_LEVEL >= 26
71 #include <vndk/window.h>
72 #else
73 #include <system/window.h>
74 #endif
75
76 #ifdef HAVE_OHOS_PLATFORM
77 #include "window.h"
78 #endif
79
80 #endif /* HAVE_ANDROID_PLATFORM */
81
82 #include "eglconfig.h"
83 #include "eglcontext.h"
84 #include "egldevice.h"
85 #include "egldisplay.h"
86 #include "egldriver.h"
87 #include "eglcurrent.h"
88 #include "egllog.h"
89 #include "eglsurface.h"
90 #include "eglimage.h"
91 #include "eglsync.h"
92
93 #include "util/u_vector.h"
94 #include "util/bitset.h"
95
96 #define EGL_DRI2_MAX_FORMATS 11
97
98 struct wl_buffer;
99
100 struct dri2_egl_display_vtbl {
101 /* mandatory on Wayland, unused otherwise */
102 int (*authenticate)(_EGLDisplay *disp, uint32_t id);
103
104 /* mandatory */
105 _EGLSurface* (*create_window_surface)(_EGLDisplay *disp, _EGLConfig *config,
106 void *native_window,
107 const EGLint *attrib_list);
108
109 /* optional */
110 _EGLSurface* (*create_pixmap_surface)(_EGLDisplay *disp, _EGLConfig *config,
111 void *native_pixmap,
112 const EGLint *attrib_list);
113
114 /* optional */
115 _EGLSurface* (*create_pbuffer_surface)(_EGLDisplay *disp, _EGLConfig *config,
116 const EGLint *attrib_list);
117
118 /* mandatory */
119 EGLBoolean (*destroy_surface)(_EGLDisplay *disp, _EGLSurface *surface);
120
121 /* optional */
122 EGLBoolean (*swap_interval)(_EGLDisplay *disp, _EGLSurface *surf,
123 EGLint interval);
124
125 /* mandatory */
126 _EGLImage* (*create_image)(_EGLDisplay *disp, _EGLContext *ctx,
127 EGLenum target, EGLClientBuffer buffer,
128 const EGLint *attr_list);
129
130 /* mandatory */
131 EGLBoolean (*swap_buffers)(_EGLDisplay *disp, _EGLSurface *surf);
132
133 /* optional - falls back to .swap_buffers */
134 EGLBoolean (*swap_buffers_with_damage)(_EGLDisplay *disp, _EGLSurface *surface,
135 const EGLint *rects, EGLint n_rects);
136
137 /* optional */
138 EGLBoolean (*swap_buffers_region)(_EGLDisplay *disp, _EGLSurface *surf,
139 EGLint numRects, const EGLint *rects);
140
141 /* optional */
142 EGLBoolean (*post_sub_buffer)(_EGLDisplay *disp, _EGLSurface *surf,
143 EGLint x, EGLint y,
144 EGLint width, EGLint height);
145
146 /* optional */
147 EGLBoolean (*copy_buffers)(_EGLDisplay *disp, _EGLSurface *surf,
148 void *native_pixmap_target);
149
150 /* optional */
151 EGLint (*query_buffer_age)(_EGLDisplay *disp, _EGLSurface *surf);
152
153 /* optional */
154 EGLBoolean (*query_surface)(_EGLDisplay *disp, _EGLSurface *surf,
155 EGLint attribute, EGLint *value);
156
157 /* optional */
158 struct wl_buffer* (*create_wayland_buffer_from_image)(_EGLDisplay *disp,
159 _EGLImage *img);
160
161 /* optional */
162 EGLBoolean (*get_sync_values)(_EGLDisplay *display, _EGLSurface *surface,
163 EGLuint64KHR *ust, EGLuint64KHR *msc,
164 EGLuint64KHR *sbc);
165
166 /* mandatory */
167 __DRIdrawable *(*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 struct dri2_egl_display
181 {
182 const struct dri2_egl_display_vtbl *vtbl;
183
184 int dri2_major;
185 int dri2_minor;
186 __DRIscreen *dri_screen;
187 bool own_dri_screen;
188 const __DRIconfig **driver_configs;
189 void *driver;
190 const __DRIcoreExtension *core;
191 const __DRIimageDriverExtension *image_driver;
192 const __DRIdri2Extension *dri2;
193 const __DRIswrastExtension *swrast;
194 const __DRI2flushExtension *flush;
195 const __DRI2flushControlExtension *flush_control;
196 const __DRItexBufferExtension *tex_buffer;
197 const __DRIimageExtension *image;
198 const __DRIrobustnessExtension *robustness;
199 const __DRInoErrorExtension *no_error;
200 const __DRI2configQueryExtension *config;
201 const __DRI2fenceExtension *fence;
202 const __DRI2bufferDamageExtension *buffer_damage;
203 const __DRI2blobExtension *blob;
204 const __DRI2rendererQueryExtension *rendererQuery;
205 const __DRI2interopExtension *interop;
206 const __DRIconfigOptionsExtension *configOptions;
207 const __DRImutableRenderBufferDriverExtension *mutable_render_buffer;
208 int fd;
209
210 /* dri2_initialize/dri2_terminate increment/decrement this count, so does
211 * dri2_make_current (tracks if there are active contexts/surfaces). */
212 int ref_count;
213
214 bool own_device;
215 bool invalidate_available;
216 int min_swap_interval;
217 int max_swap_interval;
218 int default_swap_interval;
219 #ifdef HAVE_DRM_PLATFORM
220 struct gbm_dri_device *gbm_dri;
221 #endif
222
223 char *driver_name;
224
225 const __DRIextension **loader_extensions;
226 const __DRIextension **driver_extensions;
227
228 #ifdef HAVE_X11_PLATFORM
229 xcb_connection_t *conn;
230 xcb_screen_t *screen;
231 bool swap_available;
232 #ifdef HAVE_DRI3
233 bool multibuffers_available;
234 int dri3_major_version;
235 int dri3_minor_version;
236 int present_major_version;
237 int present_minor_version;
238 struct loader_dri3_extensions loader_dri3_ext;
239 #endif
240 #endif
241
242 #ifdef HAVE_WAYLAND_PLATFORM
243 struct wl_display *wl_dpy;
244 struct wl_display *wl_dpy_wrapper;
245 struct wl_registry *wl_registry;
246 struct wl_drm *wl_server_drm;
247 struct wl_drm *wl_drm;
248 struct wl_shm *wl_shm;
249 struct wl_event_queue *wl_queue;
250 struct zwp_linux_dmabuf_v1 *wl_dmabuf;
251 struct u_vector *wl_modifiers;
252 bool authenticated;
253 BITSET_DECLARE(formats, EGL_DRI2_MAX_FORMATS);
254 uint32_t capabilities;
255 char *device_name;
256 #endif
257
258 #ifdef HAVE_ANDROID_PLATFORM
259 const gralloc_module_t *gralloc;
260 /* gralloc vendor usage bit for front rendering */
261 uint32_t front_rendering_usage;
262 #endif
263
264 bool is_render_node;
265 bool is_different_gpu;
266 };
267
268 struct dri2_egl_context
269 {
270 _EGLContext base;
271 __DRIcontext *dri_context;
272 };
273
274 #ifdef HAVE_WAYLAND_PLATFORM
275 enum wayland_buffer_type {
276 WL_BUFFER_FRONT,
277 WL_BUFFER_BACK,
278 WL_BUFFER_THIRD,
279 WL_BUFFER_COUNT
280 };
281 #endif
282
283 struct dri2_egl_surface
284 {
285 _EGLSurface base;
286 __DRIdrawable *dri_drawable;
287 __DRIbuffer buffers[5];
288 bool have_fake_front;
289
290 #ifdef HAVE_X11_PLATFORM
291 xcb_drawable_t drawable;
292 xcb_xfixes_region_t region;
293 int depth;
294 int bytes_per_pixel;
295 xcb_gcontext_t gc;
296 xcb_gcontext_t swapgc;
297 #endif
298
299 #ifdef HAVE_WAYLAND_PLATFORM
300 struct wl_egl_window *wl_win;
301 int dx;
302 int dy;
303 struct wl_event_queue *wl_queue;
304 struct wl_surface *wl_surface_wrapper;
305 struct wl_display *wl_dpy_wrapper;
306 struct wl_drm *wl_drm_wrapper;
307 struct wl_callback *throttle_callback;
308 int format;
309 bool resized;
310 #endif
311
312 #ifdef HAVE_DRM_PLATFORM
313 struct gbm_dri_surface *gbm_surf;
314 #endif
315
316 /* EGL-owned buffers */
317 __DRIbuffer *local_buffers[__DRI_BUFFER_COUNT];
318
319 #if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)
320 struct {
321 #ifdef HAVE_WAYLAND_PLATFORM
322 struct wl_buffer *wl_buffer;
323 bool wl_release;
324 __DRIimage *dri_image;
325 /* for is_different_gpu case. NULL else */
326 __DRIimage *linear_copy;
327 /* for swrast */
328 void *data;
329 int data_size;
330 #endif
331 #ifdef HAVE_DRM_PLATFORM
332 struct gbm_bo *bo;
333 #endif
334 bool locked;
335 int age;
336 } color_buffers[4], *back, *current;
337 #endif
338
339 #if defined(HAVE_ANDROID_PLATFORM) || defined (HAVE_OHOS_PLATFORM)
340 struct NativeWindow *window;
341 struct NativeWindowBuffer *buffer;
342 __DRIimage *dri_image_back;
343 __DRIimage *dri_image_front;
344
345 /* Used to record all the buffers created by ANativeWindow and their ages.
346 * Allocate number of color_buffers based on query to android bufferqueue
347 * and save color_buffers_count.
348 */
349 int color_buffers_count;
350 struct {
351 struct NativeWindowBuffer *buffer;
352 int age;
353 } *color_buffers, *back;
354 uint32_t gralloc_usage;
355 #endif
356
357 /* surfaceless and device */
358 __DRIimage *front;
359 unsigned int visual;
360
361 int out_fence_fd;
362 EGLBoolean enable_out_fence;
363
364 /* swrast device */
365 char *swrast_device_buffer;
366 };
367
368 struct dri2_egl_config
369 {
370 _EGLConfig base;
371 const __DRIconfig *dri_config[2][2];
372 };
373
374 struct dri2_egl_image
375 {
376 _EGLImage base;
377 __DRIimage *dri_image;
378 };
379
380 struct dri2_egl_sync {
381 _EGLSync base;
382 mtx_t mutex;
383 cnd_t cond;
384 int refcount;
385 void *fence;
386 };
387
388 /* From driconf.h, user exposed so should be stable */
389 #define DRI_CONF_VBLANK_NEVER 0
390 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
391 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
392 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
393
394 /* standard typecasts */
395 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
396 _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
397 _EGL_DRIVER_TYPECAST(dri2_egl_sync, _EGLSync, obj)
398
399 extern const __DRIimageLookupExtension image_lookup_extension;
400 extern const __DRIuseInvalidateExtension use_invalidate;
401 extern const __DRIbackgroundCallableExtension background_callable_extension;
402 extern const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension;
403
404 EGLBoolean
405 dri2_load_driver(_EGLDisplay *disp);
406
407 /* Helper for platforms not using dri2_create_screen */
408 void
409 dri2_setup_screen(_EGLDisplay *disp);
410
411 void
412 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval);
413
414 EGLBoolean
415 dri2_load_driver_swrast(_EGLDisplay *disp);
416
417 EGLBoolean
418 dri2_load_driver_dri3(_EGLDisplay *disp);
419
420 EGLBoolean
421 dri2_create_screen(_EGLDisplay *disp);
422
423 EGLBoolean
424 dri2_setup_extensions(_EGLDisplay *disp);
425
426 __DRIdrawable *
427 dri2_surface_get_dri_drawable(_EGLSurface *surf);
428
429 GLboolean
430 dri2_validate_egl_image(void *image, void *data);
431
432 __DRIimage *
433 dri2_lookup_egl_image_validated(void *image, void *data);
434
435 __DRIimage *
436 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data);
437
438 void
439 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
440 const __DRIconfig *config, int *shifts,
441 unsigned int *sizes);
442
443 void
444 dri2_get_render_type_float(const __DRIcoreExtension *core,
445 const __DRIconfig *config,
446 bool *is_float);
447
448 unsigned int
449 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
450 const __DRIconfig *config);
451
452 struct dri2_egl_config *
453 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
454 EGLint surface_type, const EGLint *attr_list,
455 const int *rgba_shifts, const unsigned int *rgba_sizes);
456
457 EGLBoolean
458 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp);
459
460 _EGLImage *
461 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
462 EGLClientBuffer buffer, const EGLint *attr_list);
463
464 _EGLImage *
465 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
466 EGLClientBuffer buffer, const EGLint *attr_list);
467
468 _EGLImage *
469 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image);
470
471 #ifdef HAVE_X11_PLATFORM
472 EGLBoolean
473 dri2_initialize_x11(_EGLDisplay *disp);
474 void
475 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy);
476 unsigned int
477 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth);
478 #else
479 static inline EGLBoolean
dri2_initialize_x11(_EGLDisplay * disp)480 dri2_initialize_x11(_EGLDisplay *disp)
481 {
482 return _eglError(EGL_NOT_INITIALIZED, "X11 platform not built");
483 }
484 static inline void
dri2_teardown_x11(struct dri2_egl_display * dri2_dpy)485 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy) {}
486 static inline unsigned int
dri2_x11_get_red_mask_for_depth(struct dri2_egl_display * dri2_dpy,int depth)487 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
488 {
489 return 0;
490 }
491 #endif
492
493 #ifdef HAVE_DRM_PLATFORM
494 EGLBoolean
495 dri2_initialize_drm(_EGLDisplay *disp);
496 void
497 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy);
498 #else
499 static inline EGLBoolean
dri2_initialize_drm(_EGLDisplay * disp)500 dri2_initialize_drm(_EGLDisplay *disp)
501 {
502 return _eglError(EGL_NOT_INITIALIZED, "GBM/DRM platform not built");
503 }
504 static inline void
dri2_teardown_drm(struct dri2_egl_display * dri2_dpy)505 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy) {}
506 #endif
507
508 #ifdef HAVE_WAYLAND_PLATFORM
509 EGLBoolean
510 dri2_initialize_wayland(_EGLDisplay *disp);
511 void
512 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy);
513 bool
514 dri2_wl_is_format_supported(void* user_data, uint32_t format);
515 #else
516 static inline EGLBoolean
dri2_initialize_wayland(_EGLDisplay * disp)517 dri2_initialize_wayland(_EGLDisplay *disp)
518 {
519 return _eglError(EGL_NOT_INITIALIZED, "Wayland platform not built");
520 }
521 static inline void
dri2_teardown_wayland(struct dri2_egl_display * dri2_dpy)522 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy) {}
523 #endif
524
525 #ifdef HAVE_ANDROID_PLATFORM
526 EGLBoolean
527 dri2_initialize_android(_EGLDisplay *disp);
528 #else
529 static inline EGLBoolean
dri2_initialize_android(_EGLDisplay * disp)530 dri2_initialize_android(_EGLDisplay *disp)
531 {
532 return _eglError(EGL_NOT_INITIALIZED, "Android platform not built");
533 }
534 #endif
535
536 #ifdef HAVE_OHOS_PLATFORM
537 EGLBoolean
538 dri2_initialize_ohos(_EGLDisplay *disp);
539 #else
540 static inline EGLBoolean
dri2_initialize_ohos(_EGLDisplay * disp)541 dri2_initialize_ohos(_EGLDisplay *disp)
542 {
543 return _eglError(EGL_NOT_INITIALIZED, "ohos platform not built");
544 }
545 #endif
546
547 EGLBoolean
548 dri2_initialize_surfaceless(_EGLDisplay *disp);
549
550 EGLBoolean
551 dri2_initialize_device(_EGLDisplay *disp);
552 static inline void
dri2_teardown_device(struct dri2_egl_display * dri2_dpy)553 dri2_teardown_device(struct dri2_egl_display *dri2_dpy) { /* noop */ }
554
555 void
556 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);
557
558 const __DRIconfig *
559 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
560 EGLenum colorspace);
561
562 static inline void
dri2_set_WL_bind_wayland_display(_EGLDisplay * disp)563 dri2_set_WL_bind_wayland_display(_EGLDisplay *disp)
564 {
565 #ifdef HAVE_WAYLAND_PLATFORM
566 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
567
568 if (dri2_dpy->device_name && dri2_dpy->image) {
569 if (dri2_dpy->image->base.version >= 10 &&
570 dri2_dpy->image->getCapabilities != NULL) {
571 int capabilities;
572
573 capabilities =
574 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
575 disp->Extensions.WL_bind_wayland_display =
576 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
577 } else {
578 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
579 }
580 }
581 #endif
582 }
583
584 void
585 dri2_display_destroy(_EGLDisplay *disp);
586
587 __DRIbuffer *
588 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
589 unsigned int att, unsigned int format);
590
591 void
592 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf);
593
594 EGLBoolean
595 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
596 _EGLConfig *conf, const EGLint *attrib_list,
597 EGLBoolean enable_out_fence, void *native_surface);
598
599 void
600 dri2_fini_surface(_EGLSurface *surf);
601
602 EGLBoolean
603 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
604 const __DRIconfig *config,
605 struct dri2_egl_surface *dri2_surf,
606 void *loaderPrivate);
607
608 static inline uint64_t
combine_u32_into_u64(uint32_t hi,uint32_t lo)609 combine_u32_into_u64(uint32_t hi, uint32_t lo)
610 {
611 return (((uint64_t) hi) << 32) | (((uint64_t) lo) & 0xffffffff);
612 }
613
614 #endif /* EGL_DRI2_INCLUDED */
615