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>
57 uint32_t g_id_ivisurf = 9009;
59 string token = string("wm");
60 bool enable_debug = false;
62 string app_name = string("Navigation");
68 static const struct wl_interface *types[] = {
72 &wl_surface_interface,
73 &ivi_surface_interface,
76 static const struct wl_message ivi_surface_requests[] = {
77 { "destroy", "", types + 0 },
80 static const struct wl_message ivi_surface_events[] = {
81 { "configure", "ii", types + 0 },
84 const struct wl_interface ivi_surface_interface = {
86 1, ivi_surface_requests,
87 1, ivi_surface_events,
90 static const struct wl_message ivi_application_requests[] = {
91 { "surface_create", "uon", types + 2 },
94 const struct wl_interface ivi_application_interface = {
96 1, ivi_application_requests,
100 #include "platform.h"
102 #ifndef EGL_EXT_swap_buffers_with_damage
103 #define EGL_EXT_swap_buffers_with_damage 1
104 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
107 #ifndef EGL_EXT_buffer_age
108 #define EGL_EXT_buffer_age 1
109 #define EGL_BUFFER_AGE_EXT 0x313D
116 struct wl_display *display;
117 struct wl_registry *registry;
118 struct wl_compositor *compositor;
119 struct wl_seat *seat;
125 struct window *window;
126 struct ivi_application *ivi_application;
128 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
136 struct display *display;
137 struct geometry geometry, window_size;
139 GLuint rotation_uniform;
144 uint32_t benchmark_time, frames;
145 struct wl_egl_window *native;
146 struct wl_surface *surface;
147 struct ivi_surface *ivi_surface;
148 EGLSurface egl_surface;
149 struct wl_callback *callback;
150 int fullscreen, opaque, buffer_size, frame_sync;
153 static const char *vert_shader_text =
154 "uniform mat4 rotation;\n"
155 "attribute vec4 pos;\n"
156 "attribute vec4 color;\n"
157 "varying vec4 v_color;\n"
159 " gl_Position = rotation * pos;\n"
160 " v_color = color;\n"
163 static const char *frag_shader_text =
164 "precision mediump float;\n"
165 "varying vec4 v_color;\n"
167 " gl_FragColor = v_color;\n"
170 static int running = 1;
172 static void debug_out(const char* str, ...)
178 va_start(arg_ptr, str);
179 vasprintf(&out, str, arg_ptr);
186 init_egl(struct display *display, struct window *window)
188 static const EGLint context_attribs[] = {
189 EGL_CONTEXT_CLIENT_VERSION, 2,
192 const char *extensions;
194 EGLint config_attribs[] = {
195 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
200 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
204 EGLint major, minor, n, count, i, size;
208 if (window->opaque || window->buffer_size == 16)
209 config_attribs[9] = 0;
211 display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL);
212 assert(display->egl.dpy);
214 ret = eglInitialize(display->egl.dpy, &major, &minor);
215 assert(ret == EGL_TRUE);
216 ret = eglBindAPI(EGL_OPENGL_ES_API);
217 assert(ret == EGL_TRUE);
219 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
222 configs = calloc(count, sizeof *configs);
225 ret = eglChooseConfig(display->egl.dpy, config_attribs,
227 assert(ret && n >= 1);
229 for (i = 0; i < n; i++) {
230 eglGetConfigAttrib(display->egl.dpy,
231 configs[i], EGL_BUFFER_SIZE, &size);
232 if (window->buffer_size == size) {
233 display->egl.conf = configs[i];
238 if (display->egl.conf == NULL) {
239 debug_out("did not find config with buffer size %d\n",
240 window->buffer_size);
244 display->egl.ctx = eglCreateContext(display->egl.dpy,
246 EGL_NO_CONTEXT, context_attribs);
247 assert(display->egl.ctx);
249 display->swap_buffers_with_damage = NULL;
250 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
252 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
253 strstr(extensions, "EGL_EXT_buffer_age"))
254 display->swap_buffers_with_damage =
255 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
256 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
258 if (display->swap_buffers_with_damage)
259 debug_out("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
264 fini_egl(struct display *display)
266 eglTerminate(display->egl.dpy);
271 create_shader(struct window *window, const char *source, GLenum shader_type)
276 shader = glCreateShader(shader_type);
279 glShaderSource(shader, 1, (const char **) &source, NULL);
280 glCompileShader(shader);
282 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
286 glGetShaderInfoLog(shader, 1000, &len, log);
287 debug_out("Error: compiling %s: %*s\n",
288 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
297 init_gl(struct window *window)
303 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
304 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
306 program = glCreateProgram();
307 glAttachShader(program, frag);
308 glAttachShader(program, vert);
309 glLinkProgram(program);
311 glGetProgramiv(program, GL_LINK_STATUS, &status);
315 glGetProgramInfoLog(program, 1000, &len, log);
316 debug_out("Error: linking:\n%*s\n", len, log);
320 glUseProgram(program);
325 glBindAttribLocation(program, window->gl.pos, "pos");
326 glBindAttribLocation(program, window->gl.col, "color");
327 glLinkProgram(program);
329 window->gl.rotation_uniform =
330 glGetUniformLocation(program, "rotation");
334 create_ivi_surface(struct window *window, struct display *display)
336 uint32_t id_ivisurf = g_id_ivisurf;
337 window->ivi_surface =
338 ivi_application_surface_create(display->ivi_application,
339 id_ivisurf, window->surface);
341 if (window->ivi_surface == NULL) {
342 debug_out("Failed to create ivi_client_surface\n");
349 create_surface(struct window *window)
351 struct display *display = window->display;
354 window->surface = wl_compositor_create_surface(display->compositor);
357 wl_egl_window_create(window->surface,
358 window->geometry.width,
359 window->geometry.height);
360 window->egl_surface =
361 weston_platform_create_egl_surface(display->egl.dpy,
363 window->native, NULL);
366 if (display->ivi_application ) {
367 create_ivi_surface(window, display);
372 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
373 window->egl_surface, window->display->egl.ctx);
374 assert(ret == EGL_TRUE);
376 if (!window->frame_sync)
377 eglSwapInterval(display->egl.dpy, 0);
382 destroy_surface(struct window *window)
384 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
385 * on eglReleaseThread(). */
386 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
389 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
390 wl_egl_window_destroy(window->native);
392 if (window->display->ivi_application)
393 ivi_surface_destroy(window->ivi_surface);
394 wl_surface_destroy(window->surface);
396 if (window->callback)
397 wl_callback_destroy(window->callback);
401 redraw(void *data, struct wl_callback *callback, uint32_t time)
403 struct window *window = data;
404 struct display *display = window->display;
405 static const GLfloat verts[3][2] = {
411 static const GLfloat verts_sub[3][2] = {
417 static const GLfloat colors_H[3][3] = {
423 static const GLfloat colors_N[3][3] = {
430 GLfloat rotation[4][4] = {
436 static const uint32_t speed_div = 5, benchmark_interval = 5;
437 struct wl_region *region;
439 EGLint buffer_age = 0;
442 assert(window->callback == callback);
443 window->callback = NULL;
446 wl_callback_destroy(callback);
448 gettimeofday(&tv, NULL);
449 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
450 if (window->frames == 0)
451 window->benchmark_time = time;
453 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
454 debug_out("%d frames in %d seconds: %f fps\n",
457 (float) window->frames / benchmark_interval);
458 window->benchmark_time = time;
462 angle = (time / speed_div) % 360 * M_PI / 180.0;
463 rotation[0][0] = cos(angle);
464 rotation[0][2] = sin(angle);
465 rotation[2][0] = -sin(angle);
466 rotation[2][2] = cos(angle);
468 if (display->swap_buffers_with_damage)
469 eglQuerySurface(display->egl.dpy, window->egl_surface,
470 EGL_BUFFER_AGE_EXT, &buffer_age);
472 glViewport(0, 0, window->geometry.width, window->geometry.height);
474 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
475 (GLfloat *) rotation);
477 glClearColor(0.0, 0.0, 0.0, 0.5);
478 glClear(GL_COLOR_BUFFER_BIT);
480 if(window->geometry.height == 1488)
482 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
486 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts_sub);
488 if(app_name == string("HVAC")){
489 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_H);
492 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_N);
494 glEnableVertexAttribArray(window->gl.pos);
495 glEnableVertexAttribArray(window->gl.col);
497 glDrawArrays(GL_TRIANGLES, 0, 3);
499 glDisableVertexAttribArray(window->gl.pos);
500 glDisableVertexAttribArray(window->gl.col);
502 if (window->opaque || window->fullscreen) {
503 region = wl_compositor_create_region(window->display->compositor);
504 wl_region_add(region, 0, 0,
505 window->geometry.width,
506 window->geometry.height);
507 wl_surface_set_opaque_region(window->surface, region);
508 wl_region_destroy(region);
510 wl_surface_set_opaque_region(window->surface, NULL);
513 if (display->swap_buffers_with_damage && buffer_age > 0) {
514 rect[0] = window->geometry.width / 4 - 1;
515 rect[1] = window->geometry.height / 4 - 1;
516 rect[2] = window->geometry.width / 2 + 2;
517 rect[3] = window->geometry.height / 2 + 2;
518 display->swap_buffers_with_damage(display->egl.dpy,
522 eglSwapBuffers(display->egl.dpy, window->egl_surface);
529 registry_handle_global(void *data, struct wl_registry *registry,
530 uint32_t name, const char *interface, uint32_t version)
532 struct display *d = data;
534 if (strcmp(interface, "wl_compositor") == 0) {
536 wl_registry_bind(registry, name,
537 &wl_compositor_interface, 1);
538 } else if (strcmp(interface, "ivi_application") == 0) {
540 wl_registry_bind(registry, name,
541 &ivi_application_interface, 1);
546 registry_handle_global_remove(void *data, struct wl_registry *registry,
551 static const struct wl_registry_listener registry_listener = {
552 registry_handle_global,
553 registry_handle_global_remove
557 signal_int(int signum)
563 init_wm(LibWindowmanager *wm)
567 if (wm->init(port, token.c_str()) != 0) {
568 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] wm init failed. \n");
572 json_object *obj = json_object_new_object();
573 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
574 if (wm->requestSurface(obj) != 0) {
575 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] wm request surface failed \n");
579 wm->set_event_handler(LibWindowmanager::Event_Active, [wm](json_object *object) {
580 const char *label = json_object_get_string(
581 json_object_object_get(object, wm->kKeyDrawingName));
582 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got activated! \n", label);
585 wm->set_event_handler(LibWindowmanager::Event_Inactive, [wm](json_object *object) {
586 const char *label = json_object_get_string(
587 json_object_object_get(object, wm->kKeyDrawingName));
588 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got inactivated!\n", label);
591 wm->set_event_handler(LibWindowmanager::Event_Visible, [wm](json_object *object) {
592 const char *label = json_object_get_string(
593 json_object_object_get(object, wm->kKeyDrawingName));
594 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got visibled!\n", label);
597 wm->set_event_handler(LibWindowmanager::Event_Invisible, [wm](json_object *object) {
598 const char *label = json_object_get_string(
599 json_object_object_get(object, wm->kKeyDrawingName));
600 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got invisibled!\n", label);
604 wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](json_object *object) {
605 const char *label = json_object_get_string(
606 json_object_object_get(object, wm->kKeyDrawingName));
607 const char *area = json_object_get_string(
608 json_object_object_get(object, wm->kKeyDrawingArea));
609 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got syncdraw!\n", label);
610 if ((wm->kStrLayoutNormal + "." + wm->kStrAreaFull) == std::string(area)) {
611 wl_egl_window_resize(gWindow->native, 1080, 1488, 0, 0);
612 gWindow->geometry.width = 1080;
613 gWindow->geometry.height = 1488;
615 else if ((wm->kStrLayoutSplit + "." + wm->kStrAreaMain) == std::string(area) ||
616 (wm->kStrLayoutSplit + "." + wm->kStrAreaSub) == std::string(area)) {
617 wl_egl_window_resize(gWindow->native, 1080, 744, 0, 0);
618 gWindow->geometry.width = 1080;
619 gWindow->geometry.height = 744;
621 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] try to endDraw %s \n", app_name.c_str());
622 if (!gWindow->fullscreen)
623 gWindow->window_size = gWindow->geometry;
625 json_object *obj = json_object_new_object();
626 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
631 wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [wm](json_object *object) {
632 const char *label = json_object_get_string(
633 json_object_object_get(object, wm->kKeyDrawingName));
634 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got flushdraw! \n", label);
639 surfaceIdStr = getenv("QT_IVI_SURFACE_ID");
640 } while (surfaceIdStr == NULL);
642 g_id_ivisurf = atoi(surfaceIdStr);
643 debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] IVI_SURFACE_ID: %d \n", g_id_ivisurf);
649 init_hs(LibHomeScreen* hs){
650 if(hs->init(port, token)!=0)
652 debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] homescreen init failed. \n");
656 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](json_object *object){
657 const char *application_name = json_object_get_string(
658 json_object_object_get(object, "application_name"));
659 debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] Event_TapShortcut application_name = %s \n", application_name);
660 if(strcmp(application_name, app_name.c_str()) == 0)
662 debug_out("************** [SIMPLE EGL] [HS SIMPLE] try to activesurface %s \n", app_name.c_str());
663 json_object *obj = json_object_new_object();
664 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
665 json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
667 wm->activateSurface(obj);
671 hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](json_object *object){
672 const char *display_message = json_object_get_string(
673 json_object_object_get(object, "display_message"));
674 debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] Event_OnScreenMessage display_message = %s \n", display_message);
681 main(int argc, char **argv)
683 struct sigaction sigint;
684 struct window window = { 0 };
685 struct display display = { 0 };
687 if(getenv("ENABLE_DEMO_DEBUG"))
692 window.display = &display;
693 display.window = &window;
694 window.geometry.width = 1080;
695 window.geometry.height = 1488;
696 window.window_size = window.geometry;
697 window.buffer_size = 32;
698 window.frame_sync = 1;
703 if(string(argv[0]).find("hvac") != string::npos)
704 app_name = string("HVAC");
705 port = strtol(argv[1], NULL, 10);
709 debug_out("************** [SIMPLE EGL] [MAIN] app_name: %s, port: %d, token: %s. \n", app_name.c_str(), port, token.c_str());
711 display.display = wl_display_connect(NULL);
712 assert(display.display);
714 display.registry = wl_display_get_registry(display.display);
715 wl_registry_add_listener(display.registry,
716 ®istry_listener, &display);
718 wl_display_roundtrip(display.display);
720 init_egl(&display, &window);
722 wm = new LibWindowmanager();
723 hs = new LibHomeScreen();
727 if (display.ivi_application)
728 ivi_application_destroy(display.ivi_application);
729 if (display.compositor)
730 wl_compositor_destroy(display.compositor);
731 wl_registry_destroy(display.registry);
732 wl_display_flush(display.display);
738 if (display.ivi_application)
739 ivi_application_destroy(display.ivi_application);
740 if (display.compositor)
741 wl_compositor_destroy(display.compositor);
742 wl_registry_destroy(display.registry);
743 wl_display_flush(display.display);
747 create_surface(&window);
751 sigint.sa_handler = signal_int;
752 sigemptyset(&sigint.sa_mask);
753 sigint.sa_flags = SA_RESETHAND;
754 sigaction(SIGINT, &sigint, NULL);
756 wl_display_dispatch_pending(display.display);
757 redraw(&window, NULL, 0);
758 json_object *obj = json_object_new_object();
759 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
760 json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
761 wm->activateSurface(obj);
763 /* The mainloop here is a little subtle. Redrawing will cause
764 * EGL to read events so we can just call
765 * wl_display_dispatch_pending() to handle any events that got
766 * queued up as a side effect. */
768 wl_display_dispatch_pending(display.display);
775 redraw(&window, NULL, 0);
778 debug_out("************** [SIMPLE EGL] [MAIN] simple-egl exiting! \n");
780 destroy_surface(&window);
783 if (display.ivi_application)
784 ivi_application_destroy(display.ivi_application);
786 if (display.compositor)
787 wl_compositor_destroy(display.compositor);
789 wl_registry_destroy(display.registry);
790 wl_display_flush(display.display);
791 wl_display_disconnect(display.display);