2 * Copyright © 2011 Kristian Høgsberg
3 * Copyright © 2011 Benjamin Franzke
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:
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
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.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
36 #include <wayland-server.h>
37 #include "wayland-drm.h"
38 #include "wayland-drm-server-protocol.h"
40 #define MIN(x,y) (((x)<(y))?(x):(y))
43 struct wl_display
*display
;
49 struct wayland_drm_callbacks
*callbacks
;
53 destroy_buffer(struct wl_resource
*resource
)
55 struct wl_drm_buffer
*buffer
= resource
->data
;
56 struct wl_drm
*drm
= buffer
->drm
;
58 drm
->callbacks
->release_buffer(drm
->user_data
, buffer
);
63 buffer_destroy(struct wl_client
*client
, struct wl_resource
*resource
)
65 wl_resource_destroy(resource
);
68 const static struct wl_buffer_interface drm_buffer_interface
= {
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
,
77 int32_t offset0
, int32_t stride0
,
78 int32_t offset1
, int32_t stride1
,
79 int32_t offset2
, int32_t stride2
)
81 struct wl_drm
*drm
= resource
->data
;
82 struct wl_drm_buffer
*buffer
;
84 buffer
= calloc(1, sizeof *buffer
);
86 wl_resource_post_no_memory(resource
);
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
;
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
,
110 wl_resource_create(client
, &wl_buffer_interface
, 1, id
);
111 if (!buffer
->resource
) {
112 wl_resource_post_no_memory(resource
);
117 wl_resource_set_implementation(buffer
->resource
,
118 (void (**)(void)) &drm_buffer_interface
,
119 buffer
, destroy_buffer
);
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
)
128 case WL_DRM_FORMAT_ARGB8888
:
129 case WL_DRM_FORMAT_XRGB8888
:
130 case WL_DRM_FORMAT_YUYV
:
133 wl_resource_post_error(resource
,
134 WL_DRM_ERROR_INVALID_FORMAT
,
139 create_buffer(client
, resource
, id
,
140 name
, -1, width
, height
, format
, 0, stride
, 0, 0, 0, 0);
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
)
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
:
162 wl_resource_post_error(resource
,
163 WL_DRM_ERROR_INVALID_FORMAT
,
168 create_buffer(client
, resource
, id
, name
, -1, width
, height
, format
,
169 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
173 drm_create_prime_buffer(struct wl_client
*client
,
174 struct wl_resource
*resource
,
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
)
181 create_buffer(client
, resource
, id
, 0, fd
, width
, height
, format
,
182 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
187 drm_authenticate(struct wl_client
*client
,
188 struct wl_resource
*resource
, uint32_t id
)
190 struct wl_drm
*drm
= resource
->data
;
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");
197 wl_resource_post_event(resource
, WL_DRM_AUTHENTICATED
);
200 const static struct wl_drm_interface drm_interface
= {
203 drm_create_planar_buffer
,
204 drm_create_prime_buffer
208 bind_drm(struct wl_client
*client
, void *data
, uint32_t version
, uint32_t id
)
210 struct wl_drm
*drm
= data
;
211 struct wl_resource
*resource
;
212 uint32_t capabilities
;
214 resource
= wl_resource_create(client
, &wl_drm_interface
,
215 MIN(version
, 2), id
);
217 wl_client_post_no_memory(client
);
221 wl_resource_set_implementation(resource
, &drm_interface
, data
, NULL
);
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
);
238 if (drm
->flags
& WAYLAND_DRM_PRIME
)
239 capabilities
|= WL_DRM_CAPABILITY_PRIME
;
242 wl_resource_post_event(resource
, WL_DRM_CAPABILITIES
, capabilities
);
245 struct wl_drm_buffer
*
246 wayland_drm_buffer_get(struct wl_resource
*resource
)
248 if (resource
== NULL
)
251 if (wl_resource_instance_of(resource
, &wl_buffer_interface
,
252 &drm_buffer_interface
))
253 return wl_resource_get_user_data(resource
);
259 wayland_drm_init(struct wl_display
*display
, char *device_name
,
260 struct wayland_drm_callbacks
*callbacks
, void *user_data
,
265 drm
= malloc(sizeof *drm
);
267 drm
->display
= display
;
268 drm
->device_name
= strdup(device_name
);
269 drm
->callbacks
= callbacks
;
270 drm
->user_data
= user_data
;
273 wl_global_create(display
, &wl_drm_interface
, 2, drm
, bind_drm
);
279 wayland_drm_uninit(struct wl_drm
*drm
)
281 free(drm
->device_name
);
283 /* FIXME: need wl_display_del_{object,global} */
289 wayland_drm_buffer_get_format(struct wl_drm_buffer
*buffer
)
291 return buffer
->format
;
295 wayland_drm_buffer_get_buffer(struct wl_drm_buffer
*buffer
)
297 return buffer
->driver_buffer
;