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>
26 #include "glxclient.h"
27 #include "glx_error.h"
30 #include "mock_xdisplay.h"
31 #include "fake_glx_screen.h"
33 static bool CreateContextAttribsARB_was_sent
;
34 static xcb_glx_create_context_attribs_arb_request_t req
;
35 static uint32_t sent_attribs
[1024];
36 static uint32_t next_id
;
39 struct glx_screen
*psc
;
42 glx_context_init(struct glx_context
*gc
,
43 struct glx_screen
*psc
, struct glx_config
*config
)
45 gc
->majorOpcode
= 123;
46 gc
->screen
= psc
->scr
;
49 gc
->isDirect
= GL_TRUE
;
50 gc
->currentContextTag
= -1;
55 bool GetGLXScreenConfigs_called
= false;
57 extern "C" struct glx_screen
*
58 GetGLXScreenConfigs(Display
* dpy
, int scrn
)
63 GetGLXScreenConfigs_called
= true;
68 xcb_generate_id(xcb_connection_t
*c
)
75 extern "C" xcb_void_cookie_t
76 xcb_glx_create_context_attribs_arb_checked(xcb_connection_t
*c
,
77 xcb_glx_context_t context
,
83 const uint32_t *attribs
)
87 CreateContextAttribsARB_was_sent
= true;
88 req
.context
= context
;
89 req
.fbconfig
= fbconfig
;
91 req
.share_list
= share_list
;
92 req
.is_direct
= is_direct
;
93 req
.num_attribs
= num_attribs
;
95 if (num_attribs
!= 0 && attribs
!= NULL
)
96 memcpy(sent_attribs
, attribs
, num_attribs
* 2 * sizeof(uint32_t));
98 xcb_void_cookie_t cookie
;
99 cookie
.sequence
= 0xbadc0de;
104 extern "C" xcb_generic_error_t
*
105 xcb_request_check(xcb_connection_t
*c
, xcb_void_cookie_t cookie
)
111 __glXSendErrorForXcb(Display
* dpy
, const xcb_generic_error_t
*err
)
116 __glXSendError(Display
* dpy
, int_fast8_t errorCode
, uint_fast32_t resourceID
,
117 uint_fast16_t minorCode
, bool coreX11error
)
121 class glXCreateContextAttribARB_test
: public ::testing::Test
{
123 virtual void SetUp();
126 * Replace the existing screen with a direct-rendering screen
128 void use_direct_rendering_screen();
131 struct glx_config fbc
;
135 glXCreateContextAttribARB_test::SetUp()
137 CreateContextAttribsARB_was_sent
= false;
138 memset(&req
, 0, sizeof(req
));
140 fake_glx_context::contexts_allocated
= 0;
141 psc
= new fake_glx_screen(NULL
, 0, "");
143 this->dpy
= new mock_XDisplay(1);
145 memset(&this->fbc
, 0, sizeof(this->fbc
));
146 this->fbc
.fbconfigID
= 0xbeefcafe;
150 glXCreateContextAttribARB_test::use_direct_rendering_screen()
152 struct glx_screen
*direct_psc
=
153 new fake_glx_screen_direct(psc
->display
,
162 * \name Verify detection of client-side errors
165 TEST_F(glXCreateContextAttribARB_test
, NULL_display_returns_None
)
168 glXCreateContextAttribsARB(NULL
, (GLXFBConfig
) &this->fbc
, 0,
171 EXPECT_EQ(None
, ctx
);
172 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
175 TEST_F(glXCreateContextAttribARB_test
, NULL_fbconfig_returns_None
)
178 glXCreateContextAttribsARB(this->dpy
, NULL
, 0, False
, NULL
);
180 EXPECT_EQ(None
, ctx
);
181 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
184 TEST_F(glXCreateContextAttribARB_test
, NULL_screen_returns_None
)
190 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
193 EXPECT_EQ(None
, ctx
);
194 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
199 * \name Verify that correct protocol bits are sent to the server.
202 TEST_F(glXCreateContextAttribARB_test
, does_send_protocol
)
204 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
207 EXPECT_TRUE(CreateContextAttribsARB_was_sent
);
210 TEST_F(glXCreateContextAttribARB_test
, sent_correct_context
)
212 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
215 EXPECT_EQ(99u, req
.context
);
218 TEST_F(glXCreateContextAttribARB_test
, sent_correct_fbconfig
)
220 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
223 EXPECT_EQ(0xbeefcafe, req
.fbconfig
);
226 TEST_F(glXCreateContextAttribARB_test
, sent_correct_share_list
)
229 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
232 ASSERT_NE((GLXContext
) 0, share
);
234 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, share
,
237 struct glx_context
*glx_ctx
= (struct glx_context
*) share
;
238 EXPECT_EQ(glx_ctx
->xid
, req
.share_list
);
241 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true
)
243 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
246 EXPECT_FALSE(req
.is_direct
);
249 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false
)
251 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
254 EXPECT_FALSE(req
.is_direct
);
257 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_true
)
259 this->use_direct_rendering_screen();
261 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
264 EXPECT_TRUE(req
.is_direct
);
267 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_false
)
269 this->use_direct_rendering_screen();
271 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
274 EXPECT_FALSE(req
.is_direct
);
277 TEST_F(glXCreateContextAttribARB_test
, sent_correct_screen
)
279 this->fbc
.screen
= 7;
282 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
285 EXPECT_EQ(7u, req
.screen
);
288 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs
)
290 /* Use zeros in the second half of each attribute pair to try and trick the
291 * implementation into termiating the list early.
293 * Use non-zero in the second half of the last attribute pair to try and
294 * trick the implementation into not terminating the list early enough.
296 static const int attribs
[] = {
305 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
308 EXPECT_EQ(4u, req
.num_attribs
);
311 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_empty_list
)
313 static const int attribs
[] = {
317 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
320 EXPECT_EQ(0u, req
.num_attribs
);
323 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_NULL_list_pointer
)
325 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
328 EXPECT_EQ(0u, req
.num_attribs
);
331 TEST_F(glXCreateContextAttribARB_test
, sent_correct_attrib_list
)
334 GLX_RENDER_TYPE
, GLX_RGBA_TYPE
,
335 GLX_CONTEXT_MAJOR_VERSION_ARB
, 1,
336 GLX_CONTEXT_MINOR_VERSION_ARB
, 2,
340 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
343 for (unsigned i
= 0; i
< 6; i
++) {
344 EXPECT_EQ((uint32_t) attribs
[i
], sent_attribs
[i
]);
350 * \name Verify details of the returned GLXContext
353 TEST_F(glXCreateContextAttribARB_test
, correct_context
)
356 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
359 /* Since the server did not return an error, the GLXContext should not be
362 EXPECT_NE((GLXContext
)0, ctx
);
364 /* It shouldn't be the XID of the context either.
366 EXPECT_NE((GLXContext
)99, ctx
);
369 TEST_F(glXCreateContextAttribARB_test
, correct_context_xid
)
372 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
375 /* Since the server did not return an error, the GLXContext should not be
378 ASSERT_NE((GLXContext
)0, ctx
);
380 struct glx_context
*glx_ctx
= (struct glx_context
*) ctx
;
381 EXPECT_EQ(99u, glx_ctx
->xid
);
384 TEST_F(glXCreateContextAttribARB_test
, correct_context_share_xid
)
387 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
390 ASSERT_NE((GLXContext
) 0, first
);
393 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, first
,
396 ASSERT_NE((GLXContext
) 0, second
);
398 struct glx_context
*share
= (struct glx_context
*) first
;
399 struct glx_context
*ctx
= (struct glx_context
*) second
;
400 EXPECT_EQ(share
->xid
, ctx
->share_xid
);
403 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true
)
406 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
409 ASSERT_NE((GLXContext
) 0, ctx
);
411 struct glx_context
*gc
= (struct glx_context
*) ctx
;
413 EXPECT_FALSE(gc
->isDirect
);
416 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false
)
419 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
422 ASSERT_NE((GLXContext
) 0, ctx
);
424 struct glx_context
*gc
= (struct glx_context
*) ctx
;
426 EXPECT_FALSE(gc
->isDirect
);
429 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_true
)
431 this->use_direct_rendering_screen();
434 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
437 ASSERT_NE((GLXContext
) 0, ctx
);
439 struct glx_context
*gc
= (struct glx_context
*) ctx
;
441 EXPECT_TRUE(gc
->isDirect
);
444 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_false
)
446 this->use_direct_rendering_screen();
449 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
452 ASSERT_NE((GLXContext
) 0, ctx
);
454 struct glx_context
*gc
= (struct glx_context
*) ctx
;
456 EXPECT_FALSE(gc
->isDirect
);
459 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_client_state_private
)
462 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
465 ASSERT_NE((GLXContext
) 0, ctx
);
467 struct glx_context
*gc
= (struct glx_context
*) ctx
;
469 ASSERT_FALSE(gc
->isDirect
);
470 EXPECT_EQ((struct __GLXattributeRec
*) 0xcafebabe,
471 gc
->client_state_private
);
474 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_config
)
477 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
480 ASSERT_NE((GLXContext
) 0, ctx
);
482 struct glx_context
*gc
= (struct glx_context
*) ctx
;
484 EXPECT_EQ(&this->fbc
, gc
->config
);
487 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_number
)
489 this->fbc
.screen
= 7;
493 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
496 ASSERT_NE((GLXContext
) 0, ctx
);
498 struct glx_context
*gc
= (struct glx_context
*) ctx
;
500 EXPECT_EQ(7, gc
->screen
);
503 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_pointer
)
506 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
509 ASSERT_NE((GLXContext
) 0, ctx
);
511 struct glx_context
*gc
= (struct glx_context
*) ctx
;
513 EXPECT_EQ(psc
, gc
->psc
);