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