2 * Copyright © 2013 Intel Corporation
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.
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
27 #include <wayland-egl.h>
28 #include <wayland-cursor.h>
30 #include <GLES2/gl2.h>
36 struct nested_client {
37 struct wl_display *display;
38 struct wl_registry *registry;
39 struct wl_compositor *compositor;
41 EGLDisplay egl_display;
42 EGLContext egl_context;
44 EGLSurface egl_surface;
45 struct program *color_program;
47 GLuint vert, frag, program;
52 struct wl_surface *surface;
53 struct wl_egl_window *native;
61 create_shader(const char *source, GLenum shader_type)
66 shader = glCreateShader(shader_type);
70 glShaderSource(shader, 1, (const char **) &source, NULL);
71 glCompileShader(shader);
73 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
77 glGetShaderInfoLog(shader, 1000, &len, log);
78 fprintf(stderr, "Error: compiling %s: %*s\n",
79 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
88 create_program(struct nested_client *client,
89 const char *vert, const char *frag)
93 client->vert = create_shader(vert, GL_VERTEX_SHADER);
94 client->frag = create_shader(frag, GL_FRAGMENT_SHADER);
96 client->program = glCreateProgram();
97 glAttachShader(client->program, client->frag);
98 glAttachShader(client->program, client->vert);
99 glBindAttribLocation(client->program, POS, "pos");
100 glBindAttribLocation(client->program, COL, "color");
101 glLinkProgram(client->program);
103 glGetProgramiv(client->program, GL_LINK_STATUS, &status);
107 glGetProgramInfoLog(client->program, 1000, &len, log);
108 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
113 glGetUniformLocation(client->program, "rotation");
116 static const char vertex_shader_text[] =
117 "uniform mat4 rotation;\n"
118 "attribute vec4 pos;\n"
119 "attribute vec4 color;\n"
120 "varying vec4 v_color;\n"
122 " gl_Position = rotation * pos;\n"
123 " v_color = color;\n"
126 static const char color_fragment_shader_text[] =
127 "precision mediump float;\n"
128 "varying vec4 v_color;\n"
130 " gl_FragColor = v_color;\n"
134 render_triangle(struct nested_client *client, uint32_t time)
136 static const GLfloat verts[3][2] = {
141 static const GLfloat colors[3][3] = {
147 GLfloat rotation[4][4] = {
153 static const int32_t speed_div = 5;
154 static uint32_t start_time = 0;
156 if (client->program == 0)
157 create_program(client, vertex_shader_text,
158 color_fragment_shader_text);
163 angle = ((time - start_time) / speed_div) % 360 * M_PI / 180.0;
164 rotation[0][0] = cos(angle);
165 rotation[0][2] = sin(angle);
166 rotation[2][0] = -sin(angle);
167 rotation[2][2] = cos(angle);
169 glClearColor(0.4, 0.4, 0.4, 1.0);
170 glClear(GL_COLOR_BUFFER_BIT);
172 glUseProgram(client->program);
174 glViewport(0, 0, client->width, client->height);
176 glUniformMatrix4fv(client->rotation, 1, GL_FALSE,
177 (GLfloat *) rotation);
179 glVertexAttribPointer(POS, 2, GL_FLOAT, GL_FALSE, 0, verts);
180 glVertexAttribPointer(COL, 3, GL_FLOAT, GL_FALSE, 0, colors);
181 glEnableVertexAttribArray(POS);
182 glEnableVertexAttribArray(COL);
184 glDrawArrays(GL_TRIANGLES, 0, 3);
186 glDisableVertexAttribArray(POS);
187 glDisableVertexAttribArray(COL);
193 frame_callback(void *data, struct wl_callback *callback, uint32_t time);
195 static const struct wl_callback_listener frame_listener = {
200 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
202 struct nested_client *client = data;
205 wl_callback_destroy(callback);
207 callback = wl_surface_frame(client->surface);
208 wl_callback_add_listener(callback, &frame_listener, client);
210 render_triangle(client, time);
212 eglSwapBuffers(client->egl_display, client->egl_surface);
216 registry_handle_global(void *data, struct wl_registry *registry,
217 uint32_t name, const char *interface, uint32_t version)
219 struct nested_client *client = data;
221 if (strcmp(interface, "wl_compositor") == 0) {
223 wl_registry_bind(registry, name,
224 &wl_compositor_interface, 1);
229 registry_handle_global_remove(void *data, struct wl_registry *registry,
234 static const struct wl_registry_listener registry_listener = {
235 registry_handle_global,
236 registry_handle_global_remove
239 static struct nested_client *
240 nested_client_create(void)
242 static const EGLint context_attribs[] = {
243 EGL_CONTEXT_CLIENT_VERSION, 2,
247 static const EGLint config_attribs[] = {
248 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
253 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
257 EGLint major, minor, n;
260 struct nested_client *client;
262 client = malloc(sizeof *client);
267 client->height = 250;
269 client->display = wl_display_connect(NULL);
271 client->registry = wl_display_get_registry(client->display);
272 wl_registry_add_listener(client->registry,
273 ®istry_listener, client);
276 wl_display_roundtrip(client->display);
278 client->egl_display = eglGetDisplay(client->display);
279 if (client->egl_display == NULL)
282 ret = eglInitialize(client->egl_display, &major, &minor);
285 ret = eglBindAPI(EGL_OPENGL_ES_API);
289 ret = eglChooseConfig(client->egl_display, config_attribs,
290 &client->egl_config, 1, &n);
294 client->egl_context = eglCreateContext(client->egl_display,
298 if (!client->egl_context)
301 client->surface = wl_compositor_create_surface(client->compositor);
303 client->native = wl_egl_window_create(client->surface,
304 client->width, client->height);
306 client->egl_surface =
307 eglCreateWindowSurface(client->egl_display,
309 client->native, NULL);
311 eglMakeCurrent(client->egl_display, client->egl_surface,
312 client->egl_surface, client->egl_context);
314 wl_egl_window_resize(client->native,
315 client->width, client->height, 0, 0);
317 frame_callback(client, NULL, 0);
323 nested_client_destroy(struct nested_client *client)
325 eglMakeCurrent(client->egl_display,
326 EGL_NO_SURFACE, EGL_NO_SURFACE,
329 wl_egl_window_destroy(client->native);
331 wl_surface_destroy(client->surface);
333 if (client->compositor)
334 wl_compositor_destroy(client->compositor);
336 wl_registry_destroy(client->registry);
337 wl_display_flush(client->display);
338 wl_display_disconnect(client->display);
342 main(int argc, char **argv)
344 struct nested_client *client;
347 if (getenv("WAYLAND_SOCKET") == NULL) {
349 "must be run by nested, don't run standalone\n");
353 client = nested_client_create();
358 ret = wl_display_dispatch(client->display);
360 nested_client_destroy(client);