| 1 |
/* |
| 2 |
* Copyright (c) 2014-2019, 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-egldevice.h" |
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include "wayland-eglhandle.h" |
| 33 |
#include "wayland-eglutils.h" |
| 34 |
|
| 35 |
WlEglDeviceDpy *wlGetInternalDisplay(WlEglPlatformData *data, EGLDeviceEXT device) |
| 36 |
{ |
| 37 |
static const EGLint TRACK_REFS_ATTRIBS[] = { |
| 38 |
EGL_TRACK_REFERENCES_KHR, |
| 39 |
EGL_TRUE, |
| 40 |
EGL_NONE |
| 41 |
}; |
| 42 |
|
| 43 |
WlEglDeviceDpy *devDpy = NULL; |
| 44 |
const EGLint *attribs = NULL; |
| 45 |
const char *drmName = NULL, *renderName = NULL; |
| 46 |
struct stat sb, render_sb; |
| 47 |
|
| 48 |
// First, see if we've already created an EGLDisplay for this device. |
| 49 |
wl_list_for_each(devDpy, &data->deviceDpyList, link) { |
| 50 |
if (devDpy->data == data && devDpy->eglDevice == device) { |
| 51 |
return devDpy; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// We didn't find a matching display, so create one. |
| 56 |
if (data->supportsDisplayReference) { |
| 57 |
// Always use EGL_KHR_display_reference if the driver supports it. |
| 58 |
// We'll do our own refcounting so that we can work without it, but |
| 59 |
// setting EGL_TRACK_REFERENCES_KHR means that it's less likely that |
| 60 |
// something else might grab the same EGLDevice-based display and |
| 61 |
// call eglTerminate on it. |
| 62 |
attribs = TRACK_REFS_ATTRIBS; |
| 63 |
} |
| 64 |
|
| 65 |
devDpy = calloc(1, sizeof(WlEglDeviceDpy)); |
| 66 |
if (devDpy == NULL) { |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
devDpy->eglDevice = device; |
| 71 |
devDpy->data = data; |
| 72 |
devDpy->eglDisplay = data->egl.getPlatformDisplay(EGL_PLATFORM_DEVICE_EXT, |
| 73 |
device, attribs); |
| 74 |
if (devDpy->eglDisplay == EGL_NO_DISPLAY) { |
| 75 |
goto fail; |
| 76 |
} |
| 77 |
|
| 78 |
/* Get the device in use, |
| 79 |
* calling eglQueryDeviceStringEXT(EGL_DRM_RENDER_NODE_FILE_EXT) to get drm fd. |
| 80 |
* We will be getting the dev_t for the render node and the normal node, since |
| 81 |
* we don't know for sure which one the compositor will happen to use. |
| 82 |
*/ |
| 83 |
drmName = data->egl.queryDeviceString(devDpy->eglDevice, |
| 84 |
EGL_DRM_DEVICE_FILE_EXT); |
| 85 |
if (!drmName) { |
| 86 |
goto fail; |
| 87 |
} |
| 88 |
/* Then use stat to get the dev_t for this device */ |
| 89 |
if (stat(drmName, &sb) != 0) { |
| 90 |
goto fail; |
| 91 |
} |
| 92 |
|
| 93 |
renderName = data->egl.queryDeviceString(devDpy->eglDevice, |
| 94 |
EGL_DRM_RENDER_NODE_FILE_EXT); |
| 95 |
if (!renderName) { |
| 96 |
goto fail; |
| 97 |
} |
| 98 |
if (stat(renderName, &render_sb) != 0) { |
| 99 |
goto fail; |
| 100 |
} |
| 101 |
|
| 102 |
devDpy->dev = sb.st_rdev; |
| 103 |
devDpy->renderNode = render_sb.st_rdev; |
| 104 |
|
| 105 |
wl_list_insert(&data->deviceDpyList, &devDpy->link); |
| 106 |
return devDpy; |
| 107 |
|
| 108 |
fail: |
| 109 |
free(devDpy); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
static void wlFreeInternalDisplay(WlEglDeviceDpy *devDpy) |
| 114 |
{ |
| 115 |
if (devDpy->initCount > 0) { |
| 116 |
devDpy->data->egl.terminate(devDpy->eglDisplay); |
| 117 |
} |
| 118 |
wl_list_remove(&devDpy->link); |
| 119 |
free(devDpy); |
| 120 |
} |
| 121 |
|
| 122 |
void wlFreeAllInternalDisplays(WlEglPlatformData *data) |
| 123 |
{ |
| 124 |
WlEglDeviceDpy *devDpy, *devNext; |
| 125 |
wl_list_for_each_safe(devDpy, devNext, &data->deviceDpyList, link) { |
| 126 |
assert (devDpy->data == data); |
| 127 |
wlFreeInternalDisplay(devDpy); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
EGLBoolean wlInternalInitialize(WlEglDeviceDpy *devDpy) |
| 132 |
{ |
| 133 |
if (devDpy->initCount == 0) { |
| 134 |
const char *exts; |
| 135 |
|
| 136 |
if (!devDpy->data->egl.initialize(devDpy->eglDisplay, &devDpy->major, &devDpy->minor)) { |
| 137 |
return EGL_FALSE; |
| 138 |
} |
| 139 |
|
| 140 |
exts = devDpy->data->egl.queryString(devDpy->eglDisplay, EGL_EXTENSIONS); |
| 141 |
#define CACHE_EXT(_PREFIX_, _NAME_) \ |
| 142 |
devDpy->exts._NAME_ = \ |
| 143 |
!!wlEglFindExtension("EGL_" #_PREFIX_ "_" #_NAME_, exts) |
| 144 |
|
| 145 |
CACHE_EXT(KHR, stream); |
| 146 |
CACHE_EXT(NV, stream_attrib); |
| 147 |
CACHE_EXT(KHR, stream_cross_process_fd); |
| 148 |
CACHE_EXT(NV, stream_remote); |
| 149 |
CACHE_EXT(KHR, stream_producer_eglsurface); |
| 150 |
CACHE_EXT(NV, stream_fifo_synchronous); |
| 151 |
CACHE_EXT(NV, stream_sync); |
| 152 |
CACHE_EXT(NV, stream_flush); |
| 153 |
CACHE_EXT(NV, stream_consumer_eglimage); |
| 154 |
CACHE_EXT(MESA, image_dma_buf_export); |
| 155 |
|
| 156 |
#undef CACHE_EXT |
| 157 |
} |
| 158 |
|
| 159 |
devDpy->initCount++; |
| 160 |
return EGL_TRUE; |
| 161 |
} |
| 162 |
|
| 163 |
EGLBoolean wlInternalTerminate(WlEglDeviceDpy *devDpy) |
| 164 |
{ |
| 165 |
if (devDpy->initCount > 0) { |
| 166 |
if (devDpy->initCount == 1) { |
| 167 |
if (!devDpy->data->egl.terminate(devDpy->eglDisplay)) { |
| 168 |
return EGL_FALSE; |
| 169 |
} |
| 170 |
} |
| 171 |
devDpy->initCount--; |
| 172 |
} |
| 173 |
return EGL_TRUE; |
| 174 |
} |