blob: c7ddeb64f6aa3d9b4f554b08c3704ced14d76bed [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
34struct test_data {
35 struct client *client;
36
37 EGLDisplay egl_dpy;
38 EGLContext egl_ctx;
39 EGLConfig egl_conf;
40 EGLSurface egl_surface;
41};
42
43static void
44init_egl(struct test_data *test_data)
45{
46 struct wl_egl_window *native_window;
47 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080048 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000049
50 static const EGLint context_attribs[] = {
51 EGL_CONTEXT_CLIENT_VERSION, 2,
52 EGL_NONE
53 };
54
55 EGLint config_attribs[] = {
56 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
57 EGL_RED_SIZE, 1,
58 EGL_GREEN_SIZE, 1,
59 EGL_BLUE_SIZE, 1,
60 EGL_ALPHA_SIZE, 0,
61 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
62 EGL_NONE
63 };
64
65 EGLint major, minor, n;
66 EGLBoolean ret;
67
68 test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)
69 test_data->client->wl_display);
70 assert(test_data->egl_dpy);
71
72 ret = eglInitialize(test_data->egl_dpy, &major, &minor);
73 assert(ret == EGL_TRUE);
74 ret = eglBindAPI(EGL_OPENGL_ES_API);
75 assert(ret == EGL_TRUE);
76
77 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
78 &test_data->egl_conf, 1, &n);
79 assert(ret && n == 1);
80
81 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
82 test_data->egl_conf,
83 EGL_NO_CONTEXT, context_attribs);
84 assert(test_data->egl_ctx);
85
86 native_window =
87 wl_egl_window_create(surface->wl_surface,
88 surface->width,
89 surface->height);
90 test_data->egl_surface =
91 eglCreateWindowSurface(test_data->egl_dpy,
92 test_data->egl_conf,
93 (EGLNativeWindowType) native_window,
94 NULL);
95
96 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
97 test_data->egl_surface, test_data->egl_ctx);
98 assert(ret == EGL_TRUE);
Kristian Høgsberg42284f52014-01-01 17:38:04 -080099
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200100 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800101 * first release that doesn't accidentally triple-buffer. */
102 str = (const char *) glGetString(GL_VERSION);
103 mesa = strstr(str, "Mesa ");
104 if (mesa == NULL)
105 skip("unknown EGL implementation (%s)\n", str);
106 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
107 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200108 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800109 skip("mesa version too old (%s)\n", str);
110
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000111}
112
113TEST(test_buffer_count)
114{
115 struct test_data test_data;
116 uint32_t buffer_count;
117 int i;
118
119 test_data.client = client_create(10, 10, 10, 10);
120 init_egl(&test_data);
121
122 /* This is meant to represent a typical game loop which is
123 * expecting eglSwapBuffers to block and throttle the
124 * rendering to a sensible frame rate. Therefore it doesn't
125 * expect to have to install a frame callback itself. I'd
126 * imagine this is what a typical SDL game would end up
127 * doing */
128
129 for (i = 0; i < 10; i++) {
130 glClear(GL_COLOR_BUFFER_BIT);
131 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
132 }
133
134 buffer_count = get_n_egl_buffers(test_data.client);
135
136 printf("buffers used = %i\n", buffer_count);
137
138 /* The implementation should only end up creating two buffers
139 * and cycling between them */
140 assert(buffer_count == 2);
141}