summaryrefslogtreecommitdiff
path: root/legacy/evas/src/modules/engines/gl_x11/evas_x_main.c
blob: 5571468d5bb24ab285821d13411a125b773c7d2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "evas_engine.h"

static Evas_GL_X11_Window *_evas_gl_x11_window = NULL;

#if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
static EGLContext context = EGL_NO_CONTEXT;
#else
// FIXME: this will only work for 1 display connection (glx land can have > 1)
static GLXContext context = NULL;
#endif

XVisualInfo *_evas_gl_x11_vi = NULL;
Colormap     _evas_gl_x11_cmap = 0;

Evas_GL_X11_Window *
eng_window_new(Display *disp,
	       Window   win,
	       int      screen,
	       Visual  *vis,
	       Colormap cmap,
	       int      depth,
	       int      w,
	       int      h)
{
   Evas_GL_X11_Window *gw;
   int context_attrs[3];
   int config_attrs[20];
   int major_version, minor_version;
   int num_config;
   
   if (!_evas_gl_x11_vi) return NULL;
   
   gw = calloc(1, sizeof(Evas_GL_X11_Window));
   if (!gw) return NULL;
   gw->disp = disp;
   gw->win = win;
   gw->screen = screen;
   gw->visual = vis;
   gw->colormap = cmap;
   gw->depth = depth;

   gw->visualinfo = _evas_gl_x11_vi;

// EGL / GLES
#if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
   context_attrs[0] = EGL_CONTEXT_CLIENT_VERSION;
   context_attrs[1] = 2;
   context_attrs[2] = EGL_NONE;
# if defined(GLES_VARIETY_S3C6410)
   if (_evas_gles_x11_vi->depth == 16) // 16bpp
     {
        config_attrs[0] = EGL_SURFACE_TYPE;
        config_attrs[1] = EGL_WINDOW_BIT;
        config_attrs[2] = EGL_RENDERABLE_TYPE;
        config_attrs[3] = EGL_OPENGL_ES2_BIT;
        config_attrs[4] = EGL_RED_SIZE;
        config_attrs[5] = 5;
        config_attrs[6] = EGL_GREEN_SIZE;
        config_attrs[7] = 6;
        config_attrs[8] = EGL_BLUE_SIZE;
        config_attrs[9] = 5;
        config_attrs[10] = EGL_NONE;
     }
   else // 24/32bit. no one does 8bpp anymore. and 15bpp... dead
     {
        config_attrs[0] = EGL_SURFACE_TYPE;
        config_attrs[1] = EGL_WINDOW_BIT;
        config_attrs[2] = EGL_RENDERABLE_TYPE;
        config_attrs[3] = EGL_OPENGL_ES2_BIT;
        config_attrs[4] = EGL_RED_SIZE;
        config_attrs[5] = 8;
        config_attrs[6] = EGL_GREEN_SIZE;
        config_attrs[7] = 8;
        config_attrs[8] = EGL_BLUE_SIZE;
        config_attrs[9] = 8;
        config_attrs[10] = EGL_DEPTH_SIZE;
        config_attrs[11] = 24;
        config_attrs[12] = EGL_NONE;
     }
# elif defined(GLES_VARIETY_SGX)
   context_attrs[0] = EGL_CONTEXT_CLIENT_VERSION;
   context_attrs[1] = 2;
   context_attrs[2] = EGL_NONE;
   
   config_attrs[0] = EGL_SURFACE_TYPE;
   config_attrs[1] = EGL_WINDOW_BIT;
   config_attrs[2] = EGL_RENDERABLE_TYPE;
   config_attrs[3] = EGL_OPENGL_ES2_BIT;
   config_attrs[4] = EGL_NONE;
# endif
   gw->egl_disp= eglGetDisplay((EGLNativeDisplayType)(gw->disp));
   if (!gw->egl_disp)
     {
        printf("Error: eglGetDisplay() fail.\n");
     }
   if (!eglInitialize(gw->egl_disp, &major_version, &minor_version))
     {
        printf("Error: eglInitialize() fail.\n");
     }
   eglBindAPI(EGL_OPENGL_ES_API);
   if (eglGetError() != EGL_SUCCESS)
     {
        printf("Error: eglBindAPI() fail.\n");
     }
   if (!eglChooseConfig(gw->egl_disp, config_attrs, &gw->egl_config,
                        1, &num_config) || (num_config != 1))
     {
        printf("Error: eglChooseConfig() fail.\n");
     }
   gw->egl_surface[0] = eglCreateWindowSurface(gw->egl_disp, gw->egl_config,
                                               (EGLNativeWindowType)gw->win,
                                               NULL);
   if (gw->egl_surface[0] == EGL_NO_SURFACE)
     {
        printf("Error: eglCreateWindowSurface() fail.\n", gw->win);
     }
   if (context == EGL_NO_CONTEXT)
     context = eglCreateContext(gw->egl_disp, gw->egl_config, NULL, 
                                context_attrs);
   gw->egl_context[0] = context;
   if (gw->egl_context[0] == EGL_NO_CONTEXT)
     {
        printf("Error: eglCreateContext() fail.\n");
     }
   if (eglMakeCurrent(gw->egl_disp, 
                      gw->egl_surface[0], 
                      gw->egl_surface[0],
                      gw->egl_context[0]) == EGL_FALSE)
     {
        printf("Error: eglMakeCurrent() fail.\n");
     }
// GLX   
#else
   if (!context)
     context = glXCreateContext(disp, gw->visualinfo, NULL, GL_TRUE);
   gw->context = context;
   glXMakeCurrent(gw->disp, gw->win, gw->context);
#endif
   
   gw->gl_context = evas_gl_common_context_new();
   if (!gw->gl_context)
     {
	free(gw);
	return NULL;
     }
   evas_gl_common_context_resize(gw->gl_context, w, h);
   return gw;
}

