~ubuntu-branches/ubuntu/precise/clutter-1.0/precise

« back to all changes in this revision

Viewing changes to clutter/cogl/gl/cogl-context.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental) (1.3.1 upstream)
  • 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, write to the
20
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
 
 * Boston, MA 02111-1307, USA.
22
 
 */
23
 
 
24
 
#ifdef HAVE_CONFIG_H
25
 
#include "config.h"
26
 
#endif
27
 
 
28
 
#include "cogl.h"
29
 
#include "cogl-internal.h"
30
 
#include "cogl-util.h"
31
 
#include "cogl-context.h"
32
 
#include "cogl-texture-private.h"
33
 
#include "cogl-material-private.h"
34
 
 
35
 
#include <string.h>
36
 
 
37
 
static CoglContext *_context = NULL;
38
 
static gboolean gl_is_indirect = FALSE;
39
 
 
40
 
static gboolean
41
 
cogl_create_context ()
42
 
{
43
 
  GLubyte default_texture_data[] = { 0xff, 0xff, 0xff, 0x0 };
44
 
  gulong  enable_flags = 0;
45
 
  CoglDrawBufferState *draw_buffer;
46
 
 
47
 
  if (_context != NULL)
48
 
    return FALSE;
49
 
 
50
 
  /* Allocate context memory */
51
 
  _context = (CoglContext*) g_malloc (sizeof (CoglContext));
52
 
 
53
 
  /* Init default values */
54
 
  _context->feature_flags = 0;
55
 
  _context->features_cached = FALSE;
56
 
 
57
 
  _context->enable_flags = 0;
58
 
  _context->color_alpha = 0;
59
 
 
60
 
  _context->enable_backface_culling = FALSE;
61
 
 
62
 
  _context->indirect = gl_is_indirect;
63
 
 
64
 
  _context->default_material = cogl_material_new ();
65
 
  _context->source_material = NULL;
66
 
 
67
 
  _context->default_gl_texture_2d_tex = COGL_INVALID_HANDLE;
68
 
  _context->default_gl_texture_rect_tex = COGL_INVALID_HANDLE;
69
 
 
70
 
  _context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry));
71
 
  _context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat));
72
 
 
73
 
  _context->current_material = NULL;
74
 
  _context->current_material_flags = 0;
75
 
  memset (&_context->current_material_flush_options,
76
 
          0, sizeof (CoglMaterialFlushOptions));
77
 
  _context->current_layers = g_array_new (FALSE, FALSE,
78
 
                                          sizeof (CoglLayerInfo));
79
 
  _context->n_texcoord_arrays_enabled = 0;
80
 
 
81
 
  draw_buffer = g_slice_new0 (CoglDrawBufferState);
82
 
  draw_buffer->target = COGL_WINDOW_BUFFER;
83
 
  draw_buffer->offscreen = COGL_INVALID_HANDLE;
84
 
  _context->draw_buffer_stack =
85
 
    g_slist_prepend (NULL, draw_buffer);
86
 
 
87
 
  _context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode));
88
 
  _context->last_path = 0;
89
 
  _context->stencil_material = cogl_material_new ();
90
 
 
91
 
  _context->in_begin_gl_block = FALSE;
92
 
 
93
 
  _context->viewport_width = 0;
94
 
  _context->viewport_height = 0;
95
 
 
96
 
  _context->pf_glGenRenderbuffersEXT = NULL;
97
 
  _context->pf_glBindRenderbufferEXT = NULL;
98
 
  _context->pf_glRenderbufferStorageEXT = NULL;
99
 
  _context->pf_glGenFramebuffersEXT = NULL;
100
 
  _context->pf_glBindFramebufferEXT = NULL;
101
 
  _context->pf_glFramebufferTexture2DEXT = NULL;
102
 
  _context->pf_glFramebufferRenderbufferEXT = NULL;
103
 
  _context->pf_glCheckFramebufferStatusEXT = NULL;
104
 
  _context->pf_glDeleteFramebuffersEXT = NULL;
105
 
  _context->pf_glBlitFramebufferEXT = NULL;
106
 
  _context->pf_glRenderbufferStorageMultisampleEXT = NULL;
107
 
 
108
 
  _context->pf_glCreateProgramObjectARB = NULL;
109
 
  _context->pf_glCreateShaderObjectARB = NULL;
110
 
  _context->pf_glShaderSourceARB = NULL;
111
 
  _context->pf_glCompileShaderARB = NULL;
112
 
  _context->pf_glAttachObjectARB = NULL;
113
 
  _context->pf_glLinkProgramARB = NULL;
114
 
  _context->pf_glUseProgramObjectARB = NULL;
115
 
  _context->pf_glGetUniformLocationARB = NULL;
116
 
  _context->pf_glDeleteObjectARB = NULL;
117
 
  _context->pf_glGetInfoLogARB = NULL;
118
 
  _context->pf_glGetObjectParameterivARB = NULL;
119
 
  _context->pf_glUniform1fARB = NULL;
120
 
  _context->pf_glUniform2fARB = NULL;
121
 
  _context->pf_glUniform3fARB = NULL;
122
 
  _context->pf_glUniform4fARB = NULL;
123
 
  _context->pf_glUniform1fvARB = NULL;
124
 
  _context->pf_glUniform2fvARB = NULL;
125
 
  _context->pf_glUniform3fvARB = NULL;
126
 
  _context->pf_glUniform4fvARB = NULL;
127
 
  _context->pf_glUniform1iARB = NULL;
128
 
  _context->pf_glUniform2iARB = NULL;
