Merge changes I4d4a412f,Iff0ebd69 into devel/graphics_engine/master
[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(IS_EXTENSION, 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
30 static void
31 _get_texture_states(GLenum pname, GLint *params)
32 {
33         GLuint cur_active_tex = 0;
34
35         AST(initial_ctx != NULL);
36
37         _orig_fastpath_glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&cur_active_tex);
38         int i;
39         for (i = 0; i < initial_ctx->gl_num_tex_units[0]; i++)
40         {
41                 _orig_fastpath_glActiveTexture(GL_TEXTURE0 + i);
42                 _orig_fastpath_glGetIntegerv(pname, &(((GLint *)params)[i]));
43         }
44         _orig_fastpath_glActiveTexture(cur_active_tex);
45 }
46
47 void
48 init_modules_fastpath()
49 {
50         int fastpath_opt = 0;
51         int fastpath_force_off_opt = 0;
52
53         COREGL_LOG("[CoreGL] <Fastpath> : ");
54
55         fastpath_opt = atoi(get_env_setting("COREGL_FASTPATH"));
56         fastpath_force_off_opt = atoi(get_env_setting("COREGL_FASTPATH_FORCE_OFF"));
57
58         if (fastpath_force_off_opt == 1)
59         {
60                 COREGL_LOG("\E[40;31;1m(DISABLED by force option)\E[0m ");
61                 fastpath_opt = 0;
62         }
63
64         switch (fastpath_opt)
65         {
66                 case 1:
67                         COREGL_LOG("(%d) Fastpath enabled...\n", fastpath_opt);
68                         fp_opt = FP_FAST_PATH;
69                         break;
70                 default:
71                         COREGL_LOG("(%d) Default API path enabled...\n", fastpath_opt);
72                         fp_opt = FP_NORMAL_PATH;
73                         break;
74         }
75
76         debug_nofp = atoi(get_env_setting("COREGL_DEBUG_NOFP"));
77
78 }
79
80 void
81 deinit_modules_fastpath()
82 {
83         GLContext_List *current = NULL;
84
85         AST(mutex_lock(&ctx_list_access_mutex) == 1);
86
87         // Destroy remained context & Detect leaks
88         int retry_destroy = 0;
89
90         while (1)
91         {
92                 retry_destroy = 0;
93                 current = glctx_list;
94                 while (current)
95                 {
96                         if (current->cstate != NULL)
97                         {
98                                 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);
99
100                                 _orig_fastpath_eglMakeCurrent(current->cstate->rdpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
101                                 _orig_fastpath_eglDestroyContext(current->cstate->rdpy, current->cstate->rctx);
102
103                                 fastpath_remove_context_states_from_list(current->cstate, NULL);
104                                 retry_destroy = 1;
105                                 break;
106                         }
107
108                         glctx_list = current->next;
109                         free(current);
110                         current = glctx_list;
111                 }
112                 if (retry_destroy == 0) break;
113         }
114         goto finish;
115
116 finish:
117         AST(mutex_unlock(&ctx_list_access_mutex) == 1);
118 }
119
120 void
121 init_modules_tstate_fastpath(GLThreadState *tstate)
122 {
123         MY_MODULE_TSTATE *tstate_mt = NULL;
124
125         tstate_mt = (MY_MODULE_TSTATE *)calloc(1, sizeof(MY_MODULE_TSTATE));
126
127         tstate_mt->binded_api = EGL_OPENGL_ES_API;
128
129         tstate->module_data[MY_MODULE_ID] = tstate_mt;
130 }
131
132 void
133 deinit_modules_tstate_fastpath(GLThreadState *tstate)
134 {
135         if (tstate->module_data[MY_MODULE_ID] != NULL)
136         {
137                 free(tstate->module_data[MY_MODULE_ID]);
138                 tstate->module_data[MY_MODULE_ID] = NULL;
139         }
140 }
141
142 void
143 fastpath_apply_overrides()
144 {
145         switch(fp_opt)
146         {
147                 case FP_FAST_PATH:
148                         fastpath_apply_overrides_egl(1);
149                         fastpath_apply_overrides_gl(1);
150                         break;
151                 case FP_NORMAL_PATH:
152                         break;
153                 default:
154                         COREGL_ERR("Invalide GL Override Option!!!\n");
155                         break;
156         }
157 }
158
159
160 void
161 fastpath_apply_overrides_egl(int enable)
162 {
163 #define _COREGL_SYMBOL(IS_EXTENSION, RET_TYPE, FUNC_NAME, PARAM_LIST)     COREGL_INIT_ORIGINAL(_orig_fastpath_, FUNC_NAME);
164 # include "../../headers/sym_egl.h"
165 #undef _COREGL_SYMBOL
166
167         COREGL_OVERRIDE(fastpath_, eglGetProcAddress);
168
169         COREGL_OVERRIDE(fastpath_, eglBindAPI);
170         COREGL_OVERRIDE(fastpath_, eglQueryAPI);
171
172         COREGL_OVERRIDE(fastpath_, eglCreateContext);
173         COREGL_OVERRIDE(fastpath_, eglCreateImageKHR);
174         COREGL_OVERRIDE(fastpath_, eglMakeCurrent);
175         COREGL_OVERRIDE(fastpath_, eglDestroyContext);
176         COREGL_OVERRIDE(fastpath_, eglQueryContext);
177         COREGL_OVERRIDE(fastpath_, eglGetCurrentContext);
178         COREGL_OVERRIDE(fastpath_, eglReleaseThread);
179         COREGL_OVERRIDE(fastpath_, eglGetCurrentSurface);
180         COREGL_OVERRIDE(fastpath_, eglTerminate);
181
182 }
183
184 void
185 fastpath_apply_overrides_gl(int enable)
186 {
187 #define _COREGL_SYMBOL(IS_EXTENSION, RET_TYPE, FUNC_NAME, PARAM_LIST)     COREGL_INIT_ORIGINAL(_orig_fastpath_, FUNC_NAME);
188 # include "../../headers/sym_gl.h"
189 #undef _COREGL_SYMBOL
190
191         if (debug_nofp != 1)
192         {
193                 COREGL_OVERRIDE(fastpath_, glGetError);
194
195                 COREGL_OVERRIDE(fastpath_, glGetIntegerv);
196                 COREGL_OVERRIDE(fastpath_, glGetFloatv);
197                 COREGL_OVERRIDE(fastpath_, glGetBooleanv);
198
199                 COREGL_OVERRIDE(fastpath_, glActiveTexture);
200                 COREGL_OVERRIDE(fastpath_, glGenTextures);
201                 COREGL_OVERRIDE(fastpath_, glBindTexture);
202                 COREGL_OVERRIDE(fastpath_, glIsTexture);
203                 COREGL_OVERRIDE(fastpath_, glDeleteTextures);
204                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2D);
205
206                 COREGL_OVERRIDE(fastpath_, glGenBuffers);
207                 COREGL_OVERRIDE(fastpath_, glBindBuffer);
208                 COREGL_OVERRIDE(fastpath_, glIsBuffer);
209                 COREGL_OVERRIDE(fastpath_, glDeleteBuffers);
210
211                 COREGL_OVERRIDE(fastpath_, glGenFramebuffers);
212                 COREGL_OVERRIDE(fastpath_, glBindFramebuffer);
213                 COREGL_OVERRIDE(fastpath_, glIsFramebuffer);
214                 COREGL_OVERRIDE(fastpath_, glDeleteFramebuffers);
215
216                 COREGL_OVERRIDE(fastpath_, glGenRenderbuffers);
217                 COREGL_OVERRIDE(fastpath_, glBindRenderbuffer);
218                 COREGL_OVERRIDE(fastpath_, glFramebufferRenderbuffer);
219                 COREGL_OVERRIDE(fastpath_, glIsRenderbuffer);
220                 COREGL_OVERRIDE(fastpath_, glDeleteRenderbuffers);
221
222                 COREGL_OVERRIDE(fastpath_, glCreateShader);
223                 COREGL_OVERRIDE(fastpath_, glCreateProgram);
224                 COREGL_OVERRIDE(fastpath_, glAttachShader);
225                 COREGL_OVERRIDE(fastpath_, glCompileShader);
226                 COREGL_OVERRIDE(fastpath_, glShaderBinary);
227                 COREGL_OVERRIDE(fastpath_, glDeleteShader);
228                 COREGL_OVERRIDE(fastpath_, glDetachShader);
229                 COREGL_OVERRIDE(fastpath_, glGetShaderiv);
230                 COREGL_OVERRIDE(fastpath_, glGetShaderInfoLog);
231                 COREGL_OVERRIDE(fastpath_, glGetShaderSource);
232                 COREGL_OVERRIDE(fastpath_, glIsShader);
233                 COREGL_OVERRIDE(fastpath_, glShaderSource);
234                 COREGL_OVERRIDE(fastpath_, glBindAttribLocation);
235                 COREGL_OVERRIDE(fastpath_, glDeleteProgram);
236                 COREGL_OVERRIDE(fastpath_, glGetActiveAttrib);
237                 COREGL_OVERRIDE(fastpath_, glGetActiveUniform);
238                 COREGL_OVERRIDE(fastpath_, glGetAttachedShaders);
239                 COREGL_OVERRIDE(fastpath_, glGetAttribLocation);
240                 COREGL_OVERRIDE(fastpath_, glGetProgramiv);
241                 COREGL_OVERRIDE(fastpath_, glGetProgramInfoLog);
242                 COREGL_OVERRIDE(fastpath_, glGetUniformfv);
243                 COREGL_OVERRIDE(fastpath_, glGetUniformiv);
244                 COREGL_OVERRIDE(fastpath_, glGetUniformLocation);
245                 COREGL_OVERRIDE(fastpath_, glIsProgram);
246                 COREGL_OVERRIDE(fastpath_, glLinkProgram);
247                 COREGL_OVERRIDE(fastpath_, glUseProgram);
248                 COREGL_OVERRIDE(fastpath_, glValidateProgram);
249
250                 COREGL_OVERRIDE(fastpath_, glBlendColor);
251                 COREGL_OVERRIDE(fastpath_, glBlendEquation);
252                 COREGL_OVERRIDE(fastpath_, glBlendEquationSeparate);
253                 COREGL_OVERRIDE(fastpath_, glBlendFunc);
254                 COREGL_OVERRIDE(fastpath_, glBlendFuncSeparate);
255                 COREGL_OVERRIDE(fastpath_, glClearColor);
256                 COREGL_OVERRIDE(fastpath_, glClearDepthf);
257                 COREGL_OVERRIDE(fastpath_, glClearStencil);
258                 COREGL_OVERRIDE(fastpath_, glColorMask);
259                 COREGL_OVERRIDE(fastpath_, glCullFace);
260                 COREGL_OVERRIDE(fastpath_, glDepthFunc);
261                 COREGL_OVERRIDE(fastpath_, glDepthMask);
262                 COREGL_OVERRIDE(fastpath_, glDepthRangef);
263                 COREGL_OVERRIDE(fastpath_, glDisable);
264                 COREGL_OVERRIDE(fastpath_, glDisableVertexAttribArray);
265                 COREGL_OVERRIDE(fastpath_, glEnable);
266                 COREGL_OVERRIDE(fastpath_, glEnableVertexAttribArray);
267                 COREGL_OVERRIDE(fastpath_, glFrontFace);
268                 COREGL_OVERRIDE(fastpath_, glHint);
269                 COREGL_OVERRIDE(fastpath_, glLineWidth);
270                 COREGL_OVERRIDE(fastpath_, glPixelStorei);
271                 COREGL_OVERRIDE(fastpath_, glPolygonOffset);
272                 COREGL_OVERRIDE(fastpath_, glSampleCoverage);
273                 COREGL_OVERRIDE(fastpath_, glScissor);
274                 COREGL_OVERRIDE(fastpath_, glStencilFunc);
275                 COREGL_OVERRIDE(fastpath_, glStencilFuncSeparate);
276                 COREGL_OVERRIDE(fastpath_, glStencilMask);
277                 COREGL_OVERRIDE(fastpath_, glStencilMaskSeparate);
278                 COREGL_OVERRIDE(fastpath_, glStencilOp);
279                 COREGL_OVERRIDE(fastpath_, glStencilOpSeparate);
280                 COREGL_OVERRIDE(fastpath_, glVertexAttrib1f);
281                 COREGL_OVERRIDE(fastpath_, glVertexAttrib1fv);
282                 COREGL_OVERRIDE(fastpath_, glVertexAttrib2f);
283                 COREGL_OVERRIDE(fastpath_, glVertexAttrib2fv);
284                 COREGL_OVERRIDE(fastpath_, glVertexAttrib3f);
285                 COREGL_OVERRIDE(fastpath_, glVertexAttrib3fv);
286                 COREGL_OVERRIDE(fastpath_, glVertexAttrib4f);
287                 COREGL_OVERRIDE(fastpath_, glVertexAttrib4fv);
288                 COREGL_OVERRIDE(fastpath_, glVertexAttribPointer);
289                 COREGL_OVERRIDE(fastpath_, glViewport);
290
291                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture2DMultisampleEXT);
292                 COREGL_OVERRIDE(fastpath_, glGetProgramBinaryOES);
293                 COREGL_OVERRIDE(fastpath_, glProgramBinaryOES);
294                 COREGL_OVERRIDE(fastpath_, glProgramParameteriEXT);
295                 COREGL_OVERRIDE(fastpath_, glEGLImageTargetTexture2DOES);
296                 COREGL_OVERRIDE(fastpath_, glFramebufferTexture3DOES);
297
298         }
299         else
300         {
301                 COREGL_LOG("\E[40;35;1m[CoreGL] SKIP GL FASTPATH...\E[0m\n");
302         }
303 }
304
305 #undef OVERRIDE
306
307 static inline GL_Object_Hash_Base *
308 _get_shared_object_hash(GL_Shared_Object_State *sostate, GL_Object_Type type)
309 {
310         switch (type)
311         {
312                 case GL_OBJECT_TYPE_TEXTURE:
313                         return &sostate->texture;
314                 case GL_OBJECT_TYPE_BUFFER:
315                         return &sostate->buffer;
316                 case GL_OBJECT_TYPE_FRAMEBUFFER:
317                         return &sostate->framebuffer;
318                 case GL_OBJECT_TYPE_RENDERBUFFER:
319                         return &sostate->renderbuffer;
320                 case GL_OBJECT_TYPE_PROGRAM:
321                         return &sostate->program;
322                 default:
323                         return NULL;
324         }
325 }
326
327 static inline GL_Object_Hash_Base *
328 _get_shared_object_hash_real(GL_Shared_Object_State *sostate, GL_Object_Type type)
329 {
330         switch (type)
331         {
332                 case GL_OBJECT_TYPE_TEXTURE:
333                         return &sostate->texture_real;
334                 case GL_OBJECT_TYPE_BUFFER:
335                         return &sostate->buffer_real;
336                 case GL_OBJECT_TYPE_FRAMEBUFFER:
337                         return &sostate->framebuffer_real;
338                 case GL_OBJECT_TYPE_RENDERBUFFER:
339                         return &sostate->renderbuffer_real;
340                 case GL_OBJECT_TYPE_PROGRAM:
341                         return &sostate->program_real;
342                 default:
343                         return NULL;
344         }
345 }
346
347 int
348 fastpath_add_context_state_to_list(const void *option, const int option_len, GLContextState *cstate, Mutex *mtx)
349 {
350         int ret = 0;
351         int tid = 0;
352         GLContext_List *current = NULL;
353         GLContext_List *newitm = NULL;
354
355         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
356
357         AST(cstate != NULL);
358
359         tid = get_current_thread();
360
361         current = glctx_list;
362         while (current != NULL)
363         {
364                 if (current->option_len == option_len &&
365                     memcmp(current->option, option, option_len) == 0 &&
366                     current->thread_id == tid)
367                 {
368                         AST(current->cstate == cstate);
369                         goto finish;
370                 }
371                 current = current->next;
372         }
373
374         newitm = (GLContext_List *)calloc(1, sizeof(GLContext_List));
375         if (newitm == NULL)
376         {
377                 COREGL_ERR("Failed to create context list.\n");
378                 goto finish;
379         }
380
381         newitm->cstate = cstate;
382         newitm->thread_id = tid;
383         newitm->option_len = option_len;
384         newitm->option = (void *)malloc(option_len);
385         memcpy(newitm->option, option, option_len);
386
387         if (glctx_list != NULL)
388                 newitm->next = glctx_list;
389
390         glctx_list = newitm;
391
392         ret = 1;
393         goto finish;
394
395 finish:
396         if (ret != 1)
397         {
398                 if (newitm != NULL)
399                 {
400                         free(newitm);
401                         newitm = NULL;
402                 }
403                 if (cstate != NULL)
404                 {
405                         free(cstate);
406                         cstate = NULL;
407                 }
408         }
409         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
410
411         return ret;
412 }
413
414 GLContextState *
415 fastpath_get_context_state_from_list(const void *option, const int option_len, Mutex *mtx)
416 {
417         GLContextState *ret = NULL;
418         GLContext_List *current = NULL;
419         int tid = 0;
420
421         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
422
423         tid = get_current_thread();
424
425         current = glctx_list;
426         while (current != NULL)
427         {
428                 if (current->option_len == option_len &&
429                     memcmp(current->option, option, option_len) == 0 &&
430                     current->thread_id == tid)
431                 {
432                         ret = current->cstate;
433                         goto finish;
434                 }
435                 current = current->next;
436         }
437         goto finish;
438
439 finish:
440         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
441         return ret;
442 }
443
444 int
445 fastpath_remove_context_states_from_list(GLContextState *cstate, Mutex *mtx)
446 {
447         int ret = 0;
448         int tid = 0;
449         GLContext_List *olditm = NULL;
450         GLContext_List *current = NULL;
451
452         if (mtx != NULL) AST(mutex_lock(mtx) == 1);
453
454         AST(cstate != NULL);
455
456         tid = get_current_thread();
457         current = glctx_list;
458
459         while (current != NULL)
460         {
461                 if (current->cstate == cstate)
462                 {
463                         GLContext_List *nextitm = NULL;
464                         if (olditm != NULL)
465                         {
466                                 olditm->next = current->next;
467                                 nextitm = olditm->next;
468                         }
469                         else
470                         {
471                                 glctx_list = current->next;
472                                 nextitm = glctx_list;
473                         }
474                         if (current->option != NULL)
475                         {
476                                 AST(current->option_len > 0);
477                                 free(current->option);
478                                 current->option = NULL;
479                                 current->option_len = 0;
480                         }
481                         free(current);
482                         ret = 1;
483                         current = nextitm;
484                         continue;
485                 }
486                 olditm = current;
487                 current = current->next;
488         }
489         goto finish;
490
491 finish:
492         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
493         return ret;
494 }
495
496 void
497 fastpath_sostate_init(GL_Shared_Object_State *sostate)
498 {
499 #define HASH_INIT(hash_base) \
500         hash_base.hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * GL_OBJECT_HASH_BASE); \
501         hash_base.hash_size = GL_OBJECT_HASH_BASE;
502
503         HASH_INIT(sostate->texture);
504         HASH_INIT(sostate->buffer);
505         HASH_INIT(sostate->framebuffer);
506         HASH_INIT(sostate->renderbuffer);
507         HASH_INIT(sostate->program);
508
509         HASH_INIT(sostate->texture_real);
510         HASH_INIT(sostate->buffer_real);
511         HASH_INIT(sostate->framebuffer_real);
512         HASH_INIT(sostate->renderbuffer_real);
513         HASH_INIT(sostate->program_real);
514
515 #undef HASH_INIT
516 }
517
518 static void
519 _add_hash(GL_Object_Hash_Base *hash_base, GL_Object_Hash *data)
520 {
521         int array_idx = data->hash_key & (hash_base->hash_size - 1);
522         if (hash_base->hash_field[array_idx] == NULL)
523         {
524                 hash_base->hash_field[array_idx] = data;
525         }
526         else
527         {
528                 GL_Object_Hash *current = hash_base->hash_field[array_idx];
529                 while(current->next)
530                 {
531                         AST(current->hash_key != data->hash_key);
532                         current = current->next;
533                 }
534                 current->next = data;
535         }
536         data->next = NULL;
537         hash_base->item_size++;
538 }
539
540 static int
541 _remove_hash(GL_Object_Hash_Base *hash_base, GLuint hash)
542 {
543         int ret = 0;
544         int array_idx = hash & (hash_base->hash_size - 1);
545
546         GL_Object_Hash *current = hash_base->hash_field[array_idx];
547         GL_Object_Hash *prev = NULL;
548
549         while(current)
550         {
551                 if (current->hash_key == hash)
552                 {
553                         if (prev != NULL)
554                                 prev->next = current->next;
555                         else
556                                 hash_base->hash_field[array_idx] = current->next;
557                         hash_base->item_size--;
558                         ret = 1;
559                         break;
560                 }
561                 prev = current;
562                 current = current->next;
563         }
564
565         return ret;
566 }
567
568 static void
569 _free_hash_list(GL_Object_Hash_Base *hash_base, int free_data)
570 {
571         if (hash_base->item_size == 0) return;
572
573         for (int i = 0; i < hash_base->hash_size; i++)
574         {
575                 if (hash_base->hash_field[i] != NULL)
576                 {
577                         GL_Object_Hash *current = hash_base->hash_field[i];
578
579                         while (current != NULL)
580                         {
581                                 GL_Object_Hash *current_next = current->next;
582
583                                 if (free_data == 1 && current->item != NULL)
584                                 {
585                                         free(current->item);
586                                 }
587
588                                 free(current);
589                                 hash_base->item_size--;
590                                 current = current_next;
591                         }
592                 }
593         }
594 }
595
596 void
597 fastpath_sostate_deinit(GL_Shared_Object_State *sostate)
598 {
599 #define HASH_DEINIT(hash_base, free_data) \
600         _free_hash_list(&hash_base, free_data); \
601         free(hash_base.hash_field); \
602         hash_base.hash_size = 0;
603
604         HASH_DEINIT(sostate->texture, 1);
605         HASH_DEINIT(sostate->buffer, 1);
606         HASH_DEINIT(sostate->framebuffer, 1);
607         HASH_DEINIT(sostate->renderbuffer, 1);
608         HASH_DEINIT(sostate->program, 1);
609
610         HASH_DEINIT(sostate->texture_real, 0);
611         HASH_DEINIT(sostate->buffer_real, 0);
612         HASH_DEINIT(sostate->framebuffer_real, 0);
613         HASH_DEINIT(sostate->renderbuffer_real, 0);
614         HASH_DEINIT(sostate->program_real, 0);
615
616 #undef HASH_DEINIT
617 }
618
619 #define FIND_HASH(hash_base, key, ret) \
620 { \
621         GL_Object_Hash *fh_current = hash_base->hash_field[(key) & (hash_base->hash_size - 1)]; \
622         while(fh_current) \
623         { \
624                 if (fh_current->hash_key == (key)) \
625                 { \
626                         ret = fh_current; \
627                         break; \
628                 } \
629                 fh_current = fh_current->next; \
630         } \
631 }
632
633 void
634 _sostate_hash_check(GL_Object_Hash_Base *hash_base)
635 {
636         if (hash_base->item_size + 1 < hash_base->hash_size)
637                 return;
638
639         int oldsize = hash_base->hash_size;
640         GL_Object_Hash **oldfield = hash_base->hash_field;
641
642         hash_base->hash_size = oldsize << 1;
643         hash_base->hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * hash_base->hash_size);
644         AST(hash_base->hash_field != NULL);
645
646         for (int i = 0; i < oldsize; i++)
647         {
648                 if (oldfield[i] != NULL)
649                 {
650                         GL_Object_Hash *current = oldfield[i];
651
652                         while (current != NULL)
653                         {
654                                 GL_Object_Hash *current_next = current->next;
655                                 _add_hash(hash_base, current);
656                                 hash_base->item_size--;
657                                 current = current_next;
658                         }
659                 }
660         }
661         free(oldfield);
662
663 }
664
665 GLuint
666 fastpath_sostate_create_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint real_name)
667 {
668         GLuint ret = _COREGL_INT_INIT_VALUE;
669
670         GL_Object_Hash_Base *hash_base = NULL;
671         GL_Object_Hash_Base *hash_base_real = NULL;
672         int newid = _COREGL_INT_INIT_VALUE;
673
674         hash_base = _get_shared_object_hash(sostate, type);
675         hash_base_real = _get_shared_object_hash_real(sostate, type);
676
677         newid = hash_base->last_id + 1;
678         if (newid >= hash_base->hash_size)
679         {
680                 hash_base->is_looped = 1;
681                 newid = 1;
682                 hash_base->last_id = 1;
683         }
684
685         if (hash_base->is_looped != 0)
686         {
687                 int i;
688                 int findingid = newid;
689                 newid = -1;
690                 for (i = 0; i < hash_base->hash_size; i++)
691                 {
692                         GL_Object_Hash *exist_hash = NULL;
693                         FIND_HASH(hash_base, findingid, exist_hash);
694                         if (exist_hash == NULL)
695                         {
696                                 newid = findingid;
697                                 break;
698                         }
699                         findingid++;
700                         if (findingid >= hash_base->hash_size) findingid = 1;
701                 }
702                 AST(newid != -1);
703         }
704         hash_base->last_id = newid;
705
706         {
707                 GL_Object *newobj = (GL_Object *)calloc(1, sizeof(GL_Object));
708                 AST(newobj != NULL);
709                 newobj->id = (int)type + newid;
710                 newobj->real_id = real_name;
711                 newobj->ref_count = 1;
712                 ret = newobj->id;
713
714                 GL_Object_Hash *newobj_hash = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
715                 AST(newobj_hash != NULL);
716                 newobj_hash->item = newobj;
717                 newobj_hash->hash_key = newid;
718                 _add_hash(hash_base, newobj_hash);
719
720                 GL_Object_Hash *newobj_hash_real = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
721                 AST(newobj_hash_real != NULL);
722                 newobj_hash_real->item = newobj;
723                 newobj_hash_real->hash_key = real_name;
724                 _add_hash(hash_base_real, newobj_hash_real);
725         }
726
727         _sostate_hash_check(hash_base);
728         _sostate_hash_check(hash_base_real);
729
730         goto finish;
731
732 finish:
733         return ret;
734 }
735
736 #define FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object) \
737         if ((hash) < 0) { ret = 0; goto finish; } \
738         { \
739                 GL_Object_Hash *object_hash = NULL; \
740                 FIND_HASH((hash_base), (hash), object_hash); \
741                 if (object_hash == NULL) { ret = 0; goto finish; } \
742                 (object) = object_hash->item; \
743                 if ((object) == NULL) { ret = 0; goto finish; } \
744         }
745
746 GLuint
747 fastpath_sostate_remove_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
748 {
749         GLuint ret = _COREGL_INT_INIT_VALUE;
750
751         GL_Object_Hash_Base *hash_base = NULL;
752         GL_Object_Hash_Base *hash_base_real = NULL;
753         GL_Object *object = NULL;
754
755         hash_base = _get_shared_object_hash(sostate, type);
756         hash_base_real = _get_shared_object_hash_real(sostate, type);
757
758         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
759
760         object->ref_count--;
761
762         if (object->ref_count <= 0)
763         {
764                 GL_Object_Hash *object_hash = NULL;
765
766                 FIND_HASH(hash_base, object->id - (int)type, object_hash);
767                 AST(object_hash != NULL);
768                 _remove_hash(hash_base, object->id - (int)type);
769                 free(object_hash);
770                 object_hash = NULL;
771
772                 FIND_HASH(hash_base_real, object->real_id, object_hash);
773                 AST(object_hash != NULL);
774                 _remove_hash(hash_base_real, object->real_id);
775                 free(object_hash);
776                 object_hash = NULL;
777
778                 free(object);
779                 object = NULL;
780         }
781
782         ret = 1;
783         goto finish;
784
785 finish:
786         return ret;
787 }
788
789 GLuint
790 fastpath_sostate_get_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
791 {
792         GLuint ret = _COREGL_INT_INIT_VALUE;
793
794         GL_Object_Hash_Base *hash_base = NULL;
795         GL_Object *object = NULL;
796
797         hash_base = _get_shared_object_hash(sostate, type);
798
799         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
800
801         ret = object->real_id;
802         goto finish;
803
804 finish:
805         return ret;
806 }
807
808 GLint
809 fastpath_sostate_set_object_tag(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name, GLvoid *tag)
810 {
811         GLint ret = _COREGL_INT_INIT_VALUE;
812
813         GL_Object_Hash_Base *hash_base = NULL;
814         GL_Object *object = NULL;
815         int hash = _COREGL_INT_INIT_VALUE;
816
817         hash_base = _get_shared_object_hash(sostate, type);
818
819         hash = glue_name - (int)type;
820
821         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object);
822
823         AST(object->tag == NULL);
824         object->tag = tag;
825         ret = 1;
826         goto finish;
827
828 finish:
829         return ret;
830 }
831
832 GLvoid *
833 fastpath_sostate_get_object_tag(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
834 {
835         GLvoid *ret = NULL;
836
837         GL_Object_Hash_Base *hash_base = NULL;
838         GL_Object *object = NULL;
839
840         hash_base = _get_shared_object_hash(sostate, type);
841
842         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
843
844         ret = object->tag;
845         goto finish;
846
847 finish:
848         return ret;
849 }
850
851 GLuint
852 fastpath_sostate_find_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint real_name)
853 {
854         GLuint ret = _COREGL_INT_INIT_VALUE;
855
856         GL_Object_Hash_Base *hash_base_real = NULL;
857         GL_Object *object = NULL;
858
859         hash_base_real = _get_shared_object_hash_real(sostate, type);
860
861         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base_real, real_name, object);
862
863         ret = object->id;
864         goto finish;
865
866 finish:
867         return ret;
868 }
869
870 GLint
871 fastpath_sostate_use_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
872 {
873         GLint ret = _COREGL_INT_INIT_VALUE;
874
875         GL_Object_Hash_Base *hash_base = NULL;
876         GL_Object *object = NULL;
877
878         hash_base = _get_shared_object_hash(sostate, type);
879
880         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
881
882         object->ref_count++;
883         ret = 1;
884         goto finish;
885
886 finish:
887         return ret;
888 }
889
890 void
891 fastpath_dump_context_states(GLGlueContext *ctx, int force_output)
892 {
893         static struct timeval tv_last = { 0, 0 };
894
895         if (unlikely(trace_state_flag != 1)) return;
896
897         if (!force_output)
898         {
899                 struct timeval tv_now = { 0, 0 };
900                 AST(gettimeofday(&tv_now, NULL) == 0);
901                 if (tv_now.tv_sec - tv_last.tv_sec < _COREGL_TRACE_OUTPUT_INTERVAL_SEC)
902                 {
903                         goto finish;
904                 }
905                 tv_last = tv_now;
906         }
907
908         TRACE("\n");
909         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
910         TRACE("\E[40;32;1m  State info \E[1;37;1m: <PID = %d> GlueCTX = %p\E[0m\n", getpid(), ctx);
911         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
912
913 #define PRINTF_CHAR_GLenum "%10d"
914 #define PRINTF_CHAR_GLboolean "%10d"
915 #define PRINTF_CHAR_GLint "%10d"
916 #define PRINTF_CHAR_GLsizei "%10u"
917 #define PRINTF_CHAR_GLuint "%10u"
918 #define PRINTF_CHAR_GLuintmask "0x%8X"
919
920 #define PRINTF_CHAR_GLclampf "%10.6f"
921 #define PRINTF_CHAR_GLfloat "%10.6f"
922
923 #define PRINTF_CHAR_GLvoidptr "%10p"
924
925 #define PRINTF_CHAR(type) PRINTF_CHAR_##type
926
927 #define INITIAL_CTX initial_ctx
928 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
929    { \
930       TYPE valuedata[SIZE]; \
931       TYPE *value = NULL; \
932       value = valuedata; GET_STMT; value = valuedata; \
933       TRACE("\E[40;37;1m %-30.30s : (\E[0m ", #NAME); \
934       for (int i = 0; i < SIZE; i++) \
935       { \
936          if (i > 0) { \
937             if (i % 4 == 0) \
938                TRACE("\n %-30.30s     ", "");\
939             else \
940                TRACE(", "); \
941          } \
942          TRACE(PRINTF_CHAR(TYPE), ctx->NAME[i]); \
943          TRACE("["PRINTF_CHAR(TYPE)"]", value[i]); \
944       } \
945       TRACE(" \E[40;37;1m)\E[0m\n"); \
946    }
947 # include "coregl_fastpath_state.h"
948 #undef GLUE_STATE
949 #undef INITIAL_CTX
950
951         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
952         TRACE("\n");
953
954         TRACE_END();
955
956 finish:
957         return;
958 }
959
960 int
961 fastpath_init_context_states(GLGlueContext *ctx)
962 {
963         int ret = 0;
964
965         AST(mutex_lock(&init_context_mutex) == 1);
966
967         if (ctx == NULL)
968         {
969                 COREGL_ERR("Context NULL\n");
970                 ret = 0;
971                 goto finish;
972         }
973
974         AST(ctx->initialized == 0);
975         AST(ctx->sostate != NULL);
976
977         if (initial_ctx == NULL)
978         {
979                 initial_ctx = (GLGlueContext *)calloc(1, sizeof(GLGlueContext));
980                 AST(initial_ctx != NULL);
981
982 //#define FORCE_DEFAULT_VALUE
983 #ifdef FORCE_DEFAULT_VALUE
984 # define INITIAL_CTX initial_ctx
985 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
986       { \
987          int i; \
988          TYPE valuedata[SIZE]; \
989          TYPE *value = NULL; \
990          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
991          value = valuedata; DEFAULT_STMT; value = valuedata; \
992          for (i = 0; i < SIZE; i++) \
993          { \
994             if (*((char *)(&value[i])) == 0xcc) \
995             { \
996                memset(&value[i], 0xaa, sizeof(TYPE)); \
997                value = valuedata; DEFAULT_STMT; value = valuedata; \
998                if (*((char *)(&value[i])) == 0xaa) \
999                { \
1000                   COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
1001                   break; \
1002                } \
1003             } \
1004             initial_ctx->NAME[i] = value[i]; \
1005          } \
1006       }
1007 #  include "coregl_fastpath_state.h"
1008 # undef GLUE_STATE
1009 # undef INITIAL_CTX
1010 #else
1011 # define INITIAL_CTX initial_ctx
1012 # define SET_GLUE_VALUE(DEFAULT_STMT, FALLBACK_STMT) \
1013       if (try_step == 1) \
1014       { \
1015          value = valuedata; DEFAULT_STMT; value = valuedata; \
1016       } \
1017       else \
1018       { \
1019          value = valuedata; FALLBACK_STMT; value = valuedata; \
1020       }
1021
1022 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1023       { \
1024          int i; \
1025          int try_step = 0;\
1026          TYPE valuedata[SIZE]; \
1027          TYPE *value = NULL; \
1028          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1029          do { \
1030             try_step++; \
1031             SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1032             for (i = 0; i < SIZE; i++) \
1033             { \
1034                if (*((char *)(&value[i])) == 0xcc) \
1035                { \
1036                   memset(&value[i], 0xaa, sizeof(TYPE)); \
1037                   SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1038                   if (*((char *)(&value[i])) == 0xaa) \
1039                   { \
1040                      try_step++; \
1041                      if (try_step == 2) \
1042                      { \
1043                         COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
1044                      } \
1045                      break; \
1046                   } \
1047                } \
1048                initial_ctx->NAME[i] = value[i]; \
1049             } \
1050             if (try_step != 2) \
1051             { \
1052                value = valuedata; DEFAULT_STMT; value = valuedata; \
1053                for (i = 0; i < SIZE; i++) \
1054                { \
1055                   if (initial_ctx->NAME[i] != value[i]) \
1056                   { \
1057                      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]); \
1058                   } \
1059                } \
1060             } \
1061          } \
1062          while (try_step == 2); \
1063       }
1064 #  include "coregl_fastpath_state.h"
1065 # undef SET_GLUE_VALUE
1066 # undef GLUE_STATE
1067 # undef INITIAL_CTX
1068 #endif
1069
1070                 if (initial_ctx->gl_num_vertex_attribs[0] > MAX_VERTEX_ATTRIBS)
1071                 {
1072                         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]);
1073                 }
1074                 if (initial_ctx->gl_num_tex_units[0] > MAX_TEXTURE_UNITS)
1075                 {
1076                         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]);
1077                 }
1078         }
1079
1080         {
1081                 int i;
1082 #define INITIAL_CTX initial_ctx
1083 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1084          for (i = 0; i < SIZE; i++) \
1085          { \
1086             ctx->NAME[i] = initial_ctx->NAME[i]; \
1087          }
1088 # include "coregl_fastpath_state.h"
1089 #undef GLUE_STATE
1090 #undef INITIAL_CTX
1091         }
1092
1093         ctx->initialized = 1;
1094         ret = 1;
1095         goto finish;
1096
1097 finish:
1098         AST(mutex_unlock(&init_context_mutex) == 1);
1099
1100         return ret;
1101 }
1102
1103 #ifdef COREGL_USE_MODULE_TRACEPATH
1104 extern void *tracepath_api_trace_begin(const char *name, void *hint, int trace_total_time);
1105 extern void *tracepath_api_trace_end(const char *name, void *hint, int trace_total_time);
1106 #endif
1107
1108 #define CHECK_GL_ERROR(func) \
1109         { \
1110                 func; \
1111                 int err = _orig_fastpath_glGetError(); \
1112                 if (err != GL_NO_ERROR) \
1113                 { \
1114                         COREGL_ERR("\E[40;31;1m(GL %p) : %s returns GL error 0x%X\E[0m\n", oldctx->cstate, #func, err); \
1115                         goto finish; \
1116                 } \
1117         }
1118
1119 int
1120 fastpath_make_context_current(GLGlueContext *oldctx, GLGlueContext *newctx)
1121 {
1122         int ret = 0;
1123         unsigned char flag = 0;
1124         int i = 0;
1125
1126         if (debug_nofp == 1)
1127         {
1128                 ret = 1;
1129                 goto finish;
1130         }
1131
1132         // Return if they're the same
1133         if (oldctx == newctx)
1134         {
1135                 ret = 1;
1136                 goto finish;
1137         }
1138
1139 #define STATE_COMPARE(state) \
1140    if ((oldctx->state) != (newctx->state))
1141
1142 #define STATES_COMPARE(state_ptr, bytes) \
1143    if ((memcmp((oldctx->state_ptr), (newctx->state_ptr), (bytes))) != 0)
1144
1145
1146 #ifdef COREGL_USE_MODULE_TRACEPATH
1147         static void *trace_hint_glfinish = NULL;
1148         trace_hint_glfinish = tracepath_api_trace_begin("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1149 #endif // COREGL_USE_MODULE_TRACEPATH
1150
1151         {
1152                 int err = _orig_fastpath_glGetError();
1153                 if (err != GL_NO_ERROR && oldctx->gl_error == GL_NO_ERROR)
1154                         oldctx->gl_error = err;
1155         }
1156
1157         CHECK_GL_ERROR(_orig_fastpath_glFlush())
1158
1159 #ifdef COREGL_USE_MODULE_TRACEPATH
1160         tracepath_api_trace_end("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1161 #endif // COREGL_USE_MODULE_TRACEPATH
1162
1163 #ifdef COREGL_USE_MODULE_TRACEPATH
1164         static void *trace_hint_bindbuffers = NULL;
1165         trace_hint_bindbuffers = tracepath_api_trace_begin("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1166 #endif // COREGL_USE_MODULE_TRACEPATH
1167
1168
1169         //------------------//
1170         // _bind_flag
1171         flag = oldctx->_bind_flag | newctx->_bind_flag;
1172         if (flag)
1173         {
1174                 STATE_COMPARE(gl_array_buffer_binding[0])
1175                 {
1176                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_array_buffer_binding[0]))
1177                 }
1178                 STATE_COMPARE(gl_element_array_buffer_binding[0])
1179                 {
1180                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newctx->gl_element_array_buffer_binding[0]))
1181                 }
1182                 STATE_COMPARE(gl_framebuffer_binding[0])
1183                 {
1184                         CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_FRAMEBUFFER, newctx->gl_framebuffer_binding[0]))
1185                 }
1186                 STATE_COMPARE(gl_renderbuffer_binding[0])
1187                 {
1188                         CHECK_GL_ERROR(_orig_fastpath_glBindRenderbuffer(GL_RENDERBUFFER, newctx->gl_renderbuffer_binding[0]))
1189                 }
1190         }
1191
1192 #ifdef COREGL_USE_MODULE_TRACEPATH
1193         tracepath_api_trace_end("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1194 #endif // COREGL_USE_MODULE_TRACEPATH
1195
1196
1197         //------------------//
1198         // Enable States
1199         // _enable_flag1
1200 #ifdef COREGL_USE_MODULE_TRACEPATH
1201         static void *trace_hint_enable_states = NULL;
1202         trace_hint_enable_states = tracepath_api_trace_begin("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1203 #endif // COREGL_USE_MODULE_TRACEPATH
1204
1205         flag = oldctx->_enable_flag1 | newctx->_enable_flag1;
1206         if (flag)
1207         {
1208                 STATE_COMPARE(gl_blend[0])
1209                 {
1210                         if (newctx->gl_blend[0])
1211                         {
1212                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_BLEND))
1213                         }
1214                         else
1215                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_BLEND))
1216                         }
1217                 STATE_COMPARE(gl_cull_face[0])
1218                 {
1219                         if (newctx->gl_cull_face[0])
1220                         {
1221                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_CULL_FACE))
1222                         }
1223                         else
1224                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_CULL_FACE))
1225                         }
1226                 STATE_COMPARE(gl_depth_test[0])
1227                 {
1228                         if (newctx->gl_depth_test[0])
1229                         {
1230                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DEPTH_TEST))
1231                         }
1232                         else
1233                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DEPTH_TEST))
1234                         }
1235                 STATE_COMPARE(gl_dither[0])
1236                 {
1237                         if (newctx->gl_dither[0])
1238                         {
1239                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DITHER))
1240                         }
1241                         else
1242                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DITHER))
1243                         }
1244         }
1245
1246         // _enable_flag2
1247         flag = oldctx->_enable_flag2 | newctx->_enable_flag2;
1248         if (flag)
1249         {
1250                 STATE_COMPARE(gl_polygon_offset_fill[0])
1251                 {
1252                         if (newctx->gl_polygon_offset_fill[0])
1253                         {
1254                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_POLYGON_OFFSET_FILL))
1255                         }
1256                         else
1257                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_POLYGON_OFFSET_FILL))
1258                         }
1259                 STATE_COMPARE(gl_sample_alpha_to_coverage[0])
1260                 {
1261                         if (newctx->gl_sample_alpha_to_coverage[0])
1262                         {
1263                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1264                         }
1265                         else
1266                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1267                         }
1268                 STATE_COMPARE(gl_sample_coverage[0])
1269                 {
1270                         if (newctx->gl_sample_coverage[0])
1271                         {
1272                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_COVERAGE))
1273                         }
1274                         else
1275                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_COVERAGE))
1276                         }
1277                 STATE_COMPARE(gl_scissor_test[0])
1278                 {
1279                         if (newctx->gl_scissor_test[0])
1280                         {
1281                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SCISSOR_TEST))
1282                         }
1283                         else
1284                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SCISSOR_TEST))
1285                         }
1286                 STATE_COMPARE(gl_stencil_test[0])
1287                 {
1288                         if (newctx->gl_stencil_test[0])
1289                         {
1290                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_STENCIL_TEST))
1291                         }
1292                         else
1293                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_STENCIL_TEST))
1294                         }
1295         }
1296
1297 #ifdef COREGL_USE_MODULE_TRACEPATH
1298         tracepath_api_trace_end("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1299 #endif // COREGL_USE_MODULE_TRACEPATH
1300
1301         //------------------//
1302         // _clear_flag1
1303 #ifdef COREGL_USE_MODULE_TRACEPATH
1304         static void *trace_hint_clear_viewport = NULL;
1305         trace_hint_clear_viewport = tracepath_api_trace_begin("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1306 #endif // COREGL_USE_MODULE_TRACEPATH
1307
1308         flag = oldctx->_clear_flag1 | newctx->_clear_flag1;
1309         if (flag)
1310         {
1311                 // Viewport.
1312                 STATES_COMPARE(gl_viewport, 4 * sizeof(GLint))
1313                 {
1314                         CHECK_GL_ERROR(_orig_fastpath_glViewport(newctx->gl_viewport[0],
1315                                        newctx->gl_viewport[1],
1316                                        newctx->gl_viewport[2],
1317                                        newctx->gl_viewport[3]))
1318                 }
1319
1320                 STATE_COMPARE(gl_current_program[0])
1321                 {
1322                         CHECK_GL_ERROR(_orig_fastpath_glUseProgram(newctx->gl_current_program[0]))
1323                 }
1324                 STATES_COMPARE(gl_color_clear_value, 4 * sizeof(GLclampf))
1325                 {
1326                         CHECK_GL_ERROR(_orig_fastpath_glClearColor(newctx->gl_color_clear_value[0],
1327                                        newctx->gl_color_clear_value[1],
1328                                        newctx->gl_color_clear_value[2],
1329                                        newctx->gl_color_clear_value[3]))
1330                 }
1331         }
1332
1333
1334         // _clear_flag2
1335         flag = oldctx->_clear_flag2 | newctx->_clear_flag2;
1336         if (flag)
1337         {
1338                 STATES_COMPARE(gl_color_writemask, 4 * sizeof(GLboolean))
1339                 {
1340                         CHECK_GL_ERROR(_orig_fastpath_glColorMask(newctx->gl_color_writemask[0],
1341                                        newctx->gl_color_writemask[1],
1342                                        newctx->gl_color_writemask[2],
1343                                        newctx->gl_color_writemask[3]))
1344                 }
1345                 STATES_COMPARE(gl_depth_range, 2 * sizeof(GLclampf))
1346                 {
1347                         CHECK_GL_ERROR(_orig_fastpath_glDepthRangef(newctx->gl_depth_range[0],
1348                                        newctx->gl_depth_range[1]))
1349                 }
1350                 STATE_COMPARE(gl_depth_clear_value[0])
1351                 {
1352                         CHECK_GL_ERROR(_orig_fastpath_glClearDepthf(newctx->gl_depth_clear_value[0]))
1353                 }
1354                 STATE_COMPARE(gl_depth_func[0])
1355                 {
1356                         CHECK_GL_ERROR(_orig_fastpath_glDepthFunc(newctx->gl_depth_func[0]))
1357                 }
1358                 STATE_COMPARE(gl_depth_writemask[0])
1359                 {
1360                         CHECK_GL_ERROR(_orig_fastpath_glDepthMask(newctx->gl_depth_writemask[0]))
1361                 }
1362                 STATE_COMPARE(gl_cull_face_mode[0])
1363                 {
1364                         CHECK_GL_ERROR(_orig_fastpath_glCullFace(newctx->gl_cull_face_mode[0]))
1365                 }
1366
1367         }
1368
1369 #ifdef COREGL_USE_MODULE_TRACEPATH
1370         tracepath_api_trace_end("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1371 #endif // COREGL_USE_MODULE_TRACEPATH
1372
1373         //------------------//
1374         // Texture here...
1375 #ifdef COREGL_USE_MODULE_TRACEPATH
1376         static void *trace_hint_bind_textures = NULL;
1377         trace_hint_bind_textures = tracepath_api_trace_begin("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
1378 #endif // COREGL_USE_MODULE_TRACEPATH
1379
1380         flag = oldctx->_tex_flag1 | newctx->_tex_flag1;
1381         if (flag)
1382         {
1383
1384                 for (i = 0; i < oldctx->gl_num_tex_units[0]; i++)
1385                 {
1386                         STATE_COMPARE(gl_tex_2d_state[i])
1387                         {
1388                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1389                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D, newctx->gl_tex_2d_state[i]))
1390                         }
1391
1392                         STATE_COMPARE(gl_tex_cube_state[i])
1393                         {
1394                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1395                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_CUBE_MAP, newctx->gl_tex_cube_state[i]))
1396                         }
1397                 }
1398
1399                 // Restore active texture
1400                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(newctx->gl_active_texture[0]))
1401
1402                 STATE_COMPARE(gl_generate_mipmap_hint[0])
1403                 {
1404                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_GENERATE_MIPMAP_HINT, newctx->gl_generate_mipmap_hint[0]))
1405                 }
1406         }
1407 #ifdef COREGL_USE_MODULE_TRACEPATH
1408         tracepath_api_trace_end("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
1409 #endif // COREGL_USE_MODULE_TRACEPATH
1410
1411         //------------------//
1412 #ifdef COREGL_USE_MODULE_TRACEPATH
1413         static void *trace_hint_etc = NULL;
1414         trace_hint_etc = tracepath_api_trace_begin("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
1415 #endif // COREGL_USE_MODULE_TRACEPATH
1416
1417         flag = oldctx->_blend_flag | newctx->_blend_flag;
1418         if (flag)
1419         {
1420                 STATES_COMPARE(gl_blend_color, 4 * sizeof(GLclampf))
1421                 {
1422                         CHECK_GL_ERROR(_orig_fastpath_glBlendColor(newctx->gl_blend_color[0],
1423                                        newctx->gl_blend_color[1],
1424                                        newctx->gl_blend_color[2],
1425                                        newctx->gl_blend_color[3]))
1426                 }
1427                 if ((oldctx->gl_blend_src_rgb[0] != newctx->gl_blend_src_rgb[0]) ||
1428                     (oldctx->gl_blend_dst_rgb[0] != newctx->gl_blend_dst_rgb[0]) ||
1429                     (oldctx->gl_blend_src_alpha[0] != newctx->gl_blend_src_alpha[0]) ||
1430                     (oldctx->gl_blend_dst_alpha[0] != newctx->gl_blend_dst_alpha[0]))
1431                 {
1432                         CHECK_GL_ERROR(_orig_fastpath_glBlendFuncSeparate(newctx->gl_blend_src_rgb[0],
1433                                        newctx->gl_blend_dst_rgb[0],
1434                                        newctx->gl_blend_src_alpha[0],
1435                                        newctx->gl_blend_dst_alpha[0]))
1436                 }
1437                 if ((oldctx->gl_blend_equation_rgb[0] != newctx->gl_blend_equation_rgb[0]) ||
1438                     (oldctx->gl_blend_equation_alpha[0] != newctx->gl_blend_equation_alpha[0]))
1439                 {
1440                         CHECK_GL_ERROR(_orig_fastpath_glBlendEquationSeparate(newctx->gl_blend_equation_rgb[0], newctx->gl_blend_equation_alpha[0]))
1441                 }
1442
1443         }
1444
1445         //------------------//
1446         // _stencil_flag1
1447         flag = oldctx->_stencil_flag1 | newctx->_stencil_flag1;
1448         if (flag)
1449         {
1450                 if ((oldctx->gl_stencil_func[0] != newctx->gl_stencil_func[0]) ||
1451                     (oldctx->gl_stencil_ref[0]  != newctx->gl_stencil_ref[0])  ||
1452                     (oldctx->gl_stencil_value_mask[0] != newctx->gl_stencil_value_mask[0]))
1453                 {
1454                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_FRONT,
1455                                        newctx->gl_stencil_func[0],
1456                                        newctx->gl_stencil_ref[0],
1457                                        newctx->gl_stencil_value_mask[0]))
1458                 }
1459                 if ((oldctx->gl_stencil_fail[0] != newctx->gl_stencil_fail[0]) ||
1460                     (oldctx->gl_stencil_pass_depth_fail[0] != newctx->gl_stencil_pass_depth_fail[0]) ||
1461                     (oldctx->gl_stencil_pass_depth_pass[0] != newctx->gl_stencil_pass_depth_pass[0]))
1462                 {
1463                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_FRONT,
1464                                        newctx->gl_stencil_fail[0],
1465                                        newctx->gl_stencil_pass_depth_fail[0],
1466                                        newctx->gl_stencil_pass_depth_pass[0]))
1467                 }
1468
1469                 STATE_COMPARE(gl_stencil_writemask[0])
1470                 {
1471                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_FRONT, newctx->gl_stencil_writemask[0]))
1472                 }
1473         }
1474
1475
1476         // _stencil_flag1
1477         flag = oldctx->_stencil_flag2 | newctx->_stencil_flag2;
1478         if (flag)
1479         {
1480                 if ((oldctx->gl_stencil_back_func[0] != newctx->gl_stencil_back_func[0]) ||
1481                     (oldctx->gl_stencil_back_ref[0]  != newctx->gl_stencil_back_ref[0])  ||
1482                     (oldctx->gl_stencil_back_value_mask[0] != newctx->gl_stencil_back_value_mask[0]))
1483                 {
1484                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_BACK,
1485                                        newctx->gl_stencil_back_func[0],
1486                                        newctx->gl_stencil_back_ref[0],
1487                                        newctx->gl_stencil_back_value_mask[0]))
1488                 }
1489                 if ((oldctx->gl_stencil_back_fail[0] != newctx->gl_stencil_back_fail[0]) ||
1490                     (oldctx->gl_stencil_back_pass_depth_fail[0] != newctx->gl_stencil_back_pass_depth_fail[0]) ||
1491                     (oldctx->gl_stencil_back_pass_depth_pass[0] != newctx->gl_stencil_back_pass_depth_pass[0]))
1492                 {
1493                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_BACK,
1494                                        newctx->gl_stencil_back_fail[0],
1495                                        newctx->gl_stencil_back_pass_depth_fail[0],
1496                                        newctx->gl_stencil_back_pass_depth_pass[0]))
1497                 }
1498
1499                 STATE_COMPARE(gl_stencil_back_writemask[0])
1500                 {
1501                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_BACK, newctx->gl_stencil_back_writemask[0]))
1502                 }
1503                 STATE_COMPARE(gl_stencil_clear_value[0])
1504                 {
1505                         CHECK_GL_ERROR(_orig_fastpath_glClearStencil(newctx->gl_stencil_clear_value[0]))
1506                 }
1507         }
1508
1509         //------------------//
1510         // _misc_flag1
1511         flag = oldctx->_misc_flag1 | newctx->_misc_flag1;
1512         if (flag)
1513         {
1514                 STATE_COMPARE(gl_front_face[0])
1515                 {
1516                         CHECK_GL_ERROR(_orig_fastpath_glFrontFace(newctx->gl_front_face[0]))
1517                 }
1518                 STATE_COMPARE(gl_line_width[0])
1519                 {
1520                         CHECK_GL_ERROR(_orig_fastpath_glLineWidth(newctx->gl_line_width[0]))
1521                 }
1522                 if ((oldctx->gl_polygon_offset_factor[0] != newctx->gl_polygon_offset_factor[0]) ||
1523                     (oldctx->gl_polygon_offset_units[0]  != newctx->gl_polygon_offset_units[0]))
1524                 {
1525                         CHECK_GL_ERROR(_orig_fastpath_glPolygonOffset(newctx->gl_polygon_offset_factor[0],
1526                                        newctx->gl_polygon_offset_units[0]))
1527                 }
1528                 if ((oldctx->gl_sample_coverage_value[0]  != newctx->gl_sample_coverage_value[0]) ||
1529                     (oldctx->gl_sample_coverage_invert[0] != newctx->gl_sample_coverage_invert[0]))
1530                 {
1531                         CHECK_GL_ERROR(_orig_fastpath_glSampleCoverage(newctx->gl_sample_coverage_value[0],
1532                                        newctx->gl_sample_coverage_invert[0]))
1533                 }
1534                 STATE_COMPARE(gl_fragment_shader_derivative_hint[0])
1535                 {
1536                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES, newctx->gl_fragment_shader_derivative_hint[0]))
1537                 }
1538         }
1539
1540         // _misc_flag2
1541         flag = oldctx->_misc_flag2 | newctx->_misc_flag2;
1542         if (flag)
1543         {
1544                 STATES_COMPARE(gl_scissor_box, 4 * sizeof(GLint))
1545                 {
1546                         CHECK_GL_ERROR(_orig_fastpath_glScissor(newctx->gl_scissor_box[0],
1547                                                                 newctx->gl_scissor_box[1],
1548                                                                 newctx->gl_scissor_box[2],
1549                                                                 newctx->gl_scissor_box[3]))
1550                 }
1551                 STATE_COMPARE(gl_pack_alignment[0])
1552                 {
1553                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ALIGNMENT, newctx->gl_pack_alignment[0]))
1554                 }
1555                 STATE_COMPARE(gl_unpack_alignment[0])
1556                 {
1557                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ALIGNMENT, newctx->gl_unpack_alignment[0]))
1558                 }
1559         }
1560 #ifdef COREGL_USE_MODULE_TRACEPATH
1561         tracepath_api_trace_end("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
1562 #endif // COREGL_USE_MODULE_TRACEPATH
1563
1564         // _varray_flag
1565 #ifdef COREGL_USE_MODULE_TRACEPATH
1566         static void *trace_hint_vertex_attrib = NULL;
1567         trace_hint_vertex_attrib = tracepath_api_trace_begin("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1568 #endif // COREGL_USE_MODULE_TRACEPATH
1569
1570         flag = oldctx->_vattrib_flag | newctx->_vattrib_flag;
1571         if (flag)
1572         {
1573                 for (i = 0; i < oldctx->gl_num_vertex_attribs[0]; i++)
1574                 {
1575                         if (newctx->gl_vertex_array_buf_id[i] != oldctx->gl_vertex_array_buf_id[i])
1576                         {
1577                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_vertex_array_buf_id[i]))
1578                         }
1579                         else
1580                         {
1581                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, 0))
1582                         }
1583
1584                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribPointer(i,
1585                                        newctx->gl_vertex_array_size[i],
1586                                        newctx->gl_vertex_array_type[i],
1587                                        newctx->gl_vertex_array_normalized[i],
1588                                        newctx->gl_vertex_array_stride[i],
1589                                        newctx->gl_vertex_array_pointer[i]))
1590
1591                         STATES_COMPARE(gl_vertex_attrib_value + 4 * i, 4 * sizeof(GLfloat))
1592                         {
1593                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttrib4fv(i, &newctx->gl_vertex_attrib_value[4 * i]))
1594                         }
1595
1596                         if (newctx->gl_vertex_array_enabled[i] == GL_TRUE)
1597                         {
1598                                 CHECK_GL_ERROR(_orig_fastpath_glEnableVertexAttribArray(i))
1599                         }
1600                         else
1601                         {
1602                                 CHECK_GL_ERROR(_orig_fastpath_glDisableVertexAttribArray(i))
1603                         }
1604                 }
1605
1606                 STATE_COMPARE(gl_array_buffer_binding[0])
1607                 {
1608                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_array_buffer_binding[0]))
1609                 }
1610                 STATE_COMPARE(gl_element_array_buffer_binding[0])
1611                 {
1612                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newctx->gl_element_array_buffer_binding[0]))
1613                 }
1614
1615         }
1616
1617 #ifdef COREGL_USE_MODULE_TRACEPATH
1618         tracepath_api_trace_end("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1619 #endif // COREGL_USE_MODULE_TRACEPATH
1620
1621         ret = 1;
1622         goto finish;
1623
1624 finish:
1625
1626 #ifdef COREGL_FASTPATH_TRACE_STATE_INFO
1627         if (unlikely(trace_state_flag == 1))
1628                 fastpath_dump_context_states(newctx, 0);
1629 #endif // COREGL_FASTPATH_TRACE_STATE_INFO
1630         return ret;
1631 #undef STATE_COMPARE
1632 #undef STATES_COMPARE
1633 }
1634