summaryrefslogtreecommitdiff
path: root/src/tests/evas/evas_test_evasgl.c
blob: af840b5da19a417aa3ad690d23541cd4b413a630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Test Evas GL EAPIs.
 *
 * This will try with opengl_x11 and buffer (OSMesa) and silently fail if
 * the engine or the GL library can't be initialized. This is to test Evas GL
 * APIs when they can actually work, ie. when OSMesa exists or when the engine
 * is GL.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <dlfcn.h>

#define EFL_GFX_FILTER_BETA
#include "evas_suite.h"
#include "Evas.h"
#include "Evas_GL.h"
#include "Ecore_Evas.h"

static int
_detect_osmesa(void)
{
   /* assume that if libOSMesa.so links, then we can create an Evas GL */
   void *lib = dlopen("libOSMesa.so", RTLD_NOW);
   if (!lib)
     {
        printf("Could not find OSMesa! Skipping Evas GL tests.\n");
        return 0;
     }
   dlclose(lib);
   return 1;
}

#define START_EVASGL_TEST(engine, options) \
   Ecore_Evas *ee; Evas *evas; Evas_Object *im = NULL; \
   if (!strcmp(engine, "buffer") && !_detect_osmesa()) return; \
   putenv("EVAS_GL_API_DEBUG=1"); \
   evas_init(); \
   ecore_evas_init(); \
   ee = ecore_evas_new(engine, 0, 0, 1, 1, options); \
   if (!ee) { printf("Could not create ecore evas. Skipping Evas GL tests.\n"); \
              goto init_failed; } \
   ecore_evas_show(ee); \
   ecore_evas_manual_render_set(ee, EINA_TRUE); \
   evas = ecore_evas_get(ee); \
   im = evas_object_image_filled_add(evas); \
   evas_object_geometry_set(im, 0, 0, 1, 1); \
   evas_object_show(im); \
   ecore_evas_manual_render(ee); \
   do {} while (0)

#define END_EVASGL_TEST() \
   init_failed: \
   evas_object_del(im); \
   ecore_evas_free(ee); \
   ecore_evas_shutdown(); \
   evas_shutdown(); \
   do {} while (0)

static void
_test_evasgl_init(const char *engine, const char *options)
{
   START_EVASGL_TEST(engine, options);
   Evas_GL_Context *ctx;
   Evas_GL_Surface *sfc;
   Evas_GL_Config *cfg;
   Evas_GL_API *gl;
   Evas_GL *evgl;

   fail_if(!(evgl = evas_gl_new(evas)));
   fail_if(!(cfg = evas_gl_config_new()));
   fail_if(!(sfc = evas_gl_surface_create(evgl, cfg, 1, 1)));
   fail_if(!(ctx = evas_gl_context_create(evgl, NULL)));

   /* valid current states */
   fail_if(!evas_gl_make_current(evgl, sfc, ctx));
   fail_if(!evas_gl_make_current(evgl, NULL, NULL));
   fail_if(!evas_gl_make_current(evgl, NULL, ctx));

   /* no context but surface: invalid */
   fprintf(stderr, "================ IGNORE ERRORS BEGIN ================\n");
   fail_if(evas_gl_make_current(evgl, sfc, NULL) != EINA_FALSE);
   fprintf(stderr, "================  IGNORE ERRORS END  ================\n");

   /* API verification */
   fail_if(!(gl = evas_gl_api_get(evgl)));

   fail_if(!evas_gl_make_current(evgl, NULL, NULL));
   evas_gl_context_destroy(evgl, ctx);
   evas_gl_surface_destroy(evgl, sfc);
   evas_gl_config_free(cfg);
   evas_gl_free(evgl);

   END_EVASGL_TEST();
}

static void
_test_evasgl_current_get(const char *engine, const char *options)
{
   if (!_detect_osmesa())
     return;

   START_EVASGL_TEST(engine, options);
   Evas_GL_Context *ctx, *ctx2;
   Evas_GL_Surface *sfc, *sfc2;
   Evas_GL_Config *cfg;
   Evas_GL *evgl, *evgl2;

   ctx2 = (void*)(intptr_t)0x1;
   sfc2 = (void*)(intptr_t)0x1;
   evgl2 = evas_gl_current_evas_gl_get(&ctx2, &sfc2);
   fail_if(evgl2 || ctx2 || sfc2);

   fail_if(!(evgl = evas_gl_new(evas)));
   fail_if(!(cfg = evas_gl_config_new()));
   fail_if(!(sfc = evas_gl_surface_create(evgl, cfg, 1, 1)));
   fail_if(!(ctx = evas_gl_context_create(evgl, NULL)));

   ctx2 = (void*)(intptr_t)0x1;
   sfc2 = (void*)(intptr_t)0x1;
   evgl2 = evas_gl_current_evas_gl_get(&ctx2, &sfc2);
   fail_if(evgl2 || ctx2 || sfc2);

   fail_if(!evas_gl_make_current(evgl, sfc, ctx));
   evgl2 = evas_gl_current_evas_gl_get(&ctx2, &sfc2);
   fail_if(evgl2 != evgl);
   fail_if(sfc2 != sfc);
   fail_if(ctx2 != ctx);

   fail_if(!evas_gl_make_current(evgl, NULL, NULL));
   evgl2 = evas_gl_current_evas_gl_get(&ctx2, &sfc2);
   fail_if(evgl2 != evgl); /* this should not reset current Evas GL */
   fail_if(sfc2 != NULL);
   fail_if(ctx2 != NULL);

   evas_gl_context_destroy(evgl, ctx);
   evas_gl_surface_destroy(evgl, sfc);
   evas_gl_free(evgl);
   END_EVASGL_TEST();
}

