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_screen_returns_None
)
181 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
184 EXPECT_EQ(None
, ctx
);
185 EXPECT_EQ(0, fake_glx_context::contexts_allocated
);
190 * \name Verify that correct protocol bits are sent to the server.
193 TEST_F(glXCreateContextAttribARB_test
, does_send_protocol
)
195 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
198 EXPECT_TRUE(CreateContextAttribsARB_was_sent
);
201 TEST_F(glXCreateContextAttribARB_test
, sent_correct_context
)
203 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
206 EXPECT_EQ(99u, req
.context
);
209 TEST_F(glXCreateContextAttribARB_test
, sent_correct_fbconfig
)
211 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
214 EXPECT_EQ(0xbeefcafe, req
.fbconfig
);
217 TEST_F(glXCreateContextAttribARB_test
, sent_correct_share_list
)
220 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
223 ASSERT_NE((GLXContext
) 0, share
);
225 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, share
,
228 struct glx_context
*glx_ctx
= (struct glx_context
*) share
;
229 EXPECT_EQ(glx_ctx
->xid
, req
.share_list
);
232 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true
)
234 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
237 EXPECT_FALSE(req
.is_direct
);
240 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false
)
242 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
245 EXPECT_FALSE(req
.is_direct
);
248 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_true
)
250 this->use_direct_rendering_screen();
252 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
255 EXPECT_TRUE(req
.is_direct
);
258 TEST_F(glXCreateContextAttribARB_test
, sent_correct_is_direct_for_direct_screen_and_direct_set_to_false
)
260 this->use_direct_rendering_screen();
262 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
265 EXPECT_FALSE(req
.is_direct
);
268 TEST_F(glXCreateContextAttribARB_test
, sent_correct_screen
)
270 this->fbc
.screen
= 7;
273 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
276 EXPECT_EQ(7u, req
.screen
);
279 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs
)
281 /* Use zeros in the second half of each attribute pair to try and trick the
282 * implementation into termiating the list early.
284 * Use non-zero in the second half of the last attribute pair to try and
285 * trick the implementation into not terminating the list early enough.
287 static const int attribs
[] = {
296 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
299 EXPECT_EQ(4u, req
.num_attribs
);
302 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_empty_list
)
304 static const int attribs
[] = {
308 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
311 EXPECT_EQ(0u, req
.num_attribs
);
314 TEST_F(glXCreateContextAttribARB_test
, sent_correct_num_attribs_NULL_list_pointer
)
316 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
319 EXPECT_EQ(0u, req
.num_attribs
);
322 TEST_F(glXCreateContextAttribARB_test
, sent_correct_attrib_list
)
325 GLX_RENDER_TYPE
, GLX_RGBA_TYPE
,
326 GLX_CONTEXT_MAJOR_VERSION_ARB
, 1,
327 GLX_CONTEXT_MINOR_VERSION_ARB
, 2,
331 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
334 for (unsigned i
= 0; i
< 6; i
++) {
335 EXPECT_EQ((uint32_t) attribs
[i
], sent_attribs
[i
]);
341 * \name Verify details of the returned GLXContext
344 TEST_F(glXCreateContextAttribARB_test
, correct_context
)
347 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
350 /* Since the server did not return an error, the GLXContext should not be
353 EXPECT_NE((GLXContext
)0, ctx
);
355 /* It shouldn't be the XID of the context either.
357 EXPECT_NE((GLXContext
)99, ctx
);
360 TEST_F(glXCreateContextAttribARB_test
, correct_context_xid
)
363 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
366 /* Since the server did not return an error, the GLXContext should not be
369 ASSERT_NE((GLXContext
)0, ctx
);
371 struct glx_context
*glx_ctx
= (struct glx_context
*) ctx
;
372 EXPECT_EQ(99u, glx_ctx
->xid
);
375 TEST_F(glXCreateContextAttribARB_test
, correct_context_share_xid
)
378 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
381 ASSERT_NE((GLXContext
) 0, first
);
384 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, first
,
387 ASSERT_NE((GLXContext
) 0, second
);
389 struct glx_context
*share
= (struct glx_context
*) first
;
390 struct glx_context
*ctx
= (struct glx_context
*) second
;
391 EXPECT_EQ(share
->xid
, ctx
->share_xid
);
394 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true
)
397 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
400 ASSERT_NE((GLXContext
) 0, ctx
);
402 struct glx_context
*gc
= (struct glx_context
*) ctx
;
404 EXPECT_FALSE(gc
->isDirect
);
407 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false
)
410 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
413 ASSERT_NE((GLXContext
) 0, ctx
);
415 struct glx_context
*gc
= (struct glx_context
*) ctx
;
417 EXPECT_FALSE(gc
->isDirect
);
420 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_true
)
422 this->use_direct_rendering_screen();
425 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
428 ASSERT_NE((GLXContext
) 0, ctx
);
430 struct glx_context
*gc
= (struct glx_context
*) ctx
;
432 EXPECT_TRUE(gc
->isDirect
);
435 TEST_F(glXCreateContextAttribARB_test
, correct_context_isDirect_for_direct_screen_and_direct_set_to_false
)
437 this->use_direct_rendering_screen();
440 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
443 ASSERT_NE((GLXContext
) 0, ctx
);
445 struct glx_context
*gc
= (struct glx_context
*) ctx
;
447 EXPECT_FALSE(gc
->isDirect
);
450 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_client_state_private
)
453 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
456 ASSERT_NE((GLXContext
) 0, ctx
);
458 struct glx_context
*gc
= (struct glx_context
*) ctx
;
460 ASSERT_FALSE(gc
->isDirect
);
461 EXPECT_EQ((struct __GLXattributeRec
*) 0xcafebabe,
462 gc
->client_state_private
);
465 TEST_F(glXCreateContextAttribARB_test
, correct_indirect_context_config
)
468 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
471 ASSERT_NE((GLXContext
) 0, ctx
);
473 struct glx_context
*gc
= (struct glx_context
*) ctx
;
475 EXPECT_EQ(&this->fbc
, gc
->config
);
478 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_number
)
480 this->fbc
.screen
= 7;
484 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
487 ASSERT_NE((GLXContext
) 0, ctx
);
489 struct glx_context
*gc
= (struct glx_context
*) ctx
;
491 EXPECT_EQ(7, gc
->screen
);
494 TEST_F(glXCreateContextAttribARB_test
, correct_context_screen_pointer
)
497 glXCreateContextAttribsARB(this->dpy
, (GLXFBConfig
) &this->fbc
, 0,
500 ASSERT_NE((GLXContext
) 0, ctx
);
502 struct glx_context
*gc
= (struct glx_context
*) ctx
;
504 EXPECT_EQ(psc
, gc
->psc
);