1bb35342d013587d607a803e0065d0bf3905de64
[platform/adaptation/renesas_rcar/wayland-kms.git] / wayland-kms.c
1 /*
2  * Copyright © 2013 Renesas Solutions Corp.
3  * Copyright © 2011 Kristian Høgsberg
4  * Copyright © 2011 Benjamin Franzke
5  *
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:
12  *
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
15  * Software.
16  *
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.
25  *
26  * Authors:
27  *    Takanari Hayama <taki@igel.co.jp>
28  *
29  * Based on wayland-drm.c by the following authors:
30  *    Kristian Høgsberg <krh@bitplanet.net>
31  *    Benjamin Franzke <benjaminfranzke@googlemail.com>
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <unistd.h>
39 #include <errno.h>
40
41 #include <xf86drm.h>
42 #include <wayland-server.h>
43 #include "wayland-kms.h"
44 #include "wayland-kms-auth.h"
45 #include "wayland-kms-server-protocol.h"
46
47 #if defined(DEBUG)
48 #       define WLKMS_DEBUG(s, x...) { printf(s, ##x); }
49 #else
50 #       define WLKMS_DEBUG(s, x...) { }
51 #endif
52
53 /*
54  * Taken from EGL/egl.h. Better to refer the egl.h
55  * in the future.
56  */
57 #ifndef EGL_TEXTURE_RGBA
58 #       define EGL_TEXTURE_RGBA         0x305E
59 #endif
60
61 struct wl_kms {
62         struct wl_display *display;
63         int fd;                         /* FD for DRM */
64         char *device_name;
65
66         struct kms_auth *auth;          /* for nested authentication */
67 };
68
69 /*
70  * wl_kms server
71  */
72
73 static void destroy_buffer(struct wl_resource *resource)
74 {
75         struct wl_kms_buffer *buffer = resource->data;
76         struct drm_gem_close close;
77         int ret;
78
79         if (buffer->handle) {
80                 close.handle = buffer->handle;
81                 ret = drmIoctl(buffer->kms->fd, DRM_IOCTL_GEM_CLOSE, &close);
82                 if (ret)
83                         WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_CLOSE failed.(%s)\n",
84                                  __FILE__, __func__, strerror(errno));
85         }
86
87         free(buffer);
88 }
89
90 static void
91 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
92 {
93         wl_resource_destroy(resource);
94 }
95
96 const static struct wl_buffer_interface kms_buffer_interface = {
97         .destroy = buffer_destroy
98 };
99
100 static void
101 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
102                  uint32_t magic)
103 {
104         struct wl_kms *kms = resource->data;
105         int err;
106
107         WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
108
109         if (kms->auth) {
110                 err = kms_auth_request(kms->auth, magic);
111         } else {
112                 err = drmAuthMagic(kms->fd, magic);
113         }
114
115         if (err < 0) {
116                 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
117                                        "authentication failed");
118                 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
119         } else {
120                 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
121                 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
122         }
123 }
124
125 static void
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)
130 {
131         struct wl_kms *kms = resource->data;
132         struct wl_kms_buffer *buffer;
133         int err, nplanes;
134
135         switch (format) {
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:
146                 nplanes = 1;
147                 break;
148
149         case WL_KMS_FORMAT_NV12:
150         case WL_KMS_FORMAT_NV21:
151         case WL_KMS_FORMAT_NV16:
152         case WL_KMS_FORMAT_NV61:
153                 nplanes = 2;
154                 break;
155
156         default:
157                 wl_resource_post_error(resource,
158                                        WL_KMS_ERROR_INVALID_FORMAT,
159                                        "invalid format");
160                 return;
161         }
162
163         buffer = calloc(1, sizeof *buffer);
164         if (buffer == NULL) {
165                 wl_resource_post_no_memory(resource);
166                 return;
167         }
168
169         buffer->kms = kms;
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;
176
177         WLKMS_DEBUG("%s: %s: %d planes (%d, %d, %d)\n", __FILE__, __func__, nplanes, fd0, fd1, fd2);
178
179         // XXX: Do we need to support multiplaner KMS BO?
180         if ((err = drmPrimeFDToHandle(kms->fd, fd0, &buffer->handle))) {
181                 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
182                 wl_resource_post_error(resource,
183                                        WL_KMS_ERROR_INVALID_FD,
184                                        "invalid prime FD");
185                 return;
186         }
187
188         // We create a wl_buffer
189         buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
190         if (!buffer->resource) {
191                 wl_resource_post_no_memory(resource);
192                 free(buffer);
193                 return;
194         }
195
196         wl_resource_set_implementation(buffer->resource,
197                                        (void (**)(void))&kms_buffer_interface,
198                                        buffer, destroy_buffer);
199 }
200
201
202 static void
203 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
204                   uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
205                   uint32_t stride, uint32_t format, uint32_t handle)
206 {
207         kms_create_mp_buffer(client, resource, id, width, height, format, prime_fd, stride,
208                              0, 0, 0, 0);
209 }
210
211 const static struct wl_kms_interface kms_interface = {
212         .authenticate = kms_authenticate,
213         .create_buffer = kms_create_buffer,
214         .create_mp_buffer = kms_create_mp_buffer,
215 };
216
217 static void
218 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
219 {
220         struct wl_kms *kms = data;
221         struct wl_resource *resource;
222         uint32_t capabilities;
223
224         resource = wl_resource_create(client, &wl_kms_interface, version, id);
225         if (!resource) {
226                 wl_client_post_no_memory(client);
227                 return;
228         }
229
230         wl_resource_set_implementation(resource, &kms_interface, data, NULL);
231
232         wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
233         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
234         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
235 }
236
237 int wayland_kms_fd_get(struct wl_kms* kms)
238 {
239         return kms->fd;
240 }
241
242 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
243 {
244         if (resource == NULL)
245                 return NULL;
246
247         if (wl_resource_instance_of(resource, &wl_buffer_interface,
248                                     &kms_buffer_interface))
249                 return wl_resource_get_user_data(resource);
250         else
251                 return NULL;
252 }
253
254 struct wl_kms *wayland_kms_init(struct wl_display *display,
255                                 struct wl_display *server, char *device_name, int fd)
256 {
257         struct wl_kms *kms;
258
259         if (!(kms = calloc(1, sizeof(struct wl_kms))))
260                 return NULL;
261
262         kms->display = display;
263         kms->device_name = strdup(device_name);
264         kms->fd = fd;
265
266         wl_global_create(display, &wl_kms_interface, 2, kms, bind_kms);
267
268         /*
269          * we're the server in the middle. we should forward the auth
270          * request to our server.
271          */
272         if (server) {
273                 drm_magic_t magic;
274
275                 if (!(kms->auth = kms_auth_init(server)))
276                         goto error;
277
278                 /* get a magic */
279                 if (drmGetMagic(fd, &magic) < 0)
280                         goto error;
281
282                 /* authenticate myself */
283                 if (kms_auth_request(kms->auth, magic) < 0)
284                         goto error;
285         }
286
287         return kms;
288
289 error:
290         free(kms);
291         return NULL;
292 }
293
294 void wayland_kms_uninit(struct wl_kms *kms)
295 {
296         free(kms->device_name);
297
298         /* FIXME: need wl_display_del_{object,global} */
299
300         free(kms);
301 }
302
303 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
304 {
305         return buffer->format;
306 }
307
308 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
309                                 enum wl_kms_attribute attr, int *value)
310 {
311         struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
312         if (!buffer)
313                 return -1;
314
315         switch(attr) {
316         case WL_KMS_WIDTH:
317                 *value = buffer->width;
318                 return 0;
319
320         case WL_KMS_HEIGHT:
321                 *value = buffer->height;
322                 return 0;
323         
324         case WL_KMS_TEXTURE_FORMAT:
325                 *value = EGL_TEXTURE_RGBA;
326                 return 0;
327         }
328
329         return -1;
330 }