blob: dfc4673add123621123eebe33006228207200ab8 [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
23#include <string.h>
24
25#include "weston-test-client-helper.h"
26#include <stdio.h>
27#include <poll.h>
28#include <time.h>
29
30#include <EGL/egl.h>
31#include <wayland-egl.h>
32#include <GLES2/gl2.h>
33
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010034#define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
35
Neil Roberts40c0c3f2013-10-29 20:13:45 +000036struct test_data {
37 struct client *client;
38
39 EGLDisplay egl_dpy;
40 EGLContext egl_ctx;
41 EGLConfig egl_conf;
42 EGLSurface egl_surface;
43};
44
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010045static int
Neil Roberts40c0c3f2013-10-29 20:13:45 +000046init_egl(struct test_data *test_data)
47{
48 struct wl_egl_window *native_window;
49 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080050 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000051
52 static const EGLint context_attribs[] = {
53 EGL_CONTEXT_CLIENT_VERSION, 2,
54 EGL_NONE
55 };
56
57 EGLint config_attribs[] = {
58 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
59 EGL_RED_SIZE, 1,
60 EGL_GREEN_SIZE, 1,
61 EGL_BLUE_SIZE, 1,
62 EGL_ALPHA_SIZE, 0,
63 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
64 EGL_NONE
65 };
66
67 EGLint major, minor, n;
68 EGLBoolean ret;
69
70 test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)
71 test_data->client->wl_display);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010072 if (!test_data->egl_dpy)
73 fail("eglGetDisplay");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000074
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010075 if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE)
76 fail("eglInitialize");
77 if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
78 fail("eglBindAPI");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000079
80 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
81 &test_data->egl_conf, 1, &n);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010082 if (!(ret && n == 1))
83 fail("eglChooseConfig");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000084
85 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
86 test_data->egl_conf,
87 EGL_NO_CONTEXT, context_attribs);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010088 if (!test_data->egl_ctx)
89 fail("eglCreateContext");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000090
91 native_window =
92 wl_egl_window_create(surface->wl_surface,
93 surface->width,
94 surface->height);
95 test_data->egl_surface =
96 eglCreateWindowSurface(test_data->egl_dpy,
97 test_data->egl_conf,
98 (EGLNativeWindowType) native_window,
99 NULL);
100
101 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
102 test_data->egl_surface, test_data->egl_ctx);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100103 if (ret != EGL_TRUE)
104 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800105
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200106 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800107 * first release that doesn't accidentally triple-buffer. */
108 str = (const char *) glGetString(GL_VERSION);
109 mesa = strstr(str, "Mesa ");
110 if (mesa == NULL)
111 skip("unknown EGL implementation (%s)\n", str);
112 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
113 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200114 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800115 skip("mesa version too old (%s)\n", str);
116
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100117 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000118}
119
120TEST(test_buffer_count)
121{
122 struct test_data test_data;
123 uint32_t buffer_count;
124 int i;
125
126 test_data.client = client_create(10, 10, 10, 10);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100127 if (init_egl(&test_data) < 0)
128 skip("could not initialize egl, "
129 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000130
131 /* This is meant to represent a typical game loop which is
132 * expecting eglSwapBuffers to block and throttle the
133 * rendering to a sensible frame rate. Therefore it doesn't
134 * expect to have to install a frame callback itself. I'd
135 * imagine this is what a typical SDL game would end up
136 * doing */
137
138 for (i = 0; i < 10; i++) {
139 glClear(GL_COLOR_BUFFER_BIT);
140 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
141 }
142
143 buffer_count = get_n_egl_buffers(test_data.client);
144
145 printf("buffers used = %i\n", buffer_count);
146
147 /* The implementation should only end up creating two buffers
148 * and cycling between them */
149 assert(buffer_count == 2);
150}