xcb-surface-source.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright © 2009 Chris Wilson
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Chris Wilson not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Chris Wilson makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Chris Wilson <chris@chris-wilson.co.uk>
  24. */
  25. #include "cairo-test.h"
  26. #if CAIRO_HAS_XCB_SURFACE
  27. #include <cairo-xcb.h>
  28. #endif
  29. #include "surface-source.c"
  30. #if CAIRO_HAS_XCB_SURFACE
  31. static cairo_user_data_key_t closure_key;
  32. struct closure {
  33. cairo_device_t *device;
  34. xcb_connection_t *connection;
  35. xcb_pixmap_t pixmap;
  36. };
  37. static void
  38. cleanup (void *data)
  39. {
  40. struct closure *arg = data;
  41. cairo_device_finish (arg->device);
  42. cairo_device_destroy (arg->device);
  43. xcb_free_pixmap (arg->connection, arg->pixmap);
  44. xcb_disconnect (arg->connection);
  45. free (arg);
  46. }
  47. static xcb_render_pictforminfo_t *
  48. find_depth (xcb_connection_t *connection, int depth, void **formats_out)
  49. {
  50. xcb_render_query_pict_formats_reply_t *formats;
  51. xcb_render_query_pict_formats_cookie_t cookie;
  52. xcb_render_pictforminfo_iterator_t i;
  53. cookie = xcb_render_query_pict_formats (connection);
  54. xcb_flush (connection);
  55. formats = xcb_render_query_pict_formats_reply (connection, cookie, 0);
  56. if (formats == NULL)
  57. return NULL;
  58. for (i = xcb_render_query_pict_formats_formats_iterator (formats);
  59. i.rem;
  60. xcb_render_pictforminfo_next (&i))
  61. {
  62. if (XCB_RENDER_PICT_TYPE_DIRECT != i.data->type)
  63. continue;
  64. if (depth != i.data->depth)
  65. continue;
  66. *formats_out = formats;
  67. return i.data;
  68. }
  69. free (formats);
  70. return NULL;
  71. }
  72. #endif
  73. static cairo_surface_t *
  74. create_source_surface (int size)
  75. {
  76. #if CAIRO_HAS_XCB_SURFACE
  77. xcb_connection_t *connection;
  78. xcb_render_pictforminfo_t *render_format;
  79. struct closure *data;
  80. cairo_surface_t *surface;
  81. xcb_screen_t *root;
  82. xcb_void_cookie_t cookie;
  83. void *formats;
  84. connection = xcb_connect (NULL, NULL);
  85. if (connection == NULL)
  86. return NULL;
  87. data = xmalloc (sizeof (struct closure));
  88. data->connection = connection;
  89. render_format = find_depth (connection, 32, &formats);
  90. if (render_format == NULL) {
  91. xcb_disconnect (connection);
  92. free (data);
  93. return NULL;
  94. }
  95. root = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;
  96. data->pixmap = xcb_generate_id (connection);
  97. cookie = xcb_create_pixmap_checked (connection, 32,
  98. data->pixmap, root->root, size, size);
  99. /* slow, but sure */
  100. if (xcb_request_check (connection, cookie) != NULL) {
  101. free (formats);
  102. xcb_disconnect (connection);
  103. free (data);
  104. return NULL;
  105. }
  106. surface = cairo_xcb_surface_create_with_xrender_format (connection,
  107. root,
  108. data->pixmap,
  109. render_format,
  110. size, size);
  111. free (formats);
  112. data->device = cairo_device_reference (cairo_surface_get_device (surface));
  113. cairo_surface_set_user_data (surface, &closure_key, data, cleanup);
  114. return surface;
  115. #else
  116. return NULL;
  117. #endif
  118. }
  119. CAIRO_TEST (xcb_surface_source,
  120. "Test using a XCB surface as the source",
  121. "source", /* keywords */
  122. NULL, /* requirements */
  123. SIZE, SIZE,
  124. preamble, draw)