2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright © 2011 Benjamin Franzke
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
31 #include <sys/types.h>
40 #include <wayland-client.h>
41 #include <wayland-egl.h>
43 #include <GLES2/gl2.h>
45 #include <EGL/eglext.h>
50 #include <libwindowmanager.h>
51 #include <libhomescreen.hpp>
53 #include <ilm/ivi-application-client-protocol.h>
54 #include "hmi-debug.h"
58 const char* log_prefix = "simple-egl";
59 uint32_t g_id_ivisurf = 9009;
61 string token = string("wm");
63 string app_name = string("Navigation");
64 const char* main_role = "navigation";
69 static const struct wl_interface *types[] = {
73 &wl_surface_interface,
74 &ivi_surface_interface,
77 static const struct wl_message ivi_surface_requests[] = {
78 { "destroy", "", types + 0 },
81 static const struct wl_message ivi_surface_events[] = {
82 { "configure", "ii", types + 0 },
85 const struct wl_interface ivi_surface_interface = {
87 1, ivi_surface_requests,
88 1, ivi_surface_events,
91 static const struct wl_message ivi_application_requests[] = {
92 { "surface_create", "uon", types + 2 },
95 const struct wl_interface ivi_application_interface = {
97 1, ivi_application_requests,
101 #include "platform.h"
103 #ifndef EGL_EXT_swap_buffers_with_damage
104 #define EGL_EXT_swap_buffers_with_damage 1
105 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
108 #ifndef EGL_EXT_buffer_age
109 #define EGL_EXT_buffer_age 1
110 #define EGL_BUFFER_AGE_EXT 0x313D
117 struct wl_display *display;
118 struct wl_registry *registry;
119 struct wl_compositor *compositor;
120 struct wl_seat *seat;
126 struct window *window;
127 struct ivi_application *ivi_application;
129 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
137 struct display *display;
138 struct geometry geometry, window_size;
140 GLuint rotation_uniform;
145 uint32_t benchmark_time, frames;
146 struct wl_egl_window *native;
147 struct wl_surface *surface;
148 struct ivi_surface *ivi_surface;
149 EGLSurface egl_surface;
150 struct wl_callback *callback;
151 int fullscreen, opaque, buffer_size, frame_sync;
154 static const char *vert_shader_text =
155 "uniform mat4 rotation;\n"
156 "attribute vec4 pos;\n"
157 "attribute vec4 color;\n"
158 "varying vec4 v_color;\n"
160 " gl_Position = rotation * pos;\n"
161 " v_color = color;\n"
164 static const char *frag_shader_text =
165 "precision mediump float;\n"
166 "varying vec4 v_color;\n"
168 " gl_FragColor = v_color;\n"
171 static int running = 1;
174 init_egl(struct display *display, struct window *window)
176 static const EGLint context_attribs[] = {
177 EGL_CONTEXT_CLIENT_VERSION, 2,
180 const char *extensions;
182 EGLint config_attribs[] = {
183 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
188 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
192 EGLint major, minor, n, count, i, size;
196 if (window->opaque || window->buffer_size == 16)
197 config_attribs[9] = 0;
199 display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL);
200 assert(display->egl.dpy);
202 ret = eglInitialize(display->egl.dpy, &major, &minor);
203 assert(ret == EGL_TRUE);
204 ret = eglBindAPI(EGL_OPENGL_ES_API);
205 assert(ret == EGL_TRUE);
207 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
210 configs = calloc(count, sizeof *configs);
213 ret = eglChooseConfig(display->egl.dpy, config_attribs,
215 assert(ret && n >= 1);
217 for (i = 0; i < n; i++) {
218 eglGetConfigAttrib(display->egl.dpy,
219 configs[i], EGL_BUFFER_SIZE, &size);
220 if (window->buffer_size == size) {
221 display->egl.conf = configs[i];
226 if (display->egl.conf == NULL) {
227 HMI_ERROR(log_prefix,"did not find config with buffer size %d",
228 window->buffer_size);
232 display->egl.ctx = eglCreateContext(display->egl.dpy,
234 EGL_NO_CONTEXT, context_attribs);
235 assert(display->egl.ctx);
237 display->swap_buffers_with_damage = NULL;
238 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
240 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
241 strstr(extensions, "EGL_EXT_buffer_age"))
242 display->swap_buffers_with_damage =
243 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
244 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
246 if (display->swap_buffers_with_damage)
247 HMI_DEBUG(log_prefix,"has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage");
252 fini_egl(struct display *display)
254 eglTerminate(display->egl.dpy);
259 create_shader(struct window *window, const char *source, GLenum shader_type)
264 shader = glCreateShader(shader_type);
267 glShaderSource(shader, 1, (const char **) &source, NULL);
268 glCompileShader(shader);
270 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
274 glGetShaderInfoLog(shader, 1000, &len, log);
275 HMI_ERROR(log_prefix,"Error: compiling %s: %*s",
276 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
285 init_gl(struct window *window)
291 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
292 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
294 program = glCreateProgram();
295 glAttachShader(program, frag);
296 glAttachShader(program, vert);
297 glLinkProgram(program);
299 glGetProgramiv(program, GL_LINK_STATUS, &status);
303 glGetProgramInfoLog(program, 1000, &len, log);
304 HMI_ERROR(log_prefix,"Error: linking:%*s", len, log);
308 glUseProgram(program);
313 glBindAttribLocation(program, window->gl.pos, "pos");
314 glBindAttribLocation(program, window->gl.col, "color");
315 glLinkProgram(program);
317 window->gl.rotation_uniform =
318 glGetUniformLocation(program, "rotation");
322 create_ivi_surface(struct window *window, struct display *display)
324 uint32_t id_ivisurf = g_id_ivisurf;
325 window->ivi_surface =
326 ivi_application_surface_create(display->ivi_application,
327 id_ivisurf, window->surface);
329 if (window->ivi_surface == NULL) {
330 HMI_ERROR(log_prefix,"Failed to create ivi_client_surface");
337 create_surface(struct window *window)
339 struct display *display = window->display;
342 window->surface = wl_compositor_create_surface(display->compositor);
345 wl_egl_window_create(window->surface,
346 window->geometry.width,
347 window->geometry.height);
348 window->egl_surface =
349 weston_platform_create_egl_surface(display->egl.dpy,
351 window->native, NULL);
354 if (display->ivi_application ) {
355 create_ivi_surface(window, display);
360 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
361 window->egl_surface, window->display->egl.ctx);
362 assert(ret == EGL_TRUE);
364 if (!window->frame_sync)
365 eglSwapInterval(display->egl.dpy, 0);
370 destroy_surface(struct window *window)
372 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
373 * on eglReleaseThread(). */
374 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
377 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
378 wl_egl_window_destroy(window->native);
380 if (window->display->ivi_application)
381 ivi_surface_destroy(window->ivi_surface);
382 wl_surface_destroy(window->surface);
384 if (window->callback)
385 wl_callback_destroy(window->callback);
389 redraw(void *data, struct wl_callback *callback, uint32_t time)
391 struct window *window = data;
392 struct display *display = window->display;
393 static const GLfloat verts[3][2] = {
399 static const GLfloat colors[3][3] = {
406 GLfloat rotation[4][4] = {
412 static const uint32_t speed_div = 5, benchmark_interval = 5;
413 struct wl_region *region;
415 EGLint buffer_age = 0;
418 assert(window->callback == callback);
419 window->callback = NULL;
422 wl_callback_destroy(callback);
424 gettimeofday(&tv, NULL);
425 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
426 if (window->frames == 0)
427 window->benchmark_time = time;
429 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
430 HMI_DEBUG(log_prefix,"%d frames in %d seconds: %f fps",
433 (float) window->frames / benchmark_interval);
434 window->benchmark_time = time;
438 angle = (time / speed_div) % 360 * M_PI / 180.0;
439 rotation[0][0] = cos(angle);
440 rotation[0][2] = sin(angle);
441 rotation[2][0] = -sin(angle);
442 rotation[2][2] = cos(angle);
444 if (display->swap_buffers_with_damage)
445 eglQuerySurface(display->egl.dpy, window->egl_surface,
446 EGL_BUFFER_AGE_EXT, &buffer_age);
448 glViewport(0, 0, window->geometry.width, window->geometry.height);
450 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
451 (GLfloat *) rotation);
453 glClearColor(0.0, 0.0, 0.0, 0.5);
454 glClear(GL_COLOR_BUFFER_BIT);
456 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
457 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
458 glEnableVertexAttribArray(window->gl.pos);
459 glEnableVertexAttribArray(window->gl.col);
461 glDrawArrays(GL_TRIANGLES, 0, 3);
463 glDisableVertexAttribArray(window->gl.pos);
464 glDisableVertexAttribArray(window->gl.col);
466 if (window->opaque || window->fullscreen) {
467 region = wl_compositor_create_region(window->display->compositor);
468 wl_region_add(region, 0, 0,
469 window->geometry.width,
470 window->geometry.height);
471 wl_surface_set_opaque_region(window->surface, region);
472 wl_region_destroy(region);
474 wl_surface_set_opaque_region(window->surface, NULL);
477 if (display->swap_buffers_with_damage && buffer_age > 0) {
478 rect[0] = window->geometry.width / 4 - 1;
479 rect[1] = window->geometry.height / 4 - 1;
480 rect[2] = window->geometry.width / 2 + 2;
481 rect[3] = window->geometry.height / 2 + 2;
482 display->swap_buffers_with_damage(display->egl.dpy,
486 eglSwapBuffers(display->egl.dpy, window->egl_surface);
493 registry_handle_global(void *data, struct wl_registry *registry,
494 uint32_t name, const char *interface, uint32_t version)
496 struct display *d = data;
498 if (strcmp(interface, "wl_compositor") == 0) {
500 wl_registry_bind(registry, name,
501 &wl_compositor_interface, 1);
502 } else if (strcmp(interface, "ivi_application") == 0) {
504 wl_registry_bind(registry, name,
505 &ivi_application_interface, 1);
510 registry_handle_global_remove(void *data, struct wl_registry *registry,
515 static const struct wl_registry_listener registry_listener = {
516 registry_handle_global,
517 registry_handle_global_remove
521 signal_int(int signum)
527 init_wm(LibWindowmanager *wm, struct window *window)
529 HMI_DEBUG(log_prefix,"called");
531 if (wm->init(port, token) != 0) {
532 HMI_ERROR(log_prefix,"wm init failed. ");
536 g_id_ivisurf = wm->requestSurface(main_role);
537 if (g_id_ivisurf < 0) {
538 HMI_ERROR(log_prefix,"wm request surface failed ");
541 HMI_DEBUG(log_prefix,"IVI_SURFACE_ID: %d ", g_id_ivisurf);
544 wmh.on_visible = [](const char* role, bool visible){
545 // Sample code if user uses visible event
546 HMI_DEBUG(log_prefix, "role: %s, visible: %s", role, visible ? "true" : "false");
548 wmh.on_sync_draw = [wm, window](const char* role, const char* area, Rect rect) {
550 HMI_DEBUG(log_prefix,"Surface %s got syncDraw! Area: %s. w:%d, h:%d", role, area, rect.width(), rect.height());
552 wl_egl_window_resize(window->native, rect.width(), rect.height(), 0, 0);
553 window->geometry.width = rect.width();
554 window->geometry.height = rect.height();
559 wm->setEventHandler(wmh);
565 init_hs(LibHomeScreen* hs){
566 if(hs->init(port, token)!=0)
568 HMI_ERROR(log_prefix,"homescreen init failed. ");
572 hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [hs](json_object *object){
573 HMI_DEBUG("simple-egl","try to activeWindow %s ", app_name.c_str());
575 struct json_object *param_obj = json_object_object_get(object, hs->_keyParameter);
576 const char *area = json_object_get_string(
577 json_object_object_get(param_obj, hs->_keyArea));
578 // Application should call LibWindowmanager::activateWindow() in showWindow handler
580 wm->activateWindow(main_role, hs->_areaNormal);
582 wm->activateWindow(main_role, area);
589 main(int argc, char **argv)
591 struct sigaction sigint;
592 struct window window = { 0 };
593 struct display display = { 0 };
595 window.display = &display;
596 display.window = &window;
597 window.geometry.width = 1080;
598 window.geometry.height = 1488;
599 window.window_size = window.geometry;
600 window.buffer_size = 32;
601 window.frame_sync = 1;
604 port = strtol(argv[1], NULL, 10);
608 HMI_DEBUG(log_prefix,"main_role: %s, port: %d, token: %s. ", main_role, port, token.c_str());
610 display.display = wl_display_connect(NULL);
611 assert(display.display);
613 display.registry = wl_display_get_registry(display.display);
614 wl_registry_add_listener(display.registry,
615 ®istry_listener, &display);
617 wl_display_roundtrip(display.display);
619 init_egl(&display, &window);
621 wm = new LibWindowmanager();
622 if(init_wm(wm, &window)!=0){
624 if (display.ivi_application)
625 ivi_application_destroy(display.ivi_application);
626 if (display.compositor)
627 wl_compositor_destroy(display.compositor);
628 wl_registry_destroy(display.registry);
629 wl_display_flush(display.display);
633 hs = new LibHomeScreen();
636 if (display.ivi_application)
637 ivi_application_destroy(display.ivi_application);
638 if (display.compositor)
639 wl_compositor_destroy(display.compositor);
640 wl_registry_destroy(display.registry);
641 wl_display_flush(display.display);
645 create_surface(&window);
649 sigint.sa_handler = signal_int;
650 sigemptyset(&sigint.sa_mask);
651 sigint.sa_flags = SA_RESETHAND;
652 sigaction(SIGINT, &sigint, NULL);
654 eglSwapBuffers(window.display->egl.dpy, window.egl_surface);
656 wm->activateWindow(main_role);
658 /* The mainloop here is a little subtle. Redrawing will cause
659 * EGL to read events so we can just call
660 * wl_display_dispatch_pending() to handle any events that got
661 * queued up as a side effect. */
663 wl_display_dispatch_pending(display.display);
664 redraw(&window, NULL, 0);
667 HMI_DEBUG(log_prefix,"simple-egl exiting! ");
669 destroy_surface(&window);
672 if (display.ivi_application)
673 ivi_application_destroy(display.ivi_application);
675 if (display.compositor)
676 wl_compositor_destroy(display.compositor);
678 wl_registry_destroy(display.registry);
679 wl_display_flush(display.display);
680 wl_display_disconnect(display.display);