blob: 23cbc6ecaeb42cf1579d008fd61d79873a06fc39 [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>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000026#include <stdio.h>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000027#include <EGL/egl.h>
28#include <wayland-egl.h>
29#include <GLES2/gl2.h>
30
Bryce Harringtona7680262014-11-19 17:18:34 -080031#include "weston-test-client-helper.h"
Jonny Lamb51a7ae52015-03-20 15:26:51 +010032#include "../shared/platform.h"
Bryce Harringtona7680262014-11-19 17:18:34 -080033
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
Jonny Lamb51a7ae52015-03-20 15:26:51 +010070 test_data->egl_dpy =
71 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
72 test_data->client->wl_display,
73 NULL);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010074 if (!test_data->egl_dpy)
Jonny Lamb51a7ae52015-03-20 15:26:51 +010075 fail("eglGetPlatformDisplay or 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 =
Jonny Lamb4bdcb572015-03-20 15:26:53 +010098 weston_platform_create_egl_window(test_data->egl_dpy,
99 test_data->egl_conf,
100 native_window, NULL);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000101
102 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
103 test_data->egl_surface, test_data->egl_ctx);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100104 if (ret != EGL_TRUE)
105 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800106
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200107 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800108 * first release that doesn't accidentally triple-buffer. */
109 str = (const char *) glGetString(GL_VERSION);
110 mesa = strstr(str, "Mesa ");
111 if (mesa == NULL)
112 skip("unknown EGL implementation (%s)\n", str);
113 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
114 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200115 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800116 skip("mesa version too old (%s)\n", str);
117
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100118 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000119}
120
121TEST(test_buffer_count)
122{
123 struct test_data test_data;
124 uint32_t buffer_count;
125 int i;
126
127 test_data.client = client_create(10, 10, 10, 10);
Derek Foreman9bb13392015-01-23 12:12:36 -0600128 if (!test_data.client->has_wl_drm)
129 skip("compositor has not bound its display to EGL\n");
130
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100131 if (init_egl(&test_data) < 0)
132 skip("could not initialize egl, "
133 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000134
135 /* This is meant to represent a typical game loop which is
136 * expecting eglSwapBuffers to block and throttle the
137 * rendering to a sensible frame rate. Therefore it doesn't
138 * expect to have to install a frame callback itself. I'd
139 * imagine this is what a typical SDL game would end up
140 * doing */
141
142 for (i = 0; i < 10; i++) {
143 glClear(GL_COLOR_BUFFER_BIT);
144 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
145 }
146
147 buffer_count = get_n_egl_buffers(test_data.client);
148
149 printf("buffers used = %i\n", buffer_count);
150
151 /* The implementation should only end up creating two buffers
152 * and cycling between them */
153 assert(buffer_count == 2);
154}