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 <krh@bitplanet.net> 27 * Benjamin Franzke <benjaminfranzke@googlemail.com> 28 */ 29 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <stddef.h> 34 35#include <wayland-server.h> 36#include "wayland-drm.h" 37#include "wayland-drm-server-protocol.h" 38 39struct wl_drm { 40 struct wl_display *display; 41 42 void *user_data; 43 char *device_name; 44 45 struct wayland_drm_callbacks *callbacks; 46}; 47 48static void 49destroy_buffer(struct wl_resource *resource) 50{ 51 struct wl_drm_buffer *buffer = resource->data; 52 struct wl_drm *drm = buffer->drm; 53 54 drm->callbacks->release_buffer(drm->user_data, buffer); 55 free(buffer); 56} 57 58static void 59buffer_destroy(struct wl_client *client, struct wl_resource *resource) 60{ 61 wl_resource_destroy(resource); 62} 63 64const static struct wl_buffer_interface drm_buffer_interface = { 65 buffer_destroy 66}; 67 68static void 69create_buffer(struct wl_client *client, struct wl_resource *resource, 70 uint32_t id, uint32_t name, int32_t width, int32_t height, 71 uint32_t format, 72 int32_t offset0, int32_t stride0, 73 int32_t offset1, int32_t stride1, 74 int32_t offset2, int32_t stride2) 75{ 76 struct wl_drm *drm = resource->data; 77 struct wl_drm_buffer *buffer; 78 79 buffer = calloc(1, sizeof *buffer); 80 if (buffer == NULL) { 81 wl_resource_post_no_memory(resource); 82 return; 83 } 84 85 buffer->drm = drm; 86 buffer->buffer.width = width; 87 buffer->buffer.height = height; 88 buffer->format = format; 89 buffer->offset[0] = offset0; 90 buffer->stride[0] = stride0; 91 buffer->offset[1] = offset1; 92 buffer->stride[1] = stride1; 93 buffer->offset[2] = offset2; 94 buffer->stride[2] = stride2; 95 96 drm->callbacks->reference_buffer(drm->user_data, name, buffer); 97 if (buffer->driver_buffer == NULL) { 98 wl_resource_post_error(resource, 99 WL_DRM_ERROR_INVALID_NAME, 100 "invalid name"); 101 return; 102 } 103 104 buffer->buffer.resource.object.id = id; 105 buffer->buffer.resource.object.interface = &wl_buffer_interface; 106 buffer->buffer.resource.object.implementation = 107 (void (**)(void)) &drm_buffer_interface; 108 buffer->buffer.resource.data = buffer; 109 110 buffer->buffer.resource.destroy = destroy_buffer; 111 buffer->buffer.resource.client = resource->client; 112 113 wl_client_add_resource(resource->client, &buffer->buffer.resource); 114} 115 116static void 117drm_create_buffer(struct wl_client *client, struct wl_resource *resource, 118 uint32_t id, uint32_t name, int32_t width, int32_t height, 119 uint32_t stride, uint32_t format) 120{ 121 switch (format) { 122 case WL_DRM_FORMAT_ARGB8888: 123 case WL_DRM_FORMAT_XRGB8888: 124 case WL_DRM_FORMAT_YUYV: 125 break; 126 default: 127 wl_resource_post_error(resource, 128 WL_DRM_ERROR_INVALID_FORMAT, 129 "invalid format"); 130 return; 131 } 132 133 create_buffer(client, resource, id, 134 name, width, height, format, 0, stride, 0, 0, 0, 0); 135} 136 137static void 138drm_create_planar_buffer(struct wl_client *client, 139 struct wl_resource *resource, 140 uint32_t id, uint32_t name, 141 int32_t width, int32_t height, uint32_t format, 142 int32_t offset0, int32_t stride0, 143 int32_t offset1, int32_t stride1, 144 int32_t offset2, int32_t stride2) 145{ 146 switch (format) { 147 case WL_DRM_FORMAT_YUV410: 148 case WL_DRM_FORMAT_YUV411: 149 case WL_DRM_FORMAT_YUV420: 150 case WL_DRM_FORMAT_YUV422: 151 case WL_DRM_FORMAT_YUV444: 152 case WL_DRM_FORMAT_NV12: 153 case WL_DRM_FORMAT_NV16: 154 break; 155 default: 156 wl_resource_post_error(resource, 157 WL_DRM_ERROR_INVALID_FORMAT, 158 "invalid format"); 159 return; 160 } 161 162 create_buffer(client, resource, id, name, width, height, format, 163 offset0, stride0, offset1, stride1, offset2, stride2); 164} 165 166static void 167drm_authenticate(struct wl_client *client, 168 struct wl_resource *resource, uint32_t id) 169{ 170 struct wl_drm *drm = resource->data; 171 172 if (drm->callbacks->authenticate(drm->user_data, id) < 0) 173 wl_resource_post_error(resource, 174 WL_DRM_ERROR_AUTHENTICATE_FAIL, 175 "authenicate failed"); 176 else 177 wl_resource_post_event(resource, WL_DRM_AUTHENTICATED); 178} 179 180const static struct wl_drm_interface drm_interface = { 181 drm_authenticate, 182 drm_create_buffer, 183 drm_create_planar_buffer 184}; 185 186static void 187bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id) 188{ 189 struct wl_drm *drm = data; 190 struct wl_resource *resource; 191 192 resource = wl_client_add_object(client, &wl_drm_interface, 193 &drm_interface, id, data); 194 wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name); 195 wl_resource_post_event(resource, WL_DRM_FORMAT, 196 WL_DRM_FORMAT_ARGB8888); 197 wl_resource_post_event(resource, WL_DRM_FORMAT, 198 WL_DRM_FORMAT_XRGB8888); 199 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV410); 200 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV411); 201 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV420); 202 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV422); 203 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV444); 204 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV12); 205 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV16); 206 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUYV); 207} 208 209struct wl_drm * 210wayland_drm_init(struct wl_display *display, char *device_name, 211 struct wayland_drm_callbacks *callbacks, void *user_data) 212{ 213 struct wl_drm *drm; 214 215 drm = malloc(sizeof *drm); 216 217 drm->display = display; 218 drm->device_name = strdup(device_name); 219 drm->callbacks = callbacks; 220 drm->user_data = user_data; 221 222 wl_display_add_global(display, &wl_drm_interface, drm, bind_drm); 223 224 return drm; 225} 226 227void 228wayland_drm_uninit(struct wl_drm *drm) 229{ 230 free(drm->device_name); 231 232 /* FIXME: need wl_display_del_{object,global} */ 233 234 free(drm); 235} 236 237int 238wayland_buffer_is_drm(struct wl_buffer *buffer) 239{ 240 return buffer->resource.object.implementation == 241 (void (**)(void)) &drm_buffer_interface; 242} 243 244uint32_t 245wayland_drm_buffer_get_format(struct wl_buffer *buffer_base) 246{ 247 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base; 248 249 return buffer->format; 250} 251 252void * 253wayland_drm_buffer_get_buffer(struct wl_buffer *buffer_base) 254{ 255 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base; 256 257 return buffer->driver_buffer; 258} 259