Merge remote branch 'origin/nvc0'
[mesa.git] / src / egl / wayland / wayland-drm / wayland-drm.c
1 /*
2 * Copyright © 2011 Kristian Høgsberg
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stddef.h>
33
34 #include <wayland-server.h>
35 #include "wayland-drm.h"
36 #include "wayland-drm-server-protocol.h"
37
38 #include "egldisplay.h"
39 #include "egldriver.h"
40 #include "eglimage.h"
41 #include "egltypedefs.h"
42
43 struct wl_drm {
44 struct wl_object object;
45 struct wl_display *display;
46
47 _EGLDisplay *edisp;
48
49 char *device_name;
50 authenticate_t authenticate;
51 };
52
53 static void
54 drm_buffer_damage(struct wl_buffer *buffer_base,
55 struct wl_surface *surface,
56 int32_t x, int32_t y, int32_t width, int32_t height)
57 {
58 }
59
60 static void
61 destroy_buffer(struct wl_resource *resource, struct wl_client *client)
62 {
63 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) resource;
64 _EGLDriver *drv = buffer->drm->edisp->Driver;
65
66 drv->API.DestroyImageKHR(drv, buffer->drm->edisp, buffer->image);
67 free(buffer);
68 }
69
70 static void
71 buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
72 {
73 wl_resource_destroy(&buffer->resource, client);
74 }
75
76 const static struct wl_buffer_interface buffer_interface = {
77 buffer_destroy
78 };
79
80 static void
81 drm_create_buffer(struct wl_client *client, struct wl_drm *drm,
82 uint32_t id, uint32_t name, int32_t width, int32_t height,
83 uint32_t stride, struct wl_visual *visual)
84 {
85 struct wl_drm_buffer *buffer;
86 EGLint attribs[] = {
87 EGL_WIDTH, 0,
88 EGL_HEIGHT, 0,
89 EGL_DRM_BUFFER_STRIDE_MESA, 0,
90 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
91 EGL_NONE
92 };
93 _EGLDriver *drv = drm->edisp->Driver;
94
95 buffer = malloc(sizeof *buffer);
96 if (buffer == NULL) {
97 wl_client_post_no_memory(client);
98 return;
99 }
100
101 buffer->drm = drm;
102 buffer->buffer.compositor = NULL;
103 buffer->buffer.width = width;
104 buffer->buffer.height = height;
105 buffer->buffer.visual = visual;
106 buffer->buffer.attach = NULL;
107 buffer->buffer.damage = drm_buffer_damage;
108
109 if (visual->object.interface != &wl_visual_interface) {
110 /* FIXME: Define a real exception event instead of
111 * abusing this one */
112 wl_client_post_event(client,
113 (struct wl_object *) drm->display,
114 WL_DISPLAY_INVALID_OBJECT, 0);
115 fprintf(stderr, "invalid visual in create_buffer\n");
116 return;
117 }
118
119 attribs[1] = width;
120 attribs[3] = height;
121 attribs[5] = stride / 4;
122 buffer->image = drv->API.CreateImageKHR(drv, drm->edisp,
123 EGL_NO_CONTEXT,
124 EGL_DRM_BUFFER_MESA,
125 (EGLClientBuffer) (intptr_t) name,
126 attribs);
127
128 if (buffer->image == NULL) {
129 /* FIXME: Define a real exception event instead of
130 * abusing this one */
131 wl_client_post_event(client,
132 (struct wl_object *) drm->display,
133 WL_DISPLAY_INVALID_OBJECT, 0);
134 fprintf(stderr, "failed to create image for name %d\n", name);
135 return;
136 }
137
138 buffer->buffer.resource.object.id = id;
139 buffer->buffer.resource.object.interface = &wl_buffer_interface;
140 buffer->buffer.resource.object.implementation = (void (**)(void))
141 &buffer_interface;
142
143 buffer->buffer.resource.destroy = destroy_buffer;
144
145 wl_client_add_resource(client, &buffer->buffer.resource);
146 }
147
148 static void
149 drm_authenticate(struct wl_client *client,
150 struct wl_drm *drm, uint32_t id)
151 {
152 if (drm->authenticate(drm->edisp, id) < 0)
153 wl_client_post_event(client,
154 (struct wl_object *) drm->display,
155 WL_DISPLAY_INVALID_OBJECT, 0);
156 else
157 wl_client_post_event(client, &drm->object,
158 WL_DRM_AUTHENTICATED);
159 }
160
161 const static struct wl_drm_interface drm_interface = {
162 drm_authenticate,
163 drm_create_buffer
164 };
165
166 static void
167 post_drm_device(struct wl_client *client, struct wl_object *global)
168 {
169 struct wl_drm *drm = (struct wl_drm *) global;
170
171 wl_client_post_event(client, global, WL_DRM_DEVICE, drm->device_name);
172 }
173
174 struct wl_drm *
175 wayland_drm_init(struct wl_display *display, _EGLDisplay *disp,
176 authenticate_t authenticate, char *device_name)
177 {
178 struct wl_drm *drm;
179
180 drm = malloc(sizeof *drm);
181
182 drm->display = display;
183 drm->edisp = disp;
184 drm->authenticate = authenticate;
185 drm->device_name = strdup(device_name);
186
187 drm->object.interface = &wl_drm_interface;
188 drm->object.implementation = (void (**)(void)) &drm_interface;
189 wl_display_add_object(display, &drm->object);
190 wl_display_add_global(display, &drm->object, post_drm_device);
191
192 return drm;
193 }
194
195 void
196 wayland_drm_destroy(struct wl_drm *drm)
197 {
198 free(drm->device_name);
199
200 /* FIXME: need wl_display_del_{object,global} */
201
202 free(drm);
203 }