blob: 8365639cd52cabbb6ea0c2aaec83a5164edb50e7 [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010026#include <stdbool.h>
27#include <math.h>
28#include <assert.h>
29
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010030#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050031#include <wayland-egl.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010032
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010033#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010034#include <EGL/egl.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035
36struct display {
37 struct wl_display *display;
Kristian Høgsberg8357cd62011-05-13 13:24:56 -040038 struct wl_visual *premultiplied_argb_visual;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050039 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040040 struct wl_shell *shell;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010041 struct {
42 EGLDisplay dpy;
43 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050044 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010045 } egl;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010046 uint32_t mask;
47};
48
49struct window {
50 struct display *display;
51 struct {
52 int width, height;
53 } geometry;
54 struct {
55 GLuint fbo;
56 GLuint color_rbo;
57
58 GLuint program;
59 GLuint rotation_uniform;
60
61 GLuint pos;
62 GLuint col;
63 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050064
65 struct wl_egl_window *native;
66 struct wl_surface *surface;
67 EGLSurface egl_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010068};
69
70static const char *vert_shader_text =
71 "uniform mat4 rotation;\n"
72 "attribute vec4 pos;\n"
73 "attribute vec4 color;\n"
74 "varying vec4 v_color;\n"
75 "void main() {\n"
76 " gl_Position = rotation * pos;\n"
77 " v_color = color;\n"
78 "}\n";
79
80static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050081 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010082 "varying vec4 v_color;\n"
83 "void main() {\n"
84 " gl_FragColor = v_color;\n"
85 "}\n";
86
87static void
88init_egl(struct display *display)
89{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050090 static const EGLint context_attribs[] = {
91 EGL_CONTEXT_CLIENT_VERSION, 2,
92 EGL_NONE
93 };
94
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050095 static const EGLint config_attribs[] = {
96 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
97 EGL_RED_SIZE, 1,
98 EGL_GREEN_SIZE, 1,
99 EGL_BLUE_SIZE, 1,
Benjamin Franzke2eae9e32011-03-17 15:43:21 +0100100 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500101 EGL_DEPTH_SIZE, 1,
102 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
103 EGL_NONE
104 };
105
106 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100107 EGLBoolean ret;
108
Kristian Høgsbergded9ff32011-05-13 17:07:23 -0400109 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400110 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100111 assert(display->egl.dpy);
112
113 ret = eglInitialize(display->egl.dpy, &major, &minor);
114 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500115 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100116 assert(ret == EGL_TRUE);
117
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500118 assert(eglChooseConfig(display->egl.dpy, config_attribs,
119 &display->egl.conf, 1, &n) && n == 1);
120
121 display->egl.ctx = eglCreateContext(display->egl.dpy,
122 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500123 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100124 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500125
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100126}
127
128static GLuint
129create_shader(struct window *window, const char *source, GLenum shader_type)
130{
131 GLuint shader;
132 GLint status;
133
134 shader = glCreateShader(shader_type);
135 assert(shader != 0);
136
137 glShaderSource(shader, 1, (const char **) &source, NULL);
138 glCompileShader(shader);
139
140 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
141 if (!status) {
142 char log[1000];
143 GLsizei len;
144 glGetShaderInfoLog(shader, 1000, &len, log);
145 fprintf(stderr, "Error: compiling %s: %*s\n",
146 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
147 len, log);
148 exit(1);
149 }
150
151 return shader;
152}
153
154static void
155init_gl(struct window *window)
156{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100157 GLuint frag, vert;
158 GLint status;
159
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100160 glViewport(0, 0, window->geometry.width, window->geometry.height);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100161
162 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
163 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
164
165 window->gl.program = glCreateProgram();
166 glAttachShader(window->gl.program, frag);
167 glAttachShader(window->gl.program, vert);
168 glLinkProgram(window->gl.program);
169
170 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
171 if (!status) {
172 char log[1000];
173 GLsizei len;
174 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
175 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
176 exit(1);
177 }
178
179 glUseProgram(window->gl.program);
180
181 window->gl.pos = 0;
182 window->gl.pos = 1;
183
184 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
185 glBindAttribLocation(window->gl.program, window->gl.col, "color");
186 glLinkProgram(window->gl.program);
187
188 window->gl.rotation_uniform =
189 glGetUniformLocation(window->gl.program, "rotation");
190}
191
192static void
193create_surface(struct window *window)
194{
195 struct display *display = window->display;
196 struct wl_visual *visual;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500197 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200198 int done = 0;
199
Kristian Høgsberg33418202011-08-16 23:01:28 -0400200 if (!display->premultiplied_argb_visual)
201 wl_display_roundtrip(display->display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200202 if (!display->premultiplied_argb_visual) {
Kristian Høgsberg33418202011-08-16 23:01:28 -0400203 fprintf(stderr, "premultiplied argb visual missing\n");
204 exit(1);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200205 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100206
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500207 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400208 visual = display->premultiplied_argb_visual;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500209 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400210 wl_egl_window_create(window->surface,
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500211 window->geometry.width,
212 window->geometry.height,
213 visual);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500214 window->egl_surface =
215 eglCreateWindowSurface(display->egl.dpy,
216 display->egl.conf,
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -0500217 window->native,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500218 NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100219
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400220 wl_shell_set_toplevel(display->shell, window->surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100221
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500222 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
223 window->egl_surface, window->display->egl.ctx);
224 assert(ret == EGL_TRUE);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100225}
226
Kristian Høgsberg33418202011-08-16 23:01:28 -0400227static const struct wl_callback_listener frame_listener;
228
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100229static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400230redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100231{
232 struct window *window = data;
233 static const GLfloat verts[3][2] = {
234 { -0.5, -0.5 },
235 { 0.5, -0.5 },
236 { 0, 0.5 }
237 };
238 static const GLfloat colors[3][3] = {
239 { 1, 0, 0 },
240 { 0, 1, 0 },
241 { 0, 0, 1 }
242 };
243 GLfloat angle;
244 GLfloat rotation[4][4] = {
245 { 1, 0, 0, 0 },
246 { 0, 1, 0, 0 },
247 { 0, 0, 1, 0 },
248 { 0, 0, 0, 1 }
249 };
250 static const int32_t speed_div = 5;
251 static uint32_t start_time = 0;
252
253 if (start_time == 0)
254 start_time = time;
255
256 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
257 rotation[0][0] = cos(angle);
258 rotation[0][2] = sin(angle);
259 rotation[2][0] = -sin(angle);
260 rotation[2][2] = cos(angle);
261
262 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
263 (GLfloat *) rotation);
264
265 glClearColor(0.0, 0.0, 0.0, 0.5);
266 glClear(GL_COLOR_BUFFER_BIT);
267
268 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
269 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
270 glEnableVertexAttribArray(window->gl.pos);
271 glEnableVertexAttribArray(window->gl.col);
272
273 glDrawArrays(GL_TRIANGLES, 0, 3);
274
275 glDisableVertexAttribArray(window->gl.pos);
276 glDisableVertexAttribArray(window->gl.col);
277
278 glFlush();
279
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500280 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400281 if (callback)
282 wl_callback_destroy(callback);
283
284 callback = wl_surface_frame(window->surface);
285 wl_callback_add_listener(callback, &frame_listener, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100286}
287
Kristian Høgsberg33418202011-08-16 23:01:28 -0400288static const struct wl_callback_listener frame_listener = {
289 redraw
290};
291
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100292static void
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400293compositor_handle_visual(void *data,
294 struct wl_compositor *compositor,
295 uint32_t id, uint32_t token)
296{
297 struct display *d = data;
298
299 switch (token) {
300 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
301 d->premultiplied_argb_visual =
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400302 wl_display_bind(d->display, id, &wl_visual_interface);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400303 break;
304 }
305}
306
307static const struct wl_compositor_listener compositor_listener = {
308 compositor_handle_visual,
309};
310
311static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100312display_handle_global(struct wl_display *display, uint32_t id,
313 const char *interface, uint32_t version, void *data)
314{
315 struct display *d = data;
316
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400317 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400318 d->compositor =
319 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400320 wl_compositor_add_listener(d->compositor,
321 &compositor_listener, d);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400322 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400323 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400324 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100325}
326
327static int
328event_mask_update(uint32_t mask, void *data)
329{
330 struct display *d = data;
331
332 d->mask = mask;
333
334 return 0;
335}
336
337int
338main(int argc, char **argv)
339{
340 struct display display = { 0 };
341 struct window window = { 0 };
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100342
343 memset(&display, 0, sizeof display);
344 memset(&window, 0, sizeof window);
345
346 window.display = &display;
347 window.geometry.width = 250;
348 window.geometry.height = 250;
349
350 display.display = wl_display_connect(NULL);
351 assert(display.display);
352
353 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500354 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100355
Benjamin Franzke65e50512011-05-31 11:36:31 +0200356 wl_display_get_fd(display.display, event_mask_update, &display);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400357 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200358
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100359 init_egl(&display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100360 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500361 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100362
Kristian Høgsberg33418202011-08-16 23:01:28 -0400363 redraw(&window, NULL, 0);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100364
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100365 while (true)
366 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500367
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100368 return 0;
369}