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