downstream: ivi-shell: configure the ivi surface when created
[profile/ivi/weston-ivi-shell.git] / tests / buffer-count-test.c
1 /*
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 "config.h"
24
25 #include <string.h>
26
27 #include "weston-test-client-helper.h"
28 #include <stdio.h>
29 #include <poll.h>
30 #include <time.h>
31
32 #include <EGL/egl.h>
33 #include <wayland-egl.h>
34 #include <GLES2/gl2.h>
35
36 #define fail(msg) { fprintf(stderr, "%s failed\n", msg); return -1; }
37
38 struct test_data {
39         struct client *client;
40
41         EGLDisplay egl_dpy;
42         EGLContext egl_ctx;
43         EGLConfig egl_conf;
44         EGLSurface egl_surface;
45 };
46
47 static int
48 init_egl(struct test_data *test_data)
49 {
50         struct wl_egl_window *native_window;
51         struct surface *surface = test_data->client->surface;
52         const char *str, *mesa;
53
54         static const EGLint context_attribs[] = {
55                 EGL_CONTEXT_CLIENT_VERSION, 2,
56                 EGL_NONE
57         };
58
59         EGLint config_attribs[] = {
60                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
61                 EGL_RED_SIZE, 1,
62                 EGL_GREEN_SIZE, 1,
63                 EGL_BLUE_SIZE, 1,
64                 EGL_ALPHA_SIZE, 0,
65                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
66                 EGL_NONE
67         };
68
69         EGLint major, minor, n;
70         EGLBoolean ret;
71
72         test_data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)
73                                            test_data->client->wl_display);
74         if (!test_data->egl_dpy)
75                 fail("eglGetDisplay");
76
77         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");
81
82         ret = eglChooseConfig(test_data->egl_dpy, config_attribs,
83                               &test_data->egl_conf, 1, &n);
84         if (!(ret && n == 1))
85                 fail("eglChooseConfig");
86
87         test_data->egl_ctx = eglCreateContext(test_data->egl_dpy,
88                                               test_data->egl_conf,
89                                               EGL_NO_CONTEXT, context_attribs);
90         if (!test_data->egl_ctx)
91                 fail("eglCreateContext");
92
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);
105         if (ret != EGL_TRUE)
106                 fail("eglMakeCurrent");
107
108         /* This test is specific to mesa 10.1 and later, which is the
109          * 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);
116         if (major < 10 || (major == 10 && minor < 1))
117                 skip("mesa version too old (%s)\n", str);
118
119         return 0;
120 }
121
122 TEST(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);
129         if (init_egl(&test_data) < 0)
130                 skip("could not initialize egl, "
131                      "possibly using the headless backend\n");
132
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 }