static void
_test_evasgl_context_version(const char *engine, const char *options)
{
   if (!_detect_osmesa())
     return;

   START_EVASGL_TEST(engine, options);
   Evas_GL_Context *ctx, *ctx2;
   Evas_GL_Surface *sfc;
   Evas_GL_Config *cfg;
   Evas_GL_API *gl;
   Evas_GL *evgl;
   const char *ver, *ext, *glsl;
   int vmaj, vmin;

   fail_if(!(evgl = evas_gl_new(evas)));
   fail_if(!(cfg = evas_gl_config_new()));
   fail_if(!(sfc = evas_gl_surface_create(evgl, cfg, 1, 1)));

   /* test only GLES 2 - GLES 3 and GLES 1 not supported yet with OSMesa */
   fail_if(!(ctx = evas_gl_context_version_create(evgl, NULL, EVAS_GL_GLES_2_X)));
   fail_if(!(gl = evas_gl_api_get(evgl)));
   fail_if(gl->version != EVAS_GL_API_VERSION); /* valid for make check */

   fail_if(!evas_gl_make_current(evgl, sfc, ctx));
   fail_if(!(ver = (const char *) gl->glGetString(GL_VERSION)));
   printf("GL_VERSION: %s\n", ver);
   fail_if(sscanf(ver, "OpenGL ES %d.%d", &vmaj, &vmin) != 2);
   fail_if((vmaj < 2) || (vmin < 0));

   /* verify function pointers - GLES 2 only */
   typedef void (*fptr_t)();
   const int idx_glViewport = ((char*) &gl->glViewport - (char*) &gl->glActiveTexture) / sizeof(fptr_t);
   fptr_t *fptr= &gl->glActiveTexture;
   for (int i = 0; i < idx_glViewport; i++, fptr++)
     fail_if(!*fptr);

   /* mesa supports a ton of extensions but there are no extra pointers set in
    * Evas_GL_API for those extensions */
   fail_if(!(ext = (const char *) gl->glGetString(GL_EXTENSIONS)));
   printf("GL_EXTENSIONS: %s\n", ext);

   /* GLSL string format check */
   fail_if(!(glsl = (const char *) gl->glGetString(GL_SHADING_LANGUAGE_VERSION)));
   printf("GL_SHADING_LANGUAGE_VERSION: %s\n", glsl);
   fail_if(sscanf(glsl, "OpenGL ES GLSL ES %d", &vmaj) != 1);

   /* shared ctx */
   fail_if(!(ctx2 = evas_gl_context_version_create(evgl, ctx, EVAS_GL_GLES_2_X)));
   evas_gl_context_destroy(evgl, ctx2);

   fail_if(!evas_gl_make_current(evgl, NULL, NULL));
   evas_gl_context_destroy(evgl, ctx);
   evas_gl_surface_destroy(evgl, sfc);
   evas_gl_free(evgl);
   END_EVASGL_TEST();
}

START_TEST(evas_test_evasgl_init_opengl_x11)
{
   _test_evasgl_init("opengl_x11", NULL);
}
END_TEST

START_TEST(evas_test_evasgl_current_get_opengl_x11)
{
   _test_evasgl_current_get("opengl_x11", NULL);
}
END_TEST

START_TEST(evas_test_evasgl_context_version_opengl_x11)
{
   _test_evasgl_context_version("opengl_x11", NULL);
}
END_TEST

START_TEST(evas_test_evasgl_init_buffer)
{
   _test_evasgl_init("buffer", NULL);
}
END_TEST

START_TEST(evas_test_evasgl_current_get_buffer)
{
   _test_evasgl_current_get("buffer", NULL);
}
END_TEST

START_TEST(evas_test_evasgl_context_version_buffer)
{
   _test_evasgl_context_version("buffer", NULL);
}
END_TEST

void evas_test_evasgl(TCase *tc)
{
   if (getenv("DISPLAY"))
     {
        tcase_add_test(tc, evas_test_evasgl_init_opengl_x11);
        tcase_add_test(tc, evas_test_evasgl_current_get_opengl_x11);
        tcase_add_test(tc, evas_test_evasgl_context_version_opengl_x11);
     }

   tcase_add_test(tc, evas_test_evasgl_init_buffer);
   tcase_add_test(tc, evas_test_evasgl_current_get_buffer);
   tcase_add_test(tc, evas_test_evasgl_context_version_buffer);
}