[Title] Added supports for Qualcomm chipsets
[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                         free(current);
475                         ret = 1;
476                         current = nextitm;
477                         continue;
478                 }
479                 olditm = current;
480                 current = current->next;
481         }
482         goto finish;
483
484 finish:
485         if (mtx != NULL) AST(mutex_unlock(mtx) == 1);
486         return ret;
487 }
488
489 void
490 fastpath_sostate_init(GL_Shared_Object_State *sostate)
491 {
492 #define HASH_INIT(hash_base) \
493         hash_base.hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * GL_OBJECT_HASH_BASE); \
494         hash_base.hash_size = GL_OBJECT_HASH_BASE;
495
496         HASH_INIT(sostate->texture);
497         HASH_INIT(sostate->buffer);
498         HASH_INIT(sostate->framebuffer);
499         HASH_INIT(sostate->renderbuffer);
500         HASH_INIT(sostate->program);
501
502         HASH_INIT(sostate->texture_real);
503         HASH_INIT(sostate->buffer_real);
504         HASH_INIT(sostate->framebuffer_real);
505         HASH_INIT(sostate->renderbuffer_real);
506         HASH_INIT(sostate->program_real);
507
508 #undef HASH_INIT
509 }
510
511 static void
512 _add_hash(GL_Object_Hash_Base *hash_base, GL_Object_Hash *data)
513 {
514         int array_idx = data->hash_key & (hash_base->hash_size - 1);
515         if (hash_base->hash_field[array_idx] == NULL)
516         {
517                 hash_base->hash_field[array_idx] = data;
518         }
519         else
520         {
521                 GL_Object_Hash *current = hash_base->hash_field[array_idx];
522                 while(current->next)
523                 {
524                         AST(current->hash_key != data->hash_key);
525                         current = current->next;
526                 }
527                 current->next = data;
528         }
529         data->next = NULL;
530         hash_base->item_size++;
531 }
532
533 static int
534 _remove_hash(GL_Object_Hash_Base *hash_base, GLuint hash)
535 {
536         int ret = 0;
537         int array_idx = hash & (hash_base->hash_size - 1);
538
539         GL_Object_Hash *current = hash_base->hash_field[array_idx];
540         GL_Object_Hash *prev = NULL;
541
542         while(current)
543         {
544                 if (current->hash_key == hash)
545                 {
546                         if (prev != NULL)
547                                 prev->next = current->next;
548                         else
549                                 hash_base->hash_field[array_idx] = current->next;
550                         hash_base->item_size--;
551                         ret = 1;
552                         break;
553                 }
554                 prev = current;
555                 current = current->next;
556         }
557
558         return ret;
559 }
560
561 static void
562 _free_hash_list(GL_Object_Hash_Base *hash_base, int free_data)
563 {
564         if (hash_base->item_size == 0) return;
565
566         for (int i = 0; i < hash_base->hash_size; i++)
567         {
568                 if (hash_base->hash_field[i] != NULL)
569                 {
570                         GL_Object_Hash *current = hash_base->hash_field[i];
571
572                         while (current != NULL)
573                         {
574                                 GL_Object_Hash *current_next = current->next;
575
576                                 if (free_data == 1 && current->item != NULL)
577                                 {
578                                         free(current->item);
579                                 }
580
581                                 free(current);
582                                 hash_base->item_size--;
583                                 current = current_next;
584                         }
585                 }
586         }
587 }
588
589 void
590 fastpath_sostate_deinit(GL_Shared_Object_State *sostate)
591 {
592 #define HASH_DEINIT(hash_base, free_data) \
593         _free_hash_list(&hash_base, free_data); \
594         free(hash_base.hash_field); \
595         hash_base.hash_size = 0;
596
597         HASH_DEINIT(sostate->texture, 1);
598         HASH_DEINIT(sostate->buffer, 1);
599         HASH_DEINIT(sostate->framebuffer, 1);
600         HASH_DEINIT(sostate->renderbuffer, 1);
601         HASH_DEINIT(sostate->program, 1);
602
603         HASH_DEINIT(sostate->texture_real, 0);
604         HASH_DEINIT(sostate->buffer_real, 0);
605         HASH_DEINIT(sostate->framebuffer_real, 0);
606         HASH_DEINIT(sostate->renderbuffer_real, 0);
607         HASH_DEINIT(sostate->program_real, 0);
608
609 #undef HASH_DEINIT
610 }
611
612 #define FIND_HASH(hash_base, key, ret) \
613 { \
614         GL_Object_Hash *fh_current = hash_base->hash_field[(key) & (hash_base->hash_size - 1)]; \
615         while(fh_current) \
616         { \
617                 if (fh_current->hash_key == (key)) \
618                 { \
619                         ret = fh_current; \
620                         break; \
621                 } \
622                 fh_current = fh_current->next; \
623         } \
624 }
625
626 void
627 _sostate_hash_check(GL_Object_Hash_Base *hash_base)
628 {
629         if (hash_base->item_size + 1 < hash_base->hash_size)
630                 return;
631
632         int oldsize = hash_base->hash_size;
633         GL_Object_Hash **oldfield = hash_base->hash_field;
634
635         hash_base->hash_size = oldsize << 1;
636         hash_base->hash_field = (GL_Object_Hash **)calloc(1, sizeof(GL_Object_Hash *) * hash_base->hash_size);
637         AST(hash_base->hash_field != NULL);
638
639         for (int i = 0; i < oldsize; i++)
640         {
641                 if (oldfield[i] != NULL)
642                 {
643                         GL_Object_Hash *current = oldfield[i];
644
645                         while (current != NULL)
646                         {
647                                 GL_Object_Hash *current_next = current->next;
648                                 _add_hash(hash_base, current);
649                                 hash_base->item_size--;
650                                 current = current_next;
651                         }
652                 }
653         }
654         free(oldfield);
655
656 }
657
658 GLuint
659 fastpath_sostate_create_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint real_name)
660 {
661         GLuint ret = _COREGL_INT_INIT_VALUE;
662
663         GL_Object_Hash_Base *hash_base = NULL;
664         GL_Object_Hash_Base *hash_base_real = NULL;
665         int newid = _COREGL_INT_INIT_VALUE;
666
667         hash_base = _get_shared_object_hash(sostate, type);
668         hash_base_real = _get_shared_object_hash_real(sostate, type);
669
670         newid = hash_base->last_id + 1;
671         if (newid >= hash_base->hash_size)
672         {
673                 hash_base->is_looped = 1;
674                 newid = 1;
675                 hash_base->last_id = 1;
676         }
677
678         if (hash_base->is_looped != 0)
679         {
680                 int i;
681                 int findingid = newid;
682                 newid = -1;
683                 for (i = 0; i < hash_base->hash_size; i++)
684                 {
685                         GL_Object_Hash *exist_hash = NULL;
686                         FIND_HASH(hash_base, findingid, exist_hash);
687                         if (exist_hash == NULL)
688                         {
689                                 newid = findingid;
690                                 break;
691                         }
692                         findingid++;
693                         if (findingid >= hash_base->hash_size) findingid = 1;
694                 }
695                 AST(newid != -1);
696         }
697         hash_base->last_id = newid;
698
699         {
700                 GL_Object *newobj = (GL_Object *)calloc(1, sizeof(GL_Object));
701                 AST(newobj != NULL);
702                 newobj->id = (int)type + newid;
703                 newobj->real_id = real_name;
704                 newobj->ref_count = 1;
705                 ret = newobj->id;
706
707                 GL_Object_Hash *newobj_hash = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
708                 AST(newobj_hash != NULL);
709                 newobj_hash->item = newobj;
710                 newobj_hash->hash_key = newid;
711                 _add_hash(hash_base, newobj_hash);
712
713                 GL_Object_Hash *newobj_hash_real = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
714                 AST(newobj_hash_real != NULL);
715                 newobj_hash_real->item = newobj;
716                 newobj_hash_real->hash_key = real_name;
717                 _add_hash(hash_base_real, newobj_hash_real);
718         }
719
720         _sostate_hash_check(hash_base);
721         _sostate_hash_check(hash_base_real);
722
723         goto finish;
724
725 finish:
726         return ret;
727 }
728
729 #define FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object) \
730         if ((hash) < 0) { ret = 0; goto finish; } \
731         { \
732                 GL_Object_Hash *object_hash = NULL; \
733                 FIND_HASH((hash_base), (hash), object_hash); \
734                 if (object_hash == NULL) { ret = 0; goto finish; } \
735                 (object) = object_hash->item; \
736                 if ((object) == NULL) { ret = 0; goto finish; } \
737         }
738
739 GLuint
740 fastpath_sostate_remove_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
741 {
742         GLuint ret = _COREGL_INT_INIT_VALUE;
743
744         GL_Object_Hash_Base *hash_base = NULL;
745         GL_Object_Hash_Base *hash_base_real = NULL;
746         GL_Object *object = NULL;
747
748         hash_base = _get_shared_object_hash(sostate, type);
749         hash_base_real = _get_shared_object_hash_real(sostate, type);
750
751         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
752
753         object->ref_count--;
754
755         if (object->ref_count <= 0)
756         {
757                 GL_Object_Hash *object_hash = NULL;
758
759                 FIND_HASH(hash_base, object->id - (int)type, object_hash);
760                 AST(object_hash != NULL);
761                 _remove_hash(hash_base, object->id - (int)type);
762                 free(object_hash);
763                 object_hash = NULL;
764
765                 FIND_HASH(hash_base_real, object->real_id, object_hash);
766                 AST(object_hash != NULL);
767                 _remove_hash(hash_base_real, object->real_id);
768                 free(object_hash);
769                 object_hash = NULL;
770
771                 free(object);
772                 object = NULL;
773         }
774
775         ret = 1;
776         goto finish;
777
778 finish:
779         return ret;
780 }
781
782 GLuint
783 fastpath_sostate_get_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
784 {
785         GLuint ret = _COREGL_INT_INIT_VALUE;
786
787         GL_Object_Hash_Base *hash_base = NULL;
788         GL_Object *object = NULL;
789
790         hash_base = _get_shared_object_hash(sostate, type);
791
792         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
793
794         ret = object->real_id;
795         goto finish;
796
797 finish:
798         return ret;
799 }
800
801 GLint
802 fastpath_sostate_set_object_tag(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name, GLvoid *tag)
803 {
804         GLint ret = _COREGL_INT_INIT_VALUE;
805
806         GL_Object_Hash_Base *hash_base = NULL;
807         GL_Object *object = NULL;
808         int hash = _COREGL_INT_INIT_VALUE;
809
810         hash_base = _get_shared_object_hash(sostate, type);
811
812         hash = glue_name - (int)type;
813
814         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, hash, object);
815
816         object->tag = tag;
817         ret = 1;
818         goto finish;
819
820 finish:
821         return ret;
822 }
823
824 GLvoid *
825 fastpath_sostate_get_object_tag(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
826 {
827         GLvoid *ret = NULL;
828
829         GL_Object_Hash_Base *hash_base = NULL;
830         GL_Object *object = NULL;
831
832         hash_base = _get_shared_object_hash(sostate, type);
833
834         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
835
836         ret = object->tag;
837         goto finish;
838
839 finish:
840         return ret;
841 }
842
843 GLuint
844 fastpath_sostate_find_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint real_name)
845 {
846         GLuint ret = _COREGL_INT_INIT_VALUE;
847
848         GL_Object_Hash_Base *hash_base_real = NULL;
849         GL_Object *object = NULL;
850
851         hash_base_real = _get_shared_object_hash_real(sostate, type);
852
853         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base_real, real_name, object);
854
855         ret = object->id;
856         goto finish;
857
858 finish:
859         return ret;
860 }
861
862 GLint
863 fastpath_sostate_use_object(GL_Shared_Object_State *sostate, GL_Object_Type type, GLuint glue_name)
864 {
865         GLint ret = _COREGL_INT_INIT_VALUE;
866
867         GL_Object_Hash_Base *hash_base = NULL;
868         GL_Object *object = NULL;
869
870         hash_base = _get_shared_object_hash(sostate, type);
871
872         FIND_OBJ_FROM_HASH_WITH_VERIFY(hash_base, glue_name - (int)type, object);
873
874         object->ref_count++;
875         ret = 1;
876         goto finish;
877
878 finish:
879         return ret;
880 }
881
882 void
883 fastpath_dump_context_states(GLGlueContext *ctx, int force_output)
884 {
885         static struct timeval tv_last = { 0, 0 };
886
887         if (unlikely(trace_state_flag != 1)) return;
888
889         if (!force_output)
890         {
891                 struct timeval tv_now = { 0, 0 };
892                 AST(gettimeofday(&tv_now, NULL) == 0);
893                 if (tv_now.tv_sec - tv_last.tv_sec < _COREGL_TRACE_OUTPUT_INTERVAL_SEC)
894                 {
895                         goto finish;
896                 }
897                 tv_last = tv_now;
898         }
899
900         TRACE("\n");
901         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
902         TRACE("\E[40;32;1m  State info \E[1;37;1m: <PID = %d> GlueCTX = %p\E[0m\n", getpid(), ctx);
903         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
904
905 #define PRINTF_CHAR_GLenum "%10d"
906 #define PRINTF_CHAR_GLboolean "%10d"
907 #define PRINTF_CHAR_GLint "%10d"
908 #define PRINTF_CHAR_GLsizei "%10u"
909 #define PRINTF_CHAR_GLuint "%10u"
910 #define PRINTF_CHAR_GLuintmask "0x%8X"
911
912 #define PRINTF_CHAR_GLclampf "%10.6f"
913 #define PRINTF_CHAR_GLfloat "%10.6f"
914
915 #define PRINTF_CHAR_GLvoidptr "%10p"
916
917 #define PRINTF_CHAR(type) PRINTF_CHAR_##type
918
919 #define INITIAL_CTX initial_ctx
920 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
921    { \
922       TYPE valuedata[SIZE]; \
923       TYPE *value = NULL; \
924       value = valuedata; GET_STMT; value = valuedata; \
925       TRACE("\E[40;37;1m %-30.30s : (\E[0m ", #NAME); \
926       for (int i = 0; i < SIZE; i++) \
927       { \
928          if (i > 0) { \
929             if (i % 4 == 0) \
930                TRACE("\n %-30.30s     ", "");\
931             else \
932                TRACE(", "); \
933          } \
934          TRACE(PRINTF_CHAR(TYPE), ctx->NAME[i]); \
935          TRACE("["PRINTF_CHAR(TYPE)"]", value[i]); \
936       } \
937       TRACE(" \E[40;37;1m)\E[0m\n"); \
938    }
939 # include "coregl_fastpath_state.h"
940 #undef GLUE_STATE
941 #undef INITIAL_CTX
942
943         TRACE("\E[0;40;34m========================================================================================================================\E[0m\n");
944         TRACE("\n");
945
946         TRACE_END();
947
948 finish:
949         return;
950 }
951
952 int
953 fastpath_init_context_states(GLGlueContext *ctx)
954 {
955         int ret = 0;
956
957         AST(mutex_lock(&init_context_mutex) == 1);
958
959         if (ctx == NULL)
960         {
961                 COREGL_ERR("Context NULL\n");
962                 ret = 0;
963                 goto finish;
964         }
965
966         AST(ctx->initialized == 0);
967         AST(ctx->sostate != NULL);
968
969         if (initial_ctx == NULL)
970         {
971                 initial_ctx = (GLGlueContext *)calloc(1, sizeof(GLGlueContext));
972                 AST(initial_ctx != NULL);
973
974 //#define FORCE_DEFAULT_VALUE
975 #ifdef FORCE_DEFAULT_VALUE
976 # define INITIAL_CTX initial_ctx
977 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
978       { \
979          int i; \
980          TYPE valuedata[SIZE]; \
981          TYPE *value = NULL; \
982          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
983          value = valuedata; DEFAULT_STMT; value = valuedata; \
984          for (i = 0; i < SIZE; i++) \
985          { \
986             if (*((char *)(&value[i])) == 0xcc) \
987             { \
988                memset(&value[i], 0xaa, sizeof(TYPE)); \
989                value = valuedata; DEFAULT_STMT; value = valuedata; \
990                if (*((char *)(&value[i])) == 0xaa) \
991                { \
992                   COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
993                   break; \
994                } \
995             } \
996             initial_ctx->NAME[i] = value[i]; \
997          } \
998       }
999 #  include "coregl_fastpath_state.h"
1000 # undef GLUE_STATE
1001 # undef INITIAL_CTX
1002 #else
1003 # define INITIAL_CTX initial_ctx
1004 # define SET_GLUE_VALUE(DEFAULT_STMT, FALLBACK_STMT) \
1005       if (try_step == 1) \
1006       { \
1007          value = valuedata; DEFAULT_STMT; value = valuedata; \
1008       } \
1009       else \
1010       { \
1011          value = valuedata; FALLBACK_STMT; value = valuedata; \
1012       }
1013
1014 # define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1015       { \
1016          int i; \
1017          int try_step = 0;\
1018          TYPE valuedata[SIZE]; \
1019          TYPE *value = NULL; \
1020          memset(valuedata, 0xcc, sizeof(TYPE) * SIZE); \
1021          do { \
1022             try_step++; \
1023             SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1024             for (i = 0; i < SIZE; i++) \
1025             { \
1026                if (*((char *)(&value[i])) == 0xcc) \
1027                { \
1028                   memset(&value[i], 0xaa, sizeof(TYPE)); \
1029                   SET_GLUE_VALUE(GET_STMT, DEFAULT_STMT); \
1030                   if (*((char *)(&value[i])) == 0xaa) \
1031                   { \
1032                      try_step++; \
1033                      if (try_step == 2) \
1034                      { \
1035                         COREGL_WRN("\E[40;31;1mGL-state '"#NAME"' cannot be retrieved\E[0m\n"); \
1036                      } \
1037                      break; \
1038                   } \
1039                } \
1040                initial_ctx->NAME[i] = value[i]; \
1041             } \
1042             if (try_step != 2) \
1043             { \
1044                value = valuedata; DEFAULT_STMT; value = valuedata; \
1045                for (i = 0; i < SIZE; i++) \
1046                { \
1047                   if (initial_ctx->NAME[i] != value[i]) \
1048                   { \
1049                      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]); \
1050                   } \
1051                } \
1052             } \
1053          } \
1054          while (try_step == 2); \
1055       }
1056 #  include "coregl_fastpath_state.h"
1057 # undef SET_GLUE_VALUE
1058 # undef GLUE_STATE
1059 # undef INITIAL_CTX
1060 #endif
1061
1062                 if (initial_ctx->gl_num_vertex_attribs[0] > MAX_VERTEX_ATTRIBS)
1063                 {
1064                         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]);
1065                 }
1066                 if (initial_ctx->gl_num_tex_units[0] > MAX_TEXTURE_UNITS)
1067                 {
1068                         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]);
1069                 }
1070         }
1071
1072         {
1073                 int i;
1074 #define INITIAL_CTX initial_ctx
1075 #define GLUE_STATE(TYPE, NAME, SIZE, ARRAY_SIZE, DEFAULT_STMT, GET_STMT)  \
1076          for (i = 0; i < SIZE; i++) \
1077          { \
1078             ctx->NAME[i] = initial_ctx->NAME[i]; \
1079          }
1080 # include "coregl_fastpath_state.h"
1081 #undef GLUE_STATE
1082 #undef INITIAL_CTX
1083         }
1084
1085         ctx->initialized = 1;
1086         ret = 1;
1087         goto finish;
1088
1089 finish:
1090         AST(mutex_unlock(&init_context_mutex) == 1);
1091
1092         return ret;
1093 }
1094
1095 #ifdef COREGL_USE_MODULE_TRACEPATH
1096 extern void *tracepath_api_trace_begin(const char *name, void *hint, int trace_total_time);
1097 extern void *tracepath_api_trace_end(const char *name, void *hint, int trace_total_time);
1098 #endif
1099
1100 #define CHECK_GL_ERROR(func) \
1101         { \
1102                 func; \
1103                 int err = _orig_fastpath_glGetError(); \
1104                 if (err != GL_NO_ERROR) \
1105                 { \
1106                         COREGL_ERR("\E[40;31;1m(GL %p) : %s returns GL error 0x%X\E[0m\n", oldctx->cstate, #func, err); \
1107                         goto finish; \
1108                 } \
1109         }
1110
1111 int
1112 fastpath_make_context_current(GLGlueContext *oldctx, GLGlueContext *newctx)
1113 {
1114         int ret = 0;
1115         unsigned char flag = 0;
1116         int i = 0;
1117
1118         if (debug_nofp == 1)
1119         {
1120                 ret = 1;
1121                 goto finish;
1122         }
1123
1124         // Return if they're the same
1125         if (oldctx == newctx)
1126         {
1127                 ret = 1;
1128                 goto finish;
1129         }
1130
1131 #define STATE_COMPARE(state) \
1132    if ((oldctx->state) != (newctx->state))
1133
1134 #define STATES_COMPARE(state_ptr, bytes) \
1135    if ((memcmp((oldctx->state_ptr), (newctx->state_ptr), (bytes))) != 0)
1136
1137
1138 #ifdef COREGL_USE_MODULE_TRACEPATH
1139         static void *trace_hint_glfinish = NULL;
1140         trace_hint_glfinish = tracepath_api_trace_begin("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1141 #endif // COREGL_USE_MODULE_TRACEPATH
1142
1143         {
1144                 int err = _orig_fastpath_glGetError();
1145                 if (err != GL_NO_ERROR && oldctx->gl_error == GL_NO_ERROR)
1146                         oldctx->gl_error = err;
1147         }
1148
1149         CHECK_GL_ERROR(_orig_fastpath_glFlush())
1150
1151 #ifdef COREGL_USE_MODULE_TRACEPATH
1152         tracepath_api_trace_end("eglMakeCurrent(FP glFinish)", trace_hint_glfinish, 0);
1153 #endif // COREGL_USE_MODULE_TRACEPATH
1154
1155 #ifdef COREGL_USE_MODULE_TRACEPATH
1156         static void *trace_hint_bindbuffers = NULL;
1157         trace_hint_bindbuffers = tracepath_api_trace_begin("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1158 #endif // COREGL_USE_MODULE_TRACEPATH
1159
1160
1161         //------------------//
1162         // _bind_flag
1163         flag = oldctx->_bind_flag | newctx->_bind_flag;
1164         if (flag)
1165         {
1166                 STATE_COMPARE(gl_array_buffer_binding[0])
1167                 {
1168                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_array_buffer_binding[0]))
1169                 }
1170                 STATE_COMPARE(gl_element_array_buffer_binding[0])
1171                 {
1172                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newctx->gl_element_array_buffer_binding[0]))
1173                 }
1174                 STATE_COMPARE(gl_framebuffer_binding[0])
1175                 {
1176                         CHECK_GL_ERROR(_orig_fastpath_glBindFramebuffer(GL_FRAMEBUFFER, newctx->gl_framebuffer_binding[0]))
1177                 }
1178                 STATE_COMPARE(gl_renderbuffer_binding[0])
1179                 {
1180                         CHECK_GL_ERROR(_orig_fastpath_glBindRenderbuffer(GL_RENDERBUFFER, newctx->gl_renderbuffer_binding[0]))
1181                 }
1182         }
1183
1184 #ifdef COREGL_USE_MODULE_TRACEPATH
1185         tracepath_api_trace_end("eglMakeCurrent(FP bind buffers)", trace_hint_bindbuffers, 0);
1186 #endif // COREGL_USE_MODULE_TRACEPATH
1187
1188
1189         //------------------//
1190         // Enable States
1191         // _enable_flag1
1192 #ifdef COREGL_USE_MODULE_TRACEPATH
1193         static void *trace_hint_enable_states = NULL;
1194         trace_hint_enable_states = tracepath_api_trace_begin("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1195 #endif // COREGL_USE_MODULE_TRACEPATH
1196
1197         flag = oldctx->_enable_flag1 | newctx->_enable_flag1;
1198         if (flag)
1199         {
1200                 STATE_COMPARE(gl_blend[0])
1201                 {
1202                         if (newctx->gl_blend[0])
1203                         {
1204                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_BLEND))
1205                         }
1206                         else
1207                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_BLEND))
1208                         }
1209                 STATE_COMPARE(gl_cull_face[0])
1210                 {
1211                         if (newctx->gl_cull_face[0])
1212                         {
1213                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_CULL_FACE))
1214                         }
1215                         else
1216                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_CULL_FACE))
1217                         }
1218                 STATE_COMPARE(gl_depth_test[0])
1219                 {
1220                         if (newctx->gl_depth_test[0])
1221                         {
1222                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DEPTH_TEST))
1223                         }
1224                         else
1225                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DEPTH_TEST))
1226                         }
1227                 STATE_COMPARE(gl_dither[0])
1228                 {
1229                         if (newctx->gl_dither[0])
1230                         {
1231                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_DITHER))
1232                         }
1233                         else
1234                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_DITHER))
1235                         }
1236         }
1237
1238         // _enable_flag2
1239         flag = oldctx->_enable_flag2 | newctx->_enable_flag2;
1240         if (flag)
1241         {
1242                 STATE_COMPARE(gl_polygon_offset_fill[0])
1243                 {
1244                         if (newctx->gl_polygon_offset_fill[0])
1245                         {
1246                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_POLYGON_OFFSET_FILL))
1247                         }
1248                         else
1249                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_POLYGON_OFFSET_FILL))
1250                         }
1251                 STATE_COMPARE(gl_sample_alpha_to_coverage[0])
1252                 {
1253                         if (newctx->gl_sample_alpha_to_coverage[0])
1254                         {
1255                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1256                         }
1257                         else
1258                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE))
1259                         }
1260                 STATE_COMPARE(gl_sample_coverage[0])
1261                 {
1262                         if (newctx->gl_sample_coverage[0])
1263                         {
1264                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SAMPLE_COVERAGE))
1265                         }
1266                         else
1267                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SAMPLE_COVERAGE))
1268                         }
1269                 STATE_COMPARE(gl_scissor_test[0])
1270                 {
1271                         if (newctx->gl_scissor_test[0])
1272                         {
1273                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_SCISSOR_TEST))
1274                         }
1275                         else
1276                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_SCISSOR_TEST))
1277                         }
1278                 STATE_COMPARE(gl_stencil_test[0])
1279                 {
1280                         if (newctx->gl_stencil_test[0])
1281                         {
1282                                 CHECK_GL_ERROR(_orig_fastpath_glEnable(GL_STENCIL_TEST))
1283                         }
1284                         else
1285                                 CHECK_GL_ERROR(_orig_fastpath_glDisable(GL_STENCIL_TEST))
1286                         }
1287         }
1288
1289 #ifdef COREGL_USE_MODULE_TRACEPATH
1290         tracepath_api_trace_end("eglMakeCurrent(FP enable states)", trace_hint_enable_states, 0);
1291 #endif // COREGL_USE_MODULE_TRACEPATH
1292
1293         //------------------//
1294         // _clear_flag1
1295 #ifdef COREGL_USE_MODULE_TRACEPATH
1296         static void *trace_hint_clear_viewport = NULL;
1297         trace_hint_clear_viewport = tracepath_api_trace_begin("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1298 #endif // COREGL_USE_MODULE_TRACEPATH
1299
1300         flag = oldctx->_clear_flag1 | newctx->_clear_flag1;
1301         if (flag)
1302         {
1303                 // Viewport.
1304                 STATES_COMPARE(gl_viewport, 4 * sizeof(GLint))
1305                 {
1306                         CHECK_GL_ERROR(_orig_fastpath_glViewport(newctx->gl_viewport[0],
1307                                        newctx->gl_viewport[1],
1308                                        newctx->gl_viewport[2],
1309                                        newctx->gl_viewport[3]))
1310                 }
1311
1312                 STATE_COMPARE(gl_current_program[0])
1313                 {
1314                         CHECK_GL_ERROR(_orig_fastpath_glUseProgram(newctx->gl_current_program[0]))
1315                 }
1316                 STATES_COMPARE(gl_color_clear_value, 4 * sizeof(GLclampf))
1317                 {
1318                         CHECK_GL_ERROR(_orig_fastpath_glClearColor(newctx->gl_color_clear_value[0],
1319                                        newctx->gl_color_clear_value[1],
1320                                        newctx->gl_color_clear_value[2],
1321                                        newctx->gl_color_clear_value[3]))
1322                 }
1323         }
1324
1325
1326         // _clear_flag2
1327         flag = oldctx->_clear_flag2 | newctx->_clear_flag2;
1328         if (flag)
1329         {
1330                 STATES_COMPARE(gl_color_writemask, 4 * sizeof(GLboolean))
1331                 {
1332                         CHECK_GL_ERROR(_orig_fastpath_glColorMask(newctx->gl_color_writemask[0],
1333                                        newctx->gl_color_writemask[1],
1334                                        newctx->gl_color_writemask[2],
1335                                        newctx->gl_color_writemask[3]))
1336                 }
1337                 STATES_COMPARE(gl_depth_range, 2 * sizeof(GLclampf))
1338                 {
1339                         CHECK_GL_ERROR(_orig_fastpath_glDepthRangef(newctx->gl_depth_range[0],
1340                                        newctx->gl_depth_range[1]))
1341                 }
1342                 STATE_COMPARE(gl_depth_clear_value[0])
1343                 {
1344                         CHECK_GL_ERROR(_orig_fastpath_glClearDepthf(newctx->gl_depth_clear_value[0]))
1345                 }
1346                 STATE_COMPARE(gl_depth_func[0])
1347                 {
1348                         CHECK_GL_ERROR(_orig_fastpath_glDepthFunc(newctx->gl_depth_func[0]))
1349                 }
1350                 STATE_COMPARE(gl_depth_writemask[0])
1351                 {
1352                         CHECK_GL_ERROR(_orig_fastpath_glDepthMask(newctx->gl_depth_writemask[0]))
1353                 }
1354                 STATE_COMPARE(gl_cull_face_mode[0])
1355                 {
1356                         CHECK_GL_ERROR(_orig_fastpath_glCullFace(newctx->gl_cull_face_mode[0]))
1357                 }
1358
1359         }
1360
1361 #ifdef COREGL_USE_MODULE_TRACEPATH
1362         tracepath_api_trace_end("eglMakeCurrent(FP clear/viewport)", trace_hint_clear_viewport, 0);
1363 #endif // COREGL_USE_MODULE_TRACEPATH
1364
1365         //------------------//
1366         // Texture here...
1367 #ifdef COREGL_USE_MODULE_TRACEPATH
1368         static void *trace_hint_bind_textures = NULL;
1369         trace_hint_bind_textures = tracepath_api_trace_begin("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
1370 #endif // COREGL_USE_MODULE_TRACEPATH
1371
1372         flag = oldctx->_tex_flag1 | newctx->_tex_flag1;
1373         if (flag)
1374         {
1375
1376                 for (i = 0; i < oldctx->gl_num_tex_units[0]; i++)
1377                 {
1378                         STATE_COMPARE(gl_tex_2d_state[i])
1379                         {
1380                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1381                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_2D, newctx->gl_tex_2d_state[i]))
1382                         }
1383
1384                         STATE_COMPARE(gl_tex_cube_state[i])
1385                         {
1386                                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(GL_TEXTURE0 + i))
1387                                 CHECK_GL_ERROR(_orig_fastpath_glBindTexture(GL_TEXTURE_CUBE_MAP, newctx->gl_tex_cube_state[i]))
1388                         }
1389                 }
1390
1391                 // Restore active texture
1392                 CHECK_GL_ERROR(_orig_fastpath_glActiveTexture(newctx->gl_active_texture[0]))
1393
1394                 STATE_COMPARE(gl_generate_mipmap_hint[0])
1395                 {
1396                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_GENERATE_MIPMAP_HINT, newctx->gl_generate_mipmap_hint[0]))
1397                 }
1398         }
1399 #ifdef COREGL_USE_MODULE_TRACEPATH
1400         tracepath_api_trace_end("eglMakeCurrent(FP bind textures)", trace_hint_bind_textures, 0);
1401 #endif // COREGL_USE_MODULE_TRACEPATH
1402
1403         //------------------//
1404 #ifdef COREGL_USE_MODULE_TRACEPATH
1405         static void *trace_hint_etc = NULL;
1406         trace_hint_etc = tracepath_api_trace_begin("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
1407 #endif // COREGL_USE_MODULE_TRACEPATH
1408
1409         flag = oldctx->_blend_flag | newctx->_blend_flag;
1410         if (flag)
1411         {
1412                 STATES_COMPARE(gl_blend_color, 4 * sizeof(GLclampf))
1413                 {
1414                         CHECK_GL_ERROR(_orig_fastpath_glBlendColor(newctx->gl_blend_color[0],
1415                                        newctx->gl_blend_color[1],
1416                                        newctx->gl_blend_color[2],
1417                                        newctx->gl_blend_color[3]))
1418                 }
1419                 if ((oldctx->gl_blend_src_rgb[0] != newctx->gl_blend_src_rgb[0]) ||
1420                     (oldctx->gl_blend_dst_rgb[0] != newctx->gl_blend_dst_rgb[0]) ||
1421                     (oldctx->gl_blend_src_alpha[0] != newctx->gl_blend_src_alpha[0]) ||
1422                     (oldctx->gl_blend_dst_alpha[0] != newctx->gl_blend_dst_alpha[0]))
1423                 {
1424                         CHECK_GL_ERROR(_orig_fastpath_glBlendFuncSeparate(newctx->gl_blend_src_rgb[0],
1425                                        newctx->gl_blend_dst_rgb[0],
1426                                        newctx->gl_blend_src_alpha[0],
1427                                        newctx->gl_blend_dst_alpha[0]))
1428                 }
1429                 if ((oldctx->gl_blend_equation_rgb[0] != newctx->gl_blend_equation_rgb[0]) ||
1430                     (oldctx->gl_blend_equation_alpha[0] != newctx->gl_blend_equation_alpha[0]))
1431                 {
1432                         CHECK_GL_ERROR(_orig_fastpath_glBlendEquationSeparate(newctx->gl_blend_equation_rgb[0], newctx->gl_blend_equation_alpha[0]))
1433                 }
1434
1435         }
1436
1437         //------------------//
1438         // _stencil_flag1
1439         flag = oldctx->_stencil_flag1 | newctx->_stencil_flag1;
1440         if (flag)
1441         {
1442                 if ((oldctx->gl_stencil_func[0] != newctx->gl_stencil_func[0]) ||
1443                     (oldctx->gl_stencil_ref[0]  != newctx->gl_stencil_ref[0])  ||
1444                     (oldctx->gl_stencil_value_mask[0] != newctx->gl_stencil_value_mask[0]))
1445                 {
1446                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_FRONT,
1447                                        newctx->gl_stencil_func[0],
1448                                        newctx->gl_stencil_ref[0],
1449                                        newctx->gl_stencil_value_mask[0]))
1450                 }
1451                 if ((oldctx->gl_stencil_fail[0] != newctx->gl_stencil_fail[0]) ||
1452                     (oldctx->gl_stencil_pass_depth_fail[0] != newctx->gl_stencil_pass_depth_fail[0]) ||
1453                     (oldctx->gl_stencil_pass_depth_pass[0] != newctx->gl_stencil_pass_depth_pass[0]))
1454                 {
1455                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_FRONT,
1456                                        newctx->gl_stencil_fail[0],
1457                                        newctx->gl_stencil_pass_depth_fail[0],
1458                                        newctx->gl_stencil_pass_depth_pass[0]))
1459                 }
1460
1461                 STATE_COMPARE(gl_stencil_writemask[0])
1462                 {
1463                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_FRONT, newctx->gl_stencil_writemask[0]))
1464                 }
1465         }
1466
1467
1468         // _stencil_flag1
1469         flag = oldctx->_stencil_flag2 | newctx->_stencil_flag2;
1470         if (flag)
1471         {
1472                 if ((oldctx->gl_stencil_back_func[0] != newctx->gl_stencil_back_func[0]) ||
1473                     (oldctx->gl_stencil_back_ref[0]  != newctx->gl_stencil_back_ref[0])  ||
1474                     (oldctx->gl_stencil_back_value_mask[0] != newctx->gl_stencil_back_value_mask[0]))
1475                 {
1476                         CHECK_GL_ERROR(_orig_fastpath_glStencilFuncSeparate(GL_BACK,
1477                                        newctx->gl_stencil_back_func[0],
1478                                        newctx->gl_stencil_back_ref[0],
1479                                        newctx->gl_stencil_back_value_mask[0]))
1480                 }
1481                 if ((oldctx->gl_stencil_back_fail[0] != newctx->gl_stencil_back_fail[0]) ||
1482                     (oldctx->gl_stencil_back_pass_depth_fail[0] != newctx->gl_stencil_back_pass_depth_fail[0]) ||
1483                     (oldctx->gl_stencil_back_pass_depth_pass[0] != newctx->gl_stencil_back_pass_depth_pass[0]))
1484                 {
1485                         CHECK_GL_ERROR(_orig_fastpath_glStencilOpSeparate(GL_BACK,
1486                                        newctx->gl_stencil_back_fail[0],
1487                                        newctx->gl_stencil_back_pass_depth_fail[0],
1488                                        newctx->gl_stencil_back_pass_depth_pass[0]))
1489                 }
1490
1491                 STATE_COMPARE(gl_stencil_back_writemask[0])
1492                 {
1493                         CHECK_GL_ERROR(_orig_fastpath_glStencilMaskSeparate(GL_BACK, newctx->gl_stencil_back_writemask[0]))
1494                 }
1495                 STATE_COMPARE(gl_stencil_clear_value[0])
1496                 {
1497                         CHECK_GL_ERROR(_orig_fastpath_glClearStencil(newctx->gl_stencil_clear_value[0]))
1498                 }
1499         }
1500
1501         //------------------//
1502         // _misc_flag1
1503         flag = oldctx->_misc_flag1 | newctx->_misc_flag1;
1504         if (flag)
1505         {
1506                 STATE_COMPARE(gl_front_face[0])
1507                 {
1508                         CHECK_GL_ERROR(_orig_fastpath_glFrontFace(newctx->gl_front_face[0]))
1509                 }
1510                 STATE_COMPARE(gl_line_width[0])
1511                 {
1512                         CHECK_GL_ERROR(_orig_fastpath_glLineWidth(newctx->gl_line_width[0]))
1513                 }
1514                 if ((oldctx->gl_polygon_offset_factor[0] != newctx->gl_polygon_offset_factor[0]) ||
1515                     (oldctx->gl_polygon_offset_units[0]  != newctx->gl_polygon_offset_units[0]))
1516                 {
1517                         CHECK_GL_ERROR(_orig_fastpath_glPolygonOffset(newctx->gl_polygon_offset_factor[0],
1518                                        newctx->gl_polygon_offset_units[0]))
1519                 }
1520                 if ((oldctx->gl_sample_coverage_value[0]  != newctx->gl_sample_coverage_value[0]) ||
1521                     (oldctx->gl_sample_coverage_invert[0] != newctx->gl_sample_coverage_invert[0]))
1522                 {
1523                         CHECK_GL_ERROR(_orig_fastpath_glSampleCoverage(newctx->gl_sample_coverage_value[0],
1524                                        newctx->gl_sample_coverage_invert[0]))
1525                 }
1526                 STATE_COMPARE(gl_fragment_shader_derivative_hint[0])
1527                 {
1528                         CHECK_GL_ERROR(_orig_fastpath_glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES, newctx->gl_fragment_shader_derivative_hint[0]))
1529                 }
1530         }
1531
1532         // _misc_flag2
1533         flag = oldctx->_misc_flag2 | newctx->_misc_flag2;
1534         if (flag)
1535         {
1536                 STATES_COMPARE(gl_scissor_box, 4 * sizeof(GLint))
1537                 {
1538                         CHECK_GL_ERROR(_orig_fastpath_glScissor(newctx->gl_scissor_box[0],
1539                                                                 newctx->gl_scissor_box[1],
1540                                                                 newctx->gl_scissor_box[2],
1541                                                                 newctx->gl_scissor_box[3]))
1542                 }
1543                 STATE_COMPARE(gl_pack_alignment[0])
1544                 {
1545                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_PACK_ALIGNMENT, newctx->gl_pack_alignment[0]))
1546                 }
1547                 STATE_COMPARE(gl_unpack_alignment[0])
1548                 {
1549                         CHECK_GL_ERROR(_orig_fastpath_glPixelStorei(GL_UNPACK_ALIGNMENT, newctx->gl_unpack_alignment[0]))
1550                 }
1551         }
1552 #ifdef COREGL_USE_MODULE_TRACEPATH
1553         tracepath_api_trace_end("eglMakeCurrent(FP etc.)", trace_hint_etc, 0);
1554 #endif // COREGL_USE_MODULE_TRACEPATH
1555
1556         // _varray_flag
1557 #ifdef COREGL_USE_MODULE_TRACEPATH
1558         static void *trace_hint_vertex_attrib = NULL;
1559         trace_hint_vertex_attrib = tracepath_api_trace_begin("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1560 #endif // COREGL_USE_MODULE_TRACEPATH
1561
1562         flag = oldctx->_vattrib_flag | newctx->_vattrib_flag;
1563         if (flag)
1564         {
1565                 for (i = 0; i < oldctx->gl_num_vertex_attribs[0]; i++)
1566                 {
1567                         if (newctx->gl_vertex_array_buf_id[i] != oldctx->gl_vertex_array_buf_id[i])
1568                         {
1569                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_vertex_array_buf_id[i]))
1570                         }
1571                         else
1572                         {
1573                                 CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, 0))
1574                         }
1575
1576                         CHECK_GL_ERROR(_orig_fastpath_glVertexAttribPointer(i,
1577                                        newctx->gl_vertex_array_size[i],
1578                                        newctx->gl_vertex_array_type[i],
1579                                        newctx->gl_vertex_array_normalized[i],
1580                                        newctx->gl_vertex_array_stride[i],
1581                                        newctx->gl_vertex_array_pointer[i]))
1582
1583                         STATES_COMPARE(gl_vertex_attrib_value + 4 * i, 4 * sizeof(GLfloat))
1584                         {
1585                                 CHECK_GL_ERROR(_orig_fastpath_glVertexAttrib4fv(i, &newctx->gl_vertex_attrib_value[4 * i]))
1586                         }
1587
1588                         if (newctx->gl_vertex_array_enabled[i] == GL_TRUE)
1589                         {
1590                                 CHECK_GL_ERROR(_orig_fastpath_glEnableVertexAttribArray(i))
1591                         }
1592                         else
1593                         {
1594                                 CHECK_GL_ERROR(_orig_fastpath_glDisableVertexAttribArray(i))
1595                         }
1596                 }
1597
1598                 STATE_COMPARE(gl_array_buffer_binding[0])
1599                 {
1600                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ARRAY_BUFFER, newctx->gl_array_buffer_binding[0]))
1601                 }
1602                 STATE_COMPARE(gl_element_array_buffer_binding[0])
1603                 {
1604                         CHECK_GL_ERROR(_orig_fastpath_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newctx->gl_element_array_buffer_binding[0]))
1605                 }
1606
1607         }
1608
1609 #ifdef COREGL_USE_MODULE_TRACEPATH
1610         tracepath_api_trace_end("eglMakeCurrent(FP vertex attrib)", trace_hint_vertex_attrib, 0);
1611 #endif // COREGL_USE_MODULE_TRACEPATH
1612
1613         ret = 1;
1614         goto finish;
1615
1616 finish:
1617
1618 #ifdef COREGL_FASTPATH_TRACE_STATE_INFO
1619         if (unlikely(trace_state_flag == 1))
1620                 fastpath_dump_context_states(newctx, 0);
1621 #endif // COREGL_FASTPATH_TRACE_STATE_INFO
1622         return ret;
1623 #undef STATE_COMPARE
1624 #undef STATES_COMPARE
1625 }
1626