Make log related global variables as local
[platform/core/uifw/coregl.git] / src / modules / coregl_fastpath.c
1 #include "coregl_fastpath.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include <sys/types.h>
8 #include <unistd.h>
9
10
11 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST)     RET_TYPE (*_orig_fastpath_##FUNC_NAME) PARAM_LIST = NULL;
12 # include "../headers/sym_gl1.h"
13 # include "../headers/sym_gl2.h"
14 # include "../headers/sym_gl_common.h"
15 # include "../headers/sym_egl.h"
16 #undef _COREGL_SYMBOL
17
18 Fastpath_Opt_Flag   fp_opt = FP_UNKNOWN_PATH;
19
20 int                 debug_nofp = 0;
21 FILE               *trace_fp = NULL;
22
23 GLenum              FPGL_Error = GL_NO_ERROR;
24
25 GLGlueContext_List *gctx_list = NULL;
26
27 Mutex               init_context_mutex = MUTEX_INITIALIZER;
28 GLGlueContext      *initial_ctx = NULL;
29
30 Mutex               ctx_list_access_mutex = MUTEX_INITIALIZER;
31
32 GLContext_List     *glctx_list = NULL;
33 static int          api_gl_version = COREGL_GLAPI_2;
34
35 static void
36 _state_get_texture_states(GLenum pname, GLint *params)
37 {
38         GLuint cur_active_tex = 0;
39
40         AST(initial_ctx != NULL);
41
42         _orig_fastpath_glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&cur_active_tex);
43         int i;
44         for (i = 0; i < initial_ctx->gl_tex_units_num[0]; i++) {
45                 _orig_fastpath_glActiveTexture(GL_TEXTURE0 + i);
46                 _orig_fastpath_glGetIntegerv(pname, (GLint *)&params[i]);
47         }
48         _orig_fastpath_glActiveTexture(cur_active_tex);
49 }
50
51 static void
52 _state_get_draw_buffers(GLenum *params)
53 {
54         AST(initial_ctx != NULL);
55
56         int i;
57         for (i = 0; i < initial_ctx->gl_draw_buffers_num[0]; i++) {
58                 _orig_fastpath_glGetIntegerv(GL_DRAW_BUFFER0 + i, (GLint *)&params[i]);
59         }
60 }
61
62 static void
63 _state_get_transform_feedback_buffer_bindings(GLuint *params)
64 {
65         AST(initial_ctx != NULL);
66
67         int i;
68         for (i = 0; i < initial_ctx->gl_transform_feedback_buffer_binding_num[0];
69                         i++) {
70                 _orig_fastpath_glGetIntegeri_v(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, i,
71                                                                            (GLint *)&params[i]);
72         }
73 }
74
75 static void
76 _state_get_transform_feedback_buffer_bindings_offset(GLintptr *params)
77 {
78         AST(initial_ctx != NULL);
79
80         int i;
81         for (i = 0; i < initial_ctx->gl_transform_feedback_buffer_binding_num[0];
82                         i++) {
83                 _orig_fastpath_glGetIntegeri_v(GL_TRANSFORM_FEEDBACK_BUFFER_START, i,
84                                                                            (GLint *)&params[i]);
85         }
86 }
87
88 static void
89 _state_get_transform_feedback_buffer_bindings_size(GLsizeiptr *params)
90 {
91         AST(initial_ctx != NULL);
92
93         int i;
94         for (i = 0; i < initial_ctx->gl_transform_feedback_buffer_binding_num[0];
95                         i++) {
96                 _orig_fastpath_glGetIntegeri_v(GL_TRANSFORM_FEEDBACK_BUFFER_SIZE, i,
97                                                                            (GLint *)&params[i]);
98         }
99 }
100
101 static void
102 _state_get_uniform_buffer_bindings(GLuint *params)
103 {
104         AST(initial_ctx != NULL);
105
106         int i;
107         for (i = 0; i < initial_ctx->gl_uniform_buffer_binding_num[0]; i++) {
108 /////////////////////////////////////////////////////////////////////////////////
109 // XXXX : AVOID SEGFAULT in ADRENO
110                 ((GLint *)params)[i] = 0;
111 //              _orig_fastpath_glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, i, (GLint *)&params[i]);
112 /////////////////////////////////////////////////////////////////////////////////
113         }
114 }
115
116 static void
117 _state_get_uniform_buffer_bindings_offset(GLintptr *params)
118 {
119         AST(initial_ctx != NULL);
120
121         int i;
122         for (i = 0; i < initial_ctx->gl_uniform_buffer_binding_num[0]; i++) {
123                 _orig_fastpath_glGetIntegeri_v(GL_UNIFORM_BUFFER_START, i, (GLint *)&params[i]);
124         }
125 }
126
127 static void
128 _state_get_uniform_buffer_bindings_size(GLsizeiptr *params)
129 {
130         AST(initial_ctx != NULL);
131
132         int i;
133         for (i = 0; i < initial_ctx->gl_uniform_buffer_binding_num[0]; i++) {
134                 _orig_fastpath_glGetIntegeri_v(GL_UNIFORM_BUFFER_SIZE, i, (GLint *)&params[i]);
135         }
136 }
137
138 /* shader storage buffer */
139 static void
140 _state_get_shader_storage_buffer_bindings(GLuint *params)
141 {
142         AST(initial_ctx != NULL);
143
144         int i;
145         for (i = 0; i < initial_ctx->gl_shader_storage_buffer_binding_num[0]; i++) {
146                 _orig_fastpath_glGetIntegeri_v(GL_SHADER_STORAGE_BUFFER_BINDING, i,
147                                                                            (GLint *)&params[i]);
148         }
149 }
150
151 static void
152 _state_get_shader_storage_buffer_bindings_offset(GLintptr *params)
153 {
154         AST(initial_ctx != NULL);
155
156         int i;
157         for (i = 0; i < initial_ctx->gl_shader_storage_buffer_binding_num[0]; i++) {
158                 _orig_fastpath_glGetIntegeri_v(GL_SHADER_STORAGE_BUFFER_START, i,
159                                                                            (GLint *)&params[i]);
160         }
161 }
162
163 static void
164 _state_get_shader_storage_buffer_bindings_size(GLsizeiptr *params)
165 {
166         AST(initial_ctx != NULL);
167
168         int i;
169         for (i = 0; i < initial_ctx->gl_shader_storage_buffer_binding_num[0]; i++) {
170                 _orig_fastpath_glGetIntegeri_v(GL_SHADER_STORAGE_BUFFER_SIZE, i,
171                                                                            (GLint *)&params[i]);
172         }
173 }
174
175 /* atomic counter buffer */
176 static void
177 _state_get_atomic_counter_buffer_bindings(GLuint *params)
178 {
179         AST(initial_ctx != NULL);
180
181         int i;
182         for (i = 0; i < initial_ctx->gl_atomic_counter_buffer_binding_num[0]; i++) {
183                 _orig_fastpath_glGetIntegeri_v(GL_ATOMIC_COUNTER_BUFFER_BINDING, i,
184                                                                            (GLint *)&params[i]);
185         }
186 }
187
188 static void
189 _state_get_atomic_counter_buffer_bindings_offset(GLintptr *params)
190 {
191         AST(initial_ctx != NULL);
192
193         int i;
194         for (i = 0; i < initial_ctx->gl_atomic_counter_buffer_binding_num[0]; i++) {
195                 _orig_fastpath_glGetIntegeri_v(GL_ATOMIC_COUNTER_BUFFER_START, i,
196                                                                            (GLint *)&params[i]);
197         }
198 }
199
200 static void
201 _state_get_atomic_counter_buffer_bindings_size(GLsizeiptr *params)
202 {
203         AST(initial_ctx != NULL);
204
205         int i;
206         for (i = 0; i < initial_ctx->gl_atomic_counter_buffer_binding_num[0]; i++) {
207                 _orig_fastpath_glGetIntegeri_v(GL_ATOMIC_COUNTER_BUFFER_SIZE, i,
208                                                                            (GLint *)&params[i]);
209         }
210 }
211
212 void
213 fastpath_state_get_draw_buffers(GLenum *params)
214 {
215         _state_get_draw_buffers(params);
216 }
217
218 void
219 init_modules_fastpath()
220 {
221         int fastpath_opt = 0;
222         int fastpath_force_off_opt = 0;
223
224         COREGL_DBG("<Fastpath> : ");
225
226         fastpath_opt = atoi(get_env_setting("COREGL_FASTPATH"));
227         fastpath_force_off_opt = atoi(get_env_setting("COREGL_FASTPATH_FORCE_OFF"));
228
229         if (fastpath_force_off_opt == 1) {
230                 COREGL_DBG("DISABLED by force option");
231                 fastpath_opt = 0;
232         }
233
234         switch (fastpath_opt) {
235         case 1:
236                 COREGL_DBG("(%d) Fastpath enabled...", fastpath_opt);
237                 fp_opt = FP_FAST_PATH;
238                 break;
239         default:
240                 COREGL_DBG("(%d) Default API path enabled...", fastpath_opt);
241                 fp_opt = FP_NORMAL_PATH;
242                 break;
243         }
244
245         debug_nofp = atoi(get_env_setting("COREGL_DEBUG_NOFP"));
246
247 }
248
249 void
250 deinit_modules_fastpath()
251 {
252         GLContext_List *current = NULL;
253
254         AST(mutex_lock(&ctx_list_access_mutex) == 1);
255
256         // Destroy remained context & Detect leaks
257         int retry_destroy = 0;
258
259         while (1) {
260                 retry_destroy = 0;
261                 current = glctx_list;
262                 while (current) {
263                         if (current->cstate != NULL) {
264                                 COREGL_WARN("Context attached to [dpy=%p|rctx=%p] has not been completely destroyed.(leak)",
265                                                         current->cstate->rdpy, current->cstate->rctx);
266
267                                 _orig_fastpath_eglMakeCurrent(current->cstate->rdpy, EGL_NO_SURFACE,
268                                                                                           EGL_NO_SURFACE, EGL_NO_CONTEXT);
269                                 _orig_fastpath_eglDestroyContext(current->cstate->rdpy, current->cstate->rctx);
270
271                                 fastpath_remove_context_states_from_list(current->cstate, NULL);
272                                 retry_destroy = 1;
273                                 break;
274                         }
275
276                         glctx_list = current->next;
277                         free(current);
278                         current = glctx_list;
279                 }
280                 if (retry_destroy == 0) break;
281         }
282         goto finish;
283
284 finish:
285         AST(mutex_unlock(&ctx_list_access_mutex) == 1);
286 }
287
288 void
289 init_modules_tstate_fastpath(GLThreadState *tstate)
290 {
291         MY_MODULE_TSTATE *tstate_mt = NULL;
292
293         tstate_mt = (MY_MODULE_TSTATE *)calloc(1, sizeof(MY_MODULE_TSTATE));
294
295         if (tstate_mt == NULL) return;
296
297         tstate_mt->binded_api = EGL_OPENGL_ES_API;
298
299         tstate->module_data[MY_MODULE_ID] = tstate_mt;
300 }
301
302 void
303 deinit_modules_tstate_fastpath(GLThreadState *tstate)
304 {
305         if (tstate->module_data[MY_MODULE_ID] != NULL) {
306                 free(tstate->module_data[MY_MODULE_ID]);
307                 tstate->module_data[MY_MODULE_ID] = NULL;
308         }
309 }
310
311 void
312 fastpath_apply_overrides()
313 {
314         switch (fp_opt) {
315         case FP_FAST_PATH:
316                 fastpath_apply_overrides_egl(1);
317                 fastpath_apply_overrides_gl(1);
318                 break;
319         case FP_NORMAL_PATH:
320                 break;
321         default:
322                 COREGL_ERR("Invalide GL Override Option!!!");
323                 break;
324         }
325 }
326
327
328 void
329 fastpath_apply_overrides_egl(int enable)
330 {
331 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST)     COREGL_INIT_ORIGINAL(_orig_fastpath_, FUNC_NAME);
332 # include "../headers/sym_egl.h"
333 #undef _COREGL_SYMBOL
334
335         COREGL_OVERRIDE(fastpath_, eglGetProcAddress);
336
337         COREGL_OVERRIDE(fastpath_, eglBindAPI);
338         COREGL_OVERRIDE(fastpath_, eglQueryAPI);
339
340         COREGL_OVERRIDE(fastpath_, eglCreateContext);
341         COREGL_OVERRIDE(fastpath_, eglCreateImageKHR);
342         COREGL_OVERRIDE(fastpath_, eglMakeCurrent);
343         COREGL_OVERRIDE(fastpath_, eglDestroyContext);
344         COREGL_OVERRIDE(fastpath_, eglQueryContext);
345         COREGL_OVERRIDE(fastpath_, eglGetCurrentContext);
346         COREGL_OVERRIDE(fastpath_, eglReleaseThread);
347         COREGL_OVERRIDE(fastpath_, eglGetCurrentSurface);
348         COREGL_OVERRIDE(fastpath_, eglTerminate);
349         COREGL_OVERRIDE(fastpath_, eglGetCurrentDisplay);
350
351 }
352
353 void
354 fastpath_apply_overrides_gl(int enable)
355 {
356 #define _COREGL_START_API(version) api_gl_version = version;
357 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
358 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST)     \
359    if(api_gl_version <= driver_gl_version) COREGL_INIT_ORIGINAL(_orig_fastpath_, FUNC_NAME);
360
361 # include "../headers/sym_gl1.h"
362 # include "../headers/sym_gl2.h"
363 # include "../headers/sym_gl_common.h"
364 #undef _COREGL_SYMBOL
365 #undef _COREGL_START_API
366 #undef _COREGL_END_API
367
368         if (debug_nofp != 1) {
369                 if (driver_gl_version >= COREGL_GLAPI_1) {
370                         COREGL_OVERRIDE(fastpath_, glClientActiveTexture);
371                         COREGL_OVERRIDE(fastpath_, glSampleCoveragex);
372                 }
373
374                 COREGL_OVERRIDE(fastpath_, glGetError);
375                 COREGL_OVERRIDE(fastpath_, glGetString);
376
377                 COREGL_OVERRIDE(fastpath_, glGetIntegerv);
378                 COREGL_OVERRIDE(fastpath_, glGetFloatv);
379
380                 COREGL_OVERRIDE(fastpath_, glActiveTexture);
381                 COREGL_OVERRIDE(fastpath_, glGenTextures);
382                 COREGL_OVERRIDE(fastpath_, glBindTexture);
383                 COREGL_OVERRIDE(fastpath_, glIsTexture);
384                 COREGL_OVERRIDE(fastpath_, glDeleteTextures);
385                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2D);
386
387                 COREGL_OVERRIDE(fastpath_, glGenBuffers);
388                 COREGL_OVERRIDE(fastpath_, glBindBuffer);
389                 COREGL_OVERRIDE(fastpath_, glIsBuffer);
390                 COREGL_OVERRIDE(fastpath_, glDeleteBuffers);
391
392                 COREGL_OVERRIDE(fastpath_, glGenFramebuffers);
393                 COREGL_OVERRIDE(fastpath_, glBindFramebuffer);
394                 COREGL_OVERRIDE(fastpath_, glIsFramebuffer);
395                 COREGL_OVERRIDE(fastpath_, glDeleteFramebuffers);
396                 COREGL_OVERRIDE(fastpath_, glGetFramebufferAttachmentParameteriv);
397
398                 COREGL_OVERRIDE(fastpath_, glGenRenderbuffers);
399                 COREGL_OVERRIDE(fastpath_, glBindRenderbuffer);
400                 COREGL_OVERRIDE(fastpath_, glFramebufferRenderbuffer);
401                 COREGL_OVERRIDE(fastpath_, glIsRenderbuffer);
402                 COREGL_OVERRIDE(fastpath_, glDeleteRenderbuffers);
403
404                 COREGL_OVERRIDE(fastpath_, glCreateShader);
405                 COREGL_OVERRIDE(fastpath_, glCreateProgram);
406                 COREGL_OVERRIDE(fastpath_, glAttachShader);
407                 COREGL_OVERRIDE(fastpath_, glCompileShader);
408                 COREGL_OVERRIDE(fastpath_, glShaderBinary);
409                 COREGL_OVERRIDE(fastpath_, glDeleteShader);
410                 COREGL_OVERRIDE(fastpath_, glDetachShader);
411                 COREGL_OVERRIDE(fastpath_, glGetShaderiv);
412                 COREGL_OVERRIDE(fastpath_, glGetShaderInfoLog);
413                 COREGL_OVERRIDE(fastpath_, glGetShaderSource);
414                 COREGL_OVERRIDE(fastpath_, glIsShader);
415                 COREGL_OVERRIDE(fastpath_, glShaderSource);
416                 COREGL_OVERRIDE(fastpath_, glBindAttribLocation);
417                 COREGL_OVERRIDE(fastpath_, glDeleteProgram);
418                 COREGL_OVERRIDE(fastpath_, glGetActiveAttrib);
419                 COREGL_OVERRIDE(fastpath_, glGetActiveUniform);
420                 COREGL_OVERRIDE(fastpath_, glGetAttachedShaders);
421                 COREGL_OVERRIDE(fastpath_, glGetAttribLocation);
422                 COREGL_OVERRIDE(fastpath_, glGetProgramiv);
423                 COREGL_OVERRIDE(fastpath_, glGetProgramInfoLog);
424                 COREGL_OVERRIDE(fastpath_, glGetUniformfv);
425                 COREGL_OVERRIDE(fastpath_, glGetUniformiv);
426                 COREGL_OVERRIDE(fastpath_, glGetUniformLocation);
427                 COREGL_OVERRIDE(fastpath_, glIsProgram);
428                 COREGL_OVERRIDE(fastpath_, glLinkProgram);
429                 COREGL_OVERRIDE(fastpath_, glUseProgram);
430                 COREGL_OVERRIDE(fastpath_, glValidateProgram);
431
432                 COREGL_OVERRIDE(fastpath_, glBlendColor);
433                 COREGL_OVERRIDE(fastpath_, glBlendEquation);
434                 COREGL_OVERRIDE(fastpath_, glBlendEquationSeparate);
435                 COREGL_OVERRIDE(fastpath_, glBlendFunc);
436                 COREGL_OVERRIDE(fastpath_, glBlendFuncSeparate);
437                 COREGL_OVERRIDE(fastpath_, glClearColor);
438                 COREGL_OVERRIDE(fastpath_, glClearDepthf);
439                 COREGL_OVERRIDE(fastpath_, glClearStencil);
440                 COREGL_OVERRIDE(fastpath_, glColorMask);
441                 COREGL_OVERRIDE(fastpath_, glColorMaski);
442                 COREGL_OVERRIDE(fastpath_, glColorMaskiOES);
443                 COREGL_OVERRIDE(fastpath_, glCullFace);
444                 COREGL_OVERRIDE(fastpath_, glDepthFunc);
445                 COREGL_OVERRIDE(fastpath_, glDepthMask);
446                 COREGL_OVERRIDE(fastpath_, glDepthRangef);
447                 COREGL_OVERRIDE(fastpath_, glDisable);
448                 COREGL_OVERRIDE(fastpath_, glDisableVertexAttribArray);
449                 COREGL_OVERRIDE(fastpath_, glEnable);
450                 COREGL_OVERRIDE(fastpath_, glEnableVertexAttribArray);
451                 COREGL_OVERRIDE(fastpath_, glFrontFace);
452                 COREGL_OVERRIDE(fastpath_, glHint);
453                 COREGL_OVERRIDE(fastpath_, glLineWidth);
454                 COREGL_OVERRIDE(fastpath_, glPixelStorei);
455                 COREGL_OVERRIDE(fastpath_, glPolygonOffset);
456                 COREGL_OVERRIDE(fastpath_, glSampleCoverage);
457                 COREGL_OVERRIDE(fastpath_, glScissor);
458                 COREGL_OVERRIDE(fastpath_, glStencilFunc);
459                 COREGL_OVERRIDE(fastpath_, glStencilFuncSeparate);
460                 COREGL_OVERRIDE(fastpath_, glStencilMask);
461                 COREGL_OVERRIDE(fastpath_, glStencilMaskSeparate);
462                 COREGL_OVERRIDE(fastpath_, glStencilOp);
463                 COREGL_OVERRIDE(fastpath_, glStencilOpSeparate);
464                 COREGL_OVERRIDE(fastpath_, glVertexAttrib1f);
465                 COREGL_OVERRIDE(fastpath_, glVertexAttrib1fv);
466                 COREGL_OVERRIDE(fastpath_, glVertexAttrib2f);
467                 COREGL_OVERRIDE(fastpath_, glVertexAttrib2fv);
468                 COREGL_OVERRIDE(fastpath_, glVertexAttrib3f);
469                 COREGL_OVERRIDE(fastpath_, glVertexAttrib3fv);
470                 COREGL_OVERRIDE(fastpath_, glVertexAttrib4f);
471                 COREGL_OVERRIDE(fastpath_, glVertexAttrib4fv);
472                 COREGL_OVERRIDE(fastpath_, glVertexAttribPointer);
473                 COREGL_OVERRIDE(fastpath_, glViewport);
474
475                 COREGL_OVERRIDE(fastpath_, glGetProgramBinaryOES);
476                 COREGL_OVERRIDE(fastpath_, glProgramBinaryOES);
477
478                 COREGL_OVERRIDE(fastpath_, glUseProgramStagesEXT);
479                 COREGL_OVERRIDE(fastpath_, glActiveShaderProgramEXT);
480                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
481                 COREGL_OVERRIDE(fastpath_, glProgramUniform1iEXT);
482                 COREGL_OVERRIDE(fastpath_, glProgramUniform2iEXT);
483                 COREGL_OVERRIDE(fastpath_, glProgramUniform3iEXT);
484                 COREGL_OVERRIDE(fastpath_, glProgramUniform4iEXT);
485                 COREGL_OVERRIDE(fastpath_, glProgramUniform1fEXT);
486                 COREGL_OVERRIDE(fastpath_, glProgramUniform2fEXT);
487                 COREGL_OVERRIDE(fastpath_, glProgramUniform3fEXT);
488                 COREGL_OVERRIDE(fastpath_, glProgramUniform4fEXT);
489                 COREGL_OVERRIDE(fastpath_, glProgramUniform1ivEXT);
490                 COREGL_OVERRIDE(fastpath_, glProgramUniform2ivEXT);
491                 COREGL_OVERRIDE(fastpath_, glProgramUniform3ivEXT);
492                 COREGL_OVERRIDE(fastpath_, glProgramUniform4ivEXT);
493                 COREGL_OVERRIDE(fastpath_, glProgramUniform1fvEXT);
494                 COREGL_OVERRIDE(fastpath_, glProgramUniform2fvEXT);
495                 COREGL_OVERRIDE(fastpath_, glProgramUniform3fvEXT);
496                 COREGL_OVERRIDE(fastpath_, glProgramUniform4fvEXT);
497                 COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2fvEXT);
498                 COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3fvEXT);
499                 COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4fvEXT);
500
501                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2DMultisampleEXT);
502                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture3DOES);
503
504                 COREGL_OVERRIDE(fastpath_, glBindFramebufferOES);
505                 COREGL_OVERRIDE(fastpath_, glBindRenderbufferOES);
506                 COREGL_OVERRIDE(fastpath_, glDeleteFramebuffersOES);
507                 COREGL_OVERRIDE(fastpath_, glDeleteRenderbuffersOES);
508                 COREGL_OVERRIDE(fastpath_, glDepthRangefOES);
509                 COREGL_OVERRIDE(fastpath_, glDepthRangexOES);
510                 COREGL_OVERRIDE(fastpath_, glFramebufferRenderbufferOES);
511                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2DOES);
512                 COREGL_OVERRIDE(fastpath_, glGenFramebuffersOES);
513                 COREGL_OVERRIDE(fastpath_, glGenRenderbuffersOES);
514                 COREGL_OVERRIDE(fastpath_, glGetFramebufferAttachmentParameterivOES);
515                 COREGL_OVERRIDE(fastpath_, glGetQueryObjecti64vEXT);
516                 COREGL_OVERRIDE(fastpath_, glGetQueryObjectivEXT);
517                 COREGL_OVERRIDE(fastpath_, glGetQueryObjectui64vEXT);
518                 COREGL_OVERRIDE(fastpath_, glGetQueryObjectuivEXT);
519                 COREGL_OVERRIDE(fastpath_, glGetQueryivEXT);
520                 COREGL_OVERRIDE(fastpath_, glBeginQueryEXT);
521                 COREGL_OVERRIDE(fastpath_, glDeleteQueriesEXT);
522                 COREGL_OVERRIDE(fastpath_, glGenQueriesEXT);
523                 COREGL_OVERRIDE(fastpath_, glIsFramebufferOES);
524                 COREGL_OVERRIDE(fastpath_, glIsQueryEXT);
525                 COREGL_OVERRIDE(fastpath_, glIsRenderbufferOES);
526                 COREGL_OVERRIDE(fastpath_, glBlendEquationOES);
527                 COREGL_OVERRIDE(fastpath_, glBlendEquationSeparateOES);
528                 COREGL_OVERRIDE(fastpath_, glBlendFuncSeparateOES);
529                 COREGL_OVERRIDE(fastpath_, glPolygonOffsetxOES);
530                 COREGL_OVERRIDE(fastpath_, glLineWidthxOES);
531                 COREGL_OVERRIDE(fastpath_, glSampleCoveragexOES);
532                 COREGL_OVERRIDE(fastpath_, glQueryCounterEXT);
533                 COREGL_OVERRIDE(fastpath_, glBindVertexArrayOES);
534                 COREGL_OVERRIDE(fastpath_, glDeleteVertexArraysOES);
535                 COREGL_OVERRIDE(fastpath_, glGenVertexArraysOES);
536                 COREGL_OVERRIDE(fastpath_, glIsVertexArrayOES);
537                 COREGL_OVERRIDE(fastpath_, glClearDepthfOES);
538                 COREGL_OVERRIDE(fastpath_, glClearDepthxOES);
539                 COREGL_OVERRIDE(fastpath_, glGetVertexAttribiv);
540                 COREGL_OVERRIDE(fastpath_, glTexBufferOES);
541                 COREGL_OVERRIDE(fastpath_, glFramebufferTextureEXT);
542                 COREGL_OVERRIDE(fastpath_, glFramebufferTextureOES);
543                 COREGL_OVERRIDE(fastpath_, glTexBufferRangeOES);
544                 COREGL_OVERRIDE(fastpath_, glSamplerParameterIuivOES);
545                 COREGL_OVERRIDE(fastpath_, glSamplerParameterIivOES);
546                 COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIuivOES);
547                 COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIivOES);
548                 COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIivEXT);
549                 COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIuivEXT);
550                 COREGL_OVERRIDE(fastpath_, glSamplerParameterIivEXT);
551                 COREGL_OVERRIDE(fastpath_, glSamplerParameterIuivEXT);
552                 COREGL_OVERRIDE(fastpath_, glTexBufferRangeEXT);
553                 COREGL_OVERRIDE(fastpath_, glTexBufferEXT);
554                 COREGL_OVERRIDE(fastpath_, glFramebufferTextureMultiviewOVR);
555                 COREGL_OVERRIDE(fastpath_, glFramebufferTextureMultisampleMultiviewOVR);
556                 /* Start overriding GLES 3.0 */
557                 if (driver_gl_version >= COREGL_GLAPI_3) {
558                         COREGL_OVERRIDE(fastpath_, glReadBuffer);
559
560                         COREGL_OVERRIDE(fastpath_, glGenQueries);
561                         COREGL_OVERRIDE(fastpath_, glDeleteQueries);
562                         COREGL_OVERRIDE(fastpath_, glIsQuery);
563                         COREGL_OVERRIDE(fastpath_, glBeginQuery);
564                         COREGL_OVERRIDE(fastpath_, glGetQueryiv);
565                         COREGL_OVERRIDE(fastpath_, glGetQueryObjectuiv);
566                         COREGL_OVERRIDE(fastpath_, glDrawBuffers);
567                         COREGL_OVERRIDE(fastpath_, glFramebufferTextureLayer);
568
569                         COREGL_OVERRIDE(fastpath_, glBindVertexArray);
570                         COREGL_OVERRIDE(fastpath_, glDeleteVertexArrays);
571                         COREGL_OVERRIDE(fastpath_, glGenVertexArrays);
572                         COREGL_OVERRIDE(fastpath_, glIsVertexArray);
573
574                         COREGL_OVERRIDE(fastpath_, glGetIntegeri_v);
575
576                         COREGL_OVERRIDE(fastpath_, glBindTransformFeedback);
577                         COREGL_OVERRIDE(fastpath_, glBeginTransformFeedback);
578                         COREGL_OVERRIDE(fastpath_, glEndTransformFeedback);
579                         COREGL_OVERRIDE(fastpath_, glPauseTransformFeedback);
580                         COREGL_OVERRIDE(fastpath_, glResumeTransformFeedback);
581                         COREGL_OVERRIDE(fastpath_, glIsTransformFeedback);
582                         COREGL_OVERRIDE(fastpath_, glDeleteTransformFeedbacks);
583                         COREGL_OVERRIDE(fastpath_, glGenTransformFeedbacks);
584                         COREGL_OVERRIDE(fastpath_, glIsTransformFeedback);
585
586                         COREGL_OVERRIDE(fastpath_, glBindBufferRange);
587                         COREGL_OVERRIDE(fastpath_, glBindBufferBase);
588                         COREGL_OVERRIDE(fastpath_, glTransformFeedbackVaryings);
589                         COREGL_OVERRIDE(fastpath_, glGetTransformFeedbackVarying);
590                         COREGL_OVERRIDE(fastpath_, glVertexAttribIPointer);
591                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4i);
592                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4ui);
593                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4iv);
594                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4uiv);
595                         COREGL_OVERRIDE(fastpath_, glGetUniformuiv);
596                         COREGL_OVERRIDE(fastpath_, glGetFragDataLocation);
597                         COREGL_OVERRIDE(fastpath_, glGetStringi);
598                         COREGL_OVERRIDE(fastpath_, glGetUniformIndices);
599                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformsiv);
600                         COREGL_OVERRIDE(fastpath_, glGetUniformBlockIndex);
601                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformBlockiv);
602                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformBlockName);
603                         COREGL_OVERRIDE(fastpath_, glUniformBlockBinding);
604                         COREGL_OVERRIDE(fastpath_, glGetInteger64v);
605                         COREGL_OVERRIDE(fastpath_, glGetInteger64i_v);
606                         COREGL_OVERRIDE(fastpath_, glGenSamplers);
607                         COREGL_OVERRIDE(fastpath_, glDeleteSamplers);
608                         COREGL_OVERRIDE(fastpath_, glIsSampler);
609                         COREGL_OVERRIDE(fastpath_, glBindSampler);
610                         COREGL_OVERRIDE(fastpath_, glSamplerParameteri);
611                         COREGL_OVERRIDE(fastpath_, glSamplerParameteriv);
612                         COREGL_OVERRIDE(fastpath_, glSamplerParameterf);
613                         COREGL_OVERRIDE(fastpath_, glSamplerParameterfv);
614                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameteriv);
615                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameterfv);
616                         COREGL_OVERRIDE(fastpath_, glVertexAttribDivisor);
617                         COREGL_OVERRIDE(fastpath_, glGetProgramBinary);
618                         COREGL_OVERRIDE(fastpath_, glProgramBinary);
619                         COREGL_OVERRIDE(fastpath_, glProgramParameteri);
620                 } // End of GLES 3.0
621
622                 if (driver_gl_version >= COREGL_GLAPI_31) {
623                         COREGL_OVERRIDE(fastpath_, glCreateShaderProgramv);
624                         COREGL_OVERRIDE(fastpath_, glGenProgramPipelines);
625                         COREGL_OVERRIDE(fastpath_, glGetProgramPipelineiv);
626                         COREGL_OVERRIDE(fastpath_, glBindProgramPipeline);
627                         COREGL_OVERRIDE(fastpath_, glDeleteProgramPipelines);
628                         COREGL_OVERRIDE(fastpath_, glIsProgramPipeline);
629                         COREGL_OVERRIDE(fastpath_, glValidateProgramPipeline);
630                         COREGL_OVERRIDE(fastpath_, glGetProgramPipelineInfoLog);
631                         COREGL_OVERRIDE(fastpath_, glDispatchCompute);
632                         COREGL_OVERRIDE(fastpath_, glDispatchComputeIndirect);
633                         COREGL_OVERRIDE(fastpath_, glGetProgramInterfaceiv);
634                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceIndex);
635                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceName);
636                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceiv);
637                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceLocation);
638                         COREGL_OVERRIDE(fastpath_, glUseProgramStages);
639                         COREGL_OVERRIDE(fastpath_, glActiveShaderProgram);
640                         COREGL_OVERRIDE(fastpath_, glProgramUniform1iv);
641                         COREGL_OVERRIDE(fastpath_, glProgramUniform2iv);
642                         COREGL_OVERRIDE(fastpath_, glProgramUniform3iv);
643                         COREGL_OVERRIDE(fastpath_, glProgramUniform4iv);
644                         COREGL_OVERRIDE(fastpath_, glProgramUniform1fv);
645                         COREGL_OVERRIDE(fastpath_, glProgramUniform2fv);
646                         COREGL_OVERRIDE(fastpath_, glProgramUniform3fv);
647                         COREGL_OVERRIDE(fastpath_, glProgramUniform4fv);
648                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2fv);
649                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3fv);
650                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4fv);
651                         COREGL_OVERRIDE(fastpath_, glProgramUniform1i);
652                         COREGL_OVERRIDE(fastpath_, glProgramUniform2i);
653                         COREGL_OVERRIDE(fastpath_, glProgramUniform3i);
654                         COREGL_OVERRIDE(fastpath_, glProgramUniform4i);
655                         COREGL_OVERRIDE(fastpath_, glProgramUniform1f);
656                         COREGL_OVERRIDE(fastpath_, glProgramUniform2f);
657                         COREGL_OVERRIDE(fastpath_, glProgramUniform3f);
658                         COREGL_OVERRIDE(fastpath_, glProgramUniform4f);
659                         COREGL_OVERRIDE(fastpath_, glProgramUniform1uiv);
660                         COREGL_OVERRIDE(fastpath_, glProgramUniform2uiv);
661                         COREGL_OVERRIDE(fastpath_, glProgramUniform3uiv);
662                         COREGL_OVERRIDE(fastpath_, glProgramUniform4uiv);
663                         COREGL_OVERRIDE(fastpath_, glProgramUniform1ui);
664                         COREGL_OVERRIDE(fastpath_, glProgramUniform2ui);
665                         COREGL_OVERRIDE(fastpath_, glProgramUniform3ui);
666                         COREGL_OVERRIDE(fastpath_, glProgramUniform4ui);
667                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2x3fv);
668                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3x2fv);
669                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4x2fv);
670                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2x4fv);
671                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3x4fv);
672                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4x3fv);
673                         COREGL_OVERRIDE(fastpath_, glBindImageTexture);
674                         COREGL_OVERRIDE(fastpath_, glGetBooleani_v);
675                         COREGL_OVERRIDE(fastpath_, glGetMultisamplefv);
676                         COREGL_OVERRIDE(fastpath_, glGetTexLevelParameteriv);
677                         COREGL_OVERRIDE(fastpath_, glGetTexLevelParameterfv);
678                         COREGL_OVERRIDE(fastpath_, glBindVertexBuffer);
679                 }
680
681                 if (driver_gl_version >= COREGL_GLAPI_32) {
682                         COREGL_OVERRIDE(fastpath_, glBlendEquationi);
683                         COREGL_OVERRIDE(fastpath_, glBlendEquationSeparatei);
684                         COREGL_OVERRIDE(fastpath_, glTexBuffer);
685                         COREGL_OVERRIDE(fastpath_, glTexBufferRange);
686                         COREGL_OVERRIDE(fastpath_, glFramebufferTexture);
687                         COREGL_OVERRIDE(fastpath_, glGetnUniformfv);
688                         COREGL_OVERRIDE(fastpath_, glGetnUniformiv);
689                         COREGL_OVERRIDE(fastpath_, glGetnUniformuiv);
690                         COREGL_OVERRIDE(fastpath_, glSamplerParameterIiv);
691                         COREGL_OVERRIDE(fastpath_, glSamplerParameterIuiv);
692                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIiv);
693                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameterIuiv);
694                 }
695         } else {
696                 COREGL_DBG("SKIP GL FASTPATH...");
697         }
698 }
699
700 #undef OVERRIDE
701
702 static inline GL_Object_Hash_Base *
703 _lock_gl_object_hash(GL_Object_State *ostate, GL_Object_Type type)
704 {
705         switch (type) {
706         case GL_OBJECT_TYPE_QUERY:
707                 return &ostate->query;
708         case GL_OBJECT_TYPE_TEXTURE:
709                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
710                 return &ostate->shared->texture;
711         case GL_OBJECT_TYPE_BUFFER:
712                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
713                 return &ostate->shared->buffer;
714         case GL_OBJECT_TYPE_FRAMEBUFFER:
715                 return &ostate->framebuffer;
716         case GL_OBJECT_TYPE_RENDERBUFFER:
717                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
718                 return &ostate->shared->renderbuffer;
719         case GL_OBJECT_TYPE_PROGRAM:
720                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
721                 return &ostate->shared->program;
722         case GL_OBJECT_TYPE_VERTEXARRAY:
723                 return &ostate->vertexarray;
724         case GL_OBJECT_TYPE_SAMPLER:
725                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
726                 return &ostate->shared->sampler;
727         case GL_OBJECT_TYPE_TRANSFORMFEEDBACK:
728                 return &ostate->transformfeedback;
729         case GL_OBJECT_TYPE_PROGRAMPIPELINE:
730                 AST(mutex_lock(&ostate->shared->access_mutex) == 1);
731                 return &ostate->shared->programpipeline;
732         default:
733                 return NULL;
734         }
735 }
736
737 static inline void
738 _unlock_gl_object_hash(GL_Object_State *ostate, GL_Object_Type type)
739 {
740         switch (type) {
741         case GL_OBJECT_TYPE_TEXTURE:
742         case GL_OBJECT_TYPE_BUFFER:
743         case GL_OBJECT_TYPE_RENDERBUFFER:
744         case GL_OBJECT_TYPE_PROGRAM:
745         case GL_OBJECT_TYPE_SAMPLER:
746         case GL_OBJECT_TYPE_PROGRAMPIPELINE:
747                 AST(mutex_unlock(&ostate->shared->access_mutex) == 1);
748         default:
749                 break;
750         }
751 }
752
753 static inline GL_Object_Hash_Base *
754 _lock_gl_object_hash_real(GL_Object_State *ostate, GL_Object_Type type)
755 {
756         switch (type) {
757         case GL_OBJECT_TYPE_QUERY:
758                 return &ostate->query_real;
759         case GL_OBJECT_TYPE_TEXTURE:
760                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
761                 return &ostate->shared->texture_real;
762         case GL_OBJECT_TYPE_BUFFER:
763                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
764                 return &ostate->shared->buffer_real;
765         case GL_OBJECT_TYPE_FRAMEBUFFER:
766                 return &ostate->framebuffer_real;
767         case GL_OBJECT_TYPE_RENDERBUFFER:
768                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
769                 return &ostate->shared->renderbuffer_real;
770         case GL_OBJECT_TYPE_PROGRAM:
771                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
772                 return &ostate->shared->program_real;
773         case GL_OBJECT_TYPE_VERTEXARRAY:
774                 return &ostate->vertexarray_real;
775         case GL_OBJECT_TYPE_SAMPLER:
776                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
777                 return &ostate->shared->sampler_real;
778         case GL_OBJECT_TYPE_TRANSFORMFEEDBACK:
779                 return &ostate->transformfeedback_real;
780         case GL_OBJECT_TYPE_PROGRAMPIPELINE:
781                 AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
782                 return &ostate->shared->programpipeline_real;
783         default:
784                 return NULL;
785         }
786 }
787
788 static inline void
789 _unlock_gl_object_hash_real(GL_Object_State *ostate, GL_Object_Type type)
790 {
791         switch (type) {
792         case GL_OBJECT_TYPE_TEXTURE:
793         case GL_OBJECT_TYPE_BUFFER:
794         case GL_OBJECT_TYPE_RENDERBUFFER:
795         case GL_OBJECT_TYPE_PROGRAM:
796         case GL_OBJECT_TYPE_SAMPLER:
797         case GL_OBJECT_TYPE_PROGRAMPIPELINE:
798                 AST(mutex_unlock(&ostate->shared->real_access_mutex) == 1);
799                 break;
800         default:
801                 break;
802         }
803 }
804
805 int
806 fastpath_add_context_state_to_list(const void *option, const int option_len,
807                                                                    GLContextState *cstate, Mutex *mtx)
808 {
809         int ret = 0;
810         int tid = 0;
811         GLContext_List *current = NULL;
812         GLContext_List *newitm = NULL;
813
814         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
815
816         AST(cstate != NULL);
817
818         tid = get_current_thread();
819
820         current = glctx_list;
821         while (current != NULL) {
822                 if (current->option_len == option_len &&
823                                 memcmp(current->option, option, option_len) == 0 &&
824                                 current->thread_id == tid) {
825                         AST(current->cstate == cstate);
826                         goto finish;
827                 }
828                 current = current->next;
829         }
830
831         newitm = (GLContext_List *)calloc(1, sizeof(GLContext_List));
832         if (newitm == NULL) {
833                 COREGL_ERR("Failed to create context list.");
834                 goto finish;
835         }
836
837         newitm->cstate = cstate;
838         newitm->thread_id = tid;
839         newitm->option_len = option_len;
840         newitm->option = (void *)malloc(option_len);
841         memcpy(newitm->option, option, option_len);
842
843         if (glctx_list != NULL)
844                 newitm->next = glctx_list;
845
846         glctx_list = newitm;
847
848         ret = 1;
849         goto finish;
850
851 finish:
852         if (ret != 1) {
853                 if (newitm != NULL) {
854                         free(newitm);
855                         newitm = NULL;
856                 }
857                 if (cstate != NULL) {
858                         free(cstate);
859                         cstate = NULL;
860                 }
861         }
862         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
863
864         return ret;
865 }
866
867 GLContextState *
868 fastpath_get_context_state_from_list(const void *option, const int option_len,
869                                                                          Mutex *mtx)
870 {
871         GLContextState *ret = NULL;
872         GLContext_List *current = NULL;
873         int tid = 0;
874
875         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
876
877         tid = get_current_thread();
878
879         current = glctx_list;
880         while (current != NULL) {
881                 if (current->option_len == option_len &&
882                                 memcmp(current->option, option, option_len) == 0 &&
883                                 current->thread_id == tid) {
884                         ret = current->cstate;
885                         goto finish;
886                 }
887                 current = current->next;
888         }
889         goto finish;
890
891 finish:
892         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
893         return ret;
894 }
895
896 int
897 fastpath_remove_context_states_from_list(GLContextState *cstate, Mutex *mtx)
898 {
899         int ret = 0;
900         GLContext_List *olditm = NULL;
901         GLContext_List *current = NULL;
902
903         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
904
905         AST(cstate != NULL);
906
907         current = glctx_list;
908
909         while (current != NULL) {
910                 if (current->cstate == cstate) {
911                         GLContext_List *nextitm = NULL;
912                         if (olditm != NULL) {
913                                 olditm->next = current->next;
914                                 nextitm = olditm->next;
915                         } else {
916                                 glctx_list = current->next;
917                                 nextitm = glctx_list;
918                         }
919                         if (current->option != NULL) {
920                                 AST(current->option_len > 0);
921                                 free(current->option);
922                                 current->option = NULL;
923                                 current->option_len = 0;
924                         }
925                         free(current);
926                         ret = 1;
927                         current = nextitm;
928                         continue;
929                 }
930                 olditm = current;
931                 current = current->next;
932         }
933         goto finish;
934
935 finish:
936         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
937         return ret;
938 }
939
940
941
942 #define HASH_INIT(hash_base) \
943         hash_base.hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * GL_OBJECT_HASH_BASE); \
944         hash_base.hash_size = GL_OBJECT_HASH_BASE;
945
946 void
947 fastpath_ostate_init(GL_Object_State *ostate)
948 {
949         HASH_INIT(ostate->query);
950         HASH_INIT(ostate->framebuffer);
951         HASH_INIT(ostate->vertexarray);
952         HASH_INIT(ostate->transformfeedback);
953
954         HASH_INIT(ostate->query_real);
955         HASH_INIT(ostate->framebuffer_real);
956         HASH_INIT(ostate->vertexarray_real);
957         HASH_INIT(ostate->transformfeedback_real);
958 }
959
960 void
961 fastpath_sostate_init(GL_Shared_Object_State *sostate)
962 {
963         mutex_init(&sostate->access_mutex);
964
965         HASH_INIT(sostate->texture);
966         HASH_INIT(sostate->buffer);
967         HASH_INIT(sostate->renderbuffer);
968         HASH_INIT(sostate->program);
969         HASH_INIT(sostate->sampler);
970         HASH_INIT(sostate->programpipeline);
971
972         HASH_INIT(sostate->texture_real);
973         HASH_INIT(sostate->buffer_real);
974         HASH_INIT(sostate->renderbuffer_real);
975         HASH_INIT(sostate->program_real);
976         HASH_INIT(sostate->sampler_real);
977         HASH_INIT(sostate->programpipeline_real);
978 }
979
980 #undef HASH_INIT
981
982
983 static void
984 _add_hash(GL_Object_Hash_Base *hash_base, GL_Object_Hash *data)
985 {
986         int array_idx = data->hash_key & (hash_base->hash_size - 1);
987         if (hash_base->hash_field[array_idx] == NULL) {
988                 hash_base->hash_field[array_idx] = data;
989         } else {
990                 GL_Object_Hash *current = hash_base->hash_field[array_idx];
991                 while (current->next) {
992                         AST(current->hash_key != data->hash_key);
993                         current = current->next;
994                 }
995                 current->next = data;
996         }
997         data->next = NULL;
998         hash_base->item_size++;
999 }
1000
1001 static int
1002 _remove_hash(GL_Object_Hash_Base *hash_base, GLuint hash)
1003 {
1004         int ret = 0;
1005         int array_idx = hash & (hash_base->hash_size - 1);
1006
1007         GL_Object_Hash *current = hash_base->hash_field[array_idx];
1008         GL_Object_Hash *prev = NULL;
1009
1010         while (current) {
1011                 if (current->hash_key == hash) {
1012                         if (prev != NULL)
1013                                 prev->next = current->next;
1014                         else
1015                                 hash_base->hash_field[array_idx] = current->next;
1016                         hash_base->item_size--;
1017                         ret = 1;
1018                         break;
1019                 }
1020                 prev = current;
1021                 current = current->next;
1022         }
1023
1024         return ret;
1025 }
1026
1027 static void
1028 _free_hash_list(GL_Object_Hash_Base *hash_base, int free_data)
1029 {
1030         if (hash_base->item_size == 0) return;
1031
1032         for (int i = 0; i < hash_base->hash_size; i++) {
1033                 if (hash_base->hash_field[i] != NULL) {
1034                         GL_Object_Hash *current = hash_base->hash_field[i];
1035
1036                         while (current != NULL) {
1037                                 GL_Object_Hash *current_next = current->next;
1038
1039                                 if (free_data == 1 && current->item != NULL) {
1040                                         free(current->item);
1041                                 }
1042
1043                                 free(current);
1044                                 hash_base->item_size--;
1045                                 current = current_next;
1046                         }
1047                 }
1048         }
1049 }
1050
1051
1052
1053 #define HASH_DEINIT(hash_base, free_data) \
1054         _free_hash_list(&hash_base, free_data); \
1055         free(hash_base.hash_field); \
1056         hash_base.hash_size = 0;
1057
1058 void
1059 fastpath_ostate_deinit(GL_Object_State *ostate)
1060 {
1061         HASH_DEINIT(ostate->query, 1);
1062         HASH_DEINIT(ostate->framebuffer, 1);
1063         HASH_DEINIT(ostate->vertexarray, 1);
1064         HASH_DEINIT(ostate->transformfeedback, 1);
1065
1066         HASH_DEINIT(ostate->query_real, 0);
1067         HASH_DEINIT(ostate->framebuffer_real, 0);
1068         HASH_DEINIT(ostate->vertexarray_real, 0);
1069         HASH_DEINIT(ostate->transformfeedback_real, 0);
1070 }
1071
1072 void
1073 fastpath_sostate_deinit(GL_Shared_Object_State *sostate)
1074 {
1075         HASH_DEINIT(sostate->texture, 1);
1076         HASH_DEINIT(sostate->buffer, 1);
1077         HASH_DEINIT(sostate->renderbuffer, 1);
1078         HASH_DEINIT(sostate->program, 1);
1079         HASH_DEINIT(sostate->sampler, 1);
1080         HASH_DEINIT(sostate->programpipeline, 1);
1081
1082         HASH_DEINIT(sostate->texture_real, 0);
1083         HASH_DEINIT(sostate->buffer_real, 0);
1084         HASH_DEINIT(sostate->renderbuffer_real, 0);
1085         HASH_DEINIT(sostate->program_real, 0);
1086         HASH_DEINIT(sostate->sampler_real, 0);
1087
1088         HASH_DEINIT(sostate->programpipeline_real, 0);
1089 }
1090
1091 #undef HASH_DEINIT
1092
1093
1094
1095 #define FIND_HASH(hash_base, key, ret) \
1096 { \
1097         GL_Object_Hash *fh_current = hash_base->hash_field[(key) & (hash_base->hash_size - 1)]; \
1098         while(fh_current) \
1099         { \
1100                 if (fh_current->hash_key == (key)) \
1101                 { \
1102                         ret = fh_current; \
1103                         break; \
1104                 } \
1105                 fh_current = fh_current->next; \
1106         } \
1107 }
1108
1109 void
1110 _ostate_hash_check(GL_Object_Hash_Base *hash_base)
1111 {
1112         if (hash_base->item_size + 1 < hash_base->hash_size)
1113                 return;
1114
1115         int oldsize = hash_base->hash_size;
1116         GL_Object_Hash **oldfield = hash_base->hash_field;
1117
1118         hash_base->hash_size = oldsize << 1;
1119         hash_base->hash_field = (GL_Object_Hash **)calloc(1,
1120                                                         sizeof(GL_Object_Hash *) * hash_base->hash_size);
1121         AST(hash_base->hash_field != NULL);
1122
1123         for (int i = 0; i < oldsize; i++) {
1124                 if (oldfield[i] != NULL) {
1125                         GL_Object_Hash *current = oldfield[i];
1126
1127                         while (current != NULL) {
1128                                 GL_Object_Hash *current_next = current->next;
1129                                 _add_hash(hash_base, current);
1130                                 hash_base->item_size--;
1131                                 current = current_next;
1132                         }
1133                 }
1134         }
1135         free(oldfield);
1136
1137 }
1138
1139 GLuint
1140 fastpath_ostate_create_object(GL_Object_State *ostate, GL_Object_Type type,
1141                                                           GLuint real_name)
1142 {
1143         GLuint ret = _COREGL_INT_INIT_VALUE;
1144
1145         GL_Object_Hash_Base *hash_base = NULL;
1146         GL_Object_Hash_Base *hash_base_real = NULL;
1147         int newid = _COREGL_INT_INIT_VALUE;
1148
1149         hash_base = _lock_gl_object_hash(ostate, type);
1150         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1151
1152         newid = hash_base->last_id + 1;
1153         if (newid >= hash_base->hash_size) {
1154                 hash_base->is_looped = 1;
1155                 newid = 1;
1156                 hash_base->last_id = 1;
1157         }
1158
1159         if (hash_base->is_looped != 0) {
1160                 int i;
1161                 int findingid = newid;
1162                 newid = -1;
1163                 for (i = 0; i < hash_base->hash_size; i++) {
1164                         GL_Object_Hash *exist_hash = NULL;
1165                         FIND_HASH(hash_base, findingid, exist_hash);
1166                         if (exist_hash == NULL) {
1167                                 newid = findingid;
1168                                 break;
1169                         }
1170                         findingid++;
1171                         if (findingid >= hash_base->hash_size) findingid = 1;
1172                 }
1173                 AST(newid != -1);
1174         }
1175         hash_base->last_id = newid;
1176
1177         {
1178                 GL_Object *newobj = (GL_Object *)calloc(1, sizeof(GL_Object));
1179                 if (newobj == NULL)
1180                         goto finish;
1181                 newobj->id = (int)type + newid;
1182                 newobj->real_id = real_name;
1183                 newobj->ref_count = 1;
1184
1185
1186                 GL_Object_Hash *newobj_hash = (GL_Object_Hash *)calloc(1,
1187                                                                           sizeof(GL_Object_Hash));
1188                 if (newobj_hash == NULL) {
1189                         free(newobj);
1190                         goto finish;
1191                 }
1192                 newobj_hash->item = newobj;
1193                 newobj_hash->hash_key = newid;
1194                 _add_hash(hash_base, newobj_hash);
1195
1196                 GL_Object_Hash *newobj_hash_real = (GL_Object_Hash *)calloc(1,
1197                                                                                    sizeof(GL_Object_Hash));
1198                 if (newobj_hash_real == NULL) {
1199                         free(newobj);
1200                         free(newobj_hash);
1201                         goto finish;
1202                 }
1203                 newobj_hash_real->item = newobj;
1204                 newobj_hash_real->hash_key = real_name;
1205                 _add_hash(hash_base_real, newobj_hash_real);
1206
1207                 ret = newobj->id;
1208         }
1209
1210         _ostate_hash_check(hash_base);
1211         _ostate_hash_check(hash_base_real);
1212
1213         goto finish;
1214
1215 finish:
1216         _unlock_gl_object_hash(ostate, type);
1217         _unlock_gl_object_hash_real(ostate, type);
1218         return ret;
1219 }
1220
1221 #define FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object) \
1222         if (((int)(hash)) < 0) { ret = 0; goto finish; } \
1223         { \
1224                 GL_Object_Hash *object_hash = NULL; \
1225                 FIND_HASH((hash_base), (int)(hash), object_hash); \
1226                 if (object_hash == NULL) { ret = 0; goto finish; } \
1227                 (object) = object_hash->item; \
1228                 if ((object) == NULL) { ret = 0; goto finish; } \
1229         }
1230
1231 GLuint
1232 fastpath_ostate_remove_object(GL_Object_State *ostate, GL_Object_Type type,
1233                                                           GLuint glue_name)
1234 {
1235         GLuint ret = _COREGL_INT_INIT_VALUE;
1236
1237         GL_Object_Hash_Base *hash_base = NULL;
1238         GL_Object_Hash_Base *hash_base_real = NULL;
1239         GL_Object *object = NULL;
1240
1241         hash_base = _lock_gl_object_hash(ostate, type);
1242         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1243
1244         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1245
1246         object->ref_count--;
1247
1248         if (object->ref_count <= 0) {
1249                 GL_Object_Hash *object_hash = NULL;
1250
1251                 FIND_HASH(hash_base, object->id - (int)type, object_hash);
1252                 AST(object_hash != NULL);
1253                 _remove_hash(hash_base, object->id - (int)type);
1254                 free(object_hash);
1255                 object_hash = NULL;
1256
1257                 FIND_HASH(hash_base_real, object->real_id, object_hash);
1258                 AST(object_hash != NULL);
1259                 _remove_hash(hash_base_real, object->real_id);
1260                 free(object_hash);
1261                 object_hash = NULL;
1262
1263                 free(object);
1264                 object = NULL;
1265         }
1266
1267         ret = 1;
1268         goto finish;
1269
1270 finish:
1271         _unlock_gl_object_hash(ostate, type);
1272         _unlock_gl_object_hash_real(ostate, type);
1273         return ret;
1274 }
1275
1276 GLuint
1277 fastpath_ostate_get_object(GL_Object_State *ostate, GL_Object_Type type,
1278                                                    GLuint glue_name)
1279 {
1280         GLuint ret = _COREGL_INT_INIT_VALUE;
1281
1282         GL_Object_Hash_Base *hash_base = NULL;
1283         GL_Object *object = NULL;
1284
1285         hash_base = _lock_gl_object_hash(ostate, type);
1286
1287         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1288
1289         ret = object->real_id;
1290         goto finish;
1291
1292 finish:
1293         _unlock_gl_object_hash(ostate, type);
1294         return ret;
1295 }
1296
1297 /* Check if the context's state contains object of a given type */
1298 GLuint
1299 fastpath_ostate_has_object_type(GL_Object_State *ostate, GL_Object_Type type)
1300 {
1301         GLuint ret = _COREGL_INT_INIT_VALUE;
1302
1303         GL_Object_Hash_Base *hash_base = NULL;
1304         GL_Object *object = NULL;
1305         hash_base = _lock_gl_object_hash(ostate, type);
1306
1307         if (hash_base->hash_field == 0) {
1308                 ret = 0;
1309                 goto finish;
1310         }
1311
1312         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, 1, object);
1313
1314         ret = object->real_id;
1315         goto finish;
1316
1317 finish:
1318         _unlock_gl_object_hash(ostate, type);
1319         return ret;
1320 }
1321
1322
1323 GLint
1324 fastpath_ostate_set_object_tag(GL_Object_State *ostate, GL_Object_Type type,
1325                                                            GLuint glue_name, GLvoid *tag)
1326 {
1327         GLint ret = _COREGL_INT_INIT_VALUE;
1328
1329         GL_Object_Hash_Base *hash_base = NULL;
1330         GL_Object *object = NULL;
1331         int hash = _COREGL_INT_INIT_VALUE;
1332
1333         hash_base = _lock_gl_object_hash(ostate, type);
1334
1335         hash = glue_name - (int)type;
1336
1337         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object);
1338
1339         AST(object->tag == NULL);
1340         object->tag = tag;
1341         ret = 1;
1342         goto finish;
1343
1344 finish:
1345         _unlock_gl_object_hash(ostate, type);
1346         return ret;
1347 }
1348
1349 GLvoid *
1350 fastpath_ostate_get_object_tag(GL_Object_State *ostate, GL_Object_Type type,
1351                                                            GLuint glue_name)
1352 {
1353         GLvoid *ret = NULL;
1354
1355         GL_Object_Hash_Base *hash_base = NULL;
1356         GL_Object *object = NULL;
1357
1358         hash_base = _lock_gl_object_hash(ostate, type);
1359
1360         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1361
1362         ret = object->tag;
1363         goto finish;
1364
1365 finish:
1366         _unlock_gl_object_hash(ostate, type);
1367         return ret;
1368 }
1369
1370 GLuint
1371 fastpath_ostate_find_object(GL_Object_State *ostate, GL_Object_Type type,
1372                                                         GLuint real_name)
1373 {
1374         GLuint ret = _COREGL_INT_INIT_VALUE;
1375
1376         GL_Object_Hash_Base *hash_base_real = NULL;
1377         GL_Object *object = NULL;
1378
1379         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1380
1381         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base_real, real_name, object);
1382
1383         ret = object->id;
1384         goto finish;
1385
1386 finish:
1387         _unlock_gl_object_hash_real(ostate, type);
1388         return ret;
1389 }
1390
1391 GLint
1392 fastpath_ostate_use_object(GL_Object_State *ostate, GL_Object_Type type,
1393                                                    GLuint glue_name)
1394 {
1395         GLint ret = _COREGL_INT_INIT_VALUE;
1396
1397         GL_Object_Hash_Base *hash_base = NULL;
1398         GL_Object *object = NULL;
1399
1400         hash_base = _lock_gl_object_hash(ostate, type);
1401
1402         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1403
1404         object->ref_count++;
1405         ret = 1;
1406         goto finish;
1407
1408 finish:
1409         _unlock_gl_object_hash(ostate, type);
1410         return ret;
1411 }
1412
1413 void
1414 fastpath_dump_context_states(GLGlueContext *ctx, int force_output)
1415 {
1416         static struct timeval tv_last = { 0, 0 };
1417
1418         if (unlikely(get_trace_state_flag() != 1)) return;
1419
1420         if (!force_output) {
1421                 struct timeval tv_now = { 0, 0 };
1422                 AST(gettimeofday(&tv_now, NULL) == 0);
1423                 if (tv_now.tv_sec - tv_last.tv_sec < _COREGL_TRACE_OUTPUT_INTERVAL_SEC) {
1424                         goto finish;
1425                 }
1426                 tv_last = tv_now;
1427         }
1428
1429         TRACE("\n");
1430         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1431         TRACE("\E[40;32;1m  State info \E[1;37;1m: <PID = %d> GlueCTX = %p\E[0m\n",
1432                   getpid(), ctx);
1433         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1434
1435 #define PRINTF_CHAR_GLenum "0x%8X"
1436 #define PRINTF_CHAR_GLboolean "%10d"
1437 #define PRINTF_CHAR_GLint "%10d"
1438 #define PRINTF_CHAR_GLsizei "%10u"
1439 #define PRINTF_CHAR_GLuint "%10u"
1440 #define PRINTF_CHAR_GLuintmask "0x%8X"
1441 #define PRINTF_CHAR_GLintptr "%10ld"
1442 #define PRINTF_CHAR_GLsizeiptr "%10ld"
1443
1444 #define PRINTF_CHAR_GLclampf "%10.6f"
1445 #define PRINTF_CHAR_GLfloat "%10.6f"
1446
1447 #define PRINTF_CHAR_GLvoidptr "%10p"
1448
1449 #define PRINTF_CHAR(type) PRINTF_CHAR_##type
1450
1451 #define _COREGL_START_API(version) api_gl_version = version;
1452 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1453 #define INITIAL_CTX initial_ctx
1454 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1455    { \
1456       TYPE valuedata[SIZE]; \
1457       TYPE *value = NULL; \
1458       if(api_gl_version <= driver_gl_version) { \
1459          value = valuedata; GET_STMT; value = valuedata; \
1460          _sym_glGetError();\
1461          TRACE("\E[40;37;1m %-30.30s : (\E[0m ", #NAME); \
1462          for (int i = 0; i < SIZE; i++) \
1463          { \
1464             if (i > 0) { \
1465                if (i % 4 == 0) \
1466                   TRACE("\n %-30.30s     ", "");\
1467                else \
1468                   TRACE(", "); \
1469             } \
1470             if (ctx->NAME[i] != value[i]) { TRACE("\E[40;31;1m"); } \
1471                TRACE(PRINTF_CHAR(TYPE), ctx->NAME[i]); \
1472                TRACE("["PRINTF_CHAR(TYPE)"]", value[i]); \
1473             if (ctx->NAME[i] != value[i]) { TRACE("\E[0m"); } \
1474          } \
1475          TRACE(" \E[40;37;1m)\E[0m\n"); \
1476       } \
1477    }
1478 # include "coregl_fastpath_state.h"
1479 #undef GLUE_STATE
1480 #undef INITIAL_CTX
1481 #undef _COREGL_START_API
1482 #undef _COREGL_END_API
1483
1484         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1485         TRACE("\n");
1486
1487         TRACE_END();
1488
1489 finish:
1490         return;
1491 }
1492
1493 int
1494 fastpath_init_context_states(GLGlueContext *ctx)
1495 {
1496         int ret = 0;
1497
1498         AST(mutex_lock(&init_context_mutex) == 1);
1499
1500         if (ctx == NULL) {
1501                 COREGL_ERR("Context NULL");
1502                 ret = 0;
1503                 goto finish;
1504         }
1505
1506         AST(ctx->initialized == 0);
1507         AST(ctx->ostate.shared != NULL);
1508
1509         if (initial_ctx == NULL) {
1510                 initial_ctx = (GLGlueContext *)calloc(1, sizeof(GLGlueContext));
1511                 AST(initial_ctx != NULL);
1512
1513 //#define FORCE_DEFAULT_VALUE
1514 #define _COREGL_START_API(version) api_gl_version = version;
1515 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1516 #ifdef FORCE_DEFAULT_VALUE
1517 # define INITIAL_CTX initial_ctx
1518 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1519       { \
1520          if(SIZE > 0) { \
1521              int i; \
1522              TYPE valuedata[SIZE]; \
1523              TYPE *value = NULL; \
1524              memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1525              initial_ctx->NAME = (TYPE *)calloc(SIZE, sizeof(TYPE));\
1526              initial_ctx->NAME##_updated = (GLboolean *)calloc(SIZE, sizeof(GLboolean)); \
1527              if(api_gl_version <= driver_gl_version) { \
1528                  value = valuedata; DEFAULT_STMT; value = valuedata; \
1529                  for (i = 0; i < SIZE; i++) \
1530                  { \
1531                     if (*((char *)(&value[i])) == 0xcc) \
1532                     { \
1533                         memset(&value[i], 0xaa, sizeof(TYPE)); \
1534                         value = valuedata; DEFAULT_STMT; value = valuedata; \
1535                         if (*((char *)(&value[i])) == 0xaa) \
1536                         { \
1537                             COREGL_WARN("GL-state '"#NAME"' cannot be retrieved"); \
1538                             break; \
1539                         } \
1540                     } \
1541                     initial_ctx->NAME[i] = value[i]; \
1542                     initial_ctx->NAME##_updated[i] = GL_FALSE; \
1543                  } \
1544              } \
1545         }\
1546       }
1547 #  include "coregl_fastpath_state.h"
1548 # undef GLUE_STATE
1549 # undef INITIAL_CTX
1550 #else
1551 # define INITIAL_CTX initial_ctx
1552 # define SET_GLUE_VALUE(DEFAULT_STMT, FALLBACK_STMT) \
1553       if (try_step == 1) \
1554       { \
1555          value = valuedata; DEFAULT_STMT; value = valuedata; \
1556       } \
1557       else \
1558       { \
1559          value = valuedata; FALLBACK_STMT; value = valuedata; \
1560       }
1561
1562 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1563       { \
1564          if (SIZE > 0) { \
1565              int i; \
1566              int try_step = 0;\
1567              TYPE valuedata[SIZE]; \
1568              TYPE *value = NULL; \
1569              _sym_glGetError(); \
1570              memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1571              initial_ctx->NAME = (TYPE *)calloc(SIZE, sizeof(TYPE));\
1572              initial_ctx->NAME##_updated = (GLboolean *)calloc(SIZE, sizeof(GLboolean)); \
1573              if(api_gl_version <= driver_gl_version) { \
1574                 do { \
1575                    try_step++; \
1576                    SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1577                    if (_sym_glGetError() == GL_INVALID_ENUM) \
1578                    { \
1579                       initial_ctx->NAME##_used = 0; \
1580                       value = valuedata; DEFAULT_STMT; value = valuedata; \
1581                       break; \
1582                    } \
1583                    initial_ctx->NAME##_used = 1; \
1584                    for (i = 0; i < SIZE; i++) \
1585                    { \
1586                       if (*((char *)(&value[i])) == 0xcc) \
1587                       { \
1588                          memset(&value[i], 0xaa, sizeof(TYPE)); \
1589                          SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1590                          if (*((char *)(&value[i])) == 0xaa) \
1591                          { \
1592                             try_step++; \
1593                             if (try_step == 2) \
1594                             { \
1595                                COREGL_WARN("GL-state '"#NAME"' cannot be retrieved"); \
1596                             } \
1597                             break; \
1598                          } \
1599                       } \
1600                       initial_ctx->NAME[i] = value[i]; \
1601                       initial_ctx->NAME##_updated[i] = GL_FALSE; \
1602                    } \
1603                    if (try_step != 2) \
1604                    { \
1605                       value = valuedata; DEFAULT_STMT; value = valuedata; \
1606                       for (i = 0; i < SIZE; i++) \
1607                       { \
1608                          if (initial_ctx->NAME[i] != value[i]) \
1609                          { \
1610                             COREGL_WARN("GL-state '"#NAME"'[%d] value ["PRINTF_CHAR(TYPE)"] is different from SPEC-DEFAULT ["PRINTF_CHAR(TYPE)"]", i, initial_ctx->NAME[i], value[i]); \
1611                          } \
1612                       } \
1613                    } \
1614                 } \
1615                 while (try_step == 2); \
1616              }\
1617         }\
1618       }
1619 #  include "coregl_fastpath_state.h"
1620 # undef SET_GLUE_VALUE
1621 # undef GLUE_STATE
1622 # undef INITIAL_CTX
1623 #endif
1624 # undef _COREGL_END_API
1625 # undef _COREGL_START_API
1626
1627                 if (initial_ctx->gl_vertex_attribs_num[0] > MAX_VERTEX_ATTRIBS) {
1628                         COREGL_WARN("Number of vertex attrib is too big! (%d-%d)",
1629                                                 MAX_VERTEX_ATTRIBS, initial_ctx->gl_vertex_attribs_num[0]);
1630                 }
1631                 if (initial_ctx->gl_tex_units_num[0] > MAX_TEXTURE_UNITS) {
1632                         COREGL_WARN("Number of texture unit is too big! (%d-%d)",
1633                                                 MAX_TEXTURE_UNITS, initial_ctx->gl_tex_units_num[0]);
1634                 }
1635                 if (initial_ctx->gl_transform_feedback_buffer_binding_num[0] >
1636                                 MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS) {
1637                         COREGL_WARN("Number of transform feedback separate attrib is too big! (%d-%d)",
1638                                                 MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS,
1639                                                 initial_ctx->gl_transform_feedback_buffer_binding_num[0]);
1640                 }
1641                 if (initial_ctx->gl_uniform_buffer_binding_num[0] >
1642                                 MAX_UNIFORM_BUFFER_BINDINGS) {
1643                         COREGL_WARN("Number of uniform buffer binding is too big! (%d-%d)",
1644                                                 MAX_UNIFORM_BUFFER_BINDINGS, initial_ctx->gl_uniform_buffer_binding_num[0]);
1645                 }
1646                 if (initial_ctx->gl_shader_storage_buffer_binding_num[0] >
1647                                 MAX_SHADER_STORAGE_BUFFER_BINDINGS) {
1648                         COREGL_WARN("Number of shader storage buffer binding is too big! (%d-%d)",
1649                                                 MAX_SHADER_STORAGE_BUFFER_BINDINGS,
1650                                                 initial_ctx->gl_shader_storage_buffer_binding_num[0]);
1651                 }
1652                 if (initial_ctx->gl_atomic_counter_buffer_binding_num[0] >
1653                                 MAX_ATOMIC_COUNTER_BUFFER_BINDING) {
1654                         COREGL_WARN("Number of uniform buffer binding is too big! (%d-%d)",
1655                                                 MAX_ATOMIC_COUNTER_BUFFER_BINDING,
1656                                                 initial_ctx->gl_atomic_counter_buffer_binding_num[0]);
1657                 }
1658         }
1659
1660         {
1661                 int i;
1662 #define _COREGL_START_API(version) api_gl_version = version;
1663 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1664 #define INITIAL_CTX initial_ctx
1665 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1666       { \
1667         if(SIZE > 0) { \
1668             ctx->NAME = (TYPE *)calloc(SIZE, sizeof(TYPE)); \
1669             ctx->NAME##_updated = (GLboolean *)calloc(SIZE, sizeof(GLboolean)); \
1670             if(api_gl_version <= driver_gl_version) { \
1671                 for (i = 0; i < SIZE; i++) \
1672                 { \
1673                    ctx->NAME[i] = initial_ctx->NAME[i]; \
1674                    ctx->NAME##_used = initial_ctx->NAME##_used; \
1675                    ctx->NAME##_updated[i] = initial_ctx->NAME##_updated[i]; \
1676                 }\
1677             } \
1678         } \
1679       }
1680 # include "coregl_fastpath_state.h"
1681 #undef GLUE_STATE
1682 #undef INITIAL_CTX
1683 #undef _COREGL_START_API
1684 #undef _COREGL_END_API
1685         }
1686
1687         ctx->initialized = 1;
1688         ret = 1;
1689         goto finish;
1690
1691 finish:
1692         AST(mutex_unlock(&init_context_mutex) == 1);
1693
1694         return ret;
1695 }
1696
1697 #ifdef COREGL_USE_MODULE_TRACEPATH
1698 extern void *tracepath_api_trace_begin(const char *name, void *hint, int trace_total_time);
1699 extern void *tracepath_api_trace_end(const char *name, void *hint, int trace_total_time);
1700 #endif
1701
1702 #define CHECK_GL_ERROR(func) \
1703         do { \
1704                 func; \
1705                 int err = _orig_fastpath_glGetError(); \
1706                 if (err != GL_NO_ERROR) { \
1707                         COREGL_ERR("(GL %p) : %s returns GL error 0x%X", oldctx->cstate, #func, err); \
1708                         return 0; \
1709                 } \
1710         } while(0);
1711
1712 #define STATE_COMPARE(state) (oldctx->state != newctx->state)
1713 #define STATES_COMPARE(state_ptr, bytes) (memcmp(oldctx->state_ptr, newctx->state_ptr, bytes) != 0)
1714
1715 GLuint
1716 fastpath_check_context_vertex_attribs(GLGlueContext *oldctx, GLGlueContext *newctx)
1717 {
1718         int i;
1719
1720         if(!(oldctx->_vattrib_flag | newctx->_vattrib_flag)) {
1721                 return 1;
1722         }
1723
1724         for (i = 0; i < oldctx->gl_vertex_attribs_num[0]; i++) {
1725                 if (newctx->gl_vertex_array_buf_id[i] != oldctx->gl_vertex_array_buf_id[i]) {
1726                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER,
1727                                                    newctx->gl_vertex_array_buf_id[i]))
1728                 } else {
1729                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, 0))
1730                 }
1731
1732                 if STATE_COMPARE(gl_vertex_array_divisor[i]) {
1733                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribDivisor(i,
1734                                                    newctx->gl_vertex_array_divisor[i]))
1735                 }
1736
1737                 if (newctx->gl_vertex_array_size[i] != 0) {
1738                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribPointer(i,
1739                                                    newctx->gl_vertex_array_size[i],
1740                                                    newctx->gl_vertex_array_type[i],
1741                                                    newctx->gl_vertex_array_normalized[i],
1742                                                    newctx->gl_vertex_array_stride[i],
1743                                                    newctx->gl_vertex_array_pointer[i]))
1744                 } else {
1745                         if (newctx->gl_vertex_array_integer[0] == GL_TRUE) {
1746                                 if (newctx->gl_vertex_array_type[0] == GL_UNSIGNED_INT) {
1747                                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribI4uiv(i,
1748                                                                    &newctx->gl_vertex_attrib_value_unsigned_integer[4 * i]))
1749                                 } else {
1750                                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribI4iv(i,
1751                                                                    &newctx->gl_vertex_attrib_value_integer[4 * i]))
1752                                 }
1753                         } else {
1754                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttrib4fv(i,
1755                                                            &newctx->gl_vertex_attrib_value[4 * i]))
1756                         }
1757                 }
1758
1759                 if (newctx->gl_vertex_array_enabled[i] == GL_TRUE) {
1760                         CHECK_GL_ERROR(_orig_fastpath_glEnableVertexAttribArray(i))
1761                 } else {
1762                         CHECK_GL_ERROR(_orig_fastpath_glDisableVertexAttribArray(i))
1763                 }
1764         }
1765
1766         return 1;
1767 }
1768
1769 GLuint
1770 fastpath_check_context_bind_1(GLGlueContext *oldctx, GLGlueContext *newctx)
1771 {
1772         if(!(oldctx->_bind_flag1 | newctx->_bind_flag1)) {
1773                 return 1;
1774         }
1775
1776         if STATE_COMPARE(gl_array_buffer_binding[0]) {
1777                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER,
1778                                            newctx->gl_array_buffer_binding[0]))
1779         }
1780
1781         if STATE_COMPARE(gl_element_array_buffer_binding[0]) {
1782                 if STATE_COMPARE(gl_vertex_array_binding[0]) {
1783                         CHECK_GL_ERROR(_orig_fastpath_glBindVertexArray(
1784                                                    newctx->gl_vertex_array_binding[0]))
1785                 }
1786                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
1787                                            newctx->gl_element_array_buffer_binding[0]))
1788         }
1789
1790         if (newctx->gl_framebuffer_binding_read_used == 1 &&
1791                         newctx->gl_framebuffer_binding_draw_used == 1) {
1792                 if STATE_COMPARE(gl_framebuffer_binding_read[0]) {
1793                         if (driver_gl_version >= 2) {
1794                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_READ_FRAMEBUFFER,
1795                                                            newctx->gl_framebuffer_binding_read[0]))
1796                         } else {
1797                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebufferOES(GL_READ_FRAMEBUFFER,
1798                                                            newctx->gl_framebuffer_binding_read[0]))
1799                         }
1800                 }
1801
1802                 if STATE_COMPARE(gl_framebuffer_binding_draw[0]) {
1803                         if (driver_gl_version >= 2) {
1804                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
1805                                                            newctx->gl_framebuffer_binding_draw[0]))
1806                         } else {
1807                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebufferOES(GL_DRAW_FRAMEBUFFER,
1808                                                            newctx->gl_framebuffer_binding_draw[0]))
1809                         }
1810                 }
1811         } else {
1812                 if STATE_COMPARE(gl_framebuffer_binding[0]) {
1813                         if (driver_gl_version >= 2) {
1814                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_FRAMEBUFFER,
1815                                                            newctx->gl_framebuffer_binding[0]))
1816                         } else {
1817                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebufferOES(GL_FRAMEBUFFER,
1818                                                            newctx->gl_framebuffer_binding[0]))
1819                         }
1820                 }
1821         }
1822
1823         if STATE_COMPARE(gl_renderbuffer_binding[0]) {
1824                 CHECK_GL_ERROR(_orig_fastpath_glBindRenderbuffer(GL_RENDERBUFFER,
1825                                            newctx->gl_renderbuffer_binding[0]))
1826         }
1827
1828         return 1;
1829 }
1830
1831 GLuint
1832 fastpath_check_context_bind_2(GLGlueContext *oldctx, GLGlueContext *newctx)
1833 {
1834         int i;
1835
1836         if(!(oldctx->_bind_flag2 | newctx->_bind_flag2)) {
1837                 return 1;
1838         }
1839
1840         if STATE_COMPARE(gl_copy_read_buffer_binding[0]) {
1841                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_COPY_READ_BUFFER,
1842                                            newctx->gl_copy_read_buffer_binding[0]))
1843         }
1844
1845         if STATE_COMPARE(gl_copy_write_buffer_binding[0]) {
1846                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_COPY_WRITE_BUFFER,
1847                                            newctx->gl_copy_write_buffer_binding[0]))
1848         }
1849
1850         if STATE_COMPARE(gl_pixel_pack_buffer_binding[0]) {
1851                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_PIXEL_PACK_BUFFER,
1852                                            newctx->gl_pixel_pack_buffer_binding[0]))
1853         }
1854
1855         if STATE_COMPARE(gl_pixel_unpack_buffer_binding[0]) {
1856                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_PIXEL_UNPACK_BUFFER,
1857                                            newctx->gl_pixel_unpack_buffer_binding[0]))
1858         }
1859
1860         if STATE_COMPARE(gl_transform_feedback_buffer_binding[0]) {
1861                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER,
1862                                            newctx->gl_transform_feedback_buffer_binding[0]))
1863         }
1864
1865         if STATE_COMPARE(gl_uniform_buffer_binding[0]) {
1866                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_UNIFORM_BUFFER,
1867                                            newctx->gl_uniform_buffer_binding[0]))
1868         }
1869         /* handle states set in fastpath_glBindBufferBase() */
1870         for (i = 0; i < newctx->gl_transform_feedback_buffer_binding_num[0]; i++) {
1871                 if STATE_COMPARE(gl_transform_feedback_buffer_binding_array[i]) {
1872                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, i,
1873                                                    newctx->gl_transform_feedback_buffer_binding_array[i]))
1874                 }
1875         }
1876
1877         for (i = 0; i < newctx->gl_uniform_buffer_binding_num[0]; i++) {
1878                 if STATE_COMPARE(gl_uniform_buffer_binding_array[i]) {
1879                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferBase(GL_UNIFORM_BUFFER, i,
1880                                                    newctx->gl_uniform_buffer_binding_array[i]))
1881                 }
1882         }
1883
1884         return 1;
1885 }
1886
1887 GLuint
1888 fastpath_check_context_bind_3(GLGlueContext *oldctx, GLGlueContext *newctx)
1889 {
1890         int i;
1891
1892         if(!(oldctx->_bind_flag3 | newctx->_bind_flag3)) {
1893                 return 1;
1894         }
1895
1896         if STATE_COMPARE(gl_shader_storage_buffer_binding[0]) {
1897                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_SHADER_STORAGE_BUFFER,
1898                                            newctx->gl_shader_storage_buffer_binding[0]))
1899         }
1900
1901         if STATE_COMPARE(gl_atomic_counter_buffer_binding[0]) {
1902                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ATOMIC_COUNTER_BUFFER,
1903                                            newctx->gl_atomic_counter_buffer_binding[0]))
1904         }
1905
1906         if STATE_COMPARE(gl_draw_indirect_buffer_binding[0]) {
1907                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_DRAW_INDIRECT_BUFFER,
1908                                            newctx->gl_draw_indirect_buffer_binding[0]))
1909         }
1910
1911         if STATE_COMPARE(gl_dispatch_indirect_buffer_binding[0]) {
1912                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER,
1913                                            newctx->gl_dispatch_indirect_buffer_binding[0]))
1914         }
1915
1916         if STATE_COMPARE(gl_texture_buffer_binding[0]) {
1917                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_TEXTURE_BUFFER,
1918                                            newctx->gl_texture_buffer_binding[0]))
1919         }
1920
1921         /* handle states set in fastpath_glBindBufferBase() */
1922         for (i = 0; i < newctx->gl_shader_storage_buffer_binding_num[0]; i++) {
1923                 if STATE_COMPARE(gl_shader_storage_buffer_binding_array[i]) {
1924                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferBase(GL_SHADER_STORAGE_BUFFER, i,
1925                                                    newctx->gl_shader_storage_buffer_binding_array[i]))
1926                 }
1927         }
1928
1929         for (i = 0; i < newctx->gl_atomic_counter_buffer_binding_num[0]; i++) {
1930                 if STATE_COMPARE(gl_atomic_counter_buffer_binding_array[i]) {
1931                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i,
1932                                                    newctx->gl_atomic_counter_buffer_binding_array[i]))
1933                 }
1934         }
1935
1936         return 1;
1937 }
1938
1939 GLuint
1940 fastpath_check_context_bind_4(GLGlueContext *oldctx, GLGlueContext *newctx)
1941 {
1942         int i;
1943
1944         if(!(oldctx->_bind_flag4 | newctx->_bind_flag4)) {
1945                 return 1;
1946         }
1947
1948         /* handle states set in fastpath_BindBufferRrange() */
1949         for (i = 0; i < newctx->gl_transform_feedback_buffer_binding_num[0]; i++) {
1950                 if (STATE_COMPARE(gl_transform_feedback_buffer_range_binding_array[i]) ||
1951                                 STATE_COMPARE(gl_transform_feedback_buffer_range_binding_array_offset[i]) ||
1952                                 STATE_COMPARE(gl_transform_feedback_buffer_range_binding_array_size[i])) {
1953                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, i,
1954                                                    newctx->gl_transform_feedback_buffer_range_binding_array[i],
1955                                                    newctx->gl_transform_feedback_buffer_range_binding_array_offset[i],
1956                                                    newctx->gl_transform_feedback_buffer_range_binding_array_size[i]))
1957                 }
1958         }
1959
1960         for (i = 0; i < newctx->gl_uniform_buffer_binding_num[0]; i++) {
1961                 if (STATE_COMPARE(gl_uniform_buffer_range_binding_array[i]) ||
1962                                 STATE_COMPARE(gl_uniform_buffer_range_binding_array_offset[i]) ||
1963                                 STATE_COMPARE(gl_uniform_buffer_range_binding_array_size[i])) {
1964                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferRange(GL_UNIFORM_BUFFER, i,
1965                                                    newctx->gl_uniform_buffer_range_binding_array[i],
1966                                                    newctx->gl_uniform_buffer_range_binding_array_offset[i],
1967                                                    newctx->gl_uniform_buffer_range_binding_array_size[i]))
1968                 }
1969         }
1970
1971         for (i = 0; i < newctx->gl_shader_storage_buffer_binding_num[0]; i++) {
1972                 if (STATE_COMPARE(gl_shader_storage_buffer_range_binding_array[i]) ||
1973                                 STATE_COMPARE(gl_shader_storage_buffer_range_binding_array_offset[i]) ||
1974                                 STATE_COMPARE(gl_shader_storage_buffer_range_binding_array_size[i])) {
1975                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferRange(GL_SHADER_STORAGE_BUFFER, i,
1976                                                    newctx->gl_shader_storage_buffer_range_binding_array[i],
1977                                                    newctx->gl_shader_storage_buffer_range_binding_array_offset[i],
1978                                                    newctx->gl_shader_storage_buffer_range_binding_array_size[i]))
1979                 }
1980         }
1981
1982         for (i = 0; i < newctx->gl_atomic_counter_buffer_binding_num[0]; i++) {
1983                 if (STATE_COMPARE(gl_atomic_counter_buffer_range_binding_array[i]) ||
1984                                 STATE_COMPARE(gl_atomic_counter_buffer_range_binding_array_offset[i]) ||
1985                                 STATE_COMPARE(gl_atomic_counter_buffer_range_binding_array_size[i])) {
1986                         CHECK_GL_ERROR(_orig_fastpath_glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, i,
1987                                                    newctx->gl_atomic_counter_buffer_range_binding_array[i],
1988                                                    newctx->gl_atomic_counter_buffer_range_binding_array_offset[i],
1989                                                    newctx->gl_atomic_counter_buffer_range_binding_array_size[i]))
1990                 }
1991         }
1992
1993         return 1;
1994 }
1995
1996 GLuint
1997 fastpath_check_context_enable_1(GLGlueContext *oldctx, GLGlueContext *newctx)
1998 {
1999         if(!(oldctx->_enable_flag1 | newctx->_enable_flag1)) {
2000                 return 1;
2001         }
2002
2003         if STATE_COMPARE(gl_blend[0]) {
2004                 if (newctx->gl_blend[0]) {
2005                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_BLEND))
2006                 } else {
2007                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_BLEND))
2008                 }
2009         }
2010
2011         if STATE_COMPARE(gl_cull_face[0]) {
2012                 if (newctx->gl_cull_face[0]) {
2013                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_CULL_FACE))
2014                 } else {
2015                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_CULL_FACE))
2016                 }
2017         }
2018
2019         if STATE_COMPARE(gl_depth_test[0]) {
2020                 if (newctx->gl_depth_test[0]) {
2021                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DEPTH_TEST))
2022                 } else {
2023                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DEPTH_TEST))
2024                 }
2025         }
2026
2027         if STATE_COMPARE(gl_dither[0]) {
2028                 if (newctx->gl_dither[0]) {
2029                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DITHER))
2030                 } else {
2031                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DITHER))
2032                 }
2033         }
2034
2035         return 1;
2036 }
2037
2038 GLuint
2039 fastpath_check_context_enable_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2040 {
2041         if(!(oldctx->_enable_flag2 | newctx->_enable_flag2)) {
2042                 return 1;
2043         }
2044
2045         if STATE_COMPARE(gl_polygon_offset_fill[0]) {
2046                 if (newctx->gl_polygon_offset_fill[0]) {
2047                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_POLYGON_OFFSET_FILL))
2048                 } else {
2049                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_POLYGON_OFFSET_FILL))
2050                 }
2051         }
2052
2053         if STATE_COMPARE(gl_sample_alpha_to_coverage[0]) {
2054                 if (newctx->gl_sample_alpha_to_coverage[0]) {
2055                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE))
2056                 } else {
2057                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE))
2058                 }
2059         }
2060
2061         if STATE_COMPARE(gl_sample_coverage[0]) {
2062                 if (newctx->gl_sample_coverage[0]) {
2063                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_COVERAGE))
2064                 } else {
2065                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_COVERAGE))
2066                 }
2067         }
2068
2069         if STATE_COMPARE(gl_scissor_test[0]) {
2070                 if (newctx->gl_scissor_test[0]) {
2071                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SCISSOR_TEST))
2072                 } else {
2073                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SCISSOR_TEST))
2074                 }
2075         }
2076
2077         if STATE_COMPARE(gl_stencil_test[0]) {
2078                 if (newctx->gl_stencil_test[0]) {
2079                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_STENCIL_TEST))
2080                 } else {
2081                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_STENCIL_TEST))
2082                 }
2083         }
2084
2085         if STATE_COMPARE(gl_sample_shading_oes[0]) {
2086                 if (newctx->gl_sample_shading_oes[0]) {
2087                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_SHADING_OES))
2088                 } else {
2089                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_SHADING_OES))
2090                 }
2091         }
2092
2093         if STATE_COMPARE(gl_sample_mask[0]) {
2094                 if (newctx->gl_sample_mask[0]) {
2095                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_MASK))
2096                 } else {
2097                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_MASK))
2098                 }
2099         }
2100
2101         return 1;
2102 }
2103
2104 GLuint
2105 fastpath_check_context_enable_3(GLGlueContext *oldctx, GLGlueContext *newctx)
2106 {
2107         if(!(oldctx->_enable_flag3 | newctx->_enable_flag3)) {
2108                 return 1;
2109         }
2110
2111         if STATE_COMPARE(gl_primitive_restart_fixed_index[0]) {
2112                 if (newctx->gl_primitive_restart_fixed_index[0]) {
2113                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX))
2114                 } else {
2115                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_PRIMITIVE_RESTART_FIXED_INDEX))
2116                 }
2117         }
2118
2119         if STATE_COMPARE(gl_rasterizer_discard[0]) {
2120                 if (newctx->gl_rasterizer_discard[0]) {
2121                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_RASTERIZER_DISCARD))
2122                 } else {
2123                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_RASTERIZER_DISCARD))
2124                 }
2125         }
2126
2127         if STATE_COMPARE(gl_blend_advanced_coherent_khr[0]) {
2128                 if (newctx->gl_blend_advanced_coherent_khr[0]) {
2129                         CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_BLEND_ADVANCED_COHERENT_KHR))
2130                 } else {
2131                         CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_BLEND_ADVANCED_COHERENT_KHR))
2132                 }
2133         }
2134
2135         return 1;
2136 }
2137
2138 GLuint
2139 fastpath_check_context_clear_1(GLGlueContext *oldctx, GLGlueContext *newctx)
2140 {
2141         if(!(oldctx->_clear_flag1 | newctx->_clear_flag1)) {
2142                 return 1;
2143         }
2144
2145         /* Viewport */
2146         if STATES_COMPARE(gl_viewport, 4 * sizeof(GLint)) {
2147                 CHECK_GL_ERROR(_orig_fastpath_glViewport(newctx->gl_viewport[0],
2148                                            newctx->gl_viewport[1],
2149                                            newctx->gl_viewport[2],
2150                                            newctx->gl_viewport[3]))
2151         }
2152
2153         if STATE_COMPARE(gl_current_program[0]) {
2154                 CHECK_GL_ERROR(_orig_fastpath_glUseProgram(newctx->gl_current_program[0]))
2155         }
2156
2157         if STATES_COMPARE(gl_color_clear_value, 4 * sizeof(GLclampf)) {
2158                 CHECK_GL_ERROR(_orig_fastpath_glClearColor(newctx->gl_color_clear_value[0],
2159                                            newctx->gl_color_clear_value[1],
2160                                            newctx->gl_color_clear_value[2],
2161                                            newctx->gl_color_clear_value[3]))
2162         }
2163
2164         return 1;
2165 }
2166
2167 GLuint
2168 fastpath_check_context_clear_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2169 {
2170         int i;
2171
2172         if(!(oldctx->_clear_flag2 | newctx->_clear_flag2)) {
2173                 return 1;
2174         }
2175
2176         if STATES_COMPARE(gl_color_writemask, 4 * GL_CLOR_WRITEMASK_NUM * sizeof(GLboolean)) {
2177                 for (i = 0; i < GL_CLOR_WRITEMASK_NUM; i++) {
2178                         if(NULL != _orig_fastpath_glColorMaski) {
2179                                 CHECK_GL_ERROR(_orig_fastpath_glColorMaski(i,
2180                                                            (newctx->gl_color_writemask + i)[0],
2181                                                            (newctx->gl_color_writemask + i)[1],
2182                                                            (newctx->gl_color_writemask + i)[2],
2183                                                            (newctx->gl_color_writemask + i)[3]))
2184                         }
2185                 }
2186         }
2187
2188         if STATES_COMPARE(gl_depth_range, 2 * sizeof(GLclampf)) {
2189                 CHECK_GL_ERROR(_orig_fastpath_glDepthRangef(newctx->gl_depth_range[0],
2190                                            newctx->gl_depth_range[1]))
2191         }
2192
2193         if STATE_COMPARE(gl_depth_clear_value[0]) {
2194                 CHECK_GL_ERROR(_orig_fastpath_glClearDepthf(newctx->gl_depth_clear_value[0]))
2195         }
2196
2197         if STATE_COMPARE(gl_depth_func[0]) {
2198                 CHECK_GL_ERROR(_orig_fastpath_glDepthFunc(newctx->gl_depth_func[0]))
2199         }
2200
2201         if STATE_COMPARE(gl_depth_writemask[0]) {
2202                 CHECK_GL_ERROR(_orig_fastpath_glDepthMask(newctx->gl_depth_writemask[0]))
2203         }
2204
2205         if STATE_COMPARE(gl_cull_face_mode[0]) {
2206                 CHECK_GL_ERROR(_orig_fastpath_glCullFace(newctx->gl_cull_face_mode[0]))
2207         }
2208
2209         return 1;
2210 }
2211
2212 GLuint
2213 fastpath_check_context_tex_1(GLGlueContext *oldctx, GLGlueContext *newctx)
2214 {
2215         int i;
2216
2217         if(!(oldctx->_tex_flag1 | newctx->_tex_flag1)) {
2218                 return 1;
2219         }
2220
2221         for (i = 0; i < oldctx->gl_tex_units_num[0]; i++) {
2222                 if STATE_COMPARE(gl_tex_2d_state[i]) {
2223                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2224                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D,
2225                                                    newctx->gl_tex_2d_state[i]))
2226                 }
2227
2228                 if STATE_COMPARE(gl_tex_3d_state[i]) {
2229                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2230                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_3D,
2231                                                    newctx->gl_tex_3d_state[i]))
2232                 }
2233
2234                 if STATE_COMPARE(gl_tex_2d_array_state[i]) {
2235                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2236                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D_ARRAY,
2237                                                    newctx->gl_tex_2d_array_state[i]))
2238                 }
2239
2240                 if STATE_COMPARE(gl_tex_cube_state[i]) {
2241                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2242                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_CUBE_MAP,
2243                                                    newctx->gl_tex_cube_state[i]))
2244                 }
2245
2246                 if STATE_COMPARE(gl_tex_external_oes_state[i]) {
2247                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2248                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_EXTERNAL_OES,
2249                                                    newctx->gl_tex_external_oes_state[i]))
2250                 }
2251         }
2252
2253         /* Restore active texture */
2254         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(newctx->gl_active_texture[0]))
2255
2256         if STATE_COMPARE(gl_generate_mipmap_hint[0]) {
2257                 CHECK_GL_ERROR(_orig_fastpath_glHint(GL_GENERATE_MIPMAP_HINT,
2258                                            newctx->gl_generate_mipmap_hint[0]))
2259         }
2260
2261         return 1;
2262 }
2263
2264 GLuint
2265 fastpath_check_context_tex_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2266 {
2267         int i;
2268
2269         if(!(oldctx->_tex_flag2 | newctx->_tex_flag2)) {
2270                 return 1;
2271         }
2272
2273         for (i = 0; i < oldctx->gl_tex_units_num[0]; i++) {
2274                 if STATE_COMPARE(gl_tex_buffer_ext_state[i]) {
2275                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2276                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_BUFFER_EXT,
2277                                                    newctx->gl_tex_buffer_ext_state[i]))
2278                 }
2279
2280                 if STATE_COMPARE(gl_tex_2d_multisample_state[i]) {
2281                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2282                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,
2283                                                    newctx->gl_tex_2d_multisample_state[i]))
2284                 }
2285
2286                 if STATE_COMPARE(gl_tex_cube_map_array_ext_state[i]) {
2287                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2288                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY_EXT,
2289                                                    newctx->gl_tex_cube_map_array_ext_state[i]))
2290                 }
2291
2292                 if STATE_COMPARE(gl_tex_2d_multisample_array_oes_state[i]) {
2293                         CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
2294                         CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY_OES,
2295                                                    newctx->gl_tex_2d_multisample_array_oes_state[i]))
2296                 }
2297         }
2298
2299         return 1;
2300 }
2301
2302 GLuint
2303 fastpath_check_context_blend(GLGlueContext *oldctx, GLGlueContext *newctx)
2304 {
2305         if(!(oldctx->_blend_flag | newctx->_blend_flag)) {
2306                 return 1;
2307         }
2308
2309         if STATES_COMPARE(gl_blend_color, 4 * sizeof(GLclampf)) {
2310                 CHECK_GL_ERROR(_orig_fastpath_glBlendColor(newctx->gl_blend_color[0],
2311                                            newctx->gl_blend_color[1],
2312                                            newctx->gl_blend_color[2],
2313                                            newctx->gl_blend_color[3]))
2314         }
2315
2316         if ((oldctx->gl_blend_src_rgb[0] != newctx->gl_blend_src_rgb[0]) ||
2317                         (oldctx->gl_blend_dst_rgb[0] != newctx->gl_blend_dst_rgb[0]) ||
2318                         (oldctx->gl_blend_src_alpha[0] != newctx->gl_blend_src_alpha[0]) ||
2319                         (oldctx->gl_blend_dst_alpha[0] != newctx->gl_blend_dst_alpha[0])) {
2320                 CHECK_GL_ERROR(_orig_fastpath_glBlendFuncSeparate(newctx->gl_blend_src_rgb[0],
2321                                            newctx->gl_blend_dst_rgb[0],
2322                                            newctx->gl_blend_src_alpha[0],
2323                                            newctx->gl_blend_dst_alpha[0]))
2324         }
2325
2326         if ((oldctx->gl_blend_equation_rgb[0] != newctx->gl_blend_equation_rgb[0]) ||
2327                         (oldctx->gl_blend_equation_alpha[0] != newctx->gl_blend_equation_alpha[0])) {
2328                 CHECK_GL_ERROR(_orig_fastpath_glBlendEquationSeparate(
2329                                            newctx->gl_blend_equation_rgb[0],
2330                                            newctx->gl_blend_equation_alpha[0]))
2331         }
2332
2333         return 1;
2334 }
2335
2336 GLuint
2337 fastpath_check_context_stencil_1(GLGlueContext *oldctx, GLGlueContext *newctx)
2338 {
2339         if(!(oldctx->_stencil_flag1 | newctx->_stencil_flag1)) {
2340                 return 1;
2341         }
2342
2343         if ((oldctx->gl_stencil_func[0] != newctx->gl_stencil_func[0]) ||
2344                         (oldctx->gl_stencil_ref[0]  != newctx->gl_stencil_ref[0]) ||
2345                         (oldctx->gl_stencil_value_mask[0] != newctx->gl_stencil_value_mask[0])) {
2346                 CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_FRONT,
2347                                            newctx->gl_stencil_func[0],
2348                                            newctx->gl_stencil_ref[0],
2349                                            newctx->gl_stencil_value_mask[0]))
2350         }
2351
2352         if ((oldctx->gl_stencil_fail[0] != newctx->gl_stencil_fail[0]) ||
2353                         (oldctx->gl_stencil_pass_depth_fail[0] != newctx->gl_stencil_pass_depth_fail[0]) ||
2354                         (oldctx->gl_stencil_pass_depth_pass[0] != newctx->gl_stencil_pass_depth_pass[0])) {
2355                 CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_FRONT,
2356                                            newctx->gl_stencil_fail[0],
2357                                            newctx->gl_stencil_pass_depth_fail[0],
2358                                            newctx->gl_stencil_pass_depth_pass[0]))
2359         }
2360
2361         if STATE_COMPARE(gl_stencil_writemask[0]) {
2362                 CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_FRONT,
2363                                            newctx->gl_stencil_writemask[0]))
2364         }
2365
2366         return 1;
2367 }
2368
2369 GLuint
2370 fastpath_check_context_stencil_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2371 {
2372         if(!(oldctx->_stencil_flag2 | newctx->_stencil_flag2)) {
2373                 return 1;
2374         }
2375
2376         if ((oldctx->gl_stencil_back_func[0] != newctx->gl_stencil_back_func[0]) ||
2377                         (oldctx->gl_stencil_back_ref[0]  != newctx->gl_stencil_back_ref[0]) ||
2378                         (oldctx->gl_stencil_back_value_mask[0] != newctx->gl_stencil_back_value_mask[0])) {
2379                 CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_BACK,
2380                                            newctx->gl_stencil_back_func[0],
2381                                            newctx->gl_stencil_back_ref[0],
2382                                            newctx->gl_stencil_back_value_mask[0]))
2383         }
2384
2385         if ((oldctx->gl_stencil_back_fail[0] != newctx->gl_stencil_back_fail[0]) ||
2386                         (oldctx->gl_stencil_back_pass_depth_fail[0] != newctx->gl_stencil_back_pass_depth_fail[0]) ||
2387                         (oldctx->gl_stencil_back_pass_depth_pass[0] != newctx->gl_stencil_back_pass_depth_pass[0])) {
2388                 CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_BACK,
2389                                            newctx->gl_stencil_back_fail[0],
2390                                            newctx->gl_stencil_back_pass_depth_fail[0],
2391                                            newctx->gl_stencil_back_pass_depth_pass[0]))
2392         }
2393
2394         if STATE_COMPARE(gl_stencil_back_writemask[0]) {
2395                 CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_BACK,
2396                                            newctx->gl_stencil_back_writemask[0]))
2397         }
2398
2399         if STATE_COMPARE(gl_stencil_clear_value[0]) {
2400                 CHECK_GL_ERROR(_orig_fastpath_glClearStencil(newctx->gl_stencil_clear_value[0]))
2401         }
2402
2403         return 1;
2404 }
2405
2406 GLuint
2407 fastpath_check_context_pixel_1(GLGlueContext *oldctx, GLGlueContext *newctx)
2408 {
2409         if(!(oldctx->_pixel_flag1 | newctx->_pixel_flag1)) {
2410                 return 1;
2411         }
2412
2413         if STATE_COMPARE(gl_pack_row_length[0]) {
2414                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ROW_LENGTH,
2415                                            newctx->gl_pack_row_length[0]))
2416         }
2417
2418         if STATE_COMPARE(gl_pack_skip_rows[0]) {
2419                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_SKIP_ROWS,
2420                                            newctx->gl_pack_skip_rows[0]))
2421         }
2422
2423         if STATE_COMPARE(gl_pack_skip_pixels[0]) {
2424                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_SKIP_PIXELS,
2425                                            newctx->gl_pack_skip_pixels[0]))
2426         }
2427
2428         if STATE_COMPARE(gl_pack_alignment[0]) {
2429                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ALIGNMENT,
2430                                            newctx->gl_pack_alignment[0]))
2431         }
2432
2433         return 1;
2434 }
2435
2436 GLuint
2437 fastpath_check_context_pixel_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2438 {
2439         if(!(oldctx->_pixel_flag2 | newctx->_pixel_flag2)) {
2440                 return 1;
2441         }
2442
2443         if STATE_COMPARE(gl_unpack_row_length[0]) {
2444                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ROW_LENGTH,
2445                                            newctx->gl_unpack_row_length[0]))
2446         }
2447
2448         if STATE_COMPARE(gl_unpack_skip_rows[0]) {
2449                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_ROWS,
2450                                            newctx->gl_unpack_skip_rows[0]))
2451         }
2452
2453         if STATE_COMPARE(gl_unpack_skip_pixels[0]) {
2454                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_PIXELS,
2455                                            newctx->gl_unpack_skip_pixels[0]))
2456         }
2457
2458         if STATE_COMPARE(gl_unpack_alignment[0]) {
2459                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ALIGNMENT,
2460                                            newctx->gl_unpack_alignment[0]))
2461         }
2462
2463         if STATE_COMPARE(gl_unpack_image_height[0]) {
2464                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2465                                            newctx->gl_unpack_image_height[0]))
2466         }
2467
2468         if STATE_COMPARE(gl_unpack_skip_images[0]) {
2469                 CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_IMAGES,
2470                                            newctx->gl_unpack_skip_images[0]))
2471         }
2472
2473         return 1;
2474 }
2475
2476 GLuint
2477 fastpath_check_context_misc_1(GLGlueContext *oldctx, GLGlueContext *newctx)
2478 {
2479         if(!(oldctx->_misc_flag1 | newctx->_misc_flag1)) {
2480                 return 1;
2481         }
2482
2483         if STATE_COMPARE(gl_front_face[0]) {
2484                 CHECK_GL_ERROR(_orig_fastpath_glFrontFace(newctx->gl_front_face[0]))
2485         }
2486
2487         if STATE_COMPARE(gl_line_width[0]) {
2488                 CHECK_GL_ERROR(_orig_fastpath_glLineWidth(newctx->gl_line_width[0]))
2489         }
2490
2491         if ((oldctx->gl_polygon_offset_factor[0] != newctx->gl_polygon_offset_factor[0]) ||
2492                         (oldctx->gl_polygon_offset_units[0]  != newctx->gl_polygon_offset_units[0])) {
2493                 CHECK_GL_ERROR(_orig_fastpath_glPolygonOffset(
2494                                            newctx->gl_polygon_offset_factor[0],
2495                                            newctx->gl_polygon_offset_units[0]))
2496         }
2497
2498         if ((oldctx->gl_sample_coverage_value[0] !=     newctx->gl_sample_coverage_value[0]) ||
2499                         (oldctx->gl_sample_coverage_invert[0] != newctx->gl_sample_coverage_invert[0])) {
2500                 CHECK_GL_ERROR(_orig_fastpath_glSampleCoverage(
2501                                            newctx->gl_sample_coverage_value[0],
2502                                            newctx->gl_sample_coverage_invert[0]))
2503         }
2504
2505         if STATE_COMPARE(gl_fragment_shader_derivative_hint[0]) {
2506                 CHECK_GL_ERROR(_orig_fastpath_glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES,
2507                                            newctx->gl_fragment_shader_derivative_hint[0]))
2508         }
2509
2510         return 1;
2511 }
2512
2513 GLuint
2514 fastpath_check_context_misc_2(GLGlueContext *oldctx, GLGlueContext *newctx)
2515 {
2516         if(!(oldctx->_misc_flag2 | newctx->_misc_flag2)) {
2517                 return 1;
2518         }
2519
2520         if STATES_COMPARE(gl_scissor_box, 4 * sizeof(GLint)) {
2521                 CHECK_GL_ERROR(_orig_fastpath_glScissor(newctx->gl_scissor_box[0],
2522                                            newctx->gl_scissor_box[1],
2523                                            newctx->gl_scissor_box[2],
2524                                            newctx->gl_scissor_box[3]))
2525         }
2526
2527         return 1;
2528 }
2529
2530 GLuint
2531 fastpath_check_context_misc_3(GLGlueContext *oldctx, GLGlueContext *newctx)
2532 {
2533         if(!(oldctx->_misc_flag3 | newctx->_misc_flag3)) {
2534                 return 1;
2535         }
2536
2537         if STATE_COMPARE(gl_read_buffer[0]) {
2538                 CHECK_GL_ERROR(_orig_fastpath_glReadBuffer(newctx->gl_read_buffer[0]))
2539         }
2540
2541         if STATES_COMPARE(gl_draw_buffers, oldctx->gl_draw_buffers_num[0] * sizeof(GLenum)) {
2542                 int drawBuffSize = 16;
2543                 /* If the  context has only default framebuffer, then size of glDrawBuffers can only be 1 */
2544                 if (fastpath_ostate_has_object_type(&newctx->ostate, GL_OBJECT_TYPE_FRAMEBUFFER) == 0) {
2545                         drawBuffSize = 1;
2546                 }
2547
2548                 CHECK_GL_ERROR(_orig_fastpath_glDrawBuffers(drawBuffSize, newctx->gl_draw_buffers))
2549         }
2550
2551         if (oldctx->gl_transform_feedback_active[0] == GL_TRUE &&
2552                         oldctx->gl_transform_feedback_paused[0] == GL_FALSE) {
2553                 CHECK_GL_ERROR(_orig_fastpath_glPauseTransformFeedback())
2554         }
2555
2556         if STATE_COMPARE(gl_transform_feedback_binding[0]) {
2557                 CHECK_GL_ERROR(_orig_fastpath_glBindTransformFeedback(GL_TRANSFORM_FEEDBACK,
2558                                            newctx->gl_transform_feedback_binding[0]))
2559         }
2560
2561         if (newctx->gl_transform_feedback_active[0] == GL_TRUE &&
2562                         newctx->gl_transform_feedback_paused[0] == GL_FALSE) {
2563                 CHECK_GL_ERROR(_orig_fastpath_glResumeTransformFeedback())
2564         }
2565
2566         return 1;
2567 }
2568
2569 #ifdef COREGL_USE_MODULE_TRACEPATH
2570 #define TRACEPATH_API_TRACE_BEGIN(funcname, hint, trace_total_time) \
2571                 tracepath_api_trace_begin(funcname, hint, trace_total_time)
2572 #define TRACEPATH_API_TRACE_END(funcname, hint, trace_total_time) \
2573                 tracepath_api_trace_end(funcname, hint, trace_total_time)
2574 #else
2575 #define TRACEPATH_API_TRACE_BEGIN(funcname, hint, trace_total_time)
2576 #define TRACEPATH_API_TRACE_END(funcname, hint, trace_total_time)
2577 #endif
2578
2579 int
2580 fastpath_make_context_current(GLGlueContext *oldctx, GLGlueContext *newctx)
2581 {
2582         int ret = 0;
2583
2584         if (debug_nofp == 1) {
2585                 ret = 1;
2586                 goto finish;
2587         }
2588
2589         /* Return if they're the same */
2590         if (oldctx == newctx) {
2591                 ret = 1;
2592                 goto finish;
2593         }
2594
2595         static void *trace_hint_glfinish = NULL;
2596         trace_hint_glfinish = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP glFinish)",
2597                                                   trace_hint_glfinish, 0);
2598
2599         int err = _orig_fastpath_glGetError();
2600         if (err != GL_NO_ERROR && oldctx->gl_error == GL_NO_ERROR) {
2601                 oldctx->gl_error = err;
2602         }
2603
2604         CHECK_GL_ERROR(_orig_fastpath_glFlush())
2605
2606         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
2607
2608         static void *trace_hint_vertex_attrib = NULL;
2609         trace_hint_vertex_attrib = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP vertex attrib)",
2610                                                            trace_hint_vertex_attrib, 0);
2611
2612         if(!fastpath_check_context_vertex_attribs(oldctx, newctx)) {
2613                 goto finish;
2614         }
2615
2616         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP vertex attrib)",     trace_hint_vertex_attrib, 0);
2617
2618         static void *trace_hint_bindbuffers = NULL;
2619         trace_hint_bindbuffers = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP bind buffers)",
2620                                                          trace_hint_bindbuffers, 0);
2621
2622         if(!fastpath_check_context_bind_1(oldctx, newctx)) {
2623                 goto finish;
2624         }
2625
2626         if(!fastpath_check_context_bind_2(oldctx, newctx)) {
2627                 goto finish;
2628         }
2629
2630         if(!fastpath_check_context_bind_3(oldctx, newctx)) {
2631                 goto finish;
2632         }
2633
2634         if(!fastpath_check_context_bind_4(oldctx, newctx)) {
2635                 goto finish;
2636         }
2637
2638         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
2639
2640         static void *trace_hint_enable_states = NULL;
2641         trace_hint_enable_states = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP enable states)",
2642                                                            trace_hint_enable_states, 0);
2643
2644         if(!fastpath_check_context_enable_1(oldctx, newctx)) {
2645                 goto finish;
2646         }
2647
2648         if(!fastpath_check_context_enable_2(oldctx, newctx)) {
2649                 goto finish;
2650         }
2651
2652         if(!fastpath_check_context_enable_3(oldctx, newctx)) {
2653                 goto finish;
2654         }
2655
2656         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP enable states)",     trace_hint_enable_states, 0);
2657
2658         static void *trace_hint_clear_viewport = NULL;
2659         trace_hint_clear_viewport = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP clear/viewport)",
2660                                                                 trace_hint_clear_viewport, 0);
2661
2662         if(!fastpath_check_context_clear_1(oldctx, newctx)) {
2663                 goto finish;
2664         }
2665
2666         if(!fastpath_check_context_clear_2(oldctx, newctx)) {
2667                 goto finish;
2668         }
2669
2670         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP clear/viewport)",
2671                                                         trace_hint_clear_viewport, 0);
2672
2673         static void *trace_hint_bind_textures = NULL;
2674         trace_hint_bind_textures = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP bind textures)",
2675                                                            trace_hint_bind_textures, 0);
2676
2677         if(!fastpath_check_context_tex_1(oldctx, newctx)) {
2678                 goto finish;
2679         }
2680
2681         if(!fastpath_check_context_tex_2(oldctx, newctx)) {
2682                 goto finish;
2683         }
2684
2685         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
2686
2687         static void *trace_hint_etc = NULL;
2688         trace_hint_etc = TRACEPATH_API_TRACE_BEGIN("eglMakeCurrent(FP etc.)",
2689                                          trace_hint_etc, 0);
2690
2691         if(!fastpath_check_context_blend(oldctx, newctx)) {
2692                 goto finish;
2693         }
2694
2695         if(!fastpath_check_context_stencil_1(oldctx, newctx)) {
2696                 goto finish;
2697         }
2698
2699         if(!fastpath_check_context_stencil_2(oldctx, newctx)) {
2700                 goto finish;
2701         }
2702
2703         if(!fastpath_check_context_pixel_1(oldctx, newctx)) {
2704                 goto finish;
2705         }
2706
2707         if(!fastpath_check_context_pixel_2(oldctx, newctx)) {
2708                 goto finish;
2709         }
2710
2711         if(!fastpath_check_context_misc_1(oldctx, newctx)) {
2712                 goto finish;
2713         }
2714
2715         if(!fastpath_check_context_misc_2(oldctx, newctx)) {
2716                 goto finish;
2717         }
2718
2719         if(!fastpath_check_context_misc_3(oldctx, newctx)) {
2720                 goto finish;
2721         }
2722
2723         TRACEPATH_API_TRACE_END("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
2724
2725         ret = 1;
2726
2727 finish:
2728
2729 #ifdef COREGL_FASTPATH_TRACE_STATE_INFO
2730         if (unlikely(get_trace_state_flag() == 1)) {
2731                 fastpath_dump_context_states(newctx, 0);
2732         }
2733
2734 #endif // COREGL_FASTPATH_TRACE_STATE_INFO
2735         return ret;
2736 #undef STATE_COMPARE
2737 #undef STATES_COMPARE
2738 }