| 1 |
/* |
| 2 |
* Copyright (c) 2014-2016, 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 <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stddef.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <sys/socket.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <netdb.h> |
| 33 |
|
| 34 |
#include <wayland-server.h> |
| 35 |
#include <EGL/egl.h> |
| 36 |
#include <EGL/eglext.h> |
| 37 |
|
| 38 |
#include "wayland-eglstream-server.h" |
| 39 |
#include "wayland-eglstream-server-protocol.h" |
| 40 |
#include "wayland-eglstream.h" |
| 41 |
#include "wayland-drm.h" |
| 42 |
#include "wayland-eglswap.h" |
| 43 |
#include "wayland-eglutils.h" |
| 44 |
#include "wayland-thread.h" |
| 45 |
|
| 46 |
#define MASK(_VAL_) (1 << (_VAL_)) |
| 47 |
|
| 48 |
static struct wl_list wlStreamDpyList = WL_LIST_INITIALIZER(&wlStreamDpyList); |
| 49 |
|
| 50 |
static void |
| 51 |
destroy_wl_eglstream_resource(struct wl_resource *resource) |
| 52 |
{ |
| 53 |
struct wl_eglstream *wlStream = wl_resource_get_user_data(resource); |
| 54 |
|
| 55 |
if (wlStream->handle >= 0) { |
| 56 |
close(wlStream->handle); |
| 57 |
} |
| 58 |
|
| 59 |
free(wlStream); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
destroy_wl_eglstream(struct wl_client *client, struct wl_resource *resource) |
| 64 |
{ |
| 65 |
(void) client; |
| 66 |
wl_resource_destroy(resource); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
handle_create_stream(struct wl_client *client, |
| 71 |
struct wl_resource *resource, uint32_t id, |
| 72 |
int32_t width, int32_t height, |
| 73 |
int handle, int handle_type, |
| 74 |
struct wl_array *attribs) |
| 75 |
{ |
| 76 |
struct wl_eglstream_display *wlStreamDpy = |
| 77 |
wl_resource_get_user_data(resource); |
| 78 |
struct wl_eglstream *wlStream; |
| 79 |
struct sockaddr_in sockAddr; |
| 80 |
char sockAddrStr[NI_MAXHOST]; |
| 81 |
intptr_t *attr; |
| 82 |
int mask = 0; |
| 83 |
enum wl_eglstream_error err; |
| 84 |
|
| 85 |
wlStream = calloc(1, sizeof *wlStream); |
| 86 |
if (wlStream == NULL) { |
| 87 |
err = WL_EGLSTREAM_ERROR_BAD_ALLOC; |
| 88 |
goto error_create_stream; |
| 89 |
} |
| 90 |
|
| 91 |
wlStream->wlStreamDpy = wlStreamDpy; |
| 92 |
wlStream->eglStream = EGL_NO_STREAM_KHR; |
| 93 |
wlStream->width = width; |
| 94 |
wlStream->height = height; |
| 95 |
wlStream->handle = -1; |
| 96 |
wlStream->yInverted = EGL_FALSE; |
| 97 |
|
| 98 |
memset(&sockAddr, 0, sizeof(sockAddr)); |
| 99 |
|
| 100 |
switch (handle_type) { |
| 101 |
case WL_EGLSTREAM_HANDLE_TYPE_FD: |
| 102 |
wlStream->handle = handle; |
| 103 |
wlStream->fromFd = EGL_TRUE; |
| 104 |
break; |
| 105 |
|
| 106 |
case WL_EGLSTREAM_HANDLE_TYPE_INET: |
| 107 |
sockAddr.sin_family = AF_INET; |
| 108 |
wlStream->isInet = EGL_TRUE; |
| 109 |
/* Close the given dummy fd */ |
| 110 |
close(handle); |
| 111 |
break; |
| 112 |
|
| 113 |
case WL_EGLSTREAM_HANDLE_TYPE_SOCKET: |
| 114 |
wlStream->handle = handle; |
| 115 |
break; |
| 116 |
|
| 117 |
default: |
| 118 |
err = WL_EGLSTREAM_ERROR_BAD_HANDLE; |
| 119 |
goto error_create_stream; |
| 120 |
} |
| 121 |
|
| 122 |
wl_array_for_each(attr, attribs) { |
| 123 |
switch (attr[0]) { |
| 124 |
case WL_EGLSTREAM_ATTRIB_INET_ADDR: |
| 125 |
/* INET_ADDR should only be set once */ |
| 126 |
if (mask & MASK(WL_EGLSTREAM_ATTRIB_INET_ADDR)) { |
| 127 |
err = WL_EGLSTREAM_ERROR_BAD_ATTRIBS; |
| 128 |
goto error_create_stream; |
| 129 |
} |
| 130 |
sockAddr.sin_addr.s_addr = htonl((int)attr[1]); |
| 131 |
mask |= MASK(WL_EGLSTREAM_ATTRIB_INET_ADDR); |
| 132 |
break; |
| 133 |
|
| 134 |
case WL_EGLSTREAM_ATTRIB_INET_PORT: |
| 135 |
/* INET_PORT should only be set once */ |
| 136 |
if (mask & MASK(WL_EGLSTREAM_ATTRIB_INET_PORT)) { |
| 137 |
err = WL_EGLSTREAM_ERROR_BAD_ATTRIBS; |
| 138 |
goto error_create_stream; |
| 139 |
} |
| 140 |
sockAddr.sin_port = htons((int)attr[1]); |
| 141 |
mask |= MASK(WL_EGLSTREAM_ATTRIB_INET_PORT); |
| 142 |
break; |
| 143 |
|
| 144 |
case WL_EGLSTREAM_ATTRIB_Y_INVERTED: |
| 145 |
/* Y_INVERTED should only be set once */ |
| 146 |
if (mask & MASK(WL_EGLSTREAM_ATTRIB_Y_INVERTED)) { |
| 147 |
err = WL_EGLSTREAM_ERROR_BAD_ATTRIBS; |
| 148 |
goto error_create_stream; |
| 149 |
} |
| 150 |
wlStream->yInverted = (EGLBoolean)attr[1]; |
| 151 |
mask |= MASK(WL_EGLSTREAM_ATTRIB_Y_INVERTED); |
| 152 |
break; |
| 153 |
|
| 154 |
default: |
| 155 |
assert(!"Unknown attribute"); |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
/* Attribs processed in pairs */ |
| 160 |
attr++; |
| 161 |
} |
| 162 |
|
| 163 |
if (wlStream->isInet) { |
| 164 |
/* Both address and port should have been set */ |
| 165 |
if (mask != (MASK(WL_EGLSTREAM_ATTRIB_INET_ADDR) | |
| 166 |
MASK(WL_EGLSTREAM_ATTRIB_INET_PORT))) { |
| 167 |
err = WL_EGLSTREAM_ERROR_BAD_ATTRIBS; |
| 168 |
goto error_create_stream; |
| 169 |
} |
| 170 |
|
| 171 |
wlStream->handle = socket(AF_INET, SOCK_STREAM, 0); |
| 172 |
if (wlStream->handle == -1) { |
| 173 |
err = WL_EGLSTREAM_ERROR_BAD_ALLOC; |
| 174 |
goto error_create_stream; |
| 175 |
} |
| 176 |
|
| 177 |
if (connect(wlStream->handle, |
| 178 |
(struct sockaddr *)&sockAddr, |
| 179 |
sizeof(sockAddr)) < 0) { |
| 180 |
err = WL_EGLSTREAM_ERROR_BAD_ADDRESS; |
| 181 |
goto error_create_stream; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
wlStream->resource = |
| 186 |
wl_resource_create(client, &wl_buffer_interface, 1, id); |
| 187 |
if (!wlStream->resource) { |
| 188 |
err = WL_EGLSTREAM_ERROR_BAD_ALLOC; |
| 189 |
goto error_create_stream; |
| 190 |
} |
| 191 |
|
| 192 |
wl_resource_set_implementation( |
| 193 |
wlStream->resource, |
| 194 |
(void (**)(void))&wlStreamDpy->wl_eglstream_interface, |
| 195 |
wlStream, |
| 196 |
destroy_wl_eglstream_resource); |
| 197 |
return; |
| 198 |
|
| 199 |
error_create_stream: |
| 200 |
switch (err) { |
| 201 |
case WL_EGLSTREAM_ERROR_BAD_ALLOC: |
| 202 |
wl_resource_post_no_memory(resource); |
| 203 |
break; |
| 204 |
case WL_EGLSTREAM_ERROR_BAD_HANDLE: |
| 205 |
wl_resource_post_error(resource, err, "Invalid or unknown handle"); |
| 206 |
break; |
| 207 |
case WL_EGLSTREAM_ERROR_BAD_ATTRIBS: |
| 208 |
wl_resource_post_error(resource, err, "Malformed attributes list"); |
| 209 |
break; |
| 210 |
case WL_EGLSTREAM_ERROR_BAD_ADDRESS: |
| 211 |
wl_resource_post_error(resource, err, "Unable to connect to %s:%d.", |
| 212 |
(getnameinfo((struct sockaddr *)&sockAddr, |
| 213 |
sizeof(sockAddr), |
| 214 |
sockAddrStr, NI_MAXHOST, |
| 215 |
NULL, 0, |
| 216 |
NI_NUMERICHOST) ? |
| 217 |
"<invalid IP>" : sockAddrStr), |
| 218 |
ntohs(sockAddr.sin_port)); |
| 219 |
break; |
| 220 |
default: |
| 221 |
assert(!"Unknown error code"); |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
if (wlStream) { |
| 226 |
if (wlStream->isInet && wlStream->handle >= 0) { |
| 227 |
close(wlStream->handle); |
| 228 |
} |
| 229 |
free(wlStream); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
handle_swap_interval(struct wl_client *client, |
| 235 |
struct wl_resource *displayResource, |
| 236 |
struct wl_resource *streamResource, |
| 237 |
int interval) |
| 238 |
{ |
| 239 |
struct wl_eglstream_display *wlStreamDpy = |
| 240 |
wl_resource_get_user_data(displayResource); |
| 241 |
struct wl_eglstream *wlStream = |
| 242 |
wl_eglstream_display_get_stream(wlStreamDpy, streamResource); |
| 243 |
(void) client; |
| 244 |
|
| 245 |
if (wlEglStreamSwapIntervalCallback(wlStreamDpy->data, |
| 246 |
wlStream->eglStream, |
| 247 |
&interval) == EGL_BAD_MATCH) { |
| 248 |
wl_eglstream_display_send_swapinterval_override(displayResource, |
| 249 |
interval, |
| 250 |
streamResource); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
static const struct wl_eglstream_display_interface |
| 255 |
wl_eglstream_display_interface_impl = { |
| 256 |
handle_create_stream, |
| 257 |
handle_swap_interval, |
| 258 |
}; |
| 259 |
|
| 260 |
static void |
| 261 |
wl_eglstream_display_global_bind(struct wl_client *client, |
| 262 |
void *data, |
| 263 |
uint32_t version, |
| 264 |
uint32_t id) |
| 265 |
{ |
| 266 |
struct wl_eglstream_display *wlStreamDpy = NULL; |
| 267 |
struct wl_resource *resource = NULL; |
| 268 |
|
| 269 |
wlStreamDpy = (struct wl_eglstream_display *)data; |
| 270 |
resource = wl_resource_create(client, |
| 271 |
&wl_eglstream_display_interface, |
| 272 |
version, |
| 273 |
id); |
| 274 |
if (!resource) { |
| 275 |
wl_client_post_no_memory(client); |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
wl_resource_set_implementation(resource, |
| 280 |
&wl_eglstream_display_interface_impl, |
| 281 |
data, |
| 282 |
NULL); |
| 283 |
|
| 284 |
|
| 285 |
wl_eglstream_display_send_caps(resource, wlStreamDpy->supported_caps); |
| 286 |
} |
| 287 |
|
| 288 |
EGLBoolean |
| 289 |
wl_eglstream_display_bind(WlEglPlatformData *data, |
| 290 |
struct wl_display *wlDisplay, |
| 291 |
EGLDisplay eglDisplay, |
| 292 |
const char *exts, |
| 293 |
const char *dev_name) |
| 294 |
{ |
| 295 |
struct wl_eglstream_display *wlStreamDpy = NULL; |
| 296 |
char *env = NULL; |
| 297 |
|
| 298 |
/* Check whether there's an EGLDisplay already bound to the given |
| 299 |
* wl_display */ |
| 300 |
if (wl_eglstream_display_get(eglDisplay) != NULL) { |
| 301 |
return EGL_FALSE; |
| 302 |
} |
| 303 |
|
| 304 |
wlStreamDpy = calloc(1, sizeof(*wlStreamDpy)); |
| 305 |
if (!wlStreamDpy) { |
| 306 |
return EGL_FALSE; |
| 307 |
} |
| 308 |
|
| 309 |
wlStreamDpy->data = data; |
| 310 |
wlStreamDpy->wlDisplay = wlDisplay; |
| 311 |
wlStreamDpy->eglDisplay = eglDisplay; |
| 312 |
wlStreamDpy->caps_override = 0; |
| 313 |
|
| 314 |
#define CACHE_EXT(_PREFIX_, _NAME_) \ |
| 315 |
wlStreamDpy->exts._NAME_ = \ |
| 316 |
!!wlEglFindExtension("EGL_" #_PREFIX_ "_" #_NAME_, exts) |
| 317 |
|
| 318 |
CACHE_EXT(NV, stream_attrib); |
| 319 |
CACHE_EXT(KHR, stream_cross_process_fd); |
| 320 |
CACHE_EXT(NV, stream_remote); |
| 321 |
CACHE_EXT(NV, stream_socket); |
| 322 |
CACHE_EXT(NV, stream_socket_inet); |
| 323 |
CACHE_EXT(NV, stream_socket_unix); |
| 324 |
CACHE_EXT(NV, stream_origin); |
| 325 |
|
| 326 |
#undef CACHE_EXT |
| 327 |
|
| 328 |
/* Advertise server capabilities */ |
| 329 |
if (wlStreamDpy->exts.stream_cross_process_fd) { |
| 330 |
wlStreamDpy->supported_caps |= WL_EGLSTREAM_DISPLAY_CAP_STREAM_FD; |
| 331 |
} |
| 332 |
if (wlStreamDpy->exts.stream_attrib && |
| 333 |
wlStreamDpy->exts.stream_remote && |
| 334 |
wlStreamDpy->exts.stream_socket) { |
| 335 |
if (wlStreamDpy->exts.stream_socket_inet) { |
| 336 |
wlStreamDpy->supported_caps |= WL_EGLSTREAM_DISPLAY_CAP_STREAM_INET; |
| 337 |
} |
| 338 |
if (wlStreamDpy->exts.stream_socket_unix) { |
| 339 |
wlStreamDpy->supported_caps |= WL_EGLSTREAM_DISPLAY_CAP_STREAM_SOCKET; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
env = getenv("WL_EGLSTREAM_CAP_OVERRIDE"); |
| 344 |
if (env) { |
| 345 |
int serverCapOverride = atoi(env); |
| 346 |
wlStreamDpy->caps_override = (wlStreamDpy->supported_caps |
| 347 |
& serverCapOverride) != |
| 348 |
wlStreamDpy->supported_caps; |
| 349 |
wlStreamDpy->supported_caps &= serverCapOverride; |
| 350 |
} |
| 351 |
|
| 352 |
wlStreamDpy->wl_eglstream_interface.destroy = destroy_wl_eglstream; |
| 353 |
wlStreamDpy->global = wl_global_create(wlDisplay, |
| 354 |
&wl_eglstream_display_interface, 1, |
| 355 |
wlStreamDpy, |
| 356 |
wl_eglstream_display_global_bind); |
| 357 |
|
| 358 |
/* Failure is not fatal */ |
| 359 |
wl_drm_display_bind(wlDisplay, wlStreamDpy, dev_name); |
| 360 |
|
| 361 |
wl_list_insert(&wlStreamDpyList, &wlStreamDpy->link); |
| 362 |
|
| 363 |
return EGL_TRUE; |
| 364 |
} |
| 365 |
|
| 366 |
void |
| 367 |
wl_eglstream_display_unbind(struct wl_eglstream_display *wlStreamDpy) |
| 368 |
{ |
| 369 |
wl_drm_display_unbind(wlStreamDpy); |
| 370 |
wl_global_destroy(wlStreamDpy->global); |
| 371 |
wl_list_remove(&wlStreamDpy->link); |
| 372 |
free(wlStreamDpy); |
| 373 |
} |
| 374 |
|
| 375 |
struct wl_eglstream_display* wl_eglstream_display_get(EGLDisplay eglDisplay) |
| 376 |
{ |
| 377 |
struct wl_eglstream_display *wlDisplay; |
| 378 |
|
| 379 |
wl_list_for_each(wlDisplay, &wlStreamDpyList, link) { |
| 380 |
if (wlDisplay->eglDisplay == eglDisplay) { |
| 381 |
return wlDisplay; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
struct wl_eglstream* |
| 389 |
wl_eglstream_display_get_stream(struct wl_eglstream_display *wlStreamDpy, |
| 390 |
struct wl_resource *resource) |
| 391 |
{ |
| 392 |
if (resource == NULL) { |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
|
| 396 |
if (wl_resource_instance_of(resource, &wl_buffer_interface, |
| 397 |
&wlStreamDpy->wl_eglstream_interface)) { |
| 398 |
return wl_resource_get_user_data(resource); |
| 399 |
} else { |
| 400 |
return NULL; |
| 401 |
} |
| 402 |
} |
| 403 |
|