| 1 |
/* |
| 2 |
* Copyright (c) 2014-2022, NVIDIA CORPORATION. All rights reserved. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "wayland-eglhandle.h" |
| 24 |
#include "wayland-egldisplay.h" |
| 25 |
#include "wayland-eglsurface-internal.h" |
| 26 |
#include "wayland-thread.h" |
| 27 |
#include <stdlib.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <dlfcn.h> |
| 31 |
|
| 32 |
WlEglPlatformData* |
| 33 |
wlEglCreatePlatformData(int apiMajor, int apiMinor, const EGLExtDriver *driver) |
| 34 |
{ |
| 35 |
const char *exts = NULL; |
| 36 |
WlEglPlatformData *res = NULL; |
| 37 |
|
| 38 |
assert((driver != NULL) && (driver->getProcAddress != NULL)); |
| 39 |
|
| 40 |
/* Allocate platform data and fetch EGL functions */ |
| 41 |
res = calloc(1, sizeof(WlEglPlatformData)); |
| 42 |
if (res == NULL) { |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
wl_list_init(&res->deviceDpyList); |
| 47 |
|
| 48 |
/* Cache the EGL driver version */ |
| 49 |
#if EGL_EXTERNAL_PLATFORM_HAS(DRIVER_VERSION) |
| 50 |
if (EGL_EXTERNAL_PLATFORM_SUPPORTS(apiMajor, apiMinor, DRIVER_VERSION)) { |
| 51 |
res->egl.major = driver->major; |
| 52 |
res->egl.minor = driver->minor; |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
/* Fetch all required driver functions */ |
| 57 |
#define GET_PROC(_FIELD_, _NAME_) \ |
| 58 |
do { \ |
| 59 |
res->egl._FIELD_ = driver->getProcAddress(#_NAME_); \ |
| 60 |
if (res->egl._FIELD_ == NULL) { \ |
| 61 |
goto fail; \ |
| 62 |
} \ |
| 63 |
} while (0) |
| 64 |
|
| 65 |
/* Core and basic stream functionality */ |
| 66 |
GET_PROC(queryString, eglQueryString); |
| 67 |
GET_PROC(queryDevices, eglQueryDevicesEXT); |
| 68 |
|
| 69 |
/* TODO: use eglGetPlatformDisplay instead of eglGetPlatformDisplayEXT |
| 70 |
if EGL 1.5 is available */ |
| 71 |
GET_PROC(getPlatformDisplay, eglGetPlatformDisplayEXT); |
| 72 |
GET_PROC(initialize, eglInitialize); |
| 73 |
GET_PROC(terminate, eglTerminate); |
| 74 |
GET_PROC(chooseConfig, eglChooseConfig); |
| 75 |
GET_PROC(getConfigAttrib, eglGetConfigAttrib); |
| 76 |
GET_PROC(querySurface, eglQuerySurface); |
| 77 |
|
| 78 |
GET_PROC(getCurrentContext, eglGetCurrentContext); |
| 79 |
GET_PROC(getCurrentSurface, eglGetCurrentSurface); |
| 80 |
GET_PROC(makeCurrent, eglMakeCurrent); |
| 81 |
|
| 82 |
GET_PROC(createStream, eglCreateStreamKHR); |
| 83 |
GET_PROC(createStreamFromFD, eglCreateStreamFromFileDescriptorKHR); |
| 84 |
GET_PROC(createStreamAttrib, eglCreateStreamAttribNV); |
| 85 |
GET_PROC(getStreamFileDescriptor, eglGetStreamFileDescriptorKHR); |
| 86 |
GET_PROC(createStreamProducerSurface, eglCreateStreamProducerSurfaceKHR); |
| 87 |
GET_PROC(createPbufferSurface, eglCreatePbufferSurface); |
| 88 |
GET_PROC(destroyStream, eglDestroyStreamKHR); |
| 89 |
GET_PROC(destroySurface, eglDestroySurface); |
| 90 |
|
| 91 |
GET_PROC(swapBuffers, eglSwapBuffers); |
| 92 |
GET_PROC(swapBuffersWithDamage, eglSwapBuffersWithDamageKHR); |
| 93 |
GET_PROC(swapInterval, eglSwapInterval); |
| 94 |
|
| 95 |
GET_PROC(getError, eglGetError); |
| 96 |
GET_PROC(releaseThread, eglReleaseThread); |
| 97 |
|
| 98 |
/* From EGL_EXT_device_query, used by the wayland-drm implementation */ |
| 99 |
GET_PROC(queryDisplayAttrib, eglQueryDisplayAttribEXT); |
| 100 |
GET_PROC(queryDeviceString, eglQueryDeviceStringEXT); |
| 101 |
|
| 102 |
#undef GET_PROC |
| 103 |
|
| 104 |
/* Fetch all optional driver functions */ |
| 105 |
#define GET_PROC(_FIELD_, _NAME_) \ |
| 106 |
res->egl._FIELD_ = driver->getProcAddress(#_NAME_) |
| 107 |
|
| 108 |
/* Used by damage thread */ |
| 109 |
GET_PROC(queryStream, eglQueryStreamKHR); |
| 110 |
GET_PROC(queryStreamu64, eglQueryStreamu64KHR); |
| 111 |
GET_PROC(createStreamSync, eglCreateStreamSyncNV); |
| 112 |
GET_PROC(clientWaitSync, eglClientWaitSyncKHR); |
| 113 |
GET_PROC(signalSync, eglSignalSyncKHR); |
| 114 |
GET_PROC(destroySync, eglDestroySyncKHR); |
| 115 |
GET_PROC(createSync, eglCreateSyncKHR); |
| 116 |
GET_PROC(dupNativeFenceFD, eglDupNativeFenceFDANDROID); |
| 117 |
|
| 118 |
/* Stream flush */ |
| 119 |
GET_PROC(streamFlush, eglStreamFlushNV); |
| 120 |
|
| 121 |
/* EGLImage Stream consumer and dependencies */ |
| 122 |
GET_PROC(streamImageConsumerConnect, eglStreamImageConsumerConnectNV); |
| 123 |
GET_PROC(streamAcquireImage, eglStreamAcquireImageNV); |
| 124 |
GET_PROC(streamReleaseImage, eglStreamReleaseImageNV); |
| 125 |
GET_PROC(queryStreamConsumerEvent, eglQueryStreamConsumerEventNV); |
| 126 |
GET_PROC(exportDMABUFImage, eglExportDMABUFImageMESA); |
| 127 |
GET_PROC(exportDMABUFImageQuery, eglExportDMABUFImageQueryMESA); |
| 128 |
GET_PROC(createImage, eglCreateImageKHR); |
| 129 |
GET_PROC(destroyImage, eglDestroyImageKHR); |
| 130 |
|
| 131 |
#undef GET_PROC |
| 132 |
|
| 133 |
/* Check for required EGL client extensions */ |
| 134 |
exts = res->egl.queryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
| 135 |
if (exts == NULL) { |
| 136 |
goto fail; |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* Note EGL_EXT_platform_device implies support for EGL_EXT_device_base, |
| 141 |
* which is equivalent to the combination of EGL_EXT_device_query and |
| 142 |
* EGL_EXT_device_enumeration. The wayland-drm implementation assumes |
| 143 |
* EGL_EXT_device_query is supported based on this check. |
| 144 |
*/ |
| 145 |
if (!wlEglFindExtension("EGL_EXT_platform_base", exts) || |
| 146 |
!wlEglFindExtension("EGL_EXT_platform_device", exts)) { |
| 147 |
goto fail; |
| 148 |
} |
| 149 |
res->supportsDisplayReference = wlEglFindExtension("EGL_KHR_display_reference", exts); |
| 150 |
|
| 151 |
/* Cache driver imports */ |
| 152 |
res->callbacks.setError = driver->setError; |
| 153 |
res->callbacks.streamSwapInterval = driver->streamSwapInterval; |
| 154 |
|
| 155 |
/* |
| 156 |
* try to find drmGetDeviceFromDevId. First try the default search method, |
| 157 |
* but certain application tricks may interfere with this. Most notably |
| 158 |
* steam's overlay. If we can't find it through default methods fall back |
| 159 |
* to directly opening libdrm. |
| 160 |
*/ |
| 161 |
res->getDeviceFromDevId = dlsym(RTLD_DEFAULT, "drmGetDeviceFromDevId"); |
| 162 |
if (!res->getDeviceFromDevId) { |
| 163 |
res->libdrmDlHandle = dlopen("libdrm.so.2", RTLD_LAZY); |
| 164 |
res->getDeviceFromDevId = dlsym(res->libdrmDlHandle, "drmGetDeviceFromDevId"); |
| 165 |
} |
| 166 |
|
| 167 |
return res; |
| 168 |
|
| 169 |
fail: |
| 170 |
free(res); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
void wlEglDestroyPlatformData(WlEglPlatformData *data) |
| 175 |
{ |
| 176 |
if (data->libdrmDlHandle) { |
| 177 |
dlclose(data->libdrmDlHandle); |
| 178 |
} |
| 179 |
free(data); |
| 180 |
} |
| 181 |
|
| 182 |
void* wlEglGetInternalHandleExport(EGLDisplay dpy, EGLenum type, void *handle) |
| 183 |
{ |
| 184 |
WlEglDisplay *display; |
| 185 |
if (type == EGL_OBJECT_DISPLAY_KHR) { |
| 186 |
display = wlEglAcquireDisplay(handle); |
| 187 |
if (display) { |
| 188 |
handle = (void *)display->devDpy->eglDisplay; |
| 189 |
wlEglReleaseDisplay(display); |
| 190 |
} |
| 191 |
} else if (type == EGL_OBJECT_SURFACE_KHR) { |
| 192 |
display = wlEglAcquireDisplay(dpy); |
| 193 |
if (display) { |
| 194 |
pthread_mutex_lock(&display->mutex); |
| 195 |
if (wlEglIsWlEglSurfaceForDisplay(display, (WlEglSurface *)handle)) { |
| 196 |
handle = (void *)(((WlEglSurface *)handle)->ctx.eglSurface); |
| 197 |
} |
| 198 |
pthread_mutex_unlock(&display->mutex); |
| 199 |
wlEglReleaseDisplay(dpy); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return handle; |
| 204 |
} |