xref: /aosp_15_r20/external/mesa3d/src/egl/wayland/wayland-drm/wayland-drm.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2011 Kristian Høgsberg
3  * Copyright © 2011 Benjamin Franzke
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Kristian Høgsberg <[email protected]>
27  *    Benjamin Franzke <[email protected]>
28  */
29 
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "wayland-drm-server-protocol.h"
37 #include "wayland-drm.h"
38 #include <wayland-server.h>
39 
40 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
41 
42 static void
destroy_buffer(struct wl_resource * resource)43 destroy_buffer(struct wl_resource *resource)
44 {
45    struct wl_drm_buffer *buffer = wl_resource_get_user_data(resource);
46    struct wl_drm *drm = buffer->drm;
47 
48    drm->callbacks.release_buffer(drm->user_data, buffer);
49    free(buffer);
50 }
51 
52 static void
buffer_destroy(struct wl_client * client,struct wl_resource * resource)53 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
54 {
55    wl_resource_destroy(resource);
56 }
57 
58 static void
create_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int fd,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)59 create_buffer(struct wl_client *client, struct wl_resource *resource,
60               uint32_t id, uint32_t name, int fd, int32_t width, int32_t height,
61               uint32_t format, int32_t offset0, int32_t stride0,
62               int32_t offset1, int32_t stride1, int32_t offset2,
63               int32_t stride2)
64 {
65    struct wl_drm *drm = wl_resource_get_user_data(resource);
66    struct wl_drm_buffer *buffer;
67 
68    buffer = calloc(1, sizeof *buffer);
69    if (buffer == NULL) {
70       wl_resource_post_no_memory(resource);
71       return;
72    }
73 
74    buffer->drm = drm;
75    buffer->width = width;
76    buffer->height = height;
77    buffer->format = format;
78    buffer->offset[0] = offset0;
79    buffer->stride[0] = stride0;
80    buffer->offset[1] = offset1;
81    buffer->stride[1] = stride1;
82    buffer->offset[2] = offset2;
83    buffer->stride[2] = stride2;
84 
85    drm->callbacks.reference_buffer(drm->user_data, name, fd, buffer);
86    if (buffer->driver_buffer == NULL) {
87       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME,
88                              "invalid name");
89       free(buffer);
90       return;
91    }
92 
93    buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
94    if (!buffer->resource) {
95       wl_resource_post_no_memory(resource);
96       free(buffer);
97       return;
98    }
99 
100    wl_resource_set_implementation(buffer->resource,
101                                   (void (**)(void)) & drm->buffer_interface,
102                                   buffer, destroy_buffer);
103 }
104 
105 static void
drm_create_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int32_t width,int32_t height,uint32_t stride,uint32_t format)106 drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
107                   uint32_t id, uint32_t name, int32_t width, int32_t height,
108                   uint32_t stride, uint32_t format)
109 {
110    switch (format) {
111    case WL_DRM_FORMAT_ABGR2101010:
112    case WL_DRM_FORMAT_XBGR2101010:
113    case WL_DRM_FORMAT_ARGB2101010:
114    case WL_DRM_FORMAT_XRGB2101010:
115    case WL_DRM_FORMAT_ARGB8888:
116    case WL_DRM_FORMAT_XRGB8888:
117    case WL_DRM_FORMAT_YUYV:
118    case WL_DRM_FORMAT_RGB565:
119       break;
120    default:
121       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
122                              "invalid format");
123       return;
124    }
125 
126    create_buffer(client, resource, id, name, -1, width, height, format, 0,
127                  stride, 0, 0, 0, 0);
128 }
129 
130 static void
drm_create_planar_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,uint32_t name,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)131 drm_create_planar_buffer(struct wl_client *client, struct wl_resource *resource,
132                          uint32_t id, uint32_t name, int32_t width,
133                          int32_t height, uint32_t format, int32_t offset0,
134                          int32_t stride0, int32_t offset1, int32_t stride1,
135                          int32_t offset2, int32_t stride2)
136 {
137    switch (format) {
138    case WL_DRM_FORMAT_YUV410:
139    case WL_DRM_FORMAT_YUV411:
140    case WL_DRM_FORMAT_YUV420:
141    case WL_DRM_FORMAT_YUV422:
142    case WL_DRM_FORMAT_YUV444:
143    case WL_DRM_FORMAT_NV12:
144    case WL_DRM_FORMAT_NV16:
145       break;
146    default:
147       wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
148                              "invalid format");
149       return;
150    }
151 
152    create_buffer(client, resource, id, name, -1, width, height, format, offset0,
153                  stride0, offset1, stride1, offset2, stride2);
154 }
155 
156 static void
drm_create_prime_buffer(struct wl_client * client,struct wl_resource * resource,uint32_t id,int fd,int32_t width,int32_t height,uint32_t format,int32_t offset0,int32_t stride0,int32_t offset1,int32_t stride1,int32_t offset2,int32_t stride2)157 drm_create_prime_buffer(struct wl_client *client, struct wl_resource *resource,
158                         uint32_t id, int fd, int32_t width, int32_t height,
159                         uint32_t format, int32_t offset0, int32_t stride0,
160                         int32_t offset1, int32_t stride1, int32_t offset2,
161                         int32_t stride2)
162 {
163    create_buffer(client, resource, id, 0, fd, width, height, format, offset0,
164                  stride0, offset1, stride1, offset2, stride2);
165    close(fd);
166 }
167 
168 static void
drm_authenticate(struct wl_client * client,struct wl_resource * resource,uint32_t id)169 drm_authenticate(struct wl_client *client, struct wl_resource *resource,
170                  uint32_t id)
171 {
172    struct wl_drm *drm = wl_resource_get_user_data(resource);
173 
174    if (!drm->callbacks.authenticate ||
175        drm->callbacks.authenticate(drm->user_data, id) < 0)
176       wl_resource_post_error(resource, WL_DRM_ERROR_AUTHENTICATE_FAIL,
177                              "authenticate failed");
178    else
179       wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
180 }
181 
182 static const struct wl_drm_interface drm_interface = {
183    drm_authenticate,
184    drm_create_buffer,
185    drm_create_planar_buffer,
186    drm_create_prime_buffer,
187 };
188 
189 static void
bind_drm(struct wl_client * client,void * data,uint32_t version,uint32_t id)190 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
191 {
192    struct wl_drm *drm = data;
193    struct wl_resource *resource;
194    uint32_t capabilities;
195 
196    resource =
197       wl_resource_create(client, &wl_drm_interface, MIN(version, 2), id);
198    if (!resource) {
199       wl_client_post_no_memory(client);
200       return;
201    }
202 
203    wl_resource_set_implementation(resource, &drm_interface, data, NULL);
204 
205    wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
206 
207    if (drm->callbacks.is_format_supported(drm->user_data,
208                                           WL_DRM_FORMAT_ARGB2101010)) {
209       wl_resource_post_event(resource, WL_DRM_FORMAT,
210                              WL_DRM_FORMAT_ARGB2101010);
211    }
212 
213    if (drm->callbacks.is_format_supported(drm->user_data,
214                                           WL_DRM_FORMAT_XRGB2101010)) {
215       wl_resource_post_event(resource, WL_DRM_FORMAT,
216                              WL_DRM_FORMAT_XRGB2101010);
217    }
218 
219    if (drm->callbacks.is_format_supported(drm->user_data,
220                                           WL_DRM_FORMAT_ABGR2101010)) {
221       wl_resource_post_event(resource, WL_DRM_FORMAT,
222                              WL_DRM_FORMAT_ABGR2101010);
223    }
224 
225    if (drm->callbacks.is_format_supported(drm->user_data,
226                                           WL_DRM_FORMAT_XBGR2101010)) {
227       wl_resource_post_event(resource, WL_DRM_FORMAT,
228                              WL_DRM_FORMAT_XBGR2101010);
229    }
230 
231    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_ARGB8888);
232    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_XRGB8888);
233    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_RGB565);
234    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV410);
235    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV411);
236    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV420);
237    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV422);
238    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV444);
239    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV12);
240    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV16);
241    wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUYV);
242 
243    capabilities = 0;
244    if (drm->flags & WAYLAND_DRM_PRIME)
245       capabilities |= WL_DRM_CAPABILITY_PRIME;
246 
247    if (version >= 2)
248       wl_resource_post_event(resource, WL_DRM_CAPABILITIES, capabilities);
249 }
250 
251 struct wl_drm *
wayland_drm_init(struct wl_display * display,char * device_name,const struct wayland_drm_callbacks * callbacks,void * user_data,uint32_t flags)252 wayland_drm_init(struct wl_display *display, char *device_name,
253                  const struct wayland_drm_callbacks *callbacks, void *user_data,
254                  uint32_t flags)
255 {
256    struct wl_drm *drm;
257 
258    drm = malloc(sizeof *drm);
259    if (!drm)
260       return NULL;
261 
262    drm->display = display;
263    drm->device_name = strdup(device_name);
264    drm->callbacks = *callbacks;
265    drm->user_data = user_data;
266    drm->flags = flags;
267 
268    drm->buffer_interface.destroy = buffer_destroy;
269 
270    drm->wl_drm_global =
271       wl_global_create(display, &wl_drm_interface, 2, drm, bind_drm);
272 
273    return drm;
274 }
275 
276 void
wayland_drm_uninit(struct wl_drm * drm)277 wayland_drm_uninit(struct wl_drm *drm)
278 {
279    free(drm->device_name);
280 
281    wl_global_destroy(drm->wl_drm_global);
282 
283    free(drm);
284 }
285