blob: 065249de4fb09481aabc45212f14c25b211febdf [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>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000029#include <stdio.h>
Neil Roberts40c0c3f2013-10-29 20:13:45 +000030#include <EGL/egl.h>
31#include <wayland-egl.h>
32#include <GLES2/gl2.h>
33
Bryce Harringtona7680262014-11-19 17:18:34 -080034#include "weston-test-client-helper.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070035#include "shared/platform.h"
Bryce Harringtona7680262014-11-19 17:18:34 -080036
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010037#define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
38
Neil Roberts40c0c3f2013-10-29 20:13:45 +000039struct test_data {
40 struct client *client;
41
42 EGLDisplay egl_dpy;
43 EGLContext egl_ctx;
44 EGLConfig egl_conf;
45 EGLSurface egl_surface;
46};
47
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010048static int
Neil Roberts40c0c3f2013-10-29 20:13:45 +000049init_egl(struct test_data *test_data)
50{
51 struct wl_egl_window *native_window;
52 struct surface *surface = test_data->client->surface;
Kristian Høgsberg42284f52014-01-01 17:38:04 -080053 const char *str, *mesa;
Neil Roberts40c0c3f2013-10-29 20:13:45 +000054
55 static const EGLint context_attribs[] = {
56 EGL_CONTEXT_CLIENT_VERSION, 2,
57 EGL_NONE
58 };
59
60 EGLint config_attribs[] = {
61 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
62 EGL_RED_SIZE, 1,
63 EGL_GREEN_SIZE, 1,
64 EGL_BLUE_SIZE, 1,
65 EGL_ALPHA_SIZE, 0,
66 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
67 EGL_NONE
68 };
69
70 EGLint major, minor, n;
71 EGLBoolean ret;
72
Jonny Lamb51a7ae52015-03-20 15:26:51 +010073 test_data->egl_dpy =
74 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
75 test_data->client->wl_display,
76 NULL);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010077 if (!test_data->egl_dpy)
Jonny Lamb51a7ae52015-03-20 15:26:51 +010078 fail("eglGetPlatformDisplay or eglGetDisplay");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000079
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010080 if (eglInitialize(test_data->egl_dpy, &major, &minor) != EGL_TRUE)
81 fail("eglInitialize");
82 if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE)
83 fail("eglBindAPI");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000084
85 ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
86 &test_data->egl_conf, 1, &n);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010087 if (!(ret && n == 1))
88 fail("eglChooseConfig");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000089
90 test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
91 test_data->egl_conf,
92 EGL_NO_CONTEXT, context_attribs);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +010093 if (!test_data->egl_ctx)
94 fail("eglCreateContext");
Neil Roberts40c0c3f2013-10-29 20:13:45 +000095
96 native_window =
97 wl_egl_window_create(surface->wl_surface,
98 surface->width,
99 surface->height);
100 test_data->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100101 weston_platform_create_egl_surface(test_data->egl_dpy,
102 test_data->egl_conf,
103 native_window, NULL);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000104
105 ret = eglMakeCurrent(test_data->egl_dpy, test_data->egl_surface,
106 test_data->egl_surface, test_data->egl_ctx);
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100107 if (ret != EGL_TRUE)
108 fail("eglMakeCurrent");
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800109
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200110 /* This test is specific to mesa 10.1 and later, which is the
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800111 * first release that doesn't accidentally triple-buffer. */
112 str = (const char *) glGetString(GL_VERSION);
113 mesa = strstr(str, "Mesa ");
114 if (mesa == NULL)
115 skip("unknown EGL implementation (%s)\n", str);
116 if (sscanf(mesa + 5, "%d.%d", &major, &minor) != 2)
117 skip("unrecognized mesa version (%s)\n", str);
Pekka Paalanenca6bd742014-01-08 16:05:34 +0200118 if (major < 10 || (major == 10 && minor < 1))
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800119 skip("mesa version too old (%s)\n", str);
120
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100121 return 0;
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000122}
123
124TEST(test_buffer_count)
125{
126 struct test_data test_data;
127 uint32_t buffer_count;
128 int i;
129
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200130 test_data.client = create_client_and_test_surface(10, 10, 10, 10);
Derek Foreman9bb13392015-01-23 12:12:36 -0600131 if (!test_data.client->has_wl_drm)
132 skip("compositor has not bound its display to EGL\n");
133
Emilio Pozuelo Monfort08dbd312014-02-07 09:34:46 +0100134 if (init_egl(&test_data) < 0)
135 skip("could not initialize egl, "
136 "possibly using the headless backend\n");
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000137
138 /* This is meant to represent a typical game loop which is
139 * expecting eglSwapBuffers to block and throttle the
140 * rendering to a sensible frame rate. Therefore it doesn't
141 * expect to have to install a frame callback itself. I'd
142 * imagine this is what a typical SDL game would end up
143 * doing */
144
145 for (i = 0; i < 10; i++) {
146 glClear(GL_COLOR_BUFFER_BIT);
147 eglSwapBuffers(test_data.egl_dpy, test_data.egl_surface);
148 }
149
150 buffer_count = get_n_egl_buffers(test_data.client);
151
152 printf("buffers used = %i\n", buffer_count);
153
154 /* The implementation should only end up creating two buffers
155 * and cycling between them */
156 assert(buffer_count == 2);
157}