~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/driver/gl/cogl.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, feature_flags)  \
 
149
  static const CoglFeatureFunction cogl_feature_ ## name ## _funcs[] = {
 
150
#define COGL_FEATURE_FUNCTION(ret, name, args)                          \
 
151
  { G_STRINGIFY (name), G_STRUCT_OFFSET (CoglContext, drv.pf_ ## name) },
 
152
#define COGL_FEATURE_END()                      \
 
153
  { NULL, 0 },                                  \
 
154
  };
 
155
#include "cogl-feature-functions.h"
 
156
 
 
157
/* Define an array of features */
 
158
#undef COGL_FEATURE_BEGIN
 
159
#define COGL_FEATURE_BEGIN(name, min_gl_major, min_gl_minor,            \
 
160
                           namespaces, extension_names, feature_flags)  \
 
161
  { min_gl_major, min_gl_minor, namespaces,                             \
 
162
      extension_names, feature_flags,                                   \
 
163
      cogl_feature_ ## name ## _funcs },
 
164
#undef COGL_FEATURE_FUNCTION
 
165
#define COGL_FEATURE_FUNCTION(ret, name, args)
 
166
#undef COGL_FEATURE_END
 
167
#define COGL_FEATURE_END()
 
168
 
 
169
static const CoglFeatureData cogl_feature_data[] =
 
170
  {
 
171
#include "cogl-feature-functions.h"
 
172
  };
 
173
 
 
174
void
 
175
_cogl_features_init (void)
 
176
{
 
177
  CoglFeatureFlags  flags = 0;
 
178
  const char       *gl_extensions;
 
179
  GLint             max_clip_planes = 0;
 
180
  GLint             num_stencil_bits = 0;
 
181
  int               gl_major = 0, gl_minor = 0;
 
182
  int               i;
 
183
 
 
184
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
185
 
 
186
  _cogl_get_gl_version (&gl_major, &gl_minor);
 
187
 
 
188
  flags = (COGL_FEATURE_TEXTURE_READ_PIXELS
 
189
           | COGL_FEATURE_UNSIGNED_INT_INDICES);
 
190
 
 
191
  gl_extensions = (const char*) glGetString (GL_EXTENSIONS);
 
192
 
 
193
  if (COGL_CHECK_GL_VERSION (gl_major, gl_minor, 2, 0) ||
 
194
      _cogl_check_extension ("GL_ARB_texture_non_power_of_two", gl_extensions))
 
195
    {
 
196
#ifdef HAVE_CLUTTER_OSX
 
197
      if (really_enable_npot ())
 
198
#endif
 
199
        flags |= COGL_FEATURE_TEXTURE_NPOT;
 
200
    }
 
201
 
 
202
#ifdef GL_YCBCR_MESA
 
203
  if (_cogl_check_extension ("GL_MESA_ycbcr_texture", gl_extensions))
 
204
    {
 
205
      flags |= COGL_FEATURE_TEXTURE_YUV;
 
206
    }
 
207
#endif
 
208
 
 
209
  GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) );
 
210
  /* We need at least three stencil bits to combine clips */
 
211
  if (num_stencil_bits > 2)
 
212
    flags |= COGL_FEATURE_STENCIL_BUFFER;
 
213
 
 
214
  GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) );
 
215
  if (max_clip_planes >= 4)
 
216
    flags |= COGL_FEATURE_FOUR_CLIP_PLANES;
 
217
 
 
218
  for (i = 0; i < G_N_ELEMENTS (cogl_feature_data); i++)
 
219
    if (_cogl_feature_check (cogl_feature_data + i,
 
220
                             gl_major, gl_minor,
 
221
                             gl_extensions))
 
222
        flags |= cogl_feature_data[i].feature_flags;
 
223
 
 
224
  /* Cache features */
 
225
  ctx->feature_flags = flags;
 
226
  ctx->features_cached = TRUE;
 
227
}