Release DMABUFs when destroying the buffer.
[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 gem_close;
77         int i, ret;
78
79         for (i = 0; i < buffer->num_planes; i++)
80                 close(buffer->planes[i].fd);
81
82         if (buffer->handle) {
83                 gem_close.handle = buffer->handle;
84                 ret = drmIoctl(buffer->kms->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
85                 if (ret)
86                         WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_CLOSE failed.(%s)\n",
87                                  __FILE__, __func__, strerror(errno));
88         }
89
90         free(buffer);
91 }
92
93 static void
94 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
95 {
96         wl_resource_destroy(resource);
97 }
98
99 const static struct wl_buffer_interface kms_buffer_interface = {
100         .destroy = buffer_destroy
101 };
102
103 static void
104 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
105                  uint32_t magic)
106 {
107         struct wl_kms *kms = resource->data;
108         int err;
109
110         WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
111
112         if (kms->auth) {
113                 err = kms_auth_request(kms->auth, magic);
114         } else {
115                 err = drmAuthMagic(kms->fd, magic);
116         }
117
118         if (err < 0) {
119                 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
120                                        "authentication failed");
121                 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
122         } else {
123                 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
124                 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
125         }
126 }
127
128 static void
129 kms_create_mp_buffer(struct wl_client *client, struct wl_resource *resource,
130                      uint32_t id, int32_t width, int32_t height, uint32_t format,
131                      int32_t fd0, uint32_t stride0, int32_t fd1, uint32_t stride1,
132                      int32_t fd2, uint32_t stride2)
133 {
134         struct wl_kms *kms = resource->data;
135         struct wl_kms_buffer *buffer;
136         int err, nplanes;
137
138         switch (format) {
139         case WL_KMS_FORMAT_ARGB8888:
140         case WL_KMS_FORMAT_XRGB8888:
141         case WL_KMS_FORMAT_ABGR8888:
142         case WL_KMS_FORMAT_XBGR8888:
143         case WL_KMS_FORMAT_RGB888:
144         case WL_KMS_FORMAT_BGR888:
145         case WL_KMS_FORMAT_YUYV:
146         case WL_KMS_FORMAT_UYVY:
147         case WL_KMS_FORMAT_RGB565:
148         case WL_KMS_FORMAT_BGR565:
149                 nplanes = 1;
150                 break;
151
152         case WL_KMS_FORMAT_NV12:
153         case WL_KMS_FORMAT_NV21:
154         case WL_KMS_FORMAT_NV16:
155         case WL_KMS_FORMAT_NV61:
156                 nplanes = 2;
157                 break;
158
159         default:
160                 wl_resource_post_error(resource,
161                                        WL_KMS_ERROR_INVALID_FORMAT,
162                                        "invalid format");
163                 return;
164         }
165
166         buffer = calloc(1, sizeof *buffer);
167         if (buffer == NULL) {
168                 wl_resource_post_no_memory(resource);
169                 return;
170         }
171
172         buffer->kms = kms;
173         buffer->width = width;
174         buffer->height = height;
175         buffer->format = format;
176         buffer->num_planes = nplanes;
177         buffer->stride = buffer->planes[0].stride = stride0;
178         buffer->fd = buffer->planes[0].fd = fd0;
179
180         if (nplanes > 1) {
181                 buffer->planes[1].stride = stride1;
182                 buffer->planes[1].fd = fd1;
183         }
184
185         if (nplanes > 2) {
186                 buffer->planes[2].stride = stride2;
187                 buffer->planes[2].fd = fd2;
188         }
189
190         WLKMS_DEBUG("%s: %s: %d planes (%d, %d, %d)\n", __FILE__, __func__, nplanes, fd0, fd1, fd2);
191
192         // XXX: Do we need to support multiplaner KMS BO?
193         if ((nplanes == 1) && (err = drmPrimeFDToHandle(kms->fd, fd0, &buffer->handle))) {
194                 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
195                 wl_resource_post_error(resource,
196                                        WL_KMS_ERROR_INVALID_FD,
197                                        "invalid prime FD");
198                 return;
199         }
200
201         // We create a wl_buffer
202         buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
203         if (!buffer->resource) {
204                 wl_resource_post_no_memory(resource);
205                 free(buffer);
206                 return;
207         }
208
209         wl_resource_set_implementation(buffer->resource,
210                                        (void (**)(void))&kms_buffer_interface,
211                                        buffer, destroy_buffer);
212 }
213
214
215 static void
216 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
217                   uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
218                   uint32_t stride, uint32_t format, uint32_t handle)
219 {
220         kms_create_mp_buffer(client, resource, id, width, height, format, prime_fd, stride,
221                              0, 0, 0, 0);
222 }
223
224 const static struct wl_kms_interface kms_interface = {
225         .authenticate = kms_authenticate,
226         .create_buffer = kms_create_buffer,
227         .create_mp_buffer = kms_create_mp_buffer,
228 };
229
230 static void
231 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
232 {
233         struct wl_kms *kms = data;
234         struct wl_resource *resource;
235         uint32_t capabilities;
236
237         resource = wl_resource_create(client, &wl_kms_interface, version, id);
238         if (!resource) {
239                 wl_client_post_no_memory(client);
240                 return;
241         }
242
243         wl_resource_set_implementation(resource, &kms_interface, data, NULL);
244
245         wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
246         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
247         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
248 }
249
250 int wayland_kms_fd_get(struct wl_kms* kms)
251 {
252         return kms->fd;
253 }
254
255 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
256 {
257         if (resource == NULL)
258                 return NULL;
259
260         if (wl_resource_instance_of(resource, &wl_buffer_interface,
261                                     &kms_buffer_interface))
262                 return wl_resource_get_user_data(resource);
263         else
264                 return NULL;
265 }
266
267 struct wl_kms *wayland_kms_init(struct wl_display *display,
268                                 struct wl_display *server, char *device_name, int fd)
269 {
270         struct wl_kms *kms;
271
272         if (!(kms = calloc(1, sizeof(struct wl_kms))))
273                 return NULL;
274
275         kms->display = display;
276         kms->device_name = strdup(device_name);
277         kms->fd = fd;
278
279         wl_global_create(display, &wl_kms_interface, 2, kms, bind_kms);
280
281         /*
282          * we're the server in the middle. we should forward the auth
283          * request to our server.
284          */
285         if (server) {
286                 drm_magic_t magic;
287
288                 if (!(kms->auth = kms_auth_init(server)))
289                         goto error;
290
291                 /* get a magic */
292                 if (drmGetMagic(fd, &magic) < 0)
293                         goto error;
294
295                 /* authenticate myself */
296                 if (kms_auth_request(kms->auth, magic) < 0)
297                         goto error;
298         }
299
300         return kms;
301
302 error:
303         free(kms);
304         return NULL;
305 }
306
307 void wayland_kms_uninit(struct wl_kms *kms)
308 {
309         free(kms->device_name);
310
311         /* FIXME: need wl_display_del_{object,global} */
312
313         free(kms);
314 }
315
316 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
317 {
318         return buffer->format;
319 }
320
321 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
322                                 enum wl_kms_attribute attr, int *value)
323 {
324         struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
325         if (!buffer)
326                 return -1;
327
328         switch(attr) {
329         case WL_KMS_WIDTH:
330                 *value = buffer->width;
331                 return 0;
332
333         case WL_KMS_HEIGHT:
334                 *value = buffer->height;
335                 return 0;
336         
337         case WL_KMS_TEXTURE_FORMAT:
338                 *value = EGL_TEXTURE_RGBA;
339                 return 0;
340         }
341
342         return -1;
343 }