blob: d10e20b86a8cc8151e441ee298d183fee502c394 [file] [log] [blame]
Kristian Høgsberg084d41a2010-08-18 14:51:26 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "compositor.h"
24
25static void
26drm_authenticate(struct wl_client *client,
27 struct wl_drm *drm_base, uint32_t id)
28{
29 struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
30 struct wlsc_compositor *compositor =
31 container_of(drm, struct wlsc_compositor, drm);
32
33 if (compositor->authenticate(compositor, id) < 0)
34 wl_client_post_event(client,
35 (struct wl_object *) compositor->wl_display,
36 WL_DISPLAY_INVALID_OBJECT, 0);
37 else
38 wl_client_post_event(client, &drm->base, WL_DRM_AUTHENTICATED);
39}
40
41static void
42destroy_buffer(struct wl_resource *resource, struct wl_client *client)
43{
44 struct wlsc_buffer *buffer =
45 container_of(resource, struct wlsc_buffer, base.base);
46
47#if 0
48 /* FIXME: Need backlink to EGL display here */
49 eglDestroyImageKHR(ec->display, buffer->image);
50#endif
51
52 free(buffer);
53}
54
55static void
56buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
57{
58 wl_resource_destroy(&buffer->base, client);
59}
60
61const static struct wl_buffer_interface buffer_interface = {
62 buffer_destroy
63};
64
65static void
66drm_create_buffer(struct wl_client *client, struct wl_drm *drm_base,
67 uint32_t id, uint32_t name, int32_t width, int32_t height,
68 uint32_t stride, struct wl_visual *visual)
69{
70 struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
71 struct wlsc_compositor *compositor =
72 container_of(drm, struct wlsc_compositor, drm);
73 struct wlsc_buffer *buffer;
74 EGLint attribs[] = {
75 EGL_WIDTH, 0,
76 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -040077 EGL_DRM_BUFFER_STRIDE_MESA, 0,
78 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsberg084d41a2010-08-18 14:51:26 -040079 EGL_NONE
80 };
81
82 if (visual != &compositor->argb_visual &&
83 visual != &compositor->premultiplied_argb_visual &&
84 visual != &compositor->rgb_visual) {
85 /* FIXME: Define a real exception event instead of
86 * abusing this one */
87 wl_client_post_event(client,
88 (struct wl_object *) compositor->wl_display,
89 WL_DISPLAY_INVALID_OBJECT, 0);
90 fprintf(stderr, "invalid visual in create_buffer\n");
91 return;
92 }
93
94 buffer = malloc(sizeof *buffer);
95 if (buffer == NULL) {
96 wl_client_post_event(client,
97 (struct wl_object *) compositor->wl_display,
98 WL_DISPLAY_NO_MEMORY, 0);
99 return;
100 }
101
102 attribs[1] = width;
103 attribs[3] = height;
104 attribs[5] = stride / 4;
105
106 buffer->width = width;
107 buffer->height = height;
108 buffer->visual = visual;
109 buffer->image = eglCreateImageKHR(compositor->display,
110 compositor->context,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400111 EGL_DRM_BUFFER_MESA,
Kristian Høgsberg084d41a2010-08-18 14:51:26 -0400112 (EGLClientBuffer) name, attribs);
113 if (buffer->image == NULL) {
114 /* FIXME: Define a real exception event instead of
115 * abusing this one */
116 free(buffer);
117 wl_client_post_event(client,
118 (struct wl_object *) compositor->wl_display,
119 WL_DISPLAY_INVALID_OBJECT, 0);
120 fprintf(stderr, "failed to create image for name %d\n", name);
121 return;
122 }
123
124 buffer->base.base.base.id = id;
125 buffer->base.base.base.interface = &wl_buffer_interface;
126 buffer->base.base.base.implementation = (void (**)(void))
127 &buffer_interface;
128
129 buffer->base.base.destroy = destroy_buffer;
130
131 wl_client_add_resource(client, &buffer->base.base);
132}
133
134const static struct wl_drm_interface drm_interface = {
135 drm_authenticate,
136 drm_create_buffer
137};
138
139static void
140post_drm_device(struct wl_client *client, struct wl_object *global)
141{
142 struct wlsc_drm *drm = container_of(global, struct wlsc_drm, base);
143
144 wl_client_post_event(client, global, WL_DRM_DEVICE, drm->filename);
145}
146
147int
148wlsc_drm_init(struct wlsc_compositor *ec, int fd, const char *filename)
149{
150 struct wlsc_drm *drm = &ec->drm;
151
152 drm->fd = fd;
153 drm->filename = strdup(filename);
154 if (drm->filename == NULL)
155 return -1;
156
157 drm->base.interface = &wl_drm_interface;
158 drm->base.implementation = (void (**)(void)) &drm_interface;
159 wl_display_add_object(ec->wl_display, &drm->base);
160 wl_display_add_global(ec->wl_display, &drm->base, post_drm_device);
161
162 return 0;
163}