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;
76 struct drm_gem_close close;
80 close.handle = buffer->handle;
81 ret = drmIoctl(buffer->kms->fd, DRM_IOCTL_GEM_CLOSE, &close);
83 WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_CLOSE failed.(%s)\n",
84 __FILE__, __func__, strerror(errno));
91 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
93 wl_resource_destroy(resource);
96 const static struct wl_buffer_interface kms_buffer_interface = {
97 .destroy = buffer_destroy
101 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
104 struct wl_kms *kms = resource->data;
107 WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
110 err = kms_auth_request(kms->auth, magic);
112 err = drmAuthMagic(kms->fd, magic);
116 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
117 "authentication failed");
118 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
120 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
121 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
126 kms_create_mp_buffer(struct wl_client *client, struct wl_resource *resource,
127 uint32_t id, int32_t width, int32_t height, uint32_t format,
128 int32_t fd0, uint32_t stride0, int32_t fd1, uint32_t stride1,
129 int32_t fd2, uint32_t stride2)
131 struct wl_kms *kms = resource->data;
132 struct wl_kms_buffer *buffer;
136 case WL_KMS_FORMAT_ARGB8888:
137 case WL_KMS_FORMAT_XRGB8888:
138 case WL_KMS_FORMAT_ABGR8888:
139 case WL_KMS_FORMAT_XBGR8888:
140 case WL_KMS_FORMAT_RGB888:
141 case WL_KMS_FORMAT_BGR888:
142 case WL_KMS_FORMAT_YUYV:
143 case WL_KMS_FORMAT_UYVY:
144 case WL_KMS_FORMAT_RGB565:
145 case WL_KMS_FORMAT_BGR565:
149 case WL_KMS_FORMAT_NV12:
150 case WL_KMS_FORMAT_NV21:
151 case WL_KMS_FORMAT_NV16:
152 case WL_KMS_FORMAT_NV61:
157 wl_resource_post_error(resource,
158 WL_KMS_ERROR_INVALID_FORMAT,
163 buffer = calloc(1, sizeof *buffer);
164 if (buffer == NULL) {
165 wl_resource_post_no_memory(resource);
170 buffer->width = width;
171 buffer->height = height;
172 buffer->format = format;
173 buffer->num_planes = nplanes;
174 buffer->stride = buffer->planes[0].stride = stride0;
175 buffer->fd = buffer->planes[0].fd = fd0;
178 buffer->planes[1].stride = stride1;
179 buffer->planes[1].fd = fd1;
183 buffer->planes[2].stride = stride2;
184 buffer->planes[2].fd = fd2;
187 WLKMS_DEBUG("%s: %s: %d planes (%d, %d, %d)\n", __FILE__, __func__, nplanes, fd0, fd1, fd2);
189 // XXX: Do we need to support multiplaner KMS BO?
190 if ((nplanes == 1) && (err = drmPrimeFDToHandle(kms->fd, fd0, &buffer->handle))) {
191 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
192 wl_resource_post_error(resource,
193 WL_KMS_ERROR_INVALID_FD,
198 // We create a wl_buffer
199 buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
200 if (!buffer->resource) {
201 wl_resource_post_no_memory(resource);
206 wl_resource_set_implementation(buffer->resource,
207 (void (**)(void))&kms_buffer_interface,
208 buffer, destroy_buffer);
213 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
214 uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
215 uint32_t stride, uint32_t format, uint32_t handle)
217 kms_create_mp_buffer(client, resource, id, width, height, format, prime_fd, stride,
221 const static struct wl_kms_interface kms_interface = {
222 .authenticate = kms_authenticate,
223 .create_buffer = kms_create_buffer,
224 .create_mp_buffer = kms_create_mp_buffer,
228 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
230 struct wl_kms *kms = data;
231 struct wl_resource *resource;
232 uint32_t capabilities;
234 resource = wl_resource_create(client, &wl_kms_interface, version, id);
236 wl_client_post_no_memory(client);
240 wl_resource_set_implementation(resource, &kms_interface, data, NULL);
242 wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
243 wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
244 wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
247 int wayland_kms_fd_get(struct wl_kms* kms)
252 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
254 if (resource == NULL)
257 if (wl_resource_instance_of(resource, &wl_buffer_interface,
258 &kms_buffer_interface))
259 return wl_resource_get_user_data(resource);
264 struct wl_kms *wayland_kms_init(struct wl_display *display,
265 struct wl_display *server, char *device_name, int fd)
269 if (!(kms = calloc(1, sizeof(struct wl_kms))))
272 kms->display = display;
273 kms->device_name = strdup(device_name);
276 wl_global_create(display, &wl_kms_interface, 2, kms, bind_kms);
279 * we're the server in the middle. we should forward the auth
280 * request to our server.
285 if (!(kms->auth = kms_auth_init(server)))
289 if (drmGetMagic(fd, &magic) < 0)
292 /* authenticate myself */
293 if (kms_auth_request(kms->auth, magic) < 0)
304 void wayland_kms_uninit(struct wl_kms *kms)
306 free(kms->device_name);
308 /* FIXME: need wl_display_del_{object,global} */
313 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
315 return buffer->format;
318 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
319 enum wl_kms_attribute attr, int *value)
321 struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
327 *value = buffer->width;
331 *value = buffer->height;
334 case WL_KMS_TEXTURE_FORMAT:
335 *value = EGL_TEXTURE_RGBA;