Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1 | #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 | |
| 19 | struct egl_compositor { |
| 20 | struct wl_compositor base; |
| 21 | EGLDisplay display; |
| 22 | EGLSurface surface; |
| 23 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 24 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 25 | struct wl_display *wl_display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 26 | int gem_fd; |
| 27 | }; |
| 28 | |
| 29 | struct surface_data { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 30 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 31 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 32 | EGLSurface surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 35 | static void |
| 36 | repaint(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øgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 43 | GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 44 | GLuint indices[4] = { 0, 1, 2, 3 }; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 45 | |
| 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øgsberg | 48a33ba | 2008-10-12 12:56:11 -0400 | [diff] [blame] | 61 | vertices[7] = sd->map.y; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 62 | vertices[8] = 0; |
| 63 | |
| 64 | vertices[9] = sd->map.x + sd->map.width; |
Kristian Høgsberg | 48a33ba | 2008-10-12 12:56:11 -0400 | [diff] [blame] | 65 | vertices[10] = sd->map.y + sd->map.height; |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 66 | vertices[11] = 0; |
| 67 | |
| 68 | glBindTexture(GL_TEXTURE_2D, sd->texture); |
| 69 | glEnable(GL_TEXTURE_2D); |
| 70 | glEnable(GL_BLEND); |
Kristian Høgsberg | 3f59e82 | 2008-11-03 06:35:46 -0500 | [diff] [blame] | 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); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 74 | |
| 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øgsberg | 48a33ba | 2008-10-12 12:56:11 -0400 | [diff] [blame] | 79 | glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 80 | } |
| 81 | wl_surface_iterator_destroy(iterator); |
| 82 | |
| 83 | glFlush(); |
| 84 | |
| 85 | eglSwapBuffers(ec->display, ec->surface); |
| 86 | } |
| 87 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 88 | static void |
| 89 | schedule_repaint(struct egl_compositor *ec) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 90 | { |
| 91 | struct wl_event_loop *loop; |
| 92 | |
| 93 | loop = wl_display_get_event_loop(ec->wl_display); |
| 94 | wl_event_loop_add_idle(loop, repaint, ec); |
| 95 | } |
| 96 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 97 | static void |
| 98 | notify_surface_create(struct wl_compositor *compositor, |
| 99 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 100 | { |
| 101 | struct surface_data *sd; |
| 102 | |
| 103 | sd = malloc(sizeof *sd); |
| 104 | if (sd == NULL) |
| 105 | return; |
| 106 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 107 | sd->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 108 | wl_surface_set_data(surface, sd); |
| 109 | |
| 110 | glGenTextures(1, &sd->texture); |
| 111 | } |
| 112 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 113 | static void |
| 114 | notify_surface_destroy(struct wl_compositor *compositor, |
| 115 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 116 | { |
| 117 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 118 | struct surface_data *sd; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 119 | |
| 120 | sd = wl_surface_get_data(surface); |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 121 | if (sd == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 122 | return; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 123 | |
| 124 | if (sd->surface != EGL_NO_SURFACE) |
| 125 | eglDestroySurface(ec->display, sd->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 126 | |
| 127 | glDeleteTextures(1, &sd->texture); |
| 128 | |
| 129 | free(sd); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 130 | |
| 131 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 134 | static void |
| 135 | notify_surface_attach(struct wl_compositor *compositor, |
| 136 | struct wl_surface *surface, uint32_t name, |
| 137 | uint32_t width, uint32_t height, uint32_t stride) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 138 | { |
| 139 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 140 | struct surface_data *sd; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 141 | |
| 142 | sd = wl_surface_get_data(surface); |
| 143 | if (sd == NULL) |
| 144 | return; |
| 145 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 146 | if (sd->surface != EGL_NO_SURFACE) |
| 147 | eglDestroySurface(ec->display, sd->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 148 | |
Kristian Høgsberg | 56f3c71 | 2008-11-05 07:55:45 -0500 | [diff] [blame] | 149 | /* FIXME: We need to use a single buffer config without depth |
| 150 | * or stencil buffers here to keep egl from creating auxillary |
| 151 | * buffers for the pixmap here. */ |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 152 | sd->surface = eglCreatePixmapForName(ec->display, ec->config, |
| 153 | name, width, height, stride, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 154 | |
| 155 | glBindTexture(GL_TEXTURE_2D, sd->texture); |
| 156 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 157 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 158 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 159 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 160 | eglBindTexImage(ec->display, sd->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 161 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 162 | schedule_repaint(ec); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 165 | static void |
| 166 | notify_surface_map(struct wl_compositor *compositor, |
| 167 | struct wl_surface *surface, struct wl_map *map) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 168 | { |
| 169 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 170 | struct surface_data *sd; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 171 | |
| 172 | sd = wl_surface_get_data(surface); |
| 173 | if (sd == NULL) |
| 174 | return; |
| 175 | |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 176 | sd->map = *map; |
Kristian Høgsberg | 5ebb317 | 2008-10-11 19:21:35 -0400 | [diff] [blame] | 177 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 178 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame^] | 181 | static const struct wl_compositor_interface interface = { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 182 | notify_surface_create, |
| 183 | notify_surface_destroy, |
| 184 | notify_surface_attach, |
| 185 | notify_surface_map |
| 186 | }; |
| 187 | |
| 188 | static const char gem_device[] = "/dev/dri/card0"; |
| 189 | |
| 190 | struct wl_compositor * |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 191 | wl_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 192 | { |
| 193 | EGLConfig configs[64]; |
| 194 | EGLint major, minor, count; |
| 195 | struct egl_compositor *ec; |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 196 | const int width = 1024, height = 768; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 197 | |
| 198 | ec = malloc(sizeof *ec); |
| 199 | if (ec == NULL) |
| 200 | return NULL; |
| 201 | |
| 202 | ec->base.interface = &interface; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 203 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 204 | |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 205 | ec->display = eglCreateDisplayNative(gem_device, "i965"); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 206 | if (ec->display == NULL) { |
| 207 | fprintf(stderr, "failed to create display\n"); |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 212 | fprintf(stderr, "failed to initialize display\n"); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) { |
| 217 | fprintf(stderr, "failed to get configs\n"); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 221 | ec->config = configs[24]; |
| 222 | ec->surface = eglCreateSurfaceNative(ec->display, ec->config, |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 223 | 0, 0, width, height); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 224 | if (ec->surface == NULL) { |
| 225 | fprintf(stderr, "failed to create surface\n"); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 229 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 230 | if (ec->context == NULL) { |
| 231 | fprintf(stderr, "failed to create context\n"); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) { |
| 236 | fprintf(stderr, "failed to make context current\n"); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | glViewport(0, 0, width, height); |
| 241 | glMatrixMode(GL_PROJECTION); |
| 242 | glLoadIdentity(); |
| 243 | glOrtho(0, width, height, 0, 0, 1000.0); |
| 244 | glMatrixMode(GL_MODELVIEW); |
Kristian Høgsberg | a234e70 | 2008-10-11 22:13:51 -0400 | [diff] [blame] | 245 | glClearColor(0.0, 0.05, 0.2, 0.0); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 246 | |
| 247 | ec->gem_fd = open(gem_device, O_RDWR); |
| 248 | if (ec->gem_fd < 0) { |
| 249 | fprintf(stderr, "failed to open drm device\n"); |
| 250 | return NULL; |
| 251 | } |
| 252 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 253 | schedule_repaint(ec); |
| 254 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 255 | return &ec->base; |
| 256 | } |