wayland-drm: Implement wl_buffer.damage in old versions of Wayland
[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 /* Git master of Wayland is moving towards a stable version of the
40 * protocol, but breaking from 0.85 in the process. For the time
41 * being, it's convenient to be able to build Mesa against both master
42 * and 0.85.x of Wayland. To make this work we'll do a compile-time
43 * version check and work around the difference in API and protocol */
44 #if defined (WAYLAND_VERSION_MAJOR) && \
45 WAYLAND_VERSION_MAJOR == 0 && \
46 WAYLAND_VERSION_MINOR == 85
47 #define HAS_WAYLAND_0_85
48 #endif
49
50 struct wl_drm {
51 struct wl_display *display;
52
53 void *user_data;
54 char *device_name;
55
56 struct wayland_drm_callbacks *callbacks;
57 };
58
59 struct wl_drm_buffer {
60 struct wl_buffer buffer;
61 struct wl_drm *drm;
62 uint32_t format;
63
64 void *driver_buffer;
65 };
66
67 static void
68 destroy_buffer(struct wl_resource *resource)
69 {
70 struct wl_drm_buffer *buffer = resource->data;
71 struct wl_drm *drm = buffer->drm;
72
73 drm->callbacks->release_buffer(drm->user_data,
74 buffer->driver_buffer);
75 free(buffer);
76 }
77
78 static void
79 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
80 {
81 #ifdef HAS_WAYLAND_0_85
82 wl_resource_destroy(resource, 0);
83 #else
84 wl_resource_destroy(resource);
85 #endif
86 }
87
88 #ifdef HAS_WAYLAND_0_85
89 static void
90 buffer_damage(struct wl_client *client, struct wl_resource *buffer,
91 int32_t x, int32_t y, int32_t width, int32_t height)
92 {
93 }
94 #endif
95
96 const static struct wl_buffer_interface drm_buffer_interface = {
97 #ifdef HAS_WAYLAND_0_85
98 buffer_damage,
99 #endif
100 buffer_destroy
101 };
102
103 static void
104 drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
105 uint32_t id, uint32_t name, int32_t width, int32_t height,
106 uint32_t stride, uint32_t format)
107 {
108 struct wl_drm *drm = resource->data;
109 struct wl_drm_buffer *buffer;
110
111 switch (format) {
112 case WL_DRM_FORMAT_ARGB8888:
113 case WL_DRM_FORMAT_XRGB8888:
114 break;
115 default:
116 wl_resource_post_error(resource,
117 WL_DRM_ERROR_INVALID_FORMAT,
118 "invalid format");
119 return;
120 }
121
122 buffer = calloc(1, sizeof *buffer);
123 if (buffer == NULL) {
124 wl_resource_post_no_memory(resource);
125 return;
126 }
127
128 buffer->drm = drm;
129 buffer->buffer.width = width;
130 buffer->buffer.height = height;
131 buffer->format = format;
132
133 buffer->driver_buffer =
134 drm->callbacks->reference_buffer(drm->user_data, name,
135 width, height,
136 stride, format);
137
138 if (buffer->driver_buffer == NULL) {
139 wl_resource_post_error(resource,
140 WL_DRM_ERROR_INVALID_NAME,
141 "invalid name");
142 return;
143 }
144
145 buffer->buffer.resource.object.id = id;
146 buffer->buffer.resource.object.interface = &wl_buffer_interface;
147 buffer->buffer.resource.object.implementation =
148 (void (**)(void)) &drm_buffer_interface;
149 buffer->buffer.resource.data = buffer;
150
151 buffer->buffer.resource.destroy = destroy_buffer;
152 buffer->buffer.resource.client = resource->client;
153
154 wl_client_add_resource(resource->client, &buffer->buffer.resource);
155 }
156
157 static void
158 drm_authenticate(struct wl_client *client,
159 struct wl_resource *resource, uint32_t id)
160 {
161 struct wl_drm *drm = resource->data;
162
163 if (drm->callbacks->authenticate(drm->user_data, id) < 0)
164 wl_resource_post_error(resource,
165 WL_DRM_ERROR_AUTHENTICATE_FAIL,
166 "authenicate failed");
167 else
168 wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
169 }
170
171 const static struct wl_drm_interface drm_interface = {
172 drm_authenticate,
173 drm_create_buffer
174 };
175
176 static void
177 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
178 {
179 struct wl_drm *drm = data;
180 struct wl_resource *resource;
181
182 resource = wl_client_add_object(client, &wl_drm_interface,
183 &drm_interface, id, data);
184 wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
185 wl_resource_post_event(resource, WL_DRM_FORMAT,
186 WL_DRM_FORMAT_ARGB8888);
187 wl_resource_post_event(resource, WL_DRM_FORMAT,
188 WL_DRM_FORMAT_XRGB8888);
189 }
190
191 struct wl_drm *
192 wayland_drm_init(struct wl_display *display, char *device_name,
193 struct wayland_drm_callbacks *callbacks, void *user_data)
194 {
195 struct wl_drm *drm;
196
197 drm = malloc(sizeof *drm);
198
199 drm->display = display;
200 drm->device_name = strdup(device_name);
201 drm->callbacks = callbacks;
202 drm->user_data = user_data;
203
204 wl_display_add_global(display, &wl_drm_interface, drm, bind_drm);
205
206 return drm;
207 }
208
209 void
210 wayland_drm_uninit(struct wl_drm *drm)
211 {
212 free(drm->device_name);
213
214 /* FIXME: need wl_display_del_{object,global} */
215
216 free(drm);
217 }
218
219 int
220 wayland_buffer_is_drm(struct wl_buffer *buffer)
221 {
222 return buffer->resource.object.implementation ==
223 (void (**)(void)) &drm_buffer_interface;
224 }
225
226 uint32_t
227 wayland_drm_buffer_get_format(struct wl_buffer *buffer_base)
228 {
229 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base;
230
231 return buffer->format;
232 }
233
234 void *
235 wayland_drm_buffer_get_buffer(struct wl_buffer *buffer_base)
236 {
237 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base;
238
239 return buffer->driver_buffer;
240 }