17 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
19 struct egl_compositor {
20 struct wl_compositor base;
24 struct wl_display *wl_display;
30 int32_t width, height, stride;
38 struct egl_compositor *ec = data;
39 struct wl_surface_iterator *iterator;
40 struct wl_surface *surface;
41 struct surface_data *sd;
43 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
44 GLuint indices[4] = { 0, 1, 2, 3 };
46 iterator = wl_surface_iterator_create(ec->wl_display, 0);
47 while (wl_surface_iterator_next(iterator, &surface)) {
48 sd = wl_surface_get_data(surface);
52 vertices[0] = sd->map.x;
53 vertices[1] = sd->map.y;
56 vertices[3] = sd->map.x;
57 vertices[4] = sd->map.y + sd->map.height;
60 vertices[6] = sd->map.x + sd->map.width;
61 vertices[7] = sd->map.y;
64 vertices[9] = sd->map.x + sd->map.width;
65 vertices[10] = sd->map.y + sd->map.height;
68 glBindTexture(GL_TEXTURE_2D, sd->texture);
69 glEnable(GL_TEXTURE_2D);
71 /* Assume pre-multiplied alpha for now, this probably
72 * needs to be a wayland visual type of thing. */
73 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
75 glEnableClientState(GL_VERTEX_ARRAY);
76 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
77 glVertexPointer(3, GL_INT, 0, vertices);
78 glTexCoordPointer(2, GL_INT, 0, tex_coords);
79 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
81 wl_surface_iterator_destroy(iterator);
85 eglSwapBuffers(ec->display, ec->surface);
88 static void schedule_repaint(struct egl_compositor *ec)
90 struct wl_event_loop *loop;
92 loop = wl_display_get_event_loop(ec->wl_display);
93 wl_event_loop_add_idle(loop, repaint, ec);
96 void notify_surface_create(struct wl_compositor *compositor,
97 struct wl_surface *surface)
99 struct surface_data *sd;
101 sd = malloc(sizeof *sd);
106 wl_surface_set_data(surface, sd);
108 glGenTextures(1, &sd->texture);
111 void notify_surface_destroy(struct wl_compositor *compositor,
112 struct wl_surface *surface)
114 struct egl_compositor *ec = (struct egl_compositor *) compositor;
115 struct surface_data *sd;
116 struct drm_gem_close close_arg;
119 sd = wl_surface_get_data(surface);
120 if (sd == NULL || sd->handle == 0)
123 close_arg.handle = sd->handle;
124 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
126 fprintf(stderr, "failed to gem_close handle %d: %m\n", sd->handle);
129 glDeleteTextures(1, &sd->texture);
133 schedule_repaint(ec);
136 void notify_surface_attach(struct wl_compositor *compositor,
137 struct wl_surface *surface, uint32_t name,
138 uint32_t width, uint32_t height, uint32_t stride)
140 struct egl_compositor *ec = (struct egl_compositor *) compositor;
141 struct surface_data *sd;
142 struct drm_gem_open open_arg;
143 struct drm_gem_close close_arg;
144 struct drm_i915_gem_pread pread;
149 sd = wl_surface_get_data(surface);
153 if (sd->handle != 0) {
154 close_arg.handle = sd->handle;
155 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
157 fprintf(stderr, "failed to gem_close name %d: %m\n", name);
161 open_arg.name = name;
162 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
164 fprintf(stderr, "failed to gem_open name %d, fd=%d: %m\n", name, ec->gem_fd);
168 sd->handle = open_arg.handle;
173 size = sd->height * sd->stride;
176 fprintf(stderr, "swap buffers malloc failed\n");
180 pread.handle = sd->handle;
184 pread.data_ptr = (long) data;
186 if (ioctl(ec->gem_fd, DRM_IOCTL_I915_GEM_PREAD, &pread)) {
187 fprintf(stderr, "gem pread failed\n");
191 glBindTexture(GL_TEXTURE_2D, sd->texture);
192 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
193 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
194 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
195 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
197 GL_BGRA, GL_UNSIGNED_BYTE, data);
201 schedule_repaint(ec);
204 void notify_surface_map(struct wl_compositor *compositor,
205 struct wl_surface *surface, struct wl_map *map)
207 struct egl_compositor *ec = (struct egl_compositor *) compositor;
208 struct surface_data *sd;
210 sd = wl_surface_get_data(surface);
216 schedule_repaint(ec);
219 struct wl_compositor_interface interface = {
220 notify_surface_create,
221 notify_surface_destroy,
222 notify_surface_attach,
226 static const char gem_device[] = "/dev/dri/card0";
228 struct wl_compositor *
229 wl_compositor_create(struct wl_display *display)
231 EGLConfig configs[64];
232 EGLint major, minor, count;
233 struct egl_compositor *ec;
234 const int width = 1024, height = 768;
236 ec = malloc(sizeof *ec);
240 ec->base.interface = &interface;
241 ec->wl_display = display;
243 ec->display = eglCreateDisplayNative(gem_device, "i965");
244 if (ec->display == NULL) {
245 fprintf(stderr, "failed to create display\n");
249 if (!eglInitialize(ec->display, &major, &minor)) {
250 fprintf(stderr, "failed to initialize display\n");
254 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
255 fprintf(stderr, "failed to get configs\n");
259 ec->surface = eglCreateSurfaceNative(ec->display, configs[24],
260 0, 0, width, height);
261 if (ec->surface == NULL) {
262 fprintf(stderr, "failed to create surface\n");
266 ec->context = eglCreateContext(ec->display, configs[24], NULL, NULL);
267 if (ec->context == NULL) {
268 fprintf(stderr, "failed to create context\n");
272 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
273 fprintf(stderr, "failed to make context current\n");
277 glViewport(0, 0, width, height);
278 glMatrixMode(GL_PROJECTION);
280 glOrtho(0, width, height, 0, 0, 1000.0);
281 glMatrixMode(GL_MODELVIEW);
282 glClearColor(0.0, 0.05, 0.2, 0.0);
284 ec->gem_fd = open(gem_device, O_RDWR);
285 if (ec->gem_fd < 0) {
286 fprintf(stderr, "failed to open drm device\n");
290 schedule_repaint(ec);