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