void
eng_window_free(Evas_GL_X11_Window *gw)
{
   if (gw == _evas_gl_x11_window) _evas_gl_x11_window = NULL;
   evas_gl_common_context_free(gw->gl_context);
#if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
   if (gw->egl_surface[0] != EGL_NO_SURFACE)
     eglDestroySurface(gw->egl_disp, gw->egl_surface[0]);
#else   
   // FIXME: refcount context   
   //   glXDestroyContext(gw->disp, gw->context);
#endif
   free(gw);
}

void
eng_window_use(Evas_GL_X11_Window *gw)
{
   if (_evas_gl_x11_window != gw)
     {
	_evas_gl_x11_window = gw;
// EGL / GLES
#if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
        if (eglMakeCurrent(gw->egl_disp, 
                           gw->egl_surface[0], 
                           gw->egl_surface[0],
                           gw->egl_context[0]) == EGL_FALSE)
          {
             printf("Error: eglMakeCurrent() failed!\n");
          }
// GLX        
#else
        glXMakeCurrent(gw->disp, gw->win, gw->context);
#endif
     }
   evas_gl_common_context_use(gw->gl_context);
}

Visual *
eng_best_visual_get(Display *disp, int screen)
{
   if (!disp) return NULL;
   if (!_evas_gl_x11_vi)
     {
// EGL / GLES
#if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
        int depth = DefaultDepth(disp, screen);
        _evas_gl_x11_vi = calloc(1, sizeof(XVisualInfo));
        XMatchVisualInfo(disp, screen, depth, TrueColor, _evas_gl_x11_vi);
// GLX
#else
        int _evas_gl_x11_configuration[9] =
          {
             GLX_DOUBLEBUFFER,
               GLX_RGBA,
               GLX_RED_SIZE,   1,
               GLX_GREEN_SIZE, 1,
               GLX_BLUE_SIZE,  1,
               None
          };
        _evas_gl_x11_vi = glXChooseVisual(disp, screen,
                                          _evas_gl_x11_configuration);
#endif
     }
   if (!_evas_gl_x11_vi) return NULL;
   return _evas_gl_x11_vi->visual;
}

Colormap
eng_best_colormap_get(Display *disp, int screen)
{
   if (!disp) return 0;
   if (!_evas_gl_x11_vi) eng_best_visual_get(disp, screen);
   if (!_evas_gl_x11_vi) return 0;
   if (!_evas_gl_x11_cmap)
     {
        _evas_gl_x11_cmap = XCreateColormap(disp, 
                                            RootWindow(disp, screen),
                                            _evas_gl_x11_vi->visual, 
                                            0);
     }
   return _evas_gl_x11_cmap;
}
                                 
int
eng_best_depth_get(Display *disp, int screen)
{
   if (!disp) return 0;
   if (!_evas_gl_x11_vi) eng_best_visual_get(disp, screen);
   if (!_evas_gl_x11_vi) return 0;
   return _evas_gl_x11_vi->depth;
}