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>
44 #include <Evas_Engine_GL_X11.h>
46 #include <FBaseSysLog.h>
47 #include <FGrpBitmap.h>
48 #include <FGrpCoordinateSystem.h>
49 #include <FUiControl.h>
50 #include <FUiCtrlForm.h>
51 #include <FUiCtrlFrame.h>
52 #include <FUiWindow.h>
54 #include <FUi_EcoreEvasMgr.h>
55 #include <FUi_EcoreEvas.h>
57 #include <FGrp_BitmapTool.h>
58 #include <FGrp_BitmapImpl.h>
59 #include <FUi_ControlImpl.h>
60 #include <FUi_Control.h>
61 #include <FUi_WindowImpl.h>
62 #include <FUiAnim_VisualElementSurfaceImpl.h>
63 #include <FUiAnim_VisualElementImpl.h>
71 #include <elm_config.h>
73 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
77 #include <tbm_bufmgr.h>
81 typedef int _SglIndex;
84 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList);
85 EGLSurface _SglGetCurrentSurface(EGLint readDraw);
86 EGLNativePixmapType _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pixmap);
87 EGLNativePixmapType _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo);
89 #if defined(FGRAPHICS_INTERNAL_USE_DYNAMIC_LOADING)
90 void _GlesInterfaceTerminate_1();
91 void _GlesInterfaceTerminate_2();
94 #include <GLES2/gl2.h>
96 void _GlFlush_1(void);
97 void _GlFlush_2(void);
98 void _GlClear_1(GLbitfield mask);
99 void _GlClear_2(GLbitfield mask);
101 EGLBoolean _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
103 using namespace Tizen::Ui::Controls;
109 using namespace Tizen::Ui::Animations;
110 using namespace Tizen::Ui;
111 using namespace Tizen::Base;
113 namespace Tizen { namespace Graphics
124 #define FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT
125 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
126 void _PostRenderCallback(Ecore_Evas* ee);
127 void _SaveCurrentContext(_SglInfo* pSglInfo);
128 void _UnregisterRenderCallback(Evas_Object* pObject);
129 int __registerCallbackCount = 0;
130 _SglInfo* __pPreviousSglInfo = null;
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 (drawSurface != EGL_NO_SURFACE && readSurface != EGL_NO_SURFACE)
351 needSglMakeCurrent = true;
355 drawSurface = eglGetCurrentSurface(EGL_DRAW);
356 readSurface = eglGetCurrentSurface(EGL_READ);
359 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
360 EGLBoolean ret = eglDestroySurface(pSglInfo->display, pSglInfo->surface);
361 pSglInfo->surface = null;
362 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
364 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
365 if (pSglInfo->pEcoreImage != null)
367 ecore_x_image_free(pSglInfo->pEcoreImage);
368 pSglInfo->pEcoreImage = null;
372 FloatRectangle rect = pSglInfo->pVisualElement->GetBounds();
373 int width = int(rect.width);
374 int height = int(rect.height);
375 SysTryReturnVoidResult(NID_GRP, width > 0 && height > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
376 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
377 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
378 SysTryReturnVoidResult(NID_GRP, widthPhysical > 0 && heightPhysical > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", widthPhysical, heightPhysical);
380 evas_object_image_size_set(pSglInfo->pObject, widthPhysical, heightPhysical);
381 evas_object_resize(pSglInfo->pObject, widthPhysical, heightPhysical);
383 delete pSglInfo->pBitmap;
384 pSglInfo->pBitmap = null;
385 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
386 SysTryReturnVoidResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
387 result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
388 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
389 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfo->sglIndex, bitmap.get());
390 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
391 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfo->display, pSglInfo->config, nativePixmap, null);
392 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
394 pSglInfo->nativePixmap = nativePixmap;
395 pSglInfo->surface = eglSurface;
396 pSglInfo->isBackbuffer = true;
397 pSglInfo->wasShown = false;
398 pSglInfo->swapDone = false;
399 pSglInfo->isFirstSwap = true;
401 if (__isDoublePixmapEnabled)
403 _SglInfo* pSglInfoSecond = pSglInfo->pSecondSglInfo;
404 SysTryReturnVoidResult(NID_GRP, pSglInfoSecond != null, E_INVALID_STATE, "Invalid pSecondSglInfo.");
406 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
407 ret = eglDestroySurface(pSglInfoSecond->display, pSglInfoSecond->surface);
408 pSglInfoSecond->surface = null;
409 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
411 evas_object_image_size_set(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
412 evas_object_resize(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
414 delete pSglInfoSecond->pBitmap;
415 pSglInfoSecond->pBitmap = null;
416 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
417 SysTryReturnVoidResult(NID_GRP, bitmapSecond.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
418 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
419 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
420 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfoSecond->sglIndex, bitmapSecond.get());
421 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
422 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfoSecond->display, pSglInfoSecond->config, nativePixmap, null);
423 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
425 pSglInfoSecond->nativePixmap = nativePixmap;
426 pSglInfoSecond->surface = eglSurface;
427 pSglInfoSecond->pBitmap = bitmapSecond.release();
428 pSglInfoSecond->isBackbuffer = true;
429 pSglInfoSecond->wasShown = false;
430 pSglInfoSecond->swapDone = true;
432 pSglInfo->isFirstSwap = true;
434 _EvasObjectImageChange(pSglInfoSecond);
437 pSglInfo->pBitmap = bitmap.release();
438 _EvasObjectImageChange(pSglInfo);
440 BufferInfo bufferInfo;
441 pSglInfo->pBitmap->Lock(bufferInfo);
442 pSglInfo->pBitmap->Unlock();
444 if (needSglMakeCurrent)
446 _SglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
450 eglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
457 _OnDestroyed(void* pData)
459 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
461 if (pSglInfo != null)
463 pSglInfo->pVisualElement = null;
465 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
467 pSglInfo->pSecondSglInfo->pVisualElement = null;
472 class _SglInfoTableManipulator
475 _SglInfoTableManipulator()
477 result r = __SglMutex.Create();
478 SysTryLog(NID_GRP, r == E_SUCCESS, "Failed to create mutex. [%s]", GetErrorMessage(r));
479 __SglIndexLastUsed = INVALID_SGL_INDEX;
480 __SglInitialized = false;
483 ~_SglInfoTableManipulator()
488 FindNextEmptySlotOfSglInfoTable()
490 if (!__SglInitialized)
492 if (CreateSglInfoTable() == false)
494 return INVALID_SGL_INDEX;
498 _SglIndex sglIndex = INVALID_SGL_INDEX;
499 _SglIndex index = __SglIndexLastUsed;
501 __SglMutex.Acquire();
502 for (int i = 1; i < MAX_SGL_INDEX; i++)
506 if (index >= MAX_SGL_INDEX)
511 if (__SglInfoTable[index] == null)
514 __SglIndexLastUsed = index;
518 __SglMutex.Release();
521 if (sglIndex <= INVALID_SGL_INDEX)
523 return INVALID_SGL_INDEX;
534 __SglMutex.Acquire();
536 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
538 __SglInfoTable[i] = null;
541 __SglInfoTable[0] = new (std::nothrow) _SglInfo;
542 if (__SglInfoTable[0] != null)
545 __SglInitialized = true;
546 __SglInfoTable[0]->glVersion = 2;
549 __SglMutex.Release();
555 DestroySglInfoTable()
557 __SglMutex.Acquire();
559 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
561 if (__SglInfoTable[i] != null)
563 delete __SglInfoTable[i];
564 __SglInfoTable[i] = null;
567 __SglInitialized = false;
569 __SglMutex.Release();
573 LockSglInfoTable(_SglIndex index)
575 __SglMutex.Acquire();
577 if (index < INVALID_SGL_INDEX || index >= MAX_SGL_INDEX)
582 return __SglInfoTable[index];
588 __SglMutex.Release();
594 _SglInfo* newSglInfo = new (std::nothrow) _SglInfo;
595 if (newSglInfo == null)
597 SysLog(NID_GRP, "CreateSglIndex fail to allocate _SglInfo!!");
598 return INVALID_SGL_INDEX;
601 int sglIndex = FindNextEmptySlotOfSglInfoTable();
602 if (sglIndex == INVALID_SGL_INDEX)
606 SysLog(NID_GRP, "CreateSglIndex failed!!");
607 return INVALID_SGL_INDEX;
610 __SglMutex.Acquire();
611 __SglInfoTable[sglIndex] = newSglInfo;
612 __SglMutex.Release();
618 DestroySglIndex(_SglIndex sglIndex)
620 if (sglIndex < 1 || sglIndex >= MAX_SGL_INDEX)
622 SysLog(NID_GRP, "sglIndex:%#x is out of range.", (unsigned int)sglIndex);
626 __SglMutex.Acquire();
628 if (sglIndex <= INVALID_SGL_INDEX || __SglInfoTable[sglIndex] == null)
630 __SglMutex.Release();
632 SysLog(NID_GRP, "DestroySglIndex failed!! sglIndex:%#x", (unsigned int)sglIndex);
636 delete __SglInfoTable[sglIndex];
637 __SglInfoTable[sglIndex] = null;
639 __SglMutex.Release();
645 GetSglIndexForSurface(EGLSurface surface)
647 __SglMutex.Acquire();
650 for (int i = 0; i < MAX_SGL_INDEX; i++)
652 if (__SglInfoTable[i] != null && __SglInfoTable[i]->surface == surface)
659 __SglMutex.Release();
664 static _SglInfoTableManipulator* GetInstance(void)
666 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
670 pthread_once(&once_block, __InitSglInfoTableManipulator);
673 return __pTheInstance;
677 static void __InitSglInfoTableManipulator(void)
679 static _SglInfoTableManipulator instance;
680 __pTheInstance = &instance;
684 Runtime::Mutex __SglMutex;
685 _SglInfo* __SglInfoTable[MAX_SGL_INDEX];
686 _SglIndex __SglIndexLastUsed;
687 bool __SglInitialized;
689 static _SglInfoTableManipulator* __pTheInstance;
691 _SglInfoTableManipulator* _SglInfoTableManipulator::__pTheInstance = null;
694 _PixmapSurfaceDestroyCallback(void* pCallbackParam)
696 delete (_PixmapInfo*)pCallbackParam;
699 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
701 _PixmapLockCallBack(void* pCallbackParam)
703 tbm_bo_map((tbm_bo)pCallbackParam, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
707 _PixmapUnlockCallBack(void* pCallbackParam)
709 tbm_bo_unmap((tbm_bo)pCallbackParam);
714 _GetEvasEngineType(Evas_Object* pObject)
718 const char* pString = elm_config_preferred_engine_get();
719 String engineName(pString);
720 SysLog(NID_GRP, "evas backend : %s", pString);
722 if (engineName.Contains(String("opengl_x11")))
724 return ENGINE_TYPE_OPENGL_X11;
726 else if (engineName.Contains(String("software_x11")))
728 return ENGINE_TYPE_SOFEWARE_X11;
732 pString = elm_config_engine_get();
733 String engineName(pString);
734 SysLog(NID_GRP, "system evas backend : %s", pString);
735 if (engineName.Contains(String("opengl_x11")))
737 return ENGINE_TYPE_OPENGL_X11;
741 return ENGINE_TYPE_SOFEWARE_X11;
747 Evas* pEvas = evas_object_evas_get(pObject);
750 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
751 return ENGINE_TYPE_SOFEWARE_X11;
754 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
755 if (pEcoreEvas == null)
757 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEcoreEvas failed.");
758 return ENGINE_TYPE_SOFEWARE_X11;
761 const char* pString = ecore_evas_engine_name_get(pEcoreEvas);
762 String engineName(pString);
763 SysLog(NID_GRP, "evas backend : %s", pString);
765 if (engineName.Contains(String("opengl_x11")))
767 return ENGINE_TYPE_OPENGL_X11;
769 else if (engineName.Contains(String("software_x11")))
771 return ENGINE_TYPE_SOFEWARE_X11;
775 return ENGINE_TYPE_ETC;
781 _EvasObjectImageChange(_SglInfo* pSglInfo)
783 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
785 Evas* pEvas = evas_object_evas_get(pSglInfo->pObject);
788 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
792 Evas_Engine_Info_GL_X11* pEvasInfo = (Evas_Engine_Info_GL_X11*)evas_engine_info_get(pEvas);
793 if (pEvasInfo == null)
795 SysLog(NID_GRP, "Getting evas engine info failed!");
799 Evas_Native_Surface nativeSurface = {0, };
800 nativeSurface.type = EVAS_NATIVE_SURFACE_X11;
801 nativeSurface.version = EVAS_NATIVE_SURFACE_VERSION;
802 nativeSurface.data.x11.pixmap = (unsigned long)pSglInfo->nativePixmap;
803 nativeSurface.data.x11.visual = pEvasInfo->info.visual;
804 evas_object_image_native_surface_set(pSglInfo->pObject, &nativeSurface);
808 BufferInfo bufferInfo;
809 pSglInfo->pBitmap->Lock(bufferInfo);
810 evas_object_image_data_set(pSglInfo->pObject, bufferInfo.pPixels);
811 pSglInfo->pBitmap->Unlock();
815 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
817 _PostRenderCallback(Ecore_Evas* ee)
819 if (__pPreviousSglInfo != null)
821 EGLDisplay display = __pPreviousSglInfo->display;
822 EGLSurface surface = __pPreviousSglInfo->surface;
823 EGLContext context = __pPreviousSglInfo->context;
825 if (__isDoublePixmapEnabled)
827 _SglInfo* pBackSglInfo = null;
828 _SglInfo* pFrontSglInfo = null;
830 if (__pPreviousSglInfo->isBackbuffer)
832 pBackSglInfo = __pPreviousSglInfo;
833 pFrontSglInfo = __pPreviousSglInfo->pSecondSglInfo;
837 pBackSglInfo = __pPreviousSglInfo->pSecondSglInfo;
838 pFrontSglInfo = __pPreviousSglInfo;
841 if (!pBackSglInfo->wasShown && __pPreviousSglInfo->pVisualElement != null)
843 __pPreviousSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
844 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
845 evas_object_hide(pBackSglInfo->pObject);
847 pBackSglInfo->wasShown = true;
848 pFrontSglInfo->swapDone = true;
851 surface = pBackSglInfo->surface;
854 EGLBoolean ret = eglMakeCurrent(display, surface, surface, context);
855 SysTryLog(NID_GRP, ret == EGL_TRUE, "fail to restore previous surface and context. %#x %#x %#x %#x egl error:%#x"
856 , (unsigned int)display
857 , (unsigned int)surface
858 , (unsigned int)surface
859 , (unsigned int)context
860 , (unsigned int)eglGetError());
865 _SaveCurrentContext(_SglInfo* pSglInfo)
867 __pPreviousSglInfo = pSglInfo;
871 _RegisterRenderCallback(Evas_Object* pObject)
875 if (__registerCallbackCount == 0)
877 Evas* pEvas = evas_object_evas_get(pObject);
883 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
884 if (pEcoreEvas == null)
889 ecore_evas_callback_post_render_set(pEcoreEvas, _PostRenderCallback);
891 __registerCallbackCount++;
896 _UnregisterRenderCallback(Evas_Object* pObject)
900 __registerCallbackCount--;
902 if (__registerCallbackCount == 0)
904 Evas* pEvas = evas_object_evas_get(pObject);
910 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
911 if (pEcoreEvas == null)
916 ecore_evas_callback_post_render_set(pEcoreEvas, NULL);
920 #endif //#if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
932 return eglGetError();
936 _SglGetDisplay(EGLNativeDisplayType displayId)
938 if (displayId == (EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)
940 return eglGetDisplay((EGLNativeDisplayType) ecore_x_display_get());
943 return eglGetDisplay((EGLNativeDisplayType) displayId);
947 _SglInitialize(EGLDisplay dpy, EGLint* pMajor, EGLint* pMinor)
949 return eglInitialize(dpy, pMajor, pMinor);
953 _SglTerminate(EGLDisplay dpy)
955 _GlesInterfaceTerminate_1();
956 _GlesInterfaceTerminate_2();
958 _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
960 if (!__isDoublePixmapEnabled)
965 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
966 __registerCallbackCount = 0;
967 __pPreviousSglInfo = null;
975 _SglQueryString(EGLDisplay dpy, EGLint name)
977 return eglQueryString(dpy, name);
981 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
983 return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
987 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
989 return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
993 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
995 return eglGetConfigAttrib(dpy, config, attribute, pValue);
999 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
1001 _SglIndex sglIndex = INVALID_SGL_INDEX;
1002 Object* pObj = (Object*)win;
1003 VisualElement* pVisualElement = dynamic_cast<VisualElement*> (pObj);
1004 Tizen::Ui::Control* pControl = dynamic_cast<Tizen::Ui::Control*> (pObj);
1005 #if 0//!defined(_OSP_EMUL_)
1006 Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
1008 SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1009 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1011 #if 0//!defined(_OSP_EMUL_)
1012 if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
1014 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(*pControl);
1015 _WindowImpl* pWindowImpl = dynamic_cast<_WindowImpl*> (pControlImpl);
1016 SysTryReturn(NID_GRP, pWindowImpl != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window state.");
1018 EGLNativeWindowType nativeWindow = (EGLNativeWindowType)pWindowImpl->GetNativeHandle();
1019 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1020 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid SglIndex.");
1022 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1023 if (windowSurface == EGL_NO_SURFACE)
1025 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1030 VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1031 if (pVisualElement == null)
1033 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1034 return EGL_NO_SURFACE;
1037 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1038 if (pVisualElementImpl == null)
1040 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1041 return EGL_NO_SURFACE;
1044 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1046 pSglInfo->surface = windowSurface;
1048 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1051 return (EGLSurface)sglIndex;
1054 #endif //#if !defined(_OSP_EMUL_)
1056 result r = E_SUCCESS;
1058 if (pVisualElement == null)
1060 pVisualElement = pControl->GetVisualElement();
1062 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1064 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1065 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1067 FloatRectangle rect = pVisualElement->GetBounds();
1068 int width = int(rect.width);
1069 int height = int(rect.height);
1070 SysTryReturn(NID_GRP, width > 0 && height > 0, EGL_NO_SURFACE, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
1072 Control* pFrame = pControl;
1073 Control* pTemp = pFrame->GetParent();
1074 while(pTemp != null)
1077 pTemp = pTemp->GetParent();
1080 Tizen::Ui::Window* pWindow = dynamic_cast<Tizen::Ui::Window*> (pFrame);
1081 SysTryReturn(NID_GRP, pWindow != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window.");
1083 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1084 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1086 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1087 if (pVisualElementSurfaceImpl == null)
1089 SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1090 delete pVisualElementSurface;
1091 return EGL_NO_SURFACE;
1094 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1095 delete pVisualElementSurface;
1097 Evas* pEvas = evas_object_evas_get(pObject);
1098 pObject = evas_object_image_filled_add(pEvas);
1099 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1101 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1102 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
1104 evas_object_image_alpha_set(pObject, EINA_TRUE);
1105 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1106 evas_object_resize(pObject, widthPhysical, heightPhysical);
1107 evas_object_hide(pObject);
1109 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1110 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1111 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1113 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
1114 SysTryReturn(NID_GRP, bitmap.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1116 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1117 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1119 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmap.get(), null);
1120 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1122 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1123 pSglInfo->sglIndex = sglIndex;
1124 pSglInfo->display = dpy;
1125 pSglInfo->config = config;
1126 pSglInfo->pVisualElement = pVisualElement;
1127 pSglInfo->pWindow = pWindow;
1128 pSglInfo->pObject = pObject;
1129 pSglInfo->pVisualElementSurface = pVisualElementSurface;
1130 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1132 __evasEngineType = _GetEvasEngineType(pObject);
1134 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1135 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1137 __isDoublePixmapEnabled = true;
1141 __isDoublePixmapEnabled = false;
1144 __isDoublePixmapEnabled = false;
1147 if (__isDoublePixmapEnabled)
1149 pObject = evas_object_image_filled_add(pEvas);
1150 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1152 evas_object_image_alpha_set(pObject, EINA_TRUE);
1153 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1154 evas_object_resize(pObject, widthPhysical, heightPhysical);
1155 evas_object_hide(pObject);
1157 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1158 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1159 SysTryReturn(NID_GRP, pSglInfo->pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1161 delete pSglInfo->pBitmap;
1162 pSglInfo->pBitmap = null;
1164 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
1165 SysTryReturn(NID_GRP, bitmapSecond.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1167 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1168 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1170 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmapSecond.get(), null);
1171 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1173 _SglInfo* pSglInfoSecond = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1174 pSglInfoSecond->sglIndex = sglIndex;
1175 pSglInfoSecond->display = dpy;
1176 pSglInfoSecond->config = config;
1177 pSglInfoSecond->pVisualElement = pVisualElement;
1178 pSglInfoSecond->pWindow = pWindow;
1179 pSglInfoSecond->pObject = pObject;
1180 pSglInfoSecond->pVisualElementSurface = pVisualElementSurface;
1181 pSglInfoSecond->swapDone = true;
1182 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1184 pSglInfo->pSecondSglInfo = pSglInfoSecond;
1185 pSglInfoSecond->pSecondSglInfo = pSglInfo;
1187 pSglInfoSecond->pBitmap = bitmapSecond.release();
1188 _EvasObjectImageChange(pSglInfoSecond);
1192 pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1195 pSglInfo->pBitmap = bitmap.release();
1196 _EvasObjectImageChange(pSglInfo);
1198 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1199 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1200 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1201 _RegisterRenderCallback(pSglInfo->pObject);
1203 return (EGLSurface)pSglInfo->sglIndex;
1208 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1210 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1211 _SglIndex sglIndex = INVALID_SGL_INDEX;
1213 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1214 if (sglIndex <= INVALID_SGL_INDEX)
1216 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1217 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1219 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1222 EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1223 if (pbufferSurface == EGL_NO_SURFACE)
1225 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1230 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1232 pSglInfo->surface = pbufferSurface;
1234 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1237 return (EGLSurface)sglIndex;
1240 _OSP_LOCAL_ EGLNativePixmapType
1241 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
1245 int bitsPerPixel = 0;
1246 Display* pNativeDisplay = null;
1247 Drawable nativeWindow = 0;
1248 result r = E_FAILURE;
1249 Tizen::Graphics::BufferInfo bufferInfo;
1250 _PixmapInfo* pPixmapInfo = null;
1251 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1252 unsigned int attachments[] = { DRI2BufferFrontLeft };
1253 char* pDeviceName = null;
1254 char* pDriverName = null;
1255 DRI2Buffer* pDri2Buffer = null;
1258 int dri2BufferCount = 0;
1259 tbm_bo_handle tbmData = { null, };
1262 drm_magic_t magic = 0;
1264 _SglInfo* pSglInfo = null;
1265 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1267 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1268 if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1270 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1273 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1275 r = pBitmap->Lock(bufferInfo);
1281 width = bufferInfo.width;
1282 height = bufferInfo.height;
1283 bitsPerPixel = bufferInfo.bitsPerPixel;
1285 r = pBitmap->Unlock();
1286 if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1291 pNativeDisplay = (Display*) ecore_x_display_get();
1292 nativeWindow = DefaultRootWindow(pNativeDisplay);
1294 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1295 if (pPixmapInfo == null)
1300 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height,
1301 ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1302 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1307 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1308 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1309 XSync(pNativeDisplay, False);
1311 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1313 if (!ret || pDeviceName == null)
1318 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1321 if (pPixmapInfo->drmFd < 0)
1326 drmGetMagic(pPixmapInfo->drmFd, &magic);
1327 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1333 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1334 if (pPixmapInfo->pTbmBufMgr == null)
1339 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1340 if (pDri2Buffer == null)
1345 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1347 if (pPixmapInfo->pTbmBufferObject == null)
1352 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1353 if (tbmData.ptr == null)
1358 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1359 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1365 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1366 _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1372 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1374 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1376 pSglInfo->pEcoreImage = ecore_x_image_new(width
1378 , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1379 , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1381 if (pSglInfo->pEcoreImage == null)
1383 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1387 ecore_x_image_get(pSglInfo->pEcoreImage
1388 , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1396 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1398 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1399 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1402 ecore_x_image_free(pSglInfo->pEcoreImage);
1403 pSglInfo->pEcoreImage = null;
1404 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1406 SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1410 pSglInfo->width = width;
1411 pSglInfo->height = height;
1413 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1415 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1416 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1418 pBitmap->Lock(bufferInfo);
1419 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1422 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1425 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1426 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1427 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1431 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1435 close(pPixmapInfo->drmFd);
1439 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1441 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1449 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1450 return (EGLNativePixmapType)0;
1453 _OSP_LOCAL_ EGLNativePixmapType
1454 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
1458 int bitsPerPixel = 0;
1459 Display* pNativeDisplay = null;
1460 Drawable nativeWindow = 0;
1461 Tizen::Graphics::_BitmapImpl* pBitmapImpl = null;
1462 _PixmapInfo* pPixmapInfo = null;
1463 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1464 unsigned int attachments[] = { DRI2BufferFrontLeft };
1465 char* pDeviceName = null;
1466 char* pDriverName = null;
1467 DRI2Buffer* pDri2Buffer = null;
1470 int dri2BufferCount = 0;
1471 tbm_bo_handle tbmData = { null, };
1474 result r = E_FAILURE;
1475 drm_magic_t magic = 0;
1477 if (pBitmap == null)
1482 pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1483 if (pBitmapImpl == null)
1488 width = bufferInfo.width;
1489 height = bufferInfo.height;
1490 bitsPerPixel = bufferInfo.bitsPerPixel;
1492 if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1497 pNativeDisplay = (Display*) ecore_x_display_get();
1498 nativeWindow = DefaultRootWindow(pNativeDisplay);
1500 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1501 if (pPixmapInfo == null)
1506 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1508 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1513 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1514 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1515 XSync(pNativeDisplay, False);
1517 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1519 if (!ret || pDeviceName == null)
1524 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1526 if (pPixmapInfo->drmFd < 0)
1531 drmGetMagic(pPixmapInfo->drmFd, &magic);
1532 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1538 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1539 if (pPixmapInfo->pTbmBufMgr == null)
1544 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1545 if (pDri2Buffer == null)
1550 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1552 if (pPixmapInfo->pTbmBufferObject == null)
1557 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1558 if (tbmData.ptr == null)
1562 bufferInfo.pPixels = tbmData.ptr;
1564 bufferInfo.bitsPerPixel = 32;
1565 bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
1566 bufferInfo.pitch = bufferInfo.width * bufferInfo.bitsPerPixel / 8;
1567 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1568 r = pBitmapImpl->Construct(bufferInfo);
1574 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1575 , _PixmapSurfaceDestroyCallback, pPixmapInfo
1576 , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1577 ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1581 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1583 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1585 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1587 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1588 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1592 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1596 close(pPixmapInfo->drmFd);
1600 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1601 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1610 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1611 return (EGLNativePixmapType)0;
1615 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
1617 _SglIndex sglIndex = INVALID_SGL_INDEX;
1618 Tizen::Graphics::Bitmap* pBitmap = dynamic_cast<Tizen::Graphics::Bitmap*>((Tizen::Graphics::Bitmap*)pixmap);
1619 EGLNativePixmapType eglNativePixmap = 0;
1620 EGLSurface pixmapSurface = EGL_NO_SURFACE;
1621 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1623 if (pBitmap == null)
1628 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1629 if (sglIndex <= INVALID_SGL_INDEX)
1634 eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1635 if (eglNativePixmap == 0)
1640 pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1641 if (pixmapSurface == EGL_NO_SURFACE)
1643 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1648 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1650 pSglInfo->nativePixmap = eglNativePixmap;
1651 pSglInfo->surface = pixmapSurface;
1653 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1656 return (EGLSurface)sglIndex;
1659 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1663 SysLog(NID_GRP, "_SglCreatePixmapSurface failed!! dpy:%#x config:%#x pixmap:%#x pAttribList:%#x",
1664 (unsigned int)dpy, (unsigned int)config, (unsigned int)pixmap, (unsigned int)pAttribList);
1665 return eglCreatePixmapSurface(EGL_NO_DISPLAY, config, (EGLNativePixmapType) 0, pAttribList);
1669 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1671 EGLBoolean ret = EGL_FALSE;
1672 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1674 _SglIndex sglIndex = (_SglIndex)surface;
1675 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1677 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1679 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1680 SysLog(NID_GRP, "_SglDestroySurface failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1681 , (unsigned int)surface);
1682 return eglDestroySurface(dpy, EGL_NO_SURFACE);
1685 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1686 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1688 __pPreviousSglInfo = null;
1692 ret = eglDestroySurface(dpy, pSglInfo->surface);
1693 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1695 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1697 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1698 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1701 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1707 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1709 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1711 _SglIndex sglIndex = (_SglIndex)surface;
1712 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1714 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1716 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1717 SysLog(NID_GRP, "_SglQuerySurface failed!! dpy:%#x sglIndex:%#x attribute:%#x pValue:%#x",
1718 (unsigned int)dpy, (unsigned int)sglIndex, (unsigned int)attribute, (unsigned int)pValue);
1719 return eglQuerySurface(dpy, EGL_NO_SURFACE, attribute, pValue);
1722 EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1723 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1729 _SglBindAPI(EGLenum api)
1731 return eglBindAPI(api);
1737 return eglQueryAPI();
1743 return eglWaitClient();
1747 _SglReleaseThread(void)
1749 return eglReleaseThread();
1753 _SglCreatePbufferFromClientBuffer(
1754 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1756 _SglIndex sglIndex = INVALID_SGL_INDEX;
1757 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1759 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1760 if (sglIndex <= INVALID_SGL_INDEX)
1762 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!!"
1763 " dpy:%#x buftype:%#x buffer:%#x config:%#x pAttribList:%#x",
1764 (unsigned int)dpy, (unsigned int)buftype, (unsigned int)buffer, (unsigned int)config, (unsigned int)pAttribList);
1766 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1769 EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1770 if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1772 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1777 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1779 pSglInfo->surface = pbufferFromClientBuffer;
1781 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1784 return (EGLSurface)sglIndex;
1788 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1790 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1792 _SglIndex sglIndex = (_SglIndex)surface;
1793 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1795 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1797 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1798 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!! "
1799 "dpy:%#x surface:%#x attribute:%#x value:%#x",
1800 (unsigned int)dpy, (unsigned int)surface, (unsigned int)attribute, (unsigned int)value);
1802 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1805 EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1806 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1812 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1814 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1816 _SglIndex sglIndex = (_SglIndex)surface;
1817 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1819 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1821 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1822 SysLog(NID_GRP, "_SglBindTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1823 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1825 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1828 EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1829 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1835 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1837 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1839 _SglIndex sglIndex = (_SglIndex)surface;
1840 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1842 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1844 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1845 SysLog(NID_GRP, "_SglReleaseTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1846 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1848 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1851 EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1852 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1858 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1860 return eglSwapInterval(dpy, interval);
1864 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1866 return eglCreateContext(dpy, config, shareContext, pAttribList);
1870 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1872 return eglDestroyContext(dpy, ctx);
1876 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1878 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1880 _SglIndex sglIndexDraw = (_SglIndex)draw;
1881 _SglIndex sglIndexRead = (_SglIndex)read;
1883 EGLSurface sglInfoReadSurface;
1885 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1887 if (sglInfoRead == null)
1889 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1890 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1891 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1892 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1894 sglInfoReadSurface = sglInfoRead->surface;
1896 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer)
1898 sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1901 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1904 _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1905 if (pSglInfoDraw == null)
1907 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1908 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1909 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1910 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1913 if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1915 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1918 pSglInfoDraw->context = ctx;
1920 EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1922 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1923 if (draw != EGL_NO_SURFACE)
1925 _SaveCurrentContext(pSglInfoDraw);
1929 if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1931 eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1934 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1940 _SglGetCurrentContext()
1942 return eglGetCurrentContext();
1946 _SglGetCurrentSurface(EGLint readDraw)
1948 EGLSurface surface = EGL_NO_SURFACE;
1949 _SglIndex sglIndex = INVALID_SGL_INDEX;
1951 surface = eglGetCurrentSurface(readDraw);
1952 sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1954 if (sglIndex < INVALID_SGL_INDEX)
1956 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1957 return EGL_NO_SURFACE;
1960 return (EGLSurface)sglIndex;
1964 _SglGetCurrentDisplay(void)
1966 return eglGetCurrentDisplay();
1970 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1972 return eglQueryContext(dpy, ctx, attribute, pValue);
1982 _SglWaitNative(EGLint engine)
1984 return eglWaitNative(engine);
1987 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
1990 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1992 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1994 _SglIndex sglIndex = (_SglIndex)surface;
1995 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1997 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1999 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2000 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
2001 , (unsigned int)sglIndex);
2003 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
2006 if (pSglInfo->pBitmap != null)
2008 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2009 _SglUpdateBufferOSP(dpy, surface);
2011 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2013 if (pSglInfo->glVersion == 1)
2028 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2030 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2032 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2034 _SglInfo* pBackSglInfo = null;
2035 _SglInfo* pFrontSglInfo = null;
2037 if (pSglInfo->isBackbuffer)
2039 pBackSglInfo = pSglInfo;
2040 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2044 pBackSglInfo = pSglInfo->pSecondSglInfo;
2045 pFrontSglInfo = pSglInfo;
2047 pBackSglInfo->isBackbuffer = false;
2048 pFrontSglInfo->isBackbuffer = true;
2050 pFrontSglInfo->wasShown = false;
2051 pFrontSglInfo->swapDone = false;
2052 if (pSglInfo->pVisualElement != null)
2054 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2056 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2057 evas_object_hide(pBackSglInfo->pObject);
2059 if (pSglInfo->isFirstSwap)
2061 pBackSglInfo->isFirstSwap = false;
2062 pFrontSglInfo->isFirstSwap = false;
2065 BufferInfo bufferInfo;
2066 pFrontSglInfo->pBitmap->Lock(bufferInfo);
2067 pFrontSglInfo->pBitmap->Merge(Point(0, 0), *pBackSglInfo->pBitmap, Rectangle(0, 0, bufferInfo.width, bufferInfo.height));
2068 pFrontSglInfo->pBitmap->Unlock();
2074 if (pSglInfo->pObject != null)
2076 evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2082 if (pSglInfo->pVisualElement != null)
2084 pSglInfo->pVisualElement->SetFlushNeeded();
2089 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2094 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2095 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2102 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2104 Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2105 EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2106 return eglCopyBuffers(dpy, surface, nativePixmap);
2109 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2111 __EglMustCastToProperFunctionPointerType
2112 _SglGetProcAddress(const char* pProcName)
2114 return eglGetProcAddress(pProcName);
2118 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2120 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2121 Tizen::Graphics::BufferInfo bufferInfo;
2122 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2124 _SglIndex sglIndex = (_SglIndex)surface;
2125 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2127 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2129 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2130 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
2134 eglCopyBuffers(dpy, pSglInfo->surface, pSglInfo->nativePixmap);
2135 ecore_x_image_get(pSglInfo->pEcoreImage, (Ecore_X_Drawable)pSglInfo->nativePixmap,
2136 0, 0, 0, 0, pSglInfo->width, pSglInfo->height);
2138 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2149 }} // Tizen::Graphics