wayland: Track changes to drop wl_visual
[mesa.git] / src / egl / wayland / wayland-drm / wayland-drm.c
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 <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stddef.h>
34
35 #include <wayland-server.h>
36 #include "wayland-drm.h"
37 #include "wayland-drm-server-protocol.h"
38
39 struct wl_drm {
40 struct wl_display *display;
41
42 void *user_data;
43 char *device_name;
44
45 struct wayland_drm_callbacks *callbacks;
46 };
47
48 struct wl_drm_buffer {
49 struct wl_buffer buffer;
50 struct wl_drm *drm;
51 uint32_t format;
52
53 void *driver_buffer;
54 };
55
56 static void
57 buffer_damage(struct wl_client *client, struct wl_resource *buffer,
58 int32_t x, int32_t y, int32_t width, int32_t height)
59 {
60 }
61
62 static void
63 destroy_buffer(struct wl_resource *resource)
64 {
65 struct wl_drm_buffer *buffer = resource->data;
66 struct wl_drm *drm = buffer->drm;
67
68 drm->callbacks->release_buffer(drm->user_data,
69 buffer->driver_buffer);
70 free(buffer);
71 }
72
73 static void
74 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
75 {
76 wl_resource_destroy(resource, 0);
77 }
78
79 const static struct wl_buffer_interface drm_buffer_interface = {
80 buffer_damage,
81 buffer_destroy
82 };
83
84 static void
85 drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
86 uint32_t id, uint32_t name, int32_t width, int32_t height,
87 uint32_t stride, uint32_t format)
88 {
89 struct wl_drm *drm = resource->data;
90 struct wl_drm_buffer *buffer;
91
92 switch (format) {
93 case WL_DRM_FORMAT_ARGB32:
94 case WL_DRM_FORMAT_PREMULTIPLIED_ARGB32:
95 case WL_DRM_FORMAT_XRGB32:
96 break;
97 default:
98 wl_resource_post_error(resource,
99 WL_DRM_ERROR_INVALID_FORMAT,
100 "invalid format");
101 return;
102 }
103
104 buffer = calloc(1, sizeof *buffer);
105 if (buffer == NULL) {
106 wl_client_post_no_memory(resource->client);
107 return;
108 }
109
110 buffer->drm = drm;
111 buffer->buffer.width = width;
112 buffer->buffer.height = height;
113 buffer->format = format;
114
115 buffer->driver_buffer =
116 drm->callbacks->reference_buffer(drm->user_data, name,
117 width, height,
118 stride, format);
119
120 if (buffer->driver_buffer == NULL) {
121 wl_resource_post_error(resource,
122 WL_DRM_ERROR_INVALID_NAME,
123 "invalid name");
124 return;
125 }
126
127 buffer->buffer.resource.object.id = id;
128 buffer->buffer.resource.object.interface = &wl_buffer_interface;
129 buffer->buffer.resource.object.implementation =
130 (void (**)(void)) &drm_buffer_interface;
131 buffer->buffer.resource.data = buffer;
132
133 buffer->buffer.resource.destroy = destroy_buffer;
134 buffer->buffer.resource.client = resource->client;
135
136 wl_client_add_resource(resource->client, &buffer->buffer.resource);
137 }
138
139 static void
140 drm_authenticate(struct wl_client *client,
141 struct wl_resource *resource, uint32_t id)
142 {
143 struct wl_drm *drm = resource->data;
144
145 if (drm->callbacks->authenticate(drm->user_data, id) < 0)
146 wl_resource_post_error(resource,
147 WL_DRM_ERROR_AUTHENTICATE_FAIL,
148 "authenicate failed");
149 else
150 wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
151 }
152
153 const static struct wl_drm_interface drm_interface = {
154 drm_authenticate,
155 drm_create_buffer
156 };
157
158 static void
159 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
160 {
161 struct wl_drm *drm = data;
162 struct wl_resource *resource;
163
164 resource = wl_client_add_object(client, &wl_drm_interface,
165 &drm_interface, id, data);
166 wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
167 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_ARGB32);
168 wl_resource_post_event(resource, WL_DRM_FORMAT,
169 WL_DRM_FORMAT_PREMULTIPLIED_ARGB32);
170 wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_XRGB32);
171 }
172
173 struct wl_drm *
174 wayland_drm_init(struct wl_display *display, char *device_name,
175 struct wayland_drm_callbacks *callbacks, void *user_data)
176 {
177 struct wl_drm *drm;
178
179 drm = malloc(sizeof *drm);
180
181 drm->display = display;
182 drm->device_name = strdup(device_name);
183 drm->callbacks = callbacks;
184 drm->user_data = user_data;
185
186 wl_display_add_global(display, &wl_drm_interface, drm, bind_drm);
187
188 return drm;
189 }
190
191 void
192 wayland_drm_uninit(struct wl_drm *drm)
193 {
194 free(drm->device_name);
195
196 /* FIXME: need wl_display_del_{object,global} */
197
198 free(drm);
199 }
200
201 int
202 wayland_buffer_is_drm(struct wl_buffer *buffer)
203 {
204 return buffer->resource.object.implementation ==
205 (void (**)(void)) &drm_buffer_interface;
206 }
207
208 void *
209 wayland_drm_buffer_get_buffer(struct wl_buffer *buffer_base)
210 {
211 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base;
212
213 return buffer->driver_buffer;
214 }