Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 28 | #include <string.h> |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 30 | #include <EGL/egl.h> |
| 31 | #include <wayland-egl.h> |
| 32 | #include <GLES2/gl2.h> |
| 33 | |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 34 | #include "weston-test-client-helper.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 35 | #include "shared/platform.h" |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 36 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 37 | #define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; } |
| 38 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 39 | struct test_data { |
| 40 | struct client *client; |
| 41 | |
| 42 | EGLDisplay egl_dpy; |
| 43 | EGLContext egl_ctx; |
| 44 | EGLConfig egl_conf; |
| 45 | EGLSurface egl_surface; |
| 46 | }; |
| 47 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 48 | static int |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 49 | init_egl(struct test_data *test_data) |
| 50 | { |
| 51 | struct wl_egl_window *native_window; |
| 52 | struct surface *surface = test_data->client->surface; |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 53 | const char *str, *mesa; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 54 | |
| 55 | static const EGLint context_attribs[] = { |
| 56 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 57 | EGL_NONE |
| 58 | }; |
| 59 | |
| 60 | EGLint config_attribs[] = { |
| 61 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 62 | EGL_RED_SIZE, 1, |
| 63 | EGL_GREEN_SIZE, 1, |
| 64 | EGL_BLUE_SIZE, 1, |
| 65 | EGL_ALPHA_SIZE, 0, |
| 66 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 67 | EGL_NONE |
| 68 | }; |
| 69 | |
| 70 | EGLint major, minor, n; |
| 71 | EGLBoolean ret; |
| 72 | |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 73 | test_data->egl_dpy = |
| 74 | weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, |
| 75 | test_data->client->wl_display, |
| 76 | NULL); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 77 | if (!test_data->egl_dpy) |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 78 | fail("eglGetPlatformDisplay or eglGetDisplay"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 79 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 80 | if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE) |
| 81 | fail("eglInitialize"); |
| 82 | if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) |
| 83 | fail("eglBindAPI"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 84 | |
| 85 | ret = eglChooseConfig(test_data->egl_dpy, config_attribs, |
| 86 | &test_data->egl_conf, 1, &n); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 87 | if (!(ret && n == 1)) |
| 88 | fail("eglChooseConfig"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 89 | |
| 90 | test_data->egl_ctx = eglCreateContext(test_data->egl_dpy, |
| 91 | test_data->egl_conf, |
| 92 | EGL_NO_CONTEXT, context_attribs); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 93 | if (!test_data->egl_ctx) |
| 94 | fail("eglCreateContext"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 95 | |
| 96 | native_window = |
| 97 | wl_egl_window_create(surface->wl_surface, |
| 98 | surface->width, |
| 99 | surface->height); |
| 100 | test_data->egl_surface = |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 101 | weston_platform_create_egl_surface(test_data->egl_dpy, |
| 102 | test_data->egl_conf, |
| 103 | native_window, NULL); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 104 | |
| 105 | ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface, |
| 106 | test_data->egl_surface, test_data->egl_ctx); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 107 | if (ret != EGL_TRUE) |
| 108 | fail("eglMakeCurrent"); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 109 | |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 110 | /* This test is specific to mesa 10.1 and later, which is the |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 111 | * first release that doesn't accidentally triple-buffer. */ |
| 112 | str = (const char *) glGetString(GL_VERSION); |
| 113 | mesa = strstr(str, "Mesa "); |
| 114 | if (mesa == NULL) |
| 115 | skip("unknown EGL implementation (%s)\n", str); |
| 116 | if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2) |
| 117 | skip("unrecognized mesa version (%s)\n", str); |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 118 | if (major < 10 || (major == 10 && minor < 1)) |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 119 | skip("mesa version too old (%s)\n", str); |
| 120 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 121 | return 0; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST(test_buffer_count) |
| 125 | { |
| 126 | struct test_data test_data; |
| 127 | uint32_t buffer_count; |
| 128 | int i; |
| 129 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 130 | test_data.client = create_client_and_test_surface(10, 10, 10, 10); |
Derek Foreman | 9bb1339 | 2015-01-23 12:12:36 -0600 | [diff] [blame] | 131 | if (!test_data.client->has_wl_drm) |
| 132 | skip("compositor has not bound its display to EGL\n"); |
| 133 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 134 | if (init_egl(&test_data) < 0) |
| 135 | skip("could not initialize egl, " |
| 136 | "possibly using the headless backend\n"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 137 | |
| 138 | /* This is meant to represent a typical game loop which is |
| 139 | * expecting eglSwapBuffers to block and throttle the |
| 140 | * rendering to a sensible frame rate. Therefore it doesn't |
| 141 | * expect to have to install a frame callback itself. I'd |
| 142 | * imagine this is what a typical SDL game would end up |
| 143 | * doing */ |
| 144 | |
| 145 | for (i = 0; i < 10; i++) { |
| 146 | glClear(GL_COLOR_BUFFER_BIT); |
| 147 | eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface); |
| 148 | } |
| 149 | |
| 150 | buffer_count = get_n_egl_buffers(test_data.client); |
| 151 | |
| 152 | printf("buffers used = %i\n", buffer_count); |
| 153 | |
| 154 | /* The implementation should only end up creating two buffers |
| 155 | * and cycling between them */ |
| 156 | assert(buffer_count == 2); |
| 157 | } |