wayland: Don't rely on static variable for identifying wl_drm buffers
[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 struct wl_buffer_interface buffer_interface;
52 };
53
54 static void
55 destroy_buffer(struct wl_resource *resource)
56 {
57 struct wl_drm_buffer *buffer = resource->data;
58 struct wl_drm *drm = buffer->drm;
59
60 drm->callbacks->release_buffer(drm->user_data, buffer);
61 free(buffer);
62 }
63
64 static void
65 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
66 {
67 wl_resource_destroy(resource);
68 }
69
70 static void
71 create_buffer(struct wl_client *client, struct wl_resource *resource,
72 uint32_t id, uint32_t name, int fd,
73 int32_t width, int32_t height,
74 uint32_t format,
75 int32_t offset0, int32_t stride0,
76 int32_t offset1, int32_t stride1,
77 int32_t offset2, int32_t stride2)
78 {
79 struct wl_drm *drm = resource->data;
80 struct wl_drm_buffer *buffer;
81
82 buffer = calloc(1, sizeof *buffer);
83 if (buffer == NULL) {
84 wl_resource_post_no_memory(resource);
85 return;
86 }
87
88 buffer->drm = drm;
89 buffer->width = width;
90 buffer->height = height;
91 buffer->format = format;
92 buffer->offset[0] = offset0;
93 buffer->stride[0] = stride0;
94 buffer->offset[1] = offset1;
95 buffer->stride[1] = stride1;
96 buffer->offset[2] = offset2;
97 buffer->stride[2] = stride2;
98
99 drm->callbacks->reference_buffer(drm->user_data, name, fd, buffer);
100 if (buffer->driver_buffer == NULL) {
101 wl_resource_post_error(resource,
102 WL_DRM_ERROR_INVALID_NAME,
103 "invalid name");
104 return;
105 }
106
107 buffer->resource =
108 wl_resource_create(client, &wl_buffer_interface, 1, id);
109 if (!buffer->resource) {
110 wl_resource_post_no_memory(resource);
111 free(buffer);
112 return;
113 }
114
115 wl_resource_set_implementation(buffer->resource,
116 (void (**)(void)) &drm->buffer_interface,
117 buffer, destroy_buffer);
118 }
119
120 static void
121 drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
122 uint32_t id, uint32_t name, int32_t width, int32_t height,
123 uint32_t stride, uint32_t format)
124 {
125 switch (format) {
126 case WL_DRM_FORMAT_ARGB8888:
127 case WL_DRM_FORMAT_XRGB8888:
128 case WL_DRM_FORMAT_YUYV:
129 break;
130 default:
131 wl_resource_post_error(resource,
132 WL_DRM_ERROR_INVALID_FORMAT,
133 "invalid format");
134 return;
135 }
136
137 create_buffer(client, resource, id,
138 name, -1, width, height, format, 0, stride, 0, 0, 0, 0);
139 }
140
141 static void
142 drm_create_planar_buffer(struct wl_client *client,
143 struct wl_resource *resource,
144 uint32_t id, uint32_t name,
145 int32_t width, int32_t height, uint32_t format,
146 int32_t offset0, int32_t stride0,
147 int32_t offset1, int32_t stride1,
148 int32_t offset2, int32_t stride2)
149 {
150 switch (format) {
151 case WL_DRM_FORMAT_YUV410:
152 case WL_DRM_FORMAT_YUV411:
153 case WL_DRM_FORMAT_YUV420:
154 case WL_DRM_FORMAT_YUV422:
155 case WL_DRM_FORMAT_YUV444:
156 case WL_DRM_FORMAT_NV12:
157 case WL_DRM_FORMAT_NV16:
158 break;
159 default:
160 wl_resource_post_error(resource,
161 WL_DRM_ERROR_INVALID_FORMAT,
162 "invalid format");
163 return;
164 }
165
166 create_buffer(client, resource, id, name, -1, width, height, format,
167 offset0, stride0, offset1, stride1, offset2, stride2);
168 }
169
170 static void
171 drm_create_prime_buffer(struct wl_client *client,
172 struct wl_resource *resource,
173 uint32_t id, int fd,
174 int32_t width, int32_t height, uint32_t format,
175 int32_t offset0, int32_t stride0,
176 int32_t offset1, int32_t stride1,
177 int32_t offset2, int32_t stride2)
178 {
179 create_buffer(client, resource, id, 0, fd, width, height, format,
180 offset0, stride0, offset1, stride1, offset2, stride2);
181 close(fd);
182 }
183
184 static void
185 drm_authenticate(struct wl_client *client,
186 struct wl_resource *resource, uint32_t id)
187 {
188 struct wl_drm *drm = resource->data;
189
190 if (drm->callbacks->authenticate(drm->user_data, id) < 0)
191 wl_resource_post_error(resource,
192 WL_DRM_ERROR_AUTHENTICATE_FAIL,
193 "authenicate failed");
194 else
195 wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
196 }
197
198 const static struct wl_drm_interface drm_interface = {
199 drm_authenticate,
200 drm_create_buffer,
201 drm_create_planar_buffer,
202 drm_create_prime_buffer
203 };
204
205 static void
206 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
207 {
208 struct wl_drm *drm = data;
209 struct wl_resource *resource;
210 uint32_t capabilities;
211
212 resource = wl_resource_create(client, &wl_drm_interface,
213 MIN(version, 2), id);
214 if (!resource) {
215 wl_client_post_no_memory(client);
216 return;
217 }
218
219 wl_resource_set_implementation(resource, &drm_interface, data, NULL);
220
221 wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
222 wl_resource_post_event(resource, WL_DRM_FORMAT,
223 WL_DRM_FORMAT_ARGB8888);
224 wl_resource_post_event(resource, WL_DRM_FORMAT,
225 WL_DRM_FORMAT_XRGB8888);
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 capabilities = 0;
236 if (drm->flags & WAYLAND_DRM_PRIME)
237 capabilities |= WL_DRM_CAPABILITY_PRIME;
238
239 if (version >= 2)
240 wl_resource_post_event(resource, WL_DRM_CAPABILITIES, capabilities);
241 }
242
243 struct wl_drm_buffer *
244 wayland_drm_buffer_get(struct wl_drm *drm, struct wl_resource *resource)
245 {
246 struct wl_drm_buffer *buffer;
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 drm->buffer_interface.destroy = buffer_destroy;
274
275 wl_global_create(display, &wl_drm_interface, 2, drm, bind_drm);
276
277 return drm;
278 }
279
280 void
281 wayland_drm_uninit(struct wl_drm *drm)
282 {
283 free(drm->device_name);
284
285 /* FIXME: need wl_display_del_{object,global} */
286
287 free(drm);
288 }
289
290 uint32_t
291 wayland_drm_buffer_get_format(struct wl_drm_buffer *buffer)
292 {
293 return buffer->format;
294 }
295
296 void *
297 wayland_drm_buffer_get_buffer(struct wl_drm_buffer *buffer)
298 {
299 return buffer->driver_buffer;
300 }