/* * Copyright © 2011 Benjamin Franzke * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "shared/helpers.h" #include "shared/platform.h" #include "shared/weston-egl-ext.h" #define LOG(fmt, ...) printf("simple egl: " fmt "\n", ##__VA_ARGS__) struct window; struct seat; struct display { struct wl_display *display; struct wl_registry *registry; struct wl_compositor *compositor; // struct wl_seat *seat; // struct wl_pointer *pointer; // struct wl_touch *touch; // struct wl_keyboard *keyboard; // struct wl_shm *shm; struct wms *wms; struct { EGLDisplay dpy; EGLContext ctx; EGLConfig conf; } egl; struct window *window; PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage; }; struct geometry { int width, height; }; struct window { struct display *display; struct geometry geometry, window_size; struct { GLuint rotation_uniform; GLuint pos; GLuint col; } gl; uint32_t benchmark_time, frames; struct wl_egl_window *native; struct wl_surface *surface; EGLSurface egl_surface; struct wl_callback *callback; int fullscreen, maximized, opaque, buffer_size, frame_sync, delay; bool wait_for_configure; }; static const char *vert_shader_text = "uniform mat4 rotation;\n" "attribute vec4 pos;\n" "attribute vec4 color;\n" "varying vec4 v_color;\n" "void main() {\n" " gl_Position = rotation * pos;\n" " v_color = color;\n" "}\n"; static const char *frag_shader_text = "precision mediump float;\n" "varying vec4 v_color;\n" "void main() {\n" " gl_FragColor = v_color;\n" "}\n"; static int running = 1; static void init_egl(struct display *display, struct window *window) { static const struct { char *extension, *entrypoint; } swap_damage_ext_to_entrypoint[] = { { .extension = "EGL_EXT_swap_buffers_with_damage", .entrypoint = "eglSwapBuffersWithDamageEXT", }, { .extension = "EGL_KHR_swap_buffers_with_damage", .entrypoint = "eglSwapBuffersWithDamageKHR", }, }; static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const char *extensions; EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint major, minor, n, count, i, size; EGLConfig *configs; EGLBoolean ret; if (window->opaque || window->buffer_size == 16) config_attribs[9] = 0; display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL); assert(display->egl.dpy); ret = eglInitialize(display->egl.dpy, &major, &minor); assert(ret == EGL_TRUE); ret = eglBindAPI(EGL_OPENGL_ES_API); assert(ret == EGL_TRUE); if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1) assert(0); configs = calloc(count, sizeof *configs); assert(configs); ret = eglChooseConfig(display->egl.dpy, config_attribs, configs, count, &n); assert(ret && n >= 1); for (i = 0; i < n; i++) { eglGetConfigAttrib(display->egl.dpy, configs[i], EGL_BUFFER_SIZE, &size); if (window->buffer_size == size) { display->egl.conf = configs[i]; break; } } free(configs); if (display->egl.conf == NULL) { fprintf(stderr, "did not find config with buffer size %d\n", window->buffer_size); exit(EXIT_FAILURE); } display->egl.ctx = eglCreateContext(display->egl.dpy, display->egl.conf, EGL_NO_CONTEXT, context_attribs); assert(display->egl.ctx); display->swap_buffers_with_damage = NULL; extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS); if (extensions && weston_check_egl_extension(extensions, "EGL_EXT_buffer_age")) { for (i = 0; i < (int) ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) { if (weston_check_egl_extension(extensions, swap_damage_ext_to_entrypoint[i].extension)) { /* The EXTPROC is identical to the KHR one */ display->swap_buffers_with_damage = (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC) eglGetProcAddress(swap_damage_ext_to_entrypoint[i].entrypoint); break; } } } if (display->swap_buffers_with_damage) printf("has EGL_EXT_buffer_age and %s\n", swap_damage_ext_to_entrypoint[i].extension); } static void fini_egl(struct display *display) { eglTerminate(display->egl.dpy); eglReleaseThread(); } static GLuint create_shader(struct window *window, const char *source, GLenum shader_type) { GLuint shader; GLint status; shader = glCreateShader(shader_type); assert(shader != 0); glShaderSource(shader, 1, (const char **) &source, NULL); glCompileShader(shader); glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (!status) { char log[1000]; GLsizei len; glGetShaderInfoLog(shader, 1000, &len, log); fprintf(stderr, "Error: compiling %s: %.*s\n", shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", len, log); exit(1); } return shader; } static void init_gl(struct window *window) { GLuint frag, vert; GLuint program; GLint status; frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); program = glCreateProgram(); glAttachShader(program, frag); glAttachShader(program, vert); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &status); if (!status) { char log[1000]; GLsizei len; glGetProgramInfoLog(program, 1000, &len, log); fprintf(stderr, "Error: linking:\n%.*s\n", len, log); exit(1); } glUseProgram(program); window->gl.pos = 0; window->gl.col = 1; glBindAttribLocation(program, window->gl.pos, "pos"); glBindAttribLocation(program, window->gl.col, "color"); glLinkProgram(program); window->gl.rotation_uniform = glGetUniformLocation(program, "rotation"); } static void create_surface(struct window *window) { struct display *display = window->display; EGLBoolean ret; window->surface = wl_compositor_create_surface(display->compositor); // wms window create. wms_create_window(window->display->wms, window->surface, 0, 0); wl_display_flush(window->display->display); wl_display_roundtrip(window->display->display); wl_display_roundtrip(window->display->display); window->native = wl_egl_window_create(window->surface, window->geometry.width, window->geometry.height); window->egl_surface = weston_platform_create_egl_surface(display->egl.dpy, display->egl.conf, window->native, NULL); window->wait_for_configure = false; wl_surface_commit(window->surface); ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, window->egl_surface, window->display->egl.ctx); assert(ret == EGL_TRUE); if (!window->frame_sync) eglSwapInterval(display->egl.dpy, 0); } static void destroy_surface(struct window *window) { /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() * on eglReleaseThread(). */ eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); weston_platform_destroy_egl_surface(window->display->egl.dpy, window->egl_surface); wl_egl_window_destroy(window->native); wl_surface_destroy(window->surface); if (window->callback) wl_callback_destroy(window->callback); } static void redraw(void *data, struct wl_callback *callback, uint32_t time) { struct window *window = data; struct display *display = window->display; static const GLfloat verts[3][2] = { { -0.5, -0.5 }, { 0.5, -0.5 }, { 0, 0.5 } }; static const GLfloat colors[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; GLfloat angle; GLfloat rotation[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; static const uint32_t speed_div = 5, benchmark_interval = 5; struct wl_region *region; EGLint rect[4]; EGLint buffer_age = 0; struct timeval tv; assert(window->callback == callback); window->callback = NULL; if (callback) wl_callback_destroy(callback); gettimeofday(&tv, NULL); time = tv.tv_sec * 1000 + tv.tv_usec / 1000; if (window->frames == 0) window->benchmark_time = time; if (time - window->benchmark_time > (benchmark_interval * 1000)) { printf("%d frames in %d seconds: %f fps\n", window->frames, benchmark_interval, (float) window->frames / benchmark_interval); window->benchmark_time = time; window->frames = 0; } angle = (time / speed_div) % 360 * M_PI / 180.0; rotation[0][0] = cos(angle); rotation[0][2] = sin(angle); rotation[2][0] = -sin(angle); rotation[2][2] = cos(angle); if (display->swap_buffers_with_damage) eglQuerySurface(display->egl.dpy, window->egl_surface, EGL_BUFFER_AGE_EXT, &buffer_age); glViewport(0, 0, window->geometry.width, window->geometry.height); glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, (GLfloat *) rotation); glClearColor(0.0, 0.0, 0.0, 0.5); glClear(GL_COLOR_BUFFER_BIT); glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); glEnableVertexAttribArray(window->gl.pos); glEnableVertexAttribArray(window->gl.col); glDrawArrays(GL_TRIANGLES, 0, 3); glDisableVertexAttribArray(window->gl.pos); glDisableVertexAttribArray(window->gl.col); usleep(window->delay); if (window->opaque || window->fullscreen) { region = wl_compositor_create_region(window->display->compositor); wl_region_add(region, 0, 0, window->geometry.width, window->geometry.height); wl_surface_set_opaque_region(window->surface, region); wl_region_destroy(region); } else { wl_surface_set_opaque_region(window->surface, NULL); } if (display->swap_buffers_with_damage && buffer_age > 0) { rect[0] = window->geometry.width / 4 - 1; rect[1] = window->geometry.height / 4 - 1; rect[2] = window->geometry.width / 2 + 2; rect[3] = window->geometry.height / 2 + 2; display->swap_buffers_with_damage(display->egl.dpy, window->egl_surface, rect, 1); } else { fprintf(stderr,"pss %s %d, %s\n",__func__,__LINE__,__FILE__); eglSwapBuffers(display->egl.dpy, window->egl_surface); } window->frames++; } /** static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx, wl_fixed_t sy) { struct display *display = data; struct wl_buffer *buffer; } static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) { } static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) { } static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { struct display *display = data; } static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) { } static const struct wl_pointer_listener pointer_listener = { pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis, }; static void touch_handle_down(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time, struct wl_surface *surface, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) { struct display *d = (struct display *)data; } static void touch_handle_up(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time, int32_t id) { } static void touch_handle_motion(void *data, struct wl_touch *wl_touch, uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) { } static void touch_handle_frame(void *data, struct wl_touch *wl_touch) { } static void touch_handle_cancel(void *data, struct wl_touch *wl_touch) { } static const struct wl_touch_listener touch_listener = { touch_handle_down, touch_handle_up, touch_handle_motion, touch_handle_frame, touch_handle_cancel, }; static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) { // Just so we don’t leak the keymap fd close(fd); } static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { } static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) { } static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { struct display *d = data; if (key == KEY_ESC && state) running = 0; } static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { } static const struct wl_keyboard_listener keyboard_listener = { keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers, }; static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) { struct display *d = data; if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) { d->pointer = wl_seat_get_pointer(seat); wl_pointer_add_listener(d->pointer, &pointer_listener, d); } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) { wl_pointer_destroy(d->pointer); d->pointer = NULL; } if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) { d->keyboard = wl_seat_get_keyboard(seat); wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) { wl_keyboard_destroy(d->keyboard); d->keyboard = NULL; } if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) { d->touch = wl_seat_get_touch(seat); wl_touch_set_user_data(d->touch, d); wl_touch_add_listener(d->touch, &touch_listener, d); } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) { wl_touch_destroy(d->touch); d->touch = NULL; } } static const struct wl_seat_listener seat_listener = { seat_handle_capabilities, }; */ static void WindowShotError(void *data, struct wms *wms, uint32_t error, uint32_t window_id) { LOG("WindowShotError error: %d", error); LOG("WindowShotError window_id: %d", window_id); } static void WindowShotDone(void *data, struct wms *wms, uint32_t window_id, int32_t fd, int32_t width, int32_t height, int32_t stride, uint32_t format, uint32_t seconds, uint32_t nanoseconds) { LOG("WindowShotDone window_id: %d", window_id); LOG("WindowShotDone fd: %d", fd); LOG("WindowShotDone width: %d", width); LOG("WindowShotDone height: %d", height); LOG("WindowShotDone stride: %d", stride); LOG("WindowShotDone format: %d", format); LOG("WindowShotDone seconds: %d", seconds); LOG("WindowShotDone nanoseconds: %d", nanoseconds); LOG("windowshot OK!!!"); close(fd); } static void ScreenShotError(void *data, struct wms *wms, uint32_t error, uint32_t screen_id) { LOG("ScreenShotError error: %d", error); LOG("ScreenShotError screen_id: %d", screen_id); } static void ScreenShotDone(void *data, struct wms *wms, uint32_t screen_id, int32_t fd, int32_t width, int32_t height, int32_t stride, uint32_t format, uint32_t seconds, uint32_t nanoseconds) { LOG("ScreenShotDone screen_id: %d", screen_id); LOG("ScreenShotDone fd: %d", fd); LOG("ScreenShotDone width: %d", width); LOG("ScreenShotDone height: %d", height); LOG("ScreenShotDone stride: %d", stride); LOG("ScreenShotDone format: %d", format); LOG("ScreenShotDone seconds: %d", seconds); LOG("ScreenShotDone nanoseconds: %d", nanoseconds); LOG("screenshot OK!!!"); close(fd); } static void ReplyStatus(void *data, struct wms *wms, uint32_t status) { LOG("ReplyStatus status: %d", status); } static void DisplayMode(void *data, struct wms *wms, uint32_t flag) { LOG("DisplayMode flag: %d", flag); } void ScreenUpdate(void *data, struct wms *wms, uint32_t screen_id, const char *name, uint32_t update_state, int width, int height) { LOG("screenUpdate screen_id: %d", screen_id); LOG("screenUpdate name: %s", name); LOG("screenUpdate update_state: %d", update_state); LOG("screenUpdate width: %d", width); LOG("screenUpdate height: %d", height); if (update_state == WMS_SCREEN_STATUS_ADD) { LOG("screen add. "); } else { LOG("screen destroy."); } } void WindowUpdate(void *data, struct wms *wms, uint32_t update_state, uint32_t window_id, int32_t x, int32_t y, int32_t width, int32_t height) { struct display *d = data; LOG("WindowUpdate window_id: %d", window_id); LOG("WindowUpdate update_state: %d", update_state); LOG("WindowUpdate x:%d, y:%d", x, y); LOG("WindowUpdate width:%d, height:%d", width, height); if (update_state == WMS_WINDOW_STATUS_CREATED) { LOG("window %d create. ", window_id); d->window->geometry.width = width; d->window->geometry.height = height; } else if (update_state == WMS_WINDOW_STATUS_FAILED) { LOG("window create failed. "); } else { LOG("window %d destroy. ", window_id); } } static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct display *d = data; if (strcmp(interface, "wl_compositor") == 0) { d->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, MIN(version, 4)); } /**else if (strcmp(interface, "wl_seat") == 0) { d->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); wl_seat_add_listener(d->seat, &seat_listener, d); } else if (strcmp(interface, "wl_shm") == 0) { d->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); } */else if (strcmp(interface, "wms") == 0) { d->wms = (struct wms *)wl_registry_bind(registry, name, &wms_interface, 1); static struct wms_listener wmsListener = { WindowUpdate, ScreenUpdate, DisplayMode, ReplyStatus, ScreenShotDone, ScreenShotError, WindowShotDone, WindowShotError }; wms_add_listener(d->wms, &wmsListener, d); wl_display_flush(d->display); wl_display_roundtrip(d->display); } } static void registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { } static const struct wl_registry_listener registry_listener = { registry_handle_global, registry_handle_global_remove }; static void signal_int(int signum) { running = 0; } static void usage(int error_code) { fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n" " -d \tBuffer swap delay in microseconds\n" " -f\tRun in fullscreen mode\n" " -o\tCreate an opaque surface\n" " -s\tUse a 16 bpp EGL config\n" " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n" " -h\tThis help text\n\n"); exit(error_code); } int main(int argc, char **argv) { struct sigaction sigint; struct display display = { 0 }; struct window window = { 0 }; int i, ret = 0; window.display = &display; display.window = &window; window.geometry.width = 250; window.geometry.height = 250; window.window_size = window.geometry; window.buffer_size = 32; window.frame_sync = 1; window.delay = 0; for (i = 1; i < argc; i++) { if (strcmp("-d", argv[i]) == 0 && i+1 < argc) window.delay = atoi(argv[++i]); else if (strcmp("-f", argv[i]) == 0) window.fullscreen = 1; else if (strcmp("-o", argv[i]) == 0) window.opaque = 1; else if (strcmp("-s", argv[i]) == 0) window.buffer_size = 16; else if (strcmp("-b", argv[i]) == 0) window.frame_sync = 0; else if (strcmp("-h", argv[i]) == 0) usage(EXIT_SUCCESS); else usage(EXIT_FAILURE); } display.display = wl_display_connect(NULL); assert(display.display); display.registry = wl_display_get_registry(display.display); wl_registry_add_listener(display.registry, ®istry_listener, &display); wl_display_roundtrip(display.display); init_egl(&display, &window); create_surface(&window); init_gl(&window); sigint.sa_handler = signal_int; sigemptyset(&sigint.sa_mask); sigint.sa_flags = SA_RESETHAND; sigaction(SIGINT, &sigint, NULL); /* The mainloop here is a little subtle. Redrawing will cause * EGL to read events so we can just call * wl_display_dispatch_pending() to handle any events that got * queued up as a side effect. */ while (running && ret != -1) { if (window.wait_for_configure) { ret = wl_display_dispatch(display.display); } else { ret = wl_display_dispatch_pending(display.display); redraw(&window, NULL, 0); } } fprintf(stderr, "simple-egl exiting\n"); destroy_surface(&window); fini_egl(&display); if (display.compositor) wl_compositor_destroy(display.compositor); wl_registry_destroy(display.registry); wl_display_flush(display.display); wl_display_disconnect(display.display); return 0; }