rollback makecurrent behavior
[framework/osp/uifw.git] / src / graphics / opengl / FGrpEgl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrpEgl.cpp
20  * @brief       This is the implementation file for egl.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <X11/Xlib.h>
28 #include <X11/Xmd.h>
29 #include <EGL/egl.h>
30
31 #if defined(_OSP_EMUL_)
32 #define FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER
33 #else
34 #define FGRAPHICS_INTERNAL_USE_DRM
35 #endif
36
37 #define FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING
38 #if defined(FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING)
39 #include <dlfcn.h>
40 #endif
41
42 #include <Ecore_Evas.h>
43 #include <Ecore_X.h>
44
45 #include <FBaseSysLog.h>
46 #include <FGrpBitmap.h>
47 #include <FGrpCoordinateSystem.h>
48 #include <FUiControl.h>
49 #include <FUiCtrlForm.h>
50 #include <FUiCtrlFrame.h>
51 #include <FUiWindow.h>
52
53 #include <FUi_EcoreEvasMgr.h>
54 #include <FUi_EcoreEvas.h>
55
56 #include <FGrp_BitmapTool.h>
57 #include <FGrp_BitmapImpl.h>
58 #include <FUi_ControlImpl.h>
59 #include <FUi_Control.h>
60 #include <FUi_WindowImpl.h>
61 #include <FUiAnim_VisualElementSurfaceImpl.h>
62 #include <FUiAnim_VisualElementImpl.h>
63
64
65 #ifdef __cplusplus
66 extern "C"
67 {
68 #endif
69
70 #include <elm_config.h>
71
72 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
73 #include <fcntl.h>
74 #include <unistd.h>
75 #include <dri2.h>
76 #include <tbm_bufmgr.h>
77 #include <xf86drm.h>
78 #endif
79
80 typedef int _SglIndex;
81
82 EGLSurface
83 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList);
84 EGLSurface _SglGetCurrentSurface(EGLint readDraw);
85 EGLNativePixmapType _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pixmap);
86 EGLNativePixmapType _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo);
87
88 #if defined(FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING)
89 void _GlesInterfaceTerminate_1();
90 void _GlesInterfaceTerminate_2();
91 #endif
92
93 #include <GLES2/gl2.h>
94
95 void _GlFlush_1(void);
96 void _GlFlush_2(void);
97 void _GlClear_1(GLbitfield mask);
98 void _GlClear_2(GLbitfield mask);
99
100 EGLBoolean _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
101
102 using namespace Tizen::Ui::Controls;
103
104 #ifdef __cplusplus
105 }
106 #endif
107
108 using namespace Tizen::Ui::Animations;
109 using namespace Tizen::Ui;
110 using namespace Tizen::Base;
111
112 namespace Tizen { namespace Graphics
113 {
114
115 namespace Opengl
116 {
117
118 namespace // unnamed
119 {
120
121 class _SglInfo;
122
123 #define FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT
124 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
125 void _PostRenderCallback(Ecore_Evas* ee);
126 void _SaveCurrentContext(_SglInfo* pSglInfo);
127 void _UnregisterRenderCallback(Evas_Object* pObject);
128 int __registerCallbackCount = 0;
129 _SglInfo* __pPreviousSglInfo = null;
130 #endif
131
132 #if !defined (_OSP_EMUL_)
133 #define FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP
134 #endif
135
136 enum _engineType
137 {
138         ENGINE_TYPE_SOFEWARE_X11 = 0,
139         ENGINE_TYPE_OPENGL_X11,
140         ENGINE_TYPE_ETC,
141 };
142
143 _engineType __evasEngineType = ENGINE_TYPE_SOFEWARE_X11;
144 bool __isDoublePixmapEnabled = false;
145
146 void _OnBoundsChanged(void* pData);
147
148 const int INVALID_SGL_INDEX = 0;
149 const int MAX_SGL_INDEX = 100;
150
151 class _SglInfo
152 {
153 public:
154         _SglInfo(void)
155         : sglIndex(INVALID_SGL_INDEX)
156         , display(EGL_NO_DISPLAY)
157         , surface(EGL_NO_SURFACE)
158         , context(EGL_NO_CONTEXT)
159         , config((EGLConfig)0)
160         , pBitmap(null)
161         , pObject(null)
162         , nativePixmap((EGLNativePixmapType)0)
163         , pVisualElement(null)
164         , pVisualElementSurface(null)
165         , glVersion(0)
166         , pWindow(null)
167 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
168         , pEcoreImage(null)
169         , width(0)
170         , height(0)
171 #endif
172         , isBackbuffer(true)
173         , wasShown(false)
174         , swapDone(false)
175         , isFirstSwap(true)
176         , pSecondSglInfo(null)
177         {
178         }
179
180         virtual ~_SglInfo(void)
181         {
182 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
183                 if (pEcoreImage != null)
184                 {
185                         ecore_x_image_free(pEcoreImage);
186                 }
187 #endif
188
189                 if (pObject == null)
190                 {
191                         return;
192                 }
193
194                 if (pVisualElement != null)
195                 {
196                         _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
197                         if (pVisualElementImpl != null)
198                         {
199                                 pVisualElementImpl->SetDestroyedCallback(null, null);
200                         }
201                 }
202
203 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
204                 _UnregisterRenderCallback(pObject);
205 #endif
206                 evas_object_del(pObject);
207                 delete pVisualElementSurface;
208                 delete pBitmap;
209
210                 bool needAllocateObject = true;
211                 if (__isDoublePixmapEnabled)
212                 {
213                         if (!isBackbuffer)
214                         {
215                                 needAllocateObject = false;
216                         }
217                 }
218                 if (needAllocateObject)
219                 {
220                         if (pVisualElement != null)
221                         {
222                                 pVisualElement->SetSurface(null);
223                         }
224                 }
225
226                 sglIndex = INVALID_SGL_INDEX;
227                 display = EGL_NO_DISPLAY;
228                 surface = EGL_NO_SURFACE;
229                 context = EGL_NO_CONTEXT;
230                 config = (EGLConfig)0;
231                 pBitmap = null;
232                 pObject = null;
233                 nativePixmap = (EGLNativePixmapType)0;
234                 pVisualElement = null;
235                 pVisualElementSurface = null;
236                 glVersion = 0;
237                 pWindow = null;
238 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
239                 pEcoreImage = null;
240                 width = 0;
241                 height = 0;
242 #endif
243                 isBackbuffer = true;
244                 wasShown = false;
245                 swapDone = false;
246                 isFirstSwap = true;
247                 pSecondSglInfo = null;
248         }
249
250 public:
251         _SglIndex sglIndex;
252         EGLDisplay display;
253         EGLSurface surface;
254         EGLContext context;
255         EGLConfig config;
256         Bitmap* pBitmap;
257         Evas_Object* pObject;
258         EGLNativePixmapType nativePixmap;
259         VisualElement* pVisualElement;
260         VisualElementSurface* pVisualElementSurface;
261         int glVersion;
262         Tizen::Ui::Window* pWindow;
263 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
264         Ecore_X_Image* pEcoreImage;
265         int width;
266         int height;
267 #endif
268         bool isBackbuffer;
269         bool wasShown;
270         bool swapDone;
271         bool isFirstSwap;
272         _SglInfo* pSecondSglInfo;
273
274 private:
275         _SglInfo(const _SglInfo& sglInfo);
276         _SglInfo& operator =(const _SglInfo& rhs);
277 };
278
279 class _PixmapInfo
280 {
281 public:
282         _PixmapInfo(void)
283         : nativePixmap((Pixmap)0)
284 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
285         , pTbmBufMgr(null)
286         , pTbmBufferObject(null)
287         , drmFd(0)
288 #endif
289         {
290         }
291
292         virtual ~_PixmapInfo(void)
293         {
294                 Display* pDisplay = (Display*)ecore_x_display_get();
295 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
296                 tbm_bo_unmap(pTbmBufferObject);
297                 tbm_bo_unref(pTbmBufferObject);
298                 if (pTbmBufMgr)
299                 {
300                         close(drmFd);
301                 }
302
303                 tbm_bufmgr_deinit(pTbmBufMgr);
304                 DRI2DestroyDrawable(pDisplay, nativePixmap);
305 #endif
306                 XFreePixmap(pDisplay, nativePixmap);
307
308                 nativePixmap = (Pixmap)0;
309 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
310                 pTbmBufMgr = null;
311                 pTbmBufferObject = null;
312                 drmFd = 0;
313 #endif
314         }
315
316 public:
317         Pixmap nativePixmap;
318 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
319         tbm_bufmgr pTbmBufMgr;
320         tbm_bo pTbmBufferObject;
321         int drmFd;
322 #endif
323 };
324
325 void _EvasObjectImageChange(_SglInfo* pSglInfo);
326
327 void
328 _OnBoundsChanged(void* pData)
329 {
330         _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
331         SysTryReturnVoidResult(NID_GRP, pSglInfo != null
332                         && pSglInfo->sglIndex > INVALID_SGL_INDEX
333                         && pSglInfo->pVisualElement != null
334                         && pSglInfo->pWindow != null
335                         , E_INVALID_STATE, "Invalid state.");
336
337         if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
338         {
339                 __pPreviousSglInfo = null;
340         }
341
342         bool needSglMakeCurrent = false;
343         EGLDisplay eglDisplay = eglGetCurrentDisplay();
344         EGLSurface drawSurface = _SglGetCurrentSurface(EGL_DRAW);
345         EGLSurface readSurface = _SglGetCurrentSurface(EGL_READ);
346         EGLSurface eglContext = eglGetCurrentContext();
347
348         if (drawSurface != EGL_NO_SURFACE && readSurface != EGL_NO_SURFACE)
349         {
350                 needSglMakeCurrent = true;
351         }
352         else
353         {
354                 drawSurface = eglGetCurrentSurface(EGL_DRAW);
355                 readSurface = eglGetCurrentSurface(EGL_READ);
356         }
357
358         eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
359         EGLBoolean ret = eglDestroySurface(pSglInfo->display, pSglInfo->surface);
360         pSglInfo->surface = null;
361         SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
362
363 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
364                 if (pSglInfo->pEcoreImage != null)
365                 {
366                         ecore_x_image_free(pSglInfo->pEcoreImage);
367                         pSglInfo->pEcoreImage = null;
368                 }
369 #endif
370
371         FloatRectangle rect = pSglInfo->pVisualElement->GetBounds();
372         int width = int(rect.width);
373         int height = int(rect.height);
374         SysTryReturnVoidResult(NID_GRP, width > 0 && height > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
375         int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
376         int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
377         SysTryReturnVoidResult(NID_GRP, widthPhysical > 0 && heightPhysical > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", widthPhysical, heightPhysical);
378
379         evas_object_image_size_set(pSglInfo->pObject, widthPhysical, heightPhysical);
380         evas_object_resize(pSglInfo->pObject, widthPhysical, heightPhysical);
381
382         delete pSglInfo->pBitmap;
383         pSglInfo->pBitmap = null;
384         std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
385         SysTryReturnVoidResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
386         result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
387         SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
388         EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfo->sglIndex, bitmap.get());
389         SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
390         EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfo->display, pSglInfo->config, nativePixmap, null);
391         SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
392
393         pSglInfo->nativePixmap = nativePixmap;
394         pSglInfo->surface = eglSurface;
395         pSglInfo->isBackbuffer = true;
396         pSglInfo->wasShown = false;
397         pSglInfo->swapDone = false;
398         pSglInfo->isFirstSwap = true;
399
400         if (__isDoublePixmapEnabled)
401         {
402                 _SglInfo* pSglInfoSecond = pSglInfo->pSecondSglInfo;
403                 SysTryReturnVoidResult(NID_GRP, pSglInfoSecond != null, E_INVALID_STATE, "Invalid pSecondSglInfo.");
404
405                 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
406                 ret = eglDestroySurface(pSglInfoSecond->display, pSglInfoSecond->surface);
407                 pSglInfoSecond->surface = null;
408                 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
409
410                 evas_object_image_size_set(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
411                 evas_object_resize(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
412
413                 delete pSglInfoSecond->pBitmap;
414                 pSglInfoSecond->pBitmap = null;
415                 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
416                 SysTryReturnVoidResult(NID_GRP, bitmapSecond.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
417                 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
418                 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
419                 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfoSecond->sglIndex, bitmapSecond.get());
420                 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
421                 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfoSecond->display, pSglInfoSecond->config, nativePixmap, null);
422                 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
423
424                 pSglInfoSecond->nativePixmap = nativePixmap;
425                 pSglInfoSecond->surface = eglSurface;
426                 pSglInfoSecond->pBitmap = bitmapSecond.release();
427                 pSglInfoSecond->isBackbuffer = true;
428                 pSglInfoSecond->wasShown = false;
429                 pSglInfoSecond->swapDone = true;
430
431                 pSglInfo->isFirstSwap = true;
432
433                 _EvasObjectImageChange(pSglInfoSecond);
434         }
435
436         pSglInfo->pBitmap = bitmap.release();
437         _EvasObjectImageChange(pSglInfo);
438
439         BufferInfo bufferInfo;
440         pSglInfo->pBitmap->Lock(bufferInfo);
441         pSglInfo->pBitmap->Unlock();
442
443         if (needSglMakeCurrent)
444         {
445                 _SglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
446         }
447         else
448         {
449                 eglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
450         }
451
452         return;
453 }
454
455 void
456 _OnDestroyed(void* pData)
457 {
458         _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
459
460         if (pSglInfo != null)
461         {
462                 pSglInfo->pVisualElement = null;
463
464                 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
465                 {
466                         pSglInfo->pSecondSglInfo->pVisualElement = null;
467                 }
468         }
469 }
470
471 class _SglInfoTableManipulator
472 {
473 public:
474         _SglInfoTableManipulator()
475         {
476                 result r = __SglMutex.Create();
477                 SysTryLog(NID_GRP, r == E_SUCCESS, "Failed to create mutex. [%s]", GetErrorMessage(r));
478                 __SglIndexLastUsed = INVALID_SGL_INDEX;
479                 __SglInitialized = false;
480         }
481
482         ~_SglInfoTableManipulator()
483         {
484         }
485
486         _SglIndex
487         FindNextEmptySlotOfSglInfoTable()
488         {
489                 if (!__SglInitialized)
490                 {
491                         if (CreateSglInfoTable() == false)
492                         {
493                                 return INVALID_SGL_INDEX;
494                         }
495                 }
496
497                 _SglIndex sglIndex = INVALID_SGL_INDEX;
498                 _SglIndex index = __SglIndexLastUsed;
499                 {
500                         __SglMutex.Acquire();
501                         for (int i = 1; i < MAX_SGL_INDEX; i++)
502                         {
503                                 index++;
504
505                                 if (index >= MAX_SGL_INDEX)
506                                 {
507                                         index = 1;
508                                 }
509
510                                 if (__SglInfoTable[index] == null)
511                                 {
512                                         sglIndex = index;
513                                         __SglIndexLastUsed = index;
514                                         break;
515                                 }
516                         }
517                         __SglMutex.Release();
518                 }
519
520                 if (sglIndex <= INVALID_SGL_INDEX)
521                 {
522                         return INVALID_SGL_INDEX;
523                 }
524
525                 return sglIndex;
526         }
527
528         bool
529         CreateSglInfoTable()
530         {
531                 bool ret = false;
532
533                 __SglMutex.Acquire();
534
535                 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
536                 {
537                         __SglInfoTable[i] = null;
538                 }
539
540                 __SglInfoTable[0] = new (std::nothrow) _SglInfo;
541                 if (__SglInfoTable[0] != null)
542                 {
543                         ret = true;
544                         __SglInitialized = true;
545                         __SglInfoTable[0]->glVersion = 2;
546                 }
547
548                 __SglMutex.Release();
549
550                 return ret;
551         }
552
553         void
554         DestroySglInfoTable()
555         {
556                 __SglMutex.Acquire();
557
558                 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
559                 {
560                         if (__SglInfoTable[i] != null)
561                         {
562                                 delete __SglInfoTable[i];
563                                 __SglInfoTable[i] = null;
564                         }
565                 }
566                 __SglInitialized = false;
567
568                 __SglMutex.Release();
569         }
570
571         _SglInfo*
572         LockSglInfoTable(_SglIndex index)
573         {
574                 __SglMutex.Acquire();
575
576                 if (index < INVALID_SGL_INDEX || index >= MAX_SGL_INDEX)
577                 {
578                         return null;
579                 }
580
581                 return __SglInfoTable[index];
582         }
583
584         void
585         UnlockSglInfoTable()
586         {
587                 __SglMutex.Release();
588         }
589
590         _SglIndex
591         CreateSglIndex(void)
592         {
593                 _SglInfo* newSglInfo = new (std::nothrow) _SglInfo;
594                 if (newSglInfo == null)
595                 {
596                         SysLog(NID_GRP, "CreateSglIndex fail to allocate _SglInfo!!");
597                         return INVALID_SGL_INDEX;
598                 }
599
600                 int sglIndex = FindNextEmptySlotOfSglInfoTable();
601                 if (sglIndex == INVALID_SGL_INDEX)
602                 {
603                         delete newSglInfo;
604
605                         SysLog(NID_GRP, "CreateSglIndex failed!!");
606                         return INVALID_SGL_INDEX;
607                 }
608
609                 __SglMutex.Acquire();
610                 __SglInfoTable[sglIndex] = newSglInfo;
611                 __SglMutex.Release();
612
613                 return sglIndex;
614         }
615
616         bool
617         DestroySglIndex(_SglIndex sglIndex)
618         {
619                 if (sglIndex < 1 || sglIndex >= MAX_SGL_INDEX)
620                 {
621                         SysLog(NID_GRP, "sglIndex:%#x is out of range.", (unsigned int)sglIndex);
622                         return false;
623                 }
624
625                 __SglMutex.Acquire();
626
627                 if (sglIndex <= INVALID_SGL_INDEX || __SglInfoTable[sglIndex] == null)
628                 {
629                         __SglMutex.Release();
630
631                         SysLog(NID_GRP, "DestroySglIndex failed!! sglIndex:%#x", (unsigned int)sglIndex);
632                         return false;
633                 }
634
635                 delete __SglInfoTable[sglIndex];
636                 __SglInfoTable[sglIndex] = null;
637
638                 __SglMutex.Release();
639
640                 return true;
641         }
642
643         _SglIndex
644         GetSglIndexForSurface(EGLSurface surface)
645         {
646                 __SglMutex.Acquire();
647
648                 int sglIndex = 0;
649                 for (int i = 0; i < MAX_SGL_INDEX; i++)
650                 {
651                         if (__SglInfoTable[i] != null && __SglInfoTable[i]->surface == surface)
652                         {
653                                 sglIndex = i;
654                                 break;
655                         }
656                 }
657
658                 __SglMutex.Release();
659
660                 return sglIndex;
661         }
662
663         static _SglInfoTableManipulator* GetInstance(void)
664         {
665                 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
666
667                 if (!__pTheInstance)
668                 {
669                         pthread_once(&once_block, __InitSglInfoTableManipulator);
670                 }
671
672                 return __pTheInstance;
673         }
674
675 private:
676         static void __InitSglInfoTableManipulator(void)
677         {
678                 static _SglInfoTableManipulator instance;
679                 __pTheInstance = &instance;
680         }
681
682 private:
683         Runtime::Mutex __SglMutex;
684         _SglInfo* __SglInfoTable[MAX_SGL_INDEX];
685         _SglIndex __SglIndexLastUsed;
686         bool __SglInitialized;
687
688         static _SglInfoTableManipulator* __pTheInstance;
689 };
690 _SglInfoTableManipulator* _SglInfoTableManipulator::__pTheInstance = null;
691
692 void
693 _PixmapSurfaceDestroyCallback(void* pCallbackParam)
694 {
695         delete (_PixmapInfo*)pCallbackParam;
696 }
697
698 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
699 void
700 _PixmapLockCallBack(void* pCallbackParam)
701 {
702         tbm_bo_map((tbm_bo)pCallbackParam, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
703 }
704
705 void
706 _PixmapUnlockCallBack(void* pCallbackParam)
707 {
708         tbm_bo_unmap((tbm_bo)pCallbackParam);
709 }
710 #endif
711
712 _engineType
713 _GetEvasEngineType(Evas_Object* pObject)
714 {
715         if (pObject == null)
716         {
717                 const char* pString = elm_config_preferred_engine_get();
718                 String engineName(pString);
719                 SysLog(NID_GRP, "evas backend : %s", pString);
720
721                 if (engineName.Contains(String("opengl_x11")))
722                 {
723                         return ENGINE_TYPE_OPENGL_X11;
724                 }
725                 else if (engineName.Contains(String("software_x11")))
726                 {
727                         return ENGINE_TYPE_SOFEWARE_X11;
728                 }
729                 else
730                 {
731                         pString = elm_config_engine_get();
732                         String engineName(pString);
733                         SysLog(NID_GRP, "system evas backend : %s", pString);
734                         if (engineName.Contains(String("opengl_x11")))
735                         {
736                                 return ENGINE_TYPE_OPENGL_X11;
737                         }
738                         else
739                         {
740                                 return ENGINE_TYPE_SOFEWARE_X11;
741                         }
742                 }
743         }
744         else
745         {
746                 Evas* pEvas = evas_object_evas_get(pObject);
747                 if (pEvas == null)
748                 {
749                         SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
750                         return ENGINE_TYPE_SOFEWARE_X11;
751                 }
752
753                 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
754                 if (pEcoreEvas == null)
755                 {
756                         SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEcoreEvas failed.");
757                         return ENGINE_TYPE_SOFEWARE_X11;
758                 }
759
760                 const char* pString = ecore_evas_engine_name_get(pEcoreEvas);
761                 String engineName(pString);
762                 SysLog(NID_GRP, "evas backend : %s", pString);
763
764                 if (engineName.Contains(String("opengl_x11")))
765                 {
766                         return ENGINE_TYPE_OPENGL_X11;
767                 }
768                 else if (engineName.Contains(String("software_x11")))
769                 {
770                         return ENGINE_TYPE_SOFEWARE_X11;
771                 }
772                 else
773                 {
774                         return ENGINE_TYPE_ETC;
775                 }
776         }
777 }
778
779 void
780 _EvasObjectImageChange(_SglInfo* pSglInfo)
781 {
782         if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
783         {
784                 Evas* pEvas = evas_object_evas_get(pSglInfo->pObject);
785                 if (pEvas == null)
786                 {
787                         SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
788                         return;
789                 }
790
791                 Evas_Native_Surface nativeSurface = {0, };
792                 nativeSurface.type = EVAS_NATIVE_SURFACE_X11;
793                 nativeSurface.version = EVAS_NATIVE_SURFACE_VERSION;
794                 nativeSurface.data.x11.pixmap = (unsigned long)pSglInfo->nativePixmap;
795                 nativeSurface.data.x11.visual = ecore_x_default_visual_get(ecore_x_display_get(), ecore_x_default_screen_get());
796                 evas_object_image_native_surface_set(pSglInfo->pObject, &nativeSurface);
797         }
798         else
799         {
800                 BufferInfo bufferInfo;
801                 pSglInfo->pBitmap->Lock(bufferInfo);
802                 evas_object_image_data_set(pSglInfo->pObject, bufferInfo.pPixels);
803                 pSglInfo->pBitmap->Unlock();
804         }
805 }
806
807 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
808 void
809 _PostRenderCallback(Ecore_Evas* ee)
810 {
811         if (__pPreviousSglInfo != null)
812         {
813                 EGLDisplay display = __pPreviousSglInfo->display;
814                 EGLSurface surface = __pPreviousSglInfo->surface;
815                 EGLContext context = __pPreviousSglInfo->context;
816
817                 if (__isDoublePixmapEnabled)
818                 {
819                         _SglInfo* pBackSglInfo = null;
820                         _SglInfo* pFrontSglInfo = null;
821
822                         if (__pPreviousSglInfo->isBackbuffer)
823                         {
824                                 pBackSglInfo = __pPreviousSglInfo;
825                                 pFrontSglInfo = __pPreviousSglInfo->pSecondSglInfo;
826                         }
827                         else
828                         {
829                                 pBackSglInfo = __pPreviousSglInfo->pSecondSglInfo;
830                                 pFrontSglInfo = __pPreviousSglInfo;
831                         }
832
833                         if (!pBackSglInfo->wasShown && __pPreviousSglInfo->pVisualElement != null)
834                         {
835                                 __pPreviousSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
836                                 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
837                                 evas_object_hide(pBackSglInfo->pObject);
838
839                                 pBackSglInfo->wasShown = true;
840                                 pFrontSglInfo->swapDone = true;
841                         }
842
843                         surface = pBackSglInfo->surface;
844                 }
845
846                 EGLBoolean ret = eglMakeCurrent(display, surface, surface, context);
847                 SysTryLog(NID_GRP, ret == EGL_TRUE, "fail to restore previous surface and context. %#x %#x %#x %#x egl error:%#x"
848                                 , (unsigned int)display
849                                 , (unsigned int)surface
850                                 , (unsigned int)surface
851                                 , (unsigned int)context
852                                 , (unsigned int)eglGetError());
853         }
854 }
855
856 void
857 _SaveCurrentContext(_SglInfo* pSglInfo)
858 {
859         __pPreviousSglInfo = pSglInfo;
860 }
861
862 void
863 _RegisterRenderCallback(Evas_Object* pObject)
864 {
865         if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
866         {
867                 if (__registerCallbackCount == 0)
868                 {
869                         Evas* pEvas = evas_object_evas_get(pObject);
870                         if (pEvas == null)
871                         {
872                                 return;
873                         }
874
875                         Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
876                         if (pEcoreEvas == null)
877                         {
878                                 return;
879                         }
880
881                         ecore_evas_callback_post_render_set(pEcoreEvas, _PostRenderCallback);
882                 }
883                 __registerCallbackCount++;
884         }
885 }
886
887 void
888 _UnregisterRenderCallback(Evas_Object* pObject)
889 {
890         if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
891         {
892                 __registerCallbackCount--;
893
894                 if (__registerCallbackCount == 0)
895                 {
896                         Evas* pEvas = evas_object_evas_get(pObject);
897                         if (pEvas == null)
898                         {
899                                 return;
900                         }
901
902                         Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
903                         if (pEcoreEvas == null)
904                         {
905                                 return;
906                         }
907
908                         ecore_evas_callback_post_render_set(pEcoreEvas, NULL);
909                 }
910         }
911 }
912 #endif //#if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
913
914 }
915
916 #ifdef __cplusplus
917 extern "C"
918 {
919 #endif
920
921 EGLint
922 _SglGetError()
923 {
924         return eglGetError();
925 }
926
927 EGLDisplay
928 _SglGetDisplay(EGLNativeDisplayType displayId)
929 {
930         if (displayId == (EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)
931         {
932                 return eglGetDisplay((EGLNativeDisplayType) ecore_x_display_get());
933         }
934
935         return eglGetDisplay((EGLNativeDisplayType) displayId);
936 }
937
938 EGLBoolean
939 _SglInitialize(EGLDisplay dpy, EGLint* pMajor, EGLint* pMinor)
940 {
941         return eglInitialize(dpy, pMajor, pMinor);
942 }
943
944 EGLBoolean
945 _SglTerminate(EGLDisplay dpy)
946 {
947         _GlesInterfaceTerminate_1();
948         _GlesInterfaceTerminate_2();
949
950         _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
951
952         if (!__isDoublePixmapEnabled)
953         {
954                 eglTerminate(dpy);
955         }
956
957 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
958         __registerCallbackCount = 0;
959         __pPreviousSglInfo = null;
960 #endif
961
962
963         return EGL_TRUE;
964 }
965
966 const char*
967 _SglQueryString(EGLDisplay dpy, EGLint name)
968 {
969         return eglQueryString(dpy, name);
970 }
971
972 EGLBoolean
973 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
974 {
975         return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
976 }
977
978 EGLBoolean
979 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
980 {
981         return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
982 }
983
984 EGLBoolean
985 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
986 {
987         return eglGetConfigAttrib(dpy, config, attribute, pValue);
988 }
989
990 EGLSurface
991 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
992 {
993         _SglIndex sglIndex = INVALID_SGL_INDEX;
994         Object* pObj = (Object*)win;
995         VisualElement* pVisualElement = dynamic_cast<VisualElement*> (pObj);
996         Tizen::Ui::Control* pControl = dynamic_cast<Tizen::Ui::Control*> (pObj);
997 #if 0//!defined(_OSP_EMUL_)
998         Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
999 #endif
1000         SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1001         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1002
1003 #if 0//!defined(_OSP_EMUL_)
1004         if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
1005         {
1006                 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(*pControl);
1007                 _WindowImpl* pWindowImpl = dynamic_cast<_WindowImpl*> (pControlImpl);
1008                 SysTryReturn(NID_GRP, pWindowImpl != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window state.");
1009
1010                 EGLNativeWindowType nativeWindow = (EGLNativeWindowType)pWindowImpl->GetNativeHandle();
1011                 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1012                 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid SglIndex.");
1013
1014                 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1015                 if (windowSurface == EGL_NO_SURFACE)
1016                 {
1017                         pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1018                         sglIndex = 0;
1019                 }
1020                 else
1021                 {
1022                         VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1023                         if (pVisualElement == null)
1024                         {
1025                                 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1026                                 return EGL_NO_SURFACE;
1027                         }
1028
1029                         _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1030                         if (pVisualElementImpl == null)
1031                         {
1032                                 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1033                                 return EGL_NO_SURFACE;
1034                         }
1035
1036                         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1037
1038                         pSglInfo->surface = windowSurface;
1039
1040                         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1041                 }
1042
1043                 return (EGLSurface)sglIndex;
1044         }
1045         else
1046 #endif //#if !defined(_OSP_EMUL_)
1047         {
1048                 result r = E_SUCCESS;
1049
1050                 if (pVisualElement == null)
1051                 {
1052                         pVisualElement = pControl->GetVisualElement();
1053                 }
1054                 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1055
1056                 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1057                 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1058
1059                 FloatRectangle rect = pVisualElement->GetBounds();
1060                 int width = int(rect.width);
1061                 int height = int(rect.height);
1062                 SysTryReturn(NID_GRP, width > 0 && height > 0, EGL_NO_SURFACE, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
1063
1064                 Control* pFrame = pControl;
1065                 Control* pTemp = pFrame->GetParent();
1066                 while(pTemp != null)
1067                 {
1068                         pFrame = pTemp;
1069                         pTemp = pTemp->GetParent();
1070                 }
1071
1072                 Tizen::Ui::Window* pWindow = dynamic_cast<Tizen::Ui::Window*> (pFrame);
1073                 SysTryReturn(NID_GRP, pWindow != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window.");
1074
1075                 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1076                 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1077
1078                 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1079                 if (pVisualElementSurfaceImpl == null)
1080                 {
1081                         SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1082                         delete pVisualElementSurface;
1083                         return EGL_NO_SURFACE;
1084                 }
1085
1086                 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1087                 delete pVisualElementSurface;
1088
1089                 Evas* pEvas = evas_object_evas_get(pObject);
1090                 pObject = evas_object_image_filled_add(pEvas);
1091                 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1092
1093                 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1094                 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
1095
1096                 evas_object_image_alpha_set(pObject, EINA_TRUE);
1097                 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1098                 evas_object_resize(pObject, widthPhysical, heightPhysical);
1099                 evas_object_hide(pObject);
1100
1101                 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1102                                 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1103                 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1104
1105                 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
1106                 SysTryReturn(NID_GRP, bitmap.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1107
1108                 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1109                 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1110
1111                 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmap.get(), null);
1112                 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1113
1114                 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1115                 pSglInfo->sglIndex = sglIndex;
1116                 pSglInfo->display = dpy;
1117                 pSglInfo->config = config;
1118                 pSglInfo->pVisualElement = pVisualElement;
1119                 pSglInfo->pWindow = pWindow;
1120                 pSglInfo->pObject = pObject;
1121                 pSglInfo->pVisualElementSurface = pVisualElementSurface;
1122                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1123
1124                 __evasEngineType = _GetEvasEngineType(pObject);
1125
1126 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1127                 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1128                 {
1129                         __isDoublePixmapEnabled = true;
1130                 }
1131                 else
1132                 {
1133                         __isDoublePixmapEnabled = false;
1134                 }
1135 #else
1136                 __isDoublePixmapEnabled = false;
1137 #endif
1138
1139                 if (__isDoublePixmapEnabled)
1140                 {
1141                         pObject = evas_object_image_filled_add(pEvas);
1142                         SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1143
1144                         evas_object_image_alpha_set(pObject, EINA_TRUE);
1145                         evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1146                         evas_object_resize(pObject, widthPhysical, heightPhysical);
1147                         evas_object_hide(pObject);
1148
1149                         pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1150                                         *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1151                         SysTryReturn(NID_GRP, pSglInfo->pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1152
1153                         delete pSglInfo->pBitmap;
1154                         pSglInfo->pBitmap = null;
1155
1156                         std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
1157                         SysTryReturn(NID_GRP, bitmapSecond.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1158
1159                         r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1160                         SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1161
1162                         sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmapSecond.get(), null);
1163                         SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1164
1165                         _SglInfo* pSglInfoSecond = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1166                         pSglInfoSecond->sglIndex = sglIndex;
1167                         pSglInfoSecond->display = dpy;
1168                         pSglInfoSecond->config = config;
1169                         pSglInfoSecond->pVisualElement = pVisualElement;
1170                         pSglInfoSecond->pWindow = pWindow;
1171                         pSglInfoSecond->pObject = pObject;
1172                         pSglInfoSecond->pVisualElementSurface = pVisualElementSurface;
1173                         pSglInfoSecond->swapDone = true;
1174                         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1175
1176                         pSglInfo->pSecondSglInfo = pSglInfoSecond;
1177                         pSglInfoSecond->pSecondSglInfo = pSglInfo;
1178
1179                         pSglInfoSecond->pBitmap = bitmapSecond.release();
1180                         _EvasObjectImageChange(pSglInfoSecond);
1181                 }
1182                 else
1183                 {
1184                         pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1185                 }
1186
1187                 pSglInfo->pBitmap = bitmap.release();
1188                 _EvasObjectImageChange(pSglInfo);
1189
1190                 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1191                 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1192 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1193                 _RegisterRenderCallback(pSglInfo->pObject);
1194 #endif
1195                 return (EGLSurface)pSglInfo->sglIndex;
1196         }
1197 }
1198
1199 EGLSurface
1200 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1201 {
1202         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1203         _SglIndex sglIndex = INVALID_SGL_INDEX;
1204
1205         sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1206         if (sglIndex <= INVALID_SGL_INDEX)
1207         {
1208                 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1209                                 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1210
1211                 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1212         }
1213
1214         EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1215         if (pbufferSurface == EGL_NO_SURFACE)
1216         {
1217                 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1218                 sglIndex = 0;
1219         }
1220         else
1221         {
1222                 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1223
1224                 pSglInfo->surface = pbufferSurface;
1225
1226                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1227         }
1228
1229         return (EGLSurface)sglIndex;
1230 }
1231
1232 _OSP_LOCAL_ EGLNativePixmapType
1233 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
1234 {
1235         int width = 0;
1236         int height = 0;
1237         int bitsPerPixel = 0;
1238         Display* pNativeDisplay = null;
1239         Drawable nativeWindow = 0;
1240         result r = E_FAILURE;
1241         Tizen::Graphics::BufferInfo bufferInfo;
1242         _PixmapInfo* pPixmapInfo = null;
1243 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1244         unsigned int attachments[] = { DRI2BufferFrontLeft };
1245         char* pDeviceName = null;
1246         char* pDriverName = null;
1247         DRI2Buffer* pDri2Buffer = null;
1248         int dri2Width = 0;
1249         int dri2Height = 0;
1250         int dri2BufferCount = 0;
1251         tbm_bo_handle tbmData = { null, };
1252
1253         Bool ret = False;
1254         drm_magic_t magic = 0;
1255 #endif
1256         _SglInfo* pSglInfo = null;
1257         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1258
1259         pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1260         if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1261         {
1262                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1263                 goto CATCH_01;
1264         }
1265         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1266
1267         r = pBitmap->Lock(bufferInfo);
1268         if (r != E_SUCCESS)
1269         {
1270                 goto CATCH_01;
1271         }
1272
1273         width = bufferInfo.width;
1274         height = bufferInfo.height;
1275         bitsPerPixel = bufferInfo.bitsPerPixel;
1276
1277         r = pBitmap->Unlock();
1278         if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1279         {
1280                 goto CATCH_01;
1281         }
1282
1283         pNativeDisplay = (Display*) ecore_x_display_get();
1284         nativeWindow = DefaultRootWindow(pNativeDisplay);
1285
1286         pPixmapInfo = new (std::nothrow) _PixmapInfo;
1287         if (pPixmapInfo == null)
1288         {
1289                 goto CATCH_01;
1290         }
1291
1292         pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height,
1293                         ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1294         if (pPixmapInfo->nativePixmap == (Pixmap)0)
1295         {
1296                 goto CATCH_02;
1297         }
1298
1299 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1300         DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1301         XSync(pNativeDisplay, False);
1302
1303         ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1304         free(pDriverName);
1305         if (!ret || pDeviceName == null)
1306         {
1307                 goto CATCH_03;
1308         }
1309
1310         pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1311
1312         free(pDeviceName);
1313         if (pPixmapInfo->drmFd < 0)
1314         {
1315                 goto CATCH_03;
1316         }
1317
1318         drmGetMagic(pPixmapInfo->drmFd, &magic);
1319         ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1320         if (!ret)
1321         {
1322                 goto CATCH_04;
1323         }
1324
1325         pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1326         if (pPixmapInfo->pTbmBufMgr == null)
1327         {
1328                 goto CATCH_04;
1329         }
1330
1331         pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1332         if (pDri2Buffer == null)
1333         {
1334                 goto CATCH_05;
1335         }
1336
1337         pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1338         free(pDri2Buffer);
1339         if (pPixmapInfo->pTbmBufferObject == null)
1340         {
1341                 goto CATCH_05;
1342         }
1343
1344         tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1345         if (tbmData.ptr == null)
1346         {
1347                 goto CATCH_06;
1348         }
1349
1350         r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1351                         _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1352         if (r != E_SUCCESS)
1353         {
1354                 goto CATCH_06;
1355         }
1356
1357         if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1358                         _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1359         {
1360                 goto CATCH_06;
1361         }
1362
1363 #else
1364 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1365         {
1366                 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1367
1368                 pSglInfo->pEcoreImage = ecore_x_image_new(width
1369                         , height
1370                         , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1371                         , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1372
1373                 if (pSglInfo->pEcoreImage == null)
1374                 {
1375                         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1376                         goto CATCH_06;
1377                 }
1378
1379                 ecore_x_image_get(pSglInfo->pEcoreImage
1380                         , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1381                         , width
1382                         , height);
1383
1384                 int bpl = 0;
1385                 int rows = 0;
1386                 int bpp = 0;
1387
1388                 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1389
1390                 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1391                 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1392                 if (r != E_SUCCESS)
1393                 {
1394                         ecore_x_image_free(pSglInfo->pEcoreImage);
1395                         pSglInfo->pEcoreImage = null;
1396                         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1397
1398                         SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1399                         goto CATCH_06;
1400                 }
1401
1402                 pSglInfo->width = width;
1403                 pSglInfo->height = height;
1404
1405                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1406         }
1407 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1408 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1409
1410         pBitmap->Lock(bufferInfo);
1411         memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1412         pBitmap->Unlock();
1413
1414         return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1415
1416 CATCH_06:
1417 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1418         tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1419         tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1420         //fall through
1421
1422 CATCH_05:
1423         tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1424         //fall through
1425
1426 CATCH_04:
1427         close(pPixmapInfo->drmFd);
1428         //fall through
1429
1430 CATCH_03:
1431         DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1432 #endif
1433         XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1434         //fall through
1435
1436 CATCH_02:
1437         delete pPixmapInfo;
1438         //fall through
1439
1440 CATCH_01:
1441         SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1442         return (EGLNativePixmapType)0;
1443 }
1444
1445 _OSP_LOCAL_ EGLNativePixmapType
1446 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
1447 {
1448         int width = 0;
1449         int height = 0;
1450         int bitsPerPixel = 0;
1451         Display* pNativeDisplay = null;
1452         Drawable nativeWindow = 0;
1453         Tizen::Graphics::_BitmapImpl* pBitmapImpl = null;
1454         _PixmapInfo* pPixmapInfo = null;
1455 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1456         unsigned int attachments[] = { DRI2BufferFrontLeft };
1457         char* pDeviceName = null;
1458         char* pDriverName = null;
1459         DRI2Buffer* pDri2Buffer = null;
1460         int dri2Width = 0;
1461         int dri2Height = 0;
1462         int dri2BufferCount = 0;
1463         tbm_bo_handle tbmData = { null, };
1464
1465         Bool ret = False;
1466         result r = E_FAILURE;
1467         drm_magic_t magic = 0;
1468 #endif
1469         if (pBitmap == null)
1470         {
1471                 goto CATCH_01;
1472         }
1473
1474         pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1475         if (pBitmapImpl == null)
1476         {
1477                 goto CATCH_01;
1478         }
1479
1480         width = bufferInfo.width;
1481         height = bufferInfo.height;
1482         bitsPerPixel = bufferInfo.bitsPerPixel;
1483
1484         if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1485         {
1486                 goto CATCH_01;
1487         }
1488
1489         pNativeDisplay = (Display*) ecore_x_display_get();
1490         nativeWindow = DefaultRootWindow(pNativeDisplay);
1491
1492         pPixmapInfo = new (std::nothrow) _PixmapInfo;
1493         if (pPixmapInfo == null)
1494         {
1495                 goto CATCH_01;
1496         }
1497
1498         pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1499                         , bitsPerPixel);
1500         if (pPixmapInfo->nativePixmap == (Pixmap)0)
1501         {
1502                 goto CATCH_02;
1503         }
1504
1505 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1506         DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1507         XSync(pNativeDisplay, False);
1508
1509         ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1510         free(pDriverName);
1511         if (!ret || pDeviceName == null)
1512         {
1513                 goto CATCH_03;
1514         }
1515
1516         pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1517         free(pDeviceName);
1518         if (pPixmapInfo->drmFd < 0)
1519         {
1520                 goto CATCH_03;
1521         }
1522
1523         drmGetMagic(pPixmapInfo->drmFd, &magic);
1524         ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1525         if (!ret)
1526         {
1527                 goto CATCH_04;
1528         }
1529
1530         pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1531         if (pPixmapInfo->pTbmBufMgr == null)
1532         {
1533                 goto CATCH_04;
1534         }
1535
1536         pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1537         if (pDri2Buffer == null)
1538         {
1539                 goto CATCH_05;
1540         }
1541
1542         pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1543         free(pDri2Buffer);
1544         if (pPixmapInfo->pTbmBufferObject == null)
1545         {
1546                 goto CATCH_05;
1547         }
1548
1549         tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1550         if (tbmData.ptr == null)
1551         {
1552                 goto CATCH_06;
1553         }
1554         bufferInfo.pPixels = tbmData.ptr;
1555
1556         bufferInfo.bitsPerPixel = 32;
1557         bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
1558         bufferInfo.pitch = bufferInfo.width * bufferInfo.bitsPerPixel / 8;
1559         memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1560         r = pBitmapImpl->Construct(bufferInfo);
1561         if (r != E_SUCCESS)
1562         {
1563                 goto CATCH_06;
1564         }
1565
1566         if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1567                         , _PixmapSurfaceDestroyCallback, pPixmapInfo
1568                         , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1569                         ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1570         {
1571                 goto CATCH_06;
1572         }
1573 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1574
1575         return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1576
1577 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1578 CATCH_06:
1579         tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1580         tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1581         //fall through
1582
1583 CATCH_05:
1584         tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1585         //fall through
1586
1587 CATCH_04:
1588         close(pPixmapInfo->drmFd);
1589         //fall through
1590
1591 CATCH_03:
1592         DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1593         XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1594         //fall through
1595 #endif
1596
1597 CATCH_02:
1598         delete pPixmapInfo;
1599         //fall through
1600
1601 CATCH_01:
1602         SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1603         return (EGLNativePixmapType)0;
1604 }
1605
1606 EGLSurface
1607 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
1608 {
1609         _SglIndex sglIndex = INVALID_SGL_INDEX;
1610         Tizen::Graphics::Bitmap* pBitmap = dynamic_cast<Tizen::Graphics::Bitmap*>((Tizen::Graphics::Bitmap*)pixmap);
1611         EGLNativePixmapType eglNativePixmap = 0;
1612         EGLSurface pixmapSurface = EGL_NO_SURFACE;
1613         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1614
1615         if (pBitmap == null)
1616         {
1617                 goto CATCH_01;
1618         }
1619
1620         sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1621         if (sglIndex <= INVALID_SGL_INDEX)
1622         {
1623                 goto CATCH_01;
1624         }
1625
1626         eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1627         if (eglNativePixmap == 0)
1628         {
1629                 goto CATCH_02;
1630         }
1631
1632         pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1633         if (pixmapSurface == EGL_NO_SURFACE)
1634         {
1635                 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1636                 sglIndex = 0;
1637         }
1638         else
1639         {
1640                 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1641
1642                 pSglInfo->nativePixmap = eglNativePixmap;
1643                 pSglInfo->surface = pixmapSurface;
1644
1645                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1646         }
1647
1648         return (EGLSurface)sglIndex;
1649
1650 CATCH_02:
1651         pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1652         //fall through
1653
1654 CATCH_01:
1655         SysLog(NID_GRP, "_SglCreatePixmapSurface failed!! dpy:%#x config:%#x pixmap:%#x pAttribList:%#x",
1656                         (unsigned int)dpy, (unsigned int)config, (unsigned int)pixmap, (unsigned int)pAttribList);
1657         return eglCreatePixmapSurface(EGL_NO_DISPLAY, config, (EGLNativePixmapType) 0, pAttribList);
1658 }
1659
1660 EGLBoolean
1661 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1662 {
1663         EGLBoolean ret = EGL_FALSE;
1664         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1665
1666         _SglIndex sglIndex = (_SglIndex)surface;
1667         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1668
1669         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1670         {
1671                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1672                 SysLog(NID_GRP, "_SglDestroySurface failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1673                                 , (unsigned int)surface);
1674                 return eglDestroySurface(dpy, EGL_NO_SURFACE);
1675         }
1676
1677 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1678         if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1679         {
1680                 __pPreviousSglInfo = null;
1681         }
1682 #endif
1683
1684         ret = eglDestroySurface(dpy, pSglInfo->surface);
1685         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1686
1687         if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1688         {
1689                 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1690                 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1691         }
1692
1693         pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1694
1695         return ret;
1696 }
1697
1698 EGLBoolean
1699 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1700 {
1701         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1702
1703         _SglIndex sglIndex = (_SglIndex)surface;
1704         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1705
1706         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1707         {
1708                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1709                 SysLog(NID_GRP, "_SglQuerySurface failed!! dpy:%#x sglIndex:%#x attribute:%#x pValue:%#x",
1710                         (unsigned int)dpy, (unsigned int)sglIndex, (unsigned int)attribute, (unsigned int)pValue);
1711                 return eglQuerySurface(dpy, EGL_NO_SURFACE, attribute, pValue);
1712         }
1713
1714         EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1715         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1716
1717         return ret;
1718 }
1719
1720 EGLBoolean
1721 _SglBindAPI(EGLenum api)
1722 {
1723         return eglBindAPI(api);
1724 }
1725
1726 EGLenum
1727 _SglQueryAPI()
1728 {
1729         return eglQueryAPI();
1730 }
1731
1732 EGLBoolean
1733 _SglWaitClient()
1734 {
1735         return eglWaitClient();
1736 }
1737
1738 EGLBoolean
1739 _SglReleaseThread(void)
1740 {
1741         return eglReleaseThread();
1742 }
1743
1744 EGLSurface
1745 _SglCreatePbufferFromClientBuffer(
1746                 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1747 {
1748         _SglIndex sglIndex = INVALID_SGL_INDEX;
1749         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1750
1751         sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1752         if (sglIndex <= INVALID_SGL_INDEX)
1753         {
1754                 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!!"
1755                         " dpy:%#x buftype:%#x buffer:%#x config:%#x pAttribList:%#x",
1756                         (unsigned int)dpy, (unsigned int)buftype, (unsigned int)buffer, (unsigned int)config, (unsigned int)pAttribList);
1757
1758                 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1759         }
1760
1761         EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1762         if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1763         {
1764                 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1765                 sglIndex = 0;
1766         }
1767         else
1768         {
1769                 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1770
1771                 pSglInfo->surface = pbufferFromClientBuffer;
1772
1773                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1774         }
1775
1776         return (EGLSurface)sglIndex;
1777 }
1778
1779 EGLBoolean
1780 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1781 {
1782         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1783
1784         _SglIndex sglIndex = (_SglIndex)surface;
1785         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1786
1787         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1788         {
1789                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1790                 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!! "
1791                         "dpy:%#x surface:%#x attribute:%#x value:%#x",
1792                         (unsigned int)dpy, (unsigned int)surface, (unsigned int)attribute, (unsigned int)value);
1793
1794                 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1795         }
1796
1797         EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1798         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1799
1800         return ret;
1801 }
1802
1803 EGLBoolean
1804 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1805 {
1806         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1807
1808         _SglIndex sglIndex = (_SglIndex)surface;
1809         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1810
1811         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1812         {
1813                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1814                 SysLog(NID_GRP, "_SglBindTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1815                         (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1816
1817                 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1818         }
1819
1820         EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1821         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1822
1823         return ret;
1824 }
1825
1826 EGLBoolean
1827 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1828 {
1829         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1830
1831         _SglIndex sglIndex = (_SglIndex)surface;
1832         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1833
1834         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1835         {
1836                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1837                 SysLog(NID_GRP, "_SglReleaseTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1838                         (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1839
1840                 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1841         }
1842
1843         EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1844         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1845
1846         return ret;
1847 }
1848
1849 EGLBoolean
1850 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1851 {
1852         return eglSwapInterval(dpy, interval);
1853 }
1854
1855 EGLContext
1856 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1857 {
1858         return eglCreateContext(dpy, config, shareContext, pAttribList);
1859 }
1860
1861 EGLBoolean
1862 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1863 {
1864         return eglDestroyContext(dpy, ctx);
1865 }
1866
1867 EGLBoolean
1868 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1869 {
1870         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1871
1872         _SglIndex sglIndexDraw = (_SglIndex)draw;
1873         _SglIndex sglIndexRead = (_SglIndex)read;
1874
1875         EGLSurface sglInfoReadSurface;
1876         {
1877                 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1878
1879                 if (sglInfoRead == null)
1880                 {
1881                         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1882                         SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1883                                 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1884                         return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1885                 }
1886                 sglInfoReadSurface = sglInfoRead->surface;
1887
1888                 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer && sglInfoRead->pSecondSglInfo != null)
1889                 {
1890                         sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1891                 }
1892
1893                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1894         }
1895
1896         _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1897         if (pSglInfoDraw == null)
1898         {
1899                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1900                 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1901                                 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1902                 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1903         }
1904
1905         if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1906         {
1907                 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1908         }
1909
1910         pSglInfoDraw->context = ctx;
1911
1912         EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1913
1914 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1915         if (draw != EGL_NO_SURFACE)
1916         {
1917                 _SaveCurrentContext(pSglInfoDraw);
1918         }
1919 #endif
1920
1921         if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1922         {
1923                 eglQueryContext(dpy, ctx,  EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1924         }
1925
1926         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1927
1928         return ret;
1929 }
1930
1931 EGLContext
1932 _SglGetCurrentContext()
1933 {
1934         return eglGetCurrentContext();
1935 }
1936
1937 EGLSurface
1938 _SglGetCurrentSurface(EGLint readDraw)
1939 {
1940         EGLSurface surface = EGL_NO_SURFACE;
1941         _SglIndex sglIndex = INVALID_SGL_INDEX;
1942
1943         surface = eglGetCurrentSurface(readDraw);
1944         sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1945
1946         if (sglIndex < INVALID_SGL_INDEX)
1947         {
1948                 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1949                 return EGL_NO_SURFACE;
1950         }
1951
1952         return (EGLSurface)sglIndex;
1953 }
1954
1955 EGLDisplay
1956 _SglGetCurrentDisplay(void)
1957 {
1958         return eglGetCurrentDisplay();
1959 }
1960
1961 EGLBoolean
1962 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1963 {
1964         return eglQueryContext(dpy, ctx, attribute, pValue);
1965 }
1966
1967 EGLBoolean
1968 _SglWaitGL(void)
1969 {
1970         return eglWaitGL();
1971 }
1972
1973 EGLBoolean
1974 _SglWaitNative(EGLint engine)
1975 {
1976         return eglWaitNative(engine);
1977 }
1978
1979 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
1980
1981 EGLBoolean
1982 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1983 {
1984         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1985
1986         _SglIndex sglIndex = (_SglIndex)surface;
1987         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1988
1989         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1990         {
1991                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1992                 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1993                                 , (unsigned int)sglIndex);
1994
1995                 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
1996         }
1997
1998         if (pSglInfo->pBitmap != null)
1999         {
2000 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2001                 _SglUpdateBufferOSP(dpy, surface);
2002 #else
2003                 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2004                 {
2005                         if (pSglInfo->glVersion == 1)
2006                         {
2007                                 _GlFlush_1();
2008                         }
2009                         else
2010                         {
2011                                 _GlFlush_2();
2012                         }
2013                 }
2014                 else
2015                 {
2016                         eglWaitGL();
2017                 }
2018 #endif
2019
2020                 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2021                 {
2022                         if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2023                         {
2024                                 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2025                                 {
2026                                         _SglInfo* pBackSglInfo = null;
2027                                         _SglInfo* pFrontSglInfo = null;
2028
2029                                         if (pSglInfo->isBackbuffer)
2030                                         {
2031                                                 pBackSglInfo = pSglInfo;
2032                                                 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2033                                         }
2034                                         else
2035                                         {
2036                                                 pBackSglInfo = pSglInfo->pSecondSglInfo;
2037                                                 pFrontSglInfo = pSglInfo;
2038                                         }
2039                                         pBackSglInfo->isBackbuffer = false;
2040                                         pFrontSglInfo->isBackbuffer = true;
2041
2042                                         pFrontSglInfo->wasShown = false;
2043                                         pFrontSglInfo->swapDone = false;
2044                                         if (pSglInfo->pVisualElement != null)
2045                                         {
2046                                                 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2047                                         }
2048                                         evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2049                                         evas_object_hide(pBackSglInfo->pObject);
2050
2051                                         if (pSglInfo->isFirstSwap)
2052                                         {
2053                                                 pBackSglInfo->isFirstSwap = false;
2054                                                 pFrontSglInfo->isFirstSwap = false;
2055
2056                                                 eglWaitGL();
2057                                                 BufferInfo bufferInfo;
2058                                                 pFrontSglInfo->pBitmap->Lock(bufferInfo);
2059                                                 pFrontSglInfo->pBitmap->Merge(Point(0, 0), *pBackSglInfo->pBitmap, Rectangle(0, 0, bufferInfo.width, bufferInfo.height));
2060                                                 pFrontSglInfo->pBitmap->Unlock();
2061                                         }
2062                                 }
2063                         }
2064                         else
2065                         {
2066                                 if (pSglInfo->pObject != null)
2067                                 {
2068                                         evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2069                                 }
2070                         }
2071                 }
2072                 else
2073                 {
2074                         if (pSglInfo->pVisualElement != null)
2075                         {
2076                                 pSglInfo->pVisualElement->SetFlushNeeded();
2077                         }
2078                 }
2079
2080
2081                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2082                 return EGL_TRUE;
2083         }
2084         else
2085         {
2086                 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2087                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2088
2089                 return ret;
2090         }
2091 }
2092
2093 EGLBoolean
2094 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2095 {
2096         Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2097         EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2098         return eglCopyBuffers(dpy, surface, nativePixmap);
2099 }
2100
2101 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2102
2103 __EglMustCastToProperFunctionPointerType
2104 _SglGetProcAddress(const char* pProcName)
2105 {
2106         return eglGetProcAddress(pProcName);
2107 }
2108
2109 EGLBoolean
2110 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2111 {
2112 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2113         Tizen::Graphics::BufferInfo bufferInfo;
2114         _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2115
2116         _SglIndex sglIndex = (_SglIndex)surface;
2117         _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2118
2119         if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2120         {
2121                 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2122                 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
2123                 return EGL_FALSE;
2124         }
2125
2126         eglCopyBuffers(dpy, pSglInfo->surface, pSglInfo->nativePixmap);
2127         ecore_x_image_get(pSglInfo->pEcoreImage, (Ecore_X_Drawable)pSglInfo->nativePixmap,
2128                 0, 0, 0, 0, pSglInfo->width, pSglInfo->height);
2129
2130         pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2131 #endif
2132         return EGL_TRUE;
2133 }
2134
2135 #ifdef __cplusplus
2136 }
2137 #endif
2138
2139 } // Opengl
2140
2141 }} // Tizen::Graphics