Opensource Compliance Issue.
[platform/core/graphics/cairo.git] / test / egl-surface-source.c
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 /*
28  * This case tests using a EGL surface as the source
29  *
30  */
31
32 #include "cairo-test.h"
33 #include <cairo-gl.h>
34
35 #include "surface-source.c"
36
37 struct closure {
38     EGLDisplay dpy;
39     EGLContext ctx;
40 };
41
42 static void
43 cleanup (void *data)
44 {
45     struct closure *arg = data;
46
47     eglDestroyContext (arg->dpy, arg->ctx);
48     eglMakeCurrent (arg->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
49     eglTerminate (arg->dpy);
50
51     free (arg);
52 }
53
54 static cairo_surface_t *
55 create_source_surface (int size)
56 {
57     EGLint config_attribs[] = {
58         EGL_RED_SIZE, 8,
59         EGL_GREEN_SIZE, 8,
60         EGL_BLUE_SIZE, 8,
61         EGL_ALPHA_SIZE, 8,
62         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
63 #if CAIRO_HAS_GL_SURFACE
64         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
65 #elif CAIRO_HAS_GLESV2_SURFACE
66         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
67 #endif
68         EGL_NONE
69     };
70     const EGLint ctx_attribs[] = {
71 #if CAIRO_HAS_GLESV2_SURFACE
72         EGL_CONTEXT_CLIENT_VERSION, 2,
73 #endif
74         EGL_NONE
75     };
76
77     struct closure *arg;
78     cairo_device_t *device;
79     cairo_surface_t *surface;
80     EGLConfig config;
81     EGLint numConfigs;
82     EGLDisplay dpy;
83     EGLContext ctx;
84     int major, minor;
85
86     dpy = eglGetDisplay (EGL_DEFAULT_DISPLAY);
87     if (! eglInitialize (dpy, &major, &minor)) {
88         return NULL;
89     }
90
91     eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs);
92     if (numConfigs == 0) {
93         return NULL;
94     }
95
96 #if CAIRO_HAS_GL_SURFACE
97     eglBindAPI (EGL_OPENGL_API);
98 #elif CAIRO_HAS_GLESV2_SURFACE
99     eglBindAPI (EGL_OPENGL_ES_API);
100 #endif
101
102    ctx = eglCreateContext (dpy, config, EGL_NO_CONTEXT,
103                                   ctx_attribs);
104     if (ctx == EGL_NO_CONTEXT) {
105         eglTerminate (dpy);
106         return NULL;
107     }
108
109     arg = xmalloc (sizeof (struct closure));
110     arg->dpy = dpy;
111     arg->ctx = ctx;
112     device = cairo_egl_device_create (dpy, ctx);
113     if (cairo_device_set_user_data (device,
114                                     (cairo_user_data_key_t *) cleanup,
115                                     arg,
116                                     cleanup))
117     {
118         cleanup (arg);
119         return NULL;
120     }
121
122     surface = cairo_gl_surface_create (device,
123                                        CAIRO_CONTENT_COLOR_ALPHA,
124                                        size, size);
125     cairo_device_destroy (device);
126
127     return surface;
128 }
129
130 CAIRO_TEST (egl_surface_source,
131             "Test using a EGL surface as the source",
132             "source", /* keywords */
133             NULL, /* requirements */
134             SIZE, SIZE,
135             preamble, draw)