blob: 13c33bd9040438c15b178109637631be2afdf3e5 [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øgsberga495a5e2011-02-04 15:31:33 -050038 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040039 struct wl_shell *shell;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040 struct {
41 EGLDisplay dpy;
42 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050043 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010044 } egl;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010045 uint32_t mask;
46};
47
48struct window {
49 struct display *display;
50 struct {
51 int width, height;
52 } geometry;
53 struct {
54 GLuint fbo;
55 GLuint color_rbo;
56
57 GLuint program;
58 GLuint rotation_uniform;
59
60 GLuint pos;
61 GLuint col;
62 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050063
64 struct wl_egl_window *native;
65 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020066 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050067 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[] = {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040096 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_VG_ALPHA_FORMAT_PRE_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050097 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øgsberg91342c62011-04-14 14:44:58 -0400109 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100110 assert(display->egl.dpy);
111
112 ret = eglInitialize(display->egl.dpy, &major, &minor);
113 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500114 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100115 assert(ret == EGL_TRUE);
116
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500117 assert(eglChooseConfig(display->egl.dpy, config_attribs,
118 &display->egl.conf, 1, &n) && n == 1);
119
120 display->egl.ctx = eglCreateContext(display->egl.dpy,
121 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500122 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100123 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500124
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100125}
126
127static GLuint
128create_shader(struct window *window, const char *source, GLenum shader_type)
129{
130 GLuint shader;
131 GLint status;
132
133 shader = glCreateShader(shader_type);
134 assert(shader != 0);
135
136 glShaderSource(shader, 1, (const char **) &source, NULL);
137 glCompileShader(shader);
138
139 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
140 if (!status) {
141 char log[1000];
142 GLsizei len;
143 glGetShaderInfoLog(shader, 1000, &len, log);
144 fprintf(stderr, "Error: compiling %s: %*s\n",
145 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
146 len, log);
147 exit(1);
148 }
149
150 return shader;
151}
152
153static void
154init_gl(struct window *window)
155{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100156 GLuint frag, vert;
157 GLint status;
158
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100159 glViewport(0, 0, window->geometry.width, window->geometry.height);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100160
161 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
162 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
163
164 window->gl.program = glCreateProgram();
165 glAttachShader(window->gl.program, frag);
166 glAttachShader(window->gl.program, vert);
167 glLinkProgram(window->gl.program);
168
169 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
170 if (!status) {
171 char log[1000];
172 GLsizei len;
173 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
174 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
175 exit(1);
176 }
177
178 glUseProgram(window->gl.program);
179
180 window->gl.pos = 0;
181 window->gl.pos = 1;
182
183 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
184 glBindAttribLocation(window->gl.program, window->gl.col, "color");
185 glLinkProgram(window->gl.program);
186
187 window->gl.rotation_uniform =
188 glGetUniformLocation(window->gl.program, "rotation");
189}
190
191static void
192create_surface(struct window *window)
193{
194 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500195 EGLBoolean ret;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400196 static const EGLint surface_attribs[] = {
197 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
198 EGL_NONE
199 };
Benjamin Franzke65e50512011-05-31 11:36:31 +0200200
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500201 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200202 window->shell_surface = wl_shell_get_shell_surface(display->shell,
203 window->surface);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500204 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400205 wl_egl_window_create(window->surface,
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500206 window->geometry.width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400207 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500208 window->egl_surface =
209 eglCreateWindowSurface(display->egl.dpy,
210 display->egl.conf,
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -0500211 window->native,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400212 surface_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100213
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200214 wl_shell_surface_set_toplevel(window->shell_surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100215
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500216 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
217 window->egl_surface, window->display->egl.ctx);
218 assert(ret == EGL_TRUE);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100219}
220
Kristian Høgsberg33418202011-08-16 23:01:28 -0400221static const struct wl_callback_listener frame_listener;
222
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100223static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400224redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100225{
226 struct window *window = data;
227 static const GLfloat verts[3][2] = {
228 { -0.5, -0.5 },
229 { 0.5, -0.5 },
230 { 0, 0.5 }
231 };
232 static const GLfloat colors[3][3] = {
233 { 1, 0, 0 },
234 { 0, 1, 0 },
235 { 0, 0, 1 }
236 };
237 GLfloat angle;
238 GLfloat rotation[4][4] = {
239 { 1, 0, 0, 0 },
240 { 0, 1, 0, 0 },
241 { 0, 0, 1, 0 },
242 { 0, 0, 0, 1 }
243 };
244 static const int32_t speed_div = 5;
245 static uint32_t start_time = 0;
246
247 if (start_time == 0)
248 start_time = time;
249
250 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
251 rotation[0][0] = cos(angle);
252 rotation[0][2] = sin(angle);
253 rotation[2][0] = -sin(angle);
254 rotation[2][2] = cos(angle);
255
256 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
257 (GLfloat *) rotation);
258
259 glClearColor(0.0, 0.0, 0.0, 0.5);
260 glClear(GL_COLOR_BUFFER_BIT);
261
262 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
263 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
264 glEnableVertexAttribArray(window->gl.pos);
265 glEnableVertexAttribArray(window->gl.col);
266
267 glDrawArrays(GL_TRIANGLES, 0, 3);
268
269 glDisableVertexAttribArray(window->gl.pos);
270 glDisableVertexAttribArray(window->gl.col);
271
272 glFlush();
273
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500274 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400275 if (callback)
276 wl_callback_destroy(callback);
277
278 callback = wl_surface_frame(window->surface);
279 wl_callback_add_listener(callback, &frame_listener, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100280}
281
Kristian Høgsberg33418202011-08-16 23:01:28 -0400282static const struct wl_callback_listener frame_listener = {
283 redraw
284};
285
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100286static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100287display_handle_global(struct wl_display *display, uint32_t id,
288 const char *interface, uint32_t version, void *data)
289{
290 struct display *d = data;
291
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400292 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400293 d->compositor =
294 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400295 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400296 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400297 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100298}
299
300static int
301event_mask_update(uint32_t mask, void *data)
302{
303 struct display *d = data;
304
305 d->mask = mask;
306
307 return 0;
308}
309
310int
311main(int argc, char **argv)
312{
313 struct display display = { 0 };
314 struct window window = { 0 };
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100315
316 memset(&display, 0, sizeof display);
317 memset(&window, 0, sizeof window);
318
319 window.display = &display;
320 window.geometry.width = 250;
321 window.geometry.height = 250;
322
323 display.display = wl_display_connect(NULL);
324 assert(display.display);
325
326 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500327 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100328
Benjamin Franzke65e50512011-05-31 11:36:31 +0200329 wl_display_get_fd(display.display, event_mask_update, &display);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200331
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100332 init_egl(&display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100333 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500334 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100335
Kristian Høgsberg33418202011-08-16 23:01:28 -0400336 redraw(&window, NULL, 0);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100337
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100338 while (true)
339 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500340
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100341 return 0;
342}