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 return eglInitialize(dpy, pMajor, pMinor);
945 _SglTerminate(EGLDisplay dpy)
947 _GlesInterfaceTerminate_1();
948 _GlesInterfaceTerminate_2();
950 _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
952 if (!__isDoublePixmapEnabled)
957 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
958 __registerCallbackCount = 0;
959 __pPreviousSglInfo = null;
967 _SglQueryString(EGLDisplay dpy, EGLint name)
969 return eglQueryString(dpy, name);
973 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
975 return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
979 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
981 return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
985 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
987 return eglGetConfigAttrib(dpy, config, attribute, pValue);
991 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
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);
1000 SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1001 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1003 #if 0//!defined(_OSP_EMUL_)
1004 if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
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.");
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.");
1014 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1015 if (windowSurface == EGL_NO_SURFACE)
1017 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1022 VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1023 if (pVisualElement == null)
1025 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1026 return EGL_NO_SURFACE;
1029 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1030 if (pVisualElementImpl == null)
1032 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1033 return EGL_NO_SURFACE;
1036 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1038 pSglInfo->surface = windowSurface;
1040 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1043 return (EGLSurface)sglIndex;
1046 #endif //#if !defined(_OSP_EMUL_)
1048 result r = E_SUCCESS;
1050 if (pVisualElement == null)
1052 pVisualElement = pControl->GetVisualElement();
1054 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1056 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1057 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
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);
1064 Control* pFrame = pControl;
1065 Control* pTemp = pFrame->GetParent();
1066 while(pTemp != null)
1069 pTemp = pTemp->GetParent();
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.");
1075 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1076 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1078 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1079 if (pVisualElementSurfaceImpl == null)
1081 SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1082 delete pVisualElementSurface;
1083 return EGL_NO_SURFACE;
1086 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1087 delete pVisualElementSurface;
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.");
1093 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1094 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
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);
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.");
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.");
1108 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1109 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
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.");
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();
1124 __evasEngineType = _GetEvasEngineType(pObject);
1126 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1127 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1129 __isDoublePixmapEnabled = true;
1133 __isDoublePixmapEnabled = false;
1136 __isDoublePixmapEnabled = false;
1139 if (__isDoublePixmapEnabled)
1141 pObject = evas_object_image_filled_add(pEvas);
1142 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
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);
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.");
1153 delete pSglInfo->pBitmap;
1154 pSglInfo->pBitmap = null;
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.");
1159 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1160 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
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.");
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();
1176 pSglInfo->pSecondSglInfo = pSglInfoSecond;
1177 pSglInfoSecond->pSecondSglInfo = pSglInfo;
1179 pSglInfoSecond->pBitmap = bitmapSecond.release();
1180 _EvasObjectImageChange(pSglInfoSecond);
1184 pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1187 pSglInfo->pBitmap = bitmap.release();
1188 _EvasObjectImageChange(pSglInfo);
1190 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1191 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1192 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1193 _RegisterRenderCallback(pSglInfo->pObject);
1195 return (EGLSurface)pSglInfo->sglIndex;
1200 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1202 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1203 _SglIndex sglIndex = INVALID_SGL_INDEX;
1205 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1206 if (sglIndex <= INVALID_SGL_INDEX)
1208 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1209 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1211 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1214 EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1215 if (pbufferSurface == EGL_NO_SURFACE)
1217 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1222 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1224 pSglInfo->surface = pbufferSurface;
1226 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1229 return (EGLSurface)sglIndex;
1232 _OSP_LOCAL_ EGLNativePixmapType
1233 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
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;
1250 int dri2BufferCount = 0;
1251 tbm_bo_handle tbmData = { null, };
1254 drm_magic_t magic = 0;
1256 _SglInfo* pSglInfo = null;
1257 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1259 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1260 if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1262 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1265 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1267 r = pBitmap->Lock(bufferInfo);
1273 width = bufferInfo.width;
1274 height = bufferInfo.height;
1275 bitsPerPixel = bufferInfo.bitsPerPixel;
1277 r = pBitmap->Unlock();
1278 if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1283 pNativeDisplay = (Display*) ecore_x_display_get();
1284 nativeWindow = DefaultRootWindow(pNativeDisplay);
1286 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1287 if (pPixmapInfo == null)
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)
1299 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1300 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1301 XSync(pNativeDisplay, False);
1303 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1305 if (!ret || pDeviceName == null)
1310 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1313 if (pPixmapInfo->drmFd < 0)
1318 drmGetMagic(pPixmapInfo->drmFd, &magic);
1319 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1325 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1326 if (pPixmapInfo->pTbmBufMgr == null)
1331 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1332 if (pDri2Buffer == null)
1337 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1339 if (pPixmapInfo->pTbmBufferObject == null)
1344 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1345 if (tbmData.ptr == null)
1350 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1351 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1357 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1358 _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1364 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1366 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1368 pSglInfo->pEcoreImage = ecore_x_image_new(width
1370 , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1371 , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1373 if (pSglInfo->pEcoreImage == null)
1375 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1379 ecore_x_image_get(pSglInfo->pEcoreImage
1380 , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1388 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1390 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1391 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1394 ecore_x_image_free(pSglInfo->pEcoreImage);
1395 pSglInfo->pEcoreImage = null;
1396 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1398 SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1402 pSglInfo->width = width;
1403 pSglInfo->height = height;
1405 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1407 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1408 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1410 pBitmap->Lock(bufferInfo);
1411 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1414 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1417 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1418 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1419 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1423 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1427 close(pPixmapInfo->drmFd);
1431 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1433 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1441 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1442 return (EGLNativePixmapType)0;
1445 _OSP_LOCAL_ EGLNativePixmapType
1446 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
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;
1462 int dri2BufferCount = 0;
1463 tbm_bo_handle tbmData = { null, };
1466 result r = E_FAILURE;
1467 drm_magic_t magic = 0;
1469 if (pBitmap == null)
1474 pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1475 if (pBitmapImpl == null)
1480 width = bufferInfo.width;
1481 height = bufferInfo.height;
1482 bitsPerPixel = bufferInfo.bitsPerPixel;
1484 if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1489 pNativeDisplay = (Display*) ecore_x_display_get();
1490 nativeWindow = DefaultRootWindow(pNativeDisplay);
1492 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1493 if (pPixmapInfo == null)
1498 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1500 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1505 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1506 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1507 XSync(pNativeDisplay, False);
1509 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1511 if (!ret || pDeviceName == null)
1516 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1518 if (pPixmapInfo->drmFd < 0)
1523 drmGetMagic(pPixmapInfo->drmFd, &magic);
1524 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1530 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1531 if (pPixmapInfo->pTbmBufMgr == null)
1536 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1537 if (pDri2Buffer == null)
1542 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1544 if (pPixmapInfo->pTbmBufferObject == null)
1549 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1550 if (tbmData.ptr == null)
1554 bufferInfo.pPixels = tbmData.ptr;
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);
1566 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1567 , _PixmapSurfaceDestroyCallback, pPixmapInfo
1568 , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1569 ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1573 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1575 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1577 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1579 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1580 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1584 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1588 close(pPixmapInfo->drmFd);
1592 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1593 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1602 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1603 return (EGLNativePixmapType)0;
1607 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
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();
1615 if (pBitmap == null)
1620 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1621 if (sglIndex <= INVALID_SGL_INDEX)
1626 eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1627 if (eglNativePixmap == 0)
1632 pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1633 if (pixmapSurface == EGL_NO_SURFACE)
1635 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1640 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1642 pSglInfo->nativePixmap = eglNativePixmap;
1643 pSglInfo->surface = pixmapSurface;
1645 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1648 return (EGLSurface)sglIndex;
1651 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
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);
1661 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1663 EGLBoolean ret = EGL_FALSE;
1664 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1666 _SglIndex sglIndex = (_SglIndex)surface;
1667 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1669 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
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);
1677 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1678 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1680 __pPreviousSglInfo = null;
1684 ret = eglDestroySurface(dpy, pSglInfo->surface);
1685 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1687 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1689 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1690 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1693 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1699 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1701 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1703 _SglIndex sglIndex = (_SglIndex)surface;
1704 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1706 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
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);
1714 EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1715 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1721 _SglBindAPI(EGLenum api)
1723 return eglBindAPI(api);
1729 return eglQueryAPI();
1735 return eglWaitClient();
1739 _SglReleaseThread(void)
1741 return eglReleaseThread();
1745 _SglCreatePbufferFromClientBuffer(
1746 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1748 _SglIndex sglIndex = INVALID_SGL_INDEX;
1749 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1751 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1752 if (sglIndex <= INVALID_SGL_INDEX)
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);
1758 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1761 EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1762 if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1764 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1769 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1771 pSglInfo->surface = pbufferFromClientBuffer;
1773 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1776 return (EGLSurface)sglIndex;
1780 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1782 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1784 _SglIndex sglIndex = (_SglIndex)surface;
1785 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1787 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
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);
1794 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1797 EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1798 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1804 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1806 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1808 _SglIndex sglIndex = (_SglIndex)surface;
1809 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1811 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
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);
1817 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1820 EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1821 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1827 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1829 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1831 _SglIndex sglIndex = (_SglIndex)surface;
1832 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1834 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
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);
1840 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1843 EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1844 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1850 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1852 return eglSwapInterval(dpy, interval);
1856 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1858 return eglCreateContext(dpy, config, shareContext, pAttribList);
1862 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1864 return eglDestroyContext(dpy, ctx);
1868 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1870 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1872 _SglIndex sglIndexDraw = (_SglIndex)draw;
1873 _SglIndex sglIndexRead = (_SglIndex)read;
1875 EGLSurface sglInfoReadSurface;
1877 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1879 if (sglInfoRead == null)
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);
1886 sglInfoReadSurface = sglInfoRead->surface;
1888 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer && sglInfoRead->pSecondSglInfo != null)
1890 sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1893 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1896 _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1897 if (pSglInfoDraw == null)
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);
1905 if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1907 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1910 pSglInfoDraw->context = ctx;
1912 EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1914 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1915 if (draw != EGL_NO_SURFACE)
1917 _SaveCurrentContext(pSglInfoDraw);
1921 if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1923 eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1926 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1932 _SglGetCurrentContext()
1934 return eglGetCurrentContext();
1938 _SglGetCurrentSurface(EGLint readDraw)
1940 EGLSurface surface = EGL_NO_SURFACE;
1941 _SglIndex sglIndex = INVALID_SGL_INDEX;
1943 surface = eglGetCurrentSurface(readDraw);
1944 sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1946 if (sglIndex < INVALID_SGL_INDEX)
1948 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1949 return EGL_NO_SURFACE;
1952 return (EGLSurface)sglIndex;
1956 _SglGetCurrentDisplay(void)
1958 return eglGetCurrentDisplay();
1962 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1964 return eglQueryContext(dpy, ctx, attribute, pValue);
1974 _SglWaitNative(EGLint engine)
1976 return eglWaitNative(engine);
1979 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
1982 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1984 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1986 _SglIndex sglIndex = (_SglIndex)surface;
1987 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1989 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1991 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1992 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1993 , (unsigned int)sglIndex);
1995 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
1998 if (pSglInfo->pBitmap != null)
2000 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2001 _SglUpdateBufferOSP(dpy, surface);
2003 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2005 if (pSglInfo->glVersion == 1)
2020 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2022 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2024 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2026 _SglInfo* pBackSglInfo = null;
2027 _SglInfo* pFrontSglInfo = null;
2029 if (pSglInfo->isBackbuffer)
2031 pBackSglInfo = pSglInfo;
2032 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2036 pBackSglInfo = pSglInfo->pSecondSglInfo;
2037 pFrontSglInfo = pSglInfo;
2039 pBackSglInfo->isBackbuffer = false;
2040 pFrontSglInfo->isBackbuffer = true;
2042 pFrontSglInfo->wasShown = false;
2043 pFrontSglInfo->swapDone = false;
2044 if (pSglInfo->pVisualElement != null)
2046 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2048 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2049 evas_object_hide(pBackSglInfo->pObject);
2051 if (pSglInfo->isFirstSwap)
2053 pBackSglInfo->isFirstSwap = false;
2054 pFrontSglInfo->isFirstSwap = false;
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();
2066 if (pSglInfo->pObject != null)
2068 evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2074 if (pSglInfo->pVisualElement != null)
2076 pSglInfo->pVisualElement->SetFlushNeeded();
2081 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2086 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2087 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2094 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2096 Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2097 EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2098 return eglCopyBuffers(dpy, surface, nativePixmap);
2101 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2103 __EglMustCastToProperFunctionPointerType
2104 _SglGetProcAddress(const char* pProcName)
2106 return eglGetProcAddress(pProcName);
2110 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2112 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2113 Tizen::Graphics::BufferInfo bufferInfo;
2114 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2116 _SglIndex sglIndex = (_SglIndex)surface;
2117 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2119 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2121 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2122 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
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);
2130 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2141 }} // Tizen::Graphics