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>
35 #include <wayland-server.h>
36 #include "wayland-drm.h"
37 #include "wayland-drm-server-protocol.h"
40 struct wl_display
*display
;
45 struct wayland_drm_callbacks
*callbacks
;
49 destroy_buffer(struct wl_resource
*resource
)
51 struct wl_drm_buffer
*buffer
= resource
->data
;
52 struct wl_drm
*drm
= buffer
->drm
;
54 drm
->callbacks
->release_buffer(drm
->user_data
, buffer
);
59 buffer_destroy(struct wl_client
*client
, struct wl_resource
*resource
)
61 wl_resource_destroy(resource
);
64 const static struct wl_buffer_interface drm_buffer_interface
= {
69 create_buffer(struct wl_client
*client
, struct wl_resource
*resource
,
70 uint32_t id
, uint32_t name
, int32_t width
, int32_t height
,
72 int32_t offset0
, int32_t stride0
,
73 int32_t offset1
, int32_t stride1
,
74 int32_t offset2
, int32_t stride2
)
76 struct wl_drm
*drm
= resource
->data
;
77 struct wl_drm_buffer
*buffer
;
79 buffer
= calloc(1, sizeof *buffer
);
81 wl_resource_post_no_memory(resource
);
86 buffer
->buffer
.width
= width
;
87 buffer
->buffer
.height
= height
;
88 buffer
->format
= format
;
89 buffer
->offset
[0] = offset0
;
90 buffer
->stride
[0] = stride0
;
91 buffer
->offset
[1] = offset1
;
92 buffer
->stride
[1] = stride1
;
93 buffer
->offset
[2] = offset2
;
94 buffer
->stride
[2] = stride2
;
96 drm
->callbacks
->reference_buffer(drm
->user_data
, name
, buffer
);
97 if (buffer
->driver_buffer
== NULL
) {
98 wl_resource_post_error(resource
,
99 WL_DRM_ERROR_INVALID_NAME
,
104 buffer
->buffer
.resource
.object
.id
= id
;
105 buffer
->buffer
.resource
.object
.interface
= &wl_buffer_interface
;
106 buffer
->buffer
.resource
.object
.implementation
=
107 (void (**)(void)) &drm_buffer_interface
;
108 buffer
->buffer
.resource
.data
= buffer
;
110 buffer
->buffer
.resource
.destroy
= destroy_buffer
;
111 buffer
->buffer
.resource
.client
= resource
->client
;
113 wl_client_add_resource(resource
->client
, &buffer
->buffer
.resource
);
117 drm_create_buffer(struct wl_client
*client
, struct wl_resource
*resource
,
118 uint32_t id
, uint32_t name
, int32_t width
, int32_t height
,
119 uint32_t stride
, uint32_t format
)
122 case WL_DRM_FORMAT_ARGB8888
:
123 case WL_DRM_FORMAT_XRGB8888
:
124 case WL_DRM_FORMAT_YUYV
:
127 wl_resource_post_error(resource
,
128 WL_DRM_ERROR_INVALID_FORMAT
,
133 create_buffer(client
, resource
, id
,
134 name
, width
, height
, format
, 0, stride
, 0, 0, 0, 0);
138 drm_create_planar_buffer(struct wl_client
*client
,
139 struct wl_resource
*resource
,
140 uint32_t id
, uint32_t name
,
141 int32_t width
, int32_t height
, uint32_t format
,
142 int32_t offset0
, int32_t stride0
,
143 int32_t offset1
, int32_t stride1
,
144 int32_t offset2
, int32_t stride2
)
147 case WL_DRM_FORMAT_YUV410
:
148 case WL_DRM_FORMAT_YUV411
:
149 case WL_DRM_FORMAT_YUV420
:
150 case WL_DRM_FORMAT_YUV422
:
151 case WL_DRM_FORMAT_YUV444
:
152 case WL_DRM_FORMAT_NV12
:
153 case WL_DRM_FORMAT_NV16
:
156 wl_resource_post_error(resource
,
157 WL_DRM_ERROR_INVALID_FORMAT
,
162 create_buffer(client
, resource
, id
, name
, width
, height
, format
,
163 offset0
, stride0
, offset1
, stride1
, offset2
, stride2
);
167 drm_authenticate(struct wl_client
*client
,
168 struct wl_resource
*resource
, uint32_t id
)
170 struct wl_drm
*drm
= resource
->data
;
172 if (drm
->callbacks
->authenticate(drm
->user_data
, id
) < 0)
173 wl_resource_post_error(resource
,
174 WL_DRM_ERROR_AUTHENTICATE_FAIL
,
175 "authenicate failed");
177 wl_resource_post_event(resource
, WL_DRM_AUTHENTICATED
);
180 const static struct wl_drm_interface drm_interface
= {
183 drm_create_planar_buffer
187 bind_drm(struct wl_client
*client
, void *data
, uint32_t version
, uint32_t id
)
189 struct wl_drm
*drm
= data
;
190 struct wl_resource
*resource
;
192 resource
= wl_client_add_object(client
, &wl_drm_interface
,
193 &drm_interface
, id
, data
);
194 wl_resource_post_event(resource
, WL_DRM_DEVICE
, drm
->device_name
);
195 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
196 WL_DRM_FORMAT_ARGB8888
);
197 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
198 WL_DRM_FORMAT_XRGB8888
);
199 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV410
);
200 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV411
);
201 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV420
);
202 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV422
);
203 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUV444
);
204 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_NV12
);
205 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_NV16
);
206 wl_resource_post_event(resource
, WL_DRM_FORMAT
, WL_DRM_FORMAT_YUYV
);
210 wayland_drm_init(struct wl_display
*display
, char *device_name
,
211 struct wayland_drm_callbacks
*callbacks
, void *user_data
)
215 drm
= malloc(sizeof *drm
);
217 drm
->display
= display
;
218 drm
->device_name
= strdup(device_name
);
219 drm
->callbacks
= callbacks
;
220 drm
->user_data
= user_data
;
222 wl_display_add_global(display
, &wl_drm_interface
, drm
, bind_drm
);
228 wayland_drm_uninit(struct wl_drm
*drm
)
230 free(drm
->device_name
);
232 /* FIXME: need wl_display_del_{object,global} */
238 wayland_buffer_is_drm(struct wl_buffer
*buffer
)
240 return buffer
->resource
.object
.implementation
==
241 (void (**)(void)) &drm_buffer_interface
;
245 wayland_drm_buffer_get_format(struct wl_buffer
*buffer_base
)
247 struct wl_drm_buffer
*buffer
= (struct wl_drm_buffer
*) buffer_base
;
249 return buffer
->format
;
253 wayland_drm_buffer_get_buffer(struct wl_buffer
*buffer_base
)
255 struct wl_drm_buffer
*buffer
= (struct wl_drm_buffer
*) buffer_base
;
257 return buffer
->driver_buffer
;