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"
28 #include "glx_error.h"
32 #include "mock_xdisplay.h"
33 #include "fake_glx_screen.h"
35 static bool CreateContextAttribsARB_was_sent
;
36 static xcb_glx_create_context_attribs_arb_request_t req
;
37 static uint32_t sent_attribs
[1024];
38 static uint32_t next_id
;
41 struct glx_screen
*psc
;
44 glx_context_init(struct glx_context
*gc
,
45 struct glx_screen
*psc
, struct glx_config
*config
)
47 gc
->majorOpcode
= 123;
48 gc
->screen
= psc
->scr
;
51 gc
->isDirect
= GL_TRUE
;
52 gc
->currentContextTag
= -1;
57 bool GetGLXScreenConfigs_called
= false;
59 extern "C" struct glx_screen
*
60 GetGLXScreenConfigs(Display
* dpy
, int scrn
)
65 GetGLXScreenConfigs_called
= true;
70 xcb_generate_id(xcb_connection_t
*c
)
77 extern "C" xcb_void_cookie_t
78 xcb_glx_create_context_attribs_arb_checked(xcb_connection_t
*c
,
79 xcb_glx_context_t context
,
85 const uint32_t *attribs
)
89 CreateContextAttribsARB_was_sent
= true;
90 req
.context
= context
;
91 req
.fbconfig
= fbconfig
;
93 req
.share_list
= share_list
;
94 req
.is_direct
= is_direct
;
95 req
.num_attribs
= num_attribs
;
97 if (num_attribs
!= 0 && attribs
!= NULL
)
98 memcpy(sent_attribs
, attribs
, num_attribs
* 2 * sizeof(uint32_t));
100 xcb_void_cookie_t cookie
;
101 cookie
.sequence
= 0xbadc0de;
106 extern "C" xcb_generic_error_t
*
107 xcb_request_check(xcb_connection_t
*c
, xcb_void_cookie_t cookie
)
113 __glXSendErrorForXcb(Display
* dpy
, const xcb_generic_error_t
*err
)
118 __glXSendError(Display
* dpy
, int_fast8_t errorCode
, uint_fast32_t resourceID
,
119 uint_fast16_t minorCode
, bool coreX11error
)
123 class glXCreateContextAttribARB_test
: public ::testing::Test
{
125 virtual void SetUp();
128 * Replace the existing screen with a direct-rendering screen
130 void use_direct_rendering_screen();
133 struct glx_config fbc
;
137 glXCreateContextAttribARB_test::SetUp()
139 CreateContextAttribsARB_was_sent
= false;
140 memset(&req
, 0, sizeof(req
));
142 fake_glx_context::contexts_allocated
= 0;
143 psc
= new fake_glx_screen(NULL
, 0, "");
145 this->dpy
= new mock_XDisplay(1);
147 memset(&this->fbc
, 0, sizeof(this->fbc
));
148 this->fbc
.fbconfigID
= 0xbeefcafe;
152 glXCreateContextAttribARB_test::use_direct_rendering_screen()
154 struct glx_screen
*direct_psc
=
155 new fake_glx_screen_direct(psc
->display
,
164 * \name Verify detection of client-side errors
167 TEST_F(glXCreateContextAttribARB_test
, NULL_display_returns_None
)
170 glXCreateContextAttribsARB(NULL
, (GLXFBConfig
) &this->fbc
, 0,
173 EXPECT_EQ(None
, ctx
);
174 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
177 TEST_F(glXCreateContextAttribARB_test
, NULL_fbconfig_returns_None
)
180 glXCreateContextAttribsARB(this->dpy
, NULL
, 0, False
, NULL
);
182 EXPECT_EQ(None
, ctx
);
183 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
186 TEST_F(glXCreateContextAttribARB_test
, NULL_screen_returns_None
)
192 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
195 EXPECT_EQ(None
, ctx
);
196 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
201 * \name Verify that correct protocol bits are sent to the server.
204 TEST_F(glXCreateContextAttribARB_test
, does_send_protocol
)
206 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
209 EXPECT_TRUE(CreateContextAttribsARB_was_sent
);
212 TEST_F(glXCreateContextAttribARB_test
, sent_correct_context
)
214 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
217 EXPECT_EQ(99u, req
.context
);
220 TEST_F(glXCreateContextAttribARB_test
, sent_correct_fbconfig
)
222 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
225 EXPECT_EQ(0xbeefcafe, req
.fbconfig
);
228 TEST_F(glXCreateContextAttribARB_test
, sent_correct_share_list
)
231 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
234 ASSERT_NE((GLXContext
) 0, share
);
236 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, share
,
239 struct glx_context
*glx_ctx
= (struct glx_context
*) share
;
240 EXPECT_EQ(glx_ctx
->xid
, req
.share_list
);
243 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true
)
245 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
248 EXPECT_FALSE(req
.is_direct
);
251 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false
)
253 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
256 EXPECT_FALSE(req
.is_direct
);
259 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_true
)
261 this->use_direct_rendering_screen();
263 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
266 EXPECT_TRUE(req
.is_direct
);
269 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_false
)
271 this->use_direct_rendering_screen();
273 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
276 EXPECT_FALSE(req
.is_direct
);
279 TEST_F(glXCreateContextAttribARB_test
, sent_correct_screen
)
281 this->fbc
.screen
= 7;
284 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
287 EXPECT_EQ(7u, req
.screen
);
290 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs
)
292 /* Use zeros in the second half of each attribute pair to try and trick the
293 * implementation into termiating the list early.
295 * Use non-zero in the second half of the last attribute pair to try and
296 * trick the implementation into not terminating the list early enough.
298 static const int attribs
[] = {
307 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
310 EXPECT_EQ(4u, req
.num_attribs
);
313 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_empty_list
)
315 static const int attribs
[] = {
319 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
322 EXPECT_EQ(0u, req
.num_attribs
);
325 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_NULL_list_pointer
)
327 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
330 EXPECT_EQ(0u, req
.num_attribs
);
333 TEST_F(glXCreateContextAttribARB_test
, sent_correct_attrib_list
)
336 GLX_RENDER_TYPE
, GLX_RGBA_TYPE
,
337 GLX_CONTEXT_MAJOR_VERSION_ARB
, 1,
338 GLX_CONTEXT_MINOR_VERSION_ARB
, 2,
342 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
345 for (unsigned i
= 0; i
< 6; i
++) {
346 EXPECT_EQ((uint32_t) attribs
[i
], sent_attribs
[i
]);
352 * \name Verify details of the returned GLXContext
355 TEST_F(glXCreateContextAttribARB_test
, correct_context
)
358 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
361 /* Since the server did not return an error, the GLXContext should not be
364 EXPECT_NE((GLXContext
)0, ctx
);
366 /* It shouldn't be the XID of the context either.
368 EXPECT_NE((GLXContext
)99, ctx
);
371 TEST_F(glXCreateContextAttribARB_test
, correct_context_xid
)
374 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
377 /* Since the server did not return an error, the GLXContext should not be
380 ASSERT_NE((GLXContext
)0, ctx
);
382 struct glx_context
*glx_ctx
= (struct glx_context
*) ctx
;
383 EXPECT_EQ(99u, glx_ctx
->xid
);
386 TEST_F(glXCreateContextAttribARB_test
, correct_context_share_xid
)
389 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
392 ASSERT_NE((GLXContext
) 0, first
);
395 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, first
,
398 ASSERT_NE((GLXContext
) 0, second
);
400 struct glx_context
*share
= (struct glx_context
*) first
;
401 struct glx_context
*ctx
= (struct glx_context
*) second
;
402 EXPECT_EQ(share
->xid
, ctx
->share_xid
);
405 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true
)
408 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
411 ASSERT_NE((GLXContext
) 0, ctx
);
413 struct glx_context
*gc
= (struct glx_context
*) ctx
;
415 EXPECT_FALSE(gc
->isDirect
);
418 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false
)
421 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
424 ASSERT_NE((GLXContext
) 0, ctx
);
426 struct glx_context
*gc
= (struct glx_context
*) ctx
;
428 EXPECT_FALSE(gc
->isDirect
);
431 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_true
)
433 this->use_direct_rendering_screen();
436 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
439 ASSERT_NE((GLXContext
) 0, ctx
);
441 struct glx_context
*gc
= (struct glx_context
*) ctx
;
443 EXPECT_TRUE(gc
->isDirect
);
446 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_false
)
448 this->use_direct_rendering_screen();
451 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
454 ASSERT_NE((GLXContext
) 0, ctx
);
456 struct glx_context
*gc
= (struct glx_context
*) ctx
;
458 EXPECT_FALSE(gc
->isDirect
);
461 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_client_state_private
)
464 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
467 ASSERT_NE((GLXContext
) 0, ctx
);
469 struct glx_context
*gc
= (struct glx_context
*) ctx
;
471 ASSERT_FALSE(gc
->isDirect
);
472 EXPECT_EQ((struct __GLXattributeRec
*) 0xcafebabe,
473 gc
->client_state_private
);
476 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_config
)
479 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
482 ASSERT_NE((GLXContext
) 0, ctx
);
484 struct glx_context
*gc
= (struct glx_context
*) ctx
;
486 EXPECT_EQ(&this->fbc
, gc
->config
);
489 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_number
)
491 this->fbc
.screen
= 7;
495 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
498 ASSERT_NE((GLXContext
) 0, ctx
);
500 struct glx_context
*gc
= (struct glx_context
*) ctx
;
502 EXPECT_EQ(7, gc
->screen
);
505 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_pointer
)
508 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
511 ASSERT_NE((GLXContext
) 0, ctx
);
513 struct glx_context
*gc
= (struct glx_context
*) ctx
;
515 EXPECT_EQ(psc
, gc
->psc
);