Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame^] | 23 | #include "config.h" |
| 24 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
| 27 | #include "weston-test-client-helper.h" |
| 28 | #include <stdio.h> |
| 29 | #include <poll.h> |
| 30 | #include <time.h> |
| 31 | |
| 32 | #include <EGL/egl.h> |
| 33 | #include <wayland-egl.h> |
| 34 | #include <GLES2/gl2.h> |
| 35 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 36 | #define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; } |
| 37 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 38 | struct test_data { |
| 39 | struct client *client; |
| 40 | |
| 41 | EGLDisplay egl_dpy; |
| 42 | EGLContext egl_ctx; |
| 43 | EGLConfig egl_conf; |
| 44 | EGLSurface egl_surface; |
| 45 | }; |
| 46 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 47 | static int |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 48 | init_egl(struct test_data *test_data) |
| 49 | { |
| 50 | struct wl_egl_window *native_window; |
| 51 | struct surface *surface = test_data->client->surface; |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 52 | const char *str, *mesa; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 53 | |
| 54 | static const EGLint context_attribs[] = { |
| 55 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 56 | EGL_NONE |
| 57 | }; |
| 58 | |
| 59 | EGLint config_attribs[] = { |
| 60 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 61 | EGL_RED_SIZE, 1, |
| 62 | EGL_GREEN_SIZE, 1, |
| 63 | EGL_BLUE_SIZE, 1, |
| 64 | EGL_ALPHA_SIZE, 0, |
| 65 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 66 | EGL_NONE |
| 67 | }; |
| 68 | |
| 69 | EGLint major, minor, n; |
| 70 | EGLBoolean ret; |
| 71 | |
| 72 | test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType) |
| 73 | test_data->client->wl_display); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 74 | if (!test_data->egl_dpy) |
| 75 | fail("eglGetDisplay"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 76 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 77 | if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE) |
| 78 | fail("eglInitialize"); |
| 79 | if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) |
| 80 | fail("eglBindAPI"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 81 | |
| 82 | ret = eglChooseConfig(test_data->egl_dpy, config_attribs, |
| 83 | &test_data->egl_conf, 1, &n); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 84 | if (!(ret && n == 1)) |
| 85 | fail("eglChooseConfig"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 86 | |
| 87 | test_data->egl_ctx = eglCreateContext(test_data->egl_dpy, |
| 88 | test_data->egl_conf, |
| 89 | EGL_NO_CONTEXT, context_attribs); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 90 | if (!test_data->egl_ctx) |
| 91 | fail("eglCreateContext"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 92 | |
| 93 | native_window = |
| 94 | wl_egl_window_create(surface->wl_surface, |
| 95 | surface->width, |
| 96 | surface->height); |
| 97 | test_data->egl_surface = |
| 98 | eglCreateWindowSurface(test_data->egl_dpy, |
| 99 | test_data->egl_conf, |
| 100 | (EGLNativeWindowType) native_window, |
| 101 | NULL); |
| 102 | |
| 103 | ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface, |
| 104 | test_data->egl_surface, test_data->egl_ctx); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 105 | if (ret != EGL_TRUE) |
| 106 | fail("eglMakeCurrent"); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 107 | |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 108 | /* 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] | 109 | * first release that doesn't accidentally triple-buffer. */ |
| 110 | str = (const char *) glGetString(GL_VERSION); |
| 111 | mesa = strstr(str, "Mesa "); |
| 112 | if (mesa == NULL) |
| 113 | skip("unknown EGL implementation (%s)\n", str); |
| 114 | if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2) |
| 115 | skip("unrecognized mesa version (%s)\n", str); |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 116 | if (major < 10 || (major == 10 && minor < 1)) |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 117 | skip("mesa version too old (%s)\n", str); |
| 118 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 119 | return 0; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST(test_buffer_count) |
| 123 | { |
| 124 | struct test_data test_data; |
| 125 | uint32_t buffer_count; |
| 126 | int i; |
| 127 | |
| 128 | test_data.client = client_create(10, 10, 10, 10); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 129 | if (init_egl(&test_data) < 0) |
| 130 | skip("could not initialize egl, " |
| 131 | "possibly using the headless backend\n"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 132 | |
| 133 | /* This is meant to represent a typical game loop which is |
| 134 | * expecting eglSwapBuffers to block and throttle the |
| 135 | * rendering to a sensible frame rate. Therefore it doesn't |
| 136 | * expect to have to install a frame callback itself. I'd |
| 137 | * imagine this is what a typical SDL game would end up |
| 138 | * doing */ |
| 139 | |
| 140 | for (i = 0; i < 10; i++) { |
| 141 | glClear(GL_COLOR_BUFFER_BIT); |
| 142 | eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface); |
| 143 | } |
| 144 | |
| 145 | buffer_count = get_n_egl_buffers(test_data.client); |
| 146 | |
| 147 | printf("buffers used = %i\n", buffer_count); |
| 148 | |
| 149 | /* The implementation should only end up creating two buffers |
| 150 | * and cycling between them */ |
| 151 | assert(buffer_count == 2); |
| 152 | } |