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