2 * Copyright © 2013 Renesas Solutions Corp.
3 * Copyright © 2011 Kristian Høgsberg
4 * Copyright © 2011 Benjamin Franzke
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 * Takanari Hayama <taki@igel.co.jp>
29 * Based on wayland-drm.c by the following authors:
30 * Kristian Høgsberg <krh@bitplanet.net>
31 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include <wayland-server.h>
43 #include "wayland-kms.h"
44 #include "wayland-kms-auth.h"
45 #include "wayland-kms-server-protocol.h"
48 # define WLKMS_DEBUG(s, x...) { printf(s, ##x); }
50 # define WLKMS_DEBUG(s, x...) { }
54 * Taken from EGL/egl.h. Better to refer the egl.h
57 #ifndef EGL_TEXTURE_RGBA
58 # define EGL_TEXTURE_RGBA 0x305E
62 struct wl_display *display;
63 int fd; /* FD for DRM */
66 struct kms_auth *auth; /* for nested authentication */
73 static void destroy_buffer(struct wl_resource *resource)
75 struct wl_kms_buffer *buffer = resource->data;
80 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
82 wl_resource_destroy(resource);
85 const static struct wl_buffer_interface kms_buffer_interface = {
86 .destroy = buffer_destroy
90 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
93 struct wl_kms *kms = resource->data;
96 WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
99 err = kms_auth_request(kms->auth, magic);
101 err = drmAuthMagic(kms->fd, magic);
105 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
106 "authentication failed");
107 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
109 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
110 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
115 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
116 uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
117 uint32_t stride, uint32_t format, uint32_t handle)
119 struct wl_kms *kms = resource->data;
120 struct wl_kms_buffer *buffer;
124 case WL_KMS_FORMAT_ARGB8888:
125 case WL_KMS_FORMAT_XRGB8888:
128 wl_resource_post_error(resource,
129 WL_KMS_ERROR_INVALID_FORMAT,
134 buffer = calloc(1, sizeof *buffer);
135 if (buffer == NULL) {
136 wl_resource_post_no_memory(resource);
141 buffer->width = width;
142 buffer->height = height;
143 buffer->format = format;
144 buffer->stride = stride;
145 buffer->fd = prime_fd;
147 WLKMS_DEBUG("%s: %s: prime_fd=%d\n", __FILE__, __func__, prime_fd);
149 if ((err = drmPrimeFDToHandle(kms->fd, prime_fd, &buffer->handle))) {
150 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
151 wl_resource_post_error(resource,
152 WL_KMS_ERROR_INVALID_FD,
158 struct drm_gem_open op;
164 ret = drmIoctl(kms->fd, DRM_IOCTL_GEM_OPEN, &op);
166 WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_OPEN failed...(%s)\n", __FILE__, __func__, strerror(errno));
167 wl_resource_post_error(resource, WL_KMS_ERROR_INVALID_FD, "invalid prime FD");
170 buffer->handle = op.handle;
174 // We create a wl_buffer
175 buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
176 if (!buffer->resource) {
177 wl_resource_post_no_memory(resource);
182 wl_resource_set_implementation(buffer->resource,
183 (void (**)(void))&kms_buffer_interface,
184 buffer, destroy_buffer);
187 const static struct wl_kms_interface kms_interface = {
188 .authenticate = kms_authenticate,
189 .create_buffer = kms_create_buffer,
193 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
195 struct wl_kms *kms = data;
196 struct wl_resource *resource;
197 uint32_t capabilities;
199 resource = wl_resource_create(client, &wl_kms_interface, version, id);
201 wl_client_post_no_memory(client);
205 wl_resource_set_implementation(resource, &kms_interface, data, NULL);
207 wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
208 wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
209 wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
212 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
214 if (resource == NULL)
217 if (wl_resource_instance_of(resource, &wl_buffer_interface,
218 &kms_buffer_interface))
219 return wl_resource_get_user_data(resource);
224 struct wl_kms *wayland_kms_init(struct wl_display *display,
225 struct wl_display *server, char *device_name, int fd)
229 if (!(kms = calloc(1, sizeof(struct wl_kms))))
232 kms->display = display;
233 kms->device_name = strdup(device_name);
236 wl_global_create(display, &wl_kms_interface, 1, kms, bind_kms);
239 * we're the server in the middle. we should forward the auth
240 * request to our server.
245 if (!(kms->auth = kms_auth_init(server)))
249 if (drmGetMagic(fd, &magic) < 0)
252 /* authenticate myself */
253 if (kms_auth_request(kms->auth, magic) < 0)
264 void wayland_kms_uninit(struct wl_kms *kms)
266 free(kms->device_name);
268 /* FIXME: need wl_display_del_{object,global} */
273 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
275 return buffer->format;
278 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
279 enum wl_kms_attribute attr, int *value)
281 struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
287 *value = buffer->width;
291 *value = buffer->height;
294 case WL_KMS_TEXTURE_FORMAT:
295 *value = EGL_TEXTURE_RGBA;