Path: blob/21.2-virgl/src/egl/drivers/dri2/egl_dri2.h
4570 views
/*1* Copyright © 2011 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT18* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,19* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23* Authors:24* Kristian Høgsberg <[email protected]>25*/2627#ifndef EGL_DRI2_INCLUDED28#define EGL_DRI2_INCLUDED2930#include <stdbool.h>31#include <stdint.h>3233#ifdef HAVE_X11_PLATFORM34#include <xcb/xcb.h>35#include <xcb/dri2.h>36#include <xcb/xfixes.h>37#include <X11/Xlib-xcb.h>3839#ifdef HAVE_DRI340#include "loader_dri3_helper.h"41#endif42#endif4344#ifdef HAVE_WAYLAND_PLATFORM45/* forward declarations to avoid pulling wayland headers everywhere */46struct wl_egl_window;47struct wl_event_queue;48struct wl_callback;49struct wl_display;50struct wl_drm;51struct wl_registry;52struct wl_shm;53struct wl_surface;54struct zwp_linux_dmabuf_v1;55#endif5657#include <GL/gl.h>58#include <GL/internal/dri_interface.h>5960#ifdef HAVE_DRM_PLATFORM61#include <gbm_driint.h>62#endif6364#ifdef HAVE_ANDROID_PLATFORM65#define LOG_TAG "EGL-DRI2"6667#include <hardware/gralloc.h>6869#if ANDROID_API_LEVEL >= 2670#include <vndk/window.h>71#else72#include <system/window.h>73#endif7475#endif /* HAVE_ANDROID_PLATFORM */7677#include "eglconfig.h"78#include "eglcontext.h"79#include "egldevice.h"80#include "egldisplay.h"81#include "egldriver.h"82#include "eglcurrent.h"83#include "egllog.h"84#include "eglsurface.h"85#include "eglimage.h"86#include "eglsync.h"8788#include "util/u_vector.h"89#include "util/bitset.h"9091#define EGL_DRI2_MAX_FORMATS 109293struct wl_buffer;9495struct dri2_egl_display_vtbl {96/* mandatory on Wayland, unused otherwise */97int (*authenticate)(_EGLDisplay *disp, uint32_t id);9899/* mandatory */100_EGLSurface* (*create_window_surface)(_EGLDisplay *disp, _EGLConfig *config,101void *native_window,102const EGLint *attrib_list);103104/* optional */105_EGLSurface* (*create_pixmap_surface)(_EGLDisplay *disp, _EGLConfig *config,106void *native_pixmap,107const EGLint *attrib_list);108109/* optional */110_EGLSurface* (*create_pbuffer_surface)(_EGLDisplay *disp, _EGLConfig *config,111const EGLint *attrib_list);112113/* mandatory */114EGLBoolean (*destroy_surface)(_EGLDisplay *disp, _EGLSurface *surface);115116/* optional */117EGLBoolean (*swap_interval)(_EGLDisplay *disp, _EGLSurface *surf,118EGLint interval);119120/* mandatory */121_EGLImage* (*create_image)(_EGLDisplay *disp, _EGLContext *ctx,122EGLenum target, EGLClientBuffer buffer,123const EGLint *attr_list);124125/* mandatory */126EGLBoolean (*swap_buffers)(_EGLDisplay *disp, _EGLSurface *surf);127128/* optional - falls back to .swap_buffers */129EGLBoolean (*swap_buffers_with_damage)(_EGLDisplay *disp, _EGLSurface *surface,130const EGLint *rects, EGLint n_rects);131132/* optional */133EGLBoolean (*swap_buffers_region)(_EGLDisplay *disp, _EGLSurface *surf,134EGLint numRects, const EGLint *rects);135136/* optional */137EGLBoolean (*post_sub_buffer)(_EGLDisplay *disp, _EGLSurface *surf,138EGLint x, EGLint y,139EGLint width, EGLint height);140141/* optional */142EGLBoolean (*copy_buffers)(_EGLDisplay *disp, _EGLSurface *surf,143void *native_pixmap_target);144145/* optional */146EGLint (*query_buffer_age)(_EGLDisplay *disp, _EGLSurface *surf);147148/* optional */149EGLBoolean (*query_surface)(_EGLDisplay *disp, _EGLSurface *surf,150EGLint attribute, EGLint *value);151152/* optional */153struct wl_buffer* (*create_wayland_buffer_from_image)(_EGLDisplay *disp,154_EGLImage *img);155156/* optional */157EGLBoolean (*get_sync_values)(_EGLDisplay *display, _EGLSurface *surface,158EGLuint64KHR *ust, EGLuint64KHR *msc,159EGLuint64KHR *sbc);160161/* mandatory */162__DRIdrawable *(*get_dri_drawable)(_EGLSurface *surf);163164/* optional */165void (*close_screen_notify)(_EGLDisplay *disp);166167/* Used in EGL_KHR_mutable_render_buffer to update the native window's168* shared buffer mode.169* optional170*/171bool (*set_shared_buffer_mode)(_EGLDisplay *disp, _EGLSurface *surf,172bool mode);173};174175struct dri2_egl_display176{177const struct dri2_egl_display_vtbl *vtbl;178179int dri2_major;180int dri2_minor;181__DRIscreen *dri_screen;182bool own_dri_screen;183const __DRIconfig **driver_configs;184void *driver;185const __DRIcoreExtension *core;186const __DRIimageDriverExtension *image_driver;187const __DRIdri2Extension *dri2;188const __DRIswrastExtension *swrast;189const __DRI2flushExtension *flush;190const __DRI2flushControlExtension *flush_control;191const __DRItexBufferExtension *tex_buffer;192const __DRIimageExtension *image;193const __DRIrobustnessExtension *robustness;194const __DRInoErrorExtension *no_error;195const __DRI2configQueryExtension *config;196const __DRI2fenceExtension *fence;197const __DRI2bufferDamageExtension *buffer_damage;198const __DRI2blobExtension *blob;199const __DRI2rendererQueryExtension *rendererQuery;200const __DRI2interopExtension *interop;201const __DRIconfigOptionsExtension *configOptions;202const __DRImutableRenderBufferDriverExtension *mutable_render_buffer;203int fd;204205/* dri2_initialize/dri2_terminate increment/decrement this count, so does206* dri2_make_current (tracks if there are active contexts/surfaces). */207int ref_count;208209bool own_device;210bool invalidate_available;211int min_swap_interval;212int max_swap_interval;213int default_swap_interval;214#ifdef HAVE_DRM_PLATFORM215struct gbm_dri_device *gbm_dri;216#endif217218char *driver_name;219220const __DRIextension **loader_extensions;221const __DRIextension **driver_extensions;222223#ifdef HAVE_X11_PLATFORM224xcb_connection_t *conn;225xcb_screen_t *screen;226bool swap_available;227#ifdef HAVE_DRI3228bool multibuffers_available;229int dri3_major_version;230int dri3_minor_version;231int present_major_version;232int present_minor_version;233struct loader_dri3_extensions loader_dri3_ext;234#endif235#endif236237#ifdef HAVE_WAYLAND_PLATFORM238struct wl_display *wl_dpy;239struct wl_display *wl_dpy_wrapper;240struct wl_registry *wl_registry;241struct wl_drm *wl_server_drm;242struct wl_drm *wl_drm;243struct wl_shm *wl_shm;244struct wl_event_queue *wl_queue;245struct zwp_linux_dmabuf_v1 *wl_dmabuf;246struct u_vector *wl_modifiers;247bool authenticated;248BITSET_DECLARE(formats, EGL_DRI2_MAX_FORMATS);249uint32_t capabilities;250char *device_name;251#endif252253#ifdef HAVE_ANDROID_PLATFORM254const gralloc_module_t *gralloc;255/* gralloc vendor usage bit for front rendering */256uint32_t front_rendering_usage;257#endif258259bool is_render_node;260bool is_different_gpu;261};262263struct dri2_egl_context264{265_EGLContext base;266__DRIcontext *dri_context;267};268269#ifdef HAVE_WAYLAND_PLATFORM270enum wayland_buffer_type {271WL_BUFFER_FRONT,272WL_BUFFER_BACK,273WL_BUFFER_THIRD,274WL_BUFFER_COUNT275};276#endif277278struct dri2_egl_surface279{280_EGLSurface base;281__DRIdrawable *dri_drawable;282__DRIbuffer buffers[5];283bool have_fake_front;284285#ifdef HAVE_X11_PLATFORM286xcb_drawable_t drawable;287xcb_xfixes_region_t region;288int depth;289int bytes_per_pixel;290xcb_gcontext_t gc;291xcb_gcontext_t swapgc;292#endif293294#ifdef HAVE_WAYLAND_PLATFORM295struct wl_egl_window *wl_win;296int dx;297int dy;298struct wl_event_queue *wl_queue;299struct wl_surface *wl_surface_wrapper;300struct wl_display *wl_dpy_wrapper;301struct wl_drm *wl_drm_wrapper;302struct wl_callback *throttle_callback;303int format;304#endif305306#ifdef HAVE_DRM_PLATFORM307struct gbm_dri_surface *gbm_surf;308#endif309310/* EGL-owned buffers */311__DRIbuffer *local_buffers[__DRI_BUFFER_COUNT];312313#if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)314struct {315#ifdef HAVE_WAYLAND_PLATFORM316struct wl_buffer *wl_buffer;317bool wl_release;318__DRIimage *dri_image;319/* for is_different_gpu case. NULL else */320__DRIimage *linear_copy;321/* for swrast */322void *data;323int data_size;324#endif325#ifdef HAVE_DRM_PLATFORM326struct gbm_bo *bo;327#endif328bool locked;329int age;330} color_buffers[4], *back, *current;331#endif332333#ifdef HAVE_ANDROID_PLATFORM334struct ANativeWindow *window;335struct ANativeWindowBuffer *buffer;336__DRIimage *dri_image_back;337__DRIimage *dri_image_front;338339/* Used to record all the buffers created by ANativeWindow and their ages.340* Allocate number of color_buffers based on query to android bufferqueue341* and save color_buffers_count.342*/343int color_buffers_count;344struct {345struct ANativeWindowBuffer *buffer;346int age;347} *color_buffers, *back;348uint32_t gralloc_usage;349#endif350351/* surfaceless and device */352__DRIimage *front;353unsigned int visual;354355int out_fence_fd;356EGLBoolean enable_out_fence;357358/* swrast device */359char *swrast_device_buffer;360};361362struct dri2_egl_config363{364_EGLConfig base;365const __DRIconfig *dri_config[2][2];366};367368struct dri2_egl_image369{370_EGLImage base;371__DRIimage *dri_image;372};373374struct dri2_egl_sync {375_EGLSync base;376mtx_t mutex;377cnd_t cond;378int refcount;379void *fence;380};381382/* From driconf.h, user exposed so should be stable */383#define DRI_CONF_VBLANK_NEVER 0384#define DRI_CONF_VBLANK_DEF_INTERVAL_0 1385#define DRI_CONF_VBLANK_DEF_INTERVAL_1 2386#define DRI_CONF_VBLANK_ALWAYS_SYNC 3387388/* standard typecasts */389_EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)390_EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)391_EGL_DRIVER_TYPECAST(dri2_egl_sync, _EGLSync, obj)392393extern const __DRIimageLookupExtension image_lookup_extension;394extern const __DRIuseInvalidateExtension use_invalidate;395extern const __DRIbackgroundCallableExtension background_callable_extension;396extern const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension;397398EGLBoolean399dri2_load_driver(_EGLDisplay *disp);400401/* Helper for platforms not using dri2_create_screen */402void403dri2_setup_screen(_EGLDisplay *disp);404405void406dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval);407408EGLBoolean409dri2_load_driver_swrast(_EGLDisplay *disp);410411EGLBoolean412dri2_load_driver_dri3(_EGLDisplay *disp);413414EGLBoolean415dri2_create_screen(_EGLDisplay *disp);416417EGLBoolean418dri2_setup_extensions(_EGLDisplay *disp);419420__DRIdrawable *421dri2_surface_get_dri_drawable(_EGLSurface *surf);422423__DRIimage *424dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data);425426void427dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,428const __DRIconfig *config, int *shifts,429unsigned int *sizes);430431void432dri2_get_render_type_float(const __DRIcoreExtension *core,433const __DRIconfig *config,434bool *is_float);435436unsigned int437dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,438const __DRIconfig *config);439440struct dri2_egl_config *441dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,442EGLint surface_type, const EGLint *attr_list,443const int *rgba_shifts, const unsigned int *rgba_sizes);444445EGLBoolean446dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp);447448_EGLImage *449dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,450EGLClientBuffer buffer, const EGLint *attr_list);451452_EGLImage *453dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,454EGLClientBuffer buffer, const EGLint *attr_list);455456_EGLImage *457dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image);458459#ifdef HAVE_X11_PLATFORM460EGLBoolean461dri2_initialize_x11(_EGLDisplay *disp);462void463dri2_teardown_x11(struct dri2_egl_display *dri2_dpy);464unsigned int465dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth);466#else467static inline EGLBoolean468dri2_initialize_x11(_EGLDisplay *disp)469{470return _eglError(EGL_NOT_INITIALIZED, "X11 platform not built");471}472static inline void473dri2_teardown_x11(struct dri2_egl_display *dri2_dpy) {}474static inline unsigned int475dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)476{477return 0;478}479#endif480481#ifdef HAVE_DRM_PLATFORM482EGLBoolean483dri2_initialize_drm(_EGLDisplay *disp);484void485dri2_teardown_drm(struct dri2_egl_display *dri2_dpy);486#else487static inline EGLBoolean488dri2_initialize_drm(_EGLDisplay *disp)489{490return _eglError(EGL_NOT_INITIALIZED, "GBM/DRM platform not built");491}492static inline void493dri2_teardown_drm(struct dri2_egl_display *dri2_dpy) {}494#endif495496#ifdef HAVE_WAYLAND_PLATFORM497EGLBoolean498dri2_initialize_wayland(_EGLDisplay *disp);499void500dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy);501bool502dri2_wl_is_format_supported(void* user_data, uint32_t format);503#else504static inline EGLBoolean505dri2_initialize_wayland(_EGLDisplay *disp)506{507return _eglError(EGL_NOT_INITIALIZED, "Wayland platform not built");508}509static inline void510dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy) {}511#endif512513#ifdef HAVE_ANDROID_PLATFORM514EGLBoolean515dri2_initialize_android(_EGLDisplay *disp);516#else517static inline EGLBoolean518dri2_initialize_android(_EGLDisplay *disp)519{520return _eglError(EGL_NOT_INITIALIZED, "Android platform not built");521}522#endif523524EGLBoolean525dri2_initialize_surfaceless(_EGLDisplay *disp);526527EGLBoolean528dri2_initialize_device(_EGLDisplay *disp);529static inline void530dri2_teardown_device(struct dri2_egl_display *dri2_dpy) { /* noop */ }531532void533dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);534535const __DRIconfig *536dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,537EGLenum colorspace);538539static inline void540dri2_set_WL_bind_wayland_display(_EGLDisplay *disp)541{542#ifdef HAVE_WAYLAND_PLATFORM543struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);544545if (dri2_dpy->device_name && dri2_dpy->image) {546if (dri2_dpy->image->base.version >= 10 &&547dri2_dpy->image->getCapabilities != NULL) {548int capabilities;549550capabilities =551dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);552disp->Extensions.WL_bind_wayland_display =553(capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;554} else {555disp->Extensions.WL_bind_wayland_display = EGL_TRUE;556}557}558#endif559}560561void562dri2_display_destroy(_EGLDisplay *disp);563564__DRIbuffer *565dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,566unsigned int att, unsigned int format);567568void569dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf);570571EGLBoolean572dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,573_EGLConfig *conf, const EGLint *attrib_list,574EGLBoolean enable_out_fence, void *native_surface);575576void577dri2_fini_surface(_EGLSurface *surf);578579EGLBoolean580dri2_create_drawable(struct dri2_egl_display *dri2_dpy,581const __DRIconfig *config,582struct dri2_egl_surface *dri2_surf,583void *loaderPrivate);584585static inline uint64_t586combine_u32_into_u64(uint32_t hi, uint32_t lo)587{588return (((uint64_t) hi) << 32) | (((uint64_t) lo) & 0xffffffff);589}590591#endif /* EGL_DRI2_INCLUDED */592593594