1 | #include <gtk/gtk.h> |
2 | |
3 | #define ALL_APIS (GDK_GL_API_GL | GDK_GL_API_GLES) |
4 | |
5 | static GdkGLAPI |
6 | is_unique (GdkGLAPI api) |
7 | { |
8 | return (api & (api - 1)) == 0; |
9 | } |
10 | |
11 | static void |
12 | test_allowed_backends (gconstpointer data) |
13 | { |
14 | GdkGLAPI allowed = GPOINTER_TO_SIZE (data); |
15 | GdkGLAPI not_allowed = (~allowed) & ALL_APIS; |
16 | GdkGLAPI api, random_apis; |
17 | GdkDisplay *display = gdk_display_get_default (); |
18 | GdkGLContext *context; |
19 | GError *error = NULL; |
20 | |
21 | display = gdk_display_get_default (); |
22 | if (!gdk_display_prepare_gl (self: display, error: &error)) |
23 | { |
24 | g_test_message (format: "no GL support: %s" , error->message); |
25 | g_test_skip (msg: "no GL support" ); |
26 | g_clear_error (err: &error); |
27 | return; |
28 | } |
29 | |
30 | context = gdk_display_create_gl_context (self: display, error: &error); |
31 | g_assert (context); |
32 | g_assert_no_error (error); |
33 | g_assert_cmpint (gdk_gl_context_get_api (context), ==, 0); |
34 | g_assert_cmpint (gdk_gl_context_get_allowed_apis (context), ==, ALL_APIS); |
35 | |
36 | gdk_gl_context_set_allowed_apis (self: context, apis: allowed); |
37 | g_assert_cmpint (gdk_gl_context_get_allowed_apis (context), ==, allowed); |
38 | g_assert_cmpint (gdk_gl_context_get_api (context), ==, 0); |
39 | |
40 | if (!gdk_gl_context_realize (context, error: &error)) |
41 | { |
42 | g_assert_cmpint (gdk_gl_context_get_api (context), ==, 0); |
43 | |
44 | if (not_allowed && g_error_matches (error, GDK_GL_ERROR, code: GDK_GL_ERROR_NOT_AVAILABLE)) |
45 | { |
46 | g_clear_error (err: &error); |
47 | g_object_unref (object: context); |
48 | return; |
49 | } |
50 | g_assert_no_error (error); |
51 | } |
52 | |
53 | g_assert_no_error (error); |
54 | |
55 | g_assert_cmpint (gdk_gl_context_get_allowed_apis (context), ==, allowed); |
56 | |
57 | api = gdk_gl_context_get_api (self: context); |
58 | g_assert_cmpint (api, !=, 0); |
59 | g_assert_true (is_unique (api)); |
60 | g_assert_cmpint (api & allowed, ==, api); |
61 | g_assert_cmpint (api & not_allowed, ==, 0); |
62 | |
63 | random_apis = g_random_int_range (begin: 0, ALL_APIS + 1); |
64 | gdk_gl_context_set_allowed_apis (self: context, apis: random_apis); |
65 | g_assert_cmpint (gdk_gl_context_get_allowed_apis (context), ==, random_apis); |
66 | g_assert_cmpint (gdk_gl_context_get_api (context), ==, api); |
67 | |
68 | g_object_unref (object: context); |
69 | } |
70 | |
71 | int |
72 | main (int argc, char *argv[]) |
73 | { |
74 | gtk_test_init (argcp: &argc, argvp: &argv, NULL); |
75 | |
76 | g_test_add_data_func (testpath: "/allowed-apis/none" , GSIZE_TO_POINTER (0), test_func: test_allowed_backends); |
77 | g_test_add_data_func (testpath: "/allowed-apis/gl" , GSIZE_TO_POINTER (GDK_GL_API_GL), test_func: test_allowed_backends); |
78 | g_test_add_data_func (testpath: "/allowed-apis/gles" , GSIZE_TO_POINTER (GDK_GL_API_GLES), test_func: test_allowed_backends); |
79 | g_test_add_data_func (testpath: "/allowed-apis/all" , GSIZE_TO_POINTER (GDK_GL_API_GL | GDK_GL_API_GLES), test_func: test_allowed_backends); |
80 | |
81 | return g_test_run (); |
82 | } |
83 | |