blob: 43fb089ea97269dd11f8cc767c0fbabaf40e4b5a [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"
32
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010033#define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
34
Neil Roberts40c0c3f2013-10-29 20:13:45 +000035struct test_data {
36 struct client *client;
37
38 EGLDisplay egl_dpy;
39 EGLContext egl_ctx;
40 EGLConfig egl_conf;
41 EGLSurface egl_surface;
42};
43
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010044static int
Neil Roberts40c0c3f2013-10-29 20:13:45 +000045init_egl(struct test_data *test_data)
46{
47 struct wl_egl_window *native_window;
48 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080049 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000050
51 static const EGLint context_attribs[] = {
52 EGL_CONTEXT_CLIENT_VERSION, 2,
53 EGL_NONE
54 };
55
56 EGLint config_attribs[] = {
57 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
58 EGL_RED_SIZE, 1,
59 EGL_GREEN_SIZE, 1,
60 EGL_BLUE_SIZE, 1,
61 EGL_ALPHA_SIZE, 0,
62 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
63 EGL_NONE
64 };
65
66 EGLint major, minor, n;
67 EGLBoolean ret;
68
69 test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)
70 test_data->client->wl_display);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010071 if (!test_data->egl_dpy)
72 fail("eglGetDisplay");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000073
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010074 if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE)
75 fail("eglInitialize");
76 if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
77 fail("eglBindAPI");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000078
79 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
80 &test_data->egl_conf, 1, &n);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010081 if (!(ret && n == 1))
82 fail("eglChooseConfig");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000083
84 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
85 test_data->egl_conf,
86 EGL_NO_CONTEXT, context_attribs);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010087 if (!test_data->egl_ctx)
88 fail("eglCreateContext");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000089
90 native_window =
91 wl_egl_window_create(surface->wl_surface,
92 surface->width,
93 surface->height);
94 test_data->egl_surface =
95 eglCreateWindowSurface(test_data->egl_dpy,
96 test_data->egl_conf,
97 (EGLNativeWindowType) native_window,
98 NULL);
99
100 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
101 test_data->egl_surface, test_data->egl_ctx);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100102 if (ret != EGL_TRUE)
103 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800104
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200105 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800106 * first release that doesn't accidentally triple-buffer. */
107 str = (const char *) glGetString(GL_VERSION);
108 mesa = strstr(str, "Mesa ");
109 if (mesa == NULL)
110 skip("unknown EGL implementation (%s)\n", str);
111 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
112 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200113 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800114 skip("mesa version too old (%s)\n", str);
115
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100116 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000117}
118
119TEST(test_buffer_count)
120{
121 struct test_data test_data;
122 uint32_t buffer_count;
123 int i;
124
125 test_data.client = client_create(10, 10, 10, 10);
Derek Foreman9bb13392015-01-23 12:12:36 -0600126 if (!test_data.client->has_wl_drm)
127 skip("compositor has not bound its display to EGL\n");
128
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}