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"
59 uint32_t g_id_ivisurf = 9009;
61 string token = string("wm");
63 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;
173 init_egl(struct display *display, struct window *window)
175 static const EGLint context_attribs[] = {
176 EGL_CONTEXT_CLIENT_VERSION, 2,
179 const char *extensions;
181 EGLint config_attribs[] = {
182 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
187 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
191 EGLint major, minor, n, count, i, size;
195 if (window->opaque || window->buffer_size == 16)
196 config_attribs[9] = 0;
198 display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL);
199 assert(display->egl.dpy);
201 ret = eglInitialize(display->egl.dpy, &major, &minor);
202 assert(ret == EGL_TRUE);
203 ret = eglBindAPI(EGL_OPENGL_ES_API);
204 assert(ret == EGL_TRUE);
206 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
209 configs = calloc(count, sizeof *configs);
212 ret = eglChooseConfig(display->egl.dpy, config_attribs,
214 assert(ret && n >= 1);
216 for (i = 0; i < n; i++) {
217 eglGetConfigAttrib(display->egl.dpy,
218 configs[i], EGL_BUFFER_SIZE, &size);
219 if (window->buffer_size == size) {
220 display->egl.conf = configs[i];
225 if (display->egl.conf == NULL) {
226 HMI_ERROR("simple-egl","did not find config with buffer size %d",
227 window->buffer_size);
231 display->egl.ctx = eglCreateContext(display->egl.dpy,
233 EGL_NO_CONTEXT, context_attribs);
234 assert(display->egl.ctx);
236 display->swap_buffers_with_damage = NULL;
237 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
239 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
240 strstr(extensions, "EGL_EXT_buffer_age"))
241 display->swap_buffers_with_damage =
242 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
243 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
245 if (display->swap_buffers_with_damage)
246 HMI_DEBUG("simple-egl","has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage");
251 fini_egl(struct display *display)
253 eglTerminate(display->egl.dpy);
258 create_shader(struct window *window, const char *source, GLenum shader_type)
263 shader = glCreateShader(shader_type);
266 glShaderSource(shader, 1, (const char **) &source, NULL);
267 glCompileShader(shader);
269 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
273 glGetShaderInfoLog(shader, 1000, &len, log);
274 HMI_ERROR("simple-egl","Error: compiling %s: %*s",
275 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
284 init_gl(struct window *window)
290 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
291 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
293 program = glCreateProgram();
294 glAttachShader(program, frag);
295 glAttachShader(program, vert);
296 glLinkProgram(program);
298 glGetProgramiv(program, GL_LINK_STATUS, &status);
302 glGetProgramInfoLog(program, 1000, &len, log);
303 HMI_ERROR("simple-egl","Error: linking:%*s", len, log);
307 glUseProgram(program);
312 glBindAttribLocation(program, window->gl.pos, "pos");
313 glBindAttribLocation(program, window->gl.col, "color");
314 glLinkProgram(program);
316 window->gl.rotation_uniform =
317 glGetUniformLocation(program, "rotation");
321 create_ivi_surface(struct window *window, struct display *display)
323 uint32_t id_ivisurf = g_id_ivisurf;
324 window->ivi_surface =
325 ivi_application_surface_create(display->ivi_application,
326 id_ivisurf, window->surface);
328 if (window->ivi_surface == NULL) {
329 HMI_ERROR("simple-egl","Failed to create ivi_client_surface");
336 create_surface(struct window *window)
338 struct display *display = window->display;
341 window->surface = wl_compositor_create_surface(display->compositor);
344 wl_egl_window_create(window->surface,
345 window->geometry.width,
346 window->geometry.height);
347 window->egl_surface =
348 weston_platform_create_egl_surface(display->egl.dpy,
350 window->native, NULL);
353 if (display->ivi_application ) {
354 create_ivi_surface(window, display);
359 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
360 window->egl_surface, window->display->egl.ctx);
361 assert(ret == EGL_TRUE);
363 if (!window->frame_sync)
364 eglSwapInterval(display->egl.dpy, 0);
369 destroy_surface(struct window *window)
371 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
372 * on eglReleaseThread(). */
373 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
376 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
377 wl_egl_window_destroy(window->native);
379 if (window->display->ivi_application)
380 ivi_surface_destroy(window->ivi_surface);
381 wl_surface_destroy(window->surface);
383 if (window->callback)
384 wl_callback_destroy(window->callback);
388 redraw(void *data, struct wl_callback *callback, uint32_t time)
390 struct window *window = data;
391 struct display *display = window->display;
392 static const GLfloat verts[3][2] = {
398 static const GLfloat colors[3][3] = {
405 GLfloat rotation[4][4] = {
411 static const uint32_t speed_div = 5, benchmark_interval = 5;
412 struct wl_region *region;
414 EGLint buffer_age = 0;
417 assert(window->callback == callback);
418 window->callback = NULL;
421 wl_callback_destroy(callback);
423 gettimeofday(&tv, NULL);
424 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
425 if (window->frames == 0)
426 window->benchmark_time = time;
428 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
429 HMI_DEBUG("simple-egl","%d frames in %d seconds: %f fps",
432 (float) window->frames / benchmark_interval);
433 window->benchmark_time = time;
437 angle = (time / speed_div) % 360 * M_PI / 180.0;
438 rotation[0][0] = cos(angle);
439 rotation[0][2] = sin(angle);
440 rotation[2][0] = -sin(angle);
441 rotation[2][2] = cos(angle);
443 if (display->swap_buffers_with_damage)
444 eglQuerySurface(display->egl.dpy, window->egl_surface,
445 EGL_BUFFER_AGE_EXT, &buffer_age);
447 glViewport(0, 0, window->geometry.width, window->geometry.height);
449 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
450 (GLfloat *) rotation);
452 glClearColor(0.0, 0.0, 0.0, 0.5);
453 glClear(GL_COLOR_BUFFER_BIT);
455 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
456 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
457 glEnableVertexAttribArray(window->gl.pos);
458 glEnableVertexAttribArray(window->gl.col);
460 glDrawArrays(GL_TRIANGLES, 0, 3);
462 glDisableVertexAttribArray(window->gl.pos);
463 glDisableVertexAttribArray(window->gl.col);
465 if (window->opaque || window->fullscreen) {
466 region = wl_compositor_create_region(window->display->compositor);
467 wl_region_add(region, 0, 0,
468 window->geometry.width,
469 window->geometry.height);
470 wl_surface_set_opaque_region(window->surface, region);
471 wl_region_destroy(region);
473 wl_surface_set_opaque_region(window->surface, NULL);
476 if (display->swap_buffers_with_damage && buffer_age > 0) {
477 rect[0] = window->geometry.width / 4 - 1;
478 rect[1] = window->geometry.height / 4 - 1;
479 rect[2] = window->geometry.width / 2 + 2;
480 rect[3] = window->geometry.height / 2 + 2;
481 display->swap_buffers_with_damage(display->egl.dpy,
485 eglSwapBuffers(display->egl.dpy, window->egl_surface);
492 registry_handle_global(void *data, struct wl_registry *registry,
493 uint32_t name, const char *interface, uint32_t version)
495 struct display *d = data;
497 if (strcmp(interface, "wl_compositor") == 0) {
499 wl_registry_bind(registry, name,
500 &wl_compositor_interface, 1);
501 } else if (strcmp(interface, "ivi_application") == 0) {
503 wl_registry_bind(registry, name,
504 &ivi_application_interface, 1);
509 registry_handle_global_remove(void *data, struct wl_registry *registry,
514 static const struct wl_registry_listener registry_listener = {
515 registry_handle_global,
516 registry_handle_global_remove
520 signal_int(int signum)
526 init_wm(LibWindowmanager *wm, struct window *window)
528 HMI_DEBUG("simple-egl","called");
530 if (wm->init(port, token.c_str()) != 0) {
531 HMI_ERROR("simple-egl","wm init failed. ");
535 json_object *obj = json_object_new_object();
536 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
537 g_id_ivisurf = wm->requestSurface(obj);
538 if (g_id_ivisurf < 0) {
539 HMI_ERROR("simple-egl","wm request surface failed ");
542 HMI_DEBUG("simple-egl","IVI_SURFACE_ID: %d ", g_id_ivisurf);
543 wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm, window](json_object *object) {
544 const char *label = json_object_get_string(
545 json_object_object_get(object, wm->kKeyDrawingName));
546 const char *area = json_object_get_string(
547 json_object_object_get(object, wm->kKeyDrawingArea));
549 HMI_DEBUG("simple-egl","Surface %s got syncDraw! Area: %s. ", label, area);
550 if ((wm->kStrLayoutNormal + "." + wm->kStrAreaFull) == std::string(area)) {
551 wl_egl_window_resize(window->native, 1080, 1488, 0, 0);
552 window->geometry.width = 1080;
553 window->geometry.height = 1488;
555 else if ((wm->kStrLayoutSplit + "." + wm->kStrAreaMain) == std::string(area) ||
556 (wm->kStrLayoutSplit + "." + wm->kStrAreaSub) == std::string(area)) {
557 wl_egl_window_resize(window->native, 1080, 744, 0, 0);
558 window->geometry.width = 1080;
559 window->geometry.height = 744;
562 if (!window->fullscreen)
563 window->window_size = window->geometry;
564 json_object *obj = json_object_new_object();
565 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
574 init_hs(LibHomeScreen* hs){
575 if(hs->init(port, token)!=0)
577 HMI_ERROR("simple-egl","homescreen init failed. ");
581 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](json_object *object){
582 const char *application_name = json_object_get_string(
583 json_object_object_get(object, "application_name"));
584 HMI_DEBUG("simple-egl","Event_TapShortcut application_name = %s ", application_name);
585 if(strcmp(application_name, app_name.c_str()) == 0)
587 HMI_DEBUG("simple-egl","try to activesurface %s ", app_name.c_str());
588 json_object *obj = json_object_new_object();
589 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
590 json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
591 wm->activateSurface(obj);
599 main(int argc, char **argv)
601 struct sigaction sigint;
602 struct window window = { 0 };
603 struct display display = { 0 };
605 window.display = &display;
606 display.window = &window;
607 window.geometry.width = 1080;
608 window.geometry.height = 1488;
609 window.window_size = window.geometry;
610 window.buffer_size = 32;
611 window.frame_sync = 1;
614 port = strtol(argv[1], NULL, 10);
618 HMI_DEBUG("simple-egl","app_name: %s, port: %d, token: %s. ", app_name.c_str(), port, token.c_str());
620 display.display = wl_display_connect(NULL);
621 assert(display.display);
623 display.registry = wl_display_get_registry(display.display);
624 wl_registry_add_listener(display.registry,
625 ®istry_listener, &display);
627 wl_display_roundtrip(display.display);
629 init_egl(&display, &window);
631 wm = new LibWindowmanager();
632 if(init_wm(wm, &window)!=0){
634 if (display.ivi_application)
635 ivi_application_destroy(display.ivi_application);
636 if (display.compositor)
637 wl_compositor_destroy(display.compositor);
638 wl_registry_destroy(display.registry);
639 wl_display_flush(display.display);
643 hs = new LibHomeScreen();
646 if (display.ivi_application)
647 ivi_application_destroy(display.ivi_application);
648 if (display.compositor)
649 wl_compositor_destroy(display.compositor);
650 wl_registry_destroy(display.registry);
651 wl_display_flush(display.display);
655 create_surface(&window);
659 sigint.sa_handler = signal_int;
660 sigemptyset(&sigint.sa_mask);
661 sigint.sa_flags = SA_RESETHAND;
662 sigaction(SIGINT, &sigint, NULL);
664 eglSwapBuffers(window.display->egl.dpy, window.egl_surface);
665 json_object *obj = json_object_new_object();
666 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
667 json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
668 wm->activateSurface(obj);
670 /* The mainloop here is a little subtle. Redrawing will cause
671 * EGL to read events so we can just call
672 * wl_display_dispatch_pending() to handle any events that got
673 * queued up as a side effect. */
675 wl_display_dispatch_pending(display.display);
676 redraw(&window, NULL, 0);
679 HMI_DEBUG("simple-egl","simple-egl exiting! ");
681 destroy_surface(&window);
684 if (display.ivi_application)
685 ivi_application_destroy(display.ivi_application);
687 if (display.compositor)
688 wl_compositor_destroy(display.compositor);
690 wl_registry_destroy(display.registry);
691 wl_display_flush(display.display);
692 wl_display_disconnect(display.display);