blob: 5b78fc772a6c5b6a31a5f7c0ff9ee5d89317dba1 [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;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039 struct {
40 EGLDisplay dpy;
41 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050042 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010043 } egl;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010044 uint32_t mask;
45};
46
47struct window {
48 struct display *display;
49 struct {
50 int width, height;
51 } geometry;
52 struct {
53 GLuint fbo;
54 GLuint color_rbo;
55
56 GLuint program;
57 GLuint rotation_uniform;
58
59 GLuint pos;
60 GLuint col;
61 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050062
63 struct wl_egl_window *native;
64 struct wl_surface *surface;
65 EGLSurface egl_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010066};
67
68static const char *vert_shader_text =
69 "uniform mat4 rotation;\n"
70 "attribute vec4 pos;\n"
71 "attribute vec4 color;\n"
72 "varying vec4 v_color;\n"
73 "void main() {\n"
74 " gl_Position = rotation * pos;\n"
75 " v_color = color;\n"
76 "}\n";
77
78static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050079 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010080 "varying vec4 v_color;\n"
81 "void main() {\n"
82 " gl_FragColor = v_color;\n"
83 "}\n";
84
85static void
86init_egl(struct display *display)
87{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050088 static const EGLint context_attribs[] = {
89 EGL_CONTEXT_CLIENT_VERSION, 2,
90 EGL_NONE
91 };
92
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050093 static const EGLint config_attribs[] = {
94 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
95 EGL_RED_SIZE, 1,
96 EGL_GREEN_SIZE, 1,
97 EGL_BLUE_SIZE, 1,
Benjamin Franzke2eae9e32011-03-17 15:43:21 +010098 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050099 EGL_DEPTH_SIZE, 1,
100 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
101 EGL_NONE
102 };
103
104 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100105 EGLBoolean ret;
106
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400107 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100108 assert(display->egl.dpy);
109
110 ret = eglInitialize(display->egl.dpy, &major, &minor);
111 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500112 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100113 assert(ret == EGL_TRUE);
114
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500115 assert(eglChooseConfig(display->egl.dpy, config_attribs,
116 &display->egl.conf, 1, &n) && n == 1);
117
118 display->egl.ctx = eglCreateContext(display->egl.dpy,
119 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500120 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100121 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500122
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100123}
124
125static GLuint
126create_shader(struct window *window, const char *source, GLenum shader_type)
127{
128 GLuint shader;
129 GLint status;
130
131 shader = glCreateShader(shader_type);
132 assert(shader != 0);
133
134 glShaderSource(shader, 1, (const char **) &source, NULL);
135 glCompileShader(shader);
136
137 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
138 if (!status) {
139 char log[1000];
140 GLsizei len;
141 glGetShaderInfoLog(shader, 1000, &len, log);
142 fprintf(stderr, "Error: compiling %s: %*s\n",
143 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
144 len, log);
145 exit(1);
146 }
147
148 return shader;
149}
150
151static void
152init_gl(struct window *window)
153{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100154 GLuint frag, vert;
155 GLint status;
156
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100157 glViewport(0, 0, window->geometry.width, window->geometry.height);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100158
159 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
160 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
161
162 window->gl.program = glCreateProgram();
163 glAttachShader(window->gl.program, frag);
164 glAttachShader(window->gl.program, vert);
165 glLinkProgram(window->gl.program);
166
167 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
168 if (!status) {
169 char log[1000];
170 GLsizei len;
171 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
172 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
173 exit(1);
174 }
175
176 glUseProgram(window->gl.program);
177
178 window->gl.pos = 0;
179 window->gl.pos = 1;
180
181 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
182 glBindAttribLocation(window->gl.program, window->gl.col, "color");
183 glLinkProgram(window->gl.program);
184
185 window->gl.rotation_uniform =
186 glGetUniformLocation(window->gl.program, "rotation");
187}
188
189static void
190create_surface(struct window *window)
191{
192 struct display *display = window->display;
193 struct wl_visual *visual;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500194 EGLBoolean ret;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100195
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500196 window->surface = wl_compositor_create_surface(display->compositor);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100197 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500198 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400199 wl_egl_window_create(window->surface,
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500200 window->geometry.width,
201 window->geometry.height,
202 visual);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500203 window->egl_surface =
204 eglCreateWindowSurface(display->egl.dpy,
205 display->egl.conf,
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -0500206 window->native,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500207 NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100208
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500209 wl_surface_map_toplevel(window->surface);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100210
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500211 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
212 window->egl_surface, window->display->egl.ctx);
213 assert(ret == EGL_TRUE);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100214}
215
216static void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100217redraw(struct wl_surface *surface, void *data, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100218{
219 struct window *window = data;
220 static const GLfloat verts[3][2] = {
221 { -0.5, -0.5 },
222 { 0.5, -0.5 },
223 { 0, 0.5 }
224 };
225 static const GLfloat colors[3][3] = {
226 { 1, 0, 0 },
227 { 0, 1, 0 },
228 { 0, 0, 1 }
229 };
230 GLfloat angle;
231 GLfloat rotation[4][4] = {
232 { 1, 0, 0, 0 },
233 { 0, 1, 0, 0 },
234 { 0, 0, 1, 0 },
235 { 0, 0, 0, 1 }
236 };
237 static const int32_t speed_div = 5;
238 static uint32_t start_time = 0;
239
240 if (start_time == 0)
241 start_time = time;
242
243 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
244 rotation[0][0] = cos(angle);
245 rotation[0][2] = sin(angle);
246 rotation[2][0] = -sin(angle);
247 rotation[2][2] = cos(angle);
248
249 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
250 (GLfloat *) rotation);
251
252 glClearColor(0.0, 0.0, 0.0, 0.5);
253 glClear(GL_COLOR_BUFFER_BIT);
254
255 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
256 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
257 glEnableVertexAttribArray(window->gl.pos);
258 glEnableVertexAttribArray(window->gl.col);
259
260 glDrawArrays(GL_TRIANGLES, 0, 3);
261
262 glDisableVertexAttribArray(window->gl.pos);
263 glDisableVertexAttribArray(window->gl.col);
264
265 glFlush();
266
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500267 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100268 wl_display_frame_callback(window->display->display,
269 window->surface,
270 redraw, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100271}
272
273static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100274display_handle_global(struct wl_display *display, uint32_t id,
275 const char *interface, uint32_t version, void *data)
276{
277 struct display *d = data;
278
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500279 if (strcmp(interface, "compositor") == 0)
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400280 d->compositor = wl_compositor_create(display, id, 1);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100281}
282
283static int
284event_mask_update(uint32_t mask, void *data)
285{
286 struct display *d = data;
287
288 d->mask = mask;
289
290 return 0;
291}
292
293int
294main(int argc, char **argv)
295{
296 struct display display = { 0 };
297 struct window window = { 0 };
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100298
299 memset(&display, 0, sizeof display);
300 memset(&window, 0, sizeof window);
301
302 window.display = &display;
303 window.geometry.width = 250;
304 window.geometry.height = 250;
305
306 display.display = wl_display_connect(NULL);
307 assert(display.display);
308
309 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500310 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100311
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100312 init_egl(&display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100313 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500314 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100315
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100316 wl_display_frame_callback(display.display, window.surface,
317 redraw, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100318
319 wl_display_get_fd(display.display, event_mask_update, &display);
320 while (true)
321 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500322
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100323 return 0;
324}