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
;
51 struct wl_buffer_interface buffer_interface
;
55 destroy_buffer(struct wl_resource
*resource
)
57 struct wl_drm_buffer
*buffer
= resource
->data
;
58 struct wl_drm
*drm
= buffer
->drm
;
60 drm
->callbacks
->release_buffer(drm
->user_data
, buffer
);
65 buffer_destroy(struct wl_client
*client
, struct wl_resource
*resource
)
67 wl_resource_destroy(resource
);
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
,
75 int32_t offset0
, int32_t stride0
,
76 int32_t offset1
, int32_t stride1
,
77 int32_t offset2
, int32_t stride2
)
79 struct wl_drm
*drm
= resource
->data
;
80 struct wl_drm_buffer
*buffer
;
82 buffer
= calloc(1, sizeof *buffer
);
84 wl_resource_post_no_memory(resource
);
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
;
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
,
108 wl_resource_create(client
, &wl_buffer_interface
, 1, id
);
109 if (!buffer
->resource
) {
110 wl_resource_post_no_memory(resource
);
115 wl_resource_set_implementation(buffer
->resource
,
116 (void (**)(void)) &drm
->buffer_interface
,
117 buffer
, destroy_buffer
);
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
)
126 case WL_DRM_FORMAT_ARGB8888
:
127 case WL_DRM_FORMAT_XRGB8888
:
128 case WL_DRM_FORMAT_YUYV
:
131 wl_resource_post_error(resource
,
132 WL_DRM_ERROR_INVALID_FORMAT
,
137 create_buffer(client
, resource
, id
,
138 name
, -1, width
, height
, format
, 0, stride
, 0, 0, 0, 0);
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
)
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
:
160 wl_resource_post_error(resource
,
161 WL_DRM_ERROR_INVALID_FORMAT
,
166 create_buffer(client
, resource
, id
, name
, -1, width
, height
, format
,
167 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
171 drm_create_prime_buffer(struct wl_client
*client
,
172 struct wl_resource
*resource
,
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
)
179 create_buffer(client
, resource
, id
, 0, fd
, width
, height
, format
,
180 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
185 drm_authenticate(struct wl_client
*client
,
186 struct wl_resource
*resource
, uint32_t id
)
188 struct wl_drm
*drm
= resource
->data
;
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");
195 wl_resource_post_event(resource
, WL_DRM_AUTHENTICATED
);
198 const static struct wl_drm_interface drm_interface
= {
201 drm_create_planar_buffer
,
202 drm_create_prime_buffer
206 bind_drm(struct wl_client
*client
, void *data
, uint32_t version
, uint32_t id
)
208 struct wl_drm
*drm
= data
;
209 struct wl_resource
*resource
;
210 uint32_t capabilities
;
212 resource
= wl_resource_create(client
, &wl_drm_interface
,
213 MIN(version
, 2), id
);
215 wl_client_post_no_memory(client
);
219 wl_resource_set_implementation(resource
, &drm_interface
, data
, NULL
);
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
);
236 if (drm
->flags
& WAYLAND_DRM_PRIME
)
237 capabilities
|= WL_DRM_CAPABILITY_PRIME
;
240 wl_resource_post_event(resource
, WL_DRM_CAPABILITIES
, capabilities
);
243 struct wl_drm_buffer
*
244 wayland_drm_buffer_get(struct wl_drm
*drm
, struct wl_resource
*resource
)
246 struct wl_drm_buffer
*buffer
;
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 drm
->buffer_interface
.destroy
= buffer_destroy
;
275 wl_global_create(display
, &wl_drm_interface
, 2, drm
, bind_drm
);
281 wayland_drm_uninit(struct wl_drm
*drm
)
283 free(drm
->device_name
);
285 /* FIXME: need wl_display_del_{object,global} */
291 wayland_drm_buffer_get_format(struct wl_drm_buffer
*buffer
)
293 return buffer
->format
;
297 wayland_drm_buffer_get_buffer(struct wl_drm_buffer
*buffer
)
299 return buffer
->driver_buffer
;