Merge remote-tracking branch 'origin/master' into pipe-video
[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_object object;
41 struct wl_display *display;
42
43 void *user_data;
44 char *device_name;
45
46 struct wayland_drm_callbacks *callbacks;
47 };
48
49 struct wl_drm_buffer {
50 struct wl_buffer buffer;
51 struct wl_drm *drm;
52
53 void *driver_buffer;
54 };
55
56 static void
57 buffer_damage(struct wl_client *client, struct wl_buffer *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, struct wl_client *client)
64 {
65 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) resource;
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_buffer *buffer)
75 {
76 wl_resource_destroy(&buffer->resource, client, 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_drm *drm,
86 uint32_t id, uint32_t name, int32_t width, int32_t height,
87 uint32_t stride, struct wl_visual *visual)
88 {
89 struct wl_drm_buffer *buffer;
90
91 buffer = calloc(1, sizeof *buffer);
92 if (buffer == NULL) {
93 wl_client_post_no_memory(client);
94 return;
95 }
96
97 buffer->drm = drm;
98 buffer->buffer.width = width;
99 buffer->buffer.height = height;
100 buffer->buffer.visual = visual;
101 buffer->buffer.client = client;
102
103 if (!visual || visual->object.interface != &wl_visual_interface) {
104 wl_client_post_error(client, &drm->object,
105 WL_DRM_ERROR_INVALID_VISUAL,
106 "invalid visual");
107 free(buffer);
108 return;
109 }
110
111 buffer->driver_buffer =
112 drm->callbacks->reference_buffer(drm->user_data, name,
113 width, height,
114 stride, visual);
115
116 if (buffer->driver_buffer == NULL) {
117 wl_client_post_error(client, &drm->object,
118 WL_DRM_ERROR_INVALID_NAME,
119 "invalid name");
120 return;
121 }
122
123 buffer->buffer.resource.object.id = id;
124 buffer->buffer.resource.object.interface = &wl_buffer_interface;
125 buffer->buffer.resource.object.implementation = (void (**)(void))
126 &drm_buffer_interface;
127
128 buffer->buffer.resource.destroy = destroy_buffer;
129
130 wl_client_add_resource(client, &buffer->buffer.resource);
131 }
132
133 static void
134 drm_authenticate(struct wl_client *client,
135 struct wl_drm *drm, uint32_t id)
136 {
137 if (drm->callbacks->authenticate(drm->user_data, id) < 0)
138 wl_client_post_error(client, &drm->object,
139 WL_DRM_ERROR_AUTHENTICATE_FAIL,
140 "authenicate failed");
141 else
142 wl_client_post_event(client, &drm->object,
143 WL_DRM_AUTHENTICATED);
144 }
145
146 const static struct wl_drm_interface drm_interface = {
147 drm_authenticate,
148 drm_create_buffer
149 };
150
151 static void
152 post_drm_device(struct wl_client *client,
153 struct wl_object *global, uint32_t version)
154 {
155 struct wl_drm *drm = (struct wl_drm *) global;
156
157 wl_client_post_event(client, global, WL_DRM_DEVICE, drm->device_name);
158 }
159
160 struct wl_drm *
161 wayland_drm_init(struct wl_display *display, char *device_name,
162 struct wayland_drm_callbacks *callbacks, void *user_data)
163 {
164 struct wl_drm *drm;
165
166 drm = malloc(sizeof *drm);
167
168 drm->display = display;
169 drm->device_name = strdup(device_name);
170 drm->callbacks = callbacks;
171 drm->user_data = user_data;
172
173 drm->object.interface = &wl_drm_interface;
174 drm->object.implementation = (void (**)(void)) &drm_interface;
175 wl_display_add_object(display, &drm->object);
176 wl_display_add_global(display, &drm->object, post_drm_device);
177
178 return drm;
179 }
180
181 void
182 wayland_drm_uninit(struct wl_drm *drm)
183 {
184 free(drm->device_name);
185
186 /* FIXME: need wl_display_del_{object,global} */
187
188 free(drm);
189 }
190
191 int
192 wayland_buffer_is_drm(struct wl_buffer *buffer)
193 {
194 return buffer->resource.object.implementation ==
195 (void (**)(void)) &drm_buffer_interface;
196 }
197
198 void *
199 wayland_drm_buffer_get_buffer(struct wl_buffer *buffer_base)
200 {
201 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) buffer_base;
202
203 return buffer->driver_buffer;
204 }