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