2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
20 * @brief This is the implementation file for egl.
31 #if defined(_OSP_EMUL_)
32 #define FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER
34 #define FGRAPHICS_INTERNAL_USE_DRM
37 #define FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING
38 #if defined(FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING)
42 #include <Ecore_Evas.h>
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>
53 #include <FUi_EcoreEvasMgr.h>
54 #include <FUi_EcoreEvas.h>
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>
70 #include <elm_config.h>
72 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
76 #include <tbm_bufmgr.h>
80 typedef int _SglIndex;
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);
88 #if defined(FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING)
89 void _GlesInterfaceTerminate_1();
90 void _GlesInterfaceTerminate_2();
93 #include <GLES2/gl2.h>
95 void _GlFlush_1(void);
96 void _GlFlush_2(void);
97 void _GlClear_1(GLbitfield mask);
98 void _GlClear_2(GLbitfield mask);
100 EGLBoolean _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
102 using namespace Tizen::Ui::Controls;
108 using namespace Tizen::Ui::Animations;
109 using namespace Tizen::Ui;
110 using namespace Tizen::Base;
112 namespace Tizen { namespace Graphics
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;
132 #if !defined (_OSP_EMUL_)
133 #define FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP
138 ENGINE_TYPE_SOFEWARE_X11 = 0,
139 ENGINE_TYPE_OPENGL_X11,
143 _engineType __evasEngineType = ENGINE_TYPE_SOFEWARE_X11;
144 bool __isDoublePixmapEnabled = false;
146 void _OnBoundsChanged(void* pData);
148 const int INVALID_SGL_INDEX = 0;
149 const int MAX_SGL_INDEX = 100;
155 : sglIndex(INVALID_SGL_INDEX)
156 , display(EGL_NO_DISPLAY)
157 , surface(EGL_NO_SURFACE)
158 , context(EGL_NO_CONTEXT)
159 , config((EGLConfig)0)
162 , nativePixmap((EGLNativePixmapType)0)
163 , pVisualElement(null)
164 , pVisualElementSurface(null)
167 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
176 , pSecondSglInfo(null)
180 virtual ~_SglInfo(void)
182 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
183 if (pEcoreImage != null)
185 ecore_x_image_free(pEcoreImage);
194 if (pVisualElement != null)
196 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
197 if (pVisualElementImpl != null)
199 pVisualElementImpl->SetDestroyedCallback(null, null);
203 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
204 _UnregisterRenderCallback(pObject);
206 evas_object_del(pObject);
207 delete pVisualElementSurface;
210 bool needAllocateObject = true;
211 if (__isDoublePixmapEnabled)
215 needAllocateObject = false;
218 if (needAllocateObject)
220 if (pVisualElement != null)
222 pVisualElement->SetSurface(null);
226 sglIndex = INVALID_SGL_INDEX;
227 display = EGL_NO_DISPLAY;
228 surface = EGL_NO_SURFACE;
229 context = EGL_NO_CONTEXT;
230 config = (EGLConfig)0;
233 nativePixmap = (EGLNativePixmapType)0;
234 pVisualElement = null;
235 pVisualElementSurface = null;
238 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
247 pSecondSglInfo = null;
257 Evas_Object* pObject;
258 EGLNativePixmapType nativePixmap;
259 VisualElement* pVisualElement;
260 VisualElementSurface* pVisualElementSurface;
262 Tizen::Ui::Window* pWindow;
263 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
264 Ecore_X_Image* pEcoreImage;
272 _SglInfo* pSecondSglInfo;
275 _SglInfo(const _SglInfo& sglInfo);
276 _SglInfo& operator =(const _SglInfo& rhs);
283 : nativePixmap((Pixmap)0)
284 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
286 , pTbmBufferObject(null)
292 virtual ~_PixmapInfo(void)
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);
303 tbm_bufmgr_deinit(pTbmBufMgr);
304 DRI2DestroyDrawable(pDisplay, nativePixmap);
306 XFreePixmap(pDisplay, nativePixmap);
308 nativePixmap = (Pixmap)0;
309 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
311 pTbmBufferObject = null;
318 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
319 tbm_bufmgr pTbmBufMgr;
320 tbm_bo pTbmBufferObject;
325 void _EvasObjectImageChange(_SglInfo* pSglInfo);
328 _OnBoundsChanged(void* pData)
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.");
337 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
339 __pPreviousSglInfo = null;
342 bool needSglMakeCurrent = false;
343 EGLDisplay eglDisplay = eglGetCurrentDisplay();
344 EGLSurface drawSurface = _SglGetCurrentSurface(EGL_DRAW);
345 EGLSurface readSurface = _SglGetCurrentSurface(EGL_READ);
346 EGLSurface eglContext = eglGetCurrentContext();
348 if (drawSurface != EGL_NO_SURFACE && readSurface != EGL_NO_SURFACE)
350 needSglMakeCurrent = true;
354 drawSurface = eglGetCurrentSurface(EGL_DRAW);
355 readSurface = eglGetCurrentSurface(EGL_READ);
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.");
363 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
364 if (pSglInfo->pEcoreImage != null)
366 ecore_x_image_free(pSglInfo->pEcoreImage);
367 pSglInfo->pEcoreImage = null;
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);
379 evas_object_image_size_set(pSglInfo->pObject, widthPhysical, heightPhysical);
380 evas_object_resize(pSglInfo->pObject, widthPhysical, heightPhysical);
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.");
393 pSglInfo->nativePixmap = nativePixmap;
394 pSglInfo->surface = eglSurface;
395 pSglInfo->isBackbuffer = true;
396 pSglInfo->wasShown = false;
397 pSglInfo->swapDone = false;
398 pSglInfo->isFirstSwap = true;
400 if (__isDoublePixmapEnabled)
402 _SglInfo* pSglInfoSecond = pSglInfo->pSecondSglInfo;
403 SysTryReturnVoidResult(NID_GRP, pSglInfoSecond != null, E_INVALID_STATE, "Invalid pSecondSglInfo.");
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.");
410 evas_object_image_size_set(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
411 evas_object_resize(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
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.");
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;
431 pSglInfo->isFirstSwap = true;
433 _EvasObjectImageChange(pSglInfoSecond);
436 pSglInfo->pBitmap = bitmap.release();
437 _EvasObjectImageChange(pSglInfo);
439 BufferInfo bufferInfo;
440 pSglInfo->pBitmap->Lock(bufferInfo);
441 pSglInfo->pBitmap->Unlock();
443 if (needSglMakeCurrent)
445 _SglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
449 eglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
456 _OnDestroyed(void* pData)
458 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
460 if (pSglInfo != null)
462 pSglInfo->pVisualElement = null;
464 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
466 pSglInfo->pSecondSglInfo->pVisualElement = null;
471 class _SglInfoTableManipulator
474 _SglInfoTableManipulator()
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;
482 ~_SglInfoTableManipulator()
487 FindNextEmptySlotOfSglInfoTable()
489 if (!__SglInitialized)
491 if (CreateSglInfoTable() == false)
493 return INVALID_SGL_INDEX;
497 _SglIndex sglIndex = INVALID_SGL_INDEX;
498 _SglIndex index = __SglIndexLastUsed;
500 __SglMutex.Acquire();
501 for (int i = 1; i < MAX_SGL_INDEX; i++)
505 if (index >= MAX_SGL_INDEX)
510 if (__SglInfoTable[index] == null)
513 __SglIndexLastUsed = index;
517 __SglMutex.Release();
520 if (sglIndex <= INVALID_SGL_INDEX)
522 return INVALID_SGL_INDEX;
533 __SglMutex.Acquire();
535 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
537 __SglInfoTable[i] = null;
540 __SglInfoTable[0] = new (std::nothrow) _SglInfo;
541 if (__SglInfoTable[0] != null)
544 __SglInitialized = true;
545 __SglInfoTable[0]->glVersion = 2;
548 __SglMutex.Release();
554 DestroySglInfoTable()
556 __SglMutex.Acquire();
558 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
560 if (__SglInfoTable[i] != null)
562 delete __SglInfoTable[i];
563 __SglInfoTable[i] = null;
566 __SglInitialized = false;
568 __SglMutex.Release();
572 LockSglInfoTable(_SglIndex index)
574 __SglMutex.Acquire();
576 if (index < INVALID_SGL_INDEX || index >= MAX_SGL_INDEX)
581 return __SglInfoTable[index];
587 __SglMutex.Release();
593 _SglInfo* newSglInfo = new (std::nothrow) _SglInfo;
594 if (newSglInfo == null)
596 SysLog(NID_GRP, "CreateSglIndex fail to allocate _SglInfo!!");
597 return INVALID_SGL_INDEX;
600 int sglIndex = FindNextEmptySlotOfSglInfoTable();
601 if (sglIndex == INVALID_SGL_INDEX)
605 SysLog(NID_GRP, "CreateSglIndex failed!!");
606 return INVALID_SGL_INDEX;
609 __SglMutex.Acquire();
610 __SglInfoTable[sglIndex] = newSglInfo;
611 __SglMutex.Release();
617 DestroySglIndex(_SglIndex sglIndex)
619 if (sglIndex < 1 || sglIndex >= MAX_SGL_INDEX)
621 SysLog(NID_GRP, "sglIndex:%#x is out of range.", (unsigned int)sglIndex);
625 __SglMutex.Acquire();
627 if (sglIndex <= INVALID_SGL_INDEX || __SglInfoTable[sglIndex] == null)
629 __SglMutex.Release();
631 SysLog(NID_GRP, "DestroySglIndex failed!! sglIndex:%#x", (unsigned int)sglIndex);
635 delete __SglInfoTable[sglIndex];
636 __SglInfoTable[sglIndex] = null;
638 __SglMutex.Release();
644 GetSglIndexForSurface(EGLSurface surface)
646 __SglMutex.Acquire();
649 for (int i = 0; i < MAX_SGL_INDEX; i++)
651 if (__SglInfoTable[i] != null && __SglInfoTable[i]->surface == surface)
658 __SglMutex.Release();
663 static _SglInfoTableManipulator* GetInstance(void)
665 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
669 pthread_once(&once_block, __InitSglInfoTableManipulator);
672 return __pTheInstance;
676 static void __InitSglInfoTableManipulator(void)
678 static _SglInfoTableManipulator instance;
679 __pTheInstance = &instance;
683 Runtime::Mutex __SglMutex;
684 _SglInfo* __SglInfoTable[MAX_SGL_INDEX];
685 _SglIndex __SglIndexLastUsed;
686 bool __SglInitialized;
688 static _SglInfoTableManipulator* __pTheInstance;
690 _SglInfoTableManipulator* _SglInfoTableManipulator::__pTheInstance = null;
693 _PixmapSurfaceDestroyCallback(void* pCallbackParam)
695 delete (_PixmapInfo*)pCallbackParam;
698 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
700 _PixmapLockCallBack(void* pCallbackParam)
702 tbm_bo_map((tbm_bo)pCallbackParam, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
706 _PixmapUnlockCallBack(void* pCallbackParam)
708 tbm_bo_unmap((tbm_bo)pCallbackParam);
713 _GetEvasEngineType(Evas_Object* pObject)
717 const char* pString = elm_config_preferred_engine_get();
718 String engineName(pString);
719 SysLog(NID_GRP, "evas backend : %s", pString);
721 if (engineName.Contains(String("opengl_x11")))
723 return ENGINE_TYPE_OPENGL_X11;
725 else if (engineName.Contains(String("software_x11")))
727 return ENGINE_TYPE_SOFEWARE_X11;
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")))
736 return ENGINE_TYPE_OPENGL_X11;
740 return ENGINE_TYPE_SOFEWARE_X11;
746 Evas* pEvas = evas_object_evas_get(pObject);
749 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
750 return ENGINE_TYPE_SOFEWARE_X11;
753 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
754 if (pEcoreEvas == null)
756 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEcoreEvas failed.");
757 return ENGINE_TYPE_SOFEWARE_X11;
760 const char* pString = ecore_evas_engine_name_get(pEcoreEvas);
761 String engineName(pString);
762 SysLog(NID_GRP, "evas backend : %s", pString);
764 if (engineName.Contains(String("opengl_x11")))
766 return ENGINE_TYPE_OPENGL_X11;
768 else if (engineName.Contains(String("software_x11")))
770 return ENGINE_TYPE_SOFEWARE_X11;
774 return ENGINE_TYPE_ETC;
780 _EvasObjectImageChange(_SglInfo* pSglInfo)
782 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
784 Evas* pEvas = evas_object_evas_get(pSglInfo->pObject);
787 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
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);
800 BufferInfo bufferInfo;
801 pSglInfo->pBitmap->Lock(bufferInfo);
802 evas_object_image_data_set(pSglInfo->pObject, bufferInfo.pPixels);
803 pSglInfo->pBitmap->Unlock();
807 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
809 _PostRenderCallback(Ecore_Evas* ee)
811 if (__pPreviousSglInfo != null)
813 EGLDisplay display = __pPreviousSglInfo->display;
814 EGLSurface surface = __pPreviousSglInfo->surface;
815 EGLContext context = __pPreviousSglInfo->context;
817 if (__isDoublePixmapEnabled)
819 _SglInfo* pBackSglInfo = null;
820 _SglInfo* pFrontSglInfo = null;
822 if (__pPreviousSglInfo->isBackbuffer)
824 pBackSglInfo = __pPreviousSglInfo;
825 pFrontSglInfo = __pPreviousSglInfo->pSecondSglInfo;
829 pBackSglInfo = __pPreviousSglInfo->pSecondSglInfo;
830 pFrontSglInfo = __pPreviousSglInfo;
833 if (!pBackSglInfo->wasShown && __pPreviousSglInfo->pVisualElement != null)
835 __pPreviousSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
836 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
837 evas_object_hide(pBackSglInfo->pObject);
839 pBackSglInfo->wasShown = true;
840 pFrontSglInfo->swapDone = true;
843 surface = pBackSglInfo->surface;
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());
857 _SaveCurrentContext(_SglInfo* pSglInfo)
859 __pPreviousSglInfo = pSglInfo;
863 _RegisterRenderCallback(Evas_Object* pObject)
865 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
867 if (__registerCallbackCount == 0)
869 Evas* pEvas = evas_object_evas_get(pObject);
875 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
876 if (pEcoreEvas == null)
881 ecore_evas_callback_post_render_set(pEcoreEvas, _PostRenderCallback);
883 __registerCallbackCount++;
888 _UnregisterRenderCallback(Evas_Object* pObject)
890 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
892 __registerCallbackCount--;
894 if (__registerCallbackCount == 0)
896 Evas* pEvas = evas_object_evas_get(pObject);
902 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
903 if (pEcoreEvas == null)
908 ecore_evas_callback_post_render_set(pEcoreEvas, NULL);
912 #endif //#if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
924 return eglGetError();
928 _SglGetDisplay(EGLNativeDisplayType displayId)
930 if (displayId == (EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)
932 return eglGetDisplay((EGLNativeDisplayType) ecore_x_display_get());
935 return eglGetDisplay((EGLNativeDisplayType) displayId);
939 _SglInitialize(EGLDisplay dpy, EGLint* pMajor, EGLint* pMinor)
941 __evasEngineType = _GetEvasEngineType(null);
942 return eglInitialize(dpy, pMajor, pMinor);
946 _SglTerminate(EGLDisplay dpy)
948 _GlesInterfaceTerminate_1();
949 _GlesInterfaceTerminate_2();
951 _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
953 if (__evasEngineType != ENGINE_TYPE_OPENGL_X11)
958 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
959 __registerCallbackCount = 0;
960 __pPreviousSglInfo = null;
968 _SglQueryString(EGLDisplay dpy, EGLint name)
970 return eglQueryString(dpy, name);
974 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
976 return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
980 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
982 return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
986 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
988 return eglGetConfigAttrib(dpy, config, attribute, pValue);
992 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
994 _SglIndex sglIndex = INVALID_SGL_INDEX;
995 Object* pObj = (Object*)win;
996 VisualElement* pVisualElement = dynamic_cast<VisualElement*> (pObj);
997 Tizen::Ui::Control* pControl = dynamic_cast<Tizen::Ui::Control*> (pObj);
998 #if 0//!defined(_OSP_EMUL_)
999 Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
1001 SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1002 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1004 #if 0//!defined(_OSP_EMUL_)
1005 if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
1007 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(*pControl);
1008 _WindowImpl* pWindowImpl = dynamic_cast<_WindowImpl*> (pControlImpl);
1009 SysTryReturn(NID_GRP, pWindowImpl != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window state.");
1011 EGLNativeWindowType nativeWindow = (EGLNativeWindowType)pWindowImpl->GetNativeHandle();
1012 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1013 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid SglIndex.");
1015 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1016 if (windowSurface == EGL_NO_SURFACE)
1018 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1023 VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1024 if (pVisualElement == null)
1026 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1027 return EGL_NO_SURFACE;
1030 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1031 if (pVisualElementImpl == null)
1033 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1034 return EGL_NO_SURFACE;
1037 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1039 pSglInfo->surface = windowSurface;
1041 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1044 return (EGLSurface)sglIndex;
1047 #endif //#if !defined(_OSP_EMUL_)
1049 result r = E_SUCCESS;
1051 if (pVisualElement == null)
1053 pVisualElement = pControl->GetVisualElement();
1055 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1057 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1058 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1060 FloatRectangle rect = pVisualElement->GetBounds();
1061 int width = int(rect.width);
1062 int height = int(rect.height);
1063 SysTryReturn(NID_GRP, width > 0 && height > 0, EGL_NO_SURFACE, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
1065 Control* pFrame = pControl;
1066 Control* pTemp = pFrame->GetParent();
1067 while(pTemp != null)
1070 pTemp = pTemp->GetParent();
1073 Tizen::Ui::Window* pWindow = dynamic_cast<Tizen::Ui::Window*> (pFrame);
1074 SysTryReturn(NID_GRP, pWindow != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window.");
1076 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1077 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1079 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1080 if (pVisualElementSurfaceImpl == null)
1082 SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1083 delete pVisualElementSurface;
1084 return EGL_NO_SURFACE;
1087 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1088 delete pVisualElementSurface;
1090 Evas* pEvas = evas_object_evas_get(pObject);
1091 pObject = evas_object_image_filled_add(pEvas);
1092 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1094 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1095 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
1097 evas_object_image_alpha_set(pObject, EINA_TRUE);
1098 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1099 evas_object_resize(pObject, widthPhysical, heightPhysical);
1100 evas_object_hide(pObject);
1102 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1103 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1104 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1106 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
1107 SysTryReturn(NID_GRP, bitmap.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1109 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1110 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1112 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmap.get(), null);
1113 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1115 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1116 pSglInfo->sglIndex = sglIndex;
1117 pSglInfo->display = dpy;
1118 pSglInfo->config = config;
1119 pSglInfo->pVisualElement = pVisualElement;
1120 pSglInfo->pWindow = pWindow;
1121 pSglInfo->pObject = pObject;
1122 pSglInfo->pVisualElementSurface = pVisualElementSurface;
1123 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1125 __evasEngineType = _GetEvasEngineType(pObject);
1127 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1128 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1130 __isDoublePixmapEnabled = true;
1134 __isDoublePixmapEnabled = false;
1137 __isDoublePixmapEnabled = false;
1140 if (__isDoublePixmapEnabled)
1142 pObject = evas_object_image_filled_add(pEvas);
1143 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1145 evas_object_image_alpha_set(pObject, EINA_TRUE);
1146 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1147 evas_object_resize(pObject, widthPhysical, heightPhysical);
1148 evas_object_hide(pObject);
1150 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1151 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1152 SysTryReturn(NID_GRP, pSglInfo->pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1154 delete pSglInfo->pBitmap;
1155 pSglInfo->pBitmap = null;
1157 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
1158 SysTryReturn(NID_GRP, bitmapSecond.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1160 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1161 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1163 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmapSecond.get(), null);
1164 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1166 _SglInfo* pSglInfoSecond = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1167 pSglInfoSecond->sglIndex = sglIndex;
1168 pSglInfoSecond->display = dpy;
1169 pSglInfoSecond->config = config;
1170 pSglInfoSecond->pVisualElement = pVisualElement;
1171 pSglInfoSecond->pWindow = pWindow;
1172 pSglInfoSecond->pObject = pObject;
1173 pSglInfoSecond->pVisualElementSurface = pVisualElementSurface;
1174 pSglInfoSecond->swapDone = true;
1175 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1177 pSglInfo->pSecondSglInfo = pSglInfoSecond;
1178 pSglInfoSecond->pSecondSglInfo = pSglInfo;
1180 pSglInfoSecond->pBitmap = bitmapSecond.release();
1181 _EvasObjectImageChange(pSglInfoSecond);
1185 pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1188 pSglInfo->pBitmap = bitmap.release();
1189 _EvasObjectImageChange(pSglInfo);
1191 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1192 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1193 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1194 _RegisterRenderCallback(pSglInfo->pObject);
1196 return (EGLSurface)pSglInfo->sglIndex;
1201 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1203 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1204 _SglIndex sglIndex = INVALID_SGL_INDEX;
1206 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1207 if (sglIndex <= INVALID_SGL_INDEX)
1209 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1210 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1212 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1215 EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1216 if (pbufferSurface == EGL_NO_SURFACE)
1218 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1223 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1225 pSglInfo->surface = pbufferSurface;
1227 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1230 return (EGLSurface)sglIndex;
1233 _OSP_LOCAL_ EGLNativePixmapType
1234 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
1238 int bitsPerPixel = 0;
1239 Display* pNativeDisplay = null;
1240 Drawable nativeWindow = 0;
1241 result r = E_FAILURE;
1242 Tizen::Graphics::BufferInfo bufferInfo;
1243 _PixmapInfo* pPixmapInfo = null;
1244 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1245 unsigned int attachments[] = { DRI2BufferFrontLeft };
1246 char* pDeviceName = null;
1247 char* pDriverName = null;
1248 DRI2Buffer* pDri2Buffer = null;
1251 int dri2BufferCount = 0;
1252 tbm_bo_handle tbmData = { null, };
1255 drm_magic_t magic = 0;
1257 _SglInfo* pSglInfo = null;
1258 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1260 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1261 if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1263 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1266 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1268 r = pBitmap->Lock(bufferInfo);
1274 width = bufferInfo.width;
1275 height = bufferInfo.height;
1276 bitsPerPixel = bufferInfo.bitsPerPixel;
1278 r = pBitmap->Unlock();
1279 if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1284 pNativeDisplay = (Display*) ecore_x_display_get();
1285 nativeWindow = DefaultRootWindow(pNativeDisplay);
1287 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1288 if (pPixmapInfo == null)
1293 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height,
1294 ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1295 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1300 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1301 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1302 XSync(pNativeDisplay, False);
1304 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1306 if (!ret || pDeviceName == null)
1311 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1314 if (pPixmapInfo->drmFd < 0)
1319 drmGetMagic(pPixmapInfo->drmFd, &magic);
1320 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1326 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1327 if (pPixmapInfo->pTbmBufMgr == null)
1332 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1333 if (pDri2Buffer == null)
1338 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1340 if (pPixmapInfo->pTbmBufferObject == null)
1345 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1346 if (tbmData.ptr == null)
1351 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1352 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1358 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1359 _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1365 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1367 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1369 pSglInfo->pEcoreImage = ecore_x_image_new(width
1371 , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1372 , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1374 if (pSglInfo->pEcoreImage == null)
1376 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1380 ecore_x_image_get(pSglInfo->pEcoreImage
1381 , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1389 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1391 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1392 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1395 ecore_x_image_free(pSglInfo->pEcoreImage);
1396 pSglInfo->pEcoreImage = null;
1397 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1399 SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1403 pSglInfo->width = width;
1404 pSglInfo->height = height;
1406 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1408 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1409 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1411 pBitmap->Lock(bufferInfo);
1412 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1415 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1418 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1419 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1420 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1424 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1428 close(pPixmapInfo->drmFd);
1432 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1434 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1442 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1443 return (EGLNativePixmapType)0;
1446 _OSP_LOCAL_ EGLNativePixmapType
1447 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
1451 int bitsPerPixel = 0;
1452 Display* pNativeDisplay = null;
1453 Drawable nativeWindow = 0;
1454 Tizen::Graphics::_BitmapImpl* pBitmapImpl = null;
1455 _PixmapInfo* pPixmapInfo = null;
1456 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1457 unsigned int attachments[] = { DRI2BufferFrontLeft };
1458 char* pDeviceName = null;
1459 char* pDriverName = null;
1460 DRI2Buffer* pDri2Buffer = null;
1463 int dri2BufferCount = 0;
1464 tbm_bo_handle tbmData = { null, };
1467 result r = E_FAILURE;
1468 drm_magic_t magic = 0;
1470 if (pBitmap == null)
1475 pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1476 if (pBitmapImpl == null)
1481 width = bufferInfo.width;
1482 height = bufferInfo.height;
1483 bitsPerPixel = bufferInfo.bitsPerPixel;
1485 if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1490 pNativeDisplay = (Display*) ecore_x_display_get();
1491 nativeWindow = DefaultRootWindow(pNativeDisplay);
1493 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1494 if (pPixmapInfo == null)
1499 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1501 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1506 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1507 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1508 XSync(pNativeDisplay, False);
1510 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1512 if (!ret || pDeviceName == null)
1517 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1519 if (pPixmapInfo->drmFd < 0)
1524 drmGetMagic(pPixmapInfo->drmFd, &magic);
1525 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1531 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1532 if (pPixmapInfo->pTbmBufMgr == null)
1537 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1538 if (pDri2Buffer == null)
1543 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1545 if (pPixmapInfo->pTbmBufferObject == null)
1550 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1551 if (tbmData.ptr == null)
1555 bufferInfo.pPixels = tbmData.ptr;
1557 bufferInfo.bitsPerPixel = 32;
1558 bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
1559 bufferInfo.pitch = bufferInfo.width * bufferInfo.bitsPerPixel / 8;
1560 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1561 r = pBitmapImpl->Construct(bufferInfo);
1567 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1568 , _PixmapSurfaceDestroyCallback, pPixmapInfo
1569 , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1570 ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1574 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1576 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1578 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1580 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1581 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1585 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1589 close(pPixmapInfo->drmFd);
1593 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1594 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1603 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1604 return (EGLNativePixmapType)0;
1608 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
1610 _SglIndex sglIndex = INVALID_SGL_INDEX;
1611 Tizen::Graphics::Bitmap* pBitmap = dynamic_cast<Tizen::Graphics::Bitmap*>((Tizen::Graphics::Bitmap*)pixmap);
1612 EGLNativePixmapType eglNativePixmap = 0;
1613 EGLSurface pixmapSurface = EGL_NO_SURFACE;
1614 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1616 if (pBitmap == null)
1621 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1622 if (sglIndex <= INVALID_SGL_INDEX)
1627 eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1628 if (eglNativePixmap == 0)
1633 pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1634 if (pixmapSurface == EGL_NO_SURFACE)
1636 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1641 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1643 pSglInfo->nativePixmap = eglNativePixmap;
1644 pSglInfo->surface = pixmapSurface;
1646 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1649 return (EGLSurface)sglIndex;
1652 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1656 SysLog(NID_GRP, "_SglCreatePixmapSurface failed!! dpy:%#x config:%#x pixmap:%#x pAttribList:%#x",
1657 (unsigned int)dpy, (unsigned int)config, (unsigned int)pixmap, (unsigned int)pAttribList);
1658 return eglCreatePixmapSurface(EGL_NO_DISPLAY, config, (EGLNativePixmapType) 0, pAttribList);
1662 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1664 EGLBoolean ret = EGL_FALSE;
1665 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1667 _SglIndex sglIndex = (_SglIndex)surface;
1668 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1670 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1672 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1673 SysLog(NID_GRP, "_SglDestroySurface failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1674 , (unsigned int)surface);
1675 return eglDestroySurface(dpy, EGL_NO_SURFACE);
1678 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1679 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1681 __pPreviousSglInfo = null;
1685 ret = eglDestroySurface(dpy, pSglInfo->surface);
1686 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1688 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1690 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1691 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1694 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1700 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1702 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1704 _SglIndex sglIndex = (_SglIndex)surface;
1705 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1707 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1709 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1710 SysLog(NID_GRP, "_SglQuerySurface failed!! dpy:%#x sglIndex:%#x attribute:%#x pValue:%#x",
1711 (unsigned int)dpy, (unsigned int)sglIndex, (unsigned int)attribute, (unsigned int)pValue);
1712 return eglQuerySurface(dpy, EGL_NO_SURFACE, attribute, pValue);
1715 EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1716 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1722 _SglBindAPI(EGLenum api)
1724 return eglBindAPI(api);
1730 return eglQueryAPI();
1736 return eglWaitClient();
1740 _SglReleaseThread(void)
1742 return eglReleaseThread();
1746 _SglCreatePbufferFromClientBuffer(
1747 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1749 _SglIndex sglIndex = INVALID_SGL_INDEX;
1750 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1752 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1753 if (sglIndex <= INVALID_SGL_INDEX)
1755 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!!"
1756 " dpy:%#x buftype:%#x buffer:%#x config:%#x pAttribList:%#x",
1757 (unsigned int)dpy, (unsigned int)buftype, (unsigned int)buffer, (unsigned int)config, (unsigned int)pAttribList);
1759 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1762 EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1763 if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1765 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1770 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1772 pSglInfo->surface = pbufferFromClientBuffer;
1774 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1777 return (EGLSurface)sglIndex;
1781 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1783 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1785 _SglIndex sglIndex = (_SglIndex)surface;
1786 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1788 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1790 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1791 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!! "
1792 "dpy:%#x surface:%#x attribute:%#x value:%#x",
1793 (unsigned int)dpy, (unsigned int)surface, (unsigned int)attribute, (unsigned int)value);
1795 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1798 EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1799 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1805 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1807 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1809 _SglIndex sglIndex = (_SglIndex)surface;
1810 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1812 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1814 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1815 SysLog(NID_GRP, "_SglBindTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1816 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1818 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1821 EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1822 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1828 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1830 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1832 _SglIndex sglIndex = (_SglIndex)surface;
1833 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1835 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1837 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1838 SysLog(NID_GRP, "_SglReleaseTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1839 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1841 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1844 EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1845 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1851 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1853 return eglSwapInterval(dpy, interval);
1857 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1859 return eglCreateContext(dpy, config, shareContext, pAttribList);
1863 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1865 return eglDestroyContext(dpy, ctx);
1869 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1871 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1873 _SglIndex sglIndexDraw = (_SglIndex)draw;
1874 _SglIndex sglIndexRead = (_SglIndex)read;
1876 EGLSurface sglInfoReadSurface;
1878 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1880 if (sglInfoRead == null)
1882 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1883 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1884 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1885 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1887 sglInfoReadSurface = sglInfoRead->surface;
1889 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer && sglInfoRead->pSecondSglInfo != null)
1891 sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1894 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1897 _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1898 if (pSglInfoDraw == null)
1900 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1901 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1902 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1903 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1906 if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1908 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1911 pSglInfoDraw->context = ctx;
1913 EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1915 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1916 if (draw != EGL_NO_SURFACE)
1918 _SaveCurrentContext(pSglInfoDraw);
1922 if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1924 eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1927 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1933 _SglGetCurrentContext()
1935 return eglGetCurrentContext();
1939 _SglGetCurrentSurface(EGLint readDraw)
1941 EGLSurface surface = EGL_NO_SURFACE;
1942 _SglIndex sglIndex = INVALID_SGL_INDEX;
1944 surface = eglGetCurrentSurface(readDraw);
1945 sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1947 if (sglIndex < INVALID_SGL_INDEX)
1949 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1950 return EGL_NO_SURFACE;
1953 return (EGLSurface)sglIndex;
1957 _SglGetCurrentDisplay(void)
1959 return eglGetCurrentDisplay();
1963 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1965 return eglQueryContext(dpy, ctx, attribute, pValue);
1975 _SglWaitNative(EGLint engine)
1977 return eglWaitNative(engine);
1980 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
1983 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1985 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1987 _SglIndex sglIndex = (_SglIndex)surface;
1988 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1990 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1992 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1993 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1994 , (unsigned int)sglIndex);
1996 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
1999 if (pSglInfo->pBitmap != null)
2001 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2002 _SglUpdateBufferOSP(dpy, surface);
2004 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2006 if (pSglInfo->glVersion == 1)
2021 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2023 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2025 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2027 _SglInfo* pBackSglInfo = null;
2028 _SglInfo* pFrontSglInfo = null;
2030 if (pSglInfo->isBackbuffer)
2032 pBackSglInfo = pSglInfo;
2033 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2037 pBackSglInfo = pSglInfo->pSecondSglInfo;
2038 pFrontSglInfo = pSglInfo;
2040 pBackSglInfo->isBackbuffer = false;
2041 pFrontSglInfo->isBackbuffer = true;
2043 pFrontSglInfo->wasShown = false;
2044 pFrontSglInfo->swapDone = false;
2045 if (pSglInfo->pVisualElement != null)
2047 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2049 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2050 evas_object_hide(pBackSglInfo->pObject);
2052 if (pSglInfo->isFirstSwap)
2054 pBackSglInfo->isFirstSwap = false;
2055 pFrontSglInfo->isFirstSwap = false;
2058 BufferInfo bufferInfo;
2059 pFrontSglInfo->pBitmap->Lock(bufferInfo);
2060 pFrontSglInfo->pBitmap->Merge(Point(0, 0), *pBackSglInfo->pBitmap, Rectangle(0, 0, bufferInfo.width, bufferInfo.height));
2061 pFrontSglInfo->pBitmap->Unlock();
2067 if (pSglInfo->pObject != null)
2069 evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2075 if (pSglInfo->pVisualElement != null)
2077 pSglInfo->pVisualElement->SetFlushNeeded();
2082 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2087 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2088 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2095 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2097 Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2098 EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2099 return eglCopyBuffers(dpy, surface, nativePixmap);
2102 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2104 __EglMustCastToProperFunctionPointerType
2105 _SglGetProcAddress(const char* pProcName)
2107 return eglGetProcAddress(pProcName);
2111 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2113 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2114 Tizen::Graphics::BufferInfo bufferInfo;
2115 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2117 _SglIndex sglIndex = (_SglIndex)surface;
2118 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2120 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2122 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2123 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
2127 eglCopyBuffers(dpy, pSglInfo->surface, pSglInfo->nativePixmap);
2128 ecore_x_image_get(pSglInfo->pEcoreImage, (Ecore_X_Drawable)pSglInfo->nativePixmap,
2129 0, 0, 0, 0, pSglInfo->width, pSglInfo->height);
2131 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2142 }} // Tizen::Graphics