2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 #include <gtest/gtest.h>
27 #include "glxclient.h"
32 #include "mock_xdisplay.h"
33 #include "fake_glx_screen.h"
36 * \name Wrappers around some X structures to make the more usable for tests
39 class fake_glx_screen;
41 class fake_glx_display : public glx_display {
43 fake_glx_display(mock_XDisplay *dpy, int major, int minor)
47 this->majorOpcode = 0;
48 this->majorVersion = major;
49 this->minorVersion = minor;
50 this->serverGLXvendor = 0;
51 this->serverGLXversion = 0;
52 this->glXDrawHash = 0;
54 this->screens = new glx_screen *[dpy->nscreens];
55 memset(this->screens, 0, sizeof(struct glx_screen *) * dpy->nscreens);
60 for (int i = 0; i < this->dpy->nscreens; i++) {
61 if (this->screens[i] != NULL)
62 delete this->screens[i];
65 delete [] this->screens;
68 void init_screen(int i, const char *ext);
71 class glX_send_client_info_test : public ::testing::Test {
73 glX_send_client_info_test();
74 virtual ~glX_send_client_info_test();
77 void common_protocol_expected_false_test(unsigned major, unsigned minor,
78 const char *glx_ext, bool *value);
80 void common_protocol_expected_true_test(unsigned major, unsigned minor,
81 const char *glx_ext, bool *value);
83 void create_single_screen_display(unsigned major, unsigned minor,
86 void destroy_display();
89 fake_glx_display *glx_dpy;
90 mock_XDisplay *display;
94 fake_glx_display::init_screen(int i, const char *ext)
96 if (this->screens[i] != NULL)
97 delete this->screens[i];
99 this->screens[i] = new fake_glx_screen(this, i, ext);
103 static const char ext[] = "GL_XXX_dummy";
105 static bool ClientInfo_was_sent;
106 static bool SetClientInfoARB_was_sent;
107 static bool SetClientInfo2ARB_was_sent;
108 static xcb_connection_t *connection_used;
109 static int gl_ext_length;
110 static char *gl_ext_string;
111 static int glx_ext_length;
112 static char *glx_ext_string;
113 static int num_gl_versions;
114 static uint32_t *gl_versions;
115 static int glx_major;
116 static int glx_minor;
118 extern "C" xcb_connection_t *
119 XGetXCBConnection(Display *dpy)
121 return (xcb_connection_t *) 0xdeadbeef;
124 extern "C" xcb_void_cookie_t
125 xcb_glx_client_info(xcb_connection_t *c,
126 uint32_t major_version,
127 uint32_t minor_version,
131 xcb_void_cookie_t cookie;
133 ClientInfo_was_sent = true;
136 gl_ext_string = (char *) malloc(str_len);
137 memcpy(gl_ext_string, string, str_len);
138 gl_ext_length = str_len;
140 glx_major = major_version;
141 glx_minor = minor_version;
147 extern "C" xcb_void_cookie_t
148 xcb_glx_set_client_info_arb(xcb_connection_t *c,
149 uint32_t major_version,
150 uint32_t minor_version,
151 uint32_t num_versions,
153 uint32_t glx_str_len,
154 const uint32_t *versions,
155 const char *gl_string,
156 const char *glx_string)
158 xcb_void_cookie_t cookie;
160 SetClientInfoARB_was_sent = true;
163 gl_ext_string = new char[gl_str_len];
164 memcpy(gl_ext_string, gl_string, gl_str_len);
165 gl_ext_length = gl_str_len;
167 glx_ext_string = new char[glx_str_len];
168 memcpy(glx_ext_string, glx_string, glx_str_len);
169 glx_ext_length = glx_str_len;
171 gl_versions = new uint32_t[num_versions * 2];
172 memcpy(gl_versions, versions, sizeof(uint32_t) * num_versions * 2);
173 num_gl_versions = num_versions;
175 glx_major = major_version;
176 glx_minor = minor_version;
182 extern "C" xcb_void_cookie_t
183 xcb_glx_set_client_info_2arb(xcb_connection_t *c,
184 uint32_t major_version,
185 uint32_t minor_version,
186 uint32_t num_versions,
188 uint32_t glx_str_len,
189 const uint32_t *versions,
190 const char *gl_string,
191 const char *glx_string)
193 xcb_void_cookie_t cookie;
195 SetClientInfo2ARB_was_sent = true;
198 gl_ext_string = new char[gl_str_len];
199 memcpy(gl_ext_string, gl_string, gl_str_len);
200 gl_ext_length = gl_str_len;
202 glx_ext_string = new char[glx_str_len];
203 memcpy(glx_ext_string, glx_string, glx_str_len);
204 glx_ext_length = glx_str_len;
206 gl_versions = new uint32_t[num_versions * 3];
207 memcpy(gl_versions, versions, sizeof(uint32_t) * num_versions * 3);
208 num_gl_versions = num_versions;
210 glx_major = major_version;
211 glx_minor = minor_version;
218 __glXGetClientGLExtensionString()
220 char *str = (char *) malloc(sizeof(ext));
222 memcpy(str, ext, sizeof(ext));
226 glX_send_client_info_test::glX_send_client_info_test()
227 : glx_dpy(0), display(0)
232 glX_send_client_info_test::~glX_send_client_info_test()
242 glX_send_client_info_test::destroy_display()
244 if (this->glx_dpy != NULL) {
245 if (this->glx_dpy->screens != NULL) {
246 for (int i = 0; i < this->display->nscreens; i++) {
247 delete [] this->glx_dpy->screens[i]->serverGLXexts;
248 delete this->glx_dpy->screens[i];
251 delete [] this->glx_dpy->screens;
254 delete this->glx_dpy;
255 delete this->display;
260 glX_send_client_info_test::SetUp()
262 ClientInfo_was_sent = false;
263 SetClientInfoARB_was_sent = false;
264 SetClientInfo2ARB_was_sent = false;
265 connection_used = (xcb_connection_t *) ~0;
267 gl_ext_string = (char *) 0;
269 glx_ext_string = (char *) 0;
271 gl_versions = (uint32_t *) 0;
277 glX_send_client_info_test::create_single_screen_display(unsigned major,
281 this->display = new mock_XDisplay(1);
283 this->glx_dpy = new fake_glx_display(this->display, major, minor);
284 this->glx_dpy->init_screen(0, glx_ext);
288 glX_send_client_info_test::common_protocol_expected_false_test(unsigned major,
293 create_single_screen_display(major, minor, glx_ext);
294 __glX_send_client_info(this->glx_dpy);
295 EXPECT_FALSE(*value);
299 glX_send_client_info_test::common_protocol_expected_true_test(unsigned major,
304 create_single_screen_display(major, minor, glx_ext);
305 __glX_send_client_info(this->glx_dpy);
309 TEST_F(glX_send_client_info_test, doesnt_send_ClientInfo_for_1_0)
311 /* The glXClientInfo protocol was added in GLX 1.1. Verify that no
312 * glXClientInfo is sent to a GLX server that only has GLX 1.0.
314 common_protocol_expected_false_test(1, 0, "", &ClientInfo_was_sent);
317 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_0)
319 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
320 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
321 * sent to a GLX server that only has GLX 1.0 regardless of the extension
324 common_protocol_expected_false_test(1, 0,
325 "GLX_ARB_create_context",
326 &SetClientInfoARB_was_sent);
329 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_1)
331 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
332 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
333 * sent to a GLX server that only has GLX 1.0 regardless of the extension
336 common_protocol_expected_false_test(1, 1,
337 "GLX_ARB_create_context",
338 &SetClientInfoARB_was_sent);
341 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_4_with_empty_extensions)
343 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
344 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
345 * sent to a GLX server that has GLX 1.4 but has an empty extension string
346 * (i.e., no extensions at all).
348 common_protocol_expected_false_test(1, 4,
350 &SetClientInfoARB_was_sent);
353 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_4_without_extension)
355 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
356 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
357 * sent to a GLX server that has GLX 1.4 but doesn't have the extension.
359 common_protocol_expected_false_test(1, 4,
360 "GLX_EXT_texture_from_pixmap",
361 &SetClientInfoARB_was_sent);
364 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_4_with_wrong_extension)
366 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
367 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
368 * sent to a GLX server that has GLX 1.4 but does not have the extension.
370 * This test differs from
371 * doesnt_send_SetClientInfoARB_for_1_4_without_extension in that an
372 * extension exists that looks like the correct extension but isn't.
374 common_protocol_expected_false_test(1, 4,
375 "GLX_ARB_create_context2",
376 &SetClientInfoARB_was_sent);
379 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfoARB_for_1_4_with_profile_extension)
381 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
382 * GLX_ARB_create_context extension. Verify that no glXSetClientInfoARB is
383 * sent to a GLX server that has GLX 1.4 but does not have the extension.
385 * This test differs from
386 * doesnt_send_SetClientInfoARB_for_1_4_without_extension in that an
387 * extension exists that looks like the correct extension but isn't.
389 common_protocol_expected_false_test(1, 4,
390 "GLX_ARB_create_context_profile",
391 &SetClientInfoARB_was_sent);
394 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfo2ARB_for_1_0)
396 /* The glXSetClientInfo2ARB protocol was added in GLX 1.4 with the
397 * GLX_ARB_create_context_profile extension. Verify that no
398 * glXSetClientInfo2ARB is sent to a GLX server that only has GLX 1.0
399 * regardless of the extension setting.
401 common_protocol_expected_false_test(1, 0,
402 "GLX_ARB_create_context_profile",
403 &SetClientInfo2ARB_was_sent);
406 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfo2ARB_for_1_1)
408 /* The glXSetClientInfo2ARB protocol was added in GLX 1.4 with the
409 * GLX_ARB_create_context_profile extension. Verify that no
410 * glXSetClientInfo2ARB is sent to a GLX server that only has GLX 1.1
411 * regardless of the extension setting.
413 common_protocol_expected_false_test(1, 1,
414 "GLX_ARB_create_context_profile",
415 &SetClientInfo2ARB_was_sent);
418 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfo2ARB_for_1_4_with_empty_extensions)
420 /* The glXSetClientInfo2ARB protocol was added in GLX 1.4 with the
421 * GLX_ARB_create_context_profile extension. Verify that no
422 * glXSetClientInfo2ARB is sent to a GLX server that has GLX 1.4 but has an
423 * empty extension string (i.e., no extensions at all).
425 common_protocol_expected_false_test(1, 4,
427 &SetClientInfo2ARB_was_sent);
430 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfo2ARB_for_1_4_without_extension)
432 /* The glXSetClientInfo2ARB protocol was added in GLX 1.4 with the
433 * GLX_ARB_create_context_profile extension. Verify that no
434 * glXSetClientInfo2ARB is sent to a GLX server that has GLX 1.4 but
435 * doesn't have the extension.
437 common_protocol_expected_false_test(1, 4,
438 "GLX_EXT_texture_from_pixmap",
439 &SetClientInfo2ARB_was_sent);
442 TEST_F(glX_send_client_info_test, doesnt_send_SetClientInfo2ARB_for_1_4_with_wrong_extension)
444 /* The glXSetClientInfo2ARB protocol was added in GLX 1.4 with the
445 * GLX_ARB_create_context_profile extension. Verify that no
446 * glXSetClientInfo2ARB is sent to a GLX server that has GLX 1.4 but does
447 * not have the extension.
449 * This test differs from
450 * doesnt_send_SetClientInfo2ARB_for_1_4_without_extension in that an
451 * extension exists that looks like the correct extension but isn't.
453 common_protocol_expected_false_test(1, 4,
454 "GLX_ARB_create_context_profile2",
455 &SetClientInfo2ARB_was_sent);
458 TEST_F(glX_send_client_info_test, does_send_ClientInfo_for_1_1)
460 /* The glXClientInfo protocol was added in GLX 1.1. Verify that
461 * glXClientInfo is sent to a GLX server that has GLX 1.1.
463 common_protocol_expected_true_test(1, 1,
465 &ClientInfo_was_sent);
468 TEST_F(glX_send_client_info_test, does_send_SetClientInfoARB_for_1_4_with_extension)
470 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
471 * GLX_ARB_create_context extension. Verify that glXSetClientInfoARB is
472 * sent to a GLX server that has GLX 1.4 and the extension.
474 common_protocol_expected_true_test(1, 4,
475 "GLX_ARB_create_context",
476 &SetClientInfoARB_was_sent);
479 TEST_F(glX_send_client_info_test, does_send_SetClientInfo2ARB_for_1_4_with_just_profile_extension)
481 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
482 * GLX_ARB_create_context extension. Verify that glXSetClientInfoARB is
483 * sent to a GLX server that has GLX 1.4 and the extension.
485 common_protocol_expected_true_test(1, 4,
486 "GLX_ARB_create_context_profile",
487 &SetClientInfo2ARB_was_sent);
490 TEST_F(glX_send_client_info_test, does_send_SetClientInfo2ARB_for_1_4_with_both_extensions)
492 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
493 * GLX_ARB_create_context extension. Verify that glXSetClientInfoARB is
494 * sent to a GLX server that has GLX 1.4 and the extension.
496 common_protocol_expected_true_test(1, 4,
497 "GLX_ARB_create_context "
498 "GLX_ARB_create_context_profile",
499 &SetClientInfo2ARB_was_sent);
502 TEST_F(glX_send_client_info_test, does_send_SetClientInfo2ARB_for_1_4_with_both_extensions_reversed)
504 /* The glXSetClientInfoARB protocol was added in GLX 1.4 with the
505 * GLX_ARB_create_context extension. Verify that glXSetClientInfoARB is
506 * sent to a GLX server that has GLX 1.4 and the extension.
508 common_protocol_expected_true_test(1, 4,
509 "GLX_ARB_create_context_profile "
510 "GLX_ARB_create_context",
511 &SetClientInfo2ARB_was_sent);
514 TEST_F(glX_send_client_info_test, uses_correct_connection)
516 create_single_screen_display(1, 1, "");
517 __glX_send_client_info(this->glx_dpy);
518 EXPECT_EQ((xcb_connection_t *) 0xdeadbeef, connection_used);
521 TEST_F(glX_send_client_info_test, sends_correct_gl_extension_string)
523 create_single_screen_display(1, 1, "");
524 __glX_send_client_info(this->glx_dpy);
526 ASSERT_EQ(sizeof(ext), gl_ext_length);
527 ASSERT_NE((char *) 0, gl_ext_string);
528 EXPECT_EQ(0, memcmp(gl_ext_string, ext, sizeof(ext)));
531 TEST_F(glX_send_client_info_test, gl_versions_are_sane)
533 create_single_screen_display(1, 4, "GLX_ARB_create_context");
534 __glX_send_client_info(this->glx_dpy);
536 ASSERT_NE(0, num_gl_versions);
538 unsigned versions_below_3_0 = 0;
539 for (int i = 0; i < num_gl_versions; i++) {
540 EXPECT_LT(0, gl_versions[i * 2]);
541 EXPECT_GE(4, gl_versions[i * 2]);
543 /* Verify that the minor version advertised with the major version makes
546 switch (gl_versions[i * 2]) {
548 EXPECT_GE(5, gl_versions[i * 2 + 1]);
549 versions_below_3_0++;
552 EXPECT_GE(1, gl_versions[i * 2 + 1]);
553 versions_below_3_0++;
556 EXPECT_GE(3, gl_versions[i * 2 + 1]);
559 EXPECT_GE(2, gl_versions[i * 2 + 1]);
564 /* From the GLX_ARB_create_context spec:
566 * "Only the highest supported version below 3.0 should be sent, since
567 * OpenGL 2.1 is backwards compatible with all earlier versions."
569 EXPECT_LE(versions_below_3_0, 1);
572 TEST_F(glX_send_client_info_test, gl_versions_and_profiles_are_sane)
574 create_single_screen_display(1, 4, "GLX_ARB_create_context_profile");
575 __glX_send_client_info(this->glx_dpy);
577 ASSERT_NE(0, num_gl_versions);
579 const uint32_t all_valid_bits = GLX_CONTEXT_CORE_PROFILE_BIT_ARB
580 | GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
582 unsigned versions_below_3_0 = 0;
584 for (int i = 0; i < num_gl_versions; i++) {
585 EXPECT_LT(0, gl_versions[i * 3]);
586 EXPECT_GE(4, gl_versions[i * 3]);
588 /* Verify that the minor version advertised with the major version makes
591 switch (gl_versions[i * 3]) {
593 EXPECT_GE(5, gl_versions[i * 3 + 1]);
594 EXPECT_EQ(0, gl_versions[i * 3 + 2]);
595 versions_below_3_0++;
598 EXPECT_GE(1, gl_versions[i * 3 + 1]);
599 EXPECT_EQ(0, gl_versions[i * 3 + 2]);
600 versions_below_3_0++;
603 EXPECT_GE(3, gl_versions[i * 3 + 1]);
605 /* Profiles were not introduced until OpenGL 3.2.
607 if (gl_versions[i * 3 + 1] < 2) {
608 EXPECT_EQ(0, gl_versions[i * 3 + 2]);
610 EXPECT_EQ(0, gl_versions[i * 3 + 2] & ~all_valid_bits);
614 EXPECT_GE(2, gl_versions[i * 3 + 1]);
615 EXPECT_EQ(0, gl_versions[i * 3 + 2] & ~all_valid_bits);
620 /* From the GLX_ARB_create_context_profile spec:
622 * "Only the highest supported version below 3.0 should be sent, since
623 * OpenGL 2.1 is backwards compatible with all earlier versions."
625 EXPECT_LE(versions_below_3_0, 1);
628 TEST_F(glX_send_client_info_test, glx_version_is_1_4_for_1_1)
630 create_single_screen_display(1, 1, "");
631 __glX_send_client_info(this->glx_dpy);
633 EXPECT_EQ(1, glx_major);
634 EXPECT_EQ(4, glx_minor);
637 TEST_F(glX_send_client_info_test, glx_version_is_1_4_for_1_4)
639 create_single_screen_display(1, 4, "");
640 __glX_send_client_info(this->glx_dpy);
642 EXPECT_EQ(1, glx_major);
643 EXPECT_EQ(4, glx_minor);
646 TEST_F(glX_send_client_info_test, glx_version_is_1_4_for_1_4_with_ARB_create_context)
648 create_single_screen_display(1, 4, "GLX_ARB_create_context");
649 __glX_send_client_info(this->glx_dpy);
651 EXPECT_EQ(1, glx_major);
652 EXPECT_EQ(4, glx_minor);
655 TEST_F(glX_send_client_info_test, glx_version_is_1_4_for_1_4_with_ARB_create_context_profile)
657 create_single_screen_display(1, 4, "GLX_ARB_create_context_profile");
658 __glX_send_client_info(this->glx_dpy);
660 EXPECT_EQ(1, glx_major);
661 EXPECT_EQ(4, glx_minor);
664 TEST_F(glX_send_client_info_test, glx_version_is_1_4_for_1_5)
666 create_single_screen_display(1, 5, "");
667 __glX_send_client_info(this->glx_dpy);
669 EXPECT_EQ(1, glx_major);
670 EXPECT_EQ(4, glx_minor);
673 TEST_F(glX_send_client_info_test, glx_extensions_has_GLX_ARB_create_context)
675 create_single_screen_display(1, 4, "GLX_ARB_create_context");
676 __glX_send_client_info(this->glx_dpy);
678 ASSERT_NE(0, glx_ext_length);
679 ASSERT_NE((char *) 0, glx_ext_string);
681 bool found_GLX_ARB_create_context = false;
682 const char *const needle = "GLX_ARB_create_context";
683 const unsigned len = strlen(needle);
684 char *haystack = glx_ext_string;
685 while (haystack != NULL) {
686 char *match = strstr(haystack, needle);
688 if (match[len] == '\0' || match[len] == ' ') {
689 found_GLX_ARB_create_context = true;
693 haystack = match + len;
696 EXPECT_TRUE(found_GLX_ARB_create_context);
699 TEST_F(glX_send_client_info_test, glx_extensions_has_GLX_ARB_create_context_profile)
701 create_single_screen_display(1, 4, "GLX_ARB_create_context_profile");
702 __glX_send_client_info(this->glx_dpy);
704 ASSERT_NE(0, glx_ext_length);
705 ASSERT_NE((char *) 0, glx_ext_string);
707 bool found_GLX_ARB_create_context_profile = false;
708 const char *const needle = "GLX_ARB_create_context_profile";
709 const unsigned len = strlen(needle);
710 char *haystack = glx_ext_string;
711 while (haystack != NULL) {
712 char *match = strstr(haystack, needle);
714 if (match[len] == '\0' || match[len] == ' ') {
715 found_GLX_ARB_create_context_profile = true;
719 haystack = match + len;
722 EXPECT_TRUE(found_GLX_ARB_create_context_profile);