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> |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 27 | #include <EGL/egl.h> |
| 28 | #include <wayland-egl.h> |
| 29 | #include <GLES2/gl2.h> |
| 30 | |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame^] | 31 | #include "weston-test-client-helper.h" |
| 32 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 33 | #define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; } |
| 34 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 35 | struct test_data { |
| 36 | struct client *client; |
| 37 | |
| 38 | EGLDisplay egl_dpy; |
| 39 | EGLContext egl_ctx; |
| 40 | EGLConfig egl_conf; |
| 41 | EGLSurface egl_surface; |
| 42 | }; |
| 43 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 44 | static int |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 45 | init_egl(struct test_data *test_data) |
| 46 | { |
| 47 | struct wl_egl_window *native_window; |
| 48 | struct surface *surface = test_data->client->surface; |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 49 | const char *str, *mesa; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 50 | |
| 51 | static const EGLint context_attribs[] = { |
| 52 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 53 | EGL_NONE |
| 54 | }; |
| 55 | |
| 56 | EGLint config_attribs[] = { |
| 57 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 58 | EGL_RED_SIZE, 1, |
| 59 | EGL_GREEN_SIZE, 1, |
| 60 | EGL_BLUE_SIZE, 1, |
| 61 | EGL_ALPHA_SIZE, 0, |
| 62 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 63 | EGL_NONE |
| 64 | }; |
| 65 | |
| 66 | EGLint major, minor, n; |
| 67 | EGLBoolean ret; |
| 68 | |
| 69 | test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType) |
| 70 | test_data->client->wl_display); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 71 | if (!test_data->egl_dpy) |
| 72 | fail("eglGetDisplay"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 73 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 74 | if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE) |
| 75 | fail("eglInitialize"); |
| 76 | if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) |
| 77 | fail("eglBindAPI"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 78 | |
| 79 | ret = eglChooseConfig(test_data->egl_dpy, config_attribs, |
| 80 | &test_data->egl_conf, 1, &n); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 81 | if (!(ret && n == 1)) |
| 82 | fail("eglChooseConfig"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 83 | |
| 84 | test_data->egl_ctx = eglCreateContext(test_data->egl_dpy, |
| 85 | test_data->egl_conf, |
| 86 | EGL_NO_CONTEXT, context_attribs); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 87 | if (!test_data->egl_ctx) |
| 88 | fail("eglCreateContext"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 89 | |
| 90 | native_window = |
| 91 | wl_egl_window_create(surface->wl_surface, |
| 92 | surface->width, |
| 93 | surface->height); |
| 94 | test_data->egl_surface = |
| 95 | eglCreateWindowSurface(test_data->egl_dpy, |
| 96 | test_data->egl_conf, |
| 97 | (EGLNativeWindowType) native_window, |
| 98 | NULL); |
| 99 | |
| 100 | ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface, |
| 101 | test_data->egl_surface, test_data->egl_ctx); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 102 | if (ret != EGL_TRUE) |
| 103 | fail("eglMakeCurrent"); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 104 | |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 105 | /* 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] | 106 | * first release that doesn't accidentally triple-buffer. */ |
| 107 | str = (const char *) glGetString(GL_VERSION); |
| 108 | mesa = strstr(str, "Mesa "); |
| 109 | if (mesa == NULL) |
| 110 | skip("unknown EGL implementation (%s)\n", str); |
| 111 | if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2) |
| 112 | skip("unrecognized mesa version (%s)\n", str); |
Pekka Paalanen | ca6bd74 | 2014-01-08 16:05:34 +0200 | [diff] [blame] | 113 | if (major < 10 || (major == 10 && minor < 1)) |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 114 | skip("mesa version too old (%s)\n", str); |
| 115 | |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 116 | return 0; |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(test_buffer_count) |
| 120 | { |
| 121 | struct test_data test_data; |
| 122 | uint32_t buffer_count; |
| 123 | int i; |
| 124 | |
| 125 | test_data.client = client_create(10, 10, 10, 10); |
Emilio Pozuelo Monfort | 08dbd31 | 2014-02-07 09:34:46 +0100 | [diff] [blame] | 126 | if (init_egl(&test_data) < 0) |
| 127 | skip("could not initialize egl, " |
| 128 | "possibly using the headless backend\n"); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 129 | |
| 130 | /* This is meant to represent a typical game loop which is |
| 131 | * expecting eglSwapBuffers to block and throttle the |
| 132 | * rendering to a sensible frame rate. Therefore it doesn't |
| 133 | * expect to have to install a frame callback itself. I'd |
| 134 | * imagine this is what a typical SDL game would end up |
| 135 | * doing */ |
| 136 | |
| 137 | for (i = 0; i < 10; i++) { |
| 138 | glClear(GL_COLOR_BUFFER_BIT); |
| 139 | eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface); |
| 140 | } |
| 141 | |
| 142 | buffer_count = get_n_egl_buffers(test_data.client); |
| 143 | |
| 144 | printf("buffers used = %i\n", buffer_count); |
| 145 | |
| 146 | /* The implementation should only end up creating two buffers |
| 147 | * and cycling between them */ |
| 148 | assert(buffer_count == 2); |
| 149 | } |