egl-surface-source.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright © 2014 Samsung Electronics
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Ravi Nanjundappa <nravi.n@samsung.com>
  25. */
  26. /*
  27. * This case tests using a EGL surface as the source
  28. *
  29. */
  30. #include "cairo-test.h"
  31. #include <cairo-gl.h>
  32. #include "surface-source.c"
  33. struct closure {
  34. EGLDisplay dpy;
  35. EGLContext ctx;
  36. };
  37. static void
  38. cleanup (void *data)
  39. {
  40. struct closure *arg = data;
  41. eglDestroyContext (arg->dpy, arg->ctx);
  42. eglMakeCurrent (arg->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  43. eglTerminate (arg->dpy);
  44. free (arg);
  45. }
  46. static cairo_surface_t *
  47. create_source_surface (int size)
  48. {
  49. EGLint config_attribs[] = {
  50. EGL_RED_SIZE, 8,
  51. EGL_GREEN_SIZE, 8,
  52. EGL_BLUE_SIZE, 8,
  53. EGL_ALPHA_SIZE, 8,
  54. EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  55. #if CAIRO_HAS_GL_SURFACE
  56. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  57. #elif CAIRO_HAS_GLESV2_SURFACE
  58. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  59. #endif
  60. EGL_NONE
  61. };
  62. const EGLint ctx_attribs[] = {
  63. #if CAIRO_HAS_GLESV2_SURFACE
  64. EGL_CONTEXT_CLIENT_VERSION, 2,
  65. #endif
  66. EGL_NONE
  67. };
  68. struct closure *arg;
  69. cairo_device_t *device;
  70. cairo_surface_t *surface;
  71. EGLConfig config;
  72. EGLint numConfigs;
  73. EGLDisplay dpy;
  74. EGLContext ctx;
  75. int major, minor;
  76. dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
  77. if (! eglInitialize (dpy, &major, &minor)) {
  78. return NULL;
  79. }
  80. eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs);
  81. if (numConfigs == 0) {
  82. return NULL;
  83. }
  84. #if CAIRO_HAS_GL_SURFACE
  85. eglBindAPI (EGL_OPENGL_API);
  86. #elif CAIRO_HAS_GLESV2_SURFACE
  87. eglBindAPI (EGL_OPENGL_ES_API);
  88. #endif
  89. ctx = eglCreateContext (dpy, config, EGL_NO_CONTEXT,
  90. ctx_attribs);
  91. if (ctx == EGL_NO_CONTEXT) {
  92. eglTerminate (dpy);
  93. return NULL;
  94. }
  95. arg = xmalloc (sizeof (struct closure));
  96. arg->dpy = dpy;
  97. arg->ctx = ctx;
  98. device = cairo_egl_device_create (dpy, ctx);
  99. if (cairo_device_set_user_data (device,
  100. (cairo_user_data_key_t *) cleanup,
  101. arg,
  102. cleanup))
  103. {
  104. cleanup (arg);
  105. return NULL;
  106. }
  107. surface = cairo_gl_surface_create (device,
  108. CAIRO_CONTENT_COLOR_ALPHA,
  109. size, size);
  110. cairo_device_destroy (device);
  111. return surface;
  112. }
  113. CAIRO_TEST (egl_surface_source,
  114. "Test using a EGL surface as the source",
  115. "source", /* keywords */
  116. NULL, /* requirements */
  117. SIZE, SIZE,
  118. preamble, draw)