2 * Copyright © 2013 Intel Corporation
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.
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.
27 #include "weston-test-client-helper.h"
33 #include <wayland-egl.h>
34 #include <GLES2/gl2.h>
36 #define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
39 struct client *client;
44 EGLSurface egl_surface;
48 init_egl(struct test_data *test_data)
50 struct wl_egl_window *native_window;
51 struct surface *surface = test_data->client->surface;
52 const char *str, *mesa;
54 static const EGLint context_attribs[] = {
55 EGL_CONTEXT_CLIENT_VERSION, 2,
59 EGLint config_attribs[] = {
60 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
65 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
69 EGLint major, minor, n;
72 test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)
73 test_data->client->wl_display);
74 if (!test_data->egl_dpy)
75 fail("eglGetDisplay");
77 if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE)
78 fail("eglInitialize");
79 if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
82 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
83 &test_data->egl_conf, 1, &n);
85 fail("eglChooseConfig");
87 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
89 EGL_NO_CONTEXT, context_attribs);
90 if (!test_data->egl_ctx)
91 fail("eglCreateContext");
94 wl_egl_window_create(surface->wl_surface,
97 test_data->egl_surface =
98 eglCreateWindowSurface(test_data->egl_dpy,
100 (EGLNativeWindowType) native_window,
103 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
104 test_data->egl_surface, test_data->egl_ctx);
106 fail("eglMakeCurrent");
108 /* This test is specific to mesa 10.1 and later, which is the
109 * first release that doesn't accidentally triple-buffer. */
110 str = (const char *) glGetString(GL_VERSION);
111 mesa = strstr(str, "Mesa ");
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);
116 if (major < 10 || (major == 10 && minor < 1))
117 skip("mesa version too old (%s)\n", str);
122 TEST(test_buffer_count)
124 struct test_data test_data;
125 uint32_t buffer_count;
128 test_data.client = client_create(10, 10, 10, 10);
129 if (init_egl(&test_data) < 0)
130 skip("could not initialize egl, "
131 "possibly using the headless backend\n");
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
140 for (i = 0; i < 10; i++) {
141 glClear(GL_COLOR_BUFFER_BIT);
142 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
145 buffer_count = get_n_egl_buffers(test_data.client);
147 printf("buffers used = %i\n", buffer_count);
149 /* The implementation should only end up creating two buffers
150 * and cycling between them */
151 assert(buffer_count == 2);