blob: e7d8ca7de635fe1b47c61d9ba9b19fa0ae91c988 [file] [log] [blame]
Neil Roberts40c0c3f2013-10-29 20:13:45 +00001/*
2 * Copyright © 2013 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Neil Roberts40c0c3f2013-10-29 20:13:45 +000011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Neil Roberts40c0c3f2013-10-29 20:13:45 +000024 */
25
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010026#include "config.h"
27
Neil Roberts40c0c3f2013-10-29 20:13:45 +000028#include <string.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000030#include <stdio.h>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000031#include <EGL/egl.h>
32#include <wayland-egl.h>
33#include <GLES2/gl2.h>
34
Bryce Harringtona7680262014-11-19 17:18:34 -080035#include "weston-test-client-helper.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070036#include "shared/platform.h"
Bryce Harringtona7680262014-11-19 17:18:34 -080037
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010038#define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
39
Neil Roberts40c0c3f2013-10-29 20:13:45 +000040struct test_data {
41 struct client *client;
42
43 EGLDisplay egl_dpy;
44 EGLContext egl_ctx;
45 EGLConfig egl_conf;
46 EGLSurface egl_surface;
47};
48
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010049static int
Neil Roberts40c0c3f2013-10-29 20:13:45 +000050init_egl(struct test_data *test_data)
51{
52 struct wl_egl_window *native_window;
53 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080054 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000055
56 static const EGLint context_attribs[] = {
57 EGL_CONTEXT_CLIENT_VERSION, 2,
58 EGL_NONE
59 };
60
61 EGLint config_attribs[] = {
62 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
63 EGL_RED_SIZE, 1,
64 EGL_GREEN_SIZE, 1,
65 EGL_BLUE_SIZE, 1,
66 EGL_ALPHA_SIZE, 0,
67 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
68 EGL_NONE
69 };
70
71 EGLint major, minor, n;
72 EGLBoolean ret;
73
Jonny Lamb51a7ae52015-03-20 15:26:51 +010074 test_data->egl_dpy =
75 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
76 test_data->client->wl_display,
77 NULL);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010078 if (!test_data->egl_dpy)
Jonny Lamb51a7ae52015-03-20 15:26:51 +010079 fail("eglGetPlatformDisplay or eglGetDisplay");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000080
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010081 if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE)
82 fail("eglInitialize");
83 if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
84 fail("eglBindAPI");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000085
86 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
87 &test_data->egl_conf, 1, &n);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010088 if (!(ret && n == 1))
89 fail("eglChooseConfig");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000090
91 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
92 test_data->egl_conf,
93 EGL_NO_CONTEXT, context_attribs);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010094 if (!test_data->egl_ctx)
95 fail("eglCreateContext");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000096
97 native_window =
98 wl_egl_window_create(surface->wl_surface,
99 surface->width,
100 surface->height);
101 test_data->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100102 weston_platform_create_egl_surface(test_data->egl_dpy,
103 test_data->egl_conf,
104 native_window, NULL);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000105
106 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
107 test_data->egl_surface, test_data->egl_ctx);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100108 if (ret != EGL_TRUE)
109 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800110
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200111 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800112 * first release that doesn't accidentally triple-buffer. */
113 str = (const char *) glGetString(GL_VERSION);
114 mesa = strstr(str, "Mesa ");
115 if (mesa == NULL)
116 skip("unknown EGL implementation (%s)\n", str);
117 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
118 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200119 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800120 skip("mesa version too old (%s)\n", str);
121
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100122 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000123}
124
125TEST(test_buffer_count)
126{
127 struct test_data test_data;
128 uint32_t buffer_count;
129 int i;
130
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200131 test_data.client = create_client_and_test_surface(10, 10, 10, 10);
Derek Foreman9bb13392015-01-23 12:12:36 -0600132 if (!test_data.client->has_wl_drm)
133 skip("compositor has not bound its display to EGL\n");
134
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100135 if (init_egl(&test_data) < 0)
136 skip("could not initialize egl, "
137 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000138
139 /* This is meant to represent a typical game loop which is
140 * expecting eglSwapBuffers to block and throttle the
141 * rendering to a sensible frame rate. Therefore it doesn't
142 * expect to have to install a frame callback itself. I'd
143 * imagine this is what a typical SDL game would end up
144 * doing */
145
146 for (i = 0; i < 10; i++) {
147 glClear(GL_COLOR_BUFFER_BIT);
148 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
149 }
150
151 buffer_count = get_n_egl_buffers(test_data.client);
152
153 printf("buffers used = %i\n", buffer_count);
154
155 /* The implementation should only end up creating two buffers
156 * and cycling between them */
157 assert(buffer_count == 2);
158}