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