17 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
19 struct egl_compositor {
20 struct wl_compositor base;
25 struct wl_display *wl_display;
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);
105 sd->surface = EGL_NO_SURFACE;
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;
117 sd = wl_surface_get_data(surface);
121 if (sd->surface != EGL_NO_SURFACE)
122 eglDestroySurface(ec->display, sd->surface);
124 glDeleteTextures(1, &sd->texture);
128 schedule_repaint(ec);
131 void 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)
135 struct egl_compositor *ec = (struct egl_compositor *) compositor;
136 struct surface_data *sd;
138 sd = wl_surface_get_data(surface);
142 if (sd->surface != EGL_NO_SURFACE)
143 eglDestroySurface(ec->display, sd->surface);
145 sd->surface = eglCreatePixmapForName(ec->display, ec->config,
146 name, width, height, stride, NULL);
148 glBindTexture(GL_TEXTURE_2D, sd->texture);
149 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
150 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
151 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
152 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
153 eglBindTexImage(ec->display, sd->surface, GL_TEXTURE_2D);
155 schedule_repaint(ec);
158 void notify_surface_map(struct wl_compositor *compositor,
159 struct wl_surface *surface, struct wl_map *map)
161 struct egl_compositor *ec = (struct egl_compositor *) compositor;
162 struct surface_data *sd;
164 sd = wl_surface_get_data(surface);
170 schedule_repaint(ec);
173 struct wl_compositor_interface interface = {
174 notify_surface_create,
175 notify_surface_destroy,
176 notify_surface_attach,
180 static const char gem_device[] = "/dev/dri/card0";
182 struct wl_compositor *
183 wl_compositor_create(struct wl_display *display)
185 EGLConfig configs[64];
186 EGLint major, minor, count;
187 struct egl_compositor *ec;
188 const int width = 1024, height = 768;
190 ec = malloc(sizeof *ec);
194 ec->base.interface = &interface;
195 ec->wl_display = display;
197 ec->display = eglCreateDisplayNative(gem_device, "i965");
198 if (ec->display == NULL) {
199 fprintf(stderr, "failed to create display\n");
203 if (!eglInitialize(ec->display, &major, &minor)) {
204 fprintf(stderr, "failed to initialize display\n");
208 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
209 fprintf(stderr, "failed to get configs\n");
213 ec->config = configs[24];
214 ec->surface = eglCreateSurfaceNative(ec->display, ec->config,
215 0, 0, width, height);
216 if (ec->surface == NULL) {
217 fprintf(stderr, "failed to create surface\n");
221 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
222 if (ec->context == NULL) {
223 fprintf(stderr, "failed to create context\n");
227 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
228 fprintf(stderr, "failed to make context current\n");
232 glViewport(0, 0, width, height);
233 glMatrixMode(GL_PROJECTION);
235 glOrtho(0, width, height, 0, 0, 1000.0);
236 glMatrixMode(GL_MODELVIEW);
237 glClearColor(0.0, 0.05, 0.2, 0.0);
239 ec->gem_fd = open(gem_device, O_RDWR);
240 if (ec->gem_fd < 0) {
241 fprintf(stderr, "failed to open drm device\n");
245 schedule_repaint(ec);