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 extern "C" struct glx_screen
*
58 GetGLXScreenConfigs(Display
* dpy
, int scrn
)
66 xcb_generate_id(xcb_connection_t
*c
)
73 extern "C" xcb_void_cookie_t
74 xcb_glx_create_context_attribs_arb_checked(xcb_connection_t
*c
,
75 xcb_glx_context_t context
,
81 const uint32_t *attribs
)
85 CreateContextAttribsARB_was_sent
= true;
86 req
.context
= context
;
87 req
.fbconfig
= fbconfig
;
89 req
.share_list
= share_list
;
90 req
.is_direct
= is_direct
;
91 req
.num_attribs
= num_attribs
;
93 if (num_attribs
!= 0 && attribs
!= NULL
)
94 memcpy(sent_attribs
, attribs
, num_attribs
* 2 * sizeof(uint32_t));
96 xcb_void_cookie_t cookie
;
97 cookie
.sequence
= 0xbadc0de;
102 extern "C" xcb_generic_error_t
*
103 xcb_request_check(xcb_connection_t
*c
, xcb_void_cookie_t cookie
)
109 __glXSendErrorForXcb(Display
* dpy
, const xcb_generic_error_t
*err
)
114 __glXSendError(Display
* dpy
, int_fast8_t errorCode
, uint_fast32_t resourceID
,
115 uint_fast16_t minorCode
, bool coreX11error
)
119 class glXCreateContextAttribARB_test
: public ::testing::Test
{
121 virtual void SetUp();
124 * Replace the existing screen with a direct-rendering screen
126 void use_direct_rendering_screen();
129 struct glx_config fbc
;
133 glXCreateContextAttribARB_test::SetUp()
135 CreateContextAttribsARB_was_sent
= false;
136 memset(&req
, 0, sizeof(req
));
138 fake_glx_context::contexts_allocated
= 0;
139 psc
= new fake_glx_screen(NULL
, 0, "");
141 this->dpy
= new mock_XDisplay(1);
143 memset(&this->fbc
, 0, sizeof(this->fbc
));
144 this->fbc
.fbconfigID
= 0xbeefcafe;
148 glXCreateContextAttribARB_test::use_direct_rendering_screen()
150 struct glx_screen
*direct_psc
=
151 new fake_glx_screen_direct(psc
->display
,
160 * \name Verify detection of client-side errors
163 TEST_F(glXCreateContextAttribARB_test
, NULL_display_returns_None
)
166 glXCreateContextAttribsARB(NULL
, (GLXFBConfig
) &this->fbc
, 0,
169 EXPECT_EQ(None
, ctx
);
170 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
173 TEST_F(glXCreateContextAttribARB_test
, NULL_fbconfig_returns_None
)
176 glXCreateContextAttribsARB(this->dpy
, NULL
, 0, False
, NULL
);
178 EXPECT_EQ(None
, ctx
);
179 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
182 TEST_F(glXCreateContextAttribARB_test
, NULL_screen_returns_None
)
188 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
191 EXPECT_EQ(None
, ctx
);
192 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
197 * \name Verify that correct protocol bits are sent to the server.
200 TEST_F(glXCreateContextAttribARB_test
, does_send_protocol
)
202 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
205 EXPECT_TRUE(CreateContextAttribsARB_was_sent
);
208 TEST_F(glXCreateContextAttribARB_test
, sent_correct_context
)
210 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
213 EXPECT_EQ(99u, req
.context
);
216 TEST_F(glXCreateContextAttribARB_test
, sent_correct_fbconfig
)
218 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
221 EXPECT_EQ(0xbeefcafe, req
.fbconfig
);
224 TEST_F(glXCreateContextAttribARB_test
, sent_correct_share_list
)
227 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
230 ASSERT_NE((GLXContext
) 0, share
);
232 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, share
,
235 struct glx_context
*glx_ctx
= (struct glx_context
*) share
;
236 EXPECT_EQ(glx_ctx
->xid
, req
.share_list
);
239 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true
)
241 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
244 EXPECT_FALSE(req
.is_direct
);
247 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false
)
249 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
252 EXPECT_FALSE(req
.is_direct
);
255 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_true
)
257 this->use_direct_rendering_screen();
259 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
262 EXPECT_TRUE(req
.is_direct
);
265 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_false
)
267 this->use_direct_rendering_screen();
269 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
272 EXPECT_FALSE(req
.is_direct
);
275 TEST_F(glXCreateContextAttribARB_test
, sent_correct_screen
)
277 this->fbc
.screen
= 7;
280 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
283 EXPECT_EQ(7u, req
.screen
);
286 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs
)
288 /* Use zeros in the second half of each attribute pair to try and trick the
289 * implementation into termiating the list early.
291 * Use non-zero in the second half of the last attribute pair to try and
292 * trick the implementation into not terminating the list early enough.
294 static const int attribs
[] = {
303 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
306 EXPECT_EQ(4u, req
.num_attribs
);
309 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_empty_list
)
311 static const int attribs
[] = {
315 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
318 EXPECT_EQ(0u, req
.num_attribs
);
321 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_NULL_list_pointer
)
323 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
326 EXPECT_EQ(0u, req
.num_attribs
);
329 TEST_F(glXCreateContextAttribARB_test
, sent_correct_attrib_list
)
332 GLX_RENDER_TYPE
, GLX_RGBA_TYPE
,
333 GLX_CONTEXT_MAJOR_VERSION_ARB
, 1,
334 GLX_CONTEXT_MINOR_VERSION_ARB
, 2,
338 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
341 for (unsigned i
= 0; i
< 6; i
++) {
342 EXPECT_EQ((uint32_t) attribs
[i
], sent_attribs
[i
]);
348 * \name Verify details of the returned GLXContext
351 TEST_F(glXCreateContextAttribARB_test
, correct_context
)
354 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
357 /* Since the server did not return an error, the GLXContext should not be
360 EXPECT_NE((GLXContext
)0, ctx
);
362 /* It shouldn't be the XID of the context either.
364 EXPECT_NE((GLXContext
)99, ctx
);
367 TEST_F(glXCreateContextAttribARB_test
, correct_context_xid
)
370 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
373 /* Since the server did not return an error, the GLXContext should not be
376 ASSERT_NE((GLXContext
)0, ctx
);
378 struct glx_context
*glx_ctx
= (struct glx_context
*) ctx
;
379 EXPECT_EQ(99u, glx_ctx
->xid
);
382 TEST_F(glXCreateContextAttribARB_test
, correct_context_share_xid
)
385 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
388 ASSERT_NE((GLXContext
) 0, first
);
391 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, first
,
394 ASSERT_NE((GLXContext
) 0, second
);
396 struct glx_context
*share
= (struct glx_context
*) first
;
397 struct glx_context
*ctx
= (struct glx_context
*) second
;
398 EXPECT_EQ(share
->xid
, ctx
->share_xid
);
401 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true
)
404 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
407 ASSERT_NE((GLXContext
) 0, ctx
);
409 struct glx_context
*gc
= (struct glx_context
*) ctx
;
411 EXPECT_FALSE(gc
->isDirect
);
414 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false
)
417 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
420 ASSERT_NE((GLXContext
) 0, ctx
);
422 struct glx_context
*gc
= (struct glx_context
*) ctx
;
424 EXPECT_FALSE(gc
->isDirect
);
427 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_true
)
429 this->use_direct_rendering_screen();
432 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
435 ASSERT_NE((GLXContext
) 0, ctx
);
437 struct glx_context
*gc
= (struct glx_context
*) ctx
;
439 EXPECT_TRUE(gc
->isDirect
);
442 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_false
)
444 this->use_direct_rendering_screen();
447 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
450 ASSERT_NE((GLXContext
) 0, ctx
);
452 struct glx_context
*gc
= (struct glx_context
*) ctx
;
454 EXPECT_FALSE(gc
->isDirect
);
457 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_client_state_private
)
460 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
463 ASSERT_NE((GLXContext
) 0, ctx
);
465 struct glx_context
*gc
= (struct glx_context
*) ctx
;
467 ASSERT_FALSE(gc
->isDirect
);
468 EXPECT_EQ((struct __GLXattributeRec
*) 0xcafebabe,
469 gc
->client_state_private
);
472 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_config
)
475 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
478 ASSERT_NE((GLXContext
) 0, ctx
);
480 struct glx_context
*gc
= (struct glx_context
*) ctx
;
482 EXPECT_EQ(&this->fbc
, gc
->config
);
485 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_number
)
487 this->fbc
.screen
= 7;
491 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
494 ASSERT_NE((GLXContext
) 0, ctx
);
496 struct glx_context
*gc
= (struct glx_context
*) ctx
;
498 EXPECT_EQ(7, gc
->screen
);
501 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_pointer
)
504 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
507 ASSERT_NE((GLXContext
) 0, ctx
);
509 struct glx_context
*gc
= (struct glx_context
*) ctx
;
511 EXPECT_EQ(psc
, gc
->psc
);