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"
39 /* Git master of Wayland is moving towards a stable version of the
40 * protocol, but breaking from 0.85 in the process. For the time
41 * being, it's convenient to be able to build Mesa against both master
42 * and 0.85.x of Wayland. To make this work we'll do a compile-time
43 * version check and work around the difference in API and protocol */
44 #if defined (WAYLAND_VERSION_MAJOR) && \
45 WAYLAND_VERSION_MAJOR == 0 && \
46 WAYLAND_VERSION_MINOR == 85
47 #define HAS_WAYLAND_0_85
51 struct wl_display
*display
;
56 struct wayland_drm_callbacks
*callbacks
;
60 destroy_buffer(struct wl_resource
*resource
)
62 struct wl_drm_buffer
*buffer
= resource
->data
;
63 struct wl_drm
*drm
= buffer
->drm
;
65 drm
->callbacks
->release_buffer(drm
->user_data
, buffer
);
70 buffer_destroy(struct wl_client
*client
, struct wl_resource
*resource
)
72 #ifdef HAS_WAYLAND_0_85
73 wl_resource_destroy(resource
, 0);
75 wl_resource_destroy(resource
);
79 #ifdef HAS_WAYLAND_0_85
81 buffer_damage(struct wl_client
*client
, struct wl_resource
*buffer
,
82 int32_t x
, int32_t y
, int32_t width
, int32_t height
)
87 const static struct wl_buffer_interface drm_buffer_interface
= {
88 #ifdef HAS_WAYLAND_0_85
95 drm_create_buffer(struct wl_client
*client
, struct wl_resource
*resource
,
96 uint32_t id
, uint32_t name
, int32_t width
, int32_t height
,
97 uint32_t stride
, uint32_t format
)
99 struct wl_drm
*drm
= resource
->data
;
100 struct wl_drm_buffer
*buffer
;
103 case WL_DRM_FORMAT_ARGB8888
:
104 case WL_DRM_FORMAT_XRGB8888
:
107 wl_resource_post_error(resource
,
108 WL_DRM_ERROR_INVALID_FORMAT
,
113 buffer
= calloc(1, sizeof *buffer
);
114 if (buffer
== NULL
) {
115 wl_resource_post_no_memory(resource
);
120 buffer
->buffer
.width
= width
;
121 buffer
->buffer
.height
= height
;
122 buffer
->format
= format
;
124 buffer
->stride0
= stride
;
126 drm
->callbacks
->reference_buffer(drm
->user_data
, name
, buffer
);
127 if (buffer
->driver_buffer
== NULL
) {
128 wl_resource_post_error(resource
,
129 WL_DRM_ERROR_INVALID_NAME
,
134 buffer
->buffer
.resource
.object
.id
= id
;
135 buffer
->buffer
.resource
.object
.interface
= &wl_buffer_interface
;
136 buffer
->buffer
.resource
.object
.implementation
=
137 (void (**)(void)) &drm_buffer_interface
;
138 buffer
->buffer
.resource
.data
= buffer
;
140 buffer
->buffer
.resource
.destroy
= destroy_buffer
;
141 buffer
->buffer
.resource
.client
= resource
->client
;
143 wl_client_add_resource(resource
->client
, &buffer
->buffer
.resource
);
147 drm_authenticate(struct wl_client
*client
,
148 struct wl_resource
*resource
, uint32_t id
)
150 struct wl_drm
*drm
= resource
->data
;
152 if (drm
->callbacks
->authenticate(drm
->user_data
, id
) < 0)
153 wl_resource_post_error(resource
,
154 WL_DRM_ERROR_AUTHENTICATE_FAIL
,
155 "authenicate failed");
157 wl_resource_post_event(resource
, WL_DRM_AUTHENTICATED
);
160 const static struct wl_drm_interface drm_interface
= {
166 bind_drm(struct wl_client
*client
, void *data
, uint32_t version
, uint32_t id
)
168 struct wl_drm
*drm
= data
;
169 struct wl_resource
*resource
;
171 resource
= wl_client_add_object(client
, &wl_drm_interface
,
172 &drm_interface
, id
, data
);
173 wl_resource_post_event(resource
, WL_DRM_DEVICE
, drm
->device_name
);
174 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
175 WL_DRM_FORMAT_ARGB8888
);
176 wl_resource_post_event(resource
, WL_DRM_FORMAT
,
177 WL_DRM_FORMAT_XRGB8888
);
181 wayland_drm_init(struct wl_display
*display
, char *device_name
,
182 struct wayland_drm_callbacks
*callbacks
, void *user_data
)
186 drm
= malloc(sizeof *drm
);
188 drm
->display
= display
;
189 drm
->device_name
= strdup(device_name
);
190 drm
->callbacks
= callbacks
;
191 drm
->user_data
= user_data
;
193 wl_display_add_global(display
, &wl_drm_interface
, drm
, bind_drm
);
199 wayland_drm_uninit(struct wl_drm
*drm
)
201 free(drm
->device_name
);
203 /* FIXME: need wl_display_del_{object,global} */
209 wayland_buffer_is_drm(struct wl_buffer
*buffer
)
211 return buffer
->resource
.object
.implementation
==
212 (void (**)(void)) &drm_buffer_interface
;
216 wayland_drm_buffer_get_format(struct wl_buffer
*buffer_base
)
218 struct wl_drm_buffer
*buffer
= (struct wl_drm_buffer
*) buffer_base
;
220 return buffer
->format
;
224 wayland_drm_buffer_get_buffer(struct wl_buffer
*buffer_base
)
226 struct wl_drm_buffer
*buffer
= (struct wl_drm_buffer
*) buffer_base
;
228 return buffer
->driver_buffer
;