• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "wayland-drm-server-protocol.h"
37 #include "wayland-drm.h"
38 #include <wayland-server.h>
39 
40 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
41 
42 static void
destroy_buffer(struct wl_resource * resource)43 destroy_buffer(struct wl_resource *resource)
44 {
45    struct wl_drm_buffer *buffer = wl_resource_get_user_data(resource);
46    struct wl_drm *drm = buffer->drm;
47 
48    drm->callbacks.release_buffer(drm->user_data, buffer);
49    free(buffer);
50 }
51 
52 static void
buffer_destroy(struct wl_client * client,struct wl_resource * resource)53 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
54 {
55    wl_resource_destroy(resource);
56 }
57 
58 static void
create_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int fd,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)59 create_buffer(struct wl_client *client, struct wl_resource *resource,
60               uint32_t id, uint32_t name, int fd, int32_t width, int32_t height,
61               uint32_t format, int32_t offset0, int32_t stride0,
62               int32_t offset1, int32_t stride1, int32_t offset2,
63               int32_t stride2)
64 {
65    struct wl_drm *drm = wl_resource_get_user_data(resource);
66    struct wl_drm_buffer *buffer;
67 
68    buffer = calloc(1, sizeof *buffer);
69    if (buffer == NULL) {
70       wl_resource_post_no_memory(resource);
71       return;
72    }
73 
74    buffer->drm = drm;
75    buffer->width = width;
76    buffer->height = height;
77    buffer->format = format;
78    buffer->offset[0] = offset0;
79    buffer->stride[0] = stride0;
80    buffer->offset[1] = offset1;
81    buffer->stride[1] = stride1;
82    buffer->offset[2] = offset2;
83    buffer->stride[2] = stride2;
84 
85    drm->callbacks.reference_buffer(drm->user_data, name, fd, buffer);
86    if (buffer->driver_buffer == NULL) {
87       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME,
88                              "invalid name");
89       return;
90    }
91 
92    buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
93    if (!buffer->resource) {
94       wl_resource_post_no_memory(resource);
95       free(buffer);
96       return;
97    }
98 
99    wl_resource_set_implementation(buffer->resource,
100                                   (void (**)(void)) & drm->buffer_interface,
101                                   buffer, destroy_buffer);
102 }
103 
104 static void
drm_create_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int32_t width,int32_t height,uint32_t stride,uint32_t format)105 drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
106                   uint32_t id, uint32_t name, int32_t width, int32_t height,
107                   uint32_t stride, uint32_t format)
108 {
109    switch (format) {
110    case WL_DRM_FORMAT_ABGR2101010:
111    case WL_DRM_FORMAT_XBGR2101010:
112    case WL_DRM_FORMAT_ARGB2101010:
113    case WL_DRM_FORMAT_XRGB2101010:
114    case WL_DRM_FORMAT_ARGB8888:
115    case WL_DRM_FORMAT_XRGB8888:
116    case WL_DRM_FORMAT_YUYV:
117    case WL_DRM_FORMAT_RGB565:
118       break;
119    default:
120       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
121                              "invalid format");
122       return;
123    }
124 
125    create_buffer(client, resource, id, name, -1, width, height, format, 0,
126                  stride, 0, 0, 0, 0);
127 }
128 
129 static void
drm_create_planar_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)130 drm_create_planar_buffer(struct wl_client *client, struct wl_resource *resource,
131                          uint32_t id, uint32_t name, int32_t width,
132                          int32_t height, uint32_t format, int32_t offset0,
133                          int32_t stride0, int32_t offset1, int32_t stride1,
134                          int32_t offset2, int32_t stride2)
135 {
136    switch (format) {
137    case WL_DRM_FORMAT_YUV410:
138    case WL_DRM_FORMAT_YUV411:
139    case WL_DRM_FORMAT_YUV420:
140    case WL_DRM_FORMAT_YUV422:
141    case WL_DRM_FORMAT_YUV444:
142    case WL_DRM_FORMAT_NV12:
143    case WL_DRM_FORMAT_NV16:
144       break;
145    default:
146       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
147                              "invalid format");
148       return;
149    }
150 
151    create_buffer(client, resource, id, name, -1, width, height, format, offset0,
152                  stride0, offset1, stride1, offset2, stride2);
153 }
154 
155 static void
drm_create_prime_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,int fd,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)156 drm_create_prime_buffer(struct wl_client *client, struct wl_resource *resource,
157                         uint32_t id, int fd, int32_t width, int32_t height,
158                         uint32_t format, int32_t offset0, int32_t stride0,
159                         int32_t offset1, int32_t stride1, int32_t offset2,
160                         int32_t stride2)
161 {
162    create_buffer(client, resource, id, 0, fd, width, height, format, offset0,
163                  stride0, offset1, stride1, offset2, stride2);
164    close(fd);
165 }
166 
167 static void
drm_authenticate(struct wl_client * client,struct wl_resource * resource,uint32_t id)168 drm_authenticate(struct wl_client *client, struct wl_resource *resource,
169                  uint32_t id)
170 {
171    struct wl_drm *drm = wl_resource_get_user_data(resource);
172 
173    if (!drm->callbacks.authenticate ||
174        drm->callbacks.authenticate(drm->user_data, id) < 0)
175       wl_resource_post_error(resource, WL_DRM_ERROR_AUTHENTICATE_FAIL,
176                              "authenticate failed");
177    else
178       wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
179 }
180 
181 static const struct wl_drm_interface drm_interface = {
182    drm_authenticate,
183    drm_create_buffer,
184    drm_create_planar_buffer,
185    drm_create_prime_buffer,
186 };
187 
188 static void
bind_drm(struct wl_client * client,void * data,uint32_t version,uint32_t id)189 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
190 {
191    struct wl_drm *drm = data;
192    struct wl_resource *resource;
193    uint32_t capabilities;
194 
195    resource =
196       wl_resource_create(client, &wl_drm_interface, MIN(version, 2), id);
197    if (!resource) {
198       wl_client_post_no_memory(client);
199       return;
200    }
201 
202    wl_resource_set_implementation(resource, &drm_interface, data, NULL);
203 
204    wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
205 
206    if (drm->callbacks.is_format_supported(drm->user_data,
207                                           WL_DRM_FORMAT_ARGB2101010)) {
208       wl_resource_post_event(resource, WL_DRM_FORMAT,
209                              WL_DRM_FORMAT_ARGB2101010);
210    }
211 
212    if (drm->callbacks.is_format_supported(drm->user_data,
213                                           WL_DRM_FORMAT_XRGB2101010)) {
214       wl_resource_post_event(resource, WL_DRM_FORMAT,
215                              WL_DRM_FORMAT_XRGB2101010);
216    }
217 
218    if (drm->callbacks.is_format_supported(drm->user_data,
219                                           WL_DRM_FORMAT_ABGR2101010)) {
220       wl_resource_post_event(resource, WL_DRM_FORMAT,
221                              WL_DRM_FORMAT_ABGR2101010);
222    }
223 
224    if (drm->callbacks.is_format_supported(drm->user_data,
225                                           WL_DRM_FORMAT_XBGR2101010)) {
226       wl_resource_post_event(resource, WL_DRM_FORMAT,
227                              WL_DRM_FORMAT_XBGR2101010);
228    }
229 
230    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_ARGB8888);
231    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_XRGB8888);
232    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_RGB565);
233    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV410);
234    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV411);
235    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV420);
236    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV422);
237    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV444);
238    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV12);
239    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV16);
240    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUYV);
241 
242    capabilities = 0;
243    if (drm->flags & WAYLAND_DRM_PRIME)
244       capabilities |= WL_DRM_CAPABILITY_PRIME;
245 
246    if (version >= 2)
247       wl_resource_post_event(resource, WL_DRM_CAPABILITIES, capabilities);
248 }
249 
250 struct wl_drm *
wayland_drm_init(struct wl_display * display,char * device_name,const struct wayland_drm_callbacks * callbacks,void * user_data,uint32_t flags)251 wayland_drm_init(struct wl_display *display, char *device_name,
252                  const struct wayland_drm_callbacks *callbacks, void *user_data,
253                  uint32_t flags)
254 {
255    struct wl_drm *drm;
256 
257    drm = malloc(sizeof *drm);
258    if (!drm)
259       return NULL;
260 
261    drm->display = display;
262    drm->device_name = strdup(device_name);
263    drm->callbacks = *callbacks;
264    drm->user_data = user_data;
265    drm->flags = flags;
266 
267    drm->buffer_interface.destroy = buffer_destroy;
268 
269    drm->wl_drm_global =
270       wl_global_create(display, &wl_drm_interface, 2, drm, bind_drm);
271 
272    return drm;
273 }
274 
275 void
wayland_drm_uninit(struct wl_drm * drm)276 wayland_drm_uninit(struct wl_drm *drm)
277 {
278    free(drm->device_name);
279 
280    wl_global_destroy(drm->wl_drm_global);
281 
282    free(drm);
283 }
284