cally: Do not use deprecated functions
[profile/ivi/clutter.git] / clutter / cogl / cogl / driver / gl / cogl-gl.c
1 /*
2  * Cogl
3  *
4  * An object oriented GL/GLES Abstraction/Utility Layer
5  *
6  * Copyright (C) 2007,2008,2009 Intel Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <string.h>
29
30 #include "cogl.h"
31
32 #include "cogl-internal.h"
33 #include "cogl-context.h"
34 #include "cogl-feature-private.h"
35
36 #ifdef HAVE_CLUTTER_OSX
37 static gboolean
38 really_enable_npot (void)
39 {
40   /* OSX backend + ATI Radeon X1600 + NPOT texture + GL_REPEAT seems to crash
41    * http://bugzilla.openedhand.com/show_bug.cgi?id=929
42    *
43    * Temporary workaround until post 0.8 we rejig the features set up a
44    * little to allow the backend to overide.
45    */
46   const char *gl_renderer;
47   const char *env_string;
48
49   /* Regardless of hardware, allow user to decide. */
50   env_string = g_getenv ("COGL_ENABLE_NPOT");
51   if (env_string != NULL)
52     return env_string[0] == '1';
53
54   gl_renderer = (char*)glGetString (GL_RENDERER);
55   if (strstr (gl_renderer, "ATI Radeon X1600") != NULL)
56     return FALSE;
57
58   return TRUE;
59 }
60 #endif
61
62 static gboolean
63 _cogl_get_gl_version (int *major_out, int *minor_out)
64 {
65   const char *version_string, *major_end, *minor_end;
66   int major = 0, minor = 0;
67
68   /* Get the OpenGL version number */
69   if ((version_string = (const char *) glGetString (GL_VERSION)) == NULL)
70     return FALSE;
71
72   /* Extract the major number */
73   for (major_end = version_string; *major_end >= '0'
74          && *major_end <= '9'; major_end++)
75     major = (major * 10) + *major_end - '0';
76   /* If there were no digits or the major number isn't followed by a
77      dot then it is invalid */
78   if (major_end == version_string || *major_end != '.')
79     return FALSE;
80
81   /* Extract the minor number */
82   for (minor_end = major_end + 1; *minor_end >= '0'
83          && *minor_end <= '9'; minor_end++)
84     minor = (minor * 10) + *minor_end - '0';
85   /* If there were no digits or there is an unexpected character then
86      it is invalid */
87   if (minor_end == major_end + 1
88       || (*minor_end && *minor_end != ' ' && *minor_end != '.'))
89     return FALSE;
90
91   *major_out = major;
92   *minor_out = minor;
93
94   return TRUE;
95 }
96
97 gboolean
98 _cogl_check_driver_valid (GError **error)
99 {
100   int major, minor;
101   const char *gl_extensions;
102
103   if (!_cogl_get_gl_version (&major, &minor))
104     {
105       g_set_error (error,
106                    COGL_DRIVER_ERROR,
107                    COGL_DRIVER_ERROR_UNKNOWN_VERSION,
108                    "The OpenGL version could not be determined");
109       return FALSE;
110     }
111
112   /* GL 1.3 supports all of the required functionality in core */
113   if (COGL_CHECK_GL_VERSION (major, minor, 1, 3))
114     return TRUE;
115
116   gl_extensions = (const char*) glGetString (GL_EXTENSIONS);
117
118   /* OpenGL 1.2 is only supported if we have the multitexturing
119      extension */
120   if (!_cogl_check_extension ("GL_ARB_multitexture", gl_extensions))
121     {
122       g_set_error (error,
123                    COGL_DRIVER_ERROR,
124                    COGL_DRIVER_ERROR_INVALID_VERSION,
125                    "The OpenGL driver is missing "
126                    "the GL_ARB_multitexture extension");
127       return FALSE;
128     }
129
130   /* OpenGL 1.2 is required */
131   if (!COGL_CHECK_GL_VERSION (major, minor, 1, 2))
132     {
133       g_set_error (error,
134                    COGL_DRIVER_ERROR,
135                    COGL_DRIVER_ERROR_INVALID_VERSION,
136                    "The OpenGL version of your driver (%i.%i) "
137                    "is not compatible with Cogl",
138                    major, minor);
139       return FALSE;
140     }
141
142   return TRUE;
143 }
144
145 /* Define a set of arrays containing the functions required from GL
146    for each feature */
147 #define COGL_FEATURE_BEGIN(name, min_gl_major, min_gl_minor,            \
148                            namespaces, extension_names,                 \
149                            feature_flags, feature_flags_private)        \
150   static const CoglFeatureFunction cogl_feature_ ## name ## _funcs[] = {
151 #define COGL_FEATURE_FUNCTION(ret, name, args)                          \
152   { G_STRINGIFY (name), G_STRUCT_OFFSET (CoglContext, drv.pf_ ## name) },
153 #define COGL_FEATURE_END()                      \
154   { NULL, 0 },                                  \
155   };
156 #include "cogl-feature-functions-gl.h"
157
158 /* Define an array of features */
159 #undef COGL_FEATURE_BEGIN
160 #define COGL_FEATURE_BEGIN(name, min_gl_major, min_gl_minor,            \
161                            namespaces, extension_names,                 \
162                            feature_flags, feature_flags_private)        \
163   { min_gl_major, min_gl_minor, namespaces,                             \
164       extension_names, feature_flags, feature_flags_private,             \
165       cogl_feature_ ## name ## _funcs },
166 #undef COGL_FEATURE_FUNCTION
167 #define COGL_FEATURE_FUNCTION(ret, name, args)
168 #undef COGL_FEATURE_END
169 #define COGL_FEATURE_END()
170
171 static const CoglFeatureData cogl_feature_data[] =
172   {
173 #include "cogl-feature-functions-gl.h"
174   };
175
176 void
177 _cogl_features_init (void)
178 {
179   CoglFeatureFlags         flags = 0;
180   CoglFeatureFlagsPrivate  flags_private = 0;
181   const char              *gl_extensions;
182   GLint                    max_clip_planes = 0;
183   GLint                    num_stencil_bits = 0;
184   int                      gl_major = 0, gl_minor = 0;
185   int                      i;
186
187   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
188
189   _cogl_get_gl_version (&gl_major, &gl_minor);
190
191   flags = (COGL_FEATURE_TEXTURE_READ_PIXELS
192            | COGL_FEATURE_UNSIGNED_INT_INDICES);
193
194   gl_extensions = (const char*) glGetString (GL_EXTENSIONS);
195
196   if (COGL_CHECK_GL_VERSION (gl_major, gl_minor, 2, 0) ||
197       _cogl_check_extension ("GL_ARB_texture_non_power_of_two", gl_extensions))
198     {
199 #ifdef HAVE_CLUTTER_OSX
200       if (really_enable_npot ())
201 #endif
202         flags |= COGL_FEATURE_TEXTURE_NPOT
203           | COGL_FEATURE_TEXTURE_NPOT_BASIC
204           | COGL_FEATURE_TEXTURE_NPOT_MIPMAP
205           | COGL_FEATURE_TEXTURE_NPOT_REPEAT;
206     }
207
208 #ifdef GL_YCBCR_MESA
209   if (_cogl_check_extension ("GL_MESA_ycbcr_texture", gl_extensions))
210     {
211       flags |= COGL_FEATURE_TEXTURE_YUV;
212     }
213 #endif
214
215   GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) );
216   /* We need at least three stencil bits to combine clips */
217   if (num_stencil_bits > 2)
218     flags |= COGL_FEATURE_STENCIL_BUFFER;
219
220   GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) );
221   if (max_clip_planes >= 4)
222     flags |= COGL_FEATURE_FOUR_CLIP_PLANES;
223
224   for (i = 0; i < G_N_ELEMENTS (cogl_feature_data); i++)
225     if (_cogl_feature_check ("GL", cogl_feature_data + i,
226                              gl_major, gl_minor,
227                              gl_extensions))
228       {
229         flags |= cogl_feature_data[i].feature_flags;
230         flags_private |= cogl_feature_data[i].feature_flags_private;
231       }
232
233   /* Cache features */
234   ctx->feature_flags = flags;
235   ctx->feature_flags_private = flags_private;
236   ctx->features_cached = TRUE;
237 }