| 1 |
/* |
| 2 |
* Copyright © 2011 Kristian Høgsberg |
| 3 |
* Copyright © 2011 Benjamin Franzke |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 |
* DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* Authors: |
| 26 |
* Kristian Høgsberg <[email protected]> |
| 27 |
* Benjamin Franzke <[email protected]> |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <stddef.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
#include "EGL/egl.h" |
| 37 |
#include "EGL/eglext.h" |
| 38 |
|
| 39 |
#include "wayland-drm-server-protocol.h" |
| 40 |
#include "wayland-drm.h" |
| 41 |
#include <wayland-server.h> |
| 42 |
|
| 43 |
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 44 |
|
| 45 |
static void |
| 46 |
destroy_buffer(struct wl_resource *resource) |
| 47 |
{ |
| 48 |
struct wl_drm_buffer *buffer = wl_resource_get_user_data(resource); |
| 49 |
struct wl_drm *drm = buffer->drm; |
| 50 |
|
| 51 |
drm->callbacks.release_buffer(drm->user_data, buffer); |
| 52 |
free(buffer); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
buffer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 57 |
{ |
| 58 |
wl_resource_destroy(resource); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
drm_create_buffer(struct wl_client *client, struct wl_resource *resource, |
| 63 |
uint32_t id, uint32_t name, int32_t width, int32_t height, |
| 64 |
uint32_t stride, uint32_t format) |
| 65 |
{ |
| 66 |
wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME, |
| 67 |
"global GEM names are no longer supported"); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
drm_create_planar_buffer(struct wl_client *client, struct wl_resource *resource, |
| 72 |
uint32_t id, uint32_t name, int32_t width, |
| 73 |
int32_t height, uint32_t format, int32_t offset0, |
| 74 |
int32_t stride0, int32_t offset1, int32_t stride1, |
| 75 |
int32_t offset2, int32_t stride2) |
| 76 |
{ |
| 77 |
wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME, |
| 78 |
"global GEM names are no longer supported"); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
drm_create_prime_buffer(struct wl_client *client, struct wl_resource *resource, |
| 83 |
uint32_t id, int fd, int32_t width, int32_t height, |
| 84 |
uint32_t format, int32_t offset0, int32_t stride0, |
| 85 |
int32_t offset1, int32_t stride1, int32_t offset2, |
| 86 |
int32_t stride2) |
| 87 |
{ |
| 88 |
struct wl_drm *drm = wl_resource_get_user_data(resource); |
| 89 |
struct wl_drm_buffer *buffer; |
| 90 |
|
| 91 |
buffer = calloc(1, sizeof *buffer); |
| 92 |
if (buffer == NULL) { |
| 93 |
wl_resource_post_no_memory(resource); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
buffer->drm = drm; |
| 98 |
buffer->width = width; |
| 99 |
buffer->height = height; |
| 100 |
buffer->format = format; |
| 101 |
buffer->offset[0] = offset0; |
| 102 |
buffer->stride[0] = stride0; |
| 103 |
buffer->offset[1] = offset1; |
| 104 |
buffer->stride[1] = stride1; |
| 105 |
buffer->offset[2] = offset2; |
| 106 |
buffer->stride[2] = stride2; |
| 107 |
|
| 108 |
switch (format) { |
| 109 |
case WL_DRM_FORMAT_ARGB2101010: |
| 110 |
case WL_DRM_FORMAT_ABGR2101010: |
| 111 |
case WL_DRM_FORMAT_ARGB8888: |
| 112 |
buffer->egl_components = EGL_TEXTURE_RGBA; |
| 113 |
break; |
| 114 |
case WL_DRM_FORMAT_XRGB2101010: |
| 115 |
case WL_DRM_FORMAT_XBGR2101010: |
| 116 |
case WL_DRM_FORMAT_XRGB8888: |
| 117 |
case WL_DRM_FORMAT_BGR888: |
| 118 |
case WL_DRM_FORMAT_RGB888: |
| 119 |
case WL_DRM_FORMAT_RGB565: |
| 120 |
buffer->egl_components = EGL_TEXTURE_RGB; |
| 121 |
break; |
| 122 |
case WL_DRM_FORMAT_YUV410: |
| 123 |
case WL_DRM_FORMAT_YUV411: |
| 124 |
case WL_DRM_FORMAT_YUV420: |
| 125 |
case WL_DRM_FORMAT_YUV422: |
| 126 |
case WL_DRM_FORMAT_YUV444: |
| 127 |
buffer->egl_components = EGL_TEXTURE_Y_U_V_WL; |
| 128 |
break; |
| 129 |
case WL_DRM_FORMAT_NV12: |
| 130 |
case WL_DRM_FORMAT_NV16: |
| 131 |
buffer->egl_components = EGL_TEXTURE_Y_UV_WL; |
| 132 |
break; |
| 133 |
case WL_DRM_FORMAT_YUYV: |
| 134 |
buffer->egl_components = EGL_TEXTURE_Y_XUXV_WL; |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
drm->callbacks.reference_buffer(drm->user_data, fd, buffer); |
| 139 |
close(fd); |
| 140 |
if (buffer->driver_buffer == NULL) { |
| 141 |
wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME, |
| 142 |
"invalid name"); |
| 143 |
free(buffer); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id); |
| 148 |
if (!buffer->resource) { |
| 149 |
wl_resource_post_no_memory(resource); |
| 150 |
free(buffer); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
wl_resource_set_implementation(buffer->resource, |
| 155 |
(void (**)(void)) & drm->buffer_interface, |
| 156 |
buffer, destroy_buffer); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
drm_authenticate(struct wl_client *client, struct wl_resource *resource, |
| 161 |
uint32_t id) |
| 162 |
{ |
| 163 |
struct wl_drm *drm = wl_resource_get_user_data(resource); |
| 164 |
|
| 165 |
if (!drm->callbacks.authenticate || |
| 166 |
drm->callbacks.authenticate(drm->user_data, id) < 0) |
| 167 |
wl_resource_post_error(resource, WL_DRM_ERROR_AUTHENTICATE_FAIL, |
| 168 |
"authenticate failed"); |
| 169 |
else |
| 170 |
wl_resource_post_event(resource, WL_DRM_AUTHENTICATED); |
| 171 |
} |
| 172 |
|
| 173 |
static const struct wl_drm_interface drm_interface = { |
| 174 |
drm_authenticate, |
| 175 |
drm_create_buffer, |
| 176 |
drm_create_planar_buffer, |
| 177 |
drm_create_prime_buffer, |
| 178 |
}; |
| 179 |
|
| 180 |
static void |
| 181 |
bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 182 |
{ |
| 183 |
struct wl_drm *drm = data; |
| 184 |
struct wl_resource *resource; |
| 185 |
|
| 186 |
resource = |
| 187 |
wl_resource_create(client, &wl_drm_interface, MIN(version, 2), id); |
| 188 |
if (!resource) { |
| 189 |
wl_client_post_no_memory(client); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
wl_resource_set_implementation(resource, &drm_interface, data, NULL); |
| 194 |
|
| 195 |
wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name); |
| 196 |
|
| 197 |
if (drm->callbacks.is_format_supported(drm->user_data, |
| 198 |
WL_DRM_FORMAT_ARGB2101010)) { |
| 199 |
wl_resource_post_event(resource, WL_DRM_FORMAT, |
| 200 |
WL_DRM_FORMAT_ARGB2101010); |
| 201 |
} |
| 202 |
|
| 203 |
if (drm->callbacks.is_format_supported(drm->user_data, |
| 204 |
WL_DRM_FORMAT_XRGB2101010)) { |
| 205 |
wl_resource_post_event(resource, WL_DRM_FORMAT, |
| 206 |
WL_DRM_FORMAT_XRGB2101010); |
| 207 |
} |
| 208 |
|
| 209 |
if (drm->callbacks.is_format_supported(drm->user_data, |
| 210 |
WL_DRM_FORMAT_ABGR2101010)) { |
| 211 |
wl_resource_post_event(resource, WL_DRM_FORMAT, |
| 212 |
WL_DRM_FORMAT_ABGR2101010); |
| 213 |
} |
| 214 |
|
| 215 |
if (drm->callbacks.is_format_supported(drm->user_data, |
| 216 |
WL_DRM_FORMAT_XBGR2101010)) { |
| 217 |
wl_resource_post_event(resource, WL_DRM_FORMAT, |
| 218 |
WL_DRM_FORMAT_XBGR2101010); |
| 219 |
} |
| 220 |
|
| 221 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_ARGB8888); |
| 222 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_XRGB8888); |
| 223 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_BGR888); |
| 224 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_RGB888); |
| 225 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_RGB565); |
| 226 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV410); |
| 227 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV411); |
| 228 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV420); |
| 229 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV422); |
| 230 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV444); |
| 231 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV12); |
| 232 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV16); |
| 233 |
wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUYV); |
| 234 |
|
| 235 |
if (version >= 2) { |
| 236 |
wl_resource_post_event(resource, WL_DRM_CAPABILITIES, |
| 237 |
WL_DRM_CAPABILITY_PRIME); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
struct wl_drm * |
| 242 |
wayland_drm_init(struct wl_display *display, char *device_name, |
| 243 |
const struct wayland_drm_callbacks *callbacks, void *user_data) |
| 244 |
{ |
| 245 |
struct wl_drm *drm; |
| 246 |
|
| 247 |
drm = malloc(sizeof *drm); |
| 248 |
if (!drm) |
| 249 |
return NULL; |
| 250 |
|
| 251 |
drm->display = display; |
| 252 |
drm->device_name = strdup(device_name); |
| 253 |
drm->callbacks = *callbacks; |
| 254 |
drm->user_data = user_data; |
| 255 |
|
| 256 |
drm->buffer_interface.destroy = buffer_destroy; |
| 257 |
|
| 258 |
drm->wl_drm_global = |
| 259 |
wl_global_create(display, &wl_drm_interface, 2, drm, bind_drm); |
| 260 |
|
| 261 |
return drm; |
| 262 |
} |
| 263 |
|
| 264 |
void |
| 265 |
wayland_drm_uninit(struct wl_drm *drm) |
| 266 |
{ |
| 267 |
free(drm->device_name); |
| 268 |
|
| 269 |
wl_global_destroy(drm->wl_drm_global); |
| 270 |
|
| 271 |
free(drm); |
| 272 |
} |