Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

create_context_unittest.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright © 2011 Intel Corporation
  3. *
  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:
  10. *
  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
  13. * Software.
  14. *
  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.
  22. */
  23. #include <gtest/gtest.h>
  24. #include <string.h>
  25. extern "C" {
  26. #include "glxclient.h"
  27. #include "glx_error.h"
  28. }
  29. #include <xcb/glx.h>
  30. #include "mock_xdisplay.h"
  31. #include "fake_glx_screen.h"
  32. static bool CreateContextAttribsARB_was_sent;
  33. static xcb_glx_create_context_attribs_arb_request_t req;
  34. static uint32_t sent_attribs[1024];
  35. static uint32_t next_id;
  36. struct glx_screen *psc;
  37. extern "C" Bool
  38. glx_context_init(struct glx_context *gc,
  39. struct glx_screen *psc, struct glx_config *config)
  40. {
  41. gc->majorOpcode = 123;
  42. gc->screen = psc->scr;
  43. gc->psc = psc;
  44. gc->config = config;
  45. gc->isDirect = GL_TRUE;
  46. gc->currentContextTag = -1;
  47. return GL_TRUE;
  48. }
  49. extern "C" struct glx_screen *
  50. GetGLXScreenConfigs(Display * dpy, int scrn)
  51. {
  52. (void) dpy;
  53. (void) scrn;
  54. return psc;
  55. }
  56. extern "C" uint32_t
  57. xcb_generate_id(xcb_connection_t *c)
  58. {
  59. (void) c;
  60. return next_id++;
  61. }
  62. extern "C" xcb_void_cookie_t
  63. xcb_glx_create_context_attribs_arb_checked(xcb_connection_t *c,
  64. xcb_glx_context_t context,
  65. uint32_t fbconfig,
  66. uint32_t screen,
  67. uint32_t share_list,
  68. uint8_t is_direct,
  69. uint32_t num_attribs,
  70. const uint32_t *attribs)
  71. {
  72. (void) c;
  73. CreateContextAttribsARB_was_sent = true;
  74. req.context = context;
  75. req.fbconfig = fbconfig;
  76. req.screen = screen;
  77. req.share_list = share_list;
  78. req.is_direct = is_direct;
  79. req.num_attribs = num_attribs;
  80. if (num_attribs != 0 && attribs != NULL)
  81. memcpy(sent_attribs, attribs, num_attribs * 2 * sizeof(uint32_t));
  82. xcb_void_cookie_t cookie;
  83. cookie.sequence = 0xbadc0de;
  84. return cookie;
  85. }
  86. extern "C" xcb_generic_error_t *
  87. xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
  88. {
  89. return NULL;
  90. }
  91. extern "C" void
  92. __glXSendErrorForXcb(Display * dpy, const xcb_generic_error_t *err)
  93. {
  94. }
  95. extern "C" void
  96. __glXSendError(Display * dpy, int_fast8_t errorCode, uint_fast32_t resourceID,
  97. uint_fast16_t minorCode, bool coreX11error)
  98. {
  99. }
  100. class glXCreateContextAttribARB_test : public ::testing::Test {
  101. public:
  102. virtual void SetUp();
  103. /**
  104. * Replace the existing screen with a direct-rendering screen
  105. */
  106. void use_direct_rendering_screen();
  107. mock_XDisplay *dpy;
  108. struct glx_config fbc;
  109. };
  110. void
  111. glXCreateContextAttribARB_test::SetUp()
  112. {
  113. CreateContextAttribsARB_was_sent = false;
  114. memset(&req, 0, sizeof(req));
  115. next_id = 99;
  116. fake_glx_context::contexts_allocated = 0;
  117. psc = new fake_glx_screen(NULL, 0, "");
  118. this->dpy = new mock_XDisplay(1);
  119. memset(&this->fbc, 0, sizeof(this->fbc));
  120. this->fbc.fbconfigID = 0xbeefcafe;
  121. }
  122. void
  123. glXCreateContextAttribARB_test::use_direct_rendering_screen()
  124. {
  125. struct glx_screen *direct_psc =
  126. new fake_glx_screen_direct(psc->display,
  127. psc->scr,
  128. psc->serverGLXexts);
  129. delete psc;
  130. psc = direct_psc;
  131. }
  132. /**
  133. * \name Verify detection of client-side errors
  134. */
  135. /*@{*/
  136. TEST_F(glXCreateContextAttribARB_test, NULL_display_returns_None)
  137. {
  138. GLXContext ctx =
  139. glXCreateContextAttribsARB(NULL, (GLXFBConfig) &this->fbc, 0,
  140. False, NULL);
  141. EXPECT_EQ(None, ctx);
  142. EXPECT_EQ(0, fake_glx_context::contexts_allocated);
  143. }
  144. TEST_F(glXCreateContextAttribARB_test, NULL_fbconfig_returns_None)
  145. {
  146. GLXContext ctx =
  147. glXCreateContextAttribsARB(this->dpy, NULL, 0, False, NULL);
  148. EXPECT_EQ(None, ctx);
  149. EXPECT_EQ(0, fake_glx_context::contexts_allocated);
  150. }
  151. TEST_F(glXCreateContextAttribARB_test, NULL_screen_returns_None)
  152. {
  153. delete psc;
  154. psc = NULL;
  155. GLXContext ctx =
  156. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  157. False, NULL);
  158. EXPECT_EQ(None, ctx);
  159. EXPECT_EQ(0, fake_glx_context::contexts_allocated);
  160. }
  161. /*@}*/
  162. /**
  163. * \name Verify that correct protocol bits are sent to the server.
  164. */
  165. /*@{*/
  166. TEST_F(glXCreateContextAttribARB_test, does_send_protocol)
  167. {
  168. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  169. False, NULL);
  170. EXPECT_TRUE(CreateContextAttribsARB_was_sent);
  171. }
  172. TEST_F(glXCreateContextAttribARB_test, sent_correct_context)
  173. {
  174. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  175. False, NULL);
  176. EXPECT_EQ(99, req.context);
  177. }
  178. TEST_F(glXCreateContextAttribARB_test, sent_correct_fbconfig)
  179. {
  180. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  181. False, NULL);
  182. EXPECT_EQ(0xbeefcafe, req.fbconfig);
  183. }
  184. TEST_F(glXCreateContextAttribARB_test, sent_correct_share_list)
  185. {
  186. GLXContext share =
  187. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  188. False, NULL);
  189. ASSERT_NE((GLXContext) 0, share);
  190. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, share,
  191. False, NULL);
  192. struct glx_context *glx_ctx = (struct glx_context *) share;
  193. EXPECT_EQ(glx_ctx->xid, req.share_list);
  194. }
  195. TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true)
  196. {
  197. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  198. True, NULL);
  199. EXPECT_FALSE(req.is_direct);
  200. }
  201. TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false)
  202. {
  203. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  204. False, NULL);
  205. EXPECT_FALSE(req.is_direct);
  206. }
  207. TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_direct_screen_and_direct_set_to_true)
  208. {
  209. this->use_direct_rendering_screen();
  210. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  211. True, NULL);
  212. EXPECT_TRUE(req.is_direct);
  213. }
  214. TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_direct_screen_and_direct_set_to_false)
  215. {
  216. this->use_direct_rendering_screen();
  217. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  218. False, NULL);
  219. EXPECT_FALSE(req.is_direct);
  220. }
  221. TEST_F(glXCreateContextAttribARB_test, sent_correct_screen)
  222. {
  223. this->fbc.screen = 7;
  224. psc->scr = 7;
  225. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  226. False, NULL);
  227. EXPECT_EQ(7, req.screen);
  228. }
  229. TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs)
  230. {
  231. /* Use zeros in the second half of each attribute pair to try and trick the
  232. * implementation into termiating the list early.
  233. *
  234. * Use non-zero in the second half of the last attribute pair to try and
  235. * trick the implementation into not terminating the list early enough.
  236. */
  237. static const int attribs[] = {
  238. 1, 0,
  239. 2, 0,
  240. 3, 0,
  241. 4, 0,
  242. 0, 6,
  243. 0, 0
  244. };
  245. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  246. False, attribs);
  247. EXPECT_EQ(4, req.num_attribs);
  248. }
  249. TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs_empty_list)
  250. {
  251. static const int attribs[] = {
  252. 0,
  253. };
  254. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  255. False, attribs);
  256. EXPECT_EQ(0, req.num_attribs);
  257. }
  258. TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs_NULL_list_pointer)
  259. {
  260. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  261. False, NULL);
  262. EXPECT_EQ(0, req.num_attribs);
  263. }
  264. TEST_F(glXCreateContextAttribARB_test, sent_correct_attrib_list)
  265. {
  266. int attribs[] = {
  267. GLX_RENDER_TYPE, GLX_RGBA_TYPE,
  268. GLX_CONTEXT_MAJOR_VERSION_ARB, 1,
  269. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  270. 0
  271. };
  272. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  273. False, attribs);
  274. for (unsigned i = 0; i < 6; i++) {
  275. EXPECT_EQ(attribs[i], sent_attribs[i]);
  276. }
  277. }
  278. /*@}*/
  279. /**
  280. * \name Verify details of the returned GLXContext
  281. */
  282. /*@{*/
  283. TEST_F(glXCreateContextAttribARB_test, correct_context)
  284. {
  285. GLXContext ctx =
  286. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  287. False, NULL);
  288. /* Since the server did not return an error, the GLXContext should not be
  289. * NULL.
  290. */
  291. EXPECT_NE((GLXContext)0, ctx);
  292. /* It shouldn't be the XID of the context either.
  293. */
  294. EXPECT_NE((GLXContext)99, ctx);
  295. }
  296. TEST_F(glXCreateContextAttribARB_test, correct_context_xid)
  297. {
  298. GLXContext ctx =
  299. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  300. False, NULL);
  301. /* Since the server did not return an error, the GLXContext should not be
  302. * NULL.
  303. */
  304. ASSERT_NE((GLXContext)0, ctx);
  305. struct glx_context *glx_ctx = (struct glx_context *) ctx;
  306. EXPECT_EQ(99, glx_ctx->xid);
  307. }
  308. TEST_F(glXCreateContextAttribARB_test, correct_context_share_xid)
  309. {
  310. GLXContext first =
  311. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  312. False, NULL);
  313. ASSERT_NE((GLXContext) 0, first);
  314. GLXContext second =
  315. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, first,
  316. False, NULL);
  317. ASSERT_NE((GLXContext) 0, second);
  318. struct glx_context *share = (struct glx_context *) first;
  319. struct glx_context *ctx = (struct glx_context *) second;
  320. EXPECT_EQ(share->xid, ctx->share_xid);
  321. }
  322. TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true)
  323. {
  324. GLXContext ctx =
  325. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  326. True, NULL);
  327. ASSERT_NE((GLXContext) 0, ctx);
  328. struct glx_context *gc = (struct glx_context *) ctx;
  329. EXPECT_FALSE(gc->isDirect);
  330. }
  331. TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false)
  332. {
  333. GLXContext ctx =
  334. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  335. False, NULL);
  336. ASSERT_NE((GLXContext) 0, ctx);
  337. struct glx_context *gc = (struct glx_context *) ctx;
  338. EXPECT_FALSE(gc->isDirect);
  339. }
  340. TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_direct_screen_and_direct_set_to_true)
  341. {
  342. this->use_direct_rendering_screen();
  343. GLXContext ctx =
  344. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  345. True, NULL);
  346. ASSERT_NE((GLXContext) 0, ctx);
  347. struct glx_context *gc = (struct glx_context *) ctx;
  348. EXPECT_TRUE(gc->isDirect);
  349. }
  350. TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_direct_screen_and_direct_set_to_false)
  351. {
  352. this->use_direct_rendering_screen();
  353. GLXContext ctx =
  354. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  355. False, NULL);
  356. ASSERT_NE((GLXContext) 0, ctx);
  357. struct glx_context *gc = (struct glx_context *) ctx;
  358. EXPECT_FALSE(gc->isDirect);
  359. }
  360. TEST_F(glXCreateContextAttribARB_test, correct_indirect_context_client_state_private)
  361. {
  362. GLXContext ctx =
  363. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  364. False, NULL);
  365. ASSERT_NE((GLXContext) 0, ctx);
  366. struct glx_context *gc = (struct glx_context *) ctx;
  367. ASSERT_FALSE(gc->isDirect);
  368. EXPECT_EQ((struct __GLXattributeRec *) 0xcafebabe,
  369. gc->client_state_private);
  370. }
  371. TEST_F(glXCreateContextAttribARB_test, correct_indirect_context_config)
  372. {
  373. GLXContext ctx =
  374. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  375. False, NULL);
  376. ASSERT_NE((GLXContext) 0, ctx);
  377. struct glx_context *gc = (struct glx_context *) ctx;
  378. EXPECT_EQ(&this->fbc, gc->config);
  379. }
  380. TEST_F(glXCreateContextAttribARB_test, correct_context_screen_number)
  381. {
  382. this->fbc.screen = 7;
  383. psc->scr = 7;
  384. GLXContext ctx =
  385. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  386. False, NULL);
  387. ASSERT_NE((GLXContext) 0, ctx);
  388. struct glx_context *gc = (struct glx_context *) ctx;
  389. EXPECT_EQ(7, gc->screen);
  390. }
  391. TEST_F(glXCreateContextAttribARB_test, correct_context_screen_pointer)
  392. {
  393. GLXContext ctx =
  394. glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
  395. False, NULL);
  396. ASSERT_NE((GLXContext) 0, ctx);
  397. struct glx_context *gc = (struct glx_context *) ctx;
  398. EXPECT_EQ(psc, gc->psc);
  399. }
  400. /*@}*/