129
 
  _context->pf_glUniform3iARB = NULL;
130
 
  _context->pf_glUniform4iARB = NULL;
131
 
  _context->pf_glUniform1ivARB = NULL;
132
 
  _context->pf_glUniform2ivARB = NULL;
133
 
  _context->pf_glUniform3ivARB = NULL;
134
 
  _context->pf_glUniform4ivARB = NULL;
135
 
  _context->pf_glUniformMatrix2fvARB = NULL;
136
 
  _context->pf_glUniformMatrix3fvARB = NULL;
137
 
  _context->pf_glUniformMatrix4fvARB = NULL;
138
 
 
139
 
  _context->pf_glDrawRangeElements = NULL;
140
 
  _context->pf_glActiveTexture = NULL;
141
 
  _context->pf_glClientActiveTexture = NULL;
142
 
 
143
 
  _context->pf_glBlendFuncSeparate = NULL;
144
 
  _context->pf_glBlendEquationSeparate = NULL;
145
 
 
146
 
  /* Initialise the clip stack */
147
 
  _cogl_clip_stack_state_init ();
148
 
 
149
 
  /* Initialise matrix stack */
150
 
  _cogl_current_matrix_state_init ();
151
 
 
152
 
  /* Create default textures used for fall backs */
153
 
  _context->default_gl_texture_2d_tex =
154
 
    cogl_texture_new_from_data (1, /* width */
155
 
                                1, /* height */
156
 
                                COGL_TEXTURE_NO_SLICING,
157
 
                                COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* data format */
158
 
                                /* internal format */
159
 
                                COGL_PIXEL_FORMAT_RGBA_8888_PRE,
160
 
                                0, /* auto calc row stride */
161
 
                                default_texture_data);
162
 
  _context->default_gl_texture_rect_tex =
163
 
    cogl_texture_new_from_data (1, /* width */
164
 
                                1, /* height */
165
 
                                COGL_TEXTURE_NO_SLICING,
166
 
                                COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* data format */
167
 
                                /* internal format */
168
 
                                COGL_PIXEL_FORMAT_RGBA_8888_PRE,
169
 
                                0, /* auto calc row stride */
170
 
                                default_texture_data);
171
 
 
172
 
  cogl_set_source (_context->default_material);
173
 
  _cogl_material_flush_gl_state (_context->source_material, NULL);
174
 
  enable_flags =
175
 
    _cogl_material_get_cogl_enable_flags (_context->source_material);
176
 
  cogl_enable (enable_flags);
177
 
 
178
 
  _context->quad_indices_byte = COGL_INVALID_HANDLE;
179
 
  _context->quad_indices_short = COGL_INVALID_HANDLE;
180
 
  _context->quad_indices_short_len = 0;
181
 
 
182
 
  return TRUE;
183
 
}
184
 
 
185
 
void
186
 
_cogl_destroy_context ()
187
 
{
188
 
  if (_context == NULL)
189
 
    return;
190
 
 
191
 
  _cogl_clip_stack_state_destroy ();
192
 
 
193
 
  _cogl_current_matrix_state_destroy ();
194
 
 
195
 
  if (_context->path_nodes)
196
 
    g_array_free (_context->path_nodes, TRUE);
197
 
 
198
 
  if (_context->default_gl_texture_2d_tex)
199
 
    cogl_handle_unref (_context->default_gl_texture_2d_tex);
200
 
  if (_context->default_gl_texture_rect_tex)
201
 
    cogl_handle_unref (_context->default_gl_texture_rect_tex);
202
 
 
203
 
  if (_context->default_material)
204
 
    cogl_handle_unref (_context->default_material);
205
 
 
206
 
  if (_context->journal)
207
 
    g_array_free (_context->journal, TRUE);
208
 
  if (_context->logged_vertices)
209
 
    g_array_free (_context->logged_vertices, TRUE);
210
 
 
211
 
  if (_context->current_layers)
212
 
    g_array_free (_context->current_layers, TRUE);
213
 
 
214
 
  if (_context->quad_indices_byte)
215
 
    cogl_handle_unref (_context->quad_indices_byte);
216
 
  if (_context->quad_indices_short)
217
 
    cogl_handle_unref (_context->quad_indices_short);
218
 
 
219
 
  g_free (_context);
220
 
}
221
 
 
222
 
CoglContext *
223
 
_cogl_context_get_default ()
224
 
{
225
 
  /* Create if doesn't exist yet */
226
 
  if (_context == NULL)
227
 
    cogl_create_context ();
228
 
 
229
 
  return _context;
230
 
}
231
 
 
232
 
/**
233
 
 * _cogl_set_indirect_context:
234
 
 * @indirect: TRUE if GL context is indirect
235
 
 *
236
 
 * Advises COGL that the GL context is indirect (commands are sent
237
 
 * over a socket). COGL uses this information to try to avoid
238
 
 * round-trips in its use of GL, for example.
239
 
 *
240
 
 * This function cannot be called "on the fly," only before COGL
241
 
 * initializes.
242
 
 */
243
 
void
244
 
_cogl_set_indirect_context (gboolean indirect)
245
 
{
246
 
  /* we get called multiple times if someone creates
247
 
   * more than the default stage
248
 
   */
249
 
  if (_context != NULL)
250
 
    {
251
 
      if (indirect != _context->indirect)
252
 
        g_warning ("Right now all stages will be treated as "
253
 
                   "either direct or indirect, ignoring attempt "
254
 
                   "to change to indirect=%d", indirect);
255
 
      return;
256
 
    }
257
 
 
258
 
  gl_is_indirect = indirect;
259
 
}