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;
130 EGLSurface __pPreviousMakeCurrent = EGL_NO_SURFACE;
133 #if !defined (_OSP_EMUL_)
134 #define FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP
139 ENGINE_TYPE_SOFEWARE_X11 = 0,
140 ENGINE_TYPE_OPENGL_X11,
144 _engineType __evasEngineType = ENGINE_TYPE_SOFEWARE_X11;
145 bool __isDoublePixmapEnabled = false;
147 void _OnBoundsChanged(void* pData);
149 const int INVALID_SGL_INDEX = 0;
150 const int MAX_SGL_INDEX = 100;
156 : sglIndex(INVALID_SGL_INDEX)
157 , display(EGL_NO_DISPLAY)
158 , surface(EGL_NO_SURFACE)
159 , context(EGL_NO_CONTEXT)
160 , config((EGLConfig)0)
163 , nativePixmap((EGLNativePixmapType)0)
164 , pVisualElement(null)
165 , pVisualElementSurface(null)
168 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
177 , pSecondSglInfo(null)
181 virtual ~_SglInfo(void)
183 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
184 if (pEcoreImage != null)
186 ecore_x_image_free(pEcoreImage);
195 if (pVisualElement != null)
197 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
198 if (pVisualElementImpl != null)
200 pVisualElementImpl->SetDestroyedCallback(null, null);
204 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
205 _UnregisterRenderCallback(pObject);
207 evas_object_del(pObject);
208 delete pVisualElementSurface;
211 bool needAllocateObject = true;
212 if (__isDoublePixmapEnabled)
216 needAllocateObject = false;
219 if (needAllocateObject)
221 if (pVisualElement != null)
223 pVisualElement->SetSurface(null);
227 sglIndex = INVALID_SGL_INDEX;
228 display = EGL_NO_DISPLAY;
229 surface = EGL_NO_SURFACE;
230 context = EGL_NO_CONTEXT;
231 config = (EGLConfig)0;
234 nativePixmap = (EGLNativePixmapType)0;
235 pVisualElement = null;
236 pVisualElementSurface = null;
239 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
248 pSecondSglInfo = null;
258 Evas_Object* pObject;
259 EGLNativePixmapType nativePixmap;
260 VisualElement* pVisualElement;
261 VisualElementSurface* pVisualElementSurface;
263 Tizen::Ui::Window* pWindow;
264 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
265 Ecore_X_Image* pEcoreImage;
273 _SglInfo* pSecondSglInfo;
276 _SglInfo(const _SglInfo& sglInfo);
277 _SglInfo& operator =(const _SglInfo& rhs);
284 : nativePixmap((Pixmap)0)
285 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
287 , pTbmBufferObject(null)
293 virtual ~_PixmapInfo(void)
295 Display* pDisplay = (Display*)ecore_x_display_get();
296 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
297 tbm_bo_unmap(pTbmBufferObject);
298 tbm_bo_unref(pTbmBufferObject);
304 tbm_bufmgr_deinit(pTbmBufMgr);
305 DRI2DestroyDrawable(pDisplay, nativePixmap);
307 XFreePixmap(pDisplay, nativePixmap);
309 nativePixmap = (Pixmap)0;
310 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
312 pTbmBufferObject = null;
319 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
320 tbm_bufmgr pTbmBufMgr;
321 tbm_bo pTbmBufferObject;
326 void _EvasObjectImageChange(_SglInfo* pSglInfo);
329 _OnBoundsChanged(void* pData)
331 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
332 SysTryReturnVoidResult(NID_GRP, pSglInfo != null
333 && pSglInfo->sglIndex > INVALID_SGL_INDEX
334 && pSglInfo->pVisualElement != null
335 && pSglInfo->pWindow != null
336 , E_INVALID_STATE, "Invalid state.");
338 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
340 __pPreviousSglInfo = null;
343 bool needSglMakeCurrent = false;
344 EGLDisplay eglDisplay = eglGetCurrentDisplay();
345 EGLSurface drawSurface = _SglGetCurrentSurface(EGL_DRAW);
346 EGLSurface readSurface = _SglGetCurrentSurface(EGL_READ);
347 EGLSurface eglContext = eglGetCurrentContext();
349 if (__pPreviousMakeCurrent == drawSurface)
351 __pPreviousMakeCurrent = EGL_NO_SURFACE;
354 if (drawSurface != EGL_NO_SURFACE && readSurface != EGL_NO_SURFACE)
356 needSglMakeCurrent = true;
360 drawSurface = eglGetCurrentSurface(EGL_DRAW);
361 readSurface = eglGetCurrentSurface(EGL_READ);
364 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
365 EGLBoolean ret = eglDestroySurface(pSglInfo->display, pSglInfo->surface);
366 pSglInfo->surface = null;
367 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
369 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
370 if (pSglInfo->pEcoreImage != null)
372 ecore_x_image_free(pSglInfo->pEcoreImage);
373 pSglInfo->pEcoreImage = null;
377 FloatRectangle rect = pSglInfo->pVisualElement->GetBounds();
378 int width = int(rect.width);
379 int height = int(rect.height);
380 SysTryReturnVoidResult(NID_GRP, width > 0 && height > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
381 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
382 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
383 SysTryReturnVoidResult(NID_GRP, widthPhysical > 0 && heightPhysical > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", widthPhysical, heightPhysical);
385 evas_object_image_size_set(pSglInfo->pObject, widthPhysical, heightPhysical);
386 evas_object_resize(pSglInfo->pObject, widthPhysical, heightPhysical);
388 delete pSglInfo->pBitmap;
389 pSglInfo->pBitmap = null;
390 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
391 SysTryReturnVoidResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
392 result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
393 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
394 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfo->sglIndex, bitmap.get());
395 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
396 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfo->display, pSglInfo->config, nativePixmap, null);
397 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
399 pSglInfo->nativePixmap = nativePixmap;
400 pSglInfo->surface = eglSurface;
401 pSglInfo->isBackbuffer = true;
402 pSglInfo->wasShown = false;
403 pSglInfo->swapDone = false;
404 pSglInfo->isFirstSwap = true;
406 if (__isDoublePixmapEnabled)
408 _SglInfo* pSglInfoSecond = pSglInfo->pSecondSglInfo;
409 SysTryReturnVoidResult(NID_GRP, pSglInfoSecond != null, E_INVALID_STATE, "Invalid pSecondSglInfo.");
411 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
412 ret = eglDestroySurface(pSglInfoSecond->display, pSglInfoSecond->surface);
413 pSglInfoSecond->surface = null;
414 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
416 evas_object_image_size_set(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
417 evas_object_resize(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
419 delete pSglInfoSecond->pBitmap;
420 pSglInfoSecond->pBitmap = null;
421 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
422 SysTryReturnVoidResult(NID_GRP, bitmapSecond.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
423 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
424 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
425 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfoSecond->sglIndex, bitmapSecond.get());
426 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
427 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfoSecond->display, pSglInfoSecond->config, nativePixmap, null);
428 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
430 pSglInfoSecond->nativePixmap = nativePixmap;
431 pSglInfoSecond->surface = eglSurface;
432 pSglInfoSecond->pBitmap = bitmapSecond.release();
433 pSglInfoSecond->isBackbuffer = true;
434 pSglInfoSecond->wasShown = false;
435 pSglInfoSecond->swapDone = true;
437 pSglInfo->isFirstSwap = true;
439 _EvasObjectImageChange(pSglInfoSecond);
442 pSglInfo->pBitmap = bitmap.release();
443 _EvasObjectImageChange(pSglInfo);
445 BufferInfo bufferInfo;
446 pSglInfo->pBitmap->Lock(bufferInfo);
447 pSglInfo->pBitmap->Unlock();
449 if (needSglMakeCurrent)
451 _SglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
455 eglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
462 _OnDestroyed(void* pData)
464 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
466 if (pSglInfo != null)
468 pSglInfo->pVisualElement = null;
470 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
472 pSglInfo->pSecondSglInfo->pVisualElement = null;
477 class _SglInfoTableManipulator
480 _SglInfoTableManipulator()
482 result r = __SglMutex.Create();
483 SysTryLog(NID_GRP, r == E_SUCCESS, "Failed to create mutex. [%s]", GetErrorMessage(r));
484 __SglIndexLastUsed = INVALID_SGL_INDEX;
485 __SglInitialized = false;
488 ~_SglInfoTableManipulator()
493 FindNextEmptySlotOfSglInfoTable()
495 if (!__SglInitialized)
497 if (CreateSglInfoTable() == false)
499 return INVALID_SGL_INDEX;
503 _SglIndex sglIndex = INVALID_SGL_INDEX;
504 _SglIndex index = __SglIndexLastUsed;
506 __SglMutex.Acquire();
507 for (int i = 1; i < MAX_SGL_INDEX; i++)
511 if (index >= MAX_SGL_INDEX)
516 if (__SglInfoTable[index] == null)
519 __SglIndexLastUsed = index;
523 __SglMutex.Release();
526 if (sglIndex <= INVALID_SGL_INDEX)
528 return INVALID_SGL_INDEX;
539 __SglMutex.Acquire();
541 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
543 __SglInfoTable[i] = null;
546 __SglInfoTable[0] = new (std::nothrow) _SglInfo;
547 if (__SglInfoTable[0] != null)
550 __SglInitialized = true;
551 __SglInfoTable[0]->glVersion = 2;
554 __SglMutex.Release();
560 DestroySglInfoTable()
562 __SglMutex.Acquire();
564 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
566 if (__SglInfoTable[i] != null)
568 delete __SglInfoTable[i];
569 __SglInfoTable[i] = null;
572 __SglInitialized = false;
574 __SglMutex.Release();
578 LockSglInfoTable(_SglIndex index)
580 __SglMutex.Acquire();
582 if (index < INVALID_SGL_INDEX || index >= MAX_SGL_INDEX)
587 return __SglInfoTable[index];
593 __SglMutex.Release();
599 _SglInfo* newSglInfo = new (std::nothrow) _SglInfo;
600 if (newSglInfo == null)
602 SysLog(NID_GRP, "CreateSglIndex fail to allocate _SglInfo!!");
603 return INVALID_SGL_INDEX;
606 int sglIndex = FindNextEmptySlotOfSglInfoTable();
607 if (sglIndex == INVALID_SGL_INDEX)
611 SysLog(NID_GRP, "CreateSglIndex failed!!");
612 return INVALID_SGL_INDEX;
615 __SglMutex.Acquire();
616 __SglInfoTable[sglIndex] = newSglInfo;
617 __SglMutex.Release();
623 DestroySglIndex(_SglIndex sglIndex)
625 if (sglIndex < 1 || sglIndex >= MAX_SGL_INDEX)
627 SysLog(NID_GRP, "sglIndex:%#x is out of range.", (unsigned int)sglIndex);
631 __SglMutex.Acquire();
633 if (sglIndex <= INVALID_SGL_INDEX || __SglInfoTable[sglIndex] == null)
635 __SglMutex.Release();
637 SysLog(NID_GRP, "DestroySglIndex failed!! sglIndex:%#x", (unsigned int)sglIndex);
641 delete __SglInfoTable[sglIndex];
642 __SglInfoTable[sglIndex] = null;
644 __SglMutex.Release();
650 GetSglIndexForSurface(EGLSurface surface)
652 __SglMutex.Acquire();
655 for (int i = 0; i < MAX_SGL_INDEX; i++)
657 if (__SglInfoTable[i] != null && __SglInfoTable[i]->surface == surface)
664 __SglMutex.Release();
669 static _SglInfoTableManipulator* GetInstance(void)
671 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
675 pthread_once(&once_block, __InitSglInfoTableManipulator);
678 return __pTheInstance;
682 static void __InitSglInfoTableManipulator(void)
684 static _SglInfoTableManipulator instance;
685 __pTheInstance = &instance;
689 Runtime::Mutex __SglMutex;
690 _SglInfo* __SglInfoTable[MAX_SGL_INDEX];
691 _SglIndex __SglIndexLastUsed;
692 bool __SglInitialized;
694 static _SglInfoTableManipulator* __pTheInstance;
696 _SglInfoTableManipulator* _SglInfoTableManipulator::__pTheInstance = null;
699 _PixmapSurfaceDestroyCallback(void* pCallbackParam)
701 delete (_PixmapInfo*)pCallbackParam;
704 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
706 _PixmapLockCallBack(void* pCallbackParam)
708 tbm_bo_map((tbm_bo)pCallbackParam, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
712 _PixmapUnlockCallBack(void* pCallbackParam)
714 tbm_bo_unmap((tbm_bo)pCallbackParam);
719 _GetEvasEngineType(Evas_Object* pObject)
723 const char* pString = elm_config_preferred_engine_get();
724 String engineName(pString);
725 SysLog(NID_GRP, "evas backend : %s", pString);
727 if (engineName.Contains(String("opengl_x11")))
729 return ENGINE_TYPE_OPENGL_X11;
731 else if (engineName.Contains(String("software_x11")))
733 return ENGINE_TYPE_SOFEWARE_X11;
737 pString = elm_config_engine_get();
738 String engineName(pString);
739 SysLog(NID_GRP, "system evas backend : %s", pString);
740 if (engineName.Contains(String("opengl_x11")))
742 return ENGINE_TYPE_OPENGL_X11;
746 return ENGINE_TYPE_SOFEWARE_X11;
752 Evas* pEvas = evas_object_evas_get(pObject);
755 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
756 return ENGINE_TYPE_SOFEWARE_X11;
759 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
760 if (pEcoreEvas == null)
762 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEcoreEvas failed.");
763 return ENGINE_TYPE_SOFEWARE_X11;
766 const char* pString = ecore_evas_engine_name_get(pEcoreEvas);
767 String engineName(pString);
768 SysLog(NID_GRP, "evas backend : %s", pString);
770 if (engineName.Contains(String("opengl_x11")))
772 return ENGINE_TYPE_OPENGL_X11;
774 else if (engineName.Contains(String("software_x11")))
776 return ENGINE_TYPE_SOFEWARE_X11;
780 return ENGINE_TYPE_ETC;
786 _EvasObjectImageChange(_SglInfo* pSglInfo)
788 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
790 Evas* pEvas = evas_object_evas_get(pSglInfo->pObject);
793 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
797 Evas_Native_Surface nativeSurface = {0, };
798 nativeSurface.type = EVAS_NATIVE_SURFACE_X11;
799 nativeSurface.version = EVAS_NATIVE_SURFACE_VERSION;
800 nativeSurface.data.x11.pixmap = (unsigned long)pSglInfo->nativePixmap;
801 nativeSurface.data.x11.visual = ecore_x_default_visual_get(ecore_x_display_get(), ecore_x_default_screen_get());
802 evas_object_image_native_surface_set(pSglInfo->pObject, &nativeSurface);
806 BufferInfo bufferInfo;
807 pSglInfo->pBitmap->Lock(bufferInfo);
808 evas_object_image_data_set(pSglInfo->pObject, bufferInfo.pPixels);
809 pSglInfo->pBitmap->Unlock();
813 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
815 _PostRenderCallback(Ecore_Evas* ee)
817 if (__pPreviousSglInfo != null)
819 EGLDisplay display = __pPreviousSglInfo->display;
820 EGLSurface surface = __pPreviousSglInfo->surface;
821 EGLContext context = __pPreviousSglInfo->context;
823 if (__isDoublePixmapEnabled)
825 _SglInfo* pBackSglInfo = null;
826 _SglInfo* pFrontSglInfo = null;
828 if (__pPreviousSglInfo->isBackbuffer)
830 pBackSglInfo = __pPreviousSglInfo;
831 pFrontSglInfo = __pPreviousSglInfo->pSecondSglInfo;
835 pBackSglInfo = __pPreviousSglInfo->pSecondSglInfo;
836 pFrontSglInfo = __pPreviousSglInfo;
839 if (!pBackSglInfo->wasShown && __pPreviousSglInfo->pVisualElement != null)
841 __pPreviousSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
842 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
843 evas_object_hide(pBackSglInfo->pObject);
845 pBackSglInfo->wasShown = true;
846 pFrontSglInfo->swapDone = true;
849 surface = pBackSglInfo->surface;
852 EGLBoolean ret = eglMakeCurrent(display, surface, surface, context);
853 SysTryLog(NID_GRP, ret == EGL_TRUE, "fail to restore previous surface and context. %#x %#x %#x %#x egl error:%#x"
854 , (unsigned int)display
855 , (unsigned int)surface
856 , (unsigned int)surface
857 , (unsigned int)context
858 , (unsigned int)eglGetError());
863 _SaveCurrentContext(_SglInfo* pSglInfo)
865 __pPreviousSglInfo = pSglInfo;
869 _RegisterRenderCallback(Evas_Object* pObject)
873 if (__registerCallbackCount == 0)
875 Evas* pEvas = evas_object_evas_get(pObject);
881 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
882 if (pEcoreEvas == null)
887 ecore_evas_callback_post_render_set(pEcoreEvas, _PostRenderCallback);
889 __registerCallbackCount++;
894 _UnregisterRenderCallback(Evas_Object* pObject)
898 __registerCallbackCount--;
900 if (__registerCallbackCount == 0)
902 Evas* pEvas = evas_object_evas_get(pObject);
908 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
909 if (pEcoreEvas == null)
914 ecore_evas_callback_post_render_set(pEcoreEvas, NULL);
918 #endif //#if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
930 return eglGetError();
934 _SglGetDisplay(EGLNativeDisplayType displayId)
936 if (displayId == (EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)
938 return eglGetDisplay((EGLNativeDisplayType) ecore_x_display_get());
941 return eglGetDisplay((EGLNativeDisplayType) displayId);
945 _SglInitialize(EGLDisplay dpy, EGLint* pMajor, EGLint* pMinor)
947 return eglInitialize(dpy, pMajor, pMinor);
951 _SglTerminate(EGLDisplay dpy)
953 _GlesInterfaceTerminate_1();
954 _GlesInterfaceTerminate_2();
956 _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
958 if (!__isDoublePixmapEnabled)
963 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
964 __registerCallbackCount = 0;
965 __pPreviousSglInfo = null;
973 _SglQueryString(EGLDisplay dpy, EGLint name)
975 return eglQueryString(dpy, name);
979 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
981 return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
985 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
987 return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
991 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
993 return eglGetConfigAttrib(dpy, config, attribute, pValue);
997 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
999 _SglIndex sglIndex = INVALID_SGL_INDEX;
1000 Object* pObj = (Object*)win;
1001 VisualElement* pVisualElement = dynamic_cast<VisualElement*> (pObj);
1002 Tizen::Ui::Control* pControl = dynamic_cast<Tizen::Ui::Control*> (pObj);
1003 #if 0//!defined(_OSP_EMUL_)
1004 Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
1006 SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1007 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1009 #if 0//!defined(_OSP_EMUL_)
1010 if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
1012 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(*pControl);
1013 _WindowImpl* pWindowImpl = dynamic_cast<_WindowImpl*> (pControlImpl);
1014 SysTryReturn(NID_GRP, pWindowImpl != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window state.");
1016 EGLNativeWindowType nativeWindow = (EGLNativeWindowType)pWindowImpl->GetNativeHandle();
1017 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1018 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid SglIndex.");
1020 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1021 if (windowSurface == EGL_NO_SURFACE)
1023 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1028 VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1029 if (pVisualElement == null)
1031 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1032 return EGL_NO_SURFACE;
1035 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1036 if (pVisualElementImpl == null)
1038 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1039 return EGL_NO_SURFACE;
1042 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1044 pSglInfo->surface = windowSurface;
1046 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1049 return (EGLSurface)sglIndex;
1052 #endif //#if !defined(_OSP_EMUL_)
1054 result r = E_SUCCESS;
1056 if (pVisualElement == null)
1058 pVisualElement = pControl->GetVisualElement();
1060 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1062 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1063 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1065 FloatRectangle rect = pVisualElement->GetBounds();
1066 int width = int(rect.width);
1067 int height = int(rect.height);
1068 SysTryReturn(NID_GRP, width > 0 && height > 0, EGL_NO_SURFACE, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
1070 Control* pFrame = pControl;
1071 Control* pTemp = pFrame->GetParent();
1072 while(pTemp != null)
1075 pTemp = pTemp->GetParent();
1078 Tizen::Ui::Window* pWindow = dynamic_cast<Tizen::Ui::Window*> (pFrame);
1079 SysTryReturn(NID_GRP, pWindow != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window.");
1081 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1082 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1084 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1085 if (pVisualElementSurfaceImpl == null)
1087 SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1088 delete pVisualElementSurface;
1089 return EGL_NO_SURFACE;
1092 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1093 delete pVisualElementSurface;
1095 Evas* pEvas = evas_object_evas_get(pObject);
1096 pObject = evas_object_image_filled_add(pEvas);
1097 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1099 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1100 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
1102 evas_object_image_alpha_set(pObject, EINA_TRUE);
1103 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1104 evas_object_resize(pObject, widthPhysical, heightPhysical);
1105 evas_object_hide(pObject);
1107 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1108 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1109 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1111 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
1112 SysTryReturn(NID_GRP, bitmap.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1114 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1115 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1117 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmap.get(), null);
1118 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1120 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1121 pSglInfo->sglIndex = sglIndex;
1122 pSglInfo->display = dpy;
1123 pSglInfo->config = config;
1124 pSglInfo->pVisualElement = pVisualElement;
1125 pSglInfo->pWindow = pWindow;
1126 pSglInfo->pObject = pObject;
1127 pSglInfo->pVisualElementSurface = pVisualElementSurface;
1128 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1130 __evasEngineType = _GetEvasEngineType(pObject);
1132 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1133 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1135 __isDoublePixmapEnabled = true;
1139 __isDoublePixmapEnabled = false;
1142 __isDoublePixmapEnabled = false;
1145 if (__isDoublePixmapEnabled)
1147 pObject = evas_object_image_filled_add(pEvas);
1148 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1150 evas_object_image_alpha_set(pObject, EINA_TRUE);
1151 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1152 evas_object_resize(pObject, widthPhysical, heightPhysical);
1153 evas_object_hide(pObject);
1155 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1156 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1157 SysTryReturn(NID_GRP, pSglInfo->pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1159 delete pSglInfo->pBitmap;
1160 pSglInfo->pBitmap = null;
1162 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
1163 SysTryReturn(NID_GRP, bitmapSecond.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1165 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1166 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1168 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmapSecond.get(), null);
1169 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1171 _SglInfo* pSglInfoSecond = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1172 pSglInfoSecond->sglIndex = sglIndex;
1173 pSglInfoSecond->display = dpy;
1174 pSglInfoSecond->config = config;
1175 pSglInfoSecond->pVisualElement = pVisualElement;
1176 pSglInfoSecond->pWindow = pWindow;
1177 pSglInfoSecond->pObject = pObject;
1178 pSglInfoSecond->pVisualElementSurface = pVisualElementSurface;
1179 pSglInfoSecond->swapDone = true;
1180 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1182 pSglInfo->pSecondSglInfo = pSglInfoSecond;
1183 pSglInfoSecond->pSecondSglInfo = pSglInfo;
1185 pSglInfoSecond->pBitmap = bitmapSecond.release();
1186 _EvasObjectImageChange(pSglInfoSecond);
1190 pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1193 pSglInfo->pBitmap = bitmap.release();
1194 _EvasObjectImageChange(pSglInfo);
1196 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1197 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1198 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1199 _RegisterRenderCallback(pSglInfo->pObject);
1201 return (EGLSurface)pSglInfo->sglIndex;
1206 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1208 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1209 _SglIndex sglIndex = INVALID_SGL_INDEX;
1211 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1212 if (sglIndex <= INVALID_SGL_INDEX)
1214 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1215 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1217 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1220 EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1221 if (pbufferSurface == EGL_NO_SURFACE)
1223 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1228 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1230 pSglInfo->surface = pbufferSurface;
1232 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1235 return (EGLSurface)sglIndex;
1238 _OSP_LOCAL_ EGLNativePixmapType
1239 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
1243 int bitsPerPixel = 0;
1244 Display* pNativeDisplay = null;
1245 Drawable nativeWindow = 0;
1246 result r = E_FAILURE;
1247 Tizen::Graphics::BufferInfo bufferInfo;
1248 _PixmapInfo* pPixmapInfo = null;
1249 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1250 unsigned int attachments[] = { DRI2BufferFrontLeft };
1251 char* pDeviceName = null;
1252 char* pDriverName = null;
1253 DRI2Buffer* pDri2Buffer = null;
1256 int dri2BufferCount = 0;
1257 tbm_bo_handle tbmData = { null, };
1260 drm_magic_t magic = 0;
1262 _SglInfo* pSglInfo = null;
1263 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1265 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1266 if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1268 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1271 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1273 r = pBitmap->Lock(bufferInfo);
1279 width = bufferInfo.width;
1280 height = bufferInfo.height;
1281 bitsPerPixel = bufferInfo.bitsPerPixel;
1283 r = pBitmap->Unlock();
1284 if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1289 pNativeDisplay = (Display*) ecore_x_display_get();
1290 nativeWindow = DefaultRootWindow(pNativeDisplay);
1292 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1293 if (pPixmapInfo == null)
1298 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height,
1299 ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1300 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1305 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1306 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1307 XSync(pNativeDisplay, False);
1309 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1311 if (!ret || pDeviceName == null)
1316 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1319 if (pPixmapInfo->drmFd < 0)
1324 drmGetMagic(pPixmapInfo->drmFd, &magic);
1325 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1331 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1332 if (pPixmapInfo->pTbmBufMgr == null)
1337 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1338 if (pDri2Buffer == null)
1343 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1345 if (pPixmapInfo->pTbmBufferObject == null)
1350 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1351 if (tbmData.ptr == null)
1356 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1357 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1363 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1364 _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1370 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1372 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1374 pSglInfo->pEcoreImage = ecore_x_image_new(width
1376 , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1377 , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1379 if (pSglInfo->pEcoreImage == null)
1381 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1385 ecore_x_image_get(pSglInfo->pEcoreImage
1386 , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1394 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1396 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1397 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1400 ecore_x_image_free(pSglInfo->pEcoreImage);
1401 pSglInfo->pEcoreImage = null;
1402 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1404 SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1408 pSglInfo->width = width;
1409 pSglInfo->height = height;
1411 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1413 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1414 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1416 pBitmap->Lock(bufferInfo);
1417 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1420 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1423 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1424 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1425 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1429 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1433 close(pPixmapInfo->drmFd);
1437 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1439 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1447 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1448 return (EGLNativePixmapType)0;
1451 _OSP_LOCAL_ EGLNativePixmapType
1452 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
1456 int bitsPerPixel = 0;
1457 Display* pNativeDisplay = null;
1458 Drawable nativeWindow = 0;
1459 Tizen::Graphics::_BitmapImpl* pBitmapImpl = null;
1460 _PixmapInfo* pPixmapInfo = null;
1461 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1462 unsigned int attachments[] = { DRI2BufferFrontLeft };
1463 char* pDeviceName = null;
1464 char* pDriverName = null;
1465 DRI2Buffer* pDri2Buffer = null;
1468 int dri2BufferCount = 0;
1469 tbm_bo_handle tbmData = { null, };
1472 result r = E_FAILURE;
1473 drm_magic_t magic = 0;
1475 if (pBitmap == null)
1480 pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1481 if (pBitmapImpl == null)
1486 width = bufferInfo.width;
1487 height = bufferInfo.height;
1488 bitsPerPixel = bufferInfo.bitsPerPixel;
1490 if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1495 pNativeDisplay = (Display*) ecore_x_display_get();
1496 nativeWindow = DefaultRootWindow(pNativeDisplay);
1498 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1499 if (pPixmapInfo == null)
1504 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1506 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1511 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1512 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1513 XSync(pNativeDisplay, False);
1515 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1517 if (!ret || pDeviceName == null)
1522 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1524 if (pPixmapInfo->drmFd < 0)
1529 drmGetMagic(pPixmapInfo->drmFd, &magic);
1530 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1536 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1537 if (pPixmapInfo->pTbmBufMgr == null)
1542 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1543 if (pDri2Buffer == null)
1548 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1550 if (pPixmapInfo->pTbmBufferObject == null)
1555 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1556 if (tbmData.ptr == null)
1560 bufferInfo.pPixels = tbmData.ptr;
1562 bufferInfo.bitsPerPixel = 32;
1563 bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
1564 bufferInfo.pitch = bufferInfo.width * bufferInfo.bitsPerPixel / 8;
1565 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1566 r = pBitmapImpl->Construct(bufferInfo);
1572 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1573 , _PixmapSurfaceDestroyCallback, pPixmapInfo
1574 , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1575 ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1579 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1581 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1583 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1585 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1586 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1590 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1594 close(pPixmapInfo->drmFd);
1598 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1599 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1608 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1609 return (EGLNativePixmapType)0;
1613 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
1615 _SglIndex sglIndex = INVALID_SGL_INDEX;
1616 Tizen::Graphics::Bitmap* pBitmap = dynamic_cast<Tizen::Graphics::Bitmap*>((Tizen::Graphics::Bitmap*)pixmap);
1617 EGLNativePixmapType eglNativePixmap = 0;
1618 EGLSurface pixmapSurface = EGL_NO_SURFACE;
1619 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1621 if (pBitmap == null)
1626 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1627 if (sglIndex <= INVALID_SGL_INDEX)
1632 eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1633 if (eglNativePixmap == 0)
1638 pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1639 if (pixmapSurface == EGL_NO_SURFACE)
1641 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1646 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1648 pSglInfo->nativePixmap = eglNativePixmap;
1649 pSglInfo->surface = pixmapSurface;
1651 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1654 return (EGLSurface)sglIndex;
1657 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1661 SysLog(NID_GRP, "_SglCreatePixmapSurface failed!! dpy:%#x config:%#x pixmap:%#x pAttribList:%#x",
1662 (unsigned int)dpy, (unsigned int)config, (unsigned int)pixmap, (unsigned int)pAttribList);
1663 return eglCreatePixmapSurface(EGL_NO_DISPLAY, config, (EGLNativePixmapType) 0, pAttribList);
1667 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1669 if (__pPreviousMakeCurrent == surface)
1671 __pPreviousMakeCurrent = EGL_NO_SURFACE;
1674 EGLBoolean ret = EGL_FALSE;
1675 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1677 _SglIndex sglIndex = (_SglIndex)surface;
1678 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1680 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1682 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1683 SysLog(NID_GRP, "_SglDestroySurface failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1684 , (unsigned int)surface);
1685 return eglDestroySurface(dpy, EGL_NO_SURFACE);
1688 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1689 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1691 __pPreviousSglInfo = null;
1695 ret = eglDestroySurface(dpy, pSglInfo->surface);
1696 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1698 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1700 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1701 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1704 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1710 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1712 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1714 _SglIndex sglIndex = (_SglIndex)surface;
1715 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1717 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1719 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1720 SysLog(NID_GRP, "_SglQuerySurface failed!! dpy:%#x sglIndex:%#x attribute:%#x pValue:%#x",
1721 (unsigned int)dpy, (unsigned int)sglIndex, (unsigned int)attribute, (unsigned int)pValue);
1722 return eglQuerySurface(dpy, EGL_NO_SURFACE, attribute, pValue);
1725 EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1726 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1732 _SglBindAPI(EGLenum api)
1734 return eglBindAPI(api);
1740 return eglQueryAPI();
1746 return eglWaitClient();
1750 _SglReleaseThread(void)
1752 return eglReleaseThread();
1756 _SglCreatePbufferFromClientBuffer(
1757 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1759 _SglIndex sglIndex = INVALID_SGL_INDEX;
1760 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1762 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1763 if (sglIndex <= INVALID_SGL_INDEX)
1765 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!!"
1766 " dpy:%#x buftype:%#x buffer:%#x config:%#x pAttribList:%#x",
1767 (unsigned int)dpy, (unsigned int)buftype, (unsigned int)buffer, (unsigned int)config, (unsigned int)pAttribList);
1769 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1772 EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1773 if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1775 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1780 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1782 pSglInfo->surface = pbufferFromClientBuffer;
1784 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1787 return (EGLSurface)sglIndex;
1791 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1793 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1795 _SglIndex sglIndex = (_SglIndex)surface;
1796 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1798 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1800 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1801 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!! "
1802 "dpy:%#x surface:%#x attribute:%#x value:%#x",
1803 (unsigned int)dpy, (unsigned int)surface, (unsigned int)attribute, (unsigned int)value);
1805 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1808 EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1809 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1815 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1817 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1819 _SglIndex sglIndex = (_SglIndex)surface;
1820 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1822 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1824 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1825 SysLog(NID_GRP, "_SglBindTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1826 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1828 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1831 EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1832 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1838 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1840 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1842 _SglIndex sglIndex = (_SglIndex)surface;
1843 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1845 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1847 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1848 SysLog(NID_GRP, "_SglReleaseTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1849 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1851 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1854 EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1855 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1861 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1863 return eglSwapInterval(dpy, interval);
1867 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1869 return eglCreateContext(dpy, config, shareContext, pAttribList);
1873 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1875 return eglDestroyContext(dpy, ctx);
1879 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1881 if (__pPreviousMakeCurrent == draw)
1885 __pPreviousMakeCurrent = draw;
1887 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1889 _SglIndex sglIndexDraw = (_SglIndex)draw;
1890 _SglIndex sglIndexRead = (_SglIndex)read;
1892 EGLSurface sglInfoReadSurface;
1894 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1896 if (sglInfoRead == null)
1898 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1899 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1900 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1901 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1903 sglInfoReadSurface = sglInfoRead->surface;
1905 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer && sglInfoRead->pSecondSglInfo != null)
1907 sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1910 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1913 _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1914 if (pSglInfoDraw == null)
1916 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1917 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1918 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1919 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1922 if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1924 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1927 pSglInfoDraw->context = ctx;
1929 EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1931 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1932 if (draw != EGL_NO_SURFACE)
1934 _SaveCurrentContext(pSglInfoDraw);
1938 if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1940 eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1943 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1949 _SglGetCurrentContext()
1951 return eglGetCurrentContext();
1955 _SglGetCurrentSurface(EGLint readDraw)
1957 EGLSurface surface = EGL_NO_SURFACE;
1958 _SglIndex sglIndex = INVALID_SGL_INDEX;
1960 surface = eglGetCurrentSurface(readDraw);
1961 sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1963 if (sglIndex < INVALID_SGL_INDEX)
1965 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1966 return EGL_NO_SURFACE;
1969 return (EGLSurface)sglIndex;
1973 _SglGetCurrentDisplay(void)
1975 return eglGetCurrentDisplay();
1979 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1981 return eglQueryContext(dpy, ctx, attribute, pValue);
1991 _SglWaitNative(EGLint engine)
1993 return eglWaitNative(engine);
1996 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
1999 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
2001 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2003 _SglIndex sglIndex = (_SglIndex)surface;
2004 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2006 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2008 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2009 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
2010 , (unsigned int)sglIndex);
2012 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
2015 if (pSglInfo->pBitmap != null)
2017 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2018 _SglUpdateBufferOSP(dpy, surface);
2020 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2022 if (pSglInfo->glVersion == 1)
2037 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2039 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2041 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2043 _SglInfo* pBackSglInfo = null;
2044 _SglInfo* pFrontSglInfo = null;
2046 if (pSglInfo->isBackbuffer)
2048 pBackSglInfo = pSglInfo;
2049 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2053 pBackSglInfo = pSglInfo->pSecondSglInfo;
2054 pFrontSglInfo = pSglInfo;
2056 pBackSglInfo->isBackbuffer = false;
2057 pFrontSglInfo->isBackbuffer = true;
2059 pFrontSglInfo->wasShown = false;
2060 pFrontSglInfo->swapDone = false;
2061 if (pSglInfo->pVisualElement != null)
2063 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2065 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2066 evas_object_hide(pBackSglInfo->pObject);
2068 if (pSglInfo->isFirstSwap)
2070 pBackSglInfo->isFirstSwap = false;
2071 pFrontSglInfo->isFirstSwap = false;
2074 BufferInfo bufferInfo;
2075 pFrontSglInfo->pBitmap->Lock(bufferInfo);
2076 pFrontSglInfo->pBitmap->Merge(Point(0, 0), *pBackSglInfo->pBitmap, Rectangle(0, 0, bufferInfo.width, bufferInfo.height));
2077 pFrontSglInfo->pBitmap->Unlock();
2083 if (pSglInfo->pObject != null)
2085 evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2091 if (pSglInfo->pVisualElement != null)
2093 pSglInfo->pVisualElement->SetFlushNeeded();
2098 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2103 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2104 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2111 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2113 Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2114 EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2115 return eglCopyBuffers(dpy, surface, nativePixmap);
2118 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2120 __EglMustCastToProperFunctionPointerType
2121 _SglGetProcAddress(const char* pProcName)
2123 return eglGetProcAddress(pProcName);
2127 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2129 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2130 Tizen::Graphics::BufferInfo bufferInfo;
2131 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2133 _SglIndex sglIndex = (_SglIndex)surface;
2134 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2136 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2138 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2139 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
2143 eglCopyBuffers(dpy, pSglInfo->surface, pSglInfo->nativePixmap);
2144 ecore_x_image_get(pSglInfo->pEcoreImage, (Ecore_X_Drawable)pSglInfo->nativePixmap,
2145 0, 0, 0, 0, pSglInfo->width, pSglInfo->height);
2147 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2158 }} // Tizen::Graphics