4
* An object oriented GL/GLES Abstraction/Utility Layer
6
* Copyright (C) 2007,2008,2009 Intel Corporation.
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.
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.
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.
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"
37
static CoglContext *_context = NULL;
38
static gboolean gl_is_indirect = FALSE;
41
cogl_create_context ()
43
GLubyte default_texture_data[] = { 0xff, 0xff, 0xff, 0x0 };
44
gulong enable_flags = 0;
45
CoglDrawBufferState *draw_buffer;
50
/* Allocate context memory */
51
_context = (CoglContext*) g_malloc (sizeof (CoglContext));
53
/* Init default values */
54
_context->feature_flags = 0;
55
_context->features_cached = FALSE;
57
_context->enable_flags = 0;
58
_context->color_alpha = 0;
60
_context->enable_backface_culling = FALSE;
62
_context->indirect = gl_is_indirect;
64
_context->default_material = cogl_material_new ();
65
_context->source_material = NULL;
67
_context->default_gl_texture_2d_tex = COGL_INVALID_HANDLE;
68
_context->default_gl_texture_rect_tex = COGL_INVALID_HANDLE;
70
_context->journal = g_array_new (FALSE, FALSE, sizeof (CoglJournalEntry));
71
_context->logged_vertices = g_array_new (FALSE, FALSE, sizeof (GLfloat));
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;
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);
87
_context->path_nodes = g_array_new (FALSE, FALSE, sizeof (CoglPathNode));
88
_context->last_path = 0;
89
_context->stencil_material = cogl_material_new ();
91
_context->in_begin_gl_block = FALSE;
93
_context->viewport_width = 0;
94
_context->viewport_height = 0;
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;
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;
139
_context->pf_glDrawRangeElements = NULL;
140
_context->pf_glActiveTexture = NULL;
141
_context->pf_glClientActiveTexture = NULL;
143
_context->pf_glBlendFuncSeparate = NULL;
144
_context->pf_glBlendEquationSeparate = NULL;
146
/* Initialise the clip stack */
147
_cogl_clip_stack_state_init ();
149
/* Initialise matrix stack */
150
_cogl_current_matrix_state_init ();
152
/* Create default textures used for fall backs */
153
_context->default_gl_texture_2d_tex =
154
cogl_texture_new_from_data (1, /* width */
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 */
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);
172
cogl_set_source (_context->default_material);
173
_cogl_material_flush_gl_state (_context->source_material, NULL);
175
_cogl_material_get_cogl_enable_flags (_context->source_material);
176
cogl_enable (enable_flags);
178
_context->quad_indices_byte = COGL_INVALID_HANDLE;
179
_context->quad_indices_short = COGL_INVALID_HANDLE;
180
_context->quad_indices_short_len = 0;
186
_cogl_destroy_context ()
188
if (_context == NULL)
191
_cogl_clip_stack_state_destroy ();
193
_cogl_current_matrix_state_destroy ();
195
if (_context->path_nodes)
196
g_array_free (_context->path_nodes, TRUE);
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);
203
if (_context->default_material)
204
cogl_handle_unref (_context->default_material);
206
if (_context->journal)
207
g_array_free (_context->journal, TRUE);
208
if (_context->logged_vertices)
209
g_array_free (_context->logged_vertices, TRUE);
211
if (_context->current_layers)
212
g_array_free (_context->current_layers, TRUE);
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);
223
_cogl_context_get_default ()
225
/* Create if doesn't exist yet */
226
if (_context == NULL)
227
cogl_create_context ();
233
* _cogl_set_indirect_context:
234
* @indirect: TRUE if GL context is indirect
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.
240
* This function cannot be called "on the fly," only before COGL
244
_cogl_set_indirect_context (gboolean indirect)
246
/* we get called multiple times if someone creates
247
* more than the default stage
249
if (_context != NULL)
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);
258
gl_is_indirect = indirect;