Path: blob/21.2-virgl/src/egl/drivers/dri2/egl_dri2.c
4570 views
/*1* Copyright © 2010 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#include <stdbool.h>28#include <stdint.h>29#include <stdbool.h>30#include <stdlib.h>31#include <string.h>32#include <stdio.h>33#include <limits.h>34#include <dlfcn.h>35#include <fcntl.h>36#include <errno.h>37#include <unistd.h>38#include <c11/threads.h>39#include <time.h>40#ifdef HAVE_LIBDRM41#include <xf86drm.h>42#include "drm-uapi/drm_fourcc.h"43#endif44#include <GL/gl.h>45#include <GL/internal/dri_interface.h>46#include <sys/types.h>47#include <sys/stat.h>4849#ifdef HAVE_WAYLAND_PLATFORM50#include <wayland-client.h>51#include "wayland-drm.h"52#include "wayland-drm-client-protocol.h"53#include "linux-dmabuf-unstable-v1-client-protocol.h"54#endif5556#ifdef HAVE_X11_PLATFORM57#include "X11/Xlibint.h"58#endif5960#include "egldefines.h"61#include "egl_dri2.h"62#include "GL/mesa_glinterop.h"63#include "loader/loader.h"64#include "util/libsync.h"65#include "util/os_file.h"66#include "util/u_atomic.h"67#include "util/u_vector.h"68#include "mapi/glapi/glapi.h"69#include "util/bitscan.h"70#include "util/u_math.h"7172#define NUM_ATTRIBS 127374static const struct dri2_pbuffer_visual {75const char *format_name;76unsigned int dri_image_format;77int rgba_shifts[4];78unsigned int rgba_sizes[4];79} dri2_pbuffer_visuals[] = {80{81"ABGR16F",82__DRI_IMAGE_FORMAT_ABGR16161616F,83{ 0, 16, 32, 48 },84{ 16, 16, 16, 16 }85},86{87"XBGR16F",88__DRI_IMAGE_FORMAT_XBGR16161616F,89{ 0, 16, 32, -1 },90{ 16, 16, 16, 0 }91},92{93"A2RGB10",94__DRI_IMAGE_FORMAT_ARGB2101010,95{ 20, 10, 0, 30 },96{ 10, 10, 10, 2 }97},98{99"X2RGB10",100__DRI_IMAGE_FORMAT_XRGB2101010,101{ 20, 10, 0, -1 },102{ 10, 10, 10, 0 }103},104{105"ARGB8888",106__DRI_IMAGE_FORMAT_ARGB8888,107{ 16, 8, 0, 24 },108{ 8, 8, 8, 8 }109},110{111"RGB888",112__DRI_IMAGE_FORMAT_XRGB8888,113{ 16, 8, 0, -1 },114{ 8, 8, 8, 0 }115},116{117"RGB565",118__DRI_IMAGE_FORMAT_RGB565,119{ 11, 5, 0, -1 },120{ 5, 6, 5, 0 }121},122};123124static void125dri_set_background_context(void *loaderPrivate)126{127_EGLContext *ctx = _eglGetCurrentContext();128_EGLThreadInfo *t = _eglGetCurrentThread();129130_eglBindContextToThread(ctx, t);131}132133static void134dri2_gl_flush()135{136static void (*glFlush)(void);137static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;138139mtx_lock(&glFlushMutex);140if (!glFlush)141glFlush = _glapi_get_proc_address("glFlush");142mtx_unlock(&glFlushMutex);143144/* if glFlush is not available things are horribly broken */145if (!glFlush) {146_eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");147return;148}149150glFlush();151}152153static GLboolean154dri_is_thread_safe(void *loaderPrivate)155{156struct dri2_egl_surface *dri2_surf = loaderPrivate;157UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;158159#ifdef HAVE_X11_PLATFORM160Display *xdpy = (Display*)display->PlatformDisplay;161162/* Check Xlib is running in thread safe mode when running on EGL/X11-xlib163* platform164*165* 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.166* It wll be NULL if XInitThreads wasn't called.167*/168if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)169return false;170#endif171172return true;173}174175const __DRIbackgroundCallableExtension background_callable_extension = {176.base = { __DRI_BACKGROUND_CALLABLE, 2 },177178.setBackgroundContext = dri_set_background_context,179.isThreadSafe = dri_is_thread_safe,180};181182const __DRIuseInvalidateExtension use_invalidate = {183.base = { __DRI_USE_INVALIDATE, 1 }184};185186static void187dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,188int *x, int *y, int *w, int *h,189void *loaderPrivate)190{191struct dri2_egl_surface *dri2_surf = loaderPrivate;192193*x = *y = 0;194*w = dri2_surf->base.Width;195*h = dri2_surf->base.Height;196}197198static int199dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)200{201const int depth = dri2_surf->base.Config->BufferSize;202return depth ? util_next_power_of_two(depth / 8) : 0;203}204205static void206dri2_put_image(__DRIdrawable * draw, int op,207int x, int y, int w, int h,208char *data, void *loaderPrivate)209{210struct dri2_egl_surface *dri2_surf = loaderPrivate;211const int bpp = dri2_get_bytes_per_pixel(dri2_surf);212const int width = dri2_surf->base.Width;213const int height = dri2_surf->base.Height;214const int dst_stride = width*bpp;215const int src_stride = w*bpp;216const int x_offset = x*bpp;217int copy_width = src_stride;218219if (!dri2_surf->swrast_device_buffer)220dri2_surf->swrast_device_buffer = malloc(height*dst_stride);221222if (dri2_surf->swrast_device_buffer) {223const char *src = data;224char *dst = dri2_surf->swrast_device_buffer;225226dst += x_offset;227dst += y*dst_stride;228229/* Drivers are allowed to submit OOB PutImage requests, so clip here. */230if (copy_width > dst_stride - x_offset)231copy_width = dst_stride - x_offset;232if (h > height - y)233h = height - y;234235for (; 0 < h; --h) {236memcpy(dst, src, copy_width);237dst += dst_stride;238src += src_stride;239}240}241}242243static void244dri2_get_image(__DRIdrawable * read,245int x, int y, int w, int h,246char *data, void *loaderPrivate)247{248struct dri2_egl_surface *dri2_surf = loaderPrivate;249const int bpp = dri2_get_bytes_per_pixel(dri2_surf);250const int width = dri2_surf->base.Width;251const int height = dri2_surf->base.Height;252const int src_stride = width*bpp;253const int dst_stride = w*bpp;254const int x_offset = x*bpp;255int copy_width = dst_stride;256const char *src = dri2_surf->swrast_device_buffer;257char *dst = data;258259if (!src) {260memset(data, 0, copy_width * h);261return;262}263264src += x_offset;265src += y*src_stride;266267/* Drivers are allowed to submit OOB GetImage requests, so clip here. */268if (copy_width > src_stride - x_offset)269copy_width = src_stride - x_offset;270if (h > height - y)271h = height - y;272273for (; 0 < h; --h) {274memcpy(dst, src, copy_width);275src += src_stride;276dst += dst_stride;277}278279}280281/* HACK: technically we should have swrast_null, instead of these.282*/283const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {284.base = { __DRI_SWRAST_LOADER, 1 },285.getDrawableInfo = dri2_get_pbuffer_drawable_info,286.putImage = dri2_put_image,287.getImage = dri2_get_image,288};289290static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {291[__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,292[__DRI_ATTRIB_LEVEL] = EGL_LEVEL,293[__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,294[__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,295[__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,296[__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,297[__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,298[__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,299[__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,300[__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,301[__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,302[__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,303[__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,304};305306const __DRIconfig *307dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,308EGLenum colorspace)309{310const bool double_buffer = surface_type == EGL_WINDOW_BIT;311const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;312313return conf->dri_config[double_buffer][srgb];314}315316static EGLBoolean317dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)318{319if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)320return EGL_FALSE;321322if (!_eglMatchConfig(conf, criteria))323return EGL_FALSE;324325return EGL_TRUE;326}327328void329dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,330const __DRIconfig *config, int *shifts,331unsigned int *sizes)332{333unsigned int mask;334335if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {336core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);337core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);338core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);339} else {340/* Driver isn't exposing shifts, so convert masks to shifts */341core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);342shifts[0] = ffs(mask) - 1;343core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);344shifts[1] = ffs(mask) - 1;345core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);346shifts[2] = ffs(mask) - 1;347core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);348shifts[3] = ffs(mask) - 1;349}350351core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);352core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);353core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);354core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);355}356357void358dri2_get_render_type_float(const __DRIcoreExtension *core,359const __DRIconfig *config,360bool *is_float)361{362unsigned int render_type;363364core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);365*is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;366}367368unsigned int369dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,370const __DRIconfig *config)371{372int shifts[4];373unsigned int sizes[4];374375dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);376377for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {378const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];379380if (shifts[0] == visual->rgba_shifts[0] &&381shifts[1] == visual->rgba_shifts[1] &&382shifts[2] == visual->rgba_shifts[2] &&383shifts[3] == visual->rgba_shifts[3] &&384sizes[0] == visual->rgba_sizes[0] &&385sizes[1] == visual->rgba_sizes[1] &&386sizes[2] == visual->rgba_sizes[2] &&387sizes[3] == visual->rgba_sizes[3]) {388return visual->dri_image_format;389}390}391392return __DRI_IMAGE_FORMAT_NONE;393}394395struct dri2_egl_config *396dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,397EGLint surface_type, const EGLint *attr_list,398const int *rgba_shifts, const unsigned int *rgba_sizes)399{400struct dri2_egl_config *conf;401struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);402_EGLConfig base;403unsigned int attrib, value, double_buffer;404bool srgb = false;405EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;406int dri_shifts[4] = { -1, -1, -1, -1 };407unsigned int dri_sizes[4] = { 0, 0, 0, 0 };408_EGLConfig *matching_config;409EGLint num_configs = 0;410EGLint config_id;411412_eglInitConfig(&base, disp, id);413414double_buffer = 0;415bind_to_texture_rgb = 0;416bind_to_texture_rgba = 0;417418for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {419if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))420break;421422switch (attrib) {423case __DRI_ATTRIB_RENDER_TYPE:424if (value & __DRI_ATTRIB_FLOAT_BIT)425base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;426if (value & __DRI_ATTRIB_RGBA_BIT)427value = EGL_RGB_BUFFER;428else if (value & __DRI_ATTRIB_LUMINANCE_BIT)429value = EGL_LUMINANCE_BUFFER;430else431return NULL;432base.ColorBufferType = value;433break;434435case __DRI_ATTRIB_CONFIG_CAVEAT:436if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)437value = EGL_NON_CONFORMANT_CONFIG;438else if (value & __DRI_ATTRIB_SLOW_BIT)439value = EGL_SLOW_CONFIG;440else441value = EGL_NONE;442base.ConfigCaveat = value;443break;444445case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:446bind_to_texture_rgb = value;447break;448449case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:450bind_to_texture_rgba = value;451break;452453case __DRI_ATTRIB_DOUBLE_BUFFER:454double_buffer = value;455break;456457case __DRI_ATTRIB_RED_SIZE:458dri_sizes[0] = value;459base.RedSize = value;460break;461462case __DRI_ATTRIB_RED_MASK:463dri_shifts[0] = ffs(value) - 1;464break;465466case __DRI_ATTRIB_RED_SHIFT:467dri_shifts[0] = value;468break;469470case __DRI_ATTRIB_GREEN_SIZE:471dri_sizes[1] = value;472base.GreenSize = value;473break;474475case __DRI_ATTRIB_GREEN_MASK:476dri_shifts[1] = ffs(value) - 1;477break;478479case __DRI_ATTRIB_GREEN_SHIFT:480dri_shifts[1] = value;481break;482483case __DRI_ATTRIB_BLUE_SIZE:484dri_sizes[2] = value;485base.BlueSize = value;486break;487488case __DRI_ATTRIB_BLUE_MASK:489dri_shifts[2] = ffs(value) - 1;490break;491492case __DRI_ATTRIB_BLUE_SHIFT:493dri_shifts[2] = value;494break;495496case __DRI_ATTRIB_ALPHA_SIZE:497dri_sizes[3] = value;498base.AlphaSize = value;499break;500501case __DRI_ATTRIB_ALPHA_MASK:502dri_shifts[3] = ffs(value) - 1;503break;504505case __DRI_ATTRIB_ALPHA_SHIFT:506dri_shifts[3] = value;507break;508509case __DRI_ATTRIB_ACCUM_RED_SIZE:510case __DRI_ATTRIB_ACCUM_GREEN_SIZE:511case __DRI_ATTRIB_ACCUM_BLUE_SIZE:512case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:513/* Don't expose visuals with the accumulation buffer. */514if (value > 0)515return NULL;516break;517518case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:519srgb = value != 0;520if (!disp->Extensions.KHR_gl_colorspace && srgb)521return NULL;522break;523524case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:525base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;526break;527case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:528base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;529break;530case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:531if (disp->Extensions.KHR_mutable_render_buffer)532surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;533break;534default:535key = dri2_to_egl_attribute_map[attrib];536if (key != 0)537_eglSetConfigKey(&base, key, value);538break;539}540}541542if (attr_list)543for (int i = 0; attr_list[i] != EGL_NONE; i += 2)544_eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);545546if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))547return NULL;548549if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))550return NULL;551552base.NativeRenderable = EGL_TRUE;553554base.SurfaceType = surface_type;555if (surface_type & (EGL_PBUFFER_BIT |556(disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {557base.BindToTextureRGB = bind_to_texture_rgb;558if (base.AlphaSize > 0)559base.BindToTextureRGBA = bind_to_texture_rgba;560}561562if (double_buffer) {563surface_type &= ~EGL_PIXMAP_BIT;564}565566/* No support for pbuffer + MSAA for now.567*568* XXX TODO: pbuffer + MSAA does not work and causes crashes.569* See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509570*/571if (base.Samples) {572surface_type &= ~EGL_PBUFFER_BIT;573}574575if (!surface_type)576return NULL;577578base.RenderableType = disp->ClientAPIs;579base.Conformant = disp->ClientAPIs;580581base.MinSwapInterval = dri2_dpy->min_swap_interval;582base.MaxSwapInterval = dri2_dpy->max_swap_interval;583584if (!_eglValidateConfig(&base, EGL_FALSE)) {585_eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);586return NULL;587}588589config_id = base.ConfigID;590base.ConfigID = EGL_DONT_CARE;591base.SurfaceType = EGL_DONT_CARE;592num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,593(_EGLArrayForEach) dri2_match_config, &base);594595if (num_configs == 1) {596conf = (struct dri2_egl_config *) matching_config;597598if (!conf->dri_config[double_buffer][srgb])599conf->dri_config[double_buffer][srgb] = dri_config;600else601/* a similar config type is already added (unlikely) => discard */602return NULL;603}604else if (num_configs == 0) {605conf = calloc(1, sizeof *conf);606if (conf == NULL)607return NULL;608609conf->dri_config[double_buffer][srgb] = dri_config;610611memcpy(&conf->base, &base, sizeof base);612conf->base.SurfaceType = 0;613conf->base.ConfigID = config_id;614615_eglLinkConfig(&conf->base);616}617else {618unreachable("duplicates should not be possible");619return NULL;620}621622conf->base.SurfaceType |= surface_type;623624return conf;625}626627EGLBoolean628dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)629{630struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);631unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };632unsigned int config_count = 0;633634for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {635for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {636struct dri2_egl_config *dri2_conf;637638dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],639config_count + 1, EGL_PBUFFER_BIT, NULL,640dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);641642if (dri2_conf) {643if (dri2_conf->base.ConfigID == config_count + 1)644config_count++;645format_count[j]++;646}647}648}649650for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {651if (!format_count[i]) {652_eglLog(_EGL_DEBUG, "No DRI config supports native format %s",653dri2_pbuffer_visuals[i].format_name);654}655}656657return (config_count != 0);658}659660__DRIimage *661dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)662{663_EGLDisplay *disp = data;664struct dri2_egl_image *dri2_img;665_EGLImage *img;666667(void) screen;668669mtx_lock(&disp->Mutex);670img = _eglLookupImage(image, disp);671mtx_unlock(&disp->Mutex);672673if (img == NULL) {674_eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");675return NULL;676}677678dri2_img = dri2_egl_image(image);679680return dri2_img->dri_image;681}682683const __DRIimageLookupExtension image_lookup_extension = {684.base = { __DRI_IMAGE_LOOKUP, 1 },685686.lookupEGLImage = dri2_lookup_egl_image687};688689struct dri2_extension_match {690const char *name;691int version;692int offset;693};694695static const struct dri2_extension_match dri3_driver_extensions[] = {696{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },697{ __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },698{ NULL, 0, 0 }699};700701static const struct dri2_extension_match dri2_driver_extensions[] = {702{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },703{ __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },704{ NULL, 0, 0 }705};706707static const struct dri2_extension_match dri2_core_extensions[] = {708{ __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },709{ __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },710{ __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },711{ NULL, 0, 0 }712};713714static const struct dri2_extension_match swrast_driver_extensions[] = {715{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },716{ __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },717{ NULL, 0, 0 }718};719720static const struct dri2_extension_match swrast_core_extensions[] = {721{ __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },722{ NULL, 0, 0 }723};724725static const struct dri2_extension_match optional_driver_extensions[] = {726{ __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },727{ NULL, 0, 0 }728};729730static const struct dri2_extension_match optional_core_extensions[] = {731{ __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },732{ __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },733{ __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },734{ __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },735{ __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },736{ __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },737{ __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },738{ __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },739{ __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },740{ __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },741{ __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },742{ NULL, 0, 0 }743};744745static EGLBoolean746dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,747const struct dri2_extension_match *matches,748const __DRIextension **extensions,749bool optional)750{751int ret = EGL_TRUE;752void *field;753754for (int i = 0; extensions[i]; i++) {755_eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);756for (int j = 0; matches[j].name; j++) {757if (strcmp(extensions[i]->name, matches[j].name) == 0 &&758extensions[i]->version >= matches[j].version) {759field = ((char *) dri2_dpy + matches[j].offset);760*(const __DRIextension **) field = extensions[i];761_eglLog(_EGL_INFO, "found extension %s version %d",762extensions[i]->name, extensions[i]->version);763break;764}765}766}767768for (int j = 0; matches[j].name; j++) {769field = ((char *) dri2_dpy + matches[j].offset);770if (*(const __DRIextension **) field == NULL) {771if (optional) {772_eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",773matches[j].name, matches[j].version);774} else {775_eglLog(_EGL_WARNING, "did not find extension %s version %d",776matches[j].name, matches[j].version);777ret = EGL_FALSE;778}779}780}781782return ret;783}784785static const __DRIextension **786dri2_open_driver(_EGLDisplay *disp)787{788struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);789static const char *search_path_vars[] = {790"LIBGL_DRIVERS_PATH",791NULL,792};793794return loader_open_driver(dri2_dpy->driver_name,795&dri2_dpy->driver,796search_path_vars);797}798799static EGLBoolean800dri2_load_driver_common(_EGLDisplay *disp,801const struct dri2_extension_match *driver_extensions)802{803struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);804const __DRIextension **extensions;805806extensions = dri2_open_driver(disp);807if (!extensions)808return EGL_FALSE;809810if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {811dlclose(dri2_dpy->driver);812dri2_dpy->driver = NULL;813return EGL_FALSE;814}815dri2_dpy->driver_extensions = extensions;816817dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);818819return EGL_TRUE;820}821822EGLBoolean823dri2_load_driver(_EGLDisplay *disp)824{825return dri2_load_driver_common(disp, dri2_driver_extensions);826}827828EGLBoolean829dri2_load_driver_dri3(_EGLDisplay *disp)830{831return dri2_load_driver_common(disp, dri3_driver_extensions);832}833834EGLBoolean835dri2_load_driver_swrast(_EGLDisplay *disp)836{837return dri2_load_driver_common(disp, swrast_driver_extensions);838}839840static unsigned841dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)842{843const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;844unsigned int value = 0;845846if (!rendererQuery ||847rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)848return 0;849850return value;851}852853static const char *854dri2_query_driver_name(_EGLDisplay *disp)855{856struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);857return dri2_dpy->driver_name;858}859860static char *861dri2_query_driver_config(_EGLDisplay *disp)862{863struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);864const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;865866if (ext->base.version >= 2)867return ext->getXml(dri2_dpy->driver_name);868869return strdup(ext->xml);870}871872873void874dri2_setup_screen(_EGLDisplay *disp)875{876struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);877unsigned int api_mask;878879/*880* EGL 1.5 specification defines the default value to 1. Moreover,881* eglSwapInterval() is required to clamp requested value to the supported882* range. Since the default value is implicitly assumed to be supported,883* use it as both minimum and maximum for the platforms that do not allow884* changing the interval. Platforms, which allow it (e.g. x11, wayland)885* override these values already.886*/887dri2_dpy->min_swap_interval = 1;888dri2_dpy->max_swap_interval = 1;889dri2_dpy->default_swap_interval = 1;890891if (dri2_dpy->image_driver) {892api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);893} else if (dri2_dpy->dri2) {894api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);895} else {896assert(dri2_dpy->swrast);897api_mask = 1 << __DRI_API_OPENGL |8981 << __DRI_API_GLES |8991 << __DRI_API_GLES2 |9001 << __DRI_API_GLES3;901}902903disp->ClientAPIs = 0;904if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))905disp->ClientAPIs |= EGL_OPENGL_BIT;906if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))907disp->ClientAPIs |= EGL_OPENGL_ES_BIT;908if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))909disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;910if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))911disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;912913assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);914disp->Extensions.KHR_no_config_context = EGL_TRUE;915disp->Extensions.KHR_surfaceless_context = EGL_TRUE;916917if (dri2_dpy->configOptions) {918disp->Extensions.MESA_query_driver = EGL_TRUE;919}920921/* Report back to EGL the bitmask of priorities supported */922disp->Extensions.IMG_context_priority =923dri2_renderer_query_integer(dri2_dpy,924__DRI2_RENDERER_HAS_CONTEXT_PRIORITY);925926disp->Extensions.EXT_pixel_format_float = EGL_TRUE;927928if (dri2_renderer_query_integer(dri2_dpy,929__DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))930disp->Extensions.KHR_gl_colorspace = EGL_TRUE;931932if (dri2_dpy->image_driver ||933(dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||934(dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {935disp->Extensions.KHR_create_context = EGL_TRUE;936937if (dri2_dpy->robustness)938disp->Extensions.EXT_create_context_robustness = EGL_TRUE;939}940941if (dri2_dpy->no_error)942disp->Extensions.KHR_create_context_no_error = EGL_TRUE;943944if (dri2_dpy->fence) {945disp->Extensions.KHR_fence_sync = EGL_TRUE;946disp->Extensions.KHR_wait_sync = EGL_TRUE;947if (dri2_dpy->fence->get_fence_from_cl_event)948disp->Extensions.KHR_cl_event2 = EGL_TRUE;949if (dri2_dpy->fence->base.version >= 2 &&950dri2_dpy->fence->get_capabilities) {951unsigned capabilities =952dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);953disp->Extensions.ANDROID_native_fence_sync =954(capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;955}956}957958if (dri2_dpy->blob)959disp->Extensions.ANDROID_blob_cache = EGL_TRUE;960961disp->Extensions.KHR_reusable_sync = EGL_TRUE;962963if (dri2_dpy->image) {964if (dri2_dpy->image->base.version >= 10 &&965dri2_dpy->image->getCapabilities != NULL) {966int capabilities;967968capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);969disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;970971if (dri2_dpy->image->base.version >= 11)972disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;973} else {974disp->Extensions.MESA_drm_image = EGL_TRUE;975if (dri2_dpy->image->base.version >= 11)976disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;977}978979disp->Extensions.KHR_image_base = EGL_TRUE;980disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;981if (dri2_dpy->image->base.version >= 5 &&982dri2_dpy->image->createImageFromTexture) {983disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;984disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;985986if (dri2_renderer_query_integer(dri2_dpy,987__DRI2_RENDERER_HAS_TEXTURE_3D))988disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;989}990#ifdef HAVE_LIBDRM991if (dri2_dpy->image->base.version >= 8 &&992dri2_dpy->image->createImageFromDmaBufs) {993disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;994disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;995}996#endif997}998999if (dri2_dpy->flush_control)1000disp->Extensions.KHR_context_flush_control = EGL_TRUE;10011002if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)1003disp->Extensions.KHR_partial_update = EGL_TRUE;10041005disp->Extensions.EXT_protected_surface =1006dri2_renderer_query_integer(dri2_dpy,1007__DRI2_RENDERER_HAS_PROTECTED_CONTENT);1008}10091010void1011dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)1012{1013struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1014GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;10151016/* Allow driconf to override applications.*/1017if (dri2_dpy->config)1018dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,1019"vblank_mode", &vblank_mode);1020switch (vblank_mode) {1021case DRI_CONF_VBLANK_NEVER:1022dri2_dpy->min_swap_interval = 0;1023dri2_dpy->max_swap_interval = 0;1024dri2_dpy->default_swap_interval = 0;1025break;1026case DRI_CONF_VBLANK_ALWAYS_SYNC:1027dri2_dpy->min_swap_interval = 1;1028dri2_dpy->max_swap_interval = max_swap_interval;1029dri2_dpy->default_swap_interval = 1;1030break;1031case DRI_CONF_VBLANK_DEF_INTERVAL_0:1032dri2_dpy->min_swap_interval = 0;1033dri2_dpy->max_swap_interval = max_swap_interval;1034dri2_dpy->default_swap_interval = 0;1035break;1036default:1037case DRI_CONF_VBLANK_DEF_INTERVAL_1:1038dri2_dpy->min_swap_interval = 0;1039dri2_dpy->max_swap_interval = max_swap_interval;1040dri2_dpy->default_swap_interval = 1;1041break;1042}1043}10441045/* All platforms but DRM call this function to create the screen and populate1046* the driver_configs. DRM inherits that information from its display - GBM.1047*/1048EGLBoolean1049dri2_create_screen(_EGLDisplay *disp)1050{1051struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);10521053if (dri2_dpy->image_driver) {1054dri2_dpy->dri_screen =1055dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,1056dri2_dpy->loader_extensions,1057dri2_dpy->driver_extensions,1058&dri2_dpy->driver_configs,1059disp);1060} else if (dri2_dpy->dri2) {1061if (dri2_dpy->dri2->base.version >= 4) {1062dri2_dpy->dri_screen =1063dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,1064dri2_dpy->loader_extensions,1065dri2_dpy->driver_extensions,1066&dri2_dpy->driver_configs, disp);1067} else {1068dri2_dpy->dri_screen =1069dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,1070dri2_dpy->loader_extensions,1071&dri2_dpy->driver_configs, disp);1072}1073} else {1074assert(dri2_dpy->swrast);1075if (dri2_dpy->swrast->base.version >= 4) {1076dri2_dpy->dri_screen =1077dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,1078dri2_dpy->driver_extensions,1079&dri2_dpy->driver_configs, disp);1080} else {1081dri2_dpy->dri_screen =1082dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,1083&dri2_dpy->driver_configs, disp);1084}1085}10861087if (dri2_dpy->dri_screen == NULL) {1088_eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");1089return EGL_FALSE;1090}10911092dri2_dpy->own_dri_screen = true;1093return EGL_TRUE;1094}10951096EGLBoolean1097dri2_setup_extensions(_EGLDisplay *disp)1098{1099struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1100const struct dri2_extension_match *mandatory_core_extensions;1101const __DRIextension **extensions;11021103extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);11041105if (dri2_dpy->image_driver || dri2_dpy->dri2)1106mandatory_core_extensions = dri2_core_extensions;1107else1108mandatory_core_extensions = swrast_core_extensions;11091110if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))1111return EGL_FALSE;11121113#ifdef HAVE_DRI3_MODIFIERS1114dri2_dpy->multibuffers_available =1115(dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&1116dri2_dpy->dri3_minor_version >= 2)) &&1117(dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&1118dri2_dpy->present_minor_version >= 2)) &&1119(dri2_dpy->image && dri2_dpy->image->base.version >= 15);1120#endif11211122dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);1123return EGL_TRUE;1124}11251126/**1127* Called via eglInitialize(), drv->Initialize().1128*1129* This must be guaranteed to be called exactly once, even if eglInitialize is1130* called many times (without a eglTerminate in between).1131*/1132static EGLBoolean1133dri2_initialize(_EGLDisplay *disp)1134{1135EGLBoolean ret = EGL_FALSE;1136struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);11371138/* In the case where the application calls eglMakeCurrent(context1),1139* eglTerminate, then eglInitialize again (without a call to eglReleaseThread1140* or eglMakeCurrent(NULL) before that), dri2_dpy structure is still1141* initialized, as we need it to be able to free context1 correctly.1142*1143* It would probably be safest to forcibly release the display with1144* dri2_display_release, to make sure the display is reinitialized correctly.1145* However, the EGL spec states that we need to keep a reference to the1146* current context (so we cannot call dri2_make_current(NULL)), and therefore1147* we would leak context1 as we would be missing the old display connection1148* to free it up correctly.1149*/1150if (dri2_dpy) {1151dri2_dpy->ref_count++;1152return EGL_TRUE;1153}11541155loader_set_logger(_eglLog);11561157switch (disp->Platform) {1158case _EGL_PLATFORM_SURFACELESS:1159ret = dri2_initialize_surfaceless(disp);1160break;1161case _EGL_PLATFORM_DEVICE:1162ret = dri2_initialize_device(disp);1163break;1164case _EGL_PLATFORM_X11:1165case _EGL_PLATFORM_XCB:1166ret = dri2_initialize_x11(disp);1167break;1168case _EGL_PLATFORM_DRM:1169ret = dri2_initialize_drm(disp);1170break;1171case _EGL_PLATFORM_WAYLAND:1172ret = dri2_initialize_wayland(disp);1173break;1174case _EGL_PLATFORM_ANDROID:1175ret = dri2_initialize_android(disp);1176break;1177default:1178unreachable("Callers ensure we cannot get here.");1179return EGL_FALSE;1180}11811182if (!ret)1183return EGL_FALSE;11841185dri2_dpy = dri2_egl_display(disp);1186dri2_dpy->ref_count++;11871188return EGL_TRUE;1189}11901191/**1192* Decrement display reference count, and free up display if necessary.1193*/1194static void1195dri2_display_release(_EGLDisplay *disp)1196{1197struct dri2_egl_display *dri2_dpy;11981199if (!disp)1200return;12011202dri2_dpy = dri2_egl_display(disp);12031204assert(dri2_dpy->ref_count > 0);1205dri2_dpy->ref_count--;12061207if (dri2_dpy->ref_count > 0)1208return;12091210_eglCleanupDisplay(disp);1211dri2_display_destroy(disp);1212}12131214void1215dri2_display_destroy(_EGLDisplay *disp)1216{1217struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);12181219if (dri2_dpy->own_dri_screen) {1220if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)1221dri2_dpy->vtbl->close_screen_notify(disp);1222dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);1223}1224if (dri2_dpy->fd >= 0)1225close(dri2_dpy->fd);12261227/* Don't dlclose the driver when building with the address sanitizer, so you1228* get good symbols from the leak reports.1229*/1230#if !BUILT_WITH_ASAN || defined(NDEBUG)1231if (dri2_dpy->driver)1232dlclose(dri2_dpy->driver);1233#endif12341235free(dri2_dpy->driver_name);12361237#ifdef HAVE_WAYLAND_PLATFORM1238free(dri2_dpy->device_name);1239#endif12401241switch (disp->Platform) {1242case _EGL_PLATFORM_X11:1243dri2_teardown_x11(dri2_dpy);1244break;1245case _EGL_PLATFORM_DRM:1246dri2_teardown_drm(dri2_dpy);1247break;1248case _EGL_PLATFORM_WAYLAND:1249dri2_teardown_wayland(dri2_dpy);1250break;1251default:1252/* TODO: add teardown for other platforms */1253break;1254}12551256/* The drm platform does not create the screen/driver_configs but reuses1257* the ones from the gbm device. As such the gbm itself is responsible1258* for the cleanup.1259*/1260if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {1261for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)1262free((__DRIconfig *) dri2_dpy->driver_configs[i]);1263free(dri2_dpy->driver_configs);1264}1265free(dri2_dpy);1266disp->DriverData = NULL;1267}12681269__DRIbuffer *1270dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,1271unsigned int att, unsigned int format)1272{1273struct dri2_egl_display *dri2_dpy =1274dri2_egl_display(dri2_surf->base.Resource.Display);12751276if (att >= ARRAY_SIZE(dri2_surf->local_buffers))1277return NULL;12781279if (!dri2_surf->local_buffers[att]) {1280dri2_surf->local_buffers[att] =1281dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,1282dri2_surf->base.Width, dri2_surf->base.Height);1283}12841285return dri2_surf->local_buffers[att];1286}12871288void1289dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)1290{1291struct dri2_egl_display *dri2_dpy =1292dri2_egl_display(dri2_surf->base.Resource.Display);12931294for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {1295if (dri2_surf->local_buffers[i]) {1296dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,1297dri2_surf->local_buffers[i]);1298dri2_surf->local_buffers[i] = NULL;1299}1300}1301}13021303/**1304* Called via eglTerminate(), drv->Terminate().1305*1306* This must be guaranteed to be called exactly once, even if eglTerminate is1307* called many times (without a eglInitialize in between).1308*/1309static EGLBoolean1310dri2_terminate(_EGLDisplay *disp)1311{1312/* Release all non-current Context/Surfaces. */1313_eglReleaseDisplayResources(disp);13141315dri2_display_release(disp);13161317return EGL_TRUE;1318}13191320/**1321* Set the error code after a call to1322* dri2_egl_display::dri2::createContextAttribs.1323*/1324static void1325dri2_create_context_attribs_error(int dri_error)1326{1327EGLint egl_error;13281329switch (dri_error) {1330case __DRI_CTX_ERROR_SUCCESS:1331return;13321333case __DRI_CTX_ERROR_NO_MEMORY:1334egl_error = EGL_BAD_ALLOC;1335break;13361337/* From the EGL_KHR_create_context spec, section "Errors":1338*1339* * If <config> does not support a client API context compatible1340* with the requested API major and minor version, [...] context flags,1341* and context reset notification behavior (for client API types where1342* these attributes are supported), then an EGL_BAD_MATCH error is1343* generated.1344*1345* * If an OpenGL ES context is requested and the values for1346* attributes EGL_CONTEXT_MAJOR_VERSION_KHR and1347* EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that1348* is not defined, than an EGL_BAD_MATCH error is generated.1349*1350* * If an OpenGL context is requested, the requested version is1351* greater than 3.2, and the value for attribute1352* EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any1353* bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and1354* EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than1355* one of these bits set; or if the implementation does not support1356* the requested profile, then an EGL_BAD_MATCH error is generated.1357*/1358case __DRI_CTX_ERROR_BAD_API:1359case __DRI_CTX_ERROR_BAD_VERSION:1360case __DRI_CTX_ERROR_BAD_FLAG:1361egl_error = EGL_BAD_MATCH;1362break;13631364/* From the EGL_KHR_create_context spec, section "Errors":1365*1366* * If an attribute name or attribute value in <attrib_list> is not1367* recognized (including unrecognized bits in bitmask attributes),1368* then an EGL_BAD_ATTRIBUTE error is generated."1369*/1370case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:1371case __DRI_CTX_ERROR_UNKNOWN_FLAG:1372egl_error = EGL_BAD_ATTRIBUTE;1373break;13741375default:1376assert(!"unknown dri_error code");1377egl_error = EGL_BAD_MATCH;1378break;1379}13801381_eglError(egl_error, "dri2_create_context");1382}13831384static bool1385dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,1386struct dri2_egl_display *dri2_dpy,1387uint32_t *ctx_attribs,1388unsigned *num_attribs)1389{1390int pos = 0;13911392assert(*num_attribs >= NUM_ATTRIBS);13931394ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;1395ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;1396ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;1397ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;13981399if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {1400/* If the implementation doesn't support the __DRI2_ROBUSTNESS1401* extension, don't even try to send it the robust-access flag.1402* It may explode. Instead, generate the required EGL error here.1403*/1404if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 01405&& !dri2_dpy->robustness) {1406_eglError(EGL_BAD_MATCH, "eglCreateContext");1407return false;1408}14091410ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;1411ctx_attribs[pos++] = dri2_ctx->base.Flags |1412(dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);1413}14141415if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {1416/* If the implementation doesn't support the __DRI2_ROBUSTNESS1417* extension, don't even try to send it a reset strategy. It may1418* explode. Instead, generate the required EGL error here.1419*/1420if (!dri2_dpy->robustness) {1421_eglError(EGL_BAD_CONFIG, "eglCreateContext");1422return false;1423}14241425ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;1426ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;1427}14281429if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {1430unsigned val;14311432switch (dri2_ctx->base.ContextPriority) {1433case EGL_CONTEXT_PRIORITY_HIGH_IMG:1434val = __DRI_CTX_PRIORITY_HIGH;1435break;1436case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:1437val = __DRI_CTX_PRIORITY_MEDIUM;1438break;1439case EGL_CONTEXT_PRIORITY_LOW_IMG:1440val = __DRI_CTX_PRIORITY_LOW;1441break;1442default:1443_eglError(EGL_BAD_CONFIG, "eglCreateContext");1444return false;1445}14461447ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;1448ctx_attribs[pos++] = val;1449}14501451if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {1452ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;1453ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;1454}14551456*num_attribs = pos;14571458return true;1459}14601461/**1462* Called via eglCreateContext(), drv->CreateContext().1463*/1464static _EGLContext *1465dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,1466_EGLContext *share_list, const EGLint *attrib_list)1467{1468struct dri2_egl_context *dri2_ctx;1469struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1470struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);1471__DRIcontext *shared =1472dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;1473struct dri2_egl_config *dri2_config = dri2_egl_config(conf);1474const __DRIconfig *dri_config;1475int api;1476unsigned error;1477unsigned num_attribs = NUM_ATTRIBS;1478uint32_t ctx_attribs[NUM_ATTRIBS];14791480dri2_ctx = malloc(sizeof *dri2_ctx);1481if (!dri2_ctx) {1482_eglError(EGL_BAD_ALLOC, "eglCreateContext");1483return NULL;1484}14851486if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))1487goto cleanup;14881489/* The EGL_EXT_create_context_robustness spec says:1490*1491* "Add to the eglCreateContext context creation errors: [...]1492*1493* * If the reset notification behavior of <share_context> and the1494* newly created context are different then an EGL_BAD_MATCH error is1495* generated."1496*/1497if (share_list && share_list->ResetNotificationStrategy !=1498dri2_ctx->base.ResetNotificationStrategy) {1499_eglError(EGL_BAD_MATCH, "eglCreateContext");1500goto cleanup;1501}15021503/* The EGL_KHR_create_context_no_error spec says:1504*1505* "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR1506* used to create <share_context> does not match the value of1507* EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."1508*/1509if (share_list && share_list->NoError != dri2_ctx->base.NoError) {1510_eglError(EGL_BAD_MATCH, "eglCreateContext");1511goto cleanup;1512}15131514switch (dri2_ctx->base.ClientAPI) {1515case EGL_OPENGL_ES_API:1516switch (dri2_ctx->base.ClientMajorVersion) {1517case 1:1518api = __DRI_API_GLES;1519break;1520case 2:1521api = __DRI_API_GLES2;1522break;1523case 3:1524api = __DRI_API_GLES3;1525break;1526default:1527_eglError(EGL_BAD_PARAMETER, "eglCreateContext");1528free(dri2_ctx);1529return NULL;1530}1531break;1532case EGL_OPENGL_API:1533if ((dri2_ctx->base.ClientMajorVersion >= 41534|| (dri2_ctx->base.ClientMajorVersion == 31535&& dri2_ctx->base.ClientMinorVersion >= 2))1536&& dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)1537api = __DRI_API_OPENGL_CORE;1538else if (dri2_ctx->base.ClientMajorVersion == 3 &&1539dri2_ctx->base.ClientMinorVersion == 1)1540api = __DRI_API_OPENGL_CORE;1541else1542api = __DRI_API_OPENGL;1543break;1544default:1545_eglError(EGL_BAD_PARAMETER, "eglCreateContext");1546free(dri2_ctx);1547return NULL;1548}15491550if (conf != NULL) {1551/* The config chosen here isn't necessarily1552* used for surfaces later.1553* A pixmap surface will use the single config.1554* This opportunity depends on disabling the1555* doubleBufferMode check in1556* src/mesa/main/context.c:check_compatible()1557*/1558if (dri2_config->dri_config[1][0])1559dri_config = dri2_config->dri_config[1][0];1560else1561dri_config = dri2_config->dri_config[0][0];1562}1563else1564dri_config = NULL;15651566if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,1567&num_attribs))1568goto cleanup;15691570if (dri2_dpy->image_driver) {1571dri2_ctx->dri_context =1572dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,1573api,1574dri_config,1575shared,1576num_attribs / 2,1577ctx_attribs,1578& error,1579dri2_ctx);1580dri2_create_context_attribs_error(error);1581} else if (dri2_dpy->dri2) {1582if (dri2_dpy->dri2->base.version >= 3) {1583dri2_ctx->dri_context =1584dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,1585api,1586dri_config,1587shared,1588num_attribs / 2,1589ctx_attribs,1590& error,1591dri2_ctx);1592dri2_create_context_attribs_error(error);1593} else {1594dri2_ctx->dri_context =1595dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,1596api,1597dri_config,1598shared,1599dri2_ctx);1600}1601} else {1602assert(dri2_dpy->swrast);1603if (dri2_dpy->swrast->base.version >= 3) {1604dri2_ctx->dri_context =1605dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,1606api,1607dri_config,1608shared,1609num_attribs / 2,1610ctx_attribs,1611& error,1612dri2_ctx);1613dri2_create_context_attribs_error(error);1614} else {1615dri2_ctx->dri_context =1616dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,1617api,1618dri_config,1619shared,1620dri2_ctx);1621}1622}16231624if (!dri2_ctx->dri_context)1625goto cleanup;16261627return &dri2_ctx->base;16281629cleanup:1630free(dri2_ctx);1631return NULL;1632}16331634/**1635* Called via eglDestroyContext(), drv->DestroyContext().1636*/1637static EGLBoolean1638dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)1639{1640struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);1641struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);16421643if (_eglPutContext(ctx)) {1644dri2_dpy->core->destroyContext(dri2_ctx->dri_context);1645free(dri2_ctx);1646}16471648return EGL_TRUE;1649}16501651EGLBoolean1652dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,1653_EGLConfig *conf, const EGLint *attrib_list,1654EGLBoolean enable_out_fence, void *native_surface)1655{1656struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);1657struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);16581659dri2_surf->out_fence_fd = -1;1660dri2_surf->enable_out_fence = false;1661if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&1662dri2_dpy->fence->get_capabilities &&1663(dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &1664__DRI_FENCE_CAP_NATIVE_FD)) {1665dri2_surf->enable_out_fence = enable_out_fence;1666}16671668return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);1669}16701671static void1672dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)1673{1674struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);16751676if (dri2_surf->out_fence_fd >= 0)1677close(dri2_surf->out_fence_fd);16781679dri2_surf->out_fence_fd = fence_fd;1680}16811682void1683dri2_fini_surface(_EGLSurface *surf)1684{1685struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);16861687dri2_surface_set_out_fence_fd(surf, -1);1688dri2_surf->enable_out_fence = false;1689}16901691static EGLBoolean1692dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)1693{1694struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);16951696if (!_eglPutSurface(surf))1697return EGL_TRUE;16981699return dri2_dpy->vtbl->destroy_surface(disp, surf);1700}17011702static void1703dri2_surf_update_fence_fd(_EGLContext *ctx,1704_EGLDisplay *disp, _EGLSurface *surf)1705{1706__DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;1707struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1708struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);1709int fence_fd = -1;1710void *fence;17111712if (!dri2_surf->enable_out_fence)1713return;17141715fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);1716if (fence) {1717fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,1718fence);1719dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);1720}1721dri2_surface_set_out_fence_fd(surf, fence_fd);1722}17231724EGLBoolean1725dri2_create_drawable(struct dri2_egl_display *dri2_dpy,1726const __DRIconfig *config,1727struct dri2_egl_surface *dri2_surf,1728void *loaderPrivate)1729{1730__DRIcreateNewDrawableFunc createNewDrawable;17311732if (dri2_dpy->image_driver)1733createNewDrawable = dri2_dpy->image_driver->createNewDrawable;1734else if (dri2_dpy->dri2)1735createNewDrawable = dri2_dpy->dri2->createNewDrawable;1736else if (dri2_dpy->swrast)1737createNewDrawable = dri2_dpy->swrast->createNewDrawable;1738else1739return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");17401741dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,1742config, loaderPrivate);1743if (dri2_surf->dri_drawable == NULL)1744return _eglError(EGL_BAD_ALLOC, "createNewDrawable");17451746return EGL_TRUE;1747}17481749/**1750* Called via eglMakeCurrent(), drv->MakeCurrent().1751*/1752static EGLBoolean1753dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,1754_EGLSurface *rsurf, _EGLContext *ctx)1755{1756struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1757struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);1758_EGLDisplay *old_disp = NULL;1759struct dri2_egl_display *old_dri2_dpy = NULL;1760_EGLContext *old_ctx;1761_EGLSurface *old_dsurf, *old_rsurf;1762_EGLSurface *tmp_dsurf, *tmp_rsurf;1763__DRIdrawable *ddraw, *rdraw;1764__DRIcontext *cctx;1765EGLint egl_error = EGL_SUCCESS;17661767if (!dri2_dpy)1768return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");17691770/* make new bindings, set the EGL error otherwise */1771if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))1772return EGL_FALSE;17731774if (old_ctx) {1775__DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;1776old_disp = old_ctx->Resource.Display;1777old_dri2_dpy = dri2_egl_display(old_disp);17781779/* flush before context switch */1780dri2_gl_flush();17811782if (old_dsurf)1783dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);17841785/* Disable shared buffer mode */1786if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&1787old_dri2_dpy->vtbl->set_shared_buffer_mode) {1788old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);1789}17901791dri2_dpy->core->unbindContext(old_cctx);1792}17931794ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;1795rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;1796cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;17971798if (cctx || ddraw || rdraw) {1799if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {1800_EGLContext *tmp_ctx;18011802/* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but1803* setting the error to EGL_BAD_MATCH is surely better than leaving it1804* as EGL_SUCCESS.1805*/1806egl_error = EGL_BAD_MATCH;18071808/* undo the previous _eglBindContext */1809_eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);1810assert(&dri2_ctx->base == ctx &&1811tmp_dsurf == dsurf &&1812tmp_rsurf == rsurf);18131814_eglPutSurface(dsurf);1815_eglPutSurface(rsurf);1816_eglPutContext(ctx);18171818_eglPutSurface(old_dsurf);1819_eglPutSurface(old_rsurf);1820_eglPutContext(old_ctx);18211822ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;1823rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;1824cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;18251826/* undo the previous dri2_dpy->core->unbindContext */1827if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {1828if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&1829old_dri2_dpy->vtbl->set_shared_buffer_mode) {1830old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);1831}18321833return _eglError(egl_error, "eglMakeCurrent");1834}18351836/* We cannot restore the same state as it was before calling1837* eglMakeCurrent() and the spec isn't clear about what to do. We1838* can prevent EGL from calling into the DRI driver with no DRI1839* context bound.1840*/1841dsurf = rsurf = NULL;1842ctx = NULL;18431844_eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);1845assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&1846tmp_rsurf == old_rsurf);18471848_eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");1849} else {1850/* dri2_dpy->core->bindContext succeeded, so take a reference on the1851* dri2_dpy. This prevents dri2_dpy from being reinitialized when a1852* EGLDisplay is terminated and then initialized again while a1853* context is still bound. See dri2_intitialize() for a more in depth1854* explanation. */1855dri2_dpy->ref_count++;1856}1857}18581859dri2_destroy_surface(disp, old_dsurf);1860dri2_destroy_surface(disp, old_rsurf);18611862if (old_ctx) {1863dri2_destroy_context(disp, old_ctx);1864dri2_display_release(old_disp);1865}18661867if (egl_error != EGL_SUCCESS)1868return _eglError(egl_error, "eglMakeCurrent");18691870if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&1871dri2_dpy->vtbl->set_shared_buffer_mode) {1872/* Always update the shared buffer mode. This is obviously needed when1873* the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When1874* EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the1875* case where external non-EGL API may have changed window's shared1876* buffer mode since we last saw it.1877*/1878bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);1879dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);1880}18811882return EGL_TRUE;1883}18841885__DRIdrawable *1886dri2_surface_get_dri_drawable(_EGLSurface *surf)1887{1888struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);18891890return dri2_surf->dri_drawable;1891}18921893/*1894* Called from eglGetProcAddress() via drv->GetProcAddress().1895*/1896static _EGLProc1897dri2_get_proc_address(const char *procname)1898{1899return _glapi_get_proc_address(procname);1900}19011902static _EGLSurface*1903dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,1904void *native_window, const EGLint *attrib_list)1905{1906struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1907return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,1908attrib_list);1909}19101911static _EGLSurface*1912dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,1913void *native_pixmap, const EGLint *attrib_list)1914{1915struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1916if (!dri2_dpy->vtbl->create_pixmap_surface)1917return NULL;1918return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,1919attrib_list);1920}19211922static _EGLSurface*1923dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,1924const EGLint *attrib_list)1925{1926struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1927if (!dri2_dpy->vtbl->create_pbuffer_surface)1928return NULL;1929return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);1930}19311932static EGLBoolean1933dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)1934{1935struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1936if (!dri2_dpy->vtbl->swap_interval)1937return EGL_TRUE;1938return dri2_dpy->vtbl->swap_interval(disp, surf, interval);1939}19401941/**1942* Asks the client API to flush any rendering to the drawable so that we can1943* do our swapbuffers.1944*/1945void1946dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)1947{1948struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1949__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);19501951if (dri2_dpy->flush) {1952if (dri2_dpy->flush->base.version >= 4) {1953/* We know there's a current context because:1954*1955* "If surface is not bound to the calling thread’s current1956* context, an EGL_BAD_SURFACE error is generated."1957*/1958_EGLContext *ctx = _eglGetCurrentContext();1959struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);19601961/* From the EGL 1.4 spec (page 52):1962*1963* "The contents of ancillary buffers are always undefined1964* after calling eglSwapBuffers."1965*/1966dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,1967dri_drawable,1968__DRI2_FLUSH_DRAWABLE |1969__DRI2_FLUSH_INVALIDATE_ANCILLARY,1970__DRI2_THROTTLE_SWAPBUFFER);1971} else {1972dri2_dpy->flush->flush(dri_drawable);1973}1974}1975}19761977static EGLBoolean1978dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)1979{1980struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);1981__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);1982_EGLContext *ctx = _eglGetCurrentContext();1983EGLBoolean ret;19841985if (ctx && surf)1986dri2_surf_update_fence_fd(ctx, disp, surf);1987ret = dri2_dpy->vtbl->swap_buffers(disp, surf);19881989/* SwapBuffers marks the end of the frame; reset the damage region for1990* use again next time.1991*/1992if (ret && dri2_dpy->buffer_damage &&1993dri2_dpy->buffer_damage->set_damage_region)1994dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);19951996return ret;1997}19981999static EGLBoolean2000dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,2001const EGLint *rects, EGLint n_rects)2002{2003struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2004__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);2005_EGLContext *ctx = _eglGetCurrentContext();2006EGLBoolean ret;20072008if (ctx && surf)2009dri2_surf_update_fence_fd(ctx, disp, surf);2010if (dri2_dpy->vtbl->swap_buffers_with_damage)2011ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,2012rects, n_rects);2013else2014ret = dri2_dpy->vtbl->swap_buffers(disp, surf);20152016/* SwapBuffers marks the end of the frame; reset the damage region for2017* use again next time.2018*/2019if (ret && dri2_dpy->buffer_damage &&2020dri2_dpy->buffer_damage->set_damage_region)2021dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);20222023return ret;2024}20252026static EGLBoolean2027dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,2028EGLint numRects, const EGLint *rects)2029{2030struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2031__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);2032EGLBoolean ret;20332034if (!dri2_dpy->vtbl->swap_buffers_region)2035return EGL_FALSE;2036ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);20372038/* SwapBuffers marks the end of the frame; reset the damage region for2039* use again next time.2040*/2041if (ret && dri2_dpy->buffer_damage &&2042dri2_dpy->buffer_damage->set_damage_region)2043dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);20442045return ret;2046}20472048static EGLBoolean2049dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,2050EGLint *rects, EGLint n_rects)2051{2052struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2053__DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);20542055if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)2056return EGL_FALSE;20572058dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);2059return EGL_TRUE;2060}20612062static EGLBoolean2063dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,2064EGLint x, EGLint y, EGLint width, EGLint height)2065{2066struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2067if (!dri2_dpy->vtbl->post_sub_buffer)2068return EGL_FALSE;2069return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);2070}20712072static EGLBoolean2073dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)2074{2075struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2076if (!dri2_dpy->vtbl->copy_buffers)2077return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");2078return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);2079}20802081static EGLint2082dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)2083{2084struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2085if (!dri2_dpy->vtbl->query_buffer_age)2086return 0;2087return dri2_dpy->vtbl->query_buffer_age(disp, surf);2088}20892090static EGLBoolean2091dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)2092{2093struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2094_EGLSurface *surf = ctx->DrawSurface;2095__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);20962097/* FIXME: If EGL allows frontbuffer rendering for window surfaces,2098* we need to copy fake to real here.*/20992100if (dri2_dpy->flush != NULL)2101dri2_dpy->flush->flush(dri_drawable);21022103return EGL_TRUE;2104}21052106static EGLBoolean2107dri2_wait_native(EGLint engine)2108{2109if (engine != EGL_CORE_NATIVE_ENGINE)2110return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");2111/* glXWaitX(); */21122113return EGL_TRUE;2114}21152116static EGLBoolean2117dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)2118{2119struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2120struct dri2_egl_context *dri2_ctx;2121_EGLContext *ctx;2122GLint format, target;2123__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);21242125ctx = _eglGetCurrentContext();2126dri2_ctx = dri2_egl_context(ctx);21272128if (!_eglBindTexImage(disp, surf, buffer))2129return EGL_FALSE;21302131switch (surf->TextureFormat) {2132case EGL_TEXTURE_RGB:2133format = __DRI_TEXTURE_FORMAT_RGB;2134break;2135case EGL_TEXTURE_RGBA:2136format = __DRI_TEXTURE_FORMAT_RGBA;2137break;2138default:2139assert(!"Unexpected texture format in dri2_bind_tex_image()");2140format = __DRI_TEXTURE_FORMAT_RGBA;2141}21422143switch (surf->TextureTarget) {2144case EGL_TEXTURE_2D:2145target = GL_TEXTURE_2D;2146break;2147default:2148target = GL_TEXTURE_2D;2149assert(!"Unexpected texture target in dri2_bind_tex_image()");2150}21512152dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,2153target, format,2154dri_drawable);21552156return EGL_TRUE;2157}21582159static EGLBoolean2160dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)2161{2162struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2163struct dri2_egl_context *dri2_ctx;2164_EGLContext *ctx;2165GLint target;2166__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);21672168ctx = _eglGetCurrentContext();2169dri2_ctx = dri2_egl_context(ctx);21702171if (!_eglReleaseTexImage(disp, surf, buffer))2172return EGL_FALSE;21732174switch (surf->TextureTarget) {2175case EGL_TEXTURE_2D:2176target = GL_TEXTURE_2D;2177break;2178default:2179assert(!"missing texture target");2180}21812182if (dri2_dpy->tex_buffer->base.version >= 3 &&2183dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {2184dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,2185target, dri_drawable);2186}21872188return EGL_TRUE;2189}21902191static _EGLImage*2192dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,2193EGLClientBuffer buffer, const EGLint *attr_list)2194{2195struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2196return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,2197attr_list);2198}21992200_EGLImage *2201dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)2202{2203struct dri2_egl_image *dri2_img;22042205if (dri_image == NULL) {2206_eglError(EGL_BAD_ALLOC, "dri2_create_image");2207return NULL;2208}22092210dri2_img = malloc(sizeof *dri2_img);2211if (!dri2_img) {2212_eglError(EGL_BAD_ALLOC, "dri2_create_image");2213return NULL;2214}22152216_eglInitImage(&dri2_img->base, disp);22172218dri2_img->dri_image = dri_image;22192220return &dri2_img->base;2221}22222223/**2224* Translate a DRI Image extension error code into an EGL error code.2225*/2226static EGLint2227egl_error_from_dri_image_error(int dri_error)2228{2229switch (dri_error) {2230case __DRI_IMAGE_ERROR_SUCCESS:2231return EGL_SUCCESS;2232case __DRI_IMAGE_ERROR_BAD_ALLOC:2233return EGL_BAD_ALLOC;2234case __DRI_IMAGE_ERROR_BAD_MATCH:2235return EGL_BAD_MATCH;2236case __DRI_IMAGE_ERROR_BAD_PARAMETER:2237return EGL_BAD_PARAMETER;2238case __DRI_IMAGE_ERROR_BAD_ACCESS:2239return EGL_BAD_ACCESS;2240default:2241assert(!"unknown dri_error code");2242return EGL_BAD_ALLOC;2243}2244}22452246static _EGLImage *2247dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,2248EGLClientBuffer buffer,2249const EGLint *attr_list)2250{2251struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2252struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);2253GLuint renderbuffer = (GLuint) (uintptr_t) buffer;2254__DRIimage *dri_image;22552256if (renderbuffer == 0) {2257_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2258return EGL_NO_IMAGE_KHR;2259}22602261if (!disp->Extensions.KHR_gl_renderbuffer_image) {2262_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2263return EGL_NO_IMAGE_KHR;2264}22652266if (dri2_dpy->image->base.version >= 17 &&2267dri2_dpy->image->createImageFromRenderbuffer2) {2268unsigned error = ~0;22692270dri_image = dri2_dpy->image->createImageFromRenderbuffer2(2271dri2_ctx->dri_context, renderbuffer, NULL, &error);22722273assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));22742275if (!dri_image) {2276_eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");2277return EGL_NO_IMAGE_KHR;2278}2279} else {2280dri_image = dri2_dpy->image->createImageFromRenderbuffer(2281dri2_ctx->dri_context, renderbuffer, NULL);2282if (!dri_image) {2283_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");2284return EGL_NO_IMAGE_KHR;2285}2286}22872288return dri2_create_image_from_dri(disp, dri_image);2289}22902291#ifdef HAVE_WAYLAND_PLATFORM22922293/* This structure describes how a wl_buffer maps to one or more2294* __DRIimages. A wl_drm_buffer stores the wl_drm format code and the2295* offsets and strides of the planes in the buffer. This table maps a2296* wl_drm format code to a description of the planes in the buffer2297* that lets us create a __DRIimage for each of the planes. */22982299static const struct wl_drm_components_descriptor {2300uint32_t dri_components;2301EGLint components;2302int nplanes;2303} wl_drm_components[] = {2304{ __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },2305{ __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },2306{ __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },2307{ __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },2308{ __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },2309};23102311static _EGLImage *2312dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,2313EGLClientBuffer _buffer,2314const EGLint *attr_list)2315{2316struct wl_drm_buffer *buffer;2317struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2318const struct wl_drm_components_descriptor *f;2319__DRIimage *dri_image;2320_EGLImageAttribs attrs;2321int32_t plane;23222323buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,2324(struct wl_resource *) _buffer);2325if (!buffer)2326return NULL;23272328if (!_eglParseImageAttribList(&attrs, disp, attr_list))2329return NULL;23302331plane = attrs.PlaneWL;2332f = buffer->driver_format;2333if (plane < 0 || plane >= f->nplanes) {2334_eglError(EGL_BAD_PARAMETER,2335"dri2_create_image_wayland_wl_buffer (plane out of bounds)");2336return NULL;2337}23382339dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);2340if (dri_image == NULL && plane == 0)2341dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);2342if (dri_image == NULL) {2343_eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");2344return NULL;2345}23462347return dri2_create_image_from_dri(disp, dri_image);2348}2349#endif23502351static EGLBoolean2352dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,2353EGLuint64KHR *ust, EGLuint64KHR *msc,2354EGLuint64KHR *sbc)2355{2356struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2357if (!dri2_dpy->vtbl->get_sync_values)2358return EGL_FALSE;2359return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);2360}23612362/**2363* Set the error code after a call to2364* dri2_egl_image::dri_image::createImageFromTexture.2365*/2366static void2367dri2_create_image_khr_texture_error(int dri_error)2368{2369EGLint egl_error = egl_error_from_dri_image_error(dri_error);23702371if (egl_error != EGL_SUCCESS)2372_eglError(egl_error, "dri2_create_image_khr_texture");2373}23742375static _EGLImage *2376dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,2377EGLenum target,2378EGLClientBuffer buffer,2379const EGLint *attr_list)2380{2381struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2382struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);2383struct dri2_egl_image *dri2_img;2384GLuint texture = (GLuint) (uintptr_t) buffer;2385_EGLImageAttribs attrs;2386GLuint depth;2387GLenum gl_target;2388unsigned error;23892390if (texture == 0) {2391_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2392return EGL_NO_IMAGE_KHR;2393}23942395if (!_eglParseImageAttribList(&attrs, disp, attr_list))2396return EGL_NO_IMAGE_KHR;23972398switch (target) {2399case EGL_GL_TEXTURE_2D_KHR:2400if (!disp->Extensions.KHR_gl_texture_2D_image) {2401_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2402return EGL_NO_IMAGE_KHR;2403}2404depth = 0;2405gl_target = GL_TEXTURE_2D;2406break;2407case EGL_GL_TEXTURE_3D_KHR:2408if (!disp->Extensions.KHR_gl_texture_3D_image) {2409_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2410return EGL_NO_IMAGE_KHR;2411}24122413depth = attrs.GLTextureZOffset;2414gl_target = GL_TEXTURE_3D;2415break;2416case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:2417case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:2418case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:2419case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:2420case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:2421case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:2422if (!disp->Extensions.KHR_gl_texture_cubemap_image) {2423_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");2424return EGL_NO_IMAGE_KHR;2425}24262427depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;2428gl_target = GL_TEXTURE_CUBE_MAP;2429break;2430default:2431unreachable("Unexpected target in dri2_create_image_khr_texture()");2432return EGL_NO_IMAGE_KHR;2433}24342435dri2_img = malloc(sizeof *dri2_img);2436if (!dri2_img) {2437_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");2438return EGL_NO_IMAGE_KHR;2439}24402441_eglInitImage(&dri2_img->base, disp);24422443dri2_img->dri_image =2444dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,2445gl_target,2446texture,2447depth,2448attrs.GLTextureLevel,2449&error,2450NULL);2451dri2_create_image_khr_texture_error(error);24522453if (!dri2_img->dri_image) {2454free(dri2_img);2455return EGL_NO_IMAGE_KHR;2456}2457return &dri2_img->base;2458}24592460static EGLBoolean2461dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,2462EGLint attribute, EGLint *value)2463{2464struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2465if (!dri2_dpy->vtbl->query_surface)2466return _eglQuerySurface(disp, surf, attribute, value);2467return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);2468}24692470static struct wl_buffer*2471dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)2472{2473struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2474if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)2475return NULL;2476return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);2477}24782479#ifdef HAVE_LIBDRM2480static _EGLImage *2481dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,2482EGLClientBuffer buffer, const EGLint *attr_list)2483{2484struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2485EGLint format, name, pitch;2486_EGLImageAttribs attrs;2487__DRIimage *dri_image;24882489name = (EGLint) (uintptr_t) buffer;24902491if (!_eglParseImageAttribList(&attrs, disp, attr_list))2492return NULL;24932494if (attrs.Width <= 0 || attrs.Height <= 0 ||2495attrs.DRMBufferStrideMESA <= 0) {2496_eglError(EGL_BAD_PARAMETER,2497"bad width, height or stride");2498return NULL;2499}25002501switch (attrs.DRMBufferFormatMESA) {2502case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:2503format = __DRI_IMAGE_FORMAT_ARGB8888;2504pitch = attrs.DRMBufferStrideMESA;2505break;2506default:2507_eglError(EGL_BAD_PARAMETER,2508"dri2_create_image_khr: unsupported pixmap depth");2509return NULL;2510}25112512dri_image =2513dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,2514attrs.Width,2515attrs.Height,2516format,2517name,2518pitch,2519NULL);25202521return dri2_create_image_from_dri(disp, dri_image);2522}25232524static EGLBoolean2525dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)2526{2527/**2528* The spec says:2529*2530* "Required attributes and their values are as follows:2531*2532* * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels2533*2534* * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified2535* by drm_fourcc.h and used as the pixel_format parameter of the2536* drm_mode_fb_cmd2 ioctl."2537*2538* and2539*2540* "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is2541* incomplete, EGL_BAD_PARAMETER is generated."2542*/2543if (attrs->Width <= 0 || attrs->Height <= 0 ||2544!attrs->DMABufFourCC.IsPresent)2545return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");25462547/**2548* Also:2549*2550* "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values2551* specified for a plane's pitch or offset isn't supported by EGL,2552* EGL_BAD_ACCESS is generated."2553*/2554for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {2555if (attrs->DMABufPlanePitches[i].IsPresent &&2556attrs->DMABufPlanePitches[i].Value <= 0)2557return _eglError(EGL_BAD_ACCESS, "invalid pitch");2558}25592560/**2561* If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following2562* attribute values may be given.2563*2564* This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and2565* EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.2566*/2567for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {2568if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=2569attrs->DMABufPlaneModifiersHi[i].IsPresent)2570return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");2571}25722573/* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't2574* mandate it, we only accept the same modifier across all planes. */2575for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {2576if (attrs->DMABufPlaneFds[i].IsPresent) {2577if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=2578attrs->DMABufPlaneModifiersLo[i].IsPresent) ||2579(attrs->DMABufPlaneModifiersLo[0].Value !=2580attrs->DMABufPlaneModifiersLo[i].Value) ||2581(attrs->DMABufPlaneModifiersHi[0].Value !=2582attrs->DMABufPlaneModifiersHi[i].Value))2583return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");2584}2585}25862587return EGL_TRUE;2588}25892590/* Returns the total number of planes for the format or zero if it isn't a2591* valid fourcc format.2592*/2593static unsigned2594dri2_num_fourcc_format_planes(EGLint format)2595{2596switch (format) {2597case DRM_FORMAT_R8:2598case DRM_FORMAT_RG88:2599case DRM_FORMAT_GR88:2600case DRM_FORMAT_R16:2601case DRM_FORMAT_GR1616:2602case DRM_FORMAT_RGB332:2603case DRM_FORMAT_BGR233:2604case DRM_FORMAT_XRGB4444:2605case DRM_FORMAT_XBGR4444:2606case DRM_FORMAT_RGBX4444:2607case DRM_FORMAT_BGRX4444:2608case DRM_FORMAT_ARGB4444:2609case DRM_FORMAT_ABGR4444:2610case DRM_FORMAT_RGBA4444:2611case DRM_FORMAT_BGRA4444:2612case DRM_FORMAT_XRGB1555:2613case DRM_FORMAT_XBGR1555:2614case DRM_FORMAT_RGBX5551:2615case DRM_FORMAT_BGRX5551:2616case DRM_FORMAT_ARGB1555:2617case DRM_FORMAT_ABGR1555:2618case DRM_FORMAT_RGBA5551:2619case DRM_FORMAT_BGRA5551:2620case DRM_FORMAT_RGB565:2621case DRM_FORMAT_BGR565:2622case DRM_FORMAT_RGB888:2623case DRM_FORMAT_BGR888:2624case DRM_FORMAT_XRGB8888:2625case DRM_FORMAT_XBGR8888:2626case DRM_FORMAT_RGBX8888:2627case DRM_FORMAT_BGRX8888:2628case DRM_FORMAT_ARGB8888:2629case DRM_FORMAT_ABGR8888:2630case DRM_FORMAT_RGBA8888:2631case DRM_FORMAT_BGRA8888:2632case DRM_FORMAT_XRGB2101010:2633case DRM_FORMAT_XBGR2101010:2634case DRM_FORMAT_RGBX1010102:2635case DRM_FORMAT_BGRX1010102:2636case DRM_FORMAT_ARGB2101010:2637case DRM_FORMAT_ABGR2101010:2638case DRM_FORMAT_RGBA1010102:2639case DRM_FORMAT_BGRA1010102:2640case DRM_FORMAT_XBGR16161616F:2641case DRM_FORMAT_ABGR16161616F:2642case DRM_FORMAT_YUYV:2643case DRM_FORMAT_YVYU:2644case DRM_FORMAT_UYVY:2645case DRM_FORMAT_VYUY:2646case DRM_FORMAT_AYUV:2647case DRM_FORMAT_XYUV8888:2648case DRM_FORMAT_Y210:2649case DRM_FORMAT_Y212:2650case DRM_FORMAT_Y216:2651case DRM_FORMAT_Y410:2652case DRM_FORMAT_Y412:2653case DRM_FORMAT_Y416:2654return 1;26552656case DRM_FORMAT_NV12:2657case DRM_FORMAT_NV21:2658case DRM_FORMAT_NV16:2659case DRM_FORMAT_NV61:2660case DRM_FORMAT_P010:2661case DRM_FORMAT_P012:2662case DRM_FORMAT_P016:2663return 2;26642665case DRM_FORMAT_YUV410:2666case DRM_FORMAT_YVU410:2667case DRM_FORMAT_YUV411:2668case DRM_FORMAT_YVU411:2669case DRM_FORMAT_YUV420:2670case DRM_FORMAT_YVU420:2671case DRM_FORMAT_YUV422:2672case DRM_FORMAT_YVU422:2673case DRM_FORMAT_YUV444:2674case DRM_FORMAT_YVU444:2675return 3;26762677default:2678return 0;2679}2680}26812682/* Returns the total number of file descriptors. Zero indicates an error. */2683static unsigned2684dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)2685{2686unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);2687if (plane_n == 0) {2688_eglError(EGL_BAD_MATCH, "unknown drm fourcc format");2689return 0;2690}26912692for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {2693/**2694* The modifiers extension spec says:2695*2696* "Modifiers may modify any attribute of a buffer import, including2697* but not limited to adding extra planes to a format which2698* otherwise does not have those planes. As an example, a modifier2699* may add a plane for an external compression buffer to a2700* single-plane format. The exact meaning and effect of any2701* modifier is canonically defined by drm_fourcc.h, not as part of2702* this extension."2703*/2704if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&2705attrs->DMABufPlaneModifiersHi[i].IsPresent) {2706plane_n = i + 1;2707}2708}27092710/**2711* The spec says:2712*2713* "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is2714* incomplete, EGL_BAD_PARAMETER is generated."2715*/2716for (unsigned i = 0; i < plane_n; ++i) {2717if (!attrs->DMABufPlaneFds[i].IsPresent ||2718!attrs->DMABufPlaneOffsets[i].IsPresent ||2719!attrs->DMABufPlanePitches[i].IsPresent) {2720_eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");2721return 0;2722}2723}27242725/**2726* The spec also says:2727*2728* "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT2729* attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is2730* generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*2731* or EGL_DMA_BUF_PLANE3_* attributes are specified."2732*/2733for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {2734if (attrs->DMABufPlaneFds[i].IsPresent ||2735attrs->DMABufPlaneOffsets[i].IsPresent ||2736attrs->DMABufPlanePitches[i].IsPresent) {2737_eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");2738return 0;2739}2740}27412742return plane_n;2743}27442745static EGLBoolean2746dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,2747EGLint *formats, EGLint *count)2748{2749struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2750if (max < 0 || (max > 0 && formats == NULL))2751return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");27522753if (dri2_dpy->image->base.version < 15 ||2754dri2_dpy->image->queryDmaBufFormats == NULL)2755return EGL_FALSE;27562757if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,2758formats, count))2759return EGL_FALSE;27602761if (max > 0) {2762/* Assert that all of the formats returned are actually fourcc formats.2763* Some day, if we want the internal interface function to be able to2764* return the fake fourcc formats defined in dri_interface.h, we'll have2765* to do something more clever here to pair the list down to just real2766* fourcc formats so that we don't leak the fake internal ones.2767*/2768for (int i = 0; i < *count; i++) {2769assert(dri2_num_fourcc_format_planes(formats[i]) > 0);2770}2771}27722773return EGL_TRUE;2774}27752776static EGLBoolean2777dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,2778EGLint max, EGLuint64KHR *modifiers,2779EGLBoolean *external_only, EGLint *count)2780{2781struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);27822783if (dri2_num_fourcc_format_planes(format) == 0)2784return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");27852786if (max < 0)2787return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");27882789if (max > 0 && modifiers == NULL)2790return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");27912792if (dri2_dpy->image->base.version < 15 ||2793dri2_dpy->image->queryDmaBufModifiers == NULL)2794return EGL_FALSE;27952796if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,2797max, modifiers,2798(unsigned int *) external_only,2799count) == false)2800return _eglError(EGL_BAD_PARAMETER, "invalid format");28012802return EGL_TRUE;2803}28042805/**2806* The spec says:2807*2808* "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the2809* EGL will take a reference to the dma_buf(s) which it will release at any2810* time while the EGLDisplay is initialized. It is the responsibility of the2811* application to close the dma_buf file descriptors."2812*2813* Therefore we must never close or otherwise modify the file descriptors.2814*/2815_EGLImage *2816dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,2817EGLClientBuffer buffer, const EGLint *attr_list)2818{2819struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2820_EGLImage *res;2821_EGLImageAttribs attrs;2822__DRIimage *dri_image;2823unsigned num_fds;2824int fds[DMA_BUF_MAX_PLANES];2825int pitches[DMA_BUF_MAX_PLANES];2826int offsets[DMA_BUF_MAX_PLANES];2827uint64_t modifier;2828bool has_modifier = false;2829unsigned error;28302831/**2832* The spec says:2833*2834* ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the2835* error EGL_BAD_PARAMETER is generated."2836*/2837if (buffer != NULL) {2838_eglError(EGL_BAD_PARAMETER, "buffer not NULL");2839return NULL;2840}28412842if (!_eglParseImageAttribList(&attrs, disp, attr_list))2843return NULL;28442845if (!dri2_check_dma_buf_attribs(&attrs))2846return NULL;28472848num_fds = dri2_check_dma_buf_format(&attrs);2849if (!num_fds)2850return NULL;28512852for (unsigned i = 0; i < num_fds; ++i) {2853fds[i] = attrs.DMABufPlaneFds[i].Value;2854pitches[i] = attrs.DMABufPlanePitches[i].Value;2855offsets[i] = attrs.DMABufPlaneOffsets[i].Value;2856}28572858/* dri2_check_dma_buf_attribs ensures that the modifier, if available,2859* will be present in attrs.DMABufPlaneModifiersLo[0] and2860* attrs.DMABufPlaneModifiersHi[0] */2861if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {2862modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,2863attrs.DMABufPlaneModifiersLo[0].Value);2864has_modifier = true;2865}28662867if (attrs.ProtectedContent) {2868if (dri2_dpy->image->base.version < 18 ||2869dri2_dpy->image->createImageFromDmaBufs3 == NULL) {2870_eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");2871return EGL_NO_IMAGE_KHR;2872}2873if (!has_modifier)2874modifier = DRM_FORMAT_MOD_INVALID;28752876dri_image =2877dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,2878attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,2879modifier, fds, num_fds, pitches, offsets,2880attrs.DMABufYuvColorSpaceHint.Value,2881attrs.DMABufSampleRangeHint.Value,2882attrs.DMABufChromaHorizontalSiting.Value,2883attrs.DMABufChromaVerticalSiting.Value,2884attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,2885&error,2886NULL);2887}2888else if (has_modifier) {2889if (dri2_dpy->image->base.version < 15 ||2890dri2_dpy->image->createImageFromDmaBufs2 == NULL) {2891_eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");2892return EGL_NO_IMAGE_KHR;2893}2894dri_image =2895dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,2896attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,2897modifier, fds, num_fds, pitches, offsets,2898attrs.DMABufYuvColorSpaceHint.Value,2899attrs.DMABufSampleRangeHint.Value,2900attrs.DMABufChromaHorizontalSiting.Value,2901attrs.DMABufChromaVerticalSiting.Value,2902&error,2903NULL);2904}2905else {2906dri_image =2907dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,2908attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,2909fds, num_fds, pitches, offsets,2910attrs.DMABufYuvColorSpaceHint.Value,2911attrs.DMABufSampleRangeHint.Value,2912attrs.DMABufChromaHorizontalSiting.Value,2913attrs.DMABufChromaVerticalSiting.Value,2914&error,2915NULL);2916}2917dri2_create_image_khr_texture_error(error);29182919if (!dri_image)2920return EGL_NO_IMAGE_KHR;29212922res = dri2_create_image_from_dri(disp, dri_image);29232924return res;2925}2926static _EGLImage *2927dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)2928{2929struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);2930struct dri2_egl_image *dri2_img;2931_EGLImageAttribs attrs;2932unsigned int dri_use, valid_mask;2933int format;29342935if (!attr_list) {2936_eglError(EGL_BAD_PARAMETER, __func__);2937return EGL_NO_IMAGE_KHR;2938}29392940if (!_eglParseImageAttribList(&attrs, disp, attr_list))2941return EGL_NO_IMAGE_KHR;29422943if (attrs.Width <= 0 || attrs.Height <= 0) {2944_eglError(EGL_BAD_PARAMETER, __func__);2945return EGL_NO_IMAGE_KHR;2946}29472948switch (attrs.DRMBufferFormatMESA) {2949case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:2950format = __DRI_IMAGE_FORMAT_ARGB8888;2951break;2952default:2953_eglError(EGL_BAD_PARAMETER, __func__);2954return EGL_NO_IMAGE_KHR;2955}29562957valid_mask =2958EGL_DRM_BUFFER_USE_SCANOUT_MESA |2959EGL_DRM_BUFFER_USE_SHARE_MESA |2960EGL_DRM_BUFFER_USE_CURSOR_MESA;2961if (attrs.DRMBufferUseMESA & ~valid_mask) {2962_eglError(EGL_BAD_PARAMETER, __func__);2963return EGL_NO_IMAGE_KHR;2964}29652966dri_use = 0;2967if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)2968dri_use |= __DRI_IMAGE_USE_SHARE;2969if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)2970dri_use |= __DRI_IMAGE_USE_SCANOUT;2971if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)2972dri_use |= __DRI_IMAGE_USE_CURSOR;29732974dri2_img = malloc(sizeof *dri2_img);2975if (!dri2_img) {2976_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");2977return EGL_NO_IMAGE_KHR;2978}29792980_eglInitImage(&dri2_img->base, disp);29812982dri2_img->dri_image =2983dri2_dpy->image->createImage(dri2_dpy->dri_screen,2984attrs.Width, attrs.Height,2985format, dri_use, dri2_img);2986if (dri2_img->dri_image == NULL) {2987free(dri2_img);2988_eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");2989return EGL_NO_IMAGE_KHR;2990}29912992return &dri2_img->base;2993}29942995static EGLBoolean2996dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,2997EGLint *name, EGLint *handle, EGLint *stride)2998{2999struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3000struct dri2_egl_image *dri2_img = dri2_egl_image(img);30013002if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,3003__DRI_IMAGE_ATTRIB_NAME, name))3004return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");30053006if (handle)3007dri2_dpy->image->queryImage(dri2_img->dri_image,3008__DRI_IMAGE_ATTRIB_HANDLE, handle);30093010if (stride)3011dri2_dpy->image->queryImage(dri2_img->dri_image,3012__DRI_IMAGE_ATTRIB_STRIDE, stride);30133014return EGL_TRUE;3015}30163017/**3018* Checks if we can support EGL_MESA_image_dma_buf_export on this image.30193020* The spec provides a boolean return for the driver to reject exporting for3021* basically any reason, but doesn't specify any particular error cases. For3022* now, we just fail if we don't have a DRM fourcc for the format.3023*/3024static bool3025dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)3026{3027struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3028struct dri2_egl_image *dri2_img = dri2_egl_image(img);3029EGLint fourcc;30303031if (!dri2_dpy->image->queryImage(dri2_img->dri_image,3032__DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {3033return false;3034}30353036return true;3037}30383039static EGLBoolean3040dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,3041EGLint *fourcc, EGLint *nplanes,3042EGLuint64KHR *modifiers)3043{3044struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3045struct dri2_egl_image *dri2_img = dri2_egl_image(img);3046int num_planes;30473048if (!dri2_can_export_dma_buf_image(disp, img))3049return EGL_FALSE;30503051dri2_dpy->image->queryImage(dri2_img->dri_image,3052__DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);3053if (nplanes)3054*nplanes = num_planes;30553056if (fourcc)3057dri2_dpy->image->queryImage(dri2_img->dri_image,3058__DRI_IMAGE_ATTRIB_FOURCC, fourcc);30593060if (modifiers) {3061int mod_hi, mod_lo;3062uint64_t modifier = DRM_FORMAT_MOD_INVALID;3063bool query;30643065query = dri2_dpy->image->queryImage(dri2_img->dri_image,3066__DRI_IMAGE_ATTRIB_MODIFIER_UPPER,3067&mod_hi);3068query &= dri2_dpy->image->queryImage(dri2_img->dri_image,3069__DRI_IMAGE_ATTRIB_MODIFIER_LOWER,3070&mod_lo);3071if (query)3072modifier = combine_u32_into_u64 (mod_hi, mod_lo);30733074for (int i = 0; i < num_planes; i++)3075modifiers[i] = modifier;3076}30773078return EGL_TRUE;3079}30803081static EGLBoolean3082dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,3083int *fds, EGLint *strides, EGLint *offsets)3084{3085struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3086struct dri2_egl_image *dri2_img = dri2_egl_image(img);3087EGLint nplanes;30883089if (!dri2_can_export_dma_buf_image(disp, img))3090return EGL_FALSE;30913092/* EGL_MESA_image_dma_buf_export spec says:3093* "If the number of fds is less than the number of planes, then3094* subsequent fd slots should contain -1."3095*/3096if (fds) {3097/* Query nplanes so that we know how big the given array is. */3098dri2_dpy->image->queryImage(dri2_img->dri_image,3099__DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);3100memset(fds, -1, nplanes * sizeof(int));3101}31023103/* rework later to provide multiple fds/strides/offsets */3104if (fds)3105dri2_dpy->image->queryImage(dri2_img->dri_image,3106__DRI_IMAGE_ATTRIB_FD, fds);31073108if (strides)3109dri2_dpy->image->queryImage(dri2_img->dri_image,3110__DRI_IMAGE_ATTRIB_STRIDE, strides);31113112if (offsets) {3113int img_offset;3114bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,3115__DRI_IMAGE_ATTRIB_OFFSET, &img_offset);3116if (ret)3117offsets[0] = img_offset;3118else3119offsets[0] = 0;3120}31213122return EGL_TRUE;3123}31243125#endif31263127_EGLImage *3128dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,3129EGLClientBuffer buffer, const EGLint *attr_list)3130{3131switch (target) {3132case EGL_GL_TEXTURE_2D_KHR:3133case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:3134case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:3135case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:3136case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:3137case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:3138case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:3139case EGL_GL_TEXTURE_3D_KHR:3140return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);3141case EGL_GL_RENDERBUFFER_KHR:3142return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);3143#ifdef HAVE_LIBDRM3144case EGL_DRM_BUFFER_MESA:3145return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);3146case EGL_LINUX_DMA_BUF_EXT:3147return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);3148#endif3149#ifdef HAVE_WAYLAND_PLATFORM3150case EGL_WAYLAND_BUFFER_WL:3151return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);3152#endif3153default:3154_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");3155return EGL_NO_IMAGE_KHR;3156}3157}31583159static EGLBoolean3160dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)3161{3162struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3163struct dri2_egl_image *dri2_img = dri2_egl_image(image);31643165dri2_dpy->image->destroyImage(dri2_img->dri_image);3166free(dri2_img);31673168return EGL_TRUE;3169}31703171#ifdef HAVE_WAYLAND_PLATFORM31723173static void3174dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,3175struct wl_drm_buffer *buffer)3176{3177_EGLDisplay *disp = user_data;3178struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3179__DRIimage *img;3180int dri_components = 0;31813182if (fd == -1)3183img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,3184buffer->width,3185buffer->height,3186buffer->format,3187(int*)&name, 1,3188buffer->stride,3189buffer->offset,3190NULL);3191else3192img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,3193buffer->width,3194buffer->height,3195buffer->format,3196&fd, 1,3197buffer->stride,3198buffer->offset,3199NULL);32003201if (img == NULL)3202return;32033204dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);32053206buffer->driver_format = NULL;3207for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)3208if (wl_drm_components[i].dri_components == dri_components)3209buffer->driver_format = &wl_drm_components[i];32103211if (buffer->driver_format == NULL)3212dri2_dpy->image->destroyImage(img);3213else3214buffer->driver_buffer = img;3215}32163217static void3218dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)3219{3220_EGLDisplay *disp = user_data;3221struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);32223223dri2_dpy->image->destroyImage(buffer->driver_buffer);3224}32253226static EGLBoolean3227dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)3228{3229struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3230const struct wayland_drm_callbacks wl_drm_callbacks = {3231.authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,3232.reference_buffer = dri2_wl_reference_buffer,3233.release_buffer = dri2_wl_release_buffer,3234.is_format_supported = dri2_wl_is_format_supported3235};3236int flags = 0;3237char *device_name;3238uint64_t cap;32393240if (dri2_dpy->wl_server_drm)3241return EGL_FALSE;32423243device_name = drmGetRenderDeviceNameFromFd(dri2_dpy->fd);3244if (!device_name)3245device_name = strdup(dri2_dpy->device_name);3246if (!device_name)3247return EGL_FALSE;32483249if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&3250cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&3251dri2_dpy->image->base.version >= 7 &&3252dri2_dpy->image->createImageFromFds != NULL)3253flags |= WAYLAND_DRM_PRIME;32543255dri2_dpy->wl_server_drm =3256wayland_drm_init(wl_dpy, device_name,3257&wl_drm_callbacks, disp, flags);32583259free(device_name);32603261if (!dri2_dpy->wl_server_drm)3262return EGL_FALSE;32633264#ifdef HAVE_DRM_PLATFORM3265/* We have to share the wl_drm instance with gbm, so gbm can convert3266* wl_buffers to gbm bos. */3267if (dri2_dpy->gbm_dri)3268dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;3269#endif32703271return EGL_TRUE;3272}32733274static EGLBoolean3275dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)3276{3277struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);32783279if (!dri2_dpy->wl_server_drm)3280return EGL_FALSE;32813282wayland_drm_uninit(dri2_dpy->wl_server_drm);3283dri2_dpy->wl_server_drm = NULL;32843285return EGL_TRUE;3286}32873288static EGLBoolean3289dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,3290EGLint attribute, EGLint *value)3291{3292struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3293struct wl_drm_buffer *buffer;3294const struct wl_drm_components_descriptor *format;32953296buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);3297if (!buffer)3298return EGL_FALSE;32993300format = buffer->driver_format;3301switch (attribute) {3302case EGL_TEXTURE_FORMAT:3303*value = format->components;3304return EGL_TRUE;3305case EGL_WIDTH:3306*value = buffer->width;3307return EGL_TRUE;3308case EGL_HEIGHT:3309*value = buffer->height;3310return EGL_TRUE;3311}33123313return EGL_FALSE;3314}3315#endif33163317static void3318dri2_egl_ref_sync(struct dri2_egl_sync *sync)3319{3320p_atomic_inc(&sync->refcount);3321}33223323static void3324dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,3325struct dri2_egl_sync *dri2_sync)3326{3327if (p_atomic_dec_zero(&dri2_sync->refcount)) {3328switch (dri2_sync->base.Type) {3329case EGL_SYNC_REUSABLE_KHR:3330cnd_destroy(&dri2_sync->cond);3331break;3332case EGL_SYNC_NATIVE_FENCE_ANDROID:3333if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)3334close(dri2_sync->base.SyncFd);3335break;3336default:3337break;3338}33393340if (dri2_sync->fence)3341dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);33423343free(dri2_sync);3344}3345}33463347static _EGLSync *3348dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)3349{3350_EGLContext *ctx = _eglGetCurrentContext();3351struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3352struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);3353struct dri2_egl_sync *dri2_sync;3354EGLint ret;3355pthread_condattr_t attr;33563357dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));3358if (!dri2_sync) {3359_eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");3360return NULL;3361}33623363if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {3364free(dri2_sync);3365return NULL;3366}33673368switch (type) {3369case EGL_SYNC_FENCE_KHR:3370dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);3371if (!dri2_sync->fence) {3372/* Why did it fail? DRI doesn't return an error code, so we emit3373* a generic EGL error that doesn't communicate user error.3374*/3375_eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");3376free(dri2_sync);3377return NULL;3378}3379break;33803381case EGL_SYNC_CL_EVENT_KHR:3382dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(3383dri2_dpy->dri_screen,3384dri2_sync->base.CLEvent);3385/* this can only happen if the cl_event passed in is invalid. */3386if (!dri2_sync->fence) {3387_eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");3388free(dri2_sync);3389return NULL;3390}33913392/* the initial status must be "signaled" if the cl_event is signaled */3393if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,3394dri2_sync->fence, 0, 0))3395dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;3396break;33973398case EGL_SYNC_REUSABLE_KHR:3399/* intialize attr */3400ret = pthread_condattr_init(&attr);34013402if (ret) {3403_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");3404free(dri2_sync);3405return NULL;3406}34073408/* change clock attribute to CLOCK_MONOTONIC */3409ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);34103411if (ret) {3412_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");3413free(dri2_sync);3414return NULL;3415}34163417ret = pthread_cond_init(&dri2_sync->cond, &attr);34183419if (ret) {3420_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");3421free(dri2_sync);3422return NULL;3423}34243425/* initial status of reusable sync must be "unsignaled" */3426dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;3427break;34283429case EGL_SYNC_NATIVE_FENCE_ANDROID:3430if (dri2_dpy->fence->create_fence_fd) {3431dri2_sync->fence = dri2_dpy->fence->create_fence_fd(3432dri2_ctx->dri_context,3433dri2_sync->base.SyncFd);3434}3435if (!dri2_sync->fence) {3436_eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");3437free(dri2_sync);3438return NULL;3439}3440break;3441}34423443p_atomic_set(&dri2_sync->refcount, 1);3444return &dri2_sync->base;3445}34463447static EGLBoolean3448dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)3449{3450struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3451struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);3452EGLint ret = EGL_TRUE;3453EGLint err;34543455/* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,3456* then unlock all threads possibly blocked by the reusable sync before3457* destroying it.3458*/3459if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&3460dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {3461dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;3462/* unblock all threads currently blocked by sync */3463err = cnd_broadcast(&dri2_sync->cond);34643465if (err) {3466_eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");3467ret = EGL_FALSE;3468}3469}34703471dri2_egl_unref_sync(dri2_dpy, dri2_sync);34723473return ret;3474}34753476static EGLint3477dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)3478{3479struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3480struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);34813482assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);34833484if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {3485/* try to retrieve the actual native fence fd.. if rendering is3486* not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:3487*/3488sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,3489dri2_sync->fence);3490}34913492if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {3493/* if native fence fd still not created, return an error: */3494_eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");3495return EGL_NO_NATIVE_FENCE_FD_ANDROID;3496}34973498assert(sync_valid_fd(sync->SyncFd));34993500return os_dupfd_cloexec(sync->SyncFd);3501}35023503static void3504dri2_set_blob_cache_funcs(_EGLDisplay *disp,3505EGLSetBlobFuncANDROID set,3506EGLGetBlobFuncANDROID get)3507{3508struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3509dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,3510disp->BlobCacheSet,3511disp->BlobCacheGet);3512}35133514static EGLint3515dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,3516EGLint flags, EGLTime timeout)3517{3518_EGLContext *ctx = _eglGetCurrentContext();3519struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3520struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);3521struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);3522unsigned wait_flags = 0;35233524EGLint ret = EGL_CONDITION_SATISFIED_KHR;35253526/* The EGL_KHR_fence_sync spec states:3527*3528* "If no context is current for the bound API,3529* the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.3530*/3531if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)3532wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;35333534/* the sync object should take a reference while waiting */3535dri2_egl_ref_sync(dri2_sync);35363537switch (sync->Type) {3538case EGL_SYNC_FENCE_KHR:3539case EGL_SYNC_NATIVE_FENCE_ANDROID:3540case EGL_SYNC_CL_EVENT_KHR:3541if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,3542dri2_sync->fence, wait_flags,3543timeout))3544dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;3545else3546ret = EGL_TIMEOUT_EXPIRED_KHR;3547break;35483549case EGL_SYNC_REUSABLE_KHR:3550if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&3551(flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {3552/* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */3553dri2_gl_flush();3554}35553556/* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/3557if (timeout == EGL_FOREVER_KHR) {3558mtx_lock(&dri2_sync->mutex);3559cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);3560mtx_unlock(&dri2_sync->mutex);3561} else {3562/* if reusable sync has not been yet signaled */3563if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {3564/* timespecs for cnd_timedwait */3565struct timespec current;3566struct timespec expire;35673568/* We override the clock to monotonic when creating the condition3569* variable. */3570clock_gettime(CLOCK_MONOTONIC, ¤t);35713572/* calculating when to expire */3573expire.tv_nsec = timeout % 1000000000L;3574expire.tv_sec = timeout / 1000000000L;35753576expire.tv_nsec += current.tv_nsec;3577expire.tv_sec += current.tv_sec;35783579/* expire.nsec now is a number between 0 and 1999999998 */3580if (expire.tv_nsec > 999999999L) {3581expire.tv_sec++;3582expire.tv_nsec -= 1000000000L;3583}35843585mtx_lock(&dri2_sync->mutex);3586ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);3587mtx_unlock(&dri2_sync->mutex);35883589if (ret == thrd_busy) {3590if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {3591ret = EGL_TIMEOUT_EXPIRED_KHR;3592} else {3593_eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");3594ret = EGL_FALSE;3595}3596}3597}3598}3599break;3600}3601dri2_egl_unref_sync(dri2_dpy, dri2_sync);36023603return ret;3604}36053606static EGLBoolean3607dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)3608{3609struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);3610EGLint ret;36113612if (sync->Type != EGL_SYNC_REUSABLE_KHR)3613return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");36143615if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)3616return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");36173618dri2_sync->base.SyncStatus = mode;36193620if (mode == EGL_SIGNALED_KHR) {3621ret = cnd_broadcast(&dri2_sync->cond);36223623/* fail to broadcast */3624if (ret)3625return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");3626}36273628return EGL_TRUE;3629}36303631static EGLint3632dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)3633{3634_EGLContext *ctx = _eglGetCurrentContext();3635struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3636struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);3637struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);36383639dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,3640dri2_sync->fence, 0);3641return EGL_TRUE;3642}36433644static int3645dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,3646struct mesa_glinterop_device_info *out)3647{3648struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3649struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);36503651if (!dri2_dpy->interop)3652return MESA_GLINTEROP_UNSUPPORTED;36533654return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);3655}36563657static int3658dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,3659struct mesa_glinterop_export_in *in,3660struct mesa_glinterop_export_out *out)3661{3662struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);3663struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);36643665if (!dri2_dpy->interop)3666return MESA_GLINTEROP_UNSUPPORTED;36673668return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);3669}36703671const _EGLDriver _eglDriver = {3672.Initialize = dri2_initialize,3673.Terminate = dri2_terminate,3674.CreateContext = dri2_create_context,3675.DestroyContext = dri2_destroy_context,3676.MakeCurrent = dri2_make_current,3677.CreateWindowSurface = dri2_create_window_surface,3678.CreatePixmapSurface = dri2_create_pixmap_surface,3679.CreatePbufferSurface = dri2_create_pbuffer_surface,3680.DestroySurface = dri2_destroy_surface,3681.GetProcAddress = dri2_get_proc_address,3682.WaitClient = dri2_wait_client,3683.WaitNative = dri2_wait_native,3684.BindTexImage = dri2_bind_tex_image,3685.ReleaseTexImage = dri2_release_tex_image,3686.SwapInterval = dri2_swap_interval,3687.SwapBuffers = dri2_swap_buffers,3688.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,3689.SwapBuffersRegionNOK = dri2_swap_buffers_region,3690.SetDamageRegion = dri2_set_damage_region,3691.PostSubBufferNV = dri2_post_sub_buffer,3692.CopyBuffers = dri2_copy_buffers,3693.QueryBufferAge = dri2_query_buffer_age,3694.CreateImageKHR = dri2_create_image,3695.DestroyImageKHR = dri2_destroy_image_khr,3696.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,3697.QuerySurface = dri2_query_surface,3698.QueryDriverName = dri2_query_driver_name,3699.QueryDriverConfig = dri2_query_driver_config,3700#ifdef HAVE_LIBDRM3701.CreateDRMImageMESA = dri2_create_drm_image_mesa,3702.ExportDRMImageMESA = dri2_export_drm_image_mesa,3703.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,3704.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,3705.QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,3706.QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,3707#endif3708#ifdef HAVE_WAYLAND_PLATFORM3709.BindWaylandDisplayWL = dri2_bind_wayland_display_wl,3710.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,3711.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,3712#endif3713.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,3714.CreateSyncKHR = dri2_create_sync,3715.ClientWaitSyncKHR = dri2_client_wait_sync,3716.SignalSyncKHR = dri2_signal_sync,3717.WaitSyncKHR = dri2_server_wait_sync,3718.DestroySyncKHR = dri2_destroy_sync,3719.GLInteropQueryDeviceInfo = dri2_interop_query_device_info,3720.GLInteropExportObject = dri2_interop_export_object,3721.DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,3722.SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,3723};372437253726