xlib-surface-source.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright © 2008 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. #include <cairo-xlib.h>
  27. #if CAIRO_HAS_XLIB_XRENDER_SURFACE
  28. #include <cairo-xlib-xrender.h>
  29. #endif
  30. #include "surface-source.c"
  31. static cairo_user_data_key_t closure_key;
  32. struct closure {
  33. cairo_device_t *device;
  34. Display *dpy;
  35. Pixmap pix;
  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. XFreePixmap (arg->dpy, arg->pix);
  44. XCloseDisplay (arg->dpy);
  45. free (arg);
  46. }
  47. static cairo_surface_t *
  48. create_source_surface (int size)
  49. {
  50. #if CAIRO_HAS_XLIB_XRENDER_SURFACE
  51. XRenderPictFormat *xrender_format;
  52. struct closure *data;
  53. cairo_surface_t *surface;
  54. data = xmalloc (sizeof (struct closure));
  55. data->dpy = XOpenDisplay (NULL);
  56. if (!data->dpy) {
  57. return NULL;
  58. }
  59. xrender_format = XRenderFindStandardFormat (data->dpy, PictStandardARGB32);
  60. data->pix = XCreatePixmap (data->dpy, DefaultRootWindow (data->dpy),
  61. size, size, xrender_format->depth);
  62. surface = cairo_xlib_surface_create_with_xrender_format (data->dpy,
  63. data->pix,
  64. DefaultScreenOfDisplay (data->dpy),
  65. xrender_format,
  66. size, size);
  67. data->device = cairo_device_reference (cairo_surface_get_device (surface));
  68. if (cairo_surface_set_user_data (surface, &closure_key, data, cleanup)) {
  69. cairo_surface_finish (surface);
  70. cairo_surface_destroy (surface);
  71. cleanup (data);
  72. return NULL;
  73. }
  74. return surface;
  75. #else
  76. return NULL;
  77. #endif
  78. }
  79. CAIRO_TEST (xlib_surface_source,
  80. "Test using a Xlib surface as the source",
  81. "source", /* keywords */
  82. NULL, /* requirements */
  83. SIZE, SIZE,
  84. preamble, draw)