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 destroy_buffer(struct wl_resource
*resource
)
45 struct wl_drm_buffer
*buffer
= wl_resource_get_user_data(resource
);
46 struct wl_drm
*drm
= buffer
->drm
;
48 drm
->callbacks
.release_buffer(drm
->user_data
, buffer
);
53 buffer_destroy(struct wl_client
*client
, struct wl_resource
*resource
)
55 wl_resource_destroy(resource
);
59 create_buffer(struct wl_client
*client
, struct wl_resource
*resource
,
60 uint32_t id
, uint32_t name
, int fd
,
61 int32_t width
, int32_t height
,
63 int32_t offset0
, int32_t stride0
,
64 int32_t offset1
, int32_t stride1
,
65 int32_t offset2
, int32_t stride2
)
67 struct wl_drm
*drm
= wl_resource_get_user_data(resource
);
68 struct wl_drm_buffer
*buffer
;
70 buffer
= calloc(1, sizeof *buffer
);
72 wl_resource_post_no_memory(resource
);
77 buffer
->width
= width
;
78 buffer
->height
= height
;
79 buffer
->format
= format
;
80 buffer
->offset
[0] = offset0
;
81 buffer
->stride
[0] = stride0
;
82 buffer
->offset
[1] = offset1
;
83 buffer
->stride
[1] = stride1
;
84 buffer
->offset
[2] = offset2
;
85 buffer
->stride
[2] = stride2
;
87 drm
->callbacks
.reference_buffer(drm
->user_data
, name
, fd
, buffer
);
88 if (buffer
->driver_buffer
== NULL
) {
89 wl_resource_post_error(resource
,
90 WL_DRM_ERROR_INVALID_NAME
,
96 wl_resource_create(client
, &wl_buffer_interface
, 1, id
);
97 if (!buffer
->resource
) {
98 wl_resource_post_no_memory(resource
);
103 wl_resource_set_implementation(buffer
->resource
,
104 (void (**)(void)) &drm
->buffer_interface
,
105 buffer
, destroy_buffer
);
109 drm_create_buffer(struct wl_client
*client
, struct wl_resource
*resource
,
110 uint32_t id
, uint32_t name
, int32_t width
, int32_t height
,
111 uint32_t stride
, uint32_t format
)
114 case WL_DRM_FORMAT_ARGB8888
:
115 case WL_DRM_FORMAT_XRGB8888
:
116 case WL_DRM_FORMAT_YUYV
:
117 case WL_DRM_FORMAT_RGB565
:
120 wl_resource_post_error(resource
,
121 WL_DRM_ERROR_INVALID_FORMAT
,
126 create_buffer(client
, resource
, id
,
127 name
, -1, width
, height
, format
, 0, stride
, 0, 0, 0, 0);
131 drm_create_planar_buffer(struct wl_client
*client
,
132 struct wl_resource
*resource
,
133 uint32_t id
, uint32_t name
,
134 int32_t width
, int32_t height
, uint32_t format
,
135 int32_t offset0
, int32_t stride0
,
136 int32_t offset1
, int32_t stride1
,
137 int32_t offset2
, int32_t stride2
)
140 case WL_DRM_FORMAT_YUV410
:
141 case WL_DRM_FORMAT_YUV411
:
142 case WL_DRM_FORMAT_YUV420
:
143 case WL_DRM_FORMAT_YUV422
:
144 case WL_DRM_FORMAT_YUV444
:
145 case WL_DRM_FORMAT_NV12
:
146 case WL_DRM_FORMAT_NV16
:
149 wl_resource_post_error(resource
,
150 WL_DRM_ERROR_INVALID_FORMAT
,
155 create_buffer(client
, resource
, id
, name
, -1, width
, height
, format
,
156 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
160 drm_create_prime_buffer(struct wl_client
*client
,
161 struct wl_resource
*resource
,
163 int32_t width
, int32_t height
, uint32_t format
,
164 int32_t offset0
, int32_t stride0
,
165 int32_t offset1
, int32_t stride1
,
166 int32_t offset2
, int32_t stride2
)
168 create_buffer(client
, resource
, id
, 0, fd
, width
, height
, format
,
169 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
174 drm_authenticate(struct wl_client
*client
,
175 struct wl_resource
*resource
, uint32_t id
)
177 struct wl_drm
*drm
= wl_resource_get_user_data(resource
);
179 if (drm
->callbacks
.authenticate(drm
->user_data
, id
) < 0)
180 wl_resource_post_error(resource
,
181 WL_DRM_ERROR_AUTHENTICATE_FAIL
,
182 "authenicate failed");
184 wl_resource_post_event(resource
, WL_DRM_AUTHENTICATED
);
187 static const struct wl_drm_interface drm_interface
= {
190 drm_create_planar_buffer
,
191 drm_create_prime_buffer
195 bind_drm(struct wl_client
*client
, void *data
, uint32_t version
, uint32_t id
)
197 struct wl_drm
*drm
= data
;
198 struct wl_resource
*resource
;
199 uint32_t capabilities
;
201 resource
= wl_resource_create(client
, &wl_drm_interface
,
202 MIN(version
, 2), id
);
204 wl_client_post_no_memory(client
);
208 wl_resource_set_implementation(resource
, &drm_interface
, data
, NULL
);
210 wl_resource_post_event(resource
, WL_DRM_DEVICE
, drm
->device_name
);
211 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
212 WL_DRM_FORMAT_ARGB8888
);
213 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
214 WL_DRM_FORMAT_XRGB8888
);
215 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
216 WL_DRM_FORMAT_RGB565
);
217 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV410
);
218 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV411
);
219 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV420
);
220 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV422
);
221 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV444
);
222 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_NV12
);
223 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_NV16
);
224 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUYV
);
227 if (drm
->flags
& WAYLAND_DRM_PRIME
)
228 capabilities
|= WL_DRM_CAPABILITY_PRIME
;
231 wl_resource_post_event(resource
, WL_DRM_CAPABILITIES
, capabilities
);
235 wayland_drm_init(struct wl_display
*display
, char *device_name
,
236 const struct wayland_drm_callbacks
*callbacks
, void *user_data
,
241 drm
= malloc(sizeof *drm
);
245 drm
->display
= display
;
246 drm
->device_name
= strdup(device_name
);
247 drm
->callbacks
= *callbacks
;
248 drm
->user_data
= user_data
;
251 drm
->buffer_interface
.destroy
= buffer_destroy
;
254 wl_global_create(display
, &wl_drm_interface
, 2,
261 wayland_drm_uninit(struct wl_drm
*drm
)
263 free(drm
->device_name
);
265 wl_global_destroy(drm
->wl_drm_global
);