78b916a0e5de404f8927d88e13174daf92e882a0
[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-server-protocol.h"
45
46 #if defined(DEBUG)
47 #       define WLKMS_DEBUG(s, x...) { printf(s, ##x); }
48 #else
49 #       define WLKMS_DEBUG(s, x...) { }
50 #endif
51
52 struct wl_kms {
53         struct wl_display *display;
54         int fd;                         /* FD for DRM */
55         char *device_name;
56 };
57
58 static void destroy_buffer(struct wl_resource *resource)
59 {
60         struct wl_kms_buffer *buffer = resource->data;
61         free(buffer);
62 }
63
64 static void
65 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
66 {
67         wl_resource_destroy(resource);
68 }
69
70 const static struct wl_buffer_interface kms_buffer_interface = {
71         .destroy = buffer_destroy
72 };
73
74 static void
75 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
76                  uint32_t magic)
77 {
78         struct wl_kms *kms = resource->data;
79         int err;
80
81         WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
82
83         if (drmAuthMagic(kms->fd, magic) < 0) {
84                 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
85                                        "authentication failed");
86                 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
87         } else {
88                 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
89                 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
90         }
91 }
92
93 static void
94 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
95                   uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
96                   uint32_t stride, uint32_t format, uint32_t handle)
97 {
98         struct wl_kms *kms = resource->data;
99         struct wl_kms_buffer *buffer;
100         int err;
101
102         switch (format) {
103         case WL_KMS_FORMAT_ARGB8888:
104         case WL_KMS_FORMAT_XRGB8888:
105                 break;
106         default:
107                 wl_resource_post_error(resource,
108                                        WL_KMS_ERROR_INVALID_FORMAT,
109                                        "invalid format");
110                 return;
111         }
112
113         buffer = calloc(1, sizeof *buffer);
114         if (buffer == NULL) {
115                 wl_resource_post_no_memory(resource);
116                 return;
117         }
118
119         buffer->kms = kms;
120         buffer->width = width;
121         buffer->height = height;
122         buffer->format = format;
123         buffer->stride = stride;
124         buffer->fd = prime_fd;
125
126         WLKMS_DEBUG("%s: %s: prime_fd=%d\n", __FILE__, __func__, prime_fd);
127 #if 0
128         if ((err = drmPrimeFDToHandle(kms->fd, prime_fd, &buffer->handle))) {
129                 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
130                 wl_resource_post_error(resource,
131                                        WL_KMS_ERROR_INVALID_FD,
132                                        "invalid prime FD");
133                 return;
134         }
135 #else
136         {
137                 struct drm_gem_open op;
138                 int ret;
139
140                 op.name   = prime_fd;
141                 op.handle = 0;
142
143                 ret = drmIoctl(kms->fd, DRM_IOCTL_GEM_OPEN, &op);
144                 if (ret) {
145                         WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_OPEN failed...(%s)\n", __FILE__, __func__, strerror(errno));
146                         wl_resource_post_error(resource, WL_KMS_ERROR_INVALID_FD, "invalid prime FD");
147                         return;
148                 }
149                 buffer->handle = op.handle;
150         }
151 #endif
152
153         // We create a wl_buffer
154         buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
155         if (!buffer->resource) {
156                 wl_resource_post_no_memory(resource);
157                 free(buffer);
158                 return;
159         }
160
161         wl_resource_set_implementation(buffer->resource,
162                                        (void (**)(void))&kms_buffer_interface,
163                                        buffer, destroy_buffer);
164 }
165
166 const static struct wl_kms_interface kms_interface = {
167         .authenticate = kms_authenticate,
168         .create_buffer = kms_create_buffer,
169 };
170
171 static void
172 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
173 {
174         struct wl_kms *kms = data;
175         struct wl_resource *resource;
176         uint32_t capabilities;
177
178         resource = wl_resource_create(client, &wl_kms_interface, version, id);
179         if (!resource) {
180                 wl_client_post_no_memory(client);
181                 return;
182         }
183
184         wl_resource_set_implementation(resource, &kms_interface, data, NULL);
185
186         wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
187         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
188         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
189 }
190
191 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
192 {
193         if (resource == NULL)
194                 return NULL;
195
196         if (wl_resource_instance_of(resource, &wl_buffer_interface,
197                                     &kms_buffer_interface))
198                 return wl_resource_get_user_data(resource);
199         else
200                 return NULL;
201 }
202
203 struct wl_kms *wayland_kms_init(struct wl_display *display, char *device_name, int fd)
204 {
205         struct wl_kms *kms;
206
207         if (!(kms = calloc(1, sizeof(struct wl_kms))))
208                 return NULL;
209
210         kms->display = display;
211         kms->device_name = strdup(device_name);
212         kms->fd = fd;
213
214         wl_global_create(display, &wl_kms_interface, 1, kms, bind_kms);
215
216         return kms;
217 }
218
219 void wayland_kms_uninit(struct wl_kms *kms)
220 {
221         free(kms->device_name);
222
223         /* FIXME: need wl_display_del_{object,global} */
224
225         free(kms);
226 }
227
228 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
229 {
230         return buffer->format;
231 }
232
233 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
234                                 enum wl_kms_attribute attr, int *value)
235 {
236         struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
237         if (!buffer)
238                 return -1;
239
240         switch(attr) {
241         case WL_KMS_WIDTH:
242                 *value = buffer->width;
243                 return 0;
244
245         case WL_KMS_HEIGHT:
246                 *value = buffer->height;
247                 return 0;
248         
249         case WL_KMS_TEXTURE_FORMAT:
250                 *value = WL_KMS_FORMAT_ARGB8888;
251                 return 0;
252         }
253
254         return -1;
255 }