blob: 8b1dd5d15dee9121869d690e49612b8b69507511 [file] [log] [blame]
Neil Roberts40c0c3f2013-10-29 20:13:45 +00001/*
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 Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Neil Roberts40c0c3f2013-10-29 20:13:45 +000025#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 Monfort08dbd312014-02-07 09:34:46 +010036#define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
37
Neil Roberts40c0c3f2013-10-29 20:13:45 +000038struct 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 Monfort08dbd312014-02-07 09:34:46 +010047static int
Neil Roberts40c0c3f2013-10-29 20:13:45 +000048init_egl(struct test_data *test_data)
49{
50 struct wl_egl_window *native_window;
51 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080052 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000053
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 Monfort08dbd312014-02-07 09:34:46 +010074 if (!test_data->egl_dpy)
75 fail("eglGetDisplay");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000076
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010077 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 Roberts40c0c3f2013-10-29 20:13:45 +000081
82 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
83 &test_data->egl_conf, 1, &n);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010084 if (!(ret && n == 1))
85 fail("eglChooseConfig");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000086
87 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
88 test_data->egl_conf,
89 EGL_NO_CONTEXT, context_attribs);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010090 if (!test_data->egl_ctx)
91 fail("eglCreateContext");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000092
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 Monfort08dbd312014-02-07 09:34:46 +0100105 if (ret != EGL_TRUE)
106 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800107
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200108 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800109 * 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 Paalanenca6bd742014-01-08 16:05:34 +0200116 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800117 skip("mesa version too old (%s)\n", str);
118
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100119 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000120}
121
122TEST(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 Monfort08dbd312014-02-07 09:34:46 +0100129 if (init_egl(&test_data) < 0)
130 skip("could not initialize egl, "
131 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000132
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}