blob: 40ffc4cf842b04499db9bc123c09448f0737d521 [file] [log] [blame]
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <i915_drm.h>
6#include <sys/ioctl.h>
7#include <sys/mman.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <linux/fb.h>
11
12#include "wayland.h"
13
14#include <GL/gl.h>
15#include <eagle.h>
16
17#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
18
19struct egl_compositor {
20 struct wl_compositor base;
21 EGLDisplay display;
22 EGLSurface surface;
23 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050024 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040025 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026 int gem_fd;
27};
28
29struct surface_data {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040031 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050032 EGLSurface surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040033};
34
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040035static void
36repaint(void *data)
37{
38 struct egl_compositor *ec = data;
39 struct wl_surface_iterator *iterator;
40 struct wl_surface *surface;
41 struct surface_data *sd;
42 GLint vertices[12];
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050043 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040044 GLuint indices[4] = { 0, 1, 2, 3 };
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040045
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);
49 if (sd == NULL)
50 continue;
51
52 vertices[0] = sd->map.x;
53 vertices[1] = sd->map.y;
54 vertices[2] = 0;
55
56 vertices[3] = sd->map.x;
57 vertices[4] = sd->map.y + sd->map.height;
58 vertices[5] = 0;
59
60 vertices[6] = sd->map.x + sd->map.width;
Kristian Høgsberg48a33ba2008-10-12 12:56:11 -040061 vertices[7] = sd->map.y;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040062 vertices[8] = 0;
63
64 vertices[9] = sd->map.x + sd->map.width;
Kristian Høgsberg48a33ba2008-10-12 12:56:11 -040065 vertices[10] = sd->map.y + sd->map.height;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040066 vertices[11] = 0;
67
68 glBindTexture(GL_TEXTURE_2D, sd->texture);
69 glEnable(GL_TEXTURE_2D);
70 glEnable(GL_BLEND);
Kristian Høgsberg3f59e822008-11-03 06:35:46 -050071 /* 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);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040074
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);
Kristian Høgsberg48a33ba2008-10-12 12:56:11 -040079 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -040080 }
81 wl_surface_iterator_destroy(iterator);
82
83 glFlush();
84
85 eglSwapBuffers(ec->display, ec->surface);
86}
87
88static void schedule_repaint(struct egl_compositor *ec)
89{
90 struct wl_event_loop *loop;
91
92 loop = wl_display_get_event_loop(ec->wl_display);
93 wl_event_loop_add_idle(loop, repaint, ec);
94}
95
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040096void notify_surface_create(struct wl_compositor *compositor,
97 struct wl_surface *surface)
98{
99 struct surface_data *sd;
100
101 sd = malloc(sizeof *sd);
102 if (sd == NULL)
103 return;
104
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500105 sd->surface = EGL_NO_SURFACE;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400106 wl_surface_set_data(surface, sd);
107
108 glGenTextures(1, &sd->texture);
109}
110
111void notify_surface_destroy(struct wl_compositor *compositor,
112 struct wl_surface *surface)
113{
114 struct egl_compositor *ec = (struct egl_compositor *) compositor;
115 struct surface_data *sd;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400116
117 sd = wl_surface_get_data(surface);
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500118 if (sd == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400119 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500120
121 if (sd->surface != EGL_NO_SURFACE)
122 eglDestroySurface(ec->display, sd->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400123
124 glDeleteTextures(1, &sd->texture);
125
126 free(sd);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400127
128 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400129}
130
131void notify_surface_attach(struct wl_compositor *compositor,
132 struct wl_surface *surface, uint32_t name,
133 uint32_t width, uint32_t height, uint32_t stride)
134{
135 struct egl_compositor *ec = (struct egl_compositor *) compositor;
136 struct surface_data *sd;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400137
138 sd = wl_surface_get_data(surface);
139 if (sd == NULL)
140 return;
141
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500142 if (sd->surface != EGL_NO_SURFACE)
143 eglDestroySurface(ec->display, sd->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400144
Kristian Høgsberg56f3c712008-11-05 07:55:45 -0500145 /* FIXME: We need to use a single buffer config without depth
146 * or stencil buffers here to keep egl from creating auxillary
147 * buffers for the pixmap here. */
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500148 sd->surface = eglCreatePixmapForName(ec->display, ec->config,
149 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400150
151 glBindTexture(GL_TEXTURE_2D, sd->texture);
152 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
153 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
154 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
155 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500156 eglBindTexImage(ec->display, sd->surface, GL_TEXTURE_2D);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400157
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400158 schedule_repaint(ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400159}
160
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400161void notify_surface_map(struct wl_compositor *compositor,
162 struct wl_surface *surface, struct wl_map *map)
163{
164 struct egl_compositor *ec = (struct egl_compositor *) compositor;
165 struct surface_data *sd;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400166
167 sd = wl_surface_get_data(surface);
168 if (sd == NULL)
169 return;
170
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400171 sd->map = *map;
Kristian Høgsberg5ebb3172008-10-11 19:21:35 -0400172
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400173 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400174}
175
176struct wl_compositor_interface interface = {
177 notify_surface_create,
178 notify_surface_destroy,
179 notify_surface_attach,
180 notify_surface_map
181};
182
183static const char gem_device[] = "/dev/dri/card0";
184
185struct wl_compositor *
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400186wl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400187{
188 EGLConfig configs[64];
189 EGLint major, minor, count;
190 struct egl_compositor *ec;
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400191 const int width = 1024, height = 768;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400192
193 ec = malloc(sizeof *ec);
194 if (ec == NULL)
195 return NULL;
196
197 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400198 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400199
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400200 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400201 if (ec->display == NULL) {
202 fprintf(stderr, "failed to create display\n");
203 return NULL;
204 }
205
206 if (!eglInitialize(ec->display, &major, &minor)) {
207 fprintf(stderr, "failed to initialize display\n");
208 return NULL;
209 }
210
211 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
212 fprintf(stderr, "failed to get configs\n");
213 return NULL;
214 }
215
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500216 ec->config = configs[24];
217 ec->surface = eglCreateSurfaceNative(ec->display, ec->config,
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400218 0, 0, width, height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400219 if (ec->surface == NULL) {
220 fprintf(stderr, "failed to create surface\n");
221 return NULL;
222 }
223
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500224 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400225 if (ec->context == NULL) {
226 fprintf(stderr, "failed to create context\n");
227 return NULL;
228 }
229
230 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
231 fprintf(stderr, "failed to make context current\n");
232 return NULL;
233 }
234
235 glViewport(0, 0, width, height);
236 glMatrixMode(GL_PROJECTION);
237 glLoadIdentity();
238 glOrtho(0, width, height, 0, 0, 1000.0);
239 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400240 glClearColor(0.0, 0.05, 0.2, 0.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400241
242 ec->gem_fd = open(gem_device, O_RDWR);
243 if (ec->gem_fd < 0) {
244 fprintf(stderr, "failed to open drm device\n");
245 return NULL;
246 }
247
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400248 schedule_repaint(ec);
249
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400250 return &ec->base;
251}