blob: 815e1cc86703f3f3edcac7de85d761a78e8023ce [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 =
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);
Derek Foreman9bb13392015-01-23 12:12:36 -0600129 if (!test_data.client->has_wl_drm)
130 skip("compositor has not bound its display to EGL\n");
131
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100132 if (init_egl(&test_data) < 0)
133 skip("could not initialize egl, "
134 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000135
136 /* This is meant to represent a typical game loop which is
137 * expecting eglSwapBuffers to block and throttle the
138 * rendering to a sensible frame rate. Therefore it doesn't
139 * expect to have to install a frame callback itself. I'd
140 * imagine this is what a typical SDL game would end up
141 * doing */
142
143 for (i = 0; i < 10; i++) {
144 glClear(GL_COLOR_BUFFER_BIT);
145 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
146 }
147
148 buffer_count = get_n_egl_buffers(test_data.client);
149
150 printf("buffers used = %i\n", buffer_count);
151
152 /* The implementation should only end up creating two buffers
153 * and cycling between them */
154 assert(buffer_count == 2);
155}