Add fastpath APIs for OpenGLES_DDK 3.1
[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                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
421                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
422                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
423                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
424                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
425
426                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2DMultisampleEXT);
427                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture3DOES);
428
429                 /* Start overriding GLES 3.0 */
430                 if(driver_gl_version >= COREGL_GLAPI_3) {
431                         COREGL_OVERRIDE(fastpath_, glReadBuffer);
432
433                         COREGL_OVERRIDE(fastpath_, glGenQueries);
434                         COREGL_OVERRIDE(fastpath_, glDeleteQueries);
435                         COREGL_OVERRIDE(fastpath_, glIsQuery);
436                         COREGL_OVERRIDE(fastpath_, glBeginQuery);
437                         COREGL_OVERRIDE(fastpath_, glGetQueryiv);
438                         COREGL_OVERRIDE(fastpath_, glGetQueryObjectuiv);
439                         COREGL_OVERRIDE(fastpath_, glDrawBuffers);
440                         COREGL_OVERRIDE(fastpath_, glFramebufferTextureLayer);
441
442                         COREGL_OVERRIDE(fastpath_, glBindVertexArray);
443                         COREGL_OVERRIDE(fastpath_, glDeleteVertexArrays);
444                         COREGL_OVERRIDE(fastpath_, glGenVertexArrays);
445                         COREGL_OVERRIDE(fastpath_, glIsVertexArray);
446
447                         COREGL_OVERRIDE(fastpath_, glGetIntegeri_v);
448
449                         COREGL_OVERRIDE(fastpath_, glBindTransformFeedback);
450                         COREGL_OVERRIDE(fastpath_, glDeleteTransformFeedbacks);
451                         COREGL_OVERRIDE(fastpath_, glGenTransformFeedbacks);
452                         COREGL_OVERRIDE(fastpath_, glIsTransformFeedback);
453
454                         COREGL_OVERRIDE(fastpath_, glBindBufferRange);
455                         COREGL_OVERRIDE(fastpath_, glBindBufferBase);
456                         COREGL_OVERRIDE(fastpath_, glTransformFeedbackVaryings);
457                         COREGL_OVERRIDE(fastpath_, glGetTransformFeedbackVarying);
458                         COREGL_OVERRIDE(fastpath_, glVertexAttribIPointer);
459                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4i);
460                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4ui);
461                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4iv);
462                         COREGL_OVERRIDE(fastpath_, glVertexAttribI4uiv);
463                         COREGL_OVERRIDE(fastpath_, glGetUniformuiv);
464                         COREGL_OVERRIDE(fastpath_, glGetFragDataLocation);
465                         COREGL_OVERRIDE(fastpath_, glGetStringi);
466                         COREGL_OVERRIDE(fastpath_, glGetUniformIndices);
467                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformsiv);
468                         COREGL_OVERRIDE(fastpath_, glGetUniformBlockIndex);
469                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformBlockiv);
470                         COREGL_OVERRIDE(fastpath_, glGetActiveUniformBlockName);
471                         COREGL_OVERRIDE(fastpath_, glUniformBlockBinding);
472                         COREGL_OVERRIDE(fastpath_, glGetInteger64v);
473                         COREGL_OVERRIDE(fastpath_, glGetInteger64i_v);
474                         COREGL_OVERRIDE(fastpath_, glGenSamplers);
475                         COREGL_OVERRIDE(fastpath_, glDeleteSamplers);
476                         COREGL_OVERRIDE(fastpath_, glIsSampler);
477                         COREGL_OVERRIDE(fastpath_, glBindSampler);
478                         COREGL_OVERRIDE(fastpath_, glSamplerParameteri);
479                         COREGL_OVERRIDE(fastpath_, glSamplerParameteriv);
480                         COREGL_OVERRIDE(fastpath_, glSamplerParameterf);
481                         COREGL_OVERRIDE(fastpath_, glSamplerParameterfv);
482                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameteriv);
483                         COREGL_OVERRIDE(fastpath_, glGetSamplerParameterfv);
484                         COREGL_OVERRIDE(fastpath_, glVertexAttribDivisor);
485                         COREGL_OVERRIDE(fastpath_, glGetProgramBinary);
486                         COREGL_OVERRIDE(fastpath_, glProgramBinary);
487                         COREGL_OVERRIDE(fastpath_, glProgramParameteri);
488                 } // End of GLES 3.0
489
490                 if(driver_gl_version >= COREGL_GLAPI_31)
491                 {
492                         COREGL_OVERRIDE(fastpath_, glCreateShaderProgramv);
493                         COREGL_OVERRIDE(fastpath_, glGenProgramPipelines);
494                         COREGL_OVERRIDE(fastpath_, glGetProgramPipelineiv);
495                         COREGL_OVERRIDE(fastpath_, glBindProgramPipeline);
496                         COREGL_OVERRIDE(fastpath_, glDeleteProgramPipelines);
497                         COREGL_OVERRIDE(fastpath_, glIsProgramPipeline);
498                         COREGL_OVERRIDE(fastpath_, glValidateProgramPipeline);
499                         COREGL_OVERRIDE(fastpath_, glGetProgramPipelineInfoLog);
500                         COREGL_OVERRIDE(fastpath_, glDispatchCompute);
501                         COREGL_OVERRIDE(fastpath_, glDispatchComputeIndirect);
502                         COREGL_OVERRIDE(fastpath_, glGetProgramInterfaceiv);
503                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceIndex);
504                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceName);
505                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceiv);
506                         COREGL_OVERRIDE(fastpath_, glGetProgramResourceLocation);
507                         COREGL_OVERRIDE(fastpath_, glUseProgramStages);
508                         COREGL_OVERRIDE(fastpath_, glActiveShaderProgram);
509                         COREGL_OVERRIDE(fastpath_, glProgramUniform1iv);
510                         COREGL_OVERRIDE(fastpath_, glProgramUniform2iv);
511                         COREGL_OVERRIDE(fastpath_, glProgramUniform3iv);
512                         COREGL_OVERRIDE(fastpath_, glProgramUniform4iv);
513                         COREGL_OVERRIDE(fastpath_, glProgramUniform1fv);
514                         COREGL_OVERRIDE(fastpath_, glProgramUniform2fv);
515                         COREGL_OVERRIDE(fastpath_, glProgramUniform3fv);
516                         COREGL_OVERRIDE(fastpath_, glProgramUniform4fv);
517                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2fv);
518                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3fv);
519                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4fv);
520                         COREGL_OVERRIDE(fastpath_, glProgramUniform1i);
521                         COREGL_OVERRIDE(fastpath_, glProgramUniform2i);
522                         COREGL_OVERRIDE(fastpath_, glProgramUniform3i);
523                         COREGL_OVERRIDE(fastpath_, glProgramUniform4i);
524                         COREGL_OVERRIDE(fastpath_, glProgramUniform1f);
525                         COREGL_OVERRIDE(fastpath_, glProgramUniform2f);
526                         COREGL_OVERRIDE(fastpath_, glProgramUniform3f);
527                         COREGL_OVERRIDE(fastpath_, glProgramUniform4f);
528                         COREGL_OVERRIDE(fastpath_, glProgramUniform1uiv);
529                         COREGL_OVERRIDE(fastpath_, glProgramUniform2uiv);
530                         COREGL_OVERRIDE(fastpath_, glProgramUniform3uiv);
531                         COREGL_OVERRIDE(fastpath_, glProgramUniform4uiv);
532                         COREGL_OVERRIDE(fastpath_, glProgramUniform1ui);
533                         COREGL_OVERRIDE(fastpath_, glProgramUniform2ui);
534                         COREGL_OVERRIDE(fastpath_, glProgramUniform3ui);
535                         COREGL_OVERRIDE(fastpath_, glProgramUniform4ui);
536                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2x3fv);
537                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3x2fv);
538                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4x2fv);
539                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix2x4fv);
540                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix3x4fv);
541                         COREGL_OVERRIDE(fastpath_, glProgramUniformMatrix4x3fv);
542                         COREGL_OVERRIDE(fastpath_, glBindImageTexture);
543                         COREGL_OVERRIDE(fastpath_, glGetBooleani_v);
544                         COREGL_OVERRIDE(fastpath_, glMemoryBarrier);
545                         COREGL_OVERRIDE(fastpath_, glMemoryBarrierByRegion);
546                         COREGL_OVERRIDE(fastpath_, glTexStorage2DMultisample);
547                         COREGL_OVERRIDE(fastpath_, glGetMultisamplefv);
548                         COREGL_OVERRIDE(fastpath_, glSampleMaski);
549                         COREGL_OVERRIDE(fastpath_, glGetTexLevelParameteriv);
550                         COREGL_OVERRIDE(fastpath_, glGetTexLevelParameterfv);
551                         COREGL_OVERRIDE(fastpath_, glBindVertexBuffer);
552                         COREGL_OVERRIDE(fastpath_, glVertexAttribFormat);
553                         COREGL_OVERRIDE(fastpath_, glVertexAttribIFormat);
554                         COREGL_OVERRIDE(fastpath_, glVertexAttribBinding);
555                         COREGL_OVERRIDE(fastpath_, glVertexBindingDivisor);
556                 }
557         }
558         else
559         {
560                 COREGL_LOG("\E[40;35;1m[CoreGL] SKIP GL FASTPATH...\E[0m\n");
561         }
562 }
563
564 #undef OVERRIDE
565
566 static inline GL_Object_Hash_Base *
567 _lock_gl_object_hash(GL_Object_State *ostate, GL_Object_Type type)
568 {
569         switch (type)
570         {
571                 case GL_OBJECT_TYPE_QUERY:
572                         return &ostate->query;
573                 case GL_OBJECT_TYPE_TEXTURE:
574                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
575                         return &ostate->shared->texture;
576                 case GL_OBJECT_TYPE_BUFFER:
577                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
578                         return &ostate->shared->buffer;
579                 case GL_OBJECT_TYPE_FRAMEBUFFER:
580                         return &ostate->framebuffer;
581                 case GL_OBJECT_TYPE_RENDERBUFFER:
582                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
583                         return &ostate->shared->renderbuffer;
584                 case GL_OBJECT_TYPE_PROGRAM:
585                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
586                         return &ostate->shared->program;
587                 case GL_OBJECT_TYPE_VERTEXARRAY:
588                         return &ostate->vertexarray;
589                 case GL_OBJECT_TYPE_SAMPLER:
590                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
591                         return &ostate->shared->sampler;
592                 case GL_OBJECT_TYPE_TRANSFORMFEEDBACK:
593                         return &ostate->transformfeedback;
594                 case GL_OBJECT_TYPE_PROGRAMPIPELINE:
595                         AST(mutex_lock(&ostate->shared->access_mutex) == 1);
596                         return &ostate->shared->programpipeline;
597                 default:
598                         return NULL;
599         }
600 }
601
602 static inline void
603 _unlock_gl_object_hash(GL_Object_State *ostate, GL_Object_Type type)
604 {
605         switch (type)
606         {
607                 case GL_OBJECT_TYPE_TEXTURE:
608                 case GL_OBJECT_TYPE_BUFFER:
609                 case GL_OBJECT_TYPE_RENDERBUFFER:
610                 case GL_OBJECT_TYPE_PROGRAM:
611                 case GL_OBJECT_TYPE_SAMPLER:
612                 case GL_OBJECT_TYPE_PROGRAMPIPELINE:
613                         AST(mutex_unlock(&ostate->shared->access_mutex) == 1);
614                 default:
615                         break;
616         }
617 }
618
619 static inline GL_Object_Hash_Base *
620 _lock_gl_object_hash_real(GL_Object_State *ostate, GL_Object_Type type)
621 {
622         switch (type)
623         {
624                 case GL_OBJECT_TYPE_QUERY:
625                         return &ostate->query_real;
626                 case GL_OBJECT_TYPE_TEXTURE:
627                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
628                         return &ostate->shared->texture_real;
629                 case GL_OBJECT_TYPE_BUFFER:
630                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
631                         return &ostate->shared->buffer_real;
632                 case GL_OBJECT_TYPE_FRAMEBUFFER:
633                         return &ostate->framebuffer_real;
634                 case GL_OBJECT_TYPE_RENDERBUFFER:
635                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
636                         return &ostate->shared->renderbuffer_real;
637                 case GL_OBJECT_TYPE_PROGRAM:
638                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
639                         return &ostate->shared->program_real;
640                 case GL_OBJECT_TYPE_VERTEXARRAY:
641                         return &ostate->vertexarray_real;
642                 case GL_OBJECT_TYPE_SAMPLER:
643                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
644                         return &ostate->shared->sampler_real;
645                 case GL_OBJECT_TYPE_TRANSFORMFEEDBACK:
646                         return &ostate->transformfeedback_real;
647                 case GL_OBJECT_TYPE_PROGRAMPIPELINE:
648                         AST(mutex_lock(&ostate->shared->real_access_mutex) == 1);
649                         return &ostate->shared->programpipeline_real;
650                 default:
651                         return NULL;
652         }
653 }
654
655 static inline void
656 _unlock_gl_object_hash_real(GL_Object_State *ostate, GL_Object_Type type)
657 {
658         switch (type)
659         {
660                 case GL_OBJECT_TYPE_TEXTURE:
661                 case GL_OBJECT_TYPE_BUFFER:
662                 case GL_OBJECT_TYPE_RENDERBUFFER:
663                 case GL_OBJECT_TYPE_PROGRAM:
664                 case GL_OBJECT_TYPE_SAMPLER:
665                 case GL_OBJECT_TYPE_PROGRAMPIPELINE:
666                         AST(mutex_unlock(&ostate->shared->real_access_mutex) == 1);
667                         break;
668                 default:
669                         break;
670         }
671 }
672
673 int
674 fastpath_add_context_state_to_list(const void *option, const int option_len, GLContextState *cstate, Mutex *mtx)
675 {
676         int ret = 0;
677         int tid = 0;
678         GLContext_List *current = NULL;
679         GLContext_List *newitm = NULL;
680
681         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
682
683         AST(cstate != NULL);
684
685         tid = get_current_thread();
686
687         current = glctx_list;
688         while (current != NULL)
689         {
690                 if (current->option_len == option_len &&
691                     memcmp(current->option, option, option_len) == 0 &&
692                     current->thread_id == tid)
693                 {
694                         AST(current->cstate == cstate);
695                         goto finish;
696                 }
697                 current = current->next;
698         }
699
700         newitm = (GLContext_List *)calloc(1, sizeof(GLContext_List));
701         if (newitm == NULL)
702         {
703                 COREGL_ERR("Failed to create context list.\n");
704                 goto finish;
705         }
706
707         newitm->cstate = cstate;
708         newitm->thread_id = tid;
709         newitm->option_len = option_len;
710         newitm->option = (void *)malloc(option_len);
711         memcpy(newitm->option, option, option_len);
712
713         if (glctx_list != NULL)
714                 newitm->next = glctx_list;
715
716         glctx_list = newitm;
717
718         ret = 1;
719         goto finish;
720
721 finish:
722         if (ret != 1)
723         {
724                 if (newitm != NULL)
725                 {
726                         free(newitm);
727                         newitm = NULL;
728                 }
729                 if (cstate != NULL)
730                 {
731                         free(cstate);
732                         cstate = NULL;
733                 }
734         }
735         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
736
737         return ret;
738 }
739
740 GLContextState *
741 fastpath_get_context_state_from_list(const void *option, const int option_len, Mutex *mtx)
742 {
743         GLContextState *ret = NULL;
744         GLContext_List *current = NULL;
745         int tid = 0;
746
747         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
748
749         tid = get_current_thread();
750
751         current = glctx_list;
752         while (current != NULL)
753         {
754                 if (current->option_len == option_len &&
755                     memcmp(current->option, option, option_len) == 0 &&
756                     current->thread_id == tid)
757                 {
758                         ret = current->cstate;
759                         goto finish;
760                 }
761                 current = current->next;
762         }
763         goto finish;
764
765 finish:
766         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
767         return ret;
768 }
769
770 int
771 fastpath_remove_context_states_from_list(GLContextState *cstate, Mutex *mtx)
772 {
773         int ret = 0;
774         GLContext_List *olditm = NULL;
775         GLContext_List *current = NULL;
776
777         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
778
779         AST(cstate != NULL);
780
781         current = glctx_list;
782
783         while (current != NULL)
784         {
785                 if (current->cstate == cstate)
786                 {
787                         GLContext_List *nextitm = NULL;
788                         if (olditm != NULL)
789                         {
790                                 olditm->next = current->next;
791                                 nextitm = olditm->next;
792                         }
793                         else
794                         {
795                                 glctx_list = current->next;
796                                 nextitm = glctx_list;
797                         }
798                         if (current->option != NULL)
799                         {
800                                 AST(current->option_len > 0);
801                                 free(current->option);
802                                 current->option = NULL;
803                                 current->option_len = 0;
804                         }
805                         free(current);
806                         ret = 1;
807                         current = nextitm;
808                         continue;
809                 }
810                 olditm = current;
811                 current = current->next;
812         }
813         goto finish;
814
815 finish:
816         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
817         return ret;
818 }
819
820
821
822 #define HASH_INIT(hash_base) \
823         hash_base.hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * GL_OBJECT_HASH_BASE); \
824         hash_base.hash_size = GL_OBJECT_HASH_BASE;
825
826 void
827 fastpath_ostate_init(GL_Object_State *ostate)
828 {
829         HASH_INIT(ostate->query);
830         HASH_INIT(ostate->framebuffer);
831         HASH_INIT(ostate->vertexarray);
832         HASH_INIT(ostate->transformfeedback);
833
834         HASH_INIT(ostate->query_real);
835         HASH_INIT(ostate->framebuffer_real);
836         HASH_INIT(ostate->vertexarray_real);
837         HASH_INIT(ostate->transformfeedback_real);
838 }
839
840 void
841 fastpath_sostate_init(GL_Shared_Object_State *sostate)
842 {
843         mutex_init(&sostate->access_mutex);
844
845         HASH_INIT(sostate->texture);
846         HASH_INIT(sostate->buffer);
847         HASH_INIT(sostate->renderbuffer);
848         HASH_INIT(sostate->program);
849         HASH_INIT(sostate->sampler);
850         HASH_INIT(sostate->programpipeline);
851
852         HASH_INIT(sostate->texture_real);
853         HASH_INIT(sostate->buffer_real);
854         HASH_INIT(sostate->renderbuffer_real);
855         HASH_INIT(sostate->program_real);
856         HASH_INIT(sostate->sampler_real);
857         HASH_INIT(sostate->programpipeline_real);
858 }
859
860 #undef HASH_INIT
861
862
863 static void
864 _add_hash(GL_Object_Hash_Base *hash_base, GL_Object_Hash *data)
865 {
866         int array_idx = data->hash_key & (hash_base->hash_size - 1);
867         if (hash_base->hash_field[array_idx] == NULL)
868         {
869                 hash_base->hash_field[array_idx] = data;
870         }
871         else
872         {
873                 GL_Object_Hash *current = hash_base->hash_field[array_idx];
874                 while(current->next)
875                 {
876                         AST(current->hash_key != data->hash_key);
877                         current = current->next;
878                 }
879                 current->next = data;
880         }
881         data->next = NULL;
882         hash_base->item_size++;
883 }
884
885 static int
886 _remove_hash(GL_Object_Hash_Base *hash_base, GLuint hash)
887 {
888         int ret = 0;
889         int array_idx = hash & (hash_base->hash_size - 1);
890
891         GL_Object_Hash *current = hash_base->hash_field[array_idx];
892         GL_Object_Hash *prev = NULL;
893
894         while(current)
895         {
896                 if (current->hash_key == hash)
897                 {
898                         if (prev != NULL)
899                                 prev->next = current->next;
900                         else
901                                 hash_base->hash_field[array_idx] = current->next;
902                         hash_base->item_size--;
903                         ret = 1;
904                         break;
905                 }
906                 prev = current;
907                 current = current->next;
908         }
909
910         return ret;
911 }
912
913 static void
914 _free_hash_list(GL_Object_Hash_Base *hash_base, int free_data)
915 {
916         if (hash_base->item_size == 0) return;
917
918         for (int i = 0; i < hash_base->hash_size; i++)
919         {
920                 if (hash_base->hash_field[i] != NULL)
921                 {
922                         GL_Object_Hash *current = hash_base->hash_field[i];
923
924                         while (current != NULL)
925                         {
926                                 GL_Object_Hash *current_next = current->next;
927
928                                 if (free_data == 1 && current->item != NULL)
929                                 {
930                                         free(current->item);
931                                 }
932
933                                 free(current);
934                                 hash_base->item_size--;
935                                 current = current_next;
936                         }
937                 }
938         }
939 }
940
941
942
943 #define HASH_DEINIT(hash_base, free_data) \
944         _free_hash_list(&hash_base, free_data); \
945         free(hash_base.hash_field); \
946         hash_base.hash_size = 0;
947
948 void
949 fastpath_ostate_deinit(GL_Object_State *ostate)
950 {
951         HASH_DEINIT(ostate->query, 1);
952         HASH_DEINIT(ostate->framebuffer, 1);
953         HASH_DEINIT(ostate->vertexarray, 1);
954         HASH_DEINIT(ostate->transformfeedback, 1);
955
956         HASH_DEINIT(ostate->query_real, 0);
957         HASH_DEINIT(ostate->framebuffer_real, 0);
958         HASH_DEINIT(ostate->vertexarray_real, 0);
959         HASH_DEINIT(ostate->transformfeedback_real, 0);
960 }
961
962 void
963 fastpath_sostate_deinit(GL_Shared_Object_State *sostate)
964 {
965         HASH_DEINIT(sostate->texture, 1);
966         HASH_DEINIT(sostate->buffer, 1);
967         HASH_DEINIT(sostate->renderbuffer, 1);
968         HASH_DEINIT(sostate->program, 1);
969         HASH_DEINIT(sostate->sampler, 1);
970         HASH_DEINIT(sostate->programpipeline, 1);
971
972         HASH_DEINIT(sostate->texture_real, 0);
973         HASH_DEINIT(sostate->buffer_real, 0);
974         HASH_DEINIT(sostate->renderbuffer_real, 0);
975         HASH_DEINIT(sostate->program_real, 0);
976         HASH_DEINIT(sostate->sampler_real, 0);
977
978         HASH_DEINIT(sostate->programpipeline_real, 0);
979 }
980
981 #undef HASH_DEINIT
982
983
984
985 #define FIND_HASH(hash_base, key, ret) \
986 { \
987         GL_Object_Hash *fh_current = hash_base->hash_field[(key) & (hash_base->hash_size - 1)]; \
988         while(fh_current) \
989         { \
990                 if (fh_current->hash_key == (key)) \
991                 { \
992                         ret = fh_current; \
993                         break; \
994                 } \
995                 fh_current = fh_current->next; \
996         } \
997 }
998
999 void
1000 _ostate_hash_check(GL_Object_Hash_Base *hash_base)
1001 {
1002         if (hash_base->item_size + 1 < hash_base->hash_size)
1003                 return;
1004
1005         int oldsize = hash_base->hash_size;
1006         GL_Object_Hash **oldfield = hash_base->hash_field;
1007
1008         hash_base->hash_size = oldsize << 1;
1009         hash_base->hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * hash_base->hash_size);
1010         AST(hash_base->hash_field != NULL);
1011
1012         for (int i = 0; i < oldsize; i++)
1013         {
1014                 if (oldfield[i] != NULL)
1015                 {
1016                         GL_Object_Hash *current = oldfield[i];
1017
1018                         while (current != NULL)
1019                         {
1020                                 GL_Object_Hash *current_next = current->next;
1021                                 _add_hash(hash_base, current);
1022                                 hash_base->item_size--;
1023                                 current = current_next;
1024                         }
1025                 }
1026         }
1027         free(oldfield);
1028
1029 }
1030
1031 GLuint
1032 fastpath_ostate_create_object(GL_Object_State *ostate, GL_Object_Type type, GLuint real_name)
1033 {
1034         GLuint ret = _COREGL_INT_INIT_VALUE;
1035
1036         GL_Object_Hash_Base *hash_base = NULL;
1037         GL_Object_Hash_Base *hash_base_real = NULL;
1038         int newid = _COREGL_INT_INIT_VALUE;
1039
1040         hash_base = _lock_gl_object_hash(ostate, type);
1041         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1042
1043         newid = hash_base->last_id + 1;
1044         if (newid >= hash_base->hash_size)
1045         {
1046                 hash_base->is_looped = 1;
1047                 newid = 1;
1048                 hash_base->last_id = 1;
1049         }
1050
1051         if (hash_base->is_looped != 0)
1052         {
1053                 int i;
1054                 int findingid = newid;
1055                 newid = -1;
1056                 for (i = 0; i < hash_base->hash_size; i++)
1057                 {
1058                         GL_Object_Hash *exist_hash = NULL;
1059                         FIND_HASH(hash_base, findingid, exist_hash);
1060                         if (exist_hash == NULL)
1061                         {
1062                                 newid = findingid;
1063                                 break;
1064                         }
1065                         findingid++;
1066                         if (findingid >= hash_base->hash_size) findingid = 1;
1067                 }
1068                 AST(newid != -1);
1069         }
1070         hash_base->last_id = newid;
1071
1072         {
1073                 GL_Object *newobj = (GL_Object *)calloc(1, sizeof(GL_Object));
1074                 AST(newobj != NULL);
1075                 newobj->id = (int)type + newid;
1076                 newobj->real_id = real_name;
1077                 newobj->ref_count = 1;
1078                 ret = newobj->id;
1079
1080                 GL_Object_Hash *newobj_hash = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
1081                 AST(newobj_hash != NULL);
1082                 newobj_hash->item = newobj;
1083                 newobj_hash->hash_key = newid;
1084                 _add_hash(hash_base, newobj_hash);
1085
1086                 GL_Object_Hash *newobj_hash_real = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
1087                 AST(newobj_hash_real != NULL);
1088                 newobj_hash_real->item = newobj;
1089                 newobj_hash_real->hash_key = real_name;
1090                 _add_hash(hash_base_real, newobj_hash_real);
1091         }
1092
1093         _ostate_hash_check(hash_base);
1094         _ostate_hash_check(hash_base_real);
1095
1096         goto finish;
1097
1098 finish:
1099         _unlock_gl_object_hash(ostate, type);
1100         _unlock_gl_object_hash_real(ostate, type);
1101         return ret;
1102 }
1103
1104 #define FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object) \
1105         if (((int)(hash)) < 0) { ret = 0; goto finish; } \
1106         { \
1107                 GL_Object_Hash *object_hash = NULL; \
1108                 FIND_HASH((hash_base), (int)(hash), object_hash); \
1109                 if (object_hash == NULL) { ret = 0; goto finish; } \
1110                 (object) = object_hash->item; \
1111                 if ((object) == NULL) { ret = 0; goto finish; } \
1112         }
1113
1114 GLuint
1115 fastpath_ostate_remove_object(GL_Object_State *ostate, GL_Object_Type type, GLuint glue_name)
1116 {
1117         GLuint ret = _COREGL_INT_INIT_VALUE;
1118
1119         GL_Object_Hash_Base *hash_base = NULL;
1120         GL_Object_Hash_Base *hash_base_real = NULL;
1121         GL_Object *object = NULL;
1122
1123         hash_base = _lock_gl_object_hash(ostate, type);
1124         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1125
1126         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1127
1128         object->ref_count--;
1129
1130         if (object->ref_count <= 0)
1131         {
1132                 GL_Object_Hash *object_hash = NULL;
1133
1134                 FIND_HASH(hash_base, object->id - (int)type, object_hash);
1135                 AST(object_hash != NULL);
1136                 _remove_hash(hash_base, object->id - (int)type);
1137                 free(object_hash);
1138                 object_hash = NULL;
1139
1140                 FIND_HASH(hash_base_real, object->real_id, object_hash);
1141                 AST(object_hash != NULL);
1142                 _remove_hash(hash_base_real, object->real_id);
1143                 free(object_hash);
1144                 object_hash = NULL;
1145
1146                 free(object);
1147                 object = NULL;
1148         }
1149
1150         ret = 1;
1151         goto finish;
1152
1153 finish:
1154         _unlock_gl_object_hash(ostate, type);
1155         _unlock_gl_object_hash_real(ostate, type);
1156         return ret;
1157 }
1158
1159 GLuint
1160 fastpath_ostate_get_object(GL_Object_State *ostate, GL_Object_Type type, GLuint glue_name)
1161 {
1162         GLuint ret = _COREGL_INT_INIT_VALUE;
1163
1164         GL_Object_Hash_Base *hash_base = NULL;
1165         GL_Object *object = NULL;
1166
1167         hash_base = _lock_gl_object_hash(ostate, type);
1168
1169         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1170
1171         ret = object->real_id;
1172         goto finish;
1173
1174 finish:
1175         _unlock_gl_object_hash(ostate, type);
1176         return ret;
1177 }
1178
1179
1180 /* Check if the context's state contains object of a given type */
1181 GLuint
1182 fastpath_ostate_has_object_type(GL_Object_State *ostate, GL_Object_Type type)
1183 {
1184         GLuint ret = _COREGL_INT_INIT_VALUE;
1185
1186         GL_Object_Hash_Base *hash_base = NULL;
1187         GL_Object *object = NULL;
1188         hash_base = _lock_gl_object_hash(ostate, type);
1189
1190         if(hash_base->hash_field == 0)
1191         {
1192                 ret = 0;
1193                 goto finish;
1194         }
1195
1196         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, 1, object);
1197
1198         ret = object->real_id;
1199         goto finish;
1200
1201 finish:
1202         _unlock_gl_object_hash(ostate, type);
1203         return ret;
1204 }
1205
1206
1207 GLint
1208 fastpath_ostate_set_object_tag(GL_Object_State *ostate, GL_Object_Type type, GLuint glue_name, GLvoid *tag)
1209 {
1210         GLint ret = _COREGL_INT_INIT_VALUE;
1211
1212         GL_Object_Hash_Base *hash_base = NULL;
1213         GL_Object *object = NULL;
1214         int hash = _COREGL_INT_INIT_VALUE;
1215
1216         hash_base = _lock_gl_object_hash(ostate, type);
1217
1218         hash = glue_name - (int)type;
1219
1220         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object);
1221
1222         AST(object->tag == NULL);
1223         object->tag = tag;
1224         ret = 1;
1225         goto finish;
1226
1227 finish:
1228         _unlock_gl_object_hash(ostate, type);
1229         return ret;
1230 }
1231
1232 GLvoid *
1233 fastpath_ostate_get_object_tag(GL_Object_State *ostate, GL_Object_Type type, GLuint glue_name)
1234 {
1235         GLvoid *ret = NULL;
1236
1237         GL_Object_Hash_Base *hash_base = NULL;
1238         GL_Object *object = NULL;
1239
1240         hash_base = _lock_gl_object_hash(ostate, type);
1241
1242         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1243
1244         ret = object->tag;
1245         goto finish;
1246
1247 finish:
1248         _unlock_gl_object_hash(ostate, type);
1249         return ret;
1250 }
1251
1252 GLuint
1253 fastpath_ostate_find_object(GL_Object_State *ostate, GL_Object_Type type, GLuint real_name)
1254 {
1255         GLuint ret = _COREGL_INT_INIT_VALUE;
1256
1257         GL_Object_Hash_Base *hash_base_real = NULL;
1258         GL_Object *object = NULL;
1259
1260         hash_base_real = _lock_gl_object_hash_real(ostate, type);
1261
1262         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base_real, real_name, object);
1263
1264         ret = object->id;
1265         goto finish;
1266
1267 finish:
1268         _unlock_gl_object_hash_real(ostate, type);
1269         return ret;
1270 }
1271
1272 GLint
1273 fastpath_ostate_use_object(GL_Object_State *ostate, GL_Object_Type type, GLuint glue_name)
1274 {
1275         GLint ret = _COREGL_INT_INIT_VALUE;
1276
1277         GL_Object_Hash_Base *hash_base = NULL;
1278         GL_Object *object = NULL;
1279
1280         hash_base = _lock_gl_object_hash(ostate, type);
1281
1282         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
1283
1284         object->ref_count++;
1285         ret = 1;
1286         goto finish;
1287
1288 finish:
1289         _unlock_gl_object_hash(ostate, type);
1290         return ret;
1291 }
1292
1293 void
1294 fastpath_dump_context_states(GLGlueContext *ctx, int force_output)
1295 {
1296         static struct timeval tv_last = { 0, 0 };
1297
1298         if (unlikely(trace_state_flag != 1)) return;
1299
1300         if (!force_output)
1301         {
1302                 struct timeval tv_now = { 0, 0 };
1303                 AST(gettimeofday(&tv_now, NULL) == 0);
1304                 if (tv_now.tv_sec - tv_last.tv_sec < _COREGL_TRACE_OUTPUT_INTERVAL_SEC)
1305                 {
1306                         goto finish;
1307                 }
1308                 tv_last = tv_now;
1309         }
1310
1311         TRACE("\n");
1312         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1313         TRACE("\E[40;32;1m  State info \E[1;37;1m: <PID = %d> GlueCTX = %p\E[0m\n", getpid(), ctx);
1314         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1315
1316 #define PRINTF_CHAR_GLenum "0x%8X"
1317 #define PRINTF_CHAR_GLboolean "%10d"
1318 #define PRINTF_CHAR_GLint "%10d"
1319 #define PRINTF_CHAR_GLsizei "%10u"
1320 #define PRINTF_CHAR_GLuint "%10u"
1321 #define PRINTF_CHAR_GLuintmask "0x%8X"
1322 #define PRINTF_CHAR_GLintptr "%10ld"
1323 #define PRINTF_CHAR_GLsizeiptr "%10ld"
1324
1325 #define PRINTF_CHAR_GLclampf "%10.6f"
1326 #define PRINTF_CHAR_GLfloat "%10.6f"
1327
1328 #define PRINTF_CHAR_GLvoidptr "%10p"
1329
1330 #define PRINTF_CHAR(type) PRINTF_CHAR_##type
1331
1332 #define _COREGL_START_API(version) api_gl_version = version;
1333 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1334 #define INITIAL_CTX initial_ctx
1335 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1336    { \
1337       TYPE valuedata[SIZE]; \
1338       TYPE *value = NULL; \
1339       if(api_gl_version <= driver_gl_version) { \
1340          value = valuedata; GET_STMT; value = valuedata; \
1341          TRACE("\E[40;37;1m %-30.30s : (\E[0m ", #NAME); \
1342          for (int i = 0; i < SIZE; i++) \
1343          { \
1344             if (i > 0) { \
1345                if (i % 4 == 0) \
1346                   TRACE("\n %-30.30s     ", "");\
1347                else \
1348                   TRACE(", "); \
1349             } \
1350             if (ctx->NAME[i] != value[i]) { TRACE("\E[40;31;1m"); } \
1351                TRACE(PRINTF_CHAR(TYPE), ctx->NAME[i]); \
1352                TRACE("["PRINTF_CHAR(TYPE)"]", value[i]); \
1353             if (ctx->NAME[i] != value[i]) { TRACE("\E[0m"); } \
1354          } \
1355          TRACE(" \E[40;37;1m)\E[0m\n"); \
1356       } \
1357    }
1358 # include "coregl_fastpath_state.h"
1359 #undef GLUE_STATE
1360 #undef INITIAL_CTX
1361 #undef _COREGL_START_API
1362 #undef _COREGL_END_API
1363
1364         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
1365         TRACE("\n");
1366
1367         TRACE_END();
1368
1369 finish:
1370         return;
1371 }
1372
1373 int
1374 fastpath_init_context_states(GLGlueContext *ctx)
1375 {
1376         int ret = 0;
1377
1378         AST(mutex_lock(&init_context_mutex) == 1);
1379
1380         if (ctx == NULL)
1381         {
1382                 COREGL_ERR("Context NULL\n");
1383                 ret = 0;
1384                 goto finish;
1385         }
1386
1387         AST(ctx->initialized == 0);
1388         AST(ctx->ostate.shared != NULL);
1389
1390         if (initial_ctx == NULL)
1391         {
1392                 initial_ctx = (GLGlueContext *)calloc(1, sizeof(GLGlueContext));
1393                 AST(initial_ctx != NULL);
1394
1395 //#define FORCE_DEFAULT_VALUE
1396 #define _COREGL_START_API(version) api_gl_version = version;
1397 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1398 #ifdef FORCE_DEFAULT_VALUE
1399 # define INITIAL_CTX initial_ctx
1400 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1401       { \
1402          int i; \
1403          TYPE valuedata[SIZE]; \
1404          TYPE *value = NULL; \
1405          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1406          if(api_gl_version <= driver_gl_version) { \
1407             value = valuedata; DEFAULT_STMT; value = valuedata; \
1408             for (i = 0; i < SIZE; i++) \
1409             { \
1410                if (*((char *)(&value[i])) == 0xcc) \
1411                { \
1412                   memset(&value[i], 0xaa, sizeof(TYPE)); \
1413                   value = valuedata; DEFAULT_STMT; value = valuedata; \
1414                   if (*((char *)(&value[i])) == 0xaa) \
1415                   { \
1416                      COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
1417                      break; \
1418                   } \
1419                } \
1420                initial_ctx->NAME[i] = value[i]; \
1421             } \
1422         }\
1423       }
1424 #  include "coregl_fastpath_state.h"
1425 # undef GLUE_STATE
1426 # undef INITIAL_CTX
1427 #else
1428 # define INITIAL_CTX initial_ctx
1429 # define SET_GLUE_VALUE(DEFAULT_STMT, FALLBACK_STMT) \
1430       if (try_step == 1) \
1431       { \
1432          value = valuedata; DEFAULT_STMT; value = valuedata; \
1433       } \
1434       else \
1435       { \
1436          value = valuedata; FALLBACK_STMT; value = valuedata; \
1437       }
1438
1439 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1440       { \
1441          int i; \
1442          int try_step = 0;\
1443          TYPE valuedata[SIZE]; \
1444          TYPE *value = NULL; \
1445          _sym_glGetError(); \
1446          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1447          if(api_gl_version <= driver_gl_version) { \
1448             do { \
1449                try_step++; \
1450                SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1451                if (_sym_glGetError() == GL_INVALID_ENUM) \
1452                { \
1453                   initial_ctx->NAME##_used = 0; \
1454                   value = valuedata; DEFAULT_STMT; value = valuedata; \
1455                   break; \
1456                } \
1457                initial_ctx->NAME##_used = 1; \
1458                for (i = 0; i < SIZE; i++) \
1459                { \
1460                   if (*((char *)(&value[i])) == 0xcc) \
1461                   { \
1462                      memset(&value[i], 0xaa, sizeof(TYPE)); \
1463                      SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1464                      if (*((char *)(&value[i])) == 0xaa) \
1465                      { \
1466                         try_step++; \
1467                         if (try_step == 2) \
1468                         { \
1469                            COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
1470                         } \
1471                         break; \
1472                      } \
1473                   } \
1474                   initial_ctx->NAME[i] = value[i]; \
1475                } \
1476                if (try_step != 2) \
1477                { \
1478                   value = valuedata; DEFAULT_STMT; value = valuedata; \
1479                   for (i = 0; i < SIZE; i++) \
1480                   { \
1481                      if (initial_ctx->NAME[i] != value[i]) \
1482                      { \
1483                         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]); \
1484                      } \
1485                   } \
1486                } \
1487             } \
1488             while (try_step == 2); \
1489          }\
1490       }
1491 #  include "coregl_fastpath_state.h"
1492 # undef SET_GLUE_VALUE
1493 # undef GLUE_STATE
1494 # undef INITIAL_CTX
1495 #endif
1496 # undef _COREGL_END_API
1497 # undef _COREGL_START_API
1498
1499                 if (initial_ctx->gl_num_vertex_attribs[0] > MAX_VERTEX_ATTRIBS)
1500                 {
1501                         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]);
1502                 }
1503                 if (initial_ctx->gl_num_tex_units[0] > MAX_TEXTURE_UNITS)
1504                 {
1505                         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]);
1506                 }
1507                 if (initial_ctx->gl_num_transform_feedback_separate_attribs[0] > MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS)
1508                 {
1509                         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]);
1510                 }
1511                 if (initial_ctx->gl_num_uniform_buffer_bindings[0] > MAX_UNIFORM_BUFFER_BINDINGS)
1512                 {
1513                         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]);
1514                 }
1515         }
1516
1517         {
1518                 int i;
1519 #define _COREGL_START_API(version) api_gl_version = version;
1520 #define _COREGL_END_API(version) api_gl_version = COREGL_GLAPI_2;
1521 #define INITIAL_CTX initial_ctx
1522 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1523       if(api_gl_version <= driver_gl_version) { \
1524          for (i = 0; i < SIZE; i++) \
1525          { \
1526             ctx->NAME[i] = initial_ctx->NAME[i]; \
1527             ctx->NAME##_used = initial_ctx->NAME##_used; \
1528          }\
1529       }
1530 # include "coregl_fastpath_state.h"
1531 #undef GLUE_STATE
1532 #undef INITIAL_CTX
1533 #undef _COREGL_START_API
1534 #undef _COREGL_END_API
1535         }
1536
1537         ctx->initialized = 1;
1538         ret = 1;
1539         goto finish;
1540
1541 finish:
1542         AST(mutex_unlock(&init_context_mutex) == 1);
1543
1544         return ret;
1545 }
1546
1547 #ifdef COREGL_USE_MODULE_TRACEPATH
1548 extern void *tracepath_api_trace_begin(const char *name, void *hint, int trace_total_time);
1549 extern void *tracepath_api_trace_end(const char *name, void *hint, int trace_total_time);
1550 #endif
1551
1552 #define CHECK_GL_ERROR(func) \
1553         { \
1554                 func; \
1555                 int err = _orig_fastpath_glGetError(); \
1556                 if (err != GL_NO_ERROR) \
1557                 { \
1558                         COREGL_ERR("\E[40;31;1m(GL %p) : %s returns GL error 0x%X\E[0m\n", oldctx->cstate, #func, err); \
1559                         goto finish; \
1560                 } \
1561         }
1562
1563 int
1564 fastpath_make_context_current(GLGlueContext *oldctx, GLGlueContext *newctx)
1565 {
1566         int ret = 0;
1567         unsigned char flag = 0;
1568         int i = 0;
1569
1570         if (debug_nofp == 1)
1571         {
1572                 ret = 1;
1573                 goto finish;
1574         }
1575
1576         // Return if they're the same
1577         if (oldctx == newctx)
1578         {
1579                 ret = 1;
1580                 goto finish;
1581         }
1582
1583 #define STATE_COMPARE(state) \
1584    if ((oldctx->state) != (newctx->state))
1585
1586 #define STATES_COMPARE(state_ptr, bytes) \
1587    if ((memcmp((oldctx->state_ptr), (newctx->state_ptr), (bytes))) != 0)
1588
1589
1590 #ifdef COREGL_USE_MODULE_TRACEPATH
1591         static void *trace_hint_glfinish = NULL;
1592         trace_hint_glfinish = tracepath_api_trace_begin("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1593 #endif // COREGL_USE_MODULE_TRACEPATH
1594
1595         {
1596                 int err = _orig_fastpath_glGetError();
1597                 if (err != GL_NO_ERROR && oldctx->gl_error == GL_NO_ERROR)
1598                         oldctx->gl_error = err;
1599         }
1600
1601         CHECK_GL_ERROR(_orig_fastpath_glFlush())
1602
1603 #ifdef COREGL_USE_MODULE_TRACEPATH
1604         tracepath_api_trace_end("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1605 #endif // COREGL_USE_MODULE_TRACEPATH
1606
1607         // _varray_flag
1608 #ifdef COREGL_USE_MODULE_TRACEPATH
1609         static void *trace_hint_vertex_attrib = NULL;
1610         trace_hint_vertex_attrib = tracepath_api_trace_begin("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1611 #endif // COREGL_USE_MODULE_TRACEPATH
1612
1613         flag = oldctx->_vattrib_flag | newctx->_vattrib_flag;
1614         if (flag)
1615         {
1616                 for (i = 0; i < oldctx->gl_num_vertex_attribs[0]; i++)
1617                 {
1618                         if (newctx->gl_vertex_array_buf_id[i] != oldctx->gl_vertex_array_buf_id[i])
1619                         {
1620                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_vertex_array_buf_id[i]))
1621                         }
1622                         else
1623                         {
1624                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, 0))
1625                         }
1626
1627                         STATE_COMPARE(gl_vertex_array_divisor[i])
1628                         {
1629                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttribDivisor(i, newctx->gl_vertex_array_divisor[i]))
1630                         }
1631
1632                         if (newctx->gl_vertex_array_size[i] != 0)
1633                         {
1634                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttribPointer(i,
1635                                                newctx->gl_vertex_array_size[i],
1636                                                newctx->gl_vertex_array_type[i],
1637                                                newctx->gl_vertex_array_normalized[i],
1638                                                newctx->gl_vertex_array_stride[i],
1639                                                newctx->gl_vertex_array_pointer[i]))
1640                         }
1641                         else
1642                         {
1643                                 if (newctx->gl_vertex_array_integer[0] == GL_TRUE)
1644                                 {
1645                                         if (newctx->gl_vertex_array_type[0] == GL_UNSIGNED_INT)
1646                                         {
1647                                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttribI4uiv(i, &newctx->gl_vertex_attrib_value_unsigned_integer[4 * i]))
1648                                         }
1649                                         else
1650                                         {
1651                                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttribI4iv(i, &newctx->gl_vertex_attrib_value_integer[4 * i]))
1652                                         }
1653                                 }
1654                                 else
1655                                 {
1656                                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttrib4fv(i, &newctx->gl_vertex_attrib_value[4 * i]))
1657                                 }
1658                         }
1659
1660                         if (newctx->gl_vertex_array_enabled[i] == GL_TRUE)
1661                         {
1662                                 CHECK_GL_ERROR(_orig_fastpath_glEnableVertexAttribArray(i))
1663                         }
1664                         else
1665                         {
1666                                 CHECK_GL_ERROR(_orig_fastpath_glDisableVertexAttribArray(i))
1667                         }
1668                 }
1669
1670         }
1671
1672 #ifdef COREGL_USE_MODULE_TRACEPATH
1673         tracepath_api_trace_end("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1674 #endif // COREGL_USE_MODULE_TRACEPATH
1675
1676
1677 #ifdef COREGL_USE_MODULE_TRACEPATH
1678         static void *trace_hint_bindbuffers = NULL;
1679         trace_hint_bindbuffers = tracepath_api_trace_begin("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1680 #endif // COREGL_USE_MODULE_TRACEPATH
1681
1682         //------------------//
1683         // _bind_flag1
1684         flag = oldctx->_bind_flag1 | newctx->_bind_flag1;
1685         if (flag)
1686         {
1687                 STATE_COMPARE(gl_array_buffer_binding[0])
1688                 {
1689                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_array_buffer_binding[0]))
1690                 }
1691                 STATE_COMPARE(gl_element_array_buffer_binding[0])
1692                 {
1693                         STATE_COMPARE(gl_vertex_array_binding[0])
1694                         {
1695                                 CHECK_GL_ERROR(_orig_fastpath_glBindVertexArray(newctx->gl_vertex_array_binding[0]))
1696                         }
1697                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newctx->gl_element_array_buffer_binding[0]))
1698                 }
1699
1700                 if (newctx->gl_framebuffer_binding_read_used == 1)
1701                 {
1702                         STATE_COMPARE(gl_framebuffer_binding_read[0])
1703                         {
1704                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_READ_FRAMEBUFFER, newctx->gl_framebuffer_binding_read[0]))
1705                         }
1706                         STATE_COMPARE(gl_framebuffer_binding_draw[0])
1707                         {
1708                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_DRAW_FRAMEBUFFER, newctx->gl_framebuffer_binding_draw[0]))
1709                         }
1710                 }
1711                 else
1712                 {
1713                         STATE_COMPARE(gl_framebuffer_binding[0])
1714                         {
1715                                 CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_FRAMEBUFFER, newctx->gl_framebuffer_binding[0]))
1716                         }
1717                 }
1718                 STATE_COMPARE(gl_renderbuffer_binding[0])
1719                 {
1720                         CHECK_GL_ERROR(_orig_fastpath_glBindRenderbuffer(GL_RENDERBUFFER, newctx->gl_renderbuffer_binding[0]))
1721                 }
1722         }
1723
1724         //------------------//
1725         // _bind_flag2
1726         flag = oldctx->_bind_flag2 | newctx->_bind_flag2;
1727         if (flag)
1728         {
1729                 STATE_COMPARE(gl_copy_read_buffer_binding[0])
1730                 {
1731                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_COPY_READ_BUFFER, newctx->gl_copy_read_buffer_binding[0]))
1732                 }
1733                 STATE_COMPARE(gl_copy_write_buffer_binding[0])
1734                 {
1735                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_COPY_WRITE_BUFFER, newctx->gl_copy_write_buffer_binding[0]))
1736                 }
1737                 STATE_COMPARE(gl_pixel_pack_buffer_binding[0])
1738                 {
1739                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_PIXEL_PACK_BUFFER, newctx->gl_pixel_pack_buffer_binding[0]))
1740                 }
1741                 STATE_COMPARE(gl_pixel_unpack_buffer_binding[0])
1742                 {
1743                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, newctx->gl_pixel_unpack_buffer_binding[0]))
1744                 }
1745                 STATE_COMPARE(gl_transform_feedback_buffer_binding[0])
1746                 {
1747                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, newctx->gl_transform_feedback_buffer_binding[0]))
1748                 }
1749                 STATE_COMPARE(gl_uniform_buffer_binding[0])
1750                 {
1751                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_UNIFORM_BUFFER, newctx->gl_uniform_buffer_binding[0]))
1752                 }
1753         }
1754 #ifdef COREGL_USE_MODULE_TRACEPATH
1755         tracepath_api_trace_end("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1756 #endif // COREGL_USE_MODULE_TRACEPATH
1757
1758
1759         //------------------//
1760         // Enable States
1761         // _enable_flag1
1762 #ifdef COREGL_USE_MODULE_TRACEPATH
1763         static void *trace_hint_enable_states = NULL;
1764         trace_hint_enable_states = tracepath_api_trace_begin("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1765 #endif // COREGL_USE_MODULE_TRACEPATH
1766
1767         flag = oldctx->_enable_flag1 | newctx->_enable_flag1;
1768         if (flag)
1769         {
1770                 STATE_COMPARE(gl_blend[0])
1771                 {
1772                         if (newctx->gl_blend[0])
1773                         {
1774                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_BLEND))
1775                         }
1776                         else
1777                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_BLEND))
1778                 }
1779                 STATE_COMPARE(gl_cull_face[0])
1780                 {
1781                         if (newctx->gl_cull_face[0])
1782                         {
1783                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_CULL_FACE))
1784                         }
1785                         else
1786                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_CULL_FACE))
1787                 }
1788                 STATE_COMPARE(gl_depth_test[0])
1789                 {
1790                         if (newctx->gl_depth_test[0])
1791                         {
1792                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DEPTH_TEST))
1793                         }
1794                         else
1795                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DEPTH_TEST))
1796                 }
1797                 STATE_COMPARE(gl_dither[0])
1798                 {
1799                         if (newctx->gl_dither[0])
1800                         {
1801                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DITHER))
1802                         }
1803                         else
1804                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DITHER))
1805                 }
1806         }
1807
1808         // _enable_flag2
1809         flag = oldctx->_enable_flag2 | newctx->_enable_flag2;
1810         if (flag)
1811         {
1812                 STATE_COMPARE(gl_polygon_offset_fill[0])
1813                 {
1814                         if (newctx->gl_polygon_offset_fill[0])
1815                         {
1816                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_POLYGON_OFFSET_FILL))
1817                         }
1818                         else
1819                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_POLYGON_OFFSET_FILL))
1820                 }
1821                 STATE_COMPARE(gl_sample_alpha_to_coverage[0])
1822                 {
1823                         if (newctx->gl_sample_alpha_to_coverage[0])
1824                         {
1825                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1826                         }
1827                         else
1828                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1829                 }
1830                 STATE_COMPARE(gl_sample_coverage[0])
1831                 {
1832                         if (newctx->gl_sample_coverage[0])
1833                         {
1834                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_COVERAGE))
1835                         }
1836                         else
1837                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_COVERAGE))
1838                 }
1839                 STATE_COMPARE(gl_scissor_test[0])
1840                 {
1841                         if (newctx->gl_scissor_test[0])
1842                         {
1843                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SCISSOR_TEST))
1844                         }
1845                         else
1846                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SCISSOR_TEST))
1847                 }
1848                 STATE_COMPARE(gl_stencil_test[0])
1849                 {
1850                         if (newctx->gl_stencil_test[0])
1851                         {
1852                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_STENCIL_TEST))
1853                         }
1854                         else
1855                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_STENCIL_TEST))
1856                 }
1857         }
1858
1859         // _enable_flag3
1860         flag = oldctx->_enable_flag3 | newctx->_enable_flag3;
1861         if (flag)
1862         {
1863                 STATE_COMPARE(gl_primitive_restart_fixed_index[0])
1864                 {
1865                         if (newctx->gl_primitive_restart_fixed_index[0])
1866                         {
1867                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX))
1868                         }
1869                         else
1870                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_PRIMITIVE_RESTART_FIXED_INDEX))
1871                 }
1872                 STATE_COMPARE(gl_rasterizer_discard[0])
1873                 {
1874                         if (newctx->gl_rasterizer_discard[0])
1875                         {
1876                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_RASTERIZER_DISCARD))
1877                         }
1878                         else
1879                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_RASTERIZER_DISCARD))
1880                 }
1881         }
1882
1883 #ifdef COREGL_USE_MODULE_TRACEPATH
1884         tracepath_api_trace_end("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1885 #endif // COREGL_USE_MODULE_TRACEPATH
1886
1887         //------------------//
1888         // _clear_flag1
1889 #ifdef COREGL_USE_MODULE_TRACEPATH
1890         static void *trace_hint_clear_viewport = NULL;
1891         trace_hint_clear_viewport = tracepath_api_trace_begin("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1892 #endif // COREGL_USE_MODULE_TRACEPATH
1893
1894         flag = oldctx->_clear_flag1 | newctx->_clear_flag1;
1895         if (flag)
1896         {
1897                 // Viewport.
1898                 STATES_COMPARE(gl_viewport, 4 * sizeof(GLint))
1899                 {
1900                         CHECK_GL_ERROR(_orig_fastpath_glViewport(newctx->gl_viewport[0],
1901                                        newctx->gl_viewport[1],
1902                                        newctx->gl_viewport[2],
1903                                        newctx->gl_viewport[3]))
1904                 }
1905
1906                 STATE_COMPARE(gl_current_program[0])
1907                 {
1908                         CHECK_GL_ERROR(_orig_fastpath_glUseProgram(newctx->gl_current_program[0]))
1909                 }
1910                 STATES_COMPARE(gl_color_clear_value, 4 * sizeof(GLclampf))
1911                 {
1912                         CHECK_GL_ERROR(_orig_fastpath_glClearColor(newctx->gl_color_clear_value[0],
1913                                        newctx->gl_color_clear_value[1],
1914                                        newctx->gl_color_clear_value[2],
1915                                        newctx->gl_color_clear_value[3]))
1916                 }
1917         }
1918
1919
1920         // _clear_flag2
1921         flag = oldctx->_clear_flag2 | newctx->_clear_flag2;
1922         if (flag)
1923         {
1924                 STATES_COMPARE(gl_color_writemask, 4 * sizeof(GLboolean))
1925                 {
1926                         CHECK_GL_ERROR(_orig_fastpath_glColorMask(newctx->gl_color_writemask[0],
1927                                        newctx->gl_color_writemask[1],
1928                                        newctx->gl_color_writemask[2],
1929                                        newctx->gl_color_writemask[3]))
1930                 }
1931                 STATES_COMPARE(gl_depth_range, 2 * sizeof(GLclampf))
1932                 {
1933                         CHECK_GL_ERROR(_orig_fastpath_glDepthRangef(newctx->gl_depth_range[0],
1934                                        newctx->gl_depth_range[1]))
1935                 }
1936                 STATE_COMPARE(gl_depth_clear_value[0])
1937                 {
1938                         CHECK_GL_ERROR(_orig_fastpath_glClearDepthf(newctx->gl_depth_clear_value[0]))
1939                 }
1940                 STATE_COMPARE(gl_depth_func[0])
1941                 {
1942                         CHECK_GL_ERROR(_orig_fastpath_glDepthFunc(newctx->gl_depth_func[0]))
1943                 }
1944                 STATE_COMPARE(gl_depth_writemask[0])
1945                 {
1946                         CHECK_GL_ERROR(_orig_fastpath_glDepthMask(newctx->gl_depth_writemask[0]))
1947                 }
1948                 STATE_COMPARE(gl_cull_face_mode[0])
1949                 {
1950                         CHECK_GL_ERROR(_orig_fastpath_glCullFace(newctx->gl_cull_face_mode[0]))
1951                 }
1952
1953         }
1954
1955 #ifdef COREGL_USE_MODULE_TRACEPATH
1956         tracepath_api_trace_end("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1957 #endif // COREGL_USE_MODULE_TRACEPATH
1958
1959         //------------------//
1960         // Texture here...
1961 #ifdef COREGL_USE_MODULE_TRACEPATH
1962         static void *trace_hint_bind_textures = NULL;
1963         trace_hint_bind_textures = tracepath_api_trace_begin("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
1964 #endif // COREGL_USE_MODULE_TRACEPATH
1965
1966         flag = oldctx->_tex_flag1 | newctx->_tex_flag1;
1967         if (flag)
1968         {
1969
1970                 for (i = 0; i < oldctx->gl_num_tex_units[0]; i++)
1971                 {
1972                         STATE_COMPARE(gl_tex_2d_state[i])
1973                         {
1974                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1975                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D, newctx->gl_tex_2d_state[i]))
1976                         }
1977                         STATE_COMPARE(gl_tex_3d_state[i])
1978                         {
1979                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1980                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_3D, newctx->gl_tex_3d_state[i]))
1981                         }
1982                         STATE_COMPARE(gl_tex_2d_array_state[i])
1983                         {
1984                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1985                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D_ARRAY, newctx->gl_tex_2d_array_state[i]))
1986                         }
1987                         STATE_COMPARE(gl_tex_cube_state[i])
1988                         {
1989                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1990                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_CUBE_MAP, newctx->gl_tex_cube_state[i]))
1991                         }
1992                         STATE_COMPARE(gl_tex_external_oes_state[i])
1993                         {
1994                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1995                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_EXTERNAL_OES, newctx->gl_tex_external_oes_state[i]))
1996                         }
1997                 }
1998
1999                 // Restore active texture
2000                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(newctx->gl_active_texture[0]))
2001
2002                 STATE_COMPARE(gl_generate_mipmap_hint[0])
2003                 {
2004                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_GENERATE_MIPMAP_HINT, newctx->gl_generate_mipmap_hint[0]))
2005                 }
2006         }
2007 #ifdef COREGL_USE_MODULE_TRACEPATH
2008         tracepath_api_trace_end("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
2009 #endif // COREGL_USE_MODULE_TRACEPATH
2010
2011         //------------------//
2012 #ifdef COREGL_USE_MODULE_TRACEPATH
2013         static void *trace_hint_etc = NULL;
2014         trace_hint_etc = tracepath_api_trace_begin("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
2015 #endif // COREGL_USE_MODULE_TRACEPATH
2016
2017         flag = oldctx->_blend_flag | newctx->_blend_flag;
2018         if (flag)
2019         {
2020                 STATES_COMPARE(gl_blend_color, 4 * sizeof(GLclampf))
2021                 {
2022                         CHECK_GL_ERROR(_orig_fastpath_glBlendColor(newctx->gl_blend_color[0],
2023                                        newctx->gl_blend_color[1],
2024                                        newctx->gl_blend_color[2],
2025                                        newctx->gl_blend_color[3]))
2026                 }
2027                 if ((oldctx->gl_blend_src_rgb[0] != newctx->gl_blend_src_rgb[0]) ||
2028                     (oldctx->gl_blend_dst_rgb[0] != newctx->gl_blend_dst_rgb[0]) ||
2029                     (oldctx->gl_blend_src_alpha[0] != newctx->gl_blend_src_alpha[0]) ||
2030                     (oldctx->gl_blend_dst_alpha[0] != newctx->gl_blend_dst_alpha[0]))
2031                 {
2032                         CHECK_GL_ERROR(_orig_fastpath_glBlendFuncSeparate(newctx->gl_blend_src_rgb[0],
2033                                        newctx->gl_blend_dst_rgb[0],
2034                                        newctx->gl_blend_src_alpha[0],
2035                                        newctx->gl_blend_dst_alpha[0]))
2036                 }
2037                 if ((oldctx->gl_blend_equation_rgb[0] != newctx->gl_blend_equation_rgb[0]) ||
2038                     (oldctx->gl_blend_equation_alpha[0] != newctx->gl_blend_equation_alpha[0]))
2039                 {
2040                         CHECK_GL_ERROR(_orig_fastpath_glBlendEquationSeparate(newctx->gl_blend_equation_rgb[0], newctx->gl_blend_equation_alpha[0]))
2041                 }
2042
2043         }
2044
2045         //------------------//
2046         // _stencil_flag1
2047         flag = oldctx->_stencil_flag1 | newctx->_stencil_flag1;
2048         if (flag)
2049         {
2050                 if ((oldctx->gl_stencil_func[0] != newctx->gl_stencil_func[0]) ||
2051                     (oldctx->gl_stencil_ref[0]  != newctx->gl_stencil_ref[0])  ||
2052                     (oldctx->gl_stencil_value_mask[0] != newctx->gl_stencil_value_mask[0]))
2053                 {
2054                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_FRONT,
2055                                        newctx->gl_stencil_func[0],
2056                                        newctx->gl_stencil_ref[0],
2057                                        newctx->gl_stencil_value_mask[0]))
2058                 }
2059                 if ((oldctx->gl_stencil_fail[0] != newctx->gl_stencil_fail[0]) ||
2060                     (oldctx->gl_stencil_pass_depth_fail[0] != newctx->gl_stencil_pass_depth_fail[0]) ||
2061                     (oldctx->gl_stencil_pass_depth_pass[0] != newctx->gl_stencil_pass_depth_pass[0]))
2062                 {
2063                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_FRONT,
2064                                        newctx->gl_stencil_fail[0],
2065                                        newctx->gl_stencil_pass_depth_fail[0],
2066                                        newctx->gl_stencil_pass_depth_pass[0]))
2067                 }
2068
2069                 STATE_COMPARE(gl_stencil_writemask[0])
2070                 {
2071                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_FRONT, newctx->gl_stencil_writemask[0]))
2072                 }
2073         }
2074
2075
2076         // _stencil_flag1
2077         flag = oldctx->_stencil_flag2 | newctx->_stencil_flag2;
2078         if (flag)
2079         {
2080                 if ((oldctx->gl_stencil_back_func[0] != newctx->gl_stencil_back_func[0]) ||
2081                     (oldctx->gl_stencil_back_ref[0]  != newctx->gl_stencil_back_ref[0])  ||
2082                     (oldctx->gl_stencil_back_value_mask[0] != newctx->gl_stencil_back_value_mask[0]))
2083                 {
2084                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_BACK,
2085                                        newctx->gl_stencil_back_func[0],
2086                                        newctx->gl_stencil_back_ref[0],
2087                                        newctx->gl_stencil_back_value_mask[0]))
2088                 }
2089                 if ((oldctx->gl_stencil_back_fail[0] != newctx->gl_stencil_back_fail[0]) ||
2090                     (oldctx->gl_stencil_back_pass_depth_fail[0] != newctx->gl_stencil_back_pass_depth_fail[0]) ||
2091                     (oldctx->gl_stencil_back_pass_depth_pass[0] != newctx->gl_stencil_back_pass_depth_pass[0]))
2092                 {
2093                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_BACK,
2094                                        newctx->gl_stencil_back_fail[0],
2095                                        newctx->gl_stencil_back_pass_depth_fail[0],
2096                                        newctx->gl_stencil_back_pass_depth_pass[0]))
2097                 }
2098
2099                 STATE_COMPARE(gl_stencil_back_writemask[0])
2100                 {
2101                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_BACK, newctx->gl_stencil_back_writemask[0]))
2102                 }
2103                 STATE_COMPARE(gl_stencil_clear_value[0])
2104                 {
2105                         CHECK_GL_ERROR(_orig_fastpath_glClearStencil(newctx->gl_stencil_clear_value[0]))
2106                 }
2107         }
2108
2109         //------------------//
2110         // _pixel_flag1
2111         flag = oldctx->_pixel_flag1 | newctx->_pixel_flag1;
2112         if (flag)
2113         {
2114                 STATE_COMPARE(gl_pack_row_length[0])
2115                 {
2116                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ROW_LENGTH, newctx->gl_pack_row_length[0]))
2117                 }
2118                 STATE_COMPARE(gl_pack_skip_rows[0])
2119                 {
2120                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_SKIP_ROWS, newctx->gl_pack_skip_rows[0]))
2121                 }
2122                 STATE_COMPARE(gl_pack_skip_pixels[0])
2123                 {
2124                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_SKIP_PIXELS, newctx->gl_pack_skip_pixels[0]))
2125                 }
2126                 STATE_COMPARE(gl_pack_alignment[0])
2127                 {
2128                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ALIGNMENT, newctx->gl_pack_alignment[0]))
2129                 }
2130         }
2131
2132         // _pixel_flag2
2133         flag = oldctx->_pixel_flag2 | newctx->_pixel_flag2;
2134         if (flag)
2135         {
2136                 STATE_COMPARE(gl_unpack_row_length[0])
2137                 {
2138                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ROW_LENGTH, newctx->gl_unpack_row_length[0]))
2139                 }
2140                 STATE_COMPARE(gl_unpack_skip_rows[0])
2141                 {
2142                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_ROWS, newctx->gl_unpack_skip_rows[0]))
2143                 }
2144                 STATE_COMPARE(gl_unpack_skip_pixels[0])
2145                 {
2146                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_PIXELS, newctx->gl_unpack_skip_pixels[0]))
2147                 }
2148                 STATE_COMPARE(gl_unpack_alignment[0])
2149                 {
2150                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ALIGNMENT, newctx->gl_unpack_alignment[0]))
2151                 }
2152                 STATE_COMPARE(gl_unpack_image_height[0])
2153                 {
2154                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, newctx->gl_unpack_image_height[0]))
2155                 }
2156                 STATE_COMPARE(gl_unpack_skip_images[0])
2157                 {
2158                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_SKIP_IMAGES, newctx->gl_unpack_skip_images[0]))
2159                 }
2160         }
2161
2162         //------------------//
2163         // _misc_flag1
2164         flag = oldctx->_misc_flag1 | newctx->_misc_flag1;
2165         if (flag)
2166         {
2167                 STATE_COMPARE(gl_front_face[0])
2168                 {
2169                         CHECK_GL_ERROR(_orig_fastpath_glFrontFace(newctx->gl_front_face[0]))
2170                 }
2171                 STATE_COMPARE(gl_line_width[0])
2172                 {
2173                         CHECK_GL_ERROR(_orig_fastpath_glLineWidth(newctx->gl_line_width[0]))
2174                 }
2175                 if ((oldctx->gl_polygon_offset_factor[0] != newctx->gl_polygon_offset_factor[0]) ||
2176                     (oldctx->gl_polygon_offset_units[0]  != newctx->gl_polygon_offset_units[0]))
2177                 {
2178                         CHECK_GL_ERROR(_orig_fastpath_glPolygonOffset(newctx->gl_polygon_offset_factor[0],
2179                                        newctx->gl_polygon_offset_units[0]))
2180                 }
2181                 if ((oldctx->gl_sample_coverage_value[0]  != newctx->gl_sample_coverage_value[0]) ||
2182                     (oldctx->gl_sample_coverage_invert[0] != newctx->gl_sample_coverage_invert[0]))
2183                 {
2184                         CHECK_GL_ERROR(_orig_fastpath_glSampleCoverage(newctx->gl_sample_coverage_value[0],
2185                                        newctx->gl_sample_coverage_invert[0]))
2186                 }
2187                 STATE_COMPARE(gl_fragment_shader_derivative_hint[0])
2188                 {
2189                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES, newctx->gl_fragment_shader_derivative_hint[0]))
2190                 }
2191         }
2192
2193         // _misc_flag2
2194         flag = oldctx->_misc_flag2 | newctx->_misc_flag2;
2195         if (flag)
2196         {
2197                 STATES_COMPARE(gl_scissor_box, 4 * sizeof(GLint))
2198                 {
2199                         CHECK_GL_ERROR(_orig_fastpath_glScissor(newctx->gl_scissor_box[0],
2200                                                                 newctx->gl_scissor_box[1],
2201                                                                 newctx->gl_scissor_box[2],
2202                                                                 newctx->gl_scissor_box[3]))
2203                 }
2204         }
2205
2206         // _misc_flag3
2207         flag = oldctx->_misc_flag3 | newctx->_misc_flag3;
2208         if (flag)
2209         {
2210                 STATE_COMPARE(gl_read_buffer[0])
2211                 {
2212                         CHECK_GL_ERROR(_orig_fastpath_glReadBuffer(newctx->gl_read_buffer[0]))
2213                 }
2214                 STATES_COMPARE(gl_draw_buffers, 16 * sizeof(GLenum))
2215                 {
2216                         int drawBuffSize = 16;
2217                         /* If the  context has only default framebuffer, then size of glDrawBuffers can only be 1 */
2218                         if(fastpath_ostate_has_object_type(&newctx->ostate, GL_OBJECT_TYPE_FRAMEBUFFER) == 0) {
2219                                 drawBuffSize = 1;
2220                         }
2221
2222                         CHECK_GL_ERROR(_orig_fastpath_glDrawBuffers(drawBuffSize, newctx->gl_draw_buffers))
2223                 }
2224
2225                 if (oldctx->gl_transform_feedback_active[0] == GL_TRUE && oldctx->gl_transform_feedback_paused[0] == GL_FALSE)
2226                 {
2227                         CHECK_GL_ERROR(_orig_fastpath_glPauseTransformFeedback())
2228                 }
2229                 STATE_COMPARE(gl_transform_feedback_binding[0])
2230                 {
2231                         CHECK_GL_ERROR(_orig_fastpath_glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, newctx->gl_transform_feedback_binding[0]))
2232                 }
2233                 if (newctx->gl_transform_feedback_active[0] == GL_TRUE && newctx->gl_transform_feedback_paused[0] == GL_FALSE)
2234                 {
2235                         CHECK_GL_ERROR(_orig_fastpath_glResumeTransformFeedback())
2236                 }
2237         }
2238
2239 #ifdef COREGL_USE_MODULE_TRACEPATH
2240         tracepath_api_trace_end("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
2241 #endif // COREGL_USE_MODULE_TRACEPATH
2242
2243         ret = 1;
2244         goto finish;
2245
2246 finish:
2247
2248 #ifdef COREGL_FASTPATH_TRACE_STATE_INFO
2249         if (unlikely(trace_state_flag == 1))
2250                 fastpath_dump_context_states(newctx, 0);
2251 #endif // COREGL_FASTPATH_TRACE_STATE_INFO
2252         return ret;
2253 #undef STATE_COMPARE
2254 #undef STATES_COMPARE
2255 }
2256