| 1 |
/* |
| 2 |
* Copyright (c) 2021, 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 <stdlib.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include <wayland-server.h> |
| 27 |
#include "wayland-eglstream-server.h" |
| 28 |
#include "wayland-drm.h" |
| 29 |
#include "wayland-eglutils.h" |
| 30 |
#include "wayland-drm-server-protocol.h" |
| 31 |
|
| 32 |
static void |
| 33 |
authenticate(struct wl_client *client, |
| 34 |
struct wl_resource *resource, uint32_t id) |
| 35 |
{ |
| 36 |
(void)client; |
| 37 |
(void)id; |
| 38 |
|
| 39 |
/* |
| 40 |
* This implementation will only ever report render node files, and |
| 41 |
* authentication is not supported nor required for render node devices. |
| 42 |
*/ |
| 43 |
wl_resource_post_error(resource, |
| 44 |
WL_DRM_ERROR_AUTHENTICATE_FAIL, |
| 45 |
"authenticate failed"); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
create_buffer(struct wl_client *client, struct wl_resource *resource, |
| 50 |
uint32_t id, uint32_t name, int32_t width, int32_t height, |
| 51 |
uint32_t stride, uint32_t format) |
| 52 |
{ |
| 53 |
(void)client; |
| 54 |
(void)id; |
| 55 |
(void)name; |
| 56 |
(void)width; |
| 57 |
(void)height; |
| 58 |
(void)stride; |
| 59 |
(void)format; |
| 60 |
|
| 61 |
wl_resource_post_error(resource, |
| 62 |
WL_DRM_ERROR_INVALID_FORMAT, |
| 63 |
"invalid format"); |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
create_planar_buffer(struct wl_client *client, struct wl_resource *resource, |
| 68 |
uint32_t id, uint32_t name, int32_t width, int32_t height, |
| 69 |
uint32_t format, |
| 70 |
int32_t offset0, int32_t stride0, |
| 71 |
int32_t offset1, int32_t stride1, |
| 72 |
int32_t offset2, int32_t stride2) |
| 73 |
{ |
| 74 |
(void)client; |
| 75 |
(void)id; |
| 76 |
(void)name; |
| 77 |
(void)width; |
| 78 |
(void)height; |
| 79 |
(void)format; |
| 80 |
(void)offset0; |
| 81 |
(void)stride0; |
| 82 |
(void)offset1; |
| 83 |
(void)stride1; |
| 84 |
(void)offset2; |
| 85 |
(void)stride2; |
| 86 |
|
| 87 |
wl_resource_post_error(resource, |
| 88 |
WL_DRM_ERROR_INVALID_FORMAT, |
| 89 |
"invalid format"); |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
create_prime_buffer(struct wl_client *client, struct wl_resource *resource, |
| 94 |
uint32_t id, int fd, int32_t width, int32_t height, |
| 95 |
uint32_t format, |
| 96 |
int32_t offset0, int32_t stride0, |
| 97 |
int32_t offset1, int32_t stride1, |
| 98 |
int32_t offset2, int32_t stride2) |
| 99 |
{ |
| 100 |
(void)client; |
| 101 |
(void)id; |
| 102 |
(void)fd; |
| 103 |
(void)width; |
| 104 |
(void)height; |
| 105 |
(void)format; |
| 106 |
(void)offset0; |
| 107 |
(void)stride0; |
| 108 |
(void)offset1; |
| 109 |
(void)stride1; |
| 110 |
(void)offset2; |
| 111 |
(void)stride2; |
| 112 |
|
| 113 |
wl_resource_post_error(resource, |
| 114 |
WL_DRM_ERROR_INVALID_FORMAT, |
| 115 |
"invalid format"); |
| 116 |
close(fd); |
| 117 |
} |
| 118 |
|
| 119 |
static const struct wl_drm_interface interface = { |
| 120 |
authenticate, |
| 121 |
create_buffer, |
| 122 |
create_planar_buffer, |
| 123 |
create_prime_buffer, |
| 124 |
}; |
| 125 |
|
| 126 |
static void |
| 127 |
bind(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 128 |
{ |
| 129 |
struct wl_eglstream_display *wlStreamDpy = data; |
| 130 |
struct wl_resource *resource = wl_resource_create(client, &wl_drm_interface, |
| 131 |
version > 2 ? 2 : version, |
| 132 |
id); |
| 133 |
|
| 134 |
if (!resource) { |
| 135 |
wl_client_post_no_memory(client); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
wl_resource_set_implementation(resource, &interface, data, NULL); |
| 140 |
wl_resource_post_event(resource, WL_DRM_DEVICE, wlStreamDpy->drm->device_name); |
| 141 |
|
| 142 |
/* |
| 143 |
* Don't send any format events. This implementation doesn't support |
| 144 |
* creating Wayland buffers of any format. |
| 145 |
*/ |
| 146 |
|
| 147 |
/* |
| 148 |
* Don't report any capabilities beyond the baseline, as currently all |
| 149 |
* capabilities are only relevant when buffer creation is supported. |
| 150 |
*/ |
| 151 |
if (version >= 2) |
| 152 |
wl_resource_post_event(resource, WL_DRM_CAPABILITIES, 0); |
| 153 |
} |
| 154 |
|
| 155 |
const char * |
| 156 |
wl_drm_get_dev_name(const WlEglPlatformData *data, |
| 157 |
EGLDisplay dpy) |
| 158 |
{ |
| 159 |
EGLDeviceEXT egl_dev; |
| 160 |
const char *dev_exts; |
| 161 |
|
| 162 |
if (!data->egl.queryDisplayAttrib(dpy, EGL_DEVICE_EXT, |
| 163 |
(EGLAttribKHR*)&egl_dev)) { |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
dev_exts = data->egl.queryDeviceString(egl_dev, EGL_EXTENSIONS); |
| 168 |
|
| 169 |
if (!dev_exts) { |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
if (!wlEglFindExtension("EGL_EXT_device_drm_render_node", dev_exts)) { |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
|
| 177 |
return data->egl.queryDeviceString(egl_dev, EGL_DRM_RENDER_NODE_FILE_EXT); |
| 178 |
} |
| 179 |
|
| 180 |
EGLBoolean |
| 181 |
wl_drm_display_bind(struct wl_display *display, |
| 182 |
struct wl_eglstream_display *wlStreamDpy, |
| 183 |
const char *dev_name) |
| 184 |
{ |
| 185 |
if (!dev_name) { |
| 186 |
return EGL_FALSE; |
| 187 |
} |
| 188 |
|
| 189 |
wlStreamDpy->drm = calloc(1, sizeof *wlStreamDpy->drm); |
| 190 |
|
| 191 |
if (!wlStreamDpy->drm) { |
| 192 |
return EGL_FALSE; |
| 193 |
} |
| 194 |
|
| 195 |
wlStreamDpy->drm->device_name = dev_name; |
| 196 |
wlStreamDpy->drm->global = wl_global_create(display, &wl_drm_interface, 2, |
| 197 |
wlStreamDpy, bind); |
| 198 |
|
| 199 |
return EGL_TRUE; |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
wl_drm_display_unbind(struct wl_eglstream_display *wlStreamDpy) |
| 204 |
{ |
| 205 |
if (wlStreamDpy->drm) { |
| 206 |
wl_global_destroy(wlStreamDpy->drm->global); |
| 207 |
free(wlStreamDpy->drm); |
| 208 |
wlStreamDpy->drm = NULL; |
| 209 |
} |
| 210 |
} |